Пример #1
0
    def op_LOAD_CONST(self, value, target):
        if isinstance(value, bool):
            self.stack[target] = Constant.int(bool_type, value)
        elif isinstance(value, int):
            self.stack[target] = Constant.int(int_type, value)
        elif isinstance(value, (float, long)):
            self.stack[target] = Constant.real(float_type, value)
        elif isinstance(value, str):
            # create null terminated string
            n = 0
            content = Constant.stringz(value)

            # create a unique global constant name, keep hashing
            # until we find one
            while True:
                try:
                    name = CONSTANT_NAMING % (abs(hash(value)) ^ n)
                    break
                except LLVMException:
                    n += 1
                    pass

            globalstr = self.module.add_global_variable(content.type, name)
            globalstr.initializer = content
            globalstr.linkage = lc.LINKAGE_LINKONCE_ODR
            self.stack[target] = globalstr.bitcast(
                pointer(content.type.element))
        else:
            raise NotImplementedError
Пример #2
0
    def op_LOAD_CONST(self, value, target):
        if isinstance(value, bool):
            self.stack[target] = Constant.int(bool_type, value)
        elif isinstance(value, int):
            self.stack[target] = Constant.int(int_type, value)
        elif isinstance(value, (float, long)):
            self.stack[target] = Constant.real(float_type, value)
        elif isinstance(value, str):
            # create null terminated string
            n = 0
            content = Constant.stringz(value)

            # create a unique global constant name, keep hashing
            # until we find one
            while True:
                try:
                    name = CONSTANT_NAMING % (abs(hash(value)) ^ n)
                    break
                except LLVMException:
                    n += 1
                    pass

            globalstr = self.module.add_global_variable(content.type, name)
            globalstr.initializer = content
            globalstr.linkage = lc.LINKAGE_LINKONCE_ODR
            self.stack[target] = globalstr.bitcast(pointer(content.type.element))
        else:
            raise NotImplementedError
Пример #3
0
    def insert_string_const_addrspace(self, builder, string):
        """
        Insert a constant string in the constant addresspace and return a
        generic i8 pointer to the data.

        This function attempts to deduplicate.
        """
        lmod = builder.basic_block.function.module
        text = Constant.stringz(string)
        name = "__conststring__.%s" % string
        charty = Type.int(8)

        for gv in lmod.global_variables:
            if gv.name == name and gv.type.pointee == text.type:
                break
        else:
            gl = lmod.add_global_variable(text.type, name=name,
                                          addrspace=nvvm.ADDRSPACE_CONSTANT)
            gl.linkage = LINKAGE_INTERNAL
            gl.global_constant = True
            gl.initializer = text

            constcharptrty = Type.pointer(charty, nvvm.ADDRSPACE_CONSTANT)
            charptr = builder.bitcast(gl, constcharptrty)

        conv = nvvmutils.insert_addrspace_conv(lmod, charty,
                                               nvvm.ADDRSPACE_CONSTANT)
        return builder.call(conv, [charptr])
Пример #4
0
    def insert_string_const_addrspace(self, builder, string):
        """
        Insert a constant string in the constant addresspace and return a
        generic i8 pointer to the data.

        This function attempts to deduplicate.
        """
        lmod = builder.basic_block.function.module
        text = Constant.stringz(string)
        name = "__conststring__.%s" % string
        charty = Type.int(8)

        for gv in lmod.global_variables:
            if gv.name == name and gv.type.pointee == text.type:
                break
        else:
            gl = lmod.add_global_variable(text.type,
                                          name=name,
                                          addrspace=nvvm.ADDRSPACE_CONSTANT)
            gl.linkage = LINKAGE_INTERNAL
            gl.global_constant = True
            gl.initializer = text

            constcharptrty = Type.pointer(charty, nvvm.ADDRSPACE_CONSTANT)
            charptr = builder.bitcast(gl, constcharptrty)

        conv = nvvmutils.insert_addrspace_conv(lmod, charty,
                                               nvvm.ADDRSPACE_CONSTANT)
        return builder.call(conv, [charptr])
Пример #5
0
 def CodeGen(self):
     print >> stderr, "codegening string node"
     k = Constant.stringz(self.value)
     # TODO: memory leak???
     ptr = G_LLVM_BUILDER.alloca(k.type)
     G_LLVM_BUILDER.store(k, ptr)
     return ptr
Пример #6
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)
Пример #7
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)
Пример #8
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
Пример #9
0
 def insert_const_string(self, mod, string):
     stringtype = GENERIC_POINTER
     text = Constant.stringz(string)
     name = ".const.%s" % string
     for gv in mod.global_variables:
         if gv.name == name and gv.type.pointee == text.type:
             break
     else:
         gv = cgutils.global_constant(mod, name, text)
     return Constant.bitcast(gv, stringtype)
