From 552cb8ead6a3d780b4a179539e03cb9d3a38b48c Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Fri, 3 Jun 2022 14:44:12 -0400 Subject: [PATCH] Continued Work On Linker --- Resources/CodeGen.br | 6 +-- Resources/CodeGen.wasm | Bin 457 -> 337 bytes Resources/Linker.br | 3 ++ Resources/Linker.wasm | Bin 0 -> 339 bytes Resources/runtime/constants.br | 5 ++- Resources/runtime/constants.wasm | Bin 188 -> 228 bytes src/Linker/index.ts | 41 ++++++++++++++------ src/cli/index.ts | 3 +- test.wat | 64 +++++++++++++++++++++++++++---- 9 files changed, 97 insertions(+), 25 deletions(-) create mode 100644 Resources/Linker.br create mode 100644 Resources/Linker.wasm diff --git a/Resources/CodeGen.br b/Resources/CodeGen.br index 3ebbbee..e694664 100644 --- a/Resources/CodeGen.br +++ b/Resources/CodeGen.br @@ -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 diff --git a/Resources/CodeGen.wasm b/Resources/CodeGen.wasm index f42cc7822684ee9ccf57aad5bb79fa6c604f94cd..8865b2c506ffb857ece3f0776770e024fb00a0aa 100644 GIT binary patch literal 337 zcmXYsO;5r=7=&luEvPgGP~!)uMZD+%64bk^37))Y;#CT4NR{rAc5C$1KkE;%h?mJ^ z=E)?3q{s+>Z8c@ZYAV%60okXr?z6GZQx^agDMF%%Yp6zT+3cta2Ju!{yPX!BnazCu zE!d^nJ5-y#9yg3h7>OyZS9M8Nni^f8FPet$eQGhBM0XmR1L+dI*svX+L1Vz0>uH!D z)PO`S_INhot$AA^frl3AP_4@b|o@MS)?#puB= z?4xzVScV literal 457 zcmaKpy-ve06ov0iQWuh~;ztZ@nG;~_29;nbLV^bXH@z)Zrx5@!!8d^D6O+@KRcjPEihVJ&qFr1Cok(#C!Saj0coo9a5Km%X}6sJ09)7_)bh z>+ZFiV2&e%L5zSx;0A6X8>T3_)Ci6c(gY*O;0sS${KpphMJ-wS#u7k0XsJejaSs&f zShJ$+@&9R*?m^G?>A$NSE(%4$QWKnq+t1`oV=Hpy#LVe|I1B=h5yWfw8|$Mm0Qd$p CERFpD diff --git a/Resources/Linker.br b/Resources/Linker.br new file mode 100644 index 0000000..e63f5c1 --- /dev/null +++ b/Resources/Linker.br @@ -0,0 +1,3 @@ +import True from './runtime/constants.br'; +import wasm print: (i32) => i32 from 'env'; +print(True); \ No newline at end of file diff --git a/Resources/Linker.wasm b/Resources/Linker.wasm new file mode 100644 index 0000000000000000000000000000000000000000..271da952276e3648daeeb54888a446d111ef8b0e GIT binary patch literal 339 zcmaKoF;BxV5QX2_NlFt2kZ1=are;nUn7RQH69}oW(=@o1Rcu#h8;L1@og|bEv7B^w zPj}yYXuKf+G!<*=s9OVqy5>7GWW7rnDhh^%^N$?$OgH_ZR2xJhhd6y~$4 dUMcS)x60Q;DvEn2bSCg)fU>tfAr3+k;T!#YT|WQ- literal 0 HcmV?d00001 diff --git a/Resources/runtime/constants.br b/Resources/runtime/constants.br index 232ba0b..4261769 100644 --- a/Resources/runtime/constants.br +++ b/Resources/runtime/constants.br @@ -1 +1,4 @@ -export const heapOffset: i32 = 0n; \ No newline at end of file +// export const heapOffset: i32 = 0n; +import wasm print: (f32) => i32 from 'env'; +print(1f); +export const True: i32 = 1n; \ No newline at end of file diff --git a/Resources/runtime/constants.wasm b/Resources/runtime/constants.wasm index a32a268ba085e1f864d2f64b0c41bb15609dc5dc..61e5f671d27b24846b9b998c08004f1cc1ab97c1 100644 GIT binary patch literal 228 zcmYk0F^j@L6ohBq#zZd&ilWE0m)2NWi&|T0uhCnMaJQSV*$5V*Ki#N}&G0eI41=sP z1i+5$dEs>rNS8|6Q>%2j^?(vG2v~wGMKJ8CD;9`f!iJ(xiIb(vtG{IHYON$R+7#|A zXp^0Yk2LWyU>_^sHvet_UkfG@V={ae*Tql$uQ&Ox&3x@NAw=*P!}~HY3v6aI#-nuV dYU^{Or^mupKC`|`4~{*->o&t(<2o;j#2t2;FYy2X delta 144 zcmaFDxQ8*AA+b1@k%57Mk%cjVfq|Kck%5(&k%5too2j0`k%7CO5lFDhF$t7 UJU=HR16zD?Nn%k66VOfu0Qr+2JOBUy diff --git a/src/Linker/index.ts b/src/Linker/index.ts index 0987205..fcdcd37 100644 --- a/src/Linker/index.ts +++ b/src/Linker/index.ts @@ -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, @@ -15,7 +16,7 @@ import { // addImport, // addMemory, // addType, - // compileModule, + compileModule, // createCustomSection, // createFunctionImport, // createGlobalImport, @@ -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 { + // 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 { // 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; } @@ -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; diff --git a/src/cli/index.ts b/src/cli/index.ts index f48482e..c5c015c 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -61,7 +61,8 @@ program.argument('', '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); }); diff --git a/test.wat b/test.wat index b060768..4115a57 100644 --- a/test.wat +++ b/test.wat @@ -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))