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 asmListfile: 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_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) {
OutputType.RAW -> {
asmgen.out("; ---- raw assembler program ----")

View File

@ -144,6 +144,15 @@ class AstToXmlConverter(internal val program: PtProgram,
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()
}

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()) {
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 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 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)
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)
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 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)
try {
@ -84,6 +84,8 @@ private fun compileMain(args: Array<String>): Boolean {
return runVm(moduleFiles.first())
}
val processedSymbols = processSymbolDefs(symbolDefs) ?: return false
if(watchMode==true) {
val watchservice = FileSystems.getDefault().newWatchService()
val allImportedFiles = mutableSetOf<Path>()
@ -104,6 +106,7 @@ private fun compileMain(args: Array<String>): Boolean {
asmListfile == true,
experimentalCodegen == true,
compilationTarget,
processedSymbols,
srcdirs,
outputPath
)
@ -156,6 +159,7 @@ private fun compileMain(args: Array<String>): Boolean {
asmListfile == true,
experimentalCodegen == true,
compilationTarget,
processedSymbols,
srcdirs,
outputPath
)
@ -193,6 +197,21 @@ private fun compileMain(args: Array<String>): Boolean {
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 {
val name =
if(listingFilename.endsWith(".p8virt"))

View File

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

View File

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

View File

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

View File

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

View File

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