2021-07-17 08:37:58 +00:00
|
|
|
package prog8tests.helpers
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
import io.kotest.assertions.withClue
|
|
|
|
import io.kotest.matchers.shouldBe
|
2021-11-09 02:45:07 +00:00
|
|
|
import prog8.ast.Program
|
2022-02-06 21:56:17 +00:00
|
|
|
import prog8.codegen.cpu6502.AsmGen
|
2022-02-05 02:50:54 +00:00
|
|
|
import prog8.codegen.target.C64Target
|
|
|
|
import prog8.codegen.target.c64.C64Zeropage
|
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-11-09 02:45:07 +00:00
|
|
|
import prog8.compilerinterface.*
|
2021-10-10 22:22:04 +00:00
|
|
|
import java.nio.file.Path
|
|
|
|
import kotlin.io.path.name
|
2021-07-17 08:37:58 +00:00
|
|
|
|
2021-07-30 15:39:43 +00:00
|
|
|
|
2021-07-17 08:37:58 +00:00
|
|
|
internal fun CompilationResult.assertSuccess(description: String = ""): CompilationResult {
|
2021-11-07 23:16:58 +00:00
|
|
|
withClue("expected successful compilation but failed $description") {
|
|
|
|
success shouldBe true
|
|
|
|
}
|
2021-07-17 08:37:58 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
internal fun CompilationResult.assertFailure(description: String = ""): CompilationResult {
|
2021-11-07 23:16:58 +00:00
|
|
|
withClue("expected failure to compile but succeeded $description") {
|
|
|
|
success shouldBe false
|
|
|
|
}
|
2021-07-17 08:37:58 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see CompilationResult.assertSuccess
|
|
|
|
* @see CompilationResult.assertFailure
|
|
|
|
*/
|
|
|
|
internal fun compileFile(
|
|
|
|
platform: ICompilationTarget,
|
|
|
|
optimize: Boolean,
|
|
|
|
fileDir: Path,
|
|
|
|
fileName: String,
|
2021-12-04 17:20:22 +00:00
|
|
|
outputDir: Path = prog8tests.helpers.outputDir,
|
2021-10-21 23:25:26 +00:00
|
|
|
errors: IErrorReporter? = null,
|
2021-11-09 02:45:07 +00:00
|
|
|
writeAssembly: Boolean = true,
|
|
|
|
optFloatExpr: Boolean = true
|
2021-07-17 08:37:58 +00:00
|
|
|
) : CompilationResult {
|
|
|
|
val filepath = fileDir.resolve(fileName)
|
|
|
|
assumeReadableFile(filepath)
|
2021-11-30 00:40:21 +00:00
|
|
|
val args = CompilerArguments(
|
2021-07-17 08:37:58 +00:00
|
|
|
filepath,
|
|
|
|
optimize,
|
2021-11-09 02:45:07 +00:00
|
|
|
optimizeFloatExpressions = optFloatExpr,
|
2022-01-01 15:35:36 +00:00
|
|
|
dontReinitGlobals = false,
|
2021-10-21 23:25:26 +00:00
|
|
|
writeAssembly = writeAssembly,
|
2021-07-17 08:37:58 +00:00
|
|
|
slowCodegenWarnings = false,
|
2021-10-30 13:26:40 +00:00
|
|
|
quietAssembler = true,
|
2021-12-30 13:38:40 +00:00
|
|
|
asmListfile = false,
|
2022-02-05 20:25:19 +00:00
|
|
|
experimentalCodegen = false,
|
2021-07-17 08:37:58 +00:00
|
|
|
platform.name,
|
2021-11-30 00:40:21 +00:00
|
|
|
outputDir = outputDir,
|
2021-10-29 14:46:56 +00:00
|
|
|
errors = errors ?: ErrorReporterForTests()
|
2021-07-17 08:37:58 +00:00
|
|
|
)
|
2021-11-30 00:40:21 +00:00
|
|
|
return compileProgram(args)
|
2021-07-17 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2021-10-21 23:25:26 +00:00
|
|
|
sourceText: String,
|
|
|
|
errors: IErrorReporter? = null,
|
2021-11-09 02:45:07 +00:00
|
|
|
writeAssembly: Boolean = true,
|
|
|
|
optFloatExpr: Boolean = true
|
2021-07-17 08:37:58 +00:00
|
|
|
) : 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)
|
2021-11-09 02:45:07 +00:00
|
|
|
return compileFile(platform, optimize, filePath.parent, filePath.name, errors=errors, writeAssembly=writeAssembly, optFloatExpr = optFloatExpr)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal fun generateAssembly(
|
|
|
|
program: Program,
|
2022-02-09 14:45:47 +00:00
|
|
|
variables: IVariablesAndConsts,
|
2021-11-09 02:45:07 +00:00
|
|
|
options: CompilationOptions? = null
|
2022-02-05 20:25:19 +00:00
|
|
|
): IAssemblyProgram? {
|
2022-02-26 14:36:22 +00:00
|
|
|
val coptions = options ?: CompilationOptions(OutputType.RAW, CbmPrgLauncherType.BASIC, ZeropageType.DONTUSE, emptyList(), true, true, C64Target(), outputDir = outputDir)
|
2022-02-05 20:25:19 +00:00
|
|
|
coptions.compTarget.machine.zeropage = C64Zeropage(coptions)
|
2022-02-09 14:45:47 +00:00
|
|
|
val asmgen = AsmGen(program, ErrorReporterForTests(), variables, coptions)
|
2021-11-09 02:45:07 +00:00
|
|
|
return asmgen.compileToAssembly()
|
2021-07-17 08:37:58 +00:00
|
|
|
}
|