• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- NaClBitcodeDecoders.cpp --------------------------------------------===//
2 //     Internal implementation of decoder functions for PNaCl Bitcode files.
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h"
12 
13 namespace llvm {
14 namespace naclbitc {
15 
DecodeCastOpcode(uint64_t NaClOpcode,Instruction::CastOps & LLVMOpcode)16 bool DecodeCastOpcode(uint64_t NaClOpcode,
17                       Instruction::CastOps &LLVMOpcode) {
18   switch (NaClOpcode) {
19   default:
20     LLVMOpcode = Instruction::BitCast;
21     return false;
22   case naclbitc::CAST_TRUNC:
23     LLVMOpcode = Instruction::Trunc;
24     return true;
25   case naclbitc::CAST_ZEXT:
26     LLVMOpcode = Instruction::ZExt;
27     return true;
28   case naclbitc::CAST_SEXT:
29     LLVMOpcode = Instruction::SExt;
30     return true;
31   case naclbitc::CAST_FPTOUI:
32     LLVMOpcode = Instruction::FPToUI;
33     return true;
34   case naclbitc::CAST_FPTOSI:
35     LLVMOpcode = Instruction::FPToSI;
36     return true;
37   case naclbitc::CAST_UITOFP:
38     LLVMOpcode = Instruction::UIToFP;
39     return true;
40   case naclbitc::CAST_SITOFP:
41     LLVMOpcode = Instruction::SIToFP;
42     return true;
43   case naclbitc::CAST_FPTRUNC:
44     LLVMOpcode = Instruction::FPTrunc;
45     return true;
46   case naclbitc::CAST_FPEXT:
47     LLVMOpcode = Instruction::FPExt;
48     return true;
49   case naclbitc::CAST_BITCAST:
50     LLVMOpcode = Instruction::BitCast;
51     return true;
52   }
53 }
54 
DecodeLinkage(uint64_t NaClLinkage,GlobalValue::LinkageTypes & LLVMLinkage)55 bool DecodeLinkage(uint64_t NaClLinkage,
56                    GlobalValue::LinkageTypes &LLVMLinkage) {
57   switch (NaClLinkage) {
58   default:
59     LLVMLinkage = GlobalValue::InternalLinkage;
60     return false;
61   case naclbitc::LINKAGE_EXTERNAL:
62     LLVMLinkage = GlobalValue::ExternalLinkage;
63     return true;
64   case naclbitc::LINKAGE_INTERNAL:
65     LLVMLinkage = GlobalValue::InternalLinkage;
66     return true;
67   }
68 }
69 
DecodeBinaryOpcode(uint64_t NaClOpcode,Type * Ty,Instruction::BinaryOps & LLVMOpcode)70 bool DecodeBinaryOpcode(uint64_t NaClOpcode, Type *Ty,
71                         Instruction::BinaryOps &LLVMOpcode) {
72   switch (NaClOpcode) {
73   default:
74     LLVMOpcode = Instruction::Add;
75     return false;
76   case naclbitc::BINOP_ADD:
77     LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
78     return true;
79   case naclbitc::BINOP_SUB:
80     LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
81     return true;
82   case naclbitc::BINOP_MUL:
83     LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
84     return true;
85   case naclbitc::BINOP_UDIV:
86     LLVMOpcode = Instruction::UDiv;
87     return true;
88   case naclbitc::BINOP_SDIV:
89     LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
90     return true;
91   case naclbitc::BINOP_UREM:
92     LLVMOpcode = Instruction::URem;
93     return true;
94   case naclbitc::BINOP_SREM:
95     LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
96     return true;
97   case naclbitc::BINOP_SHL:
98     LLVMOpcode = Instruction::Shl;
99     return true;
100   case naclbitc::BINOP_LSHR:
101     LLVMOpcode = Instruction::LShr;
102     return true;
103   case naclbitc::BINOP_ASHR:
104     LLVMOpcode = Instruction::AShr;
105     return true;
106   case naclbitc::BINOP_AND:
107     LLVMOpcode = Instruction::And;
108     return true;
109   case naclbitc::BINOP_OR:
110     LLVMOpcode = Instruction::Or;
111     return true;
112   case naclbitc::BINOP_XOR:
113     LLVMOpcode = Instruction::Xor;
114     return true;
115   }
116 }
117 
DecodeCallingConv(uint64_t NaClCallingConv,CallingConv::ID & LLVMCallingConv)118 bool DecodeCallingConv(uint64_t NaClCallingConv,
119                        CallingConv::ID &LLVMCallingConv) {
120   switch (NaClCallingConv) {
121   default:
122     LLVMCallingConv = CallingConv::C;
123     return false;
124   case naclbitc::C_CallingConv:
125     LLVMCallingConv = CallingConv::C;
126     return true;
127   }
128 }
129 
DecodeFcmpPredicate(uint64_t NaClPredicate,CmpInst::Predicate & LLVMPredicate)130 bool DecodeFcmpPredicate(uint64_t NaClPredicate,
131                          CmpInst::Predicate &LLVMPredicate) {
132   switch (NaClPredicate) {
133   default:
134     LLVMPredicate = CmpInst::FCMP_FALSE;
135     return false;
136   case naclbitc::FCMP_FALSE:
137     LLVMPredicate = CmpInst::FCMP_FALSE;
138     return true;
139   case naclbitc::FCMP_OEQ:
140     LLVMPredicate = CmpInst::FCMP_OEQ;
141     return true;
142   case naclbitc::FCMP_OGT:
143     LLVMPredicate = CmpInst::FCMP_OGT;
144     return true;
145   case naclbitc::FCMP_OGE:
146     LLVMPredicate = CmpInst::FCMP_OGE;
147     return true;
148   case naclbitc::FCMP_OLT:
149     LLVMPredicate = CmpInst::FCMP_OLT;
150     return true;
151   case naclbitc::FCMP_OLE:
152     LLVMPredicate = CmpInst::FCMP_OLE;
153     return true;
154   case naclbitc::FCMP_ONE:
155     LLVMPredicate = CmpInst::FCMP_ONE;
156     return true;
157   case naclbitc::FCMP_ORD:
158     LLVMPredicate = CmpInst::FCMP_ORD;
159     return true;
160   case naclbitc::FCMP_UNO:
161     LLVMPredicate = CmpInst::FCMP_UNO;
162     return true;
163   case naclbitc::FCMP_UEQ:
164     LLVMPredicate = CmpInst::FCMP_UEQ;
165     return true;
166   case naclbitc::FCMP_UGT:
167     LLVMPredicate = CmpInst::FCMP_UGT;
168     return true;
169   case naclbitc::FCMP_UGE:
170     LLVMPredicate = CmpInst::FCMP_UGE;
171     return true;
172   case naclbitc::FCMP_ULT:
173     LLVMPredicate = CmpInst::FCMP_ULT;
174     return true;
175   case naclbitc::FCMP_ULE:
176     LLVMPredicate = CmpInst::FCMP_ULE;
177     return true;
178   case naclbitc::FCMP_UNE:
179     LLVMPredicate = CmpInst::FCMP_UNE;
180     return true;
181   case naclbitc::FCMP_TRUE:
182     LLVMPredicate = CmpInst::FCMP_TRUE;
183     return true;
184   }
185 }
186 
DecodeIcmpPredicate(uint64_t NaClPredicate,CmpInst::Predicate & LLVMPredicate)187 bool DecodeIcmpPredicate(uint64_t NaClPredicate,
188                          CmpInst::Predicate &LLVMPredicate) {
189   switch (NaClPredicate) {
190   default:
191     LLVMPredicate = CmpInst::ICMP_EQ;
192     return false;
193   case naclbitc::ICMP_EQ:
194     LLVMPredicate = CmpInst::ICMP_EQ;
195     return true;
196   case naclbitc::ICMP_NE:
197     LLVMPredicate = CmpInst::ICMP_NE;
198     return true;
199   case naclbitc::ICMP_UGT:
200     LLVMPredicate = CmpInst::ICMP_UGT;
201     return true;
202   case naclbitc::ICMP_UGE:
203     LLVMPredicate = CmpInst::ICMP_UGE;
204     return true;
205   case naclbitc::ICMP_ULT:
206     LLVMPredicate = CmpInst::ICMP_ULT;
207     return true;
208   case naclbitc::ICMP_ULE:
209     LLVMPredicate = CmpInst::ICMP_ULE;
210     return true;
211   case naclbitc::ICMP_SGT:
212     LLVMPredicate = CmpInst::ICMP_SGT;
213     return true;
214   case naclbitc::ICMP_SGE:
215     LLVMPredicate = CmpInst::ICMP_SGE;
216     return true;
217   case naclbitc::ICMP_SLT:
218     LLVMPredicate = CmpInst::ICMP_SLT;
219     return true;
220   case naclbitc::ICMP_SLE:
221     LLVMPredicate = CmpInst::ICMP_SLE;
222     return true;
223   }
224 }
225 
226 
227 }
228 }
229