diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-08-27 13:59:39 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-08-27 13:59:39 -0300 |
commit | df13f259487459f3a28d31d76c890aa6c2d061e0 (patch) | |
tree | f354a0746530369d4dd70113c9752d019fbd200b /testes | |
parent | 643188d6e58dfd3270d689230867289347260b74 (diff) | |
download | lua-df13f259487459f3a28d31d76c890aa6c2d061e0.tar.gz lua-df13f259487459f3a28d31d76c890aa6c2d061e0.tar.bz2 lua-df13f259487459f3a28d31d76c890aa6c2d061e0.zip |
First version of OP_MMBIN opcodes
In arithmetic/bitwise operators, the call to metamethods is made
in a separate opcode following the main one. (The main
opcode skips this next one when the operation succeeds.) This
change reduces slightly the size of the binary and the complexity
of the arithmetic/bitwise opcodes. It also simplfies the treatment
of errors and yeld/resume in these operations, as there are much
fewer cases to consider. (Only OP_MMBIN/OP_MMBINI/OP_MMBINK,
instead of all variants of all arithmetic/bitwise operators.)
Diffstat (limited to 'testes')
-rw-r--r-- | testes/code.lua | 63 | ||||
-rw-r--r-- | testes/coroutine.lua | 2 |
2 files changed, 36 insertions, 29 deletions
diff --git a/testes/code.lua b/testes/code.lua index 3b768f33..fcb5c309 100644 --- a/testes/code.lua +++ b/testes/code.lua | |||
@@ -156,9 +156,9 @@ check(function () | |||
156 | c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b | 156 | c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b |
157 | end, | 157 | end, |
158 | 'LOADNIL', | 158 | 'LOADNIL', |
159 | 'MUL', | 159 | 'MUL', 'MMBIN', |
160 | 'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETFIELD', 'POW', | 160 | 'DIV', 'MMBIN', 'ADD', 'MMBIN', 'GETTABLE', 'SUB', 'MMBIN', |
161 | 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0') | 161 | 'GETFIELD', 'POW', 'MMBIN', 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0') |
162 | 162 | ||
163 | 163 | ||
164 | -- direct access to constants | 164 | -- direct access to constants |
@@ -188,7 +188,7 @@ check(function () | |||
188 | b = a/a | 188 | b = a/a |
189 | b = 5-4 | 189 | b = 5-4 |
190 | end, | 190 | end, |
191 | 'LOADNIL', 'SUB', 'DIV', 'LOADI', 'RETURN0') | 191 | 'LOADNIL', 'SUB', 'MMBIN', 'DIV', 'MMBIN', 'LOADI', 'RETURN0') |
192 | 192 | ||
193 | check(function () | 193 | check(function () |
194 | local a,b | 194 | local a,b |
@@ -292,38 +292,45 @@ checkK(function () return -(border + 1) end, -(sbx + 1.0)) | |||
292 | 292 | ||
293 | 293 | ||
294 | -- immediate operands | 294 | -- immediate operands |
295 | checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'RETURN1') | 295 | checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1') |
296 | checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1') | 296 | checkR(function (x) return 128 + x end, 0.0, 128.0, |
297 | checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1') | 297 | 'ADDI', 'MMBINI', 'RETURN1') |
298 | checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1') | 298 | checkR(function (x) return x * -127 end, -1.0, 127.0, |
299 | checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1') | 299 | 'MULI', 'MMBINI', 'RETURN1') |
300 | checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1') | 300 | checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'MMBINI', 'RETURN1') |
301 | checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1') | 301 | checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'MMBINI', 'RETURN1') |
302 | checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1') | 302 | checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'MMBINI', 'RETURN1') |
303 | checkR(function (x) return x // 1 end, 10.0, 10.0, | ||
304 | 'IDIVI', 'MMBINI', 'RETURN1') | ||
305 | checkR(function (x) return x % (100 - 10) end, 91, 1, | ||
306 | 'MODI', 'MMBINI', 'RETURN1') | ||
303 | checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'RETURN1') | 307 | checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'RETURN1') |
304 | checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') | 308 | checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') |
305 | checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') | 309 | checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') |
306 | checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1') | 310 | checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1') |
307 | checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'RETURN1') | 311 | checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1') |
308 | checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'RETURN1') | 312 | checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1') |
309 | 313 | ||
310 | -- K operands in arithmetic operations | 314 | -- K operands in arithmetic operations |
311 | checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'RETURN1') | 315 | checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'MMBINK', 'RETURN1') |
312 | -- check(function (x) return 128 + x end, 'ADDK', 'RETURN1') | 316 | -- check(function (x) return 128 + x end, 'ADDK', 'MMBINK', 'RETURN1') |
313 | checkR(function (x) return x * -10000 end, 2, -20000, 'MULK', 'RETURN1') | 317 | checkR(function (x) return x * -10000 end, 2, -20000, |
314 | -- check(function (x) return 20 * x end, 'MULK', 'RETURN1') | 318 | 'MULK', 'MMBINK', 'RETURN1') |
315 | checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'RETURN1') | 319 | -- check(function (x) return 20 * x end, 'MULK', 'MMBINK', 'RETURN1') |
316 | checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'RETURN1') | 320 | checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'MMBINK', 'RETURN1') |
317 | checkR(function (x) return x // 10000 end, 10000, 1, 'IDIVK', 'RETURN1') | 321 | checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'MMBINK', 'RETURN1') |
318 | checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 'MODK', 'RETURN1') | 322 | checkR(function (x) return x // 10000 end, 10000, 1, |
323 | 'IDIVK', 'MMBINK', 'RETURN1') | ||
324 | checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, | ||
325 | 'MODK', 'MMBINK', 'RETURN1') | ||
319 | 326 | ||
320 | -- no foldings (and immediate operands) | 327 | -- no foldings (and immediate operands) |
321 | check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') | 328 | check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') |
322 | check(function () return k3/0 end, 'LOADI', 'DIVI', 'RETURN1') | 329 | check(function () return k3/0 end, 'LOADI', 'DIVI', 'MMBINI', 'RETURN1') |
323 | check(function () return 0%0 end, 'LOADI', 'MODI', 'RETURN1') | 330 | check(function () return 0%0 end, 'LOADI', 'MODI', 'MMBINI', 'RETURN1') |
324 | check(function () return -4//0 end, 'LOADI', 'IDIVI', 'RETURN1') | 331 | check(function () return -4//0 end, 'LOADI', 'IDIVI', 'MMBINI', 'RETURN1') |
325 | check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1') | 332 | check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1') |
326 | check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'RETURN1') | 333 | check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1') |
327 | 334 | ||
328 | -- basic 'for' loops | 335 | -- basic 'for' loops |
329 | check(function () for i = -10, 10.5 do end end, | 336 | check(function () for i = -10, 10.5 do end end, |
@@ -379,7 +386,7 @@ check(function (a, b) | |||
379 | if b then break else a = a + 1 end | 386 | if b then break else a = a + 1 end |
380 | end | 387 | end |
381 | end, | 388 | end, |
382 | 'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'JMP', 'RETURN0') | 389 | 'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0') |
383 | 390 | ||
384 | checkequal( | 391 | checkequal( |
385 | function (a) while a < 10 do a = a + 1 end end, | 392 | function (a) while a < 10 do a = a + 1 end end, |
diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 48f4c5bf..81d848a3 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua | |||
@@ -751,7 +751,7 @@ assert(run(function () return a >> 2 end, {"shr"}) == 10 >> 2) | |||
751 | assert(run(function () return 1 >> a end, {"shr"}) == 1 >> 10) | 751 | assert(run(function () return 1 >> a end, {"shr"}) == 1 >> 10) |
752 | assert(run(function () return a << 2 end, {"shl"}) == 10 << 2) | 752 | assert(run(function () return a << 2 end, {"shl"}) == 10 << 2) |
753 | assert(run(function () return 1 << a end, {"shl"}) == 1 << 10) | 753 | assert(run(function () return 1 << a end, {"shl"}) == 1 << 10) |
754 | assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2) | 754 | assert(run(function () return 2 ~ a end, {"bxor"}) == 2 ~ 10) |
755 | 755 | ||
756 | 756 | ||
757 | assert(run(function () return a..b end, {"concat"}) == "1012") | 757 | assert(run(function () return a..b end, {"concat"}) == "1012") |