From f19378e14579a2245c316282aa47df75da31b7d0 Mon Sep 17 00:00:00 2001 From: e5l Date: Fri, 19 Aug 2016 15:54:58 +0300 Subject: [PATCH] car_fmw: route and debug fsm, translator: minor fixes --- car_fmw/src/Control.kt | 69 ++++ car_fmw/src/ProtoRW.kt | 40 ++ car_fmw/src/{Debug.kt => Tests.kt} | 2 +- car_fmw/src/echo.kt | 32 -- car_fmw/src/include/DebugInfo.kt | 12 + car_fmw/src/main.kt | 4 +- car_fmw/src/proto/Debug.proto | 18 + car_fmw/src/proto/Direction.proto | 10 +- car_fmw/src/proto/Route.proto | 4 +- car_fmw/src/proto/Task.proto | 12 + car_fmw/src/proto_src/Debug.kt | 388 ++++++++++++++++++ car_fmw/src/proto_src/Direction.kt | 22 +- car_fmw/src/proto_src/Route.kt | 143 ++++--- car_fmw/src/proto_src/Task.kt | 146 +++++++ car_fmw/src/rcControl.kt | 30 -- kotstd/libc/memory.c | 18 + .../kotlinnative/translator/BlockCodegen.kt | 44 +- 17 files changed, 816 insertions(+), 178 deletions(-) create mode 100644 car_fmw/src/Control.kt create mode 100644 car_fmw/src/ProtoRW.kt rename car_fmw/src/{Debug.kt => Tests.kt} (97%) delete mode 100644 car_fmw/src/echo.kt create mode 100644 car_fmw/src/include/DebugInfo.kt create mode 100644 car_fmw/src/proto/Debug.proto create mode 100644 car_fmw/src/proto/Task.proto create mode 100644 car_fmw/src/proto_src/Debug.kt create mode 100644 car_fmw/src/proto_src/Task.kt delete mode 100644 car_fmw/src/rcControl.kt diff --git a/car_fmw/src/Control.kt b/car_fmw/src/Control.kt new file mode 100644 index 00000000000..04feb74038e --- /dev/null +++ b/car_fmw/src/Control.kt @@ -0,0 +1,69 @@ + +enum class RouteType(val id: Int) { + FORWARD(0), + BACKWARD(1), + LEFT(2), + RIGHT(3); +} + +object Control { + fun run() { + Memory.setHeap(Memory.DYNAMIC_HEAP) + while (true) { + executeCommand() + Memory.cleanDynamicHeap() + } + } + + fun executeCommand() { + val task = Reader.readTask() + when (task.type.id) { + TaskRequest.Type.DEBUG.id -> debugTask() + TaskRequest.Type.ROUTE.id -> routeTask() + } + } + + fun debugTask() { + val request = Reader.readDebug() + + when (request.type.id) { + DebugRequest.Type.MEMORY_STATS.id -> sendMemoryStats() + } + } + + fun routeTask() { + val route = Reader.readRoute() + + val times = route.times + val directions = route.directions + var j = 0 + + while (j < times.size) { + val time = times[j] + val direction = directions[j] + + when (direction) { + RouteType.FORWARD.id -> Engine.forward() + RouteType.BACKWARD.id -> Engine.backward() + RouteType.LEFT.id -> Engine.left() + RouteType.RIGHT.id -> Engine.right() + } + + Time.wait(time) + Engine.stop() + j++ + } + + } + + fun sendMemoryStats() { + val stats = DebugResponseMemoryStats.BuilderDebugResponseMemoryStats( + DebugInfo.getDynamicHeapTail(), + DebugInfo.getStaticHeapTail(), + DebugInfo.getDynamicHeapMaxSize(), + DebugInfo.getDynamicHeapTotalBytes() + ).build() + + Writer.writeMemoryStats(stats) + } +} diff --git a/car_fmw/src/ProtoRW.kt b/car_fmw/src/ProtoRW.kt new file mode 100644 index 00000000000..c033ac84a01 --- /dev/null +++ b/car_fmw/src/ProtoRW.kt @@ -0,0 +1,40 @@ +object Reader { + fun readRoute(): RouteRequest { + val stream = getInputStream() + return RouteRequest.BuilderRouteRequest(IntArray(0), IntArray(0)).parseFrom(stream).build() + } + + fun readTask(): TaskRequest { + val stream = getInputStream() + return TaskRequest.BuilderTaskRequest(TaskRequest.Type.DEBUG).parseFrom(stream).build() + } + + fun readDebug(): DebugRequest { + val stream = getInputStream() + return DebugRequest.BuilderDebugRequest(DebugRequest.Type.MEMORY_STATS).parseFrom(stream).build() + } + + fun getInputStream(): CodedInputStream { + val buffer = Connection.receiveByteArray() + return CodedInputStream(buffer) + } +} + +object Writer { + fun writeRoute(route: RouteRequest) { + val stream = makeOutputStream(route.getSizeNoTag()) + route.writeTo(stream) + Connection.sendByteArray(stream.buffer) + } + + fun writeMemoryStats(stats: DebugResponseMemoryStats) { + val stream = makeOutputStream(stats.getSizeNoTag()) + stats.writeTo(stream) + Connection.sendByteArray(stream.buffer) + } + + fun makeOutputStream(size: Int): CodedOutputStream { + val buffer = ByteArray(size) + return CodedOutputStream(buffer) + } +} \ No newline at end of file diff --git a/car_fmw/src/Debug.kt b/car_fmw/src/Tests.kt similarity index 97% rename from car_fmw/src/Debug.kt rename to car_fmw/src/Tests.kt index 0f68d31936c..0ed436db767 100644 --- a/car_fmw/src/Debug.kt +++ b/car_fmw/src/Tests.kt @@ -1,5 +1,5 @@ -object Debug { +object Tests { var PROGRAM_DURATION = 1000 fun echoUsbTest() { diff --git a/car_fmw/src/echo.kt b/car_fmw/src/echo.kt deleted file mode 100644 index bc924067a31..00000000000 --- a/car_fmw/src/echo.kt +++ /dev/null @@ -1,32 +0,0 @@ - -fun echoProto() { - - Memory.setHeap(Memory.DYNAMIC_HEAP) - while (true) { - val route = readRoute() - Leds.blink() - go(route) - Leds.blink() - Time.wait(1000) - Memory.cleanDynamicHeap() - } -} - -fun readRoute(): RouteRequest { - val buffer = Connection.receiveByteArray() - val stream = CodedInputStream(buffer) - val result = RouteRequest.BuilderRouteRequest(IntArray(0), IntArray(0)).parseFrom(stream).build() - - return result -} - -fun writeRoute(route: RouteRequest) { - val size = route.getSizeNoTag() - val buffer = ByteArray(size) - val stream = CodedOutputStream(buffer) - - route.writeTo(stream) - - Connection.sendByteArray(buffer) -} - diff --git a/car_fmw/src/include/DebugInfo.kt b/car_fmw/src/include/DebugInfo.kt new file mode 100644 index 00000000000..f4c2fa47144 --- /dev/null +++ b/car_fmw/src/include/DebugInfo.kt @@ -0,0 +1,12 @@ + +external fun dynamic_heap_tail(): Int +external fun static_heap_tail(): Int +external fun dynamic_heap_max_bytes(): Int +external fun dynamic_heap_total(): Int + +object DebugInfo { + fun getDynamicHeapTail(): Int = dynamic_heap_tail() + fun getStaticHeapTail(): Int = static_heap_tail() + fun getDynamicHeapMaxSize(): Int = dynamic_heap_max_bytes() + fun getDynamicHeapTotalBytes(): Int = dynamic_heap_total() +} \ No newline at end of file diff --git a/car_fmw/src/main.kt b/car_fmw/src/main.kt index f59bc1b4f63..ee2706f8482 100644 --- a/car_fmw/src/main.kt +++ b/car_fmw/src/main.kt @@ -6,6 +6,6 @@ fun main() { Leds.init() Connection.init() - /* run test */ - echoProto() + /* car task */ + Control.run() } diff --git a/car_fmw/src/proto/Debug.proto b/car_fmw/src/proto/Debug.proto new file mode 100644 index 00000000000..7b0e7908729 --- /dev/null +++ b/car_fmw/src/proto/Debug.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; +package carkot; + +message DebugRequest { + + Type type = 1; + + enum Type { + MEMORY_STATS = 0; + } +} + +message DebugResponseMemoryStats { + int32 heapDynamicTail = 1; + int32 heapStaticTail = 2; + int32 heapDynamicMaxBytes = 3; + int32 heapDynamicTotalBytes = 4; +} diff --git a/car_fmw/src/proto/Direction.proto b/car_fmw/src/proto/Direction.proto index 196fcebbc27..7b5c1e7a85a 100644 --- a/car_fmw/src/proto/Direction.proto +++ b/car_fmw/src/proto/Direction.proto @@ -6,11 +6,11 @@ option java_outer_classname = "Direction"; message DirectionRequest { enum Command { - stop = 0; - forward = 1; - backward = 2; - left = 3; - right = 4; + STOP = 0; + FORWARD = 1; + BACKWARD = 2; + LEFT = 3; + RIGHT = 4; } Command command = 1; int32 sid = 2; diff --git a/car_fmw/src/proto/Route.proto b/car_fmw/src/proto/Route.proto index 77425485cc9..249bba0ba20 100644 --- a/car_fmw/src/proto/Route.proto +++ b/car_fmw/src/proto/Route.proto @@ -2,8 +2,8 @@ syntax = "proto3"; package carkot; message RouteRequest { - repeated int32 distances = 1; - repeated int32 angles = 2; + repeated int32 times = 1; + repeated int32 directions = 2; } message RouteResponse { diff --git a/car_fmw/src/proto/Task.proto b/car_fmw/src/proto/Task.proto new file mode 100644 index 00000000000..11fe5145fec --- /dev/null +++ b/car_fmw/src/proto/Task.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; +package carkot; + +message TaskRequest { + + Type type = 1; + + enum Type { + DEBUG = 0; + ROUTE = 1; + } +} diff --git a/car_fmw/src/proto_src/Debug.kt b/car_fmw/src/proto_src/Debug.kt new file mode 100644 index 00000000000..aebee97b2f9 --- /dev/null +++ b/car_fmw/src/proto_src/Debug.kt @@ -0,0 +1,388 @@ +class DebugRequest private constructor (var type: DebugRequest.Type) { + //========== Properties =========== + //enum type = 1 + + var errorCode: Int = 0 + + //========== Nested enums declarations =========== + enum class Type(val id: Int) { + MEMORY_STATS (0), + Unexpected(1); + + companion object { + fun fromIntToType (ord: Int): Type { + return when (ord) { + 0 -> Type.MEMORY_STATS + else -> Unexpected + } + } + } + } + //========== Serialization methods =========== + fun writeTo (output: CodedOutputStream) { + //enum type = 1 + if (type.id != DebugRequest.Type.fromIntToType(0).id) { + output.writeEnum (1, type.id) + } + + } + + fun mergeWith (other: DebugRequest) { + type = other.type + this.errorCode = other.errorCode + } + + fun mergeFromWithSize (input: CodedInputStream, expectedSize: Int) { + val builder = DebugRequest.BuilderDebugRequest(DebugRequest.Type.fromIntToType(0)) + mergeWith(builder.parseFromWithSize(input, expectedSize).build()) + } + + fun mergeFrom (input: CodedInputStream) { + val builder = DebugRequest.BuilderDebugRequest(DebugRequest.Type.fromIntToType(0)) + mergeWith(builder.parseFrom(input).build()) + } + + //========== Size-related methods =========== + fun getSize(fieldNumber: Int): Int { + var size = 0 + if (type != DebugRequest.Type.fromIntToType(0)) { + size += WireFormat.getEnumSize(1, type.id) + } + size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED) + return size + } + + fun getSizeNoTag(): Int { + var size = 0 + if (type != DebugRequest.Type.fromIntToType(0)) { + size += WireFormat.getEnumSize(1, type.id) + } + return size + } + + //========== Builder =========== + class BuilderDebugRequest constructor (var type: DebugRequest.Type) { + //========== Properties =========== + //enum type = 1 + fun setType(value: DebugRequest.Type): DebugRequest.BuilderDebugRequest { + type = value + return this + } + + var errorCode: Int = 0 + + //========== Serialization methods =========== + fun writeTo (output: CodedOutputStream) { + //enum type = 1 + if (type.id != DebugRequest.Type.fromIntToType(0).id) { + output.writeEnum (1, type.id) + } + + } + + //========== Mutating methods =========== + fun build(): DebugRequest { + val res = DebugRequest(type) + res.errorCode = errorCode + return res + } + + fun parseFieldFrom(input: CodedInputStream): Boolean { + if (input.isAtEnd()) { return false } + val tag = input.readInt32NoTag() + if (tag == 0) { return false } + val fieldNumber = WireFormat.getTagFieldNumber(tag) + val wireType = WireFormat.getTagWireType(tag) + when(fieldNumber) { + 1 -> { + if (wireType.id != WireType.VARINT.id) { + errorCode = 1 + return false + } + type = DebugRequest.Type.fromIntToType(input.readEnumNoTag()) + } + else -> errorCode = 4 + } + return true + } + + fun parseFromWithSize(input: CodedInputStream, expectedSize: Int): DebugRequest.BuilderDebugRequest { + while(getSizeNoTag() < expectedSize) { + parseFieldFrom(input) + } + if (getSizeNoTag() > expectedSize) { errorCode = 2 } + return this + } + + fun parseFrom(input: CodedInputStream): DebugRequest.BuilderDebugRequest { + while(parseFieldFrom(input)) {} + return this + } + + //========== Size-related methods =========== + fun getSize(fieldNumber: Int): Int { + var size = 0 + if (type != DebugRequest.Type.fromIntToType(0)) { + size += WireFormat.getEnumSize(1, type.id) + } + size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED) + return size + } + + fun getSizeNoTag(): Int { + var size = 0 + if (type != DebugRequest.Type.fromIntToType(0)) { + size += WireFormat.getEnumSize(1, type.id) + } + return size + } + + } + +} + + +class DebugResponseMemoryStats private constructor (var heapDynamicTail: Int, var heapStaticTail: Int, var heapDynamicMaxBytes: Int, var heapDynamicTotalBytes: Int) { + //========== Properties =========== + //int32 heapDynamicTail = 1 + + //int32 heapStaticTail = 2 + + //int32 heapDynamicMaxBytes = 3 + + //int32 heapDynamicTotalBytes = 4 + + var errorCode: Int = 0 + + //========== Serialization methods =========== + fun writeTo (output: CodedOutputStream) { + //int32 heapDynamicTail = 1 + if (heapDynamicTail != 0) { + output.writeInt32 (1, heapDynamicTail) + } + + //int32 heapStaticTail = 2 + if (heapStaticTail != 0) { + output.writeInt32 (2, heapStaticTail) + } + + //int32 heapDynamicMaxBytes = 3 + if (heapDynamicMaxBytes != 0) { + output.writeInt32 (3, heapDynamicMaxBytes) + } + + //int32 heapDynamicTotalBytes = 4 + if (heapDynamicTotalBytes != 0) { + output.writeInt32 (4, heapDynamicTotalBytes) + } + + } + + fun mergeWith (other: DebugResponseMemoryStats) { + heapDynamicTail = other.heapDynamicTail + heapStaticTail = other.heapStaticTail + heapDynamicMaxBytes = other.heapDynamicMaxBytes + heapDynamicTotalBytes = other.heapDynamicTotalBytes + this.errorCode = other.errorCode + } + + fun mergeFromWithSize (input: CodedInputStream, expectedSize: Int) { + val builder = DebugResponseMemoryStats.BuilderDebugResponseMemoryStats(0, 0, 0, 0) + mergeWith(builder.parseFromWithSize(input, expectedSize).build()) + } + + fun mergeFrom (input: CodedInputStream) { + val builder = DebugResponseMemoryStats.BuilderDebugResponseMemoryStats(0, 0, 0, 0) + mergeWith(builder.parseFrom(input).build()) + } + + //========== Size-related methods =========== + fun getSize(fieldNumber: Int): Int { + var size = 0 + if (heapDynamicTail != 0) { + size += WireFormat.getInt32Size(1, heapDynamicTail) + } + if (heapStaticTail != 0) { + size += WireFormat.getInt32Size(2, heapStaticTail) + } + if (heapDynamicMaxBytes != 0) { + size += WireFormat.getInt32Size(3, heapDynamicMaxBytes) + } + if (heapDynamicTotalBytes != 0) { + size += WireFormat.getInt32Size(4, heapDynamicTotalBytes) + } + size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED) + return size + } + + fun getSizeNoTag(): Int { + var size = 0 + if (heapDynamicTail != 0) { + size += WireFormat.getInt32Size(1, heapDynamicTail) + } + if (heapStaticTail != 0) { + size += WireFormat.getInt32Size(2, heapStaticTail) + } + if (heapDynamicMaxBytes != 0) { + size += WireFormat.getInt32Size(3, heapDynamicMaxBytes) + } + if (heapDynamicTotalBytes != 0) { + size += WireFormat.getInt32Size(4, heapDynamicTotalBytes) + } + return size + } + + //========== Builder =========== + class BuilderDebugResponseMemoryStats constructor (var heapDynamicTail: Int, var heapStaticTail: Int, var heapDynamicMaxBytes: Int, var heapDynamicTotalBytes: Int) { + //========== Properties =========== + //int32 heapDynamicTail = 1 + fun setHeapDynamicTail(value: Int): DebugResponseMemoryStats.BuilderDebugResponseMemoryStats { + heapDynamicTail = value + return this + } + + //int32 heapStaticTail = 2 + fun setHeapStaticTail(value: Int): DebugResponseMemoryStats.BuilderDebugResponseMemoryStats { + heapStaticTail = value + return this + } + + //int32 heapDynamicMaxBytes = 3 + fun setHeapDynamicMaxBytes(value: Int): DebugResponseMemoryStats.BuilderDebugResponseMemoryStats { + heapDynamicMaxBytes = value + return this + } + + //int32 heapDynamicTotalBytes = 4 + fun setHeapDynamicTotalBytes(value: Int): DebugResponseMemoryStats.BuilderDebugResponseMemoryStats { + heapDynamicTotalBytes = value + return this + } + + var errorCode: Int = 0 + + //========== Serialization methods =========== + fun writeTo (output: CodedOutputStream) { + //int32 heapDynamicTail = 1 + if (heapDynamicTail != 0) { + output.writeInt32 (1, heapDynamicTail) + } + + //int32 heapStaticTail = 2 + if (heapStaticTail != 0) { + output.writeInt32 (2, heapStaticTail) + } + + //int32 heapDynamicMaxBytes = 3 + if (heapDynamicMaxBytes != 0) { + output.writeInt32 (3, heapDynamicMaxBytes) + } + + //int32 heapDynamicTotalBytes = 4 + if (heapDynamicTotalBytes != 0) { + output.writeInt32 (4, heapDynamicTotalBytes) + } + + } + + //========== Mutating methods =========== + fun build(): DebugResponseMemoryStats { + val res = DebugResponseMemoryStats(heapDynamicTail, heapStaticTail, heapDynamicMaxBytes, heapDynamicTotalBytes) + res.errorCode = errorCode + return res + } + + fun parseFieldFrom(input: CodedInputStream): Boolean { + if (input.isAtEnd()) { return false } + val tag = input.readInt32NoTag() + if (tag == 0) { return false } + val fieldNumber = WireFormat.getTagFieldNumber(tag) + val wireType = WireFormat.getTagWireType(tag) + when(fieldNumber) { + 1 -> { + if (wireType.id != WireType.VARINT.id) { + errorCode = 1 + return false + } + heapDynamicTail = input.readInt32NoTag() + } + 2 -> { + if (wireType.id != WireType.VARINT.id) { + errorCode = 1 + return false + } + heapStaticTail = input.readInt32NoTag() + } + 3 -> { + if (wireType.id != WireType.VARINT.id) { + errorCode = 1 + return false + } + heapDynamicMaxBytes = input.readInt32NoTag() + } + 4 -> { + if (wireType.id != WireType.VARINT.id) { + errorCode = 1 + return false + } + heapDynamicTotalBytes = input.readInt32NoTag() + } + else -> errorCode = 4 + } + return true + } + + fun parseFromWithSize(input: CodedInputStream, expectedSize: Int): DebugResponseMemoryStats.BuilderDebugResponseMemoryStats { + while(getSizeNoTag() < expectedSize) { + parseFieldFrom(input) + } + if (getSizeNoTag() > expectedSize) { errorCode = 2 } + return this + } + + fun parseFrom(input: CodedInputStream): DebugResponseMemoryStats.BuilderDebugResponseMemoryStats { + while(parseFieldFrom(input)) {} + return this + } + + //========== Size-related methods =========== + fun getSize(fieldNumber: Int): Int { + var size = 0 + if (heapDynamicTail != 0) { + size += WireFormat.getInt32Size(1, heapDynamicTail) + } + if (heapStaticTail != 0) { + size += WireFormat.getInt32Size(2, heapStaticTail) + } + if (heapDynamicMaxBytes != 0) { + size += WireFormat.getInt32Size(3, heapDynamicMaxBytes) + } + if (heapDynamicTotalBytes != 0) { + size += WireFormat.getInt32Size(4, heapDynamicTotalBytes) + } + size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED) + return size + } + + fun getSizeNoTag(): Int { + var size = 0 + if (heapDynamicTail != 0) { + size += WireFormat.getInt32Size(1, heapDynamicTail) + } + if (heapStaticTail != 0) { + size += WireFormat.getInt32Size(2, heapStaticTail) + } + if (heapDynamicMaxBytes != 0) { + size += WireFormat.getInt32Size(3, heapDynamicMaxBytes) + } + if (heapDynamicTotalBytes != 0) { + size += WireFormat.getInt32Size(4, heapDynamicTotalBytes) + } + return size + } + + } + +} + + diff --git a/car_fmw/src/proto_src/Direction.kt b/car_fmw/src/proto_src/Direction.kt index a609525acdc..615b884b3b1 100644 --- a/car_fmw/src/proto_src/Direction.kt +++ b/car_fmw/src/proto_src/Direction.kt @@ -1,4 +1,3 @@ - class DirectionRequest private constructor (var command: DirectionRequest.Command, var sid: Int) { //========== Properties =========== //enum command = 1 @@ -9,21 +8,21 @@ class DirectionRequest private constructor (var command: DirectionRequest.Comman //========== Nested enums declarations =========== enum class Command(val id: Int) { - stop (0), - forward (1), - backward (2), - left (3), - right (4), + STOP (0), + FORWARD (1), + BACKWARD (2), + LEFT (3), + RIGHT (4), Unexpected(5); companion object { fun fromIntToCommand (ord: Int): Command { return when (ord) { - 0 -> Command.stop - 1 -> Command.forward - 2 -> Command.backward - 3 -> Command.left - 4 -> Command.right + 0 -> Command.STOP + 1 -> Command.FORWARD + 2 -> Command.BACKWARD + 3 -> Command.LEFT + 4 -> Command.RIGHT else -> Unexpected } } @@ -188,6 +187,7 @@ class DirectionRequest private constructor (var command: DirectionRequest.Comman } + class DirectionResponse private constructor (var code: Int) { //========== Properties =========== //int32 code = 1 diff --git a/car_fmw/src/proto_src/Route.kt b/car_fmw/src/proto_src/Route.kt index e277428c0cf..91550301708 100644 --- a/car_fmw/src/proto_src/Route.kt +++ b/car_fmw/src/proto_src/Route.kt @@ -1,25 +1,24 @@ - -class RouteRequest private constructor (var distances: IntArray, var angles: IntArray) { +class RouteRequest private constructor (var times: IntArray, var directions: IntArray) { //========== Properties =========== - //repeated int32 distances = 1 + //repeated int32 times = 1 - //repeated int32 angles = 2 + //repeated int32 directions = 2 var errorCode: Int = 0 //========== Serialization methods =========== fun writeTo (output: CodedOutputStream) { - //repeated int32 distances = 1 - if (distances.size > 0) { + //repeated int32 times = 1 + if (times.size > 0) { output.writeTag(1, WireType.LENGTH_DELIMITED) var arrayByteSize = 0 - if (distances.size != 0) { + if (times.size != 0) { do { var arraySize = 0 var i = 0 - while (i < distances.size) { - arraySize += WireFormat.getInt32SizeNoTag(distances[i]) + while (i < times.size) { + arraySize += WireFormat.getInt32SizeNoTag(times[i]) i += 1 } arrayByteSize += arraySize @@ -29,24 +28,24 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int do { var i = 0 - while (i < distances.size) { - output.writeInt32NoTag (distances[i]) + while (i < times.size) { + output.writeInt32NoTag (times[i]) i += 1 } } while(false) } - //repeated int32 angles = 2 - if (angles.size > 0) { + //repeated int32 directions = 2 + if (directions.size > 0) { output.writeTag(2, WireType.LENGTH_DELIMITED) var arrayByteSize = 0 - if (angles.size != 0) { + if (directions.size != 0) { do { var arraySize = 0 var i = 0 - while (i < angles.size) { - arraySize += WireFormat.getInt32SizeNoTag(angles[i]) + while (i < directions.size) { + arraySize += WireFormat.getInt32SizeNoTag(directions[i]) i += 1 } arrayByteSize += arraySize @@ -56,8 +55,8 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int do { var i = 0 - while (i < angles.size) { - output.writeInt32NoTag (angles[i]) + while (i < directions.size) { + output.writeInt32NoTag (directions[i]) i += 1 } } while(false) @@ -66,8 +65,8 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int } fun mergeWith (other: RouteRequest) { - distances = distances.plus((other.distances)) - angles = angles.plus((other.angles)) + times = times.plus((other.times)) + directions = directions.plus((other.directions)) this.errorCode = other.errorCode } @@ -84,26 +83,26 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int //========== Size-related methods =========== fun getSize(fieldNumber: Int): Int { var size = 0 - if (distances.size != 0) { + if (times.size != 0) { do { var arraySize = 0 size += WireFormat.getTagSize(1, WireType.LENGTH_DELIMITED) var i = 0 - while (i < distances.size) { - arraySize += WireFormat.getInt32SizeNoTag(distances[i]) + while (i < times.size) { + arraySize += WireFormat.getInt32SizeNoTag(times[i]) i += 1 } size += arraySize size += WireFormat.getInt32SizeNoTag(arraySize) } while(false) } - if (angles.size != 0) { + if (directions.size != 0) { do { var arraySize = 0 size += WireFormat.getTagSize(2, WireType.LENGTH_DELIMITED) var i = 0 - while (i < angles.size) { - arraySize += WireFormat.getInt32SizeNoTag(angles[i]) + while (i < directions.size) { + arraySize += WireFormat.getInt32SizeNoTag(directions[i]) i += 1 } size += arraySize @@ -116,26 +115,26 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int fun getSizeNoTag(): Int { var size = 0 - if (distances.size != 0) { + if (times.size != 0) { do { var arraySize = 0 size += WireFormat.getTagSize(1, WireType.LENGTH_DELIMITED) var i = 0 - while (i < distances.size) { - arraySize += WireFormat.getInt32SizeNoTag(distances[i]) + while (i < times.size) { + arraySize += WireFormat.getInt32SizeNoTag(times[i]) i += 1 } size += arraySize size += WireFormat.getInt32SizeNoTag(arraySize) } while(false) } - if (angles.size != 0) { + if (directions.size != 0) { do { var arraySize = 0 size += WireFormat.getTagSize(2, WireType.LENGTH_DELIMITED) var i = 0 - while (i < angles.size) { - arraySize += WireFormat.getInt32SizeNoTag(angles[i]) + while (i < directions.size) { + arraySize += WireFormat.getInt32SizeNoTag(directions[i]) i += 1 } size += arraySize @@ -146,25 +145,25 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int } //========== Builder =========== - class BuilderRouteRequest constructor (var distances: IntArray, var angles: IntArray) { + class BuilderRouteRequest constructor (var times: IntArray, var directions: IntArray) { //========== Properties =========== - //repeated int32 distances = 1 - fun setDistances(value: IntArray): RouteRequest.BuilderRouteRequest { - distances = value + //repeated int32 times = 1 + fun setTimes(value: IntArray): RouteRequest.BuilderRouteRequest { + times = value return this } - fun setdistancesByIndex(index: Int, value: Int): RouteRequest.BuilderRouteRequest { - distances[index] = value + fun settimesByIndex(index: Int, value: Int): RouteRequest.BuilderRouteRequest { + times[index] = value return this } - //repeated int32 angles = 2 - fun setAngles(value: IntArray): RouteRequest.BuilderRouteRequest { - angles = value + //repeated int32 directions = 2 + fun setDirections(value: IntArray): RouteRequest.BuilderRouteRequest { + directions = value return this } - fun setanglesByIndex(index: Int, value: Int): RouteRequest.BuilderRouteRequest { - angles[index] = value + fun setdirectionsByIndex(index: Int, value: Int): RouteRequest.BuilderRouteRequest { + directions[index] = value return this } @@ -172,17 +171,17 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int //========== Serialization methods =========== fun writeTo (output: CodedOutputStream) { - //repeated int32 distances = 1 - if (distances.size > 0) { + //repeated int32 times = 1 + if (times.size > 0) { output.writeTag(1, WireType.LENGTH_DELIMITED) var arrayByteSize = 0 - if (distances.size != 0) { + if (times.size != 0) { do { var arraySize = 0 var i = 0 - while (i < distances.size) { - arraySize += WireFormat.getInt32SizeNoTag(distances[i]) + while (i < times.size) { + arraySize += WireFormat.getInt32SizeNoTag(times[i]) i += 1 } arrayByteSize += arraySize @@ -192,24 +191,24 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int do { var i = 0 - while (i < distances.size) { - output.writeInt32NoTag (distances[i]) + while (i < times.size) { + output.writeInt32NoTag (times[i]) i += 1 } } while(false) } - //repeated int32 angles = 2 - if (angles.size > 0) { + //repeated int32 directions = 2 + if (directions.size > 0) { output.writeTag(2, WireType.LENGTH_DELIMITED) var arrayByteSize = 0 - if (angles.size != 0) { + if (directions.size != 0) { do { var arraySize = 0 var i = 0 - while (i < angles.size) { - arraySize += WireFormat.getInt32SizeNoTag(angles[i]) + while (i < directions.size) { + arraySize += WireFormat.getInt32SizeNoTag(directions[i]) i += 1 } arrayByteSize += arraySize @@ -219,8 +218,8 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int do { var i = 0 - while (i < angles.size) { - output.writeInt32NoTag (angles[i]) + while (i < directions.size) { + output.writeInt32NoTag (directions[i]) i += 1 } } while(false) @@ -230,7 +229,7 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int //========== Mutating methods =========== fun build(): RouteRequest { - val res = RouteRequest(distances, angles) + val res = RouteRequest(times, directions) res.errorCode = errorCode return res } @@ -267,7 +266,7 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int while(i < arraySize) { newArray[i] = input.readInt32NoTag() i += 1} - distances = newArray + times = newArray } while (false) } 2 -> { @@ -295,7 +294,7 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int while(i < arraySize) { newArray[i] = input.readInt32NoTag() i += 1} - angles = newArray + directions = newArray } while (false) } else -> errorCode = 4 @@ -319,26 +318,26 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int //========== Size-related methods =========== fun getSize(fieldNumber: Int): Int { var size = 0 - if (distances.size != 0) { + if (times.size != 0) { do { var arraySize = 0 size += WireFormat.getTagSize(1, WireType.LENGTH_DELIMITED) var i = 0 - while (i < distances.size) { - arraySize += WireFormat.getInt32SizeNoTag(distances[i]) + while (i < times.size) { + arraySize += WireFormat.getInt32SizeNoTag(times[i]) i += 1 } size += arraySize size += WireFormat.getInt32SizeNoTag(arraySize) } while(false) } - if (angles.size != 0) { + if (directions.size != 0) { do { var arraySize = 0 size += WireFormat.getTagSize(2, WireType.LENGTH_DELIMITED) var i = 0 - while (i < angles.size) { - arraySize += WireFormat.getInt32SizeNoTag(angles[i]) + while (i < directions.size) { + arraySize += WireFormat.getInt32SizeNoTag(directions[i]) i += 1 } size += arraySize @@ -351,26 +350,26 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int fun getSizeNoTag(): Int { var size = 0 - if (distances.size != 0) { + if (times.size != 0) { do { var arraySize = 0 size += WireFormat.getTagSize(1, WireType.LENGTH_DELIMITED) var i = 0 - while (i < distances.size) { - arraySize += WireFormat.getInt32SizeNoTag(distances[i]) + while (i < times.size) { + arraySize += WireFormat.getInt32SizeNoTag(times[i]) i += 1 } size += arraySize size += WireFormat.getInt32SizeNoTag(arraySize) } while(false) } - if (angles.size != 0) { + if (directions.size != 0) { do { var arraySize = 0 size += WireFormat.getTagSize(2, WireType.LENGTH_DELIMITED) var i = 0 - while (i < angles.size) { - arraySize += WireFormat.getInt32SizeNoTag(angles[i]) + while (i < directions.size) { + arraySize += WireFormat.getInt32SizeNoTag(directions[i]) i += 1 } size += arraySize diff --git a/car_fmw/src/proto_src/Task.kt b/car_fmw/src/proto_src/Task.kt new file mode 100644 index 00000000000..a3cb6d830bf --- /dev/null +++ b/car_fmw/src/proto_src/Task.kt @@ -0,0 +1,146 @@ +class TaskRequest private constructor (var type: TaskRequest.Type) { + //========== Properties =========== + //enum type = 1 + + var errorCode: Int = 0 + + //========== Nested enums declarations =========== + enum class Type(val id: Int) { + DEBUG (0), + ROUTE (1), + Unexpected(2); + + companion object { + fun fromIntToType (ord: Int): Type { + return when (ord) { + 0 -> Type.DEBUG + 1 -> Type.ROUTE + else -> Unexpected + } + } + } + } + //========== Serialization methods =========== + fun writeTo (output: CodedOutputStream) { + //enum type = 1 + if (type.id != TaskRequest.Type.fromIntToType(0).id) { + output.writeEnum (1, type.id) + } + + } + + fun mergeWith (other: TaskRequest) { + type = other.type + this.errorCode = other.errorCode + } + + fun mergeFromWithSize (input: CodedInputStream, expectedSize: Int) { + val builder = TaskRequest.BuilderTaskRequest(TaskRequest.Type.fromIntToType(0)) + mergeWith(builder.parseFromWithSize(input, expectedSize).build()) + } + + fun mergeFrom (input: CodedInputStream) { + val builder = TaskRequest.BuilderTaskRequest(TaskRequest.Type.fromIntToType(0)) + mergeWith(builder.parseFrom(input).build()) + } + + //========== Size-related methods =========== + fun getSize(fieldNumber: Int): Int { + var size = 0 + if (type != TaskRequest.Type.fromIntToType(0)) { + size += WireFormat.getEnumSize(1, type.id) + } + size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED) + return size + } + + fun getSizeNoTag(): Int { + var size = 0 + if (type != TaskRequest.Type.fromIntToType(0)) { + size += WireFormat.getEnumSize(1, type.id) + } + return size + } + + //========== Builder =========== + class BuilderTaskRequest constructor (var type: TaskRequest.Type) { + //========== Properties =========== + //enum type = 1 + fun setType(value: TaskRequest.Type): TaskRequest.BuilderTaskRequest { + type = value + return this + } + + var errorCode: Int = 0 + + //========== Serialization methods =========== + fun writeTo (output: CodedOutputStream) { + //enum type = 1 + if (type.id != TaskRequest.Type.fromIntToType(0).id) { + output.writeEnum (1, type.id) + } + + } + + //========== Mutating methods =========== + fun build(): TaskRequest { + val res = TaskRequest(type) + res.errorCode = errorCode + return res + } + + fun parseFieldFrom(input: CodedInputStream): Boolean { + if (input.isAtEnd()) { return false } + val tag = input.readInt32NoTag() + if (tag == 0) { return false } + val fieldNumber = WireFormat.getTagFieldNumber(tag) + val wireType = WireFormat.getTagWireType(tag) + when(fieldNumber) { + 1 -> { + if (wireType.id != WireType.VARINT.id) { + errorCode = 1 + return false + } + type = TaskRequest.Type.fromIntToType(input.readEnumNoTag()) + } + else -> errorCode = 4 + } + return true + } + + fun parseFromWithSize(input: CodedInputStream, expectedSize: Int): TaskRequest.BuilderTaskRequest { + while(getSizeNoTag() < expectedSize) { + parseFieldFrom(input) + } + if (getSizeNoTag() > expectedSize) { errorCode = 2 } + return this + } + + fun parseFrom(input: CodedInputStream): TaskRequest.BuilderTaskRequest { + while(parseFieldFrom(input)) {} + return this + } + + //========== Size-related methods =========== + fun getSize(fieldNumber: Int): Int { + var size = 0 + if (type != TaskRequest.Type.fromIntToType(0)) { + size += WireFormat.getEnumSize(1, type.id) + } + size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED) + return size + } + + fun getSizeNoTag(): Int { + var size = 0 + if (type != TaskRequest.Type.fromIntToType(0)) { + size += WireFormat.getEnumSize(1, type.id) + } + return size + } + + } + +} + + diff --git a/car_fmw/src/rcControl.kt b/car_fmw/src/rcControl.kt deleted file mode 100644 index 2ece5440406..00000000000 --- a/car_fmw/src/rcControl.kt +++ /dev/null @@ -1,30 +0,0 @@ - -enum class RouteType(val id: Int) { - FORWARD(0), - BACKWARD(1), - LEFT(2), - RIGHT(3); -} - -fun go(request: RouteRequest) { - val times = request.distances - val actions = request.angles - var j = 0 - - while (j < times.size) { - val time = times[j] - val action = actions[j] - - when (action) { - RouteType.FORWARD.id -> Engine.forward() - RouteType.BACKWARD.id -> Engine.backward() - RouteType.LEFT.id -> Engine.left() - RouteType.RIGHT.id -> Engine.right() - } - - Time.wait(time) - Engine.stop() - j++ - } - -} \ No newline at end of file diff --git a/kotstd/libc/memory.c b/kotstd/libc/memory.c index c448d57b1a4..18844c7c0ef 100644 --- a/kotstd/libc/memory.c +++ b/kotstd/libc/memory.c @@ -62,3 +62,21 @@ void clean_dynamic_heap() { heap_tails[DYNAMIC_HEAP] = 0; #endif } + +#ifdef ARM +int dynamic_heap_tail() { + return heap_tails[DYNAMIC_HEAP]; +} + +int static_heap_tail() { + return heap_tails[STATIC_HEAP]; +} + +int dynamic_heap_max_bytes() { + return dynamic_heap_max; +} + +int dynamic_heap_total() { + return dynamic_heap_consume; +} +#endif diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt index fed76ad9d47..8d94784655c 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt @@ -65,7 +65,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va is KtBinaryExpression -> evaluateBinaryExpression(expr, scopeDepth) is KtCallExpression -> evaluateCallExpression(expr, scopeDepth) is KtDoWhileExpression -> evaluateDoWhileExpression(expr.firstChild, scopeDepth + 1) - is KtBreakExpression -> evaluateBreakExpression(expr, breakLabel!!, scopeDepth + 1) + is KtBreakExpression -> evaluateBreakExpression(breakLabel!!) is KtDotQualifiedExpression -> evaluateDotExpression(expr, scopeDepth) is KtWhenExpression -> evaluateWhenExpression(expr, scopeDepth) is PsiElement -> evaluateExpression(expr.firstChild, scopeDepth + 1) @@ -79,7 +79,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va expressionWalker(expr.getNextSiblingIgnoringWhitespaceAndComments(), breakLabel, scopeDepth) } - private fun evaluateBreakExpression(expr: KtBreakExpression, breakLabel: LLVMLabel, scopeDepth: Int) { + private fun evaluateBreakExpression(breakLabel: LLVMLabel) { codeBuilder.addUnconditionalJump(breakLabel) } @@ -228,7 +228,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va } } - val clazz = resolveCodegen(receiverExpr) ?: return evaluateExtensionExpression(receiverExpr, receiver, selectorExpr as KtCallExpression, scopeDepth) + val clazz = resolveCodegen(receiverExpr) ?: return evaluateExtensionExpression(receiverExpr, receiver, selectorExpr as? KtCallExpression ?: throw UnexpectedException(selectorExpr.text), scopeDepth) return evaluateClassScopedDotExpression(clazz, selectorExpr, scopeDepth, receiver) } @@ -442,26 +442,27 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va } val containingClass = resolveContainingClass(expr) - if (containingClass != null) { - val name = "${containingClass.fullName}.$function" - val method = containingClass.methods[name]!! - val args = mutableListOf() - val leftName = (expr.context as? KtDotQualifiedExpression)?.receiverExpression?.text - if (caller != null) { - args.add(caller) - } else if (variableManager[containingClass.fullName] != null) { - args.add(variableManager[containingClass.fullName]!!) - } else { - args.add(variableManager["this"]!!) - } - - args.addAll(loadArgsIfRequired(names, method.args)) - - return evaluateFunctionCallExpression(LLVMVariable(method.fullName, method.returnType?.type ?: LLVMVoidType(), scope = LLVMVariableScope()), args) + if (containingClass == null) { + return null } - return null + val name = "${containingClass.fullName}.$function" + val method = containingClass.methods[name]!! + val args = mutableListOf() + val leftName = (expr.context as? KtDotQualifiedExpression)?.receiverExpression?.text + + if (caller != null) { + args.add(caller) + } else if (variableManager[containingClass.fullName] != null) { + args.add(variableManager[containingClass.fullName]!!) + } else { + args.add(variableManager["this"]!!) + } + + args.addAll(loadArgsIfRequired(names, method.args)) + + return evaluateFunctionCallExpression(LLVMVariable(method.fullName, method.returnType?.type ?: LLVMVoidType(), scope = LLVMVariableScope()), args) } private fun resolveContainingClass(expr: KtElement): StructCodegen? { @@ -1076,9 +1077,6 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va codeBuilder.addConstant(newVar, assignExpression) variableManager.addVariable(identifier, newVar, scopeDepth) } - null -> { - val xrptrz = 442 - } else -> { throw UnsupportedOperationException() }