diff --git a/compiler/src/main/kotlin/asmble/cli/Main.kt b/compiler/src/main/kotlin/asmble/cli/Main.kt index 62792f4..92d7ae8 100644 --- a/compiler/src/main/kotlin/asmble/cli/Main.kt +++ b/compiler/src/main/kotlin/asmble/cli/Main.kt @@ -31,6 +31,7 @@ fun main(args: Array) { val globals = Main.globalArgs(argBuild) logger = Logger.Print(globals.logLevel) command.logger = logger + logger.info { "Running the command=${command.name} with args=${argBuild.args}" } command.runWithArgs(argBuild) } catch (e: Exception) { logger.error { "Error ${command?.let { "in command '${it.name}'" } ?: ""}: ${e.message}" } diff --git a/compiler/src/main/kotlin/asmble/cli/Translate.kt b/compiler/src/main/kotlin/asmble/cli/Translate.kt index 648ca57..d2ff48f 100644 --- a/compiler/src/main/kotlin/asmble/cli/Translate.kt +++ b/compiler/src/main/kotlin/asmble/cli/Translate.kt @@ -75,7 +75,10 @@ open class Translate : Command() { fun inToAst(inFile: String, inFormat: String): Script { val inBytes = if (inFile == "--") System.`in`.use { it.readBytes() } - else File(inFile).let { f -> FileInputStream(f).use { it.readBytes(f.length().toIntExact()) } } + else File(inFile).let { f -> + // Input file might not fit into the memory + FileInputStream(f).use { it.readBytes(f.length().toIntExact()) } + } return when (inFormat) { "wast" -> StrToSExpr.parse(inBytes.toString(Charsets.UTF_8)).let { res -> when (res) { diff --git a/compiler/src/main/kotlin/asmble/io/BinaryToAst.kt b/compiler/src/main/kotlin/asmble/io/BinaryToAst.kt index 533cb64..1a88b94 100644 --- a/compiler/src/main/kotlin/asmble/io/BinaryToAst.kt +++ b/compiler/src/main/kotlin/asmble/io/BinaryToAst.kt @@ -150,26 +150,28 @@ open class BinaryToAst( fun toMemoryType(b: ByteReader) = Node.Type.Memory(toResizableLimits(b)) - fun toModule(b: ByteReader): Node.Module { - if (b.readUInt32() != 0x6d736100L) throw IoErr.InvalidMagicNumber() - b.readUInt32().let { if (it != version) throw IoErr.InvalidVersion(it, listOf(version)) } + fun toModule(bytes: ByteReader): Node.Module { + if (bytes.readUInt32() != 0x6d736100L) throw IoErr.InvalidMagicNumber() + bytes.readUInt32().let { if (it != version) throw IoErr.InvalidVersion(it, listOf(version)) } // Slice up all the sections var maxSectionId = 0 var sections = emptyList>() - while (!b.isEof) { - val sectionId = b.readVarUInt7().toInt() + while (!bytes.isEof) { + val sectionId = bytes.readVarUInt7().toInt() if (sectionId > 11) throw IoErr.InvalidSectionId(sectionId) if (sectionId != 0) require(sectionId > maxSectionId) { "Section ID $sectionId came after $maxSectionId" }. also { maxSectionId = sectionId } - val sectionLen = b.readVarUInt32AsInt() - sections += sectionId to b.read(sectionLen) + val sectionLen = bytes.readVarUInt32AsInt() + // each 'read' invocation creates new InputStream and don't closes it + sections += sectionId to bytes.read(sectionLen) } // Now build the module fun readSectionList(sectionId: Int, fn: (ByteReader) -> T) = sections.find { it.first == sectionId }?.second?.readList(fn) ?: emptyList() + val types = readSectionList(1, this::toFuncType) val funcIndices = readSectionList(3) { it.readVarUInt32AsInt() } return Node.Module( diff --git a/compiler/src/main/kotlin/asmble/io/ByteReader.kt b/compiler/src/main/kotlin/asmble/io/ByteReader.kt index 09cce85..531a0bd 100644 --- a/compiler/src/main/kotlin/asmble/io/ByteReader.kt +++ b/compiler/src/main/kotlin/asmble/io/ByteReader.kt @@ -100,6 +100,7 @@ abstract class ByteReader { return result } + // todo looks like this InputStream isn't ever closed class InputStream(val ins: java.io.InputStream) : ByteReader() { private var nextByte: Byte? = null private var sawEof = false diff --git a/examples/README.md b/examples/README.md index 02b55e9..61faadc 100644 --- a/examples/README.md +++ b/examples/README.md @@ -9,9 +9,3 @@ Compile Rust to WASM and then to the JVM. In order of complexity: * [rust-simple](rust-simple) * [rust-string](rust-string) * [rust-regex](rust-regex) - -### C/C++ - -Compile C to WASM and then to the JVM. In order of complexity: - -* [c-simple](c-simple) \ No newline at end of file diff --git a/examples/rust-string/src/lib.rs b/examples/rust-string/src/lib.rs index 3fb74de..7274b2d 100644 --- a/examples/rust-string/src/lib.rs +++ b/examples/rust-string/src/lib.rs @@ -1,6 +1,7 @@ #![feature(allocator_api)] -use std::heap::{Alloc, Heap, Layout}; +use std::ptr::NonNull; +use std::alloc::{Alloc, Global, Layout}; use std::ffi::{CString}; use std::mem; use std::os::raw::c_char; @@ -30,17 +31,17 @@ pub extern "C" fn prepend_from_rust(ptr: *mut u8, len: usize) -> *const c_char { } #[no_mangle] -pub extern "C" fn alloc(size: usize) -> *mut u8 { +pub extern "C" fn alloc(size: usize) -> NonNull { unsafe { let layout = Layout::from_size_align(size, mem::align_of::()).unwrap(); - Heap.alloc(layout).unwrap() + Global.alloc(layout).unwrap() } } #[no_mangle] -pub extern "C" fn dealloc(ptr: *mut u8, size: usize) { +pub extern "C" fn dealloc(ptr: NonNull, size: usize) { unsafe { let layout = Layout::from_size_align(size, mem::align_of::()).unwrap(); - Heap.dealloc(ptr, layout); + Global.dealloc(ptr, layout); } } \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index edd9a86..baf6681 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,7 +1,6 @@ rootProject.name = 'asmble' include 'annotations', 'compiler', - 'examples:c-simple', 'examples:rust-regex', 'examples:rust-simple', 'examples:rust-string' \ No newline at end of file