@@ -123,38 +123,53 @@ fun irExprToLow(expr: IRExpr, info: AsmProgramInfo): List<Instruction> {
123123 tmp
124124 }
125125 is IRCallOutExpr -> {
126+ var pushCount = 0
126127 for ((i, arg) in expr.argList.withIndex()) {
127- val register = when (i) {
128- 0 -> Register (" rdi" )
129- 1 -> Register (" rsi" )
130- 2 -> Register (" rdx" )
131- 3 -> Register (" rcx" )
132- 4 -> Register (" r8" )
133- 5 -> Register (" r9" )
134- else -> throw IllegalArgumentException (" too many arguments to callout expression" )
135- }
136- when (arg) {
137- is StringCallOutArg -> {
138- val loc = info.addGlobalString(arg.arg)
139- instructions.add(
140- MoveInstruction (
141- MemLoc (Register .basePointer(), StringOffset (loc)),
142- register
128+ if (i < 6 ) {
129+ val register = when (i) {
130+ 0 -> Register (" rdi" )
131+ 1 -> Register (" rsi" )
132+ 2 -> Register (" rdx" )
133+ 3 -> Register (" rcx" )
134+ 4 -> Register (" r8" )
135+ 5 -> Register (" r9" )
136+ else -> throw IllegalArgumentException (" code screwed up" )
137+ }
138+ when (arg) {
139+ is StringCallOutArg -> {
140+ val loc = info.addGlobalString(arg.arg)
141+ instructions.add(
142+ LeaqInstruction (
143+ MemLoc (Register .basePointer(), StringOffset (loc)),
144+ register
145+ )
143146 )
144- )
147+ }
148+ is ExprCallOutArg -> {
149+ val loc = traverse(arg.arg)
150+ instructions.add(MoveInstruction (loc, register))
151+ }
145152 }
146- is ExprCallOutArg -> {
147- val loc = traverse(arg.arg)
148- instructions.add(MoveInstruction (loc, register))
153+ } else {
154+ pushCount++
155+ when (arg) {
156+ is StringCallOutArg -> {
157+ val loc = info.addGlobalString(arg.arg)
158+ instructions.add(PushInstruction (MemLoc (Register .basePointer(), StringOffset (loc))))
159+ }
160+ is ExprCallOutArg -> {
161+ val loc = traverse(arg.arg)
162+ instructions.add(PushInstruction (loc))
163+ }
149164 }
150165 }
151166 }
152- val mustPush = info.stackSize % 2 == 1
153- if (mustPush) {
167+ if (info.stackSize % 2 == 1 ) {
154168 instructions.add(PushInstruction (Register .r10()))
169+ pushCount++
155170 }
156171 instructions.add(CallInstruction (expr.name))
157- if (mustPush ) {
172+ for (i in 1 .. pushCount ) {
158173 instructions.add(PopInstruction (Register .r10()))
159174 }
160175 val tmp = info.addVariable(getUUID())
0 commit comments