Commit 7d1088e6 authored by Léo Grange's avatar Léo Grange
Browse files

lot of fixes to have a working llvm-as

parent 88aaa041
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -183,8 +183,14 @@ void CompileContext::generateMain() {
	// FIXME main should be void -> int
	llvm::Function *main = helperCreateFunction("main");

	
	//llvm::BasicBlock *mainLoop= & main->getEntryBlock();
	//mainLoop->setName("infiniteLoop");

	llvm::BasicBlock *mainLoop = llvm::BasicBlock::Create(llvm::getGlobalContext(),
			"infiniteLoop", main);
			"infiniteLoop");
	irbuilder.CreateBr(mainLoop); // needed to have an enty without predecessor
	main->getBasicBlockList().push_back(mainLoop);
	irbuilder.SetInsertPoint(mainLoop);

	// call each state's 'do step' function
@@ -194,6 +200,11 @@ void CompileContext::generateMain() {

	// this is an infinite loop for now ;)
	irbuilder.CreateBr(mainLoop);

	llvm::BasicBlock *useless = llvm::BasicBlock::Create(llvm::getGlobalContext(),
			"useless_ret");
	main->getBasicBlockList().push_back(useless);
	irbuilder.SetInsertPoint(useless);
	irbuilder.CreateRet(nullptr);
}

@@ -211,7 +222,7 @@ llvm::Function *CompileContext::helperCreateFunction(const std::string &name,
				false);
	}

	ret = llvm::Function::Create(type, llvm::GlobalValue::CommonLinkage,
	ret = llvm::Function::Create(type, llvm::GlobalValue::ExternalLinkage,
			name, module.get());

	// create a new Basic Block
+5 −2
Original line number Diff line number Diff line
@@ -257,8 +257,11 @@ public:
	
	void addVariable(const std::string &name, Variable var = Variable()) {
		// create a new global variable
		if(!var.value)
			var.value = helperCreateVariable(variableName(name));
		if(!var.value) {
			llvm::ConstantInt *zero = llvm::ConstantInt::get(
					llvm::Type::getInt32Ty(llvm::getGlobalContext()), 0, false);
			var.value = helperCreateVariable(variableName(name), zero);
		}
			
		variables[name] = var;

+15 −2
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@
#include <llvm/IR/PassManager.h>
#include <llvm/LinkAllPasses.h>

#include <llvm/Support/TargetRegistry.h>

//extern "C" int yylex() {}
extern int yyparse();

@@ -48,15 +50,26 @@ int main() {
	// now generate the code for the main()
	ccon.generateMain();

	std::cout << "Done." << std::endl;
	std::cerr << "Code generated." << std::endl;

	llvm::ModulePassManager pm;
	llvm::ModulePass * printer = llvm::createPrintModulePass(llvm::outs());
	MagicModulePass magic(printer);
	// FUUUUUCK....

	// for a strange reason, my LLVM version have compile-time problems if
	// I give the printer ModulePass directly directly
	pm.addPass<MagicModulePass>(magic);
	pm.run(ccon.getModule());

	std::cerr << "\nListing supported targets :" << std::endl;
	for(auto& target: llvm::TargetRegistry::targets()) {
		std::cerr << target.getName() << std::endl;
	}

	//llvm::TargetRegistry::printRegisteredTargetsForVersion();

	//llvm::TargetMachine *armMachine = new llvm::ARM

	return 0;
}
+2 −3
Original line number Diff line number Diff line
@@ -231,7 +231,6 @@ llvm::Value *NAutoItem::codeGen(CompileContext &ccon) {
	// add initialization function
	acon->activateDefinition();
	stmts->codeGen(ccon);

	acon->terminateInitiliazer();

	// add states : special case, need to "declare" all states before
@@ -256,7 +255,7 @@ llvm::Value *NState::codeGen(CompileContext &ccon) {
	// generate the init function
	thisState->activateInitializer();
	stmts->codeGen(ccon);
	thisState->activateInitializer();
	thisState->terminateInitiliazer();
	
	// generate the polling function
	thisState->activateSignalPolling();
@@ -390,7 +389,7 @@ llvm::Value *NBitfieldVariable::assignCodeGen(CompileContext &ccon,
	if(!bitFrom && !bitTo) {
		Variable var = ccon.lookupVariable(ident->ident);
		if(var.value) {
			return ccon.builder().CreateStore(var.value, val);
			return ccon.builder().CreateStore(val, var.value);
		}	
		else {
			if(ccon.lookupConstant(ident->ident)) {