tweak codegen of inline sub

This commit is contained in:
Irmen de Jong 2023-02-09 21:53:35 +01:00
parent 694d088160
commit fe29d8a23f
7 changed files with 18 additions and 48 deletions

View File

@ -86,8 +86,7 @@ fun printAst(root: PtNode, output: (text: String) -> Unit) {
}
is PtSub -> {
val params = if (node.parameters.isEmpty()) "" else "...TODO ${node.parameters.size} PARAMS..."
var str = if(node.inline) "inline " else ""
str += "sub ${node.name}($params) "
var str = "sub ${node.name}($params) "
if(node.returntype!=null)
str += "-> ${node.returntype.name.lowercase()}"
str

View File

@ -26,7 +26,6 @@ class PtSub(
name: String,
val parameters: List<PtSubroutineParameter>,
val returntype: DataType?,
val inline: Boolean,
position: Position
) : PtNamedNode(name, position), IPtSubroutine {
override fun printProperties() {

View File

@ -76,9 +76,6 @@ internal class FunctionCallAsmGen(private val program: PtProgram, private val as
}
}
else if(sub is PtSub) {
if(sub.inline)
throw AssemblyError("can only reliably inline asmsub routines at this time")
if(optimizeIntArgsViaRegisters(sub)) {
if(sub.parameters.size==1) {
val register = if (sub.parameters[0].type in ByteDatatypes) RegisterOrPair.A else RegisterOrPair.AY

View File

@ -271,21 +271,6 @@ internal class ProgramAndVarsGen(
internal fun translateSubroutine(sub: PtSub) {
var onlyVariables = false
if(sub.inline) {
if(options.optimize) {
TODO("check if sub is unused, is this even still reached?")
// if(callGraph.unused(sub))
// return
// from an inlined subroutine only the local variables are generated,
// all other code statements are omitted in the subroutine itself
// (they've been inlined at the call site, remember?)
onlyVariables = true
}
}
asmgen.out("")
val asmStartScope: String
@ -342,10 +327,8 @@ internal class ProgramAndVarsGen(
}
}
if(!onlyVariables) {
asmgen.out("; statements")
sub.children.forEach { asmgen.translate(it) }
}
asmgen.out("; statements")
sub.children.forEach { asmgen.translate(it) }
asmgen.out("; variables")
val asmGenInfo = asmgen.subroutineExtra(sub)

View File

@ -662,8 +662,12 @@ internal class AstChecker(private val program: Program,
err("string var must be initialized with a string literal")
}
if(decl.value !is StringLiteral)
err("string var must be initialized with a string literal")
if(decl.value !is StringLiteral) {
if(decl.type==VarDeclType.MEMORY)
err("strings can't be memory mapped")
else
err("string var must be initialized with a string literal")
}
}
if(compilerOptions.zeropage==ZeropageType.DONTUSE && decl.zeropage == ZeropageWish.REQUIRE_ZEROPAGE)

View File

@ -326,10 +326,11 @@ class IntermediateAstMaker(private val program: Program, private val options: Co
var returntype = srcSub.returntypes.singleOrNull()
if(returntype==DataType.STR)
returntype=DataType.UWORD // if a sub returns 'str', replace with uword. Intermediate AST and I.R. don't contain 'str' datatype anymore.
if(srcSub.inline)
throw FatalAstException("non-asm subs cannot be inline")
val sub = PtSub(srcSub.name,
srcSub.parameters.map { PtSubroutineParameter(it.name, it.type, it.position) },
returntype,
srcSub.inline,
srcSub.position)
sub.parameters.forEach { it.parent=sub }
makeScopeVarsDecls(vardecls).forEach { sub.add(it) }

View File

@ -1,26 +1,13 @@
%zeropage basicsafe
main {
&ubyte[40] topline = $0400
sub start() {
cx16.r0 = memory("slab", $c000, 0)
cx16.r1 = memory("slab", $c000, 0)
testscope.duplicate()
cx16.r0L = testscope.duplicate2()
}
}
testscope {
sub sub1() {
ubyte @shared duplicate
ubyte @shared duplicate2
}
sub duplicate() {
; do nothing
}
sub duplicate2() -> ubyte {
return cx16.r0L
topline[0] = 'a'
topline[1] = 'b'
topline[2] = 'd'
topline[39] = '@'
}
}