Skip to content

Commit

Permalink
Opaque pointer GEP fixes for BrainF example
Browse files Browse the repository at this point in the history
  • Loading branch information
dwblaikie committed Jul 19, 2021
1 parent 9bdf1ab commit 649977c
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions llvm/examples/BrainF/BrainF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,13 @@ void BrainF::header(LLVMContext& C) {

//%arrmax = getelementptr i8 *%arr, i32 %d
if (comflag & flag_arraybounds) {
ptr_arrmax = builder->
CreateGEP(ptr_arr, ConstantInt::get(C, APInt(32, memtotal)), "arrmax");
ptr_arrmax = builder->CreateGEP(
Int8Ty, ptr_arr, ConstantInt::get(C, APInt(32, memtotal)), "arrmax");
}

//%head.%d = getelementptr i8 *%arr, i32 %d
curhead = builder->CreateGEP(ptr_arr,
ConstantInt::get(C, APInt(32, memtotal/2)),
headreg);
curhead = builder->CreateGEP(
Int8Ty, ptr_arr, ConstantInt::get(C, APInt(32, memtotal / 2)), headreg);

//Function footer

Expand Down Expand Up @@ -195,6 +194,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
char c;
int loop;
int direction;
Type *Int8Ty = IntegerType::getInt8Ty(C);

while(cursym != SYM_EOF && cursym != SYM_ENDLOOP) {
// Write out commands
Expand Down Expand Up @@ -223,8 +223,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
case SYM_WRITE:
{
//%tape.%d = load i8 *%head.%d
LoadInst *tape_0 =
builder->CreateLoad(IntegerType::getInt8Ty(C), curhead, tapereg);
LoadInst *tape_0 = builder->CreateLoad(Int8Ty, curhead, tapereg);

//%tape.%d = sext i8 %tape.%d to i32
Value *tape_1 = builder->
Expand All @@ -244,9 +243,9 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
case SYM_MOVE:
{
//%head.%d = getelementptr i8 *%head.%d, i32 %d
curhead = builder->
CreateGEP(curhead, ConstantInt::get(C, APInt(32, curvalue)),
headreg);
curhead = builder->CreateGEP(Int8Ty, curhead,
ConstantInt::get(C, APInt(32, curvalue)),
headreg);

//Error block for array out of bounds
if (comflag & flag_arraybounds)
Expand Down Expand Up @@ -276,8 +275,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
case SYM_CHANGE:
{
//%tape.%d = load i8 *%head.%d
LoadInst *tape_0 =
builder->CreateLoad(IntegerType::getInt8Ty(C), curhead, tapereg);
LoadInst *tape_0 = builder->CreateLoad(Int8Ty, curhead, tapereg);

//%tape.%d = add i8 %tape.%d, %d
Value *tape_1 = builder->
Expand All @@ -300,9 +298,8 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
builder->SetInsertPoint(bb_1);

// Make part of PHI instruction now, wait until end of loop to finish
PHINode *phi_0 =
PHINode::Create(PointerType::getUnqual(IntegerType::getInt8Ty(C)),
2, headreg, testbb);
PHINode *phi_0 = PHINode::Create(PointerType::getUnqual(Int8Ty), 2,
headreg, testbb);
phi_0->addIncoming(curhead, bb_0);
curhead = phi_0;

Expand Down Expand Up @@ -441,8 +438,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
Value *head_0 = phi;

//%tape.%d = load i8 *%head.%d
LoadInst *tape_0 = new LoadInst(IntegerType::getInt8Ty(C), head_0,
tapereg, testbb);
LoadInst *tape_0 = new LoadInst(Int8Ty, head_0, tapereg, testbb);

//%test.%d = icmp eq i8 %tape.%d, 0
ICmpInst *test_0 = new ICmpInst(*testbb, ICmpInst::ICMP_EQ, tape_0,
Expand All @@ -456,9 +452,8 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
builder->SetInsertPoint(bb_0);

//%head.%d = phi i8 *[%head.%d, %main.%d]
PHINode *phi_1 = builder->
CreatePHI(PointerType::getUnqual(IntegerType::getInt8Ty(C)), 1,
headreg);
PHINode *phi_1 =
builder->CreatePHI(PointerType::getUnqual(Int8Ty), 1, headreg);
phi_1->addIncoming(head_0, testbb);
curhead = phi_1;
}
Expand Down

0 comments on commit 649977c

Please sign in to comment.