Skip to content

Commit

Permalink
codegen: ReturnStatement code generation inside function
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Feb 3, 2025
1 parent 29c6b04 commit 2dd7c71
Show file tree
Hide file tree
Showing 5 changed files with 1,190 additions and 25 deletions.
49 changes: 25 additions & 24 deletions packages/codegen/src/codegen
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import
expressionShouldBeAllocated,
expressionShouldBeCopied,
expressionShouldBeFreed,
normalizeBlock,
statementHasPrecedingNonDeclaration,
statementIsMacroInvocation,
statementIsMacroInvocationDeclare,
Expand Down Expand Up @@ -171,6 +172,7 @@ export obj Codegen {
"-W" + "error",
"-W" + "extra",
"-pedantic-errors",
"-Wno-parentheses-equality",
"-Wno-strict-prototypes",
"-Wno-unused-but-set-variable",
"-Wno-unused-parameter",
Expand Down Expand Up @@ -933,19 +935,19 @@ export obj Codegen {
)
}

if blockData.usedReturnVariable { // TODO: !this->state.cleanUp.empty()
//(*c)->append(
// CodegenASTStmtIf::create(
// CodegenASTExprBinary::create(
// CodegenASTExprAccess::create("r"),
// "==",
// CodegenASTExprLiteral::create("1")
// ),
// initialStateCleanUp.hasCleanUp(CODEGEN_CLEANUP_FN)
// ? CodegenASTStmtGoto::create(initialStateCleanUp.currentLabel())
// : CodegenASTStmtReturn::create()
// )
//);
if blockData.usedReturnVariable && !blockData.empty() {
finalResult.push(
AST.createIfStatement(
AST.createBinaryExpression(
AST.createIdentifier(blockData.returnVariable()),
"==",
AST.createLiteral("1"),
),
self.blockData.hasTeardownUntil(.Fn)
? AST.createGotoStatement(self.blockData.getLabel())
: AST.createReturnStatement(),
),
)
}

return finalResult
Expand Down Expand Up @@ -1627,7 +1629,8 @@ export obj Codegen {

statement := item.asIf()
condition := self._generateExpression(ref statement.condition)
consequent := self._generateStatement(ref statement.consequent)
items := normalizeBlock(ref statement.consequent)
consequent := AST.createCompoundStatement(self._generateBlock(ref items))
mut alternate: AST.CStatement?

if statement.alternate != nil {
Expand Down Expand Up @@ -1707,15 +1710,7 @@ export obj Codegen {
}
}

mut bodyItems: Parser.Statement[]

if bodyIsBlock {
blockStatement := statement.body.asBlock()
bodyItems.merge(blockStatement.body)
} else {
bodyItems.push(statement.body)
}

mut bodyItems := normalizeBlock(ref statement.body)
body.merge(self._generateBlockBody(ref bodyItems))
self.blockData.decreaseVariableIndexes()

Expand Down Expand Up @@ -1832,14 +1827,20 @@ export obj Codegen {
)
}

if self.blockData.currentHasTeardown() {
atRoot := item.parent().isDeclaration()

if (
item.hasNext() ||
(!atRoot && self.blockData.hasTeardownUntil(.Fn))
) {
result.push(AST.createGotoStatement(self.blockData.getLabel()))
}
} elif cExpression != nil {
result.push(AST.createReturnStatement(cExpression))
} else {
parent := item.parent()

// TODO: better handling of deep nested main declaration
if parent.isMainDeclaration() {
result.push(AST.createReturnStatement(AST.createLiteral("0")))
} else {
Expand Down
13 changes: 13 additions & 0 deletions packages/codegen/src/helpers
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ export fn expressionShouldBeFreed (item: ref Parser.Expression) bool {
)
}

export fn normalizeBlock (statement: ref Parser.Statement) Parser.Statement[] {
mut result: Parser.Statement[]

if statement.isBlock() {
block := statement.asBlock()
result.merge(block.body)
} else {
result.push(statement)
}

return result
}

export fn statementHasPrecedingNonDeclaration (item: ref Parser.Statement) bool {
mut it := item

Expand Down
Loading

0 comments on commit 2dd7c71

Please sign in to comment.