/// <summary> /// Generates the BrainF runtime environment. /// </summary> private void GenerateHeader() { this.module = new Module(this.context, "BrainF"); // === Function prototypes ========================================= Function memsetFunction = Intrinsic.GetDecleration(this.module, IntrinsicType.memset, Type.GetInteger8PointerType(this.context), Type.GetInteger32Type(this.context)); // declare i32 @getchar() this.getcharFunction = (Function)this.module.GetOrInsertFunction("getchar", new FunctionType(Type.GetInteger32Type(this.context))); // declare i32 @putchar(i32) this.putcharFunction = (Function)this.module.GetOrInsertFunction("putchar", new FunctionType(Type.GetInteger32Type(this.context), Type.GetInteger32Type(this.context))); // === Function header ============================================= // define void @brainf() this.brainfFunction = (Function)this.module.GetOrInsertFunction("brainf", new FunctionType(Type.GetVoidType(this.context))); this.builder = new IRBuilder(new BasicBlock( this.context, this.brainfFunction, "brainf")); // %arr = malloc i8, i32 %d Constant valMemory = new Constant(this.context, 32, this.totalMemory); BasicBlock bb = this.builder.GetInsertBlock(); Type intType = Type.GetInteger32Type(this.context); Type byteType = Type.GetInteger8Type(this.context); Constant allocsize = new Constant(byteType); allocsize.TruncOrBitCast(intType); Instruction pointerArray = CallInstruction.CreateMalloc(bb, intType, byteType, allocsize, valMemory, "arr"); bb.PushBack(pointerArray); // call void @llvm.memset. CallInstruction memsetCall = this.builder.CreateCall( memsetFunction, pointerArray, new Constant(this.context, 8, 0), valMemory, new Constant(this.context, 32, 1), new Constant(this.context, 1, 0)); memsetCall.TailCall = false; // this.currentHead = this.builder.CreateGEP( pointerArray, new Constant(this.context, 32, this.totalMemory / 2), "head"); // Footer this.endBlock = new BasicBlock(this.context, this.brainfFunction, "brainf"); this.endBlock.PushBack(CallInstruction.CreateFree(pointerArray, this.endBlock)); ReturnInstruction.Create(this.context, this.endBlock); }