diff --git a/Resources/LinkTest/index.br b/Resources/LinkTest/index.br new file mode 100644 index 0000000..337d60c --- /dev/null +++ b/Resources/LinkTest/index.br @@ -0,0 +1,3 @@ +import print32 from './print32.br'; +print32(0n); +print32(1n); \ No newline at end of file diff --git a/Resources/LinkTest/index.br.wasm b/Resources/LinkTest/index.br.wasm new file mode 100644 index 0000000..7f08af8 Binary files /dev/null and b/Resources/LinkTest/index.br.wasm differ diff --git a/Resources/LinkTest/print32.br b/Resources/LinkTest/print32.br new file mode 100644 index 0000000..bca80dd --- /dev/null +++ b/Resources/LinkTest/print32.br @@ -0,0 +1,30 @@ +import wasm fd_write: (i32, i32, i32, i32) => i32 from 'wasi_snapshot_preview1'; +// TODO: Implement toString32 +export const print32: Function = (value: i32): Void => { + // Helper + let writeCharacterCode: Function = (let pointer: i32, character: i32): i32 => { + // Store Character + @wasm.i32.store(pointer, character); + // Add To The Index + pointer++; + // Return Pointer + return(pointer); + }; + // Create String + const basePointer: i32 = 0n; + let pointer: i32 = @wasm.i32.add(basePointer, 8n); + let length: i32 = 0n; + // Handle String Conversion In toString + if (@wasm.i32.eqz(value)) { + pointer = writeCharacterCode(pointer, 48n); + length++; + }; + // Write New Line + pointer = writeCharacterCode(pointer, 10n); + length++; + // Store Iov Info + @wasm.i32.store(basePointer, 8n); + @wasm.i32.store(@wasm.i32.add(basePointer, 4n), length); + // Write String + fd_write(1n, 0n, 1n, 40n); +}; \ No newline at end of file diff --git a/Resources/LinkTest/print32.br.wasm b/Resources/LinkTest/print32.br.wasm new file mode 100644 index 0000000..afa415c Binary files /dev/null and b/Resources/LinkTest/print32.br.wasm differ diff --git a/Resources/LinkerTest/Linker.br.wasm b/Resources/LinkerTest/Linker.br.wasm index 5b99c19..e52211c 100644 Binary files a/Resources/LinkerTest/Linker.br.wasm and b/Resources/LinkerTest/Linker.br.wasm differ diff --git a/Resources/LinkerTest/constants.br.wasm b/Resources/LinkerTest/constants.br.wasm index 4432948..b27d4c1 100644 Binary files a/Resources/LinkerTest/constants.br.wasm and b/Resources/LinkerTest/constants.br.wasm differ diff --git a/src/Compiler/Codegen/index.ts b/src/Compiler/Codegen/index.ts index e6b24d8..f52e9ab 100644 --- a/src/Compiler/Codegen/index.ts +++ b/src/Compiler/Codegen/index.ts @@ -522,17 +522,6 @@ const generateCodeProgram = (rawProgram: string, program: ProgramNode): Uint8Arr Types.createNumericType(WasmTypes.WasmI32), Expressions.i32_ConstExpression(0) ); - if (program.data._imports.size != 0) { - addImport( - wasmModule, - createGlobalImport( - 'brisk:LinkingConstant', - brisk_moduleFunctionOffset, - Types.createNumericType(WasmTypes.WasmI32), - false - ) - ); - } // Create Function let func = createFunction('_start', Types.createFunctionType([], []), [], [], []); // Build The Body diff --git a/src/wasmBuilder/Build/WasmModule.ts b/src/wasmBuilder/Build/WasmModule.ts index ed6e91c..1616669 100644 --- a/src/wasmBuilder/Build/WasmModule.ts +++ b/src/wasmBuilder/Build/WasmModule.ts @@ -137,7 +137,16 @@ export const addFunction = (wasmModule: WasmModule, func: WasmFunction): WasmMod localNames.set(local[1], localNames.size); } // Resolve Function Body Labels - const wasmBody: number[] = []; + const wasmBody: number[] = [ + ...unsignedLEB128(func.locals.length), + // TODO: Optimize The local Format So That They Are Grouped + ...func.locals + .map(([local]) => { + // Change The 1 to the local length + return [1, ...local]; + }) + .flat(), + ]; const functionReferences: number[] = []; const typeReferences: number[] = []; const globalReferences: number[] = []; @@ -175,16 +184,7 @@ export const addFunction = (wasmModule: WasmModule, func: WasmFunction): WasmMod // Add Function To FunctionSection wasmModule.functionSection.push([typeReference]); // Add Function To CodeSection - const code: number[] = [ - ...unsignedLEB128(func.locals.length), - // TODO: Optimize The local Format So That They Are Grouped - ...func.locals - .map(([local]) => { - // Change The 1 to the local length - return [1, ...local]; - }) - .flat(), - ]; + const code: number[] = []; // Push References To Linking Info wasmModule.codeReferences.push([functionReferences, typeReferences, globalReferences]); // Finish Code Section @@ -314,13 +314,11 @@ export const compileModule = ( includeDataCount = true ): Uint8Array => { // Add A Table - if (wasmModule.elementSection.length > 0) { - wasmModule.tableSection.push([ - 0x70, // Table Type - 0x00, // Limit Flag That We Only Want A Min Value - ...unsignedLEB128(wasmModule.elementSection.length), // Min Value - ]); - } + wasmModule.tableSection.push([ + 0x70, // Table Type + 0x00, // Limit Flag That We Only Want A Min Value + ...unsignedLEB128(wasmModule.elementSection.length), // Min Value + ]); // Compile Name Section const funcNameSection: number[] = [wasmModule.functionMap.size]; for (const [name, index] of wasmModule.functionMap.entries()) {