prog8/compiler/test/helpers/compileXyz.kt

93 lines
3.0 KiB
Kotlin
Raw Normal View History

package prog8tests.helpers
import io.kotest.assertions.withClue
import io.kotest.matchers.shouldBe
import prog8.ast.Program
2021-10-29 03:28:02 +00:00
import prog8.compiler.CompilationResult
2021-11-30 00:40:21 +00:00
import prog8.compiler.CompilerArguments
2021-10-29 03:28:02 +00:00
import prog8.compiler.compileProgram
2021-12-28 13:23:36 +00:00
import prog8.codegen.target.C64Target
import prog8.codegen.target.c64.C64Zeropage
import prog8.codegen.target.cpu6502.codegen.AsmGen
import prog8.compilerinterface.*
2021-10-10 22:22:04 +00:00
import java.nio.file.Path
import kotlin.io.path.name
internal fun CompilationResult.assertSuccess(description: String = ""): CompilationResult {
withClue("expected successful compilation but failed $description") {
success shouldBe true
}
return this
}
internal fun CompilationResult.assertFailure(description: String = ""): CompilationResult {
withClue("expected failure to compile but succeeded $description") {
success shouldBe false
}
return this
}
/**
* @see CompilationResult.assertSuccess
* @see CompilationResult.assertFailure
*/
internal fun compileFile(
platform: ICompilationTarget,
optimize: Boolean,
fileDir: Path,
fileName: String,
outputDir: Path = prog8tests.helpers.outputDir,
errors: IErrorReporter? = null,
writeAssembly: Boolean = true,
optFloatExpr: Boolean = true
) : CompilationResult {
val filepath = fileDir.resolve(fileName)
assumeReadableFile(filepath)
2021-11-30 00:40:21 +00:00
val args = CompilerArguments(
filepath,
optimize,
optimizeFloatExpressions = optFloatExpr,
2022-01-01 15:35:36 +00:00
dontReinitGlobals = false,
writeAssembly = writeAssembly,
slowCodegenWarnings = false,
quietAssembler = true,
asmListfile = false,
platform.name,
2021-11-30 00:40:21 +00:00
outputDir = outputDir,
2021-10-29 14:46:56 +00:00
errors = errors ?: ErrorReporterForTests()
)
2021-11-30 00:40:21 +00: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
) : CompilationResult {
val filePath = 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 {
val coptions = options ?: CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.DONTUSE, emptyList(), true, true, C64Target)
2021-12-04 17:59:49 +00:00
val zp = C64Zeropage(coptions)
coptions.compTarget.machine.zeropage=zp
val asmgen = AsmGen(program, ErrorReporterForTests(), zp, coptions, C64Target, outputDir)
return asmgen.compileToAssembly()
}