opcode.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package lua
  2. import (
  3. "fmt"
  4. )
  5. /*
  6. gopherlua uses Lua 5.1.4's opcodes.
  7. Lua 5.1.4 opcodes layout:
  8. instruction = 32bit(fixed length)
  9. +---------------------------------------------+
  10. |0-5(6bits)|6-13(8bit)|14-22(9bit)|23-31(9bit)|
  11. |==========+==========+===========+===========|
  12. | opcode | A | C | B |
  13. |----------+----------+-----------+-----------|
  14. | opcode | A | Bx(unsigned) |
  15. |----------+----------+-----------+-----------|
  16. | opcode | A | sBx(signed) |
  17. +---------------------------------------------+
  18. */
  19. const opInvalidInstruction = ^uint32(0)
  20. const opSizeCode = 6
  21. const opSizeA = 8
  22. const opSizeB = 9
  23. const opSizeC = 9
  24. const opSizeBx = 18
  25. const opSizesBx = 18
  26. const opMaxArgsA = (1 << opSizeA) - 1
  27. const opMaxArgsB = (1 << opSizeB) - 1
  28. const opMaxArgsC = (1 << opSizeC) - 1
  29. const opMaxArgBx = (1 << opSizeBx) - 1
  30. const opMaxArgSbx = opMaxArgBx >> 1
  31. const (
  32. OP_MOVE int = iota /* A B R(A) := R(B) */
  33. OP_MOVEN /* A B R(A) := R(B); followed by R(C) MOVE ops */
  34. OP_LOADK /* A Bx R(A) := Kst(Bx) */
  35. OP_LOADBOOL /* A B C R(A) := (Bool)B; if (C) pc++ */
  36. OP_LOADNIL /* A B R(A) := ... := R(B) := nil */
  37. OP_GETUPVAL /* A B R(A) := UpValue[B] */
  38. OP_GETGLOBAL /* A Bx R(A) := Gbl[Kst(Bx)] */
  39. OP_GETTABLE /* A B C R(A) := R(B)[RK(C)] */
  40. OP_GETTABLEKS /* A B C R(A) := R(B)[RK(C)] ; RK(C) is constant string */
  41. OP_SETGLOBAL /* A Bx Gbl[Kst(Bx)] := R(A) */
  42. OP_SETUPVAL /* A B UpValue[B] := R(A) */
  43. OP_SETTABLE /* A B C R(A)[RK(B)] := RK(C) */
  44. OP_SETTABLEKS /* A B C R(A)[RK(B)] := RK(C) ; RK(B) is constant string */
  45. OP_NEWTABLE /* A B C R(A) := {} (size = BC) */
  46. OP_SELF /* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
  47. OP_ADD /* A B C R(A) := RK(B) + RK(C) */
  48. OP_SUB /* A B C R(A) := RK(B) - RK(C) */
  49. OP_MUL /* A B C R(A) := RK(B) * RK(C) */
  50. OP_DIV /* A B C R(A) := RK(B) / RK(C) */
  51. OP_MOD /* A B C R(A) := RK(B) % RK(C) */
  52. OP_POW /* A B C R(A) := RK(B) ^ RK(C) */
  53. OP_UNM /* A B R(A) := -R(B) */
  54. OP_NOT /* A B R(A) := not R(B) */
  55. OP_LEN /* A B R(A) := length of R(B) */
  56. OP_CONCAT /* A B C R(A) := R(B).. ... ..R(C) */
  57. OP_JMP /* sBx pc+=sBx */
  58. OP_EQ /* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
  59. OP_LT /* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
  60. OP_LE /* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
  61. OP_TEST /* A C if not (R(A) <=> C) then pc++ */
  62. OP_TESTSET /* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
  63. OP_CALL /* A B C R(A) ... R(A+C-2) := R(A)(R(A+1) ... R(A+B-1)) */
  64. OP_TAILCALL /* A B C return R(A)(R(A+1) ... R(A+B-1)) */
  65. OP_RETURN /* A B return R(A) ... R(A+B-2) (see note) */
  66. OP_FORLOOP /* A sBx R(A)+=R(A+2);
  67. if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
  68. OP_FORPREP /* A sBx R(A)-=R(A+2); pc+=sBx */
  69. OP_TFORLOOP /* A C R(A+3) ... R(A+3+C) := R(A)(R(A+1) R(A+2));
  70. if R(A+3) ~= nil then { pc++; R(A+2)=R(A+3); } */
  71. OP_SETLIST /* A B C R(A)[(C-1)*FPF+i] := R(A+i) 1 <= i <= B */
  72. OP_CLOSE /* A close all variables in the stack up to (>=) R(A)*/
  73. OP_CLOSURE /* A Bx R(A) := closure(KPROTO[Bx] R(A) ... R(A+n)) */
  74. OP_VARARG /* A B R(A) R(A+1) ... R(A+B-1) = vararg */
  75. OP_NOP /* NOP */
  76. )
  77. const opCodeMax = OP_NOP
  78. type opArgMode int
  79. const (
  80. opArgModeN opArgMode = iota
  81. opArgModeU
  82. opArgModeR
  83. opArgModeK
  84. )
  85. type opType int
  86. const (
  87. opTypeABC = iota
  88. opTypeABx
  89. opTypeASbx
  90. )
  91. type opProp struct {
  92. Name string
  93. IsTest bool
  94. SetRegA bool
  95. ModeArgB opArgMode
  96. ModeArgC opArgMode
  97. Type opType
  98. }
  99. var opProps = []opProp{
  100. opProp{"MOVE", false, true, opArgModeR, opArgModeN, opTypeABC},
  101. opProp{"MOVEN", false, true, opArgModeR, opArgModeN, opTypeABC},
  102. opProp{"LOADK", false, true, opArgModeK, opArgModeN, opTypeABx},
  103. opProp{"LOADBOOL", false, true, opArgModeU, opArgModeU, opTypeABC},
  104. opProp{"LOADNIL", false, true, opArgModeR, opArgModeN, opTypeABC},
  105. opProp{"GETUPVAL", false, true, opArgModeU, opArgModeN, opTypeABC},
  106. opProp{"GETGLOBAL", false, true, opArgModeK, opArgModeN, opTypeABx},
  107. opProp{"GETTABLE", false, true, opArgModeR, opArgModeK, opTypeABC},
  108. opProp{"GETTABLEKS", false, true, opArgModeR, opArgModeK, opTypeABC},
  109. opProp{"SETGLOBAL", false, false, opArgModeK, opArgModeN, opTypeABx},
  110. opProp{"SETUPVAL", false, false, opArgModeU, opArgModeN, opTypeABC},
  111. opProp{"SETTABLE", false, false, opArgModeK, opArgModeK, opTypeABC},
  112. opProp{"SETTABLEKS", false, false, opArgModeK, opArgModeK, opTypeABC},
  113. opProp{"NEWTABLE", false, true, opArgModeU, opArgModeU, opTypeABC},
  114. opProp{"SELF", false, true, opArgModeR, opArgModeK, opTypeABC},
  115. opProp{"ADD", false, true, opArgModeK, opArgModeK, opTypeABC},
  116. opProp{"SUB", false, true, opArgModeK, opArgModeK, opTypeABC},
  117. opProp{"MUL", false, true, opArgModeK, opArgModeK, opTypeABC},
  118. opProp{"DIV", false, true, opArgModeK, opArgModeK, opTypeABC},
  119. opProp{"MOD", false, true, opArgModeK, opArgModeK, opTypeABC},
  120. opProp{"POW", false, true, opArgModeK, opArgModeK, opTypeABC},
  121. opProp{"UNM", false, true, opArgModeR, opArgModeN, opTypeABC},
  122. opProp{"NOT", false, true, opArgModeR, opArgModeN, opTypeABC},
  123. opProp{"LEN", false, true, opArgModeR, opArgModeN, opTypeABC},
  124. opProp{"CONCAT", false, true, opArgModeR, opArgModeR, opTypeABC},
  125. opProp{"JMP", false, false, opArgModeR, opArgModeN, opTypeASbx},
  126. opProp{"EQ", true, false, opArgModeK, opArgModeK, opTypeABC},
  127. opProp{"LT", true, false, opArgModeK, opArgModeK, opTypeABC},
  128. opProp{"LE", true, false, opArgModeK, opArgModeK, opTypeABC},
  129. opProp{"TEST", true, true, opArgModeR, opArgModeU, opTypeABC},
  130. opProp{"TESTSET", true, true, opArgModeR, opArgModeU, opTypeABC},
  131. opProp{"CALL", false, true, opArgModeU, opArgModeU, opTypeABC},
  132. opProp{"TAILCALL", false, true, opArgModeU, opArgModeU, opTypeABC},
  133. opProp{"RETURN", false, false, opArgModeU, opArgModeN, opTypeABC},
  134. opProp{"FORLOOP", false, true, opArgModeR, opArgModeN, opTypeASbx},
  135. opProp{"FORPREP", false, true, opArgModeR, opArgModeN, opTypeASbx},
  136. opProp{"TFORLOOP", true, false, opArgModeN, opArgModeU, opTypeABC},
  137. opProp{"SETLIST", false, false, opArgModeU, opArgModeU, opTypeABC},
  138. opProp{"CLOSE", false, false, opArgModeN, opArgModeN, opTypeABC},
  139. opProp{"CLOSURE", false, true, opArgModeU, opArgModeN, opTypeABx},
  140. opProp{"VARARG", false, true, opArgModeU, opArgModeN, opTypeABC},
  141. opProp{"NOP", false, false, opArgModeR, opArgModeN, opTypeASbx},
  142. }
  143. func opGetOpCode(inst uint32) int {
  144. return int(inst >> 26)
  145. }
  146. func opSetOpCode(inst *uint32, opcode int) {
  147. *inst = (*inst & 0x3ffffff) | uint32(opcode<<26)
  148. }
  149. func opGetArgA(inst uint32) int {
  150. return int(inst>>18) & 0xff
  151. }
  152. func opSetArgA(inst *uint32, arg int) {
  153. *inst = (*inst & 0xfc03ffff) | uint32((arg&0xff)<<18)
  154. }
  155. func opGetArgB(inst uint32) int {
  156. return int(inst & 0x1ff)
  157. }
  158. func opSetArgB(inst *uint32, arg int) {
  159. *inst = (*inst & 0xfffffe00) | uint32(arg&0x1ff)
  160. }
  161. func opGetArgC(inst uint32) int {
  162. return int(inst>>9) & 0x1ff
  163. }
  164. func opSetArgC(inst *uint32, arg int) {
  165. *inst = (*inst & 0xfffc01ff) | uint32((arg&0x1ff)<<9)
  166. }
  167. func opGetArgBx(inst uint32) int {
  168. return int(inst & 0x3ffff)
  169. }
  170. func opSetArgBx(inst *uint32, arg int) {
  171. *inst = (*inst & 0xfffc0000) | uint32(arg&0x3ffff)
  172. }
  173. func opGetArgSbx(inst uint32) int {
  174. return opGetArgBx(inst) - opMaxArgSbx
  175. }
  176. func opSetArgSbx(inst *uint32, arg int) {
  177. opSetArgBx(inst, arg+opMaxArgSbx)
  178. }
  179. func opCreateABC(op int, a int, b int, c int) uint32 {
  180. var inst uint32 = 0
  181. opSetOpCode(&inst, op)
  182. opSetArgA(&inst, a)
  183. opSetArgB(&inst, b)
  184. opSetArgC(&inst, c)
  185. return inst
  186. }
  187. func opCreateABx(op int, a int, bx int) uint32 {
  188. var inst uint32 = 0
  189. opSetOpCode(&inst, op)
  190. opSetArgA(&inst, a)
  191. opSetArgBx(&inst, bx)
  192. return inst
  193. }
  194. func opCreateASbx(op int, a int, sbx int) uint32 {
  195. var inst uint32 = 0
  196. opSetOpCode(&inst, op)
  197. opSetArgA(&inst, a)
  198. opSetArgSbx(&inst, sbx)
  199. return inst
  200. }
  201. const opBitRk = 1 << (opSizeB - 1)
  202. const opMaxIndexRk = opBitRk - 1
  203. func opIsK(value int) bool {
  204. return bool((value & opBitRk) != 0)
  205. }
  206. func opIndexK(value int) int {
  207. return value & ^opBitRk
  208. }
  209. func opRkAsk(value int) int {
  210. return value | opBitRk
  211. }
  212. func opToString(inst uint32) string {
  213. op := opGetOpCode(inst)
  214. if op > opCodeMax {
  215. return ""
  216. }
  217. prop := &(opProps[op])
  218. arga := opGetArgA(inst)
  219. argb := opGetArgB(inst)
  220. argc := opGetArgC(inst)
  221. argbx := opGetArgBx(inst)
  222. argsbx := opGetArgSbx(inst)
  223. buf := ""
  224. switch prop.Type {
  225. case opTypeABC:
  226. buf = fmt.Sprintf("%s | %d, %d, %d", prop.Name, arga, argb, argc)
  227. case opTypeABx:
  228. buf = fmt.Sprintf("%s | %d, %d", prop.Name, arga, argbx)
  229. case opTypeASbx:
  230. buf = fmt.Sprintf("%s | %d, %d", prop.Name, arga, argsbx)
  231. }
  232. switch op {
  233. case OP_MOVE:
  234. buf += fmt.Sprintf("; R(%v) := R(%v)", arga, argb)
  235. case OP_MOVEN:
  236. buf += fmt.Sprintf("; R(%v) := R(%v); followed by %v MOVE ops", arga, argb, argc)
  237. case OP_LOADK:
  238. buf += fmt.Sprintf("; R(%v) := Kst(%v)", arga, argbx)
  239. case OP_LOADBOOL:
  240. buf += fmt.Sprintf("; R(%v) := (Bool)%v; if (%v) pc++", arga, argb, argc)
  241. case OP_LOADNIL:
  242. buf += fmt.Sprintf("; R(%v) := ... := R(%v) := nil", arga, argb)
  243. case OP_GETUPVAL:
  244. buf += fmt.Sprintf("; R(%v) := UpValue[%v]", arga, argb)
  245. case OP_GETGLOBAL:
  246. buf += fmt.Sprintf("; R(%v) := Gbl[Kst(%v)]", arga, argbx)
  247. case OP_GETTABLE:
  248. buf += fmt.Sprintf("; R(%v) := R(%v)[RK(%v)]", arga, argb, argc)
  249. case OP_GETTABLEKS:
  250. buf += fmt.Sprintf("; R(%v) := R(%v)[RK(%v)] ; RK(%v) is constant string", arga, argb, argc, argc)
  251. case OP_SETGLOBAL:
  252. buf += fmt.Sprintf("; Gbl[Kst(%v)] := R(%v)", argbx, arga)
  253. case OP_SETUPVAL:
  254. buf += fmt.Sprintf("; UpValue[%v] := R(%v)", argb, arga)
  255. case OP_SETTABLE:
  256. buf += fmt.Sprintf("; R(%v)[RK(%v)] := RK(%v)", arga, argb, argc)
  257. case OP_SETTABLEKS:
  258. buf += fmt.Sprintf("; R(%v)[RK(%v)] := RK(%v) ; RK(%v) is constant string", arga, argb, argc, argb)
  259. case OP_NEWTABLE:
  260. buf += fmt.Sprintf("; R(%v) := {} (size = BC)", arga)
  261. case OP_SELF:
  262. buf += fmt.Sprintf("; R(%v+1) := R(%v); R(%v) := R(%v)[RK(%v)]", arga, argb, arga, argb, argc)
  263. case OP_ADD:
  264. buf += fmt.Sprintf("; R(%v) := RK(%v) + RK(%v)", arga, argb, argc)
  265. case OP_SUB:
  266. buf += fmt.Sprintf("; R(%v) := RK(%v) - RK(%v)", arga, argb, argc)
  267. case OP_MUL:
  268. buf += fmt.Sprintf("; R(%v) := RK(%v) * RK(%v)", arga, argb, argc)
  269. case OP_DIV:
  270. buf += fmt.Sprintf("; R(%v) := RK(%v) / RK(%v)", arga, argb, argc)
  271. case OP_MOD:
  272. buf += fmt.Sprintf("; R(%v) := RK(%v) %% RK(%v)", arga, argb, argc)
  273. case OP_POW:
  274. buf += fmt.Sprintf("; R(%v) := RK(%v) ^ RK(%v)", arga, argb, argc)
  275. case OP_UNM:
  276. buf += fmt.Sprintf("; R(%v) := -R(%v)", arga, argb)
  277. case OP_NOT:
  278. buf += fmt.Sprintf("; R(%v) := not R(%v)", arga, argb)
  279. case OP_LEN:
  280. buf += fmt.Sprintf("; R(%v) := length of R(%v)", arga, argb)
  281. case OP_CONCAT:
  282. buf += fmt.Sprintf("; R(%v) := R(%v).. ... ..R(%v)", arga, argb, argc)
  283. case OP_JMP:
  284. buf += fmt.Sprintf("; pc+=%v", argsbx)
  285. case OP_EQ:
  286. buf += fmt.Sprintf("; if ((RK(%v) == RK(%v)) ~= %v) then pc++", argb, argc, arga)
  287. case OP_LT:
  288. buf += fmt.Sprintf("; if ((RK(%v) < RK(%v)) ~= %v) then pc++", argb, argc, arga)
  289. case OP_LE:
  290. buf += fmt.Sprintf("; if ((RK(%v) <= RK(%v)) ~= %v) then pc++", argb, argc, arga)
  291. case OP_TEST:
  292. buf += fmt.Sprintf("; if not (R(%v) <=> %v) then pc++", arga, argc)
  293. case OP_TESTSET:
  294. buf += fmt.Sprintf("; if (R(%v) <=> %v) then R(%v) := R(%v) else pc++", argb, argc, arga, argb)
  295. case OP_CALL:
  296. buf += fmt.Sprintf("; R(%v) ... R(%v+%v-2) := R(%v)(R(%v+1) ... R(%v+%v-1))", arga, arga, argc, arga, arga, arga, argb)
  297. case OP_TAILCALL:
  298. buf += fmt.Sprintf("; return R(%v)(R(%v+1) ... R(%v+%v-1))", arga, arga, arga, argb)
  299. case OP_RETURN:
  300. buf += fmt.Sprintf("; return R(%v) ... R(%v+%v-2)", arga, arga, argb)
  301. case OP_FORLOOP:
  302. buf += fmt.Sprintf("; R(%v)+=R(%v+2); if R(%v) <?= R(%v+1) then { pc+=%v; R(%v+3)=R(%v) }", arga, arga, arga, arga, argsbx, arga, arga)
  303. case OP_FORPREP:
  304. buf += fmt.Sprintf("; R(%v)-=R(%v+2); pc+=%v", arga, arga, argsbx)
  305. case OP_TFORLOOP:
  306. buf += fmt.Sprintf("; R(%v+3) ... R(%v+3+%v) := R(%v)(R(%v+1) R(%v+2)); if R(%v+3) ~= nil then { pc++; R(%v+2)=R(%v+3); }", arga, arga, argc, arga, arga, arga, arga, arga, arga)
  307. case OP_SETLIST:
  308. buf += fmt.Sprintf("; R(%v)[(%v-1)*FPF+i] := R(%v+i) 1 <= i <= %v", arga, argc, arga, argb)
  309. case OP_CLOSE:
  310. buf += fmt.Sprintf("; close all variables in the stack up to (>=) R(%v)", arga)
  311. case OP_CLOSURE:
  312. buf += fmt.Sprintf("; R(%v) := closure(KPROTO[%v] R(%v) ... R(%v+n))", arga, argbx, arga, arga)
  313. case OP_VARARG:
  314. buf += fmt.Sprintf("; R(%v) R(%v+1) ... R(%v+%v-1) = vararg", arga, arga, arga, argb)
  315. case OP_NOP:
  316. /* nothing to do */
  317. }
  318. return buf
  319. }