Skip to content

Commit

Permalink
Continued Work On Linker
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Jun 3, 2022
1 parent 8fa5c9e commit 552cb8e
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 25 deletions.
6 changes: 3 additions & 3 deletions Resources/CodeGen.br
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import print from './runtime/print.br';
import malloc from './memory.br';
import heapOffset from './runtime/constants.br';
// import print from './runtime/print.br';
// import malloc from './memory.br';
// import heapOffset from './runtime/constants.br';
// import wasm print: (i32) => i32 from 'env';
// import wasm fd_write: (i32, i32, i32, i32) => i32 from 'wasi_snapshot_preview1';
// // Write Hello World
Expand Down
Binary file modified Resources/CodeGen.wasm
Binary file not shown.
3 changes: 3 additions & 0 deletions Resources/Linker.br
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import True from './runtime/constants.br';
import wasm print: (i32) => i32 from 'env';
print(True);
Binary file added Resources/Linker.wasm
Binary file not shown.
5 changes: 4 additions & 1 deletion Resources/runtime/constants.br
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const heapOffset: i32 = 0n;
// export const heapOffset: i32 = 0n;
import wasm print: (f32) => i32 from 'env';
print(1f);
export const True: i32 = 1n;
Binary file modified Resources/runtime/constants.wasm
Binary file not shown.
41 changes: 29 additions & 12 deletions src/Linker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
createBaseUnionType,
createPrimType,
} from '../Compiler/Helpers/typeBuilders';
import { WasmModule } from '../wasmBuilder/Types/Nodes';
import { WasmFunction, WasmModule } from '../wasmBuilder/Types/Nodes';
import { createFunction } from '../wasmBuilder/Build/Function';
import {
// addElement,
// addExport,
Expand All @@ -15,7 +16,7 @@ import {
// addImport,
// addMemory,
// addType,
// compileModule,
compileModule,
// createCustomSection,
// createFunctionImport,
// createGlobalImport,
Expand Down Expand Up @@ -123,31 +124,49 @@ class FileDecoder extends Decoder {
if (!foundSignature) throw 'Cannot Find Module Signature';
}
// Link Function
private async linkFunction(
globalOffset: number,
functionOffset: number,
func: number[]
): Promise<WasmFunction> {
// Get Names From Parent Function
// TODO: Remap Globals
// TODO: Remap Calls
// const func = createFunction();
// TODO: Return The Function
return createFunction(functionOffset);
}
// Link Function
public async link(filePath: string): Promise<WasmModule> {
// Collect The Dependency's Below
const depTree = await this.collectDependencyTree(filePath, new Map());
depTree.set(filePath, this);
// Variables Needed For Linking
let tableSize = 0;
let elementCount = 0;
let globalCount = 0;
let functionCount = 1; // Set to 1 for the New Main Function
// For Each Module
for (const [importPath, briskImport] of depTree) {
// TODO: Combine The Function Tables
if (briskImport.tableSection.length != 0) {
console.log(briskImport.tableSection);
const tableDecoder = new Decoder(briskImport.tableSection, 1);
tableDecoder.getCurrentIndex();
tableDecoder.getCurrentIndex();
tableSize += tableDecoder.decodeUnSignedLeb128();
elementCount += tableDecoder.decodeUnSignedLeb128();
}
// TODO: Move The Table Elements
// TODO: Set The Function Offset
// TODO: Resolve Imports
// TODO: Rename The Globals And Functions
// TODO: For Each Function
// TODO: Remap The Function Calls
// TODO: Remap The Global Calls
// TODO: Remap Each Function
// TODO: Move Over Data Sections
// Combine The Other Parts
// TODO: Fix Name Section
// TODO: Remap Name Function
}
// TODO: Create New Table Section
// TODO: Create The New Main Function And Call The Other Main Functions
// TODO: Set The Main Function To The Start
// TODO: Reexport the entry's exports
// Return Module
return this.wasmModule;
}
Expand Down Expand Up @@ -292,9 +311,7 @@ const Link = async (filePath: string) => {
// Decode File
const decodedFile = new FileDecoder(Array.from(wasmBinary));
// Perform Linking
await decodedFile.link(filePath);
// console.log(decodedFile);
return false;
return compileModule(await decodedFile.link(filePath), filePath, true);
};
// Export Linker
export default Link;
3 changes: 2 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ program.argument('<file>', 'File to compile').action(async (filePath: string) =>
console.log('================================================================');
console.dir(output, { depth: null });
// Link
await Link(compiledPath);
const linked = await Link(compiledPath);
await fs.writeFile(compiledPath, linked);
// Run
Runner(output);
});
Expand Down
64 changes: 56 additions & 8 deletions test.wat
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
(module $./runtime/constants.br
(type (;0;) (func))
(func $_start (type 0)
(global.set 1
(i32.const 0)))
(module $./Resources/CodeGen.br
(type (;0;) (func (param i32) (result i32)))
(type (;1;) (func (param i32 i32) (result i32)))
(type (;2;) (func))
(import "env" "print" (func $print (type 0)))
(func $loop (type 1) (param $t2 i32) (param $index3 i32) (result i32)
(local $test4 i32)
(local.set $test4
(call_indirect (type 0)
(local.get $index3)
(local.get $t2)))
(local.set $index3
(i32.add
(local.get $index3)
(i32.const 1)))
(if ;; label = @1
(i32.eq
(local.get $test4)
(i32.const 1))
(then
(drop
(call_indirect (type 1)
(local.get $t2)
(local.get $index3)
(global.get 2)))))
(return
(i32.const 1)))
(func $AmbiguousFunction2 (type 0) (param $index5 i32) (result i32)
(drop
(call_indirect (type 0)
(local.get $index5)
(global.get 1)))
(if ;; label = @1
(i32.eq
(local.get $index5)
(i32.const 100))
(then
(return
(i32.const 0))))
(return
(i32.const 1)))
(func $_start (type 2)
(global.set 2
(i32.const 1))
(drop
(call_indirect (type 1)
(i32.const 2)
(i32.const 0)
(global.get 2))))
(table (;0;) 3 funcref)
(memory (;0;) 1)
(global (;0;) i32 (i32.const 0))
(global (;1;) (mut i32) (i32.const 0))
(export "$Brisk$heapOffset" (global 1))
(global (;1;) i32 (i32.const 0))
(global (;2;) (mut i32) (i32.const 0))
(export "memory" (memory 0))
(start $_start))
(start $_start)
(elem (;0;) (i32.const 0) func $print)
(elem (;1;) (i32.const 1) func $loop)
(elem (;2;) (i32.const 2) func $AmbiguousFunction2))

0 comments on commit 552cb8e

Please sign in to comment.