Exemple #1
0
    def code_gen(self):

        if self.context.parent_context is None:
            if self.var_name_token.word in self.context.type_table:
                raise cmexception.RedefineException(self.var_name_token,
                                                    'Global Variable')
            else:
                t = Helper.get_type(self.typo.word)
                gv = GlobalVariable.new(g_llvm_module, t,
                                        self.var_name_token.word)
                self.context.type_table[self.var_name_token.word] = t
                self.context.value_table[self.var_name_token.word] = gv
                if self.typo.word == 'int':
                    gv.initializer = Constant.int(Type.int(32), 0)
                elif self.typo.word == 'double':
                    gv.initializer = Constant.real(Type.double(), 0)
                elif self.typo.word == 'char':
                    gv.initializer = Constant.int(Type.int(8), 0)
                else:
                    gv.initializer = Constant.stringz("")
        else:
            if not self.var_name_token.word in self.context.type_table:
                t = Helper.get_type(self.typo.word)
                var_address = g_llvm_builder.alloca(
                    t, name=self.var_name_token.word)
                self.context.type_table[self.var_name_token.word] = t
                self.context.value_table[
                    self.var_name_token.word] = var_address
            else:
                raise cmexception.RedefineException(self.var_name_token)
Exemple #2
0
    def code_gen(self):
        if self.context.parent_context is None:
            if self.var_name_token.word in self.context.type_table:
                raise cmexception.RedefineException(self.var_name_token, 'Global Variable')
            else:
                t = Helper.get_type(self.typo.word)
                expr = self.expr_node.code_gen()
                if expr in g_llvm_module.global_variables:
                    expr.name = self.var_name_token.word
                    self.context.value_table[self.var_name_token.word] = expr
                else:
                    gv = GlobalVariable.new(g_llvm_module, t, self.var_name_token.word)
                    gv.initializer = expr
                    self.context.value_table[self.var_name_token.word] = gv
                self.context.type_table[self.var_name_token.word] = t

        else:
            if not self.var_name_token.word in self.context.type_table:
                t = Helper.get_type(self.typo.word)
                var_address = g_llvm_builder.alloca(t, name=self.var_name_token.word)
                self.context.type_table[self.var_name_token.word] = t
                self.context.value_table[self.var_name_token.word] = var_address
                expr = self.expr_node.code_gen()
                if expr in g_llvm_module.global_variables:
                    expr = g_llvm_builder.gep(expr, [Constant.int(Type.int(32), 0), Constant.int(Type.int(32), 0)])
                casted_expr = Helper.auto_cast(g_llvm_builder, expr, t)
                if casted_expr:
                    g_llvm_builder.store(casted_expr, var_address)
                else:
                    raise cmexception.CastException(self.var_name_token, t, expr.type)
            else:
                raise cmexception.RedefineException(self.var_name_token)
Exemple #3
0
def str_const(mod, s, n):
  c = Constant.stringz(s)
  v = GlobalVariable.new(mod, c.type, n)
  v.initializer = c
  v.linkage = llvm.core.LINKAGE_INTERNAL
  v.global_constant = True
  return v
Exemple #4
0
 def code_gen(self):
     if self.context.parent_context is None:
         if self.array_name_token.word in self.context.type_table:
             raise cmexception.RedefineException(self.array_name_token)
         else:
             t = Helper.get_array_type(self.typo.word, int(self.length.word))
             gv = GlobalVariable.new(g_llvm_module, t, self.array_name_token.word)
             initials = [i.code_gen() for i in self.initial_value if True]
             constant_array = ConstantArray.array(Helper.get_type(self.typo.word), initials)
             gv.initializer = constant_array
             self.context.type_table[self.array_name_token.word] = t
             self.context.value_table[self.array_name_token.word] = gv
     else:
         if self.array_name_token.word in self.context.type_table:
             raise cmexception.RedefineException(self.array_name_token)
         else:
             t = Helper.get_array_type(self.typo.word, int(self.length.word))
             array_address = g_llvm_builder.alloca(t, name=self.array_name_token.word)
             inx = 0
             for i in self.initial_value:
                 value = i.code_gen()
                 if value in g_llvm_module.global_variables:
                     string_value_ptr = g_llvm_builder.gep(
                         value, [Constant.int(Type.int(32), 0), Constant.int(Type.int(32), 0)])
                     var_address = g_llvm_builder.gep(
                         array_address, [Constant.int(Type.int(32), 0), Constant.int(Type.int(32), inx)])
                     g_llvm_builder.store(string_value_ptr, var_address)
                 else:
                     var_address = g_llvm_builder.gep(
                         array_address, [Constant.int(Type.int(32), 0), Constant.int(Type.int(32), inx)])
                     g_llvm_builder.store(value, var_address)
                 inx += 1
             self.context.type_table[self.array_name_token.word] = t
             self.context.value_table[self.array_name_token.word] = array_address
