Skip to content

Commit bfa9fba

Browse files
alex28shSpace Team
authored andcommitted
[Wasm] fix merge (KT-79357)
1 parent 1c7c3ed commit bfa9fba

File tree

1 file changed

+31
-18
lines changed
  • compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm

1 file changed

+31
-18
lines changed

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/ConstantData.kt

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private fun addressToString(address: Int): String =
2727
class ConstantDataCharField(val value: WasmSymbol<Char>) : ConstantDataElement() {
2828
constructor(value: Char) : this(WasmSymbol(value))
2929

30-
override fun toBytes(): ByteArray = value.owner.toLittleEndianBytes(false)
30+
override fun toBytes(): ByteArray = ByteArray(2).apply { value.owner.toLittleEndianBytes(this, 0, false) }
3131

3232
override fun dump(indent: String, startAddress: Int): String {
3333
return "${addressToString(startAddress)}: $indent i32 : ${value.owner} ;;\n"
@@ -39,7 +39,7 @@ class ConstantDataCharField(val value: WasmSymbol<Char>) : ConstantDataElement()
3939
class ConstantDataIntField(val value: WasmSymbol<Int>) : ConstantDataElement() {
4040
constructor(value: Int) : this(WasmSymbol(value))
4141

42-
override fun toBytes(): ByteArray = value.owner.toLittleEndianBytes()
42+
override fun toBytes(): ByteArray = ByteArray(4).apply { value.owner.toLittleEndianBytes(this, 0) }
4343

4444
override fun dump(indent: String, startAddress: Int): String {
4545
return "${addressToString(startAddress)}: $indent i32 : ${value.owner} ;;\n"
@@ -67,7 +67,11 @@ class ConstantDataIntegerArray(val value: List<Long>, val integerSize: Int) : Co
6767

6868
class ConstantDataIntArray(val value: List<WasmSymbol<Int>>) : ConstantDataElement() {
6969
override fun toBytes(): ByteArray {
70-
return value.fold(byteArrayOf()) { acc, el -> acc + el.owner.toLittleEndianBytes() }
70+
return ByteArray(value.size * 4).apply {
71+
for (index in value.indices) {
72+
value[index].owner.toLittleEndianBytes(this, index * 4)
73+
}
74+
}
7175
}
7276

7377
override fun dump(indent: String, startAddress: Int): String {
@@ -82,23 +86,31 @@ class ConstantDataCharArray(val value: List<WasmSymbol<Char>>, val fitsLatin1: B
8286
constructor(value: CharArray, fitsLatin1: Boolean) : this(value.map { WasmSymbol(it) }, fitsLatin1)
8387

8488
override fun toBytes(): ByteArray {
85-
return value
86-
.map { it.owner.toLittleEndianBytes(fitsLatin1) }
87-
.fold(byteArrayOf(), ByteArray::plus)
89+
return ByteArray(value.size * bytesPerChar).apply {
90+
value.forEachIndexed { index, symbol -> symbol.owner.toLittleEndianBytes(this, index * bytesPerChar, fitsLatin1) }
91+
}
8892
}
8993

9094
override fun dump(indent: String, startAddress: Int): String {
9195
if (value.isEmpty()) return ""
9296
return "${addressToString(startAddress)}: $indent i16[] : ${value.map { it.owner }.toCharArray().contentToString()} ;;\n"
9397
}
9498

95-
override val sizeInBytes: Int = value.size *
96-
if (fitsLatin1) BYTE_SIZE_BYTES else CHAR_SIZE_BYTES
99+
private val bytesPerChar = if (fitsLatin1) BYTE_SIZE_BYTES else CHAR_SIZE_BYTES
100+
101+
override val sizeInBytes: Int = value.size * bytesPerChar
102+
97103
}
98104

99105
class ConstantDataStruct(val elements: List<ConstantDataElement>) : ConstantDataElement() {
100106
override fun toBytes(): ByteArray {
101-
return elements.fold(byteArrayOf()) { acc, el -> acc + el.toBytes() }
107+
return buildList {
108+
elements.forEach {
109+
for (byte in it.toBytes()) {
110+
add(byte)
111+
}
112+
}
113+
}.toByteArray()
102114
}
103115

104116
override fun dump(indent: String, startAddress: Int): String {
@@ -113,7 +125,7 @@ class ConstantDataStruct(val elements: List<ConstantDataElement>) : ConstantData
113125
return res
114126
}
115127

116-
override val sizeInBytes: Int = elements.map { it.sizeInBytes }.sum()
128+
override val sizeInBytes: Int = elements.sumOf { it.sizeInBytes }
117129
}
118130

119131
fun Long.toLittleEndianBytesTo(to: ByteArray, offset: Int, size: Int) {
@@ -123,15 +135,16 @@ fun Long.toLittleEndianBytesTo(to: ByteArray, offset: Int, size: Int) {
123135
}
124136

125137

126-
fun Int.toLittleEndianBytes(): ByteArray {
127-
return ByteArray(4) {
128-
(this ushr (it * 8)).toByte()
129-
}
138+
fun Int.toLittleEndianBytes(to: ByteArray, offset: Int) {
139+
to[offset] = this.toByte()
140+
to[offset + 1] = (this ushr 8).toByte()
141+
to[offset + 2] = (this ushr 16).toByte()
142+
to[offset + 3] = (this ushr 24).toByte()
130143
}
131144

132-
fun Char.toLittleEndianBytes(fitsLatin1: Boolean): ByteArray {
133-
if (fitsLatin1) {
134-
return byteArrayOf((this.code and 0xFF).toByte())
145+
fun Char.toLittleEndianBytes(to: ByteArray, offset: Int, fitsLatin1: Boolean) {
146+
to[offset] = (this.code and 0xFF).toByte()
147+
if (!fitsLatin1) {
148+
to[offset + 1] = (this.code ushr Byte.SIZE_BITS).toByte()
135149
}
136-
return byteArrayOf((this.code and 0xFF).toByte(), (this.code ushr Byte.SIZE_BITS).toByte())
137150
}

0 commit comments

Comments
 (0)