Ejemplo n.º 1
0
 def _fill_function_table(self, node):
     for decl in node.decls:
         if not isinstance(decl, ast.Function):
             continue
         proto = decl.proto
         ret_ty = convert_to_llvm_ty(proto.ret_ty)
         args = list(map(lambda arg: convert_to_llvm_ty(arg.ty),
                         proto.args))
         func_ty = ir.FunctionType(ret_ty, args)
         func = ir.Function(self.ctx.module, func_ty, name=proto.name)
         self.ctx.function_table.set(proto.name, func)
Ejemplo n.º 2
0
 def _cast_float_to_int(self, value, from_ty, to_ty):
     """Cast float value to signed/unsigned integer value."""
     builder = self.ctx.builder
     ty = convert_to_llvm_ty(to_ty)
     if to_ty in BuiltinTypes.SIGNED_INTS:
         return builder.fptosi(value, ty, name='fptosi')
     return builder.fptoui(value, ty, name='fptoui')
Ejemplo n.º 3
0
 def _cast_int_to_float(self, value, from_ty, to_ty):
     """Cast signed/unsigned integer value to floating value."""
     builder = self.ctx.builder
     ty = convert_to_llvm_ty(to_ty)
     if from_ty in BuiltinTypes.SIGNED_INTS:
         return builder.sitofp(value, ty, name='sitofp')
     return builder.uitofp(value, ty, name='uitofp')
Ejemplo n.º 4
0
 def visit_Var(self, node):
     builder = self.ctx.builder
     ty = convert_to_llvm_ty(node.ty)
     initial_value = self.expr_codegen.visit(node.initial_value)
     ptr = builder.alloca(ty, name=node.name)
     builder.store(initial_value, ptr)
     self.ctx.symbol_table.set(node.name, ptr)
Ejemplo n.º 5
0
 def visit_StringConstant(self, node):
     ctx = self.ctx
     buf = bytearray((node.value + '\00').encode('ascii'))
     value = ir.Constant(ir.ArrayType(ir.IntType(8), len(buf)), buf)
     const = _make_global_constant(ctx.module, value, 'str')
     ty = convert_to_llvm_ty(BuiltinTypes.STR)
     result = ctx.builder.bitcast(const, ty, name='res')
     return result
Ejemplo n.º 6
0
 def _cast_float_to_float(self, value, from_ty, to_ty):
     """Truncate or extend a float value."""
     from_nbits = NBITS[from_ty]
     to_nbits = NBITS[to_ty]
     if from_nbits == to_nbits:
         return value
     builder = self.ctx.builder
     ty = convert_to_llvm_ty(to_ty)
     if from_nbits > to_nbits:
         return builder.fptrunc(value, ty, name='trunc')
     return builder.fpext(value, ty, name='ext')
Ejemplo n.º 7
0
 def _cast_int_to_int(self, value, from_ty, to_ty):
     """Truncate or extend an integer value."""
     from_nbits = NBITS[from_ty]
     to_nbits = NBITS[to_ty]
     if to_nbits == from_nbits:
         return value
     builder = self.ctx.builder
     ty = convert_to_llvm_ty(to_ty)
     if from_nbits > to_nbits:
         return builder.trunc(value, ty, name='trunc')
     if from_ty in BuiltinTypes.SIGNED_INTS:
         return builder.sext(value, ty, name='ext')
     return builder.zext(value, ty, name='ext')
Ejemplo n.º 8
0
 def visit_BooleanConstant(self, node):
     ty = convert_to_llvm_ty(BuiltinTypes.BOOL)
     result = ir.Constant(ty, int(node.value))
     return result
Ejemplo n.º 9
0
 def visit_FloatConstant(self, node):
     ty = convert_to_llvm_ty(BuiltinTypes.F32)
     result = ir.Constant(ty, node.value)
     return result