Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dleiferives committed May 22, 2024
1 parent 2a8d29f commit 6c9555e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/ir/ir_phi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ pub const Function = struct {
pub const NotFoundError = error{ OutOfMemory, UnboundIdentifier, AllocFailed };

pub fn getNamedRef(self: *Function, ir: *const IR, name: StrID, bb: IR.BasicBlock.ID) NotFoundError!Ref {
if (name != IR.InternPool.NULL) {
std.debug.print("getting ref for {s}\n", .{ir.getIdent(name)});
} else {
std.debug.print("getting ref for NULL\n", .{});
}
// check if the register is in the current block
if (self.bbs.get(bb).versionMap.contains(name)) {
return self.bbs.get(bb).versionMap.get(name).?;
Expand All @@ -430,7 +435,9 @@ pub const Function = struct {
try visited.put(bb, true);
while (queue.items.len > 0) {
const current = queue.orderedRemove(0);
std.debug.print("visiting {s}\n", .{self.bbs.get(current).name});
if (self.bbs.get(current).versionMap.contains(name)) {
std.debug.print("found in block {d}\n", .{current});
return self.bbs.get(current).versionMap.get(name).?;
}
for (self.bbs.get(current).incomers.items) |incomer| {
Expand Down Expand Up @@ -1459,6 +1466,8 @@ pub const CfgFunction = struct {
while (statIter.nextInc()) |c_stat| {
const statementIndex = c_stat.kind.Statement.statement;
const statementNode = c_stat.kind.Statement;
self.printBlockName(cBlock);
ast.printNodeLine(c_stat);
const innerNode = ast.get(statementIndex);
const kind = innerNode.kind;
const finalIndex = c_stat.kind.Statement.finalIndex;
Expand All @@ -1470,6 +1479,9 @@ pub const CfgFunction = struct {
try self.blocks.items[cBlock].addIdentsFromStatement(ir, ast, c_stat);
// add the statement to the block
try self.blocks.items[cBlock].statements.append(c_stat);
std.debug.print("items in block ", .{});
self.printBlockName(cBlock);
std.debug.print("{any}\n", .{self.blocks.items[cBlock].statements.items});
continue;
}

Expand Down Expand Up @@ -1826,7 +1838,9 @@ pub const BasicBlock = struct {
const predInst = predBB.versionMap.get(ident);
// if there is no phi for the pred block then continue
if (predInst == null) {
try bbPhi.entries.append(IR.PhiEntry{ .ref = IR.Ref.default, .bb = it });
var phiEntryTemp = IR.PhiEntry{ .ref = IR.Ref.default, .bb = it };
phiEntryTemp.ref.name = ident;
try bbPhi.entries.append(phiEntryTemp);
continue;
}
try bbPhi.entries.append(IR.PhiEntry{ .ref = predInst.?, .bb = it });
Expand Down
23 changes: 17 additions & 6 deletions src/ir/phi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,20 @@ pub fn gen_function(
// this makes it so ret reg is always `%0`
if (fun.returnType != .void) {
// allocate a stack slot for the return value in the entry
retReg = try fun.addInst(entryBB, Inst.alloca(fun.returnType), fun.returnType);
retReg = try fun.addNamedInst(entryBB, Inst.alloca(fun.returnType), retReg.name, fun.returnType);
// save it in the function for easy access later
fun.setReturnReg(retReg.id);
}
try fun.typesMap.put(retReg.name, fun.returnType);

// go through the basic blocks and add the statements for each.
// update variableMap as we go
ast.debugPrintAst();
// ast.debugPrintAst();
for (fun.cfg.postOrder.items) |cfgBlockID| {
fun.cfg.printBlockName(cfgBlockID);
try generateInstsFromCfg(ir, ast, fun, cfgBlockID);
}
// link the entry block to the first block
try fun.addCtrlFlowInst(entryBB, Inst.jmp(IR.Ref.label(fun.cfgToBBs.get(0).?)));

// relink all the phi nodes
for (fun.bbs.items()) |bb| {
Expand Down Expand Up @@ -312,6 +312,7 @@ pub fn gen_function(
_ = try fun.addInst(fun.exitBBID, Inst.retVoid(), .void);
}

try fun.addCtrlFlowInst(entryBB, Inst.jmp(IR.Ref.label(fun.cfgToBBs.get(0).?)));
return fun;
}

Expand Down Expand Up @@ -351,6 +352,10 @@ pub fn generateInstsFromCfg(ir: *IR, ast: *const Ast, fun: *IR.Function, cfgBloc
const bbID = fun.cfgToBBs.get(cfgBlockID).?;
const cfgBlock = fun.cfg.blocks.items[cfgBlockID];
const bb = fun.bbs.get(bbID);
for (cfgBlock.statements.items) |stmtNode| {
fun.cfg.printBlockName(cfgBlockID);
ast.printNodeLine(stmtNode);
}

if (cfgBlock.conditional) {
const statments = cfgBlock.statements;
Expand All @@ -360,6 +365,7 @@ pub fn generateInstsFromCfg(ir: *IR, ast: *const Ast, fun: *IR.Function, cfgBloc
//TODO generate the control flow jump
const brInst = Inst.br(condRef, IR.Ref.label(bb.outgoers[0].?), IR.Ref.label(bb.outgoers[1].?));
try fun.addCtrlFlowInst(bbID, brInst);
try fun.bbs.get(bbID).versionMap.put(condRef.name, condRef);

return;
} else {
Expand Down Expand Up @@ -488,7 +494,7 @@ fn gen_statement(
.Assignment => |assign| {
const to = ast.get(assign.lhs).kind.LValue;
const toName = ir.internIdentNodeAt(ast, to.ident);
log.trace("assign to: {s} [{d}]\n", .{ ast.getIdentValue(to.ident), toName });
std.debug.print("assign to: {s} [{d}]\n", .{ ast.getIdentValue(to.ident), toName });
var name = toName;

// FIXME: handle selector chain
Expand All @@ -509,7 +515,11 @@ fn gen_statement(
// FIXME: rhs could also be a `read` handle!
const exprNode = ast.get(assign.rhs).*;
const exprRef = try gen_expression(ir, ast, fun, bb, exprNode);
_ = fun.renameRef(exprRef, toName);
// _ = fun.renameRef(exprRef, toName);
if (exprRef.name != IR.InternPool.NULL) {
std.debug.print("exprRef name {s}\n", .{ir.getIdent(exprRef.name)});
}
try fun.bbs.get(bb).versionMap.put(exprRef.name, exprRef);
try fun.bbs.get(bb).versionMap.put(name, exprRef);
},
.Print => |print| {
Expand Down Expand Up @@ -625,6 +635,7 @@ fn gen_expression(
.Identifier => ident: {
const identID = ir.internToken(ast, atom.token);
const ref = try fun.getNamedRef(ir, identID, bb);
try fun.bbs.get(bb).versionMap.put(identID, ref);
break :ident ref;
},
.False => false: {
Expand Down Expand Up @@ -1229,7 +1240,7 @@ test "phi.print_addition2" {

test "phi.print_test_if" {
errdefer log.print();
const in = "fun main() void { int a,b,c; if(a == 1){ b =c;} b = a; }";
const in = "fun main() int {\n int a,b,c;\n if(a == 1){\n b =c;\n}\n b = a; return b;\n }";
var str = try inputToIRString(in, testAlloc);
std.debug.print("{s}\n", .{str});
// print out the IR
Expand Down

0 comments on commit 6c9555e

Please sign in to comment.