Esempio n. 1
0
def geraLLVM(arvore):
    percorreArvore(arvore)
    arquivo = open('vars.ll', 'w')
    arquivo.write(str(module))
    arquivo.close()
    print(module)

    llvm.shutdown()
Esempio n. 2
0
def main():
    global root, func_table, var_table, module
    arg = sys.argv[1]
    if not arg.endswith('.tpp'):
        raise IOError("Not a tpp file!")

    data = open(arg, 'r')
    source_file = data.read()
    data.close()

    tree = SemanticAnalyzer(source_file)

    root = tree.root
    func_table = tree.func_table
    var_table = tree.var_table

    llvm.initialize()
    llvm.initialize_all_targets()
    llvm.initialize_native_target()
    llvm.initialize_native_asmprinter()

    module = ir.Module('meu_modulo.bc')
    module.triple = llvm.get_process_triple()
    target = llvm.Target.from_triple(module.triple)
    target_machine = target.create_target_machine()
    module.data_layout = target_machine.target_data

    generate_code()
    tree.set_atribution_asto()
    tree.export_tree()

    arquivo = open('atribuicao.ll', 'w')
    arquivo.write(str(module))
    arquivo.close()
    print(module)
    llvm.shutdown()
Esempio n. 3
0
mod = lc.Module()
mod.triple = llvm.get_default_triple()
func = lc.Function(mod,
                   lc.FunctionType(lc.VoidType(), [lc.IntType(32)]),
                   name='foo')
builder = lc.IRBuilder(func.append_basic_block())
builder.ret_void()

print(mod)

mod = llvm.parse_assembly(str(mod))

mod.verify()
print(repr(mod))
print(mod)

with llvm.create_module_pass_manager() as pm:
    with llvm.create_pass_manager_builder() as pmb:
        pmb.populate(pm)
    pm.run(mod)

print(mod)

tm = llvm.Target.from_default_triple().create_target_machine()
ee = llvm.create_mcjit_compiler(mod, tm)
func = mod.get_function("foo")
print(func, ee.get_function_address("foo"))
ee.close()

llvm.shutdown()
Esempio n. 4
0
mod = lc.Module()
mod.triple = llvm.get_default_triple()
func = lc.Function(mod, lc.FunctionType(lc.VoidType(), [lc.IntType(32)]),
                   name='foo')
builder = lc.IRBuilder(func.append_basic_block())
builder.ret_void()

print(mod)

mod = llvm.parse_assembly(str(mod))

mod.verify()
print(repr(mod))
print(mod)

with llvm.create_module_pass_manager() as pm:
    with llvm.create_pass_manager_builder() as pmb:
        pmb.populate(pm)
    pm.run(mod)

print(mod)

tm = llvm.Target.from_default_triple().create_target_machine()
ee = llvm.create_mcjit_compiler(mod, tm)
func = mod.get_function("foo")
print(func, ee.get_function_address("foo"))
ee.close()

llvm.shutdown()

Esempio n. 5
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     llvm.shutdown()
Esempio n. 6
0
def main():
    # Initialize LLVM Core and others
    binding.initialize()
    binding.initialize_native_target()
    binding.initialize_native_asmprinter()
    #################################

    # Argument parser
    parser_args = argparse.ArgumentParser(
        prog='sadbeep', description='Implementação simples de um compilador')
    parser_args.add_argument('input', type=str, help='source code')
    parser_args.add_argument('-l',
                             metavar='*.ll',
                             type=str,
                             help='llvm output')
    parser_args.add_argument('-s',
                             metavar='*.s',
                             type=str,
                             help='native assembly output')
    parser_args.add_argument('-o',
                             metavar='',
                             type=str,
                             default='a.out',
                             help='binary output, default a.out')
    parser_args.add_argument('--clang',
                             action='store_const',
                             const=True,
                             help='use clang instead of gcc to assemble')

    args = parser_args.parse_args()
    #################################

    input_stream = FileStream(args.input)

    lexer = sadbeepLexer(input_stream)
    stream = CommonTokenStream(lexer)
    parser = sadbeepParser(stream)

    tree = parser.parse()  # Get AST

    #if parser.getNumberOfSyntaxErrors() > 0:
    #    exit(-1)

    # Transverse AST to generate llvm
    vis = visitor(args.input)
    try:
        vis.visitParse(tree)
    except AttributeError as msg:
        print(
            f"Erro no arquivo {input_stream.fileName}, não é possível executar operações ou declarar variáveis fora "
            f"de funções!")
        exit(-1)
    #################################

    constain_main = False
    for i in range(len(vis.module.functions)):
        if vis.module.functions[i].name == 'main':
            constain_main = True

    if not constain_main:
        print(
            f"O arquivo {input_stream.fileName} não contém a declaração de uma função main!"
        )
        exit(-1)

    # Output llvm
    if args.l:
        with open(args.l, 'w') as lout:
            lout.write(str(vis.module))
    #################################

    # Output native assembly
    if args.s:
        with open(args.s, 'w') as asm:
            asm.write(vis.asm())
    #################################

    # Assemble and link
    assemble(str(vis.module) if args.clang else vis.asm(),
             args.o,
             gcc=not args.clang)
    #################################

    binding.shutdown()  # Shotdown LLVM core