Exemple #5
0
    def code_gen(self):

        if self.context.parent_context is None:
            if self.var_name_token.word in self.context.type_table:
                raise cmexception.RedefineException(self.var_name_token, 'Global Variable')
            else:
                t = Helper.get_type(self.typo.word)
                gv = GlobalVariable.new(g_llvm_module, t, self.var_name_token.word)
                self.context.type_table[self.var_name_token.word] = t
                self.context.value_table[self.var_name_token.word] = gv
                if self.typo.word == 'int':
                    gv.initializer = Constant.int(Type.int(32), 0)
                elif self.typo.word == 'double':
                    gv.initializer = Constant.real(Type.double(), 0)
                elif self.typo.word == 'char':
                    gv.initializer = Constant.int(Type.int(8), 0)
                else:
                    gv.initializer = Constant.stringz("")
        else:
            if not self.var_name_token.word in self.context.type_table:
                t = Helper.get_type(self.typo.word)
                var_address = g_llvm_builder.alloca(t, name=self.var_name_token.word)
                self.context.type_table[self.var_name_token.word] = t
                self.context.value_table[self.var_name_token.word] = var_address
            else:
                raise cmexception.RedefineException(self.var_name_token)
Exemple #6
0
 def code_gen(self):
     if self.context.parent_context is None:
         if self.array_name_token.word in self.context.type_table:
             raise cmexception.RedefineException(self.array_name_token)
         else:
             t = Helper.get_array_type(self.typo.word,
                                       int(self.length.word))
             gv = GlobalVariable.new(g_llvm_module, t,
                                     self.array_name_token.word)
             initials = [i.code_gen() for i in self.initial_value if True]
             constant_array = ConstantArray.array(
                 Helper.get_type(self.typo.word), initials)
             gv.initializer = constant_array
             self.context.type_table[self.array_name_token.word] = t
             self.context.value_table[self.array_name_token.word] = gv
     else:
         if self.array_name_token.word in self.context.type_table:
             raise cmexception.RedefineException(self.array_name_token)
         else:
             t = Helper.get_array_type(self.typo.word,
                                       int(self.length.word))
             array_address = g_llvm_builder.alloca(
                 t, name=self.array_name_token.word)
             inx = 0
             for i in self.initial_value:
                 value = i.code_gen()
                 if value in g_llvm_module.global_variables:
                     string_value_ptr = g_llvm_builder.gep(
                         value, [
                             Constant.int(Type.int(32), 0),
                             Constant.int(Type.int(32), 0)
                         ])
                     var_address = g_llvm_builder.gep(
                         array_address, [
                             Constant.int(Type.int(32), 0),
                             Constant.int(Type.int(32), inx)
                         ])
                     g_llvm_builder.store(string_value_ptr, var_address)
                 else:
                     var_address = g_llvm_builder.gep(
                         array_address, [
                             Constant.int(Type.int(32), 0),
                             Constant.int(Type.int(32), inx)
                         ])
                     g_llvm_builder.store(value, var_address)
                 inx += 1
             self.context.type_table[self.array_name_token.word] = t
             self.context.value_table[
                 self.array_name_token.word] = array_address
Exemple #7
0
 def code_gen(self):
     if self.constant_token.lexme_type == LexmeType.Integer:
         return Constant.int(Helper.get_type(self.constant_token.lexme_type), self.constant_token.word)
     elif self.constant_token.lexme_type == LexmeType.Double:
         return Constant.real(Helper.get_type(self.constant_token.lexme_type), self.constant_token.word)
     elif self.constant_token.lexme_type == LexmeType.String:
         s = self.constant_token.word.strip('"')
         global constant_string_num
         global_string = GlobalVariable.new(g_llvm_module, Type.array(Type.int(8), len(s) + 1),
                                            ".str%d" % constant_string_num)
         constant_string_num += 1
         global_string.initializer = Constant.stringz(s)
         return global_string
     elif self.constant_token.lexme_type == LexmeType.Char:
         ascii = ord(self.constant_token.word.strip("'"))
         return Constant.int(Helper.get_type(self.constant_token.lexme_type), ascii)
