fix: include correct main position and don't re-create label

This commit is contained in:
2025-03-14 15:51:34 +00:00
parent 8f7c902ed5
commit 68435207fe
6 changed files with 12 additions and 16 deletions

View File

@@ -51,9 +51,9 @@ private class LabelGenerator {
permittedFuncFile match {
case Some(f) if f != pos.file.getCanonicalPath => Chain.empty
case _ =>
val customLabel = if name == "main" then Chain.empty else Chain(LabelDef(name))
permittedFuncFile = Some(pos.file.getCanonicalPath)
Chain(
LabelDef(name),
customLabel ++ Chain(
Directive.Location(getDebugFile(pos.file), pos.line, None),
Directive.Type(label, SymbolType.Function),
Directive.Func(name, label)

View File

@@ -37,11 +37,7 @@ object asmGenerator {
val Program(funcs, main) = microProg
val mainLabel = LabelDef("main")
val mainAsm = main.headOption match {
case Some(stmt) =>
labelGenerator.getDebugFunc(stmt.pos, "$main", mainLabel) + mainLabel
case None => Chain.one(mainLabel)
}
val mainAsm = labelGenerator.getDebugFunc(microProg.pos, "main", mainLabel) + mainLabel
val progAsm = mainAsm.concatAll(
funcPrologue(),
main.foldMap(generateStmt(_)),

View File

@@ -210,12 +210,12 @@ object assemblyIR {
case Size(label: LabelDef, expr: SizeExpr)
override def toString(): String = this match {
case IntelSyntax => ".intel_syntax noprefix"
case Global(name) => s".globl $name"
case Text => ".text"
case RoData => ".section .rodata"
case Int(value) => s"\t.int $value"
case Asciz(string) => s"\t.asciz \"$string\""
case IntelSyntax => ".intel_syntax noprefix"
case Global(name) => s".globl $name"
case Text => ".text"
case RoData => ".section .rodata"
case Int(value) => s"\t.int $value"
case Asciz(string) => s"\t.asciz \"$string\""
case File(no, file) => s".file $no \"${file}\""
case Location(fileNo, lineNo, colNo) =>
s"\t.loc $fileNo $lineNo" + colNo.map(c => s" $c").getOrElse("")

View File

@@ -93,5 +93,5 @@ object microWacc {
// Program
case class FuncDecl(name: Ident, params: List[Ident], body: Chain[Stmt])(val pos: Position)
case class Program(funcs: Chain[FuncDecl], stmts: Chain[Stmt])
case class Program(funcs: Chain[FuncDecl], stmts: Chain[Stmt])(val pos: Position)
}

View File

@@ -200,7 +200,7 @@ object renamer {
(chunks :+ func, errors ++ scope.add(name, public = true))
}
// ...and main body.
val mainBodyIdent = Ident(MAIN, ty = FuncType(?, Nil))(prog.pos)
val mainBodyIdent = Ident(MAIN, ty = FuncType(?, Nil))(main.head.pos)
val mainBodyErrors = scope.add(mainBodyIdent, public = false)
val mainBodyChunk = FuncDecl(IntType()(prog.pos), mainBodyIdent, Nil, main)(prog.pos)

View File

@@ -36,7 +36,7 @@ object semantics {
case Some((head, tail)) => (head.body, tail)
case None => (Chain.empty, Chain.empty)
}
} yield (microWacc.Program(funcs, typedMain), globalErrors ++ errors)
} yield (microWacc.Program(funcs, typedMain)(main.pos), globalErrors ++ errors)
}
}