Ejemplo n.º 1
0
def convert_number_to_constant(value: str) -> Constant:
    value.replace("_", "")
    value_lowered = value.lower()

    if "." in value or "e" in value:
        if value.endswith("f"):
            return ir.FloatType()(float(value.strip("f")))
        if value.endswith("h"):
            return ir.HalfType()(float(value.strip("h")))
        return ir.DoubleType()(float(value.strip("d")))

    if value.startswith("0x"):
        return Int32(int(value, 16))

    if value.startswith("0b"):
        return Int32(int(value, 2))

    if value_lowered.endswith("b"):
        return LLVMUIntType(8)(int(value_lowered.strip("b")))

    if value_lowered.endswith("ul"):
        return LLVMUIntType(64)(int(value_lowered.strip("ul")))

    if value_lowered.endswith("u"):
        return LLVMUIntType(32)(int(value_lowered.strip("u")))

    if value_lowered.endswith("l"):
        return ir.IntType(64)(int(value_lowered.strip("l")))

    return Int32(int(value))
Ejemplo n.º 2
0
    def ll_type(self):
        if self.is_int:
            return ll.IntType(self.precision)
        elif self.is_bool:
            return ll.IntType(8)

        if self.precision >= 64:
            return ll.DoubleType()
        elif self.precision >= 32:
            return ll.FloatType()
        elif self.precision >= 16:
            return ll.HalfType()
Ejemplo n.º 3
0
def map_type_to_llvm(rial_type: str) -> Optional[Type]:
    rial_type = map_shortcut_to_type(rial_type)

    if rial_type == "Int32":
        # 32bit integer
        return ir.IntType(32)

    if rial_type == "UInt32":
        return LLVMUIntType(32)

    if rial_type == "Int64":
        return ir.IntType(64)

    if rial_type == "UInt64":
        return LLVMUIntType(64)

    if rial_type == "Boolean":
        # 1 bit
        return ir.IntType(1)

    if rial_type == "CString":
        # Char pointer
        return ir.IntType(8).as_pointer()

    if rial_type == "void":
        # Void
        return ir.VoidType()

    if rial_type == "Float32":
        return ir.FloatType()

    if rial_type == "Double64":
        return ir.DoubleType()

    if rial_type == "Byte":
        return LLVMUIntType(8)

    if rial_type == "Char":
        return ir.IntType(8)

    if rial_type == "Half":
        return ir.HalfType()

    # Variable integer
    match = re.match(r"^Int([0-9]+)$", rial_type)

    if match is not None:
        count = match.group(1)

        return ir.IntType(int(count))

    return None
Ejemplo n.º 4
0
 def half():
     return ir.HalfType()
Ejemplo n.º 5
0
 def gen_half(self, number: float):
     return ir.Constant(ir.HalfType(), number)
Ejemplo n.º 6
0
    'CString': 'Char[]'
}

TYPE_TO_LLVM = {
    'Int32': ir.IntType(32),
    'UInt32': LLVMUIntType(32),
    'Int64': ir.IntType(64),
    'UInt64': LLVMUIntType(64),
    'Boolean': ir.IntType(1),
    'Void': ir.VoidType(),
    'Float32': ir.FloatType(),
    'Double64': ir.DoubleType(),
    'Byte': LLVMUIntType(8),
    'SByte': ir.IntType(8),
    'Char': LLVMUIntType(8),
    'Half': ir.HalfType()
}


def null(ty):
    return ir.Constant(ty, None)


@lru_cache(len(TYPE_TO_LLVM) + 20)
def is_builtin_type(ty: str):
    return map_type_to_llvm(ty) is not None and ty != "CString"


@lru_cache(len(SHORTCUT_TO_TYPE) + 20)
def map_shortcut_to_type(shortcut: str) -> str:
    if shortcut.endswith("[]"):