Skip to content

Commit 8ef6570

Browse files
committed
Added assembly generation to main function
1 parent 79d4f04 commit 8ef6570

File tree

7 files changed

+75
-7
lines changed

7 files changed

+75
-7
lines changed

.idea/artifacts/DecafKotlin_jar.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/commons_io_2_8_0.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DecafKotlin.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
1414
<orderEntry type="library" name="antlr-4.9-complete" level="project" />
1515
<orderEntry type="library" name="picocli-4.5.2" level="project" />
16+
<orderEntry type="library" name="commons-io-2.8.0" level="project" />
1617
</component>
1718
</module>

libs/commons-io-2.8.0.jar

279 KB
Binary file not shown.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Manifest-Version: 1.0
2-
Main-Class: deep.decaf.main.CompilerKt
3-
1+
Manifest-Version: 1.0
2+
Main-Class: deep.decaf.main.CompilerKt
3+

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fun irExprToLow(expr: IRExpr, info: AsmProgramInfo): List<Instruction> {
140140
val loc = info.addGlobalString(arg.arg)
141141
instructions.add(
142142
LeaqInstruction(
143-
MemLoc(Register.basePointer(), StringOffset(loc)),
143+
MemLoc(Register.instructionPointer(), StringOffset(loc)),
144144
register
145145
)
146146
)
@@ -155,7 +155,7 @@ fun irExprToLow(expr: IRExpr, info: AsmProgramInfo): List<Instruction> {
155155
when (arg) {
156156
is StringCallOutArg -> {
157157
val loc = info.addGlobalString(arg.arg)
158-
instructions.add(PushInstruction(MemLoc(Register.basePointer(), StringOffset(loc))))
158+
instructions.add(PushInstruction(MemLoc(Register.instructionPointer(), StringOffset(loc))))
159159
}
160160
is ExprCallOutArg -> {
161161
val loc = traverse(arg.arg)

src/main/kotlin/deep/decaf/main/Compiler.kt

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package deep.decaf.main
22

33
import deep.decaf.ir.*
4+
import deep.decaf.low.amd64.*
45
import deep.decaf.parser.*
56
import org.antlr.v4.runtime.*
7+
import org.apache.commons.io.*
68
import picocli.*
7-
import picocli.CommandLine.*;
9+
import picocli.CommandLine.*
810
import java.io.*
911
import java.util.concurrent.*
1012
import kotlin.system.*
@@ -30,11 +32,17 @@ class Compiler : Callable<Int> {
3032

3133
@Option(names = ["-c", "--semantic"], description = ["Scan, parse and then perform semantic checks"])
3234
var check = false
35+
36+
@Option(names = ["-a", "--all"], description = ["Perform all steps and create assembly code"])
37+
var all = false
3338
}
3439

3540
@Option(names = ["-g", "--debug"], description = ["Enable debug mode"])
3641
var debug = false
3742

43+
@Option(names = ["-o", "--output"], description = ["Name of output file"])
44+
lateinit var output: File
45+
3846
override fun call(): Int {
3947
if (!file.exists()) {
4048
val msg = Help.Ansi.AUTO.string("@|bold,red ${file.absolutePath} does not exist|@")
@@ -64,7 +72,41 @@ class Compiler : Callable<Int> {
6472
} else newExitCode
6573
} else exitCode
6674
}
67-
else -> 0
75+
stage.all -> {
76+
val lexer = makeLexer(file)
77+
val exitCode = scanFile(lexer, debug)
78+
if (exitCode == 0) {
79+
val parser = makeParser(lexer)
80+
val (newExitCode, tree) = parseFile(parser)
81+
if (newExitCode == 0) {
82+
val ir = makeIR(tree)
83+
val semanticCheckCode = semanticCheck(ir, debug)
84+
if (semanticCheckCode == 0) {
85+
try {
86+
val bufferedWriter: BufferedWriter = if (::output.isInitialized) {
87+
output.bufferedWriter()
88+
} else {
89+
val fileNameNoExtension = FilenameUtils.getBaseName(file.name)
90+
val basePath = FilenameUtils.getFullPath(file.absolutePath)
91+
val outFilePath = "$basePath${File.separator}$fileNameNoExtension.s"
92+
val outFile = File(outFilePath)
93+
outFile.bufferedWriter()
94+
}
95+
val program = irProgramToLow(ir)
96+
bufferedWriter.append(program.toString())
97+
bufferedWriter.flush()
98+
bufferedWriter.close()
99+
0
100+
} catch (e: IOException) {
101+
val msg = Help.Ansi.AUTO.string("@|bold,red $e|@")
102+
println(msg)
103+
1
104+
}
105+
} else semanticCheckCode
106+
} else newExitCode
107+
} else exitCode
108+
}
109+
else -> 1
68110
}
69111
}
70112
}

0 commit comments

Comments
 (0)