Exemple #8
0
 def code_gen(self):
     length = int(self.length_token.word)
     if length < 0 or length > (1 << 31) - 1:
         raise cmexception.SyntaxException(self.length_token, ['<a integer between 0 and 2e31-1>'])
     if self.context.parent_context is None:
         if self.array_name_token.word in self.context.type_table:
             raise cmexception.RedefineException(self.array_name_token, 'Global Variable')
         else:
             t = Helper.get_array_type(self.typo.word, length)
             gv = GlobalVariable.new(g_llvm_module, t, self.array_name_token.word)
             self.context.type_table[self.array_name_token.word] = t
             self.context.value_table[self.array_name_token.word] = gv
     else:
         if not self.array_name_token.word in self.context.type_table:
             t = Helper.get_array_type(self.typo.word, length)
             var_addr = g_llvm_builder.alloca(t, name=self.array_name_token.word)
             self.context.type_table[self.array_name_token.word] = t
             self.context.value_table[self.array_name_token.word] = var_addr
         else:
             raise cmexception.RedefineException(self.array_name_token)
Exemple #9
0
    def code_gen(self):
        if self.context.parent_context is None:
            if self.var_name_token.word in self.context.type_table:
                raise cmexception.RedefineException(self.var_name_token,
                                                    'Global Variable')
            else:
                t = Helper.get_type(self.typo.word)
                expr = self.expr_node.code_gen()
                if expr in g_llvm_module.global_variables:
                    expr.name = self.var_name_token.word
                    self.context.value_table[self.var_name_token.word] = expr
                else:
                    gv = GlobalVariable.new(g_llvm_module, t,
                                            self.var_name_token.word)
                    gv.initializer = expr
                    self.context.value_table[self.var_name_token.word] = gv
                self.context.type_table[self.var_name_token.word] = t

        else:
            if not self.var_name_token.word in self.context.type_table:
                t = Helper.get_type(self.typo.word)
                var_address = g_llvm_builder.alloca(
                    t, name=self.var_name_token.word)
                self.context.type_table[self.var_name_token.word] = t
                self.context.value_table[
                    self.var_name_token.word] = var_address
                expr = self.expr_node.code_gen()
                if expr in g_llvm_module.global_variables:
                    expr = g_llvm_builder.gep(expr, [
                        Constant.int(Type.int(32), 0),
                        Constant.int(Type.int(32), 0)
                    ])
                casted_expr = Helper.auto_cast(g_llvm_builder, expr, t)
                if casted_expr:
                    g_llvm_builder.store(casted_expr, var_address)
                else:
                    raise cmexception.CastException(self.var_name_token, t,
                                                    expr.type)
            else:
                raise cmexception.RedefineException(self.var_name_token)
Exemple #10
0
 def code_gen(self):
     if self.constant_token.lexme_type == LexmeType.Integer:
         return Constant.int(
             Helper.get_type(self.constant_token.lexme_type),
             self.constant_token.word)
     elif self.constant_token.lexme_type == LexmeType.Double:
         return Constant.real(
             Helper.get_type(self.constant_token.lexme_type),
             self.constant_token.word)
     elif self.constant_token.lexme_type == LexmeType.String:
         s = self.constant_token.word.strip('"')
         global constant_string_num
         global_string = GlobalVariable.new(
             g_llvm_module, Type.array(Type.int(8),
                                       len(s) + 1),
             ".str%d" % constant_string_num)
         constant_string_num += 1
         global_string.initializer = Constant.stringz(s)
         return global_string
     elif self.constant_token.lexme_type == LexmeType.Char:
         ascii = ord(self.constant_token.word.strip("'"))
         return Constant.int(
             Helper.get_type(self.constant_token.lexme_type), ascii)
Exemple #11
0
 def code_gen(self):
     length = int(self.length_token.word)
     if length < 0 or length > (1 << 31) - 1:
         raise cmexception.SyntaxException(
             self.length_token, ['<a integer between 0 and 2e31-1>'])
     if self.context.parent_context is None:
         if self.array_name_token.word in self.context.type_table:
             raise cmexception.RedefineException(self.array_name_token,
                                                 'Global Variable')
         else:
             t = Helper.get_array_type(self.typo.word, length)
             gv = GlobalVariable.new(g_llvm_module, t,
                                     self.array_name_token.word)
             self.context.type_table[self.array_name_token.word] = t
             self.context.value_table[self.array_name_token.word] = gv
     else:
         if not self.array_name_token.word in self.context.type_table:
             t = Helper.get_array_type(self.typo.word, length)
             var_addr = g_llvm_builder.alloca(
                 t, name=self.array_name_token.word)
             self.context.type_table[self.array_name_token.word] = t
             self.context.value_table[self.array_name_token.word] = var_addr
         else:
             raise cmexception.RedefineException(self.array_name_token)
