added -D command line option to define symbols in the assembly file

This commit is contained in:
Irmen de Jong 2022-07-06 23:40:36 +02:00
parent beea6bc794
commit b41779bd02
10 changed files with 48 additions and 3 deletions

View File

@ -20,5 +20,6 @@ class CompilationOptions(val output: OutputType,
var asmQuiet: Boolean = false, var asmQuiet: Boolean = false,
var asmListfile: Boolean = false, var asmListfile: Boolean = false,
var experimentalCodegen: Boolean = false, var experimentalCodegen: Boolean = false,
var outputDir: Path = Path("") var outputDir: Path = Path(""),
var symbolDefs: Map<String, String> = emptyMap()
) )

View File

@ -73,6 +73,13 @@ internal class ProgramAndVarsGen(
asmgen.out("P8ESTACK_LO = ${compTarget.machine.ESTACK_LO.toHex()}") asmgen.out("P8ESTACK_LO = ${compTarget.machine.ESTACK_LO.toHex()}")
asmgen.out("P8ESTACK_HI = ${compTarget.machine.ESTACK_HI.toHex()}") asmgen.out("P8ESTACK_HI = ${compTarget.machine.ESTACK_HI.toHex()}")
if(options.symbolDefs.isNotEmpty()) {
asmgen.out("; -- user supplied symbols on the command line")
for((name, value) in options.symbolDefs) {
asmgen.out("$name = $value")
}
}
when(options.output) { when(options.output) {
OutputType.RAW -> { OutputType.RAW -> {
asmgen.out("; ---- raw assembler program ----") asmgen.out("; ---- raw assembler program ----")

View File

@ -144,6 +144,15 @@ class AstToXmlConverter(internal val program: PtProgram,
xml.endElt() xml.endElt()
} }
} }
if(options.symbolDefs.isNotEmpty()) {
xml.startChildren()
options.symbolDefs.forEach { name, value ->
xml.elt("symboldef")
xml.attr("name", name)
xml.attr("value", value)
xml.endElt()
}
}
xml.endElt() xml.endElt()
} }

View File

@ -58,6 +58,9 @@ class CodeGen(internal val program: PtProgram,
} }
} }
if(options.symbolDefs.isNotEmpty())
throw AssemblyError("virtual target doesn't support symbols defined on the commandline")
for (block in program.allBlocks()) { for (block in program.allBlocks()) {
vmprog.addBlock(translate(block)) vmprog.addBlock(translate(block))
} }

View File

