1
1
mirror of https://github.com/wader/fq.git synced 2024-11-30 09:58:13 +03:00

luajit: explain LuaJITDecodeKNum, fix negative in bug

This commit is contained in:
Babz 2023-06-21 23:36:49 +02:00
parent 64c11bedfa
commit 751ee5a328
4 changed files with 140 additions and 1 deletions

View File

@ -261,10 +261,32 @@ func LuaJITDecodeKGC(d *decode.D) {
}
func LuaJITDecodeKNum(d *decode.D) any {
// knum = intU0 | (loU1 hiU)
// ...
// W = 32 bit, U = ULEB128 of W, U0/U1 = ULEB128 of W+1
// intU0 encodes 33 bits : a (signed) int32, plus the LSB=0
// loU1 encodes 33 bits : the lower half of a float64, plus the LSB=1
// hiU encodes 32 bits : the higher half of the float64
lo := d.ULEB128()
if lo&1 == 0 {
return lo >> 1
// we have an int32 (aka LuaJIT 'int')
// drop the LSB
lo_data := lo >> 1
// downcast to 32bits. should not overflow
lo_data_u32 := uint32(lo_data)
// make it a signed integer
lo_data_i32 := int32(lo_data_u32)
// return a larger type to make fq happy
return int64(lo_data_i32)
} else {
// we have float64 (aka LuaJIT 'number')
hi := d.ULEB128()
return u64tof64((hi << 32) + (lo >> 1))
}

110
format/luajit/testdata/negative.fqtest vendored Normal file
View File

@ -0,0 +1,110 @@
# luajit -b -s negative.lua negative.luac
$ fq dv negative.luac
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.{}: negative.luac (luajit) 0x0-0x58.7 (89)
| | | header{}: 0x0-0x4.7 (5)
0x00|1b 4c 4a |.LJ | magic: raw bits (valid) 0x0-0x2.7 (3)
0x00| 02 | . | version: 2 0x3-0x3.7 (1)
| | | flags{}: 0x4-0x4.7 (1)
0x00| 0a | . | raw: 10 0x4-0x4.7 (1)
| | | be: false 0x5-NA (0)
| | | strip: true 0x5-NA (0)
| | | ffi: false 0x5-NA (0)
| | | fr2: true 0x5-NA (0)
| | | proto[0:3]: 0x5-0x57.7 (83)
| | | [0]{}: proto 0x5-0x19.7 (21)
0x00| 14 | . | length: 20 0x5-0x5.7 (1)
| | | pdata{}: 0x6-0x19.7 (20)
| | | phead{}: 0x6-0xc.7 (7)
0x00| 00 | . | flags: 0 0x6-0x6.7 (1)
0x00| 01 | . | numparams: 1 0x7-0x7.7 (1)
0x00| 02 | . | framesize: 2 0x8-0x8.7 (1)
0x00| 00 | . | numuv: 0 0x9-0x9.7 (1)
0x00| 00 | . | numkgc: 0 0xa-0xa.7 (1)
0x00| 01 | . | numkn: 1 0xb-0xb.7 (1)
0x00| 02 | . | numbc: 2 0xc-0xc.7 (1)
| | | bcins[0:2]: 0xd-0x14.7 (8)
| | | [0]{}: ins 0xd-0x10.7 (4)
0x00| 18 | . | op: "MULVN" (24) 0xd-0xd.7 (1)
0x00| 01 | . | a: 1 0xe-0xe.7 (1)
0x00| 00| .| c: 0 0xf-0xf.7 (1)
0x10|00 |. | b: 0 0x10-0x10.7 (1)
| | | [1]{}: ins 0x11-0x14.7 (4)
0x10| 4c | L | op: "RET1" (76) 0x11-0x11.7 (1)
0x10| 01 | . | a: 1 0x12-0x12.7 (1)
0x10| 02 00 | .. | d: 2 0x13-0x14.7 (2)
| | | uvdata[0:0]: 0x15-NA (0)
| | | kgc[0:0]: 0x15-NA (0)
| | | knum[0:1]: 0x15-0x19.7 (5)
0x10| ae 86 95 fd 1f | ..... | [0]: -2973289 knum 0x15-0x19.7 (5)
| | | [1]{}: proto 0x1a-0x33.7 (26)
0x10| 19 | . | length: 25 0x1a-0x1a.7 (1)
| | | pdata{}: 0x1b-0x33.7 (25)
| | | phead{}: 0x1b-0x21.7 (7)
0x10| 00 | . | flags: 0 0x1b-0x1b.7 (1)
0x10| 01 | . | numparams: 1 0x1c-0x1c.7 (1)
0x10| 02 | . | framesize: 2 0x1d-0x1d.7 (1)
0x10| 00 | . | numuv: 0 0x1e-0x1e.7 (1)
0x10| 00| .| numkgc: 0 0x1f-0x1f.7 (1)
0x20|01 |. | numkn: 1 0x20-0x20.7 (1)
0x20| 02 | . | numbc: 2 0x21-0x21.7 (1)
| | | bcins[0:2]: 0x22-0x29.7 (8)
| | | [0]{}: ins 0x22-0x25.7 (4)
0x20| 18 | . | op: "MULVN" (24) 0x22-0x22.7 (1)
0x20| 01 | . | a: 1 0x23-0x23.7 (1)
0x20| 00 | . | c: 0 0x24-0x24.7 (1)
0x20| 00 | . | b: 0 0x25-0x25.7 (1)
| | | [1]{}: ins 0x26-0x29.7 (4)
0x20| 4c | L | op: "RET1" (76) 0x26-0x26.7 (1)
0x20| 01 | . | a: 1 0x27-0x27.7 (1)
0x20| 02 00 | .. | d: 2 0x28-0x29.7 (2)
| | | uvdata[0:0]: 0x2a-NA (0)
| | | kgc[0:0]: 0x2a-NA (0)
| | | knum[0:1]: 0x2a-0x33.7 (10)
0x20| 81 80 90 9d 0c 8a| ......| [0]: -3.8793457897e+10 knum 0x2a-0x33.7 (10)
0x30|a1 88 91 0c |.... |
| | | [2]{}: proto 0x34-0x57.7 (36)
0x30| 23 | # | length: 35 0x34-0x34.7 (1)
| | | pdata{}: 0x35-0x57.7 (35)
| | | phead{}: 0x35-0x3b.7 (7)
0x30| 03 | . | flags: 3 0x35-0x35.7 (1)
0x30| 00 | . | numparams: 0 0x36-0x36.7 (1)
0x30| 01 | . | framesize: 1 0x37-0x37.7 (1)
0x30| 00 | . | numuv: 0 0x38-0x38.7 (1)
0x30| 04 | . | numkgc: 4 0x39-0x39.7 (1)
0x30| 00 | . | numkn: 0 0x3a-0x3a.7 (1)
0x30| 05 | . | numbc: 5 0x3b-0x3b.7 (1)
| | | bcins[0:5]: 0x3c-0x4f.7 (20)
| | | [0]{}: ins 0x3c-0x3f.7 (4)
0x30| 33 | 3 | op: "FNEW" (51) 0x3c-0x3c.7 (1)
0x30| 00 | . | a: 0 0x3d-0x3d.7 (1)
0x30| 00 00| ..| d: 0 0x3e-0x3f.7 (2)
| | | [1]{}: ins 0x40-0x43.7 (4)
0x40|37 |7 | op: "GSET" (55) 0x40-0x40.7 (1)
0x40| 00 | . | a: 0 0x41-0x41.7 (1)
0x40| 01 00 | .. | d: 1 0x42-0x43.7 (2)
| | | [2]{}: ins 0x44-0x47.7 (4)
0x40| 33 | 3 | op: "FNEW" (51) 0x44-0x44.7 (1)
0x40| 00 | . | a: 0 0x45-0x45.7 (1)
0x40| 02 00 | .. | d: 2 0x46-0x47.7 (2)
| | | [3]{}: ins 0x48-0x4b.7 (4)
0x40| 37 | 7 | op: "GSET" (55) 0x48-0x48.7 (1)
0x40| 00 | . | a: 0 0x49-0x49.7 (1)
0x40| 03 00 | .. | d: 3 0x4a-0x4b.7 (2)
| | | [4]{}: ins 0x4c-0x4f.7 (4)
0x40| 4b | K | op: "RET0" (75) 0x4c-0x4c.7 (1)
0x40| 00 | . | a: 0 0x4d-0x4d.7 (1)
0x40| 01 00| ..| d: 1 0x4e-0x4f.7 (2)
| | | uvdata[0:0]: 0x50-NA (0)
| | | kgc[0:4]: 0x50-0x57.7 (8)
| | | [0]{}: kgc 0x50-0x52.7 (3)
0x50|07 |. | type: "str" (7) 0x50-0x50.7 (1)
0x50| 66 32 | f2 | value: "f2" 0x51-0x52.7 (2)
| | | [1]{}: kgc 0x53-0x53.7 (1)
0x50| 00 | . | type: "child" (0) 0x53-0x53.7 (1)
| | | [2]{}: kgc 0x54-0x56.7 (3)
0x50| 07 | . | type: "str" (7) 0x54-0x54.7 (1)
0x50| 66 31 | f1 | value: "f1" 0x55-0x56.7 (2)
| | | [3]{}: kgc 0x57-0x57.7 (1)
0x50| 00 | . | type: "child" (0) 0x57-0x57.7 (1)
| | | knum[0:0]: 0x58-NA (0)
0x50| 00| | .| | end: 0 0x58-0x58.7 (1)

7
format/luajit/testdata/negative.lua vendored Normal file
View File

@ -0,0 +1,7 @@
f1 = function(x)
return x * -2973289
end
f2 = function(x)
return x * -38793457897
end

BIN
format/luajit/testdata/negative.luac vendored Normal file

Binary file not shown.