Exemple #12
0
 def op_GLOBAL(self, ty, name):
     lty = typemap[ty.name]
     var = GlobalVariable.new(self.module, lty, name)
     var.initializer = self.const(ty.zero)
     self.globals[name] = var
Exemple #13
0
    def test_objcache(self):
        # Testing module aliasing
        m1 = Module.new("a")
        t = Type.int()
        ft = Type.function(t, [t])
        f1 = m1.add_function(ft, "func")
        m2 = f1.module
        self.assert_(m1 is m2)

        # Testing global vairable aliasing 1
        gv1 = GlobalVariable.new(m1, t, "gv")
        gv2 = GlobalVariable.get(m1, "gv")
        self.assert_(gv1 is gv2)

        # Testing global vairable aliasing 2
        gv3 = m1.global_variables[0]
        self.assert_(gv1 is gv3)

        # Testing global vairable aliasing 3
        gv2 = None
        gv3 = None

        gv1.delete()

        gv4 = GlobalVariable.new(m1, t, "gv")

        self.assert_(gv1 is not gv4)

        # Testing function aliasing 1
        b1 = f1.append_basic_block("entry")
        f2 = b1.function
        self.assert_(f1 is f2)

        # Testing function aliasing 2
        f3 = m1.get_function_named("func")
        self.assert_(f1 is f3)

        # Testing function aliasing 3
        f4 = Function.get_or_insert(m1, ft, "func")
        self.assert_(f1 is f4)

        # Testing function aliasing 4
        f5 = Function.get(m1, "func")
        self.assert_(f1 is f5)

        # Testing function aliasing 5
        f6 = m1.get_or_insert_function(ft, "func")
        self.assert_(f1 is f6)

        # Testing function aliasing 6
        f7 = m1.functions[0]
        self.assert_(f1 is f7)

        # Testing argument aliasing
        a1 = f1.args[0]
        a2 = f1.args[0]
        self.assert_(a1 is a2)

        # Testing basic block aliasing 1
        b2 = f1.basic_blocks[0]
        self.assert_(b1 is b2)

        # Testing basic block aliasing 2
        b3 = f1.entry_basic_block
        self.assert_(b1 is b3)

        # Testing basic block aliasing 3
        b31 = f1.entry_basic_block
        self.assert_(b1 is b31)

        # Testing basic block aliasing 4
        bldr = Builder.new(b1)
        b4 = bldr.basic_block
        self.assert_(b1 is b4)

        # Testing basic block aliasing 5
        i1 = bldr.ret_void()
        b5 = i1.basic_block
        self.assert_(b1 is b5)

        # Testing instruction aliasing 1
        i2 = b5.instructions[0]
        self.assert_(i1 is i2)

        # phi node
        phi = bldr.phi(t)
        phi.add_incoming(f1.args[0], b1)
        v2 = phi.get_incoming_value(0)
        b6 = phi.get_incoming_block(0)

        # Testing PHI / basic block aliasing 5
        self.assert_(b1 is b6)

        # Testing PHI / value aliasing
        self.assert_(f1.args[0] is v2)
Exemple #14
0
def buildMemory(module, maxmem):
    '''Build memory array'''
    memoryType = Type.array(Type.int(), maxmem)
    memory = GlobalVariable.new(module, memoryType, 'memory')
    memory.initializer = array([num(0)] * maxmem)
    return memory
Exemple #15
0
 def emit_global_float(self, name):
     var = GlobalVariable.new(self.module, float_type, name)
     var.initializer = Constant.real(float_type, 0)
     self.vars[name] = var
Exemple #16
0
 def emit_global_bool(self, name):
     var = GlobalVariable.new(self.module, bool_type, name)
     var.initializer = Constant.int(bool_type, 0)
     self.vars[name] = var
Exemple #17
0
 def emit_global_int(self, name):
     var = GlobalVariable.new(self.module, int_type, name)
     var.initializer = Constant.int(int_type, 0)
     self.globals[name] = var
