prog8/compiler/test/helpers/compileXyz.kt

84 lines
2.8 KiB
Kotlin
Raw Normal View History

package prog8tests.helpers
import prog8.ast.Program
2022-03-10 23:08:41 +01:00
import prog8.code.core.*
2022-03-11 20:35:25 +01:00
import prog8.code.target.C64Target
import prog8.code.target.c64.C64Zeropage
2022-02-06 22:56:17 +01:00
import prog8.codegen.cpu6502.AsmGen
2021-10-29 05:28:02 +02:00
import prog8.compiler.CompilationResult
2021-11-30 01:40:21 +01:00
import prog8.compiler.CompilerArguments
2022-03-04 23:25:26 +01:00
import prog8.compiler.astprocessing.SymbolTableMaker
2021-10-29 05:28:02 +02:00
import prog8.compiler.compileProgram
2022-03-13 12:52:12 +01:00
import prog8.compiler.determineProgramLoadAddress
2021-10-11 00:22:04 +02:00
import java.nio.file.Path
import kotlin.io.path.name
internal fun compileFile(
platform: ICompilationTarget,
optimize: Boolean,
fileDir: Path,
fileName: String,
outputDir: Path = prog8tests.helpers.Helpers.outputDir,
errors: IErrorReporter? = null,
writeAssembly: Boolean = true,
optFloatExpr: Boolean = true
2022-03-07 21:41:12 +01:00
) : CompilationResult? {
val filepath = fileDir.resolve(fileName)
Helpers.assumeReadableFile(filepath)
2021-11-30 01:40:21 +01:00
val args = CompilerArguments(
filepath,
optimize,
optimizeFloatExpressions = optFloatExpr,
2022-01-01 16:35:36 +01:00
dontReinitGlobals = false,
writeAssembly = writeAssembly,
slowCodegenWarnings = false,
quietAssembler = true,
asmListfile = false,
experimentalCodegen = false,
platform.name,
2021-11-30 01:40:21 +01:00
outputDir = outputDir,
2021-10-29 16:46:56 +02:00
errors = errors ?: ErrorReporterForTests()
)
2021-11-30 01:40:21 +01:00
return compileProgram(args)
}
/**
* Takes a [sourceText] as a String, writes it to a temporary
* file and then runs the compiler on that.
* @see compileFile
*/
internal fun compileText(
platform: ICompilationTarget,
optimize: Boolean,
sourceText: String,
errors: IErrorReporter? = null,
writeAssembly: Boolean = true,
optFloatExpr: Boolean = true
2022-03-07 21:41:12 +01:00
) : CompilationResult? {
val filePath = Helpers.outputDir.resolve("on_the_fly_test_" + sourceText.hashCode().toUInt().toString(16) + ".p8")
// we don't assumeNotExists(filePath) - should be ok to just overwrite it
filePath.toFile().writeText(sourceText)
return compileFile(platform, optimize, filePath.parent, filePath.name, errors=errors, writeAssembly=writeAssembly, optFloatExpr = optFloatExpr)
}
internal fun generateAssembly(
program: Program,
options: CompilationOptions? = null
): IAssemblyProgram? {
2022-03-13 12:52:12 +01:00
val coptions = options ?: CompilationOptions(OutputType.RAW, CbmPrgLauncherType.BASIC, ZeropageType.DONTUSE, emptyList(),
floats = true,
noSysInit = true,
compTarget = C64Target(),
loadAddress = 0u, outputDir = Helpers.outputDir)
coptions.compTarget.machine.zeropage = C64Zeropage(coptions)
2022-03-04 23:25:26 +01:00
val st = SymbolTableMaker().makeFrom(program)
2022-03-13 12:52:12 +01:00
val errors = ErrorReporterForTests()
determineProgramLoadAddress(program, coptions, errors)
errors.report()
val asmgen = AsmGen(program, st, coptions, errors)
2022-03-13 12:52:12 +01:00
errors.report()
return asmgen.compileToAssembly()
}