Skip to content

Commit

Permalink
refactor: load modules once
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Nov 3, 2023
1 parent 2b8516f commit 4693a1d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/beize_compiler/example/project/main.beize
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "./benchmark.beize" as benchmark;
import "./benchmark.beize" as benchmarask;

benchmarker := benchmark.createBenchmarker();
benchmarker.start();
Expand Down
3 changes: 2 additions & 1 deletion packages/beize_compiler/lib/compiler/compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ class BeizeCompiler {
compiler.prepare(isAsync: true);
final int nameIndex =
compiler.makeConstant(compiler.resolveRelativePath(fullPath));
compiler.makeConstant(compiler.currentFunction);
final int functionIndex = compiler.makeConstant(compiler.currentFunction);
compiler.modules.add(nameIndex);
compiler.modules.add(functionIndex);
await compiler.compile();
return BeizeProgramConstant(
modules: compiler.modules,
Expand Down
8 changes: 5 additions & 3 deletions packages/beize_compiler/lib/compiler/parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ abstract class BeizeParser {
compiler.resolveImportPath(compiler.previousToken.literal as String);
final String moduleName = compiler.resolveRelativePath(modulePath);
int moduleIndex = -1;
for (int i = 0; i < compiler.modules.length; i++) {
for (int i = 0; i < compiler.modules.length; i += 2) {
final int x = compiler.modules[i];
if (compiler.constants[x] == moduleName) {
moduleIndex = x;
moduleIndex = i;
}
}
compiler.consume(BeizeTokens.asKw);
Expand All @@ -229,8 +229,10 @@ abstract class BeizeParser {
modulePath,
isAsync: false,
);
compiler.makeConstant(moduleCompiler.currentFunction);
final int functionIndex =
compiler.makeConstant(moduleCompiler.currentFunction);
compiler.modules.add(nameIndex);
compiler.modules.add(functionIndex);
await moduleCompiler.compile();
}
compiler.emitCode(moduleIndex);
Expand Down
11 changes: 7 additions & 4 deletions packages/beize_compiler/lib/disassembler/disassembler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class BeizeDisassembler {

case BeizeOpCodes.opImport:
final int moduleIndex = chunk.codeAt(ip + 1);
final int nameIndex = program.moduleAt(moduleIndex);
final int asIndex = chunk.codeAt(ip + 2);
final int nameIndex = program.modules[moduleIndex];
final String moduleName = program.constantAt(nameIndex) as String;
final BeizeConstant importName = program.constantAt(asIndex);
writeInstruction(
Expand Down Expand Up @@ -124,11 +124,14 @@ class BeizeDisassembler {

static void disassembleProgram(final BeizeProgramConstant program) {
final BeizeDisassemblerOutput output = BeizeDisassemblerConsoleOutput();
for (final int nameIndex in program.modules) {
for (int i = 0; i < program.modules.length; i += 2) {
final int nameIndex = program.modules[i];
final int functionIndex = program.modules[i + 1];
final String moduleName = program.constantAt(nameIndex) as String;
final BeizeFunctionConstant function =
program.constantAt(nameIndex + 1) as BeizeFunctionConstant;
output.write('> $moduleName ${nameIndex == 0 ? "(entrypoint)" : ""}');
program.constantAt(functionIndex) as BeizeFunctionConstant;
output.write(
'> $moduleName (constant [$nameIndex]) at constant [$functionIndex] ${nameIndex == 0 ? "(entrypoint)" : ""}');
final BeizeDisassembler disassembler =
BeizeDisassembler(program, function.chunk, output);
disassembler.dissassemble();
Expand Down
21 changes: 11 additions & 10 deletions packages/beize_compiler/tests/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,23 @@ class BeizeTestProgram {

BeizeTestChunk addModule(
final int moduleIndex,
final int nameIndex,
final int moduleNameIndex,
final String moduleName,
final int functionIndex,
) {
int missingCount = moduleIndex - modules.length + 1;
while (missingCount > 0) {
modules.add(0);
missingCount--;
}
modules[moduleIndex] = nameIndex;
modules[moduleIndex] = moduleNameIndex;
final BeizeTestChunk chunk = BeizeTestChunk(
this,
BeizeChunk.empty(moduleIndex),
);
addConstant(nameIndex, moduleName);
addConstant(moduleNameIndex, moduleName);
addConstant(
nameIndex + 1,
functionIndex,
BeizeFunctionConstant(
isAsync: true,
arguments: <int>[],
Expand Down Expand Up @@ -104,13 +105,14 @@ String buildExpectedProgramCode(
final StringBuffer output = buffer ?? StringBuffer();
output.write('final BeizeTestProgram expectedProgram = BeizeTestProgram();');
for (int i = 0; i < program.modules.length; i++) {
final int nameIndex = program.moduleAt(i);
final String moduleName = program.constantAt(i) as String;
final int moduleNameIndex = program.modules[i];
final int functionIndex = program.modules[i + 1];
final String moduleName = program.constantAt(moduleNameIndex) as String;
final BeizeFunctionConstant function =
program.constantAt(nameIndex + 1) as BeizeFunctionConstant;
program.constantAt(functionIndex) as BeizeFunctionConstant;
final String variableName = 'expectedModule$i';
output.write(
"final BeizeTestChunk $variableName = expectedProgram.addModule($i, $nameIndex, '${escapedString(moduleName, "'")}');",
"final BeizeTestChunk $variableName = expectedProgram.addModule($i, $moduleNameIndex, '${escapedString(moduleName, "'")}', $functionIndex);",
);
buildExpectedChunkCode(
program,
Expand Down Expand Up @@ -188,8 +190,7 @@ String buildExpectedChunkCode(
case BeizeOpCodes.opImport:
final int moduleIndex = chunk.codeAt(ip + 1);
final int asIndex = chunk.codeAt(ip + 2);
final int nameIndex = program.moduleAt(moduleIndex);
final String name = program.constantAt(nameIndex) as String;
final String name = program.constantAt(asIndex) as String;
output.writeln('$variableName.addCode($moduleIndex);');
output.writeln("$variableName.addConstant($asIndex, '$name');");
bump += 2;
Expand Down

0 comments on commit 4693a1d

Please sign in to comment.