Пример #10
0
 def insert_const_string(self, mod, string):
     stringtype = GENERIC_POINTER
     text = Constant.stringz(string)
     name = ".const.%s" % string
     for gv in mod.global_variables:
         if gv.name == name and gv.type.pointee == text.type:
             break
     else:
         gv = cgutils.global_constant(mod, name, text)
     return Constant.bitcast(gv, stringtype)
Пример #11
0
 def const(self, val):
     if isinstance(val, (int, long)):
         return Constant.int(int_type, val)
     elif isinstance(val, float):
         return Constant.real(float_type, val)
     elif isinstance(val, bool):
         return Constant.int(bool_type, int(val))
     elif isinstance(val, str):
         return Constant.stringz(val)
     else:
         raise NotImplementedError
Пример #12
0
 def const(self, val):
     if isinstance(val, (int, long)):
         return Constant.int(int_type, val)
     elif isinstance(val, float):
         return Constant.real(double_type, val)
     elif isinstance(val, bool):
         return Constant.int(bool_type, int(val))
     elif isinstance(val, str):
         return Constant.stringz(val)
     else:
         raise NotImplementedError
Пример #13
0
 def insert_const_string(self, mod, string):
     stringtype = Type.pointer(Type.int(8))
     text = Constant.stringz(string)
     name = ".const.%s" % string
     for gv in mod.global_variables:
         if gv.name == name and gv.type.pointee == text.type:
             break
     else:
         gv = mod.add_global_variable(text.type, name=name)
         gv.global_constant = True
         gv.initializer = text
         gv.linkage = lc.LINKAGE_INTERNAL
     return Constant.bitcast(gv, stringtype)
Пример #14
0
 def atom(self, expr):
     if expr.assym in self.locl:
         return self.locl[expr.assym]
     if expr.assym in self.env.names:
         return self.env.names[expr.assym]
     if expr.isnum:
         return Constant.int(Type.int(), expr.value)
     if expr.isstring:
         s = Constant.stringz(expr.value.encode('utf-8'))
         mem = self.env.constant(s)
         return self.bb.gep(mem, [Constant.int(Type.int(), 0)]*2)
     sys.stderr.write("atom undefined {}\n".format(expr))
     sys.exit(1)
Пример #15
0
def EmitGlobalString(module, builder, g_string):
    """
    Emits a global string and returns a pointer to it.
    :param llvm.core.Module module: The current LLVM module.
    :param llvm.core.Builder builder: The current LLVM IR builder.
    :param str g_string: The string to emit.
    :rtype: llvm.core.PointerType
    """
    s = Constant.stringz(g_string)
    gs = module.add_global_variable(s.type, 'g_string')
    gs.initializer = s
    return builder.gep(gs,
                       [Constant.int(tp_int, 0), Constant.int(tp_int, 0)],
                       inbounds=True)
Пример #16
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)
Пример #17
0
    def op_LOAD_CONST(self, value, target):
        if isinstance(value, bool):
            self.stack[target] = Constant.int(bool_type, value)
        elif isinstance(value, int):
            self.stack[target] = Constant.int(int_type, value)
        elif isinstance(value, (float, int)):
            self.stack[target] = Constant.real(float_type, value)
        elif isinstance(value, str):
            content = Constant.stringz(value)
            name = CONSTANT_NAMING % self.string_count
            self.string_count += 1

            globalstr = self.module.add_global_variable(content.type, name)
            globalstr.initializer = content
            globalstr.linkage = lc.LINKAGE_LINKONCE_ODR
            self.stack[target] = globalstr.bitcast(pointer(content.type.element))
        else:
            raise NotImplementedError
Пример #18
0
def printf(builder, format_string, *values):
    str_const = Constant.stringz(format_string)
    global_str_const = get_module(builder).add_global_variable(str_const.type,
                                                               '')
    global_str_const.initializer = str_const

    idx = [Constant.int(Type.int(32), 0), Constant.int(Type.int(32), 0)]
    str_addr = global_str_const.gep(idx)

    args = []
    for v in values:
        if isinstance(v, int):
            args.append(Constant.int(Type.int(), v))
        elif isinstance(v, float):
            args.append(Constant.real(Type.double(), v))

    functype = Type.function(Type.int(32), [Type.pointer(Type.int(8))], True)
    fn = get_module(builder).add_function(functype, 'printf')
    builder.call(fn, [str_addr] + args)
Пример #19
0
def printf(builder, format_string, *values):
    str_const = Constant.stringz(format_string)
    global_str_const = get_module(builder).add_global_variable(
        str_const.type, '')
    global_str_const.initializer = str_const

    idx = [Constant.int(Type.int(32), 0), Constant.int(Type.int(32), 0)]
    str_addr = global_str_const.gep(idx)

    args = []
    for v in values:
        if isinstance(v, int):
            args.append(Constant.int(Type.int(), v))
        elif isinstance(v, float):
            args.append(Constant.real(Type.double(), v))

    functype = Type.function(Type.int(32), [Type.pointer(Type.int(8))], True)
    fn = get_module(builder).add_function(functype, 'printf')
    builder.call(fn, [str_addr] + args)
Пример #20
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)