Ejemplo n.º 1
0
		TType* typeChk(Symtable& t,TType* expected = NULL){
			bool err=false;
			TType* x=t.lookupType(type.name);
#ifdef DEBUG
		    cout<<"Type Function test "<<type.name<<endl;
#endif
			if(x==NULL){
				cerr<<"Type "<<type.name<<" not defined"<<endl;
			}
			TType* c;
			for(int i=0;i<args.size();i++){
				c=args[i]->typeChk(t);
				if(c==NULL) err=true;
			}
			block->typeChk(t,x);
			//cerr<<x<<err<<endl;
			if(err){
				cerr << "Error in "<<id.name<<" declaration"<<endl;
				return NULL;
			}
			return t.lookupType("void");
		}
Ejemplo 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()), argTypes, false);
	mainFunction = Function::Create(ftype, GlobalValue::ExternalLinkage, "main", module);
	BasicBlock *bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0);
	/* Push a new variable/block context */
	pushBlock(bblock);
	root.codeGen(*this); /* emit bytecode for the toplevel block */
	builder.CreateRetVoid();
	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);
}
Ejemplo n.º 3
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);
}