Exemple #18
0
    def test_objcache(self):
        # Testing module aliasing
        m1 = Module.new('a')
        t = Type.int()
        ft = Type.function(t, [t])
        f1 = m1.add_function(ft, "func")
        m2 = f1.module
        self.assert_(m1 is m2)

        # Testing global vairable aliasing 1
        gv1 = GlobalVariable.new(m1, t, "gv")
        gv2 = GlobalVariable.get(m1, "gv")
        self.assert_(gv1 is gv2)

        # Testing global vairable aliasing 2
        gv3 = m1.global_variables[0]
        self.assert_(gv1 is gv3)

        # Testing global vairable aliasing 3
        gv2 = None
        gv3 = None

        gv1.delete()
        gv4 = GlobalVariable.new(m1, t, "gv")

        self.assert_(gv1 is not gv4)

        # Testing function aliasing 1
        b1 = f1.append_basic_block('entry')
        f2 = b1.function
        self.assert_(f1 is f2)

        # Testing function aliasing 2
        f3 = m1.get_function_named("func")
        self.assert_(f1 is f3)

        # Testing function aliasing 3
        f4 = Function.get_or_insert(m1, ft, "func")
        self.assert_(f1 is f4)

        # Testing function aliasing 4
        f5 = Function.get(m1, "func")
        self.assert_(f1 is f5)

        # Testing function aliasing 5
        f6 = m1.get_or_insert_function(ft, "func")
        self.assert_(f1 is f6)

        # Testing function aliasing 6
        f7 = m1.functions[0]
        self.assert_(f1 is f7)

        # Testing argument aliasing
        a1 = f1.args[0]
        a2 = f1.args[0]
        self.assert_(a1 is a2)

        # Testing basic block aliasing 1
        b2 = f1.basic_blocks[0]
        self.assert_(b1 is b2)

        # Testing basic block aliasing 2
        b3 = f1.get_entry_basic_block()
        self.assert_(b1 is b3)

        # Testing basic block aliasing 3
        b31 = f1.entry_basic_block
        self.assert_(b1 is b31)

        # Testing basic block aliasing 4
        bldr = Builder.new(b1)
        b4 = bldr.basic_block
        self.assert_(b1 is b4)

        # Testing basic block aliasing 5
        i1 = bldr.ret_void()
        b5 = i1.basic_block
        self.assert_(b1 is b5)

        # Testing instruction aliasing 1
        i2 = b5.instructions[0]
        self.assert_(i1 is i2)

        # phi node
        phi = bldr.phi(t)
        phi.add_incoming(f1.args[0], b1)
        v2 = phi.get_incoming_value(0)
        b6 = phi.get_incoming_block(0)

        # Testing PHI / basic block aliasing 5
        self.assert_(b1 is b6)

        # Testing PHI / value aliasing
        self.assert_(f1.args[0] is v2)
Exemple #19
0
 def emit_global_string(self, name):
     var = GlobalVariable.new(self.module, string_type, name)
     var.initializer = Constant.null(string_type)
     self.vars[name] = var
Exemple #20
0
 def op_GLOBAL(self, ty, name):
     lty = typemap[ty.name]
     var = GlobalVariable.new(self.module, lty, name)
     var.initializer = self.const(ty.zero)
     self.globals[name] = var
Exemple #21
0
print_float_func = Function.new(mod, Type.function(Type.void(),
                                [Type.double()], False), "print_float")
engine.run_function(print_float_func, [result])


main_func = Function.new(mod, Type.function(Type.void(), [], False), "main")
block = main_func.append_basic_block("entry")
builder = Builder.new(block)
x = Constant.real(Type.double(), 3.0)
y = Constant.real(Type.double(), 4.0)
d = builder.call(distance_func, [x,y])
builder.call(print_float_func, [d])
builder.ret_void()
engine.run_function(main_func, [])

x_var = GlobalVariable.new(mod, Type.double(), "x")
x_var.initializer = Constant.real(Type.double(), 0.0)

incr_func = Function.new(mod, Type.function(Type.void(), [], False), "incr")
block = incr_func.append_basic_block("entry")
builder = Builder.new(block)
tmp1 = builder.load(x_var)
tmp2 = builder.fadd(tmp1, Constant.real(Type.double(),1.0))
builder.call(print_float_func, [tmp2])
builder.store(tmp2, x_var)
builder.ret_void()
print(mod)

for i in range(10):
    r = engine.run_function(incr_func, [])