@ -46,10 +46,10 @@ private fun compileMain(args: Array<String>): Boolean {
val quietAssembler by cli.option(ArgType.Boolean, fullName = "quietasm", description = "don't print assembler output results") val quietAssembler by cli.option(ArgType.Boolean, fullName = "quietasm", description = "don't print assembler output results")
val asmListfile by cli.option(ArgType.Boolean, fullName = "asmlist", description = "make the assembler produce a listing file as well") val asmListfile by cli.option(ArgType.Boolean, fullName = "asmlist", description = "make the assembler produce a listing file as well")
val experimentalCodegen by cli.option(ArgType.Boolean, fullName = "expericodegen", description = "use experimental/alternative codegen") val experimentalCodegen by cli.option(ArgType.Boolean, fullName = "expericodegen", description = "use experimental/alternative codegen")
val compilationTarget by cli.option(ArgType.String, fullName = "target", description = "target output of the compiler (one of '${C64Target.NAME}', '${C128Target.NAME}', '${Cx16Target.NAME}', '${AtariTarget.NAME}', '${VMTarget.NAME}')") val compilationTarget by cli.option(ArgType.String, fullName = "target", description = "target output of the compiler (one of '${C64Target.NAME}', '${C128Target.NAME}', '${Cx16Target.NAME}', '${AtariTarget.NAME}', '${VMTarget.NAME}')").default(C64Target.NAME)
.default(C64Target.NAME)
val sourceDirs by cli.option(ArgType.String, fullName="srcdirs", description = "list of extra paths, separated with ${File.pathSeparator}, to search in for imported modules").multiple().delimiter(File.pathSeparator) val sourceDirs by cli.option(ArgType.String, fullName="srcdirs", description = "list of extra paths, separated with ${File.pathSeparator}, to search in for imported modules").multiple().delimiter(File.pathSeparator)
val startVm by cli.option(ArgType.Boolean, fullName = "vm", description = "load and run a p8-virt listing in the VM instead") val startVm by cli.option(ArgType.Boolean, fullName = "vm", description = "load and run a p8-virt listing in the VM instead")
val symbolDefs by cli.option(ArgType.String, fullName = "D", description = "define assembly symbol(s) like -D SYMBOL=VALUE").multiple()
val moduleFiles by cli.argument(ArgType.String, fullName = "modules", description = "main module file(s) to compile").multiple(999) val moduleFiles by cli.argument(ArgType.String, fullName = "modules", description = "main module file(s) to compile").multiple(999)
try { try {
@ -84,6 +84,8 @@ private fun compileMain(args: Array<String>): Boolean {
return runVm(moduleFiles.first()) return runVm(moduleFiles.first())
} }
val processedSymbols = processSymbolDefs(symbolDefs) ?: return false
if(watchMode==true) { if(watchMode==true) {
val watchservice = FileSystems.getDefault().newWatchService() val watchservice = FileSystems.getDefault().newWatchService()
val allImportedFiles = mutableSetOf<Path>() val allImportedFiles = mutableSetOf<Path>()
@ -104,6 +106,7 @@ private fun compileMain(args: Array<String>): Boolean {
asmListfile == true, asmListfile == true,
experimentalCodegen == true, experimentalCodegen == true,
compilationTarget, compilationTarget,
processedSymbols,
srcdirs, srcdirs,
outputPath outputPath
) )
@ -156,6 +159,7 @@ private fun compileMain(args: Array<String>): Boolean {
asmListfile == true, asmListfile == true,
experimentalCodegen == true, experimentalCodegen == true,
compilationTarget, compilationTarget,
processedSymbols,
srcdirs, srcdirs,
outputPath outputPath
) )
@ -193,6 +197,21 @@ private fun compileMain(args: Array<String>): Boolean {
return true return true
} }
private fun processSymbolDefs(symbolDefs: List<String>): Map<String, String>? {
val result = mutableMapOf<String, String>()
val defPattern = """(.+)\s*=\s*(.+)""".toRegex()
for(def in symbolDefs) {
val match = defPattern.matchEntire(def.trim())
if(match==null) {
System.err.println("invalid symbol definition (expected NAME=VALUE): $def")
return null
}
val (_, name, value) = match.groupValues
result[name.trim()] = value.trim()
}
return result
}
fun runVm(listingFilename: String): Boolean { fun runVm(listingFilename: String): Boolean {
val name = val name =
if(listingFilename.endsWith(".p8virt")) if(listingFilename.endsWith(".p8virt"))

View File

@ -38,6 +38,7 @@ class CompilerArguments(val filepath: Path,
val asmListfile: Boolean, val asmListfile: Boolean,
val experimentalCodegen: Boolean, val experimentalCodegen: Boolean,
val compilationTarget: String, val compilationTarget: String,
val symbolDefs: Map<String, String>,
val sourceDirs: List<String> = emptyList(), val sourceDirs: List<String> = emptyList(),
val outputDir: Path = Path(""), val outputDir: Path = Path(""),
val errors: IErrorReporter = ErrorReporter()) val errors: IErrorReporter = ErrorReporter())
@ -78,6 +79,7 @@ fun compileProgram(args: CompilerArguments): CompilationResult? {
asmListfile = args.asmListfile asmListfile = args.asmListfile
experimentalCodegen = args.experimentalCodegen experimentalCodegen = args.experimentalCodegen
outputDir = args.outputDir.normalize() outputDir = args.outputDir.normalize()
symbolDefs = args.symbolDefs
} }
program = programresult program = programresult
importedFiles = imported importedFiles = imported

View File

@ -37,6 +37,7 @@ private fun compileTheThing(filepath: Path, optimize: Boolean, target: ICompilat
asmListfile = false, asmListfile = false,
experimentalCodegen = false, experimentalCodegen = false,
compilationTarget = target.name, compilationTarget = target.name,
symbolDefs = emptyMap(),
outputDir = outputDir outputDir = outputDir
) )
return compileProgram(args) return compileProgram(args)

View File

@ -51,6 +51,7 @@ class TestCompilerOptionSourcedirs: FunSpec({
asmListfile = false, asmListfile = false,
experimentalCodegen = false, experimentalCodegen = false,
compilationTarget = Cx16Target.NAME, compilationTarget = Cx16Target.NAME,
symbolDefs = emptyMap(),
sourceDirs, sourceDirs,
outputDir outputDir
) )

View File

@ -37,6 +37,7 @@ internal fun compileFile(
asmListfile = false, asmListfile = false,
experimentalCodegen = false, experimentalCodegen = false,
platform.name, platform.name,
symbolDefs = emptyMap(),
outputDir = outputDir, outputDir = outputDir,
errors = errors ?: ErrorReporterForTests() errors = errors ?: ErrorReporterForTests()
) )

View File

@ -38,6 +38,7 @@ class RequestParser : Take {
writeAssembly = true, writeAssembly = true,
slowCodegenWarnings = true, slowCodegenWarnings = true,
compilationTarget = "c64", compilationTarget = "c64",
symbolDefs = emptyMap(),
quietAssembler = false, quietAssembler = false,
asmListfile = false, asmListfile = false,
experimentalCodegen = false experimentalCodegen = false