Skip to content

Commit 79d4f04

Browse files
committed
Some more fine touches
1 parent fdd37c1 commit 79d4f04

File tree

3 files changed

+51
-29
lines changed

3 files changed

+51
-29
lines changed

src/main/kotlin/deep/decaf/low/amd64/IRToLow.kt

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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())

src/main/kotlin/deep/decaf/low/amd64/Low.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ data class MoveInstruction(val src: Location, val dest: Location) : Instruction(
182182
}
183183
}
184184

185+
data class LeaqInstruction(val src: Location, val dest: Location) : Instruction() {
186+
override fun toString(): String {
187+
return "leaq $src, $dest"
188+
}
189+
}
190+
185191
data class CMoveInstruction(val type: AsmCMoveOp, val src: Register, val dest: Register) : Instruction() {
186192
override fun toString(): String {
187193
return "$type $src $dest"
@@ -266,19 +272,19 @@ data class Program(
266272
override fun toString(): String {
267273
val stringBuilder = StringBuilder()
268274
stringBuilder.append(".section .rodata").append("\n")
269-
for (text in globalText.keys) {
270-
stringBuilder.append("${globalText[text]}:").append("\n")
271-
stringBuilder.append("\t").append(".string \"$text\"").append("\n")
275+
for (text in globalText.keys) {
276+
stringBuilder.append("$text:").append("\n")
277+
stringBuilder.append("\t").append(".string ${globalText[text]}").append("\n")
272278
}
273279

274280
stringBuilder.append(".section .text").append("\n")
275281
stringBuilder.append("\t").append(".global main").append("\n")
276-
for (method in methods) {
282+
for (method in methods) {
277283
if (method.name == "main") {
278284
stringBuilder.append(method.toString())
279285
}
280286
}
281-
for (method in methods) {
287+
for (method in methods) {
282288
if (method.name != "main") {
283289
stringBuilder.append(method.toString())
284290
}

src/main/kotlin/deep/decaf/tester/LowMethodTester.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.antlr.v4.runtime.*
77
import java.io.*
88

99
fun main() {
10-
val folder = File("/home/deep/work/compiler/low1")
10+
val folder = File("/home/deep/work/compiler/p3files/codegen")
1111
val files = folder.listFiles()!!.filter { !it.isDirectory }.sorted()
1212
for (file in files) {
1313
val scanner = DecafLexer(CharStreams.fromFileName(file.absolutePath))
@@ -16,6 +16,7 @@ fun main() {
1616
val errors = checkSemantics(irTree)
1717
if (errors.isNotEmpty())
1818
throw RuntimeException("Invalid program")
19+
println(file.absolutePath)
1920
val program = irProgramToLow(irTree)
2021
println(program)
2122
}

0 commit comments

Comments
 (0)