Exemplo n.º 1
0
void CodeGenContext::generateCode(NBlock& root)
{
	std::cout << "Generating code...\n";

	vector<Type*> argTypes;
	FunctionType* ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), makeArrayRef(argTypes), false);
	mainFunction = Function::Create(ftype, GlobalValue::InternalLinkage, "main", module);
	BasicBlock* bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0);

	pushBlock(bblock);
	root.codeGen(*this);
	ReturnInst::Create(getGlobalContext(), bblock);
	popBlock();

	std::cout << "Code generation complete.\n";

	PassManager pm;
	pm.add(createPrintModulePass(outs()));
	pm.run(*module);

	//Pass* printer = createPrintModulePass(outs());
	//ModulePassManager pm;
	//pm.addPass(printer);
	//pm.run(module);
}
Exemplo n.º 2
0
/* Compile the AST into a module */
void CodeGenContext::generateCode(NBlock& root)
{
	//std::cout << "Generating code...\n";

	/* Create the top level interpreter function to call as entry */
	vector<Type*> argTypes;
	FunctionType *ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), makeArrayRef(argTypes), false);
	mainFunction = Function::Create(ftype, GlobalValue::InternalLinkage, "main", module);
	BasicBlock *bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0);

	currentFunction = mainFunction;

	/* Push a new variable/block context */
	pushBlock(bblock);
	root.codeGen(*this); /* emit bytecode for the toplevel block */
	ReturnInst::Create(getGlobalContext(), bblock);
	popBlock();

	/* Print the bytecode in a human-readable format
	   to see if our program compiled properly
	 */
	//std::cout << "Code is generated.\n";
	PassManager pm;
	pm.add(createPrintModulePass(outs()));
	pm.run(*module);
}
Exemplo n.º 3
0
/* Compile the AST into a module */
void CodeGenContext::generateCode( NBlock &root )
{
    std::cout << "Generating code...\n";

    /* Create the top level interpreter function to call as entry */
    FunctionType *ftype = FunctionType::get( Type::getInt64Ty( getGlobalContext() ), false );
    mainFunction = Function::Create( ftype, Function::ExternalLinkage, "main", module );
    BasicBlock *bblock = BasicBlock::Create( getGlobalContext(), "entry", mainFunction, 0 );

    /* Push a new variable/block context */
    pushBlock( bblock );
    pushParentFunction( mainFunction );
    root.codeGen( *this ); /* emit bytecode for the toplevel block */
    //ReturnInst::Create( getGlobalContext(), getCurrentReturnValue(), currentBlock() );
    popParentFunction();
    popBlock();

    /* Print the bytecode in a human-readable format
	   to see if our program compiled properly
	 */
    std::cout << "Code is generated.\n";
    llvm::legacy::PassManager pm;

    pm.add( createPrintModulePass( outs() ) );
    pm.run( *module );
}
Exemplo n.º 4
0
/* Compile the AST into a module */
void CodeGenContext::generateCode(NBlock& root)
{
	std::cout << "Generating code...\n";
	
	/* Create the top level interpreter function to call as entry */
	vector<Type*> argTypes;
	FunctionType *ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), makeArrayRef(argTypes), false);
	//mainFunction = Function::Create(ftype, GlobalValue::ExternalLinkage, "main", module);
	//BasicBlock *bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0);
	//mainFunction->setCallingConv(CallingConv::C);

	AttributeSet func_func_PAL;
	{
	 	SmallVector<AttributeSet, 4> Attrs;
	  	AttributeSet PAS;
	  	{
	    	AttrBuilder B;
	    	B.addAttribute(Attribute::NoUnwind);
	    	PAS = AttributeSet::get(getGlobalContext(), ~0U, B);
	    }
	  
	  	Attrs.push_back(PAS);
	  	func_func_PAL = AttributeSet::get(getGlobalContext(), Attrs);
	 }
	 //mainFunction->setAttributes(func_func_PAL);
	
	/* Push a new variable/block context */
	//pushBlock(bblock, false);
	root.codeGen(*this); /* emit bytecode for the toplevel block */
	//ReturnInst::Create(getGlobalContext(), bblock);
	//popBlock();
	
	/* Print the bytecode in a human-readable format 
	   to see if our program compiled properly
	 */
	std::cout << "Code is generated.\n";
	//module->dump();
	verifyModule(*module, PrintMessageAction);
	PassManager pm;
	pm.add(createPrintModulePass(&outs()));
	pm.run(*module);
}
Exemplo n.º 5
0
/* Compile AST into a Module */
void CodeGenContext::generateCode(NBlock& root)
{
  std::cout << "Generating Code...\n";

  // Create top level interpreter functions to use as an entry.
  vector<const Type*> argTypes;
  FunctionType *ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), argTypes, false);
  mainFunction = Function::Create(ftype, GlobalValue::InternalLinkage, "main", module);
  BasicBlock *bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0);

  // Create a new variable/block context.
  pushBlock(bblock);
  root.codeGen(*this); /* emit bytecode for the toplevel block. */
  ReturnInst::Create(getGlobalContext(), bblock);
  popBlock();

  /* Print the bytecode out in a human-readable format.
     Then we can see if it compiled right */
  std::cout << "Code Generation Finished.\n";
  PassManager pm;
  pm.add(createPrintModulePass(&outs()));
  pm.run(*module);
}