Skip to content

Commit

Permalink
Added assembly generation to main function
Browse files Browse the repository at this point in the history
  • Loading branch information
RedDocMD committed Dec 25, 2020
1 parent 79d4f04 commit 8ef6570
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 7 deletions.
16 changes: 16 additions & 0 deletions .idea/artifacts/DecafKotlin_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/libraries/commons_io_2_8_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions DecafKotlin.iml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
<orderEntry type="library" name="antlr-4.9-complete" level="project" />
<orderEntry type="library" name="picocli-4.5.2" level="project" />
<orderEntry type="library" name="commons-io-2.8.0" level="project" />
</component>
</module>
Binary file added libs/commons-io-2.8.0.jar
Binary file not shown.
6 changes: 3 additions & 3 deletions src/main/kotlin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: deep.decaf.main.CompilerKt

Manifest-Version: 1.0
Main-Class: deep.decaf.main.CompilerKt

4 changes: 2 additions & 2 deletions src/main/kotlin/deep/decaf/low/amd64/IRToLow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fun irExprToLow(expr: IRExpr, info: AsmProgramInfo): List<Instruction> {
val loc = info.addGlobalString(arg.arg)
instructions.add(
LeaqInstruction(
MemLoc(Register.basePointer(), StringOffset(loc)),
MemLoc(Register.instructionPointer(), StringOffset(loc)),
register
)
)
Expand All @@ -155,7 +155,7 @@ fun irExprToLow(expr: IRExpr, info: AsmProgramInfo): List<Instruction> {
when (arg) {
is StringCallOutArg -> {
val loc = info.addGlobalString(arg.arg)
instructions.add(PushInstruction(MemLoc(Register.basePointer(), StringOffset(loc))))
instructions.add(PushInstruction(MemLoc(Register.instructionPointer(), StringOffset(loc))))
}
is ExprCallOutArg -> {
val loc = traverse(arg.arg)
Expand Down
46 changes: 44 additions & 2 deletions src/main/kotlin/deep/decaf/main/Compiler.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package deep.decaf.main

import deep.decaf.ir.*
import deep.decaf.low.amd64.*
import deep.decaf.parser.*
import org.antlr.v4.runtime.*
import org.apache.commons.io.*
import picocli.*
import picocli.CommandLine.*;
import picocli.CommandLine.*
import java.io.*
import java.util.concurrent.*
import kotlin.system.*
Expand All @@ -30,11 +32,17 @@ class Compiler : Callable<Int> {

@Option(names = ["-c", "--semantic"], description = ["Scan, parse and then perform semantic checks"])
var check = false

@Option(names = ["-a", "--all"], description = ["Perform all steps and create assembly code"])
var all = false
}

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

@Option(names = ["-o", "--output"], description = ["Name of output file"])
lateinit var output: File

override fun call(): Int {
if (!file.exists()) {
val msg = Help.Ansi.AUTO.string("@|bold,red ${file.absolutePath} does not exist|@")
Expand Down Expand Up @@ -64,7 +72,41 @@ class Compiler : Callable<Int> {
} else newExitCode
} else exitCode
}
else -> 0
stage.all -> {
val lexer = makeLexer(file)
val exitCode = scanFile(lexer, debug)
if (exitCode == 0) {
val parser = makeParser(lexer)
val (newExitCode, tree) = parseFile(parser)
if (newExitCode == 0) {
val ir = makeIR(tree)
val semanticCheckCode = semanticCheck(ir, debug)
if (semanticCheckCode == 0) {
try {
val bufferedWriter: BufferedWriter = if (::output.isInitialized) {
output.bufferedWriter()
} else {
val fileNameNoExtension = FilenameUtils.getBaseName(file.name)
val basePath = FilenameUtils.getFullPath(file.absolutePath)
val outFilePath = "$basePath${File.separator}$fileNameNoExtension.s"
val outFile = File(outFilePath)
outFile.bufferedWriter()
}
val program = irProgramToLow(ir)
bufferedWriter.append(program.toString())
bufferedWriter.flush()
bufferedWriter.close()
0
} catch (e: IOException) {
val msg = Help.Ansi.AUTO.string("@|bold,red $e|@")
println(msg)
1
}
} else semanticCheckCode
} else newExitCode
} else exitCode
}
else -> 1
}
}
}
Expand Down

0 comments on commit 8ef6570

Please sign in to comment.