src/ - luajit-2.0-src

- Makefile

- host/

- jit/

- lauxlib.h

- lib_aux.c

- lib_base.c

- lib_bit.c

- lib_debug.c

- lib_ffi.c

- lib_init.c

- lib_io.c

- lib_jit.c

- lib_math.c

- lib_os.c

- lib_package.c

- lib_string.c

- lib_table.c

- lj_alloc.c

- lj_alloc.h

- lj_api.c

- lj_arch.h

- lj_asm.c

- lj_asm.h

- lj_asm_arm.h

- lj_asm_mips.h

- lj_asm_ppc.h

- lj_asm_x86.h

- lj_bc.c

- lj_bc.h

- lj_bcdef.h

- lj_bcdump.h

- lj_bcread.c

- lj_bcwrite.c

- lj_buf.c

- lj_buf.h

- lj_carith.c

- lj_carith.h

- lj_ccall.c

- lj_ccall.h

- lj_ccallback.c

- lj_ccallback.h

- lj_cconv.c

- lj_cconv.h

- lj_cdata.c

- lj_cdata.h

- lj_char.c

- lj_char.h

- lj_clib.c

- lj_clib.h

- lj_cparse.c

- lj_cparse.h

- lj_crecord.c

- lj_crecord.h

- lj_ctype.c

- lj_ctype.h

- lj_debug.c

- lj_debug.h

- lj_def.h

- lj_dispatch.c

- lj_dispatch.h

- lj_emit_arm.h

- lj_emit_mips.h

- lj_emit_ppc.h

- lj_emit_x86.h

- lj_err.c

- lj_err.h

- lj_errmsg.h

- lj_ff.h

- lj_ffdef.h

- lj_ffrecord.c

- lj_ffrecord.h

- lj_folddef.h

- lj_frame.h

- lj_func.c

- lj_func.h

- lj_gc.c

- lj_gc.h

- lj_gdbjit.c

- lj_gdbjit.h

- lj_ir.c

- lj_ir.h

- lj_ircall.h

- lj_iropt.h

- lj_jit.h

- lj_lex.c

- lj_lex.h

- lj_lib.c

- lj_lib.h

- lj_libdef.h

- lj_load.c

- lj_mcode.c

- lj_mcode.h

- lj_meta.c

- lj_meta.h

- lj_obj.c

- lj_obj.h

- lj_opt_dce.c

- lj_opt_fold.c

- lj_opt_loop.c

- lj_opt_mem.c

- lj_opt_narrow.c

- lj_opt_sink.c

- lj_opt_split.c

- lj_parse.c

- lj_parse.h

- lj_profile.c

- lj_profile.h

- lj_recdef.h

- lj_record.c

- lj_record.h

- lj_snap.c

- lj_snap.h

- lj_state.c

- lj_state.h

- lj_str.c

- lj_str.h

- lj_strfmt.c

- lj_strfmt.h

- lj_strscan.c

- lj_strscan.h

- lj_tab.c

- lj_tab.h

- lj_target.h

- lj_target_arm.h

- lj_target_arm64.h

- lj_target_mips.h

- lj_target_ppc.h

- lj_target_x86.h

- lj_trace.c

- lj_trace.h

- lj_traceerr.h

- lj_udata.c

- lj_udata.h

- lj_vm.S

- lj_vm.h

- lj_vm.s

- lj_vmevent.c

- lj_vmevent.h

- lj_vmmath.c

- ljamalg.c

- lua.h

- lua.hpp

- luaconf.h

- luajit.c

- luajit.h

- lualib.h

- msvcbuild.bat

- ps4build.bat

- psvitabuild.bat

- xedkbuild.bat

One Level Up                               Top Level

src/Makefile - luajit-2.0-src

1. ##############################################################################

2. # LuaJIT Makefile. Requires GNU Make.

3. #

4. # Please read doc/install.html before changing any variables!

5. #

6. # Suitable for POSIX platforms (Linux, *BSD, OSX etc.).

7. # Also works with MinGW and Cygwin on Windows.

8. # Please check msvcbuild.bat for building with MSVC on Windows.

9. #

10. # Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

11. ##############################################################################

13. MAJVER=  2

14. MINVER=  1

15. RELVER=  0

16. ABIVER=  5.1

17. NODOTABIVER= 51

19. ##############################################################################

20. #############################  COMPILER OPTIONS  #############################

21. ##############################################################################

22. # These options mainly affect the speed of the JIT compiler itself, not the

23. # speed of the JIT-compiled code. Turn any of the optional settings on by

24. # removing the '#' in front of them. Make sure you force a full recompile

25. # with "make clean", followed by "make" if you change any options.

26. #

27. # LuaJIT builds as a native 32 or 64 bit binary by default.

28. CC= gcc

29. #

30. # Use this if you want to force a 32 bit build on a 64 bit multilib OS.

31. #CC= gcc -m32

32. #

33. # Since the assembler part does NOT maintain a frame pointer, it's pointless

34. # to slow down the C part by not omitting it. Debugging, tracebacks and

35. # unwinding are not affected -- the assembler part has frame unwind

36. # information and GCC emits it where needed (x64) or with -g (see CCDEBUG).

37. CCOPT= -O2 -fomit-frame-pointer

38. # Use this if you want to generate a smaller binary (but it's slower):

39. #CCOPT= -Os -fomit-frame-pointer

40. # Note: it's no longer recommended to use -O3 with GCC 4.x.

41. # The I-Cache bloat usually outweighs the benefits from aggressive inlining.

42. #

43. # Target-specific compiler options:

44. #

45. # x86/x64 only: For GCC 4.2 or higher and if you don't intend to distribute

46. # the binaries to a different machine you could also use: -march=native

47. #

48. CCOPT_x86= -march=i686 -msse -msse2 -mfpmath=sse

49. CCOPT_x64=

50. CCOPT_arm=

51. CCOPT_arm64=

52. CCOPT_ppc=

53. CCOPT_mips=

54. #

55. CCDEBUG=

56. # Uncomment the next line to generate debug information:

57. #CCDEBUG= -g

58. #

59. CCWARN= -Wall

60. # Uncomment the next line to enable more warnings:

61. #CCWARN+= -Wextra -Wdeclaration-after-statement -Wredundant-decls -Wshadow -Wpointer-arith

62. #

63. ##############################################################################

65. ##############################################################################

66. ################################  BUILD MODE  ################################

67. ##############################################################################

68. # The default build mode is mixed mode on POSIX. On Windows this is the same

69. # as dynamic mode.

70. #

71. # Mixed mode creates a static + dynamic library and a statically linked luajit.

72. BUILDMODE= mixed

73. #

74. # Static mode creates a static library and a statically linked luajit.

75. #BUILDMODE= static

76. #

77. # Dynamic mode creates a dynamic library and a dynamically linked luajit.

78. # Note: this executable will only run when the library is installed!

79. #BUILDMODE= dynamic

80. #

81. ##############################################################################

83. ##############################################################################

84. #################################  FEATURES  #################################

85. ##############################################################################

86. # Enable/disable these features as needed, but make sure you force a full

87. # recompile with "make clean", followed by "make".

88. XCFLAGS=

89. #

90. # Permanently disable the FFI extension to reduce the size of the LuaJIT

91. # executable. But please consider that the FFI library is compiled-in,

92. # but NOT loaded by default. It only allocates any memory, if you actually

93. # make use of it.

94. #XCFLAGS+= -DLUAJIT_DISABLE_FFI

95. #

96. # Features from Lua 5.2 that are unlikely to break existing code are

97. # enabled by default. Some other features that *might* break some existing

98. # code (e.g. __pairs or os.execute() return values) can be enabled here.

99. # Note: this does not provide full compatibility with Lua 5.2 at this time.

100. #XCFLAGS+= -DLUAJIT_ENABLE_LUA52COMPAT

101. #

102. # Disable the JIT compiler, i.e. turn LuaJIT into a pure interpreter.

103. #XCFLAGS+= -DLUAJIT_DISABLE_JIT

104. #

105. # Some architectures (e.g. PPC) can use either single-number (1) or

106. # dual-number (2) mode. Uncomment one of these lines to override the

107. # default mode. Please see LJ_ARCH_NUMMODE in lj_arch.h for details.

108. #XCFLAGS+= -DLUAJIT_NUMMODE=1

109. #XCFLAGS+= -DLUAJIT_NUMMODE=2

110. #

111. ##############################################################################

113. ##############################################################################

114. ############################  DEBUGGING SUPPORT  #############################

115. ##############################################################################

116. # Enable these options as needed, but make sure you force a full recompile

117. # with "make clean", followed by "make".

118. # Note that most of these are NOT suitable for benchmarking or release mode!

119. #

120. # Use the system provided memory allocator (realloc) instead of the

121. # bundled memory allocator. This is slower, but sometimes helpful for

122. # debugging. This option cannot be enabled on x64, since realloc usually

123. # doesn't return addresses in the right address range.

124. # OTOH this option is mandatory for Valgrind's memcheck tool on x64 and

125. # the only way to get useful results from it for all other architectures.

126. #XCFLAGS+= -DLUAJIT_USE_SYSMALLOC

127. #

128. # This define is required to run LuaJIT under Valgrind. The Valgrind

129. # header files must be installed. You should enable debug information, too.

130. # Use --suppressions=lj.supp to avoid some false positives.

131. #XCFLAGS+= -DLUAJIT_USE_VALGRIND

132. #

133. # This is the client for the GDB JIT API. GDB 7.0 or higher is required

134. # to make use of it. See lj_gdbjit.c for details. Enabling this causes

135. # a non-negligible overhead, even when not running under GDB.

136. #XCFLAGS+= -DLUAJIT_USE_GDBJIT

137. #

138. # Turn on assertions for the Lua/C API to debug problems with lua_* calls.

139. # This is rather slow -- use only while developing C libraries/embeddings.

140. #XCFLAGS+= -DLUA_USE_APICHECK

141. #

142. # Turn on assertions for the whole LuaJIT VM. This significantly slows down

143. # everything. Use only if you suspect a problem with LuaJIT itself.

144. #XCFLAGS+= -DLUA_USE_ASSERT

145. #

146. ##############################################################################

147. # You probably don't need to change anything below this line!

148. ##############################################################################

150. ##############################################################################

151. # Flags and options for host and target.

152. ##############################################################################

154. # You can override the following variables at the make command line:

155. #   CC       HOST_CC       STATIC_CC       DYNAMIC_CC

156. #   CFLAGS   HOST_CFLAGS   TARGET_CFLAGS

157. #   LDFLAGS  HOST_LDFLAGS  TARGET_LDFLAGS  TARGET_SHLDFLAGS

158. #   LIBS     HOST_LIBS     TARGET_LIBS

159. #   CROSS    HOST_SYS      TARGET_SYS      TARGET_FLAGS

160. #

161. # Cross-compilation examples:

162. #   make HOST_CC="gcc -m32" CROSS=i586-mingw32msvc- TARGET_SYS=Windows

163. #   make HOST_CC="gcc -m32" CROSS=powerpc-linux-gnu-

165. ASOPTIONS= $(CCOPT) $(CCWARN) $(XCFLAGS) $(CFLAGS)

166. CCOPTIONS= $(CCDEBUG) $(ASOPTIONS)

167. LDOPTIONS= $(CCDEBUG) $(LDFLAGS)

169. HOST_CC= $(CC)

170. HOST_RM= rm -f

171. # If left blank, minilua is built and used. You can supply an installed

172. # copy of (plain) Lua 5.1 or 5.2, plus Lua BitOp. E.g. with: HOST_LUA=lua

173. HOST_LUA=

175. HOST_XCFLAGS= -I.

176. HOST_XLDFLAGS=

177. HOST_XLIBS=

178. HOST_ACFLAGS= $(CCOPTIONS) $(HOST_XCFLAGS) $(TARGET_ARCH) $(HOST_CFLAGS)

179. HOST_ALDFLAGS= $(LDOPTIONS) $(HOST_XLDFLAGS) $(HOST_LDFLAGS)

180. HOST_ALIBS= $(HOST_XLIBS) $(LIBS) $(HOST_LIBS)

182. STATIC_CC = $(CROSS)$(CC)

183. DYNAMIC_CC = $(CROSS)$(CC) -fPIC

184. TARGET_CC= $(STATIC_CC)

185. TARGET_STCC= $(STATIC_CC)

186. TARGET_DYNCC= $(DYNAMIC_CC)

187. TARGET_LD= $(CROSS)$(CC)

188. TARGET_AR= $(CROSS)ar rcus

189. TARGET_STRIP= $(CROSS)strip

191. TARGET_LIBPATH= $(or $(PREFIX),/usr/local)/$(or $(MULTILIB),lib)

192. TARGET_SONAME= libluajit-$(ABIVER).so.$(MAJVER)

193. TARGET_DYLIBNAME= libluajit-$(ABIVER).$(MAJVER).dylib

194. TARGET_DYLIBPATH= $(TARGET_LIBPATH)/$(TARGET_DYLIBNAME)

195. TARGET_DLLNAME= lua$(NODOTABIVER).dll

196. TARGET_XSHLDFLAGS= -shared -fPIC -Wl,-soname,$(TARGET_SONAME)

197. TARGET_DYNXLDOPTS=

199. TARGET_LFSFLAGS= -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE

200. TARGET_XCFLAGS= $(TARGET_LFSFLAGS) -U_FORTIFY_SOURCE

201. TARGET_XLDFLAGS=

202. TARGET_XLIBS= -lm

203. TARGET_TCFLAGS= $(CCOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS)

204. TARGET_ACFLAGS= $(CCOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS)

205. TARGET_ASFLAGS= $(ASOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS)

206. TARGET_ALDFLAGS= $(LDOPTIONS) $(TARGET_XLDFLAGS) $(TARGET_FLAGS) $(TARGET_LDFLAGS)

207. TARGET_ASHLDFLAGS= $(LDOPTIONS) $(TARGET_XSHLDFLAGS) $(TARGET_FLAGS) $(TARGET_SHLDFLAGS)

208. TARGET_ALIBS= $(TARGET_XLIBS) $(LIBS) $(TARGET_LIBS)

210. TARGET_TESTARCH=$(shell $(TARGET_CC) $(TARGET_TCFLAGS) -E lj_arch.h -dM)

211. ifneq (,$(findstring LJ_TARGET_X64 ,$(TARGET_TESTARCH)))

212. TARGET_LJARCH= x64

213. else

214. ifneq (,$(findstring LJ_TARGET_X86 ,$(TARGET_TESTARCH)))

215. TARGET_LJARCH= x86

216. else

217. ifneq (,$(findstring LJ_TARGET_ARM ,$(TARGET_TESTARCH)))

218. TARGET_LJARCH= arm

219. else

220. ifneq (,$(findstring LJ_TARGET_ARM64 ,$(TARGET_TESTARCH)))

221. TARGET_LJARCH= arm64

222. else

223. ifneq (,$(findstring LJ_TARGET_PPC ,$(TARGET_TESTARCH)))

224. TARGET_LJARCH= ppc

225. else

226. ifneq (,$(findstring LJ_TARGET_MIPS ,$(TARGET_TESTARCH)))

227. ifneq (,$(findstring MIPSEL ,$(TARGET_TESTARCH)))

228. TARGET_ARCH= -D__MIPSEL__=1

229. endif

230. TARGET_LJARCH= mips

231. else

232. $(error Unsupported target architecture)

233. endif

234. endif

235. endif

236. endif

237. endif

238. endif

240. ifneq (,$(findstring LJ_TARGET_PS3 1,$(TARGET_TESTARCH)))

241. TARGET_SYS= PS3

242. TARGET_ARCH+= -D__CELLOS_LV2__

243. TARGET_XCFLAGS+= -DLUAJIT_USE_SYSMALLOC

244. TARGET_XLIBS+= -lpthread

245. endif

246. ifneq (,$(findstring LJ_NO_UNWIND 1,$(TARGET_TESTARCH)))

247. TARGET_ARCH+= -DLUAJIT_NO_UNWIND

248. endif

250. TARGET_XCFLAGS+= $(CCOPT_$(TARGET_LJARCH))

251. TARGET_ARCH+= $(patsubst %,-DLUAJIT_TARGET=LUAJIT_ARCH_%,$(TARGET_LJARCH))

253. ifneq (,$(PREFIX))

254. ifneq (/usr/local,$(PREFIX))

255. TARGET_XCFLAGS+= -DLUA_ROOT=\"$(PREFIX)\"

256. ifneq (/usr,$(PREFIX))

257. TARGET_DYNXLDOPTS= -Wl,-rpath,$(TARGET_LIBPATH)

258. endif

259. endif

260. endif

261. ifneq (,$(MULTILIB))

262. TARGET_XCFLAGS+= -DLUA_MULTILIB=\"$(MULTILIB)\"

263. endif

264. ifneq (,$(LMULTILIB))

265. TARGET_XCFLAGS+= -DLUA_LMULTILIB=\"$(LMULTILIB)\"

266. endif

268. ##############################################################################

269. # System detection.

270. ##############################################################################

272. ifeq (Windows,$(findstring Windows,$(OS))$(MSYSTEM)$(TERM))

273. HOST_SYS= Windows

274. HOST_RM= del

275. else

276. HOST_SYS:= $(shell uname -s)

277. ifneq (,$(findstring MINGW,$(HOST_SYS)))

278. HOST_SYS= Windows

279. HOST_MSYS= mingw

280. endif

281. ifneq (,$(findstring CYGWIN,$(HOST_SYS)))

282. HOST_SYS= Windows

283. HOST_MSYS= cygwin

284. endif

285. endif

287. TARGET_SYS?= $(HOST_SYS)

288. ifeq (Windows,$(TARGET_SYS))

289. TARGET_STRIP+= --strip-unneeded

290. TARGET_XSHLDFLAGS= -shared

291. TARGET_DYNXLDOPTS=

292. else

293. ifeq (,$(shell $(TARGET_CC) -o /dev/null -c -x c /dev/null -fno-stack-protector 2>/dev/null || echo 1))

294. TARGET_XCFLAGS+= -fno-stack-protector

295. endif

296. ifeq (Darwin,$(TARGET_SYS))

297. ifeq (,$(MACOSX_DEPLOYMENT_TARGET))

298. export MACOSX_DEPLOYMENT_TARGET=10.4

299. endif

300. TARGET_STRIP+= -x

301. TARGET_AR+= 2>/dev/null

302. TARGET_XSHLDFLAGS= -dynamiclib -single_module -undefined dynamic_lookup -fPIC

303. TARGET_DYNXLDOPTS=

304. TARGET_XSHLDFLAGS+= -install_name $(TARGET_DYLIBPATH) -compatibility_version $(MAJVER).$(MINVER) -current_version $(MAJVER).$(MINVER).$(RELVER)

305. ifeq (x64,$(TARGET_LJARCH))

306. TARGET_XLDFLAGS+= -pagezero_size 10000 -image_base 100000000

307. TARGET_XSHLDFLAGS+= -image_base 7fff04c4a000

308. endif

309. else

310. ifeq (iOS,$(TARGET_SYS))

311. TARGET_STRIP+= -x

312. TARGET_AR+= 2>/dev/null

313. TARGET_XSHLDFLAGS= -dynamiclib -single_module -undefined dynamic_lookup -fPIC

314. TARGET_DYNXLDOPTS=

315. TARGET_XSHLDFLAGS+= -install_name $(TARGET_DYLIBPATH) -compatibility_version $(MAJVER).$(MINVER) -current_version $(MAJVER).$(MINVER).$(RELVER)

316. ifeq (arm64,$(TARGET_LJARCH))

317. TARGET_XCFLAGS+= -fno-omit-frame-pointer

318. endif

319. else

320. ifneq (SunOS,$(TARGET_SYS))

321. ifneq (PS3,$(TARGET_SYS))

322. TARGET_XLDFLAGS+= -Wl,-E

323. endif

324. endif

325. ifeq (Linux,$(TARGET_SYS))

326. TARGET_XLIBS+= -ldl

327. endif

328. ifeq (GNU/kFreeBSD,$(TARGET_SYS))

329. TARGET_XLIBS+= -ldl

330. endif

331. endif

332. endif

333. endif

335. ifneq ($(HOST_SYS),$(TARGET_SYS))

336. ifeq (Windows,$(TARGET_SYS))

337. HOST_XCFLAGS+= -malign-double -DLUAJIT_OS=LUAJIT_OS_WINDOWS

338. else

339. ifeq (Linux,$(TARGET_SYS))

340. HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_LINUX

341. else

342. ifeq (Darwin,$(TARGET_SYS))

343. HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OSX

344. else

345. ifeq (iOS,$(TARGET_SYS))

346. HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OSX

347. else

348. HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_OTHER

349. endif

350. endif

351. endif

352. endif

353. endif

355. ifneq (,$(CCDEBUG))

356. TARGET_STRIP= @:

357. endif

359. ##############################################################################

360. # Files and pathnames.

361. ##############################################################################

363. MINILUA_O= host/minilua.o

364. MINILUA_LIBS= -lm

365. MINILUA_T= host/minilua

366. MINILUA_X= $(MINILUA_T)

368. ifeq (,$(HOST_LUA))

369. HOST_LUA= $(MINILUA_X)

370. DASM_DEP= $(MINILUA_T)

371. endif

373. DASM_DIR= ../dynasm

374. DASM= $(HOST_LUA) $(DASM_DIR)/dynasm.lua

375. DASM_XFLAGS=

376. DASM_AFLAGS=

377. DASM_ARCH= $(TARGET_LJARCH)

379. ifneq (,$(findstring LJ_ARCH_BITS 64,$(TARGET_TESTARCH)))

380. DASM_AFLAGS+= -D P64

381. endif

382. ifneq (,$(findstring LJ_HASJIT 1,$(TARGET_TESTARCH)))

383. DASM_AFLAGS+= -D JIT

384. endif

385. ifneq (,$(findstring LJ_HASFFI 1,$(TARGET_TESTARCH)))

386. DASM_AFLAGS+= -D FFI

387. endif

388. ifneq (,$(findstring LJ_DUALNUM 1,$(TARGET_TESTARCH)))

389. DASM_AFLAGS+= -D DUALNUM

390. endif

391. ifneq (,$(findstring LJ_ARCH_HASFPU 1,$(TARGET_TESTARCH)))

392. DASM_AFLAGS+= -D FPU

393. TARGET_ARCH+= -DLJ_ARCH_HASFPU=1

394. else

395. TARGET_ARCH+= -DLJ_ARCH_HASFPU=0

396. endif

397. ifeq (,$(findstring LJ_ABI_SOFTFP 1,$(TARGET_TESTARCH)))

398. DASM_AFLAGS+= -D HFABI

399. TARGET_ARCH+= -DLJ_ABI_SOFTFP=0

400. else

401. TARGET_ARCH+= -DLJ_ABI_SOFTFP=1

402. endif

403. DASM_AFLAGS+= -D VER=$(subst LJ_ARCH_VERSION_,,$(filter LJ_ARCH_VERSION_%,$(subst LJ_ARCH_VERSION ,LJ_ARCH_VERSION_,$(TARGET_TESTARCH))))

404. ifeq (Windows,$(TARGET_SYS))

405. DASM_AFLAGS+= -D WIN

406. endif

407. ifeq (x64,$(TARGET_LJARCH))

408. ifeq (,$(findstring LJ_FR2 1,$(TARGET_TESTARCH)))

409. DASM_ARCH= x86

410. endif

411. else

412. ifeq (arm,$(TARGET_LJARCH))

413. ifeq (iOS,$(TARGET_SYS))

414. DASM_AFLAGS+= -D IOS

415. endif

416. else

417. ifeq (ppc,$(TARGET_LJARCH))

418. ifneq (,$(findstring LJ_ARCH_SQRT 1,$(TARGET_TESTARCH)))

419. DASM_AFLAGS+= -D SQRT

420. endif

421. ifneq (,$(findstring LJ_ARCH_ROUND 1,$(TARGET_TESTARCH)))

422. DASM_AFLAGS+= -D ROUND

423. endif

424. ifneq (,$(findstring LJ_ARCH_PPC32ON64 1,$(TARGET_TESTARCH)))

425. DASM_AFLAGS+= -D GPR64

426. endif

427. ifeq (PS3,$(TARGET_SYS))

428. DASM_AFLAGS+= -D PPE -D TOC

429. endif

430. ifneq (,$(findstring LJ_ARCH_PPC64 ,$(TARGET_TESTARCH)))

431. DASM_ARCH= ppc64

432. endif

433. endif

434. endif

435. endif

437. DASM_FLAGS= $(DASM_XFLAGS) $(DASM_AFLAGS)

438. DASM_DASC= vm_$(DASM_ARCH).dasc

440. BUILDVM_O= host/buildvm.o host/buildvm_asm.o host/buildvm_peobj.o \

441. host/buildvm_lib.o host/buildvm_fold.o

442. BUILDVM_T= host/buildvm

443. BUILDVM_X= $(BUILDVM_T)

445. HOST_O= $(MINILUA_O) $(BUILDVM_O)

446. HOST_T= $(MINILUA_T) $(BUILDVM_T)

448. LJVM_S= lj_vm.S

449. LJVM_O= lj_vm.o

450. LJVM_BOUT= $(LJVM_S)

451. LJVM_MODE= elfasm

453. LJLIB_O= lib_base.o lib_math.o lib_bit.o lib_string.o lib_table.o \

454. lib_io.o lib_os.o lib_package.o lib_debug.o lib_jit.o lib_ffi.o

455. LJLIB_C= $(LJLIB_O:.o=.c)

457. LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o lj_buf.o \

458. lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \

459. lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \

460. lj_strfmt.o lj_api.o lj_profile.o \

461. lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \

462. lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \

463. lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \

464. lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \

465. lj_asm.o lj_trace.o lj_gdbjit.o \

466. lj_ctype.o lj_cdata.o lj_cconv.o lj_ccall.o lj_ccallback.o \

467. lj_carith.o lj_clib.o lj_cparse.o \

468. lj_lib.o lj_alloc.o lib_aux.o \

469. $(LJLIB_O) lib_init.o

471. LJVMCORE_O= $(LJVM_O) $(LJCORE_O)

472. LJVMCORE_DYNO= $(LJVMCORE_O:.o=_dyn.o)

474. LIB_VMDEF= jit/vmdef.lua

475. LIB_VMDEFP= $(LIB_VMDEF)

477. LUAJIT_O= luajit.o

478. LUAJIT_A= libluajit.a

479. LUAJIT_SO= libluajit.so

480. LUAJIT_T= luajit

482. ALL_T= $(LUAJIT_T) $(LUAJIT_A) $(LUAJIT_SO) $(HOST_T)

483. ALL_HDRGEN= lj_bcdef.h lj_ffdef.h lj_libdef.h lj_recdef.h lj_folddef.h \

484. host/buildvm_arch.h

485. ALL_GEN= $(LJVM_S) $(ALL_HDRGEN) $(LIB_VMDEFP)

486. WIN_RM= *.obj *.lib *.exp *.dll *.exe *.manifest *.pdb *.ilk

487. ALL_RM= $(ALL_T) $(ALL_GEN) *.o host/*.o $(WIN_RM)

489. ##############################################################################

490. # Build mode handling.

491. ##############################################################################

493. # Mixed mode defaults.

494. TARGET_O= $(LUAJIT_A)

495. TARGET_T= $(LUAJIT_T) $(LUAJIT_SO)

496. TARGET_DEP= $(LIB_VMDEF) $(LUAJIT_SO)

498. ifeq (Windows,$(TARGET_SYS))

499. TARGET_DYNCC= $(STATIC_CC)

500. LJVM_MODE= peobj

501. LJVM_BOUT= $(LJVM_O)

502. LUAJIT_T= luajit.exe

503. ifeq (cygwin,$(HOST_MSYS))

504. LUAJIT_SO= cyg$(TARGET_DLLNAME)

505. else

506. LUAJIT_SO= $(TARGET_DLLNAME)

507. endif

508. # Mixed mode is not supported on Windows. And static mode doesn't work well.

509. # C modules cannot be loaded, because they bind to lua51.dll.

510. ifneq (static,$(BUILDMODE))

511. BUILDMODE= dynamic

512. TARGET_XCFLAGS+= -DLUA_BUILD_AS_DLL

513. endif

514. endif

515. ifeq (Darwin,$(TARGET_SYS))

516. LJVM_MODE= machasm

517. endif

518. ifeq (iOS,$(TARGET_SYS))

519. LJVM_MODE= machasm

520. endif

521. ifeq (SunOS,$(TARGET_SYS))

522. BUILDMODE= static

523. endif

524. ifeq (PS3,$(TARGET_SYS))

525. BUILDMODE= static

526. endif

528. ifeq (Windows,$(HOST_SYS))

529. MINILUA_T= host/minilua.exe

530. BUILDVM_T= host/buildvm.exe

531. ifeq (,$(HOST_MSYS))

532. MINILUA_X= host\minilua

533. BUILDVM_X= host\buildvm

534. ALL_RM:= $(subst /,\,$(ALL_RM))

535. endif

536. endif

538. ifeq (static,$(BUILDMODE))

539. TARGET_DYNCC= @:

540. TARGET_T= $(LUAJIT_T)

541. TARGET_DEP= $(LIB_VMDEF)

542. else

543. ifeq (dynamic,$(BUILDMODE))

544. ifneq (Windows,$(TARGET_SYS))

545. TARGET_CC= $(DYNAMIC_CC)

546. endif

547. TARGET_DYNCC= @:

548. LJVMCORE_DYNO= $(LJVMCORE_O)

549. TARGET_O= $(LUAJIT_SO)

550. TARGET_XLDFLAGS+= $(TARGET_DYNXLDOPTS)

551. else

552. ifeq (Darwin,$(TARGET_SYS))

553. TARGET_DYNCC= @:

554. LJVMCORE_DYNO= $(LJVMCORE_O)

555. endif

556. ifeq (iOS,$(TARGET_SYS))

557. TARGET_DYNCC= @:

558. LJVMCORE_DYNO= $(LJVMCORE_O)

559. endif

560. endif

561. endif

563. Q= @

564. E= @echo

565. #Q=

566. #E= @:

568. ##############################################################################

569. # Make targets.

570. ##############################################################################

572. default all:        $(TARGET_T)

574. amalg:

575. @grep "^[+|]" ljamalg.c

576. $(MAKE) all "LJCORE_O=ljamalg.o"

578. clean:

579. $(HOST_RM) $(ALL_RM)

581. libbc:

582. ./$(LUAJIT_T) host/genlibbc.lua -o host/buildvm_libbc.h $(LJLIB_C)

583. $(MAKE) all

585. depend:

586. @for file in $(ALL_HDRGEN); do \

587. test -f $$file || touch $$file; \

588. done

589. @$(HOST_CC) $(HOST_ACFLAGS) -MM *.c host/*.c | \

590. sed -e "s| [^ ]*/dasm_\S*\.h||g" \

591. -e "s|^\([^l ]\)|host/\1|" \

592. -e "s| lj_target_\S*\.h| lj_target_*.h|g" \

593. -e "s| lj_emit_\S*\.h| lj_emit_*.h|g" \

594. -e "s| lj_asm_\S*\.h| lj_asm_*.h|g" >Makefile.dep

595. @for file in $(ALL_HDRGEN); do \

596. test -s $$file || $(HOST_RM) $$file; \

597. done

599. .PHONY: default all amalg clean libbc depend

601. ##############################################################################

602. # Rules for generated files.

603. ##############################################################################

605. $(MINILUA_T): $(MINILUA_O)

606. $(E) "HOSTLINK  $@"

607. $(Q)$(HOST_CC) $(HOST_ALDFLAGS) -o $@ $(MINILUA_O) $(MINILUA_LIBS) $(HOST_ALIBS)

609. host/buildvm_arch.h: $(DASM_DASC) $(DASM_DEP)

610. $(E) "DYNASM    $@"

611. $(Q)$(DASM) $(DASM_FLAGS) -o $@ $(DASM_DASC)

613. host/buildvm.o: $(DASM_DIR)/dasm_*.h

615. $(BUILDVM_T): $(BUILDVM_O)

616. $(E) "HOSTLINK  $@"

617. $(Q)$(HOST_CC) $(HOST_ALDFLAGS) -o $@ $(BUILDVM_O) $(HOST_ALIBS)

619. $(LJVM_BOUT): $(BUILDVM_T)

620. $(E) "BUILDVM   $@"

621. $(Q)$(BUILDVM_X) -m $(LJVM_MODE) -o $@

623. lj_bcdef.h: $(BUILDVM_T) $(LJLIB_C)

624. $(E) "BUILDVM   $@"

625. $(Q)$(BUILDVM_X) -m bcdef -o $@ $(LJLIB_C)

627. lj_ffdef.h: $(BUILDVM_T) $(LJLIB_C)

628. $(E) "BUILDVM   $@"

629. $(Q)$(BUILDVM_X) -m ffdef -o $@ $(LJLIB_C)

631. lj_libdef.h: $(BUILDVM_T) $(LJLIB_C)

632. $(E) "BUILDVM   $@"

633. $(Q)$(BUILDVM_X) -m libdef -o $@ $(LJLIB_C)

635. lj_recdef.h: $(BUILDVM_T) $(LJLIB_C)

636. $(E) "BUILDVM   $@"

637. $(Q)$(BUILDVM_X) -m recdef -o $@ $(LJLIB_C)

639. $(LIB_VMDEF): $(BUILDVM_T) $(LJLIB_C)

640. $(E) "BUILDVM   $@"

641. $(Q)$(BUILDVM_X) -m vmdef -o $(LIB_VMDEFP) $(LJLIB_C)

643. lj_folddef.h: $(BUILDVM_T) lj_opt_fold.c

644. $(E) "BUILDVM   $@"

645. $(Q)$(BUILDVM_X) -m folddef -o $@ lj_opt_fold.c

647. ##############################################################################

648. # Object file rules.

649. ##############################################################################

651. %.o: %.c

652. $(E) "CC        $@"

653. $(Q)$(TARGET_DYNCC) $(TARGET_ACFLAGS) -c -o $(@:.o=_dyn.o) $<

654. $(Q)$(TARGET_CC) $(TARGET_ACFLAGS) -c -o $@ $<

656. %.o: %.S

657. $(E) "ASM       $@"

658. $(Q)$(TARGET_DYNCC) $(TARGET_ASFLAGS) -c -o $(@:.o=_dyn.o) $<

659. $(Q)$(TARGET_CC) $(TARGET_ASFLAGS) -c -o $@ $<

661. $(LUAJIT_O):

662. $(E) "CC        $@"

663. $(Q)$(TARGET_STCC) $(TARGET_ACFLAGS) -c -o $@ $<

665. $(HOST_O): %.o: %.c

666. $(E) "HOSTCC    $@"

667. $(Q)$(HOST_CC) $(HOST_ACFLAGS) -c -o $@ $<

669. include Makefile.dep

671. ##############################################################################

672. # Target file rules.

673. ##############################################################################

675. $(LUAJIT_A): $(LJVMCORE_O)

676. $(E) "AR        $@"

677. $(Q)$(TARGET_AR) $@ $(LJVMCORE_O)

679. # The dependency on _O, but linking with _DYNO is intentional.

680. $(LUAJIT_SO): $(LJVMCORE_O)

681. $(E) "DYNLINK   $@"

682. $(Q)$(TARGET_LD) $(TARGET_ASHLDFLAGS) -o $@ $(LJVMCORE_DYNO) $(TARGET_ALIBS)

683. $(Q)$(TARGET_STRIP) $@

685. $(LUAJIT_T): $(TARGET_O) $(LUAJIT_O) $(TARGET_DEP)

686. $(E) "LINK      $@"

687. $(Q)$(TARGET_LD) $(TARGET_ALDFLAGS) -o $@ $(LUAJIT_O) $(TARGET_O) $(TARGET_ALIBS)

688. $(Q)$(TARGET_STRIP) $@

689. $(E) "OK        Successfully built LuaJIT"

691. ##############################################################################

One Level Up                               Top Level

One Level Up                               Top Level

src/host/ - luajit-2.0-src

- buildvm.c

- buildvm.h

- buildvm_arch.h

- buildvm_asm.c

- buildvm_fold.c

- buildvm_lib.c

- buildvm_libbc.h

- buildvm_peobj.c

- genlibbc.lua

One Level Up                               Top Level

One Level Up                               Top Level

src/host/buildvm.c - luajit-2.0-src

Global variables defined

- bc_names

- ir_names

- ircall_names

- irfield_names

- irfpm_names

- irt_names

- modenames

- relocmap

- trace_errors

Functions defined

- build_code

- collect_reloc

- emit_bcdef

- emit_raw

- emit_vmdef

- lower

- main

- owrite

- parseargs

- parsemode

- sym_decorate

- sym_insert

- usage

Macros defined

- BCNAME

- BCNAME

- BUILDNAME

- BUILDNAME

- DASM_ALIGNED_WRITES

- DASM_CHECKS

- DASM_EXTERN

- Dst

- Dst_DECL

- Dst_REF

- FLNAME

- FLNAME

- FPMNAME

- FPMNAME

- IRCALLNAME

- IRCALLNAME

- IRNAME

- IRNAME

- IRTNAME

- IRTNAME

- NRELOCSYM

- TREDEF

Source code

1. /*

2. ** LuaJIT VM builder.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** This is a tool to build the hand-tuned assembler code required for

6. ** LuaJIT's bytecode interpreter. It supports a variety of output formats

7. ** to feed different toolchains (see usage() below).

8. **

9. ** This tool is not particularly optimized because it's only used while

10. ** _building_ LuaJIT. There's no point in distributing or installing it.

11. ** Only the object code generated by this tool is linked into LuaJIT.

12. **

13. ** Caveat: some memory is not free'd, error handling is lazy.

14. ** It's a one-shot tool -- any effort fixing this would be wasted.

15. */

17. #include "buildvm.h"

18. #include "lj_obj.h"

19. #include "lj_gc.h"

20. #include "lj_bc.h"

21. #include "lj_ir.h"

22. #include "lj_ircall.h"

23. #include "lj_frame.h"

24. #include "lj_dispatch.h"

25. #if LJ_HASFFI

26. #include "lj_ctype.h"

27. #include "lj_ccall.h"

28. #endif

29. #include "luajit.h"

31. #if defined(_WIN32)

32. #include <fcntl.h>

33. #include <io.h>

34. #endif

36. /* ------------------------------------------------------------------------ */

38. /* DynASM glue definitions. */

39. ‌#define Dst                ctx

40. ‌#define Dst_DECL        BuildCtx *ctx

41. ‌#define Dst_REF                (ctx->D)

42. ‌#define DASM_CHECKS        1

44. #include "../dynasm/dasm_proto.h"

46. /* Glue macros for DynASM. */

47. static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type);

49. ‌#define DASM_EXTERN(ctx, addr, idx, type) \

50. collect_reloc(ctx, addr, idx, type)

52. /* ------------------------------------------------------------------------ */

54. /* Avoid trouble if cross-compiling for an x86 target. Speed doesn't matter. */

55. ‌#define DASM_ALIGNED_WRITES        1

57. /* Embed architecture-specific DynASM encoder. */

58. #if LJ_TARGET_X86ORX64

59. #include "../dynasm/dasm_x86.h"

60. #elif LJ_TARGET_ARM

61. #include "../dynasm/dasm_arm.h"

62. #elif LJ_TARGET_ARM64

63. #include "../dynasm/dasm_arm64.h"

64. #elif LJ_TARGET_PPC

65. #include "../dynasm/dasm_ppc.h"

66. #elif LJ_TARGET_MIPS

67. #include "../dynasm/dasm_mips.h"

68. #else

69. #error "No support for this architecture (yet)"

70. #endif

72. /* Embed generated architecture-specific backend. */

73. #include "buildvm_arch.h"

75. /* ------------------------------------------------------------------------ */

77. ‌void owrite(BuildCtx *ctx, const void *ptr, size_t sz)

78. {

79. if (fwrite(ptr, 1, sz, ctx->fp) != sz) {

80. fprintf(stderr, "Error: cannot write to output file: %s\n",

81. strerror(errno));

82. exit(1);

83. }

84. }

86. /* ------------------------------------------------------------------------ */

88. /* Emit code as raw bytes. Only used for DynASM debugging. */

89. ‌static void emit_raw(BuildCtx *ctx)

90. {

91. owrite(ctx, ctx->code, ctx->codesz);

92. }

94. /* -- Build machine code -------------------------------------------------- */

96. ‌static const char *sym_decorate(BuildCtx *ctx,

97. const char *prefix, const char *suffix)

98. {

99. char name[256];

100. char *p;

101. #if LJ_64

102. const char *symprefix = ctx->mode == BUILD_machasm ? "_" : "";

103. #elif LJ_TARGET_XBOX360

104. const char *symprefix = "";

105. #else

106. const char *symprefix = ctx->mode != BUILD_elfasm ? "_" : "";

107. #endif

108. sprintf(name, "%s%s%s", symprefix, prefix, suffix);

109. p = strchr(name, '@');

110. if (p) {

111. #if LJ_TARGET_X86ORX64

112. if (!LJ_64 && (ctx->mode == BUILD_coffasm || ctx->mode == BUILD_peobj))

113. name[0] = '@';

114. else

115. *p = '\0';

116. #elif LJ_TARGET_PPC && !LJ_TARGET_CONSOLE

117. /* Keep @plt etc. */

118. #else

119. *p = '\0';

120. #endif

121. }

122. p = (char *)malloc(strlen(name)+1);  /* MSVC doesn't like strdup. */

123. strcpy(p, name);

124. return p;

125. }

127. ‌#define NRELOCSYM        (sizeof(extnames)/sizeof(extnames[0])-1)

129. ‌static int relocmap[NRELOCSYM];

131. /* Collect external relocations. */

132. ‌static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type)

133. {

134. if (ctx->nreloc >= BUILD_MAX_RELOC) {

135. fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n");

136. exit(1);

137. }

138. if (relocmap[idx] < 0) {

139. relocmap[idx] = ctx->nrelocsym;

140. ctx->relocsym[ctx->nrelocsym] = sym_decorate(ctx, "", extnames[idx]);

141. ctx->nrelocsym++;

142. }

143. ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code);

144. ctx->reloc[ctx->nreloc].sym = relocmap[idx];

145. ctx->reloc[ctx->nreloc].type = type;

146. ctx->nreloc++;

147. #if LJ_TARGET_XBOX360

148. return (int)(ctx->code - addr) + 4;  /* Encode symbol offset of .text. */

149. #else

150. return 0;  /* Encode symbol offset of 0. */

151. #endif

152. }

154. /* Naive insertion sort. Performance doesn't matter here. */

155. ‌static void sym_insert(BuildCtx *ctx, int32_t ofs,

156. const char *prefix, const char *suffix)

157. {

158. ptrdiff_t i = ctx->nsym++;

159. while (i > 0) {

160. if (ctx->sym[i-1].ofs <= ofs)

161. break;

162. ctx->sym[i] = ctx->sym[i-1];

163. i--;

164. }

165. ctx->sym[i].ofs = ofs;

166. ctx->sym[i].name = sym_decorate(ctx, prefix, suffix);

167. }

169. /* Build the machine code. */

170. ‌static int build_code(BuildCtx *ctx)

171. {

172. int status;

173. int i;

175. /* Initialize DynASM structures. */

176. ctx->nglob = GLOB__MAX;

177. ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *));

178. memset(ctx->glob, 0, ctx->nglob*sizeof(void *));

179. ctx->nreloc = 0;

181. ctx->globnames = globnames;

182. ctx->extnames = extnames;

183. ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *));

184. ctx->nrelocsym = 0;

185. for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1;

187. ctx->dasm_ident = DASM_IDENT;

188. ctx->dasm_arch = DASM_ARCH;

190. dasm_init(Dst, DASM_MAXSECTION);

191. dasm_setupglobal(Dst, ctx->glob, ctx->nglob);

192. dasm_setup(Dst, build_actionlist);

194. /* Call arch-specific backend to emit the code. */

195. ctx->npc = build_backend(ctx);

197. /* Finalize the code. */

198. (void)dasm_checkstep(Dst, -1);

199. if ((status = dasm_link(Dst, &ctx->codesz))) return status;

200. ctx->code = (uint8_t *)malloc(ctx->codesz);

201. if ((status = dasm_encode(Dst, (void *)ctx->code))) return status;

203. /* Allocate symbol table and bytecode offsets. */

204. ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin");

205. ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym));

206. ctx->nsym = 0;

207. ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t));

209. /* Collect the opcodes (PC labels). */

210. for (i = 0; i < ctx->npc; i++) {

211. int32_t ofs = dasm_getpclabel(Dst, i);

212. if (ofs < 0) return 0x22000000|i;

213. ctx->bc_ofs[i] = ofs;

214. if ((LJ_HASJIT ||

215. !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP ||

216. i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) &&

217. (LJ_HASFFI || i != BC_KCDATA))

218. sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]);

219. }

221. /* Collect the globals (named labels). */

222. for (i = 0; i < ctx->nglob; i++) {

223. const char *gl = globnames[i];

224. int len = (int)strlen(gl);

225. if (!ctx->glob[i]) {

226. fprintf(stderr, "Error: undefined global %s\n", gl);

227. exit(2);

228. }

229. /* Skip the _Z symbols. */

230. if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z'))

231. sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code),

232. LABEL_PREFIX, globnames[i]);

233. }

235. /* Close the address range. */

236. sym_insert(ctx, (int32_t)ctx->codesz, "", "");

237. ctx->nsym--;

239. dasm_free(Dst);

241. return 0;

242. }

244. /* -- Generate VM enums --------------------------------------------------- */

246. ‌const char *const bc_names[] = {

247. ‌#define BCNAME(name, ma, mb, mc, mt)       #name,

248. BCDEF(BCNAME)

249. ‌#undef BCNAME

250. NULL

251. };

253. ‌const char *const ir_names[] = {

254. ‌#define IRNAME(name, m, m1, m2)        #name,

255. IRDEF(IRNAME)

256. ‌#undef IRNAME

257. NULL

258. };

260. ‌const char *const irt_names[] = {

261. ‌#define IRTNAME(name, size)        #name,

262. IRTDEF(IRTNAME)

263. ‌#undef IRTNAME

264. NULL

265. };

267. ‌const char *const irfpm_names[] = {

268. ‌#define FPMNAME(name)                #name,

269. IRFPMDEF(FPMNAME)

270. ‌#undef FPMNAME

271. NULL

272. };

274. ‌const char *const irfield_names[] = {

275. ‌#define FLNAME(name, ofs)        #name,

276. IRFLDEF(FLNAME)

277. ‌#undef FLNAME

278. NULL

279. };

281. ‌const char *const ircall_names[] = {

282. ‌#define IRCALLNAME(cond, name, nargs, kind, type, flags)        #name,

283. IRCALLDEF(IRCALLNAME)

284. ‌#undef IRCALLNAME

285. NULL

286. };

288. ‌static const char *const trace_errors[] = {

289. ‌#define TREDEF(name, msg)        msg,

290. #include "lj_traceerr.h"

291. NULL

292. };

294. ‌static const char *lower(char *buf, const char *s)

295. {

296. char *p = buf;

297. while (*s) {

298. *p++ = (*s >= 'A' && *s <= 'Z') ? *s+0x20 : *s;

299. s++;

300. }

301. *p = '\0';

302. return buf;

303. }

305. /* Emit C source code for bytecode-related definitions. */

306. ‌static void emit_bcdef(BuildCtx *ctx)

307. {

308. int i;

309. fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n");

310. fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_ofs[] = {\n");

311. for (i = 0; i < ctx->npc; i++) {

312. if (i != 0)

313. fprintf(ctx->fp, ",\n");

314. fprintf(ctx->fp, "%d", ctx->bc_ofs[i]);

315. }

316. }

318. /* Emit VM definitions as Lua code for debug modules. */

319. ‌static void emit_vmdef(BuildCtx *ctx)

320. {

321. char buf[80];

322. int i;

323. fprintf(ctx->fp, "-- This is a generated file. DO NOT EDIT!\n\n");

324. fprintf(ctx->fp, "return {\n\n");

326. fprintf(ctx->fp, "bcnames = \"");

327. for (i = 0; bc_names[i]; i++) fprintf(ctx->fp, "%-6s", bc_names[i]);

328. fprintf(ctx->fp, "\",\n\n");

330. fprintf(ctx->fp, "irnames = \"");

331. for (i = 0; ir_names[i]; i++) fprintf(ctx->fp, "%-6s", ir_names[i]);

332. fprintf(ctx->fp, "\",\n\n");

334. fprintf(ctx->fp, "irfpm = { [0]=");

335. for (i = 0; irfpm_names[i]; i++)

336. fprintf(ctx->fp, "\"%s\", ", lower(buf, irfpm_names[i]));

337. fprintf(ctx->fp, "},\n\n");

339. fprintf(ctx->fp, "irfield = { [0]=");

340. for (i = 0; irfield_names[i]; i++) {

341. char *p;

342. lower(buf, irfield_names[i]);

343. p = strchr(buf, '_');

344. if (p) *p = '.';

345. fprintf(ctx->fp, "\"%s\", ", buf);

346. }

347. fprintf(ctx->fp, "},\n\n");

349. fprintf(ctx->fp, "ircall = {\n[0]=");

350. for (i = 0; ircall_names[i]; i++)

351. fprintf(ctx->fp, "\"%s\",\n", ircall_names[i]);

352. fprintf(ctx->fp, "},\n\n");

354. fprintf(ctx->fp, "traceerr = {\n[0]=");

355. for (i = 0; trace_errors[i]; i++)

356. fprintf(ctx->fp, "\"%s\",\n", trace_errors[i]);

357. fprintf(ctx->fp, "},\n\n");

358. }

360. /* -- Argument parsing ---------------------------------------------------- */

362. /* Build mode names. */

363. ‌static const char *const modenames[] = {

364. ‌#define BUILDNAME(name)                #name,

365. BUILDDEF(BUILDNAME)

366. ‌#undef BUILDNAME

367. NULL

368. };

370. /* Print usage information and exit. */

371. ‌static void usage(void)

372. {

373. int i;

374. fprintf(stderr, LUAJIT_VERSION " VM builder.\n");

375. fprintf(stderr, LUAJIT_COPYRIGHT ", " LUAJIT_URL "\n");

376. fprintf(stderr, "Target architecture: " LJ_ARCH_NAME "\n\n");

377. fprintf(stderr, "Usage: buildvm -m mode [-o outfile] [infiles...]\n\n");

378. fprintf(stderr, "Available modes:\n");

379. for (i = 0; i < BUILD__MAX; i++)

380. fprintf(stderr, "  %s\n", modenames[i]);

381. exit(1);

382. }

384. /* Parse the output mode name. */

385. ‌static BuildMode parsemode(const char *mode)

386. {

387. int i;

388. for (i = 0; modenames[i]; i++)

389. if (!strcmp(mode, modenames[i]))

390. return (BuildMode)i;

391. usage();

392. return (BuildMode)-1;

393. }

395. /* Parse arguments. */

396. ‌static void parseargs(BuildCtx *ctx, char **argv)

397. {

398. const char *a;

399. int i;

400. ctx->mode = (BuildMode)-1;

401. ctx->outname = "-";

402. for (i = 1; (a = argv[i]) != NULL; i++) {

403. if (a[0] != '-')

404. break;

405. switch (a[1]) {

406. case '-':

407. if (a[2]) goto err;

408. i++;

409. goto ok;

410. case '\0':

411. goto ok;

412. case 'm':

413. i++;

414. if (a[2] || argv[i] == NULL) goto err;

415. ctx->mode = parsemode(argv[i]);

416. break;

417. case 'o':

418. i++;

419. if (a[2] || argv[i] == NULL) goto err;

420. ctx->outname = argv[i];

421. break;

422. default: err:

423. usage();

424. break;

425. }

426. }

427. ok:

428. ctx->args = argv+i;

429. if (ctx->mode == (BuildMode)-1) goto err;

430. }

432. ‌int main(int argc, char **argv)

433. {

434. BuildCtx ctx_;

435. BuildCtx *ctx = &ctx_;

436. int status, binmode;

438. if (sizeof(void *) != 4*LJ_32+8*LJ_64) {

439. fprintf(stderr,"Error: pointer size mismatch in cross-build.\n");

440. fprintf(stderr,"Try: make HOST_CC=\"gcc -m32\" CROSS=...\n\n");

441. return 1;

442. }

444. UNUSED(argc);

445. parseargs(ctx, argv);

447. if ((status = build_code(ctx))) {

448. fprintf(stderr,"Error: DASM error %08x\n", status);

449. return 1;

450. }

452. switch (ctx->mode) {

453. case BUILD_peobj:

454. case BUILD_raw:

455. binmode = 1;

456. break;

457. default:

458. binmode = 0;

459. break;

460. }

462. if (ctx->outname[0] == '-' && ctx->outname[1] == '\0') {

463. ctx->fp = stdout;

464. #if defined(_WIN32)

465. if (binmode)

466. _setmode(_fileno(stdout), _O_BINARY);  /* Yuck. */

467. #endif

468. } else if (!(ctx->fp = fopen(ctx->outname, binmode ? "wb" : "w"))) {

469. fprintf(stderr, "Error: cannot open output file '%s': %s\n",

470. ctx->outname, strerror(errno));

471. exit(1);

472. }

474. switch (ctx->mode) {

475. case BUILD_elfasm:

476. case BUILD_coffasm:

477. case BUILD_machasm:

478. emit_asm(ctx);

479. emit_asm_debug(ctx);

480. break;

481. case BUILD_peobj:

482. emit_peobj(ctx);

483. break;

484. case BUILD_raw:

485. emit_raw(ctx);

486. break;

487. case BUILD_bcdef:

488. emit_bcdef(ctx);

489. emit_lib(ctx);

490. break;

491. case BUILD_vmdef:

492. emit_vmdef(ctx);

493. emit_lib(ctx);

494. fprintf(ctx->fp, "}\n\n");

495. break;

496. case BUILD_ffdef:

497. case BUILD_libdef:

498. case BUILD_recdef:

499. emit_lib(ctx);

500. break;

501. case BUILD_folddef:

502. emit_fold(ctx);

503. break;

504. default:

505. break;

506. }

508. fflush(ctx->fp);

509. if (ferror(ctx->fp)) {

510. fprintf(stderr, "Error: cannot write to output file: %s\n",

511. strerror(errno));

512. exit(1);

513. }

514. fclose(ctx->fp);

516. return 0;

517. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_arch.h - luajit-2.0-src

Macros defined

- LJ_32

- LJ_32

- LJ_52

- LJ_52

- LJ_64

- LJ_64

- LJ_ABI_EABI

- LJ_ABI_SOFTFP

- LJ_ABI_SOFTFP

- LJ_ABI_WIN

- LJ_ABI_WIN

- LJ_ABI_WIN

- LJ_ARCH_BITS

- LJ_ARCH_BITS

- LJ_ARCH_BITS

- LJ_ARCH_BITS

- LJ_ARCH_BITS

- LJ_ARCH_BITS

- LJ_ARCH_BITS

- LJ_ARCH_CELL

- LJ_ARCH_ENDIAN

- LJ_ARCH_ENDIAN

- LJ_ARCH_ENDIAN

- LJ_ARCH_ENDIAN

- LJ_ARCH_ENDIAN

- LJ_ARCH_ENDIAN

- LJ_ARCH_ENDIAN

- LJ_ARCH_ENDIAN

- LJ_ARCH_HASFPU

- LJ_ARCH_HASFPU

- LJ_ARCH_NAME

- LJ_ARCH_NAME

- LJ_ARCH_NAME

- LJ_ARCH_NAME

- LJ_ARCH_NAME

- LJ_ARCH_NAME

- LJ_ARCH_NAME

- LJ_ARCH_NAME

- LJ_ARCH_NAME

- LJ_ARCH_NOFFI

- LJ_ARCH_NOJIT

- LJ_ARCH_NOJIT

- LJ_ARCH_NUMMODE

- LJ_ARCH_NUMMODE

- LJ_ARCH_NUMMODE

- LJ_ARCH_NUMMODE

- LJ_ARCH_NUMMODE

- LJ_ARCH_NUMMODE

- LJ_ARCH_PPC32ON64

- LJ_ARCH_PPC64

- LJ_ARCH_ROUND

- LJ_ARCH_SQRT

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_VERSION

- LJ_ARCH_XENON

- LJ_BE

- LJ_BE

- LJ_DUALNUM

- LJ_DUALNUM

- LJ_ENDIAN_LOHI

- LJ_ENDIAN_LOHI

- LJ_ENDIAN_SELECT

- LJ_ENDIAN_SELECT

- LJ_FR2

- LJ_FR2

- LJ_GC64

- LJ_GC64

- LJ_HASFFI

- LJ_HASFFI

- LJ_HASJIT

- LJ_HASJIT

- LJ_HASPROFILE

- LJ_HASPROFILE

- LJ_HASPROFILE

- LJ_HASPROFILE

- LJ_HASPROFILE

- LJ_LE

- LJ_LE

- LJ_NO_UNWIND

- LJ_NUMMODE_DUAL

- LJ_NUMMODE_DUAL_SINGLE

- LJ_NUMMODE_SINGLE

- LJ_NUMMODE_SINGLE_DUAL

- LJ_OS_NAME

- LJ_OS_NAME

- LJ_OS_NAME

- LJ_OS_NAME

- LJ_OS_NAME

- LJ_OS_NAME

- LJ_OS_NOJIT

- LJ_PAGESIZE

- LJ_PROFILE_PTHREAD

- LJ_PROFILE_SIGPROF

- LJ_PROFILE_WTHREAD

- LJ_SOFTFP

- LJ_TARGET_ARM

- LJ_TARGET_ARM64

- LJ_TARGET_CONSOLE

- LJ_TARGET_CONSOLE

- LJ_TARGET_CONSOLE

- LJ_TARGET_CONSOLE

- LJ_TARGET_DLOPEN

- LJ_TARGET_EHRETREG

- LJ_TARGET_EHRETREG

- LJ_TARGET_EHRETREG

- LJ_TARGET_EHRETREG

- LJ_TARGET_EHRETREG

- LJ_TARGET_EHRETREG

- LJ_TARGET_GC64

- LJ_TARGET_GC64

- LJ_TARGET_IOS

- LJ_TARGET_JUMPRANGE

- LJ_TARGET_JUMPRANGE

- LJ_TARGET_JUMPRANGE

- LJ_TARGET_JUMPRANGE

- LJ_TARGET_JUMPRANGE

- LJ_TARGET_LINUX

- LJ_TARGET_MASKROT

- LJ_TARGET_MASKROT

- LJ_TARGET_MASKROT

- LJ_TARGET_MASKROT

- LJ_TARGET_MASKROT

- LJ_TARGET_MASKROT

- LJ_TARGET_MASKSHIFT

- LJ_TARGET_MASKSHIFT

- LJ_TARGET_MASKSHIFT

- LJ_TARGET_MASKSHIFT

- LJ_TARGET_MASKSHIFT

- LJ_TARGET_MASKSHIFT

- LJ_TARGET_MIPS

- LJ_TARGET_OSX

- LJ_TARGET_POSIX

- LJ_TARGET_PPC

- LJ_TARGET_PS3

- LJ_TARGET_PS4

- LJ_TARGET_PSVITA

- LJ_TARGET_UNALIGNED

- LJ_TARGET_UNALIGNED

- LJ_TARGET_UNALIGNED

- LJ_TARGET_UNIFYROT

- LJ_TARGET_UNIFYROT

- LJ_TARGET_UNIFYROT

- LJ_TARGET_UNIFYROT

- LJ_TARGET_WINDOWS

- LJ_TARGET_X64

- LJ_TARGET_X86

- LJ_TARGET_X86ORX64

- LJ_TARGET_X86ORX64

- LJ_TARGET_XBOX360

- LUAJIT_ARCH_ARM

- LUAJIT_ARCH_ARM64

- LUAJIT_ARCH_MIPS

- LUAJIT_ARCH_PPC

- LUAJIT_ARCH_X64

- LUAJIT_ARCH_X86

- LUAJIT_ARCH_arm

- LUAJIT_ARCH_arm64

- LUAJIT_ARCH_mips

- LUAJIT_ARCH_ppc

- LUAJIT_ARCH_x64

- LUAJIT_ARCH_x86

- LUAJIT_BE

- LUAJIT_LE

- LUAJIT_NO_EXP2

- LUAJIT_NO_LOG2

- LUAJIT_OS

- LUAJIT_OS

- LUAJIT_OS

- LUAJIT_OS

- LUAJIT_OS

- LUAJIT_OS

- LUAJIT_OS_BSD

- LUAJIT_OS_LINUX

- LUAJIT_OS_OSX

- LUAJIT_OS_OTHER

- LUAJIT_OS_POSIX

- LUAJIT_OS_WINDOWS

- LUAJIT_TARGET

- LUAJIT_TARGET

- LUAJIT_TARGET

- LUAJIT_TARGET

- LUAJIT_TARGET

- LUAJIT_TARGET

- NULL

- NULL

- _LJ_ARCH_H

Source code

1. /*

2. ** Target architecture selection.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_ARCH_H

7. ‌#define _LJ_ARCH_H

9. #include "lua.h"

11. /* Target endianess. */

12. ‌#define LUAJIT_LE        0

13. ‌#define LUAJIT_BE        1

15. /* Target architectures. */

16. ‌#define LUAJIT_ARCH_X86                1

17. ‌#define LUAJIT_ARCH_x86                1

18. ‌#define LUAJIT_ARCH_X64                2

19. ‌#define LUAJIT_ARCH_x64                2

20. ‌#define LUAJIT_ARCH_ARM                3

21. ‌#define LUAJIT_ARCH_arm                3

22. ‌#define LUAJIT_ARCH_ARM64        4

23. ‌#define LUAJIT_ARCH_arm64        4

24. ‌#define LUAJIT_ARCH_PPC                5

25. ‌#define LUAJIT_ARCH_ppc                5

26. ‌#define LUAJIT_ARCH_MIPS        6

27. ‌#define LUAJIT_ARCH_mips        6

29. /* Target OS. */

30. ‌#define LUAJIT_OS_OTHER                0

31. ‌#define LUAJIT_OS_WINDOWS        1

32. ‌#define LUAJIT_OS_LINUX                2

33. ‌#define LUAJIT_OS_OSX                3

34. ‌#define LUAJIT_OS_BSD                4

35. ‌#define LUAJIT_OS_POSIX                5

37. /* Select native target if no target defined. */

38. #ifndef LUAJIT_TARGET

40. #if defined(__i386) || defined(__i386__) || defined(_M_IX86)

41. ‌#define LUAJIT_TARGET        LUAJIT_ARCH_X86

42. #elif defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)

43. ‌#define LUAJIT_TARGET        LUAJIT_ARCH_X64

44. #elif defined(__arm__) || defined(__arm) || defined(__ARM__) || defined(__ARM)

45. ‌#define LUAJIT_TARGET        LUAJIT_ARCH_ARM

46. #elif defined(__aarch64__)

47. ‌#define LUAJIT_TARGET        LUAJIT_ARCH_ARM64

48. #elif defined(__ppc__) || defined(__ppc) || defined(__PPC__) || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__POWERPC) || defined(_M_PPC)

49. ‌#define LUAJIT_TARGET        LUAJIT_ARCH_PPC

50. #elif defined(__mips__) || defined(__mips) || defined(__MIPS__) || defined(__MIPS)

51. ‌#define LUAJIT_TARGET        LUAJIT_ARCH_MIPS

52. #else

53. #error "No support for this architecture (yet)"

54. #endif

56. #endif

58. /* Select native OS if no target OS defined. */

59. #ifndef LUAJIT_OS

61. #if defined(_WIN32) && !defined(_XBOX_VER)

62. ‌#define LUAJIT_OS        LUAJIT_OS_WINDOWS

63. #elif defined(__linux__)

64. ‌#define LUAJIT_OS        LUAJIT_OS_LINUX

65. #elif defined(__MACH__) && defined(__APPLE__)

66. ‌#define LUAJIT_OS        LUAJIT_OS_OSX

67. #elif (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \

68. defined(__NetBSD__) || defined(__OpenBSD__) || \

69. defined(__DragonFly__)) && !defined(__ORBIS__)

70. ‌#define LUAJIT_OS        LUAJIT_OS_BSD

71. #elif (defined(__sun__) && defined(__svr4__)) || defined(__CYGWIN__)

72. ‌#define LUAJIT_OS        LUAJIT_OS_POSIX

73. #else

74. ‌#define LUAJIT_OS        LUAJIT_OS_OTHER

75. #endif

77. #endif

79. /* Set target OS properties. */

80. #if LUAJIT_OS == LUAJIT_OS_WINDOWS

81. ‌#define LJ_OS_NAME        "Windows"

82. #elif LUAJIT_OS == LUAJIT_OS_LINUX

83. ‌#define LJ_OS_NAME        "Linux"

84. #elif LUAJIT_OS == LUAJIT_OS_OSX

85. ‌#define LJ_OS_NAME        "OSX"

86. #elif LUAJIT_OS == LUAJIT_OS_BSD

87. ‌#define LJ_OS_NAME        "BSD"

88. #elif LUAJIT_OS == LUAJIT_OS_POSIX

89. ‌#define LJ_OS_NAME        "POSIX"

90. #else

91. ‌#define LJ_OS_NAME        "Other"

92. #endif

94. ‌#define LJ_TARGET_WINDOWS        (LUAJIT_OS == LUAJIT_OS_WINDOWS)

95. ‌#define LJ_TARGET_LINUX                (LUAJIT_OS == LUAJIT_OS_LINUX)

96. ‌#define LJ_TARGET_OSX                (LUAJIT_OS == LUAJIT_OS_OSX)

97. ‌#define LJ_TARGET_IOS                (LJ_TARGET_OSX && (LUAJIT_TARGET == LUAJIT_ARCH_ARM || LUAJIT_TARGET == LUAJIT_ARCH_ARM64))

98. ‌#define LJ_TARGET_POSIX                (LUAJIT_OS > LUAJIT_OS_WINDOWS)

99. ‌#define LJ_TARGET_DLOPEN        LJ_TARGET_POSIX

101. #ifdef __CELLOS_LV2__

102. ‌#define LJ_TARGET_PS3                1

103. ‌#define LJ_TARGET_CONSOLE        1

104. #endif

106. #ifdef __ORBIS__

107. ‌#define LJ_TARGET_PS4                1

108. ‌#define LJ_TARGET_CONSOLE        1

109. ‌#undef NULL

110. ‌#define NULL ((void*)0)

111. #endif

113. #ifdef __psp2__

114. ‌#define LJ_TARGET_PSVITA        1

115. ‌#define LJ_TARGET_CONSOLE        1

116. #endif

118. #if _XBOX_VER >= 200

119. ‌#define LJ_TARGET_XBOX360        1

120. ‌#define LJ_TARGET_CONSOLE        1

121. #endif

123. ‌#define LJ_NUMMODE_SINGLE        0        /* Single-number mode only. */

124. ‌#define LJ_NUMMODE_SINGLE_DUAL        1        /* Default to single-number mode. */

125. ‌#define LJ_NUMMODE_DUAL                2        /* Dual-number mode only. */

126. ‌#define LJ_NUMMODE_DUAL_SINGLE        3        /* Default to dual-number mode. */

128. /* Set target architecture properties. */

129. #if LUAJIT_TARGET == LUAJIT_ARCH_X86

131. ‌#define LJ_ARCH_NAME                "x86"

132. ‌#define LJ_ARCH_BITS                32

133. ‌#define LJ_ARCH_ENDIAN                LUAJIT_LE

134. #if LJ_TARGET_WINDOWS || __CYGWIN__

135. ‌#define LJ_ABI_WIN                1

136. #else

137. ‌#define LJ_ABI_WIN                0

138. #endif

139. ‌#define LJ_TARGET_X86                1

140. ‌#define LJ_TARGET_X86ORX64        1

141. ‌#define LJ_TARGET_EHRETREG        0

142. ‌#define LJ_TARGET_MASKSHIFT        1

143. ‌#define LJ_TARGET_MASKROT        1

144. ‌#define LJ_TARGET_UNALIGNED        1

145. ‌#define LJ_ARCH_NUMMODE                LJ_NUMMODE_SINGLE_DUAL

147. #elif LUAJIT_TARGET == LUAJIT_ARCH_X64

149. ‌#define LJ_ARCH_NAME                "x64"

150. ‌#define LJ_ARCH_BITS                64

151. ‌#define LJ_ARCH_ENDIAN                LUAJIT_LE

152. ‌#define LJ_ABI_WIN                LJ_TARGET_WINDOWS

153. ‌#define LJ_TARGET_X64                1

154. ‌#define LJ_TARGET_X86ORX64        1

155. ‌#define LJ_TARGET_EHRETREG        0

156. ‌#define LJ_TARGET_JUMPRANGE        31        /* +-2^31 = +-2GB */

157. ‌#define LJ_TARGET_MASKSHIFT        1

158. ‌#define LJ_TARGET_MASKROT        1

159. ‌#define LJ_TARGET_UNALIGNED        1

160. ‌#define LJ_ARCH_NUMMODE                LJ_NUMMODE_SINGLE_DUAL

162. #elif LUAJIT_TARGET == LUAJIT_ARCH_ARM

164. ‌#define LJ_ARCH_NAME                "arm"

165. ‌#define LJ_ARCH_BITS                32

166. ‌#define LJ_ARCH_ENDIAN                LUAJIT_LE

167. #if !defined(LJ_ARCH_HASFPU) && __SOFTFP__

168. ‌#define LJ_ARCH_HASFPU                0

169. #endif

170. #if !defined(LJ_ABI_SOFTFP) && !__ARM_PCS_VFP

171. ‌#define LJ_ABI_SOFTFP                1

172. #endif

173. ‌#define LJ_ABI_EABI                1

174. ‌#define LJ_TARGET_ARM                1

175. ‌#define LJ_TARGET_EHRETREG        0

176. ‌#define LJ_TARGET_JUMPRANGE        25        /* +-2^25 = +-32MB */

177. ‌#define LJ_TARGET_MASKSHIFT        0

178. ‌#define LJ_TARGET_MASKROT        1

179. ‌#define LJ_TARGET_UNIFYROT        2        /* Want only IR_BROR. */

180. ‌#define LJ_ARCH_NUMMODE                LJ_NUMMODE_DUAL

182. #if __ARM_ARCH____ARM_ARCH_8__ || __ARM_ARCH_8A__

183. ‌#define LJ_ARCH_VERSION                80

184. #elif __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH_7S__ || __ARM_ARCH_7VE__

185. ‌#define LJ_ARCH_VERSION                70

186. #elif __ARM_ARCH_6T2__

187. ‌#define LJ_ARCH_VERSION                61

188. #elif __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6K__ || __ARM_ARCH_6Z__ || __ARM_ARCH_6ZK__

189. ‌#define LJ_ARCH_VERSION                60

190. #else

191. ‌#define LJ_ARCH_VERSION                50

192. #endif

194. #elif LUAJIT_TARGET == LUAJIT_ARCH_ARM64

196. ‌#define LJ_ARCH_NAME                "arm64"

197. ‌#define LJ_ARCH_BITS                64

198. ‌#define LJ_ARCH_ENDIAN                LUAJIT_LE

199. ‌#define LJ_TARGET_ARM64                1

200. ‌#define LJ_TARGET_EHRETREG        0

201. ‌#define LJ_TARGET_JUMPRANGE        27        /* +-2^27 = +-128MB */

202. ‌#define LJ_TARGET_MASKSHIFT        1

203. ‌#define LJ_TARGET_MASKROT        1

204. ‌#define LJ_TARGET_UNIFYROT        2        /* Want only IR_BROR. */

205. ‌#define LJ_TARGET_GC64                1

206. ‌#define LJ_ARCH_NUMMODE                LJ_NUMMODE_DUAL

207. ‌#define LJ_ARCH_NOJIT                1        /* NYI */

209. ‌#define LJ_ARCH_VERSION                80

211. #elif LUAJIT_TARGET == LUAJIT_ARCH_PPC

213. #if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__

214. ‌#define LJ_ARCH_ENDIAN                LUAJIT_LE

215. #else

216. ‌#define LJ_ARCH_ENDIAN                LUAJIT_BE

217. #endif

219. #if _LP64

220. ‌#define LJ_ARCH_BITS                64

221. #if LJ_ARCH_ENDIAN == LUAJIT_LE

222. ‌#define LJ_ARCH_NAME                "ppc64le"

223. #else

224. ‌#define LJ_ARCH_NAME                "ppc64"

225. #endif

226. #else

227. ‌#define LJ_ARCH_BITS                32

228. ‌#define LJ_ARCH_NAME                "ppc"

229. #endif

231. ‌#define LJ_TARGET_PPC                1

232. ‌#define LJ_TARGET_EHRETREG        3

233. ‌#define LJ_TARGET_JUMPRANGE        25        /* +-2^25 = +-32MB */

234. ‌#define LJ_TARGET_MASKSHIFT        0

235. ‌#define LJ_TARGET_MASKROT        1

236. ‌#define LJ_TARGET_UNIFYROT        1        /* Want only IR_BROL. */

237. ‌#define LJ_ARCH_NUMMODE                LJ_NUMMODE_DUAL_SINGLE

239. #if LJ_TARGET_CONSOLE

240. ‌#define LJ_ARCH_PPC32ON64        1

241. ‌#define LJ_ARCH_NOFFI                1

242. #elif LJ_ARCH_BITS == 64

243. ‌#define LJ_ARCH_PPC64                1

244. ‌#define LJ_TARGET_GC64                1

245. ‌#define LJ_ARCH_NOJIT                1        /* NYI */

246. #endif

248. #if _ARCH_PWR7

249. ‌#define LJ_ARCH_VERSION                70

250. #elif _ARCH_PWR6

251. ‌#define LJ_ARCH_VERSION                60

252. #elif _ARCH_PWR5X

253. ‌#define LJ_ARCH_VERSION                51

254. #elif _ARCH_PWR5

255. ‌#define LJ_ARCH_VERSION                50

256. #elif _ARCH_PWR4

257. ‌#define LJ_ARCH_VERSION                40

258. #else

259. ‌#define LJ_ARCH_VERSION                0

260. #endif

261. #if _ARCH_PPCSQ

262. ‌#define LJ_ARCH_SQRT                1

263. #endif

264. #if _ARCH_PWR5X

265. ‌#define LJ_ARCH_ROUND                1

266. #endif

267. #if __PPU__

268. ‌#define LJ_ARCH_CELL                1

269. #endif

270. #if LJ_TARGET_XBOX360

271. ‌#define LJ_ARCH_XENON                1

272. #endif

274. #elif LUAJIT_TARGET == LUAJIT_ARCH_MIPS

276. #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL)

277. ‌#define LJ_ARCH_NAME                "mipsel"

278. ‌#define LJ_ARCH_ENDIAN                LUAJIT_LE

279. #else

280. ‌#define LJ_ARCH_NAME                "mips"

281. ‌#define LJ_ARCH_ENDIAN                LUAJIT_BE

282. #endif

283. ‌#define LJ_ARCH_BITS                32

284. ‌#define LJ_TARGET_MIPS                1

285. ‌#define LJ_TARGET_EHRETREG        4

286. ‌#define LJ_TARGET_JUMPRANGE        27        /* 2*2^27 = 256MB-aligned region */

287. ‌#define LJ_TARGET_MASKSHIFT        1

288. ‌#define LJ_TARGET_MASKROT        1

289. ‌#define LJ_TARGET_UNIFYROT        2        /* Want only IR_BROR. */

290. ‌#define LJ_ARCH_NUMMODE                LJ_NUMMODE_SINGLE

292. #if _MIPS_ARCH_MIPS32R2

293. ‌#define LJ_ARCH_VERSION                20

294. #else

295. ‌#define LJ_ARCH_VERSION                10

296. #endif

298. #else

299. #error "No target architecture defined"

300. #endif

302. #ifndef LJ_PAGESIZE

303. ‌#define LJ_PAGESIZE                4096

304. #endif

306. /* Check for minimum required compiler versions. */

307. #if defined(__GNUC__)

308. #if LJ_TARGET_X86

309. #if (__GNUC__ < 3) || ((__GNUC__ == 3) && __GNUC_MINOR__ < 4)

310. #error "Need at least GCC 3.4 or newer"

311. #endif

312. #elif LJ_TARGET_X64

313. #if __GNUC__ < 4

314. #error "Need at least GCC 4.0 or newer"

315. #endif

316. #elif LJ_TARGET_ARM

317. #if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 2)

318. #error "Need at least GCC 4.2 or newer"

319. #endif

320. #elif LJ_TARGET_ARM64

321. #if __clang__

322. #if (__clang_major__ < 3) || ((__clang_major__ == 3) && __clang_minor__ < 5)

323. #error "Need at least Clang 3.5 or newer"

324. #endif

325. #else

326. #if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 8)

327. #error "Need at least GCC 4.8 or newer"

328. #endif

329. #endif

330. #elif !LJ_TARGET_PS3

331. #if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 3)

332. #error "Need at least GCC 4.3 or newer"

333. #endif

334. #endif

335. #endif

337. /* Check target-specific constraints. */

338. #ifndef _BUILDVM_H

339. #if LJ_TARGET_X64

340. #if __USING_SJLJ_EXCEPTIONS__

341. #error "Need a C compiler with native exception handling on x64"

342. #endif

343. #elif LJ_TARGET_ARM

344. #if defined(__ARMEB__)

345. #error "No support for big-endian ARM"

346. #endif

347. #if __ARM_ARCH_6M__ || __ARM_ARCH_7M__ || __ARM_ARCH_7EM__

348. #error "No support for Cortex-M CPUs"

349. #endif

350. #if !(__ARM_EABI__ || LJ_TARGET_IOS)

351. #error "Only ARM EABI or iOS 3.0+ ABI is supported"

352. #endif

353. #elif LJ_TARGET_ARM64

354. #if defined(__AARCH64EB__)

355. #error "No support for big-endian ARM64"

356. #endif

357. #if defined(_ILP32)

358. #error "No support for ILP32 model on ARM64"

359. #endif

360. #elif LJ_TARGET_PPC

361. #if defined(_SOFT_FLOAT) || defined(_SOFT_DOUBLE)

362. #error "No support for PowerPC CPUs without double-precision FPU"

363. #endif

364. #if !LJ_ARCH_PPC64 && LJ_ARCH_ENDIAN == LUAJIT_LE

365. #error "No support for little-endian PPC32"

366. #endif

367. #if LJ_ARCH_PPC64

368. #error "No support for PowerPC 64 bit mode (yet)"

369. #endif

370. #ifdef __NO_FPRS__

371. #error "No support for PPC/e500 anymore (use LuaJIT 2.0)"

372. #endif

373. #elif LJ_TARGET_MIPS

374. #if defined(__mips_soft_float)

375. #error "No support for MIPS CPUs without FPU"

376. #endif

377. #if defined(_LP64)

378. #error "No support for MIPS64"

379. #endif

380. #endif

381. #endif

383. /* Enable or disable the dual-number mode for the VM. */

384. #if (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE && LUAJIT_NUMMODE == 2) || \

385. (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL && LUAJIT_NUMMODE == 1)

386. #error "No support for this number mode on this architecture"

387. #endif

388. #if LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL || \

389. (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL_SINGLE && LUAJIT_NUMMODE != 1) || \

390. (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE_DUAL && LUAJIT_NUMMODE == 2)

391. ‌#define LJ_DUALNUM                1

392. #else

393. ‌#define LJ_DUALNUM                0

394. #endif

396. #if LJ_TARGET_IOS || LJ_TARGET_CONSOLE

397. /* Runtime code generation is restricted on iOS. Complain to Apple, not me. */

398. /* Ditto for the consoles. Complain to Sony or MS, not me. */

399. #ifndef LUAJIT_ENABLE_JIT

400. ‌#define LJ_OS_NOJIT                1

401. #endif

402. #endif

404. /* 64 bit GC references. */

405. #if LJ_TARGET_GC64

406. ‌#define LJ_GC64                        1

407. #else

408. ‌#define LJ_GC64                        0

409. #endif

411. /* 2-slot frame info. */

412. #if LJ_GC64

413. ‌#define LJ_FR2                        1

414. #else

415. ‌#define LJ_FR2                        0

416. #endif

418. /* Disable or enable the JIT compiler. */

419. #if defined(LUAJIT_DISABLE_JIT) || defined(LJ_ARCH_NOJIT) || defined(LJ_OS_NOJIT) || LJ_FR2 || LJ_GC64

420. ‌#define LJ_HASJIT                0

421. #else

422. ‌#define LJ_HASJIT                1

423. #endif

425. /* Disable or enable the FFI extension. */

426. #if defined(LUAJIT_DISABLE_FFI) || defined(LJ_ARCH_NOFFI)

427. ‌#define LJ_HASFFI                0

428. #else

429. ‌#define LJ_HASFFI                1

430. #endif

432. #if defined(LUAJIT_DISABLE_PROFILE)

433. ‌#define LJ_HASPROFILE                0

434. #elif LJ_TARGET_POSIX

435. ‌#define LJ_HASPROFILE                1

436. ‌#define LJ_PROFILE_SIGPROF        1

437. #elif LJ_TARGET_PS3

438. ‌#define LJ_HASPROFILE                1

439. ‌#define LJ_PROFILE_PTHREAD        1

440. #elif LJ_TARGET_WINDOWS || LJ_TARGET_XBOX360

441. ‌#define LJ_HASPROFILE                1

442. ‌#define LJ_PROFILE_WTHREAD        1

443. #else

444. ‌#define LJ_HASPROFILE                0

445. #endif

447. #ifndef LJ_ARCH_HASFPU

448. ‌#define LJ_ARCH_HASFPU                1

449. #endif

450. #ifndef LJ_ABI_SOFTFP

451. ‌#define LJ_ABI_SOFTFP                0

452. #endif

453. ‌#define LJ_SOFTFP                (!LJ_ARCH_HASFPU)

455. #if LJ_ARCH_ENDIAN == LUAJIT_BE

456. ‌#define LJ_LE                        0

457. ‌#define LJ_BE                        1

458. ‌#define LJ_ENDIAN_SELECT(le, be)        be

459. ‌#define LJ_ENDIAN_LOHI(lo, hi)                hi lo

460. #else

461. ‌#define LJ_LE                        1

462. ‌#define LJ_BE                        0

463. ‌#define LJ_ENDIAN_SELECT(le, be)        le

464. ‌#define LJ_ENDIAN_LOHI(lo, hi)                lo hi

465. #endif

467. #if LJ_ARCH_BITS == 32

468. ‌#define LJ_32                        1

469. ‌#define LJ_64                        0

470. #else

471. ‌#define LJ_32                        0

472. ‌#define LJ_64                        1

473. #endif

475. #ifndef LJ_TARGET_UNALIGNED

476. ‌#define LJ_TARGET_UNALIGNED        0

477. #endif

479. /* Various workarounds for embedded operating systems or weak C runtimes. */

480. #if (defined(__ANDROID__) && !defined(LJ_TARGET_X86ORX64)) || defined(__symbian__) || LJ_TARGET_XBOX360 || LJ_TARGET_WINDOWS

481. ‌#define LUAJIT_NO_LOG2

482. #endif

483. #if defined(__symbian__) || LJ_TARGET_WINDOWS

484. ‌#define LUAJIT_NO_EXP2

485. #endif

487. #if defined(LUAJIT_NO_UNWIND) || defined(__symbian__) || LJ_TARGET_IOS || LJ_TARGET_PS3

488. ‌#define LJ_NO_UNWIND                1

489. #endif

491. /* Compatibility with Lua 5.1 vs. 5.2. */

492. #ifdef LUAJIT_ENABLE_LUA52COMPAT

493. ‌#define LJ_52                        1

494. #else

495. ‌#define LJ_52                        0

496. #endif

498. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/host/buildvm.h - luajit-2.0-src

Data types defined

- BuildCtx

- BuildCtx

- BuildMode

- BuildReloc

- BuildReloc

- BuildSym

- BuildSym

Macros defined

- BUILDDEF

- BUILDENUM

- BUILDENUM

- BUILD_MAX_FOLD

- BUILD_MAX_RELOC

- FOLDDEF_PREFIX

- LABEL_PREFIX

- LABEL_PREFIX_BC

- LABEL_PREFIX_CF

- LABEL_PREFIX_FF

- LABEL_PREFIX_FFH

- LABEL_PREFIX_LIBCF

- LABEL_PREFIX_LIBINIT

- LIBDEF_PREFIX

- _BUILDVM_H

Source code

1. /*

2. ** LuaJIT VM builder.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _BUILDVM_H

7. ‌#define _BUILDVM_H

9. #include <sys/types.h>

10. #include <stdio.h>

11. #include <stdlib.h>

12. #include <string.h>

13. #include <errno.h>

15. #include "lj_def.h"

16. #include "lj_arch.h"

18. /* Hardcoded limits. Increase as needed. */

19. ‌#define BUILD_MAX_RELOC                200        /* Max. number of relocations. */

20. ‌#define BUILD_MAX_FOLD                4096        /* Max. number of fold rules. */

22. /* Prefix for scanned library definitions. */

23. ‌#define LIBDEF_PREFIX                "LJLIB_"

25. /* Prefix for scanned fold definitions. */

26. ‌#define FOLDDEF_PREFIX                "LJFOLD"

28. /* Prefixes for generated labels. */

29. ‌#define LABEL_PREFIX                "lj_"

30. ‌#define LABEL_PREFIX_BC                LABEL_PREFIX "BC_"

31. ‌#define LABEL_PREFIX_FF                LABEL_PREFIX "ff_"

32. ‌#define LABEL_PREFIX_CF                LABEL_PREFIX "cf_"

33. ‌#define LABEL_PREFIX_FFH        LABEL_PREFIX "ffh_"

34. ‌#define LABEL_PREFIX_LIBCF        LABEL_PREFIX "lib_cf_"

35. ‌#define LABEL_PREFIX_LIBINIT        LABEL_PREFIX "lib_init_"

37. /* Forward declaration. */

38. struct dasm_State;

40. /* Build modes. */

41. ‌#define BUILDDEF(_) \

42. _(elfasm) _(coffasm) _(machasm) _(peobj) _(raw) \

43. _(bcdef) _(ffdef) _(libdef) _(recdef) _(vmdef) \

44. _(folddef)

46. typedef enum {

47. ‌#define BUILDENUM(name)                BUILD_##name,

48. BUILDDEF(BUILDENUM)

49. ‌#undef BUILDENUM

50. BUILD__MAX

51. ‌} BuildMode;

53. /* Code relocation. */

54. ‌typedef struct BuildReloc {

55. int32_t ofs;

56. int sym;

57. int type;

58. ‌} BuildReloc;

60. ‌typedef struct BuildSym {

61. const char *name;

62. int32_t ofs;

63. ‌} BuildSym;

65. /* Build context structure. */

66. ‌typedef struct BuildCtx {

67. /* DynASM state pointer. Should be first member. */

68. struct dasm_State *D;

69. /* Parsed command line. */

70. BuildMode mode;

71. FILE *fp;

72. const char *outname;

73. char **args;

74. /* Code and symbols generated by DynASM. */

75. uint8_t *code;

76. size_t codesz;

77. int npc, nglob, nsym, nreloc, nrelocsym;

78. void **glob;

79. BuildSym *sym;

80. const char **relocsym;

81. int32_t *bc_ofs;

82. const char *beginsym;

83. /* Strings generated by DynASM. */

84. const char *const *globnames;

85. const char *const *extnames;

86. const char *dasm_ident;

87. const char *dasm_arch;

88. /* Relocations. */

89. BuildReloc reloc[BUILD_MAX_RELOC];

90. ‌} BuildCtx;

92. extern void owrite(BuildCtx *ctx, const void *ptr, size_t sz);

93. extern void emit_asm(BuildCtx *ctx);

94. extern void emit_peobj(BuildCtx *ctx);

95. extern void emit_lib(BuildCtx *ctx);

96. extern void emit_fold(BuildCtx *ctx);

98. extern const char *const bc_names[];

99. extern const char *const ir_names[];

100. extern const char *const irt_names[];

101. extern const char *const irfpm_names[];

102. extern const char *const irfield_names[];

103. extern const char *const ircall_names[];

105. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_opt_fold.c - luajit-2.0-src

Data types defined

- FoldFunc

Functions defined

- LJFOLDF(kfold_numarith)

- LJFOLDF(kfold_ldexp)

- LJFOLDF(kfold_fpmath)

- LJFOLDF(kfold_numpow)

- LJFOLDF(kfold_numcomp)

- LJFOLDF(kfold_intarith)

- LJFOLDF(kfold_intovarith)

- LJFOLDF(kfold_bnot)

- LJFOLDF(kfold_bswap)

- LJFOLDF(kfold_intcomp)

- LJFOLDF(kfold_intcomp0)

- LJFOLDF(kfold_int64arith)

- LJFOLDF(kfold_int64arith2)

- LJFOLDF(kfold_int64shift)

- LJFOLDF(kfold_bnot64)

- LJFOLDF(kfold_bswap64)

- LJFOLDF(kfold_int64comp)

- LJFOLDF(kfold_int64comp0)

- LJFOLDF(kfold_snew_kptr)

- LJFOLDF(kfold_snew_empty)

- LJFOLDF(kfold_strref)

- LJFOLDF(kfold_strref_snew)

- LJFOLDF(kfold_strcmp)

- LJFOLDF(bufput_append)

- LJFOLDF(bufput_kgc)

- LJFOLDF(bufstr_kfold_cse)

- LJFOLDF(bufput_kfold_op)

- LJFOLDF(bufput_kfold_rep)

- LJFOLDF(bufput_kfold_fmt)

- LJFOLDF(kfold_add_kgc)

- LJFOLDF(kfold_add_kptr)

- LJFOLDF(kfold_add_kright)

- LJFOLDF(kfold_tobit)

- LJFOLDF(kfold_conv_kint_num)

- LJFOLDF(kfold_conv_kintu32_num)

- LJFOLDF(kfold_conv_kint_ext)

- LJFOLDF(kfold_conv_kint_i64)

- LJFOLDF(kfold_conv_kint64_num_i64)

- LJFOLDF(kfold_conv_kint64_num_u64)

- LJFOLDF(kfold_conv_kint64_int_i64)

- LJFOLDF(kfold_conv_knum_int_num)

- LJFOLDF(kfold_conv_knum_u32_num)

- LJFOLDF(kfold_conv_knum_i64_num)

- LJFOLDF(kfold_conv_knum_u64_num)

- LJFOLDF(kfold_tostr_knum)

- LJFOLDF(kfold_tostr_kint)

- LJFOLDF(kfold_strto)

- LJFOLDF(shortcut_round)

- LJFOLDF(shortcut_left)

- LJFOLDF(shortcut_dropleft)

- LJFOLDF(shortcut_leftleft)

- LJFOLDF(simplify_numadd_negx)

- LJFOLDF(simplify_numadd_xneg)

- LJFOLDF(simplify_numsub_k)

- LJFOLDF(simplify_numsub_negk)

- LJFOLDF(simplify_numsub_xneg)

- LJFOLDF(simplify_nummuldiv_k)

- LJFOLDF(simplify_nummuldiv_negk)

- LJFOLDF(simplify_nummuldiv_negneg)

- LJFOLDF(simplify_numpow_xk)

- LJFOLDF(simplify_numpow_kx)

- LJFOLDF(shortcut_conv_num_int)

- LJFOLDF(simplify_conv_int_num)

- LJFOLDF(simplify_conv_i64_num)

- LJFOLDF(simplify_conv_int_i64)

- LJFOLDF(simplify_conv_flt_num)

- LJFOLDF(simplify_tobit_conv)

- LJFOLDF(simplify_floor_conv)

- LJFOLDF(simplify_conv_sext)

- LJFOLDF(simplify_conv_narrow)

- LJFOLDF(cse_conv)

- LJFOLDF(narrow_convert)

- LJFOLDF(simplify_intadd_k)

- LJFOLDF(simplify_intmul_k)

- LJFOLDF(simplify_intsub_k)

- LJFOLDF(simplify_intsub_kleft)

- LJFOLDF(simplify_intadd_k64)

- LJFOLDF(simplify_intsub_k64)

- LJFOLDF(simplify_intmul_k32)

- LJFOLDF(simplify_intmul_k64)

- LJFOLDF(simplify_intmod_k)

- LJFOLDF(simplify_intmod_kleft)

- LJFOLDF(simplify_intsub)

- LJFOLDF(simplify_intsubadd_leftcancel)

- LJFOLDF(simplify_intsubsub_leftcancel)

- LJFOLDF(simplify_intsubsub_rightcancel)

- LJFOLDF(simplify_intsubadd_rightcancel)

- LJFOLDF(simplify_intsubaddadd_cancel)

- LJFOLDF(simplify_band_k)

- LJFOLDF(simplify_bor_k)

- LJFOLDF(simplify_bxor_k)

- LJFOLDF(simplify_shift_ik)

- LJFOLDF(simplify_shift_andk)

- LJFOLDF(simplify_shift1_ki)

- LJFOLDF(simplify_shift2_ki)

- LJFOLDF(simplify_shiftk_andk)

- LJFOLDF(simplify_andk_shiftk)

- LJFOLDF(reassoc_intarith_k)

- LJFOLDF(reassoc_intarith_k64)

- LJFOLDF(reassoc_dup)

- LJFOLDF(reassoc_bxor)

- LJFOLDF(reassoc_shift)

- LJFOLDF(reassoc_minmax_k)

- LJFOLDF(reassoc_minmax_left)

- LJFOLDF(reassoc_minmax_right)

- LJFOLDF(abc_fwd)

- LJFOLDF(abc_k)

- LJFOLDF(abc_invar)

- LJFOLDF(comm_swap)

- LJFOLDF(comm_equal)

- LJFOLDF(comm_comp)

- LJFOLDF(comm_dup)

- LJFOLDF(comm_bxor)

- LJFOLDF(merge_eqne_snew_kgc)

- LJFOLDF(fwd_href_tdup)

- LJFOLDF(fload_tab_tnew_asize)

- LJFOLDF(fload_tab_tnew_hmask)

- LJFOLDF(fload_tab_tdup_asize)

- LJFOLDF(fload_tab_tdup_hmask)

- LJFOLDF(fload_tab_ah)

- LJFOLDF(fload_str_len_kgc)

- LJFOLDF(fload_str_len_snew)

- LJFOLDF(fload_str_len_tostr)

- LJFOLDF(fload_cdata_typeid_kgc)

- LJFOLDF(fload_cdata_int64_kgc)

- LJFOLDF(fload_cdata_typeid_cnew)

- LJFOLDF(fload_cdata_ptr_int64_cnew)

- LJFOLDF(xload_kptr)

- LJFOLDF(barrier_tnew_tdup)

- LJFOLDF(prof)

- LJFOLDX(lj_opt_cse)

- LJFOLDX(lj_opt_fwd_aload)

- LJFOLDX(lj_opt_fwd_hload)

- LJFOLDX(lj_opt_fwd_hrefk)

- LJFOLDX(lj_opt_cse)

- LJFOLDX(lj_opt_fwd_xload)

- LJFOLDX(lj_opt_dse_ahstore)

- kfold_int64arith

- kfold_intop

- kfold_xload

- lj_opt_cse

- lj_opt_cselim

- simplify_intmul_k

Macros defined

- FOLD_SNEW_MAX_LEN

- FOLD_SNEW_TYPE8

- IR

- IR

- LJFOLD

- LJFOLDF

- LJFOLDX

- LUA_CORE

- PHIBARRIER

- emitir

- emitir

- fins

- fins

- fleft

- fleft

- fright

- fright

- gcstep_barrier

- knumleft

- knumleft

- knumright

- knumright

- lj_opt_fold_c

Source code

1. /*

2. ** FOLD: Constant Folding, Algebraic Simplifications and Reassociation.

3. ** ABCelim: Array Bounds Check Elimination.

4. ** CSE: Common-Subexpression Elimination.

5. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

6. */

8. ‌#define lj_opt_fold_c

9. ‌#define LUA_CORE

11. #include <math.h>

13. #include "lj_obj.h"

15. #if LJ_HASJIT

17. #include "lj_buf.h"

18. #include "lj_str.h"

19. #include "lj_tab.h"

20. #include "lj_ir.h"

21. #include "lj_jit.h"

22. #include "lj_ircall.h"

23. #include "lj_iropt.h"

24. #include "lj_trace.h"

25. #if LJ_HASFFI

26. #include "lj_ctype.h"

27. #include "lj_carith.h"

28. #endif

29. #include "lj_vm.h"

30. #include "lj_strscan.h"

31. #include "lj_strfmt.h"

33. /* Here's a short description how the FOLD engine processes instructions:

34. **

35. ** The FOLD engine receives a single instruction stored in fins (J->fold.ins).

36. ** The instruction and its operands are used to select matching fold rules.

37. ** These are applied iteratively until a fixed point is reached.

38. **

39. ** The 8 bit opcode of the instruction itself plus the opcodes of the

40. ** two instructions referenced by its operands form a 24 bit key

41. ** 'ins left right' (unused operands -> 0, literals -> lowest 8 bits).

42. **

43. ** This key is used for partial matching against the fold rules. The

44. ** left/right operand fields of the key are successively masked with

45. ** the 'any' wildcard, from most specific to least specific:

46. **

47. **   ins left right

48. **   ins any  right

49. **   ins left any

50. **   ins any  any

51. **

52. ** The masked key is used to lookup a matching fold rule in a semi-perfect

53. ** hash table. If a matching rule is found, the related fold function is run.

54. ** Multiple rules can share the same fold function. A fold rule may return

55. ** one of several special values:

56. **

57. ** - NEXTFOLD means no folding was applied, because an additional test

58. **   inside the fold function failed. Matching continues against less

59. **   specific fold rules. Finally the instruction is passed on to CSE.

60. **

61. ** - RETRYFOLD means the instruction was modified in-place. Folding is

62. **   retried as if this instruction had just been received.

63. **

64. ** All other return values are terminal actions -- no further folding is

65. ** applied:

66. **

67. ** - INTFOLD(i) returns a reference to the integer constant i.

68. **

69. ** - LEFTFOLD and RIGHTFOLD return the left/right operand reference

70. **   without emitting an instruction.

71. **

72. ** - CSEFOLD and EMITFOLD pass the instruction directly to CSE or emit

73. **   it without passing through any further optimizations.

74. **

75. ** - FAILFOLD, DROPFOLD and CONDFOLD only apply to instructions which have

76. **   no result (e.g. guarded assertions): FAILFOLD means the guard would

77. **   always fail, i.e. the current trace is pointless. DROPFOLD means

78. **   the guard is always true and has been eliminated. CONDFOLD is a

79. **   shortcut for FAILFOLD + cond (i.e. drop if true, otherwise fail).

80. **

81. ** - Any other return value is interpreted as an IRRef or TRef. This

82. **   can be a reference to an existing or a newly created instruction.

83. **   Only the least-significant 16 bits (IRRef1) are used to form a TRef

84. **   which is finally returned to the caller.

85. **

86. ** The FOLD engine receives instructions both from the trace recorder and

87. ** substituted instructions from LOOP unrolling. This means all types

88. ** of instructions may end up here, even though the recorder bypasses

89. ** FOLD in some cases. Thus all loads, stores and allocations must have

90. ** an any/any rule to avoid being passed on to CSE.

91. **

92. ** Carefully read the following requirements before adding or modifying

93. ** any fold rules:

94. **

95. ** Requirement #1: All fold rules must preserve their destination type.

96. **

97. ** Consistently use INTFOLD() (KINT result) or lj_ir_knum() (KNUM result).

98. ** Never use lj_ir_knumint() which can have either a KINT or KNUM result.

99. **

100. ** Requirement #2: Fold rules should not create *new* instructions which

101. ** reference operands *across* PHIs.

102. **

103. ** E.g. a RETRYFOLD with 'fins->op1 = fleft->op1' is invalid if the

104. ** left operand is a PHI. Then fleft->op1 would point across the PHI

105. ** frontier to an invariant instruction. Adding a PHI for this instruction

106. ** would be counterproductive. The solution is to add a barrier which

107. ** prevents folding across PHIs, i.e. 'PHIBARRIER(fleft)' in this case.

108. ** The only exception is for recurrences with high latencies like

109. ** repeated int->num->int conversions.

110. **

111. ** One could relax this condition a bit if the referenced instruction is

112. ** a PHI, too. But this often leads to worse code due to excessive

113. ** register shuffling.

114. **

115. ** Note: returning *existing* instructions (e.g. LEFTFOLD) is ok, though.

116. ** Even returning fleft->op1 would be ok, because a new PHI will added,

117. ** if needed. But again, this leads to excessive register shuffling and

118. ** should be avoided.

119. **

120. ** Requirement #3: The set of all fold rules must be monotonic to guarantee

121. ** termination.

122. **

123. ** The goal is optimization, so one primarily wants to add strength-reducing

124. ** rules. This means eliminating an instruction or replacing an instruction

125. ** with one or more simpler instructions. Don't add fold rules which point

126. ** into the other direction.

127. **

128. ** Some rules (like commutativity) do not directly reduce the strength of

129. ** an instruction, but enable other fold rules (e.g. by moving constants

130. ** to the right operand). These rules must be made unidirectional to avoid

131. ** cycles.

132. **

133. ** Rule of thumb: the trace recorder expands the IR and FOLD shrinks it.

134. */

136. /* Some local macros to save typing. Undef'd at the end. */

137. ‌#define IR(ref)                (&J->cur.ir[(ref)])

138. ‌#define fins                (&J->fold.ins)

139. ‌#define fleft                (&J->fold.left)

140. ‌#define fright                (&J->fold.right)

141. ‌#define knumleft        (ir_knum(fleft)->n)

142. ‌#define knumright        (ir_knum(fright)->n)

144. /* Pass IR on to next optimization in chain (FOLD). */

145. ‌#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))

147. /* Fold function type. Fastcall on x86 significantly reduces their size. */

148. ‌typedef IRRef (LJ_FASTCALL *FoldFunc)(jit_State *J);

150. /* Macros for the fold specs, so buildvm can recognize them. */

151. ‌#define LJFOLD(x)

152. ‌#define LJFOLDX(x)

153. ‌#define LJFOLDF(name)        static TRef LJ_FASTCALL fold_##name(jit_State *J)

154. /* Note: They must be at the start of a line or buildvm ignores them! */

156. /* Barrier to prevent using operands across PHIs. */

157. ‌#define PHIBARRIER(ir)        if (irt_isphi((ir)->t)) return NEXTFOLD

159. /* Barrier to prevent folding across a GC step.

160. ** GC steps can only happen at the head of a trace and at LOOP.

161. ** And the GC is only driven forward if there's at least one allocation.

162. */

163. ‌#define gcstep_barrier(J, ref) \

164. ((ref) < J->chain[IR_LOOP] && \

165. (J->chain[IR_SNEW] || J->chain[IR_XSNEW] || \

166. J->chain[IR_TNEW] || J->chain[IR_TDUP] || \

167. J->chain[IR_CNEW] || J->chain[IR_CNEWI] || \

168. J->chain[IR_BUFSTR] || J->chain[IR_TOSTR] || J->chain[IR_CALLA]))

170. /* -- Constant folding for FP numbers ------------------------------------- */

172. LJFOLD(ADD KNUM KNUM)

173. LJFOLD(SUB KNUM KNUM)

174. LJFOLD(MUL KNUM KNUM)

175. LJFOLD(DIV KNUM KNUM)

176. LJFOLD(NEG KNUM KNUM)

177. LJFOLD(ABS KNUM KNUM)

178. LJFOLD(ATAN2 KNUM KNUM)

179. LJFOLD(LDEXP KNUM KNUM)

180. LJFOLD(MIN KNUM KNUM)

181. LJFOLD(MAX KNUM KNUM)

182. ‌LJFOLDF(kfold_numarith)

183. {

184. lua_Number a = knumleft;

185. lua_Number b = knumright;

186. lua_Number y = lj_vm_foldarith(a, b, fins->o - IR_ADD);

187. return lj_ir_knum(J, y);

188. }

190. LJFOLD(LDEXP KNUM KINT)

191. ‌LJFOLDF(kfold_ldexp)

192. {

193. #if LJ_TARGET_X86ORX64

194. UNUSED(J);

195. return NEXTFOLD;

196. #else

197. return lj_ir_knum(J, ldexp(knumleft, fright->i));

198. #endif

199. }

201. LJFOLD(FPMATH KNUM any)

202. ‌LJFOLDF(kfold_fpmath)

203. {

204. lua_Number a = knumleft;

205. lua_Number y = lj_vm_foldfpm(a, fins->op2);

206. return lj_ir_knum(J, y);

207. }

209. LJFOLD(POW KNUM KINT)

210. ‌LJFOLDF(kfold_numpow)

211. {

212. lua_Number a = knumleft;

213. lua_Number b = (lua_Number)fright->i;

214. lua_Number y = lj_vm_foldarith(a, b, IR_POW - IR_ADD);

215. return lj_ir_knum(J, y);

216. }

218. /* Must not use kfold_kref for numbers (could be NaN). */

219. LJFOLD(EQ KNUM KNUM)

220. LJFOLD(NE KNUM KNUM)

221. LJFOLD(LT KNUM KNUM)

222. LJFOLD(GE KNUM KNUM)

223. LJFOLD(LE KNUM KNUM)

224. LJFOLD(GT KNUM KNUM)

225. LJFOLD(ULT KNUM KNUM)

226. LJFOLD(UGE KNUM KNUM)

227. LJFOLD(ULE KNUM KNUM)

228. LJFOLD(UGT KNUM KNUM)

229. ‌LJFOLDF(kfold_numcomp)

230. {

231. return CONDFOLD(lj_ir_numcmp(knumleft, knumright, (IROp)fins->o));

232. }

234. /* -- Constant folding for 32 bit integers -------------------------------- */

236. ‌static int32_t kfold_intop(int32_t k1, int32_t k2, IROp op)

237. {

238. switch (op) {

239. case IR_ADD: k1 += k2; break;

240. case IR_SUB: k1 -= k2; break;

241. case IR_MUL: k1 *= k2; break;

242. case IR_MOD: k1 = lj_vm_modi(k1, k2); break;

243. case IR_NEG: k1 = -k1; break;

244. case IR_BAND: k1 &= k2; break;

245. case IR_BOR: k1 |= k2; break;

246. case IR_BXOR: k1 ^= k2; break;

247. case IR_BSHL: k1 <<= (k2 & 31); break;

248. case IR_BSHR: k1 = (int32_t)((uint32_t)k1 >> (k2 & 31)); break;

249. case IR_BSAR: k1 >>= (k2 & 31); break;

250. case IR_BROL: k1 = (int32_t)lj_rol((uint32_t)k1, (k2 & 31)); break;

251. case IR_BROR: k1 = (int32_t)lj_ror((uint32_t)k1, (k2 & 31)); break;

252. case IR_MIN: k1 = k1 < k2 ? k1 : k2; break;

253. case IR_MAX: k1 = k1 > k2 ? k1 : k2; break;

254. default: lua_assert(0); break;

255. }

256. return k1;

257. }

259. LJFOLD(ADD KINT KINT)

260. LJFOLD(SUB KINT KINT)

261. LJFOLD(MUL KINT KINT)

262. LJFOLD(MOD KINT KINT)

263. LJFOLD(NEG KINT KINT)

264. LJFOLD(BAND KINT KINT)

265. LJFOLD(BOR KINT KINT)

266. LJFOLD(BXOR KINT KINT)

267. LJFOLD(BSHL KINT KINT)

268. LJFOLD(BSHR KINT KINT)

269. LJFOLD(BSAR KINT KINT)

270. LJFOLD(BROL KINT KINT)

271. LJFOLD(BROR KINT KINT)

272. LJFOLD(MIN KINT KINT)

273. LJFOLD(MAX KINT KINT)

274. ‌LJFOLDF(kfold_intarith)

275. {

276. return INTFOLD(kfold_intop(fleft->i, fright->i, (IROp)fins->o));

277. }

279. LJFOLD(ADDOV KINT KINT)

280. LJFOLD(SUBOV KINT KINT)

281. LJFOLD(MULOV KINT KINT)

282. ‌LJFOLDF(kfold_intovarith)

283. {

284. lua_Number n = lj_vm_foldarith((lua_Number)fleft->i, (lua_Number)fright->i,

285. fins->o - IR_ADDOV);

286. int32_t k = lj_num2int(n);

287. if (n != (lua_Number)k)

288. return FAILFOLD;

289. return INTFOLD(k);

290. }

292. LJFOLD(BNOT KINT)

293. ‌LJFOLDF(kfold_bnot)

294. {

295. return INTFOLD(~fleft->i);

296. }

298. LJFOLD(BSWAP KINT)

299. ‌LJFOLDF(kfold_bswap)

300. {

301. return INTFOLD((int32_t)lj_bswap((uint32_t)fleft->i));

302. }

304. LJFOLD(LT KINT KINT)

305. LJFOLD(GE KINT KINT)

306. LJFOLD(LE KINT KINT)

307. LJFOLD(GT KINT KINT)

308. LJFOLD(ULT KINT KINT)

309. LJFOLD(UGE KINT KINT)

310. LJFOLD(ULE KINT KINT)

311. LJFOLD(UGT KINT KINT)

312. LJFOLD(ABC KINT KINT)

313. ‌LJFOLDF(kfold_intcomp)

314. {

315. int32_t a = fleft->i, b = fright->i;

316. switch ((IROp)fins->o) {

317. case IR_LT: return CONDFOLD(a < b);

318. case IR_GE: return CONDFOLD(a >= b);

319. case IR_LE: return CONDFOLD(a <= b);

320. case IR_GT: return CONDFOLD(a > b);

321. case IR_ULT: return CONDFOLD((uint32_t)a < (uint32_t)b);

322. case IR_UGE: return CONDFOLD((uint32_t)a >= (uint32_t)b);

323. case IR_ULE: return CONDFOLD((uint32_t)a <= (uint32_t)b);

324. case IR_ABC:

325. case IR_UGT: return CONDFOLD((uint32_t)a > (uint32_t)b);

326. default: lua_assert(0); return FAILFOLD;

327. }

328. }

330. LJFOLD(UGE any KINT)

331. ‌LJFOLDF(kfold_intcomp0)

332. {

333. if (fright->i == 0)

334. return DROPFOLD;

335. return NEXTFOLD;

336. }

338. /* -- Constant folding for 64 bit integers -------------------------------- */

340. ‌static uint64_t kfold_int64arith(uint64_t k1, uint64_t k2, IROp op)

341. {

342. switch (op) {

343. #if LJ_HASFFI

344. case IR_ADD: k1 += k2; break;

345. case IR_SUB: k1 -= k2; break;

346. case IR_MUL: k1 *= k2; break;

347. case IR_BAND: k1 &= k2; break;

348. case IR_BOR: k1 |= k2; break;

349. case IR_BXOR: k1 ^= k2; break;

350. #endif

351. default: UNUSED(k2); lua_assert(0); break;

352. }

353. return k1;

354. }

356. LJFOLD(ADD KINT64 KINT64)

357. LJFOLD(SUB KINT64 KINT64)

358. LJFOLD(MUL KINT64 KINT64)

359. LJFOLD(BAND KINT64 KINT64)

360. LJFOLD(BOR KINT64 KINT64)

361. LJFOLD(BXOR KINT64 KINT64)

362. ‌LJFOLDF(kfold_int64arith)

363. {

364. return INT64FOLD(kfold_int64arith(ir_k64(fleft)->u64,

365. ir_k64(fright)->u64, (IROp)fins->o));

366. }

368. LJFOLD(DIV KINT64 KINT64)

369. LJFOLD(MOD KINT64 KINT64)

370. LJFOLD(POW KINT64 KINT64)

371. ‌LJFOLDF(kfold_int64arith2)

372. {

373. #if LJ_HASFFI

374. uint64_t k1 = ir_k64(fleft)->u64, k2 = ir_k64(fright)->u64;

375. if (irt_isi64(fins->t)) {

376. k1 = fins->o == IR_DIV ? lj_carith_divi64((int64_t)k1, (int64_t)k2) :

377. fins->o == IR_MOD ? lj_carith_modi64((int64_t)k1, (int64_t)k2) :

378. lj_carith_powi64((int64_t)k1, (int64_t)k2);

379. } else {

380. k1 = fins->o == IR_DIV ? lj_carith_divu64(k1, k2) :

381. fins->o == IR_MOD ? lj_carith_modu64(k1, k2) :

382. lj_carith_powu64(k1, k2);

383. }

384. return INT64FOLD(k1);

385. #else

386. UNUSED(J); lua_assert(0); return FAILFOLD;

387. #endif

388. }

390. LJFOLD(BSHL KINT64 KINT)

391. LJFOLD(BSHR KINT64 KINT)

392. LJFOLD(BSAR KINT64 KINT)

393. LJFOLD(BROL KINT64 KINT)

394. LJFOLD(BROR KINT64 KINT)

395. ‌LJFOLDF(kfold_int64shift)

396. {

397. #if LJ_HASFFI

398. uint64_t k = ir_k64(fleft)->u64;

399. int32_t sh = (fright->i & 63);

400. return INT64FOLD(lj_carith_shift64(k, sh, fins->o - IR_BSHL));

401. #else

402. UNUSED(J); lua_assert(0); return FAILFOLD;

403. #endif

404. }

406. LJFOLD(BNOT KINT64)

407. ‌LJFOLDF(kfold_bnot64)

408. {

409. #if LJ_HASFFI

410. return INT64FOLD(~ir_k64(fleft)->u64);

411. #else

412. UNUSED(J); lua_assert(0); return FAILFOLD;

413. #endif

414. }

416. LJFOLD(BSWAP KINT64)

417. ‌LJFOLDF(kfold_bswap64)

418. {

419. #if LJ_HASFFI

420. return INT64FOLD(lj_bswap64(ir_k64(fleft)->u64));

421. #else

422. UNUSED(J); lua_assert(0); return FAILFOLD;

423. #endif

424. }

426. LJFOLD(LT KINT64 KINT64)

427. LJFOLD(GE KINT64 KINT64)

428. LJFOLD(LE KINT64 KINT64)

429. LJFOLD(GT KINT64 KINT64)

430. LJFOLD(ULT KINT64 KINT64)

431. LJFOLD(UGE KINT64 KINT64)

432. LJFOLD(ULE KINT64 KINT64)

433. LJFOLD(UGT KINT64 KINT64)

434. ‌LJFOLDF(kfold_int64comp)

435. {

436. #if LJ_HASFFI

437. uint64_t a = ir_k64(fleft)->u64, b = ir_k64(fright)->u64;

438. switch ((IROp)fins->o) {

439. case IR_LT: return CONDFOLD(a < b);

440. case IR_GE: return CONDFOLD(a >= b);

441. case IR_LE: return CONDFOLD(a <= b);

442. case IR_GT: return CONDFOLD(a > b);

443. case IR_ULT: return CONDFOLD((uint64_t)a < (uint64_t)b);

444. case IR_UGE: return CONDFOLD((uint64_t)a >= (uint64_t)b);

445. case IR_ULE: return CONDFOLD((uint64_t)a <= (uint64_t)b);

446. case IR_UGT: return CONDFOLD((uint64_t)a > (uint64_t)b);

447. default: lua_assert(0); return FAILFOLD;

448. }

449. #else

450. UNUSED(J); lua_assert(0); return FAILFOLD;

451. #endif

452. }

454. LJFOLD(UGE any KINT64)

455. ‌LJFOLDF(kfold_int64comp0)

456. {

457. #if LJ_HASFFI

458. if (ir_k64(fright)->u64 == 0)

459. return DROPFOLD;

460. return NEXTFOLD;

461. #else

462. UNUSED(J); lua_assert(0); return FAILFOLD;

463. #endif

464. }

466. /* -- Constant folding for strings ---------------------------------------- */

468. LJFOLD(SNEW KKPTR KINT)

469. ‌LJFOLDF(kfold_snew_kptr)

470. {

471. GCstr *s = lj_str_new(J->L, (const char *)ir_kptr(fleft), (size_t)fright->i);

472. return lj_ir_kstr(J, s);

473. }

475. LJFOLD(SNEW any KINT)

476. ‌LJFOLDF(kfold_snew_empty)

477. {

478. if (fright->i == 0)

479. return lj_ir_kstr(J, &J2G(J)->strempty);

480. return NEXTFOLD;

481. }

483. LJFOLD(STRREF KGC KINT)

484. ‌LJFOLDF(kfold_strref)

485. {

486. GCstr *str = ir_kstr(fleft);

487. lua_assert((MSize)fright->i <= str->len);

488. return lj_ir_kkptr(J, (char *)strdata(str) + fright->i);

489. }

491. LJFOLD(STRREF SNEW any)

492. ‌LJFOLDF(kfold_strref_snew)

493. {

494. PHIBARRIER(fleft);

495. if (irref_isk(fins->op2) && fright->i == 0) {

496. return fleft->op1;  /* strref(snew(ptr, len), 0) ==> ptr */

497. } else {

498. /* Reassociate: strref(snew(strref(str, a), len), b) ==> strref(str, a+b) */

499. IRIns *ir = IR(fleft->op1);

500. if (ir->o == IR_STRREF) {

501. IRRef1 str = ir->op1;  /* IRIns * is not valid across emitir. */

502. PHIBARRIER(ir);

503. fins->op2 = emitir(IRTI(IR_ADD), ir->op2, fins->op2); /* Clobbers fins! */

504. fins->op1 = str;

505. fins->ot = IRT(IR_STRREF, IRT_P32);

506. return RETRYFOLD;

507. }

508. }

509. return NEXTFOLD;

510. }

512. LJFOLD(CALLN CARG IRCALL_lj_str_cmp)

513. ‌LJFOLDF(kfold_strcmp)

514. {

515. if (irref_isk(fleft->op1) && irref_isk(fleft->op2)) {

516. GCstr *a = ir_kstr(IR(fleft->op1));

517. GCstr *b = ir_kstr(IR(fleft->op2));

518. return INTFOLD(lj_str_cmp(a, b));

519. }

520. return NEXTFOLD;

521. }

523. /* -- Constant folding and forwarding for buffers ------------------------- */

525. /*

526. ** Buffer ops perform stores, but their effect is limited to the buffer

527. ** itself. Also, buffer ops are chained: a use of an op implies a use of

528. ** all other ops up the chain. Conversely, if an op is unused, all ops

529. ** up the chain can go unsed. This largely eliminates the need to treat

530. ** them as stores.

531. **

532. ** Alas, treating them as normal (IRM_N) ops doesn't work, because they

533. ** cannot be CSEd in isolation. CSE for IRM_N is implicitly done in LOOP

534. ** or if FOLD is disabled.

535. **

536. ** The compromise is to declare them as loads, emit them like stores and

537. ** CSE whole chains manually when the BUFSTR is to be emitted. Any chain

538. ** fragments left over from CSE are eliminated by DCE.

539. */

541. /* BUFHDR is emitted like a store, see below. */

543. LJFOLD(BUFPUT BUFHDR BUFSTR)

544. ‌LJFOLDF(bufput_append)

545. {

546. /* New buffer, no other buffer op inbetween and same buffer? */

547. if ((J->flags & JIT_F_OPT_FWD) &&

548. !(fleft->op2 & IRBUFHDR_APPEND) &&

549. fleft->prev == fright->op2 &&

550. fleft->op1 == IR(fright->op2)->op1) {

551. IRRef ref = fins->op1;

552. IR(ref)->op2 = (fleft->op2 | IRBUFHDR_APPEND);  /* Modify BUFHDR. */

553. IR(ref)->op1 = fright->op1;

554. return ref;

555. }

556. return EMITFOLD;  /* Always emit, CSE later. */

557. }

559. LJFOLD(BUFPUT any any)

560. ‌LJFOLDF(bufput_kgc)

561. {

562. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && fright->o == IR_KGC) {

563. GCstr *s2 = ir_kstr(fright);

564. if (s2->len == 0) {  /* Empty string? */

565. return LEFTFOLD;

566. } else {

567. if (fleft->o == IR_BUFPUT && irref_isk(fleft->op2) &&

568. !irt_isphi(fleft->t)) {  /* Join two constant string puts in a row. */

569. GCstr *s1 = ir_kstr(IR(fleft->op2));

570. IRRef kref = lj_ir_kstr(J, lj_buf_cat2str(J->L, s1, s2));

571. /* lj_ir_kstr() may realloc the IR and invalidates any IRIns *. */

572. IR(fins->op1)->op2 = kref;  /* Modify previous BUFPUT. */

573. return fins->op1;

574. }

575. }

576. }

577. return EMITFOLD;  /* Always emit, CSE later. */

578. }

580. LJFOLD(BUFSTR any any)

581. ‌LJFOLDF(bufstr_kfold_cse)

582. {

583. lua_assert(fleft->o == IR_BUFHDR || fleft->o == IR_BUFPUT ||

584. fleft->o == IR_CALLL);

585. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) {

586. if (fleft->o == IR_BUFHDR) {  /* No put operations? */

587. if (!(fleft->op2 & IRBUFHDR_APPEND))  /* Empty buffer? */

588. return lj_ir_kstr(J, &J2G(J)->strempty);

589. fins->op1 = fleft->op1;

590. fins->op2 = fleft->prev;  /* Relies on checks in bufput_append. */

591. return CSEFOLD;

592. } else if (fleft->o == IR_BUFPUT) {

593. IRIns *irb = IR(fleft->op1);

594. if (irb->o == IR_BUFHDR && !(irb->op2 & IRBUFHDR_APPEND))

595. return fleft->op2;  /* Shortcut for a single put operation. */

596. }

597. }

598. /* Try to CSE the whole chain. */

599. if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) {

600. IRRef ref = J->chain[IR_BUFSTR];

601. while (ref) {

602. IRIns *irs = IR(ref), *ira = fleft, *irb = IR(irs->op1);

603. while (ira->o == irb->o && ira->op2 == irb->op2) {

604. lua_assert(ira->o == IR_BUFHDR || ira->o == IR_BUFPUT ||

605. ira->o == IR_CALLL || ira->o == IR_CARG);

606. if (ira->o == IR_BUFHDR && !(ira->op2 & IRBUFHDR_APPEND))

607. return ref;  /* CSE succeeded. */

608. if (ira->o == IR_CALLL && ira->op2 == IRCALL_lj_buf_puttab)

609. break;

610. ira = IR(ira->op1);

611. irb = IR(irb->op1);

612. }

613. ref = irs->prev;

614. }

615. }

616. return EMITFOLD;  /* No CSE possible. */

617. }

619. LJFOLD(CALLL CARG IRCALL_lj_buf_putstr_reverse)

620. LJFOLD(CALLL CARG IRCALL_lj_buf_putstr_upper)

621. LJFOLD(CALLL CARG IRCALL_lj_buf_putstr_lower)

622. LJFOLD(CALLL CARG IRCALL_lj_strfmt_putquoted)

623. ‌LJFOLDF(bufput_kfold_op)

624. {

625. if (irref_isk(fleft->op2)) {

626. const CCallInfo *ci = &lj_ir_callinfo[fins->op2];

627. SBuf *sb = lj_buf_tmp_(J->L);

628. sb = ((SBuf * (LJ_FASTCALL *)(SBuf *, GCstr *))ci->func)(sb,

629. ir_kstr(IR(fleft->op2)));

630. fins->o = IR_BUFPUT;

631. fins->op1 = fleft->op1;

632. fins->op2 = lj_ir_kstr(J, lj_buf_tostr(sb));

633. return RETRYFOLD;

634. }

635. return EMITFOLD;  /* Always emit, CSE later. */

636. }

638. LJFOLD(CALLL CARG IRCALL_lj_buf_putstr_rep)

639. ‌LJFOLDF(bufput_kfold_rep)

640. {

641. if (irref_isk(fleft->op2)) {

642. IRIns *irc = IR(fleft->op1);

643. if (irref_isk(irc->op2)) {

644. SBuf *sb = lj_buf_tmp_(J->L);

645. sb = lj_buf_putstr_rep(sb, ir_kstr(IR(irc->op2)), IR(fleft->op2)->i);

646. fins->o = IR_BUFPUT;

647. fins->op1 = irc->op1;

648. fins->op2 = lj_ir_kstr(J, lj_buf_tostr(sb));

649. return RETRYFOLD;

650. }

651. }

652. return EMITFOLD;  /* Always emit, CSE later. */

653. }

655. LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfxint)

656. LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfnum_int)

657. LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfnum_uint)

658. LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfnum)

659. LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfstr)

660. LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfchar)

661. ‌LJFOLDF(bufput_kfold_fmt)

662. {

663. IRIns *irc = IR(fleft->op1);

664. lua_assert(irref_isk(irc->op2));  /* SFormat must be const. */

665. if (irref_isk(fleft->op2)) {

666. SFormat sf = (SFormat)IR(irc->op2)->i;

667. IRIns *ira = IR(fleft->op2);

668. SBuf *sb = lj_buf_tmp_(J->L);

669. switch (fins->op2) {

670. case IRCALL_lj_strfmt_putfxint:

671. sb = lj_strfmt_putfxint(sb, sf, ir_k64(ira)->u64);

672. break;

673. case IRCALL_lj_strfmt_putfstr:

674. sb = lj_strfmt_putfstr(sb, sf, ir_kstr(ira));

675. break;

676. case IRCALL_lj_strfmt_putfchar:

677. sb = lj_strfmt_putfchar(sb, sf, ira->i);

678. break;

679. case IRCALL_lj_strfmt_putfnum_int:

680. case IRCALL_lj_strfmt_putfnum_uint:

681. case IRCALL_lj_strfmt_putfnum:

682. default: {

683. const CCallInfo *ci = &lj_ir_callinfo[fins->op2];

684. sb = ((SBuf * (*)(SBuf *, SFormat, lua_Number))ci->func)(sb, sf,

685. ir_knum(ira)->n);

686. break;

687. }

688. }

689. fins->o = IR_BUFPUT;

690. fins->op1 = irc->op1;

691. fins->op2 = lj_ir_kstr(J, lj_buf_tostr(sb));

692. return RETRYFOLD;

693. }

694. return EMITFOLD;  /* Always emit, CSE later. */

695. }

697. /* -- Constant folding of pointer arithmetic ------------------------------ */

699. LJFOLD(ADD KGC KINT)

700. LJFOLD(ADD KGC KINT64)

701. ‌LJFOLDF(kfold_add_kgc)

702. {

703. GCobj *o = ir_kgc(fleft);

704. #if LJ_64

705. ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64;

706. #else

707. ptrdiff_t ofs = fright->i;

708. #endif

709. #if LJ_HASFFI

710. if (irt_iscdata(fleft->t)) {

711. CType *ct = ctype_raw(ctype_ctsG(J2G(J)), gco2cd(o)->ctypeid);

712. if (ctype_isnum(ct->info) || ctype_isenum(ct->info) ||

713. ctype_isptr(ct->info) || ctype_isfunc(ct->info) ||

714. ctype_iscomplex(ct->info) || ctype_isvector(ct->info))

715. return lj_ir_kkptr(J, (char *)o + ofs);

716. }

717. #endif

718. return lj_ir_kptr(J, (char *)o + ofs);

719. }

721. LJFOLD(ADD KPTR KINT)

722. LJFOLD(ADD KPTR KINT64)

723. LJFOLD(ADD KKPTR KINT)

724. LJFOLD(ADD KKPTR KINT64)

725. ‌LJFOLDF(kfold_add_kptr)

726. {

727. void *p = ir_kptr(fleft);

728. #if LJ_64

729. ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64;

730. #else

731. ptrdiff_t ofs = fright->i;

732. #endif

733. return lj_ir_kptr_(J, fleft->o, (char *)p + ofs);

734. }

736. LJFOLD(ADD any KGC)

737. LJFOLD(ADD any KPTR)

738. LJFOLD(ADD any KKPTR)

739. ‌LJFOLDF(kfold_add_kright)

740. {

741. if (fleft->o == IR_KINT || fleft->o == IR_KINT64) {

742. IRRef1 tmp = fins->op1; fins->op1 = fins->op2; fins->op2 = tmp;

743. return RETRYFOLD;

744. }

745. return NEXTFOLD;

746. }

748. /* -- Constant folding of conversions ------------------------------------- */

750. LJFOLD(TOBIT KNUM KNUM)

751. ‌LJFOLDF(kfold_tobit)

752. {

753. return INTFOLD(lj_num2bit(knumleft));

754. }

756. LJFOLD(CONV KINT IRCONV_NUM_INT)

757. ‌LJFOLDF(kfold_conv_kint_num)

758. {

759. return lj_ir_knum(J, (lua_Number)fleft->i);

760. }

762. LJFOLD(CONV KINT IRCONV_NUM_U32)

763. ‌LJFOLDF(kfold_conv_kintu32_num)

764. {

765. return lj_ir_knum(J, (lua_Number)(uint32_t)fleft->i);

766. }

768. LJFOLD(CONV KINT IRCONV_INT_I8)

769. LJFOLD(CONV KINT IRCONV_INT_U8)

770. LJFOLD(CONV KINT IRCONV_INT_I16)

771. LJFOLD(CONV KINT IRCONV_INT_U16)

772. ‌LJFOLDF(kfold_conv_kint_ext)

773. {

774. int32_t k = fleft->i;

775. if ((fins->op2 & IRCONV_SRCMASK) == IRT_I8) k = (int8_t)k;

776. else if ((fins->op2 & IRCONV_SRCMASK) == IRT_U8) k = (uint8_t)k;

777. else if ((fins->op2 & IRCONV_SRCMASK) == IRT_I16) k = (int16_t)k;

778. else k = (uint16_t)k;

779. return INTFOLD(k);

780. }

782. LJFOLD(CONV KINT IRCONV_I64_INT)

783. LJFOLD(CONV KINT IRCONV_U64_INT)

784. LJFOLD(CONV KINT IRCONV_I64_U32)

785. LJFOLD(CONV KINT IRCONV_U64_U32)

786. ‌LJFOLDF(kfold_conv_kint_i64)

787. {

788. if ((fins->op2 & IRCONV_SEXT))

789. return INT64FOLD((uint64_t)(int64_t)fleft->i);

790. else

791. return INT64FOLD((uint64_t)(int64_t)(uint32_t)fleft->i);

792. }

794. LJFOLD(CONV KINT64 IRCONV_NUM_I64)

795. ‌LJFOLDF(kfold_conv_kint64_num_i64)

796. {

797. return lj_ir_knum(J, (lua_Number)(int64_t)ir_kint64(fleft)->u64);

798. }

800. LJFOLD(CONV KINT64 IRCONV_NUM_U64)

801. ‌LJFOLDF(kfold_conv_kint64_num_u64)

802. {

803. return lj_ir_knum(J, (lua_Number)ir_kint64(fleft)->u64);

804. }

806. LJFOLD(CONV KINT64 IRCONV_INT_I64)

807. LJFOLD(CONV KINT64 IRCONV_U32_I64)

808. ‌LJFOLDF(kfold_conv_kint64_int_i64)

809. {

810. return INTFOLD((int32_t)ir_kint64(fleft)->u64);

811. }

813. LJFOLD(CONV KNUM IRCONV_INT_NUM)

814. ‌LJFOLDF(kfold_conv_knum_int_num)

815. {

816. lua_Number n = knumleft;

817. int32_t k = lj_num2int(n);

818. if (irt_isguard(fins->t) && n != (lua_Number)k) {

819. /* We're about to create a guard which always fails, like CONV +1.5.

820. ** Some pathological loops cause this during LICM, e.g.:

821. **   local x,k,t = 0,1.5,{1,[1.5]=2}

822. **   for i=1,200 do x = x+ t[k]; k = k == 1 and 1.5 or 1 end

823. **   assert(x == 300)

824. */

825. return FAILFOLD;

826. }

827. return INTFOLD(k);

828. }

830. LJFOLD(CONV KNUM IRCONV_U32_NUM)

831. ‌LJFOLDF(kfold_conv_knum_u32_num)

832. {

833. #ifdef _MSC_VER

834. {  /* Workaround for MSVC bug. */

835. volatile uint32_t u = (uint32_t)knumleft;

836. return INTFOLD((int32_t)u);

837. }

838. #else

839. return INTFOLD((int32_t)(uint32_t)knumleft);

840. #endif

841. }

843. LJFOLD(CONV KNUM IRCONV_I64_NUM)

844. ‌LJFOLDF(kfold_conv_knum_i64_num)

845. {

846. return INT64FOLD((uint64_t)(int64_t)knumleft);

847. }

849. LJFOLD(CONV KNUM IRCONV_U64_NUM)

850. ‌LJFOLDF(kfold_conv_knum_u64_num)

851. {

852. return INT64FOLD(lj_num2u64(knumleft));

853. }

855. LJFOLD(TOSTR KNUM any)

856. ‌LJFOLDF(kfold_tostr_knum)

857. {

858. return lj_ir_kstr(J, lj_strfmt_num(J->L, ir_knum(fleft)));

859. }

861. LJFOLD(TOSTR KINT any)

862. ‌LJFOLDF(kfold_tostr_kint)

863. {

864. return lj_ir_kstr(J, fins->op2 == IRTOSTR_INT ?

865. lj_strfmt_int(J->L, fleft->i) :

866. lj_strfmt_char(J->L, fleft->i));

867. }

869. LJFOLD(STRTO KGC)

870. ‌LJFOLDF(kfold_strto)

871. {

872. TValue n;

873. if (lj_strscan_num(ir_kstr(fleft), &n))

874. return lj_ir_knum(J, numV(&n));

875. return FAILFOLD;

876. }

878. /* -- Constant folding of equality checks --------------------------------- */

880. /* Don't constant-fold away FLOAD checks against KNULL. */

881. LJFOLD(EQ FLOAD KNULL)

882. LJFOLD(NE FLOAD KNULL)

883. ‌LJFOLDX(lj_opt_cse)

885. /* But fold all other KNULL compares, since only KNULL is equal to KNULL. */

886. LJFOLD(EQ any KNULL)

887. LJFOLD(NE any KNULL)

888. LJFOLD(EQ KNULL any)

889. LJFOLD(NE KNULL any)

890. LJFOLD(EQ KINT KINT)  /* Constants are unique, so same refs <==> same value. */

891. LJFOLD(NE KINT KINT)

892. LJFOLD(EQ KINT64 KINT64)

893. LJFOLD(NE KINT64 KINT64)

894. LJFOLD(EQ KGC KGC)

895. LJFOLD(NE KGC KGC)

896. LJFOLDF(kfold_kref)

897. {

898. return CONDFOLD((fins->op1 == fins->op2) ^ (fins->o == IR_NE));

899. }

901. /* -- Algebraic shortcuts ------------------------------------------------- */

903. LJFOLD(FPMATH FPMATH IRFPM_FLOOR)

904. LJFOLD(FPMATH FPMATH IRFPM_CEIL)

905. LJFOLD(FPMATH FPMATH IRFPM_TRUNC)

906. ‌LJFOLDF(shortcut_round)

907. {

908. IRFPMathOp op = (IRFPMathOp)fleft->op2;

909. if (op == IRFPM_FLOOR || op == IRFPM_CEIL || op == IRFPM_TRUNC)

910. return LEFTFOLD;  /* round(round_left(x)) = round_left(x) */

911. return NEXTFOLD;

912. }

914. LJFOLD(ABS ABS KNUM)

915. ‌LJFOLDF(shortcut_left)

916. {

917. return LEFTFOLD;  /* f(g(x)) ==> g(x) */

918. }

920. LJFOLD(ABS NEG KNUM)

921. ‌LJFOLDF(shortcut_dropleft)

922. {

923. PHIBARRIER(fleft);

924. fins->op1 = fleft->op1;  /* abs(neg(x)) ==> abs(x) */

925. return RETRYFOLD;

926. }

928. /* Note: no safe shortcuts with STRTO and TOSTR ("1e2" ==> +100 ==> "100"). */

929. LJFOLD(NEG NEG any)

930. LJFOLD(BNOT BNOT)

931. LJFOLD(BSWAP BSWAP)

932. ‌LJFOLDF(shortcut_leftleft)

933. {

934. PHIBARRIER(fleft);  /* See above. Fold would be ok, but not beneficial. */

935. return fleft->op1;  /* f(g(x)) ==> x */

936. }

938. /* -- FP algebraic simplifications ---------------------------------------- */

940. /* FP arithmetic is tricky -- there's not much to simplify.

941. ** Please note the following common pitfalls before sending "improvements":

942. **   x+0 ==> x  is INVALID for x=-0

943. **   0-x ==> -x is INVALID for x=+0

944. **   x*0 ==> 0  is INVALID for x=-0, x=+-Inf or x=NaN

945. */

947. LJFOLD(ADD NEG any)

948. ‌LJFOLDF(simplify_numadd_negx)

949. {

950. PHIBARRIER(fleft);

951. fins->o = IR_SUB;  /* (-a) + b ==> b - a */

952. fins->op1 = fins->op2;

953. fins->op2 = fleft->op1;

954. return RETRYFOLD;

955. }

957. LJFOLD(ADD any NEG)

958. ‌LJFOLDF(simplify_numadd_xneg)

959. {

960. PHIBARRIER(fright);

961. fins->o = IR_SUB;  /* a + (-b) ==> a - b */

962. fins->op2 = fright->op1;

963. return RETRYFOLD;

964. }

966. LJFOLD(SUB any KNUM)

967. ‌LJFOLDF(simplify_numsub_k)

968. {

969. lua_Number n = knumright;

970. if (n == 0.0)  /* x - (+-0) ==> x */

971. return LEFTFOLD;

972. return NEXTFOLD;

973. }

975. LJFOLD(SUB NEG KNUM)

976. ‌LJFOLDF(simplify_numsub_negk)

977. {

978. PHIBARRIER(fleft);

979. fins->op2 = fleft->op1;  /* (-x) - k ==> (-k) - x */

980. fins->op1 = (IRRef1)lj_ir_knum(J, -knumright);

981. return RETRYFOLD;

982. }

984. LJFOLD(SUB any NEG)

985. ‌LJFOLDF(simplify_numsub_xneg)

986. {

987. PHIBARRIER(fright);

988. fins->o = IR_ADD;  /* a - (-b) ==> a + b */

989. fins->op2 = fright->op1;

990. return RETRYFOLD;

991. }

993. LJFOLD(MUL any KNUM)

994. LJFOLD(DIV any KNUM)

995. ‌LJFOLDF(simplify_nummuldiv_k)

996. {

997. lua_Number n = knumright;

998. if (n == 1.0) {  /* x o 1 ==> x */

999. return LEFTFOLD;

1000. } else if (n == -1.0) {  /* x o -1 ==> -x */

1001. fins->o = IR_NEG;

1002. fins->op2 = (IRRef1)lj_ir_knum_neg(J);

1003. return RETRYFOLD;

1004. } else if (fins->o == IR_MUL && n == 2.0) {  /* x * 2 ==> x + x */

1005. fins->o = IR_ADD;

1006. fins->op2 = fins->op1;

1007. return RETRYFOLD;

1008. } else if (fins->o == IR_DIV) {  /* x / 2^k ==> x * 2^-k */

1009. uint64_t u = ir_knum(fright)->u64;

1010. uint32_t ex = ((uint32_t)(u >> 52) & 0x7ff);

1011. if ((u & U64x(000fffff,ffffffff)) == 0 && ex - 1 < 0x7fd) {

1012. u = (u & ((uint64_t)1 << 63)) | ((uint64_t)(0x7fe - ex) << 52);

1013. fins->o = IR_MUL;  /* Multiply by exact reciprocal. */

1014. fins->op2 = lj_ir_knum_u64(J, u);

1015. return RETRYFOLD;

1016. }

1017. }

1018. return NEXTFOLD;

1019. }

1021. LJFOLD(MUL NEG KNUM)

1022. LJFOLD(DIV NEG KNUM)

1023. ‌LJFOLDF(simplify_nummuldiv_negk)

1024. {

1025. PHIBARRIER(fleft);

1026. fins->op1 = fleft->op1;  /* (-a) o k ==> a o (-k) */

1027. fins->op2 = (IRRef1)lj_ir_knum(J, -knumright);

1028. return RETRYFOLD;

1029. }

1031. LJFOLD(MUL NEG NEG)

1032. LJFOLD(DIV NEG NEG)

1033. ‌LJFOLDF(simplify_nummuldiv_negneg)

1034. {

1035. PHIBARRIER(fleft);

1036. PHIBARRIER(fright);

1037. fins->op1 = fleft->op1;  /* (-a) o (-b) ==> a o b */

1038. fins->op2 = fright->op1;

1039. return RETRYFOLD;

1040. }

1042. LJFOLD(POW any KINT)

1043. ‌LJFOLDF(simplify_numpow_xk)

1044. {

1045. int32_t k = fright->i;

1046. TRef ref = fins->op1;

1047. if (k == 0)  /* x ^ 0 ==> 1 */

1048. return lj_ir_knum_one(J);  /* Result must be a number, not an int. */

1049. if (k == 1)  /* x ^ 1 ==> x */

1050. return LEFTFOLD;

1051. if ((uint32_t)(k+65536) > 2*65536u)  /* Limit code explosion. */

1052. return NEXTFOLD;

1053. if (k < 0) {  /* x ^ (-k) ==> (1/x) ^ k. */

1054. ref = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), ref);

1055. k = -k;

1056. }

1057. /* Unroll x^k for 1 <= k <= 65536. */

1058. for (; (k & 1) == 0; k >>= 1)  /* Handle leading zeros. */

1059. ref = emitir(IRTN(IR_MUL), ref, ref);

1060. if ((k >>= 1) != 0) {  /* Handle trailing bits. */

1061. TRef tmp = emitir(IRTN(IR_MUL), ref, ref);

1062. for (; k != 1; k >>= 1) {

1063. if (k & 1)

1064. ref = emitir(IRTN(IR_MUL), ref, tmp);

1065. tmp = emitir(IRTN(IR_MUL), tmp, tmp);

1066. }

1067. ref = emitir(IRTN(IR_MUL), ref, tmp);

1068. }

1069. return ref;

1070. }

1072. LJFOLD(POW KNUM any)

1073. ‌LJFOLDF(simplify_numpow_kx)

1074. {

1075. lua_Number n = knumleft;

1076. if (n == 2.0) {  /* 2.0 ^ i ==> ldexp(1.0, tonum(i)) */

1077. fins->o = IR_CONV;

1078. #if LJ_TARGET_X86ORX64

1079. fins->op1 = fins->op2;

1080. fins->op2 = IRCONV_NUM_INT;

1081. fins->op2 = (IRRef1)lj_opt_fold(J);

1082. #endif

1083. fins->op1 = (IRRef1)lj_ir_knum_one(J);

1084. fins->o = IR_LDEXP;

1085. return RETRYFOLD;

1086. }

1087. return NEXTFOLD;

1088. }

1090. /* -- Simplify conversions ------------------------------------------------ */

1092. LJFOLD(CONV CONV IRCONV_NUM_INT)  /* _NUM */

1093. ‌LJFOLDF(shortcut_conv_num_int)

1094. {

1095. PHIBARRIER(fleft);

1096. /* Only safe with a guarded conversion to int. */

1097. if ((fleft->op2 & IRCONV_SRCMASK) == IRT_NUM && irt_isguard(fleft->t))

1098. return fleft->op1;  /* f(g(x)) ==> x */

1099. return NEXTFOLD;

1100. }

1102. LJFOLD(CONV CONV IRCONV_INT_NUM)  /* _INT */

1103. LJFOLD(CONV CONV IRCONV_U32_NUM)  /* _U32*/

1104. ‌LJFOLDF(simplify_conv_int_num)

1105. {

1106. /* Fold even across PHI to avoid expensive num->int conversions in loop. */

1107. if ((fleft->op2 & IRCONV_SRCMASK) ==

1108. ((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH))

1109. return fleft->op1;

1110. return NEXTFOLD;

1111. }

1113. LJFOLD(CONV CONV IRCONV_I64_NUM)  /* _INT or _U32 */

1114. LJFOLD(CONV CONV IRCONV_U64_NUM)  /* _INT or _U32 */

1115. ‌LJFOLDF(simplify_conv_i64_num)

1116. {

1117. PHIBARRIER(fleft);

1118. if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT) {

1119. /* Reduce to a sign-extension. */

1120. fins->op1 = fleft->op1;

1121. fins->op2 = ((IRT_I64<<5)|IRT_INT|IRCONV_SEXT);

1122. return RETRYFOLD;

1123. } else if ((fleft->op2 & IRCONV_SRCMASK) == IRT_U32) {

1124. #if LJ_TARGET_X64

1125. return fleft->op1;

1126. #else

1127. /* Reduce to a zero-extension. */

1128. fins->op1 = fleft->op1;

1129. fins->op2 = (IRT_I64<<5)|IRT_U32;

1130. return RETRYFOLD;

1131. #endif

1132. }

1133. return NEXTFOLD;

1134. }

1136. LJFOLD(CONV CONV IRCONV_INT_I64)  /* _INT or _U32 */

1137. LJFOLD(CONV CONV IRCONV_INT_U64)  /* _INT or _U32 */

1138. LJFOLD(CONV CONV IRCONV_U32_I64)  /* _INT or _U32 */

1139. LJFOLD(CONV CONV IRCONV_U32_U64)  /* _INT or _U32 */

1140. ‌LJFOLDF(simplify_conv_int_i64)

1141. {

1142. int src;

1143. PHIBARRIER(fleft);

1144. src = (fleft->op2 & IRCONV_SRCMASK);

1145. if (src == IRT_INT || src == IRT_U32) {

1146. if (src == ((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH)) {

1147. return fleft->op1;

1148. } else {

1149. fins->op2 = ((fins->op2 & IRCONV_DSTMASK) | src);

1150. fins->op1 = fleft->op1;

1151. return RETRYFOLD;

1152. }

1153. }

1154. return NEXTFOLD;

1155. }

1157. LJFOLD(CONV CONV IRCONV_FLOAT_NUM)  /* _FLOAT */

1158. ‌LJFOLDF(simplify_conv_flt_num)

1159. {

1160. PHIBARRIER(fleft);

1161. if ((fleft->op2 & IRCONV_SRCMASK) == IRT_FLOAT)

1162. return fleft->op1;

1163. return NEXTFOLD;

1164. }

1166. /* Shortcut TOBIT + IRT_NUM <- IRT_INT/IRT_U32 conversion. */

1167. LJFOLD(TOBIT CONV KNUM)

1168. ‌LJFOLDF(simplify_tobit_conv)

1169. {

1170. /* Fold even across PHI to avoid expensive num->int conversions in loop. */

1171. if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT) {

1172. lua_assert(irt_isnum(fleft->t));

1173. return fleft->op1;

1174. } else if ((fleft->op2 & IRCONV_SRCMASK) == IRT_U32) {

1175. lua_assert(irt_isnum(fleft->t));

1176. fins->o = IR_CONV;

1177. fins->op1 = fleft->op1;

1178. fins->op2 = (IRT_INT<<5)|IRT_U32;

1179. return RETRYFOLD;

1180. }

1181. return NEXTFOLD;

1182. }

1184. /* Shortcut floor/ceil/round + IRT_NUM <- IRT_INT/IRT_U32 conversion. */

1185. LJFOLD(FPMATH CONV IRFPM_FLOOR)

1186. LJFOLD(FPMATH CONV IRFPM_CEIL)

1187. LJFOLD(FPMATH CONV IRFPM_TRUNC)

1188. ‌LJFOLDF(simplify_floor_conv)

1189. {

1190. if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT ||

1191. (fleft->op2 & IRCONV_SRCMASK) == IRT_U32)

1192. return LEFTFOLD;

1193. return NEXTFOLD;

1194. }

1196. /* Strength reduction of widening. */

1197. LJFOLD(CONV any IRCONV_I64_INT)

1198. LJFOLD(CONV any IRCONV_U64_INT)

1199. ‌LJFOLDF(simplify_conv_sext)

1200. {

1201. IRRef ref = fins->op1;

1202. int64_t ofs = 0;

1203. if (!(fins->op2 & IRCONV_SEXT))

1204. return NEXTFOLD;

1205. PHIBARRIER(fleft);

1206. if (fleft->o == IR_XLOAD && (irt_isu8(fleft->t) || irt_isu16(fleft->t)))

1207. goto ok_reduce;

1208. if (fleft->o == IR_ADD && irref_isk(fleft->op2)) {

1209. ofs = (int64_t)IR(fleft->op2)->i;

1210. ref = fleft->op1;

1211. }

1212. /* Use scalar evolution analysis results to strength-reduce sign-extension. */

1213. if (ref == J->scev.idx) {

1214. IRRef lo = J->scev.dir ? J->scev.start : J->scev.stop;

1215. lua_assert(irt_isint(J->scev.t));

1216. if (lo && IR(lo)->i + ofs >= 0) {

1217. ok_reduce:

1218. #if LJ_TARGET_X64

1219. /* Eliminate widening. All 32 bit ops do an implicit zero-extension. */

1220. return LEFTFOLD;

1221. #else

1222. /* Reduce to a (cheaper) zero-extension. */

1223. fins->op2 &= ~IRCONV_SEXT;

1224. return RETRYFOLD;

1225. #endif

1226. }

1227. }

1228. return NEXTFOLD;

1229. }

1231. /* Strength reduction of narrowing. */

1232. LJFOLD(CONV ADD IRCONV_INT_I64)

1233. LJFOLD(CONV SUB IRCONV_INT_I64)

1234. LJFOLD(CONV MUL IRCONV_INT_I64)

1235. LJFOLD(CONV ADD IRCONV_INT_U64)

1236. LJFOLD(CONV SUB IRCONV_INT_U64)

1237. LJFOLD(CONV MUL IRCONV_INT_U64)

1238. LJFOLD(CONV ADD IRCONV_U32_I64)

1239. LJFOLD(CONV SUB IRCONV_U32_I64)

1240. LJFOLD(CONV MUL IRCONV_U32_I64)

1241. LJFOLD(CONV ADD IRCONV_U32_U64)

1242. LJFOLD(CONV SUB IRCONV_U32_U64)

1243. LJFOLD(CONV MUL IRCONV_U32_U64)

1244. ‌LJFOLDF(simplify_conv_narrow)

1245. {

1246. IROp op = (IROp)fleft->o;

1247. IRType t = irt_type(fins->t);

1248. IRRef op1 = fleft->op1, op2 = fleft->op2, mode = fins->op2;

1249. PHIBARRIER(fleft);

1250. op1 = emitir(IRTI(IR_CONV), op1, mode);

1251. op2 = emitir(IRTI(IR_CONV), op2, mode);

1252. fins->ot = IRT(op, t);

1253. fins->op1 = op1;

1254. fins->op2 = op2;

1255. return RETRYFOLD;

1256. }

1258. /* Special CSE rule for CONV. */

1259. LJFOLD(CONV any any)

1260. ‌LJFOLDF(cse_conv)

1261. {

1262. if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) {

1263. IRRef op1 = fins->op1, op2 = (fins->op2 & IRCONV_MODEMASK);

1264. uint8_t guard = irt_isguard(fins->t);

1265. IRRef ref = J->chain[IR_CONV];

1266. while (ref > op1) {

1267. IRIns *ir = IR(ref);

1268. /* Commoning with stronger checks is ok. */

1269. if (ir->op1 == op1 && (ir->op2 & IRCONV_MODEMASK) == op2 &&

1270. irt_isguard(ir->t) >= guard)

1271. return ref;

1272. ref = ir->prev;

1273. }

1274. }

1275. return EMITFOLD;  /* No fallthrough to regular CSE. */

1276. }

1278. /* FP conversion narrowing. */

1279. LJFOLD(TOBIT ADD KNUM)

1280. LJFOLD(TOBIT SUB KNUM)

1281. LJFOLD(CONV ADD IRCONV_INT_NUM)

1282. LJFOLD(CONV SUB IRCONV_INT_NUM)

1283. LJFOLD(CONV ADD IRCONV_I64_NUM)

1284. LJFOLD(CONV SUB IRCONV_I64_NUM)

1285. ‌LJFOLDF(narrow_convert)

1286. {

1287. PHIBARRIER(fleft);

1288. /* Narrowing ignores PHIs and repeating it inside the loop is not useful. */

1289. if (J->chain[IR_LOOP])

1290. return NEXTFOLD;

1291. lua_assert(fins->o != IR_CONV || (fins->op2&IRCONV_CONVMASK) != IRCONV_TOBIT);

1292. return lj_opt_narrow_convert(J);

1293. }

1295. /* -- Integer algebraic simplifications ----------------------------------- */

1297. LJFOLD(ADD any KINT)

1298. LJFOLD(ADDOV any KINT)

1299. LJFOLD(SUBOV any KINT)

1300. ‌LJFOLDF(simplify_intadd_k)

1301. {

1302. if (fright->i == 0)  /* i o 0 ==> i */

1303. return LEFTFOLD;

1304. return NEXTFOLD;

1305. }

1307. LJFOLD(MULOV any KINT)

1308. ‌LJFOLDF(simplify_intmul_k)

1309. {

1310. if (fright->i == 0)  /* i * 0 ==> 0 */

1311. return RIGHTFOLD;

1312. if (fright->i == 1)  /* i * 1 ==> i */

1313. return LEFTFOLD;

1314. if (fright->i == 2) {  /* i * 2 ==> i + i */

1315. fins->o = IR_ADDOV;

1316. fins->op2 = fins->op1;

1317. return RETRYFOLD;

1318. }

1319. return NEXTFOLD;

1320. }

1322. LJFOLD(SUB any KINT)

1323. ‌LJFOLDF(simplify_intsub_k)

1324. {

1325. if (fright->i == 0)  /* i - 0 ==> i */

1326. return LEFTFOLD;

1327. fins->o = IR_ADD;  /* i - k ==> i + (-k) */

1328. fins->op2 = (IRRef1)lj_ir_kint(J, -fright->i);  /* Overflow for -2^31 ok. */

1329. return RETRYFOLD;

1330. }

1332. LJFOLD(SUB KINT any)

1333. LJFOLD(SUB KINT64 any)

1334. ‌LJFOLDF(simplify_intsub_kleft)

1335. {

1336. if (fleft->o == IR_KINT ? (fleft->i == 0) : (ir_kint64(fleft)->u64 == 0)) {

1337. fins->o = IR_NEG;  /* 0 - i ==> -i */

1338. fins->op1 = fins->op2;

1339. return RETRYFOLD;

1340. }

1341. return NEXTFOLD;

1342. }

1344. LJFOLD(ADD any KINT64)

1345. ‌LJFOLDF(simplify_intadd_k64)

1346. {

1347. if (ir_kint64(fright)->u64 == 0)  /* i + 0 ==> i */

1348. return LEFTFOLD;

1349. return NEXTFOLD;

1350. }

1352. LJFOLD(SUB any KINT64)

1353. ‌LJFOLDF(simplify_intsub_k64)

1354. {

1355. uint64_t k = ir_kint64(fright)->u64;

1356. if (k == 0)  /* i - 0 ==> i */

1357. return LEFTFOLD;

1358. fins->o = IR_ADD;  /* i - k ==> i + (-k) */

1359. fins->op2 = (IRRef1)lj_ir_kint64(J, (uint64_t)-(int64_t)k);

1360. return RETRYFOLD;

1361. }

1363. ‌static TRef simplify_intmul_k(jit_State *J, int32_t k)

1364. {

1365. /* Note: many more simplifications are possible, e.g. 2^k1 +- 2^k2.

1366. ** But this is mainly intended for simple address arithmetic.

1367. ** Also it's easier for the backend to optimize the original multiplies.

1368. */

1369. if (k == 0) {  /* i * 0 ==> 0 */

1370. return RIGHTFOLD;

1371. } else if (k == 1) {  /* i * 1 ==> i */

1372. return LEFTFOLD;

1373. } else if ((k & (k-1)) == 0) {  /* i * 2^k ==> i << k */

1374. fins->o = IR_BSHL;

1375. fins->op2 = lj_ir_kint(J, lj_fls((uint32_t)k));

1376. return RETRYFOLD;

1377. }

1378. return NEXTFOLD;

1379. }

1381. LJFOLD(MUL any KINT)

1382. ‌LJFOLDF(simplify_intmul_k32)

1383. {

1384. if (fright->i >= 0)

1385. return simplify_intmul_k(J, fright->i);

1386. return NEXTFOLD;

1387. }

1389. LJFOLD(MUL any KINT64)

1390. ‌LJFOLDF(simplify_intmul_k64)

1391. {

1392. #if LJ_HASFFI

1393. if (ir_kint64(fright)->u64 < 0x80000000u)

1394. return simplify_intmul_k(J, (int32_t)ir_kint64(fright)->u64);

1395. return NEXTFOLD;

1396. #else

1397. UNUSED(J); lua_assert(0); return FAILFOLD;

1398. #endif

1399. }

1401. LJFOLD(MOD any KINT)

1402. ‌LJFOLDF(simplify_intmod_k)

1403. {

1404. int32_t k = fright->i;

1405. lua_assert(k != 0);

1406. if (k > 0 && (k & (k-1)) == 0) {  /* i % (2^k) ==> i & (2^k-1) */

1407. fins->o = IR_BAND;

1408. fins->op2 = lj_ir_kint(J, k-1);

1409. return RETRYFOLD;

1410. }

1411. return NEXTFOLD;

1412. }

1414. LJFOLD(MOD KINT any)

1415. ‌LJFOLDF(simplify_intmod_kleft)

1416. {

1417. if (fleft->i == 0)

1418. return INTFOLD(0);

1419. return NEXTFOLD;

1420. }

1422. LJFOLD(SUB any any)

1423. LJFOLD(SUBOV any any)

1424. ‌LJFOLDF(simplify_intsub)

1425. {

1426. if (fins->op1 == fins->op2 && !irt_isnum(fins->t))  /* i - i ==> 0 */

1427. return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0);

1428. return NEXTFOLD;

1429. }

1431. LJFOLD(SUB ADD any)

1432. ‌LJFOLDF(simplify_intsubadd_leftcancel)

1433. {

1434. if (!irt_isnum(fins->t)) {

1435. PHIBARRIER(fleft);

1436. if (fins->op2 == fleft->op1)  /* (i + j) - i ==> j */

1437. return fleft->op2;

1438. if (fins->op2 == fleft->op2)  /* (i + j) - j ==> i */

1439. return fleft->op1;

1440. }

1441. return NEXTFOLD;

1442. }

1444. LJFOLD(SUB SUB any)

1445. ‌LJFOLDF(simplify_intsubsub_leftcancel)

1446. {

1447. if (!irt_isnum(fins->t)) {

1448. PHIBARRIER(fleft);

1449. if (fins->op2 == fleft->op1) {  /* (i - j) - i ==> 0 - j */

1450. fins->op1 = (IRRef1)lj_ir_kint(J, 0);

1451. fins->op2 = fleft->op2;

1452. return RETRYFOLD;

1453. }

1454. }

1455. return NEXTFOLD;

1456. }

1458. LJFOLD(SUB any SUB)

1459. ‌LJFOLDF(simplify_intsubsub_rightcancel)

1460. {

1461. if (!irt_isnum(fins->t)) {

1462. PHIBARRIER(fright);

1463. if (fins->op1 == fright->op1)  /* i - (i - j) ==> j */

1464. return fright->op2;

1465. }

1466. return NEXTFOLD;

1467. }

1469. LJFOLD(SUB any ADD)

1470. ‌LJFOLDF(simplify_intsubadd_rightcancel)

1471. {

1472. if (!irt_isnum(fins->t)) {

1473. PHIBARRIER(fright);

1474. if (fins->op1 == fright->op1) {  /* i - (i + j) ==> 0 - j */

1475. fins->op2 = fright->op2;

1476. fins->op1 = (IRRef1)lj_ir_kint(J, 0);

1477. return RETRYFOLD;

1478. }

1479. if (fins->op1 == fright->op2) {  /* i - (j + i) ==> 0 - j */

1480. fins->op2 = fright->op1;

1481. fins->op1 = (IRRef1)lj_ir_kint(J, 0);

1482. return RETRYFOLD;

1483. }

1484. }

1485. return NEXTFOLD;

1486. }

1488. LJFOLD(SUB ADD ADD)

1489. ‌LJFOLDF(simplify_intsubaddadd_cancel)

1490. {

1491. if (!irt_isnum(fins->t)) {

1492. PHIBARRIER(fleft);

1493. PHIBARRIER(fright);

1494. if (fleft->op1 == fright->op1) {  /* (i + j1) - (i + j2) ==> j1 - j2 */

1495. fins->op1 = fleft->op2;

1496. fins->op2 = fright->op2;

1497. return RETRYFOLD;

1498. }

1499. if (fleft->op1 == fright->op2) {  /* (i + j1) - (j2 + i) ==> j1 - j2 */

1500. fins->op1 = fleft->op2;

1501. fins->op2 = fright->op1;

1502. return RETRYFOLD;

1503. }

1504. if (fleft->op2 == fright->op1) {  /* (j1 + i) - (i + j2) ==> j1 - j2 */

1505. fins->op1 = fleft->op1;

1506. fins->op2 = fright->op2;

1507. return RETRYFOLD;

1508. }

1509. if (fleft->op2 == fright->op2) {  /* (j1 + i) - (j2 + i) ==> j1 - j2 */

1510. fins->op1 = fleft->op1;

1511. fins->op2 = fright->op1;

1512. return RETRYFOLD;

1513. }

1514. }

1515. return NEXTFOLD;

1516. }

1518. LJFOLD(BAND any KINT)

1519. LJFOLD(BAND any KINT64)

1520. ‌LJFOLDF(simplify_band_k)

1521. {

1522. int64_t k = fright->o == IR_KINT ? (int64_t)fright->i :

1523. (int64_t)ir_k64(fright)->u64;

1524. if (k == 0)  /* i & 0 ==> 0 */

1525. return RIGHTFOLD;

1526. if (k == -1)  /* i & -1 ==> i */

1527. return LEFTFOLD;

1528. return NEXTFOLD;

1529. }

1531. LJFOLD(BOR any KINT)

1532. LJFOLD(BOR any KINT64)

1533. ‌LJFOLDF(simplify_bor_k)

1534. {

1535. int64_t k = fright->o == IR_KINT ? (int64_t)fright->i :

1536. (int64_t)ir_k64(fright)->u64;

1537. if (k == 0)  /* i | 0 ==> i */

1538. return LEFTFOLD;

1539. if (k == -1)  /* i | -1 ==> -1 */

1540. return RIGHTFOLD;

1541. return NEXTFOLD;

1542. }

1544. LJFOLD(BXOR any KINT)

1545. LJFOLD(BXOR any KINT64)

1546. ‌LJFOLDF(simplify_bxor_k)

1547. {

1548. int64_t k = fright->o == IR_KINT ? (int64_t)fright->i :

1549. (int64_t)ir_k64(fright)->u64;

1550. if (k == 0)  /* i xor 0 ==> i */

1551. return LEFTFOLD;

1552. if (k == -1) {  /* i xor -1 ==> ~i */

1553. fins->o = IR_BNOT;

1554. fins->op2 = 0;

1555. return RETRYFOLD;

1556. }

1557. return NEXTFOLD;

1558. }

1560. LJFOLD(BSHL any KINT)

1561. LJFOLD(BSHR any KINT)

1562. LJFOLD(BSAR any KINT)

1563. LJFOLD(BROL any KINT)

1564. LJFOLD(BROR any KINT)

1565. ‌LJFOLDF(simplify_shift_ik)

1566. {

1567. int32_t mask = irt_is64(fins->t) ? 63 : 31;

1568. int32_t k = (fright->i & mask);

1569. if (k == 0)  /* i o 0 ==> i */

1570. return LEFTFOLD;

1571. if (k == 1 && fins->o == IR_BSHL) {  /* i << 1 ==> i + i */

1572. fins->o = IR_ADD;

1573. fins->op2 = fins->op1;

1574. return RETRYFOLD;

1575. }

1576. if (k != fright->i) {  /* i o k ==> i o (k & mask) */

1577. fins->op2 = (IRRef1)lj_ir_kint(J, k);

1578. return RETRYFOLD;

1579. }

1580. #ifndef LJ_TARGET_UNIFYROT

1581. if (fins->o == IR_BROR) {  /* bror(i, k) ==> brol(i, (-k)&mask) */

1582. fins->o = IR_BROL;

1583. fins->op2 = (IRRef1)lj_ir_kint(J, (-k)&mask);

1584. return RETRYFOLD;

1585. }

1586. #endif

1587. return NEXTFOLD;

1588. }

1590. LJFOLD(BSHL any BAND)

1591. LJFOLD(BSHR any BAND)

1592. LJFOLD(BSAR any BAND)

1593. LJFOLD(BROL any BAND)

1594. LJFOLD(BROR any BAND)

1595. ‌LJFOLDF(simplify_shift_andk)

1596. {

1597. IRIns *irk = IR(fright->op2);

1598. PHIBARRIER(fright);

1599. if ((fins->o < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) &&

1600. irk->o == IR_KINT) {  /* i o (j & mask) ==> i o j */

1601. int32_t mask = irt_is64(fins->t) ? 63 : 31;

1602. int32_t k = irk->i & mask;

1603. if (k == mask) {

1604. fins->op2 = fright->op1;

1605. return RETRYFOLD;

1606. }

1607. }

1608. return NEXTFOLD;

1609. }

1611. LJFOLD(BSHL KINT any)

1612. LJFOLD(BSHR KINT any)

1613. LJFOLD(BSHL KINT64 any)

1614. LJFOLD(BSHR KINT64 any)

1615. ‌LJFOLDF(simplify_shift1_ki)

1616. {

1617. int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i :

1618. (int64_t)ir_k64(fleft)->u64;

1619. if (k == 0)  /* 0 o i ==> 0 */

1620. return LEFTFOLD;

1621. return NEXTFOLD;

1622. }

1624. LJFOLD(BSAR KINT any)

1625. LJFOLD(BROL KINT any)

1626. LJFOLD(BROR KINT any)

1627. LJFOLD(BSAR KINT64 any)

1628. LJFOLD(BROL KINT64 any)

1629. LJFOLD(BROR KINT64 any)

1630. ‌LJFOLDF(simplify_shift2_ki)

1631. {

1632. int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i :

1633. (int64_t)ir_k64(fleft)->u64;

1634. if (k == 0 || k == -1)  /* 0 o i ==> 0; -1 o i ==> -1 */

1635. return LEFTFOLD;

1636. return NEXTFOLD;

1637. }

1639. LJFOLD(BSHL BAND KINT)

1640. LJFOLD(BSHR BAND KINT)

1641. LJFOLD(BROL BAND KINT)

1642. LJFOLD(BROR BAND KINT)

1643. ‌LJFOLDF(simplify_shiftk_andk)

1644. {

1645. IRIns *irk = IR(fleft->op2);

1646. PHIBARRIER(fleft);

1647. if (irk->o == IR_KINT) {  /* (i & k1) o k2 ==> (i o k2) & (k1 o k2) */

1648. int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o);

1649. fins->op1 = fleft->op1;

1650. fins->op1 = (IRRef1)lj_opt_fold(J);

1651. fins->op2 = (IRRef1)lj_ir_kint(J, k);

1652. fins->ot = IRTI(IR_BAND);

1653. return RETRYFOLD;

1654. }

1655. return NEXTFOLD;

1656. }

1658. LJFOLD(BAND BSHL KINT)

1659. LJFOLD(BAND BSHR KINT)

1660. ‌LJFOLDF(simplify_andk_shiftk)

1661. {

1662. IRIns *irk = IR(fleft->op2);

1663. if (irk->o == IR_KINT &&

1664. kfold_intop(-1, irk->i, (IROp)fleft->o) == fright->i)

1665. return LEFTFOLD;  /* (i o k1) & k2 ==> i, if (-1 o k1) == k2 */

1666. return NEXTFOLD;

1667. }

1669. /* -- Reassociation ------------------------------------------------------- */

1671. LJFOLD(ADD ADD KINT)

1672. LJFOLD(MUL MUL KINT)

1673. LJFOLD(BAND BAND KINT)

1674. LJFOLD(BOR BOR KINT)

1675. LJFOLD(BXOR BXOR KINT)

1676. ‌LJFOLDF(reassoc_intarith_k)

1677. {

1678. IRIns *irk = IR(fleft->op2);

1679. if (irk->o == IR_KINT) {

1680. int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o);

1681. if (k == irk->i)  /* (i o k1) o k2 ==> i o k1, if (k1 o k2) == k1. */

1682. return LEFTFOLD;

1683. PHIBARRIER(fleft);

1684. fins->op1 = fleft->op1;

1685. fins->op2 = (IRRef1)lj_ir_kint(J, k);

1686. return RETRYFOLD;  /* (i o k1) o k2 ==> i o (k1 o k2) */

1687. }

1688. return NEXTFOLD;

1689. }

1691. LJFOLD(ADD ADD KINT64)

1692. LJFOLD(MUL MUL KINT64)

1693. LJFOLD(BAND BAND KINT64)

1694. LJFOLD(BOR BOR KINT64)

1695. LJFOLD(BXOR BXOR KINT64)

1696. ‌LJFOLDF(reassoc_intarith_k64)

1697. {

1698. #if LJ_HASFFI

1699. IRIns *irk = IR(fleft->op2);

1700. if (irk->o == IR_KINT64) {

1701. uint64_t k = kfold_int64arith(ir_k64(irk)->u64,

1702. ir_k64(fright)->u64, (IROp)fins->o);

1703. PHIBARRIER(fleft);

1704. fins->op1 = fleft->op1;

1705. fins->op2 = (IRRef1)lj_ir_kint64(J, k);

1706. return RETRYFOLD;  /* (i o k1) o k2 ==> i o (k1 o k2) */

1707. }

1708. return NEXTFOLD;

1709. #else

1710. UNUSED(J); lua_assert(0); return FAILFOLD;

1711. #endif

1712. }

1714. LJFOLD(MIN MIN any)

1715. LJFOLD(MAX MAX any)

1716. LJFOLD(BAND BAND any)

1717. LJFOLD(BOR BOR any)

1718. ‌LJFOLDF(reassoc_dup)

1719. {

1720. if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2)

1721. return LEFTFOLD;  /* (a o b) o a ==> a o b; (a o b) o b ==> a o b */

1722. return NEXTFOLD;

1723. }

1725. LJFOLD(BXOR BXOR any)

1726. ‌LJFOLDF(reassoc_bxor)

1727. {

1728. PHIBARRIER(fleft);

1729. if (fins->op2 == fleft->op1)  /* (a xor b) xor a ==> b */

1730. return fleft->op2;

1731. if (fins->op2 == fleft->op2)  /* (a xor b) xor b ==> a */

1732. return fleft->op1;

1733. return NEXTFOLD;

1734. }

1736. LJFOLD(BSHL BSHL KINT)

1737. LJFOLD(BSHR BSHR KINT)

1738. LJFOLD(BSAR BSAR KINT)

1739. LJFOLD(BROL BROL KINT)

1740. LJFOLD(BROR BROR KINT)

1741. ‌LJFOLDF(reassoc_shift)

1742. {

1743. IRIns *irk = IR(fleft->op2);

1744. PHIBARRIER(fleft);  /* The (shift any KINT) rule covers k2 == 0 and more. */

1745. if (irk->o == IR_KINT) {  /* (i o k1) o k2 ==> i o (k1 + k2) */

1746. int32_t mask = irt_is64(fins->t) ? 63 : 31;

1747. int32_t k = (irk->i & mask) + (fright->i & mask);

1748. if (k > mask) {  /* Combined shift too wide? */

1749. if (fins->o == IR_BSHL || fins->o == IR_BSHR)

1750. return mask == 31 ? INTFOLD(0) : INT64FOLD(0);

1751. else if (fins->o == IR_BSAR)

1752. k = mask;

1753. else

1754. k &= mask;

1755. }

1756. fins->op1 = fleft->op1;

1757. fins->op2 = (IRRef1)lj_ir_kint(J, k);

1758. return RETRYFOLD;

1759. }

1760. return NEXTFOLD;

1761. }

1763. LJFOLD(MIN MIN KNUM)

1764. LJFOLD(MAX MAX KNUM)

1765. LJFOLD(MIN MIN KINT)

1766. LJFOLD(MAX MAX KINT)

1767. ‌LJFOLDF(reassoc_minmax_k)

1768. {

1769. IRIns *irk = IR(fleft->op2);

1770. if (irk->o == IR_KNUM) {

1771. lua_Number a = ir_knum(irk)->n;

1772. lua_Number y = lj_vm_foldarith(a, knumright, fins->o - IR_ADD);

1773. if (a == y)  /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */

1774. return LEFTFOLD;

1775. PHIBARRIER(fleft);

1776. fins->op1 = fleft->op1;

1777. fins->op2 = (IRRef1)lj_ir_knum(J, y);

1778. return RETRYFOLD;  /* (x o k1) o k2 ==> x o (k1 o k2) */

1779. } else if (irk->o == IR_KINT) {

1780. int32_t a = irk->i;

1781. int32_t y = kfold_intop(a, fright->i, fins->o);

1782. if (a == y)  /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */

1783. return LEFTFOLD;

1784. PHIBARRIER(fleft);

1785. fins->op1 = fleft->op1;

1786. fins->op2 = (IRRef1)lj_ir_kint(J, y);

1787. return RETRYFOLD;  /* (x o k1) o k2 ==> x o (k1 o k2) */

1788. }

1789. return NEXTFOLD;

1790. }

1792. LJFOLD(MIN MAX any)

1793. LJFOLD(MAX MIN any)

1794. ‌LJFOLDF(reassoc_minmax_left)

1795. {

1796. if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2)

1797. return RIGHTFOLD;  /* (b o1 a) o2 b ==> b; (a o1 b) o2 b ==> b */

1798. return NEXTFOLD;

1799. }

1801. LJFOLD(MIN any MAX)

1802. LJFOLD(MAX any MIN)

1803. ‌LJFOLDF(reassoc_minmax_right)

1804. {

1805. if (fins->op1 == fright->op1 || fins->op1 == fright->op2)

1806. return LEFTFOLD;  /* a o2 (a o1 b) ==> a; a o2 (b o1 a) ==> a */

1807. return NEXTFOLD;

1808. }

1810. /* -- Array bounds check elimination -------------------------------------- */

1812. /* Eliminate ABC across PHIs to handle t[i-1] forwarding case.

1813. ** ABC(asize, (i+k)+(-k)) ==> ABC(asize, i), but only if it already exists.

1814. ** Could be generalized to (i+k1)+k2 ==> i+(k1+k2), but needs better disambig.

1815. */

1816. LJFOLD(ABC any ADD)

1817. ‌LJFOLDF(abc_fwd)

1818. {

1819. if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) {

1820. if (irref_isk(fright->op2)) {

1821. IRIns *add2 = IR(fright->op1);

1822. if (add2->o == IR_ADD && irref_isk(add2->op2) &&

1823. IR(fright->op2)->i == -IR(add2->op2)->i) {

1824. IRRef ref = J->chain[IR_ABC];

1825. IRRef lim = add2->op1;

1826. if (fins->op1 > lim) lim = fins->op1;

1827. while (ref > lim) {

1828. IRIns *ir = IR(ref);

1829. if (ir->op1 == fins->op1 && ir->op2 == add2->op1)

1830. return DROPFOLD;

1831. ref = ir->prev;

1832. }

1833. }

1834. }

1835. }

1836. return NEXTFOLD;

1837. }

1839. /* Eliminate ABC for constants.

1840. ** ABC(asize, k1), ABC(asize k2) ==> ABC(asize, max(k1, k2))

1841. ** Drop second ABC if k2 is lower. Otherwise patch first ABC with k2.

1842. */

1843. LJFOLD(ABC any KINT)

1844. ‌LJFOLDF(abc_k)

1845. {

1846. if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) {

1847. IRRef ref = J->chain[IR_ABC];

1848. IRRef asize = fins->op1;

1849. while (ref > asize) {

1850. IRIns *ir = IR(ref);

1851. if (ir->op1 == asize && irref_isk(ir->op2)) {

1852. int32_t k = IR(ir->op2)->i;

1853. if (fright->i > k)

1854. ir->op2 = fins->op2;

1855. return DROPFOLD;

1856. }

1857. ref = ir->prev;

1858. }

1859. return EMITFOLD;  /* Already performed CSE. */

1860. }

1861. return NEXTFOLD;

1862. }

1864. /* Eliminate invariant ABC inside loop. */

1865. LJFOLD(ABC any any)

1866. ‌LJFOLDF(abc_invar)

1867. {

1868. /* Invariant ABC marked as PTR. Drop if op1 is invariant, too. */

1869. if (!irt_isint(fins->t) && fins->op1 < J->chain[IR_LOOP] &&

1870. !irt_isphi(IR(fins->op1)->t))

1871. return DROPFOLD;

1872. return NEXTFOLD;

1873. }

1875. /* -- Commutativity ------------------------------------------------------- */

1877. /* The refs of commutative ops are canonicalized. Lower refs go to the right.

1878. ** Rationale behind this:

1879. ** - It (also) moves constants to the right.

1880. ** - It reduces the number of FOLD rules (e.g. (BOR any KINT) suffices).

1881. ** - It helps CSE to find more matches.

1882. ** - The assembler generates better code with constants at the right.

1883. */

1885. LJFOLD(ADD any any)

1886. LJFOLD(MUL any any)

1887. LJFOLD(ADDOV any any)

1888. LJFOLD(MULOV any any)

1889. ‌LJFOLDF(comm_swap)

1890. {

1891. if (fins->op1 < fins->op2) {  /* Move lower ref to the right. */

1892. IRRef1 tmp = fins->op1;

1893. fins->op1 = fins->op2;

1894. fins->op2 = tmp;

1895. return RETRYFOLD;

1896. }

1897. return NEXTFOLD;

1898. }

1900. LJFOLD(EQ any any)

1901. LJFOLD(NE any any)

1902. ‌LJFOLDF(comm_equal)

1903. {

1904. /* For non-numbers only: x == x ==> drop; x ~= x ==> fail */

1905. if (fins->op1 == fins->op2 && !irt_isnum(fins->t))

1906. return CONDFOLD(fins->o == IR_EQ);

1907. return fold_comm_swap(J);

1908. }

1910. LJFOLD(LT any any)

1911. LJFOLD(GE any any)

1912. LJFOLD(LE any any)

1913. LJFOLD(GT any any)

1914. LJFOLD(ULT any any)

1915. LJFOLD(UGE any any)

1916. LJFOLD(ULE any any)

1917. LJFOLD(UGT any any)

1918. ‌LJFOLDF(comm_comp)

1919. {

1920. /* For non-numbers only: x <=> x ==> drop; x <> x ==> fail */

1921. if (fins->op1 == fins->op2 && !irt_isnum(fins->t))

1922. return CONDFOLD((fins->o ^ (fins->o >> 1)) & 1);

1923. if (fins->op1 < fins->op2) {  /* Move lower ref to the right. */

1924. IRRef1 tmp = fins->op1;

1925. fins->op1 = fins->op2;

1926. fins->op2 = tmp;

1927. fins->o ^= 3; /* GT <-> LT, GE <-> LE, does not affect U */

1928. return RETRYFOLD;

1929. }

1930. return NEXTFOLD;

1931. }

1933. LJFOLD(BAND any any)

1934. LJFOLD(BOR any any)

1935. LJFOLD(MIN any any)

1936. LJFOLD(MAX any any)

1937. ‌LJFOLDF(comm_dup)

1938. {

1939. if (fins->op1 == fins->op2)  /* x o x ==> x */

1940. return LEFTFOLD;

1941. return fold_comm_swap(J);

1942. }

1944. LJFOLD(BXOR any any)

1945. ‌LJFOLDF(comm_bxor)

1946. {

1947. if (fins->op1 == fins->op2)  /* i xor i ==> 0 */

1948. return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0);

1949. return fold_comm_swap(J);

1950. }

1952. /* -- Simplification of compound expressions ------------------------------ */

1954. ‌static TRef kfold_xload(jit_State *J, IRIns *ir, const void *p)

1955. {

1956. int32_t k;

1957. switch (irt_type(ir->t)) {

1958. case IRT_NUM: return lj_ir_knum_u64(J, *(uint64_t *)p);

1959. case IRT_I8: k = (int32_t)*(int8_t *)p; break;

1960. case IRT_U8: k = (int32_t)*(uint8_t *)p; break;

1961. case IRT_I16: k = (int32_t)(int16_t)lj_getu16(p); break;

1962. case IRT_U16: k = (int32_t)(uint16_t)lj_getu16(p); break;

1963. case IRT_INT: case IRT_U32: k = (int32_t)lj_getu32(p); break;

1964. case IRT_I64: case IRT_U64: return lj_ir_kint64(J, *(uint64_t *)p);

1965. default: return 0;

1966. }

1967. return lj_ir_kint(J, k);

1968. }

1970. /* Turn: string.sub(str, a, b) == kstr

1971. ** into: string.byte(str, a) == string.byte(kstr, 1) etc.

1972. ** Note: this creates unaligned XLOADs on x86/x64.

1973. */

1974. LJFOLD(EQ SNEW KGC)

1975. LJFOLD(NE SNEW KGC)

1976. ‌LJFOLDF(merge_eqne_snew_kgc)

1977. {

1978. GCstr *kstr = ir_kstr(fright);

1979. int32_t len = (int32_t)kstr->len;

1980. lua_assert(irt_isstr(fins->t));

1982. #if LJ_TARGET_UNALIGNED

1983. ‌#define FOLD_SNEW_MAX_LEN        4  /* Handle string lengths 0, 1, 2, 3, 4. */

1984. ‌#define FOLD_SNEW_TYPE8                IRT_I8        /* Creates shorter immediates. */

1985. #else

1986. #define FOLD_SNEW_MAX_LEN        1  /* Handle string lengths 0 or 1. */

1987. #define FOLD_SNEW_TYPE8                IRT_U8  /* Prefer unsigned loads. */

1988. #endif

1990. PHIBARRIER(fleft);

1991. if (len <= FOLD_SNEW_MAX_LEN) {

1992. IROp op = (IROp)fins->o;

1993. IRRef strref = fleft->op1;

1994. if (IR(strref)->o != IR_STRREF)

1995. return NEXTFOLD;

1996. if (op == IR_EQ) {

1997. emitir(IRTGI(IR_EQ), fleft->op2, lj_ir_kint(J, len));

1998. /* Caveat: fins/fleft/fright is no longer valid after emitir. */

1999. } else {

2000. /* NE is not expanded since this would need an OR of two conds. */

2001. if (!irref_isk(fleft->op2))  /* Only handle the constant length case. */

2002. return NEXTFOLD;

2003. if (IR(fleft->op2)->i != len)

2004. return DROPFOLD;

2005. }

2006. if (len > 0) {

2007. /* A 4 byte load for length 3 is ok -- all strings have an extra NUL. */

2008. uint16_t ot = (uint16_t)(len == 1 ? IRT(IR_XLOAD, FOLD_SNEW_TYPE8) :

2009. len == 2 ? IRT(IR_XLOAD, IRT_U16) :

2010. IRTI(IR_XLOAD));

2011. TRef tmp = emitir(ot, strref,

2012. IRXLOAD_READONLY | (len > 1 ? IRXLOAD_UNALIGNED : 0));

2013. TRef val = kfold_xload(J, IR(tref_ref(tmp)), strdata(kstr));

2014. if (len == 3)

2015. tmp = emitir(IRTI(IR_BAND), tmp,

2016. lj_ir_kint(J, LJ_ENDIAN_SELECT(0x00ffffff, 0xffffff00)));

2017. fins->op1 = (IRRef1)tmp;

2018. fins->op2 = (IRRef1)val;

2019. fins->ot = (IROpT)IRTGI(op);

2020. return RETRYFOLD;

2021. } else {

2022. return DROPFOLD;

2023. }

2024. }

2025. return NEXTFOLD;

2026. }

2028. /* -- Loads --------------------------------------------------------------- */

2030. /* Loads cannot be folded or passed on to CSE in general.

2031. ** Alias analysis is needed to check for forwarding opportunities.

2032. **

2033. ** Caveat: *all* loads must be listed here or they end up at CSE!

2034. */

2036. LJFOLD(ALOAD any)

2037. ‌LJFOLDX(lj_opt_fwd_aload)

2039. /* From HREF fwd (see below). Must eliminate, not supported by fwd/backend. */

2040. LJFOLD(HLOAD KKPTR)

2041. LJFOLDF(kfold_hload_kkptr)

2042. {

2043. UNUSED(J);

2044. lua_assert(ir_kptr(fleft) == niltvg(J2G(J)));

2045. return TREF_NIL;

2046. }

2048. LJFOLD(HLOAD any)

2049. ‌LJFOLDX(lj_opt_fwd_hload)

2051. LJFOLD(ULOAD any)

2052. LJFOLDX(lj_opt_fwd_uload)

2054. LJFOLD(CALLL any IRCALL_lj_tab_len)

2055. LJFOLDX(lj_opt_fwd_tab_len)

2057. /* Upvalue refs are really loads, but there are no corresponding stores.

2058. ** So CSE is ok for them, except for UREFO across a GC step (see below).

2059. ** If the referenced function is const, its upvalue addresses are const, too.

2060. ** This can be used to improve CSE by looking for the same address,

2061. ** even if the upvalues originate from a different function.

2062. */

2063. LJFOLD(UREFO KGC any)

2064. LJFOLD(UREFC KGC any)

2065. LJFOLDF(cse_uref)

2066. {

2067. if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) {

2068. IRRef ref = J->chain[fins->o];

2069. GCfunc *fn = ir_kfunc(fleft);

2070. GCupval *uv = gco2uv(gcref(fn->l.uvptr[(fins->op2 >> 8)]));

2071. while (ref > 0) {

2072. IRIns *ir = IR(ref);

2073. if (irref_isk(ir->op1)) {

2074. GCfunc *fn2 = ir_kfunc(IR(ir->op1));

2075. if (gco2uv(gcref(fn2->l.uvptr[(ir->op2 >> 8)])) == uv) {

2076. if (fins->o == IR_UREFO && gcstep_barrier(J, ref))

2077. break;

2078. return ref;

2079. }

2080. }

2081. ref = ir->prev;

2082. }

2083. }

2084. return EMITFOLD;

2085. }

2087. LJFOLD(HREFK any any)

2088. ‌LJFOLDX(lj_opt_fwd_hrefk)

2090. LJFOLD(HREF TNEW any)

2091. LJFOLDF(fwd_href_tnew)

2092. {

2093. if (lj_opt_fwd_href_nokey(J))

2094. return lj_ir_kkptr(J, niltvg(J2G(J)));

2095. return NEXTFOLD;

2096. }

2098. LJFOLD(HREF TDUP KPRI)

2099. LJFOLD(HREF TDUP KGC)

2100. LJFOLD(HREF TDUP KNUM)

2101. ‌LJFOLDF(fwd_href_tdup)

2102. {

2103. TValue keyv;

2104. lj_ir_kvalue(J->L, &keyv, fright);

2105. if (lj_tab_get(J->L, ir_ktab(IR(fleft->op1)), &keyv) == niltvg(J2G(J)) &&

2106. lj_opt_fwd_href_nokey(J))

2107. return lj_ir_kkptr(J, niltvg(J2G(J)));

2108. return NEXTFOLD;

2109. }

2111. /* We can safely FOLD/CSE array/hash refs and field loads, since there

2112. ** are no corresponding stores. But we need to check for any NEWREF with

2113. ** an aliased table, as it may invalidate all of the pointers and fields.

2114. ** Only HREF needs the NEWREF check -- AREF and HREFK already depend on

2115. ** FLOADs. And NEWREF itself is treated like a store (see below).

2116. ** LREF is constant (per trace) since coroutine switches are not inlined.

2117. */

2118. LJFOLD(FLOAD TNEW IRFL_TAB_ASIZE)

2119. ‌LJFOLDF(fload_tab_tnew_asize)

2120. {

2121. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1))

2122. return INTFOLD(fleft->op1);

2123. return NEXTFOLD;

2124. }

2126. LJFOLD(FLOAD TNEW IRFL_TAB_HMASK)

2127. ‌LJFOLDF(fload_tab_tnew_hmask)

2128. {

2129. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1))

2130. return INTFOLD((1 << fleft->op2)-1);

2131. return NEXTFOLD;

2132. }

2134. LJFOLD(FLOAD TDUP IRFL_TAB_ASIZE)

2135. ‌LJFOLDF(fload_tab_tdup_asize)

2136. {

2137. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1))

2138. return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->asize);

2139. return NEXTFOLD;

2140. }

2142. LJFOLD(FLOAD TDUP IRFL_TAB_HMASK)

2143. ‌LJFOLDF(fload_tab_tdup_hmask)

2144. {

2145. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1))

2146. return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->hmask);

2147. return NEXTFOLD;

2148. }

2150. LJFOLD(HREF any any)

2151. LJFOLD(FLOAD any IRFL_TAB_ARRAY)

2152. LJFOLD(FLOAD any IRFL_TAB_NODE)

2153. LJFOLD(FLOAD any IRFL_TAB_ASIZE)

2154. LJFOLD(FLOAD any IRFL_TAB_HMASK)

2155. ‌LJFOLDF(fload_tab_ah)

2156. {

2157. TRef tr = lj_opt_cse(J);

2158. return lj_opt_fwd_tptr(J, tref_ref(tr)) ? tr : EMITFOLD;

2159. }

2161. /* Strings are immutable, so we can safely FOLD/CSE the related FLOAD. */

2162. LJFOLD(FLOAD KGC IRFL_STR_LEN)

2163. ‌LJFOLDF(fload_str_len_kgc)

2164. {

2165. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD))

2166. return INTFOLD((int32_t)ir_kstr(fleft)->len);

2167. return NEXTFOLD;

2168. }

2170. LJFOLD(FLOAD SNEW IRFL_STR_LEN)

2171. ‌LJFOLDF(fload_str_len_snew)

2172. {

2173. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) {

2174. PHIBARRIER(fleft);

2175. return fleft->op2;

2176. }

2177. return NEXTFOLD;

2178. }

2180. LJFOLD(FLOAD TOSTR IRFL_STR_LEN)

2181. ‌LJFOLDF(fload_str_len_tostr)

2182. {

2183. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && fleft->op2 == IRTOSTR_CHAR)

2184. return INTFOLD(1);

2185. return NEXTFOLD;

2186. }

2188. /* The C type ID of cdata objects is immutable. */

2189. LJFOLD(FLOAD KGC IRFL_CDATA_CTYPEID)

2190. ‌LJFOLDF(fload_cdata_typeid_kgc)

2191. {

2192. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD))

2193. return INTFOLD((int32_t)ir_kcdata(fleft)->ctypeid);

2194. return NEXTFOLD;

2195. }

2197. /* Get the contents of immutable cdata objects. */

2198. LJFOLD(FLOAD KGC IRFL_CDATA_PTR)

2199. LJFOLD(FLOAD KGC IRFL_CDATA_INT)

2200. LJFOLD(FLOAD KGC IRFL_CDATA_INT64)

2201. ‌LJFOLDF(fload_cdata_int64_kgc)

2202. {

2203. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) {

2204. void *p = cdataptr(ir_kcdata(fleft));

2205. if (irt_is64(fins->t))

2206. return INT64FOLD(*(uint64_t *)p);

2207. else

2208. return INTFOLD(*(int32_t *)p);

2209. }

2210. return NEXTFOLD;

2211. }

2213. LJFOLD(FLOAD CNEW IRFL_CDATA_CTYPEID)

2214. LJFOLD(FLOAD CNEWI IRFL_CDATA_CTYPEID)

2215. ‌LJFOLDF(fload_cdata_typeid_cnew)

2216. {

2217. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD))

2218. return fleft->op1;  /* No PHI barrier needed. CNEW/CNEWI op1 is const. */

2219. return NEXTFOLD;

2220. }

2222. /* Pointer, int and int64 cdata objects are immutable. */

2223. LJFOLD(FLOAD CNEWI IRFL_CDATA_PTR)

2224. LJFOLD(FLOAD CNEWI IRFL_CDATA_INT)

2225. LJFOLD(FLOAD CNEWI IRFL_CDATA_INT64)

2226. ‌LJFOLDF(fload_cdata_ptr_int64_cnew)

2227. {

2228. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD))

2229. return fleft->op2;  /* Fold even across PHI to avoid allocations. */

2230. return NEXTFOLD;

2231. }

2233. LJFOLD(FLOAD any IRFL_STR_LEN)

2234. LJFOLD(FLOAD any IRFL_FUNC_ENV)

2235. LJFOLD(FLOAD any IRFL_THREAD_ENV)

2236. LJFOLD(FLOAD any IRFL_CDATA_CTYPEID)

2237. LJFOLD(FLOAD any IRFL_CDATA_PTR)

2238. LJFOLD(FLOAD any IRFL_CDATA_INT)

2239. LJFOLD(FLOAD any IRFL_CDATA_INT64)

2240. LJFOLD(VLOAD any any)  /* Vararg loads have no corresponding stores. */

2241. ‌LJFOLDX(lj_opt_cse)

2243. /* All other field loads need alias analysis. */

2244. LJFOLD(FLOAD any any)

2245. LJFOLDX(lj_opt_fwd_fload)

2247. /* This is for LOOP only. Recording handles SLOADs internally. */

2248. LJFOLD(SLOAD any any)

2249. LJFOLDF(fwd_sload)

2250. {

2251. if ((fins->op2 & IRSLOAD_FRAME)) {

2252. TRef tr = lj_opt_cse(J);

2253. return tref_ref(tr) < J->chain[IR_RETF] ? EMITFOLD : tr;

2254. } else {

2255. lua_assert(J->slot[fins->op1] != 0);

2256. return J->slot[fins->op1];

2257. }

2258. }

2260. /* Only fold for KKPTR. The pointer _and_ the contents must be const. */

2261. LJFOLD(XLOAD KKPTR any)

2262. ‌LJFOLDF(xload_kptr)

2263. {

2264. TRef tr = kfold_xload(J, fins, ir_kptr(fleft));

2265. return tr ? tr : NEXTFOLD;

2266. }

2268. LJFOLD(XLOAD any any)

2269. ‌LJFOLDX(lj_opt_fwd_xload)

2271. /* -- Write barriers ------------------------------------------------------ */

2273. /* Write barriers are amenable to CSE, but not across any incremental

2274. ** GC steps.

2275. **

2276. ** The same logic applies to open upvalue references, because a stack

2277. ** may be resized during a GC step (not the current stack, but maybe that

2278. ** of a coroutine).

2279. */

2280. LJFOLD(TBAR any)

2281. LJFOLD(OBAR any any)

2282. LJFOLD(UREFO any any)

2283. LJFOLDF(barrier_tab)

2284. {

2285. TRef tr = lj_opt_cse(J);

2286. if (gcstep_barrier(J, tref_ref(tr)))  /* CSE across GC step? */

2287. return EMITFOLD;  /* Raw emit. Assumes fins is left intact by CSE. */

2288. return tr;

2289. }

2291. LJFOLD(TBAR TNEW)

2292. LJFOLD(TBAR TDUP)

2293. ‌LJFOLDF(barrier_tnew_tdup)

2294. {

2295. /* New tables are always white and never need a barrier. */

2296. if (fins->op1 < J->chain[IR_LOOP])  /* Except across a GC step. */

2297. return NEXTFOLD;

2298. return DROPFOLD;

2299. }

2301. /* -- Profiling ----------------------------------------------------------- */

2303. LJFOLD(PROF any any)

2304. ‌LJFOLDF(prof)

2305. {

2306. IRRef ref = J->chain[IR_PROF];

2307. if (ref+1 == J->cur.nins)  /* Drop neighbouring IR_PROF. */

2308. return ref;

2309. return EMITFOLD;

2310. }

2312. /* -- Stores and allocations ---------------------------------------------- */

2314. /* Stores and allocations cannot be folded or passed on to CSE in general.

2315. ** But some stores can be eliminated with dead-store elimination (DSE).

2316. **

2317. ** Caveat: *all* stores and allocs must be listed here or they end up at CSE!

2318. */

2320. LJFOLD(ASTORE any any)

2321. LJFOLD(HSTORE any any)

2322. ‌LJFOLDX(lj_opt_dse_ahstore)

2324. LJFOLD(USTORE any any)

2325. LJFOLDX(lj_opt_dse_ustore)

2327. LJFOLD(FSTORE any any)

2328. LJFOLDX(lj_opt_dse_fstore)

2330. LJFOLD(XSTORE any any)

2331. LJFOLDX(lj_opt_dse_xstore)

2333. LJFOLD(NEWREF any any)  /* Treated like a store. */

2334. LJFOLD(CALLA any any)

2335. LJFOLD(CALLL any any)  /* Safeguard fallback. */

2336. LJFOLD(CALLS any any)

2337. LJFOLD(CALLXS any any)

2338. LJFOLD(XBAR)

2339. LJFOLD(RETF any any)  /* Modifies BASE. */

2340. LJFOLD(TNEW any any)

2341. LJFOLD(TDUP any)

2342. LJFOLD(CNEW any any)

2343. LJFOLD(XSNEW any any)

2344. LJFOLD(BUFHDR any any)

2345. LJFOLDX(lj_ir_emit)

2347. /* ------------------------------------------------------------------------ */

2349. /* Every entry in the generated hash table is a 32 bit pattern:

2350. **

2351. ** xxxxxxxx iiiiiii lllllll rrrrrrrrrr

2352. **

2353. **   xxxxxxxx = 8 bit index into fold function table

2354. **    iiiiiii = 7 bit folded instruction opcode

2355. **    lllllll = 7 bit left instruction opcode

2356. ** rrrrrrrrrr = 8 bit right instruction opcode or 10 bits from literal field

2357. */

2359. #include "lj_folddef.h"

2361. /* ------------------------------------------------------------------------ */

2363. /* Fold IR instruction. */

2364. TRef LJ_FASTCALL lj_opt_fold(jit_State *J)

2365. {

2366. uint32_t key, any;

2367. IRRef ref;

2369. if (LJ_UNLIKELY((J->flags & JIT_F_OPT_MASK) != JIT_F_OPT_DEFAULT)) {

2370. lua_assert(((JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE|JIT_F_OPT_DSE) |

2371. JIT_F_OPT_DEFAULT) == JIT_F_OPT_DEFAULT);

2372. /* Folding disabled? Chain to CSE, but not for loads/stores/allocs. */

2373. if (!(J->flags & JIT_F_OPT_FOLD) && irm_kind(lj_ir_mode[fins->o]) == IRM_N)

2374. return lj_opt_cse(J);

2376. /* No FOLD, forwarding or CSE? Emit raw IR for loads, except for SLOAD. */

2377. if ((J->flags & (JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE)) !=

2378. (JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE) &&

2379. irm_kind(lj_ir_mode[fins->o]) == IRM_L && fins->o != IR_SLOAD)

2380. return lj_ir_emit(J);

2382. /* No FOLD or DSE? Emit raw IR for stores. */

2383. if ((J->flags & (JIT_F_OPT_FOLD|JIT_F_OPT_DSE)) !=

2384. (JIT_F_OPT_FOLD|JIT_F_OPT_DSE) &&

2385. irm_kind(lj_ir_mode[fins->o]) == IRM_S)

2386. return lj_ir_emit(J);

2387. }

2389. /* Fold engine start/retry point. */

2390. retry:

2391. /* Construct key from opcode and operand opcodes (unless literal/none). */

2392. key = ((uint32_t)fins->o << 17);

2393. if (fins->op1 >= J->cur.nk) {

2394. key += (uint32_t)IR(fins->op1)->o << 10;

2395. *fleft = *IR(fins->op1);

2396. }

2397. if (fins->op2 >= J->cur.nk) {

2398. key += (uint32_t)IR(fins->op2)->o;

2399. *fright = *IR(fins->op2);

2400. } else {

2401. key += (fins->op2 & 0x3ffu);  /* Literal mask. Must include IRCONV_*MASK. */

2402. }

2404. /* Check for a match in order from most specific to least specific. */

2405. any = 0;

2406. for (;;) {

2407. uint32_t k = key | (any & 0x1ffff);

2408. uint32_t h = fold_hashkey(k);

2409. uint32_t fh = fold_hash[h];  /* Lookup key in semi-perfect hash table. */

2410. if ((fh & 0xffffff) == k || (fh = fold_hash[h+1], (fh & 0xffffff) == k)) {

2411. ref = (IRRef)tref_ref(fold_func[fh >> 24](J));

2412. if (ref != NEXTFOLD)

2413. break;

2414. }

2415. if (any == 0xfffff)  /* Exhausted folding. Pass on to CSE. */

2416. return lj_opt_cse(J);

2417. any = (any | (any >> 10)) ^ 0xffc00;

2418. }

2420. /* Return value processing, ordered by frequency. */

2421. if (LJ_LIKELY(ref >= MAX_FOLD))

2422. return TREF(ref, irt_t(IR(ref)->t));

2423. if (ref == RETRYFOLD)

2424. goto retry;

2425. if (ref == KINTFOLD)

2426. return lj_ir_kint(J, fins->i);

2427. if (ref == FAILFOLD)

2428. lj_trace_err(J, LJ_TRERR_GFAIL);

2429. lua_assert(ref == DROPFOLD);

2430. return REF_DROP;

2431. }

2433. /* -- Common-Subexpression Elimination ------------------------------------ */

2435. /* CSE an IR instruction. This is very fast due to the skip-list chains. */

2436. ‌TRef LJ_FASTCALL lj_opt_cse(jit_State *J)

2437. {

2438. /* Avoid narrow to wide store-to-load forwarding stall */

2439. IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16);

2440. IROp op = fins->o;

2441. if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) {

2442. /* Limited search for same operands in per-opcode chain. */

2443. IRRef ref = J->chain[op];

2444. IRRef lim = fins->op1;

2445. if (fins->op2 > lim) lim = fins->op2;  /* Relies on lit < REF_BIAS. */

2446. while (ref > lim) {

2447. if (IR(ref)->op12 == op12)

2448. return TREF(ref, irt_t(IR(ref)->t));  /* Common subexpression found. */

2449. ref = IR(ref)->prev;

2450. }

2451. }

2452. /* Otherwise emit IR (inlined for speed). */

2453. {

2454. IRRef ref = lj_ir_nextins(J);

2455. IRIns *ir = IR(ref);

2456. ir->prev = J->chain[op];

2457. ir->op12 = op12;

2458. J->chain[op] = (IRRef1)ref;

2459. ir->o = fins->o;

2460. J->guardemit.irt |= fins->t.irt;

2461. return TREF(ref, irt_t((ir->t = fins->t)));

2462. }

2463. }

2465. /* CSE with explicit search limit. */

2466. ‌TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim)

2467. {

2468. IRRef ref = J->chain[fins->o];

2469. IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16);

2470. while (ref > lim) {

2471. if (IR(ref)->op12 == op12)

2472. return ref;

2473. ref = IR(ref)->prev;

2474. }

2475. return lj_ir_emit(J);

2476. }

2478. /* ------------------------------------------------------------------------ */

2480. ‌#undef IR

2481. ‌#undef fins

2482. ‌#undef fleft

2483. ‌#undef fright

2484. ‌#undef knumleft

2485. ‌#undef knumright

2486. ‌#undef emitir

2488. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_opt_narrow.c - luajit-2.0-src

Data types defined

- NarrowConv

- NarrowConv

- NarrowIns

Functions defined

- lj_opt_narrow_arith

- lj_opt_narrow_cindex

- lj_opt_narrow_convert

- lj_opt_narrow_forl

- lj_opt_narrow_index

- lj_opt_narrow_mod

- lj_opt_narrow_pow

- lj_opt_narrow_tobit

- lj_opt_narrow_toint

- lj_opt_narrow_unm

- narrow_bpc_get

- narrow_bpc_set

- narrow_conv_backprop

- narrow_conv_emit

- narrow_forl

- narrow_stripov

- narrow_stripov_backprop

- numisint

Macros defined

- IR

- IR

- LUA_CORE

- NARROWINS

- NARROW_MAX_BACKPROP

- NARROW_MAX_STACK

- emitir

- emitir

- emitir_raw

- emitir_raw

- fins

- fins

- lj_opt_narrow_c

- narrow_op

- narrow_ref

Source code

1. /*

2. ** NARROW: Narrowing of numbers to integers (double to int32_t).

3. ** STRIPOV: Stripping of overflow checks.

4. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

5. */

7. ‌#define lj_opt_narrow_c

8. ‌#define LUA_CORE

10. #include "lj_obj.h"

12. #if LJ_HASJIT

14. #include "lj_bc.h"

15. #include "lj_ir.h"

16. #include "lj_jit.h"

17. #include "lj_iropt.h"

18. #include "lj_trace.h"

19. #include "lj_vm.h"

20. #include "lj_strscan.h"

22. /* Rationale for narrowing optimizations:

23. **

24. ** Lua has only a single number type and this is a FP double by default.

25. ** Narrowing doubles to integers does not pay off for the interpreter on a

26. ** current-generation x86/x64 machine. Most FP operations need the same

27. ** amount of execution resources as their integer counterparts, except

28. ** with slightly longer latencies. Longer latencies are a non-issue for

29. ** the interpreter, since they are usually hidden by other overhead.

30. **

31. ** The total CPU execution bandwidth is the sum of the bandwidth of the FP

32. ** and the integer units, because they execute in parallel. The FP units

33. ** have an equal or higher bandwidth than the integer units. Not using

34. ** them means losing execution bandwidth. Moving work away from them to

35. ** the already quite busy integer units is a losing proposition.

36. **

37. ** The situation for JIT-compiled code is a bit different: the higher code

38. ** density makes the extra latencies much more visible. Tight loops expose

39. ** the latencies for updating the induction variables. Array indexing

40. ** requires narrowing conversions with high latencies and additional

41. ** guards (to check that the index is really an integer). And many common

42. ** optimizations only work on integers.

43. **

44. ** One solution would be speculative, eager narrowing of all number loads.

45. ** This causes many problems, like losing -0 or the need to resolve type

46. ** mismatches between traces. It also effectively forces the integer type

47. ** to have overflow-checking semantics. This impedes many basic

48. ** optimizations and requires adding overflow checks to all integer

49. ** arithmetic operations (whereas FP arithmetics can do without).

50. **

51. ** Always replacing an FP op with an integer op plus an overflow check is

52. ** counter-productive on a current-generation super-scalar CPU. Although

53. ** the overflow check branches are highly predictable, they will clog the

54. ** execution port for the branch unit and tie up reorder buffers. This is

55. ** turning a pure data-flow dependency into a different data-flow

56. ** dependency (with slightly lower latency) *plus* a control dependency.

57. ** In general, you don't want to do this since latencies due to data-flow

58. ** dependencies can be well hidden by out-of-order execution.

59. **

60. ** A better solution is to keep all numbers as FP values and only narrow

61. ** when it's beneficial to do so. LuaJIT uses predictive narrowing for

62. ** induction variables and demand-driven narrowing for index expressions,

63. ** integer arguments and bit operations. Additionally it can eliminate or

64. ** hoist most of the resulting overflow checks. Regular arithmetic

65. ** computations are never narrowed to integers.

66. **

67. ** The integer type in the IR has convenient wrap-around semantics and

68. ** ignores overflow. Extra operations have been added for

69. ** overflow-checking arithmetic (ADDOV/SUBOV) instead of an extra type.

70. ** Apart from reducing overall complexity of the compiler, this also

71. ** nicely solves the problem where you want to apply algebraic

72. ** simplifications to ADD, but not to ADDOV. And the x86/x64 assembler can

73. ** use lea instead of an add for integer ADD, but not for ADDOV (lea does

74. ** not affect the flags, but it helps to avoid register moves).

75. **

76. **

77. ** All of the above has to be reconsidered for architectures with slow FP

78. ** operations or without a hardware FPU. The dual-number mode of LuaJIT

79. ** addresses this issue. Arithmetic operations are performed on integers

80. ** as far as possible and overflow checks are added as needed.

81. **

82. ** This implies that narrowing for integer arguments and bit operations

83. ** should also strip overflow checks, e.g. replace ADDOV with ADD. The

84. ** original overflow guards are weak and can be eliminated by DCE, if

85. ** there's no other use.

86. **

87. ** A slight twist is that it's usually beneficial to use overflow-checked

88. ** integer arithmetics if all inputs are already integers. This is the only

89. ** change that affects the single-number mode, too.

90. */

92. /* Some local macros to save typing. Undef'd at the end. */

93. ‌#define IR(ref)                        (&J->cur.ir[(ref)])

94. ‌#define fins                        (&J->fold.ins)

96. /* Pass IR on to next optimization in chain (FOLD). */

97. ‌#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))

99. ‌#define emitir_raw(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J))

101. /* -- Elimination of narrowing type conversions --------------------------- */

103. /* Narrowing of index expressions and bit operations is demand-driven. The

104. ** trace recorder emits a narrowing type conversion (CONV.int.num or TOBIT)

105. ** in all of these cases (e.g. array indexing or string indexing). FOLD

106. ** already takes care of eliminating simple redundant conversions like

107. ** CONV.int.num(CONV.num.int(x)) ==> x.

108. **

109. ** But the surrounding code is FP-heavy and arithmetic operations are

110. ** performed on FP numbers (for the single-number mode). Consider a common

111. ** example such as 'x=t[i+1]', with 'i' already an integer (due to induction

112. ** variable narrowing). The index expression would be recorded as

113. **   CONV.int.num(ADD(CONV.num.int(i), 1))

114. ** which is clearly suboptimal.

115. **

116. ** One can do better by recursively backpropagating the narrowing type

117. ** conversion across FP arithmetic operations. This turns FP ops into

118. ** their corresponding integer counterparts. Depending on the semantics of

119. ** the conversion they also need to check for overflow. Currently only ADD

120. ** and SUB are supported.

121. **

122. ** The above example can be rewritten as

123. **   ADDOV(CONV.int.num(CONV.num.int(i)), 1)

124. ** and then into ADDOV(i, 1) after folding of the conversions. The original

125. ** FP ops remain in the IR and are eliminated by DCE since all references to

126. ** them are gone.

127. **

128. ** [In dual-number mode the trace recorder already emits ADDOV etc., but

129. ** this can be further reduced. See below.]

130. **

131. ** Special care has to be taken to avoid narrowing across an operation

132. ** which is potentially operating on non-integral operands. One obvious

133. ** case is when an expression contains a non-integral constant, but ends

134. ** up as an integer index at runtime (like t[x+1.5] with x=0.5).

135. **

136. ** Operations with two non-constant operands illustrate a similar problem

137. ** (like t[a+b] with a=1.5 and b=2.5). Backpropagation has to stop there,

138. ** unless it can be proven that either operand is integral (e.g. by CSEing

139. ** a previous conversion). As a not-so-obvious corollary this logic also

140. ** applies for a whole expression tree (e.g. t[(a+1)+(b+1)]).

141. **

142. ** Correctness of the transformation is guaranteed by avoiding to expand

143. ** the tree by adding more conversions than the one we would need to emit

144. ** if not backpropagating. TOBIT employs a more optimistic rule, because

145. ** the conversion has special semantics, designed to make the life of the

146. ** compiler writer easier. ;-)

147. **

148. ** Using on-the-fly backpropagation of an expression tree doesn't work

149. ** because it's unknown whether the transform is correct until the end.

150. ** This either requires IR rollback and cache invalidation for every

151. ** subtree or a two-pass algorithm. The former didn't work out too well,

152. ** so the code now combines a recursive collector with a stack-based

153. ** emitter.

154. **

155. ** [A recursive backpropagation algorithm with backtracking, employing

156. ** skip-list lookup and round-robin caching, emitting stack operations

157. ** on-the-fly for a stack-based interpreter -- and all of that in a meager

158. ** kilobyte? Yep, compilers are a great treasure chest. Throw away your

159. ** textbooks and read the codebase of a compiler today!]

160. **

161. ** There's another optimization opportunity for array indexing: it's

162. ** always accompanied by an array bounds-check. The outermost overflow

163. ** check may be delegated to the ABC operation. This works because ABC is

164. ** an unsigned comparison and wrap-around due to overflow creates negative

165. ** numbers.

166. **

167. ** But this optimization is only valid for constants that cannot overflow

168. ** an int32_t into the range of valid array indexes [0..2^27+1). A check

169. ** for +-2^30 is safe since -2^31 - 2^30 wraps to 2^30 and 2^31-1 + 2^30

170. ** wraps to -2^30-1.

171. **

172. ** It's also good enough in practice, since e.g. t[i+1] or t[i-10] are

173. ** quite common. So the above example finally ends up as ADD(i, 1)!

174. **

175. ** Later on, the assembler is able to fuse the whole array reference and

176. ** the ADD into the memory operands of loads and other instructions. This

177. ** is why LuaJIT is able to generate very pretty (and fast) machine code

178. ** for array indexing. And that, my dear, concludes another story about

179. ** one of the hidden secrets of LuaJIT ...

180. */

182. /* Maximum backpropagation depth and maximum stack size. */

183. ‌#define NARROW_MAX_BACKPROP        100

184. ‌#define NARROW_MAX_STACK        256

186. /* The stack machine has a 32 bit instruction format: [IROpT | IRRef1]

187. ** The lower 16 bits hold a reference (or 0). The upper 16 bits hold

188. ** the IR opcode + type or one of the following special opcodes:

189. */

190. enum {

191. NARROW_REF,                /* Push ref. */

192. NARROW_CONV,                /* Push conversion of ref. */

193. NARROW_SEXT,                /* Push sign-extension of ref. */

194. NARROW_INT                /* Push KINT ref. The next code holds an int32_t. */

195. };

197. ‌typedef uint32_t NarrowIns;

199. ‌#define NARROWINS(op, ref)        (((op) << 16) + (ref))

200. ‌#define narrow_op(ins)                ((IROpT)((ins) >> 16))

201. ‌#define narrow_ref(ins)                ((IRRef1)(ins))

203. /* Context used for narrowing of type conversions. */

204. ‌typedef struct NarrowConv {

205. jit_State *J;                /* JIT compiler state. */

206. NarrowIns *sp;        /* Current stack pointer. */

207. NarrowIns *maxsp;        /* Maximum stack pointer minus redzone. */

208. int lim;                /* Limit on the number of emitted conversions. */

209. IRRef mode;                /* Conversion mode (IRCONV_*). */

210. IRType t;                /* Destination type: IRT_INT or IRT_I64. */

211. NarrowIns stack[NARROW_MAX_STACK];  /* Stack holding stack-machine code. */

212. ‌} NarrowConv;

214. /* Lookup a reference in the backpropagation cache. */

215. ‌static BPropEntry *narrow_bpc_get(jit_State *J, IRRef1 key, IRRef mode)

216. {

217. ptrdiff_t i;

218. for (i = 0; i < BPROP_SLOTS; i++) {

219. BPropEntry *bp = &J->bpropcache[i];

220. /* Stronger checks are ok, too. */

221. if (bp->key == key && bp->mode >= mode &&

222. ((bp->mode ^ mode) & IRCONV_MODEMASK) == 0)

223. return bp;

224. }

225. return NULL;

226. }

228. /* Add an entry to the backpropagation cache. */

229. ‌static void narrow_bpc_set(jit_State *J, IRRef1 key, IRRef1 val, IRRef mode)

230. {

231. uint32_t slot = J->bpropslot;

232. BPropEntry *bp = &J->bpropcache[slot];

233. J->bpropslot = (slot + 1) & (BPROP_SLOTS-1);

234. bp->key = key;

235. bp->val = val;

236. bp->mode = mode;

237. }

239. /* Backpropagate overflow stripping. */

240. ‌static void narrow_stripov_backprop(NarrowConv *nc, IRRef ref, int depth)

241. {

242. jit_State *J = nc->J;

243. IRIns *ir = IR(ref);

244. if (ir->o == IR_ADDOV || ir->o == IR_SUBOV ||

245. (ir->o == IR_MULOV && (nc->mode & IRCONV_CONVMASK) == IRCONV_ANY)) {

246. BPropEntry *bp = narrow_bpc_get(nc->J, ref, IRCONV_TOBIT);

247. if (bp) {

248. ref = bp->val;

249. } else if (++depth < NARROW_MAX_BACKPROP && nc->sp < nc->maxsp) {

250. narrow_stripov_backprop(nc, ir->op1, depth);

251. narrow_stripov_backprop(nc, ir->op2, depth);

252. *nc->sp++ = NARROWINS(IRT(ir->o - IR_ADDOV + IR_ADD, IRT_INT), ref);

253. return;

254. }

255. }

256. *nc->sp++ = NARROWINS(NARROW_REF, ref);

257. }

259. /* Backpropagate narrowing conversion. Return number of needed conversions. */

260. ‌static int narrow_conv_backprop(NarrowConv *nc, IRRef ref, int depth)

261. {

262. jit_State *J = nc->J;

263. IRIns *ir = IR(ref);

264. IRRef cref;

266. /* Check the easy cases first. */

267. if (ir->o == IR_CONV && (ir->op2 & IRCONV_SRCMASK) == IRT_INT) {

268. if ((nc->mode & IRCONV_CONVMASK) <= IRCONV_ANY)

269. narrow_stripov_backprop(nc, ir->op1, depth+1);

270. else

271. *nc->sp++ = NARROWINS(NARROW_REF, ir->op1);  /* Undo conversion. */

272. if (nc->t == IRT_I64)

273. *nc->sp++ = NARROWINS(NARROW_SEXT, 0);  /* Sign-extend integer. */

274. return 0;

275. } else if (ir->o == IR_KNUM) {  /* Narrow FP constant. */

276. lua_Number n = ir_knum(ir)->n;

277. if ((nc->mode & IRCONV_CONVMASK) == IRCONV_TOBIT) {

278. /* Allows a wider range of constants. */

279. int64_t k64 = (int64_t)n;

280. if (n == (lua_Number)k64) {  /* Only if const doesn't lose precision. */

281. *nc->sp++ = NARROWINS(NARROW_INT, 0);

282. *nc->sp++ = (NarrowIns)k64;  /* But always truncate to 32 bits. */

283. return 0;

284. }

285. } else {

286. int32_t k = lj_num2int(n);

287. /* Only if constant is a small integer. */

288. if (checki16(k) && n == (lua_Number)k) {

289. *nc->sp++ = NARROWINS(NARROW_INT, 0);

290. *nc->sp++ = (NarrowIns)k;

291. return 0;

292. }

293. }

294. return 10;  /* Never narrow other FP constants (this is rare). */

295. }

297. /* Try to CSE the conversion. Stronger checks are ok, too. */

298. cref = J->chain[fins->o];

299. while (cref > ref) {

300. IRIns *cr = IR(cref);

301. if (cr->op1 == ref &&

302. (fins->o == IR_TOBIT ||

303. ((cr->op2 & IRCONV_MODEMASK) == (nc->mode & IRCONV_MODEMASK) &&

304. irt_isguard(cr->t) >= irt_isguard(fins->t)))) {

305. *nc->sp++ = NARROWINS(NARROW_REF, cref);

306. return 0;  /* Already there, no additional conversion needed. */

307. }

308. cref = cr->prev;

309. }

311. /* Backpropagate across ADD/SUB. */

312. if (ir->o == IR_ADD || ir->o == IR_SUB) {

313. /* Try cache lookup first. */

314. IRRef mode = nc->mode;

315. BPropEntry *bp;

316. /* Inner conversions need a stronger check. */

317. if ((mode & IRCONV_CONVMASK) == IRCONV_INDEX && depth > 0)

318. mode += IRCONV_CHECK-IRCONV_INDEX;

319. bp = narrow_bpc_get(nc->J, (IRRef1)ref, mode);

320. if (bp) {

321. *nc->sp++ = NARROWINS(NARROW_REF, bp->val);

322. return 0;

323. } else if (nc->t == IRT_I64) {

324. /* Try sign-extending from an existing (checked) conversion to int. */

325. mode = (IRT_INT<<5)|IRT_NUM|IRCONV_INDEX;

326. bp = narrow_bpc_get(nc->J, (IRRef1)ref, mode);

327. if (bp) {

328. *nc->sp++ = NARROWINS(NARROW_REF, bp->val);

329. *nc->sp++ = NARROWINS(NARROW_SEXT, 0);

330. return 0;

331. }

332. }

333. if (++depth < NARROW_MAX_BACKPROP && nc->sp < nc->maxsp) {

334. NarrowIns *savesp = nc->sp;

335. int count = narrow_conv_backprop(nc, ir->op1, depth);

336. count += narrow_conv_backprop(nc, ir->op2, depth);

337. if (count <= nc->lim) {  /* Limit total number of conversions. */

338. *nc->sp++ = NARROWINS(IRT(ir->o, nc->t), ref);

339. return count;

340. }

341. nc->sp = savesp;  /* Too many conversions, need to backtrack. */

342. }

343. }

345. /* Otherwise add a conversion. */

346. *nc->sp++ = NARROWINS(NARROW_CONV, ref);

347. return 1;

348. }

350. /* Emit the conversions collected during backpropagation. */

351. ‌static IRRef narrow_conv_emit(jit_State *J, NarrowConv *nc)

352. {

353. /* The fins fields must be saved now -- emitir() overwrites them. */

354. IROpT guardot = irt_isguard(fins->t) ? IRTG(IR_ADDOV-IR_ADD, 0) : 0;

355. IROpT convot = fins->ot;

356. IRRef1 convop2 = fins->op2;

357. NarrowIns *next = nc->stack;  /* List of instructions from backpropagation. */

358. NarrowIns *last = nc->sp;

359. NarrowIns *sp = nc->stack;  /* Recycle the stack to store operands. */

360. while (next < last) {  /* Simple stack machine to process the ins. list. */

361. NarrowIns ref = *next++;

362. IROpT op = narrow_op(ref);

363. if (op == NARROW_REF) {

364. *sp++ = ref;

365. } else if (op == NARROW_CONV) {

366. *sp++ = emitir_raw(convot, ref, convop2);  /* Raw emit avoids a loop. */

367. } else if (op == NARROW_SEXT) {

368. lua_assert(sp >= nc->stack+1);

369. sp[-1] = emitir(IRT(IR_CONV, IRT_I64), sp[-1],

370. (IRT_I64<<5)|IRT_INT|IRCONV_SEXT);

371. } else if (op == NARROW_INT) {

372. lua_assert(next < last);

373. *sp++ = nc->t == IRT_I64 ?

374. lj_ir_kint64(J, (int64_t)(int32_t)*next++) :

375. lj_ir_kint(J, *next++);

376. } else {  /* Regular IROpT. Pops two operands and pushes one result. */

377. IRRef mode = nc->mode;

378. lua_assert(sp >= nc->stack+2);

379. sp--;

380. /* Omit some overflow checks for array indexing. See comments above. */

381. if ((mode & IRCONV_CONVMASK) == IRCONV_INDEX) {

382. if (next == last && irref_isk(narrow_ref(sp[0])) &&

383. (uint32_t)IR(narrow_ref(sp[0]))->i + 0x40000000u < 0x80000000u)

384. guardot = 0;

385. else  /* Otherwise cache a stronger check. */

386. mode += IRCONV_CHECK-IRCONV_INDEX;

387. }

388. sp[-1] = emitir(op+guardot, sp[-1], sp[0]);

389. /* Add to cache. */

390. if (narrow_ref(ref))

391. narrow_bpc_set(J, narrow_ref(ref), narrow_ref(sp[-1]), mode);

392. }

393. }

394. lua_assert(sp == nc->stack+1);

395. return nc->stack[0];

396. }

398. /* Narrow a type conversion of an arithmetic operation. */

399. ‌TRef LJ_FASTCALL lj_opt_narrow_convert(jit_State *J)

400. {

401. if ((J->flags & JIT_F_OPT_NARROW)) {

402. NarrowConv nc;

403. nc.J = J;

404. nc.sp = nc.stack;

405. nc.maxsp = &nc.stack[NARROW_MAX_STACK-4];

406. nc.t = irt_type(fins->t);

407. if (fins->o == IR_TOBIT) {

408. nc.mode = IRCONV_TOBIT;  /* Used only in the backpropagation cache. */

409. nc.lim = 2;  /* TOBIT can use a more optimistic rule. */

410. } else {

411. nc.mode = fins->op2;

412. nc.lim = 1;

413. }

414. if (narrow_conv_backprop(&nc, fins->op1, 0) <= nc.lim)

415. return narrow_conv_emit(J, &nc);

416. }

417. return NEXTFOLD;

418. }

420. /* -- Narrowing of implicit conversions ----------------------------------- */

422. /* Recursively strip overflow checks. */

423. ‌static TRef narrow_stripov(jit_State *J, TRef tr, int lastop, IRRef mode)

424. {

425. IRRef ref = tref_ref(tr);

426. IRIns *ir = IR(ref);

427. int op = ir->o;

428. if (op >= IR_ADDOV && op <= lastop) {

429. BPropEntry *bp = narrow_bpc_get(J, ref, mode);

430. if (bp) {

431. return TREF(bp->val, irt_t(IR(bp->val)->t));

432. } else {

433. IRRef op1 = ir->op1, op2 = ir->op2;  /* The IR may be reallocated. */

434. op1 = narrow_stripov(J, op1, lastop, mode);

435. op2 = narrow_stripov(J, op2, lastop, mode);

436. tr = emitir(IRT(op - IR_ADDOV + IR_ADD,

437. ((mode & IRCONV_DSTMASK) >> IRCONV_DSH)), op1, op2);

438. narrow_bpc_set(J, ref, tref_ref(tr), mode);

439. }

440. } else if (LJ_64 && (mode & IRCONV_SEXT) && !irt_is64(ir->t)) {

441. tr = emitir(IRT(IR_CONV, IRT_INTP), tr, mode);

442. }

443. return tr;

444. }

446. /* Narrow array index. */

447. ‌TRef LJ_FASTCALL lj_opt_narrow_index(jit_State *J, TRef tr)

448. {

449. IRIns *ir;

450. lua_assert(tref_isnumber(tr));

451. if (tref_isnum(tr))  /* Conversion may be narrowed, too. See above. */

452. return emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_INDEX);

453. /* Omit some overflow checks for array indexing. See comments above. */

454. ir = IR(tref_ref(tr));

455. if ((ir->o == IR_ADDOV || ir->o == IR_SUBOV) && irref_isk(ir->op2) &&

456. (uint32_t)IR(ir->op2)->i + 0x40000000u < 0x80000000u)

457. return emitir(IRTI(ir->o - IR_ADDOV + IR_ADD), ir->op1, ir->op2);

458. return tr;

459. }

461. /* Narrow conversion to integer operand (overflow undefined). */

462. ‌TRef LJ_FASTCALL lj_opt_narrow_toint(jit_State *J, TRef tr)

463. {

464. if (tref_isstr(tr))

465. tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);

466. if (tref_isnum(tr))  /* Conversion may be narrowed, too. See above. */

467. return emitir(IRTI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_ANY);

468. if (!tref_isinteger(tr))

469. lj_trace_err(J, LJ_TRERR_BADTYPE);

470. /*

471. ** Undefined overflow semantics allow stripping of ADDOV, SUBOV and MULOV.

472. ** Use IRCONV_TOBIT for the cache entries, since the semantics are the same.

473. */

474. return narrow_stripov(J, tr, IR_MULOV, (IRT_INT<<5)|IRT_INT|IRCONV_TOBIT);

475. }

477. /* Narrow conversion to bitop operand (overflow wrapped). */

478. ‌TRef LJ_FASTCALL lj_opt_narrow_tobit(jit_State *J, TRef tr)

479. {

480. if (tref_isstr(tr))

481. tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);

482. if (tref_isnum(tr))  /* Conversion may be narrowed, too. See above. */

483. return emitir(IRTI(IR_TOBIT), tr, lj_ir_knum_tobit(J));

484. if (!tref_isinteger(tr))

485. lj_trace_err(J, LJ_TRERR_BADTYPE);

486. /*

487. ** Wrapped overflow semantics allow stripping of ADDOV and SUBOV.

488. ** MULOV cannot be stripped due to precision widening.

489. */

490. return narrow_stripov(J, tr, IR_SUBOV, (IRT_INT<<5)|IRT_INT|IRCONV_TOBIT);

491. }

493. #if LJ_HASFFI

494. /* Narrow C array index (overflow undefined). */

495. ‌TRef LJ_FASTCALL lj_opt_narrow_cindex(jit_State *J, TRef tr)

496. {

497. lua_assert(tref_isnumber(tr));

498. if (tref_isnum(tr))

499. return emitir(IRT(IR_CONV, IRT_INTP), tr, (IRT_INTP<<5)|IRT_NUM|IRCONV_ANY);

500. /* Undefined overflow semantics allow stripping of ADDOV, SUBOV and MULOV. */

501. return narrow_stripov(J, tr, IR_MULOV,

502. LJ_64 ? ((IRT_INTP<<5)|IRT_INT|IRCONV_SEXT) :

503. ((IRT_INTP<<5)|IRT_INT|IRCONV_TOBIT));

504. }

505. #endif

507. /* -- Narrowing of arithmetic operators ----------------------------------- */

509. /* Check whether a number fits into an int32_t (-0 is ok, too). */

510. ‌static int numisint(lua_Number n)

511. {

512. return (n == (lua_Number)lj_num2int(n));

513. }

515. /* Narrowing of arithmetic operations. */

516. ‌TRef lj_opt_narrow_arith(jit_State *J, TRef rb, TRef rc,

517. TValue *vb, TValue *vc, IROp op)

518. {

519. if (tref_isstr(rb)) {

520. rb = emitir(IRTG(IR_STRTO, IRT_NUM), rb, 0);

521. lj_strscan_num(strV(vb), vb);

522. }

523. if (tref_isstr(rc)) {

524. rc = emitir(IRTG(IR_STRTO, IRT_NUM), rc, 0);

525. lj_strscan_num(strV(vc), vc);

526. }

527. /* Must not narrow MUL in non-DUALNUM variant, because it loses -0. */

528. if ((op >= IR_ADD && op <= (LJ_DUALNUM ? IR_MUL : IR_SUB)) &&

529. tref_isinteger(rb) && tref_isinteger(rc) &&

530. numisint(lj_vm_foldarith(numberVnum(vb), numberVnum(vc),

531. (int)op - (int)IR_ADD)))

532. return emitir(IRTGI((int)op - (int)IR_ADD + (int)IR_ADDOV), rb, rc);

533. if (!tref_isnum(rb)) rb = emitir(IRTN(IR_CONV), rb, IRCONV_NUM_INT);

534. if (!tref_isnum(rc)) rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT);

535. return emitir(IRTN(op), rb, rc);

536. }

538. /* Narrowing of unary minus operator. */

539. ‌TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc)

540. {

541. if (tref_isstr(rc)) {

542. rc = emitir(IRTG(IR_STRTO, IRT_NUM), rc, 0);

543. lj_strscan_num(strV(vc), vc);

544. }

545. if (tref_isinteger(rc)) {

546. if ((uint32_t)numberVint(vc) != 0x80000000u)

547. return emitir(IRTGI(IR_SUBOV), lj_ir_kint(J, 0), rc);

548. rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT);

549. }

550. return emitir(IRTN(IR_NEG), rc, lj_ir_knum_neg(J));

551. }

553. /* Narrowing of modulo operator. */

554. ‌TRef lj_opt_narrow_mod(jit_State *J, TRef rb, TRef rc, TValue *vc)

555. {

556. TRef tmp;

557. if (tvisstr(vc) && !lj_strscan_num(strV(vc), vc))

558. lj_trace_err(J, LJ_TRERR_BADTYPE);

559. if ((LJ_DUALNUM || (J->flags & JIT_F_OPT_NARROW)) &&

560. tref_isinteger(rb) && tref_isinteger(rc) &&

561. (tvisint(vc) ? intV(vc) != 0 : !tviszero(vc))) {

562. emitir(IRTGI(IR_NE), rc, lj_ir_kint(J, 0));

563. return emitir(IRTI(IR_MOD), rb, rc);

564. }

565. /* b % c ==> b - floor(b/c)*c */

566. rb = lj_ir_tonum(J, rb);

567. rc = lj_ir_tonum(J, rc);

568. tmp = emitir(IRTN(IR_DIV), rb, rc);

569. tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_FLOOR);

570. tmp = emitir(IRTN(IR_MUL), tmp, rc);

571. return emitir(IRTN(IR_SUB), rb, tmp);

572. }

574. /* Narrowing of power operator or math.pow. */

575. ‌TRef lj_opt_narrow_pow(jit_State *J, TRef rb, TRef rc, TValue *vc)

576. {

577. if (tvisstr(vc) && !lj_strscan_num(strV(vc), vc))

578. lj_trace_err(J, LJ_TRERR_BADTYPE);

579. /* Narrowing must be unconditional to preserve (-x)^i semantics. */

580. if (tvisint(vc) || numisint(numV(vc))) {

581. int checkrange = 0;

582. /* Split pow is faster for bigger exponents. But do this only for (+k)^i. */

583. if (tref_isk(rb) && (int32_t)ir_knum(IR(tref_ref(rb)))->u32.hi >= 0) {

584. int32_t k = numberVint(vc);

585. if (!(k >= -65536 && k <= 65536)) goto split_pow;

586. checkrange = 1;

587. }

588. if (!tref_isinteger(rc)) {

589. if (tref_isstr(rc))

590. rc = emitir(IRTG(IR_STRTO, IRT_NUM), rc, 0);

591. /* Guarded conversion to integer! */

592. rc = emitir(IRTGI(IR_CONV), rc, IRCONV_INT_NUM|IRCONV_CHECK);

593. }

594. if (checkrange && !tref_isk(rc)) {  /* Range guard: -65536 <= i <= 65536 */

595. TRef tmp = emitir(IRTI(IR_ADD), rc, lj_ir_kint(J, 65536));

596. emitir(IRTGI(IR_ULE), tmp, lj_ir_kint(J, 2*65536));

597. }

598. return emitir(IRTN(IR_POW), rb, rc);

599. }

600. split_pow:

601. /* FOLD covers most cases, but some are easier to do here. */

602. if (tref_isk(rb) && tvispone(ir_knum(IR(tref_ref(rb)))))

603. return rb;  /* 1 ^ x ==> 1 */

604. rc = lj_ir_tonum(J, rc);

605. if (tref_isk(rc) && ir_knum(IR(tref_ref(rc)))->n == 0.5)

606. return emitir(IRTN(IR_FPMATH), rb, IRFPM_SQRT);  /* x ^ 0.5 ==> sqrt(x) */

607. /* Split up b^c into exp2(c*log2(b)). Assembler may rejoin later. */

608. rb = emitir(IRTN(IR_FPMATH), rb, IRFPM_LOG2);

609. rc = emitir(IRTN(IR_MUL), rb, rc);

610. return emitir(IRTN(IR_FPMATH), rc, IRFPM_EXP2);

611. }

613. /* -- Predictive narrowing of induction variables ------------------------- */

615. /* Narrow a single runtime value. */

616. ‌static int narrow_forl(jit_State *J, cTValue *o)

617. {

618. if (tvisint(o)) return 1;

619. if (LJ_DUALNUM || (J->flags & JIT_F_OPT_NARROW)) return numisint(numV(o));

620. return 0;

621. }

623. /* Narrow the FORL index type by looking at the runtime values. */

624. ‌IRType lj_opt_narrow_forl(jit_State *J, cTValue *tv)

625. {

626. lua_assert(tvisnumber(&tv[FORL_IDX]) &&

627. tvisnumber(&tv[FORL_STOP]) &&

628. tvisnumber(&tv[FORL_STEP]));

629. /* Narrow only if the runtime values of start/stop/step are all integers. */

630. if (narrow_forl(J, &tv[FORL_IDX]) &&

631. narrow_forl(J, &tv[FORL_STOP]) &&

632. narrow_forl(J, &tv[FORL_STEP])) {

633. /* And if the loop index can't possibly overflow. */

634. lua_Number step = numberVnum(&tv[FORL_STEP]);

635. lua_Number sum = numberVnum(&tv[FORL_STOP]) + step;

636. if (0 <= step ? (sum <= 2147483647.0) : (sum >= -2147483648.0))

637. return IRT_INT;

638. }

639. return IRT_NUM;

640. }

642. ‌#undef IR

643. ‌#undef fins

644. ‌#undef emitir

645. ‌#undef emitir_raw

647. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_def.h - luajit-2.0-src

Data types defined

- BloomFilter

- Unaligned16

- Unaligned16

- Unaligned32

- Unaligned32

- int16_t

- int16_t

- int32_t

- int32_t

- int64_t

- int64_t

- int8_t

- int8_t

- intptr_t

- intptr_t

- intptr_t

- uint16_t

- uint16_t

- uint32_t

- uint32_t

- uint64_t

- uint64_t

- uint8_t

- uint8_t

- uintptr_t

- uintptr_t

- uintptr_t

Functions defined

- lj_bswap

- lj_bswap

- lj_bswap

- lj_bswap

- lj_bswap64

- lj_bswap64

- lj_bswap64

- lj_bswap64

- lj_bswap64

- lj_ffs

- lj_fls

- lj_fls

- lj_fls

- lj_getu16

- lj_getu16

- lj_getu32

- lj_getu32

Macros defined

- BLOOM_MASK

- LJ_AINLINE

- LJ_AINLINE

- LJ_ALIGN

- LJ_ALIGN

- LJ_ASMF

- LJ_ASMF_NORET

- LJ_ASSERT_NAME

- LJ_ASSERT_NAME2

- LJ_DATA

- LJ_DATADEF

- LJ_FASTCALL

- LJ_FASTCALL

- LJ_FASTCALL

- LJ_FUNC

- LJ_FUNC

- LJ_FUNCA

- LJ_FUNCA_NORET

- LJ_FUNC_NORET

- LJ_INLINE

- LJ_INLINE

- LJ_LIKELY

- LJ_LIKELY

- LJ_MAX_ABITS

- LJ_MAX_ALLOC

- LJ_MAX_ASIZE

- LJ_MAX_BCINS

- LJ_MAX_BUF

- LJ_MAX_COLOSIZE

- LJ_MAX_EXITSTUBGR

- LJ_MAX_HBITS

- LJ_MAX_IDXCHAIN

- LJ_MAX_JSLOTS

- LJ_MAX_LINE

- LJ_MAX_LOCVAR

- LJ_MAX_MEM

- LJ_MAX_MEM32

- LJ_MAX_MEM64

- LJ_MAX_PHI

- LJ_MAX_SLOTS

- LJ_MAX_STR

- LJ_MAX_STRTAB

- LJ_MAX_UDATA

- LJ_MAX_UPVAL

- LJ_MAX_XLEVEL

- LJ_MIN_GLOBAL

- LJ_MIN_IRSZ

- LJ_MIN_K64SZ

- LJ_MIN_REGISTRY

- LJ_MIN_SBUF

- LJ_MIN_STRTAB

- LJ_MIN_VECSZ

- LJ_NOAPI

- LJ_NOAPI

- LJ_NOINLINE

- LJ_NOINLINE

- LJ_NORET

- LJ_NORET

- LJ_NORET

- LJ_NUM_CBPAGE

- LJ_STACK_EXTRA

- LJ_STATIC_ASSERT

- LJ_STATIC_ASSERT

- LJ_UNLIKELY

- LJ_UNLIKELY

- U64x

- UNUSED

- _LJ_DEF_H

- api_check

- api_check

- bloombit

- bloomset

- bloomtest

- check_exp

- check_exp

- checki16

- checki32

- checki8

- checkptr32

- checkptr47

- checkptrGC

- checkptrGC

- checkptrGC

- checku16

- checku32

- checku8

- i32ptr

- lj_bswap

- lj_bswap64

- lj_ffs

- lj_fls

- lj_getu16

- lj_getu32

- lj_rol

- lj_ror

- lua_assert

- u32ptr

Source code

1. /*

2. ** LuaJIT common internal definitions.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_DEF_H

7. ‌#define _LJ_DEF_H

9. #include "lua.h"

11. #if defined(_MSC_VER)

12. /* MSVC is stuck in the last century and doesn't have C99's stdint.h. */

13. ‌typedef __int8 int8_t;

14. ‌typedef __int16 int16_t;

15. ‌typedef __int32 int32_t;

16. ‌typedef __int64 int64_t;

17. ‌typedef unsigned __int8 uint8_t;

18. ‌typedef unsigned __int16 uint16_t;

19. ‌typedef unsigned __int32 uint32_t;

20. ‌typedef unsigned __int64 uint64_t;

21. #ifdef _WIN64

22. ‌typedef __int64 intptr_t;

23. ‌typedef unsigned __int64 uintptr_t;

24. #else

25. ‌typedef __int32 intptr_t;

26. ‌typedef unsigned __int32 uintptr_t;

27. #endif

28. #elif defined(__symbian__)

29. /* Cough. */

30. ‌typedef signed char int8_t;

31. ‌typedef short int int16_t;

32. ‌typedef int int32_t;

33. ‌typedef long long int64_t;

34. ‌typedef unsigned char uint8_t;

35. ‌typedef unsigned short int uint16_t;

36. ‌typedef unsigned int uint32_t;

37. ‌typedef unsigned long long uint64_t;

38. ‌typedef int intptr_t;

39. ‌typedef unsigned int uintptr_t;

40. #else

41. #include <stdint.h>

42. #endif

44. /* Needed everywhere. */

45. #include <string.h>

46. #include <stdlib.h>

48. /* Various VM limits. */

49. ‌#define LJ_MAX_MEM32        0x7fffff00        /* Max. 32 bit memory allocation. */

50. ‌#define LJ_MAX_MEM64        ((uint64_t)1<<47)  /* Max. 64 bit memory allocation. */

51. /* Max. total memory allocation. */

52. ‌#define LJ_MAX_MEM        (LJ_GC64 ? LJ_MAX_MEM64 : LJ_MAX_MEM32)

53. ‌#define LJ_MAX_ALLOC        LJ_MAX_MEM        /* Max. individual allocation length. */

54. ‌#define LJ_MAX_STR        LJ_MAX_MEM32        /* Max. string length. */

55. ‌#define LJ_MAX_BUF        LJ_MAX_MEM32        /* Max. buffer length. */

56. ‌#define LJ_MAX_UDATA        LJ_MAX_MEM32        /* Max. userdata length. */

58. ‌#define LJ_MAX_STRTAB        (1<<26)                /* Max. string table size. */

59. ‌#define LJ_MAX_HBITS        26                /* Max. hash bits. */

60. ‌#define LJ_MAX_ABITS        28                /* Max. bits of array key. */

61. ‌#define LJ_MAX_ASIZE        ((1<<(LJ_MAX_ABITS-1))+1)  /* Max. array part size. */

62. ‌#define LJ_MAX_COLOSIZE        16                /* Max. elems for colocated array. */

64. ‌#define LJ_MAX_LINE        LJ_MAX_MEM32        /* Max. source code line number. */

65. ‌#define LJ_MAX_XLEVEL        200                /* Max. syntactic nesting level. */

66. ‌#define LJ_MAX_BCINS        (1<<26)                /* Max. # of bytecode instructions. */

67. ‌#define LJ_MAX_SLOTS        250                /* Max. # of slots in a Lua func. */

68. ‌#define LJ_MAX_LOCVAR        200                /* Max. # of local variables. */

69. ‌#define LJ_MAX_UPVAL        60                /* Max. # of upvalues. */

71. ‌#define LJ_MAX_IDXCHAIN        100                /* __index/__newindex chain limit. */

72. ‌#define LJ_STACK_EXTRA        (5+2*LJ_FR2)        /* Extra stack space (metamethods). */

74. ‌#define LJ_NUM_CBPAGE        1                /* Number of FFI callback pages. */

76. /* Minimum table/buffer sizes. */

77. ‌#define LJ_MIN_GLOBAL        6                /* Min. global table size (hbits). */

78. ‌#define LJ_MIN_REGISTRY        2                /* Min. registry size (hbits). */

79. ‌#define LJ_MIN_STRTAB        256                /* Min. string table size (pow2). */

80. ‌#define LJ_MIN_SBUF        32                /* Min. string buffer length. */

81. ‌#define LJ_MIN_VECSZ        8                /* Min. size for growable vectors. */

82. ‌#define LJ_MIN_IRSZ        32                /* Min. size for growable IR. */

83. ‌#define LJ_MIN_K64SZ        16                /* Min. size for chained K64Array. */

85. /* JIT compiler limits. */

86. ‌#define LJ_MAX_JSLOTS        250                /* Max. # of stack slots for a trace. */

87. ‌#define LJ_MAX_PHI        64                /* Max. # of PHIs for a loop. */

88. ‌#define LJ_MAX_EXITSTUBGR        16        /* Max. # of exit stub groups. */

90. /* Various macros. */

91. #ifndef UNUSED

92. ‌#define UNUSED(x)        ((void)(x))        /* to avoid warnings */

93. #endif

95. ‌#define U64x(hi, lo)        (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo)

96. ‌#define i32ptr(p)        ((int32_t)(intptr_t)(void *)(p))

97. ‌#define u32ptr(p)        ((uint32_t)(intptr_t)(void *)(p))

99. ‌#define checki8(x)        ((x) == (int32_t)(int8_t)(x))

100. ‌#define checku8(x)        ((x) == (int32_t)(uint8_t)(x))

101. ‌#define checki16(x)        ((x) == (int32_t)(int16_t)(x))

102. ‌#define checku16(x)        ((x) == (int32_t)(uint16_t)(x))

103. ‌#define checki32(x)        ((x) == (int32_t)(x))

104. ‌#define checku32(x)        ((x) == (uint32_t)(x))

105. ‌#define checkptr32(x)        ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x))

106. ‌#define checkptr47(x)        (((uint64_t)(x) >> 47) == 0)

107. #if LJ_GC64

108. ‌#define checkptrGC(x)        (checkptr47((x)))

109. #elif LJ_64

110. ‌#define checkptrGC(x)        (checkptr32((x)))

111. #else

112. ‌#define checkptrGC(x)        1

113. #endif

115. /* Every half-decent C compiler transforms this into a rotate instruction. */

116. ‌#define lj_rol(x, n)        (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1))))

117. ‌#define lj_ror(x, n)        (((x)<<(-(int)(n)&(8*sizeof(x)-1))) | ((x)>>(n)))

119. /* A really naive Bloom filter. But sufficient for our needs. */

120. ‌typedef uintptr_t BloomFilter;

121. ‌#define BLOOM_MASK        (8*sizeof(BloomFilter) - 1)

122. ‌#define bloombit(x)        ((uintptr_t)1 << ((x) & BLOOM_MASK))

123. ‌#define bloomset(b, x)        ((b) |= bloombit((x)))

124. ‌#define bloomtest(b, x)        ((b) & bloombit((x)))

126. #if defined(__GNUC__) || defined(__psp2__)

128. ‌#define LJ_NORET        __attribute__((noreturn))

129. ‌#define LJ_ALIGN(n)        __attribute__((aligned(n)))

130. ‌#define LJ_INLINE        inline

131. ‌#define LJ_AINLINE        inline __attribute__((always_inline))

132. ‌#define LJ_NOINLINE        __attribute__((noinline))

134. #if defined(__ELF__) || defined(__MACH__) || defined(__psp2__)

135. #if !((defined(__sun__) && defined(__svr4__)) || defined(__CELLOS_LV2__))

136. ‌#define LJ_NOAPI        extern __attribute__((visibility("hidden")))

137. #endif

138. #endif

140. /* Note: it's only beneficial to use fastcall on x86 and then only for up to

141. ** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only

142. ** indirect calls and related tail-called C functions are marked as fastcall.

143. */

144. #if defined(__i386__)

145. ‌#define LJ_FASTCALL        __attribute__((fastcall))

146. #endif

148. ‌#define LJ_LIKELY(x)        __builtin_expect(!!(x), 1)

149. ‌#define LJ_UNLIKELY(x)        __builtin_expect(!!(x), 0)

151. ‌#define lj_ffs(x)        ((uint32_t)__builtin_ctz(x))

152. /* Don't ask ... */

153. #if defined(__INTEL_COMPILER) && (defined(__i386__) || defined(__x86_64__))

154. ‌static LJ_AINLINE uint32_t lj_fls(uint32_t x)

155. {

156. uint32_t r; __asm__("bsrl %1, %0" : "=r" (r) : "rm" (x) : "cc"); return r;

157. }

158. #else

159. ‌#define lj_fls(x)        ((uint32_t)(__builtin_clz(x)^31))

160. #endif

162. #if defined(__arm__)

163. ‌static LJ_AINLINE uint32_t lj_bswap(uint32_t x)

164. {

165. #if defined(__psp2__)

166. return __builtin_rev(x);

167. #else

168. uint32_t r;

169. #if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\

170. __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__

171. __asm__("rev %0, %1" : "=r" (r) : "r" (x));

172. return r;

173. #else

174. #ifdef __thumb__

175. r = x ^ lj_ror(x, 16);

176. #else

177. __asm__("eor %0, %1, %1, ror #16" : "=r" (r) : "r" (x));

178. #endif

179. return ((r & 0xff00ffffu) >> 8) ^ lj_ror(x, 8);

180. #endif

181. #endif

182. }

184. ‌static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)

185. {

186. return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32));

187. }

188. #elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)

189. ‌static LJ_AINLINE uint32_t lj_bswap(uint32_t x)

190. {

191. return (uint32_t)__builtin_bswap32((int32_t)x);

192. }

194. ‌static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)

195. {

196. return (uint64_t)__builtin_bswap64((int64_t)x);

197. }

198. #elif defined(__i386__) || defined(__x86_64__)

199. ‌static LJ_AINLINE uint32_t lj_bswap(uint32_t x)

200. {

201. uint32_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r;

202. }

204. #if defined(__i386__)

205. ‌static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)

206. {

207. return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32));

208. }

209. #else

210. ‌static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)

211. {

212. uint64_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r;

213. }

214. #endif

215. #else

216. ‌static LJ_AINLINE uint32_t lj_bswap(uint32_t x)

217. {

218. return (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24);

219. }

221. ‌static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)

222. {

223. return (uint64_t)lj_bswap((uint32_t)(x >> 32)) |

224. ((uint64_t)lj_bswap((uint32_t)x) << 32);

225. }

226. #endif

228. ‌typedef union __attribute__((packed)) Unaligned16 {

229. uint16_t u;

230. uint8_t b[2];

231. ‌} Unaligned16;

233. ‌typedef union __attribute__((packed)) Unaligned32 {

234. uint32_t u;

235. uint8_t b[4];

236. ‌} Unaligned32;

238. /* Unaligned load of uint16_t. */

239. ‌static LJ_AINLINE uint16_t lj_getu16(const void *p)

240. {

241. return ((const Unaligned16 *)p)->u;

242. }

244. /* Unaligned load of uint32_t. */

245. ‌static LJ_AINLINE uint32_t lj_getu32(const void *p)

246. {

247. return ((const Unaligned32 *)p)->u;

248. }

250. #elif defined(_MSC_VER)

252. ‌#define LJ_NORET        __declspec(noreturn)

253. ‌#define LJ_ALIGN(n)        __declspec(align(n))

254. ‌#define LJ_INLINE        __inline

255. ‌#define LJ_AINLINE        __forceinline

256. ‌#define LJ_NOINLINE        __declspec(noinline)

257. #if defined(_M_IX86)

258. ‌#define LJ_FASTCALL        __fastcall

259. #endif

261. #ifdef _M_PPC

262. unsigned int _CountLeadingZeros(long);

263. #pragma intrinsic(_CountLeadingZeros)

264. ‌static LJ_AINLINE uint32_t lj_fls(uint32_t x)

265. {

266. return _CountLeadingZeros(x) ^ 31;

267. }

268. #else

269. unsigned char _BitScanForward(uint32_t *, unsigned long);

270. unsigned char _BitScanReverse(uint32_t *, unsigned long);

271. #pragma intrinsic(_BitScanForward)

272. #pragma intrinsic(_BitScanReverse)

274. ‌static LJ_AINLINE uint32_t lj_ffs(uint32_t x)

275. {

276. uint32_t r; _BitScanForward(&r, x); return r;

277. }

279. ‌static LJ_AINLINE uint32_t lj_fls(uint32_t x)

280. {

281. uint32_t r; _BitScanReverse(&r, x); return r;

282. }

283. #endif

285. unsigned long _byteswap_ulong(unsigned long);

286. uint64_t _byteswap_uint64(uint64_t);

287. ‌#define lj_bswap(x)        (_byteswap_ulong((x)))

288. ‌#define lj_bswap64(x)        (_byteswap_uint64((x)))

290. #if defined(_M_PPC) && defined(LUAJIT_NO_UNALIGNED)

291. /*

292. ** Replacement for unaligned loads on Xbox 360. Disabled by default since it's

293. ** usually more costly than the occasional stall when crossing a cache-line.

294. */

295. ‌static LJ_AINLINE uint16_t lj_getu16(const void *v)

296. {

297. const uint8_t *p = (const uint8_t *)v;

298. return (uint16_t)((p[0]<<8) | p[1]);

299. }

300. ‌static LJ_AINLINE uint32_t lj_getu32(const void *v)

301. {

302. const uint8_t *p = (const uint8_t *)v;

303. return (uint32_t)((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]);

304. }

305. #else

306. /* Unaligned loads are generally ok on x86/x64. */

307. ‌#define lj_getu16(p)        (*(uint16_t *)(p))

308. ‌#define lj_getu32(p)        (*(uint32_t *)(p))

309. #endif

311. #else

312. #error "missing defines for your compiler"

313. #endif

315. /* Optional defines. */

316. #ifndef LJ_FASTCALL

317. ‌#define LJ_FASTCALL

318. #endif

319. #ifndef LJ_NORET

320. ‌#define LJ_NORET

321. #endif

322. #ifndef LJ_NOAPI

323. ‌#define LJ_NOAPI        extern

324. #endif

325. #ifndef LJ_LIKELY

326. ‌#define LJ_LIKELY(x)        (x)

327. ‌#define LJ_UNLIKELY(x)        (x)

328. #endif

330. /* Attributes for internal functions. */

331. ‌#define LJ_DATA                LJ_NOAPI

332. ‌#define LJ_DATADEF

333. ‌#define LJ_ASMF                LJ_NOAPI

334. ‌#define LJ_FUNCA        LJ_NOAPI

335. #if defined(ljamalg_c)

336. ‌#define LJ_FUNC                static

337. #else

338. ‌#define LJ_FUNC                LJ_NOAPI

339. #endif

340. ‌#define LJ_FUNC_NORET        LJ_FUNC LJ_NORET

341. ‌#define LJ_FUNCA_NORET        LJ_FUNCA LJ_NORET

342. ‌#define LJ_ASMF_NORET        LJ_ASMF LJ_NORET

344. /* Runtime assertions. */

345. #ifdef lua_assert

346. ‌#define check_exp(c, e)                (lua_assert(c), (e))

347. ‌#define api_check(l, e)                lua_assert(e)

348. #else

349. ‌#define lua_assert(c)                ((void)0)

350. ‌#define check_exp(c, e)                (e)

351. ‌#define api_check                luai_apicheck

352. #endif

354. /* Static assertions. */

355. ‌#define LJ_ASSERT_NAME2(name, line)        name ## line

356. ‌#define LJ_ASSERT_NAME(line)                LJ_ASSERT_NAME2(lj_assert_, line)

357. #ifdef __COUNTER__

358. ‌#define LJ_STATIC_ASSERT(cond) \

359. extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])

360. #else

361. ‌#define LJ_STATIC_ASSERT(cond) \

362. extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])

363. #endif

365. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_obj.h - luajit-2.0-src

Global variables defined

- TValue

- lj_obj_itypename

- lj_obj_typename

- offsetof

- offsetof

- offsetof

- offsetof

- offsetof

- offsetof

- offsetof

- offsetof

Data types defined

- ASMFunction

- BCIns

- BCLine

- BCPos

- BCReg

- FrameLink

- GCRef

- GCRef

- GCRootID

- GCSize

- GCSize

- GCState

- GCState

- GCcdata

- GCcdata

- GCcdataVar

- GCcdataVar

- GCfunc

- GCfunc

- GCfuncC

- GCfuncC

- GCfuncL

- GCfuncL

- GChead

- GChead

- GCobj

- GCobj

- GCproto

- GCproto

- GCstr

- GCstr

- GCtab

- GCtab

- GCudata

- GCudata

- GCupval

- GCupval

- MMS

- MRef

- MRef

- MSize

- Node

- Node

- SBuf

- SBuf

- TValue

- cTValue

- global_State

- global_State

- lua_State

Functions defined

- copyTV

- lj_num2bit

- lj_num2u64

- numberVint

- numberVnum

- setgcV

- setgcVraw

- setint64V

- setintV

- setlightudV

Macros defined

- FF_C

- FF_LUA

- G

- GCHeader

- GCfuncHeader

- HOOK_ACTIVE

- HOOK_ACTIVE_SHIFT

- HOOK_EVENTMASK

- HOOK_GC

- HOOK_PROFILE

- HOOK_VMEVENT

- LAST_TT

- LJ_GCVMASK

- LJ_TCDATA

- LJ_TFALSE

- LJ_TFUNC

- LJ_TISGCV

- LJ_TISNUM

- LJ_TISNUM

- LJ_TISPRI

- LJ_TISTABUD

- LJ_TISTRUECOND

- LJ_TLIGHTUD

- LJ_TNIL

- LJ_TNUMX

- LJ_TPROTO

- LJ_TSTR

- LJ_TTAB

- LJ_TTHREAD

- LJ_TTRACE

- LJ_TTRUE

- LJ_TUDATA

- LJ_TUPVAL

- LUA_TCDATA

- LUA_TPROTO

- MMDEF

- MMDEF_FFI

- MMDEF_FFI

- MMDEF_PAIRS

- MMDEF_PAIRS

- MMENUM

- MMENUM

- MM_ipairs

- MM_pairs

- PROTO_CHILD

- PROTO_CLCOUNT

- PROTO_CLC_BITS

- PROTO_CLC_POLY

- PROTO_FFI

- PROTO_FIXUP_RETURN

- PROTO_HAS_RETURN

- PROTO_ILOOP

- PROTO_NOJIT

- PROTO_UV_IMMUTABLE

- PROTO_UV_LOCAL

- PROTO_VARARG

- SCALE_NUM_GCO

- _LJ_OBJ_H

- basemt_it

- basemt_obj

- boolV

- cdataV

- cdataisv

- cdataptr

- cdatav

- cdatavlen

- checklightudptr

- checklightudptr

- curr_func

- curr_func

- curr_func

- curr_funcisL

- curr_proto

- curr_top

- curr_topL

- define_setV

- funcV

- funcproto

- gcV

- gcnext

- gco2cd

- gco2func

- gco2pt

- gco2str

- gco2tab

- gco2th

- gco2ud

- gco2uv

- gcref

- gcref

- gcrefeq

- gcrefeq

- gcrefp

- gcrefp

- gcrefu

- gcrefu

- gcval

- gcval

- getfreetop

- getfreetop

- hook_active

- hook_enter

- hook_entergc

- hook_leave

- hook_restore

- hook_save

- hook_vmevent

- intV

- iscfunc

- isffunc

- isluafunc

- itype

- itype

- itypemap

- itypemap

- lightudV

- lightudV

- lj_num2int

- lj_typename

- mainthread

- memcdatav

- mmname_str

- mref

- mref

- nextnode

- niltv

- niltvg

- noderef

- numV

- obj2gco

- protoV

- proto_bc

- proto_bcpos

- proto_chunkname

- proto_chunknamestr

- proto_kgc

- proto_knumtv

- proto_lineinfo

- proto_uv

- proto_uvinfo

- proto_varinfo

- rawnumequal

- registry

- round_nkgc

- setboolV

- setboolV

- setcont

- setcont

- setcont

- setfreetop

- setfreetop

- setgcref

- setgcref

- setgcrefnull

- setgcrefnull

- setgcrefp

- setgcrefp

- setgcrefr

- setgcrefr

- setgcreft

- setintptrV

- setintptrV

- setitype

- setitype

- setminfV

- setmref

- setmref

- setmrefr

- setmrefr

- setnanV

- setnilV

- setnilV

- setnumV

- setpinfV

- setpriV

- setpriV

- setvmstate

- sizeCfunc

- sizeLfunc

- sizecdatav

- sizestring

- sizetabcolo

- sizeudata

- strV

- strVdata

- strdata

- strdatawr

- strref

- tabV

- tabref

- threadV

- tvchecklive

- tvisbool

- tviscdata

- tvisfalse

- tvisfunc

- tvisgcv

- tvisint

- tvislightud

- tvislightud

- tvismzero

- tvisnan

- tvisnil

- tvisnil

- tvisnum

- tvisnumber

- tvispone

- tvispri

- tvisproto

- tvispzero

- tvisstr

- tvistab

- tvistabud

- tvisthread

- tvistrue

- tvistruecond

- tvisudata

- tviszero

- tviszero

- tvref

- udataV

- uddata

- uvnext

- uvprev

- uvval

Source code

1. /*

2. ** LuaJIT VM tags, values and objects.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. #ifndef _LJ_OBJ_H

10. ‌#define _LJ_OBJ_H

12. #include "lua.h"

13. #include "lj_def.h"

14. #include "lj_arch.h"

16. /* -- Memory references (32 bit address space) ---------------------------- */

18. /* Memory and GC object sizes. */

19. ‌typedef uint32_t MSize;

20. #if LJ_GC64

21. ‌typedef uint64_t GCSize;

22. #else

23. ‌typedef uint32_t GCSize;

24. #endif

26. /* Memory reference */

27. ‌typedef struct MRef {

28. #if LJ_GC64

29. uint64_t ptr64;        /* True 64 bit pointer. */

30. #else

31. uint32_t ptr32;        /* Pseudo 32 bit pointer. */

32. #endif

33. ‌} MRef;

35. #if LJ_GC64

36. ‌#define mref(r, t)        ((t *)(void *)(r).ptr64)

38. ‌#define setmref(r, p)        ((r).ptr64 = (uint64_t)(void *)(p))

39. ‌#define setmrefr(r, v)        ((r).ptr64 = (v).ptr64)

40. #else

41. ‌#define mref(r, t)        ((t *)(void *)(uintptr_t)(r).ptr32)

43. ‌#define setmref(r, p)        ((r).ptr32 = (uint32_t)(uintptr_t)(void *)(p))

44. ‌#define setmrefr(r, v)        ((r).ptr32 = (v).ptr32)

45. #endif

47. /* -- GC object references (32 bit address space) ------------------------- */

49. /* GCobj reference */

50. ‌typedef struct GCRef {

51. #if LJ_GC64

52. uint64_t gcptr64;        /* True 64 bit pointer. */

53. #else

54. uint32_t gcptr32;        /* Pseudo 32 bit pointer. */

55. #endif

56. ‌} GCRef;

58. /* Common GC header for all collectable objects. */

59. ‌#define GCHeader        GCRef nextgc; uint8_t marked; uint8_t gct

60. /* This occupies 6 bytes, so use the next 2 bytes for non-32 bit fields. */

62. #if LJ_GC64

63. ‌#define gcref(r)        ((GCobj *)(r).gcptr64)

64. ‌#define gcrefp(r, t)        ((t *)(void *)(r).gcptr64)

65. ‌#define gcrefu(r)        ((r).gcptr64)

66. ‌#define gcrefeq(r1, r2)        ((r1).gcptr64 == (r2).gcptr64)

68. ‌#define setgcref(r, gc)        ((r).gcptr64 = (uint64_t)&(gc)->gch)

69. ‌#define setgcreft(r, gc, it) \

70. (r).gcptr64 = (uint64_t)&(gc)->gch | (((uint64_t)(it)) << 47)

71. ‌#define setgcrefp(r, p)        ((r).gcptr64 = (uint64_t)(p))

72. ‌#define setgcrefnull(r)        ((r).gcptr64 = 0)

73. ‌#define setgcrefr(r, v)        ((r).gcptr64 = (v).gcptr64)

74. #else

75. ‌#define gcref(r)        ((GCobj *)(uintptr_t)(r).gcptr32)

76. ‌#define gcrefp(r, t)        ((t *)(void *)(uintptr_t)(r).gcptr32)

77. ‌#define gcrefu(r)        ((r).gcptr32)

78. ‌#define gcrefeq(r1, r2)        ((r1).gcptr32 == (r2).gcptr32)

80. ‌#define setgcref(r, gc)        ((r).gcptr32 = (uint32_t)(uintptr_t)&(gc)->gch)

81. ‌#define setgcrefp(r, p)        ((r).gcptr32 = (uint32_t)(uintptr_t)(p))

82. ‌#define setgcrefnull(r)        ((r).gcptr32 = 0)

83. ‌#define setgcrefr(r, v)        ((r).gcptr32 = (v).gcptr32)

84. #endif

86. ‌#define gcnext(gc)        (gcref((gc)->gch.nextgc))

88. /* IMPORTANT NOTE:

89. **

90. ** All uses of the setgcref* macros MUST be accompanied with a write barrier.

91. **

92. ** This is to ensure the integrity of the incremental GC. The invariant

93. ** to preserve is that a black object never points to a white object.

94. ** I.e. never store a white object into a field of a black object.

95. **

96. ** It's ok to LEAVE OUT the write barrier ONLY in the following cases:

97. ** - The source is not a GC object (NULL).

98. ** - The target is a GC root. I.e. everything in global_State.

99. ** - The target is a lua_State field (threads are never black).

100. ** - The target is a stack slot, see setgcV et al.

101. ** - The target is an open upvalue, i.e. pointing to a stack slot.

102. ** - The target is a newly created object (i.e. marked white). But make

103. **   sure nothing invokes the GC inbetween.

104. ** - The target and the source are the same object (self-reference).

105. ** - The target already contains the object (e.g. moving elements around).

106. **

107. ** The most common case is a store to a stack slot. All other cases where

108. ** a barrier has been omitted are annotated with a NOBARRIER comment.

109. **

110. ** The same logic applies for stores to table slots (array part or hash

111. ** part). ALL uses of lj_tab_set* require a barrier for the stored value

112. ** *and* the stored key, based on the above rules. In practice this means

113. ** a barrier is needed if *either* of the key or value are a GC object.

114. **

115. ** It's ok to LEAVE OUT the write barrier in the following special cases:

116. ** - The stored value is nil. The key doesn't matter because it's either

117. **   not resurrected or lj_tab_newkey() will take care of the key barrier.

118. ** - The key doesn't matter if the *previously* stored value is guaranteed

119. **   to be non-nil (because the key is kept alive in the table).

120. ** - The key doesn't matter if it's guaranteed not to be part of the table,

121. **   since lj_tab_newkey() takes care of the key barrier. This applies

122. **   trivially to new tables, but watch out for resurrected keys. Storing

123. **   a nil value leaves the key in the table!

124. **

125. ** In case of doubt use lj_gc_anybarriert() as it's rather cheap. It's used

126. ** by the interpreter for all table stores.

127. **

128. ** Note: In contrast to Lua's GC, LuaJIT's GC does *not* specially mark

129. ** dead keys in tables. The reference is left in, but it's guaranteed to

130. ** be never dereferenced as long as the value is nil. It's ok if the key is

131. ** freed or if any object subsequently gets the same address.

132. **

133. ** Not destroying dead keys helps to keep key hash slots stable. This avoids

134. ** specialization back-off for HREFK when a value flips between nil and

135. ** non-nil and the GC gets in the way. It also allows safely hoisting

136. ** HREF/HREFK across GC steps. Dead keys are only removed if a table is

137. ** resized (i.e. by NEWREF) and xREF must not be CSEd across a resize.

138. **

139. ** The trade-off is that a write barrier for tables must take the key into

140. ** account, too. Implicitly resurrecting the key by storing a non-nil value

141. ** may invalidate the incremental GC invariant.

142. */

144. /* -- Common type definitions --------------------------------------------- */

146. /* Types for handling bytecodes. Need this here, details in lj_bc.h. */

147. ‌typedef uint32_t BCIns;  /* Bytecode instruction. */

148. ‌typedef uint32_t BCPos;  /* Bytecode position. */

149. ‌typedef uint32_t BCReg;  /* Bytecode register. */

150. ‌typedef int32_t BCLine;  /* Bytecode line number. */

152. /* Internal assembler functions. Never call these directly from C. */

153. ‌typedef void (*ASMFunction)(void);

155. /* Resizable string buffer. Need this here, details in lj_buf.h. */

156. ‌typedef struct SBuf {

157. MRef p;                /* String buffer pointer. */

158. MRef e;                /* String buffer end pointer. */

159. MRef b;                /* String buffer base. */

160. MRef L;                /* lua_State, used for buffer resizing. */

161. ‌} SBuf;

163. /* -- Tags and values ----------------------------------------------------- */

165. /* Frame link. */

166. typedef union {

167. int32_t ftsz;                /* Frame type and size of previous frame. */

168. MRef pcr;                /* Or PC for Lua frames. */

169. ‌} FrameLink;

171. /* Tagged value. */

172. ‌typedef LJ_ALIGN(8) union TValue {

173. uint64_t u64;                /* 64 bit pattern overlaps number. */

174. lua_Number n;                /* Number object overlaps split tag/value object. */

175. #if LJ_GC64

176. GCRef gcr;                /* GCobj reference with tag. */

177. int64_t it64;

178. struct {

179. LJ_ENDIAN_LOHI(

180. int32_t i;        /* Integer value. */

181. , uint32_t it;        /* Internal object tag. Must overlap MSW of number. */

182. )

183. };

184. #else

185. struct {

186. LJ_ENDIAN_LOHI(

187. union {

188. GCRef gcr;        /* GCobj reference (if any). */

189. int32_t i;        /* Integer value. */

190. };

191. , uint32_t it;        /* Internal object tag. Must overlap MSW of number. */

192. )

193. };

194. #endif

195. #if LJ_FR2

196. int64_t ftsz;                /* Frame type and size of previous frame, or PC. */

197. #else

198. struct {

199. LJ_ENDIAN_LOHI(

200. GCRef func;        /* Function for next frame (or dummy L). */

201. , FrameLink tp;        /* Link to previous frame. */

202. )

203. } fr;

204. #endif

205. struct {

206. LJ_ENDIAN_LOHI(

207. uint32_t lo;        /* Lower 32 bits of number. */

208. , uint32_t hi;        /* Upper 32 bits of number. */

209. )

210. } u32;

211. ‌} TValue;

213. ‌typedef const TValue cTValue;

215. ‌#define tvref(r)        (mref(r, TValue))

217. /* More external and GCobj tags for internal objects. */

218. ‌#define LAST_TT                LUA_TTHREAD

219. ‌#define LUA_TPROTO        (LAST_TT+1)

220. ‌#define LUA_TCDATA        (LAST_TT+2)

222. /* Internal object tags.

223. **

224. ** Format for 32 bit GC references (!LJ_GC64):

225. **

226. ** Internal tags overlap the MSW of a number object (must be a double).

227. ** Interpreted as a double these are special NaNs. The FPU only generates

228. ** one type of NaN (0xfff8_0000_0000_0000). So MSWs > 0xfff80000 are available

229. ** for use as internal tags. Small negative numbers are used to shorten the

230. ** encoding of type comparisons (reg/mem against sign-ext. 8 bit immediate).

231. **

232. **                  ---MSW---.---LSW---

233. ** primitive types |  itype  |         |

234. ** lightuserdata   |  itype  |  void * |  (32 bit platforms)

235. ** lightuserdata   |ffff|    void *    |  (64 bit platforms, 47 bit pointers)

236. ** GC objects      |  itype  |  GCRef  |

237. ** int (LJ_DUALNUM)|  itype  |   int   |

238. ** number           -------double------

239. **

240. ** Format for 64 bit GC references (LJ_GC64):

241. **

242. ** The upper 13 bits must be 1 (0xfff8...) for a special NaN. The next

243. ** 4 bits hold the internal tag. The lowest 47 bits either hold a pointer,

244. ** a zero-extended 32 bit integer or all bits set to 1 for primitive types.

245. **

246. **                     ------MSW------.------LSW------

247. ** primitive types    |1..1|itype|1..................1|

248. ** GC objects/lightud |1..1|itype|-------GCRef--------|

249. ** int (LJ_DUALNUM)   |1..1|itype|0..0|-----int-------|

250. ** number              ------------double-------------

251. **

252. ** ORDER LJ_T

253. ** Primitive types nil/false/true must be first, lightuserdata next.

254. ** GC objects are at the end, table/userdata must be lowest.

255. ** Also check lj_ir.h for similar ordering constraints.

256. */

257. ‌#define LJ_TNIL                        (~0u)

258. ‌#define LJ_TFALSE                (~1u)

259. ‌#define LJ_TTRUE                (~2u)

260. ‌#define LJ_TLIGHTUD                (~3u)

261. ‌#define LJ_TSTR                        (~4u)

262. ‌#define LJ_TUPVAL                (~5u)

263. ‌#define LJ_TTHREAD                (~6u)

264. ‌#define LJ_TPROTO                (~7u)

265. ‌#define LJ_TFUNC                (~8u)

266. ‌#define LJ_TTRACE                (~9u)

267. ‌#define LJ_TCDATA                (~10u)

268. ‌#define LJ_TTAB                        (~11u)

269. ‌#define LJ_TUDATA                (~12u)

270. /* This is just the canonical number type used in some places. */

271. ‌#define LJ_TNUMX                (~13u)

273. /* Integers have itype == LJ_TISNUM doubles have itype < LJ_TISNUM */

274. #if LJ_64 && !LJ_GC64

275. ‌#define LJ_TISNUM                0xfffeffffu

276. #else

277. ‌#define LJ_TISNUM                LJ_TNUMX

278. #endif

279. ‌#define LJ_TISTRUECOND                LJ_TFALSE

280. ‌#define LJ_TISPRI                LJ_TTRUE

281. ‌#define LJ_TISGCV                (LJ_TSTR+1)

282. ‌#define LJ_TISTABUD                LJ_TTAB

284. #if LJ_GC64

285. ‌#define LJ_GCVMASK                (((uint64_t)1 << 47) - 1)

286. #endif

288. /* -- String object ------------------------------------------------------- */

290. /* String object header. String payload follows. */

291. ‌typedef struct GCstr {

292. GCHeader;

293. uint8_t reserved;        /* Used by lexer for fast lookup of reserved words. */

294. uint8_t unused;

295. MSize hash;                /* Hash of string. */

296. MSize len;                /* Size of string. */

297. ‌} GCstr;

299. ‌#define strref(r)        (&gcref((r))->str)

300. ‌#define strdata(s)        ((const char *)((s)+1))

301. ‌#define strdatawr(s)        ((char *)((s)+1))

302. ‌#define strVdata(o)        strdata(strV(o))

303. ‌#define sizestring(s)        (sizeof(struct GCstr)+(s)->len+1)

305. /* -- Userdata object ----------------------------------------------------- */

307. /* Userdata object. Payload follows. */

308. ‌typedef struct GCudata {

309. GCHeader;

310. uint8_t udtype;        /* Userdata type. */

311. uint8_t unused2;

312. GCRef env;                /* Should be at same offset in GCfunc. */

313. MSize len;                /* Size of payload. */

314. GCRef metatable;        /* Must be at same offset in GCtab. */

315. uint32_t align1;        /* To force 8 byte alignment of the payload. */

316. ‌} GCudata;

318. /* Userdata types. */

319. enum {

320. UDTYPE_USERDATA,        /* Regular userdata. */

321. UDTYPE_IO_FILE,        /* I/O library FILE. */

322. UDTYPE_FFI_CLIB,        /* FFI C library namespace. */

323. UDTYPE__MAX

324. };

326. ‌#define uddata(u)        ((void *)((u)+1))

327. ‌#define sizeudata(u)        (sizeof(struct GCudata)+(u)->len)

329. /* -- C data object ------------------------------------------------------- */

331. /* C data object. Payload follows. */

332. ‌typedef struct GCcdata {

333. GCHeader;

334. uint16_t ctypeid;        /* C type ID. */

335. ‌} GCcdata;

337. /* Prepended to variable-sized or realigned C data objects. */

338. ‌typedef struct GCcdataVar {

339. uint16_t offset;        /* Offset to allocated memory (relative to GCcdata). */

340. uint16_t extra;        /* Extra space allocated (incl. GCcdata + GCcdatav). */

341. MSize len;                /* Size of payload. */

342. ‌} GCcdataVar;

344. ‌#define cdataptr(cd)        ((void *)((cd)+1))

345. ‌#define cdataisv(cd)        ((cd)->marked & 0x80)

346. ‌#define cdatav(cd)        ((GCcdataVar *)((char *)(cd) - sizeof(GCcdataVar)))

347. ‌#define cdatavlen(cd)        check_exp(cdataisv(cd), cdatav(cd)->len)

348. ‌#define sizecdatav(cd)        (cdatavlen(cd) + cdatav(cd)->extra)

349. ‌#define memcdatav(cd)        ((void *)((char *)(cd) - cdatav(cd)->offset))

351. /* -- Prototype object ---------------------------------------------------- */

353. ‌#define SCALE_NUM_GCO        ((int32_t)sizeof(lua_Number)/sizeof(GCRef))

354. ‌#define round_nkgc(n)        (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1))

356. ‌typedef struct GCproto {

357. GCHeader;

358. uint8_t numparams;        /* Number of parameters. */

359. uint8_t framesize;        /* Fixed frame size. */

360. MSize sizebc;                /* Number of bytecode instructions. */

361. #if LJ_GC64

362. uint32_t unused_gc64;

363. #endif

364. GCRef gclist;

365. MRef k;                /* Split constant array (points to the middle). */

366. MRef uv;                /* Upvalue list. local slot|0x8000 or parent uv idx. */

367. MSize sizekgc;        /* Number of collectable constants. */

368. MSize sizekn;                /* Number of lua_Number constants. */

369. MSize sizept;                /* Total size including colocated arrays. */

370. uint8_t sizeuv;        /* Number of upvalues. */

371. uint8_t flags;        /* Miscellaneous flags (see below). */

372. uint16_t trace;        /* Anchor for chain of root traces. */

373. /* ------ The following fields are for debugging/tracebacks only ------ */

374. GCRef chunkname;        /* Name of the chunk this function was defined in. */

375. BCLine firstline;        /* First line of the function definition. */

376. BCLine numline;        /* Number of lines for the function definition. */

377. MRef lineinfo;        /* Compressed map from bytecode ins. to source line. */

378. MRef uvinfo;                /* Upvalue names. */

379. MRef varinfo;                /* Names and compressed extents of local variables. */

380. ‌} GCproto;

382. /* Flags for prototype. */

383. ‌#define PROTO_CHILD                0x01        /* Has child prototypes. */

384. ‌#define PROTO_VARARG                0x02        /* Vararg function. */

385. ‌#define PROTO_FFI                0x04        /* Uses BC_KCDATA for FFI datatypes. */

386. ‌#define PROTO_NOJIT                0x08        /* JIT disabled for this function. */

387. ‌#define PROTO_ILOOP                0x10        /* Patched bytecode with ILOOP etc. */

388. /* Only used during parsing. */

389. ‌#define PROTO_HAS_RETURN        0x20        /* Already emitted a return. */

390. ‌#define PROTO_FIXUP_RETURN        0x40        /* Need to fixup emitted returns. */

391. /* Top bits used for counting created closures. */

392. ‌#define PROTO_CLCOUNT                0x20        /* Base of saturating 3 bit counter. */

393. ‌#define PROTO_CLC_BITS                3

394. ‌#define PROTO_CLC_POLY                (3*PROTO_CLCOUNT)  /* Polymorphic threshold. */

396. ‌#define PROTO_UV_LOCAL                0x8000        /* Upvalue for local slot. */

397. ‌#define PROTO_UV_IMMUTABLE        0x4000        /* Immutable upvalue. */

399. ‌#define proto_kgc(pt, idx) \

400. check_exp((uintptr_t)(intptr_t)(idx) >= (uintptr_t)-(intptr_t)(pt)->sizekgc, \

401. gcref(mref((pt)->k, GCRef)[(idx)]))

402. ‌#define proto_knumtv(pt, idx) \

403. check_exp((uintptr_t)(idx) < (pt)->sizekn, &mref((pt)->k, TValue)[(idx)])

404. ‌#define proto_bc(pt)                ((BCIns *)((char *)(pt) + sizeof(GCproto)))

405. ‌#define proto_bcpos(pt, pc)        ((BCPos)((pc) - proto_bc(pt)))

406. ‌#define proto_uv(pt)                (mref((pt)->uv, uint16_t))

408. ‌#define proto_chunkname(pt)        (strref((pt)->chunkname))

409. ‌#define proto_chunknamestr(pt)        (strdata(proto_chunkname((pt))))

410. ‌#define proto_lineinfo(pt)        (mref((pt)->lineinfo, const void))

411. ‌#define proto_uvinfo(pt)        (mref((pt)->uvinfo, const uint8_t))

412. ‌#define proto_varinfo(pt)        (mref((pt)->varinfo, const uint8_t))

414. /* -- Upvalue object ------------------------------------------------------ */

416. ‌typedef struct GCupval {

417. GCHeader;

418. uint8_t closed;        /* Set if closed (i.e. uv->v == &uv->u.value). */

419. uint8_t immutable;        /* Immutable value. */

420. union {

421. TValue tv;                /* If closed: the value itself. */

422. struct {                /* If open: double linked list, anchored at thread. */

423. GCRef prev;

424. GCRef next;

425. };

426. };

427. MRef v;                /* Points to stack slot (open) or above (closed). */

428. uint32_t dhash;        /* Disambiguation hash: dh1 != dh2 => cannot alias. */

429. ‌} GCupval;

431. ‌#define uvprev(uv_)        (&gcref((uv_)->prev)->uv)

432. ‌#define uvnext(uv_)        (&gcref((uv_)->next)->uv)

433. ‌#define uvval(uv_)        (mref((uv_)->v, TValue))

435. /* -- Function object (closures) ------------------------------------------ */

437. /* Common header for functions. env should be at same offset in GCudata. */

438. ‌#define GCfuncHeader \

439. GCHeader; uint8_t ffid; uint8_t nupvalues; \

440. GCRef env; GCRef gclist; MRef pc

442. ‌typedef struct GCfuncC {

443. GCfuncHeader;

444. lua_CFunction f;        /* C function to be called. */

445. TValue upvalue[1];        /* Array of upvalues (TValue). */

446. ‌} GCfuncC;

448. ‌typedef struct GCfuncL {

449. GCfuncHeader;

450. GCRef uvptr[1];        /* Array of _pointers_ to upvalue objects (GCupval). */

451. ‌} GCfuncL;

453. ‌typedef union GCfunc {

454. GCfuncC c;

455. GCfuncL l;

456. ‌} GCfunc;

458. ‌#define FF_LUA                0

459. ‌#define FF_C                1

460. ‌#define isluafunc(fn)        ((fn)->c.ffid == FF_LUA)

461. ‌#define iscfunc(fn)        ((fn)->c.ffid == FF_C)

462. ‌#define isffunc(fn)        ((fn)->c.ffid > FF_C)

463. ‌#define funcproto(fn) \

464. check_exp(isluafunc(fn), (GCproto *)(mref((fn)->l.pc, char)-sizeof(GCproto)))

465. ‌#define sizeCfunc(n)        (sizeof(GCfuncC)-sizeof(TValue)+sizeof(TValue)*(n))

466. ‌#define sizeLfunc(n)        (sizeof(GCfuncL)-sizeof(GCRef)+sizeof(GCRef)*(n))

468. /* -- Table object -------------------------------------------------------- */

470. /* Hash node. */

471. ‌typedef struct Node {

472. TValue val;                /* Value object. Must be first field. */

473. TValue key;                /* Key object. */

474. MRef next;                /* Hash chain. */

475. #if !LJ_GC64

476. MRef freetop;                /* Top of free elements (stored in t->node[0]). */

477. #endif

478. ‌} Node;

480. LJ_STATIC_ASSERT(offsetof(Node, val) == 0);

482. ‌typedef struct GCtab {

483. GCHeader;

484. uint8_t nomm;                /* Negative cache for fast metamethods. */

485. int8_t colo;                /* Array colocation. */

486. MRef array;                /* Array part. */

487. GCRef gclist;

488. GCRef metatable;        /* Must be at same offset in GCudata. */

489. MRef node;                /* Hash part. */

490. uint32_t asize;        /* Size of array part (keys [0, asize-1]). */

491. uint32_t hmask;        /* Hash part mask (size of hash part - 1). */

492. #if LJ_GC64

493. MRef freetop;                /* Top of free elements. */

494. #endif

495. ‌} GCtab;

497. ‌#define sizetabcolo(n)        ((n)*sizeof(TValue) + sizeof(GCtab))

498. ‌#define tabref(r)        (&gcref((r))->tab)

499. ‌#define noderef(r)        (mref((r), Node))

500. ‌#define nextnode(n)        (mref((n)->next, Node))

501. #if LJ_GC64

502. ‌#define getfreetop(t, n)        (noderef((t)->freetop))

503. ‌#define setfreetop(t, n, v)        (setmref((t)->freetop, (v)))

504. #else

505. ‌#define getfreetop(t, n)        (noderef((n)->freetop))

506. ‌#define setfreetop(t, n, v)        (setmref((n)->freetop, (v)))

507. #endif

509. /* -- State objects ------------------------------------------------------- */

511. /* VM states. */

512. enum {

513. LJ_VMST_INTERP,        /* Interpreter. */

514. LJ_VMST_C,                /* C function. */

515. LJ_VMST_GC,                /* Garbage collector. */

516. LJ_VMST_EXIT,                /* Trace exit handler. */

517. LJ_VMST_RECORD,        /* Trace recorder. */

518. LJ_VMST_OPT,                /* Optimizer. */

519. LJ_VMST_ASM,                /* Assembler. */

520. LJ_VMST__MAX

521. };

523. ‌#define setvmstate(g, st)        ((g)->vmstate = ~LJ_VMST_##st)

525. /* Metamethods. ORDER MM */

526. #ifdef LJ_HASFFI

527. ‌#define MMDEF_FFI(_) _(new)

528. #else

529. ‌#define MMDEF_FFI(_)

530. #endif

532. #if LJ_52 || LJ_HASFFI

533. ‌#define MMDEF_PAIRS(_) _(pairs) _(ipairs)

534. #else

535. ‌#define MMDEF_PAIRS(_)

536. ‌#define MM_pairs        255

537. ‌#define MM_ipairs        255

538. #endif

540. ‌#define MMDEF(_) \

541. _(index) _(newindex) _(gc) _(mode) _(eq) _(len) \

542. /* Only the above (fast) metamethods are negative cached (max. 8). */ \

543. _(lt) _(le) _(concat) _(call) \

544. /* The following must be in ORDER ARITH. */ \

545. _(add) _(sub) _(mul) _(div) _(mod) _(pow) _(unm) \

546. /* The following are used in the standard libraries. */ \

547. _(metatable) _(tostring) MMDEF_FFI(_) MMDEF_PAIRS(_)

549. typedef enum {

550. ‌#define MMENUM(name)        MM_##name,

551. MMDEF(MMENUM)

552. ‌#undef MMENUM

553. MM__MAX,

554. MM____ = MM__MAX,

555. MM_FAST = MM_len

556. ‌} MMS;

558. /* GC root IDs. */

559. typedef enum {

560. GCROOT_MMNAME,        /* Metamethod names. */

561. GCROOT_MMNAME_LAST = GCROOT_MMNAME + MM__MAX-1,

562. GCROOT_BASEMT,        /* Metatables for base types. */

563. GCROOT_BASEMT_NUM = GCROOT_BASEMT + ~LJ_TNUMX,

564. GCROOT_IO_INPUT,        /* Userdata for default I/O input file. */

565. GCROOT_IO_OUTPUT,        /* Userdata for default I/O output file. */

566. GCROOT_MAX

567. ‌} GCRootID;

569. ‌#define basemt_it(g, it)        ((g)->gcroot[GCROOT_BASEMT+~(it)])

570. ‌#define basemt_obj(g, o)        ((g)->gcroot[GCROOT_BASEMT+itypemap(o)])

571. ‌#define mmname_str(g, mm)        (strref((g)->gcroot[GCROOT_MMNAME+(mm)]))

573. ‌typedef struct GCState {

574. GCSize total;                /* Memory currently allocated. */

575. GCSize threshold;        /* Memory threshold. */

576. uint8_t currentwhite;        /* Current white color. */

577. uint8_t state;        /* GC state. */

578. uint8_t nocdatafin;        /* No cdata finalizer called. */

579. uint8_t unused2;

580. MSize sweepstr;        /* Sweep position in string table. */

581. GCRef root;                /* List of all collectable objects. */

582. MRef sweep;                /* Sweep position in root list. */

583. GCRef gray;                /* List of gray objects. */

584. GCRef grayagain;        /* List of objects for atomic traversal. */

585. GCRef weak;                /* List of weak tables (to be cleared). */

586. GCRef mmudata;        /* List of userdata (to be finalized). */

587. GCSize debt;                /* Debt (how much GC is behind schedule). */

588. GCSize estimate;        /* Estimate of memory actually in use. */

589. MSize stepmul;        /* Incremental GC step granularity. */

590. MSize pause;                /* Pause between successive GC cycles. */

591. ‌} GCState;

593. /* Global state, shared by all threads of a Lua universe. */

594. ‌typedef struct global_State {

595. GCRef *strhash;        /* String hash table (hash chain anchors). */

596. MSize strmask;        /* String hash mask (size of hash table - 1). */

597. MSize strnum;                /* Number of strings in hash table. */

598. lua_Alloc allocf;        /* Memory allocator. */

599. void *allocd;                /* Memory allocator data. */

600. GCState gc;                /* Garbage collector. */

601. volatile int32_t vmstate;  /* VM state or current JIT code trace number. */

602. SBuf tmpbuf;                /* Temporary string buffer. */

603. GCstr strempty;        /* Empty string. */

604. uint8_t stremptyz;        /* Zero terminator of empty string. */

605. uint8_t hookmask;        /* Hook mask. */

606. uint8_t dispatchmode;        /* Dispatch mode. */

607. uint8_t vmevmask;        /* VM event mask. */

608. GCRef mainthref;        /* Link to main thread. */

609. TValue registrytv;        /* Anchor for registry. */

610. TValue tmptv, tmptv2;        /* Temporary TValues. */

611. Node nilnode;                /* Fallback 1-element hash part (nil key and value). */

612. GCupval uvhead;        /* Head of double-linked list of all open upvalues. */

613. int32_t hookcount;        /* Instruction hook countdown. */

614. int32_t hookcstart;        /* Start count for instruction hook counter. */

615. lua_Hook hookf;        /* Hook function. */

616. lua_CFunction wrapf;        /* Wrapper for C function calls. */

617. lua_CFunction panic;        /* Called as a last resort for errors. */

618. BCIns bc_cfunc_int;        /* Bytecode for internal C function calls. */

619. BCIns bc_cfunc_ext;        /* Bytecode for external C function calls. */

620. GCRef cur_L;                /* Currently executing lua_State. */

621. MRef jit_base;        /* Current JIT code L->base or NULL. */

622. MRef ctype_state;        /* Pointer to C type state. */

623. GCRef gcroot[GCROOT_MAX];  /* GC roots. */

624. ‌} global_State;

626. ‌#define mainthread(g)        (&gcref(g->mainthref)->th)

627. ‌#define niltv(L) \

628. check_exp(tvisnil(&G(L)->nilnode.val), &G(L)->nilnode.val)

629. ‌#define niltvg(g) \

630. check_exp(tvisnil(&(g)->nilnode.val), &(g)->nilnode.val)

632. /* Hook management. Hook event masks are defined in lua.h. */

633. ‌#define HOOK_EVENTMASK                0x0f

634. ‌#define HOOK_ACTIVE                0x10

635. ‌#define HOOK_ACTIVE_SHIFT        4

636. ‌#define HOOK_VMEVENT                0x20

637. ‌#define HOOK_GC                        0x40

638. ‌#define HOOK_PROFILE                0x80

639. ‌#define hook_active(g)                ((g)->hookmask & HOOK_ACTIVE)

640. ‌#define hook_enter(g)                ((g)->hookmask |= HOOK_ACTIVE)

641. ‌#define hook_entergc(g)                ((g)->hookmask |= (HOOK_ACTIVE|HOOK_GC))

642. ‌#define hook_vmevent(g)                ((g)->hookmask |= (HOOK_ACTIVE|HOOK_VMEVENT))

643. ‌#define hook_leave(g)                ((g)->hookmask &= ~HOOK_ACTIVE)

644. ‌#define hook_save(g)                ((g)->hookmask & ~HOOK_EVENTMASK)

645. ‌#define hook_restore(g, h) \

646. ((g)->hookmask = ((g)->hookmask & HOOK_EVENTMASK) | (h))

648. /* Per-thread state object. */

649. ‌struct lua_State {

650. GCHeader;

651. uint8_t dummy_ffid;        /* Fake FF_C for curr_funcisL() on dummy frames. */

652. uint8_t status;        /* Thread status. */

653. MRef glref;                /* Link to global state. */

654. GCRef gclist;                /* GC chain. */

655. TValue *base;                /* Base of currently executing function. */

656. TValue *top;                /* First free slot in the stack. */

657. MRef maxstack;        /* Last free slot in the stack. */

658. MRef stack;                /* Stack base. */

659. GCRef openupval;        /* List of open upvalues in the stack. */

660. GCRef env;                /* Thread environment (table of globals). */

661. void *cframe;                /* End of C stack frame chain. */

662. MSize stacksize;        /* True stack size (incl. LJ_STACK_EXTRA). */

663. };

665. ‌#define G(L)                        (mref(L->glref, global_State))

666. ‌#define registry(L)                (&G(L)->registrytv)

668. /* Macros to access the currently executing (Lua) function. */

669. #if LJ_GC64

670. ‌#define curr_func(L)                (&gcval(L->base-2)->fn)

671. #elif LJ_FR2

672. ‌#define curr_func(L)                (&gcref((L->base-2)->gcr)->fn)

673. #else

674. ‌#define curr_func(L)                (&gcref((L->base-1)->fr.func)->fn)

675. #endif

676. ‌#define curr_funcisL(L)                (isluafunc(curr_func(L)))

677. ‌#define curr_proto(L)                (funcproto(curr_func(L)))

678. ‌#define curr_topL(L)                (L->base + curr_proto(L)->framesize)

679. ‌#define curr_top(L)                (curr_funcisL(L) ? curr_topL(L) : L->top)

681. /* -- GC object definition and conversions -------------------------------- */

683. /* GC header for generic access to common fields of GC objects. */

684. ‌typedef struct GChead {

685. GCHeader;

686. uint8_t unused1;

687. uint8_t unused2;

688. GCRef env;

689. GCRef gclist;

690. GCRef metatable;

691. ‌} GChead;

693. /* The env field SHOULD be at the same offset for all GC objects. */

694. ‌LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCfuncL, env));

695. ‌LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCudata, env));

697. /* The metatable field MUST be at the same offset for all GC objects. */

698. ‌LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCtab, metatable));

699. ‌LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCudata, metatable));

701. /* The gclist field MUST be at the same offset for all GC objects. */

702. ‌LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(lua_State, gclist));

703. ‌LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCproto, gclist));

704. ‌LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCfuncL, gclist));

705. ‌LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtab, gclist));

707. ‌typedef union GCobj {

708. GChead gch;

709. GCstr str;

710. GCupval uv;

711. lua_State th;

712. GCproto pt;

713. GCfunc fn;

714. GCcdata cd;

715. GCtab tab;

716. GCudata ud;

717. ‌} GCobj;

719. /* Macros to convert a GCobj pointer into a specific value. */

720. ‌#define gco2str(o)        check_exp((o)->gch.gct == ~LJ_TSTR, &(o)->str)

721. ‌#define gco2uv(o)        check_exp((o)->gch.gct == ~LJ_TUPVAL, &(o)->uv)

722. ‌#define gco2th(o)        check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th)

723. ‌#define gco2pt(o)        check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt)

724. ‌#define gco2func(o)        check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn)

725. ‌#define gco2cd(o)        check_exp((o)->gch.gct == ~LJ_TCDATA, &(o)->cd)

726. ‌#define gco2tab(o)        check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab)

727. ‌#define gco2ud(o)        check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud)

729. /* Macro to convert any collectable object into a GCobj pointer. */

730. ‌#define obj2gco(v)        ((GCobj *)(v))

732. /* -- TValue getters/setters ---------------------------------------------- */

734. #ifdef LUA_USE_ASSERT

735. #include "lj_gc.h"

736. #endif

738. /* Macros to test types. */

739. #if LJ_GC64

740. ‌#define itype(o)        ((uint32_t)((o)->it64 >> 47))

741. ‌#define tvisnil(o)        ((o)->it64 == -1)

742. #else

743. ‌#define itype(o)        ((o)->it)

744. ‌#define tvisnil(o)        (itype(o) == LJ_TNIL)

745. #endif

746. ‌#define tvisfalse(o)        (itype(o) == LJ_TFALSE)

747. ‌#define tvistrue(o)        (itype(o) == LJ_TTRUE)

748. ‌#define tvisbool(o)        (tvisfalse(o) || tvistrue(o))

749. #if LJ_64 && !LJ_GC64

750. ‌#define tvislightud(o)        (((int32_t)itype(o) >> 15) == -2)

751. #else

752. ‌#define tvislightud(o)        (itype(o) == LJ_TLIGHTUD)

753. #endif

754. ‌#define tvisstr(o)        (itype(o) == LJ_TSTR)

755. ‌#define tvisfunc(o)        (itype(o) == LJ_TFUNC)

756. ‌#define tvisthread(o)        (itype(o) == LJ_TTHREAD)

757. ‌#define tvisproto(o)        (itype(o) == LJ_TPROTO)

758. ‌#define tviscdata(o)        (itype(o) == LJ_TCDATA)

759. ‌#define tvistab(o)        (itype(o) == LJ_TTAB)

760. ‌#define tvisudata(o)        (itype(o) == LJ_TUDATA)

761. ‌#define tvisnumber(o)        (itype(o) <= LJ_TISNUM)

762. ‌#define tvisint(o)        (LJ_DUALNUM && itype(o) == LJ_TISNUM)

763. ‌#define tvisnum(o)        (itype(o) < LJ_TISNUM)

765. ‌#define tvistruecond(o)        (itype(o) < LJ_TISTRUECOND)

766. ‌#define tvispri(o)        (itype(o) >= LJ_TISPRI)

767. ‌#define tvistabud(o)        (itype(o) <= LJ_TISTABUD)  /* && !tvisnum() */

768. ‌#define tvisgcv(o)        ((itype(o) - LJ_TISGCV) > (LJ_TNUMX - LJ_TISGCV))

770. /* Special macros to test numbers for NaN, +0, -0, +1 and raw equality. */

771. ‌#define tvisnan(o)        ((o)->n != (o)->n)

772. #if LJ_64

773. ‌#define tviszero(o)        (((o)->u64 << 1) == 0)

774. #else

775. ‌#define tviszero(o)        (((o)->u32.lo | ((o)->u32.hi << 1)) == 0)

776. #endif

777. ‌#define tvispzero(o)        ((o)->u64 == 0)

778. ‌#define tvismzero(o)        ((o)->u64 == U64x(80000000,00000000))

779. ‌#define tvispone(o)        ((o)->u64 == U64x(3ff00000,00000000))

780. ‌#define rawnumequal(o1, o2)        ((o1)->u64 == (o2)->u64)

782. /* Macros to convert type ids. */

783. #if LJ_64 && !LJ_GC64

784. ‌#define itypemap(o) \

785. (tvisnumber(o) ? ~LJ_TNUMX : tvislightud(o) ? ~LJ_TLIGHTUD : ~itype(o))

786. #else

787. ‌#define itypemap(o)        (tvisnumber(o) ? ~LJ_TNUMX : ~itype(o))

788. #endif

790. /* Macros to get tagged values. */

791. #if LJ_GC64

792. ‌#define gcval(o)        ((GCobj *)(gcrefu((o)->gcr) & LJ_GCVMASK))

793. #else

794. ‌#define gcval(o)        (gcref((o)->gcr))

795. #endif

796. ‌#define boolV(o)        check_exp(tvisbool(o), (LJ_TFALSE - itype(o)))

797. #if LJ_64

798. ‌#define lightudV(o) \

799. check_exp(tvislightud(o), (void *)((o)->u64 & U64x(00007fff,ffffffff)))

800. #else

801. ‌#define lightudV(o)        check_exp(tvislightud(o), gcrefp((o)->gcr, void))

802. #endif

803. ‌#define gcV(o)                check_exp(tvisgcv(o), gcval(o))

804. ‌#define strV(o)                check_exp(tvisstr(o), &gcval(o)->str)

805. ‌#define funcV(o)        check_exp(tvisfunc(o), &gcval(o)->fn)

806. ‌#define threadV(o)        check_exp(tvisthread(o), &gcval(o)->th)

807. ‌#define protoV(o)        check_exp(tvisproto(o), &gcval(o)->pt)

808. ‌#define cdataV(o)        check_exp(tviscdata(o), &gcval(o)->cd)

809. ‌#define tabV(o)                check_exp(tvistab(o), &gcval(o)->tab)

810. ‌#define udataV(o)        check_exp(tvisudata(o), &gcval(o)->ud)

811. ‌#define numV(o)                check_exp(tvisnum(o), (o)->n)

812. ‌#define intV(o)                check_exp(tvisint(o), (int32_t)(o)->i)

814. /* Macros to set tagged values. */

815. #if LJ_GC64

816. ‌#define setitype(o, i)                ((o)->it = ((i) << 15))

817. ‌#define setnilV(o)                ((o)->it64 = -1)

818. ‌#define setpriV(o, x)                ((o)->it64 = (int64_t)~((uint64_t)~(x)<<47))

819. ‌#define setboolV(o, x)                ((o)->it64 = (int64_t)~((uint64_t)((x)+1)<<47))

820. #else

821. ‌#define setitype(o, i)                ((o)->it = (i))

822. ‌#define setnilV(o)                ((o)->it = LJ_TNIL)

823. ‌#define setboolV(o, x)                ((o)->it = LJ_TFALSE-(uint32_t)(x))

824. ‌#define setpriV(o, i)                (setitype((o), (i)))

825. #endif

827. ‌static LJ_AINLINE void setlightudV(TValue *o, void *p)

828. {

829. #if LJ_GC64

830. o->u64 = (uint64_t)p | (((uint64_t)LJ_TLIGHTUD) << 47);

831. #elif LJ_64

832. o->u64 = (uint64_t)p | (((uint64_t)0xffff) << 48);

833. #else

834. setgcrefp(o->gcr, p); setitype(o, LJ_TLIGHTUD);

835. #endif

836. }

838. #if LJ_64

839. ‌#define checklightudptr(L, p) \

840. (((uint64_t)(p) >> 47) ? (lj_err_msg(L, LJ_ERR_BADLU), NULL) : (p))

841. #else

842. ‌#define checklightudptr(L, p)        (p)

843. #endif

845. #if LJ_FR2

846. ‌#define setcont(o, f)                ((o)->u64 = (uint64_t)(uintptr_t)(void *)(f))

847. #elif LJ_64

848. ‌#define setcont(o, f) \

849. ((o)->u64 = (uint64_t)(void *)(f) - (uint64_t)lj_vm_asm_begin)

850. #else

851. ‌#define setcont(o, f)                setlightudV((o), (void *)(f))

852. #endif

854. ‌#define tvchecklive(L, o) \

855. UNUSED(L), lua_assert(!tvisgcv(o) || \

856. ((~itype(o) == gcval(o)->gch.gct) && !isdead(G(L), gcval(o))))

858. ‌static LJ_AINLINE void setgcVraw(TValue *o, GCobj *v, uint32_t itype)

859. {

860. #if LJ_GC64

861. setgcreft(o->gcr, v, itype);

862. #else

863. setgcref(o->gcr, v); setitype(o, itype);

864. #endif

865. }

867. ‌static LJ_AINLINE void setgcV(lua_State *L, TValue *o, GCobj *v, uint32_t it)

868. {

869. setgcVraw(o, v, it); tvchecklive(L, o);

870. }

872. ‌#define define_setV(name, type, tag) \

873. static LJ_AINLINE void name(lua_State *L, TValue *o, type *v) \

874. { \

875. setgcV(L, o, obj2gco(v), tag); \

876. }

877. define_setV(setstrV, GCstr, LJ_TSTR)

878. define_setV(setthreadV, lua_State, LJ_TTHREAD)

879. define_setV(setprotoV, GCproto, LJ_TPROTO)

880. define_setV(setfuncV, GCfunc, LJ_TFUNC)

881. define_setV(setcdataV, GCcdata, LJ_TCDATA)

882. define_setV(settabV, GCtab, LJ_TTAB)

883. define_setV(setudataV, GCudata, LJ_TUDATA)

885. ‌#define setnumV(o, x)                ((o)->n = (x))

886. ‌#define setnanV(o)                ((o)->u64 = U64x(fff80000,00000000))

887. ‌#define setpinfV(o)                ((o)->u64 = U64x(7ff00000,00000000))

888. ‌#define setminfV(o)                ((o)->u64 = U64x(fff00000,00000000))

890. ‌static LJ_AINLINE void setintV(TValue *o, int32_t i)

891. {

892. #if LJ_DUALNUM

893. o->i = (uint32_t)i; setitype(o, LJ_TISNUM);

894. #else

895. o->n = (lua_Number)i;

896. #endif

897. }

899. ‌static LJ_AINLINE void setint64V(TValue *o, int64_t i)

900. {

901. if (LJ_DUALNUM && LJ_LIKELY(i == (int64_t)(int32_t)i))

902. setintV(o, (int32_t)i);

903. else

904. setnumV(o, (lua_Number)i);

905. }

907. #if LJ_64

908. ‌#define setintptrV(o, i)        setint64V((o), (i))

909. #else

910. ‌#define setintptrV(o, i)        setintV((o), (i))

911. #endif

913. /* Copy tagged values. */

914. ‌static LJ_AINLINE void copyTV(lua_State *L, TValue *o1, const TValue *o2)

915. {

916. *o1 = *o2; tvchecklive(L, o1);

917. }

919. /* -- Number to integer conversion ---------------------------------------- */

921. #if LJ_SOFTFP

922. LJ_ASMF int32_t lj_vm_tobit(double x);

923. #endif

925. ‌static LJ_AINLINE int32_t lj_num2bit(lua_Number n)

926. {

927. #if LJ_SOFTFP

928. return lj_vm_tobit(n);

929. #else

930. TValue o;

931. o.n = n + 6755399441055744.0;  /* 2^52 + 2^51 */

932. return (int32_t)o.u32.lo;

933. #endif

934. }

936. ‌#define lj_num2int(n)   ((int32_t)(n))

938. ‌static LJ_AINLINE uint64_t lj_num2u64(lua_Number n)

939. {

940. #ifdef _MSC_VER

941. if (n >= 9223372036854775808.0)  /* They think it's a feature. */

942. return (uint64_t)(int64_t)(n - 18446744073709551616.0);

943. else

944. #endif

945. return (uint64_t)n;

946. }

948. ‌static LJ_AINLINE int32_t numberVint(cTValue *o)

949. {

950. if (LJ_LIKELY(tvisint(o)))

951. return intV(o);

952. else

953. return lj_num2int(numV(o));

954. }

956. ‌static LJ_AINLINE lua_Number numberVnum(cTValue *o)

957. {

958. if (LJ_UNLIKELY(tvisint(o)))

959. return (lua_Number)intV(o);

960. else

961. return numV(o);

962. }

964. /* -- Miscellaneous object handling --------------------------------------- */

966. /* Names and maps for internal and external object tags. */

967. ‌LJ_DATA const char *const lj_obj_typename[1+LUA_TCDATA+1];

968. ‌LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1];

970. ‌#define lj_typename(o)        (lj_obj_itypename[itypemap(o)])

972. /* Compare two objects without calling metamethods. */

973. LJ_FUNC int LJ_FASTCALL lj_obj_equal(cTValue *o1, cTValue *o2);

974. LJ_FUNC const void * LJ_FASTCALL lj_obj_ptr(cTValue *o);

976. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_tab.c - luajit-2.0-src

Functions defined

- bestasize

- clearapart

- clearhpart

- countarray

- counthash

- countint

- hashkey

- hashmask

- keyindex

- lj_tab_clear

- lj_tab_dup

- lj_tab_free

- lj_tab_get

- lj_tab_getinth

- lj_tab_getstr

- lj_tab_len

- lj_tab_new

- lj_tab_new1

- lj_tab_new_ah

- lj_tab_newkey

- lj_tab_next

- lj_tab_reasize

- lj_tab_rehash

- lj_tab_set

- lj_tab_setinth

- lj_tab_setstr

- newhpart

- newtab

- rehashtab

- resizetab

- unbound_search

Macros defined

- LUA_CORE

- hashgcref

- hashgcref

- hashlohi

- hashnum

- hashptr

- hashstr

- lj_tab_c

Source code

1. /*

2. ** Table handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lj_tab_c

10. ‌#define LUA_CORE

12. #include "lj_obj.h"

13. #include "lj_gc.h"

14. #include "lj_err.h"

15. #include "lj_tab.h"

17. /* -- Object hashing ------------------------------------------------------ */

19. /* Hash values are masked with the table hash mask and used as an index. */

20. ‌static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash)

21. {

22. Node *n = noderef(t->node);

23. return &n[hash & t->hmask];

24. }

26. /* String hashes are precomputed when they are interned. */

27. ‌#define hashstr(t, s)                hashmask(t, (s)->hash)

29. ‌#define hashlohi(t, lo, hi)        hashmask((t), hashrot((lo), (hi)))

30. ‌#define hashnum(t, o)                hashlohi((t), (o)->u32.lo, ((o)->u32.hi << 1))

31. ‌#define hashptr(t, p)                hashlohi((t), u32ptr(p), u32ptr(p) + HASH_BIAS)

32. #if LJ_GC64

33. ‌#define hashgcref(t, r) \

34. hashlohi((t), (uint32_t)gcrefu(r), (uint32_t)(gcrefu(r) >> 32))

35. #else

36. ‌#define hashgcref(t, r)                hashlohi((t), gcrefu(r), gcrefu(r) + HASH_BIAS)

37. #endif

39. /* Hash an arbitrary key and return its anchor position in the hash table. */

40. ‌static Node *hashkey(const GCtab *t, cTValue *key)

41. {

42. lua_assert(!tvisint(key));

43. if (tvisstr(key))

44. return hashstr(t, strV(key));

45. else if (tvisnum(key))

46. return hashnum(t, key);

47. else if (tvisbool(key))

48. return hashmask(t, boolV(key));

49. else

50. return hashgcref(t, key->gcr);

51. /* Only hash 32 bits of lightuserdata on a 64 bit CPU. Good enough? */

52. }

54. /* -- Table creation and destruction -------------------------------------- */

56. /* Create new hash part for table. */

57. ‌static LJ_AINLINE void newhpart(lua_State *L, GCtab *t, uint32_t hbits)

58. {

59. uint32_t hsize;

60. Node *node;

61. lua_assert(hbits != 0);

62. if (hbits > LJ_MAX_HBITS)

63. lj_err_msg(L, LJ_ERR_TABOV);

64. hsize = 1u << hbits;

65. node = lj_mem_newvec(L, hsize, Node);

66. setmref(t->node, node);

67. setfreetop(t, node, &node[hsize]);

68. t->hmask = hsize-1;

69. }

71. /*

72. ** Q: Why all of these copies of t->hmask, t->node etc. to local variables?

73. ** A: Because alias analysis for C is _really_ tough.

74. **    Even state-of-the-art C compilers won't produce good code without this.

75. */

77. /* Clear hash part of table. */

78. ‌static LJ_AINLINE void clearhpart(GCtab *t)

79. {

80. uint32_t i, hmask = t->hmask;

81. Node *node = noderef(t->node);

82. lua_assert(t->hmask != 0);

83. for (i = 0; i <= hmask; i++) {

84. Node *n = &node[i];

85. setmref(n->next, NULL);

86. setnilV(&n->key);

87. setnilV(&n->val);

88. }

89. }

91. /* Clear array part of table. */

92. ‌static LJ_AINLINE void clearapart(GCtab *t)

93. {

94. uint32_t i, asize = t->asize;

95. TValue *array = tvref(t->array);

96. for (i = 0; i < asize; i++)

97. setnilV(&array[i]);

98. }

100. /* Create a new table. Note: the slots are not initialized (yet). */

101. ‌static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)

102. {

103. GCtab *t;

104. /* First try to colocate the array part. */

105. if (LJ_MAX_COLOSIZE != 0 && asize > 0 && asize <= LJ_MAX_COLOSIZE) {

106. Node *nilnode;

107. lua_assert((sizeof(GCtab) & 7) == 0);

108. t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize));

109. t->gct = ~LJ_TTAB;

110. t->nomm = (uint8_t)~0;

111. t->colo = (int8_t)asize;

112. setmref(t->array, (TValue *)((char *)t + sizeof(GCtab)));

113. setgcrefnull(t->metatable);

114. t->asize = asize;

115. t->hmask = 0;

116. nilnode = &G(L)->nilnode;

117. setmref(t->node, nilnode);

118. #if LJ_GC64

119. setmref(t->freetop, nilnode);

120. #endif

121. } else {  /* Otherwise separately allocate the array part. */

122. Node *nilnode;

123. t = lj_mem_newobj(L, GCtab);

124. t->gct = ~LJ_TTAB;

125. t->nomm = (uint8_t)~0;

126. t->colo = 0;

127. setmref(t->array, NULL);

128. setgcrefnull(t->metatable);

129. t->asize = 0;  /* In case the array allocation fails. */

130. t->hmask = 0;

131. nilnode = &G(L)->nilnode;

132. setmref(t->node, nilnode);

133. #if LJ_GC64

134. setmref(t->freetop, nilnode);

135. #endif

136. if (asize > 0) {

137. if (asize > LJ_MAX_ASIZE)

138. lj_err_msg(L, LJ_ERR_TABOV);

139. setmref(t->array, lj_mem_newvec(L, asize, TValue));

140. t->asize = asize;

141. }

142. }

143. if (hbits)

144. newhpart(L, t, hbits);

145. return t;

146. }

148. /* Create a new table.

149. **

150. ** IMPORTANT NOTE: The API differs from lua_createtable()!

151. **

152. ** The array size is non-inclusive. E.g. asize=128 creates array slots

153. ** for 0..127, but not for 128. If you need slots 1..128, pass asize=129

154. ** (slot 0 is wasted in this case).

155. **

156. ** The hash size is given in hash bits. hbits=0 means no hash part.

157. ** hbits=1 creates 2 hash slots, hbits=2 creates 4 hash slots and so on.

158. */

159. ‌GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits)

160. {

161. GCtab *t = newtab(L, asize, hbits);

162. clearapart(t);

163. if (t->hmask > 0) clearhpart(t);

164. return t;

165. }

167. /* The API of this function conforms to lua_createtable(). */

168. ‌GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h)

169. {

170. return lj_tab_new(L, (uint32_t)(a > 0 ? a+1 : 0), hsize2hbits(h));

171. }

173. #if LJ_HASJIT

174. ‌GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize)

175. {

176. GCtab *t = newtab(L, ahsize & 0xffffff, ahsize >> 24);

177. clearapart(t);

178. if (t->hmask > 0) clearhpart(t);

179. return t;

180. }

181. #endif

183. /* Duplicate a table. */

184. ‌GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt)

185. {

186. GCtab *t;

187. uint32_t asize, hmask;

188. t = newtab(L, kt->asize, kt->hmask > 0 ? lj_fls(kt->hmask)+1 : 0);

189. lua_assert(kt->asize == t->asize && kt->hmask == t->hmask);

190. t->nomm = 0;  /* Keys with metamethod names may be present. */

191. asize = kt->asize;

192. if (asize > 0) {

193. TValue *array = tvref(t->array);

194. TValue *karray = tvref(kt->array);

195. if (asize < 64) {  /* An inlined loop beats memcpy for < 512 bytes. */

196. uint32_t i;

197. for (i = 0; i < asize; i++)

198. copyTV(L, &array[i], &karray[i]);

199. } else {

200. memcpy(array, karray, asize*sizeof(TValue));

201. }

202. }

203. hmask = kt->hmask;

204. if (hmask > 0) {

205. uint32_t i;

206. Node *node = noderef(t->node);

207. Node *knode = noderef(kt->node);

208. ptrdiff_t d = (char *)node - (char *)knode;

209. setfreetop(t, node, (Node *)((char *)getfreetop(kt, knode) + d));

210. for (i = 0; i <= hmask; i++) {

211. Node *kn = &knode[i];

212. Node *n = &node[i];

213. Node *next = nextnode(kn);

214. /* Don't use copyTV here, since it asserts on a copy of a dead key. */

215. n->val = kn->val; n->key = kn->key;

216. setmref(n->next, next == NULL? next : (Node *)((char *)next + d));

217. }

218. }

219. return t;

220. }

222. /* Clear a table. */

223. ‌void LJ_FASTCALL lj_tab_clear(GCtab *t)

224. {

225. clearapart(t);

226. if (t->hmask > 0) {

227. Node *node = noderef(t->node);

228. setfreetop(t, node, &node[t->hmask+1]);

229. clearhpart(t);

230. }

231. }

233. /* Free a table. */

234. ‌void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t)

235. {

236. if (t->hmask > 0)

237. lj_mem_freevec(g, noderef(t->node), t->hmask+1, Node);

238. if (t->asize > 0 && LJ_MAX_COLOSIZE != 0 && t->colo <= 0)

239. lj_mem_freevec(g, tvref(t->array), t->asize, TValue);

240. if (LJ_MAX_COLOSIZE != 0 && t->colo)

241. lj_mem_free(g, t, sizetabcolo((uint32_t)t->colo & 0x7f));

242. else

243. lj_mem_freet(g, t);

244. }

246. /* -- Table resizing ------------------------------------------------------ */

248. /* Resize a table to fit the new array/hash part sizes. */

249. ‌static void resizetab(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits)

250. {

251. Node *oldnode = noderef(t->node);

252. uint32_t oldasize = t->asize;

253. uint32_t oldhmask = t->hmask;

254. if (asize > oldasize) {  /* Array part grows? */

255. TValue *array;

256. uint32_t i;

257. if (asize > LJ_MAX_ASIZE)

258. lj_err_msg(L, LJ_ERR_TABOV);

259. if (LJ_MAX_COLOSIZE != 0 && t->colo > 0) {

260. /* A colocated array must be separated and copied. */

261. TValue *oarray = tvref(t->array);

262. array = lj_mem_newvec(L, asize, TValue);

263. t->colo = (int8_t)(t->colo | 0x80);  /* Mark as separated (colo < 0). */

264. for (i = 0; i < oldasize; i++)

265. copyTV(L, &array[i], &oarray[i]);

266. } else {

267. array = (TValue *)lj_mem_realloc(L, tvref(t->array),

268. oldasize*sizeof(TValue), asize*sizeof(TValue));

269. }

270. setmref(t->array, array);

271. t->asize = asize;

272. for (i = oldasize; i < asize; i++)  /* Clear newly allocated slots. */

273. setnilV(&array[i]);

274. }

275. /* Create new (empty) hash part. */

276. if (hbits) {

277. newhpart(L, t, hbits);

278. clearhpart(t);

279. } else {

280. global_State *g = G(L);

281. setmref(t->node, &g->nilnode);

282. #if LJ_GC64

283. setmref(t->freetop, &g->nilnode);

284. #endif

285. t->hmask = 0;

286. }

287. if (asize < oldasize) {  /* Array part shrinks? */

288. TValue *array = tvref(t->array);

289. uint32_t i;

290. t->asize = asize;  /* Note: This 'shrinks' even colocated arrays. */

291. for (i = asize; i < oldasize; i++)  /* Reinsert old array values. */

292. if (!tvisnil(&array[i]))

293. copyTV(L, lj_tab_setinth(L, t, (int32_t)i), &array[i]);

294. /* Physically shrink only separated arrays. */

295. if (LJ_MAX_COLOSIZE != 0 && t->colo <= 0)

296. setmref(t->array, lj_mem_realloc(L, array,

297. oldasize*sizeof(TValue), asize*sizeof(TValue)));

298. }

299. if (oldhmask > 0) {  /* Reinsert pairs from old hash part. */

300. global_State *g;

301. uint32_t i;

302. for (i = 0; i <= oldhmask; i++) {

303. Node *n = &oldnode[i];

304. if (!tvisnil(&n->val))

305. copyTV(L, lj_tab_set(L, t, &n->key), &n->val);

306. }

307. g = G(L);

308. lj_mem_freevec(g, oldnode, oldhmask+1, Node);

309. }

310. }

312. ‌static uint32_t countint(cTValue *key, uint32_t *bins)

313. {

314. lua_assert(!tvisint(key));

315. if (tvisnum(key)) {

316. lua_Number nk = numV(key);

317. int32_t k = lj_num2int(nk);

318. if ((uint32_t)k < LJ_MAX_ASIZE && nk == (lua_Number)k) {

319. bins[(k > 2 ? lj_fls((uint32_t)(k-1)) : 0)]++;

320. return 1;

321. }

322. }

323. return 0;

324. }

326. ‌static uint32_t countarray(const GCtab *t, uint32_t *bins)

327. {

328. uint32_t na, b, i;

329. if (t->asize == 0) return 0;

330. for (na = i = b = 0; b < LJ_MAX_ABITS; b++) {

331. uint32_t n, top = 2u << b;

332. TValue *array;

333. if (top >= t->asize) {

334. top = t->asize-1;

335. if (i > top)

336. break;

337. }

338. array = tvref(t->array);

339. for (n = 0; i <= top; i++)

340. if (!tvisnil(&array[i]))

341. n++;

342. bins[b] += n;

343. na += n;

344. }

345. return na;

346. }

348. ‌static uint32_t counthash(const GCtab *t, uint32_t *bins, uint32_t *narray)

349. {

350. uint32_t total, na, i, hmask = t->hmask;

351. Node *node = noderef(t->node);

352. for (total = na = 0, i = 0; i <= hmask; i++) {

353. Node *n = &node[i];

354. if (!tvisnil(&n->val)) {

355. na += countint(&n->key, bins);

356. total++;

357. }

358. }

359. *narray += na;

360. return total;

361. }

363. ‌static uint32_t bestasize(uint32_t bins[], uint32_t *narray)

364. {

365. uint32_t b, sum, na = 0, sz = 0, nn = *narray;

366. for (b = 0, sum = 0; 2*nn > (1u<<b) && sum != nn; b++)

367. if (bins[b] > 0 && 2*(sum += bins[b]) > (1u<<b)) {

368. sz = (2u<<b)+1;

369. na = sum;

370. }

371. *narray = sz;

372. return na;

373. }

375. ‌static void rehashtab(lua_State *L, GCtab *t, cTValue *ek)

376. {

377. uint32_t bins[LJ_MAX_ABITS];

378. uint32_t total, asize, na, i;

379. for (i = 0; i < LJ_MAX_ABITS; i++) bins[i] = 0;

380. asize = countarray(t, bins);

381. total = 1 + asize;

382. total += counthash(t, bins, &asize);

383. asize += countint(ek, bins);

384. na = bestasize(bins, &asize);

385. total -= na;

386. resizetab(L, t, asize, hsize2hbits(total));

387. }

389. #if LJ_HASFFI

390. ‌void lj_tab_rehash(lua_State *L, GCtab *t)

391. {

392. rehashtab(L, t, niltv(L));

393. }

394. #endif

396. ‌void lj_tab_reasize(lua_State *L, GCtab *t, uint32_t nasize)

397. {

398. resizetab(L, t, nasize+1, t->hmask > 0 ? lj_fls(t->hmask)+1 : 0);

399. }

401. /* -- Table getters ------------------------------------------------------- */

403. ‌cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key)

404. {

405. TValue k;

406. Node *n;

407. k.n = (lua_Number)key;

408. n = hashnum(t, &k);

409. do {

410. if (tvisnum(&n->key) && n->key.n == k.n)

411. return &n->val;

412. } while ((n = nextnode(n)));

413. return NULL;

414. }

416. ‌cTValue *lj_tab_getstr(GCtab *t, GCstr *key)

417. {

418. Node *n = hashstr(t, key);

419. do {

420. if (tvisstr(&n->key) && strV(&n->key) == key)

421. return &n->val;

422. } while ((n = nextnode(n)));

423. return NULL;

424. }

426. ‌cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key)

427. {

428. if (tvisstr(key)) {

429. cTValue *tv = lj_tab_getstr(t, strV(key));

430. if (tv)

431. return tv;

432. } else if (tvisint(key)) {

433. cTValue *tv = lj_tab_getint(t, intV(key));

434. if (tv)

435. return tv;

436. } else if (tvisnum(key)) {

437. lua_Number nk = numV(key);

438. int32_t k = lj_num2int(nk);

439. if (nk == (lua_Number)k) {

440. cTValue *tv = lj_tab_getint(t, k);

441. if (tv)

442. return tv;

443. } else {

444. goto genlookup;  /* Else use the generic lookup. */

445. }

446. } else if (!tvisnil(key)) {

447. Node *n;

448. genlookup:

449. n = hashkey(t, key);

450. do {

451. if (lj_obj_equal(&n->key, key))

452. return &n->val;

453. } while ((n = nextnode(n)));

454. }

455. return niltv(L);

456. }

458. /* -- Table setters ------------------------------------------------------- */

460. /* Insert new key. Use Brent's variation to optimize the chain length. */

461. ‌TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key)

462. {

463. Node *n = hashkey(t, key);

464. if (!tvisnil(&n->val) || t->hmask == 0) {

465. Node *nodebase = noderef(t->node);

466. Node *collide, *freenode = getfreetop(t, nodebase);

467. lua_assert(freenode >= nodebase && freenode <= nodebase+t->hmask+1);

468. do {

469. if (freenode == nodebase) {  /* No free node found? */

470. rehashtab(L, t, key);  /* Rehash table. */

471. return lj_tab_set(L, t, key);  /* Retry key insertion. */

472. }

473. } while (!tvisnil(&(--freenode)->key));

474. setfreetop(t, nodebase, freenode);

475. lua_assert(freenode != &G(L)->nilnode);

476. collide = hashkey(t, &n->key);

477. if (collide != n) {  /* Colliding node not the main node? */

478. while (noderef(collide->next) != n)  /* Find predecessor. */

479. collide = nextnode(collide);

480. setmref(collide->next, freenode);  /* Relink chain. */

481. /* Copy colliding node into free node and free main node. */

482. freenode->val = n->val;

483. freenode->key = n->key;

484. freenode->next = n->next;

485. setmref(n->next, NULL);

486. setnilV(&n->val);

487. /* Rechain pseudo-resurrected string keys with colliding hashes. */

488. while (nextnode(freenode)) {

489. Node *nn = nextnode(freenode);

490. if (tvisstr(&nn->key) && !tvisnil(&nn->val) &&

491. hashstr(t, strV(&nn->key)) == n) {

492. freenode->next = nn->next;

493. nn->next = n->next;

494. setmref(n->next, nn);

495. } else {

496. freenode = nn;

497. }

498. }

499. } else {  /* Otherwise use free node. */

500. setmrefr(freenode->next, n->next);  /* Insert into chain. */

501. setmref(n->next, freenode);

502. n = freenode;

503. }

504. }

505. n->key.u64 = key->u64;

506. if (LJ_UNLIKELY(tvismzero(&n->key)))

507. n->key.u64 = 0;

508. lj_gc_anybarriert(L, t);

509. lua_assert(tvisnil(&n->val));

510. return &n->val;

511. }

513. ‌TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key)

514. {

515. TValue k;

516. Node *n;

517. k.n = (lua_Number)key;

518. n = hashnum(t, &k);

519. do {

520. if (tvisnum(&n->key) && n->key.n == k.n)

521. return &n->val;

522. } while ((n = nextnode(n)));

523. return lj_tab_newkey(L, t, &k);

524. }

526. ‌TValue *lj_tab_setstr(lua_State *L, GCtab *t, GCstr *key)

527. {

528. TValue k;

529. Node *n = hashstr(t, key);

530. do {

531. if (tvisstr(&n->key) && strV(&n->key) == key)

532. return &n->val;

533. } while ((n = nextnode(n)));

534. setstrV(L, &k, key);

535. return lj_tab_newkey(L, t, &k);

536. }

538. ‌TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key)

539. {

540. Node *n;

541. t->nomm = 0;  /* Invalidate negative metamethod cache. */

542. if (tvisstr(key)) {

543. return lj_tab_setstr(L, t, strV(key));

544. } else if (tvisint(key)) {

545. return lj_tab_setint(L, t, intV(key));

546. } else if (tvisnum(key)) {

547. lua_Number nk = numV(key);

548. int32_t k = lj_num2int(nk);

549. if (nk == (lua_Number)k)

550. return lj_tab_setint(L, t, k);

551. if (tvisnan(key))

552. lj_err_msg(L, LJ_ERR_NANIDX);

553. /* Else use the generic lookup. */

554. } else if (tvisnil(key)) {

555. lj_err_msg(L, LJ_ERR_NILIDX);

556. }

557. n = hashkey(t, key);

558. do {

559. if (lj_obj_equal(&n->key, key))

560. return &n->val;

561. } while ((n = nextnode(n)));

562. return lj_tab_newkey(L, t, key);

563. }

565. /* -- Table traversal ----------------------------------------------------- */

567. /* Get the traversal index of a key. */

568. ‌static uint32_t keyindex(lua_State *L, GCtab *t, cTValue *key)

569. {

570. TValue tmp;

571. if (tvisint(key)) {

572. int32_t k = intV(key);

573. if ((uint32_t)k < t->asize)

574. return (uint32_t)k;  /* Array key indexes: [0..t->asize-1] */

575. setnumV(&tmp, (lua_Number)k);

576. key = &tmp;

577. } else if (tvisnum(key)) {

578. lua_Number nk = numV(key);

579. int32_t k = lj_num2int(nk);

580. if ((uint32_t)k < t->asize && nk == (lua_Number)k)

581. return (uint32_t)k;  /* Array key indexes: [0..t->asize-1] */

582. }

583. if (!tvisnil(key)) {

584. Node *n = hashkey(t, key);

585. do {

586. if (lj_obj_equal(&n->key, key))

587. return t->asize + (uint32_t)(n - noderef(t->node));

588. /* Hash key indexes: [t->asize..t->asize+t->nmask] */

589. } while ((n = nextnode(n)));

590. if (key->u32.hi == 0xfffe7fff)  /* ITERN was despecialized while running. */

591. return key->u32.lo - 1;

592. lj_err_msg(L, LJ_ERR_NEXTIDX);

593. return 0;  /* unreachable */

594. }

595. return ~0u;  /* A nil key starts the traversal. */

596. }

598. /* Advance to the next step in a table traversal. */

599. ‌int lj_tab_next(lua_State *L, GCtab *t, TValue *key)

600. {

601. uint32_t i = keyindex(L, t, key);  /* Find predecessor key index. */

602. for (i++; i < t->asize; i++)  /* First traverse the array keys. */

603. if (!tvisnil(arrayslot(t, i))) {

604. setintV(key, i);

605. copyTV(L, key+1, arrayslot(t, i));

606. return 1;

607. }

608. for (i -= t->asize; i <= t->hmask; i++) {  /* Then traverse the hash keys. */

609. Node *n = &noderef(t->node)[i];

610. if (!tvisnil(&n->val)) {

611. copyTV(L, key, &n->key);

612. copyTV(L, key+1, &n->val);

613. return 1;

614. }

615. }

616. return 0;  /* End of traversal. */

617. }

619. /* -- Table length calculation -------------------------------------------- */

621. ‌static MSize unbound_search(GCtab *t, MSize j)

622. {

623. cTValue *tv;

624. MSize i = j;  /* i is zero or a present index */

625. j++;

626. /* find `i' and `j' such that i is present and j is not */

627. while ((tv = lj_tab_getint(t, (int32_t)j)) && !tvisnil(tv)) {

628. i = j;

629. j *= 2;

630. if (j > (MSize)(INT_MAX-2)) {  /* overflow? */

631. /* table was built with bad purposes: resort to linear search */

632. i = 1;

633. while ((tv = lj_tab_getint(t, (int32_t)i)) && !tvisnil(tv)) i++;

634. return i - 1;

635. }

636. }

637. /* now do a binary search between them */

638. while (j - i > 1) {

639. MSize m = (i+j)/2;

640. cTValue *tvb = lj_tab_getint(t, (int32_t)m);

641. if (tvb && !tvisnil(tvb)) i = m; else j = m;

642. }

643. return i;

644. }

646. /*

647. ** Try to find a boundary in table `t'. A `boundary' is an integer index

648. ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).

649. */

650. ‌MSize LJ_FASTCALL lj_tab_len(GCtab *t)

651. {

652. MSize j = (MSize)t->asize;

653. if (j > 1 && tvisnil(arrayslot(t, j-1))) {

654. MSize i = 1;

655. while (j - i > 1) {

656. MSize m = (i+j)/2;

657. if (tvisnil(arrayslot(t, m-1))) j = m; else i = m;

658. }

659. return i-1;

660. }

661. if (j) j--;

662. if (t->hmask <= 0)

663. return j;

664. return unbound_search(t, j);

665. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_tab.h - luajit-2.0-src

Functions defined

- hashrot

Macros defined

- HASH_BIAS

- HASH_ROT1

- HASH_ROT2

- HASH_ROT3

- _LJ_TAB_H

- arrayslot

- hsize2hbits

- inarray

- lj_tab_getint

- lj_tab_setint

Source code

1. /*

2. ** Table handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_TAB_H

7. ‌#define _LJ_TAB_H

9. #include "lj_obj.h"

11. /* Hash constants. Tuned using a brute force search. */

12. ‌#define HASH_BIAS        (-0x04c11db7)

13. ‌#define HASH_ROT1        14

14. ‌#define HASH_ROT2        5

15. ‌#define HASH_ROT3        13

17. /* Scramble the bits of numbers and pointers. */

18. ‌static LJ_AINLINE uint32_t hashrot(uint32_t lo, uint32_t hi)

19. {

20. #if LJ_TARGET_X86ORX64

21. /* Prefer variant that compiles well for a 2-operand CPU. */

22. lo ^= hi; hi = lj_rol(hi, HASH_ROT1);

23. lo -= hi; hi = lj_rol(hi, HASH_ROT2);

24. hi ^= lo; hi -= lj_rol(lo, HASH_ROT3);

25. #else

26. lo ^= hi;

27. lo = lo - lj_rol(hi, HASH_ROT1);

28. hi = lo ^ lj_rol(hi, HASH_ROT1 + HASH_ROT2);

29. hi = hi - lj_rol(lo, HASH_ROT3);

30. #endif

31. return hi;

32. }

34. ‌#define hsize2hbits(s)        ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0)

36. LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits);

37. LJ_FUNC GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h);

38. #if LJ_HASJIT

39. LJ_FUNC GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize);

40. #endif

41. LJ_FUNCA GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt);

42. LJ_FUNC void LJ_FASTCALL lj_tab_clear(GCtab *t);

43. LJ_FUNC void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t);

44. #if LJ_HASFFI

45. LJ_FUNC void lj_tab_rehash(lua_State *L, GCtab *t);

46. #endif

47. LJ_FUNCA void lj_tab_reasize(lua_State *L, GCtab *t, uint32_t nasize);

49. /* Caveat: all getters except lj_tab_get() can return NULL! */

51. LJ_FUNCA cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key);

52. LJ_FUNC cTValue *lj_tab_getstr(GCtab *t, GCstr *key);

53. LJ_FUNCA cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key);

55. /* Caveat: all setters require a write barrier for the stored value. */

57. LJ_FUNCA TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key);

58. LJ_FUNCA TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key);

59. LJ_FUNC TValue *lj_tab_setstr(lua_State *L, GCtab *t, GCstr *key);

60. LJ_FUNC TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key);

62. ‌#define inarray(t, key)                ((MSize)(key) < (MSize)(t)->asize)

63. ‌#define arrayslot(t, i)                (&tvref((t)->array)[(i)])

64. ‌#define lj_tab_getint(t, key) \

65. (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_getinth((t), (key)))

66. ‌#define lj_tab_setint(L, t, key) \

67. (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_setinth(L, (t), (key)))

69. LJ_FUNCA int lj_tab_next(lua_State *L, GCtab *t, TValue *key);

70. LJ_FUNCA MSize LJ_FASTCALL lj_tab_len(GCtab *t);

72. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_err.c - luajit-2.0-src

Global variables defined

- lj_err_allmsg

- static_uex

- static_uex

Data types defined

- UndocumentedDispatcherContext

- UndocumentedDispatcherContext

- _Unwind_Context

- _Unwind_Exception

- _Unwind_Exception

Functions defined

- RtlUnwindEx_FIXED

- _Unwind_GetGR

- _Unwind_SetGR

- err_argmsg

- err_msgv

- err_raise_ext

- err_raise_ext

- err_unwind

- finderrfunc

- lj_err_arg

- lj_err_argt

- lj_err_argtype

- lj_err_argv

- lj_err_caller

- lj_err_callermsg

- lj_err_callerv

- lj_err_comp

- lj_err_lex

- lj_err_mem

- lj_err_msg

- lj_err_optype

- lj_err_optype_call

- lj_err_run

- lj_err_str

- lj_err_throw

- lj_err_unwind_arm

- lj_err_unwind_dwarf

- luaL_argerror

- luaL_error

- luaL_typerror

- luaL_where

- lua_atpanic

- lua_error

- unwindstack

Macros defined

- ERRDEF

- LJ_EXCODE

- LJ_EXCODE_CHECK

- LJ_EXCODE_ERRCODE

- LJ_EXCODE_MAKE

- LJ_GCC_EXCODE

- LJ_MSVC_EXCODE

- LJ_UEXCLASS

- LJ_UEXCLASS_CHECK

- LJ_UEXCLASS_ERRCODE

- LJ_UEXCLASS_MAKE

- LJ_UNWIND_EXT

- LJ_UNWIND_EXT

- LUA_CORE

- RtlUnwindEx

- WIN32_LEAN_AND_MEAN

- _UA_CLEANUP_PHASE

- _UA_FORCE_UNWIND

- _UA_HANDLER_FRAME

- _UA_SEARCH_PHASE

- _URC_CONTINUE_UNWIND

- _URC_FAILURE

- _URC_FATAL_PHASE1_ERROR

- _URC_HANDLER_FOUND

- _URC_INSTALL_CONTEXT

- _URC_OK

- _US_ACTION_MASK

- _US_FORCE_UNWIND

- _US_UNWIND_FRAME_STARTING

- _US_VIRTUAL_UNWIND_FRAME

- lj_err_c

Source code

1. /*

2. ** Error handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_err_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #include "lj_err.h"

11. #include "lj_debug.h"

12. #include "lj_str.h"

13. #include "lj_func.h"

14. #include "lj_state.h"

15. #include "lj_frame.h"

16. #include "lj_ff.h"

17. #include "lj_trace.h"

18. #include "lj_vm.h"

19. #include "lj_strfmt.h"

21. /*

22. ** LuaJIT can either use internal or external frame unwinding:

23. **

24. ** - Internal frame unwinding (INT) is free-standing and doesn't require

25. **   any OS or library support.

26. **

27. ** - External frame unwinding (EXT) uses the system-provided unwind handler.

28. **

29. ** Pros and Cons:

30. **

31. ** - EXT requires unwind tables for *all* functions on the C stack between

32. **   the pcall/catch and the error/throw. This is the default on x64,

33. **   but needs to be manually enabled on x86/PPC for non-C++ code.

34. **

35. ** - INT is faster when actually throwing errors (but this happens rarely).

36. **   Setting up error handlers is zero-cost in any case.

37. **

38. ** - EXT provides full interoperability with C++ exceptions. You can throw

39. **   Lua errors or C++ exceptions through a mix of Lua frames and C++ frames.

40. **   C++ destructors are called as needed. C++ exceptions caught by pcall

41. **   are converted to the string "C++ exception". Lua errors can be caught

42. **   with catch (...) in C++.

43. **

44. ** - INT has only limited support for automatically catching C++ exceptions

45. **   on POSIX systems using DWARF2 stack unwinding. Other systems may use

46. **   the wrapper function feature. Lua errors thrown through C++ frames

47. **   cannot be caught by C++ code and C++ destructors are not run.

48. **

49. ** EXT is the default on x64 systems, INT is the default on all other systems.

50. **

51. ** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack

52. ** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled

53. ** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set

54. ** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules

55. ** and all C libraries that have callbacks which may be used to call back

56. ** into Lua. C++ code must *not* be compiled with -fno-exceptions.

57. **

58. ** EXT cannot be enabled on WIN32 since system exceptions use code-driven SEH.

59. ** EXT is mandatory on WIN64 since the calling convention has an abundance

60. ** of callee-saved registers (rbx, rbp, rsi, rdi, r12-r15, xmm6-xmm15).

61. ** EXT is mandatory on POSIX/x64 since the interpreter doesn't save r12/r13.

62. */

64. #if defined(__GNUC__) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL))

65. ‌#define LJ_UNWIND_EXT        1

66. #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS

67. ‌#define LJ_UNWIND_EXT        1

68. #endif

70. /* -- Error messages ------------------------------------------------------ */

72. /* Error message strings. */

73. ‌LJ_DATADEF const char *lj_err_allmsg =

74. ‌#define ERRDEF(name, msg)        msg "\0"

75. #include "lj_errmsg.h"

76. ;

78. /* -- Internal frame unwinding -------------------------------------------- */

80. /* Unwind Lua stack and move error message to new top. */

81. ‌LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top)

82. {

83. lj_func_closeuv(L, top);

84. if (top < L->top-1) {

85. copyTV(L, top, L->top-1);

86. L->top = top+1;

87. }

88. lj_state_relimitstack(L);

89. }

91. /* Unwind until stop frame. Optionally cleanup frames. */

92. ‌static void *err_unwind(lua_State *L, void *stopcf, int errcode)

93. {

94. TValue *frame = L->base-1;

95. void *cf = L->cframe;

96. while (cf) {

97. int32_t nres = cframe_nres(cframe_raw(cf));

98. if (nres < 0) {  /* C frame without Lua frame? */

99. TValue *top = restorestack(L, -nres);

100. if (frame < top) {  /* Frame reached? */

101. if (errcode) {

102. L->base = frame+1;

103. L->cframe = cframe_prev(cf);

104. unwindstack(L, top);

105. }

106. return cf;

107. }

108. }

109. if (frame <= tvref(L->stack)+LJ_FR2)

110. break;

111. switch (frame_typep(frame)) {

112. case FRAME_LUA:  /* Lua frame. */

113. case FRAME_LUAP:

114. frame = frame_prevl(frame);

115. break;

116. case FRAME_C:  /* C frame. */

117. unwind_c:

118. #if LJ_UNWIND_EXT

119. if (errcode) {

120. L->base = frame_prevd(frame) + 1;

121. L->cframe = cframe_prev(cf);

122. unwindstack(L, frame);

123. } else if (cf != stopcf) {

124. cf = cframe_prev(cf);

125. frame = frame_prevd(frame);

126. break;

127. }

128. return NULL;  /* Continue unwinding. */

129. #else

130. UNUSED(stopcf);

131. cf = cframe_prev(cf);

132. frame = frame_prevd(frame);

133. break;

134. #endif

135. case FRAME_CP:  /* Protected C frame. */

136. if (cframe_canyield(cf)) {  /* Resume? */

137. if (errcode) {

138. hook_leave(G(L));  /* Assumes nobody uses coroutines inside hooks. */

139. L->cframe = NULL;

140. L->status = (uint8_t)errcode;

141. }

142. return cf;

143. }

144. if (errcode) {

145. L->base = frame_prevd(frame) + 1;

146. L->cframe = cframe_prev(cf);

147. unwindstack(L, frame);

148. }

149. return cf;

150. case FRAME_CONT:  /* Continuation frame. */

151. if (frame_iscont_fficb(frame))

152. goto unwind_c;

153. case FRAME_VARG:  /* Vararg frame. */

154. frame = frame_prevd(frame);

155. break;

156. case FRAME_PCALL:  /* FF pcall() frame. */

157. case FRAME_PCALLH:  /* FF pcall() frame inside hook. */

158. if (errcode) {

159. if (errcode == LUA_YIELD) {

160. frame = frame_prevd(frame);

161. break;

162. }

163. if (frame_typep(frame) == FRAME_PCALL)

164. hook_leave(G(L));

165. L->base = frame_prevd(frame) + 1;

166. L->cframe = cf;

167. unwindstack(L, L->base);

168. }

169. return (void *)((intptr_t)cf | CFRAME_UNWIND_FF);

170. }

171. }

172. /* No C frame. */

173. if (errcode) {

174. L->base = tvref(L->stack)+1+LJ_FR2;

175. L->cframe = NULL;

176. unwindstack(L, L->base);

177. if (G(L)->panic)

178. G(L)->panic(L);

179. exit(EXIT_FAILURE);

180. }

181. return L;  /* Anything non-NULL will do. */

182. }

184. /* -- External frame unwinding -------------------------------------------- */

186. #if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_TARGET_WINDOWS

188. /*

189. ** We have to use our own definitions instead of the mandatory (!) unwind.h,

190. ** since various OS, distros and compilers mess up the header installation.

191. */

193. ‌typedef struct _Unwind_Exception

194. {

195. uint64_t exclass;

196. void (*excleanup)(int, struct _Unwind_Exception *);

197. uintptr_t p1, p2;

198. ‌} __attribute__((__aligned__)) _Unwind_Exception;

200. ‌typedef struct _Unwind_Context _Unwind_Context;

202. ‌#define _URC_OK                        0

203. ‌#define _URC_FATAL_PHASE1_ERROR        3

204. ‌#define _URC_HANDLER_FOUND        6

205. ‌#define _URC_INSTALL_CONTEXT        7

206. ‌#define _URC_CONTINUE_UNWIND        8

207. ‌#define _URC_FAILURE                9

209. #if !LJ_TARGET_ARM

211. extern uintptr_t _Unwind_GetCFA(_Unwind_Context *);

212. extern void _Unwind_SetGR(_Unwind_Context *, int, uintptr_t);

213. extern void _Unwind_SetIP(_Unwind_Context *, uintptr_t);

214. extern void _Unwind_DeleteException(_Unwind_Exception *);

215. extern int _Unwind_RaiseException(_Unwind_Exception *);

217. ‌#define _UA_SEARCH_PHASE        1

218. ‌#define _UA_CLEANUP_PHASE        2

219. ‌#define _UA_HANDLER_FRAME        4

220. ‌#define _UA_FORCE_UNWIND        8

222. ‌#define LJ_UEXCLASS                0x4c55414a49543200ULL        /* LUAJIT2\0 */

223. ‌#define LJ_UEXCLASS_MAKE(c)        (LJ_UEXCLASS | (uint64_t)(c))

224. ‌#define LJ_UEXCLASS_CHECK(cl)        (((cl) ^ LJ_UEXCLASS) <= 0xff)

225. ‌#define LJ_UEXCLASS_ERRCODE(cl)        ((int)((cl) & 0xff))

227. /* DWARF2 personality handler referenced from interpreter .eh_frame. */

228. ‌LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions,

229. uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx)

230. {

231. void *cf;

232. lua_State *L;

233. if (version != 1)

234. return _URC_FATAL_PHASE1_ERROR;

235. UNUSED(uexclass);

236. cf = (void *)_Unwind_GetCFA(ctx);

237. L = cframe_L(cf);

238. if ((actions & _UA_SEARCH_PHASE)) {

239. #if LJ_UNWIND_EXT

240. if (err_unwind(L, cf, 0) == NULL)

241. return _URC_CONTINUE_UNWIND;

242. #endif

243. if (!LJ_UEXCLASS_CHECK(uexclass)) {

244. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));

245. }

246. return _URC_HANDLER_FOUND;

247. }

248. if ((actions & _UA_CLEANUP_PHASE)) {

249. int errcode;

250. if (LJ_UEXCLASS_CHECK(uexclass)) {

251. errcode = LJ_UEXCLASS_ERRCODE(uexclass);

252. } else {

253. if ((actions & _UA_HANDLER_FRAME))

254. _Unwind_DeleteException(uex);

255. errcode = LUA_ERRRUN;

256. }

257. #if LJ_UNWIND_EXT

258. cf = err_unwind(L, cf, errcode);

259. if ((actions & _UA_FORCE_UNWIND)) {

260. return _URC_CONTINUE_UNWIND;

261. } else if (cf) {

262. _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);

263. _Unwind_SetIP(ctx, (uintptr_t)(cframe_unwind_ff(cf) ?

264. lj_vm_unwind_ff_eh :

265. lj_vm_unwind_c_eh));

266. return _URC_INSTALL_CONTEXT;

267. }

268. #if LJ_TARGET_X86ORX64

269. else if ((actions & _UA_HANDLER_FRAME)) {

270. /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/

271. ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h

272. */

273. _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);

274. _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow);

275. return _URC_INSTALL_CONTEXT;

276. }

277. #endif

278. #else

279. /* This is not the proper way to escape from the unwinder. We get away with

280. ** it on non-x64 because the interpreter restores all callee-saved regs.

281. */

282. lj_err_throw(L, errcode);

283. #endif

284. }

285. return _URC_CONTINUE_UNWIND;

286. }

288. #if LJ_UNWIND_EXT

289. #if LJ_TARGET_OSX || defined(__OpenBSD__)

290. /* Sorry, no thread safety for OSX. Complain to Apple, not me. */

291. ‌static _Unwind_Exception static_uex;

292. #else

293. ‌static __thread _Unwind_Exception static_uex;

294. #endif

296. /* Raise DWARF2 exception. */

297. ‌static void err_raise_ext(int errcode)

298. {

299. static_uex.exclass = LJ_UEXCLASS_MAKE(errcode);

300. static_uex.excleanup = NULL;

301. _Unwind_RaiseException(&static_uex);

302. }

303. #endif

305. #else

307. extern void _Unwind_DeleteException(void *);

308. extern int __gnu_unwind_frame (void *, _Unwind_Context *);

309. extern int _Unwind_VRS_Set(_Unwind_Context *, int, uint32_t, int, void *);

310. extern int _Unwind_VRS_Get(_Unwind_Context *, int, uint32_t, int, void *);

312. ‌static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r)

313. {

314. uint32_t v;

315. _Unwind_VRS_Get(ctx, 0, r, 0, &v);

316. return v;

317. }

319. ‌static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v)

320. {

321. _Unwind_VRS_Set(ctx, 0, r, 0, &v);

322. }

324. ‌#define _US_VIRTUAL_UNWIND_FRAME        0

325. ‌#define _US_UNWIND_FRAME_STARTING        1

326. ‌#define _US_ACTION_MASK                        3

327. ‌#define _US_FORCE_UNWIND                8

329. /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */

330. ‌LJ_FUNCA int lj_err_unwind_arm(int state, void *ucb, _Unwind_Context *ctx)

331. {

332. void *cf = (void *)_Unwind_GetGR(ctx, 13);

333. lua_State *L = cframe_L(cf);

334. if ((state & _US_ACTION_MASK) == _US_VIRTUAL_UNWIND_FRAME) {

335. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));

336. return _URC_HANDLER_FOUND;

337. }

338. if ((state&(_US_ACTION_MASK|_US_FORCE_UNWIND)) == _US_UNWIND_FRAME_STARTING) {

339. _Unwind_DeleteException(ucb);

340. _Unwind_SetGR(ctx, 15, (uint32_t)(void *)lj_err_throw);

341. _Unwind_SetGR(ctx, 0, (uint32_t)L);

342. _Unwind_SetGR(ctx, 1, (uint32_t)LUA_ERRRUN);

343. return _URC_INSTALL_CONTEXT;

344. }

345. if (__gnu_unwind_frame(ucb, ctx) != _URC_OK)

346. return _URC_FAILURE;

347. return _URC_CONTINUE_UNWIND;

348. }

350. #endif

352. #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS

354. /*

355. ** Someone in Redmond owes me several days of my life. A lot of this is

356. ** undocumented or just plain wrong on MSDN. Some of it can be gathered

357. ** from 3rd party docs or must be found by trial-and-error. They really

358. ** don't want you to write your own language-specific exception handler

359. ** or to interact gracefully with MSVC. :-(

360. **

361. ** Apparently MSVC doesn't call C++ destructors for foreign exceptions

362. ** unless you compile your C++ code with /EHa. Unfortunately this means

363. ** catch (...) also catches things like access violations. The use of

364. ** _set_se_translator doesn't really help, because it requires /EHa, too.

365. */

367. ‌#define WIN32_LEAN_AND_MEAN

368. #include <windows.h>

370. /* Taken from: http://www.nynaeve.net/?p=99 */

371. ‌typedef struct UndocumentedDispatcherContext {

372. ULONG64 ControlPc;

373. ULONG64 ImageBase;

374. PRUNTIME_FUNCTION FunctionEntry;

375. ULONG64 EstablisherFrame;

376. ULONG64 TargetIp;

377. PCONTEXT ContextRecord;

378. void (*LanguageHandler)(void);

379. PVOID HandlerData;

380. PUNWIND_HISTORY_TABLE HistoryTable;

381. ULONG ScopeIndex;

382. ULONG Fill0;

383. ‌} UndocumentedDispatcherContext;

385. /* Another wild guess. */

386. extern void __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow);

388. #ifdef MINGW_SDK_INIT

389. /* Workaround for broken MinGW64 declaration. */

390. ‌VOID RtlUnwindEx_FIXED(PVOID,PVOID,PVOID,PVOID,PVOID,PVOID) asm("RtlUnwindEx");

391. ‌#define RtlUnwindEx RtlUnwindEx_FIXED

392. #endif

394. ‌#define LJ_MSVC_EXCODE                ((DWORD)0xe06d7363)

395. ‌#define LJ_GCC_EXCODE                ((DWORD)0x20474343)

397. ‌#define LJ_EXCODE                ((DWORD)0xe24c4a00)

398. ‌#define LJ_EXCODE_MAKE(c)        (LJ_EXCODE | (DWORD)(c))

399. ‌#define LJ_EXCODE_CHECK(cl)        (((cl) ^ LJ_EXCODE) <= 0xff)

400. ‌#define LJ_EXCODE_ERRCODE(cl)        ((int)((cl) & 0xff))

402. /* Win64 exception handler for interpreter frame. */

403. LJ_FUNCA EXCEPTION_DISPOSITION lj_err_unwind_win64(EXCEPTION_RECORD *rec,

404. void *cf, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch)

405. {

406. lua_State *L = cframe_L(cf);

407. int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ?

408. LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN;

409. if ((rec->ExceptionFlags & 6)) {  /* EH_UNWINDING|EH_EXIT_UNWIND */

410. /* Unwind internal frames. */

411. err_unwind(L, cf, errcode);

412. } else {

413. void *cf2 = err_unwind(L, cf, 0);

414. if (cf2) {  /* We catch it, so start unwinding the upper frames. */

415. if (rec->ExceptionCode == LJ_MSVC_EXCODE ||

416. rec->ExceptionCode == LJ_GCC_EXCODE) {

417. __DestructExceptionObject(rec, 1);

418. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));

419. } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) {

420. /* Don't catch access violations etc. */

421. return ExceptionContinueSearch;

422. }

423. /* Unwind the stack and call all handlers for all lower C frames

424. ** (including ourselves) again with EH_UNWINDING set. Then set

425. ** rsp = cf, rax = errcode and jump to the specified target.

426. */

427. RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?

428. lj_vm_unwind_ff_eh :

429. lj_vm_unwind_c_eh),

430. rec, (void *)(uintptr_t)errcode, ctx, dispatch->HistoryTable);

431. /* RtlUnwindEx should never return. */

432. }

433. }

434. return ExceptionContinueSearch;

435. }

437. /* Raise Windows exception. */

438. ‌static void err_raise_ext(int errcode)

439. {

440. RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL);

441. }

443. #endif

445. /* -- Error handling ------------------------------------------------------ */

447. /* Throw error. Find catch frame, unwind stack and continue. */

448. ‌LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)

449. {

450. global_State *g = G(L);

451. lj_trace_abort(g);

452. setmref(g->jit_base, NULL);

453. L->status = 0;

454. #if LJ_UNWIND_EXT

455. err_raise_ext(errcode);

456. /*

457. ** A return from this function signals a corrupt C stack that cannot be

458. ** unwound. We have no choice but to call the panic function and exit.

459. **

460. ** Usually this is caused by a C function without unwind information.

461. ** This should never happen on x64, but may happen if you've manually

462. ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every*

463. ** non-C++ file with -funwind-tables.

464. */

465. if (G(L)->panic)

466. G(L)->panic(L);

467. #else

468. {

469. void *cf = err_unwind(L, NULL, errcode);

470. if (cframe_unwind_ff(cf))

471. lj_vm_unwind_ff(cframe_raw(cf));

472. else

473. lj_vm_unwind_c(cframe_raw(cf), errcode);

474. }

475. #endif

476. exit(EXIT_FAILURE);

477. }

479. /* Return string object for error message. */

480. ‌LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em)

481. {

482. return lj_str_newz(L, err2msg(em));

483. }

485. /* Out-of-memory error. */

486. ‌LJ_NOINLINE void lj_err_mem(lua_State *L)

487. {

488. if (L->status == LUA_ERRERR+1)  /* Don't touch the stack during lua_open. */

489. lj_vm_unwind_c(L->cframe, LUA_ERRMEM);

490. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM));

491. lj_err_throw(L, LUA_ERRMEM);

492. }

494. /* Find error function for runtime errors. Requires an extra stack traversal. */

495. ‌static ptrdiff_t finderrfunc(lua_State *L)

496. {

497. cTValue *frame = L->base-1, *bot = tvref(L->stack)+LJ_FR2;

498. void *cf = L->cframe;

499. while (frame > bot && cf) {

500. while (cframe_nres(cframe_raw(cf)) < 0) {  /* cframe without frame? */

501. if (frame >= restorestack(L, -cframe_nres(cf)))

502. break;

503. if (cframe_errfunc(cf) >= 0)  /* Error handler not inherited (-1)? */

504. return cframe_errfunc(cf);

505. cf = cframe_prev(cf);  /* Else unwind cframe and continue searching. */

506. if (cf == NULL)

507. return 0;

508. }

509. switch (frame_typep(frame)) {

510. case FRAME_LUA:

511. case FRAME_LUAP:

512. frame = frame_prevl(frame);

513. break;

514. case FRAME_C:

515. cf = cframe_prev(cf);

516. /* fallthrough */

517. case FRAME_VARG:

518. frame = frame_prevd(frame);

519. break;

520. case FRAME_CONT:

521. if (frame_iscont_fficb(frame))

522. cf = cframe_prev(cf);

523. frame = frame_prevd(frame);

524. break;

525. case FRAME_CP:

526. if (cframe_canyield(cf)) return 0;

527. if (cframe_errfunc(cf) >= 0)

528. return cframe_errfunc(cf);

529. frame = frame_prevd(frame);

530. break;

531. case FRAME_PCALL:

532. case FRAME_PCALLH:

533. if (frame_func(frame_prevd(frame))->c.ffid == FF_xpcall)

534. return savestack(L, frame_prevd(frame)+1);  /* xpcall's errorfunc. */

535. return 0;

536. default:

537. lua_assert(0);

538. return 0;

539. }

540. }

541. return 0;

542. }

544. /* Runtime error. */

545. ‌LJ_NOINLINE void lj_err_run(lua_State *L)

546. {

547. ptrdiff_t ef = finderrfunc(L);

548. if (ef) {

549. TValue *errfunc = restorestack(L, ef);

550. TValue *top = L->top;

551. lj_trace_abort(G(L));

552. if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) {

553. setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR));

554. lj_err_throw(L, LUA_ERRERR);

555. }

556. L->status = LUA_ERRERR;

557. copyTV(L, top+LJ_FR2, top-1);

558. copyTV(L, top-1, errfunc);

559. if (LJ_FR2) setnilV(top++);

560. L->top = top+1;

561. lj_vm_call(L, top, 1+1);  /* Stack: |errfunc|msg| -> |msg| */

562. }

563. lj_err_throw(L, LUA_ERRRUN);

564. }

566. /* Formatted runtime error message. */

567. ‌LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)

568. {

569. const char *msg;

570. va_list argp;

571. va_start(argp, em);

572. if (curr_funcisL(L)) L->top = curr_topL(L);

573. msg = lj_strfmt_pushvf(L, err2msg(em), argp);

574. va_end(argp);

575. lj_debug_addloc(L, msg, L->base-1, NULL);

576. lj_err_run(L);

577. }

579. /* Non-vararg variant for better calling conventions. */

580. ‌LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em)

581. {

582. err_msgv(L, em);

583. }

585. /* Lexer error. */

586. ‌LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok,

587. BCLine line, ErrMsg em, va_list argp)

588. {

589. char buff[LUA_IDSIZE];

590. const char *msg;

591. lj_debug_shortname(buff, src, line);

592. msg = lj_strfmt_pushvf(L, err2msg(em), argp);

593. msg = lj_strfmt_pushf(L, "%s:%d: %s", buff, line, msg);

594. if (tok)

595. lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);

596. lj_err_throw(L, LUA_ERRSYNTAX);

597. }

599. /* Typecheck error for operands. */

600. ‌LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm)

601. {

602. const char *tname = lj_typename(o);

603. const char *opname = err2msg(opm);

604. if (curr_funcisL(L)) {

605. GCproto *pt = curr_proto(L);

606. const BCIns *pc = cframe_Lpc(L) - 1;

607. const char *oname = NULL;

608. const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname);

609. if (kind)

610. err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname);

611. }

612. err_msgv(L, LJ_ERR_BADOPRV, opname, tname);

613. }

615. /* Typecheck error for ordered comparisons. */

616. ‌LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2)

617. {

618. const char *t1 = lj_typename(o1);

619. const char *t2 = lj_typename(o2);

620. err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2);

621. /* This assumes the two "boolean" entries are commoned by the C compiler. */

622. }

624. /* Typecheck error for __call. */

625. ‌LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o)

626. {

627. /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:

628. ** L->base still points to the caller. So add a dummy frame with L instead

629. ** of a function. See lua_getstack().

630. */

631. const BCIns *pc = cframe_Lpc(L);

632. if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) {

633. const char *tname = lj_typename(o);

634. if (LJ_FR2) o++;

635. setframe_pc(o, pc);

636. setframe_gc(o, obj2gco(L), LJ_TTHREAD);

637. L->top = L->base = o+1;

638. err_msgv(L, LJ_ERR_BADCALL, tname);

639. }

640. lj_err_optype(L, o, LJ_ERR_OPCALL);

641. }

643. /* Error in context of caller. */

644. ‌LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg)

645. {

646. TValue *frame = L->base-1;

647. TValue *pframe = NULL;

648. if (frame_islua(frame)) {

649. pframe = frame_prevl(frame);

650. } else if (frame_iscont(frame)) {

651. if (frame_iscont_fficb(frame)) {

652. pframe = frame;

653. frame = NULL;

654. } else {

655. pframe = frame_prevd(frame);

656. #if LJ_HASFFI

657. /* Remove frame for FFI metamethods. */

658. if (frame_func(frame)->c.ffid >= FF_ffi_meta___index &&

659. frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) {

660. L->base = pframe+1;

661. L->top = frame;

662. setcframe_pc(cframe_raw(L->cframe), frame_contpc(frame));

663. }

664. #endif

665. }

666. }

667. lj_debug_addloc(L, msg, pframe, frame);

668. lj_err_run(L);

669. }

671. /* Formatted error in context of caller. */

672. ‌LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...)

673. {

674. const char *msg;

675. va_list argp;

676. va_start(argp, em);

677. msg = lj_strfmt_pushvf(L, err2msg(em), argp);

678. va_end(argp);

679. lj_err_callermsg(L, msg);

680. }

682. /* Error in context of caller. */

683. ‌LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em)

684. {

685. lj_err_callermsg(L, err2msg(em));

686. }

688. /* Argument error message. */

689. ‌LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,

690. const char *msg)

691. {

692. const char *fname = "?";

693. const char *ftype = lj_debug_funcname(L, L->base - 1, &fname);

694. if (narg < 0 && narg > LUA_REGISTRYINDEX)

695. narg = (int)(L->top - L->base) + narg + 1;

696. if (ftype && ftype[3] == 'h' && --narg == 0)  /* Check for "method". */

697. msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);

698. else

699. msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);

700. lj_err_callermsg(L, msg);

701. }

703. /* Formatted argument error. */

704. ‌LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...)

705. {

706. const char *msg;

707. va_list argp;

708. va_start(argp, em);

709. msg = lj_strfmt_pushvf(L, err2msg(em), argp);

710. va_end(argp);

711. err_argmsg(L, narg, msg);

712. }

714. /* Argument error. */

715. ‌LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em)

716. {

717. err_argmsg(L, narg, err2msg(em));

718. }

720. /* Typecheck error for arguments. */

721. ‌LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname)

722. {

723. const char *tname, *msg;

724. if (narg <= LUA_REGISTRYINDEX) {

725. if (narg >= LUA_GLOBALSINDEX) {

726. tname = lj_obj_itypename[~LJ_TTAB];

727. } else {

728. GCfunc *fn = curr_func(L);

729. int idx = LUA_GLOBALSINDEX - narg;

730. if (idx <= fn->c.nupvalues)

731. tname = lj_typename(&fn->c.upvalue[idx-1]);

732. else

733. tname = lj_obj_typename[0];

734. }

735. } else {

736. TValue *o = narg < 0 ? L->top + narg : L->base + narg-1;

737. tname = o < L->top ? lj_typename(o) : lj_obj_typename[0];

738. }

739. msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);

740. err_argmsg(L, narg, msg);

741. }

743. /* Typecheck error for arguments. */

744. ‌LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt)

745. {

746. lj_err_argtype(L, narg, lj_obj_typename[tt+1]);

747. }

749. /* -- Public error handling API ------------------------------------------- */

751. ‌LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)

752. {

753. lua_CFunction old = G(L)->panic;

754. G(L)->panic = panicf;

755. return old;

756. }

758. /* Forwarders for the public API (C calling convention and no LJ_NORET). */

759. ‌LUA_API int lua_error(lua_State *L)

760. {

761. lj_err_run(L);

762. return 0;  /* unreachable */

763. }

765. ‌LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg)

766. {

767. err_argmsg(L, narg, msg);

768. return 0;  /* unreachable */

769. }

771. ‌LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname)

772. {

773. lj_err_argtype(L, narg, xname);

774. return 0;  /* unreachable */

775. }

777. ‌LUALIB_API void luaL_where(lua_State *L, int level)

778. {

779. int size;

780. cTValue *frame = lj_debug_frame(L, level, &size);

781. lj_debug_addloc(L, "", frame, size ? frame+size : NULL);

782. }

784. ‌LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)

785. {

786. const char *msg;

787. va_list argp;

788. va_start(argp, fmt);

789. msg = lj_strfmt_pushvf(L, fmt, argp);

790. va_end(argp);

791. lj_err_callermsg(L, msg);

792. return 0;  /* unreachable */

793. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_func.c - luajit-2.0-src

Functions defined

- func_emptyuv

- func_finduv

- func_newL

- lj_func_closeuv

- lj_func_free

- lj_func_freeproto

- lj_func_freeuv

- lj_func_newC

- lj_func_newL_empty

- lj_func_newL_gc

- unlinkuv

Macros defined

- LUA_CORE

- lj_func_c

Source code

1. /*

2. ** Function handling (prototypes, functions and upvalues).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lj_func_c

10. ‌#define LUA_CORE

12. #include "lj_obj.h"

13. #include "lj_gc.h"

14. #include "lj_func.h"

15. #include "lj_trace.h"

16. #include "lj_vm.h"

18. /* -- Prototypes ---------------------------------------------------------- */

20. ‌void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt)

21. {

22. lj_mem_free(g, pt, pt->sizept);

23. }

25. /* -- Upvalues ------------------------------------------------------------ */

27. ‌static void unlinkuv(GCupval *uv)

28. {

29. lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv);

30. setgcrefr(uvnext(uv)->prev, uv->prev);

31. setgcrefr(uvprev(uv)->next, uv->next);

32. }

34. /* Find existing open upvalue for a stack slot or create a new one. */

35. ‌static GCupval *func_finduv(lua_State *L, TValue *slot)

36. {

37. global_State *g = G(L);

38. GCRef *pp = &L->openupval;

39. GCupval *p;

40. GCupval *uv;

41. /* Search the sorted list of open upvalues. */

42. while (gcref(*pp) != NULL && uvval((p = gco2uv(gcref(*pp)))) >= slot) {

43. lua_assert(!p->closed && uvval(p) != &p->tv);

44. if (uvval(p) == slot) {  /* Found open upvalue pointing to same slot? */

45. if (isdead(g, obj2gco(p)))  /* Resurrect it, if it's dead. */

46. flipwhite(obj2gco(p));

47. return p;

48. }

49. pp = &p->nextgc;

50. }

51. /* No matching upvalue found. Create a new one. */

52. uv = lj_mem_newt(L, sizeof(GCupval), GCupval);

53. newwhite(g, uv);

54. uv->gct = ~LJ_TUPVAL;

55. uv->closed = 0;  /* Still open. */

56. setmref(uv->v, slot);  /* Pointing to the stack slot. */

57. /* NOBARRIER: The GCupval is new (marked white) and open. */

58. setgcrefr(uv->nextgc, *pp);  /* Insert into sorted list of open upvalues. */

59. setgcref(*pp, obj2gco(uv));

60. setgcref(uv->prev, obj2gco(&g->uvhead));  /* Insert into GC list, too. */

61. setgcrefr(uv->next, g->uvhead.next);

62. setgcref(uvnext(uv)->prev, obj2gco(uv));

63. setgcref(g->uvhead.next, obj2gco(uv));

64. lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv);

65. return uv;

66. }

68. /* Create an empty and closed upvalue. */

69. ‌static GCupval *func_emptyuv(lua_State *L)

70. {

71. GCupval *uv = (GCupval *)lj_mem_newgco(L, sizeof(GCupval));

72. uv->gct = ~LJ_TUPVAL;

73. uv->closed = 1;

74. setnilV(&uv->tv);

75. setmref(uv->v, &uv->tv);

76. return uv;

77. }

79. /* Close all open upvalues pointing to some stack level or above. */

80. ‌void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level)

81. {

82. GCupval *uv;

83. global_State *g = G(L);

84. while (gcref(L->openupval) != NULL &&

85. uvval((uv = gco2uv(gcref(L->openupval)))) >= level) {

86. GCobj *o = obj2gco(uv);

87. lua_assert(!isblack(o) && !uv->closed && uvval(uv) != &uv->tv);

88. setgcrefr(L->openupval, uv->nextgc);  /* No longer in open list. */

89. if (isdead(g, o)) {

90. lj_func_freeuv(g, uv);

91. } else {

92. unlinkuv(uv);

93. lj_gc_closeuv(g, uv);

94. }

95. }

96. }

98. ‌void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv)

99. {

100. if (!uv->closed)

101. unlinkuv(uv);

102. lj_mem_freet(g, uv);

103. }

105. /* -- Functions (closures) ------------------------------------------------ */

107. ‌GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env)

108. {

109. GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeCfunc(nelems));

110. fn->c.gct = ~LJ_TFUNC;

111. fn->c.ffid = FF_C;

112. fn->c.nupvalues = (uint8_t)nelems;

113. /* NOBARRIER: The GCfunc is new (marked white). */

114. setmref(fn->c.pc, &G(L)->bc_cfunc_ext);

115. setgcref(fn->c.env, obj2gco(env));

116. return fn;

117. }

119. ‌static GCfunc *func_newL(lua_State *L, GCproto *pt, GCtab *env)

120. {

121. uint32_t count;

122. GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeLfunc((MSize)pt->sizeuv));

123. fn->l.gct = ~LJ_TFUNC;

124. fn->l.ffid = FF_LUA;

125. fn->l.nupvalues = 0;  /* Set to zero until upvalues are initialized. */

126. /* NOBARRIER: Really a setgcref. But the GCfunc is new (marked white). */

127. setmref(fn->l.pc, proto_bc(pt));

128. setgcref(fn->l.env, obj2gco(env));

129. /* Saturating 3 bit counter (0..7) for created closures. */

130. count = (uint32_t)pt->flags + PROTO_CLCOUNT;

131. pt->flags = (uint8_t)(count - ((count >> PROTO_CLC_BITS) & PROTO_CLCOUNT));

132. return fn;

133. }

135. /* Create a new Lua function with empty upvalues. */

136. ‌GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env)

137. {

138. GCfunc *fn = func_newL(L, pt, env);

139. MSize i, nuv = pt->sizeuv;

140. /* NOBARRIER: The GCfunc is new (marked white). */

141. for (i = 0; i < nuv; i++) {

142. GCupval *uv = func_emptyuv(L);

143. uv->dhash = (uint32_t)(uintptr_t)pt ^ ((uint32_t)proto_uv(pt)[i] << 24);

144. setgcref(fn->l.uvptr[i], obj2gco(uv));

145. }

146. fn->l.nupvalues = (uint8_t)nuv;

147. return fn;

148. }

150. /* Do a GC check and create a new Lua function with inherited upvalues. */

151. ‌GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent)

152. {

153. GCfunc *fn;

154. GCRef *puv;

155. MSize i, nuv;

156. TValue *base;

157. lj_gc_check_fixtop(L);

158. fn = func_newL(L, pt, tabref(parent->env));

159. /* NOBARRIER: The GCfunc is new (marked white). */

160. puv = parent->uvptr;

161. nuv = pt->sizeuv;

162. base = L->base;

163. for (i = 0; i < nuv; i++) {

164. uint32_t v = proto_uv(pt)[i];

165. GCupval *uv;

166. if ((v & PROTO_UV_LOCAL)) {

167. uv = func_finduv(L, base + (v & 0xff));

168. uv->immutable = ((v / PROTO_UV_IMMUTABLE) & 1);

169. uv->dhash = (uint32_t)(uintptr_t)mref(parent->pc, char) ^ (v << 24);

170. } else {

171. uv = &gcref(puv[v])->uv;

172. }

173. setgcref(fn->l.uvptr[i], obj2gco(uv));

174. }

175. fn->l.nupvalues = (uint8_t)nuv;

176. return fn;

177. }

179. ‌void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *fn)

180. {

181. MSize size = isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) :

182. sizeCfunc((MSize)fn->c.nupvalues);

183. lj_mem_free(g, fn, size);

184. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_gc.h - luajit-2.0-src

Functions defined

- lj_gc_barrierback

- lj_mem_free

Macros defined

- LJ_GC_BLACK

- LJ_GC_CDATA_FIN

- LJ_GC_COLORS

- LJ_GC_FINALIZED

- LJ_GC_FIXED

- LJ_GC_SFIXED

- LJ_GC_WEAK

- LJ_GC_WEAKKEY

- LJ_GC_WEAKVAL

- LJ_GC_WHITE0

- LJ_GC_WHITE1

- LJ_GC_WHITES

- _LJ_GC_H

- black2gray

- curwhite

- fixstring

- flipwhite

- isblack

- isdead

- isgray

- iswhite

- lj_gc_anybarriert

- lj_gc_barrier

- lj_gc_barriert

- lj_gc_check

- lj_gc_check_fixtop

- lj_gc_finalize_cdata

- lj_gc_objbarrier

- lj_gc_objbarriert

- lj_mem_freet

- lj_mem_freevec

- lj_mem_growvec

- lj_mem_new

- lj_mem_newobj

- lj_mem_newt

- lj_mem_newvec

- lj_mem_reallocvec

- makewhite

- markfinalized

- newwhite

- otherwhite

- tviswhite

Source code

1. /*

2. ** Garbage collector.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_GC_H

7. ‌#define _LJ_GC_H

9. #include "lj_obj.h"

11. /* Garbage collector states. Order matters. */

12. enum {

13. GCSpause, GCSpropagate, GCSatomic, GCSsweepstring, GCSsweep, GCSfinalize

14. };

16. /* Bitmasks for marked field of GCobj. */

17. ‌#define LJ_GC_WHITE0        0x01

18. ‌#define LJ_GC_WHITE1        0x02

19. ‌#define LJ_GC_BLACK        0x04

20. ‌#define LJ_GC_FINALIZED        0x08

21. ‌#define LJ_GC_WEAKKEY        0x08

22. ‌#define LJ_GC_WEAKVAL        0x10

23. ‌#define LJ_GC_CDATA_FIN        0x10

24. ‌#define LJ_GC_FIXED        0x20

25. ‌#define LJ_GC_SFIXED        0x40

27. ‌#define LJ_GC_WHITES        (LJ_GC_WHITE0 | LJ_GC_WHITE1)

28. ‌#define LJ_GC_COLORS        (LJ_GC_WHITES | LJ_GC_BLACK)

29. ‌#define LJ_GC_WEAK        (LJ_GC_WEAKKEY | LJ_GC_WEAKVAL)

31. /* Macros to test and set GCobj colors. */

32. ‌#define iswhite(x)        ((x)->gch.marked & LJ_GC_WHITES)

33. ‌#define isblack(x)        ((x)->gch.marked & LJ_GC_BLACK)

34. ‌#define isgray(x)        (!((x)->gch.marked & (LJ_GC_BLACK|LJ_GC_WHITES)))

35. ‌#define tviswhite(x)        (tvisgcv(x) && iswhite(gcV(x)))

36. ‌#define otherwhite(g)        (g->gc.currentwhite ^ LJ_GC_WHITES)

37. ‌#define isdead(g, v)        ((v)->gch.marked & otherwhite(g) & LJ_GC_WHITES)

39. ‌#define curwhite(g)        ((g)->gc.currentwhite & LJ_GC_WHITES)

40. ‌#define newwhite(g, x)        (obj2gco(x)->gch.marked = (uint8_t)curwhite(g))

41. ‌#define makewhite(g, x) \

42. ((x)->gch.marked = ((x)->gch.marked & (uint8_t)~LJ_GC_COLORS) | curwhite(g))

43. ‌#define flipwhite(x)        ((x)->gch.marked ^= LJ_GC_WHITES)

44. ‌#define black2gray(x)        ((x)->gch.marked &= (uint8_t)~LJ_GC_BLACK)

45. ‌#define fixstring(s)        ((s)->marked |= LJ_GC_FIXED)

46. ‌#define markfinalized(x)        ((x)->gch.marked |= LJ_GC_FINALIZED)

48. /* Collector. */

49. LJ_FUNC size_t lj_gc_separateudata(global_State *g, int all);

50. LJ_FUNC void lj_gc_finalize_udata(lua_State *L);

51. #if LJ_HASFFI

52. LJ_FUNC void lj_gc_finalize_cdata(lua_State *L);

53. #else

54. ‌#define lj_gc_finalize_cdata(L)                UNUSED(L)

55. #endif

56. LJ_FUNC void lj_gc_freeall(global_State *g);

57. LJ_FUNCA int LJ_FASTCALL lj_gc_step(lua_State *L);

58. LJ_FUNCA void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L);

59. #if LJ_HASJIT

60. LJ_FUNC int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps);

61. #endif

62. LJ_FUNC void lj_gc_fullgc(lua_State *L);

64. /* GC check: drive collector forward if the GC threshold has been reached. */

65. ‌#define lj_gc_check(L) \

66. { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \

67. lj_gc_step(L); }

68. ‌#define lj_gc_check_fixtop(L) \

69. { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \

70. lj_gc_step_fixtop(L); }

72. /* Write barriers. */

73. LJ_FUNC void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v);

74. LJ_FUNCA void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv);

75. LJ_FUNC void lj_gc_closeuv(global_State *g, GCupval *uv);

76. #if LJ_HASJIT

77. LJ_FUNC void lj_gc_barriertrace(global_State *g, uint32_t traceno);

78. #endif

80. /* Move the GC propagation frontier back for tables (make it gray again). */

81. ‌static LJ_AINLINE void lj_gc_barrierback(global_State *g, GCtab *t)

82. {

83. GCobj *o = obj2gco(t);

84. lua_assert(isblack(o) && !isdead(g, o));

85. lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause);

86. black2gray(o);

87. setgcrefr(t->gclist, g->gc.grayagain);

88. setgcref(g->gc.grayagain, o);

89. }

91. /* Barrier for stores to table objects. TValue and GCobj variant. */

92. ‌#define lj_gc_anybarriert(L, t)  \

93. { if (LJ_UNLIKELY(isblack(obj2gco(t)))) lj_gc_barrierback(G(L), (t)); }

94. ‌#define lj_gc_barriert(L, t, tv) \

95. { if (tviswhite(tv) && isblack(obj2gco(t))) \

96. lj_gc_barrierback(G(L), (t)); }

97. ‌#define lj_gc_objbarriert(L, t, o)  \

98. { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) \

99. lj_gc_barrierback(G(L), (t)); }

101. /* Barrier for stores to any other object. TValue and GCobj variant. */

102. ‌#define lj_gc_barrier(L, p, tv) \

103. { if (tviswhite(tv) && isblack(obj2gco(p))) \

104. lj_gc_barrierf(G(L), obj2gco(p), gcV(tv)); }

105. ‌#define lj_gc_objbarrier(L, p, o) \

106. { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \

107. lj_gc_barrierf(G(L), obj2gco(p), obj2gco(o)); }

109. /* Allocator. */

110. LJ_FUNC void *lj_mem_realloc(lua_State *L, void *p, GCSize osz, GCSize nsz);

111. LJ_FUNC void * LJ_FASTCALL lj_mem_newgco(lua_State *L, GCSize size);

112. LJ_FUNC void *lj_mem_grow(lua_State *L, void *p,

113. MSize *szp, MSize lim, MSize esz);

115. ‌#define lj_mem_new(L, s)        lj_mem_realloc(L, NULL, 0, (s))

117. ‌static LJ_AINLINE void lj_mem_free(global_State *g, void *p, size_t osize)

118. {

119. g->gc.total -= (GCSize)osize;

120. g->allocf(g->allocd, p, osize, 0);

121. }

123. ‌#define lj_mem_newvec(L, n, t)        ((t *)lj_mem_new(L, (GCSize)((n)*sizeof(t))))

124. ‌#define lj_mem_reallocvec(L, p, on, n, t) \

125. ((p) = (t *)lj_mem_realloc(L, p, (on)*sizeof(t), (GCSize)((n)*sizeof(t))))

126. ‌#define lj_mem_growvec(L, p, n, m, t) \

127. ((p) = (t *)lj_mem_grow(L, (p), &(n), (m), (MSize)sizeof(t)))

128. ‌#define lj_mem_freevec(g, p, n, t)        lj_mem_free(g, (p), (n)*sizeof(t))

130. ‌#define lj_mem_newobj(L, t)        ((t *)lj_mem_newgco(L, sizeof(t)))

131. ‌#define lj_mem_newt(L, s, t)        ((t *)lj_mem_new(L, (s)))

132. ‌#define lj_mem_freet(g, p)        lj_mem_free(g, (p), sizeof(*(p)))

134. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_gc.c - luajit-2.0-src

Global variables defined

- gc_freefunc

Data types defined

- GCFreeFunc

Functions defined

- atomic

- gc_call_finalizer

- gc_clearweak

- gc_finalize

- gc_mark

- gc_mark_gcroot

- gc_mark_mmudata

- gc_mark_start

- gc_mark_uv

- gc_marktrace

- gc_mayclear

- gc_onestep

- gc_propagate_gray

- gc_sweep

- gc_traverse_frames

- gc_traverse_func

- gc_traverse_proto

- gc_traverse_tab

- gc_traverse_thread

- gc_traverse_trace

- lj_gc_barrierf

- lj_gc_barriertrace

- lj_gc_barrieruv

- lj_gc_closeuv

- lj_gc_finalize_cdata

- lj_gc_finalize_udata

- lj_gc_freeall

- lj_gc_fullgc

- lj_gc_separateudata

- lj_gc_step

- lj_gc_step_fixtop

- lj_gc_step_jit

- lj_mem_grow

- lj_mem_newgco

- lj_mem_realloc

- propagatemark

Macros defined

- GCFINALIZECOST

- GCSTEPSIZE

- GCSWEEPCOST

- GCSWEEPMAX

- LUA_CORE

- TV2MARKED

- TV2MARKED

- gc_fullsweep

- gc_mark_str

- gc_markobj

- gc_marktv

- gc_traverse_curtrace

- gc_traverse_curtrace

- gray2black

- isfinalized

- lj_gc_c

- white2gray

Source code

1. /*

2. ** Garbage collector.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lj_gc_c

10. ‌#define LUA_CORE

12. #include "lj_obj.h"

13. #include "lj_gc.h"

14. #include "lj_err.h"

15. #include "lj_buf.h"

16. #include "lj_str.h"

17. #include "lj_tab.h"

18. #include "lj_func.h"

19. #include "lj_udata.h"

20. #include "lj_meta.h"

21. #include "lj_state.h"

22. #include "lj_frame.h"

23. #if LJ_HASFFI

24. #include "lj_ctype.h"

25. #include "lj_cdata.h"

26. #endif

27. #include "lj_trace.h"

28. #include "lj_vm.h"

30. ‌#define GCSTEPSIZE        1024u

31. ‌#define GCSWEEPMAX        40

32. ‌#define GCSWEEPCOST        10

33. ‌#define GCFINALIZECOST        100

35. /* Macros to set GCobj colors and flags. */

36. ‌#define white2gray(x)                ((x)->gch.marked &= (uint8_t)~LJ_GC_WHITES)

37. ‌#define gray2black(x)                ((x)->gch.marked |= LJ_GC_BLACK)

38. ‌#define isfinalized(u)                ((u)->marked & LJ_GC_FINALIZED)

40. /* -- Mark phase ---------------------------------------------------------- */

42. /* Mark a TValue (if needed). */

43. ‌#define gc_marktv(g, tv) \

44. { lua_assert(!tvisgcv(tv) || (~itype(tv) == gcval(tv)->gch.gct)); \

45. if (tviswhite(tv)) gc_mark(g, gcV(tv)); }

47. /* Mark a GCobj (if needed). */

48. ‌#define gc_markobj(g, o) \

49. { if (iswhite(obj2gco(o))) gc_mark(g, obj2gco(o)); }

51. /* Mark a string object. */

52. ‌#define gc_mark_str(s)                ((s)->marked &= (uint8_t)~LJ_GC_WHITES)

54. /* Mark a white GCobj. */

55. ‌static void gc_mark(global_State *g, GCobj *o)

56. {

57. int gct = o->gch.gct;

58. lua_assert(iswhite(o) && !isdead(g, o));

59. white2gray(o);

60. if (LJ_UNLIKELY(gct == ~LJ_TUDATA)) {

61. GCtab *mt = tabref(gco2ud(o)->metatable);

62. gray2black(o);  /* Userdata are never gray. */

63. if (mt) gc_markobj(g, mt);

64. gc_markobj(g, tabref(gco2ud(o)->env));

65. } else if (LJ_UNLIKELY(gct == ~LJ_TUPVAL)) {

66. GCupval *uv = gco2uv(o);

67. gc_marktv(g, uvval(uv));

68. if (uv->closed)

69. gray2black(o);  /* Closed upvalues are never gray. */

70. } else if (gct != ~LJ_TSTR && gct != ~LJ_TCDATA) {

71. lua_assert(gct == ~LJ_TFUNC || gct == ~LJ_TTAB ||

72. gct == ~LJ_TTHREAD || gct == ~LJ_TPROTO);

73. setgcrefr(o->gch.gclist, g->gc.gray);

74. setgcref(g->gc.gray, o);

75. }

76. }

78. /* Mark GC roots. */

79. ‌static void gc_mark_gcroot(global_State *g)

80. {

81. ptrdiff_t i;

82. for (i = 0; i < GCROOT_MAX; i++)

83. if (gcref(g->gcroot[i]) != NULL)

84. gc_markobj(g, gcref(g->gcroot[i]));

85. }

87. /* Start a GC cycle and mark the root set. */

88. ‌static void gc_mark_start(global_State *g)

89. {

90. setgcrefnull(g->gc.gray);

91. setgcrefnull(g->gc.grayagain);

92. setgcrefnull(g->gc.weak);

93. gc_markobj(g, mainthread(g));

94. gc_markobj(g, tabref(mainthread(g)->env));

95. gc_marktv(g, &g->registrytv);

96. gc_mark_gcroot(g);

97. g->gc.state = GCSpropagate;

98. }

100. /* Mark open upvalues. */

101. ‌static void gc_mark_uv(global_State *g)

102. {

103. GCupval *uv;

104. for (uv = uvnext(&g->uvhead); uv != &g->uvhead; uv = uvnext(uv)) {

105. lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv);

106. if (isgray(obj2gco(uv)))

107. gc_marktv(g, uvval(uv));

108. }

109. }

111. /* Mark userdata in mmudata list. */

112. ‌static void gc_mark_mmudata(global_State *g)

113. {

114. GCobj *root = gcref(g->gc.mmudata);

115. GCobj *u = root;

116. if (u) {

117. do {

118. u = gcnext(u);

119. makewhite(g, u);  /* Could be from previous GC. */

120. gc_mark(g, u);

121. } while (u != root);

122. }

123. }

125. /* Separate userdata objects to be finalized to mmudata list. */

126. ‌size_t lj_gc_separateudata(global_State *g, int all)

127. {

128. size_t m = 0;

129. GCRef *p = &mainthread(g)->nextgc;

130. GCobj *o;

131. while ((o = gcref(*p)) != NULL) {

132. if (!(iswhite(o) || all) || isfinalized(gco2ud(o))) {

133. p = &o->gch.nextgc;  /* Nothing to do. */

134. } else if (!lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc)) {

135. markfinalized(o);  /* Done, as there's no __gc metamethod. */

136. p = &o->gch.nextgc;

137. } else {  /* Otherwise move userdata to be finalized to mmudata list. */

138. m += sizeudata(gco2ud(o));

139. markfinalized(o);

140. *p = o->gch.nextgc;

141. if (gcref(g->gc.mmudata)) {  /* Link to end of mmudata list. */

142. GCobj *root = gcref(g->gc.mmudata);

143. setgcrefr(o->gch.nextgc, root->gch.nextgc);

144. setgcref(root->gch.nextgc, o);

145. setgcref(g->gc.mmudata, o);

146. } else {  /* Create circular list. */

147. setgcref(o->gch.nextgc, o);

148. setgcref(g->gc.mmudata, o);

149. }

150. }

151. }

152. return m;

153. }

155. /* -- Propagation phase --------------------------------------------------- */

157. /* Traverse a table. */

158. ‌static int gc_traverse_tab(global_State *g, GCtab *t)

159. {

160. int weak = 0;

161. cTValue *mode;

162. GCtab *mt = tabref(t->metatable);

163. if (mt)

164. gc_markobj(g, mt);

165. mode = lj_meta_fastg(g, mt, MM_mode);

166. if (mode && tvisstr(mode)) {  /* Valid __mode field? */

167. const char *modestr = strVdata(mode);

168. int c;

169. while ((c = *modestr++)) {

170. if (c == 'k') weak |= LJ_GC_WEAKKEY;

171. else if (c == 'v') weak |= LJ_GC_WEAKVAL;

172. else if (c == 'K') weak = (int)(~0u & ~LJ_GC_WEAKVAL);

173. }

174. if (weak > 0) {  /* Weak tables are cleared in the atomic phase. */

175. t->marked = (uint8_t)((t->marked & ~LJ_GC_WEAK) | weak);

176. setgcrefr(t->gclist, g->gc.weak);

177. setgcref(g->gc.weak, obj2gco(t));

178. }

179. }

180. if (weak == LJ_GC_WEAK)  /* Nothing to mark if both keys/values are weak. */

181. return 1;

182. if (!(weak & LJ_GC_WEAKVAL)) {  /* Mark array part. */

183. MSize i, asize = t->asize;

184. for (i = 0; i < asize; i++)

185. gc_marktv(g, arrayslot(t, i));

186. }

187. if (t->hmask > 0) {  /* Mark hash part. */

188. Node *node = noderef(t->node);

189. MSize i, hmask = t->hmask;

190. for (i = 0; i <= hmask; i++) {

191. Node *n = &node[i];

192. if (!tvisnil(&n->val)) {  /* Mark non-empty slot. */

193. lua_assert(!tvisnil(&n->key));

194. if (!(weak & LJ_GC_WEAKKEY)) gc_marktv(g, &n->key);

195. if (!(weak & LJ_GC_WEAKVAL)) gc_marktv(g, &n->val);

196. }

197. }

198. }

199. return weak;

200. }

202. /* Traverse a function. */

203. ‌static void gc_traverse_func(global_State *g, GCfunc *fn)

204. {

205. gc_markobj(g, tabref(fn->c.env));

206. if (isluafunc(fn)) {

207. uint32_t i;

208. lua_assert(fn->l.nupvalues <= funcproto(fn)->sizeuv);

209. gc_markobj(g, funcproto(fn));

210. for (i = 0; i < fn->l.nupvalues; i++)  /* Mark Lua function upvalues. */

211. gc_markobj(g, &gcref(fn->l.uvptr[i])->uv);

212. } else {

213. uint32_t i;

214. for (i = 0; i < fn->c.nupvalues; i++)  /* Mark C function upvalues. */

215. gc_marktv(g, &fn->c.upvalue[i]);

216. }

217. }

219. #if LJ_HASJIT

220. /* Mark a trace. */

221. ‌static void gc_marktrace(global_State *g, TraceNo traceno)

222. {

223. GCobj *o = obj2gco(traceref(G2J(g), traceno));

224. lua_assert(traceno != G2J(g)->cur.traceno);

225. if (iswhite(o)) {

226. white2gray(o);

227. setgcrefr(o->gch.gclist, g->gc.gray);

228. setgcref(g->gc.gray, o);

229. }

230. }

232. /* Traverse a trace. */

233. ‌static void gc_traverse_trace(global_State *g, GCtrace *T)

234. {

235. IRRef ref;

236. if (T->traceno == 0) return;

237. for (ref = T->nk; ref < REF_TRUE; ref++) {

238. IRIns *ir = &T->ir[ref];

239. if (ir->o == IR_KGC)

240. gc_markobj(g, ir_kgc(ir));

241. }

242. if (T->link) gc_marktrace(g, T->link);

243. if (T->nextroot) gc_marktrace(g, T->nextroot);

244. if (T->nextside) gc_marktrace(g, T->nextside);

245. gc_markobj(g, gcref(T->startpt));

246. }

248. /* The current trace is a GC root while not anchored in the prototype (yet). */

249. ‌#define gc_traverse_curtrace(g)        gc_traverse_trace(g, &G2J(g)->cur)

250. #else

251. ‌#define gc_traverse_curtrace(g)        UNUSED(g)

252. #endif

254. /* Traverse a prototype. */

255. ‌static void gc_traverse_proto(global_State *g, GCproto *pt)

256. {

257. ptrdiff_t i;

258. gc_mark_str(proto_chunkname(pt));

259. for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++)  /* Mark collectable consts. */

260. gc_markobj(g, proto_kgc(pt, i));

261. #if LJ_HASJIT

262. if (pt->trace) gc_marktrace(g, pt->trace);

263. #endif

264. }

266. /* Traverse the frame structure of a stack. */

267. ‌static MSize gc_traverse_frames(global_State *g, lua_State *th)

268. {

269. TValue *frame, *top = th->top-1, *bot = tvref(th->stack);

270. /* Note: extra vararg frame not skipped, marks function twice (harmless). */

271. for (frame = th->base-1; frame > bot+LJ_FR2; frame = frame_prev(frame)) {

272. GCfunc *fn = frame_func(frame);

273. TValue *ftop = frame;

274. if (isluafunc(fn)) ftop += funcproto(fn)->framesize;

275. if (ftop > top) top = ftop;

276. if (!LJ_FR2) gc_markobj(g, fn);  /* Need to mark hidden function (or L). */

277. }

278. top++;  /* Correct bias of -1 (frame == base-1). */

279. if (top > tvref(th->maxstack)) top = tvref(th->maxstack);

280. return (MSize)(top - bot);  /* Return minimum needed stack size. */

281. }

283. /* Traverse a thread object. */

284. ‌static void gc_traverse_thread(global_State *g, lua_State *th)

285. {

286. TValue *o, *top = th->top;

287. for (o = tvref(th->stack)+1+LJ_FR2; o < top; o++)

288. gc_marktv(g, o);

289. if (g->gc.state == GCSatomic) {

290. top = tvref(th->stack) + th->stacksize;

291. for (; o < top; o++)  /* Clear unmarked slots. */

292. setnilV(o);

293. }

294. gc_markobj(g, tabref(th->env));

295. lj_state_shrinkstack(th, gc_traverse_frames(g, th));

296. }

298. /* Propagate one gray object. Traverse it and turn it black. */

299. ‌static size_t propagatemark(global_State *g)

300. {

301. GCobj *o = gcref(g->gc.gray);

302. int gct = o->gch.gct;

303. lua_assert(isgray(o));

304. gray2black(o);

305. setgcrefr(g->gc.gray, o->gch.gclist);  /* Remove from gray list. */

306. if (LJ_LIKELY(gct == ~LJ_TTAB)) {

307. GCtab *t = gco2tab(o);

308. if (gc_traverse_tab(g, t) > 0)

309. black2gray(o);  /* Keep weak tables gray. */

310. return sizeof(GCtab) + sizeof(TValue) * t->asize +

311. sizeof(Node) * (t->hmask + 1);

312. } else if (LJ_LIKELY(gct == ~LJ_TFUNC)) {

313. GCfunc *fn = gco2func(o);

314. gc_traverse_func(g, fn);

315. return isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) :

316. sizeCfunc((MSize)fn->c.nupvalues);

317. } else if (LJ_LIKELY(gct == ~LJ_TPROTO)) {

318. GCproto *pt = gco2pt(o);

319. gc_traverse_proto(g, pt);

320. return pt->sizept;

321. } else if (LJ_LIKELY(gct == ~LJ_TTHREAD)) {

322. lua_State *th = gco2th(o);

323. setgcrefr(th->gclist, g->gc.grayagain);

324. setgcref(g->gc.grayagain, o);

325. black2gray(o);  /* Threads are never black. */

326. gc_traverse_thread(g, th);

327. return sizeof(lua_State) + sizeof(TValue) * th->stacksize;

328. } else {

329. #if LJ_HASJIT

330. GCtrace *T = gco2trace(o);

331. gc_traverse_trace(g, T);

332. return ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) +

333. T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry);

334. #else

335. lua_assert(0);

336. return 0;

337. #endif

338. }

339. }

341. /* Propagate all gray objects. */

342. ‌static size_t gc_propagate_gray(global_State *g)

343. {

344. size_t m = 0;

345. while (gcref(g->gc.gray) != NULL)

346. m += propagatemark(g);

347. return m;

348. }

350. /* -- Sweep phase --------------------------------------------------------- */

352. /* Type of GC free functions. */

353. ‌typedef void (LJ_FASTCALL *GCFreeFunc)(global_State *g, GCobj *o);

355. /* GC free functions for LJ_TSTR .. LJ_TUDATA. ORDER LJ_T */

356. ‌static const GCFreeFunc gc_freefunc[] = {

357. (GCFreeFunc)lj_str_free,

358. (GCFreeFunc)lj_func_freeuv,

359. (GCFreeFunc)lj_state_free,

360. (GCFreeFunc)lj_func_freeproto,

361. (GCFreeFunc)lj_func_free,

362. #if LJ_HASJIT

363. (GCFreeFunc)lj_trace_free,

364. #else

365. (GCFreeFunc)0,

366. #endif

367. #if LJ_HASFFI

368. (GCFreeFunc)lj_cdata_free,

369. #else

370. (GCFreeFunc)0,

371. #endif

372. (GCFreeFunc)lj_tab_free,

373. (GCFreeFunc)lj_udata_free

374. };

376. /* Full sweep of a GC list. */

377. ‌#define gc_fullsweep(g, p)        gc_sweep(g, (p), ~(uint32_t)0)

379. /* Partial sweep of a GC list. */

380. ‌static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim)

381. {

382. /* Mask with other white and LJ_GC_FIXED. Or LJ_GC_SFIXED on shutdown. */

383. int ow = otherwhite(g);

384. GCobj *o;

385. while ((o = gcref(*p)) != NULL && lim-- > 0) {

386. if (o->gch.gct == ~LJ_TTHREAD)  /* Need to sweep open upvalues, too. */

387. gc_fullsweep(g, &gco2th(o)->openupval);

388. if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) {  /* Black or current white? */

389. lua_assert(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED));

390. makewhite(g, o);  /* Value is alive, change to the current white. */

391. p = &o->gch.nextgc;

392. } else {  /* Otherwise value is dead, free it. */

393. lua_assert(isdead(g, o) || ow == LJ_GC_SFIXED);

394. setgcrefr(*p, o->gch.nextgc);

395. if (o == gcref(g->gc.root))

396. setgcrefr(g->gc.root, o->gch.nextgc);  /* Adjust list anchor. */

397. gc_freefunc[o->gch.gct - ~LJ_TSTR](g, o);

398. }

399. }

400. return p;

401. }

403. /* Check whether we can clear a key or a value slot from a table. */

404. ‌static int gc_mayclear(cTValue *o, int val)

405. {

406. if (tvisgcv(o)) {  /* Only collectable objects can be weak references. */

407. if (tvisstr(o)) {  /* But strings cannot be used as weak references. */

408. gc_mark_str(strV(o));  /* And need to be marked. */

409. return 0;

410. }

411. if (iswhite(gcV(o)))

412. return 1;  /* Object is about to be collected. */

413. if (tvisudata(o) && val && isfinalized(udataV(o)))

414. return 1;  /* Finalized userdata is dropped only from values. */

415. }

416. return 0;  /* Cannot clear. */

417. }

419. /* Clear collected entries from weak tables. */

420. ‌static void gc_clearweak(GCobj *o)

421. {

422. while (o) {

423. GCtab *t = gco2tab(o);

424. lua_assert((t->marked & LJ_GC_WEAK));

425. if ((t->marked & LJ_GC_WEAKVAL)) {

426. MSize i, asize = t->asize;

427. for (i = 0; i < asize; i++) {

428. /* Clear array slot when value is about to be collected. */

429. TValue *tv = arrayslot(t, i);

430. if (gc_mayclear(tv, 1))

431. setnilV(tv);

432. }

433. }

434. if (t->hmask > 0) {

435. Node *node = noderef(t->node);

436. MSize i, hmask = t->hmask;

437. for (i = 0; i <= hmask; i++) {

438. Node *n = &node[i];

439. /* Clear hash slot when key or value is about to be collected. */

440. if (!tvisnil(&n->val) && (gc_mayclear(&n->key, 0) ||

441. gc_mayclear(&n->val, 1)))

442. setnilV(&n->val);

443. }

444. }

445. o = gcref(t->gclist);

446. }

447. }

449. /* Call a userdata or cdata finalizer. */

450. ‌static void gc_call_finalizer(global_State *g, lua_State *L,

451. cTValue *mo, GCobj *o)

452. {

453. /* Save and restore lots of state around the __gc callback. */

454. uint8_t oldh = hook_save(g);

455. GCSize oldt = g->gc.threshold;

456. int errcode;

457. TValue *top;

458. lj_trace_abort(g);

459. hook_entergc(g);  /* Disable hooks and new traces during __gc. */

460. g->gc.threshold = LJ_MAX_MEM;  /* Prevent GC steps. */

461. top = L->top;

462. copyTV(L, top++, mo);

463. if (LJ_FR2) setnilV(top++);

464. setgcV(L, top, o, ~o->gch.gct);

465. L->top = top+1;

466. errcode = lj_vm_pcall(L, top, 1+0, -1);  /* Stack: |mo|o| -> | */

467. hook_restore(g, oldh);

468. g->gc.threshold = oldt;  /* Restore GC threshold. */

469. if (errcode)

470. lj_err_throw(L, errcode);  /* Propagate errors. */

471. }

473. /* Finalize one userdata or cdata object from the mmudata list. */

474. ‌static void gc_finalize(lua_State *L)

475. {

476. global_State *g = G(L);

477. GCobj *o = gcnext(gcref(g->gc.mmudata));

478. cTValue *mo;

479. lua_assert(tvref(g->jit_base) == NULL);  /* Must not be called on trace. */

480. /* Unchain from list of userdata to be finalized. */

481. if (o == gcref(g->gc.mmudata))

482. setgcrefnull(g->gc.mmudata);

483. else

484. setgcrefr(gcref(g->gc.mmudata)->gch.nextgc, o->gch.nextgc);

485. #if LJ_HASFFI

486. if (o->gch.gct == ~LJ_TCDATA) {

487. TValue tmp, *tv;

488. /* Add cdata back to the GC list and make it white. */

489. setgcrefr(o->gch.nextgc, g->gc.root);

490. setgcref(g->gc.root, o);

491. makewhite(g, o);

492. o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN;

493. /* Resolve finalizer. */

494. setcdataV(L, &tmp, gco2cd(o));

495. tv = lj_tab_set(L, ctype_ctsG(g)->finalizer, &tmp);

496. if (!tvisnil(tv)) {

497. g->gc.nocdatafin = 0;

498. copyTV(L, &tmp, tv);

499. setnilV(tv);  /* Clear entry in finalizer table. */

500. gc_call_finalizer(g, L, &tmp, o);

501. }

502. return;

503. }

504. #endif

505. /* Add userdata back to the main userdata list and make it white. */

506. setgcrefr(o->gch.nextgc, mainthread(g)->nextgc);

507. setgcref(mainthread(g)->nextgc, o);

508. makewhite(g, o);

509. /* Resolve the __gc metamethod. */

510. mo = lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc);

511. if (mo)

512. gc_call_finalizer(g, L, mo, o);

513. }

515. /* Finalize all userdata objects from mmudata list. */

516. ‌void lj_gc_finalize_udata(lua_State *L)

517. {

518. while (gcref(G(L)->gc.mmudata) != NULL)

519. gc_finalize(L);

520. }

522. #if LJ_HASFFI

523. /* Finalize all cdata objects from finalizer table. */

524. ‌void lj_gc_finalize_cdata(lua_State *L)

525. {

526. global_State *g = G(L);

527. CTState *cts = ctype_ctsG(g);

528. if (cts) {

529. GCtab *t = cts->finalizer;

530. Node *node = noderef(t->node);

531. ptrdiff_t i;

532. setgcrefnull(t->metatable);  /* Mark finalizer table as disabled. */

533. for (i = (ptrdiff_t)t->hmask; i >= 0; i--)

534. if (!tvisnil(&node[i].val) && tviscdata(&node[i].key)) {

535. GCobj *o = gcV(&node[i].key);

536. TValue tmp;

537. makewhite(g, o);

538. o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN;

539. copyTV(L, &tmp, &node[i].val);

540. setnilV(&node[i].val);

541. gc_call_finalizer(g, L, &tmp, o);

542. }

543. }

544. }

545. #endif

547. /* Free all remaining GC objects. */

548. ‌void lj_gc_freeall(global_State *g)

549. {

550. MSize i, strmask;

551. /* Free everything, except super-fixed objects (the main thread). */

552. g->gc.currentwhite = LJ_GC_WHITES | LJ_GC_SFIXED;

553. gc_fullsweep(g, &g->gc.root);

554. strmask = g->strmask;

555. for (i = 0; i <= strmask; i++)  /* Free all string hash chains. */

556. gc_fullsweep(g, &g->strhash[i]);

557. }

559. /* -- Collector ----------------------------------------------------------- */

561. /* Atomic part of the GC cycle, transitioning from mark to sweep phase. */

562. ‌static void atomic(global_State *g, lua_State *L)

563. {

564. size_t udsize;

566. gc_mark_uv(g);  /* Need to remark open upvalues (the thread may be dead). */

567. gc_propagate_gray(g);  /* Propagate any left-overs. */

569. setgcrefr(g->gc.gray, g->gc.weak);  /* Empty the list of weak tables. */

570. setgcrefnull(g->gc.weak);

571. lua_assert(!iswhite(obj2gco(mainthread(g))));

572. gc_markobj(g, L);  /* Mark running thread. */

573. gc_traverse_curtrace(g);  /* Traverse current trace. */

574. gc_mark_gcroot(g);  /* Mark GC roots (again). */

575. gc_propagate_gray(g);  /* Propagate all of the above. */

577. setgcrefr(g->gc.gray, g->gc.grayagain);  /* Empty the 2nd chance list. */

578. setgcrefnull(g->gc.grayagain);

579. gc_propagate_gray(g);  /* Propagate it. */

581. udsize = lj_gc_separateudata(g, 0);  /* Separate userdata to be finalized. */

582. gc_mark_mmudata(g);  /* Mark them. */

583. udsize += gc_propagate_gray(g);  /* And propagate the marks. */

585. /* All marking done, clear weak tables. */

586. gc_clearweak(gcref(g->gc.weak));

588. lj_buf_shrink(L, &g->tmpbuf);  /* Shrink temp buffer. */

590. /* Prepare for sweep phase. */

591. g->gc.currentwhite = (uint8_t)otherwhite(g);  /* Flip current white. */

592. g->strempty.marked = g->gc.currentwhite;

593. setmref(g->gc.sweep, &g->gc.root);

594. g->gc.estimate = g->gc.total - (GCSize)udsize;  /* Initial estimate. */

595. }

597. /* GC state machine. Returns a cost estimate for each step performed. */

598. ‌static size_t gc_onestep(lua_State *L)

599. {

600. global_State *g = G(L);

601. switch (g->gc.state) {

602. case GCSpause:

603. gc_mark_start(g);  /* Start a new GC cycle by marking all GC roots. */

604. return 0;

605. case GCSpropagate:

606. if (gcref(g->gc.gray) != NULL)

607. return propagatemark(g);  /* Propagate one gray object. */

608. g->gc.state = GCSatomic;  /* End of mark phase. */

609. return 0;

610. case GCSatomic:

611. if (tvref(g->jit_base))  /* Don't run atomic phase on trace. */

612. return LJ_MAX_MEM;

613. atomic(g, L);

614. g->gc.state = GCSsweepstring;  /* Start of sweep phase. */

615. g->gc.sweepstr = 0;

616. return 0;

617. case GCSsweepstring: {

618. GCSize old = g->gc.total;

619. gc_fullsweep(g, &g->strhash[g->gc.sweepstr++]);  /* Sweep one chain. */

620. if (g->gc.sweepstr > g->strmask)

621. g->gc.state = GCSsweep;  /* All string hash chains sweeped. */

622. lua_assert(old >= g->gc.total);

623. g->gc.estimate -= old - g->gc.total;

624. return GCSWEEPCOST;

625. }

626. case GCSsweep: {

627. GCSize old = g->gc.total;

628. setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX));

629. lua_assert(old >= g->gc.total);

630. g->gc.estimate -= old - g->gc.total;

631. if (gcref(*mref(g->gc.sweep, GCRef)) == NULL) {

632. if (g->strnum <= (g->strmask >> 2) && g->strmask > LJ_MIN_STRTAB*2-1)

633. lj_str_resize(L, g->strmask >> 1);  /* Shrink string table. */

634. if (gcref(g->gc.mmudata)) {  /* Need any finalizations? */

635. g->gc.state = GCSfinalize;

636. #if LJ_HASFFI

637. g->gc.nocdatafin = 1;

638. #endif

639. } else {  /* Otherwise skip this phase to help the JIT. */

640. g->gc.state = GCSpause;  /* End of GC cycle. */

641. g->gc.debt = 0;

642. }

643. }

644. return GCSWEEPMAX*GCSWEEPCOST;

645. }

646. case GCSfinalize:

647. if (gcref(g->gc.mmudata) != NULL) {

648. if (tvref(g->jit_base))  /* Don't call finalizers on trace. */

649. return LJ_MAX_MEM;

650. gc_finalize(L);  /* Finalize one userdata object. */

651. if (g->gc.estimate > GCFINALIZECOST)

652. g->gc.estimate -= GCFINALIZECOST;

653. return GCFINALIZECOST;

654. }

655. #if LJ_HASFFI

656. if (!g->gc.nocdatafin) lj_tab_rehash(L, ctype_ctsG(g)->finalizer);

657. #endif

658. g->gc.state = GCSpause;  /* End of GC cycle. */

659. g->gc.debt = 0;

660. return 0;

661. default:

662. lua_assert(0);

663. return 0;

664. }

665. }

667. /* Perform a limited amount of incremental GC steps. */

668. ‌int LJ_FASTCALL lj_gc_step(lua_State *L)

669. {

670. global_State *g = G(L);

671. GCSize lim;

672. int32_t ostate = g->vmstate;

673. setvmstate(g, GC);

674. lim = (GCSTEPSIZE/100) * g->gc.stepmul;

675. if (lim == 0)

676. lim = LJ_MAX_MEM;

677. if (g->gc.total > g->gc.threshold)

678. g->gc.debt += g->gc.total - g->gc.threshold;

679. do {

680. lim -= (GCSize)gc_onestep(L);

681. if (g->gc.state == GCSpause) {

682. g->gc.threshold = (g->gc.estimate/100) * g->gc.pause;

683. g->vmstate = ostate;

684. return 1;  /* Finished a GC cycle. */

685. }

686. } while (sizeof(lim) == 8 ? ((int64_t)lim > 0) : ((int32_t)lim > 0));

687. if (g->gc.debt < GCSTEPSIZE) {

688. g->gc.threshold = g->gc.total + GCSTEPSIZE;

689. g->vmstate = ostate;

690. return -1;

691. } else {

692. g->gc.debt -= GCSTEPSIZE;

693. g->gc.threshold = g->gc.total;

694. g->vmstate = ostate;

695. return 0;

696. }

697. }

699. /* Ditto, but fix the stack top first. */

700. ‌void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L)

701. {

702. if (curr_funcisL(L)) L->top = curr_topL(L);

703. lj_gc_step(L);

704. }

706. #if LJ_HASJIT

707. /* Perform multiple GC steps. Called from JIT-compiled code. */

708. ‌int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps)

709. {

710. lua_State *L = gco2th(gcref(g->cur_L));

711. L->base = tvref(G(L)->jit_base);

712. L->top = curr_topL(L);

713. while (steps-- > 0 && lj_gc_step(L) == 0)

714. ;

715. /* Return 1 to force a trace exit. */

716. return (G(L)->gc.state == GCSatomic || G(L)->gc.state == GCSfinalize);

717. }

718. #endif

720. /* Perform a full GC cycle. */

721. ‌void lj_gc_fullgc(lua_State *L)

722. {

723. global_State *g = G(L);

724. int32_t ostate = g->vmstate;

725. setvmstate(g, GC);

726. if (g->gc.state <= GCSatomic) {  /* Caught somewhere in the middle. */

727. setmref(g->gc.sweep, &g->gc.root);  /* Sweep everything (preserving it). */

728. setgcrefnull(g->gc.gray);  /* Reset lists from partial propagation. */

729. setgcrefnull(g->gc.grayagain);

730. setgcrefnull(g->gc.weak);

731. g->gc.state = GCSsweepstring;  /* Fast forward to the sweep phase. */

732. g->gc.sweepstr = 0;

733. }

734. while (g->gc.state == GCSsweepstring || g->gc.state == GCSsweep)

735. gc_onestep(L);  /* Finish sweep. */

736. lua_assert(g->gc.state == GCSfinalize || g->gc.state == GCSpause);

737. /* Now perform a full GC. */

738. g->gc.state = GCSpause;

739. do { gc_onestep(L); } while (g->gc.state != GCSpause);

740. g->gc.threshold = (g->gc.estimate/100) * g->gc.pause;

741. g->vmstate = ostate;

742. }

744. /* -- Write barriers ------------------------------------------------------ */

746. /* Move the GC propagation frontier forward. */

747. ‌void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v)

748. {

749. lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));

750. lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause);

751. lua_assert(o->gch.gct != ~LJ_TTAB);

752. /* Preserve invariant during propagation. Otherwise it doesn't matter. */

753. if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic)

754. gc_mark(g, v);  /* Move frontier forward. */

755. else

756. makewhite(g, o);  /* Make it white to avoid the following barrier. */

757. }

759. /* Specialized barrier for closed upvalue. Pass &uv->tv. */

760. ‌void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv)

761. {

762. ‌#define TV2MARKED(x) \

763. (*((uint8_t *)(x) - offsetof(GCupval, tv) + offsetof(GCupval, marked)))

764. if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic)

765. gc_mark(g, gcV(tv));

766. else

767. TV2MARKED(tv) = (TV2MARKED(tv) & (uint8_t)~LJ_GC_COLORS) | curwhite(g);

768. ‌#undef TV2MARKED

769. }

771. /* Close upvalue. Also needs a write barrier. */

772. ‌void lj_gc_closeuv(global_State *g, GCupval *uv)

773. {

774. GCobj *o = obj2gco(uv);

775. /* Copy stack slot to upvalue itself and point to the copy. */

776. copyTV(mainthread(g), &uv->tv, uvval(uv));

777. setmref(uv->v, &uv->tv);

778. uv->closed = 1;

779. setgcrefr(o->gch.nextgc, g->gc.root);

780. setgcref(g->gc.root, o);

781. if (isgray(o)) {  /* A closed upvalue is never gray, so fix this. */

782. if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) {

783. gray2black(o);  /* Make it black and preserve invariant. */

784. if (tviswhite(&uv->tv))

785. lj_gc_barrierf(g, o, gcV(&uv->tv));

786. } else {

787. makewhite(g, o);  /* Make it white, i.e. sweep the upvalue. */

788. lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause);

789. }

790. }

791. }

793. #if LJ_HASJIT

794. /* Mark a trace if it's saved during the propagation phase. */

795. ‌void lj_gc_barriertrace(global_State *g, uint32_t traceno)

796. {

797. if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic)

798. gc_marktrace(g, traceno);

799. }

800. #endif

802. /* -- Allocator ----------------------------------------------------------- */

804. /* Call pluggable memory allocator to allocate or resize a fragment. */

805. ‌void *lj_mem_realloc(lua_State *L, void *p, GCSize osz, GCSize nsz)

806. {

807. global_State *g = G(L);

808. lua_assert((osz == 0) == (p == NULL));

809. p = g->allocf(g->allocd, p, osz, nsz);

810. if (p == NULL && nsz > 0)

811. lj_err_mem(L);

812. lua_assert((nsz == 0) == (p == NULL));

813. lua_assert(checkptrGC(p));

814. g->gc.total = (g->gc.total - osz) + nsz;

815. return p;

816. }

818. /* Allocate new GC object and link it to the root set. */

819. ‌void * LJ_FASTCALL lj_mem_newgco(lua_State *L, GCSize size)

820. {

821. global_State *g = G(L);

822. GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size);

823. if (o == NULL)

824. lj_err_mem(L);

825. lua_assert(checkptrGC(o));

826. g->gc.total += size;

827. setgcrefr(o->gch.nextgc, g->gc.root);

828. setgcref(g->gc.root, o);

829. newwhite(g, o);

830. return o;

831. }

833. /* Resize growable vector. */

834. ‌void *lj_mem_grow(lua_State *L, void *p, MSize *szp, MSize lim, MSize esz)

835. {

836. MSize sz = (*szp) << 1;

837. if (sz < LJ_MIN_VECSZ)

838. sz = LJ_MIN_VECSZ;

839. if (sz > lim)

840. sz = lim;

841. p = lj_mem_realloc(L, p, (*szp)*esz, sz*esz);

842. *szp = sz;

843. return p;

844. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_meta.h - luajit-2.0-src

Macros defined

- _LJ_META_H

- lj_meta_fast

- lj_meta_fastg

Source code

1. /*

2. ** Metamethod handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_META_H

7. ‌#define _LJ_META_H

9. #include "lj_obj.h"

11. /* Metamethod handling */

12. LJ_FUNC void lj_meta_init(lua_State *L);

13. LJ_FUNC cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name);

14. LJ_FUNC cTValue *lj_meta_lookup(lua_State *L, cTValue *o, MMS mm);

15. #if LJ_HASFFI

16. LJ_FUNC int lj_meta_tailcall(lua_State *L, cTValue *tv);

17. #endif

19. ‌#define lj_meta_fastg(g, mt, mm) \

20. ((mt) == NULL ? NULL : ((mt)->nomm & (1u<<(mm))) ? NULL : \

21. lj_meta_cache(mt, mm, mmname_str(g, mm)))

22. ‌#define lj_meta_fast(L, mt, mm)        lj_meta_fastg(G(L), mt, mm)

24. /* C helpers for some instructions, called from assembler VM. */

25. LJ_FUNCA cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k);

26. LJ_FUNCA TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k);

27. LJ_FUNCA TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb,

28. cTValue *rc, BCReg op);

29. LJ_FUNCA TValue *lj_meta_cat(lua_State *L, TValue *top, int left);

30. LJ_FUNCA TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o);

31. LJ_FUNCA TValue *lj_meta_equal(lua_State *L, GCobj *o1, GCobj *o2, int ne);

32. LJ_FUNCA TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins);

33. LJ_FUNCA TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op);

34. LJ_FUNCA void lj_meta_istype(lua_State *L, BCReg ra, BCReg tp);

35. LJ_FUNCA void lj_meta_call(lua_State *L, TValue *func, TValue *top);

36. LJ_FUNCA void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o);

38. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_meta.c - luajit-2.0-src

Functions defined

- lj_meta_arith

- lj_meta_cache

- lj_meta_call

- lj_meta_cat

- lj_meta_comp

- lj_meta_equal

- lj_meta_equal_cd

- lj_meta_for

- lj_meta_init

- lj_meta_istype

- lj_meta_len

- lj_meta_lookup

- lj_meta_tailcall

- lj_meta_tget

- lj_meta_tset

- mmcall

- str2num

Macros defined

- LUA_CORE

- MMNAME

- MMNAME

- lj_meta_c

Source code

1. /*

2. ** Metamethod handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lj_meta_c

10. ‌#define LUA_CORE

12. #include "lj_obj.h"

13. #include "lj_gc.h"

14. #include "lj_err.h"

15. #include "lj_buf.h"

16. #include "lj_str.h"

17. #include "lj_tab.h"

18. #include "lj_meta.h"

19. #include "lj_frame.h"

20. #include "lj_bc.h"

21. #include "lj_vm.h"

22. #include "lj_strscan.h"

23. #include "lj_strfmt.h"

24. #include "lj_lib.h"

26. /* -- Metamethod handling ------------------------------------------------- */

28. /* String interning of metamethod names for fast indexing. */

29. ‌void lj_meta_init(lua_State *L)

30. {

31. ‌#define MMNAME(name)        "__" #name

32. const char *metanames = MMDEF(MMNAME);

33. ‌#undef MMNAME

34. global_State *g = G(L);

35. const char *p, *q;

36. uint32_t mm;

37. for (mm = 0, p = metanames; *p; mm++, p = q) {

38. GCstr *s;

39. for (q = p+2; *q && *q != '_'; q++) ;

40. s = lj_str_new(L, p, (size_t)(q-p));

41. /* NOBARRIER: g->gcroot[] is a GC root. */

42. setgcref(g->gcroot[GCROOT_MMNAME+mm], obj2gco(s));

43. }

44. }

46. /* Negative caching of a few fast metamethods. See the lj_meta_fast() macro. */

47. ‌cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name)

48. {

49. cTValue *mo = lj_tab_getstr(mt, name);

50. lua_assert(mm <= MM_FAST);

51. if (!mo || tvisnil(mo)) {  /* No metamethod? */

52. mt->nomm |= (uint8_t)(1u<<mm);  /* Set negative cache flag. */

53. return NULL;

54. }

55. return mo;

56. }

58. /* Lookup metamethod for object. */

59. ‌cTValue *lj_meta_lookup(lua_State *L, cTValue *o, MMS mm)

60. {

61. GCtab *mt;

62. if (tvistab(o))

63. mt = tabref(tabV(o)->metatable);

64. else if (tvisudata(o))

65. mt = tabref(udataV(o)->metatable);

66. else

67. mt = tabref(basemt_obj(G(L), o));

68. if (mt) {

69. cTValue *mo = lj_tab_getstr(mt, mmname_str(G(L), mm));

70. if (mo)

71. return mo;

72. }

73. return niltv(L);

74. }

76. #if LJ_HASFFI

77. /* Tailcall from C function. */

78. ‌int lj_meta_tailcall(lua_State *L, cTValue *tv)

79. {

80. TValue *base = L->base;

81. TValue *top = L->top;

82. const BCIns *pc = frame_pc(base-1);  /* Preserve old PC from frame. */

83. copyTV(L, base-1-LJ_FR2, tv);  /* Replace frame with new object. */

84. if (LJ_FR2)

85. (top++)->u64 = LJ_CONT_TAILCALL;

86. else

87. top->u32.lo = LJ_CONT_TAILCALL;

88. setframe_pc(top++, pc);

89. if (LJ_FR2) top++;

90. setframe_gc(top, obj2gco(L), LJ_TTHREAD);  /* Dummy frame object. */

91. setframe_ftsz(top, ((char *)(top+1) - (char *)base) + FRAME_CONT);

92. L->base = L->top = top+1;

93. /*

94. ** before:   [old_mo|PC]    [... ...]

95. **                         ^base     ^top

96. ** after:    [new_mo|itype] [... ...] [NULL|PC] [dummy|delta]

97. **                                                           ^base/top

98. ** tailcall: [new_mo|PC]    [... ...]

99. **                         ^base     ^top

100. */

101. return 0;

102. }

103. #endif

105. /* Setup call to metamethod to be run by Assembler VM. */

106. ‌static TValue *mmcall(lua_State *L, ASMFunction cont, cTValue *mo,

107. cTValue *a, cTValue *b)

108. {

109. /*

110. **           |-- framesize -> top       top+1       top+2 top+3

111. ** before:   [func slots ...]

112. ** mm setup: [func slots ...] [cont|?]  [mo|tmtype] [a]   [b]

113. ** in asm:   [func slots ...] [cont|PC] [mo|delta]  [a]   [b]

114. **           ^-- func base                          ^-- mm base

115. ** after mm: [func slots ...]           [result]

116. **                ^-- copy to base[PC_RA] --/     for lj_cont_ra

117. **                          istruecond + branch   for lj_cont_cond*

118. **                                       ignore   for lj_cont_nop

119. ** next PC:  [func slots ...]

120. */

121. TValue *top = L->top;

122. if (curr_funcisL(L)) top = curr_topL(L);

123. setcont(top++, cont);  /* Assembler VM stores PC in upper word or FR2. */

124. if (LJ_FR2) setnilV(top++);

125. copyTV(L, top++, mo);  /* Store metamethod and two arguments. */

126. if (LJ_FR2) setnilV(top++);

127. copyTV(L, top, a);

128. copyTV(L, top+1, b);

129. return top;  /* Return new base. */

130. }

132. /* -- C helpers for some instructions, called from assembler VM ----------- */

134. /* Helper for TGET*. __index chain and metamethod. */

135. ‌cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k)

136. {

137. int loop;

138. for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) {

139. cTValue *mo;

140. if (LJ_LIKELY(tvistab(o))) {

141. GCtab *t = tabV(o);

142. cTValue *tv = lj_tab_get(L, t, k);

143. if (!tvisnil(tv) ||

144. !(mo = lj_meta_fast(L, tabref(t->metatable), MM_index)))

145. return tv;

146. } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_index))) {

147. lj_err_optype(L, o, LJ_ERR_OPINDEX);

148. return NULL;  /* unreachable */

149. }

150. if (tvisfunc(mo)) {

151. L->top = mmcall(L, lj_cont_ra, mo, o, k);

152. return NULL;  /* Trigger metamethod call. */

153. }

154. o = mo;

155. }

156. lj_err_msg(L, LJ_ERR_GETLOOP);

157. return NULL;  /* unreachable */

158. }

160. /* Helper for TSET*. __newindex chain and metamethod. */

161. ‌TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k)

162. {

163. TValue tmp;

164. int loop;

165. for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) {

166. cTValue *mo;

167. if (LJ_LIKELY(tvistab(o))) {

168. GCtab *t = tabV(o);

169. cTValue *tv = lj_tab_get(L, t, k);

170. if (LJ_LIKELY(!tvisnil(tv))) {

171. t->nomm = 0;  /* Invalidate negative metamethod cache. */

172. lj_gc_anybarriert(L, t);

173. return (TValue *)tv;

174. } else if (!(mo = lj_meta_fast(L, tabref(t->metatable), MM_newindex))) {

175. t->nomm = 0;  /* Invalidate negative metamethod cache. */

176. lj_gc_anybarriert(L, t);

177. if (tv != niltv(L))

178. return (TValue *)tv;

179. if (tvisnil(k)) lj_err_msg(L, LJ_ERR_NILIDX);

180. else if (tvisint(k)) { setnumV(&tmp, (lua_Number)intV(k)); k = &tmp; }

181. else if (tvisnum(k) && tvisnan(k)) lj_err_msg(L, LJ_ERR_NANIDX);

182. return lj_tab_newkey(L, t, k);

183. }

184. } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_newindex))) {

185. lj_err_optype(L, o, LJ_ERR_OPINDEX);

186. return NULL;  /* unreachable */

187. }

188. if (tvisfunc(mo)) {

189. L->top = mmcall(L, lj_cont_nop, mo, o, k);

190. /* L->top+2 = v filled in by caller. */

191. return NULL;  /* Trigger metamethod call. */

192. }

193. copyTV(L, &tmp, mo);

194. o = &tmp;

195. }

196. lj_err_msg(L, LJ_ERR_SETLOOP);

197. return NULL;  /* unreachable */

198. }

200. ‌static cTValue *str2num(cTValue *o, TValue *n)

201. {

202. if (tvisnum(o))

203. return o;

204. else if (tvisint(o))

205. return (setnumV(n, (lua_Number)intV(o)), n);

206. else if (tvisstr(o) && lj_strscan_num(strV(o), n))

207. return n;

208. else

209. return NULL;

210. }

212. /* Helper for arithmetic instructions. Coercion, metamethod. */

213. ‌TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, cTValue *rc,

214. BCReg op)

215. {

216. MMS mm = bcmode_mm(op);

217. TValue tempb, tempc;

218. cTValue *b, *c;

219. if ((b = str2num(rb, &tempb)) != NULL &&

220. (c = str2num(rc, &tempc)) != NULL) {  /* Try coercion first. */

221. setnumV(ra, lj_vm_foldarith(numV(b), numV(c), (int)mm-MM_add));

222. return NULL;

223. } else {

224. cTValue *mo = lj_meta_lookup(L, rb, mm);

225. if (tvisnil(mo)) {

226. mo = lj_meta_lookup(L, rc, mm);

227. if (tvisnil(mo)) {

228. if (str2num(rb, &tempb) == NULL) rc = rb;

229. lj_err_optype(L, rc, LJ_ERR_OPARITH);

230. return NULL;  /* unreachable */

231. }

232. }

233. return mmcall(L, lj_cont_ra, mo, rb, rc);

234. }

235. }

237. /* Helper for CAT. Coercion, iterative concat, __concat metamethod. */

238. ‌TValue *lj_meta_cat(lua_State *L, TValue *top, int left)

239. {

240. int fromc = 0;

241. if (left < 0) { left = -left; fromc = 1; }

242. do {

243. if (!(tvisstr(top) || tvisnumber(top)) ||

244. !(tvisstr(top-1) || tvisnumber(top-1))) {

245. cTValue *mo = lj_meta_lookup(L, top-1, MM_concat);

246. if (tvisnil(mo)) {

247. mo = lj_meta_lookup(L, top, MM_concat);

248. if (tvisnil(mo)) {

249. if (tvisstr(top-1) || tvisnumber(top-1)) top++;

250. lj_err_optype(L, top-1, LJ_ERR_OPCAT);

251. return NULL;  /* unreachable */

252. }

253. }

254. /* One of the top two elements is not a string, call __cat metamethod:

255. **

256. ** before:    [...][CAT stack .........................]

257. **                                 top-1     top         top+1 top+2

258. ** pick two:  [...][CAT stack ...] [o1]      [o2]

259. ** setup mm:  [...][CAT stack ...] [cont|?]  [mo|tmtype] [o1]  [o2]

260. ** in asm:    [...][CAT stack ...] [cont|PC] [mo|delta]  [o1]  [o2]

261. **            ^-- func base                              ^-- mm base

262. ** after mm:  [...][CAT stack ...] <--push-- [result]

263. ** next step: [...][CAT stack .............]

264. */

265. copyTV(L, top+2*LJ_FR2+2, top);  /* Carefully ordered stack copies! */

266. copyTV(L, top+2*LJ_FR2+1, top-1);

267. copyTV(L, top+LJ_FR2, mo);

268. setcont(top-1, lj_cont_cat);

269. if (LJ_FR2) { setnilV(top); setnilV(top+2); top += 2; }

270. return top+1;  /* Trigger metamethod call. */

271. } else {

272. /* Pick as many strings as possible from the top and concatenate them:

273. **

274. ** before:    [...][CAT stack ...........................]

275. ** pick str:  [...][CAT stack ...] [...... strings ......]

276. ** concat:    [...][CAT stack ...] [result]

277. ** next step: [...][CAT stack ............]

278. */

279. TValue *e, *o = top;

280. uint64_t tlen = tvisstr(o) ? strV(o)->len : STRFMT_MAXBUF_NUM;

281. char *p, *buf;

282. do {

283. o--; tlen += tvisstr(o) ? strV(o)->len : STRFMT_MAXBUF_NUM;

284. } while (--left > 0 && (tvisstr(o-1) || tvisnumber(o-1)));

285. if (tlen >= LJ_MAX_STR) lj_err_msg(L, LJ_ERR_STROV);

286. p = buf = lj_buf_tmp(L, (MSize)tlen);

287. for (e = top, top = o; o <= e; o++) {

288. if (tvisstr(o)) {

289. GCstr *s = strV(o);

290. MSize len = s->len;

291. p = lj_buf_wmem(p, strdata(s), len);

292. } else if (tvisint(o)) {

293. p = lj_strfmt_wint(p, intV(o));

294. } else {

295. lua_assert(tvisnum(o));

296. p = lj_strfmt_wnum(p, o);

297. }

298. }

299. setstrV(L, top, lj_str_new(L, buf, (size_t)(p-buf)));

300. }

301. } while (left >= 1);

302. if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) {

303. if (!fromc) L->top = curr_topL(L);

304. lj_gc_step(L);

305. }

306. return NULL;

307. }

309. /* Helper for LEN. __len metamethod. */

310. ‌TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o)

311. {

312. cTValue *mo = lj_meta_lookup(L, o, MM_len);

313. if (tvisnil(mo)) {

314. if (LJ_52 && tvistab(o))

315. tabref(tabV(o)->metatable)->nomm |= (uint8_t)(1u<<MM_len);

316. else

317. lj_err_optype(L, o, LJ_ERR_OPLEN);

318. return NULL;

319. }

320. return mmcall(L, lj_cont_ra, mo, o, LJ_52 ? o : niltv(L));

321. }

323. /* Helper for equality comparisons. __eq metamethod. */

324. ‌TValue *lj_meta_equal(lua_State *L, GCobj *o1, GCobj *o2, int ne)

325. {

326. /* Field metatable must be at same offset for GCtab and GCudata! */

327. cTValue *mo = lj_meta_fast(L, tabref(o1->gch.metatable), MM_eq);

328. if (mo) {

329. TValue *top;

330. uint32_t it;

331. if (tabref(o1->gch.metatable) != tabref(o2->gch.metatable)) {

332. cTValue *mo2 = lj_meta_fast(L, tabref(o2->gch.metatable), MM_eq);

333. if (mo2 == NULL || !lj_obj_equal(mo, mo2))

334. return (TValue *)(intptr_t)ne;

335. }

336. top = curr_top(L);

337. setcont(top++, ne ? lj_cont_condf : lj_cont_condt);

338. if (LJ_FR2) setnilV(top++);

339. copyTV(L, top++, mo);

340. if (LJ_FR2) setnilV(top++);

341. it = ~(uint32_t)o1->gch.gct;

342. setgcV(L, top, o1, it);

343. setgcV(L, top+1, o2, it);

344. return top;  /* Trigger metamethod call. */

345. }

346. return (TValue *)(intptr_t)ne;

347. }

349. #if LJ_HASFFI

350. ‌TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins)

351. {

352. ASMFunction cont = (bc_op(ins) & 1) ? lj_cont_condf : lj_cont_condt;

353. int op = (int)bc_op(ins) & ~1;

354. TValue tv;

355. cTValue *mo, *o2, *o1 = &L->base[bc_a(ins)];

356. cTValue *o1mm = o1;

357. if (op == BC_ISEQV) {

358. o2 = &L->base[bc_d(ins)];

359. if (!tviscdata(o1mm)) o1mm = o2;

360. } else if (op == BC_ISEQS) {

361. setstrV(L, &tv, gco2str(proto_kgc(curr_proto(L), ~(ptrdiff_t)bc_d(ins))));

362. o2 = &tv;

363. } else if (op == BC_ISEQN) {

364. o2 = &mref(curr_proto(L)->k, cTValue)[bc_d(ins)];

365. } else {

366. lua_assert(op == BC_ISEQP);

367. setpriV(&tv, ~bc_d(ins));

368. o2 = &tv;

369. }

370. mo = lj_meta_lookup(L, o1mm, MM_eq);

371. if (LJ_LIKELY(!tvisnil(mo)))

372. return mmcall(L, cont, mo, o1, o2);

373. else

374. return (TValue *)(intptr_t)(bc_op(ins) & 1);

375. }

376. #endif

378. /* Helper for ordered comparisons. String compare, __lt/__le metamethods. */

379. ‌TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op)

380. {

381. if (LJ_HASFFI && (tviscdata(o1) || tviscdata(o2))) {

382. ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt;

383. MMS mm = (op & 2) ? MM_le : MM_lt;

384. cTValue *mo = lj_meta_lookup(L, tviscdata(o1) ? o1 : o2, mm);

385. if (LJ_UNLIKELY(tvisnil(mo))) goto err;

386. return mmcall(L, cont, mo, o1, o2);

387. } else if (LJ_52 || itype(o1) == itype(o2)) {

388. /* Never called with two numbers. */

389. if (tvisstr(o1) && tvisstr(o2)) {

390. int32_t res = lj_str_cmp(strV(o1), strV(o2));

391. return (TValue *)(intptr_t)(((op&2) ? res <= 0 : res < 0) ^ (op&1));

392. } else {

393. trymt:

394. while (1) {

395. ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt;

396. MMS mm = (op & 2) ? MM_le : MM_lt;

397. cTValue *mo = lj_meta_lookup(L, o1, mm);

398. #if LJ_52

399. if (tvisnil(mo) && tvisnil((mo = lj_meta_lookup(L, o2, mm))))

400. #else

401. cTValue *mo2 = lj_meta_lookup(L, o2, mm);

402. if (tvisnil(mo) || !lj_obj_equal(mo, mo2))

403. #endif

404. {

405. if (op & 2) {  /* MM_le not found: retry with MM_lt. */

406. cTValue *ot = o1; o1 = o2; o2 = ot;  /* Swap operands. */

407. op ^= 3;  /* Use LT and flip condition. */

408. continue;

409. }

410. goto err;

411. }

412. return mmcall(L, cont, mo, o1, o2);

413. }

414. }

415. } else if (tvisbool(o1) && tvisbool(o2)) {

416. goto trymt;

417. } else {

418. err:

419. lj_err_comp(L, o1, o2);

420. return NULL;

421. }

422. }

424. /* Helper for ISTYPE and ISNUM. Implicit coercion or error. */

425. ‌void lj_meta_istype(lua_State *L, BCReg ra, BCReg tp)

426. {

427. L->top = curr_topL(L);

428. ra++; tp--;

429. lua_assert(LJ_DUALNUM || tp != ~LJ_TNUMX);  /* ISTYPE -> ISNUM broken. */

430. if (LJ_DUALNUM && tp == ~LJ_TNUMX) lj_lib_checkint(L, ra);

431. else if (tp == ~LJ_TNUMX+1) lj_lib_checknum(L, ra);

432. else if (tp == ~LJ_TSTR) lj_lib_checkstr(L, ra);

433. else lj_err_argtype(L, ra, lj_obj_itypename[tp]);

434. }

436. /* Helper for calls. __call metamethod. */

437. ‌void lj_meta_call(lua_State *L, TValue *func, TValue *top)

438. {

439. cTValue *mo = lj_meta_lookup(L, func, MM_call);

440. TValue *p;

441. if (!tvisfunc(mo))

442. lj_err_optype_call(L, func);

443. for (p = top; p > func+2*LJ_FR2; p--) copyTV(L, p, p-1);

444. if (LJ_FR2) copyTV(L, func+2, func);

445. copyTV(L, func, mo);

446. }

448. /* Helper for FORI. Coercion. */

449. ‌void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o)

450. {

451. if (!lj_strscan_numberobj(o)) lj_err_msg(L, LJ_ERR_FORINIT);

452. if (!lj_strscan_numberobj(o+1)) lj_err_msg(L, LJ_ERR_FORLIM);

453. if (!lj_strscan_numberobj(o+2)) lj_err_msg(L, LJ_ERR_FORSTEP);

454. if (LJ_DUALNUM) {

455. /* Ensure all slots are integers or all slots are numbers. */

456. int32_t k[3];

457. int nint = 0;

458. ptrdiff_t i;

459. for (i = 0; i <= 2; i++) {

460. if (tvisint(o+i)) {

461. k[i] = intV(o+i); nint++;

462. } else {

463. k[i] = lj_num2int(numV(o+i)); nint += ((lua_Number)k[i] == numV(o+i));

464. }

465. }

466. if (nint == 3) {  /* Narrow to integers. */

467. setintV(o, k[0]);

468. setintV(o+1, k[1]);

469. setintV(o+2, k[2]);

470. } else if (nint != 0) {  /* Widen to numbers. */

471. if (tvisint(o)) setnumV(o, (lua_Number)intV(o));

472. if (tvisint(o+1)) setnumV(o+1, (lua_Number)intV(o+1));

473. if (tvisint(o+2)) setnumV(o+2, (lua_Number)intV(o+2));

474. }

475. }

476. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_str.c - luajit-2.0-src

Functions defined

- lj_str_cmp

- lj_str_find

- lj_str_free

- lj_str_haspattern

- lj_str_new

- lj_str_resize

- str_fastcmp

Macros defined

- LUA_CORE

- lj_str_c

Source code

1. /*

2. ** String handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_str_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_str.h"

13. #include "lj_char.h"

15. /* -- String helpers ------------------------------------------------------ */

17. /* Ordered compare of strings. Assumes string data is 4-byte aligned. */

18. ‌int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b)

19. {

20. MSize i, n = a->len > b->len ? b->len : a->len;

21. for (i = 0; i < n; i += 4) {

22. /* Note: innocuous access up to end of string + 3. */

23. uint32_t va = *(const uint32_t *)(strdata(a)+i);

24. uint32_t vb = *(const uint32_t *)(strdata(b)+i);

25. if (va != vb) {

26. #if LJ_LE

27. va = lj_bswap(va); vb = lj_bswap(vb);

28. #endif

29. i -= n;

30. if ((int32_t)i >= -3) {

31. va >>= 32+(i<<3); vb >>= 32+(i<<3);

32. if (va == vb) break;

33. }

34. return va < vb ? -1 : 1;

35. }

36. }

37. return (int32_t)(a->len - b->len);

38. }

40. /* Fast string data comparison. Caveat: unaligned access to 1st string! */

41. ‌static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len)

42. {

43. MSize i = 0;

44. lua_assert(len > 0);

45. lua_assert((((uintptr_t)a+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4);

46. do {  /* Note: innocuous access up to end of string + 3. */

47. uint32_t v = lj_getu32(a+i) ^ *(const uint32_t *)(b+i);

48. if (v) {

49. i -= len;

50. #if LJ_LE

51. return (int32_t)i >= -3 ? (v << (32+(i<<3))) : 1;

52. #else

53. return (int32_t)i >= -3 ? (v >> (32+(i<<3))) : 1;

54. #endif

55. }

56. i += 4;

57. } while (i < len);

58. return 0;

59. }

61. /* Find fixed string p inside string s. */

62. ‌const char *lj_str_find(const char *s, const char *p, MSize slen, MSize plen)

63. {

64. if (plen <= slen) {

65. if (plen == 0) {

66. return s;

67. } else {

68. int c = *(const uint8_t *)p++;

69. plen--; slen -= plen;

70. while (slen) {

71. const char *q = (const char *)memchr(s, c, slen);

72. if (!q) break;

73. if (memcmp(q+1, p, plen) == 0) return q;

74. q++; slen -= (MSize)(q-s); s = q;

75. }

76. }

77. }

78. return NULL;

79. }

81. /* Check whether a string has a pattern matching character. */

82. ‌int lj_str_haspattern(GCstr *s)

83. {

84. const char *p = strdata(s), *q = p + s->len;

85. while (p < q) {

86. int c = *(const uint8_t *)p++;

87. if (lj_char_ispunct(c) && strchr("^$*+?.([%-", c))

88. return 1;  /* Found a pattern matching char. */

89. }

90. return 0;  /* No pattern matching chars found. */

91. }

93. /* -- String interning ---------------------------------------------------- */

95. /* Resize the string hash table (grow and shrink). */

96. ‌void lj_str_resize(lua_State *L, MSize newmask)

97. {

98. global_State *g = G(L);

99. GCRef *newhash;

100. MSize i;

101. if (g->gc.state == GCSsweepstring || newmask >= LJ_MAX_STRTAB-1)

102. return;  /* No resizing during GC traversal or if already too big. */

103. newhash = lj_mem_newvec(L, newmask+1, GCRef);

104. memset(newhash, 0, (newmask+1)*sizeof(GCRef));

105. for (i = g->strmask; i != ~(MSize)0; i--) {  /* Rehash old table. */

106. GCobj *p = gcref(g->strhash[i]);

107. while (p) {  /* Follow each hash chain and reinsert all strings. */

108. MSize h = gco2str(p)->hash & newmask;

109. GCobj *next = gcnext(p);

110. /* NOBARRIER: The string table is a GC root. */

111. setgcrefr(p->gch.nextgc, newhash[h]);

112. setgcref(newhash[h], p);

113. p = next;

114. }

115. }

116. lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef);

117. g->strmask = newmask;

118. g->strhash = newhash;

119. }

121. /* Intern a string and return string object. */

122. ‌GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)

123. {

124. global_State *g;

125. GCstr *s;

126. GCobj *o;

127. MSize len = (MSize)lenx;

128. MSize a, b, h = len;

129. if (lenx >= LJ_MAX_STR)

130. lj_err_msg(L, LJ_ERR_STROV);

131. g = G(L);

132. /* Compute string hash. Constants taken from lookup3 hash by Bob Jenkins. */

133. if (len >= 4) {  /* Caveat: unaligned access! */

134. a = lj_getu32(str);

135. h ^= lj_getu32(str+len-4);

136. b = lj_getu32(str+(len>>1)-2);

137. h ^= b; h -= lj_rol(b, 14);

138. b += lj_getu32(str+(len>>2)-1);

139. } else if (len > 0) {

140. a = *(const uint8_t *)str;

141. h ^= *(const uint8_t *)(str+len-1);

142. b = *(const uint8_t *)(str+(len>>1));

143. h ^= b; h -= lj_rol(b, 14);

144. } else {

145. return &g->strempty;

146. }

147. a ^= h; a -= lj_rol(h, 11);

148. b ^= a; b -= lj_rol(a, 25);

149. h ^= b; h -= lj_rol(b, 16);

150. /* Check if the string has already been interned. */

151. o = gcref(g->strhash[h & g->strmask]);

152. if (LJ_LIKELY((((uintptr_t)str+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4)) {

153. while (o != NULL) {

154. GCstr *sx = gco2str(o);

155. if (sx->len == len && str_fastcmp(str, strdata(sx), len) == 0) {

156. /* Resurrect if dead. Can only happen with fixstring() (keywords). */

157. if (isdead(g, o)) flipwhite(o);

158. return sx;  /* Return existing string. */

159. }

160. o = gcnext(o);

161. }

162. } else {  /* Slow path: end of string is too close to a page boundary. */

163. while (o != NULL) {

164. GCstr *sx = gco2str(o);

165. if (sx->len == len && memcmp(str, strdata(sx), len) == 0) {

166. /* Resurrect if dead. Can only happen with fixstring() (keywords). */

167. if (isdead(g, o)) flipwhite(o);

168. return sx;  /* Return existing string. */

169. }

170. o = gcnext(o);

171. }

172. }

173. /* Nope, create a new string. */

174. s = lj_mem_newt(L, sizeof(GCstr)+len+1, GCstr);

175. newwhite(g, s);

176. s->gct = ~LJ_TSTR;

177. s->len = len;

178. s->hash = h;

179. s->reserved = 0;

180. memcpy(strdatawr(s), str, len);

181. strdatawr(s)[len] = '\0';  /* Zero-terminate string. */

182. /* Add it to string hash table. */

183. h &= g->strmask;

184. s->nextgc = g->strhash[h];

185. /* NOBARRIER: The string table is a GC root. */

186. setgcref(g->strhash[h], obj2gco(s));

187. if (g->strnum++ > g->strmask)  /* Allow a 100% load factor. */

188. lj_str_resize(L, (g->strmask<<1)+1);  /* Grow string table. */

189. return s;  /* Return newly interned string. */

190. }

192. ‌void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s)

193. {

194. g->strnum--;

195. lj_mem_free(g, s, sizestring(s));

196. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_char.h - luajit-2.0-src

Global variables defined

- lj_char_bits

Macros defined

- LJ_CHAR_ALNUM

- LJ_CHAR_ALPHA

- LJ_CHAR_CNTRL

- LJ_CHAR_DIGIT

- LJ_CHAR_GRAPH

- LJ_CHAR_IDENT

- LJ_CHAR_LOWER

- LJ_CHAR_PUNCT

- LJ_CHAR_SPACE

- LJ_CHAR_UPPER

- LJ_CHAR_XDIGIT

- _LJ_CHAR_H

- lj_char_isa

- lj_char_isalnum

- lj_char_isalpha

- lj_char_iscntrl

- lj_char_isdigit

- lj_char_isgraph

- lj_char_isident

- lj_char_islower

- lj_char_ispunct

- lj_char_isspace

- lj_char_isupper

- lj_char_isxdigit

- lj_char_tolower

- lj_char_toupper

Source code

1. /*

2. ** Character types.

3. ** Donated to the public domain.

4. */

6. #ifndef _LJ_CHAR_H

7. ‌#define _LJ_CHAR_H

9. #include "lj_def.h"

11. ‌#define LJ_CHAR_CNTRL        0x01

12. ‌#define LJ_CHAR_SPACE        0x02

13. ‌#define LJ_CHAR_PUNCT        0x04

14. ‌#define LJ_CHAR_DIGIT        0x08

15. ‌#define LJ_CHAR_XDIGIT        0x10

16. ‌#define LJ_CHAR_UPPER        0x20

17. ‌#define LJ_CHAR_LOWER        0x40

18. ‌#define LJ_CHAR_IDENT        0x80

19. ‌#define LJ_CHAR_ALPHA        (LJ_CHAR_LOWER|LJ_CHAR_UPPER)

20. ‌#define LJ_CHAR_ALNUM        (LJ_CHAR_ALPHA|LJ_CHAR_DIGIT)

21. ‌#define LJ_CHAR_GRAPH        (LJ_CHAR_ALNUM|LJ_CHAR_PUNCT)

23. /* Only pass -1 or 0..255 to these macros. Never pass a signed char! */

24. ‌#define lj_char_isa(c, t)        ((lj_char_bits+1)[(c)] & t)

25. ‌#define lj_char_iscntrl(c)        lj_char_isa((c), LJ_CHAR_CNTRL)

26. ‌#define lj_char_isspace(c)        lj_char_isa((c), LJ_CHAR_SPACE)

27. ‌#define lj_char_ispunct(c)        lj_char_isa((c), LJ_CHAR_PUNCT)

28. ‌#define lj_char_isdigit(c)        lj_char_isa((c), LJ_CHAR_DIGIT)

29. ‌#define lj_char_isxdigit(c)        lj_char_isa((c), LJ_CHAR_XDIGIT)

30. ‌#define lj_char_isupper(c)        lj_char_isa((c), LJ_CHAR_UPPER)

31. ‌#define lj_char_islower(c)        lj_char_isa((c), LJ_CHAR_LOWER)

32. ‌#define lj_char_isident(c)        lj_char_isa((c), LJ_CHAR_IDENT)

33. ‌#define lj_char_isalpha(c)        lj_char_isa((c), LJ_CHAR_ALPHA)

34. ‌#define lj_char_isalnum(c)        lj_char_isa((c), LJ_CHAR_ALNUM)

35. ‌#define lj_char_isgraph(c)        lj_char_isa((c), LJ_CHAR_GRAPH)

37. ‌#define lj_char_toupper(c)        ((c) - (lj_char_islower(c) >> 1))

38. ‌#define lj_char_tolower(c)        ((c) + lj_char_isupper(c))

40. ‌LJ_DATA const uint8_t lj_char_bits[257];

42. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_frame.h - luajit-2.0-src

Macros defined

- CFRAME_OFS_ERRF

- CFRAME_OFS_ERRF

- CFRAME_OFS_ERRF

- CFRAME_OFS_ERRF

- CFRAME_OFS_ERRF

- CFRAME_OFS_ERRF

- CFRAME_OFS_ERRF

- CFRAME_OFS_ERRF

- CFRAME_OFS_ERRF

- CFRAME_OFS_L

- CFRAME_OFS_L

- CFRAME_OFS_L

- CFRAME_OFS_L

- CFRAME_OFS_L

- CFRAME_OFS_L

- CFRAME_OFS_L

- CFRAME_OFS_L

- CFRAME_OFS_L

- CFRAME_OFS_MULTRES

- CFRAME_OFS_MULTRES

- CFRAME_OFS_MULTRES

- CFRAME_OFS_MULTRES

- CFRAME_OFS_MULTRES

- CFRAME_OFS_MULTRES

- CFRAME_OFS_MULTRES

- CFRAME_OFS_MULTRES

- CFRAME_OFS_MULTRES

- CFRAME_OFS_NRES

- CFRAME_OFS_NRES

- CFRAME_OFS_NRES

- CFRAME_OFS_NRES

- CFRAME_OFS_NRES

- CFRAME_OFS_NRES

- CFRAME_OFS_NRES

- CFRAME_OFS_NRES

- CFRAME_OFS_NRES

- CFRAME_OFS_PC

- CFRAME_OFS_PC

- CFRAME_OFS_PC

- CFRAME_OFS_PC

- CFRAME_OFS_PC

- CFRAME_OFS_PC

- CFRAME_OFS_PC

- CFRAME_OFS_PC

- CFRAME_OFS_PC

- CFRAME_OFS_PREV

- CFRAME_OFS_PREV

- CFRAME_OFS_PREV

- CFRAME_OFS_PREV

- CFRAME_OFS_PREV

- CFRAME_OFS_PREV

- CFRAME_OFS_PREV

- CFRAME_OFS_PREV

- CFRAME_OFS_PREV

- CFRAME_RAWMASK

- CFRAME_RESUME

- CFRAME_SHIFT_MULTRES

- CFRAME_SHIFT_MULTRES

- CFRAME_SHIFT_MULTRES

- CFRAME_SHIFT_MULTRES

- CFRAME_SHIFT_MULTRES

- CFRAME_SHIFT_MULTRES

- CFRAME_SHIFT_MULTRES

- CFRAME_SHIFT_MULTRES

- CFRAME_SHIFT_MULTRES

- CFRAME_SIZE

- CFRAME_SIZE

- CFRAME_SIZE

- CFRAME_SIZE

- CFRAME_SIZE

- CFRAME_SIZE

- CFRAME_SIZE

- CFRAME_SIZE

- CFRAME_SIZE

- CFRAME_SIZE

- CFRAME_SIZE_JIT

- CFRAME_SIZE_JIT

- CFRAME_SIZE_JIT

- CFRAME_UNWIND_FF

- FRAME_P

- FRAME_TYPE

- FRAME_TYPEP

- _LJ_FRAME_H

- cframe_L

- cframe_Lpc

- cframe_canyield

- cframe_errfunc

- cframe_multres

- cframe_multres_n

- cframe_nres

- cframe_pc

- cframe_prev

- cframe_raw

- cframe_unwind_ff

- frame_contf

- frame_contf

- frame_contf

- frame_contpc

- frame_contpc

- frame_contv

- frame_contv

- frame_delta

- frame_ftsz

- frame_ftsz

- frame_func

- frame_gc

- frame_gc

- frame_isc

- frame_iscont

- frame_iscont_fficb

- frame_islua

- frame_ispcall

- frame_isvarg

- frame_pc

- frame_pc

- frame_prev

- frame_prevd

- frame_prevl

- frame_sized

- frame_type

- frame_typep

- setcframe_L

- setcframe_pc

- setframe_ftsz

- setframe_ftsz

- setframe_gc

- setframe_gc

- setframe_pc

- setframe_pc

Source code

1. /*

2. ** Stack frames.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_FRAME_H

7. ‌#define _LJ_FRAME_H

9. #include "lj_obj.h"

10. #include "lj_bc.h"

12. /* -- Lua stack frame ----------------------------------------------------- */

14. /* Frame type markers in LSB of PC (4-byte aligned) or delta (8-byte aligned:

15. **

16. **    PC  00  Lua frame

17. ** delta 001  C frame

18. ** delta 010  Continuation frame

19. ** delta 011  Lua vararg frame

20. ** delta 101  cpcall() frame

21. ** delta 110  ff pcall() frame

22. ** delta 111  ff pcall() frame with active hook

23. */

24. enum {

25. FRAME_LUA, FRAME_C, FRAME_CONT, FRAME_VARG,

26. FRAME_LUAP, FRAME_CP, FRAME_PCALL, FRAME_PCALLH

27. };

28. ‌#define FRAME_TYPE                3

29. ‌#define FRAME_P                        4

30. ‌#define FRAME_TYPEP                (FRAME_TYPE|FRAME_P)

32. /* Macros to access and modify Lua frames. */

33. #if LJ_FR2

34. /* Two-slot frame info, required for 64 bit PC/GCRef:

35. **

36. **                   base-2  base-1      |  base  base+1 ...

37. **                  [func   PC/delta/ft] | [slots ...]

38. **                  ^-- frame            | ^-- base   ^-- top

39. **

40. ** Continuation frames:

41. **

42. **   base-4  base-3  base-2  base-1      |  base  base+1 ...

43. **  [cont      PC ] [func   PC/delta/ft] | [slots ...]

44. **                  ^-- frame            | ^-- base   ^-- top

45. */

46. ‌#define frame_gc(f)                (gcval((f)-1))

47. ‌#define frame_ftsz(f)                ((ptrdiff_t)(f)->ftsz)

48. ‌#define frame_pc(f)                ((const BCIns *)frame_ftsz(f))

49. ‌#define setframe_gc(f, p, tp)        (setgcVraw((f)-1, (p), (tp)))

50. ‌#define setframe_ftsz(f, sz)        ((f)->ftsz = (sz))

51. ‌#define setframe_pc(f, pc)        ((f)->ftsz = (int64_t)(intptr_t)(pc))

52. #else

53. /* One-slot frame info, sufficient for 32 bit PC/GCRef:

54. **

55. **              base-1              |  base  base+1 ...

56. **              lo     hi           |

57. **             [func | PC/delta/ft] | [slots ...]

58. **             ^-- frame            | ^-- base   ^-- top

59. **

60. ** Continuation frames:

61. **

62. **  base-2      base-1              |  base  base+1 ...

63. **  lo     hi   lo     hi           |

64. ** [cont | PC] [func | PC/delta/ft] | [slots ...]

65. **             ^-- frame            | ^-- base   ^-- top

66. */

67. ‌#define frame_gc(f)                (gcref((f)->fr.func))

68. ‌#define frame_ftsz(f)                ((ptrdiff_t)(f)->fr.tp.ftsz)

69. ‌#define frame_pc(f)                (mref((f)->fr.tp.pcr, const BCIns))

70. ‌#define setframe_gc(f, p, tp)        (setgcref((f)->fr.func, (p)), UNUSED(tp))

71. ‌#define setframe_ftsz(f, sz)        ((f)->fr.tp.ftsz = (int32_t)(sz))

72. ‌#define setframe_pc(f, pc)        (setmref((f)->fr.tp.pcr, (pc)))

73. #endif

75. ‌#define frame_type(f)                (frame_ftsz(f) & FRAME_TYPE)

76. ‌#define frame_typep(f)                (frame_ftsz(f) & FRAME_TYPEP)

77. ‌#define frame_islua(f)                (frame_type(f) == FRAME_LUA)

78. ‌#define frame_isc(f)                (frame_type(f) == FRAME_C)

79. ‌#define frame_iscont(f)                (frame_typep(f) == FRAME_CONT)

80. ‌#define frame_isvarg(f)                (frame_typep(f) == FRAME_VARG)

81. ‌#define frame_ispcall(f)        ((frame_ftsz(f) & 6) == FRAME_PCALL)

83. ‌#define frame_func(f)                (&frame_gc(f)->fn)

84. ‌#define frame_delta(f)                (frame_ftsz(f) >> 3)

85. ‌#define frame_sized(f)                (frame_ftsz(f) & ~FRAME_TYPEP)

87. enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK };  /* Special continuations. */

89. #if LJ_FR2

90. ‌#define frame_contpc(f)                (frame_pc((f)-2))

91. ‌#define frame_contv(f)                (((f)-3)->u64)

92. #else

93. ‌#define frame_contpc(f)                (frame_pc((f)-1))

94. ‌#define frame_contv(f)                (((f)-1)->u32.lo)

95. #endif

96. #if LJ_FR2

97. ‌#define frame_contf(f)                ((ASMFunction)(uintptr_t)((f)-3)->u64)

98. #elif LJ_64

99. ‌#define frame_contf(f) \

100. ((ASMFunction)(void *)((intptr_t)lj_vm_asm_begin + \

101. (intptr_t)(int32_t)((f)-1)->u32.lo))

102. #else

103. ‌#define frame_contf(f)                ((ASMFunction)gcrefp(((f)-1)->gcr, void))

104. #endif

105. ‌#define frame_iscont_fficb(f) \

106. (LJ_HASFFI && frame_contv(f) == LJ_CONT_FFI_CALLBACK)

108. ‌#define frame_prevl(f)                ((f) - (1+LJ_FR2+bc_a(frame_pc(f)[-1])))

109. ‌#define frame_prevd(f)                ((TValue *)((char *)(f) - frame_sized(f)))

110. ‌#define frame_prev(f)                (frame_islua(f)?frame_prevl(f):frame_prevd(f))

111. /* Note: this macro does not skip over FRAME_VARG. */

113. /* -- C stack frame ------------------------------------------------------- */

115. /* Macros to access and modify the C stack frame chain. */

117. /* These definitions must match with the arch-specific *.dasc files. */

118. #if LJ_TARGET_X86

119. ‌#define CFRAME_OFS_ERRF                (15*4)

120. ‌#define CFRAME_OFS_NRES                (14*4)

121. ‌#define CFRAME_OFS_PREV                (13*4)

122. ‌#define CFRAME_OFS_L                (12*4)

123. ‌#define CFRAME_OFS_PC                (6*4)

124. ‌#define CFRAME_OFS_MULTRES        (5*4)

125. ‌#define CFRAME_SIZE                (12*4)

126. ‌#define CFRAME_SHIFT_MULTRES        0

127. #elif LJ_TARGET_X64

128. #if LJ_ABI_WIN

129. ‌#define CFRAME_OFS_PREV                (13*8)

130. ‌#define CFRAME_OFS_PC                (25*4)

131. ‌#define CFRAME_OFS_L                (24*4)

132. ‌#define CFRAME_OFS_ERRF                (23*4)

133. ‌#define CFRAME_OFS_NRES                (22*4)

134. ‌#define CFRAME_OFS_MULTRES        (21*4)

135. ‌#define CFRAME_SIZE                (10*8)

136. ‌#define CFRAME_SIZE_JIT                (CFRAME_SIZE + 9*16 + 4*8)

137. ‌#define CFRAME_SHIFT_MULTRES        0

138. #else

139. ‌#define CFRAME_OFS_PREV                (4*8)

140. ‌#define CFRAME_OFS_PC                (7*4)

141. ‌#define CFRAME_OFS_L                (6*4)

142. ‌#define CFRAME_OFS_ERRF                (5*4)

143. ‌#define CFRAME_OFS_NRES                (4*4)

144. ‌#define CFRAME_OFS_MULTRES        (1*4)

145. ‌#define CFRAME_SIZE                (10*8)

146. ‌#define CFRAME_SIZE_JIT                (CFRAME_SIZE + 16)

147. ‌#define CFRAME_SHIFT_MULTRES        0

148. #endif

149. #elif LJ_TARGET_ARM

150. ‌#define CFRAME_OFS_ERRF                24

151. ‌#define CFRAME_OFS_NRES                20

152. ‌#define CFRAME_OFS_PREV                16

153. ‌#define CFRAME_OFS_L                12

154. ‌#define CFRAME_OFS_PC                8

155. ‌#define CFRAME_OFS_MULTRES        4

156. #if LJ_ARCH_HASFPU

157. ‌#define CFRAME_SIZE                128

158. #else

159. ‌#define CFRAME_SIZE                64

160. #endif

161. ‌#define CFRAME_SHIFT_MULTRES        3

162. #elif LJ_TARGET_ARM64

163. ‌#define CFRAME_OFS_ERRF                196

164. ‌#define CFRAME_OFS_NRES                200

165. ‌#define CFRAME_OFS_PREV                160

166. ‌#define CFRAME_OFS_L                176

167. ‌#define CFRAME_OFS_PC                168

168. ‌#define CFRAME_OFS_MULTRES        192

169. ‌#define CFRAME_SIZE                208

170. ‌#define CFRAME_SHIFT_MULTRES        3

171. #elif LJ_TARGET_PPC

172. #if LJ_TARGET_XBOX360

173. ‌#define CFRAME_OFS_ERRF                424

174. ‌#define CFRAME_OFS_NRES                420

175. ‌#define CFRAME_OFS_PREV                400

176. ‌#define CFRAME_OFS_L                416

177. ‌#define CFRAME_OFS_PC                412

178. ‌#define CFRAME_OFS_MULTRES        408

179. ‌#define CFRAME_SIZE                384

180. ‌#define CFRAME_SHIFT_MULTRES        3

181. #elif LJ_ARCH_PPC32ON64

182. ‌#define CFRAME_OFS_ERRF                472

183. ‌#define CFRAME_OFS_NRES                468

184. ‌#define CFRAME_OFS_PREV                448

185. ‌#define CFRAME_OFS_L                464

186. ‌#define CFRAME_OFS_PC                460

187. ‌#define CFRAME_OFS_MULTRES        456

188. ‌#define CFRAME_SIZE                400

189. ‌#define CFRAME_SHIFT_MULTRES        3

190. #else

191. ‌#define CFRAME_OFS_ERRF                48

192. ‌#define CFRAME_OFS_NRES                44

193. ‌#define CFRAME_OFS_PREV                40

194. ‌#define CFRAME_OFS_L                36

195. ‌#define CFRAME_OFS_PC                32

196. ‌#define CFRAME_OFS_MULTRES        28

197. ‌#define CFRAME_SIZE                272

198. ‌#define CFRAME_SHIFT_MULTRES        3

199. #endif

200. #elif LJ_TARGET_MIPS

201. ‌#define CFRAME_OFS_ERRF                124

202. ‌#define CFRAME_OFS_NRES                120

203. ‌#define CFRAME_OFS_PREV                116

204. ‌#define CFRAME_OFS_L                112

205. ‌#define CFRAME_OFS_PC                20

206. ‌#define CFRAME_OFS_MULTRES        16

207. ‌#define CFRAME_SIZE                112

208. ‌#define CFRAME_SHIFT_MULTRES        3

209. #else

210. #error "Missing CFRAME_* definitions for this architecture"

211. #endif

213. #ifndef CFRAME_SIZE_JIT

214. ‌#define CFRAME_SIZE_JIT                CFRAME_SIZE

215. #endif

217. ‌#define CFRAME_RESUME                1

218. ‌#define CFRAME_UNWIND_FF        2  /* Only used in unwinder. */

219. ‌#define CFRAME_RAWMASK                (~(intptr_t)(CFRAME_RESUME|CFRAME_UNWIND_FF))

221. ‌#define cframe_errfunc(cf)        (*(int32_t *)(((char *)(cf))+CFRAME_OFS_ERRF))

222. ‌#define cframe_nres(cf)                (*(int32_t *)(((char *)(cf))+CFRAME_OFS_NRES))

223. ‌#define cframe_prev(cf)                (*(void **)(((char *)(cf))+CFRAME_OFS_PREV))

224. ‌#define cframe_multres(cf)  (*(uint32_t *)(((char *)(cf))+CFRAME_OFS_MULTRES))

225. ‌#define cframe_multres_n(cf)        (cframe_multres((cf)) >> CFRAME_SHIFT_MULTRES)

226. ‌#define cframe_L(cf) \

227. (&gcref(*(GCRef *)(((char *)(cf))+CFRAME_OFS_L))->th)

228. ‌#define cframe_pc(cf) \

229. (mref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), const BCIns))

230. ‌#define setcframe_L(cf, L) \

231. (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_L), (L)))

232. ‌#define setcframe_pc(cf, pc) \

233. (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), (pc)))

234. ‌#define cframe_canyield(cf)        ((intptr_t)(cf) & CFRAME_RESUME)

235. ‌#define cframe_unwind_ff(cf)        ((intptr_t)(cf) & CFRAME_UNWIND_FF)

236. ‌#define cframe_raw(cf)                ((void *)((intptr_t)(cf) & CFRAME_RAWMASK))

237. ‌#define cframe_Lpc(L)                cframe_pc(cframe_raw(L->cframe))

239. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_api.c - luajit-2.0-src

Functions defined

- api_call_base

- cpcall

- getcurrenv

- index2adr

- luaL_callmeta

- luaL_checkany

- luaL_checkinteger

- luaL_checklstring

- luaL_checknumber

- luaL_checkoption

- luaL_checkstack

- luaL_checktype

- luaL_checkudata

- luaL_getmetafield

- luaL_newmetatable

- luaL_optinteger

- luaL_optlstring

- luaL_optnumber

- lua_call

- lua_checkstack

- lua_concat

- lua_cpcall

- lua_createtable

- lua_equal

- lua_gc

- lua_getallocf

- lua_getfenv

- lua_getfield

- lua_getmetatable

- lua_gettable

- lua_gettop

- lua_getupvalue

- lua_insert

- lua_iscfunction

- lua_isnumber

- lua_isstring

- lua_isuserdata

- lua_lessthan

- lua_newthread

- lua_newuserdata

- lua_next

- lua_objlen

- lua_pcall

- lua_pushboolean

- lua_pushcclosure

- lua_pushfstring

- lua_pushinteger

- lua_pushlightuserdata

- lua_pushlstring

- lua_pushnil

- lua_pushnumber

- lua_pushstring

- lua_pushthread

- lua_pushvalue

- lua_pushvfstring

- lua_rawequal

- lua_rawget

- lua_rawgeti

- lua_rawset

- lua_rawseti

- lua_remove

- lua_replace

- lua_resume

- lua_setallocf

- lua_setfenv

- lua_setfield

- lua_setmetatable

- lua_settable

- lua_settop

- lua_setupvalue

- lua_status

- lua_toboolean

- lua_tocfunction

- lua_tointeger

- lua_tolstring

- lua_tonumber

- lua_topointer

- lua_tothread

- lua_touserdata

- lua_type

- lua_typename

- lua_upvalueid

- lua_upvaluejoin

- lua_xmove

- lua_yield

- stkindex2adr

Macros defined

- LUA_CORE

- api_call_base

- api_checknelems

- api_checkvalidindex

- lj_api_c

Source code

1. /*

2. ** Public Lua/C API.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lj_api_c

10. ‌#define LUA_CORE

12. #include "lj_obj.h"

13. #include "lj_gc.h"

14. #include "lj_err.h"

15. #include "lj_debug.h"

16. #include "lj_str.h"

17. #include "lj_tab.h"

18. #include "lj_func.h"

19. #include "lj_udata.h"

20. #include "lj_meta.h"

21. #include "lj_state.h"

22. #include "lj_bc.h"

23. #include "lj_frame.h"

24. #include "lj_trace.h"

25. #include "lj_vm.h"

26. #include "lj_strscan.h"

27. #include "lj_strfmt.h"

29. /* -- Common helper functions --------------------------------------------- */

31. ‌#define api_checknelems(L, n)                api_check(L, (n) <= (L->top - L->base))

32. ‌#define api_checkvalidindex(L, i)        api_check(L, (i) != niltv(L))

34. ‌static TValue *index2adr(lua_State *L, int idx)

35. {

36. if (idx > 0) {

37. TValue *o = L->base + (idx - 1);

38. return o < L->top ? o : niltv(L);

39. } else if (idx > LUA_REGISTRYINDEX) {

40. api_check(L, idx != 0 && -idx <= L->top - L->base);

41. return L->top + idx;

42. } else if (idx == LUA_GLOBALSINDEX) {

43. TValue *o = &G(L)->tmptv;

44. settabV(L, o, tabref(L->env));

45. return o;

46. } else if (idx == LUA_REGISTRYINDEX) {

47. return registry(L);

48. } else {

49. GCfunc *fn = curr_func(L);

50. api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn));

51. if (idx == LUA_ENVIRONINDEX) {

52. TValue *o = &G(L)->tmptv;

53. settabV(L, o, tabref(fn->c.env));

54. return o;

55. } else {

56. idx = LUA_GLOBALSINDEX - idx;

57. return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);

58. }

59. }

60. }

62. ‌static TValue *stkindex2adr(lua_State *L, int idx)

63. {

64. if (idx > 0) {

65. TValue *o = L->base + (idx - 1);

66. return o < L->top ? o : niltv(L);

67. } else {

68. api_check(L, idx != 0 && -idx <= L->top - L->base);

69. return L->top + idx;

70. }

71. }

73. ‌static GCtab *getcurrenv(lua_State *L)

74. {

75. GCfunc *fn = curr_func(L);

76. return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env);

77. }

79. /* -- Miscellaneous API functions ----------------------------------------- */

81. ‌LUA_API int lua_status(lua_State *L)

82. {

83. return L->status;

84. }

86. ‌LUA_API int lua_checkstack(lua_State *L, int size)

87. {

88. if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) {

89. return 0;  /* Stack overflow. */

90. } else if (size > 0) {

91. lj_state_checkstack(L, (MSize)size);

92. }

93. return 1;

94. }

96. ‌LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg)

97. {

98. if (!lua_checkstack(L, size))

99. lj_err_callerv(L, LJ_ERR_STKOVM, msg);

100. }

102. ‌LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)

103. {

104. TValue *f, *t;

105. if (from == to) return;

106. api_checknelems(from, n);

107. api_check(from, G(from) == G(to));

108. lj_state_checkstack(to, (MSize)n);

109. f = from->top;

110. t = to->top = to->top + n;

111. while (--n >= 0) copyTV(to, --t, --f);

112. from->top = f;

113. }

115. /* -- Stack manipulation -------------------------------------------------- */

117. ‌LUA_API int lua_gettop(lua_State *L)

118. {

119. return (int)(L->top - L->base);

120. }

122. ‌LUA_API void lua_settop(lua_State *L, int idx)

123. {

124. if (idx >= 0) {

125. api_check(L, idx <= tvref(L->maxstack) - L->base);

126. if (L->base + idx > L->top) {

127. if (L->base + idx >= tvref(L->maxstack))

128. lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base));

129. do { setnilV(L->top++); } while (L->top < L->base + idx);

130. } else {

131. L->top = L->base + idx;

132. }

133. } else {

134. api_check(L, -(idx+1) <= (L->top - L->base));

135. L->top += idx+1;  /* Shrinks top (idx < 0). */

136. }

137. }

139. ‌LUA_API void lua_remove(lua_State *L, int idx)

140. {

141. TValue *p = stkindex2adr(L, idx);

142. api_checkvalidindex(L, p);

143. while (++p < L->top) copyTV(L, p-1, p);

144. L->top--;

145. }

147. ‌LUA_API void lua_insert(lua_State *L, int idx)

148. {

149. TValue *q, *p = stkindex2adr(L, idx);

150. api_checkvalidindex(L, p);

151. for (q = L->top; q > p; q--) copyTV(L, q, q-1);

152. copyTV(L, p, L->top);

153. }

155. ‌LUA_API void lua_replace(lua_State *L, int idx)

156. {

157. api_checknelems(L, 1);

158. if (idx == LUA_GLOBALSINDEX) {

159. api_check(L, tvistab(L->top-1));

160. /* NOBARRIER: A thread (i.e. L) is never black. */

161. setgcref(L->env, obj2gco(tabV(L->top-1)));

162. } else if (idx == LUA_ENVIRONINDEX) {

163. GCfunc *fn = curr_func(L);

164. if (fn->c.gct != ~LJ_TFUNC)

165. lj_err_msg(L, LJ_ERR_NOENV);

166. api_check(L, tvistab(L->top-1));

167. setgcref(fn->c.env, obj2gco(tabV(L->top-1)));

168. lj_gc_barrier(L, fn, L->top-1);

169. } else {

170. TValue *o = index2adr(L, idx);

171. api_checkvalidindex(L, o);

172. copyTV(L, o, L->top-1);

173. if (idx < LUA_GLOBALSINDEX)  /* Need a barrier for upvalues. */

174. lj_gc_barrier(L, curr_func(L), L->top-1);

175. }

176. L->top--;

177. }

179. ‌LUA_API void lua_pushvalue(lua_State *L, int idx)

180. {

181. copyTV(L, L->top, index2adr(L, idx));

182. incr_top(L);

183. }

185. /* -- Stack getters ------------------------------------------------------- */

187. ‌LUA_API int lua_type(lua_State *L, int idx)

188. {

189. cTValue *o = index2adr(L, idx);

190. if (tvisnumber(o)) {

191. return LUA_TNUMBER;

192. #if LJ_64 && !LJ_GC64

193. } else if (tvislightud(o)) {

194. return LUA_TLIGHTUSERDATA;

195. #endif

196. } else if (o == niltv(L)) {

197. return LUA_TNONE;

198. } else {  /* Magic internal/external tag conversion. ORDER LJ_T */

199. uint32_t t = ~itype(o);

200. #if LJ_64

201. int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u);

202. #else

203. int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u);

204. #endif

205. lua_assert(tt != LUA_TNIL || tvisnil(o));

206. return tt;

207. }

208. }

210. ‌LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt)

211. {

212. if (lua_type(L, idx) != tt)

213. lj_err_argt(L, idx, tt);

214. }

216. ‌LUALIB_API void luaL_checkany(lua_State *L, int idx)

217. {

218. if (index2adr(L, idx) == niltv(L))

219. lj_err_arg(L, idx, LJ_ERR_NOVAL);

220. }

222. ‌LUA_API const char *lua_typename(lua_State *L, int t)

223. {

224. UNUSED(L);

225. return lj_obj_typename[t+1];

226. }

228. ‌LUA_API int lua_iscfunction(lua_State *L, int idx)

229. {

230. cTValue *o = index2adr(L, idx);

231. return tvisfunc(o) && !isluafunc(funcV(o));

232. }

234. ‌LUA_API int lua_isnumber(lua_State *L, int idx)

235. {

236. cTValue *o = index2adr(L, idx);

237. TValue tmp;

238. return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp)));

239. }

241. ‌LUA_API int lua_isstring(lua_State *L, int idx)

242. {

243. cTValue *o = index2adr(L, idx);

244. return (tvisstr(o) || tvisnumber(o));

245. }

247. ‌LUA_API int lua_isuserdata(lua_State *L, int idx)

248. {

249. cTValue *o = index2adr(L, idx);

250. return (tvisudata(o) || tvislightud(o));

251. }

253. ‌LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2)

254. {

255. cTValue *o1 = index2adr(L, idx1);

256. cTValue *o2 = index2adr(L, idx2);

257. return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2);

258. }

260. ‌LUA_API int lua_equal(lua_State *L, int idx1, int idx2)

261. {

262. cTValue *o1 = index2adr(L, idx1);

263. cTValue *o2 = index2adr(L, idx2);

264. if (tvisint(o1) && tvisint(o2)) {

265. return intV(o1) == intV(o2);

266. } else if (tvisnumber(o1) && tvisnumber(o2)) {

267. return numberVnum(o1) == numberVnum(o2);

268. } else if (itype(o1) != itype(o2)) {

269. return 0;

270. } else if (tvispri(o1)) {

271. return o1 != niltv(L) && o2 != niltv(L);

272. #if LJ_64 && !LJ_GC64

273. } else if (tvislightud(o1)) {

274. return o1->u64 == o2->u64;

275. #endif

276. } else if (gcrefeq(o1->gcr, o2->gcr)) {

277. return 1;

278. } else if (!tvistabud(o1)) {

279. return 0;

280. } else {

281. TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0);

282. if ((uintptr_t)base <= 1) {

283. return (int)(uintptr_t)base;

284. } else {

285. L->top = base+2;

286. lj_vm_call(L, base, 1+1);

287. L->top -= 2+LJ_FR2;

288. return tvistruecond(L->top+1+LJ_FR2);

289. }

290. }

291. }

293. ‌LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2)

294. {

295. cTValue *o1 = index2adr(L, idx1);

296. cTValue *o2 = index2adr(L, idx2);

297. if (o1 == niltv(L) || o2 == niltv(L)) {

298. return 0;

299. } else if (tvisint(o1) && tvisint(o2)) {

300. return intV(o1) < intV(o2);

301. } else if (tvisnumber(o1) && tvisnumber(o2)) {

302. return numberVnum(o1) < numberVnum(o2);

303. } else {

304. TValue *base = lj_meta_comp(L, o1, o2, 0);

305. if ((uintptr_t)base <= 1) {

306. return (int)(uintptr_t)base;

307. } else {

308. L->top = base+2;

309. lj_vm_call(L, base, 1+1);

310. L->top -= 2+LJ_FR2;

311. return tvistruecond(L->top+1+LJ_FR2);

312. }

313. }

314. }

316. ‌LUA_API lua_Number lua_tonumber(lua_State *L, int idx)

317. {

318. cTValue *o = index2adr(L, idx);

319. TValue tmp;

320. if (LJ_LIKELY(tvisnumber(o)))

321. return numberVnum(o);

322. else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp))

323. return numV(&tmp);

324. else

325. return 0;

326. }

328. ‌LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx)

329. {

330. cTValue *o = index2adr(L, idx);

331. TValue tmp;

332. if (LJ_LIKELY(tvisnumber(o)))

333. return numberVnum(o);

334. else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))

335. lj_err_argt(L, idx, LUA_TNUMBER);

336. return numV(&tmp);

337. }

339. ‌LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def)

340. {

341. cTValue *o = index2adr(L, idx);

342. TValue tmp;

343. if (LJ_LIKELY(tvisnumber(o)))

344. return numberVnum(o);

345. else if (tvisnil(o))

346. return def;

347. else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp)))

348. lj_err_argt(L, idx, LUA_TNUMBER);

349. return numV(&tmp);

350. }

352. ‌LUA_API lua_Integer lua_tointeger(lua_State *L, int idx)

353. {

354. cTValue *o = index2adr(L, idx);

355. TValue tmp;

356. lua_Number n;

357. if (LJ_LIKELY(tvisint(o))) {

358. return intV(o);

359. } else if (LJ_LIKELY(tvisnum(o))) {

360. n = numV(o);

361. } else {

362. if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))

363. return 0;

364. if (tvisint(&tmp))

365. return (lua_Integer)intV(&tmp);

366. n = numV(&tmp);

367. }

368. #if LJ_64

369. return (lua_Integer)n;

370. #else

371. return lj_num2int(n);

372. #endif

373. }

375. ‌LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx)

376. {

377. cTValue *o = index2adr(L, idx);

378. TValue tmp;

379. lua_Number n;

380. if (LJ_LIKELY(tvisint(o))) {

381. return intV(o);

382. } else if (LJ_LIKELY(tvisnum(o))) {

383. n = numV(o);

384. } else {

385. if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))

386. lj_err_argt(L, idx, LUA_TNUMBER);

387. if (tvisint(&tmp))

388. return (lua_Integer)intV(&tmp);

389. n = numV(&tmp);

390. }

391. #if LJ_64

392. return (lua_Integer)n;

393. #else

394. return lj_num2int(n);

395. #endif

396. }

398. ‌LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def)

399. {

400. cTValue *o = index2adr(L, idx);

401. TValue tmp;

402. lua_Number n;

403. if (LJ_LIKELY(tvisint(o))) {

404. return intV(o);

405. } else if (LJ_LIKELY(tvisnum(o))) {

406. n = numV(o);

407. } else if (tvisnil(o)) {

408. return def;

409. } else {

410. if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp)))

411. lj_err_argt(L, idx, LUA_TNUMBER);

412. if (tvisint(&tmp))

413. return (lua_Integer)intV(&tmp);

414. n = numV(&tmp);

415. }

416. #if LJ_64

417. return (lua_Integer)n;

418. #else

419. return lj_num2int(n);

420. #endif

421. }

423. ‌LUA_API int lua_toboolean(lua_State *L, int idx)

424. {

425. cTValue *o = index2adr(L, idx);

426. return tvistruecond(o);

427. }

429. ‌LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)

430. {

431. TValue *o = index2adr(L, idx);

432. GCstr *s;

433. if (LJ_LIKELY(tvisstr(o))) {

434. s = strV(o);

435. } else if (tvisnumber(o)) {

436. lj_gc_check(L);

437. o = index2adr(L, idx);  /* GC may move the stack. */

438. s = lj_strfmt_number(L, o);

439. setstrV(L, o, s);

440. } else {

441. if (len != NULL) *len = 0;

442. return NULL;

443. }

444. if (len != NULL) *len = s->len;

445. return strdata(s);

446. }

448. ‌LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len)

449. {

450. TValue *o = index2adr(L, idx);

451. GCstr *s;

452. if (LJ_LIKELY(tvisstr(o))) {

453. s = strV(o);

454. } else if (tvisnumber(o)) {

455. lj_gc_check(L);

456. o = index2adr(L, idx);  /* GC may move the stack. */

457. s = lj_strfmt_number(L, o);

458. setstrV(L, o, s);

459. } else {

460. lj_err_argt(L, idx, LUA_TSTRING);

461. }

462. if (len != NULL) *len = s->len;

463. return strdata(s);

464. }

466. ‌LUALIB_API const char *luaL_optlstring(lua_State *L, int idx,

467. const char *def, size_t *len)

468. {

469. TValue *o = index2adr(L, idx);

470. GCstr *s;

471. if (LJ_LIKELY(tvisstr(o))) {

472. s = strV(o);

473. } else if (tvisnil(o)) {

474. if (len != NULL) *len = def ? strlen(def) : 0;

475. return def;

476. } else if (tvisnumber(o)) {

477. lj_gc_check(L);

478. o = index2adr(L, idx);  /* GC may move the stack. */

479. s = lj_strfmt_number(L, o);

480. setstrV(L, o, s);

481. } else {

482. lj_err_argt(L, idx, LUA_TSTRING);

483. }

484. if (len != NULL) *len = s->len;

485. return strdata(s);

486. }

488. ‌LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def,

489. const char *const lst[])

490. {

491. ptrdiff_t i;

492. const char *s = lua_tolstring(L, idx, NULL);

493. if (s == NULL && (s = def) == NULL)

494. lj_err_argt(L, idx, LUA_TSTRING);

495. for (i = 0; lst[i]; i++)

496. if (strcmp(lst[i], s) == 0)

497. return (int)i;

498. lj_err_argv(L, idx, LJ_ERR_INVOPTM, s);

499. }

501. ‌LUA_API size_t lua_objlen(lua_State *L, int idx)

502. {

503. TValue *o = index2adr(L, idx);

504. if (tvisstr(o)) {

505. return strV(o)->len;

506. } else if (tvistab(o)) {

507. return (size_t)lj_tab_len(tabV(o));

508. } else if (tvisudata(o)) {

509. return udataV(o)->len;

510. } else if (tvisnumber(o)) {

511. GCstr *s = lj_strfmt_number(L, o);

512. setstrV(L, o, s);

513. return s->len;

514. } else {

515. return 0;

516. }

517. }

519. ‌LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)

520. {

521. cTValue *o = index2adr(L, idx);

522. if (tvisfunc(o)) {

523. BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));

524. if (op == BC_FUNCC || op == BC_FUNCCW)

525. return funcV(o)->c.f;

526. }

527. return NULL;

528. }

530. ‌LUA_API void *lua_touserdata(lua_State *L, int idx)

531. {

532. cTValue *o = index2adr(L, idx);

533. if (tvisudata(o))

534. return uddata(udataV(o));

535. else if (tvislightud(o))

536. return lightudV(o);

537. else

538. return NULL;

539. }

541. ‌LUA_API lua_State *lua_tothread(lua_State *L, int idx)

542. {

543. cTValue *o = index2adr(L, idx);

544. return (!tvisthread(o)) ? NULL : threadV(o);

545. }

547. ‌LUA_API const void *lua_topointer(lua_State *L, int idx)

548. {

549. return lj_obj_ptr(index2adr(L, idx));

550. }

552. /* -- Stack setters (object creation) ------------------------------------- */

554. ‌LUA_API void lua_pushnil(lua_State *L)

555. {

556. setnilV(L->top);

557. incr_top(L);

558. }

560. ‌LUA_API void lua_pushnumber(lua_State *L, lua_Number n)

561. {

562. setnumV(L->top, n);

563. if (LJ_UNLIKELY(tvisnan(L->top)))

564. setnanV(L->top);  /* Canonicalize injected NaNs. */

565. incr_top(L);

566. }

568. ‌LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)

569. {

570. setintptrV(L->top, n);

571. incr_top(L);

572. }

574. ‌LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len)

575. {

576. GCstr *s;

577. lj_gc_check(L);

578. s = lj_str_new(L, str, len);

579. setstrV(L, L->top, s);

580. incr_top(L);

581. }

583. ‌LUA_API void lua_pushstring(lua_State *L, const char *str)

584. {

585. if (str == NULL) {

586. setnilV(L->top);

587. } else {

588. GCstr *s;

589. lj_gc_check(L);

590. s = lj_str_newz(L, str);

591. setstrV(L, L->top, s);

592. }

593. incr_top(L);

594. }

596. ‌LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,

597. va_list argp)

598. {

599. lj_gc_check(L);

600. return lj_strfmt_pushvf(L, fmt, argp);

601. }

603. ‌LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)

604. {

605. const char *ret;

606. va_list argp;

607. lj_gc_check(L);

608. va_start(argp, fmt);

609. ret = lj_strfmt_pushvf(L, fmt, argp);

610. va_end(argp);

611. return ret;

612. }

614. ‌LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)

615. {

616. GCfunc *fn;

617. lj_gc_check(L);

618. api_checknelems(L, n);

619. fn = lj_func_newC(L, (MSize)n, getcurrenv(L));

620. fn->c.f = f;

621. L->top -= n;

622. while (n--)

623. copyTV(L, &fn->c.upvalue[n], L->top+n);

624. setfuncV(L, L->top, fn);

625. lua_assert(iswhite(obj2gco(fn)));

626. incr_top(L);

627. }

629. ‌LUA_API void lua_pushboolean(lua_State *L, int b)

630. {

631. setboolV(L->top, (b != 0));

632. incr_top(L);

633. }

635. ‌LUA_API void lua_pushlightuserdata(lua_State *L, void *p)

636. {

637. setlightudV(L->top, checklightudptr(L, p));

638. incr_top(L);

639. }

641. ‌LUA_API void lua_createtable(lua_State *L, int narray, int nrec)

642. {

643. lj_gc_check(L);

644. settabV(L, L->top, lj_tab_new_ah(L, narray, nrec));

645. incr_top(L);

646. }

648. ‌LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)

649. {

650. GCtab *regt = tabV(registry(L));

651. TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname));

652. if (tvisnil(tv)) {

653. GCtab *mt = lj_tab_new(L, 0, 1);

654. settabV(L, tv, mt);

655. settabV(L, L->top++, mt);

656. lj_gc_anybarriert(L, regt);

657. return 1;

658. } else {

659. copyTV(L, L->top++, tv);

660. return 0;

661. }

662. }

664. ‌LUA_API int lua_pushthread(lua_State *L)

665. {

666. setthreadV(L, L->top, L);

667. incr_top(L);

668. return (mainthread(G(L)) == L);

669. }

671. ‌LUA_API lua_State *lua_newthread(lua_State *L)

672. {

673. lua_State *L1;

674. lj_gc_check(L);

675. L1 = lj_state_new(L);

676. setthreadV(L, L->top, L1);

677. incr_top(L);

678. return L1;

679. }

681. ‌LUA_API void *lua_newuserdata(lua_State *L, size_t size)

682. {

683. GCudata *ud;

684. lj_gc_check(L);

685. if (size > LJ_MAX_UDATA)

686. lj_err_msg(L, LJ_ERR_UDATAOV);

687. ud = lj_udata_new(L, (MSize)size, getcurrenv(L));

688. setudataV(L, L->top, ud);

689. incr_top(L);

690. return uddata(ud);

691. }

693. ‌LUA_API void lua_concat(lua_State *L, int n)

694. {

695. api_checknelems(L, n);

696. if (n >= 2) {

697. n--;

698. do {

699. TValue *top = lj_meta_cat(L, L->top-1, -n);

700. if (top == NULL) {

701. L->top -= n;

702. break;

703. }

704. n -= (int)(L->top - top);

705. L->top = top+2;

706. lj_vm_call(L, top, 1+1);

707. L->top -= 1+LJ_FR2;

708. copyTV(L, L->top-1, L->top+LJ_FR2);

709. } while (--n > 0);

710. } else if (n == 0) {  /* Push empty string. */

711. setstrV(L, L->top, &G(L)->strempty);

712. incr_top(L);

713. }

714. /* else n == 1: nothing to do. */

715. }

717. /* -- Object getters ------------------------------------------------------ */

719. ‌LUA_API void lua_gettable(lua_State *L, int idx)

720. {

721. cTValue *v, *t = index2adr(L, idx);

722. api_checkvalidindex(L, t);

723. v = lj_meta_tget(L, t, L->top-1);

724. if (v == NULL) {

725. L->top += 2;

726. lj_vm_call(L, L->top-2, 1+1);

727. L->top -= 2+LJ_FR2;

728. v = L->top+1+LJ_FR2;

729. }

730. copyTV(L, L->top-1, v);

731. }

733. ‌LUA_API void lua_getfield(lua_State *L, int idx, const char *k)

734. {

735. cTValue *v, *t = index2adr(L, idx);

736. TValue key;

737. api_checkvalidindex(L, t);

738. setstrV(L, &key, lj_str_newz(L, k));

739. v = lj_meta_tget(L, t, &key);

740. if (v == NULL) {

741. L->top += 2;

742. lj_vm_call(L, L->top-2, 1+1);

743. L->top -= 2+LJ_FR2;

744. v = L->top+1+LJ_FR2;

745. }

746. copyTV(L, L->top, v);

747. incr_top(L);

748. }

750. ‌LUA_API void lua_rawget(lua_State *L, int idx)

751. {

752. cTValue *t = index2adr(L, idx);

753. api_check(L, tvistab(t));

754. copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1));

755. }

757. ‌LUA_API void lua_rawgeti(lua_State *L, int idx, int n)

758. {

759. cTValue *v, *t = index2adr(L, idx);

760. api_check(L, tvistab(t));

761. v = lj_tab_getint(tabV(t), n);

762. if (v) {

763. copyTV(L, L->top, v);

764. } else {

765. setnilV(L->top);

766. }

767. incr_top(L);

768. }

770. ‌LUA_API int lua_getmetatable(lua_State *L, int idx)

771. {

772. cTValue *o = index2adr(L, idx);

773. GCtab *mt = NULL;

774. if (tvistab(o))

775. mt = tabref(tabV(o)->metatable);

776. else if (tvisudata(o))

777. mt = tabref(udataV(o)->metatable);

778. else

779. mt = tabref(basemt_obj(G(L), o));

780. if (mt == NULL)

781. return 0;

782. settabV(L, L->top, mt);

783. incr_top(L);

784. return 1;

785. }

787. ‌LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field)

788. {

789. if (lua_getmetatable(L, idx)) {

790. cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field));

791. if (tv && !tvisnil(tv)) {

792. copyTV(L, L->top-1, tv);

793. return 1;

794. }

795. L->top--;

796. }

797. return 0;

798. }

800. ‌LUA_API void lua_getfenv(lua_State *L, int idx)

801. {

802. cTValue *o = index2adr(L, idx);

803. api_checkvalidindex(L, o);

804. if (tvisfunc(o)) {

805. settabV(L, L->top, tabref(funcV(o)->c.env));

806. } else if (tvisudata(o)) {

807. settabV(L, L->top, tabref(udataV(o)->env));

808. } else if (tvisthread(o)) {

809. settabV(L, L->top, tabref(threadV(o)->env));

810. } else {

811. setnilV(L->top);

812. }

813. incr_top(L);

814. }

816. ‌LUA_API int lua_next(lua_State *L, int idx)

817. {

818. cTValue *t = index2adr(L, idx);

819. int more;

820. api_check(L, tvistab(t));

821. more = lj_tab_next(L, tabV(t), L->top-1);

822. if (more) {

823. incr_top(L);  /* Return new key and value slot. */

824. } else {  /* End of traversal. */

825. L->top--;  /* Remove key slot. */

826. }

827. return more;

828. }

830. ‌LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)

831. {

832. TValue *val;

833. const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val);

834. if (name) {

835. copyTV(L, L->top, val);

836. incr_top(L);

837. }

838. return name;

839. }

841. ‌LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)

842. {

843. GCfunc *fn = funcV(index2adr(L, idx));

844. n--;

845. api_check(L, (uint32_t)n < fn->l.nupvalues);

846. return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :

847. (void *)&fn->c.upvalue[n];

848. }

850. ‌LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)

851. {

852. GCfunc *fn1 = funcV(index2adr(L, idx1));

853. GCfunc *fn2 = funcV(index2adr(L, idx2));

854. n1--; n2--;

855. api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues);

856. api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues);

857. setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]);

858. lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));

859. }

861. ‌LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)

862. {

863. cTValue *o = index2adr(L, idx);

864. if (tvisudata(o)) {

865. GCudata *ud = udataV(o);

866. cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname));

867. if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))

868. return uddata(ud);

869. }

870. lj_err_argtype(L, idx, tname);

871. return NULL;  /* unreachable */

872. }

874. /* -- Object setters ------------------------------------------------------ */

876. ‌LUA_API void lua_settable(lua_State *L, int idx)

877. {

878. TValue *o;

879. cTValue *t = index2adr(L, idx);

880. api_checknelems(L, 2);

881. api_checkvalidindex(L, t);

882. o = lj_meta_tset(L, t, L->top-2);

883. if (o) {

884. /* NOBARRIER: lj_meta_tset ensures the table is not black. */

885. L->top -= 2;

886. copyTV(L, o, L->top+1);

887. } else {

888. TValue *base = L->top;

889. copyTV(L, base+2, base-3-2*LJ_FR2);

890. L->top = base+3;

891. lj_vm_call(L, base, 0+1);

892. L->top -= 3+LJ_FR2;

893. }

894. }

896. ‌LUA_API void lua_setfield(lua_State *L, int idx, const char *k)

897. {

898. TValue *o;

899. TValue key;

900. cTValue *t = index2adr(L, idx);

901. api_checknelems(L, 1);

902. api_checkvalidindex(L, t);

903. setstrV(L, &key, lj_str_newz(L, k));

904. o = lj_meta_tset(L, t, &key);

905. if (o) {

906. /* NOBARRIER: lj_meta_tset ensures the table is not black. */

907. copyTV(L, o, --L->top);

908. } else {

909. TValue *base = L->top;

910. copyTV(L, base+2, base-3-2*LJ_FR2);

911. L->top = base+3;

912. lj_vm_call(L, base, 0+1);

913. L->top -= 2+LJ_FR2;

914. }

915. }

917. ‌LUA_API void lua_rawset(lua_State *L, int idx)

918. {

919. GCtab *t = tabV(index2adr(L, idx));

920. TValue *dst, *key;

921. api_checknelems(L, 2);

922. key = L->top-2;

923. dst = lj_tab_set(L, t, key);

924. copyTV(L, dst, key+1);

925. lj_gc_anybarriert(L, t);

926. L->top = key;

927. }

929. ‌LUA_API void lua_rawseti(lua_State *L, int idx, int n)

930. {

931. GCtab *t = tabV(index2adr(L, idx));

932. TValue *dst, *src;

933. api_checknelems(L, 1);

934. dst = lj_tab_setint(L, t, n);

935. src = L->top-1;

936. copyTV(L, dst, src);

937. lj_gc_barriert(L, t, dst);

938. L->top = src;

939. }

941. ‌LUA_API int lua_setmetatable(lua_State *L, int idx)

942. {

943. global_State *g;

944. GCtab *mt;

945. cTValue *o = index2adr(L, idx);

946. api_checknelems(L, 1);

947. api_checkvalidindex(L, o);

948. if (tvisnil(L->top-1)) {

949. mt = NULL;

950. } else {

951. api_check(L, tvistab(L->top-1));

952. mt = tabV(L->top-1);

953. }

954. g = G(L);

955. if (tvistab(o)) {

956. setgcref(tabV(o)->metatable, obj2gco(mt));

957. if (mt)

958. lj_gc_objbarriert(L, tabV(o), mt);

959. } else if (tvisudata(o)) {

960. setgcref(udataV(o)->metatable, obj2gco(mt));

961. if (mt)

962. lj_gc_objbarrier(L, udataV(o), mt);

963. } else {

964. /* Flush cache, since traces specialize to basemt. But not during __gc. */

965. if (lj_trace_flushall(L))

966. lj_err_caller(L, LJ_ERR_NOGCMM);

967. if (tvisbool(o)) {

968. /* NOBARRIER: basemt is a GC root. */

969. setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt));

970. setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt));

971. } else {

972. /* NOBARRIER: basemt is a GC root. */

973. setgcref(basemt_obj(g, o), obj2gco(mt));

974. }

975. }

976. L->top--;

977. return 1;

978. }

980. ‌LUA_API int lua_setfenv(lua_State *L, int idx)

981. {

982. cTValue *o = index2adr(L, idx);

983. GCtab *t;

984. api_checknelems(L, 1);

985. api_checkvalidindex(L, o);

986. api_check(L, tvistab(L->top-1));

987. t = tabV(L->top-1);

988. if (tvisfunc(o)) {

989. setgcref(funcV(o)->c.env, obj2gco(t));

990. } else if (tvisudata(o)) {

991. setgcref(udataV(o)->env, obj2gco(t));

992. } else if (tvisthread(o)) {

993. setgcref(threadV(o)->env, obj2gco(t));

994. } else {

995. L->top--;

996. return 0;

997. }

998. lj_gc_objbarrier(L, gcV(o), t);

999. L->top--;

1000. return 1;

1001. }

1003. ‌LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)

1004. {

1005. cTValue *f = index2adr(L, idx);

1006. TValue *val;

1007. const char *name;

1008. api_checknelems(L, 1);

1009. name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val);

1010. if (name) {

1011. L->top--;

1012. copyTV(L, val, L->top);

1013. lj_gc_barrier(L, funcV(f), L->top);

1014. }

1015. return name;

1016. }

1018. /* -- Calls --------------------------------------------------------------- */

1020. #if LJ_FR2

1021. ‌static TValue *api_call_base(lua_State *L, int nargs)

1022. {

1023. TValue *o = L->top, *base = o - nargs;

1024. L->top = o+1;

1025. for (; o > base; o--) copyTV(L, o, o-1);

1026. setnilV(o);

1027. return o+1;

1028. }

1029. #else

1030. ‌#define api_call_base(L, nargs)        (L->top - (nargs))

1031. #endif

1033. ‌LUA_API void lua_call(lua_State *L, int nargs, int nresults)

1034. {

1035. api_check(L, L->status == 0 || L->status == LUA_ERRERR);

1036. api_checknelems(L, nargs+1);

1037. lj_vm_call(L, api_call_base(L, nargs), nresults+1);

1038. }

1040. ‌LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)

1041. {

1042. global_State *g = G(L);

1043. uint8_t oldh = hook_save(g);

1044. ptrdiff_t ef;

1045. int status;

1046. api_check(L, L->status == 0 || L->status == LUA_ERRERR);

1047. api_checknelems(L, nargs+1);

1048. if (errfunc == 0) {

1049. ef = 0;

1050. } else {

1051. cTValue *o = stkindex2adr(L, errfunc);

1052. api_checkvalidindex(L, o);

1053. ef = savestack(L, o);

1054. }

1055. status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef);

1056. if (status) hook_restore(g, oldh);

1057. return status;

1058. }

1060. ‌static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)

1061. {

1062. GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));

1063. TValue *top = L->top;

1064. fn->c.f = func;

1065. setfuncV(L, top++, fn);

1066. if (LJ_FR2) setnilV(top++);

1067. setlightudV(top++, checklightudptr(L, ud));

1068. cframe_nres(L->cframe) = 1+0;  /* Zero results. */

1069. L->top = top;

1070. return top-1;  /* Now call the newly allocated C function. */

1071. }

1073. ‌LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)

1074. {

1075. global_State *g = G(L);

1076. uint8_t oldh = hook_save(g);

1077. int status;

1078. api_check(L, L->status == 0 || L->status == LUA_ERRERR);

1079. status = lj_vm_cpcall(L, func, ud, cpcall);

1080. if (status) hook_restore(g, oldh);

1081. return status;

1082. }

1084. ‌LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)

1085. {

1086. if (luaL_getmetafield(L, idx, field)) {

1087. TValue *top = L->top--;

1088. if (LJ_FR2) setnilV(top++);

1089. copyTV(L, top++, index2adr(L, idx));

1090. L->top = top;

1091. lj_vm_call(L, top-1, 1+1);

1092. return 1;

1093. }

1094. return 0;

1095. }

1097. /* -- Coroutine yield and resume ------------------------------------------ */

1099. ‌LUA_API int lua_yield(lua_State *L, int nresults)

1100. {

1101. void *cf = L->cframe;

1102. global_State *g = G(L);

1103. if (cframe_canyield(cf)) {

1104. cf = cframe_raw(cf);

1105. if (!hook_active(g)) {  /* Regular yield: move results down if needed. */

1106. cTValue *f = L->top - nresults;

1107. if (f > L->base) {

1108. TValue *t = L->base;

1109. while (--nresults >= 0) copyTV(L, t++, f++);

1110. L->top = t;

1111. }

1112. L->cframe = NULL;

1113. L->status = LUA_YIELD;

1114. return -1;

1115. } else {  /* Yield from hook: add a pseudo-frame. */

1116. TValue *top = L->top;

1117. hook_leave(g);

1118. (top++)->u64 = cframe_multres(cf);

1119. setcont(top, lj_cont_hook);

1120. if (LJ_FR2) top++;

1121. setframe_pc(top, cframe_pc(cf)-1);

1122. if (LJ_FR2) top++;

1123. setframe_gc(top, obj2gco(L), LJ_TTHREAD);

1124. setframe_ftsz(top, ((char *)(top+1)-(char *)L->base)+FRAME_CONT);

1125. L->top = L->base = top+1;

1126. #if LJ_TARGET_X64

1127. lj_err_throw(L, LUA_YIELD);

1128. #else

1129. L->cframe = NULL;

1130. L->status = LUA_YIELD;

1131. lj_vm_unwind_c(cf, LUA_YIELD);

1132. #endif

1133. }

1134. }

1135. lj_err_msg(L, LJ_ERR_CYIELD);

1136. return 0;  /* unreachable */

1137. }

1139. ‌LUA_API int lua_resume(lua_State *L, int nargs)

1140. {

1141. if (L->cframe == NULL && L->status <= LUA_YIELD)

1142. return lj_vm_resume(L,

1143. L->status == 0 ? api_call_base(L, nargs) : L->top - nargs,

1144. 0, 0);

1145. L->top = L->base;

1146. setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP));

1147. incr_top(L);

1148. return LUA_ERRRUN;

1149. }

1151. /* -- GC and memory management -------------------------------------------- */

1153. ‌LUA_API int lua_gc(lua_State *L, int what, int data)

1154. {

1155. global_State *g = G(L);

1156. int res = 0;

1157. switch (what) {

1158. case LUA_GCSTOP:

1159. g->gc.threshold = LJ_MAX_MEM;

1160. break;

1161. case LUA_GCRESTART:

1162. g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;

1163. break;

1164. case LUA_GCCOLLECT:

1165. lj_gc_fullgc(L);

1166. break;

1167. case LUA_GCCOUNT:

1168. res = (int)(g->gc.total >> 10);

1169. break;

1170. case LUA_GCCOUNTB:

1171. res = (int)(g->gc.total & 0x3ff);

1172. break;

1173. case LUA_GCSTEP: {

1174. GCSize a = (GCSize)data << 10;

1175. g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0;

1176. while (g->gc.total >= g->gc.threshold)

1177. if (lj_gc_step(L) > 0) {

1178. res = 1;

1179. break;

1180. }

1181. break;

1182. }

1183. case LUA_GCSETPAUSE:

1184. res = (int)(g->gc.pause);

1185. g->gc.pause = (MSize)data;

1186. break;

1187. case LUA_GCSETSTEPMUL:

1188. res = (int)(g->gc.stepmul);

1189. g->gc.stepmul = (MSize)data;

1190. break;

1191. default:

1192. res = -1;  /* Invalid option. */

1193. }

1194. return res;

1195. }

1197. ‌LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud)

1198. {

1199. global_State *g = G(L);

1200. if (ud) *ud = g->allocd;

1201. return g->allocf;

1202. }

1204. ‌LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)

1205. {

1206. global_State *g = G(L);

1207. g->allocd = ud;

1208. g->allocf = f;

1209. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lua.h - luajit-2.0-src

Data types defined

- lua_Alloc

- lua_CFunction

- lua_Debug

- lua_Debug

- lua_Hook

- lua_Integer

- lua_Number

- lua_Reader

- lua_State

- lua_Writer

Macros defined

- LUA_AUTHORS

- LUA_COPYRIGHT

- LUA_ENVIRONINDEX

- LUA_ERRERR

- LUA_ERRMEM

- LUA_ERRRUN

- LUA_ERRSYNTAX

- LUA_GCCOLLECT

- LUA_GCCOUNT

- LUA_GCCOUNTB

- LUA_GCRESTART

- LUA_GCSETPAUSE

- LUA_GCSETSTEPMUL

- LUA_GCSTEP

- LUA_GCSTOP

- LUA_GLOBALSINDEX

- LUA_HOOKCALL

- LUA_HOOKCOUNT

- LUA_HOOKLINE

- LUA_HOOKRET

- LUA_HOOKTAILRET

- LUA_MASKCALL

- LUA_MASKCOUNT

- LUA_MASKLINE

- LUA_MASKRET

- LUA_MINSTACK

- LUA_MULTRET

- LUA_REGISTRYINDEX

- LUA_RELEASE

- LUA_SIGNATURE

- LUA_TBOOLEAN

- LUA_TFUNCTION

- LUA_TLIGHTUSERDATA

- LUA_TNIL

- LUA_TNONE

- LUA_TNUMBER

- LUA_TSTRING

- LUA_TTABLE

- LUA_TTHREAD

- LUA_TUSERDATA

- LUA_VERSION

- LUA_VERSION_NUM

- LUA_YIELD

- lua_Chunkreader

- lua_Chunkwriter

- lua_getgccount

- lua_getglobal

- lua_getregistry

- lua_h

- lua_isboolean

- lua_isfunction

- lua_islightuserdata

- lua_isnil

- lua_isnone

- lua_isnoneornil

- lua_istable

- lua_isthread

- lua_newtable

- lua_open

- lua_pop

- lua_pushcfunction

- lua_pushliteral

- lua_register

- lua_setglobal

- lua_strlen

- lua_tostring

- lua_upvalueindex

Source code

1. /*

2. ** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $

3. ** Lua - An Extensible Extension Language

4. ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)

5. ** See Copyright Notice at the end of this file

6. */

9. #ifndef lua_h

10. ‌#define lua_h

12. #include <stdarg.h>

13. #include <stddef.h>

16. #include "luaconf.h"

19. ‌#define LUA_VERSION        "Lua 5.1"

20. ‌#define LUA_RELEASE        "Lua 5.1.4"

21. ‌#define LUA_VERSION_NUM        501

22. ‌#define LUA_COPYRIGHT        "Copyright (C) 1994-2008 Lua.org, PUC-Rio"

23. ‌#define LUA_AUTHORS        "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"

26. /* mark for precompiled code (`<esc>Lua') */

27. ‌#define        LUA_SIGNATURE        "\033Lua"

29. /* option for multiple returns in `lua_pcall' and `lua_call' */

30. ‌#define LUA_MULTRET        (-1)

33. /*

34. ** pseudo-indices

35. */

36. ‌#define LUA_REGISTRYINDEX        (-10000)

37. ‌#define LUA_ENVIRONINDEX        (-10001)

38. ‌#define LUA_GLOBALSINDEX        (-10002)

39. ‌#define lua_upvalueindex(i)        (LUA_GLOBALSINDEX-(i))

42. /* thread status; 0 is OK */

43. ‌#define LUA_YIELD        1

44. ‌#define LUA_ERRRUN        2

45. ‌#define LUA_ERRSYNTAX        3

46. ‌#define LUA_ERRMEM        4

47. ‌#define LUA_ERRERR        5

50. ‌typedef struct lua_State lua_State;

52. ‌typedef int (*lua_CFunction) (lua_State *L);

55. /*

56. ** functions that read/write blocks when loading/dumping Lua chunks

57. */

58. ‌typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);

60. ‌typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);

63. /*

64. ** prototype for memory-allocation functions

65. */

66. ‌typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);

69. /*

70. ** basic types

71. */

72. ‌#define LUA_TNONE                (-1)

74. ‌#define LUA_TNIL                0

75. ‌#define LUA_TBOOLEAN                1

76. ‌#define LUA_TLIGHTUSERDATA        2

77. ‌#define LUA_TNUMBER                3

78. ‌#define LUA_TSTRING                4

79. ‌#define LUA_TTABLE                5

80. ‌#define LUA_TFUNCTION                6

81. ‌#define LUA_TUSERDATA                7

82. ‌#define LUA_TTHREAD                8

86. /* minimum Lua stack available to a C function */

87. ‌#define LUA_MINSTACK        20

90. /*

91. ** generic extra include file

92. */

93. #if defined(LUA_USER_H)

94. #include LUA_USER_H

95. #endif

98. /* type of numbers in Lua */

99. ‌typedef LUA_NUMBER lua_Number;

102. /* type for integer functions */

103. ‌typedef LUA_INTEGER lua_Integer;

107. /*

108. ** state manipulation

109. */

110. LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);

111. LUA_API void       (lua_close) (lua_State *L);

112. LUA_API lua_State *(lua_newthread) (lua_State *L);

114. LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);

117. /*

118. ** basic stack manipulation

119. */

120. LUA_API int   (lua_gettop) (lua_State *L);

121. LUA_API void  (lua_settop) (lua_State *L, int idx);

122. LUA_API void  (lua_pushvalue) (lua_State *L, int idx);

123. LUA_API void  (lua_remove) (lua_State *L, int idx);

124. LUA_API void  (lua_insert) (lua_State *L, int idx);

125. LUA_API void  (lua_replace) (lua_State *L, int idx);

126. LUA_API int   (lua_checkstack) (lua_State *L, int sz);

128. LUA_API void  (lua_xmove) (lua_State *from, lua_State *to, int n);

131. /*

132. ** access functions (stack -> C)

133. */

135. LUA_API int             (lua_isnumber) (lua_State *L, int idx);

136. LUA_API int             (lua_isstring) (lua_State *L, int idx);

137. LUA_API int             (lua_iscfunction) (lua_State *L, int idx);

138. LUA_API int             (lua_isuserdata) (lua_State *L, int idx);

139. LUA_API int             (lua_type) (lua_State *L, int idx);

140. LUA_API const char     *(lua_typename) (lua_State *L, int tp);

142. LUA_API int            (lua_equal) (lua_State *L, int idx1, int idx2);

143. LUA_API int            (lua_rawequal) (lua_State *L, int idx1, int idx2);

144. LUA_API int            (lua_lessthan) (lua_State *L, int idx1, int idx2);

146. LUA_API lua_Number      (lua_tonumber) (lua_State *L, int idx);

147. LUA_API lua_Integer     (lua_tointeger) (lua_State *L, int idx);

148. LUA_API int             (lua_toboolean) (lua_State *L, int idx);

149. LUA_API const char     *(lua_tolstring) (lua_State *L, int idx, size_t *len);

150. LUA_API size_t          (lua_objlen) (lua_State *L, int idx);

151. LUA_API lua_CFunction   (lua_tocfunction) (lua_State *L, int idx);

152. LUA_API void               *(lua_touserdata) (lua_State *L, int idx);

153. LUA_API lua_State      *(lua_tothread) (lua_State *L, int idx);

154. LUA_API const void     *(lua_topointer) (lua_State *L, int idx);

157. /*

158. ** push functions (C -> stack)

159. */

160. LUA_API void  (lua_pushnil) (lua_State *L);

161. LUA_API void  (lua_pushnumber) (lua_State *L, lua_Number n);

162. LUA_API void  (lua_pushinteger) (lua_State *L, lua_Integer n);

163. LUA_API void  (lua_pushlstring) (lua_State *L, const char *s, size_t l);

164. LUA_API void  (lua_pushstring) (lua_State *L, const char *s);

165. LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,

166. va_list argp);

167. LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);

168. LUA_API void  (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);

169. LUA_API void  (lua_pushboolean) (lua_State *L, int b);

170. LUA_API void  (lua_pushlightuserdata) (lua_State *L, void *p);

171. LUA_API int   (lua_pushthread) (lua_State *L);

174. /*

175. ** get functions (Lua -> stack)

176. */

177. LUA_API void  (lua_gettable) (lua_State *L, int idx);

178. LUA_API void  (lua_getfield) (lua_State *L, int idx, const char *k);

179. LUA_API void  (lua_rawget) (lua_State *L, int idx);

180. LUA_API void  (lua_rawgeti) (lua_State *L, int idx, int n);

181. LUA_API void  (lua_createtable) (lua_State *L, int narr, int nrec);

182. LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);

183. LUA_API int   (lua_getmetatable) (lua_State *L, int objindex);

184. LUA_API void  (lua_getfenv) (lua_State *L, int idx);

187. /*

188. ** set functions (stack -> Lua)

189. */

190. LUA_API void  (lua_settable) (lua_State *L, int idx);

191. LUA_API void  (lua_setfield) (lua_State *L, int idx, const char *k);

192. LUA_API void  (lua_rawset) (lua_State *L, int idx);

193. LUA_API void  (lua_rawseti) (lua_State *L, int idx, int n);

194. LUA_API int   (lua_setmetatable) (lua_State *L, int objindex);

195. LUA_API int   (lua_setfenv) (lua_State *L, int idx);

198. /*

199. ** `load' and `call' functions (load and run Lua code)

200. */

201. LUA_API void  (lua_call) (lua_State *L, int nargs, int nresults);

202. LUA_API int   (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);

203. LUA_API int   (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);

204. LUA_API int   (lua_load) (lua_State *L, lua_Reader reader, void *dt,

205. const char *chunkname);

207. LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);

210. /*

211. ** coroutine functions

212. */

213. LUA_API int  (lua_yield) (lua_State *L, int nresults);

214. LUA_API int  (lua_resume) (lua_State *L, int narg);

215. LUA_API int  (lua_status) (lua_State *L);

217. /*

218. ** garbage-collection function and options

219. */

221. ‌#define LUA_GCSTOP                0

222. ‌#define LUA_GCRESTART                1

223. ‌#define LUA_GCCOLLECT                2

224. ‌#define LUA_GCCOUNT                3

225. ‌#define LUA_GCCOUNTB                4

226. ‌#define LUA_GCSTEP                5

227. ‌#define LUA_GCSETPAUSE                6

228. ‌#define LUA_GCSETSTEPMUL        7

230. LUA_API int (lua_gc) (lua_State *L, int what, int data);

233. /*

234. ** miscellaneous functions

235. */

237. LUA_API int   (lua_error) (lua_State *L);

239. LUA_API int   (lua_next) (lua_State *L, int idx);

241. LUA_API void  (lua_concat) (lua_State *L, int n);

243. LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);

244. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);

248. /*

249. ** ===============================================================

250. ** some useful macros

251. ** ===============================================================

252. */

254. ‌#define lua_pop(L,n)                lua_settop(L, -(n)-1)

256. ‌#define lua_newtable(L)                lua_createtable(L, 0, 0)

258. ‌#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))

260. ‌#define lua_pushcfunction(L,f)        lua_pushcclosure(L, (f), 0)

262. ‌#define lua_strlen(L,i)                lua_objlen(L, (i))

264. ‌#define lua_isfunction(L,n)        (lua_type(L, (n)) == LUA_TFUNCTION)

265. ‌#define lua_istable(L,n)        (lua_type(L, (n)) == LUA_TTABLE)

266. ‌#define lua_islightuserdata(L,n)        (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)

267. ‌#define lua_isnil(L,n)                (lua_type(L, (n)) == LUA_TNIL)

268. ‌#define lua_isboolean(L,n)        (lua_type(L, (n)) == LUA_TBOOLEAN)

269. ‌#define lua_isthread(L,n)        (lua_type(L, (n)) == LUA_TTHREAD)

270. ‌#define lua_isnone(L,n)                (lua_type(L, (n)) == LUA_TNONE)

271. ‌#define lua_isnoneornil(L, n)        (lua_type(L, (n)) <= 0)

273. ‌#define lua_pushliteral(L, s)        \

274. lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)

276. ‌#define lua_setglobal(L,s)        lua_setfield(L, LUA_GLOBALSINDEX, (s))

277. ‌#define lua_getglobal(L,s)        lua_getfield(L, LUA_GLOBALSINDEX, (s))

279. ‌#define lua_tostring(L,i)        lua_tolstring(L, (i), NULL)

283. /*

284. ** compatibility macros and functions

285. */

287. ‌#define lua_open()        luaL_newstate()

289. ‌#define lua_getregistry(L)        lua_pushvalue(L, LUA_REGISTRYINDEX)

291. ‌#define lua_getgccount(L)        lua_gc(L, LUA_GCCOUNT, 0)

293. ‌#define lua_Chunkreader                lua_Reader

294. ‌#define lua_Chunkwriter                lua_Writer

297. /* hack */

298. LUA_API void lua_setlevel        (lua_State *from, lua_State *to);

301. /*

302. ** {======================================================================

303. ** Debug API

304. ** =======================================================================

305. */

308. /*

309. ** Event codes

310. */

311. ‌#define LUA_HOOKCALL        0

312. ‌#define LUA_HOOKRET        1

313. ‌#define LUA_HOOKLINE        2

314. ‌#define LUA_HOOKCOUNT        3

315. ‌#define LUA_HOOKTAILRET 4

318. /*

319. ** Event masks

320. */

321. ‌#define LUA_MASKCALL        (1 << LUA_HOOKCALL)

322. ‌#define LUA_MASKRET        (1 << LUA_HOOKRET)

323. ‌#define LUA_MASKLINE        (1 << LUA_HOOKLINE)

324. ‌#define LUA_MASKCOUNT        (1 << LUA_HOOKCOUNT)

326. ‌typedef struct lua_Debug lua_Debug;  /* activation record */

329. /* Functions to be called by the debuger in specific events */

330. ‌typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);

333. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);

334. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);

335. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);

336. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);

337. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n);

338. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n);

339. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);

340. LUA_API lua_Hook lua_gethook (lua_State *L);

341. LUA_API int lua_gethookmask (lua_State *L);

342. LUA_API int lua_gethookcount (lua_State *L);

344. /* From Lua 5.2. */

345. LUA_API void *lua_upvalueid (lua_State *L, int idx, int n);

346. LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2);

347. LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt,

348. const char *chunkname, const char *mode);

351. ‌struct lua_Debug {

352. int event;

353. const char *name;        /* (n) */

354. const char *namewhat;        /* (n) `global', `local', `field', `method' */

355. const char *what;        /* (S) `Lua', `C', `main', `tail' */

356. const char *source;        /* (S) */

357. int currentline;        /* (l) */

358. int nups;                /* (u) number of upvalues */

359. int linedefined;        /* (S) */

360. int lastlinedefined;        /* (S) */

361. char short_src[LUA_IDSIZE]; /* (S) */

362. /* private part */

363. int i_ci;  /* active function */

364. };

366. /* }====================================================================== */

369. /******************************************************************************

370. * Copyright (C) 1994-2008 Lua.org, PUC-Rio.  All rights reserved.

371. *

372. * Permission is hereby granted, free of charge, to any person obtaining

373. * a copy of this software and associated documentation files (the

374. * "Software"), to deal in the Software without restriction, including

375. * without limitation the rights to use, copy, modify, merge, publish,

376. * distribute, sublicense, and/or sell copies of the Software, and to

377. * permit persons to whom the Software is furnished to do so, subject to

378. * the following conditions:

379. *

380. * The above copyright notice and this permission notice shall be

381. * included in all copies or substantial portions of the Software.

382. *

383. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

384. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

385. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

386. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY

387. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,

388. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE

389. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

390. ******************************************************************************/

393. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/luaconf.h - luajit-2.0-src

Macros defined

- LUAI_GCMUL

- LUAI_GCPAUSE

- LUAI_MAXCSTACK

- LUAI_MAXNUMBER2STR

- LUAI_MAXSTACK

- LUAI_UACNUMBER

- LUALIB_API

- LUAL_BUFFERSIZE

- LUA_API

- LUA_API

- LUA_API

- LUA_CDIR

- LUA_COMPAT_GFIND

- LUA_COMPAT_MOD

- LUA_CPATH

- LUA_CPATH_DEFAULT

- LUA_CPATH_DEFAULT

- LUA_DIRSEP

- LUA_DIRSEP

- LUA_EXECDIR

- LUA_IDSIZE

- LUA_IGMARK

- LUA_INIT

- LUA_INTEGER

- LUA_INTFRMLEN

- LUA_INTFRM_T

- LUA_JPATH

- LUA_JROOT

- LUA_JROOT

- LUA_LCDIR

- LUA_LCPATH1

- LUA_LCPATH2

- LUA_LDIR

- LUA_LJDIR

- LUA_LLDIR

- LUA_LLPATH

- LUA_LMULTILIB

- LUA_LROOT

- LUA_LUADIR

- LUA_MAXCAPTURES

- LUA_MAXINPUT

- LUA_MULTILIB

- LUA_NUMBER

- LUA_NUMBER_DOUBLE

- LUA_NUMBER_FMT

- LUA_NUMBER_SCAN

- LUA_PATH

- LUA_PATHSEP

- LUA_PATH_CONFIG

- LUA_PATH_DEFAULT

- LUA_PATH_DEFAULT

- LUA_PATH_MARK

- LUA_PROGNAME

- LUA_PROMPT

- LUA_PROMPT2

- LUA_QL

- LUA_QS

- LUA_RCDIR

- LUA_RCPATH

- LUA_RCPATH

- LUA_RLDIR

- LUA_RLPATH

- LUA_RLPATH

- WINVER

- lua_assert

- lua_number2str

- luaconf_h

- luai_apicheck

- luai_apicheck

Source code

1. /*

2. ** Configuration header.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef luaconf_h

7. ‌#define luaconf_h

9. #ifndef WINVER

10. ‌#define WINVER 0x0501

11. #endif

12. #include <limits.h>

13. #include <stddef.h>

15. /* Default path for loading Lua and C modules with require(). */

16. #if defined(_WIN32)

17. /*

18. ** In Windows, any exclamation mark ('!') in the path is replaced by the

19. ** path of the directory of the executable file of the current process.

20. */

21. ‌#define LUA_LDIR        "!\\lua\\"

22. ‌#define LUA_CDIR        "!\\"

23. ‌#define LUA_PATH_DEFAULT \

24. ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;"

25. ‌#define LUA_CPATH_DEFAULT \

26. ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"

27. #else

28. /*

29. ** Note to distribution maintainers: do NOT patch the following lines!

30. ** Please read ../doc/install.html#distro and pass PREFIX=/usr instead.

31. */

32. #ifndef LUA_MULTILIB

33. ‌#define LUA_MULTILIB        "lib"

34. #endif

35. #ifndef LUA_LMULTILIB

36. ‌#define LUA_LMULTILIB        "lib"

37. #endif

38. ‌#define LUA_LROOT        "/usr/local"

39. ‌#define LUA_LUADIR        "/lua/5.1/"

40. ‌#define LUA_LJDIR        "/luajit-2.1.0-alpha/"

42. #ifdef LUA_ROOT

43. ‌#define LUA_JROOT        LUA_ROOT

44. ‌#define LUA_RLDIR        LUA_ROOT "/share" LUA_LUADIR

45. ‌#define LUA_RCDIR        LUA_ROOT "/" LUA_MULTILIB LUA_LUADIR

46. ‌#define LUA_RLPATH        ";" LUA_RLDIR "?.lua;" LUA_RLDIR "?/init.lua"

47. ‌#define LUA_RCPATH        ";" LUA_RCDIR "?.so"

48. #else

49. ‌#define LUA_JROOT        LUA_LROOT

50. ‌#define LUA_RLPATH

51. ‌#define LUA_RCPATH

52. #endif

54. ‌#define LUA_JPATH        ";" LUA_JROOT "/share" LUA_LJDIR "?.lua"

55. ‌#define LUA_LLDIR        LUA_LROOT "/share" LUA_LUADIR

56. ‌#define LUA_LCDIR        LUA_LROOT "/" LUA_LMULTILIB LUA_LUADIR

57. ‌#define LUA_LLPATH        ";" LUA_LLDIR "?.lua;" LUA_LLDIR "?/init.lua"

58. ‌#define LUA_LCPATH1        ";" LUA_LCDIR "?.so"

59. ‌#define LUA_LCPATH2        ";" LUA_LCDIR "loadall.so"

61. ‌#define LUA_PATH_DEFAULT        "./?.lua" LUA_JPATH LUA_LLPATH LUA_RLPATH

62. ‌#define LUA_CPATH_DEFAULT        "./?.so" LUA_LCPATH1 LUA_RCPATH LUA_LCPATH2

63. #endif

65. /* Environment variable names for path overrides and initialization code. */

66. ‌#define LUA_PATH        "LUA_PATH"

67. ‌#define LUA_CPATH        "LUA_CPATH"

68. ‌#define LUA_INIT        "LUA_INIT"

70. /* Special file system characters. */

71. #if defined(_WIN32)

72. ‌#define LUA_DIRSEP        "\\"

73. #else

74. ‌#define LUA_DIRSEP        "/"

75. #endif

76. ‌#define LUA_PATHSEP        ";"

77. ‌#define LUA_PATH_MARK        "?"

78. ‌#define LUA_EXECDIR        "!"

79. ‌#define LUA_IGMARK        "-"

80. ‌#define LUA_PATH_CONFIG \

81. LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" \

82. LUA_EXECDIR "\n" LUA_IGMARK

84. /* Quoting in error messages. */

85. ‌#define LUA_QL(x)        "'" x "'"

86. ‌#define LUA_QS                LUA_QL("%s")

88. /* Various tunables. */

89. ‌#define LUAI_MAXSTACK        65500        /* Max. # of stack slots for a thread (<64K). */

90. ‌#define LUAI_MAXCSTACK        8000        /* Max. # of stack slots for a C func (<10K). */

91. ‌#define LUAI_GCPAUSE        200        /* Pause GC until memory is at 200%. */

92. ‌#define LUAI_GCMUL        200        /* Run GC at 200% of allocation speed. */

93. ‌#define LUA_MAXCAPTURES        32        /* Max. pattern captures. */

95. /* Compatibility with older library function names. */

96. ‌#define LUA_COMPAT_MOD                /* OLD: math.mod, NEW: math.fmod */

97. ‌#define LUA_COMPAT_GFIND        /* OLD: string.gfind, NEW: string.gmatch */

99. /* Configuration for the frontend (the luajit executable). */

100. #if defined(luajit_c)

101. ‌#define LUA_PROGNAME        "luajit"  /* Fallback frontend name. */

102. ‌#define LUA_PROMPT        "> "        /* Interactive prompt. */

103. ‌#define LUA_PROMPT2        ">> "        /* Continuation prompt. */

104. ‌#define LUA_MAXINPUT        512        /* Max. input line length. */

105. #endif

107. /* Note: changing the following defines breaks the Lua 5.1 ABI. */

108. ‌#define LUA_INTEGER        ptrdiff_t

109. ‌#define LUA_IDSIZE        60        /* Size of lua_Debug.short_src. */

110. /*

111. ** Size of lauxlib and io.* on-stack buffers. Weird workaround to avoid using

112. ** unreasonable amounts of stack space, but still retain ABI compatibility.

113. ** Blame Lua for depending on BUFSIZ in the ABI, blame **** for wrecking it.

114. */

115. ‌#define LUAL_BUFFERSIZE        (BUFSIZ > 16384 ? 8192 : BUFSIZ)

117. /* The following defines are here only for compatibility with luaconf.h

118. ** from the standard Lua distribution. They must not be changed for LuaJIT.

119. */

120. ‌#define LUA_NUMBER_DOUBLE

121. ‌#define LUA_NUMBER                double

122. ‌#define LUAI_UACNUMBER                double

123. ‌#define LUA_NUMBER_SCAN                "%lf"

124. ‌#define LUA_NUMBER_FMT                "%.14g"

125. ‌#define lua_number2str(s, n)        sprintf((s), LUA_NUMBER_FMT, (n))

126. ‌#define LUAI_MAXNUMBER2STR        32

127. ‌#define LUA_INTFRMLEN                "l"

128. ‌#define LUA_INTFRM_T                long

130. /* Linkage of public API functions. */

131. #if defined(LUA_BUILD_AS_DLL)

132. #if defined(LUA_CORE) || defined(LUA_LIB)

133. ‌#define LUA_API                __declspec(dllexport)

134. #else

135. ‌#define LUA_API                __declspec(dllimport)

136. #endif

137. #else

138. ‌#define LUA_API                extern

139. #endif

141. ‌#define LUALIB_API        LUA_API

143. /* Support for internal assertions. */

144. #if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK)

145. #include <assert.h>

146. #endif

147. #ifdef LUA_USE_ASSERT

148. ‌#define lua_assert(x)                assert(x)

149. #endif

150. #ifdef LUA_USE_APICHECK

151. ‌#define luai_apicheck(L, o)        { (void)L; assert(o); }

152. #else

153. ‌#define luai_apicheck(L, o)        { (void)L; }

154. #endif

156. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/luajit.c - luajit-2.0-src

Global variables defined

- globalL

- progname

- smain

Data types defined

- Smain

Functions defined

- collectargs

- dobytecode

- docall

- dofile

- dojitcmd

- dojitopt

- dolibrary

- dostring

- dotty

- getargs

- handle_luainit

- handle_script

- incomplete

- l_message

- laction

- loadjitmodule

- loadline

- lstop

- main

- pmain

- print_jit_status

- print_usage

- print_version

- pushline

- report

- runargs

- runcmdopt

- traceback

- write_prompt

Macros defined

- FLAGS_EXEC

- FLAGS_INTERACTIVE

- FLAGS_NOENV

- FLAGS_OPTION

- FLAGS_VERSION

- lua_stdin_is_tty

- lua_stdin_is_tty

- lua_stdin_is_tty

- lua_stdin_is_tty

- luajit_c

- notail

Source code

1. /*

2. ** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. #include <stdio.h>

10. #include <stdlib.h>

11. #include <string.h>

13. ‌#define luajit_c

15. #include "lua.h"

16. #include "lauxlib.h"

17. #include "lualib.h"

18. #include "luajit.h"

20. #include "lj_arch.h"

22. #if LJ_TARGET_POSIX

23. #include <unistd.h>

24. ‌#define lua_stdin_is_tty()        isatty(0)

25. #elif LJ_TARGET_WINDOWS

26. #include <io.h>

27. #ifdef __BORLANDC__

28. ‌#define lua_stdin_is_tty()        isatty(_fileno(stdin))

29. #else

30. ‌#define lua_stdin_is_tty()        _isatty(_fileno(stdin))

31. #endif

32. #else

33. ‌#define lua_stdin_is_tty()        1

34. #endif

36. #if !LJ_TARGET_CONSOLE

37. #include <signal.h>

38. #endif

40. ‌static lua_State *globalL = NULL;

41. ‌static const char *progname = LUA_PROGNAME;

43. #if !LJ_TARGET_CONSOLE

44. ‌static void lstop(lua_State *L, lua_Debug *ar)

45. {

46. (void)ar;  /* unused arg. */

47. lua_sethook(L, NULL, 0, 0);

48. /* Avoid luaL_error -- a C hook doesn't add an extra frame. */

49. luaL_where(L, 0);

50. lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));

51. lua_error(L);

52. }

54. ‌static void laction(int i)

55. {

56. signal(i, SIG_DFL); /* if another SIGINT happens before lstop,

57. terminate process (default action) */

58. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);

59. }

60. #endif

62. ‌static void print_usage(void)

63. {

64. fputs("usage: ", stderr);

65. fputs(progname, stderr);

66. fputs(" [options]... [script [args]...].\n"

67. "Available options are:\n"

68. "  -e chunk  Execute string " LUA_QL("chunk") ".\n"

69. "  -l name   Require library " LUA_QL("name") ".\n"

70. "  -b ...    Save or list bytecode.\n"

71. "  -j cmd    Perform LuaJIT control command.\n"

72. "  -O[opt]   Control LuaJIT optimizations.\n"

73. "  -i        Enter interactive mode after executing " LUA_QL("script") ".\n"

74. "  -v        Show version information.\n"

75. "  -E        Ignore environment variables.\n"

76. "  --        Stop handling options.\n"

77. "  -         Execute stdin and stop handling options.\n", stderr);

78. fflush(stderr);

79. }

81. ‌static void l_message(const char *pname, const char *msg)

82. {

83. if (pname) { fputs(pname, stderr); fputc(':', stderr); fputc(' ', stderr); }

84. fputs(msg, stderr); fputc('\n', stderr);

85. fflush(stderr);

86. }

88. ‌static int report(lua_State *L, int status)

89. {

90. if (status && !lua_isnil(L, -1)) {

91. const char *msg = lua_tostring(L, -1);

92. if (msg == NULL) msg = "(error object is not a string)";

93. l_message(progname, msg);

94. lua_pop(L, 1);

95. }

96. return status;

97. }

99. ‌static int traceback(lua_State *L)

100. {

101. if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */

102. if (lua_isnoneornil(L, 1) ||

103. !luaL_callmeta(L, 1, "__tostring") ||

104. !lua_isstring(L, -1))

105. return 1;  /* Return non-string error object. */

106. lua_remove(L, 1);  /* Replace object by result of __tostring metamethod. */

107. }

108. luaL_traceback(L, L, lua_tostring(L, 1), 1);

109. return 1;

110. }

112. ‌static int docall(lua_State *L, int narg, int clear)

113. {

114. int status;

115. int base = lua_gettop(L) - narg;  /* function index */

116. lua_pushcfunction(L, traceback);  /* push traceback function */

117. lua_insert(L, base);  /* put it under chunk and args */

118. #if !LJ_TARGET_CONSOLE

119. signal(SIGINT, laction);

120. #endif

121. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);

122. #if !LJ_TARGET_CONSOLE

123. signal(SIGINT, SIG_DFL);

124. #endif

125. lua_remove(L, base);  /* remove traceback function */

126. /* force a complete garbage collection in case of errors */

127. if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);

128. return status;

129. }

131. ‌static void print_version(void)

132. {

133. fputs(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n", stdout);

134. }

136. ‌static void print_jit_status(lua_State *L)

137. {

138. int n;

139. const char *s;

140. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");

141. lua_getfield(L, -1, "jit");  /* Get jit.* module table. */

142. lua_remove(L, -2);

143. lua_getfield(L, -1, "status");

144. lua_remove(L, -2);

145. n = lua_gettop(L);

146. lua_call(L, 0, LUA_MULTRET);

147. fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout);

148. for (n++; (s = lua_tostring(L, n)); n++) {

149. putc(' ', stdout);

150. fputs(s, stdout);

151. }

152. putc('\n', stdout);

153. }

155. ‌static int getargs(lua_State *L, char **argv, int n)

156. {

157. int narg;

158. int i;

159. int argc = 0;

160. while (argv[argc]) argc++;  /* count total number of arguments */

161. narg = argc - (n + 1);  /* number of arguments to the script */

162. luaL_checkstack(L, narg + 3, "too many arguments to script");

163. for (i = n+1; i < argc; i++)

164. lua_pushstring(L, argv[i]);

165. lua_createtable(L, narg, n + 1);

166. for (i = 0; i < argc; i++) {

167. lua_pushstring(L, argv[i]);

168. lua_rawseti(L, -2, i - n);

169. }

170. return narg;

171. }

173. ‌static int dofile(lua_State *L, const char *name)

174. {

175. int status = luaL_loadfile(L, name) || docall(L, 0, 1);

176. return report(L, status);

177. }

179. ‌static int dostring(lua_State *L, const char *s, const char *name)

180. {

181. int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);

182. return report(L, status);

183. }

185. ‌static int dolibrary(lua_State *L, const char *name)

186. {

187. lua_getglobal(L, "require");

188. lua_pushstring(L, name);

189. return report(L, docall(L, 1, 1));

190. }

192. ‌static void write_prompt(lua_State *L, int firstline)

193. {

194. const char *p;

195. lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");

196. p = lua_tostring(L, -1);

197. if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2;

198. fputs(p, stdout);

199. fflush(stdout);

200. lua_pop(L, 1);  /* remove global */

201. }

203. ‌static int incomplete(lua_State *L, int status)

204. {

205. if (status == LUA_ERRSYNTAX) {

206. size_t lmsg;

207. const char *msg = lua_tolstring(L, -1, &lmsg);

208. const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);

209. if (strstr(msg, LUA_QL("<eof>")) == tp) {

210. lua_pop(L, 1);

211. return 1;

212. }

213. }

214. return 0;  /* else... */

215. }

217. ‌static int pushline(lua_State *L, int firstline)

218. {

219. char buf[LUA_MAXINPUT];

220. write_prompt(L, firstline);

221. if (fgets(buf, LUA_MAXINPUT, stdin)) {

222. size_t len = strlen(buf);

223. if (len > 0 && buf[len-1] == '\n')

224. buf[len-1] = '\0';

225. if (firstline && buf[0] == '=')

226. lua_pushfstring(L, "return %s", buf+1);

227. else

228. lua_pushstring(L, buf);

229. return 1;

230. }

231. return 0;

232. }

234. ‌static int loadline(lua_State *L)

235. {

236. int status;

237. lua_settop(L, 0);

238. if (!pushline(L, 1))

239. return -1;  /* no input */

240. for (;;) {  /* repeat until gets a complete line */

241. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");

242. if (!incomplete(L, status)) break;  /* cannot try to add lines? */

243. if (!pushline(L, 0))  /* no more input? */

244. return -1;

245. lua_pushliteral(L, "\n");  /* add a new line... */

246. lua_insert(L, -2);  /* ...between the two lines */

247. lua_concat(L, 3);  /* join them */

248. }

249. lua_remove(L, 1);  /* remove line */

250. return status;

251. }

253. ‌static void dotty(lua_State *L)

254. {

255. int status;

256. const char *oldprogname = progname;

257. progname = NULL;

258. while ((status = loadline(L)) != -1) {

259. if (status == 0) status = docall(L, 0, 0);

260. report(L, status);

261. if (status == 0 && lua_gettop(L) > 0) {  /* any result to print? */

262. lua_getglobal(L, "print");

263. lua_insert(L, 1);

264. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)

265. l_message(progname,

266. lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",

267. lua_tostring(L, -1)));

268. }

269. }

270. lua_settop(L, 0);  /* clear stack */

271. fputs("\n", stdout);

272. fflush(stdout);

273. progname = oldprogname;

274. }

276. ‌static int handle_script(lua_State *L, char **argv, int n)

277. {

278. int status;

279. const char *fname;

280. int narg = getargs(L, argv, n);  /* collect arguments */

281. lua_setglobal(L, "arg");

282. fname = argv[n];

283. if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)

284. fname = NULL;  /* stdin */

285. status = luaL_loadfile(L, fname);

286. lua_insert(L, -(narg+1));

287. if (status == 0)

288. status = docall(L, narg, 0);

289. else

290. lua_pop(L, narg);

291. return report(L, status);

292. }

294. /* Load add-on module. */

295. ‌static int loadjitmodule(lua_State *L)

296. {

297. lua_getglobal(L, "require");

298. lua_pushliteral(L, "jit.");

299. lua_pushvalue(L, -3);

300. lua_concat(L, 2);

301. if (lua_pcall(L, 1, 1, 0)) {

302. const char *msg = lua_tostring(L, -1);

303. if (msg && !strncmp(msg, "module ", 7))

304. goto nomodule;

305. return report(L, 1);

306. }

307. lua_getfield(L, -1, "start");

308. if (lua_isnil(L, -1)) {

309. nomodule:

310. l_message(progname,

311. "unknown luaJIT command or jit.* modules not installed");

312. return 1;

313. }

314. lua_remove(L, -2);  /* Drop module table. */

315. return 0;

316. }

318. /* Run command with options. */

319. ‌static int runcmdopt(lua_State *L, const char *opt)

320. {

321. int narg = 0;

322. if (opt && *opt) {

323. for (;;) {  /* Split arguments. */

324. const char *p = strchr(opt, ',');

325. narg++;

326. if (!p) break;

327. if (p == opt)

328. lua_pushnil(L);

329. else

330. lua_pushlstring(L, opt, (size_t)(p - opt));

331. opt = p + 1;

332. }

333. if (*opt)

334. lua_pushstring(L, opt);

335. else

336. lua_pushnil(L);

337. }

338. return report(L, lua_pcall(L, narg, 0, 0));

339. }

341. /* JIT engine control command: try jit library first or load add-on module. */

342. ‌static int dojitcmd(lua_State *L, const char *cmd)

343. {

344. const char *opt = strchr(cmd, '=');

345. lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd));

346. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");

347. lua_getfield(L, -1, "jit");  /* Get jit.* module table. */

348. lua_remove(L, -2);

349. lua_pushvalue(L, -2);

350. lua_gettable(L, -2);  /* Lookup library function. */

351. if (!lua_isfunction(L, -1)) {

352. lua_pop(L, 2);  /* Drop non-function and jit.* table, keep module name. */

353. if (loadjitmodule(L))

354. return 1;

355. } else {

356. lua_remove(L, -2);  /* Drop jit.* table. */

357. }

358. lua_remove(L, -2);  /* Drop module name. */

359. return runcmdopt(L, opt ? opt+1 : opt);

360. }

362. /* Optimization flags. */

363. ‌static int dojitopt(lua_State *L, const char *opt)

364. {

365. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");

366. lua_getfield(L, -1, "jit.opt");  /* Get jit.opt.* module table. */

367. lua_remove(L, -2);

368. lua_getfield(L, -1, "start");

369. lua_remove(L, -2);

370. return runcmdopt(L, opt);

371. }

373. /* Save or list bytecode. */

374. ‌static int dobytecode(lua_State *L, char **argv)

375. {

376. int narg = 0;

377. lua_pushliteral(L, "bcsave");

378. if (loadjitmodule(L))

379. return 1;

380. if (argv[0][2]) {

381. narg++;

382. argv[0][1] = '-';

383. lua_pushstring(L, argv[0]+1);

384. }

385. for (argv++; *argv != NULL; narg++, argv++)

386. lua_pushstring(L, *argv);

387. return report(L, lua_pcall(L, narg, 0, 0));

388. }

390. /* check that argument has no extra characters at the end */

391. ‌#define notail(x)        {if ((x)[2] != '\0') return -1;}

393. ‌#define FLAGS_INTERACTIVE        1

394. ‌#define FLAGS_VERSION                2

395. ‌#define FLAGS_EXEC                4

396. ‌#define FLAGS_OPTION                8

397. ‌#define FLAGS_NOENV                16

399. ‌static int collectargs(char **argv, int *flags)

400. {

401. int i;

402. for (i = 1; argv[i] != NULL; i++) {

403. if (argv[i][0] != '-')  /* Not an option? */

404. return i;

405. switch (argv[i][1]) {  /* Check option. */

406. case '-':

407. notail(argv[i]);

408. return (argv[i+1] != NULL ? i+1 : 0);

409. case '\0':

410. return i;

411. case 'i':

412. notail(argv[i]);

413. *flags |= FLAGS_INTERACTIVE;

414. /* fallthrough */

415. case 'v':

416. notail(argv[i]);

417. *flags |= FLAGS_VERSION;

418. break;

419. case 'e':

420. *flags |= FLAGS_EXEC;

421. case 'j':  /* LuaJIT extension */

422. case 'l':

423. *flags |= FLAGS_OPTION;

424. if (argv[i][2] == '\0') {

425. i++;

426. if (argv[i] == NULL) return -1;

427. }

428. break;

429. case 'O': break;  /* LuaJIT extension */

430. case 'b':  /* LuaJIT extension */

431. if (*flags) return -1;

432. *flags |= FLAGS_EXEC;

433. return 0;

434. case 'E':

435. *flags |= FLAGS_NOENV;

436. break;

437. default: return -1;  /* invalid option */

438. }

439. }

440. return 0;

441. }

443. ‌static int runargs(lua_State *L, char **argv, int n)

444. {

445. int i;

446. for (i = 1; i < n; i++) {

447. if (argv[i] == NULL) continue;

448. lua_assert(argv[i][0] == '-');

449. switch (argv[i][1]) {  /* option */

450. case 'e': {

451. const char *chunk = argv[i] + 2;

452. if (*chunk == '\0') chunk = argv[++i];

453. lua_assert(chunk != NULL);

454. if (dostring(L, chunk, "=(command line)") != 0)

455. return 1;

456. break;

457. }

458. case 'l': {

459. const char *filename = argv[i] + 2;

460. if (*filename == '\0') filename = argv[++i];

461. lua_assert(filename != NULL);

462. if (dolibrary(L, filename))

463. return 1;  /* stop if file fails */

464. break;

465. }

466. case 'j': {  /* LuaJIT extension */

467. const char *cmd = argv[i] + 2;

468. if (*cmd == '\0') cmd = argv[++i];

469. lua_assert(cmd != NULL);

470. if (dojitcmd(L, cmd))

471. return 1;

472. break;

473. }

474. case 'O':  /* LuaJIT extension */

475. if (dojitopt(L, argv[i] + 2))

476. return 1;

477. break;

478. case 'b':  /* LuaJIT extension */

479. return dobytecode(L, argv+i);

480. default: break;

481. }

482. }

483. return 0;

484. }

486. ‌static int handle_luainit(lua_State *L)

487. {

488. #if LJ_TARGET_CONSOLE

489. const char *init = NULL;

490. #else

491. const char *init = getenv(LUA_INIT);

492. #endif

493. if (init == NULL)

494. return 0;  /* status OK */

495. else if (init[0] == '@')

496. return dofile(L, init+1);

497. else

498. return dostring(L, init, "=" LUA_INIT);

499. }

501. ‌static struct Smain {

502. char **argv;

503. int argc;

504. int status;

505. ‌} smain;

507. ‌static int pmain(lua_State *L)

508. {

509. struct Smain *s = &smain;

510. char **argv = s->argv;

511. int script;

512. int flags = 0;

513. globalL = L;

514. if (argv[0] && argv[0][0]) progname = argv[0];

515. LUAJIT_VERSION_SYM();  /* linker-enforced version check */

516. script = collectargs(argv, &flags);

517. if (script < 0) {  /* invalid args? */

518. print_usage();

519. s->status = 1;

520. return 0;

521. }

522. if ((flags & FLAGS_NOENV)) {

523. lua_pushboolean(L, 1);

524. lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");

525. }

526. lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */

527. luaL_openlibs(L);  /* open libraries */

528. lua_gc(L, LUA_GCRESTART, -1);

529. if (!(flags & FLAGS_NOENV)) {

530. s->status = handle_luainit(L);

531. if (s->status != 0) return 0;

532. }

533. if ((flags & FLAGS_VERSION)) print_version();

534. s->status = runargs(L, argv, (script > 0) ? script : s->argc);

535. if (s->status != 0) return 0;

536. if (script) {

537. s->status = handle_script(L, argv, script);

538. if (s->status != 0) return 0;

539. }

540. if ((flags & FLAGS_INTERACTIVE)) {

541. print_jit_status(L);

542. dotty(L);

543. } else if (script == 0 && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) {

544. if (lua_stdin_is_tty()) {

545. print_version();

546. print_jit_status(L);

547. dotty(L);

548. } else {

549. dofile(L, NULL);  /* executes stdin as a file */

550. }

551. }

552. return 0;

553. }

555. ‌int main(int argc, char **argv)

556. {

557. int status;

558. lua_State *L = lua_open();  /* create state */

559. if (L == NULL) {

560. l_message(argv[0], "cannot create state: not enough memory");

561. return EXIT_FAILURE;

562. }

563. smain.argc = argc;

564. smain.argv = argv;

565. status = lua_cpcall(L, pmain, NULL);

566. report(L, status);

567. lua_close(L);

568. return (status || smain.status) ? EXIT_FAILURE : EXIT_SUCCESS;

569. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_dispatch.c - luajit-2.0-src

Global variables defined

- dispatch_got

Functions defined

- LUA_API void LUAJIT_VERSION_SYM(void)

- call_init

- callhook

- cur_topslot

- lj_dispatch_call

- lj_dispatch_init

- lj_dispatch_init_hotcount

- lj_dispatch_ins

- lj_dispatch_profile

- lj_dispatch_stitch

- lj_dispatch_update

- luaJIT_setmode

- lua_gethook

- lua_gethookcount

- lua_gethookmask

- lua_sethook

- setptmode

- setptmode_all

Macros defined

- DISPMODE_CALL

- DISPMODE_INS

- DISPMODE_JIT

- DISPMODE_PROF

- DISPMODE_REC

- DISPMODE_RET

- GOTFUNC

- GOTFUNC

- LUA_CORE

- lj_dispatch_c

- lj_dispatch_profile

- lj_dispatch_stitch

Source code

1. /*

2. ** Instruction dispatch handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_dispatch_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #include "lj_err.h"

11. #include "lj_buf.h"

12. #include "lj_func.h"

13. #include "lj_str.h"

14. #include "lj_tab.h"

15. #include "lj_meta.h"

16. #include "lj_debug.h"

17. #include "lj_state.h"

18. #include "lj_frame.h"

19. #include "lj_bc.h"

20. #include "lj_ff.h"

21. #include "lj_strfmt.h"

22. #if LJ_HASJIT

23. #include "lj_jit.h"

24. #endif

25. #if LJ_HASFFI

26. #include "lj_ccallback.h"

27. #endif

28. #include "lj_trace.h"

29. #include "lj_dispatch.h"

30. #if LJ_HASPROFILE

31. #include "lj_profile.h"

32. #endif

33. #include "lj_vm.h"

34. #include "luajit.h"

36. /* Bump GG_NUM_ASMFF in lj_dispatch.h as needed. Ugly. */

37. LJ_STATIC_ASSERT(GG_NUM_ASMFF == FF_NUM_ASMFUNC);

39. /* -- Dispatch table management ------------------------------------------- */

41. #if LJ_TARGET_MIPS

42. #include <math.h>

43. LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L,

44. lua_State *co);

45. #if !LJ_HASJIT

46. ‌#define lj_dispatch_stitch        lj_dispatch_ins

47. #endif

48. #if !LJ_HASPROFILE

49. ‌#define lj_dispatch_profile        lj_dispatch_ins

50. #endif

52. ‌#define GOTFUNC(name)        (ASMFunction)name,

53. ‌static const ASMFunction dispatch_got[] = {

54. GOTDEF(GOTFUNC)

55. };

56. ‌#undef GOTFUNC

57. #endif

59. /* Initialize instruction dispatch table and hot counters. */

60. ‌void lj_dispatch_init(GG_State *GG)

61. {

62. uint32_t i;

63. ASMFunction *disp = GG->dispatch;

64. for (i = 0; i < GG_LEN_SDISP; i++)

65. disp[GG_LEN_DDISP+i] = disp[i] = makeasmfunc(lj_bc_ofs[i]);

66. for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++)

67. disp[i] = makeasmfunc(lj_bc_ofs[i]);

68. /* The JIT engine is off by default. luaopen_jit() turns it on. */

69. disp[BC_FORL] = disp[BC_IFORL];

70. disp[BC_ITERL] = disp[BC_IITERL];

71. disp[BC_LOOP] = disp[BC_ILOOP];

72. disp[BC_FUNCF] = disp[BC_IFUNCF];

73. disp[BC_FUNCV] = disp[BC_IFUNCV];

74. GG->g.bc_cfunc_ext = GG->g.bc_cfunc_int = BCINS_AD(BC_FUNCC, LUA_MINSTACK, 0);

75. for (i = 0; i < GG_NUM_ASMFF; i++)

76. GG->bcff[i] = BCINS_AD(BC__MAX+i, 0, 0);

77. #if LJ_TARGET_MIPS

78. memcpy(GG->got, dispatch_got, LJ_GOT__MAX*4);

79. #endif

80. }

82. #if LJ_HASJIT

83. /* Initialize hotcount table. */

84. ‌void lj_dispatch_init_hotcount(global_State *g)

85. {

86. int32_t hotloop = G2J(g)->param[JIT_P_hotloop];

87. HotCount start = (HotCount)(hotloop*HOTCOUNT_LOOP - 1);

88. HotCount *hotcount = G2GG(g)->hotcount;

89. uint32_t i;

90. for (i = 0; i < HOTCOUNT_SIZE; i++)

91. hotcount[i] = start;

92. }

93. #endif

95. /* Internal dispatch mode bits. */

96. ‌#define DISPMODE_CALL        0x01        /* Override call dispatch. */

97. ‌#define DISPMODE_RET        0x02        /* Override return dispatch. */

98. ‌#define DISPMODE_INS        0x04        /* Override instruction dispatch. */

99. ‌#define DISPMODE_JIT        0x10        /* JIT compiler on. */

100. ‌#define DISPMODE_REC        0x20        /* Recording active. */

101. ‌#define DISPMODE_PROF        0x40        /* Profiling active. */

103. /* Update dispatch table depending on various flags. */

104. ‌void lj_dispatch_update(global_State *g)

105. {

106. uint8_t oldmode = g->dispatchmode;

107. uint8_t mode = 0;

108. #if LJ_HASJIT

109. mode |= (G2J(g)->flags & JIT_F_ON) ? DISPMODE_JIT : 0;

110. mode |= G2J(g)->state != LJ_TRACE_IDLE ?

111. (DISPMODE_REC|DISPMODE_INS|DISPMODE_CALL) : 0;

112. #endif

113. #if LJ_HASPROFILE

114. mode |= (g->hookmask & HOOK_PROFILE) ? (DISPMODE_PROF|DISPMODE_INS) : 0;

115. #endif

116. mode |= (g->hookmask & (LUA_MASKLINE|LUA_MASKCOUNT)) ? DISPMODE_INS : 0;

117. mode |= (g->hookmask & LUA_MASKCALL) ? DISPMODE_CALL : 0;

118. mode |= (g->hookmask & LUA_MASKRET) ? DISPMODE_RET : 0;

119. if (oldmode != mode) {  /* Mode changed? */

120. ASMFunction *disp = G2GG(g)->dispatch;

121. ASMFunction f_forl, f_iterl, f_loop, f_funcf, f_funcv;

122. g->dispatchmode = mode;

124. /* Hotcount if JIT is on, but not while recording. */

125. if ((mode & (DISPMODE_JIT|DISPMODE_REC)) == DISPMODE_JIT) {

126. f_forl = makeasmfunc(lj_bc_ofs[BC_FORL]);

127. f_iterl = makeasmfunc(lj_bc_ofs[BC_ITERL]);

128. f_loop = makeasmfunc(lj_bc_ofs[BC_LOOP]);

129. f_funcf = makeasmfunc(lj_bc_ofs[BC_FUNCF]);

130. f_funcv = makeasmfunc(lj_bc_ofs[BC_FUNCV]);

131. } else {  /* Otherwise use the non-hotcounting instructions. */

132. f_forl = disp[GG_LEN_DDISP+BC_IFORL];

133. f_iterl = disp[GG_LEN_DDISP+BC_IITERL];

134. f_loop = disp[GG_LEN_DDISP+BC_ILOOP];

135. f_funcf = makeasmfunc(lj_bc_ofs[BC_IFUNCF]);

136. f_funcv = makeasmfunc(lj_bc_ofs[BC_IFUNCV]);

137. }

138. /* Init static counting instruction dispatch first (may be copied below). */

139. disp[GG_LEN_DDISP+BC_FORL] = f_forl;

140. disp[GG_LEN_DDISP+BC_ITERL] = f_iterl;

141. disp[GG_LEN_DDISP+BC_LOOP] = f_loop;

143. /* Set dynamic instruction dispatch. */

144. if ((oldmode ^ mode) & (DISPMODE_PROF|DISPMODE_REC|DISPMODE_INS)) {

145. /* Need to update the whole table. */

146. if (!(mode & DISPMODE_INS)) {  /* No ins dispatch? */

147. /* Copy static dispatch table to dynamic dispatch table. */

148. memcpy(&disp[0], &disp[GG_LEN_DDISP], GG_LEN_SDISP*sizeof(ASMFunction));

149. /* Overwrite with dynamic return dispatch. */

150. if ((mode & DISPMODE_RET)) {

151. disp[BC_RETM] = lj_vm_rethook;

152. disp[BC_RET] = lj_vm_rethook;

153. disp[BC_RET0] = lj_vm_rethook;

154. disp[BC_RET1] = lj_vm_rethook;

155. }

156. } else {

157. /* The recording dispatch also checks for hooks. */

158. ASMFunction f = (mode & DISPMODE_PROF) ? lj_vm_profhook :

159. (mode & DISPMODE_REC) ? lj_vm_record : lj_vm_inshook;

160. uint32_t i;

161. for (i = 0; i < GG_LEN_SDISP; i++)

162. disp[i] = f;

163. }

164. } else if (!(mode & DISPMODE_INS)) {

165. /* Otherwise set dynamic counting ins. */

166. disp[BC_FORL] = f_forl;

167. disp[BC_ITERL] = f_iterl;

168. disp[BC_LOOP] = f_loop;

169. /* Set dynamic return dispatch. */

170. if ((mode & DISPMODE_RET)) {

171. disp[BC_RETM] = lj_vm_rethook;

172. disp[BC_RET] = lj_vm_rethook;

173. disp[BC_RET0] = lj_vm_rethook;

174. disp[BC_RET1] = lj_vm_rethook;

175. } else {

176. disp[BC_RETM] = disp[GG_LEN_DDISP+BC_RETM];

177. disp[BC_RET] = disp[GG_LEN_DDISP+BC_RET];

178. disp[BC_RET0] = disp[GG_LEN_DDISP+BC_RET0];

179. disp[BC_RET1] = disp[GG_LEN_DDISP+BC_RET1];

180. }

181. }

183. /* Set dynamic call dispatch. */

184. if ((oldmode ^ mode) & DISPMODE_CALL) {  /* Update the whole table? */

185. uint32_t i;

186. if ((mode & DISPMODE_CALL) == 0) {  /* No call hooks? */

187. for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++)

188. disp[i] = makeasmfunc(lj_bc_ofs[i]);

189. } else {

190. for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++)

191. disp[i] = lj_vm_callhook;

192. }

193. }

194. if (!(mode & DISPMODE_CALL)) {  /* Overwrite dynamic counting ins. */

195. disp[BC_FUNCF] = f_funcf;

196. disp[BC_FUNCV] = f_funcv;

197. }

199. #if LJ_HASJIT

200. /* Reset hotcounts for JIT off to on transition. */

201. if ((mode & DISPMODE_JIT) && !(oldmode & DISPMODE_JIT))

202. lj_dispatch_init_hotcount(g);

203. #endif

204. }

205. }

207. /* -- JIT mode setting ---------------------------------------------------- */

209. #if LJ_HASJIT

210. /* Set JIT mode for a single prototype. */

211. ‌static void setptmode(global_State *g, GCproto *pt, int mode)

212. {

213. if ((mode & LUAJIT_MODE_ON)) {  /* (Re-)enable JIT compilation. */

214. pt->flags &= ~PROTO_NOJIT;

215. lj_trace_reenableproto(pt);  /* Unpatch all ILOOP etc. bytecodes. */

216. } else {  /* Flush and/or disable JIT compilation. */

217. if (!(mode & LUAJIT_MODE_FLUSH))

218. pt->flags |= PROTO_NOJIT;

219. lj_trace_flushproto(g, pt);  /* Flush all traces of prototype. */

220. }

221. }

223. /* Recursively set the JIT mode for all children of a prototype. */

224. ‌static void setptmode_all(global_State *g, GCproto *pt, int mode)

225. {

226. ptrdiff_t i;

227. if (!(pt->flags & PROTO_CHILD)) return;

228. for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) {

229. GCobj *o = proto_kgc(pt, i);

230. if (o->gch.gct == ~LJ_TPROTO) {

231. setptmode(g, gco2pt(o), mode);

232. setptmode_all(g, gco2pt(o), mode);

233. }

234. }

235. }

236. #endif

238. /* Public API function: control the JIT engine. */

239. ‌int luaJIT_setmode(lua_State *L, int idx, int mode)

240. {

241. global_State *g = G(L);

242. int mm = mode & LUAJIT_MODE_MASK;

243. lj_trace_abort(g);  /* Abort recording on any state change. */

244. /* Avoid pulling the rug from under our own feet. */

245. if ((g->hookmask & HOOK_GC))

246. lj_err_caller(L, LJ_ERR_NOGCMM);

247. switch (mm) {

248. #if LJ_HASJIT

249. case LUAJIT_MODE_ENGINE:

250. if ((mode & LUAJIT_MODE_FLUSH)) {

251. lj_trace_flushall(L);

252. } else {

253. if (!(mode & LUAJIT_MODE_ON))

254. G2J(g)->flags &= ~(uint32_t)JIT_F_ON;

255. #if LJ_TARGET_X86ORX64

256. else if ((G2J(g)->flags & JIT_F_SSE2))

257. G2J(g)->flags |= (uint32_t)JIT_F_ON;

258. else

259. return 0;  /* Don't turn on JIT compiler without SSE2 support. */

260. #else

261. else

262. G2J(g)->flags |= (uint32_t)JIT_F_ON;

263. #endif

264. lj_dispatch_update(g);

265. }

266. break;

267. case LUAJIT_MODE_FUNC:

268. case LUAJIT_MODE_ALLFUNC:

269. case LUAJIT_MODE_ALLSUBFUNC: {

270. cTValue *tv = idx == 0 ? frame_prev(L->base-1) :

271. idx > 0 ? L->base + (idx-1) : L->top + idx;

272. GCproto *pt;

273. if ((idx == 0 || tvisfunc(tv)) && isluafunc(&gcval(tv)->fn))

274. pt = funcproto(&gcval(tv)->fn);  /* Cannot use funcV() for frame slot. */

275. else if (tvisproto(tv))

276. pt = protoV(tv);

277. else

278. return 0;  /* Failed. */

279. if (mm != LUAJIT_MODE_ALLSUBFUNC)

280. setptmode(g, pt, mode);

281. if (mm != LUAJIT_MODE_FUNC)

282. setptmode_all(g, pt, mode);

283. break;

284. }

285. case LUAJIT_MODE_TRACE:

286. if (!(mode & LUAJIT_MODE_FLUSH))

287. return 0;  /* Failed. */

288. lj_trace_flush(G2J(g), idx);

289. break;

290. #else

291. case LUAJIT_MODE_ENGINE:

292. case LUAJIT_MODE_FUNC:

293. case LUAJIT_MODE_ALLFUNC:

294. case LUAJIT_MODE_ALLSUBFUNC:

295. UNUSED(idx);

296. if ((mode & LUAJIT_MODE_ON))

297. return 0;  /* Failed. */

298. break;

299. #endif

300. case LUAJIT_MODE_WRAPCFUNC:

301. if ((mode & LUAJIT_MODE_ON)) {

302. if (idx != 0) {

303. cTValue *tv = idx > 0 ? L->base + (idx-1) : L->top + idx;

304. if (tvislightud(tv))

305. g->wrapf = (lua_CFunction)lightudV(tv);

306. else

307. return 0;  /* Failed. */

308. } else {

309. return 0;  /* Failed. */

310. }

311. g->bc_cfunc_ext = BCINS_AD(BC_FUNCCW, 0, 0);

312. } else {

313. g->bc_cfunc_ext = BCINS_AD(BC_FUNCC, 0, 0);

314. }

315. break;

316. default:

317. return 0;  /* Failed. */

318. }

319. return 1;  /* OK. */

320. }

322. /* Enforce (dynamic) linker error for version mismatches. See luajit.c. */

323. ‌LUA_API void LUAJIT_VERSION_SYM(void)

324. {

325. }

327. /* -- Hooks --------------------------------------------------------------- */

329. /* This function can be called asynchronously (e.g. during a signal). */

330. ‌LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count)

331. {

332. global_State *g = G(L);

333. mask &= HOOK_EVENTMASK;

334. if (func == NULL || mask == 0) { mask = 0; func = NULL; }  /* Consistency. */

335. g->hookf = func;

336. g->hookcount = g->hookcstart = (int32_t)count;

337. g->hookmask = (uint8_t)((g->hookmask & ~HOOK_EVENTMASK) | mask);

338. lj_trace_abort(g);  /* Abort recording on any hook change. */

339. lj_dispatch_update(g);

340. return 1;

341. }

343. ‌LUA_API lua_Hook lua_gethook(lua_State *L)

344. {

345. return G(L)->hookf;

346. }

348. ‌LUA_API int lua_gethookmask(lua_State *L)

349. {

350. return G(L)->hookmask & HOOK_EVENTMASK;

351. }

353. ‌LUA_API int lua_gethookcount(lua_State *L)

354. {

355. return (int)G(L)->hookcstart;

356. }

358. /* Call a hook. */

359. ‌static void callhook(lua_State *L, int event, BCLine line)

360. {

361. global_State *g = G(L);

362. lua_Hook hookf = g->hookf;

363. if (hookf && !hook_active(g)) {

364. lua_Debug ar;

365. lj_trace_abort(g);  /* Abort recording on any hook call. */

366. ar.event = event;

367. ar.currentline = line;

368. /* Top frame, nextframe = NULL. */

369. ar.i_ci = (int)((L->base-1) - tvref(L->stack));

370. lj_state_checkstack(L, 1+LUA_MINSTACK);

371. #if LJ_HASPROFILE && !LJ_PROFILE_SIGPROF

372. lj_profile_hook_enter(g);

373. #else

374. hook_enter(g);

375. #endif

376. hookf(L, &ar);

377. lua_assert(hook_active(g));

378. setgcref(g->cur_L, obj2gco(L));

379. #if LJ_HASPROFILE && !LJ_PROFILE_SIGPROF

380. lj_profile_hook_leave(g);

381. #else

382. hook_leave(g);

383. #endif

384. }

385. }

387. /* -- Dispatch callbacks -------------------------------------------------- */

389. /* Calculate number of used stack slots in the current frame. */

390. ‌static BCReg cur_topslot(GCproto *pt, const BCIns *pc, uint32_t nres)

391. {

392. BCIns ins = pc[-1];

393. if (bc_op(ins) == BC_UCLO)

394. ins = pc[bc_j(ins)];

395. switch (bc_op(ins)) {

396. case BC_CALLM: case BC_CALLMT: return bc_a(ins) + bc_c(ins) + nres-1+1+LJ_FR2;

397. case BC_RETM: return bc_a(ins) + bc_d(ins) + nres-1;

398. case BC_TSETM: return bc_a(ins) + nres-1;

399. default: return pt->framesize;

400. }

401. }

403. /* Instruction dispatch. Used by instr/line/return hooks or when recording. */

404. ‌void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc)

405. {

406. ERRNO_SAVE

407. GCfunc *fn = curr_func(L);

408. GCproto *pt = funcproto(fn);

409. void *cf = cframe_raw(L->cframe);

410. const BCIns *oldpc = cframe_pc(cf);

411. global_State *g = G(L);

412. BCReg slots;

413. setcframe_pc(cf, pc);

414. slots = cur_topslot(pt, pc, cframe_multres_n(cf));

415. L->top = L->base + slots;  /* Fix top. */

416. #if LJ_HASJIT

417. {

418. jit_State *J = G2J(g);

419. if (J->state != LJ_TRACE_IDLE) {

420. #ifdef LUA_USE_ASSERT

421. ptrdiff_t delta = L->top - L->base;

422. #endif

423. J->L = L;

424. lj_trace_ins(J, pc-1);  /* The interpreter bytecode PC is offset by 1. */

425. lua_assert(L->top - L->base == delta);

426. }

427. }

428. #endif

429. if ((g->hookmask & LUA_MASKCOUNT) && g->hookcount == 0) {

430. g->hookcount = g->hookcstart;

431. callhook(L, LUA_HOOKCOUNT, -1);

432. L->top = L->base + slots;  /* Fix top again. */

433. }

434. if ((g->hookmask & LUA_MASKLINE)) {

435. BCPos npc = proto_bcpos(pt, pc) - 1;

436. BCPos opc = proto_bcpos(pt, oldpc) - 1;

437. BCLine line = lj_debug_line(pt, npc);

438. if (pc <= oldpc || opc >= pt->sizebc || line != lj_debug_line(pt, opc)) {

439. callhook(L, LUA_HOOKLINE, line);

440. L->top = L->base + slots;  /* Fix top again. */

441. }

442. }

443. if ((g->hookmask & LUA_MASKRET) && bc_isret(bc_op(pc[-1])))

444. callhook(L, LUA_HOOKRET, -1);

445. ERRNO_RESTORE

446. }

448. /* Initialize call. Ensure stack space and return # of missing parameters. */

449. ‌static int call_init(lua_State *L, GCfunc *fn)

450. {

451. if (isluafunc(fn)) {

452. GCproto *pt = funcproto(fn);

453. int numparams = pt->numparams;

454. int gotparams = (int)(L->top - L->base);

455. int need = pt->framesize;

456. if ((pt->flags & PROTO_VARARG)) need += 1+gotparams;

457. lj_state_checkstack(L, (MSize)need);

458. numparams -= gotparams;

459. return numparams >= 0 ? numparams : 0;

460. } else {

461. lj_state_checkstack(L, LUA_MINSTACK);

462. return 0;

463. }

464. }

466. /* Call dispatch. Used by call hooks, hot calls or when recording. */

467. ‌ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns *pc)

468. {

469. ERRNO_SAVE

470. GCfunc *fn = curr_func(L);

471. BCOp op;

472. global_State *g = G(L);

473. #if LJ_HASJIT

474. jit_State *J = G2J(g);

475. #endif

476. int missing = call_init(L, fn);

477. #if LJ_HASJIT

478. J->L = L;

479. if ((uintptr_t)pc & 1) {  /* Marker for hot call. */

480. #ifdef LUA_USE_ASSERT

481. ptrdiff_t delta = L->top - L->base;

482. #endif

483. pc = (const BCIns *)((uintptr_t)pc & ~(uintptr_t)1);

484. lj_trace_hot(J, pc);

485. lua_assert(L->top - L->base == delta);

486. goto out;

487. } else if (J->state != LJ_TRACE_IDLE &&

488. !(g->hookmask & (HOOK_GC|HOOK_VMEVENT))) {

489. #ifdef LUA_USE_ASSERT

490. ptrdiff_t delta = L->top - L->base;

491. #endif

492. /* Record the FUNC* bytecodes, too. */

493. lj_trace_ins(J, pc-1);  /* The interpreter bytecode PC is offset by 1. */

494. lua_assert(L->top - L->base == delta);

495. }

496. #endif

497. if ((g->hookmask & LUA_MASKCALL)) {

498. int i;

499. for (i = 0; i < missing; i++)  /* Add missing parameters. */

500. setnilV(L->top++);

501. callhook(L, LUA_HOOKCALL, -1);

502. /* Preserve modifications of missing parameters by lua_setlocal(). */

503. while (missing-- > 0 && tvisnil(L->top - 1))

504. L->top--;

505. }

506. #if LJ_HASJIT

507. out:

508. #endif

509. op = bc_op(pc[-1]);  /* Get FUNC* op. */

510. #if LJ_HASJIT

511. /* Use the non-hotcounting variants if JIT is off or while recording. */

512. if ((!(J->flags & JIT_F_ON) || J->state != LJ_TRACE_IDLE) &&

513. (op == BC_FUNCF || op == BC_FUNCV))

514. op = (BCOp)((int)op+(int)BC_IFUNCF-(int)BC_FUNCF);

515. #endif

516. ERRNO_RESTORE

517. return makeasmfunc(lj_bc_ofs[op]);  /* Return static dispatch target. */

518. }

520. #if LJ_HASJIT

521. /* Stitch a new trace. */

522. ‌void LJ_FASTCALL lj_dispatch_stitch(jit_State *J, const BCIns *pc)

523. {

524. ERRNO_SAVE

525. lua_State *L = J->L;

526. void *cf = cframe_raw(L->cframe);

527. const BCIns *oldpc = cframe_pc(cf);

528. setcframe_pc(cf, pc);

529. /* Before dispatch, have to bias PC by 1. */

530. L->top = L->base + cur_topslot(curr_proto(L), pc+1, cframe_multres_n(cf));

531. lj_trace_stitch(J, pc-1);  /* Point to the CALL instruction. */

532. setcframe_pc(cf, oldpc);

533. ERRNO_RESTORE

534. }

535. #endif

537. #if LJ_HASPROFILE

538. /* Profile dispatch. */

539. ‌void LJ_FASTCALL lj_dispatch_profile(lua_State *L, const BCIns *pc)

540. {

541. ERRNO_SAVE

542. GCfunc *fn = curr_func(L);

543. GCproto *pt = funcproto(fn);

544. void *cf = cframe_raw(L->cframe);

545. const BCIns *oldpc = cframe_pc(cf);

546. global_State *g;

547. setcframe_pc(cf, pc);

548. L->top = L->base + cur_topslot(pt, pc, cframe_multres_n(cf));

549. lj_profile_interpreter(L);

550. setcframe_pc(cf, oldpc);

551. g = G(L);

552. setgcref(g->cur_L, obj2gco(L));

553. setvmstate(g, INTERP);

554. ERRNO_RESTORE

555. }

556. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_dispatch.h - luajit-2.0-src

Data types defined

- GG_State

- GG_State

- HotCount

Macros defined

- ERRNO_RESTORE

- ERRNO_RESTORE

- ERRNO_RESTORE

- ERRNO_SAVE

- ERRNO_SAVE

- ERRNO_SAVE

- FFIGOTDEF

- FFIGOTDEF

- G2GG

- G2J

- GG_DISP2G

- GG_DISP2HOT

- GG_DISP2J

- GG_DISP2STATIC

- GG_G2DISP

- GG_LEN_DDISP

- GG_LEN_DISP

- GG_LEN_SDISP

- GG_NUM_ASMFF

- GG_OFS

- GOTDEF

- GOTENUM

- GOTENUM

- HOTCOUNT_CALL

- HOTCOUNT_LOOP

- HOTCOUNT_PCMASK

- HOTCOUNT_SIZE

- J2G

- J2GG

- JITGOTDEF

- JITGOTDEF

- L2GG

- L2J

- WIN32_LEAN_AND_MEAN

- _LJ_DISPATCH_H

- hotcount_get

- hotcount_set

Source code

1. /*

2. ** Instruction dispatch handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_DISPATCH_H

7. ‌#define _LJ_DISPATCH_H

9. #include "lj_obj.h"

10. #include "lj_bc.h"

11. #if LJ_HASJIT

12. #include "lj_jit.h"

13. #endif

15. #if LJ_TARGET_MIPS

16. /* Need our own global offset table for the dreaded MIPS calling conventions. */

17. #if LJ_HASJIT

18. ‌#define JITGOTDEF(_)        _(lj_trace_exit) _(lj_trace_hot)

19. #else

20. ‌#define JITGOTDEF(_)

21. #endif

22. #if LJ_HASFFI

23. ‌#define FFIGOTDEF(_) \

24. _(lj_meta_equal_cd) _(lj_ccallback_enter) _(lj_ccallback_leave)

25. #else

26. ‌#define FFIGOTDEF(_)

27. #endif

28. ‌#define GOTDEF(_) \

29. _(floor) _(ceil) _(trunc) _(log) _(log10) _(exp) _(sin) _(cos) _(tan) \

30. _(asin) _(acos) _(atan) _(sinh) _(cosh) _(tanh) _(frexp) _(modf) _(atan2) \

31. _(pow) _(fmod) _(ldexp) \

32. _(lj_dispatch_call) _(lj_dispatch_ins) _(lj_dispatch_stitch) \

33. _(lj_dispatch_profile) _(lj_err_throw) \

34. _(lj_ffh_coroutine_wrap_err) _(lj_func_closeuv) _(lj_func_newL_gc) \

35. _(lj_gc_barrieruv) _(lj_gc_step) _(lj_gc_step_fixtop) _(lj_meta_arith) \

36. _(lj_meta_call) _(lj_meta_cat) _(lj_meta_comp) _(lj_meta_equal) \

37. _(lj_meta_for) _(lj_meta_istype) _(lj_meta_len) _(lj_meta_tget) \

38. _(lj_meta_tset) _(lj_state_growstack) _(lj_strfmt_num) \

39. _(lj_str_new) _(lj_tab_dup) _(lj_tab_get) _(lj_tab_getinth) _(lj_tab_len) \

40. _(lj_tab_new) _(lj_tab_newkey) _(lj_tab_next) _(lj_tab_reasize) \

41. _(lj_tab_setinth) _(lj_buf_putstr_reverse) _(lj_buf_putstr_lower) \

42. _(lj_buf_putstr_upper) _(lj_buf_tostr) JITGOTDEF(_) FFIGOTDEF(_)

44. enum {

45. ‌#define GOTENUM(name) LJ_GOT_##name,

46. GOTDEF(GOTENUM)

47. ‌#undef GOTENUM

48. LJ_GOT__MAX

49. };

50. #endif

52. /* Type of hot counter. Must match the code in the assembler VM. */

53. /* 16 bits are sufficient. Only 0.0015% overhead with maximum slot penalty. */

54. ‌typedef uint16_t HotCount;

56. /* Number of hot counter hash table entries (must be a power of two). */

57. ‌#define HOTCOUNT_SIZE                64

58. ‌#define HOTCOUNT_PCMASK                ((HOTCOUNT_SIZE-1)*sizeof(HotCount))

60. /* Hotcount decrements. */

61. ‌#define HOTCOUNT_LOOP                2

62. ‌#define HOTCOUNT_CALL                1

64. /* This solves a circular dependency problem -- bump as needed. Sigh. */

65. ‌#define GG_NUM_ASMFF        57

67. ‌#define GG_LEN_DDISP        (BC__MAX + GG_NUM_ASMFF)

68. ‌#define GG_LEN_SDISP        BC_FUNCF

69. ‌#define GG_LEN_DISP        (GG_LEN_DDISP + GG_LEN_SDISP)

71. /* Global state, main thread and extra fields are allocated together. */

72. ‌typedef struct GG_State {

73. lua_State L;                                /* Main thread. */

74. global_State g;                        /* Global state. */

75. #if LJ_TARGET_MIPS

76. ASMFunction got[LJ_GOT__MAX];                /* Global offset table. */

77. #endif

78. #if LJ_HASJIT

79. jit_State J;                                /* JIT state. */

80. HotCount hotcount[HOTCOUNT_SIZE];        /* Hot counters. */

81. #endif

82. ASMFunction dispatch[GG_LEN_DISP];        /* Instruction dispatch tables. */

83. BCIns bcff[GG_NUM_ASMFF];                /* Bytecode for ASM fast functions. */

84. ‌} GG_State;

86. ‌#define GG_OFS(field)        ((int)offsetof(GG_State, field))

87. ‌#define G2GG(gl)        ((GG_State *)((char *)(gl) - GG_OFS(g)))

88. ‌#define J2GG(j)                ((GG_State *)((char *)(j) - GG_OFS(J)))

89. ‌#define L2GG(L)                (G2GG(G(L)))

90. ‌#define J2G(J)                (&J2GG(J)->g)

91. ‌#define G2J(gl)                (&G2GG(gl)->J)

92. ‌#define L2J(L)                (&L2GG(L)->J)

93. ‌#define GG_G2DISP        (GG_OFS(dispatch) - GG_OFS(g))

94. ‌#define GG_DISP2G        (GG_OFS(g) - GG_OFS(dispatch))

95. ‌#define GG_DISP2J        (GG_OFS(J) - GG_OFS(dispatch))

96. ‌#define GG_DISP2HOT        (GG_OFS(hotcount) - GG_OFS(dispatch))

97. ‌#define GG_DISP2STATIC        (GG_LEN_DDISP*(int)sizeof(ASMFunction))

99. ‌#define hotcount_get(gg, pc) \

100. (gg)->hotcount[(u32ptr(pc)>>2) & (HOTCOUNT_SIZE-1)]

101. ‌#define hotcount_set(gg, pc, val) \

102. (hotcount_get((gg), (pc)) = (HotCount)(val))

104. /* Dispatch table management. */

105. LJ_FUNC void lj_dispatch_init(GG_State *GG);

106. #if LJ_HASJIT

107. LJ_FUNC void lj_dispatch_init_hotcount(global_State *g);

108. #endif

109. LJ_FUNC void lj_dispatch_update(global_State *g);

111. /* Instruction dispatch callback for hooks or when recording. */

112. LJ_FUNCA void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc);

113. LJ_FUNCA ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns*pc);

114. #if LJ_HASJIT

115. LJ_FUNCA void LJ_FASTCALL lj_dispatch_stitch(jit_State *J, const BCIns *pc);

116. #endif

117. #if LJ_HASPROFILE

118. LJ_FUNCA void LJ_FASTCALL lj_dispatch_profile(lua_State *L, const BCIns *pc);

119. #endif

121. #if LJ_HASFFI && !defined(_BUILDVM_H)

122. /* Save/restore errno and GetLastError() around hooks, exits and recording. */

123. #include <errno.h>

124. #if LJ_TARGET_WINDOWS

125. ‌#define WIN32_LEAN_AND_MEAN

126. #include <windows.h>

127. ‌#define ERRNO_SAVE        int olderr = errno; DWORD oldwerr = GetLastError();

128. ‌#define ERRNO_RESTORE        errno = olderr; SetLastError(oldwerr);

129. #else

130. ‌#define ERRNO_SAVE        int olderr = errno;

131. ‌#define ERRNO_RESTORE        errno = olderr;

132. #endif

133. #else

134. ‌#define ERRNO_SAVE

135. ‌#define ERRNO_RESTORE

136. #endif

138. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_trace.c - luajit-2.0-src

Data types defined

- ExitDataCP

- ExitDataCP

Functions defined

- blacklist_pc

- lj_trace_err

- lj_trace_err_info

- lj_trace_exit

- lj_trace_flush

- lj_trace_flushall

- lj_trace_flushproto

- lj_trace_free

- lj_trace_freestate

- lj_trace_hot

- lj_trace_initstate

- lj_trace_ins

- lj_trace_reenableproto

- lj_trace_stitch

- penalty_pc

- perftools_addtrace

- trace_abort

- trace_downrec

- trace_exit_cp

- trace_exit_find

- trace_exit_regs

- trace_findfree

- trace_flushroot

- trace_hotside

- trace_pendpatch

- trace_save

- trace_start

- trace_state

- trace_stop

- trace_unpatch

Macros defined

- LUA_CORE

- TRACE_APPENDVEC

- lj_trace_c

Source code

1. /*

2. ** Trace management.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_trace_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT

13. #include "lj_gc.h"

14. #include "lj_err.h"

15. #include "lj_debug.h"

16. #include "lj_str.h"

17. #include "lj_frame.h"

18. #include "lj_state.h"

19. #include "lj_bc.h"

20. #include "lj_ir.h"

21. #include "lj_jit.h"

22. #include "lj_iropt.h"

23. #include "lj_mcode.h"

24. #include "lj_trace.h"

25. #include "lj_snap.h"

26. #include "lj_gdbjit.h"

27. #include "lj_record.h"

28. #include "lj_asm.h"

29. #include "lj_dispatch.h"

30. #include "lj_vm.h"

31. #include "lj_vmevent.h"

32. #include "lj_target.h"

34. /* -- Error handling ------------------------------------------------------ */

36. /* Synchronous abort with error message. */

37. ‌void lj_trace_err(jit_State *J, TraceError e)

38. {

39. setnilV(&J->errinfo);  /* No error info. */

40. setintV(J->L->top++, (int32_t)e);

41. lj_err_throw(J->L, LUA_ERRRUN);

42. }

44. /* Synchronous abort with error message and error info. */

45. ‌void lj_trace_err_info(jit_State *J, TraceError e)

46. {

47. setintV(J->L->top++, (int32_t)e);

48. lj_err_throw(J->L, LUA_ERRRUN);

49. }

51. /* -- Trace management ---------------------------------------------------- */

53. /* The current trace is first assembled in J->cur. The variable length

54. ** arrays point to shared, growable buffers (J->irbuf etc.). When trace

55. ** recording ends successfully, the current trace and its data structures

56. ** are copied to a new (compact) GCtrace object.

57. */

59. /* Find a free trace number. */

60. ‌static TraceNo trace_findfree(jit_State *J)

61. {

62. MSize osz, lim;

63. if (J->freetrace == 0)

64. J->freetrace = 1;

65. for (; J->freetrace < J->sizetrace; J->freetrace++)

66. if (traceref(J, J->freetrace) == NULL)

67. return J->freetrace++;

68. /* Need to grow trace array. */

69. lim = (MSize)J->param[JIT_P_maxtrace] + 1;

70. if (lim < 2) lim = 2; else if (lim > 65535) lim = 65535;

71. osz = J->sizetrace;

72. if (osz >= lim)

73. return 0;  /* Too many traces. */

74. lj_mem_growvec(J->L, J->trace, J->sizetrace, lim, GCRef);

75. for (; osz < J->sizetrace; osz++)

76. setgcrefnull(J->trace[osz]);

77. return J->freetrace;

78. }

80. ‌#define TRACE_APPENDVEC(field, szfield, tp) \

81. T->field = (tp *)p; \

82. memcpy(p, J->cur.field, J->cur.szfield*sizeof(tp)); \

83. p += J->cur.szfield*sizeof(tp);

85. #ifdef LUAJIT_USE_PERFTOOLS

86. /*

87. ** Create symbol table of JIT-compiled code. For use with Linux perf tools.

88. ** Example usage:

89. **   perf record -f -e cycles luajit test.lua

90. **   perf report -s symbol

91. **   rm perf.data /tmp/perf-*.map

92. */

93. #include <stdio.h>

94. #include <unistd.h>

96. ‌static void perftools_addtrace(GCtrace *T)

97. {

98. static FILE *fp;

99. GCproto *pt = &gcref(T->startpt)->pt;

100. const BCIns *startpc = mref(T->startpc, const BCIns);

101. const char *name = proto_chunknamestr(pt);

102. BCLine lineno;

103. if (name[0] == '@' || name[0] == '=')

104. name++;

105. else

106. name = "(string)";

107. lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc);

108. lineno = lj_debug_line(pt, proto_bcpos(pt, startpc));

109. if (!fp) {

110. char fname[40];

111. sprintf(fname, "/tmp/perf-%d.map", getpid());

112. if (!(fp = fopen(fname, "w"))) return;

113. setlinebuf(fp);

114. }

115. fprintf(fp, "%lx %x TRACE_%d::%s:%u\n",

116. (long)T->mcode, T->szmcode, T->traceno, name, lineno);

117. }

118. #endif

120. /* Save current trace by copying and compacting it. */

121. ‌static void trace_save(jit_State *J)

122. {

123. size_t sztr = ((sizeof(GCtrace)+7)&~7);

124. size_t szins = (J->cur.nins-J->cur.nk)*sizeof(IRIns);

125. size_t sz = sztr + szins +

126. J->cur.nsnap*sizeof(SnapShot) +

127. J->cur.nsnapmap*sizeof(SnapEntry);

128. GCtrace *T = lj_mem_newt(J->L, (MSize)sz, GCtrace);

129. char *p = (char *)T + sztr;

130. memcpy(T, &J->cur, sizeof(GCtrace));

131. setgcrefr(T->nextgc, J2G(J)->gc.root);

132. setgcrefp(J2G(J)->gc.root, T);

133. newwhite(J2G(J), T);

134. T->gct = ~LJ_TTRACE;

135. T->ir = (IRIns *)p - J->cur.nk;

136. memcpy(p, J->cur.ir+J->cur.nk, szins);

137. p += szins;

138. TRACE_APPENDVEC(snap, nsnap, SnapShot)

139. TRACE_APPENDVEC(snapmap, nsnapmap, SnapEntry)

140. J->cur.traceno = 0;

141. setgcrefp(J->trace[T->traceno], T);

142. lj_gc_barriertrace(J2G(J), T->traceno);

143. lj_gdbjit_addtrace(J, T);

144. #ifdef LUAJIT_USE_PERFTOOLS

145. perftools_addtrace(T);

146. #endif

147. }

149. ‌void LJ_FASTCALL lj_trace_free(global_State *g, GCtrace *T)

150. {

151. jit_State *J = G2J(g);

152. if (T->traceno) {

153. lj_gdbjit_deltrace(J, T);

154. if (T->traceno < J->freetrace)

155. J->freetrace = T->traceno;

156. setgcrefnull(J->trace[T->traceno]);

157. }

158. lj_mem_free(g, T,

159. ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) +

160. T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry));

161. }

163. /* Re-enable compiling a prototype by unpatching any modified bytecode. */

164. ‌void lj_trace_reenableproto(GCproto *pt)

165. {

166. if ((pt->flags & PROTO_ILOOP)) {

167. BCIns *bc = proto_bc(pt);

168. BCPos i, sizebc = pt->sizebc;;

169. pt->flags &= ~PROTO_ILOOP;

170. if (bc_op(bc[0]) == BC_IFUNCF)

171. setbc_op(&bc[0], BC_FUNCF);

172. for (i = 1; i < sizebc; i++) {

173. BCOp op = bc_op(bc[i]);

174. if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP)

175. setbc_op(&bc[i], (int)op+(int)BC_LOOP-(int)BC_ILOOP);

176. }

177. }

178. }

180. /* Unpatch the bytecode modified by a root trace. */

181. ‌static void trace_unpatch(jit_State *J, GCtrace *T)

182. {

183. BCOp op = bc_op(T->startins);

184. BCIns *pc = mref(T->startpc, BCIns);

185. UNUSED(J);

186. if (op == BC_JMP)

187. return;  /* No need to unpatch branches in parent traces (yet). */

188. switch (bc_op(*pc)) {

189. case BC_JFORL:

190. lua_assert(traceref(J, bc_d(*pc)) == T);

191. *pc = T->startins;

192. pc += bc_j(T->startins);

193. lua_assert(bc_op(*pc) == BC_JFORI);

194. setbc_op(pc, BC_FORI);

195. break;

196. case BC_JITERL:

197. case BC_JLOOP:

198. lua_assert(op == BC_ITERL || op == BC_LOOP || bc_isret(op));

199. *pc = T->startins;

200. break;

201. case BC_JMP:

202. lua_assert(op == BC_ITERL);

203. pc += bc_j(*pc)+2;

204. if (bc_op(*pc) == BC_JITERL) {

205. lua_assert(traceref(J, bc_d(*pc)) == T);

206. *pc = T->startins;

207. }

208. break;

209. case BC_JFUNCF:

210. lua_assert(op == BC_FUNCF);

211. *pc = T->startins;

212. break;

213. default:  /* Already unpatched. */

214. break;

215. }

216. }

218. /* Flush a root trace. */

219. ‌static void trace_flushroot(jit_State *J, GCtrace *T)

220. {

221. GCproto *pt = &gcref(T->startpt)->pt;

222. lua_assert(T->root == 0 && pt != NULL);

223. /* First unpatch any modified bytecode. */

224. trace_unpatch(J, T);

225. /* Unlink root trace from chain anchored in prototype. */

226. if (pt->trace == T->traceno) {  /* Trace is first in chain. Easy. */

227. pt->trace = T->nextroot;

228. } else if (pt->trace) {  /* Otherwise search in chain of root traces. */

229. GCtrace *T2 = traceref(J, pt->trace);

230. if (T2) {

231. for (; T2->nextroot; T2 = traceref(J, T2->nextroot))

232. if (T2->nextroot == T->traceno) {

233. T2->nextroot = T->nextroot;  /* Unlink from chain. */

234. break;

235. }

236. }

237. }

238. }

240. /* Flush a trace. Only root traces are considered. */

241. ‌void lj_trace_flush(jit_State *J, TraceNo traceno)

242. {

243. if (traceno > 0 && traceno < J->sizetrace) {

244. GCtrace *T = traceref(J, traceno);

245. if (T && T->root == 0)

246. trace_flushroot(J, T);

247. }

248. }

250. /* Flush all traces associated with a prototype. */

251. ‌void lj_trace_flushproto(global_State *g, GCproto *pt)

252. {

253. while (pt->trace != 0)

254. trace_flushroot(G2J(g), traceref(G2J(g), pt->trace));

255. }

257. /* Flush all traces. */

258. ‌int lj_trace_flushall(lua_State *L)

259. {

260. jit_State *J = L2J(L);

261. ptrdiff_t i;

262. if ((J2G(J)->hookmask & HOOK_GC))

263. return 1;

264. for (i = (ptrdiff_t)J->sizetrace-1; i > 0; i--) {

265. GCtrace *T = traceref(J, i);

266. if (T) {

267. if (T->root == 0)

268. trace_flushroot(J, T);

269. lj_gdbjit_deltrace(J, T);

270. T->traceno = 0;

271. setgcrefnull(J->trace[i]);

272. }

273. }

274. J->cur.traceno = 0;

275. J->freetrace = 0;

276. /* Clear penalty cache. */

277. memset(J->penalty, 0, sizeof(J->penalty));

278. /* Free the whole machine code and invalidate all exit stub groups. */

279. lj_mcode_free(J);

280. memset(J->exitstubgroup, 0, sizeof(J->exitstubgroup));

281. lj_vmevent_send(L, TRACE,

282. setstrV(L, L->top++, lj_str_newlit(L, "flush"));

283. );

284. return 0;

285. }

287. /* Initialize JIT compiler state. */

288. ‌void lj_trace_initstate(global_State *g)

289. {

290. jit_State *J = G2J(g);

291. TValue *tv;

292. /* Initialize SIMD constants. */

293. tv = LJ_KSIMD(J, LJ_KSIMD_ABS);

294. tv[0].u64 = U64x(7fffffff,ffffffff);

295. tv[1].u64 = U64x(7fffffff,ffffffff);

296. tv = LJ_KSIMD(J, LJ_KSIMD_NEG);

297. tv[0].u64 = U64x(80000000,00000000);

298. tv[1].u64 = U64x(80000000,00000000);

299. }

301. /* Free everything associated with the JIT compiler state. */

302. ‌void lj_trace_freestate(global_State *g)

303. {

304. jit_State *J = G2J(g);

305. #ifdef LUA_USE_ASSERT

306. {  /* This assumes all traces have already been freed. */

307. ptrdiff_t i;

308. for (i = 1; i < (ptrdiff_t)J->sizetrace; i++)

309. lua_assert(i == (ptrdiff_t)J->cur.traceno || traceref(J, i) == NULL);

310. }

311. #endif

312. lj_mcode_free(J);

313. lj_ir_k64_freeall(J);

314. lj_mem_freevec(g, J->snapmapbuf, J->sizesnapmap, SnapEntry);

315. lj_mem_freevec(g, J->snapbuf, J->sizesnap, SnapShot);

316. lj_mem_freevec(g, J->irbuf + J->irbotlim, J->irtoplim - J->irbotlim, IRIns);

317. lj_mem_freevec(g, J->trace, J->sizetrace, GCRef);

318. }

320. /* -- Penalties and blacklisting ------------------------------------------ */

322. /* Blacklist a bytecode instruction. */

323. ‌static void blacklist_pc(GCproto *pt, BCIns *pc)

324. {

325. setbc_op(pc, (int)bc_op(*pc)+(int)BC_ILOOP-(int)BC_LOOP);

326. pt->flags |= PROTO_ILOOP;

327. }

329. /* Penalize a bytecode instruction. */

330. ‌static void penalty_pc(jit_State *J, GCproto *pt, BCIns *pc, TraceError e)

331. {

332. uint32_t i, val = PENALTY_MIN;

333. for (i = 0; i < PENALTY_SLOTS; i++)

334. if (mref(J->penalty[i].pc, const BCIns) == pc) {  /* Cache slot found? */

335. /* First try to bump its hotcount several times. */

336. val = ((uint32_t)J->penalty[i].val << 1) +

337. LJ_PRNG_BITS(J, PENALTY_RNDBITS);

338. if (val > PENALTY_MAX) {

339. blacklist_pc(pt, pc);  /* Blacklist it, if that didn't help. */

340. return;

341. }

342. goto setpenalty;

343. }

344. /* Assign a new penalty cache slot. */

345. i = J->penaltyslot;

346. J->penaltyslot = (J->penaltyslot + 1) & (PENALTY_SLOTS-1);

347. setmref(J->penalty[i].pc, pc);

348. setpenalty:

349. J->penalty[i].val = (uint16_t)val;

350. J->penalty[i].reason = e;

351. hotcount_set(J2GG(J), pc+1, val);

352. }

354. /* -- Trace compiler state machine ---------------------------------------- */

356. /* Start tracing. */

357. ‌static void trace_start(jit_State *J)

358. {

359. lua_State *L;

360. TraceNo traceno;

362. if ((J->pt->flags & PROTO_NOJIT)) {  /* JIT disabled for this proto? */

363. if (J->parent == 0 && J->exitno == 0) {

364. /* Lazy bytecode patching to disable hotcount events. */

365. lua_assert(bc_op(*J->pc) == BC_FORL || bc_op(*J->pc) == BC_ITERL ||

366. bc_op(*J->pc) == BC_LOOP || bc_op(*J->pc) == BC_FUNCF);

367. setbc_op(J->pc, (int)bc_op(*J->pc)+(int)BC_ILOOP-(int)BC_LOOP);

368. J->pt->flags |= PROTO_ILOOP;

369. }

370. J->state = LJ_TRACE_IDLE;  /* Silently ignored. */

371. return;

372. }

374. /* Get a new trace number. */

375. traceno = trace_findfree(J);

376. if (LJ_UNLIKELY(traceno == 0)) {  /* No free trace? */

377. lua_assert((J2G(J)->hookmask & HOOK_GC) == 0);

378. lj_trace_flushall(J->L);

379. J->state = LJ_TRACE_IDLE;  /* Silently ignored. */

380. return;

381. }

382. setgcrefp(J->trace[traceno], &J->cur);

384. /* Setup enough of the current trace to be able to send the vmevent. */

385. memset(&J->cur, 0, sizeof(GCtrace));

386. J->cur.traceno = traceno;

387. J->cur.nins = J->cur.nk = REF_BASE;

388. J->cur.ir = J->irbuf;

389. J->cur.snap = J->snapbuf;

390. J->cur.snapmap = J->snapmapbuf;

391. J->mergesnap = 0;

392. J->needsnap = 0;

393. J->bcskip = 0;

394. J->guardemit.irt = 0;

395. J->postproc = LJ_POST_NONE;

396. lj_resetsplit(J);

397. setgcref(J->cur.startpt, obj2gco(J->pt));

399. L = J->L;

400. lj_vmevent_send(L, TRACE,

401. setstrV(L, L->top++, lj_str_newlit(L, "start"));

402. setintV(L->top++, traceno);

403. setfuncV(L, L->top++, J->fn);

404. setintV(L->top++, proto_bcpos(J->pt, J->pc));

405. if (J->parent) {

406. setintV(L->top++, J->parent);

407. setintV(L->top++, J->exitno);

408. }

409. );

410. lj_record_setup(J);

411. }

413. /* Stop tracing. */

414. ‌static void trace_stop(jit_State *J)

415. {

416. BCIns *pc = mref(J->cur.startpc, BCIns);

417. BCOp op = bc_op(J->cur.startins);

418. GCproto *pt = &gcref(J->cur.startpt)->pt;

419. TraceNo traceno = J->cur.traceno;

420. lua_State *L;

422. switch (op) {

423. case BC_FORL:

424. setbc_op(pc+bc_j(J->cur.startins), BC_JFORI);  /* Patch FORI, too. */

425. /* fallthrough */

426. case BC_LOOP:

427. case BC_ITERL:

428. case BC_FUNCF:

429. /* Patch bytecode of starting instruction in root trace. */

430. setbc_op(pc, (int)op+(int)BC_JLOOP-(int)BC_LOOP);

431. setbc_d(pc, traceno);

432. addroot:

433. /* Add to root trace chain in prototype. */

434. J->cur.nextroot = pt->trace;

435. pt->trace = (TraceNo1)traceno;

436. break;

437. case BC_RET:

438. case BC_RET0:

439. case BC_RET1:

440. *pc = BCINS_AD(BC_JLOOP, J->cur.snap[0].nslots, traceno);

441. goto addroot;

442. case BC_JMP:

443. /* Patch exit branch in parent to side trace entry. */

444. lua_assert(J->parent != 0 && J->cur.root != 0);

445. lj_asm_patchexit(J, traceref(J, J->parent), J->exitno, J->cur.mcode);

446. /* Avoid compiling a side trace twice (stack resizing uses parent exit). */

447. traceref(J, J->parent)->snap[J->exitno].count = SNAPCOUNT_DONE;

448. /* Add to side trace chain in root trace. */

449. {

450. GCtrace *root = traceref(J, J->cur.root);

451. root->nchild++;

452. J->cur.nextside = root->nextside;

453. root->nextside = (TraceNo1)traceno;

454. }

455. break;

456. case BC_CALLM:

457. case BC_CALL:

458. case BC_ITERC:

459. /* Trace stitching: patch link of previous trace. */

460. traceref(J, J->exitno)->link = traceno;

461. break;

462. default:

463. lua_assert(0);

464. break;

465. }

467. /* Commit new mcode only after all patching is done. */

468. lj_mcode_commit(J, J->cur.mcode);

469. J->postproc = LJ_POST_NONE;

470. trace_save(J);

472. L = J->L;

473. lj_vmevent_send(L, TRACE,

474. setstrV(L, L->top++, lj_str_newlit(L, "stop"));

475. setintV(L->top++, traceno);

476. setfuncV(L, L->top++, J->fn);

477. );

478. }

480. /* Start a new root trace for down-recursion. */

481. ‌static int trace_downrec(jit_State *J)

482. {

483. /* Restart recording at the return instruction. */

484. lua_assert(J->pt != NULL);

485. lua_assert(bc_isret(bc_op(*J->pc)));

486. if (bc_op(*J->pc) == BC_RETM)

487. return 0;  /* NYI: down-recursion with RETM. */

488. J->parent = 0;

489. J->exitno = 0;

490. J->state = LJ_TRACE_RECORD;

491. trace_start(J);

492. return 1;

493. }

495. /* Abort tracing. */

496. ‌static int trace_abort(jit_State *J)

497. {

498. lua_State *L = J->L;

499. TraceError e = LJ_TRERR_RECERR;

500. TraceNo traceno;

502. J->postproc = LJ_POST_NONE;

503. lj_mcode_abort(J);

504. if (tvisnumber(L->top-1))

505. e = (TraceError)numberVint(L->top-1);

506. if (e == LJ_TRERR_MCODELM) {

507. L->top--;  /* Remove error object */

508. J->state = LJ_TRACE_ASM;

509. return 1;  /* Retry ASM with new MCode area. */

510. }

511. /* Penalize or blacklist starting bytecode instruction. */

512. if (J->parent == 0 && !bc_isret(bc_op(J->cur.startins))) {

513. if (J->exitno == 0)

514. penalty_pc(J, &gcref(J->cur.startpt)->pt, mref(J->cur.startpc, BCIns), e);

515. else

516. traceref(J, J->exitno)->link = J->exitno;  /* Self-link is blacklisted. */

517. }

519. /* Is there anything to abort? */

520. traceno = J->cur.traceno;

521. if (traceno) {

522. ptrdiff_t errobj = savestack(L, L->top-1);  /* Stack may be resized. */

523. J->cur.link = 0;

524. J->cur.linktype = LJ_TRLINK_NONE;

525. lj_vmevent_send(L, TRACE,

526. TValue *frame;

527. const BCIns *pc;

528. GCfunc *fn;

529. setstrV(L, L->top++, lj_str_newlit(L, "abort"));

530. setintV(L->top++, traceno);

531. /* Find original Lua function call to generate a better error message. */

532. frame = J->L->base-1;

533. pc = J->pc;

534. while (!isluafunc(frame_func(frame))) {

535. pc = (frame_iscont(frame) ? frame_contpc(frame) : frame_pc(frame)) - 1;

536. frame = frame_prev(frame);

537. }

538. fn = frame_func(frame);

539. setfuncV(L, L->top++, fn);

540. setintV(L->top++, proto_bcpos(funcproto(fn), pc));

541. copyTV(L, L->top++, restorestack(L, errobj));

542. copyTV(L, L->top++, &J->errinfo);

543. );

544. /* Drop aborted trace after the vmevent (which may still access it). */

545. setgcrefnull(J->trace[traceno]);

546. if (traceno < J->freetrace)

547. J->freetrace = traceno;

548. J->cur.traceno = 0;

549. }

550. L->top--;  /* Remove error object */

551. if (e == LJ_TRERR_DOWNREC)

552. return trace_downrec(J);

553. else if (e == LJ_TRERR_MCODEAL)

554. lj_trace_flushall(L);

555. return 0;

556. }

558. /* Perform pending re-patch of a bytecode instruction. */

559. ‌static LJ_AINLINE void trace_pendpatch(jit_State *J, int force)

560. {

561. if (LJ_UNLIKELY(J->patchpc)) {

562. if (force || J->bcskip == 0) {

563. *J->patchpc = J->patchins;

564. J->patchpc = NULL;

565. } else {

566. J->bcskip = 0;

567. }

568. }

569. }

571. /* State machine for the trace compiler. Protected callback. */

572. ‌static TValue *trace_state(lua_State *L, lua_CFunction dummy, void *ud)

573. {

574. jit_State *J = (jit_State *)ud;

575. UNUSED(dummy);

576. do {

577. retry:

578. switch (J->state) {

579. case LJ_TRACE_START:

580. J->state = LJ_TRACE_RECORD;  /* trace_start() may change state. */

581. trace_start(J);

582. lj_dispatch_update(J2G(J));

583. break;

585. case LJ_TRACE_RECORD:

586. trace_pendpatch(J, 0);

587. setvmstate(J2G(J), RECORD);

588. lj_vmevent_send_(L, RECORD,

589. /* Save/restore tmptv state for trace recorder. */

590. TValue savetv = J2G(J)->tmptv;

591. TValue savetv2 = J2G(J)->tmptv2;

592. setintV(L->top++, J->cur.traceno);

593. setfuncV(L, L->top++, J->fn);

594. setintV(L->top++, J->pt ? (int32_t)proto_bcpos(J->pt, J->pc) : -1);

595. setintV(L->top++, J->framedepth);

596. ,

597. J2G(J)->tmptv = savetv;

598. J2G(J)->tmptv2 = savetv2;

599. );

600. lj_record_ins(J);

601. break;

603. case LJ_TRACE_END:

604. trace_pendpatch(J, 1);

605. J->loopref = 0;

606. if ((J->flags & JIT_F_OPT_LOOP) &&

607. J->cur.link == J->cur.traceno && J->framedepth + J->retdepth == 0) {

608. setvmstate(J2G(J), OPT);

609. lj_opt_dce(J);

610. if (lj_opt_loop(J)) {  /* Loop optimization failed? */

611. J->cur.link = 0;

612. J->cur.linktype = LJ_TRLINK_NONE;

613. J->loopref = J->cur.nins;

614. J->state = LJ_TRACE_RECORD;  /* Try to continue recording. */

615. break;

616. }

617. J->loopref = J->chain[IR_LOOP];  /* Needed by assembler. */

618. }

619. lj_opt_split(J);

620. lj_opt_sink(J);

621. if (!J->loopref) J->cur.snap[J->cur.nsnap-1].count = SNAPCOUNT_DONE;

622. J->state = LJ_TRACE_ASM;

623. break;

625. case LJ_TRACE_ASM:

626. setvmstate(J2G(J), ASM);

627. lj_asm_trace(J, &J->cur);

628. trace_stop(J);

629. setvmstate(J2G(J), INTERP);

630. J->state = LJ_TRACE_IDLE;

631. lj_dispatch_update(J2G(J));

632. return NULL;

634. default:  /* Trace aborted asynchronously. */

635. setintV(L->top++, (int32_t)LJ_TRERR_RECERR);

636. /* fallthrough */

637. case LJ_TRACE_ERR:

638. trace_pendpatch(J, 1);

639. if (trace_abort(J))

640. goto retry;

641. setvmstate(J2G(J), INTERP);

642. J->state = LJ_TRACE_IDLE;

643. lj_dispatch_update(J2G(J));

644. return NULL;

645. }

646. } while (J->state > LJ_TRACE_RECORD);

647. return NULL;

648. }

650. /* -- Event handling ------------------------------------------------------ */

652. /* A bytecode instruction is about to be executed. Record it. */

653. ‌void lj_trace_ins(jit_State *J, const BCIns *pc)

654. {

655. /* Note: J->L must already be set. pc is the true bytecode PC here. */

656. J->pc = pc;

657. J->fn = curr_func(J->L);

658. J->pt = isluafunc(J->fn) ? funcproto(J->fn) : NULL;

659. while (lj_vm_cpcall(J->L, NULL, (void *)J, trace_state) != 0)

660. J->state = LJ_TRACE_ERR;

661. }

663. /* A hotcount triggered. Start recording a root trace. */

664. ‌void LJ_FASTCALL lj_trace_hot(jit_State *J, const BCIns *pc)

665. {

666. /* Note: pc is the interpreter bytecode PC here. It's offset by 1. */

667. ERRNO_SAVE

668. /* Reset hotcount. */

669. hotcount_set(J2GG(J), pc, J->param[JIT_P_hotloop]*HOTCOUNT_LOOP);

670. /* Only start a new trace if not recording or inside __gc call or vmevent. */

671. if (J->state == LJ_TRACE_IDLE &&

672. !(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT))) {

673. J->parent = 0;  /* Root trace. */

674. J->exitno = 0;

675. J->state = LJ_TRACE_START;

676. lj_trace_ins(J, pc-1);

677. }

678. ERRNO_RESTORE

679. }

681. /* Check for a hot side exit. If yes, start recording a side trace. */

682. ‌static void trace_hotside(jit_State *J, const BCIns *pc)

683. {

684. SnapShot *snap = &traceref(J, J->parent)->snap[J->exitno];

685. if (!(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT)) &&

686. isluafunc(curr_func(J->L)) &&

687. snap->count != SNAPCOUNT_DONE &&

688. ++snap->count >= J->param[JIT_P_hotexit]) {

689. lua_assert(J->state == LJ_TRACE_IDLE);

690. /* J->parent is non-zero for a side trace. */

691. J->state = LJ_TRACE_START;

692. lj_trace_ins(J, pc);

693. }

694. }

696. /* Stitch a new trace to the previous trace. */

697. ‌void LJ_FASTCALL lj_trace_stitch(jit_State *J, const BCIns *pc)

698. {

699. /* Only start a new trace if not recording or inside __gc call or vmevent. */

700. if (J->state == LJ_TRACE_IDLE &&

701. !(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT))) {

702. J->parent = 0;  /* Have to treat it like a root trace. */

703. /* J->exitno is set to the invoking trace. */

704. J->state = LJ_TRACE_START;

705. lj_trace_ins(J, pc);

706. }

707. }

710. /* Tiny struct to pass data to protected call. */

711. ‌typedef struct ExitDataCP {

712. jit_State *J;

713. void *exptr;                /* Pointer to exit state. */

714. const BCIns *pc;        /* Restart interpreter at this PC. */

715. ‌} ExitDataCP;

717. /* Need to protect lj_snap_restore because it may throw. */

718. ‌static TValue *trace_exit_cp(lua_State *L, lua_CFunction dummy, void *ud)

719. {

720. ExitDataCP *exd = (ExitDataCP *)ud;

721. cframe_errfunc(L->cframe) = -1;  /* Inherit error function. */

722. exd->pc = lj_snap_restore(exd->J, exd->exptr);

723. UNUSED(dummy);

724. return NULL;

725. }

727. #ifndef LUAJIT_DISABLE_VMEVENT

728. /* Push all registers from exit state. */

729. ‌static void trace_exit_regs(lua_State *L, ExitState *ex)

730. {

731. int32_t i;

732. setintV(L->top++, RID_NUM_GPR);

733. setintV(L->top++, RID_NUM_FPR);

734. for (i = 0; i < RID_NUM_GPR; i++) {

735. if (sizeof(ex->gpr[i]) == sizeof(int32_t))

736. setintV(L->top++, (int32_t)ex->gpr[i]);

737. else

738. setnumV(L->top++, (lua_Number)ex->gpr[i]);

739. }

740. #if !LJ_SOFTFP

741. for (i = 0; i < RID_NUM_FPR; i++) {

742. setnumV(L->top, ex->fpr[i]);

743. if (LJ_UNLIKELY(tvisnan(L->top)))

744. setnanV(L->top);

745. L->top++;

746. }

747. #endif

748. }

749. #endif

751. #ifdef EXITSTATE_PCREG

752. /* Determine trace number from pc of exit instruction. */

753. ‌static TraceNo trace_exit_find(jit_State *J, MCode *pc)

754. {

755. TraceNo traceno;

756. for (traceno = 1; traceno < J->sizetrace; traceno++) {

757. GCtrace *T = traceref(J, traceno);

758. if (T && pc >= T->mcode && pc < (MCode *)((char *)T->mcode + T->szmcode))

759. return traceno;

760. }

761. lua_assert(0);

762. return 0;

763. }

764. #endif

766. /* A trace exited. Restore interpreter state. */

767. ‌int LJ_FASTCALL lj_trace_exit(jit_State *J, void *exptr)

768. {

769. ERRNO_SAVE

770. lua_State *L = J->L;

771. ExitState *ex = (ExitState *)exptr;

772. ExitDataCP exd;

773. int errcode;

774. const BCIns *pc;

775. void *cf;

776. GCtrace *T;

777. #ifdef EXITSTATE_PCREG

778. J->parent = trace_exit_find(J, (MCode *)(intptr_t)ex->gpr[EXITSTATE_PCREG]);

779. #endif

780. T = traceref(J, J->parent); UNUSED(T);

781. #ifdef EXITSTATE_CHECKEXIT

782. if (J->exitno == T->nsnap) {  /* Treat stack check like a parent exit. */

783. lua_assert(T->root != 0);

784. J->exitno = T->ir[REF_BASE].op2;

785. J->parent = T->ir[REF_BASE].op1;

786. T = traceref(J, J->parent);

787. }

788. #endif

789. lua_assert(T != NULL && J->exitno < T->nsnap);

790. exd.J = J;

791. exd.exptr = exptr;

792. errcode = lj_vm_cpcall(L, NULL, &exd, trace_exit_cp);

793. if (errcode)

794. return -errcode;  /* Return negated error code. */

796. if (!(LJ_HASPROFILE && (G(L)->hookmask & HOOK_PROFILE)))

797. lj_vmevent_send(L, TEXIT,

798. lj_state_checkstack(L, 4+RID_NUM_GPR+RID_NUM_FPR+LUA_MINSTACK);

799. setintV(L->top++, J->parent);

800. setintV(L->top++, J->exitno);

801. trace_exit_regs(L, ex);

802. );

804. pc = exd.pc;

805. cf = cframe_raw(L->cframe);

806. setcframe_pc(cf, pc);

807. if (LJ_HASPROFILE && (G(L)->hookmask & HOOK_PROFILE)) {

808. /* Just exit to interpreter. */

809. } else if (G(L)->gc.state == GCSatomic || G(L)->gc.state == GCSfinalize) {

810. if (!(G(L)->hookmask & HOOK_GC))

811. lj_gc_step(L);  /* Exited because of GC: drive GC forward. */

812. } else {

813. trace_hotside(J, pc);

814. }

815. if (bc_op(*pc) == BC_JLOOP) {

816. BCIns *retpc = &traceref(J, bc_d(*pc))->startins;

817. if (bc_isret(bc_op(*retpc))) {

818. if (J->state == LJ_TRACE_RECORD) {

819. J->patchins = *pc;

820. J->patchpc = (BCIns *)pc;

821. *J->patchpc = *retpc;

822. J->bcskip = 1;

823. } else {

824. pc = retpc;

825. setcframe_pc(cf, pc);

826. }

827. }

828. }

829. /* Return MULTRES or 0. */

830. ERRNO_RESTORE

831. switch (bc_op(*pc)) {

832. case BC_CALLM: case BC_CALLMT:

833. return (int)((BCReg)(L->top - L->base) - bc_a(*pc) - bc_c(*pc) + LJ_FR2);

834. case BC_RETM:

835. return (int)((BCReg)(L->top - L->base) + 1 - bc_a(*pc) - bc_d(*pc));

836. case BC_TSETM:

837. return (int)((BCReg)(L->top - L->base) + 1 - bc_a(*pc));

838. default:

839. if (bc_op(*pc) >= BC_FUNCF)

840. return (int)((BCReg)(L->top - L->base) + 1);

841. return 0;

842. }

843. }

845. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_jit.h - luajit-2.0-src

Global variables defined

- offsetof

Data types defined

- BPropEntry

- BPropEntry

- ExitNo

- FoldState

- FoldState

- GCtrace

- GCtrace

- HotPenalty

- HotPenalty

- MCode

- MCode

- PostProc

- ScEvEntry

- ScEvEntry

- SnapEntry

- SnapNo

- SnapShot

- SnapShot

- TraceLink

- TraceNo

- TraceNo1

- TraceState

- jit_State

Functions defined

- static LJ_AINLINE uint32_t LJ_PRNG_BITS(jit_State *J, int bits)

- snap_nextofs

Macros defined

- BPROP_SLOTS

- JIT_F_ARMV6

- JIT_F_ARMV6T2

- JIT_F_ARMV6T2_

- JIT_F_ARMV6_

- JIT_F_ARMV7

- JIT_F_CPUSTRING

- JIT_F_CPUSTRING

- JIT_F_CPUSTRING

- JIT_F_CPUSTRING

- JIT_F_CPUSTRING

- JIT_F_CPU_FIRST

- JIT_F_CPU_FIRST

- JIT_F_CPU_FIRST

- JIT_F_CPU_FIRST

- JIT_F_CPU_FIRST

- JIT_F_LEA_AGU

- JIT_F_MIPS32R2

- JIT_F_ON

- JIT_F_OPTSTRING

- JIT_F_OPT_0

- JIT_F_OPT_1

- JIT_F_OPT_2

- JIT_F_OPT_3

- JIT_F_OPT_ABC

- JIT_F_OPT_CSE

- JIT_F_OPT_DCE

- JIT_F_OPT_DEFAULT

- JIT_F_OPT_DSE

- JIT_F_OPT_FIRST

- JIT_F_OPT_FOLD

- JIT_F_OPT_FUSE

- JIT_F_OPT_FWD

- JIT_F_OPT_LOOP

- JIT_F_OPT_MASK

- JIT_F_OPT_NARROW

- JIT_F_OPT_SINK

- JIT_F_PREFER_IMUL

- JIT_F_ROUND

- JIT_F_SQRT

- JIT_F_SSE2

- JIT_F_SSE3

- JIT_F_SSE4_1

- JIT_F_VFP

- JIT_F_VFPV2

- JIT_F_VFPV3

- JIT_PARAMDEF

- JIT_PARAMENUM

- JIT_PARAMENUM

- JIT_PARAMSTR

- JIT_P_STRING

- JIT_P_sizemcode_DEFAULT

- JIT_P_sizemcode_DEFAULT

- LJ_KSIMD

- PENALTY_MAX

- PENALTY_MIN

- PENALTY_RNDBITS

- PENALTY_SLOTS

- SNAP

- SNAPCOUNT_DONE

- SNAP_CONT

- SNAP_FRAME

- SNAP_MKFTSZ

- SNAP_MKPC

- SNAP_NORESTORE

- SNAP_SOFTFPNUM

- SNAP_TR

- _LJ_JIT_H

- gco2trace

- lj_needsplit

- lj_needsplit

- lj_resetsplit

- lj_resetsplit

- snap_isframe

- snap_pc

- snap_ref

- snap_setref

- snap_slot

- traceref

Source code

1. /*

2. ** Common definitions for the JIT compiler.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_JIT_H

7. ‌#define _LJ_JIT_H

9. #include "lj_obj.h"

10. #include "lj_ir.h"

12. /* JIT engine flags. */

13. ‌#define JIT_F_ON                0x00000001

15. /* CPU-specific JIT engine flags. */

16. #if LJ_TARGET_X86ORX64

17. ‌#define JIT_F_SSE2                0x00000010

18. ‌#define JIT_F_SSE3                0x00000020

19. ‌#define JIT_F_SSE4_1                0x00000040

20. ‌#define JIT_F_PREFER_IMUL        0x00000080

21. ‌#define JIT_F_LEA_AGU                0x00000100

23. /* Names for the CPU-specific flags. Must match the order above. */

24. ‌#define JIT_F_CPU_FIRST                JIT_F_SSE2

25. ‌#define JIT_F_CPUSTRING                "\4SSE2\4SSE3\6SSE4.1\3AMD\4ATOM"

26. #elif LJ_TARGET_ARM

27. ‌#define JIT_F_ARMV6_                0x00000010

28. ‌#define JIT_F_ARMV6T2_                0x00000020

29. ‌#define JIT_F_ARMV7                0x00000040

30. ‌#define JIT_F_VFPV2                0x00000080

31. ‌#define JIT_F_VFPV3                0x00000100

33. ‌#define JIT_F_ARMV6                (JIT_F_ARMV6_|JIT_F_ARMV6T2_|JIT_F_ARMV7)

34. ‌#define JIT_F_ARMV6T2                (JIT_F_ARMV6T2_|JIT_F_ARMV7)

35. ‌#define JIT_F_VFP                (JIT_F_VFPV2|JIT_F_VFPV3)

37. /* Names for the CPU-specific flags. Must match the order above. */

38. ‌#define JIT_F_CPU_FIRST                JIT_F_ARMV6_

39. ‌#define JIT_F_CPUSTRING                "\5ARMv6\7ARMv6T2\5ARMv7\5VFPv2\5VFPv3"

40. #elif LJ_TARGET_PPC

41. ‌#define JIT_F_SQRT                0x00000010

42. ‌#define JIT_F_ROUND                0x00000020

44. /* Names for the CPU-specific flags. Must match the order above. */

45. ‌#define JIT_F_CPU_FIRST                JIT_F_SQRT

46. ‌#define JIT_F_CPUSTRING                "\4SQRT\5ROUND"

47. #elif LJ_TARGET_MIPS

48. ‌#define JIT_F_MIPS32R2                0x00000010

50. /* Names for the CPU-specific flags. Must match the order above. */

51. ‌#define JIT_F_CPU_FIRST                JIT_F_MIPS32R2

52. ‌#define JIT_F_CPUSTRING                "\010MIPS32R2"

53. #else

54. ‌#define JIT_F_CPU_FIRST                0

55. ‌#define JIT_F_CPUSTRING                ""

56. #endif

58. /* Optimization flags. */

59. ‌#define JIT_F_OPT_MASK                0x0fff0000

61. ‌#define JIT_F_OPT_FOLD                0x00010000

62. ‌#define JIT_F_OPT_CSE                0x00020000

63. ‌#define JIT_F_OPT_DCE                0x00040000

64. ‌#define JIT_F_OPT_FWD                0x00080000

65. ‌#define JIT_F_OPT_DSE                0x00100000

66. ‌#define JIT_F_OPT_NARROW        0x00200000

67. ‌#define JIT_F_OPT_LOOP                0x00400000

68. ‌#define JIT_F_OPT_ABC                0x00800000

69. ‌#define JIT_F_OPT_SINK                0x01000000

70. ‌#define JIT_F_OPT_FUSE                0x02000000

72. /* Optimizations names for -O. Must match the order above. */

73. ‌#define JIT_F_OPT_FIRST                JIT_F_OPT_FOLD

74. ‌#define JIT_F_OPTSTRING        \

75. "\4fold\3cse\3dce\3fwd\3dse\6narrow\4loop\3abc\4sink\4fuse"

77. /* Optimization levels set a fixed combination of flags. */

78. ‌#define JIT_F_OPT_0        0

79. ‌#define JIT_F_OPT_1        (JIT_F_OPT_FOLD|JIT_F_OPT_CSE|JIT_F_OPT_DCE)

80. ‌#define JIT_F_OPT_2        (JIT_F_OPT_1|JIT_F_OPT_NARROW|JIT_F_OPT_LOOP)

81. ‌#define JIT_F_OPT_3        (JIT_F_OPT_2|\

82. JIT_F_OPT_FWD|JIT_F_OPT_DSE|JIT_F_OPT_ABC|JIT_F_OPT_SINK|JIT_F_OPT_FUSE)

83. ‌#define JIT_F_OPT_DEFAULT        JIT_F_OPT_3

85. #if LJ_TARGET_WINDOWS || LJ_64

86. /* See: http://blogs.msdn.com/oldnewthing/archive/2003/10/08/55239.aspx */

87. ‌#define JIT_P_sizemcode_DEFAULT                64

88. #else

89. /* Could go as low as 4K, but the mmap() overhead would be rather high. */

90. ‌#define JIT_P_sizemcode_DEFAULT                32

91. #endif

93. /* Optimization parameters and their defaults. Length is a char in octal! */

94. ‌#define JIT_PARAMDEF(_) \

95. _(\010, maxtrace,        1000)        /* Max. # of traces in cache. */ \

96. _(\011, maxrecord,        4000)        /* Max. # of recorded IR instructions. */ \

97. _(\012, maxirconst,        500)        /* Max. # of IR constants of a trace. */ \

98. _(\007, maxside,        100)        /* Max. # of side traces of a root trace. */ \

99. _(\007, maxsnap,        500)        /* Max. # of snapshots for a trace. */ \

100. _(\011, minstitch,        0)        /* Min. # of IR ins for a stitched trace. */ \

101. \

102. _(\007, hotloop,        56)        /* # of iter. to detect a hot loop/call. */ \

103. _(\007, hotexit,        10)        /* # of taken exits to start a side trace. */ \

104. _(\007, tryside,        4)        /* # of attempts to compile a side trace. */ \

105. \

106. _(\012, instunroll,        4)        /* Max. unroll for instable loops. */ \

107. _(\012, loopunroll,        15)        /* Max. unroll for loop ops in side traces. */ \

108. _(\012, callunroll,        3)        /* Max. unroll for recursive calls. */ \

109. _(\011, recunroll,        2)        /* Min. unroll for true recursion. */ \

110. \

111. /* Size of each machine code area (in KBytes). */ \

112. _(\011, sizemcode,        JIT_P_sizemcode_DEFAULT) \

113. /* Max. total size of all machine code areas (in KBytes). */ \

114. _(\010, maxmcode,        512) \

115. /* End of list. */

117. enum {

118. ‌#define JIT_PARAMENUM(len, name, value)        JIT_P_##name,

119. JIT_PARAMDEF(JIT_PARAMENUM)

120. ‌#undef JIT_PARAMENUM

121. JIT_P__MAX

122. };

124. ‌#define JIT_PARAMSTR(len, name, value)        #len #name

125. ‌#define JIT_P_STRING        JIT_PARAMDEF(JIT_PARAMSTR)

127. /* Trace compiler state. */

128. typedef enum {

129. LJ_TRACE_IDLE,        /* Trace compiler idle. */

130. LJ_TRACE_ACTIVE = 0x10,

131. LJ_TRACE_RECORD,        /* Bytecode recording active. */

132. LJ_TRACE_START,        /* New trace started. */

133. LJ_TRACE_END,                /* End of trace. */

134. LJ_TRACE_ASM,                /* Assemble trace. */

135. LJ_TRACE_ERR                /* Trace aborted with error. */

136. ‌} TraceState;

138. /* Post-processing action. */

139. typedef enum {

140. LJ_POST_NONE,                /* No action. */

141. LJ_POST_FIXCOMP,        /* Fixup comparison and emit pending guard. */

142. LJ_POST_FIXGUARD,        /* Fixup and emit pending guard. */

143. LJ_POST_FIXGUARDSNAP,        /* Fixup and emit pending guard and snapshot. */

144. LJ_POST_FIXBOOL,        /* Fixup boolean result. */

145. LJ_POST_FIXCONST,        /* Fixup constant results. */

146. LJ_POST_FFRETRY        /* Suppress recording of retried fast functions. */

147. ‌} PostProc;

149. /* Machine code type. */

150. #if LJ_TARGET_X86ORX64

151. ‌typedef uint8_t MCode;

152. #else

153. ‌typedef uint32_t MCode;

154. #endif

156. /* Stack snapshot header. */

157. ‌typedef struct SnapShot {

158. uint16_t mapofs;        /* Offset into snapshot map. */

159. IRRef1 ref;                /* First IR ref for this snapshot. */

160. uint8_t nslots;        /* Number of valid slots. */

161. uint8_t topslot;        /* Maximum frame extent. */

162. uint8_t nent;                /* Number of compressed entries. */

163. uint8_t count;        /* Count of taken exits for this snapshot. */

164. ‌} SnapShot;

166. ‌#define SNAPCOUNT_DONE        255        /* Already compiled and linked a side trace. */

168. /* Compressed snapshot entry. */

169. ‌typedef uint32_t SnapEntry;

171. ‌#define SNAP_FRAME                0x010000        /* Frame slot. */

172. ‌#define SNAP_CONT                0x020000        /* Continuation slot. */

173. ‌#define SNAP_NORESTORE                0x040000        /* No need to restore slot. */

174. ‌#define SNAP_SOFTFPNUM                0x080000        /* Soft-float number. */

175. LJ_STATIC_ASSERT(SNAP_FRAME == TREF_FRAME);

176. LJ_STATIC_ASSERT(SNAP_CONT == TREF_CONT);

178. ‌#define SNAP(slot, flags, ref)        (((SnapEntry)(slot) << 24) + (flags) + (ref))

179. ‌#define SNAP_TR(slot, tr) \

180. (((SnapEntry)(slot) << 24) + ((tr) & (TREF_CONT|TREF_FRAME|TREF_REFMASK)))

181. ‌#define SNAP_MKPC(pc)                ((SnapEntry)u32ptr(pc))

182. ‌#define SNAP_MKFTSZ(ftsz)        ((SnapEntry)(ftsz))

183. ‌#define snap_ref(sn)                ((sn) & 0xffff)

184. ‌#define snap_slot(sn)                ((BCReg)((sn) >> 24))

185. ‌#define snap_isframe(sn)        ((sn) & SNAP_FRAME)

186. ‌#define snap_pc(sn)                ((const BCIns *)(uintptr_t)(sn))

187. ‌#define snap_setref(sn, ref)        (((sn) & (0xffff0000&~SNAP_NORESTORE)) | (ref))

189. /* Snapshot and exit numbers. */

190. ‌typedef uint32_t SnapNo;

191. ‌typedef uint32_t ExitNo;

193. /* Trace number. */

194. ‌typedef uint32_t TraceNo;        /* Used to pass around trace numbers. */

195. ‌typedef uint16_t TraceNo1;        /* Stored trace number. */

197. /* Type of link. ORDER LJ_TRLINK */

198. typedef enum {

199. LJ_TRLINK_NONE,                /* Incomplete trace. No link, yet. */

200. LJ_TRLINK_ROOT,                /* Link to other root trace. */

201. LJ_TRLINK_LOOP,                /* Loop to same trace. */

202. LJ_TRLINK_TAILREC,                /* Tail-recursion. */

203. LJ_TRLINK_UPREC,                /* Up-recursion. */

204. LJ_TRLINK_DOWNREC,                /* Down-recursion. */

205. LJ_TRLINK_INTERP,                /* Fallback to interpreter. */

206. LJ_TRLINK_RETURN,                /* Return to interpreter. */

207. LJ_TRLINK_STITCH                /* Trace stitching. */

208. ‌} TraceLink;

210. /* Trace object. */

211. ‌typedef struct GCtrace {

212. GCHeader;

213. uint8_t topslot;        /* Top stack slot already checked to be allocated. */

214. uint8_t linktype;        /* Type of link. */

215. IRRef nins;                /* Next IR instruction. Biased with REF_BIAS. */

216. #if LJ_GC64

217. uint32_t unused_gc64;

218. #endif

219. GCRef gclist;

220. IRIns *ir;                /* IR instructions/constants. Biased with REF_BIAS. */

221. IRRef nk;                /* Lowest IR constant. Biased with REF_BIAS. */

222. uint16_t nsnap;        /* Number of snapshots. */

223. uint16_t nsnapmap;        /* Number of snapshot map elements. */

224. SnapShot *snap;        /* Snapshot array. */

225. SnapEntry *snapmap;        /* Snapshot map. */

226. GCRef startpt;        /* Starting prototype. */

227. MRef startpc;                /* Bytecode PC of starting instruction. */

228. BCIns startins;        /* Original bytecode of starting instruction. */

229. MSize szmcode;        /* Size of machine code. */

230. MCode *mcode;                /* Start of machine code. */

231. MSize mcloop;                /* Offset of loop start in machine code. */

232. uint16_t nchild;        /* Number of child traces (root trace only). */

233. uint16_t spadjust;        /* Stack pointer adjustment (offset in bytes). */

234. TraceNo1 traceno;        /* Trace number. */

235. TraceNo1 link;        /* Linked trace (or self for loops). */

236. TraceNo1 root;        /* Root trace of side trace (or 0 for root traces). */

237. TraceNo1 nextroot;        /* Next root trace for same prototype. */

238. TraceNo1 nextside;        /* Next side trace of same root trace. */

239. uint8_t sinktags;        /* Trace has SINK tags. */

240. uint8_t unused1;

241. #ifdef LUAJIT_USE_GDBJIT

242. void *gdbjit_entry;        /* GDB JIT entry. */

243. #endif

244. ‌} GCtrace;

246. ‌#define gco2trace(o)        check_exp((o)->gch.gct == ~LJ_TTRACE, (GCtrace *)(o))

247. ‌#define traceref(J, n) \

248. check_exp((n)>0 && (MSize)(n)<J->sizetrace, (GCtrace *)gcref(J->trace[(n)]))

250. ‌LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtrace, gclist));

252. ‌static LJ_AINLINE MSize snap_nextofs(GCtrace *T, SnapShot *snap)

253. {

254. if (snap+1 == &T->snap[T->nsnap])

255. return T->nsnapmap;

256. else

257. return (snap+1)->mapofs;

258. }

260. /* Round-robin penalty cache for bytecodes leading to aborted traces. */

261. ‌typedef struct HotPenalty {

262. MRef pc;                /* Starting bytecode PC. */

263. uint16_t val;                /* Penalty value, i.e. hotcount start. */

264. uint16_t reason;        /* Abort reason (really TraceErr). */

265. ‌} HotPenalty;

267. ‌#define PENALTY_SLOTS        64        /* Penalty cache slot. Must be a power of 2. */

268. ‌#define PENALTY_MIN        (36*2)        /* Minimum penalty value. */

269. ‌#define PENALTY_MAX        60000        /* Maximum penalty value. */

270. ‌#define PENALTY_RNDBITS        4        /* # of random bits to add to penalty value. */

272. /* Round-robin backpropagation cache for narrowing conversions. */

273. ‌typedef struct BPropEntry {

274. IRRef1 key;                /* Key: original reference. */

275. IRRef1 val;                /* Value: reference after conversion. */

276. IRRef mode;                /* Mode for this entry (currently IRCONV_*). */

277. ‌} BPropEntry;

279. /* Number of slots for the backpropagation cache. Must be a power of 2. */

280. ‌#define BPROP_SLOTS        16

282. /* Scalar evolution analysis cache. */

283. ‌typedef struct ScEvEntry {

284. MRef pc;                /* Bytecode PC of FORI. */

285. IRRef1 idx;                /* Index reference. */

286. IRRef1 start;                /* Constant start reference. */

287. IRRef1 stop;                /* Constant stop reference. */

288. IRRef1 step;                /* Constant step reference. */

289. IRType1 t;                /* Scalar type. */

290. uint8_t dir;                /* Direction. 1: +, 0: -. */

291. ‌} ScEvEntry;

293. /* 128 bit SIMD constants. */

294. enum {

295. LJ_KSIMD_ABS,

296. LJ_KSIMD_NEG,

297. LJ_KSIMD__MAX

298. };

300. /* Get 16 byte aligned pointer to SIMD constant. */

301. ‌#define LJ_KSIMD(J, n) \

302. ((TValue *)(((intptr_t)&J->ksimd[2*(n)] + 15) & ~(intptr_t)15))

304. /* Set/reset flag to activate the SPLIT pass for the current trace. */

305. #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)

306. ‌#define lj_needsplit(J)                (J->needsplit = 1)

307. ‌#define lj_resetsplit(J)        (J->needsplit = 0)

308. #else

309. ‌#define lj_needsplit(J)                UNUSED(J)

310. ‌#define lj_resetsplit(J)        UNUSED(J)

311. #endif

313. /* Fold state is used to fold instructions on-the-fly. */

314. ‌typedef struct FoldState {

315. IRIns ins;                /* Currently emitted instruction. */

316. IRIns left;                /* Instruction referenced by left operand. */

317. IRIns right;                /* Instruction referenced by right operand. */

318. ‌} FoldState;

320. /* JIT compiler state. */

321. ‌typedef struct jit_State {

322. GCtrace cur;                /* Current trace. */

324. lua_State *L;                /* Current Lua state. */

325. const BCIns *pc;        /* Current PC. */

326. GCfunc *fn;                /* Current function. */

327. GCproto *pt;                /* Current prototype. */

328. TRef *base;                /* Current frame base, points into J->slots. */

330. uint32_t flags;        /* JIT engine flags. */

331. BCReg maxslot;        /* Relative to baseslot. */

332. BCReg baseslot;        /* Current frame base, offset into J->slots. */

334. uint8_t mergesnap;        /* Allowed to merge with next snapshot. */

335. uint8_t needsnap;        /* Need snapshot before recording next bytecode. */

336. IRType1 guardemit;        /* Accumulated IRT_GUARD for emitted instructions. */

337. uint8_t bcskip;        /* Number of bytecode instructions to skip. */

339. FoldState fold;        /* Fold state. */

341. const BCIns *bc_min;        /* Start of allowed bytecode range for root trace. */

342. MSize bc_extent;        /* Extent of the range. */

344. TraceState state;        /* Trace compiler state. */

346. int32_t instunroll;        /* Unroll counter for instable loops. */

347. int32_t loopunroll;        /* Unroll counter for loop ops in side traces. */

348. int32_t tailcalled;        /* Number of successive tailcalls. */

349. int32_t framedepth;        /* Current frame depth. */

350. int32_t retdepth;        /* Return frame depth (count of RETF). */

352. MRef k64;                /* Pointer to chained array of 64 bit constants. */

353. TValue ksimd[LJ_KSIMD__MAX*2+1];  /* 16 byte aligned SIMD constants. */

355. IRIns *irbuf;                /* Temp. IR instruction buffer. Biased with REF_BIAS. */

356. IRRef irtoplim;        /* Upper limit of instuction buffer (biased). */

357. IRRef irbotlim;        /* Lower limit of instuction buffer (biased). */

358. IRRef loopref;        /* Last loop reference or ref of final LOOP (or 0). */

360. MSize sizesnap;        /* Size of temp. snapshot buffer. */

361. SnapShot *snapbuf;        /* Temp. snapshot buffer. */

362. SnapEntry *snapmapbuf;  /* Temp. snapshot map buffer. */

363. MSize sizesnapmap;        /* Size of temp. snapshot map buffer. */

365. PostProc postproc;        /* Required post-processing after execution. */

366. #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)

367. int needsplit;        /* Need SPLIT pass. */

368. #endif

370. GCRef *trace;                /* Array of traces. */

371. TraceNo freetrace;        /* Start of scan for next free trace. */

372. MSize sizetrace;        /* Size of trace array. */

374. IRRef1 chain[IR__MAX];  /* IR instruction skip-list chain anchors. */

375. TRef slot[LJ_MAX_JSLOTS+LJ_STACK_EXTRA];  /* Stack slot map. */

377. int32_t param[JIT_P__MAX];  /* JIT engine parameters. */

379. MCode *exitstubgroup[LJ_MAX_EXITSTUBGR];  /* Exit stub group addresses. */

381. HotPenalty penalty[PENALTY_SLOTS];  /* Penalty slots. */

382. uint32_t penaltyslot;        /* Round-robin index into penalty slots. */

383. uint32_t prngstate;        /* PRNG state. */

385. BPropEntry bpropcache[BPROP_SLOTS];  /* Backpropagation cache slots. */

386. uint32_t bpropslot;        /* Round-robin index into bpropcache slots. */

388. ScEvEntry scev;        /* Scalar evolution analysis cache slots. */

390. const BCIns *startpc;        /* Bytecode PC of starting instruction. */

391. TraceNo parent;        /* Parent of current side trace (0 for root traces). */

392. ExitNo exitno;        /* Exit number in parent of current side trace. */

394. BCIns *patchpc;        /* PC for pending re-patch. */

395. BCIns patchins;        /* Instruction for pending re-patch. */

397. int mcprot;                /* Protection of current mcode area. */

398. MCode *mcarea;        /* Base of current mcode area. */

399. MCode *mctop;                /* Top of current mcode area. */

400. MCode *mcbot;                /* Bottom of current mcode area. */

401. size_t szmcarea;        /* Size of current mcode area. */

402. size_t szallmcarea;        /* Total size of all allocated mcode areas. */

404. TValue errinfo;        /* Additional info element for trace errors. */

406. #if LJ_HASPROFILE

407. GCproto *prev_pt;        /* Previous prototype. */

408. BCLine prev_line;        /* Previous line. */

409. int prof_mode;        /* Profiling mode: 0, 'f', 'l'. */

410. #endif

411. }

412. #if LJ_TARGET_ARM

413. LJ_ALIGN(16)                /* For DISPATCH-relative addresses in assembler part. */

414. #endif

415. jit_State;

417. /* Trivial PRNG e.g. used for penalty randomization. */

418. ‌static LJ_AINLINE uint32_t LJ_PRNG_BITS(jit_State *J, int bits)

419. {

420. /* Yes, this LCG is very weak, but that doesn't matter for our use case. */

421. J->prngstate = J->prngstate * 1103515245 + 12345;

422. return J->prngstate >> (32-bits);

423. }

425. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_string.c - luajit-2.0-src

Global variables defined

- match_class_map

Data types defined

- MatchState

- MatchState

Functions defined

- LJLIB_ASM(string_char) LJLIB_REC(.)

- LJLIB_ASM(string_sub) LJLIB_REC(string_range 1)

- LJLIB_ASM(string_reverse) LJLIB_REC(string_op IRCALL_lj_buf_putstr_reverse)

- LJLIB_CF(string_rep) LJLIB_REC(.)

- LJLIB_CF(string_dump)

- LJLIB_CF(string_find) LJLIB_REC(.)

- LJLIB_CF(string_match)

- LJLIB_NOREG LJLIB_CF(string_gmatch_aux)

- LJLIB_CF(string_gmatch)

- LJLIB_CF(string_gsub)

- LJLIB_CF(string_format) LJLIB_REC(.)

- LJLIB_LUA(string_len) /*

- add_s

- add_value

- capture_to_close

- check_capture

- classend

- end_capture

- luaopen_string

- match

- match_capture

- match_class

- matchbalance

- matchbracketclass

- max_expand

- min_expand

- push_captures

- push_onecapture

- singlematch

- start_capture

- str_find_aux

- string_fmt_tostring

- writer_buf

Macros defined

- CAP_POSITION

- CAP_UNFINISHED

- LJLIB_MODULE_string

- LUA_LIB

- L_ESC

- lib_string_c

- uchar

Source code

1. /*

2. ** String library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lib_string_c

10. ‌#define LUA_LIB

12. #include "lua.h"

13. #include "lauxlib.h"

14. #include "lualib.h"

16. #include "lj_obj.h"

17. #include "lj_gc.h"

18. #include "lj_err.h"

19. #include "lj_buf.h"

20. #include "lj_str.h"

21. #include "lj_tab.h"

22. #include "lj_meta.h"

23. #include "lj_state.h"

24. #include "lj_ff.h"

25. #include "lj_bcdump.h"

26. #include "lj_char.h"

27. #include "lj_strfmt.h"

28. #include "lj_lib.h"

30. /* ------------------------------------------------------------------------ */

32. ‌#define LJLIB_MODULE_string

34. ‌LJLIB_LUA(string_len) /*

35. function(s)

36. CHECK_str(s)

37. return #s

38. end

39. */

41. LJLIB_ASM(string_byte)                LJLIB_REC(string_range 0)

42. {

43. GCstr *s = lj_lib_checkstr(L, 1);

44. int32_t len = (int32_t)s->len;

45. int32_t start = lj_lib_optint(L, 2, 1);

46. int32_t stop = lj_lib_optint(L, 3, start);

47. int32_t n, i;

48. const unsigned char *p;

49. if (stop < 0) stop += len+1;

50. if (start < 0) start += len+1;

51. if (start <= 0) start = 1;

52. if (stop > len) stop = len;

53. if (start > stop) return FFH_RES(0);  /* Empty interval: return no results. */

54. start--;

55. n = stop - start;

56. if ((uint32_t)n > LUAI_MAXCSTACK)

57. lj_err_caller(L, LJ_ERR_STRSLC);

58. lj_state_checkstack(L, (MSize)n);

59. p = (const unsigned char *)strdata(s) + start;

60. for (i = 0; i < n; i++)

61. setintV(L->base + i-1-LJ_FR2, p[i]);

62. return FFH_RES(n);

63. }

65. ‌LJLIB_ASM(string_char)                LJLIB_REC(.)

66. {

67. int i, nargs = (int)(L->top - L->base);

68. char *buf = lj_buf_tmp(L, (MSize)nargs);

69. for (i = 1; i <= nargs; i++) {

70. int32_t k = lj_lib_checkint(L, i);

71. if (!checku8(k))

72. lj_err_arg(L, i, LJ_ERR_BADVAL);

73. buf[i-1] = (char)k;

74. }

75. setstrV(L, L->base-1-LJ_FR2, lj_str_new(L, buf, (size_t)nargs));

76. return FFH_RES(1);

77. }

79. ‌LJLIB_ASM(string_sub)                LJLIB_REC(string_range 1)

80. {

81. lj_lib_checkstr(L, 1);

82. lj_lib_checkint(L, 2);

83. setintV(L->base+2, lj_lib_optint(L, 3, -1));

84. return FFH_RETRY;

85. }

87. ‌LJLIB_CF(string_rep)                LJLIB_REC(.)

88. {

89. GCstr *s = lj_lib_checkstr(L, 1);

90. int32_t rep = lj_lib_checkint(L, 2);

91. GCstr *sep = lj_lib_optstr(L, 3);

92. SBuf *sb = lj_buf_tmp_(L);

93. if (sep && rep > 1) {

94. GCstr *s2 = lj_buf_cat2str(L, sep, s);

95. lj_buf_reset(sb);

96. lj_buf_putstr(sb, s);

97. s = s2;

98. rep--;

99. }

100. sb = lj_buf_putstr_rep(sb, s, rep);

101. setstrV(L, L->top-1, lj_buf_str(L, sb));

102. lj_gc_check(L);

103. return 1;

104. }

106. ‌LJLIB_ASM(string_reverse)  LJLIB_REC(string_op IRCALL_lj_buf_putstr_reverse)

107. {

108. lj_lib_checkstr(L, 1);

109. return FFH_RETRY;

110. }

111. LJLIB_ASM_(string_lower)  LJLIB_REC(string_op IRCALL_lj_buf_putstr_lower)

112. LJLIB_ASM_(string_upper)  LJLIB_REC(string_op IRCALL_lj_buf_putstr_upper)

114. /* ------------------------------------------------------------------------ */

116. ‌static int writer_buf(lua_State *L, const void *p, size_t size, void *sb)

117. {

118. lj_buf_putmem((SBuf *)sb, p, (MSize)size);

119. UNUSED(L);

120. return 0;

121. }

123. ‌LJLIB_CF(string_dump)

124. {

125. GCfunc *fn = lj_lib_checkfunc(L, 1);

126. int strip = L->base+1 < L->top && tvistruecond(L->base+1);

127. SBuf *sb = lj_buf_tmp_(L);  /* Assumes lj_bcwrite() doesn't use tmpbuf. */

128. L->top = L->base+1;

129. if (!isluafunc(fn) || lj_bcwrite(L, funcproto(fn), writer_buf, sb, strip))

130. lj_err_caller(L, LJ_ERR_STRDUMP);

131. setstrV(L, L->top-1, lj_buf_str(L, sb));

132. lj_gc_check(L);

133. return 1;

134. }

136. /* ------------------------------------------------------------------------ */

138. /* macro to `unsign' a character */

139. ‌#define uchar(c)        ((unsigned char)(c))

141. ‌#define CAP_UNFINISHED        (-1)

142. ‌#define CAP_POSITION        (-2)

144. ‌typedef struct MatchState {

145. const char *src_init;  /* init of source string */

146. const char *src_end;  /* end (`\0') of source string */

147. lua_State *L;

148. int level;  /* total number of captures (finished or unfinished) */

149. int depth;

150. struct {

151. const char *init;

152. ptrdiff_t len;

153. } capture[LUA_MAXCAPTURES];

154. ‌} MatchState;

156. ‌#define L_ESC                '%'

158. ‌static int check_capture(MatchState *ms, int l)

159. {

160. l -= '1';

161. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)

162. lj_err_caller(ms->L, LJ_ERR_STRCAPI);

163. return l;

164. }

166. ‌static int capture_to_close(MatchState *ms)

167. {

168. int level = ms->level;

169. for (level--; level>=0; level--)

170. if (ms->capture[level].len == CAP_UNFINISHED) return level;

171. lj_err_caller(ms->L, LJ_ERR_STRPATC);

172. return 0;  /* unreachable */

173. }

175. ‌static const char *classend(MatchState *ms, const char *p)

176. {

177. switch (*p++) {

178. case L_ESC:

179. if (*p == '\0')

180. lj_err_caller(ms->L, LJ_ERR_STRPATE);

181. return p+1;

182. case '[':

183. if (*p == '^') p++;

184. do {  /* look for a `]' */

185. if (*p == '\0')

186. lj_err_caller(ms->L, LJ_ERR_STRPATM);

187. if (*(p++) == L_ESC && *p != '\0')

188. p++;  /* skip escapes (e.g. `%]') */

189. } while (*p != ']');

190. return p+1;

191. default:

192. return p;

193. }

194. }

196. ‌static const unsigned char match_class_map[32] = {

197. 0,LJ_CHAR_ALPHA,0,LJ_CHAR_CNTRL,LJ_CHAR_DIGIT,0,0,LJ_CHAR_GRAPH,0,0,0,0,

198. LJ_CHAR_LOWER,0,0,0,LJ_CHAR_PUNCT,0,0,LJ_CHAR_SPACE,0,

199. LJ_CHAR_UPPER,0,LJ_CHAR_ALNUM,LJ_CHAR_XDIGIT,0,0,0,0,0,0,0

200. };

202. ‌static int match_class(int c, int cl)

203. {

204. if ((cl & 0xc0) == 0x40) {

205. int t = match_class_map[(cl&0x1f)];

206. if (t) {

207. t = lj_char_isa(c, t);

208. return (cl & 0x20) ? t : !t;

209. }

210. if (cl == 'z') return c == 0;

211. if (cl == 'Z') return c != 0;

212. }

213. return (cl == c);

214. }

216. ‌static int matchbracketclass(int c, const char *p, const char *ec)

217. {

218. int sig = 1;

219. if (*(p+1) == '^') {

220. sig = 0;

221. p++;  /* skip the `^' */

222. }

223. while (++p < ec) {

224. if (*p == L_ESC) {

225. p++;

226. if (match_class(c, uchar(*p)))

227. return sig;

228. }

229. else if ((*(p+1) == '-') && (p+2 < ec)) {

230. p+=2;

231. if (uchar(*(p-2)) <= c && c <= uchar(*p))

232. return sig;

233. }

234. else if (uchar(*p) == c) return sig;

235. }

236. return !sig;

237. }

239. ‌static int singlematch(int c, const char *p, const char *ep)

240. {

241. switch (*p) {

242. case '.': return 1;  /* matches any char */

243. case L_ESC: return match_class(c, uchar(*(p+1)));

244. case '[': return matchbracketclass(c, p, ep-1);

245. default:  return (uchar(*p) == c);

246. }

247. }

249. static const char *match(MatchState *ms, const char *s, const char *p);

251. ‌static const char *matchbalance(MatchState *ms, const char *s, const char *p)

252. {

253. if (*p == 0 || *(p+1) == 0)

254. lj_err_caller(ms->L, LJ_ERR_STRPATU);

255. if (*s != *p) {

256. return NULL;

257. } else {

258. int b = *p;

259. int e = *(p+1);

260. int cont = 1;

261. while (++s < ms->src_end) {

262. if (*s == e) {

263. if (--cont == 0) return s+1;

264. } else if (*s == b) {

265. cont++;

266. }

267. }

268. }

269. return NULL;  /* string ends out of balance */

270. }

272. ‌static const char *max_expand(MatchState *ms, const char *s,

273. const char *p, const char *ep)

274. {

275. ptrdiff_t i = 0;  /* counts maximum expand for item */

276. while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))

277. i++;

278. /* keeps trying to match with the maximum repetitions */

279. while (i>=0) {

280. const char *res = match(ms, (s+i), ep+1);

281. if (res) return res;

282. i--;  /* else didn't match; reduce 1 repetition to try again */

283. }

284. return NULL;

285. }

287. ‌static const char *min_expand(MatchState *ms, const char *s,

288. const char *p, const char *ep)

289. {

290. for (;;) {

291. const char *res = match(ms, s, ep+1);

292. if (res != NULL)

293. return res;

294. else if (s<ms->src_end && singlematch(uchar(*s), p, ep))

295. s++;  /* try with one more repetition */

296. else

297. return NULL;

298. }

299. }

301. ‌static const char *start_capture(MatchState *ms, const char *s,

302. const char *p, int what)

303. {

304. const char *res;

305. int level = ms->level;

306. if (level >= LUA_MAXCAPTURES) lj_err_caller(ms->L, LJ_ERR_STRCAPN);

307. ms->capture[level].init = s;

308. ms->capture[level].len = what;

309. ms->level = level+1;

310. if ((res=match(ms, s, p)) == NULL)  /* match failed? */

311. ms->level--;  /* undo capture */

312. return res;

313. }

315. ‌static const char *end_capture(MatchState *ms, const char *s,

316. const char *p)

317. {

318. int l = capture_to_close(ms);

319. const char *res;

320. ms->capture[l].len = s - ms->capture[l].init;  /* close capture */

321. if ((res = match(ms, s, p)) == NULL)  /* match failed? */

322. ms->capture[l].len = CAP_UNFINISHED;  /* undo capture */

323. return res;

324. }

326. ‌static const char *match_capture(MatchState *ms, const char *s, int l)

327. {

328. size_t len;

329. l = check_capture(ms, l);

330. len = (size_t)ms->capture[l].len;

331. if ((size_t)(ms->src_end-s) >= len &&

332. memcmp(ms->capture[l].init, s, len) == 0)

333. return s+len;

334. else

335. return NULL;

336. }

338. ‌static const char *match(MatchState *ms, const char *s, const char *p)

339. {

340. if (++ms->depth > LJ_MAX_XLEVEL)

341. lj_err_caller(ms->L, LJ_ERR_STRPATX);

342. init: /* using goto's to optimize tail recursion */

343. switch (*p) {

344. case '(':  /* start capture */

345. if (*(p+1) == ')')  /* position capture? */

346. s = start_capture(ms, s, p+2, CAP_POSITION);

347. else

348. s = start_capture(ms, s, p+1, CAP_UNFINISHED);

349. break;

350. case ')':  /* end capture */

351. s = end_capture(ms, s, p+1);

352. break;

353. case L_ESC:

354. switch (*(p+1)) {

355. case 'b':  /* balanced string? */

356. s = matchbalance(ms, s, p+2);

357. if (s == NULL) break;

358. p+=4;

359. goto init;  /* else s = match(ms, s, p+4); */

360. case 'f': {  /* frontier? */

361. const char *ep; char previous;

362. p += 2;

363. if (*p != '[')

364. lj_err_caller(ms->L, LJ_ERR_STRPATB);

365. ep = classend(ms, p);  /* points to what is next */

366. previous = (s == ms->src_init) ? '\0' : *(s-1);

367. if (matchbracketclass(uchar(previous), p, ep-1) ||

368. !matchbracketclass(uchar(*s), p, ep-1)) { s = NULL; break; }

369. p=ep;

370. goto init;  /* else s = match(ms, s, ep); */

371. }

372. default:

373. if (lj_char_isdigit(uchar(*(p+1)))) {  /* capture results (%0-%9)? */

374. s = match_capture(ms, s, uchar(*(p+1)));

375. if (s == NULL) break;

376. p+=2;

377. goto init;  /* else s = match(ms, s, p+2) */

378. }

379. goto dflt;  /* case default */

380. }

381. break;

382. case '\0':  /* end of pattern */

383. break;  /* match succeeded */

384. case '$':

385. /* is the `$' the last char in pattern? */

386. if (*(p+1) != '\0') goto dflt;

387. if (s != ms->src_end) s = NULL;  /* check end of string */

388. break;

389. default: dflt: {  /* it is a pattern item */

390. const char *ep = classend(ms, p);  /* points to what is next */

391. int m = s<ms->src_end && singlematch(uchar(*s), p, ep);

392. switch (*ep) {

393. case '?': {  /* optional */

394. const char *res;

395. if (m && ((res=match(ms, s+1, ep+1)) != NULL)) {

396. s = res;

397. break;

398. }

399. p=ep+1;

400. goto init;  /* else s = match(ms, s, ep+1); */

401. }

402. case '*':  /* 0 or more repetitions */

403. s = max_expand(ms, s, p, ep);

404. break;

405. case '+':  /* 1 or more repetitions */

406. s = (m ? max_expand(ms, s+1, p, ep) : NULL);

407. break;

408. case '-':  /* 0 or more repetitions (minimum) */

409. s = min_expand(ms, s, p, ep);

410. break;

411. default:

412. if (m) { s++; p=ep; goto init; }  /* else s = match(ms, s+1, ep); */

413. s = NULL;

414. break;

415. }

416. break;

417. }

418. }

419. ms->depth--;

420. return s;

421. }

423. ‌static void push_onecapture(MatchState *ms, int i, const char *s, const char *e)

424. {

425. if (i >= ms->level) {

426. if (i == 0)  /* ms->level == 0, too */

427. lua_pushlstring(ms->L, s, (size_t)(e - s));  /* add whole match */

428. else

429. lj_err_caller(ms->L, LJ_ERR_STRCAPI);

430. } else {

431. ptrdiff_t l = ms->capture[i].len;

432. if (l == CAP_UNFINISHED) lj_err_caller(ms->L, LJ_ERR_STRCAPU);

433. if (l == CAP_POSITION)

434. lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);

435. else

436. lua_pushlstring(ms->L, ms->capture[i].init, (size_t)l);

437. }

438. }

440. ‌static int push_captures(MatchState *ms, const char *s, const char *e)

441. {

442. int i;

443. int nlevels = (ms->level == 0 && s) ? 1 : ms->level;

444. luaL_checkstack(ms->L, nlevels, "too many captures");

445. for (i = 0; i < nlevels; i++)

446. push_onecapture(ms, i, s, e);

447. return nlevels;  /* number of strings pushed */

448. }

450. ‌static int str_find_aux(lua_State *L, int find)

451. {

452. GCstr *s = lj_lib_checkstr(L, 1);

453. GCstr *p = lj_lib_checkstr(L, 2);

454. int32_t start = lj_lib_optint(L, 3, 1);

455. MSize st;

456. if (start < 0) start += (int32_t)s->len; else start--;

457. if (start < 0) start = 0;

458. st = (MSize)start;

459. if (st > s->len) {

460. #if LJ_52

461. setnilV(L->top-1);

462. return 1;

463. #else

464. st = s->len;

465. #endif

466. }

467. if (find && ((L->base+3 < L->top && tvistruecond(L->base+3)) ||

468. !lj_str_haspattern(p))) {  /* Search for fixed string. */

469. const char *q = lj_str_find(strdata(s)+st, strdata(p), s->len-st, p->len);

470. if (q) {

471. setintV(L->top-2, (int32_t)(q-strdata(s)) + 1);

472. setintV(L->top-1, (int32_t)(q-strdata(s)) + (int32_t)p->len);

473. return 2;

474. }

475. } else {  /* Search for pattern. */

476. MatchState ms;

477. const char *pstr = strdata(p);

478. const char *sstr = strdata(s) + st;

479. int anchor = 0;

480. if (*pstr == '^') { pstr++; anchor = 1; }

481. ms.L = L;

482. ms.src_init = strdata(s);

483. ms.src_end = strdata(s) + s->len;

484. do {  /* Loop through string and try to match the pattern. */

485. const char *q;

486. ms.level = ms.depth = 0;

487. q = match(&ms, sstr, pstr);

488. if (q) {

489. if (find) {

490. setintV(L->top++, (int32_t)(sstr-(strdata(s)-1)));

491. setintV(L->top++, (int32_t)(q-strdata(s)));

492. return push_captures(&ms, NULL, NULL) + 2;

493. } else {

494. return push_captures(&ms, sstr, q);

495. }

496. }

497. } while (sstr++ < ms.src_end && !anchor);

498. }

499. setnilV(L->top-1);  /* Not found. */

500. return 1;

501. }

503. ‌LJLIB_CF(string_find)                LJLIB_REC(.)

504. {

505. return str_find_aux(L, 1);

506. }

508. ‌LJLIB_CF(string_match)

509. {

510. return str_find_aux(L, 0);

511. }

513. ‌LJLIB_NOREG LJLIB_CF(string_gmatch_aux)

514. {

515. const char *p = strVdata(lj_lib_upvalue(L, 2));

516. GCstr *str = strV(lj_lib_upvalue(L, 1));

517. const char *s = strdata(str);

518. TValue *tvpos = lj_lib_upvalue(L, 3);

519. const char *src = s + tvpos->u32.lo;

520. MatchState ms;

521. ms.L = L;

522. ms.src_init = s;

523. ms.src_end = s + str->len;

524. for (; src <= ms.src_end; src++) {

525. const char *e;

526. ms.level = ms.depth = 0;

527. if ((e = match(&ms, src, p)) != NULL) {

528. int32_t pos = (int32_t)(e - s);

529. if (e == src) pos++;  /* Ensure progress for empty match. */

530. tvpos->u32.lo = (uint32_t)pos;

531. return push_captures(&ms, src, e);

532. }

533. }

534. return 0;  /* not found */

535. }

537. ‌LJLIB_CF(string_gmatch)

538. {

539. lj_lib_checkstr(L, 1);

540. lj_lib_checkstr(L, 2);

541. L->top = L->base+3;

542. (L->top-1)->u64 = 0;

543. lj_lib_pushcc(L, lj_cf_string_gmatch_aux, FF_string_gmatch_aux, 3);

544. return 1;

545. }

547. ‌static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e)

548. {

549. size_t l, i;

550. const char *news = lua_tolstring(ms->L, 3, &l);

551. for (i = 0; i < l; i++) {

552. if (news[i] != L_ESC) {

553. luaL_addchar(b, news[i]);

554. } else {

555. i++;  /* skip ESC */

556. if (!lj_char_isdigit(uchar(news[i]))) {

557. luaL_addchar(b, news[i]);

558. } else if (news[i] == '0') {

559. luaL_addlstring(b, s, (size_t)(e - s));

560. } else {

561. push_onecapture(ms, news[i] - '1', s, e);

562. luaL_addvalue(b);  /* add capture to accumulated result */

563. }

564. }

565. }

566. }

568. ‌static void add_value(MatchState *ms, luaL_Buffer *b,

569. const char *s, const char *e)

570. {

571. lua_State *L = ms->L;

572. switch (lua_type(L, 3)) {

573. case LUA_TNUMBER:

574. case LUA_TSTRING: {

575. add_s(ms, b, s, e);

576. return;

577. }

578. case LUA_TFUNCTION: {

579. int n;

580. lua_pushvalue(L, 3);

581. n = push_captures(ms, s, e);

582. lua_call(L, n, 1);

583. break;

584. }

585. case LUA_TTABLE: {

586. push_onecapture(ms, 0, s, e);

587. lua_gettable(L, 3);

588. break;

589. }

590. }

591. if (!lua_toboolean(L, -1)) {  /* nil or false? */

592. lua_pop(L, 1);

593. lua_pushlstring(L, s, (size_t)(e - s));  /* keep original text */

594. } else if (!lua_isstring(L, -1)) {

595. lj_err_callerv(L, LJ_ERR_STRGSRV, luaL_typename(L, -1));

596. }

597. luaL_addvalue(b);  /* add result to accumulator */

598. }

600. ‌LJLIB_CF(string_gsub)

601. {

602. size_t srcl;

603. const char *src = luaL_checklstring(L, 1, &srcl);

604. const char *p = luaL_checkstring(L, 2);

605. int  tr = lua_type(L, 3);

606. int max_s = luaL_optint(L, 4, (int)(srcl+1));

607. int anchor = (*p == '^') ? (p++, 1) : 0;

608. int n = 0;

609. MatchState ms;

610. luaL_Buffer b;

611. if (!(tr == LUA_TNUMBER || tr == LUA_TSTRING ||

612. tr == LUA_TFUNCTION || tr == LUA_TTABLE))

613. lj_err_arg(L, 3, LJ_ERR_NOSFT);

614. luaL_buffinit(L, &b);

615. ms.L = L;

616. ms.src_init = src;

617. ms.src_end = src+srcl;

618. while (n < max_s) {

619. const char *e;

620. ms.level = ms.depth = 0;

621. e = match(&ms, src, p);

622. if (e) {

623. n++;

624. add_value(&ms, &b, src, e);

625. }

626. if (e && e>src) /* non empty match? */

627. src = e;  /* skip it */

628. else if (src < ms.src_end)

629. luaL_addchar(&b, *src++);

630. else

631. break;

632. if (anchor)

633. break;

634. }

635. luaL_addlstring(&b, src, (size_t)(ms.src_end-src));

636. luaL_pushresult(&b);

637. lua_pushinteger(L, n);  /* number of substitutions */

638. return 2;

639. }

641. /* ------------------------------------------------------------------------ */

643. /* Emulate tostring() inline. */

644. ‌static GCstr *string_fmt_tostring(lua_State *L, int arg, int retry)

645. {

646. TValue *o = L->base+arg-1;

647. cTValue *mo;

648. lua_assert(o < L->top);  /* Caller already checks for existence. */

649. if (LJ_LIKELY(tvisstr(o)))

650. return strV(o);

651. if (retry != 2 && !tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {

652. copyTV(L, L->top++, mo);

653. copyTV(L, L->top++, o);

654. lua_call(L, 1, 1);

655. copyTV(L, L->base+arg-1, --L->top);

656. return NULL;  /* Buffer may be overwritten, retry. */

657. }

658. return lj_strfmt_obj(L, o);

659. }

661. ‌LJLIB_CF(string_format)                LJLIB_REC(.)

662. {

663. int arg, top = (int)(L->top - L->base);

664. GCstr *fmt;

665. SBuf *sb;

666. FormatState fs;

667. SFormat sf;

668. int retry = 0;

669. again:

670. arg = 1;

671. sb = lj_buf_tmp_(L);

672. fmt = lj_lib_checkstr(L, arg);

673. lj_strfmt_init(&fs, strdata(fmt), fmt->len);

674. while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) {

675. if (sf == STRFMT_LIT) {

676. lj_buf_putmem(sb, fs.str, fs.len);

677. } else if (sf == STRFMT_ERR) {

678. lj_err_callerv(L, LJ_ERR_STRFMT, strdata(lj_str_new(L, fs.str, fs.len)));

679. } else {

680. if (++arg > top)

681. luaL_argerror(L, arg, lj_obj_typename[0]);

682. switch (STRFMT_TYPE(sf)) {

683. case STRFMT_INT:

684. if (tvisint(L->base+arg-1)) {

685. int32_t k = intV(L->base+arg-1);

686. if (sf == STRFMT_INT)

687. lj_strfmt_putint(sb, k);  /* Shortcut for plain %d. */

688. else

689. lj_strfmt_putfxint(sb, sf, k);

690. } else {

691. lj_strfmt_putfnum_int(sb, sf, lj_lib_checknum(L, arg));

692. }

693. break;

694. case STRFMT_UINT:

695. if (tvisint(L->base+arg-1))

696. lj_strfmt_putfxint(sb, sf, intV(L->base+arg-1));

697. else

698. lj_strfmt_putfnum_uint(sb, sf, lj_lib_checknum(L, arg));

699. break;

700. case STRFMT_NUM:

701. lj_strfmt_putfnum(sb, sf, lj_lib_checknum(L, arg));

702. break;

703. case STRFMT_STR: {

704. GCstr *str = string_fmt_tostring(L, arg, retry);

705. if (str == NULL)

706. retry = 1;

707. else if ((sf & STRFMT_T_QUOTED))

708. lj_strfmt_putquoted(sb, str);  /* No formatting. */

709. else

710. lj_strfmt_putfstr(sb, sf, str);

711. break;

712. }

713. case STRFMT_CHAR:

714. lj_strfmt_putfchar(sb, sf, lj_lib_checkint(L, arg));

715. break;

716. case STRFMT_PTR:  /* No formatting. */

717. lj_strfmt_putptr(sb, lj_obj_ptr(L->base+arg-1));

718. break;

719. default:

720. lua_assert(0);

721. break;

722. }

723. }

724. }

725. if (retry++ == 1) goto again;

726. setstrV(L, L->top-1, lj_buf_str(L, sb));

727. lj_gc_check(L);

728. return 1;

729. }

731. /* ------------------------------------------------------------------------ */

733. #include "lj_libdef.h"

735. ‌LUALIB_API int luaopen_string(lua_State *L)

736. {

737. GCtab *mt;

738. global_State *g;

739. LJ_LIB_REG(L, LUA_STRLIBNAME, string);

740. #if defined(LUA_COMPAT_GFIND) && !LJ_52

741. lua_getfield(L, -1, "gmatch");

742. lua_setfield(L, -2, "gfind");

743. #endif

744. mt = lj_tab_new(L, 0, 1);

745. /* NOBARRIER: basemt is a GC root. */

746. g = G(L);

747. setgcref(basemt_it(g, LJ_TSTR), obj2gco(mt));

748. settabV(L, lj_tab_setstr(L, mt, mmname_str(g, MM_index)), tabV(L->top-1));

749. mt->nomm = (uint8_t)(~(1u<<MM_index));

750. return 1;

751. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_base.c - luajit-2.0-src

Functions defined

- LJLIB_ASM(assert) LJLIB_REC(.)

- LJLIB_NOREGUV LJLIB_ASM(ipairs_aux) LJLIB_REC(.)

- LJLIB_ASM(rawget) LJLIB_REC(.)

- LJLIB_ASM(tonumber) LJLIB_REC(.)

- LJLIB_ASM(tostring) LJLIB_REC(.)

- LJLIB_ASM(pcall) LJLIB_REC(.)

- LJLIB_ASM(coroutine_yield)

- LJLIB_ASM(coroutine_resume)

- LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux)

- LJLIB_ASM_(getmetatable) LJLIB_REC(.)

- LJLIB_CF(getfenv) LJLIB_REC(.)

- LJLIB_CF(setfenv)

- LJLIB_CF(rawset) LJLIB_REC(.)

- LJLIB_CF(rawequal) LJLIB_REC(.)

- LJLIB_CF(rawlen) LJLIB_REC(.)

- LJLIB_CF(unpack)

- LJLIB_CF(select) LJLIB_REC(.)

- LJLIB_CF(error)

- LJLIB_CF(loadfile)

- LJLIB_CF(load)

- LJLIB_CF(loadstring)

- LJLIB_CF(dofile)

- LJLIB_CF(gcinfo)

- LJLIB_CF(collectgarbage)

- LJLIB_CF(newproxy)

- LJLIB_CF(print)

- LJLIB_CF(coroutine_running)

- LJLIB_CF(coroutine_create)

- LJLIB_CF(coroutine_wrap)

- LJLIB_PUSH(lastcl)

- LJLIB_PUSH(lastcl)

- LJLIB_SET(_VERSION)

- ffh_pairs

- ffh_resume

- lj_ffh_coroutine_wrap_err

- load_aux

- luaopen_base

- newproxy_weaktable

- reader_func

- setpc_wrap_aux

Macros defined

- LJLIB_MODULE_base

- LJLIB_MODULE_coroutine

- LUA_LIB

- ffh_pairs

- lib_base_c

Source code

1. /*

2. ** Base and coroutine library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. #include <stdio.h>

11. ‌#define lib_base_c

12. ‌#define LUA_LIB

14. #include "lua.h"

15. #include "lauxlib.h"

16. #include "lualib.h"

18. #include "lj_obj.h"

19. #include "lj_gc.h"

20. #include "lj_err.h"

21. #include "lj_debug.h"

22. #include "lj_str.h"

23. #include "lj_tab.h"

24. #include "lj_meta.h"

25. #include "lj_state.h"

26. #if LJ_HASFFI

27. #include "lj_ctype.h"

28. #include "lj_cconv.h"

29. #endif

30. #include "lj_bc.h"

31. #include "lj_ff.h"

32. #include "lj_dispatch.h"

33. #include "lj_char.h"

34. #include "lj_strscan.h"

35. #include "lj_strfmt.h"

36. #include "lj_lib.h"

38. /* -- Base library: checks ------------------------------------------------ */

40. ‌#define LJLIB_MODULE_base

42. ‌LJLIB_ASM(assert)                LJLIB_REC(.)

43. {

44. GCstr *s;

45. lj_lib_checkany(L, 1);

46. s = lj_lib_optstr(L, 2);

47. if (s)

48. lj_err_callermsg(L, strdata(s));

49. else

50. lj_err_caller(L, LJ_ERR_ASSERT);

51. return FFH_UNREACHABLE;

52. }

54. /* ORDER LJ_T */

55. LJLIB_PUSH("nil")

56. LJLIB_PUSH("boolean")

57. LJLIB_PUSH(top-1)  /* boolean */

58. LJLIB_PUSH("userdata")

59. LJLIB_PUSH("string")

60. LJLIB_PUSH("upval")

61. LJLIB_PUSH("thread")

62. LJLIB_PUSH("proto")

63. LJLIB_PUSH("function")

64. LJLIB_PUSH("trace")

65. LJLIB_PUSH("cdata")

66. LJLIB_PUSH("table")

67. LJLIB_PUSH(top-9)  /* userdata */

68. LJLIB_PUSH("number")

69. LJLIB_ASM_(type)                LJLIB_REC(.)

70. /* Recycle the lj_lib_checkany(L, 1) from assert. */

72. /* -- Base library: iterators --------------------------------------------- */

74. /* This solves a circular dependency problem -- change FF_next_N as needed. */

75. LJ_STATIC_ASSERT((int)FF_next == FF_next_N);

77. LJLIB_ASM(next)

78. {

79. lj_lib_checktab(L, 1);

80. return FFH_UNREACHABLE;

81. }

83. #if LJ_52 || LJ_HASFFI

84. ‌static int ffh_pairs(lua_State *L, MMS mm)

85. {

86. TValue *o = lj_lib_checkany(L, 1);

87. cTValue *mo = lj_meta_lookup(L, o, mm);

88. if ((LJ_52 || tviscdata(o)) && !tvisnil(mo)) {

89. L->top = o+1;  /* Only keep one argument. */

90. copyTV(L, L->base-1-LJ_FR2, mo);  /* Replace callable. */

91. return FFH_TAILCALL;

92. } else {

93. if (!tvistab(o)) lj_err_argt(L, 1, LUA_TTABLE);

94. if (LJ_FR2) { copyTV(L, o-1, o); o--; }

95. setfuncV(L, o-1, funcV(lj_lib_upvalue(L, 1)));

96. if (mm == MM_pairs) setnilV(o+1); else setintV(o+1, 0);

97. return FFH_RES(3);

98. }

99. }

100. #else

101. ‌#define ffh_pairs(L, mm)        (lj_lib_checktab(L, 1), FFH_UNREACHABLE)

102. #endif

104. ‌LJLIB_PUSH(lastcl)

105. LJLIB_ASM(pairs)                LJLIB_REC(xpairs 0)

106. {

107. return ffh_pairs(L, MM_pairs);

108. }

110. ‌LJLIB_NOREGUV LJLIB_ASM(ipairs_aux)        LJLIB_REC(.)

111. {

112. lj_lib_checktab(L, 1);

113. lj_lib_checkint(L, 2);

114. return FFH_UNREACHABLE;

115. }

117. ‌LJLIB_PUSH(lastcl)

118. LJLIB_ASM(ipairs)                LJLIB_REC(xpairs 1)

119. {

120. return ffh_pairs(L, MM_ipairs);

121. }

123. /* -- Base library: getters and setters ----------------------------------- */

125. ‌LJLIB_ASM_(getmetatable)        LJLIB_REC(.)

126. /* Recycle the lj_lib_checkany(L, 1) from assert. */

128. LJLIB_ASM(setmetatable)                LJLIB_REC(.)

129. {

130. GCtab *t = lj_lib_checktab(L, 1);

131. GCtab *mt = lj_lib_checktabornil(L, 2);

132. if (!tvisnil(lj_meta_lookup(L, L->base, MM_metatable)))

133. lj_err_caller(L, LJ_ERR_PROTMT);

134. setgcref(t->metatable, obj2gco(mt));

135. if (mt) { lj_gc_objbarriert(L, t, mt); }

136. settabV(L, L->base-1-LJ_FR2, t);

137. return FFH_RES(1);

138. }

140. ‌LJLIB_CF(getfenv)                LJLIB_REC(.)

141. {

142. GCfunc *fn;

143. cTValue *o = L->base;

144. if (!(o < L->top && tvisfunc(o))) {

145. int level = lj_lib_optint(L, 1, 1);

146. o = lj_debug_frame(L, level, &level);

147. if (o == NULL)

148. lj_err_arg(L, 1, LJ_ERR_INVLVL);

149. if (LJ_FR2) o--;

150. }

151. fn = &gcval(o)->fn;

152. settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env));

153. return 1;

154. }

156. ‌LJLIB_CF(setfenv)

157. {

158. GCfunc *fn;

159. GCtab *t = lj_lib_checktab(L, 2);

160. cTValue *o = L->base;

161. if (!(o < L->top && tvisfunc(o))) {

162. int level = lj_lib_checkint(L, 1);

163. if (level == 0) {

164. /* NOBARRIER: A thread (i.e. L) is never black. */

165. setgcref(L->env, obj2gco(t));

166. return 0;

167. }

168. o = lj_debug_frame(L, level, &level);

169. if (o == NULL)

170. lj_err_arg(L, 1, LJ_ERR_INVLVL);

171. if (LJ_FR2) o--;

172. }

173. fn = &gcval(o)->fn;

174. if (!isluafunc(fn))

175. lj_err_caller(L, LJ_ERR_SETFENV);

176. setgcref(fn->l.env, obj2gco(t));

177. lj_gc_objbarrier(L, obj2gco(fn), t);

178. setfuncV(L, L->top++, fn);

179. return 1;

180. }

182. ‌LJLIB_ASM(rawget)                LJLIB_REC(.)

183. {

184. lj_lib_checktab(L, 1);

185. lj_lib_checkany(L, 2);

186. return FFH_UNREACHABLE;

187. }

189. ‌LJLIB_CF(rawset)                LJLIB_REC(.)

190. {

191. lj_lib_checktab(L, 1);

192. lj_lib_checkany(L, 2);

193. L->top = 1+lj_lib_checkany(L, 3);

194. lua_rawset(L, 1);

195. return 1;

196. }

198. ‌LJLIB_CF(rawequal)                LJLIB_REC(.)

199. {

200. cTValue *o1 = lj_lib_checkany(L, 1);

201. cTValue *o2 = lj_lib_checkany(L, 2);

202. setboolV(L->top-1, lj_obj_equal(o1, o2));

203. return 1;

204. }

206. #if LJ_52

207. ‌LJLIB_CF(rawlen)                LJLIB_REC(.)

208. {

209. cTValue *o = L->base;

210. int32_t len;

211. if (L->top > o && tvisstr(o))

212. len = (int32_t)strV(o)->len;

213. else

214. len = (int32_t)lj_tab_len(lj_lib_checktab(L, 1));

215. setintV(L->top-1, len);

216. return 1;

217. }

218. #endif

220. ‌LJLIB_CF(unpack)

221. {

222. GCtab *t = lj_lib_checktab(L, 1);

223. int32_t n, i = lj_lib_optint(L, 2, 1);

224. int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ?

225. lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t);

226. if (i > e) return 0;

227. n = e - i + 1;

228. if (n <= 0 || !lua_checkstack(L, n))

229. lj_err_caller(L, LJ_ERR_UNPACK);

230. do {

231. cTValue *tv = lj_tab_getint(t, i);

232. if (tv) {

233. copyTV(L, L->top++, tv);

234. } else {

235. setnilV(L->top++);

236. }

237. } while (i++ < e);

238. return n;

239. }

241. ‌LJLIB_CF(select)                LJLIB_REC(.)

242. {

243. int32_t n = (int32_t)(L->top - L->base);

244. if (n >= 1 && tvisstr(L->base) && *strVdata(L->base) == '#') {

245. setintV(L->top-1, n-1);

246. return 1;

247. } else {

248. int32_t i = lj_lib_checkint(L, 1);

249. if (i < 0) i = n + i; else if (i > n) i = n;

250. if (i < 1)

251. lj_err_arg(L, 1, LJ_ERR_IDXRNG);

252. return n - i;

253. }

254. }

256. /* -- Base library: conversions ------------------------------------------- */

258. ‌LJLIB_ASM(tonumber)                LJLIB_REC(.)

259. {

260. int32_t base = lj_lib_optint(L, 2, 10);

261. if (base == 10) {

262. TValue *o = lj_lib_checkany(L, 1);

263. if (lj_strscan_numberobj(o)) {

264. copyTV(L, L->base-1-LJ_FR2, o);

265. return FFH_RES(1);

266. }

267. #if LJ_HASFFI

268. if (tviscdata(o)) {

269. CTState *cts = ctype_cts(L);

270. CType *ct = lj_ctype_rawref(cts, cdataV(o)->ctypeid);

271. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);

272. if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {

273. if (LJ_DUALNUM && ctype_isinteger_or_bool(ct->info) &&

274. ct->size <= 4 && !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) {

275. int32_t i;

276. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, 0);

277. setintV(L->base-1-LJ_FR2, i);

278. return FFH_RES(1);

279. }

280. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_DOUBLE),

281. (uint8_t *)&(L->base-1-LJ_FR2)->n, o, 0);

282. return FFH_RES(1);

283. }

284. }

285. #endif

286. } else {

287. const char *p = strdata(lj_lib_checkstr(L, 1));

288. char *ep;

289. unsigned long ul;

290. if (base < 2 || base > 36)

291. lj_err_arg(L, 2, LJ_ERR_BASERNG);

292. ul = strtoul(p, &ep, base);

293. if (p != ep) {

294. while (lj_char_isspace((unsigned char)(*ep))) ep++;

295. if (*ep == '\0') {

296. if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u))

297. setintV(L->base-1-LJ_FR2, (int32_t)ul);

298. else

299. setnumV(L->base-1-LJ_FR2, (lua_Number)ul);

300. return FFH_RES(1);

301. }

302. }

303. }

304. setnilV(L->base-1-LJ_FR2);

305. return FFH_RES(1);

306. }

308. ‌LJLIB_ASM(tostring)                LJLIB_REC(.)

309. {

310. TValue *o = lj_lib_checkany(L, 1);

311. cTValue *mo;

312. L->top = o+1;  /* Only keep one argument. */

313. if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {

314. copyTV(L, L->base-1-LJ_FR2, mo);  /* Replace callable. */

315. return FFH_TAILCALL;

316. }

317. lj_gc_check(L);

318. setstrV(L, L->base-1-LJ_FR2, lj_strfmt_obj(L, L->base));

319. return FFH_RES(1);

320. }

322. /* -- Base library: throw and catch errors -------------------------------- */

324. ‌LJLIB_CF(error)

325. {

326. int32_t level = lj_lib_optint(L, 2, 1);

327. lua_settop(L, 1);

328. if (lua_isstring(L, 1) && level > 0) {

329. luaL_where(L, level);

330. lua_pushvalue(L, 1);

331. lua_concat(L, 2);

332. }

333. return lua_error(L);

334. }

336. ‌LJLIB_ASM(pcall)                LJLIB_REC(.)

337. {

338. lj_lib_checkany(L, 1);

339. lj_lib_checkfunc(L, 2);  /* For xpcall only. */

340. return FFH_UNREACHABLE;

341. }

342. LJLIB_ASM_(xpcall)                LJLIB_REC(.)

344. /* -- Base library: load Lua code ----------------------------------------- */

346. ‌static int load_aux(lua_State *L, int status, int envarg)

347. {

348. if (status == 0) {

349. if (tvistab(L->base+envarg-1)) {

350. GCfunc *fn = funcV(L->top-1);

351. GCtab *t = tabV(L->base+envarg-1);

352. setgcref(fn->c.env, obj2gco(t));

353. lj_gc_objbarrier(L, fn, t);

354. }

355. return 1;

356. } else {

357. setnilV(L->top-2);

358. return 2;

359. }

360. }

362. ‌LJLIB_CF(loadfile)

363. {

364. GCstr *fname = lj_lib_optstr(L, 1);

365. GCstr *mode = lj_lib_optstr(L, 2);

366. int status;

367. lua_settop(L, 3);  /* Ensure env arg exists. */

368. status = luaL_loadfilex(L, fname ? strdata(fname) : NULL,

369. mode ? strdata(mode) : NULL);

370. return load_aux(L, status, 3);

371. }

373. ‌static const char *reader_func(lua_State *L, void *ud, size_t *size)

374. {

375. UNUSED(ud);

376. luaL_checkstack(L, 2, "too many nested functions");

377. copyTV(L, L->top++, L->base);

378. lua_call(L, 0, 1);  /* Call user-supplied function. */

379. L->top--;

380. if (tvisnil(L->top)) {

381. *size = 0;

382. return NULL;

383. } else if (tvisstr(L->top) || tvisnumber(L->top)) {

384. copyTV(L, L->base+4, L->top);  /* Anchor string in reserved stack slot. */

385. return lua_tolstring(L, 5, size);

386. } else {

387. lj_err_caller(L, LJ_ERR_RDRSTR);

388. return NULL;

389. }

390. }

392. ‌LJLIB_CF(load)

393. {

394. GCstr *name = lj_lib_optstr(L, 2);

395. GCstr *mode = lj_lib_optstr(L, 3);

396. int status;

397. if (L->base < L->top && (tvisstr(L->base) || tvisnumber(L->base))) {

398. GCstr *s = lj_lib_checkstr(L, 1);

399. lua_settop(L, 4);  /* Ensure env arg exists. */

400. status = luaL_loadbufferx(L, strdata(s), s->len, strdata(name ? name : s),

401. mode ? strdata(mode) : NULL);

402. } else {

403. lj_lib_checkfunc(L, 1);

404. lua_settop(L, 5);  /* Reserve a slot for the string from the reader. */

405. status = lua_loadx(L, reader_func, NULL, name ? strdata(name) : "=(load)",

406. mode ? strdata(mode) : NULL);

407. }

408. return load_aux(L, status, 4);

409. }

411. ‌LJLIB_CF(loadstring)

412. {

413. return lj_cf_load(L);

414. }

416. ‌LJLIB_CF(dofile)

417. {

418. GCstr *fname = lj_lib_optstr(L, 1);

419. setnilV(L->top);

420. L->top = L->base+1;

421. if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != 0)

422. lua_error(L);

423. lua_call(L, 0, LUA_MULTRET);

424. return (int)(L->top - L->base) - 1;

425. }

427. /* -- Base library: GC control -------------------------------------------- */

429. ‌LJLIB_CF(gcinfo)

430. {

431. setintV(L->top++, (G(L)->gc.total >> 10));

432. return 1;

433. }

435. ‌LJLIB_CF(collectgarbage)

436. {

437. int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT,  /* ORDER LUA_GC* */

438. "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul");

439. int32_t data = lj_lib_optint(L, 2, 0);

440. if (opt == LUA_GCCOUNT) {

441. setnumV(L->top, (lua_Number)G(L)->gc.total/1024.0);

442. } else {

443. int res = lua_gc(L, opt, data);

444. if (opt == LUA_GCSTEP)

445. setboolV(L->top, res);

446. else

447. setintV(L->top, res);

448. }

449. L->top++;

450. return 1;

451. }

453. /* -- Base library: miscellaneous functions ------------------------------- */

455. LJLIB_PUSH(top-2)  /* Upvalue holds weak table. */

456. ‌LJLIB_CF(newproxy)

457. {

458. lua_settop(L, 1);

459. lua_newuserdata(L, 0);

460. if (lua_toboolean(L, 1) == 0) {  /* newproxy(): without metatable. */

461. return 1;

462. } else if (lua_isboolean(L, 1)) {  /* newproxy(true): with metatable. */

463. lua_newtable(L);

464. lua_pushvalue(L, -1);

465. lua_pushboolean(L, 1);

466. lua_rawset(L, lua_upvalueindex(1));  /* Remember mt in weak table. */

467. } else {  /* newproxy(proxy): inherit metatable. */

468. int validproxy = 0;

469. if (lua_getmetatable(L, 1)) {

470. lua_rawget(L, lua_upvalueindex(1));

471. validproxy = lua_toboolean(L, -1);

472. lua_pop(L, 1);

473. }

474. if (!validproxy)

475. lj_err_arg(L, 1, LJ_ERR_NOPROXY);

476. lua_getmetatable(L, 1);

477. }

478. lua_setmetatable(L, 2);

479. return 1;

480. }

482. LJLIB_PUSH("tostring")

483. ‌LJLIB_CF(print)

484. {

485. ptrdiff_t i, nargs = L->top - L->base;

486. cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1)));

487. int shortcut;

488. if (tv && !tvisnil(tv)) {

489. copyTV(L, L->top++, tv);

490. } else {

491. setstrV(L, L->top++, strV(lj_lib_upvalue(L, 1)));

492. lua_gettable(L, LUA_GLOBALSINDEX);

493. tv = L->top-1;

494. }

495. shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring);

496. for (i = 0; i < nargs; i++) {

497. cTValue *o = &L->base[i];

498. char buf[STRFMT_MAXBUF_NUM];

499. const char *str;

500. size_t size;

501. MSize len;

502. if (shortcut && (str = lj_strfmt_wstrnum(buf, o, &len)) != NULL) {

503. size = len;

504. } else {

505. copyTV(L, L->top+1, o);

506. copyTV(L, L->top, L->top-1);

507. L->top += 2;

508. lua_call(L, 1, 1);

509. str = lua_tolstring(L, -1, &size);

510. if (!str)

511. lj_err_caller(L, LJ_ERR_PRTOSTR);

512. L->top--;

513. }

514. if (i)

515. putchar('\t');

516. fwrite(str, 1, size, stdout);

517. }

518. putchar('\n');

519. return 0;

520. }

522. LJLIB_PUSH(top-3)

523. ‌LJLIB_SET(_VERSION)

525. #include "lj_libdef.h"

527. /* -- Coroutine library --------------------------------------------------- */

529. ‌#define LJLIB_MODULE_coroutine

531. LJLIB_CF(coroutine_status)

532. {

533. const char *s;

534. lua_State *co;

535. if (!(L->top > L->base && tvisthread(L->base)))

536. lj_err_arg(L, 1, LJ_ERR_NOCORO);

537. co = threadV(L->base);

538. if (co == L) s = "running";

539. else if (co->status == LUA_YIELD) s = "suspended";

540. else if (co->status != 0) s = "dead";

541. else if (co->base > tvref(co->stack)+1+LJ_FR2) s = "normal";

542. else if (co->top == co->base) s = "dead";

543. else s = "suspended";

544. lua_pushstring(L, s);

545. return 1;

546. }

548. ‌LJLIB_CF(coroutine_running)

549. {

550. #if LJ_52

551. int ismain = lua_pushthread(L);

552. setboolV(L->top++, ismain);

553. return 2;

554. #else

555. if (lua_pushthread(L))

556. setnilV(L->top++);

557. return 1;

558. #endif

559. }

561. ‌LJLIB_CF(coroutine_create)

562. {

563. lua_State *L1;

564. if (!(L->base < L->top && tvisfunc(L->base)))

565. lj_err_argt(L, 1, LUA_TFUNCTION);

566. L1 = lua_newthread(L);

567. setfuncV(L, L1->top++, funcV(L->base));

568. return 1;

569. }

571. ‌LJLIB_ASM(coroutine_yield)

572. {

573. lj_err_caller(L, LJ_ERR_CYIELD);

574. return FFH_UNREACHABLE;

575. }

577. ‌static int ffh_resume(lua_State *L, lua_State *co, int wrap)

578. {

579. if (co->cframe != NULL || co->status > LUA_YIELD ||

580. (co->status == 0 && co->top == co->base)) {

581. ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD;

582. if (wrap) lj_err_caller(L, em);

583. setboolV(L->base-1-LJ_FR2, 0);

584. setstrV(L, L->base-LJ_FR2, lj_err_str(L, em));

585. return FFH_RES(2);

586. }

587. lj_state_growstack(co, (MSize)(L->top - L->base));

588. return FFH_RETRY;

589. }

591. ‌LJLIB_ASM(coroutine_resume)

592. {

593. if (!(L->top > L->base && tvisthread(L->base)))

594. lj_err_arg(L, 1, LJ_ERR_NOCORO);

595. return ffh_resume(L, threadV(L->base), 0);

596. }

598. ‌LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux)

599. {

600. return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1);

601. }

603. /* Inline declarations. */

604. LJ_ASMF void lj_ff_coroutine_wrap_aux(void);

605. #if !(LJ_TARGET_MIPS && defined(ljamalg_c))

606. LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L,

607. lua_State *co);

608. #endif

610. /* Error handler, called from assembler VM. */

611. ‌void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co)

612. {

613. co->top--; copyTV(L, L->top, co->top); L->top++;

614. if (tvisstr(L->top-1))

615. lj_err_callermsg(L, strVdata(L->top-1));

616. else

617. lj_err_run(L);

618. }

620. /* Forward declaration. */

621. static void setpc_wrap_aux(lua_State *L, GCfunc *fn);

623. ‌LJLIB_CF(coroutine_wrap)

624. {

625. GCfunc *fn;

626. lj_cf_coroutine_create(L);

627. fn = lj_lib_pushcc(L, lj_ffh_coroutine_wrap_aux, FF_coroutine_wrap_aux, 1);

628. setpc_wrap_aux(L, fn);

629. return 1;

630. }

632. #include "lj_libdef.h"

634. /* Fix the PC of wrap_aux. Really ugly workaround. */

635. ‌static void setpc_wrap_aux(lua_State *L, GCfunc *fn)

636. {

637. setmref(fn->c.pc, &L2GG(L)->bcff[lj_lib_init_coroutine[1]+2]);

638. }

640. /* ------------------------------------------------------------------------ */

642. ‌static void newproxy_weaktable(lua_State *L)

643. {

644. /* NOBARRIER: The table is new (marked white). */

645. GCtab *t = lj_tab_new(L, 0, 1);

646. settabV(L, L->top++, t);

647. setgcref(t->metatable, obj2gco(t));

648. setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),

649. lj_str_newlit(L, "kv"));

650. t->nomm = (uint8_t)(~(1u<<MM_mode));

651. }

653. ‌LUALIB_API int luaopen_base(lua_State *L)

654. {

655. /* NOBARRIER: Table and value are the same. */

656. GCtab *env = tabref(L->env);

657. settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env);

658. lua_pushliteral(L, LUA_VERSION);  /* top-3. */

659. newproxy_weaktable(L);  /* top-2. */

660. LJ_LIB_REG(L, "_G", base);

661. LJ_LIB_REG(L, LUA_COLIBNAME, coroutine);

662. return 2;

663. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_lib.h - luajit-2.0-src

Data types defined

- RandomState

Macros defined

- FFH_RES

- FFH_RETRY

- FFH_TAILCALL

- FFH_UNREACHABLE

- LIBINIT_ASM

- LIBINIT_ASM_

- LIBINIT_CF

- LIBINIT_COPY

- LIBINIT_END

- LIBINIT_FFID

- LIBINIT_LASTCL

- LIBINIT_LENMASK

- LIBINIT_LUA

- LIBINIT_MAXSTR

- LIBINIT_NUMBER

- LIBINIT_SET

- LIBINIT_STRING

- LIBINIT_TAGMASK

- LJLIB_ASM

- LJLIB_ASM_

- LJLIB_CF

- LJLIB_LUA

- LJLIB_NOREG

- LJLIB_NOREGUV

- LJLIB_PUSH

- LJLIB_REC

- LJLIB_SET

- LJ_LIB_REG

- _LJ_LIB_H

- lj_lib_checkfpu

- lj_lib_checkfpu

- lj_lib_checknumber

- lj_lib_pushcf

- lj_lib_upvalue

- lj_lib_upvalue

- lj_lib_upvalue

Source code

1. /*

2. ** Library function support.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_LIB_H

7. ‌#define _LJ_LIB_H

9. #include "lj_obj.h"

11. /*

12. ** A fallback handler is called by the assembler VM if the fast path fails:

13. **

14. ** - too few arguments:   unrecoverable.

15. ** - wrong argument type:   recoverable, if coercion succeeds.

16. ** - bad argument value:  unrecoverable.

17. ** - stack overflow:        recoverable, if stack reallocation succeeds.

18. ** - extra handling:        recoverable.

19. **

20. ** The unrecoverable cases throw an error with lj_err_arg(), lj_err_argtype(),

21. ** lj_err_caller() or lj_err_callermsg().

22. ** The recoverable cases return 0 or the number of results + 1.

23. ** The assembler VM retries the fast path only if 0 is returned.

24. ** This time the fallback must not be called again or it gets stuck in a loop.

25. */

27. /* Return values from fallback handler. */

28. ‌#define FFH_RETRY        0

29. ‌#define FFH_UNREACHABLE        FFH_RETRY

30. ‌#define FFH_RES(n)        ((n)+1)

31. ‌#define FFH_TAILCALL        (-1)

33. LJ_FUNC TValue *lj_lib_checkany(lua_State *L, int narg);

34. LJ_FUNC GCstr *lj_lib_checkstr(lua_State *L, int narg);

35. LJ_FUNC GCstr *lj_lib_optstr(lua_State *L, int narg);

36. #if LJ_DUALNUM

37. LJ_FUNC void lj_lib_checknumber(lua_State *L, int narg);

38. #else

39. ‌#define lj_lib_checknumber(L, narg)        lj_lib_checknum((L), (narg))

40. #endif

41. LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg);

42. LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg);

43. LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def);

44. LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg);

45. LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg);

46. LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg);

47. LJ_FUNC int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst);

49. /* Avoid including lj_frame.h. */

50. #if LJ_GC64

51. ‌#define lj_lib_upvalue(L, n) \

52. (&gcval(L->base-2)->fn.c.upvalue[(n)-1])

53. #elif LJ_FR2

54. ‌#define lj_lib_upvalue(L, n) \

55. (&gcref((L->base-2)->gcr)->fn.c.upvalue[(n)-1])

56. #else

57. ‌#define lj_lib_upvalue(L, n) \

58. (&gcref((L->base-1)->fr.func)->fn.c.upvalue[(n)-1])

59. #endif

61. #if LJ_TARGET_WINDOWS

62. ‌#define lj_lib_checkfpu(L) \

63. do { setnumV(L->top++, (lua_Number)1437217655); \

64. if (lua_tointeger(L, -1) != 1437217655) lj_err_caller(L, LJ_ERR_BADFPU); \

65. L->top--; } while (0)

66. #else

67. ‌#define lj_lib_checkfpu(L)        UNUSED(L)

68. #endif

70. LJ_FUNC GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n);

71. ‌#define lj_lib_pushcf(L, fn, id)        (lj_lib_pushcc(L, (fn), (id), 0))

73. /* Library function declarations. Scanned by buildvm. */

74. ‌#define LJLIB_CF(name)                static int lj_cf_##name(lua_State *L)

75. ‌#define LJLIB_ASM(name)                static int lj_ffh_##name(lua_State *L)

76. ‌#define LJLIB_ASM_(name)

77. ‌#define LJLIB_LUA(name)

78. ‌#define LJLIB_SET(name)

79. ‌#define LJLIB_PUSH(arg)

80. ‌#define LJLIB_REC(handler)

81. ‌#define LJLIB_NOREGUV

82. ‌#define LJLIB_NOREG

84. ‌#define LJ_LIB_REG(L, regname, name) \

85. lj_lib_register(L, regname, lj_lib_init_##name, lj_lib_cf_##name)

87. LJ_FUNC void lj_lib_register(lua_State *L, const char *libname,

88. const uint8_t *init, const lua_CFunction *cf);

89. LJ_FUNC void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f,

90. GCtab *env);

91. LJ_FUNC int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id,

92. const char *name);

94. /* Library init data tags. */

95. ‌#define LIBINIT_LENMASK        0x3f

96. ‌#define LIBINIT_TAGMASK        0xc0

97. ‌#define LIBINIT_CF        0x00

98. ‌#define LIBINIT_ASM        0x40

99. ‌#define LIBINIT_ASM_        0x80

100. ‌#define LIBINIT_STRING        0xc0

101. ‌#define LIBINIT_MAXSTR        0x38

102. ‌#define LIBINIT_LUA        0xf9

103. ‌#define LIBINIT_SET        0xfa

104. ‌#define LIBINIT_NUMBER        0xfb

105. ‌#define LIBINIT_COPY        0xfc

106. ‌#define LIBINIT_LASTCL        0xfd

107. ‌#define LIBINIT_FFID        0xfe

108. ‌#define LIBINIT_END        0xff

110. /* Exported library functions. */

112. ‌typedef struct RandomState RandomState;

113. LJ_FUNC uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs);

115. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_lib.c - luajit-2.0-src

Functions defined

- lib_create_table

- lib_read_lfunc

- lj_lib_checkany

- lj_lib_checkfunc

- lj_lib_checkint

- lj_lib_checknum

- lj_lib_checknumber

- lj_lib_checkopt

- lj_lib_checkstr

- lj_lib_checktab

- lj_lib_checktabornil

- lj_lib_optint

- lj_lib_optstr

- lj_lib_postreg

- lj_lib_prereg

- lj_lib_pushcc

- lj_lib_register

Macros defined

- LUA_CORE

- lj_lib_c

Source code

1. /*

2. ** Library function support.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_lib_c

7. ‌#define LUA_CORE

9. #include "lauxlib.h"

11. #include "lj_obj.h"

12. #include "lj_gc.h"

13. #include "lj_err.h"

14. #include "lj_str.h"

15. #include "lj_tab.h"

16. #include "lj_func.h"

17. #include "lj_bc.h"

18. #include "lj_dispatch.h"

19. #include "lj_vm.h"

20. #include "lj_strscan.h"

21. #include "lj_strfmt.h"

22. #include "lj_lex.h"

23. #include "lj_bcdump.h"

24. #include "lj_lib.h"

26. /* -- Library initialization ---------------------------------------------- */

28. ‌static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize)

29. {

30. if (libname) {

31. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);

32. lua_getfield(L, -1, libname);

33. if (!tvistab(L->top-1)) {

34. L->top--;

35. if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, hsize) != NULL)

36. lj_err_callerv(L, LJ_ERR_BADMODN, libname);

37. settabV(L, L->top, tabV(L->top-1));

38. L->top++;

39. lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */

40. }

41. L->top--;

42. settabV(L, L->top-1, tabV(L->top));

43. } else {

44. lua_createtable(L, 0, hsize);

45. }

46. return tabV(L->top-1);

47. }

49. ‌static const uint8_t *lib_read_lfunc(lua_State *L, const uint8_t *p, GCtab *tab)

50. {

51. int len = *p++;

52. GCstr *name = lj_str_new(L, (const char *)p, len);

53. LexState ls;

54. GCproto *pt;

55. GCfunc *fn;

56. memset(&ls, 0, sizeof(ls));

57. ls.L = L;

58. ls.p = (const char *)(p+len);

59. ls.pe = (const char *)~(uintptr_t)0;

60. ls.c = -1;

61. ls.level = (BCDUMP_F_STRIP|(LJ_BE*BCDUMP_F_BE));

62. ls.chunkname = name;

63. pt = lj_bcread_proto(&ls);

64. pt->firstline = ~(BCLine)0;

65. fn = lj_func_newL_empty(L, pt, tabref(L->env));

66. /* NOBARRIER: See below for common barrier. */

67. setfuncV(L, lj_tab_setstr(L, tab, name), fn);

68. return (const uint8_t *)ls.p;

69. }

71. ‌void lj_lib_register(lua_State *L, const char *libname,

72. const uint8_t *p, const lua_CFunction *cf)

73. {

74. GCtab *env = tabref(L->env);

75. GCfunc *ofn = NULL;

76. int ffid = *p++;

77. BCIns *bcff = &L2GG(L)->bcff[*p++];

78. GCtab *tab = lib_create_table(L, libname, *p++);

79. ptrdiff_t tpos = L->top - L->base;

81. /* Avoid barriers further down. */

82. lj_gc_anybarriert(L, tab);

83. tab->nomm = 0;

85. for (;;) {

86. uint32_t tag = *p++;

87. MSize len = tag & LIBINIT_LENMASK;

88. tag &= LIBINIT_TAGMASK;

89. if (tag != LIBINIT_STRING) {

90. const char *name;

91. MSize nuv = (MSize)(L->top - L->base - tpos);

92. GCfunc *fn = lj_func_newC(L, nuv, env);

93. if (nuv) {

94. L->top = L->base + tpos;

95. memcpy(fn->c.upvalue, L->top, sizeof(TValue)*nuv);

96. }

97. fn->c.ffid = (uint8_t)(ffid++);

98. name = (const char *)p;

99. p += len;

100. if (tag == LIBINIT_CF)

101. setmref(fn->c.pc, &G(L)->bc_cfunc_int);

102. else

103. setmref(fn->c.pc, bcff++);

104. if (tag == LIBINIT_ASM_)

105. fn->c.f = ofn->c.f;  /* Copy handler from previous function. */

106. else

107. fn->c.f = *cf++;  /* Get cf or handler from C function table. */

108. if (len) {

109. /* NOBARRIER: See above for common barrier. */

110. setfuncV(L, lj_tab_setstr(L, tab, lj_str_new(L, name, len)), fn);

111. }

112. ofn = fn;

113. } else {

114. switch (tag | len) {

115. case LIBINIT_LUA:

116. p = lib_read_lfunc(L, p, tab);

117. break;

118. case LIBINIT_SET:

119. L->top -= 2;

120. if (tvisstr(L->top+1) && strV(L->top+1)->len == 0)

121. env = tabV(L->top);

122. else  /* NOBARRIER: See above for common barrier. */

123. copyTV(L, lj_tab_set(L, tab, L->top+1), L->top);

124. break;

125. case LIBINIT_NUMBER:

126. memcpy(&L->top->n, p, sizeof(double));

127. L->top++;

128. p += sizeof(double);

129. break;

130. case LIBINIT_COPY:

131. copyTV(L, L->top, L->top - *p++);

132. L->top++;

133. break;

134. case LIBINIT_LASTCL:

135. setfuncV(L, L->top++, ofn);

136. break;

137. case LIBINIT_FFID:

138. ffid++;

139. break;

140. case LIBINIT_END:

141. return;

142. default:

143. setstrV(L, L->top++, lj_str_new(L, (const char *)p, len));

144. p += len;

145. break;

146. }

147. }

148. }

149. }

151. /* Push internal function on the stack. */

152. ‌GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n)

153. {

154. GCfunc *fn;

155. lua_pushcclosure(L, f, n);

156. fn = funcV(L->top-1);

157. fn->c.ffid = (uint8_t)id;

158. setmref(fn->c.pc, &G(L)->bc_cfunc_int);

159. return fn;

160. }

162. ‌void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env)

163. {

164. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);

165. lua_pushcfunction(L, f);

166. /* NOBARRIER: The function is new (marked white). */

167. setgcref(funcV(L->top-1)->c.env, obj2gco(env));

168. lua_setfield(L, -2, name);

169. L->top--;

170. }

172. ‌int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id, const char *name)

173. {

174. GCfunc *fn = lj_lib_pushcf(L, cf, id);

175. GCtab *t = tabref(curr_func(L)->c.env);  /* Reference to parent table. */

176. setfuncV(L, lj_tab_setstr(L, t, lj_str_newz(L, name)), fn);

177. lj_gc_anybarriert(L, t);

178. setfuncV(L, L->top++, fn);

179. return 1;

180. }

182. /* -- Type checks --------------------------------------------------------- */

184. ‌TValue *lj_lib_checkany(lua_State *L, int narg)

185. {

186. TValue *o = L->base + narg-1;

187. if (o >= L->top)

188. lj_err_arg(L, narg, LJ_ERR_NOVAL);

189. return o;

190. }

192. ‌GCstr *lj_lib_checkstr(lua_State *L, int narg)

193. {

194. TValue *o = L->base + narg-1;

195. if (o < L->top) {

196. if (LJ_LIKELY(tvisstr(o))) {

197. return strV(o);

198. } else if (tvisnumber(o)) {

199. GCstr *s = lj_strfmt_number(L, o);

200. setstrV(L, o, s);

201. return s;

202. }

203. }

204. lj_err_argt(L, narg, LUA_TSTRING);

205. return NULL;  /* unreachable */

206. }

208. ‌GCstr *lj_lib_optstr(lua_State *L, int narg)

209. {

210. TValue *o = L->base + narg-1;

211. return (o < L->top && !tvisnil(o)) ? lj_lib_checkstr(L, narg) : NULL;

212. }

214. #if LJ_DUALNUM

215. ‌void lj_lib_checknumber(lua_State *L, int narg)

216. {

217. TValue *o = L->base + narg-1;

218. if (!(o < L->top && lj_strscan_numberobj(o)))

219. lj_err_argt(L, narg, LUA_TNUMBER);

220. }

221. #endif

223. ‌lua_Number lj_lib_checknum(lua_State *L, int narg)

224. {

225. TValue *o = L->base + narg-1;

226. if (!(o < L->top &&

227. (tvisnumber(o) || (tvisstr(o) && lj_strscan_num(strV(o), o)))))

228. lj_err_argt(L, narg, LUA_TNUMBER);

229. if (LJ_UNLIKELY(tvisint(o))) {

230. lua_Number n = (lua_Number)intV(o);

231. setnumV(o, n);

232. return n;

233. } else {

234. return numV(o);

235. }

236. }

238. ‌int32_t lj_lib_checkint(lua_State *L, int narg)

239. {

240. TValue *o = L->base + narg-1;

241. if (!(o < L->top && lj_strscan_numberobj(o)))

242. lj_err_argt(L, narg, LUA_TNUMBER);

243. if (LJ_LIKELY(tvisint(o))) {

244. return intV(o);

245. } else {

246. int32_t i = lj_num2int(numV(o));

247. if (LJ_DUALNUM) setintV(o, i);

248. return i;

249. }

250. }

252. ‌int32_t lj_lib_optint(lua_State *L, int narg, int32_t def)

253. {

254. TValue *o = L->base + narg-1;

255. return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def;

256. }

258. ‌GCfunc *lj_lib_checkfunc(lua_State *L, int narg)

259. {

260. TValue *o = L->base + narg-1;

261. if (!(o < L->top && tvisfunc(o)))

262. lj_err_argt(L, narg, LUA_TFUNCTION);

263. return funcV(o);

264. }

266. ‌GCtab *lj_lib_checktab(lua_State *L, int narg)

267. {

268. TValue *o = L->base + narg-1;

269. if (!(o < L->top && tvistab(o)))

270. lj_err_argt(L, narg, LUA_TTABLE);

271. return tabV(o);

272. }

274. ‌GCtab *lj_lib_checktabornil(lua_State *L, int narg)

275. {

276. TValue *o = L->base + narg-1;

277. if (o < L->top) {

278. if (tvistab(o))

279. return tabV(o);

280. else if (tvisnil(o))

281. return NULL;

282. }

283. lj_err_arg(L, narg, LJ_ERR_NOTABN);

284. return NULL;  /* unreachable */

285. }

287. ‌int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst)

288. {

289. GCstr *s = def >= 0 ? lj_lib_optstr(L, narg) : lj_lib_checkstr(L, narg);

290. if (s) {

291. const char *opt = strdata(s);

292. MSize len = s->len;

293. int i;

294. for (i = 0; *(const uint8_t *)lst; i++) {

295. if (*(const uint8_t *)lst == len && memcmp(opt, lst+1, len) == 0)

296. return i;

297. lst += 1+*(const uint8_t *)lst;

298. }

299. lj_err_argv(L, narg, LJ_ERR_INVOPTM, opt);

300. }

301. return def;

302. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_aux.c - luajit-2.0-src

Functions defined

- adjuststack

- emptybuffer

- libsize

- luaL_addlstring

- luaL_addstring

- luaL_addvalue

- luaL_buffinit

- luaL_execresult

- luaL_fileresult

- luaL_findtable

- luaL_gsub

- luaL_newstate

- luaL_newstate

- luaL_openlib

- luaL_prepbuffer

- luaL_pushresult

- luaL_ref

- luaL_register

- luaL_unref

- lua_newstate

- mem_alloc

- panic

Macros defined

- FREELIST_REF

- LUA_LIB

- abs_index

- bufffree

- bufflen

- lib_aux_c

Source code

1. /*

2. ** Auxiliary library for the Lua/C API.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major parts taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. #include <errno.h>

10. #include <stdarg.h>

11. #include <stdio.h>

13. ‌#define lib_aux_c

14. ‌#define LUA_LIB

16. #include "lua.h"

17. #include "lauxlib.h"

19. #include "lj_obj.h"

20. #include "lj_err.h"

21. #include "lj_state.h"

22. #include "lj_trace.h"

23. #include "lj_lib.h"

25. #if LJ_TARGET_POSIX

26. #include <sys/wait.h>

27. #endif

29. /* -- I/O error handling -------------------------------------------------- */

31. ‌LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname)

32. {

33. if (stat) {

34. setboolV(L->top++, 1);

35. return 1;

36. } else {

37. int en = errno;  /* Lua API calls may change this value. */

38. setnilV(L->top++);

39. if (fname)

40. lua_pushfstring(L, "%s: %s", fname, strerror(en));

41. else

42. lua_pushfstring(L, "%s", strerror(en));

43. setintV(L->top++, en);

44. lj_trace_abort(G(L));

45. return 3;

46. }

47. }

49. ‌LUALIB_API int luaL_execresult(lua_State *L, int stat)

50. {

51. if (stat != -1) {

52. #if LJ_TARGET_POSIX

53. if (WIFSIGNALED(stat)) {

54. stat = WTERMSIG(stat);

55. setnilV(L->top++);

56. lua_pushliteral(L, "signal");

57. } else {

58. if (WIFEXITED(stat))

59. stat = WEXITSTATUS(stat);

60. if (stat == 0)

61. setboolV(L->top++, 1);

62. else

63. setnilV(L->top++);

64. lua_pushliteral(L, "exit");

65. }

66. #else

67. if (stat == 0)

68. setboolV(L->top++, 1);

69. else

70. setnilV(L->top++);

71. lua_pushliteral(L, "exit");

72. #endif

73. setintV(L->top++, stat);

74. return 3;

75. }

76. return luaL_fileresult(L, 0, NULL);

77. }

79. /* -- Module registration ------------------------------------------------- */

81. ‌LUALIB_API const char *luaL_findtable(lua_State *L, int idx,

82. const char *fname, int szhint)

83. {

84. const char *e;

85. lua_pushvalue(L, idx);

86. do {

87. e = strchr(fname, '.');

88. if (e == NULL) e = fname + strlen(fname);

89. lua_pushlstring(L, fname, (size_t)(e - fname));

90. lua_rawget(L, -2);

91. if (lua_isnil(L, -1)) {  /* no such field? */

92. lua_pop(L, 1);  /* remove this nil */

93. lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */

94. lua_pushlstring(L, fname, (size_t)(e - fname));

95. lua_pushvalue(L, -2);

96. lua_settable(L, -4);  /* set new table into field */

97. } else if (!lua_istable(L, -1)) {  /* field has a non-table value? */

98. lua_pop(L, 2);  /* remove table and value */

99. return fname;  /* return problematic part of the name */

100. }

101. lua_remove(L, -2);  /* remove previous table */

102. fname = e + 1;

103. } while (*e == '.');

104. return NULL;

105. }

107. ‌static int libsize(const luaL_Reg *l)

108. {

109. int size = 0;

110. for (; l->name; l++) size++;

111. return size;

112. }

114. ‌LUALIB_API void luaL_openlib(lua_State *L, const char *libname,

115. const luaL_Reg *l, int nup)

116. {

117. lj_lib_checkfpu(L);

118. if (libname) {

119. int size = libsize(l);

120. /* check whether lib already exists */

121. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);

122. lua_getfield(L, -1, libname);  /* get _LOADED[libname] */

123. if (!lua_istable(L, -1)) {  /* not found? */

124. lua_pop(L, 1);  /* remove previous result */

125. /* try global variable (and create one if it does not exist) */

126. if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)

127. lj_err_callerv(L, LJ_ERR_BADMODN, libname);

128. lua_pushvalue(L, -1);

129. lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */

130. }

131. lua_remove(L, -2);  /* remove _LOADED table */

132. lua_insert(L, -(nup+1));  /* move library table to below upvalues */

133. }

134. for (; l->name; l++) {

135. int i;

136. for (i = 0; i < nup; i++)  /* copy upvalues to the top */

137. lua_pushvalue(L, -nup);

138. lua_pushcclosure(L, l->func, nup);

139. lua_setfield(L, -(nup+2), l->name);

140. }

141. lua_pop(L, nup);  /* remove upvalues */

142. }

144. ‌LUALIB_API void luaL_register(lua_State *L, const char *libname,

145. const luaL_Reg *l)

146. {

147. luaL_openlib(L, libname, l, 0);

148. }

150. ‌LUALIB_API const char *luaL_gsub(lua_State *L, const char *s,

151. const char *p, const char *r)

152. {

153. const char *wild;

154. size_t l = strlen(p);

155. luaL_Buffer b;

156. luaL_buffinit(L, &b);

157. while ((wild = strstr(s, p)) != NULL) {

158. luaL_addlstring(&b, s, (size_t)(wild - s));  /* push prefix */

159. luaL_addstring(&b, r);  /* push replacement in place of pattern */

160. s = wild + l;  /* continue after `p' */

161. }

162. luaL_addstring(&b, s);  /* push last suffix */

163. luaL_pushresult(&b);

164. return lua_tostring(L, -1);

165. }

167. /* -- Buffer handling ----------------------------------------------------- */

169. ‌#define bufflen(B)        ((size_t)((B)->p - (B)->buffer))

170. ‌#define bufffree(B)        ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))

172. ‌static int emptybuffer(luaL_Buffer *B)

173. {

174. size_t l = bufflen(B);

175. if (l == 0)

176. return 0;  /* put nothing on stack */

177. lua_pushlstring(B->L, B->buffer, l);

178. B->p = B->buffer;

179. B->lvl++;

180. return 1;

181. }

183. ‌static void adjuststack(luaL_Buffer *B)

184. {

185. if (B->lvl > 1) {

186. lua_State *L = B->L;

187. int toget = 1;  /* number of levels to concat */

188. size_t toplen = lua_strlen(L, -1);

189. do {

190. size_t l = lua_strlen(L, -(toget+1));

191. if (!(B->lvl - toget + 1 >= LUA_MINSTACK/2 || toplen > l))

192. break;

193. toplen += l;

194. toget++;

195. } while (toget < B->lvl);

196. lua_concat(L, toget);

197. B->lvl = B->lvl - toget + 1;

198. }

199. }

201. ‌LUALIB_API char *luaL_prepbuffer(luaL_Buffer *B)

202. {

203. if (emptybuffer(B))

204. adjuststack(B);

205. return B->buffer;

206. }

208. ‌LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)

209. {

210. while (l--)

211. luaL_addchar(B, *s++);

212. }

214. ‌LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s)

215. {

216. luaL_addlstring(B, s, strlen(s));

217. }

219. ‌LUALIB_API void luaL_pushresult(luaL_Buffer *B)

220. {

221. emptybuffer(B);

222. lua_concat(B->L, B->lvl);

223. B->lvl = 1;

224. }

226. ‌LUALIB_API void luaL_addvalue(luaL_Buffer *B)

227. {

228. lua_State *L = B->L;

229. size_t vl;

230. const char *s = lua_tolstring(L, -1, &vl);

231. if (vl <= bufffree(B)) {  /* fit into buffer? */

232. memcpy(B->p, s, vl);  /* put it there */

233. B->p += vl;

234. lua_pop(L, 1);  /* remove from stack */

235. } else {

236. if (emptybuffer(B))

237. lua_insert(L, -2);  /* put buffer before new value */

238. B->lvl++;  /* add new value into B stack */

239. adjuststack(B);

240. }

241. }

243. ‌LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)

244. {

245. B->L = L;

246. B->p = B->buffer;

247. B->lvl = 0;

248. }

250. /* -- Reference management ------------------------------------------------ */

252. ‌#define FREELIST_REF        0

254. /* Convert a stack index to an absolute index. */

255. ‌#define abs_index(L, i) \

256. ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1)

258. ‌LUALIB_API int luaL_ref(lua_State *L, int t)

259. {

260. int ref;

261. t = abs_index(L, t);

262. if (lua_isnil(L, -1)) {

263. lua_pop(L, 1);  /* remove from stack */

264. return LUA_REFNIL;  /* `nil' has a unique fixed reference */

265. }

266. lua_rawgeti(L, t, FREELIST_REF);  /* get first free element */

267. ref = (int)lua_tointeger(L, -1);  /* ref = t[FREELIST_REF] */

268. lua_pop(L, 1);  /* remove it from stack */

269. if (ref != 0) {  /* any free element? */

270. lua_rawgeti(L, t, ref);  /* remove it from list */

271. lua_rawseti(L, t, FREELIST_REF);  /* (t[FREELIST_REF] = t[ref]) */

272. } else {  /* no free elements */

273. ref = (int)lua_objlen(L, t);

274. ref++;  /* create new reference */

275. }

276. lua_rawseti(L, t, ref);

277. return ref;

278. }

280. ‌LUALIB_API void luaL_unref(lua_State *L, int t, int ref)

281. {

282. if (ref >= 0) {

283. t = abs_index(L, t);

284. lua_rawgeti(L, t, FREELIST_REF);

285. lua_rawseti(L, t, ref);  /* t[ref] = t[FREELIST_REF] */

286. lua_pushinteger(L, ref);

287. lua_rawseti(L, t, FREELIST_REF);  /* t[FREELIST_REF] = ref */

288. }

289. }

291. /* -- Default allocator and panic function -------------------------------- */

293. ‌static int panic(lua_State *L)

294. {

295. const char *s = lua_tostring(L, -1);

296. fputs("PANIC: unprotected error in call to Lua API (", stderr);

297. fputs(s ? s : "?", stderr);

298. fputc(')', stderr); fputc('\n', stderr);

299. fflush(stderr);

300. return 0;

301. }

303. #ifdef LUAJIT_USE_SYSMALLOC

305. #if LJ_64 && !defined(LUAJIT_USE_VALGRIND)

306. #error "Must use builtin allocator for 64 bit target"

307. #endif

309. ‌static void *mem_alloc(void *ud, void *ptr, size_t osize, size_t nsize)

310. {

311. (void)ud;

312. (void)osize;

313. if (nsize == 0) {

314. free(ptr);

315. return NULL;

316. } else {

317. return realloc(ptr, nsize);

318. }

319. }

321. ‌LUALIB_API lua_State *luaL_newstate(void)

322. {

323. lua_State *L = lua_newstate(mem_alloc, NULL);

324. if (L) G(L)->panic = panic;

325. return L;

326. }

328. #else

330. #include "lj_alloc.h"

332. ‌LUALIB_API lua_State *luaL_newstate(void)

333. {

334. lua_State *L;

335. void *ud = lj_alloc_create();

336. if (ud == NULL) return NULL;

337. #if LJ_64

338. L = lj_state_newstate(lj_alloc_f, ud);

339. #else

340. L = lua_newstate(lj_alloc_f, ud);

341. #endif

342. if (L) G(L)->panic = panic;

343. return L;

344. }

346. #if LJ_64

347. ‌LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)

348. {

349. UNUSED(f); UNUSED(ud);

350. fputs("Must use luaL_newstate() for 64 bit target\n", stderr);

351. return NULL;

352. }

353. #endif

355. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_trace.h - luajit-2.0-src

Data types defined

- TraceError

Macros defined

- TREDEF

- _LJ_TRACE_H

- lj_trace_abort

- lj_trace_abort

- lj_trace_end

- lj_trace_end

- lj_trace_flushall

- lj_trace_freestate

- lj_trace_initstate

Source code

1. /*

2. ** Trace management.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_TRACE_H

7. ‌#define _LJ_TRACE_H

9. #include "lj_obj.h"

11. #if LJ_HASJIT

12. #include "lj_jit.h"

13. #include "lj_dispatch.h"

15. /* Trace errors. */

16. typedef enum {

17. ‌#define TREDEF(name, msg)        LJ_TRERR_##name,

18. #include "lj_traceerr.h"

19. LJ_TRERR__MAX

20. ‌} TraceError;

22. LJ_FUNC_NORET void lj_trace_err(jit_State *J, TraceError e);

23. LJ_FUNC_NORET void lj_trace_err_info(jit_State *J, TraceError e);

25. /* Trace management. */

26. LJ_FUNC void LJ_FASTCALL lj_trace_free(global_State *g, GCtrace *T);

27. LJ_FUNC void lj_trace_reenableproto(GCproto *pt);

28. LJ_FUNC void lj_trace_flushproto(global_State *g, GCproto *pt);

29. LJ_FUNC void lj_trace_flush(jit_State *J, TraceNo traceno);

30. LJ_FUNC int lj_trace_flushall(lua_State *L);

31. LJ_FUNC void lj_trace_initstate(global_State *g);

32. LJ_FUNC void lj_trace_freestate(global_State *g);

34. /* Event handling. */

35. LJ_FUNC void lj_trace_ins(jit_State *J, const BCIns *pc);

36. LJ_FUNCA void LJ_FASTCALL lj_trace_hot(jit_State *J, const BCIns *pc);

37. LJ_FUNCA void LJ_FASTCALL lj_trace_stitch(jit_State *J, const BCIns *pc);

38. LJ_FUNCA int LJ_FASTCALL lj_trace_exit(jit_State *J, void *exptr);

40. /* Signal asynchronous abort of trace or end of trace. */

41. ‌#define lj_trace_abort(g)        (G2J(g)->state &= ~LJ_TRACE_ACTIVE)

42. ‌#define lj_trace_end(J)                (J->state = LJ_TRACE_END)

44. #else

46. ‌#define lj_trace_flushall(L)        (UNUSED(L), 0)

47. ‌#define lj_trace_initstate(g)        UNUSED(g)

48. ‌#define lj_trace_freestate(g)        UNUSED(g)

49. ‌#define lj_trace_abort(g)        UNUSED(g)

50. ‌#define lj_trace_end(J)                UNUSED(J)

52. #endif

54. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lauxlib.h - luajit-2.0-src

Data types defined

- luaL_Buffer

- luaL_Buffer

- luaL_Reg

- luaL_Reg

Macros defined

- LUA_ERRFILE

- LUA_NOREF

- LUA_REFNIL

- lauxlib_h

- luaL_addchar

- luaL_addsize

- luaL_argcheck

- luaL_checkint

- luaL_checklong

- luaL_checkstring

- luaL_dofile

- luaL_dostring

- luaL_getmetatable

- luaL_getn

- luaL_opt

- luaL_optint

- luaL_optlong

- luaL_optstring

- luaL_putchar

- luaL_reg

- luaL_setn

- luaL_typename

- lua_getref

- lua_ref

- lua_unref

Source code

1. /*

2. ** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $

3. ** Auxiliary functions for building Lua libraries

4. ** See Copyright Notice in lua.h

5. */

8. #ifndef lauxlib_h

9. ‌#define lauxlib_h

12. #include <stddef.h>

13. #include <stdio.h>

15. #include "lua.h"

18. ‌#define luaL_getn(L,i)          ((int)lua_objlen(L, i))

19. ‌#define luaL_setn(L,i,j)        ((void)0)  /* no op! */

21. /* extra error code for `luaL_load' */

22. ‌#define LUA_ERRFILE     (LUA_ERRERR+1)

24. ‌typedef struct luaL_Reg {

25. const char *name;

26. lua_CFunction func;

27. ‌} luaL_Reg;

29. LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,

30. const luaL_Reg *l, int nup);

31. LUALIB_API void (luaL_register) (lua_State *L, const char *libname,

32. const luaL_Reg *l);

33. LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);

34. LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);

35. LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);

36. LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg);

37. LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg,

38. size_t *l);

39. LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg,

40. const char *def, size_t *l);

41. LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg);

42. LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def);

44. LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);

45. LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,

46. lua_Integer def);

48. LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);

49. LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);

50. LUALIB_API void (luaL_checkany) (lua_State *L, int narg);

52. LUALIB_API int   (luaL_newmetatable) (lua_State *L, const char *tname);

53. LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);

55. LUALIB_API void (luaL_where) (lua_State *L, int lvl);

56. LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);

58. LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,

59. const char *const lst[]);

61. LUALIB_API int (luaL_ref) (lua_State *L, int t);

62. LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);

64. LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);

65. LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,

66. const char *name);

67. LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);

69. LUALIB_API lua_State *(luaL_newstate) (void);

72. LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,

73. const char *r);

75. LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,

76. const char *fname, int szhint);

78. /* From Lua 5.2. */

79. LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname);

80. LUALIB_API int luaL_execresult(lua_State *L, int stat);

81. LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename,

82. const char *mode);

83. LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,

84. const char *name, const char *mode);

85. LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,

86. int level);

89. /*

90. ** ===============================================================

91. ** some useful macros

92. ** ===============================================================

93. */

95. ‌#define luaL_argcheck(L, cond,numarg,extramsg)        \

96. ((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))

97. ‌#define luaL_checkstring(L,n)        (luaL_checklstring(L, (n), NULL))

98. ‌#define luaL_optstring(L,n,d)        (luaL_optlstring(L, (n), (d), NULL))

99. ‌#define luaL_checkint(L,n)        ((int)luaL_checkinteger(L, (n)))

100. ‌#define luaL_optint(L,n,d)        ((int)luaL_optinteger(L, (n), (d)))

101. ‌#define luaL_checklong(L,n)        ((long)luaL_checkinteger(L, (n)))

102. ‌#define luaL_optlong(L,n,d)        ((long)luaL_optinteger(L, (n), (d)))

104. ‌#define luaL_typename(L,i)        lua_typename(L, lua_type(L,(i)))

106. ‌#define luaL_dofile(L, fn) \

107. (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))

109. ‌#define luaL_dostring(L, s) \

110. (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))

112. ‌#define luaL_getmetatable(L,n)        (lua_getfield(L, LUA_REGISTRYINDEX, (n)))

114. ‌#define luaL_opt(L,f,n,d)        (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))

116. /*

117. ** {======================================================

118. ** Generic Buffer manipulation

119. ** =======================================================

120. */

124. ‌typedef struct luaL_Buffer {

125. char *p;                        /* current position in buffer */

126. int lvl;  /* number of strings in the stack (level) */

127. lua_State *L;

128. char buffer[LUAL_BUFFERSIZE];

129. ‌} luaL_Buffer;

131. ‌#define luaL_addchar(B,c) \

132. ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \

133. (*(B)->p++ = (char)(c)))

135. /* compatibility only */

136. ‌#define luaL_putchar(B,c)        luaL_addchar(B,c)

138. ‌#define luaL_addsize(B,n)        ((B)->p += (n))

140. LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);

141. LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B);

142. LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);

143. LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);

144. LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);

145. LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);

148. /* }====================================================== */

151. /* compatibility with ref system */

153. /* pre-defined references */

154. ‌#define LUA_NOREF       (-2)

155. ‌#define LUA_REFNIL      (-1)

157. ‌#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \

158. (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))

160. ‌#define lua_unref(L,ref)        luaL_unref(L, LUA_REGISTRYINDEX, (ref))

162. ‌#define lua_getref(L,ref)       lua_rawgeti(L, LUA_REGISTRYINDEX, (ref))

165. ‌#define luaL_reg        luaL_Reg

167. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_load.c - luajit-2.0-src

Data types defined

- FileReaderCtx

- FileReaderCtx

- StringReaderCtx

- StringReaderCtx

Functions defined

- cpparser

- luaL_loadbuffer

- luaL_loadbufferx

- luaL_loadfile

- luaL_loadfilex

- luaL_loadstring

- lua_dump

- lua_load

- lua_loadx

- reader_file

- reader_string

Macros defined

- LUA_CORE

- lj_load_c

Source code

1. /*

2. ** Load and dump code.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include <errno.h>

7. #include <stdio.h>

9. ‌#define lj_load_c

10. ‌#define LUA_CORE

12. #include "lua.h"

13. #include "lauxlib.h"

15. #include "lj_obj.h"

16. #include "lj_gc.h"

17. #include "lj_err.h"

18. #include "lj_buf.h"

19. #include "lj_func.h"

20. #include "lj_frame.h"

21. #include "lj_vm.h"

22. #include "lj_lex.h"

23. #include "lj_bcdump.h"

24. #include "lj_parse.h"

26. /* -- Load Lua source code and bytecode ----------------------------------- */

28. ‌static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud)

29. {

30. LexState *ls = (LexState *)ud;

31. GCproto *pt;

32. GCfunc *fn;

33. int bc;

34. UNUSED(dummy);

35. cframe_errfunc(L->cframe) = -1;  /* Inherit error function. */

36. bc = lj_lex_setup(L, ls);

37. if (ls->mode && !strchr(ls->mode, bc ? 'b' : 't')) {

38. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_XMODE));

39. lj_err_throw(L, LUA_ERRSYNTAX);

40. }

41. pt = bc ? lj_bcread(ls) : lj_parse(ls);

42. fn = lj_func_newL_empty(L, pt, tabref(L->env));

43. /* Don't combine above/below into one statement. */

44. setfuncV(L, L->top++, fn);

45. return NULL;

46. }

48. ‌LUA_API int lua_loadx(lua_State *L, lua_Reader reader, void *data,

49. const char *chunkname, const char *mode)

50. {

51. LexState ls;

52. int status;

53. ls.rfunc = reader;

54. ls.rdata = data;

55. ls.chunkarg = chunkname ? chunkname : "?";

56. ls.mode = mode;

57. lj_buf_init(L, &ls.sb);

58. status = lj_vm_cpcall(L, NULL, &ls, cpparser);

59. lj_lex_cleanup(L, &ls);

60. lj_gc_check(L);

61. return status;

62. }

64. ‌LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,

65. const char *chunkname)

66. {

67. return lua_loadx(L, reader, data, chunkname, NULL);

68. }

70. ‌typedef struct FileReaderCtx {

71. FILE *fp;

72. char buf[LUAL_BUFFERSIZE];

73. ‌} FileReaderCtx;

75. ‌static const char *reader_file(lua_State *L, void *ud, size_t *size)

76. {

77. FileReaderCtx *ctx = (FileReaderCtx *)ud;

78. UNUSED(L);

79. if (feof(ctx->fp)) return NULL;

80. *size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp);

81. return *size > 0 ? ctx->buf : NULL;

82. }

84. ‌LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename,

85. const char *mode)

86. {

87. FileReaderCtx ctx;

88. int status;

89. const char *chunkname;

90. if (filename) {

91. ctx.fp = fopen(filename, "rb");

92. if (ctx.fp == NULL) {

93. lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno));

94. return LUA_ERRFILE;

95. }

96. chunkname = lua_pushfstring(L, "@%s", filename);

97. } else {

98. ctx.fp = stdin;

99. chunkname = "=stdin";

100. }

101. status = lua_loadx(L, reader_file, &ctx, chunkname, mode);

102. if (ferror(ctx.fp)) {

103. L->top -= filename ? 2 : 1;

104. lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno));

105. if (filename)

106. fclose(ctx.fp);

107. return LUA_ERRFILE;

108. }

109. if (filename) {

110. L->top--;

111. copyTV(L, L->top-1, L->top);

112. fclose(ctx.fp);

113. }

114. return status;

115. }

117. ‌LUALIB_API int luaL_loadfile(lua_State *L, const char *filename)

118. {

119. return luaL_loadfilex(L, filename, NULL);

120. }

122. ‌typedef struct StringReaderCtx {

123. const char *str;

124. size_t size;

125. ‌} StringReaderCtx;

127. ‌static const char *reader_string(lua_State *L, void *ud, size_t *size)

128. {

129. StringReaderCtx *ctx = (StringReaderCtx *)ud;

130. UNUSED(L);

131. if (ctx->size == 0) return NULL;

132. *size = ctx->size;

133. ctx->size = 0;

134. return ctx->str;

135. }

137. ‌LUALIB_API int luaL_loadbufferx(lua_State *L, const char *buf, size_t size,

138. const char *name, const char *mode)

139. {

140. StringReaderCtx ctx;

141. ctx.str = buf;

142. ctx.size = size;

143. return lua_loadx(L, reader_string, &ctx, name, mode);

144. }

146. ‌LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size,

147. const char *name)

148. {

149. return luaL_loadbufferx(L, buf, size, name, NULL);

150. }

152. ‌LUALIB_API int luaL_loadstring(lua_State *L, const char *s)

153. {

154. return luaL_loadbuffer(L, s, strlen(s), s);

155. }

157. /* -- Dump bytecode ------------------------------------------------------- */

159. ‌LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)

160. {

161. cTValue *o = L->top-1;

162. api_check(L, L->top > L->base);

163. if (tvisfunc(o) && isluafunc(funcV(o)))

164. return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0);

165. else

166. return 1;

167. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_parse.c - luajit-2.0-src

Global variables defined

- priority

Data types defined

- BinOpr

- BinOpr

- ExpDesc

- ExpDesc

- ExpKind

- FuncScope

- FuncScope

- FuncState

- FuncState

- LHSVarList

- LHSVarList

- VarIndex

Functions defined

- assign_adjust

- assign_hazard

- bcemit_INS

- bcemit_arith

- bcemit_binop

- bcemit_binop_left

- bcemit_branch

- bcemit_branch_f

- bcemit_branch_t

- bcemit_comp

- bcemit_jmp

- bcemit_method

- bcemit_nil

- bcemit_store

- bcemit_unop

- bcopisret

- bcreg_bump

- bcreg_free

- bcreg_reserve

- const_gc

- const_num

- const_str

- err_limit

- err_syntax

- err_token

- expr

- expr_binop

- expr_bracket

- expr_cond

- expr_discharge

- expr_field

- expr_free

- expr_index

- expr_init

- expr_kvalue

- expr_list

- expr_next

- expr_numiszero

- expr_primary

- expr_simple

- expr_str

- expr_table

- expr_toanyreg

- expr_tonextreg

- expr_toreg

- expr_toreg_nobranch

- expr_toval

- expr_unop

- foldarith

- fs_finish

- fs_fixup_bc

- fs_fixup_k

- fs_fixup_line

- fs_fixup_ret

- fs_fixup_uv1

- fs_fixup_uv2

- fs_fixup_var

- fs_init

- fs_prep_line

- fs_prep_var

- fscope_begin

- fscope_end

- fscope_uvmark

- gola_close

- gola_findlabel

- gola_fixup

- gola_new

- gola_patch

- gola_resolve

- invertcond

- jmp_append

- jmp_dropval

- jmp_next

- jmp_novalue

- jmp_patch

- jmp_patchins

- jmp_patchtestreg

- jmp_patchval

- jmp_tohere

- lex_check

- lex_match

- lex_opt

- lex_str

- lj_parse

- lj_parse_keepcdata

- lj_parse_keepstr

- parse_args

- parse_assignment

- parse_block

- parse_body

- parse_break

- parse_call_assign

- parse_chunk

- parse_for

- parse_for_iter

- parse_for_num

- parse_func

- parse_goto

- parse_if

- parse_isend

- parse_label

- parse_local

- parse_params

- parse_repeat

- parse_return

- parse_stmt

- parse_then

- parse_while

- predict_next

- synlevel_begin

- token2binop

- var_add

- var_lookup_

- var_lookup_local

- var_lookup_uv

- var_new

- var_remove

Macros defined

- FSCOPE_BREAK

- FSCOPE_GOLA

- FSCOPE_LOOP

- FSCOPE_NOCLOSE

- FSCOPE_UPVAL

- LJ_MAX_VSTACK

- LUA_CORE

- NAME_BREAK

- UNARY_PRIORITY

- VSTACK_GOTO

- VSTACK_LABEL

- VSTACK_VAR_RW

- bcemit_ABC

- bcemit_AD

- bcemit_AJ

- bcptr

- checkcond

- checklimit

- checklimitgt

- const_pri

- expr_hasjump

- expr_isk

- expr_isk_nojump

- expr_isnumk

- expr_isnumk_nojump

- expr_isstrk

- expr_numberV

- expr_numtv

- fs_fixup_line

- fs_fixup_var

- fs_prep_line

- fs_prep_var

- gola_isgoto

- gola_isgotolabel

- gola_islabel

- lj_parse_c

- synlevel_end

- tvhaskslot

- tvkslot

- var_get

- var_lookup

- var_new_fixed

- var_new_lit

Source code

1. /*

2. ** Lua parser (source code -> bytecode).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lj_parse_c

10. ‌#define LUA_CORE

12. #include "lj_obj.h"

13. #include "lj_gc.h"

14. #include "lj_err.h"

15. #include "lj_debug.h"

16. #include "lj_buf.h"

17. #include "lj_str.h"

18. #include "lj_tab.h"

19. #include "lj_func.h"

20. #include "lj_state.h"

21. #include "lj_bc.h"

22. #if LJ_HASFFI

23. #include "lj_ctype.h"

24. #endif

25. #include "lj_strfmt.h"

26. #include "lj_lex.h"

27. #include "lj_parse.h"

28. #include "lj_vm.h"

29. #include "lj_vmevent.h"

31. /* -- Parser structures and definitions ----------------------------------- */

33. /* Expression kinds. */

34. typedef enum {

35. /* Constant expressions must be first and in this order: */

36. VKNIL,

37. VKFALSE,

38. VKTRUE,

39. VKSTR,        /* sval = string value */

40. VKNUM,        /* nval = number value */

41. VKLAST = VKNUM,

42. VKCDATA,        /* nval = cdata value, not treated as a constant expression */

43. /* Non-constant expressions follow: */

44. VLOCAL,        /* info = local register, aux = vstack index */

45. VUPVAL,        /* info = upvalue index, aux = vstack index */

46. VGLOBAL,        /* sval = string value */

47. VINDEXED,        /* info = table register, aux = index reg/byte/string const */

48. VJMP,                /* info = instruction PC */

49. VRELOCABLE,        /* info = instruction PC */

50. VNONRELOC,        /* info = result register */

51. VCALL,        /* info = instruction PC, aux = base */

52. VVOID

53. ‌} ExpKind;

55. /* Expression descriptor. */

56. ‌typedef struct ExpDesc {

57. union {

58. struct {

59. uint32_t info;        /* Primary info. */

60. uint32_t aux;        /* Secondary info. */

61. } s;

62. TValue nval;        /* Number value. */

63. GCstr *sval;        /* String value. */

64. } u;

65. ExpKind k;

66. BCPos t;                /* True condition jump list. */

67. BCPos f;                /* False condition jump list. */

68. ‌} ExpDesc;

70. /* Macros for expressions. */

71. ‌#define expr_hasjump(e)                ((e)->t != (e)->f)

73. ‌#define expr_isk(e)                ((e)->k <= VKLAST)

74. ‌#define expr_isk_nojump(e)        (expr_isk(e) && !expr_hasjump(e))

75. ‌#define expr_isnumk(e)                ((e)->k == VKNUM)

76. ‌#define expr_isnumk_nojump(e)        (expr_isnumk(e) && !expr_hasjump(e))

77. ‌#define expr_isstrk(e)                ((e)->k == VKSTR)

79. ‌#define expr_numtv(e)                check_exp(expr_isnumk((e)), &(e)->u.nval)

80. ‌#define expr_numberV(e)                numberVnum(expr_numtv((e)))

82. /* Initialize expression. */

83. ‌static LJ_AINLINE void expr_init(ExpDesc *e, ExpKind k, uint32_t info)

84. {

85. e->k = k;

86. e->u.s.info = info;

87. e->f = e->t = NO_JMP;

88. }

90. /* Check number constant for +-0. */

91. ‌static int expr_numiszero(ExpDesc *e)

92. {

93. TValue *o = expr_numtv(e);

94. return tvisint(o) ? (intV(o) == 0) : tviszero(o);

95. }

97. /* Per-function linked list of scope blocks. */

98. ‌typedef struct FuncScope {

99. struct FuncScope *prev;        /* Link to outer scope. */

100. MSize vstart;                        /* Start of block-local variables. */

101. uint8_t nactvar;                /* Number of active vars outside the scope. */

102. uint8_t flags;                /* Scope flags. */

103. ‌} FuncScope;

105. ‌#define FSCOPE_LOOP                0x01        /* Scope is a (breakable) loop. */

106. ‌#define FSCOPE_BREAK                0x02        /* Break used in scope. */

107. ‌#define FSCOPE_GOLA                0x04        /* Goto or label used in scope. */

108. ‌#define FSCOPE_UPVAL                0x08        /* Upvalue in scope. */

109. ‌#define FSCOPE_NOCLOSE                0x10        /* Do not close upvalues. */

111. ‌#define NAME_BREAK                ((GCstr *)(uintptr_t)1)

113. /* Index into variable stack. */

114. ‌typedef uint16_t VarIndex;

115. ‌#define LJ_MAX_VSTACK                (65536 - LJ_MAX_UPVAL)

117. /* Variable/goto/label info. */

118. ‌#define VSTACK_VAR_RW                0x01        /* R/W variable. */

119. ‌#define VSTACK_GOTO                0x02        /* Pending goto. */

120. ‌#define VSTACK_LABEL                0x04        /* Label. */

122. /* Per-function state. */

123. ‌typedef struct FuncState {

124. GCtab *kt;                        /* Hash table for constants. */

125. LexState *ls;                        /* Lexer state. */

126. lua_State *L;                        /* Lua state. */

127. FuncScope *bl;                /* Current scope. */

128. struct FuncState *prev;        /* Enclosing function. */

129. BCPos pc;                        /* Next bytecode position. */

130. BCPos lasttarget;                /* Bytecode position of last jump target. */

131. BCPos jpc;                        /* Pending jump list to next bytecode. */

132. BCReg freereg;                /* First free register. */

133. BCReg nactvar;                /* Number of active local variables. */

134. BCReg nkn, nkgc;                /* Number of lua_Number/GCobj constants */

135. BCLine linedefined;                /* First line of the function definition. */

136. BCInsLine *bcbase;                /* Base of bytecode stack. */

137. BCPos bclim;                        /* Limit of bytecode stack. */

138. MSize vbase;                        /* Base of variable stack for this function. */

139. uint8_t flags;                /* Prototype flags. */

140. uint8_t numparams;                /* Number of parameters. */

141. uint8_t framesize;                /* Fixed frame size. */

142. uint8_t nuv;                        /* Number of upvalues */

143. VarIndex varmap[LJ_MAX_LOCVAR];  /* Map from register to variable idx. */

144. VarIndex uvmap[LJ_MAX_UPVAL];        /* Map from upvalue to variable idx. */

145. VarIndex uvtmp[LJ_MAX_UPVAL];        /* Temporary upvalue map. */

146. ‌} FuncState;

148. /* Binary and unary operators. ORDER OPR */

149. ‌typedef enum BinOpr {

150. OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,  /* ORDER ARITH */

151. OPR_CONCAT,

152. OPR_NE, OPR_EQ,

153. OPR_LT, OPR_GE, OPR_LE, OPR_GT,

154. OPR_AND, OPR_OR,

155. OPR_NOBINOPR

156. ‌} BinOpr;

158. LJ_STATIC_ASSERT((int)BC_ISGE-(int)BC_ISLT == (int)OPR_GE-(int)OPR_LT);

159. LJ_STATIC_ASSERT((int)BC_ISLE-(int)BC_ISLT == (int)OPR_LE-(int)OPR_LT);

160. LJ_STATIC_ASSERT((int)BC_ISGT-(int)BC_ISLT == (int)OPR_GT-(int)OPR_LT);

161. LJ_STATIC_ASSERT((int)BC_SUBVV-(int)BC_ADDVV == (int)OPR_SUB-(int)OPR_ADD);

162. LJ_STATIC_ASSERT((int)BC_MULVV-(int)BC_ADDVV == (int)OPR_MUL-(int)OPR_ADD);

163. LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD);

164. LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD);

166. /* -- Error handling ------------------------------------------------------ */

168. ‌LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em)

169. {

170. lj_lex_error(ls, ls->tok, em);

171. }

173. ‌LJ_NORET LJ_NOINLINE static void err_token(LexState *ls, LexToken tok)

174. {

175. lj_lex_error(ls, ls->tok, LJ_ERR_XTOKEN, lj_lex_token2str(ls, tok));

176. }

178. ‌LJ_NORET static void err_limit(FuncState *fs, uint32_t limit, const char *what)

179. {

180. if (fs->linedefined == 0)

181. lj_lex_error(fs->ls, 0, LJ_ERR_XLIMM, limit, what);

182. else

183. lj_lex_error(fs->ls, 0, LJ_ERR_XLIMF, fs->linedefined, limit, what);

184. }

186. ‌#define checklimit(fs, v, l, m)                if ((v) >= (l)) err_limit(fs, l, m)

187. ‌#define checklimitgt(fs, v, l, m)        if ((v) > (l)) err_limit(fs, l, m)

188. ‌#define checkcond(ls, c, em)                { if (!(c)) err_syntax(ls, em); }

190. /* -- Management of constants --------------------------------------------- */

192. /* Return bytecode encoding for primitive constant. */

193. ‌#define const_pri(e)                check_exp((e)->k <= VKTRUE, (e)->k)

195. ‌#define tvhaskslot(o)        ((o)->u32.hi == 0)

196. ‌#define tvkslot(o)        ((o)->u32.lo)

198. /* Add a number constant. */

199. ‌static BCReg const_num(FuncState *fs, ExpDesc *e)

200. {

201. lua_State *L = fs->L;

202. TValue *o;

203. lua_assert(expr_isnumk(e));

204. o = lj_tab_set(L, fs->kt, &e->u.nval);

205. if (tvhaskslot(o))

206. return tvkslot(o);

207. o->u64 = fs->nkn;

208. return fs->nkn++;

209. }

211. /* Add a GC object constant. */

212. ‌static BCReg const_gc(FuncState *fs, GCobj *gc, uint32_t itype)

213. {

214. lua_State *L = fs->L;

215. TValue key, *o;

216. setgcV(L, &key, gc, itype);

217. /* NOBARRIER: the key is new or kept alive. */

218. o = lj_tab_set(L, fs->kt, &key);

219. if (tvhaskslot(o))

220. return tvkslot(o);

221. o->u64 = fs->nkgc;

222. return fs->nkgc++;

223. }

225. /* Add a string constant. */

226. ‌static BCReg const_str(FuncState *fs, ExpDesc *e)

227. {

228. lua_assert(expr_isstrk(e) || e->k == VGLOBAL);

229. return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR);

230. }

232. /* Anchor string constant to avoid GC. */

233. ‌GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t len)

234. {

235. /* NOBARRIER: the key is new or kept alive. */

236. lua_State *L = ls->L;

237. GCstr *s = lj_str_new(L, str, len);

238. TValue *tv = lj_tab_setstr(L, ls->fs->kt, s);

239. if (tvisnil(tv)) setboolV(tv, 1);

240. lj_gc_check(L);

241. return s;

242. }

244. #if LJ_HASFFI

245. /* Anchor cdata to avoid GC. */

246. ‌void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd)

247. {

248. /* NOBARRIER: the key is new or kept alive. */

249. lua_State *L = ls->L;

250. setcdataV(L, tv, cd);

251. setboolV(lj_tab_set(L, ls->fs->kt, tv), 1);

252. }

253. #endif

255. /* -- Jump list handling -------------------------------------------------- */

257. /* Get next element in jump list. */

258. ‌static BCPos jmp_next(FuncState *fs, BCPos pc)

259. {

260. ptrdiff_t delta = bc_j(fs->bcbase[pc].ins);

261. if ((BCPos)delta == NO_JMP)

262. return NO_JMP;

263. else

264. return (BCPos)(((ptrdiff_t)pc+1)+delta);

265. }

267. /* Check if any of the instructions on the jump list produce no value. */

268. ‌static int jmp_novalue(FuncState *fs, BCPos list)

269. {

270. for (; list != NO_JMP; list = jmp_next(fs, list)) {

271. BCIns p = fs->bcbase[list >= 1 ? list-1 : list].ins;

272. if (!(bc_op(p) == BC_ISTC || bc_op(p) == BC_ISFC || bc_a(p) == NO_REG))

273. return 1;

274. }

275. return 0;

276. }

278. /* Patch register of test instructions. */

279. ‌static int jmp_patchtestreg(FuncState *fs, BCPos pc, BCReg reg)

280. {

281. BCInsLine *ilp = &fs->bcbase[pc >= 1 ? pc-1 : pc];

282. BCOp op = bc_op(ilp->ins);

283. if (op == BC_ISTC || op == BC_ISFC) {

284. if (reg != NO_REG && reg != bc_d(ilp->ins)) {

285. setbc_a(&ilp->ins, reg);

286. } else {  /* Nothing to store or already in the right register. */

287. setbc_op(&ilp->ins, op+(BC_IST-BC_ISTC));

288. setbc_a(&ilp->ins, 0);

289. }

290. } else if (bc_a(ilp->ins) == NO_REG) {

291. if (reg == NO_REG) {

292. ilp->ins = BCINS_AJ(BC_JMP, bc_a(fs->bcbase[pc].ins), 0);

293. } else {

294. setbc_a(&ilp->ins, reg);

295. if (reg >= bc_a(ilp[1].ins))

296. setbc_a(&ilp[1].ins, reg+1);

297. }

298. } else {

299. return 0;  /* Cannot patch other instructions. */

300. }

301. return 1;

302. }

304. /* Drop values for all instructions on jump list. */

305. ‌static void jmp_dropval(FuncState *fs, BCPos list)

306. {

307. for (; list != NO_JMP; list = jmp_next(fs, list))

308. jmp_patchtestreg(fs, list, NO_REG);

309. }

311. /* Patch jump instruction to target. */

312. ‌static void jmp_patchins(FuncState *fs, BCPos pc, BCPos dest)

313. {

314. BCIns *jmp = &fs->bcbase[pc].ins;

315. BCPos offset = dest-(pc+1)+BCBIAS_J;

316. lua_assert(dest != NO_JMP);

317. if (offset > BCMAX_D)

318. err_syntax(fs->ls, LJ_ERR_XJUMP);

319. setbc_d(jmp, offset);

320. }

322. /* Append to jump list. */

323. ‌static void jmp_append(FuncState *fs, BCPos *l1, BCPos l2)

324. {

325. if (l2 == NO_JMP) {

326. return;

327. } else if (*l1 == NO_JMP) {

328. *l1 = l2;

329. } else {

330. BCPos list = *l1;

331. BCPos next;

332. while ((next = jmp_next(fs, list)) != NO_JMP)  /* Find last element. */

333. list = next;

334. jmp_patchins(fs, list, l2);

335. }

336. }

338. /* Patch jump list and preserve produced values. */

339. ‌static void jmp_patchval(FuncState *fs, BCPos list, BCPos vtarget,

340. BCReg reg, BCPos dtarget)

341. {

342. while (list != NO_JMP) {

343. BCPos next = jmp_next(fs, list);

344. if (jmp_patchtestreg(fs, list, reg))

345. jmp_patchins(fs, list, vtarget);  /* Jump to target with value. */

346. else

347. jmp_patchins(fs, list, dtarget);  /* Jump to default target. */

348. list = next;

349. }

350. }

352. /* Jump to following instruction. Append to list of pending jumps. */

353. ‌static void jmp_tohere(FuncState *fs, BCPos list)

354. {

355. fs->lasttarget = fs->pc;

356. jmp_append(fs, &fs->jpc, list);

357. }

359. /* Patch jump list to target. */

360. ‌static void jmp_patch(FuncState *fs, BCPos list, BCPos target)

361. {

362. if (target == fs->pc) {

363. jmp_tohere(fs, list);

364. } else {

365. lua_assert(target < fs->pc);

366. jmp_patchval(fs, list, target, NO_REG, target);

367. }

368. }

370. /* -- Bytecode register allocator ----------------------------------------- */

372. /* Bump frame size. */

373. ‌static void bcreg_bump(FuncState *fs, BCReg n)

374. {

375. BCReg sz = fs->freereg + n;

376. if (sz > fs->framesize) {

377. if (sz >= LJ_MAX_SLOTS)

378. err_syntax(fs->ls, LJ_ERR_XSLOTS);

379. fs->framesize = (uint8_t)sz;

380. }

381. }

383. /* Reserve registers. */

384. ‌static void bcreg_reserve(FuncState *fs, BCReg n)

385. {

386. bcreg_bump(fs, n);

387. fs->freereg += n;

388. }

390. /* Free register. */

391. ‌static void bcreg_free(FuncState *fs, BCReg reg)

392. {

393. if (reg >= fs->nactvar) {

394. fs->freereg--;

395. lua_assert(reg == fs->freereg);

396. }

397. }

399. /* Free register for expression. */

400. ‌static void expr_free(FuncState *fs, ExpDesc *e)

401. {

402. if (e->k == VNONRELOC)

403. bcreg_free(fs, e->u.s.info);

404. }

406. /* -- Bytecode emitter ---------------------------------------------------- */

408. /* Emit bytecode instruction. */

409. ‌static BCPos bcemit_INS(FuncState *fs, BCIns ins)

410. {

411. BCPos pc = fs->pc;

412. LexState *ls = fs->ls;

413. jmp_patchval(fs, fs->jpc, pc, NO_REG, pc);

414. fs->jpc = NO_JMP;

415. if (LJ_UNLIKELY(pc >= fs->bclim)) {

416. ptrdiff_t base = fs->bcbase - ls->bcstack;

417. checklimit(fs, ls->sizebcstack, LJ_MAX_BCINS, "bytecode instructions");

418. lj_mem_growvec(fs->L, ls->bcstack, ls->sizebcstack, LJ_MAX_BCINS,BCInsLine);

419. fs->bclim = (BCPos)(ls->sizebcstack - base);

420. fs->bcbase = ls->bcstack + base;

421. }

422. fs->bcbase[pc].ins = ins;

423. fs->bcbase[pc].line = ls->lastline;

424. fs->pc = pc+1;

425. return pc;

426. }

428. ‌#define bcemit_ABC(fs, o, a, b, c)        bcemit_INS(fs, BCINS_ABC(o, a, b, c))

429. ‌#define bcemit_AD(fs, o, a, d)                bcemit_INS(fs, BCINS_AD(o, a, d))

430. ‌#define bcemit_AJ(fs, o, a, j)                bcemit_INS(fs, BCINS_AJ(o, a, j))

432. ‌#define bcptr(fs, e)                        (&(fs)->bcbase[(e)->u.s.info].ins)

434. /* -- Bytecode emitter for expressions ------------------------------------ */

436. /* Discharge non-constant expression to any register. */

437. ‌static void expr_discharge(FuncState *fs, ExpDesc *e)

438. {

439. BCIns ins;

440. if (e->k == VUPVAL) {

441. ins = BCINS_AD(BC_UGET, 0, e->u.s.info);

442. } else if (e->k == VGLOBAL) {

443. ins = BCINS_AD(BC_GGET, 0, const_str(fs, e));

444. } else if (e->k == VINDEXED) {

445. BCReg rc = e->u.s.aux;

446. if ((int32_t)rc < 0) {

447. ins = BCINS_ABC(BC_TGETS, 0, e->u.s.info, ~rc);

448. } else if (rc > BCMAX_C) {

449. ins = BCINS_ABC(BC_TGETB, 0, e->u.s.info, rc-(BCMAX_C+1));

450. } else {

451. bcreg_free(fs, rc);

452. ins = BCINS_ABC(BC_TGETV, 0, e->u.s.info, rc);

453. }

454. bcreg_free(fs, e->u.s.info);

455. } else if (e->k == VCALL) {

456. e->u.s.info = e->u.s.aux;

457. e->k = VNONRELOC;

458. return;

459. } else if (e->k == VLOCAL) {

460. e->k = VNONRELOC;

461. return;

462. } else {

463. return;

464. }

465. e->u.s.info = bcemit_INS(fs, ins);

466. e->k = VRELOCABLE;

467. }

469. /* Emit bytecode to set a range of registers to nil. */

470. ‌static void bcemit_nil(FuncState *fs, BCReg from, BCReg n)

471. {

472. if (fs->pc > fs->lasttarget) {  /* No jumps to current position? */

473. BCIns *ip = &fs->bcbase[fs->pc-1].ins;

474. BCReg pto, pfrom = bc_a(*ip);

475. switch (bc_op(*ip)) {  /* Try to merge with the previous instruction. */

476. case BC_KPRI:

477. if (bc_d(*ip) != ~LJ_TNIL) break;

478. if (from == pfrom) {

479. if (n == 1) return;

480. } else if (from == pfrom+1) {

481. from = pfrom;

482. n++;

483. } else {

484. break;

485. }

486. *ip = BCINS_AD(BC_KNIL, from, from+n-1);  /* Replace KPRI. */

487. return;

488. case BC_KNIL:

489. pto = bc_d(*ip);

490. if (pfrom <= from && from <= pto+1) {  /* Can we connect both ranges? */

491. if (from+n-1 > pto)

492. setbc_d(ip, from+n-1);  /* Patch previous instruction range. */

493. return;

494. }

495. break;

496. default:

497. break;

498. }

499. }

500. /* Emit new instruction or replace old instruction. */

501. bcemit_INS(fs, n == 1 ? BCINS_AD(BC_KPRI, from, VKNIL) :

502. BCINS_AD(BC_KNIL, from, from+n-1));

503. }

505. /* Discharge an expression to a specific register. Ignore branches. */

506. ‌static void expr_toreg_nobranch(FuncState *fs, ExpDesc *e, BCReg reg)

507. {

508. BCIns ins;

509. expr_discharge(fs, e);

510. if (e->k == VKSTR) {

511. ins = BCINS_AD(BC_KSTR, reg, const_str(fs, e));

512. } else if (e->k == VKNUM) {

513. #if LJ_DUALNUM

514. cTValue *tv = expr_numtv(e);

515. if (tvisint(tv) && checki16(intV(tv)))

516. ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)intV(tv));

517. else

518. #else

519. lua_Number n = expr_numberV(e);

520. int32_t k = lj_num2int(n);

521. if (checki16(k) && n == (lua_Number)k)

522. ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)k);

523. else

524. #endif

525. ins = BCINS_AD(BC_KNUM, reg, const_num(fs, e));

526. #if LJ_HASFFI

527. } else if (e->k == VKCDATA) {

528. fs->flags |= PROTO_FFI;

529. ins = BCINS_AD(BC_KCDATA, reg,

530. const_gc(fs, obj2gco(cdataV(&e->u.nval)), LJ_TCDATA));

531. #endif

532. } else if (e->k == VRELOCABLE) {

533. setbc_a(bcptr(fs, e), reg);

534. goto noins;

535. } else if (e->k == VNONRELOC) {

536. if (reg == e->u.s.info)

537. goto noins;

538. ins = BCINS_AD(BC_MOV, reg, e->u.s.info);

539. } else if (e->k == VKNIL) {

540. bcemit_nil(fs, reg, 1);

541. goto noins;

542. } else if (e->k <= VKTRUE) {

543. ins = BCINS_AD(BC_KPRI, reg, const_pri(e));

544. } else {

545. lua_assert(e->k == VVOID || e->k == VJMP);

546. return;

547. }

548. bcemit_INS(fs, ins);

549. noins:

550. e->u.s.info = reg;

551. e->k = VNONRELOC;

552. }

554. /* Forward declaration. */

555. static BCPos bcemit_jmp(FuncState *fs);

557. /* Discharge an expression to a specific register. */

558. ‌static void expr_toreg(FuncState *fs, ExpDesc *e, BCReg reg)

559. {

560. expr_toreg_nobranch(fs, e, reg);

561. if (e->k == VJMP)

562. jmp_append(fs, &e->t, e->u.s.info);  /* Add it to the true jump list. */

563. if (expr_hasjump(e)) {  /* Discharge expression with branches. */

564. BCPos jend, jfalse = NO_JMP, jtrue = NO_JMP;

565. if (jmp_novalue(fs, e->t) || jmp_novalue(fs, e->f)) {

566. BCPos jval = (e->k == VJMP) ? NO_JMP : bcemit_jmp(fs);

567. jfalse = bcemit_AD(fs, BC_KPRI, reg, VKFALSE);

568. bcemit_AJ(fs, BC_JMP, fs->freereg, 1);

569. jtrue = bcemit_AD(fs, BC_KPRI, reg, VKTRUE);

570. jmp_tohere(fs, jval);

571. }

572. jend = fs->pc;

573. fs->lasttarget = jend;

574. jmp_patchval(fs, e->f, jend, reg, jfalse);

575. jmp_patchval(fs, e->t, jend, reg, jtrue);

576. }

577. e->f = e->t = NO_JMP;

578. e->u.s.info = reg;

579. e->k = VNONRELOC;

580. }

582. /* Discharge an expression to the next free register. */

583. ‌static void expr_tonextreg(FuncState *fs, ExpDesc *e)

584. {

585. expr_discharge(fs, e);

586. expr_free(fs, e);

587. bcreg_reserve(fs, 1);

588. expr_toreg(fs, e, fs->freereg - 1);

589. }

591. /* Discharge an expression to any register. */

592. ‌static BCReg expr_toanyreg(FuncState *fs, ExpDesc *e)

593. {

594. expr_discharge(fs, e);

595. if (e->k == VNONRELOC) {

596. if (!expr_hasjump(e)) return e->u.s.info;  /* Already in a register. */

597. if (e->u.s.info >= fs->nactvar) {

598. expr_toreg(fs, e, e->u.s.info);  /* Discharge to temp. register. */

599. return e->u.s.info;

600. }

601. }

602. expr_tonextreg(fs, e);  /* Discharge to next register. */

603. return e->u.s.info;

604. }

606. /* Partially discharge expression to a value. */

607. ‌static void expr_toval(FuncState *fs, ExpDesc *e)

608. {

609. if (expr_hasjump(e))

610. expr_toanyreg(fs, e);

611. else

612. expr_discharge(fs, e);

613. }

615. /* Emit store for LHS expression. */

616. ‌static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e)

617. {

618. BCIns ins;

619. if (var->k == VLOCAL) {

620. fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;

621. expr_free(fs, e);

622. expr_toreg(fs, e, var->u.s.info);

623. return;

624. } else if (var->k == VUPVAL) {

625. fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;

626. expr_toval(fs, e);

627. if (e->k <= VKTRUE)

628. ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e));

629. else if (e->k == VKSTR)

630. ins = BCINS_AD(BC_USETS, var->u.s.info, const_str(fs, e));

631. else if (e->k == VKNUM)

632. ins = BCINS_AD(BC_USETN, var->u.s.info, const_num(fs, e));

633. else

634. ins = BCINS_AD(BC_USETV, var->u.s.info, expr_toanyreg(fs, e));

635. } else if (var->k == VGLOBAL) {

636. BCReg ra = expr_toanyreg(fs, e);

637. ins = BCINS_AD(BC_GSET, ra, const_str(fs, var));

638. } else {

639. BCReg ra, rc;

640. lua_assert(var->k == VINDEXED);

641. ra = expr_toanyreg(fs, e);

642. rc = var->u.s.aux;

643. if ((int32_t)rc < 0) {

644. ins = BCINS_ABC(BC_TSETS, ra, var->u.s.info, ~rc);

645. } else if (rc > BCMAX_C) {

646. ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1));

647. } else {

648. /* Free late alloced key reg to avoid assert on free of value reg. */

649. /* This can only happen when called from expr_table(). */

650. lua_assert(e->k != VNONRELOC || ra < fs->nactvar ||

651. rc < ra || (bcreg_free(fs, rc),1));

652. ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc);

653. }

654. }

655. bcemit_INS(fs, ins);

656. expr_free(fs, e);

657. }

659. /* Emit method lookup expression. */

660. ‌static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key)

661. {

662. BCReg idx, func, obj = expr_toanyreg(fs, e);

663. expr_free(fs, e);

664. func = fs->freereg;

665. bcemit_AD(fs, BC_MOV, func+1+LJ_FR2, obj);  /* Copy object to 1st argument. */

666. lua_assert(expr_isstrk(key));

667. idx = const_str(fs, key);

668. if (idx <= BCMAX_C) {

669. bcreg_reserve(fs, 2+LJ_FR2);

670. bcemit_ABC(fs, BC_TGETS, func, obj, idx);

671. } else {

672. bcreg_reserve(fs, 3+LJ_FR2);

673. bcemit_AD(fs, BC_KSTR, func+2+LJ_FR2, idx);

674. bcemit_ABC(fs, BC_TGETV, func, obj, func+2+LJ_FR2);

675. fs->freereg--;

676. }

677. e->u.s.info = func;

678. e->k = VNONRELOC;

679. }

681. /* -- Bytecode emitter for branches --------------------------------------- */

683. /* Emit unconditional branch. */

684. ‌static BCPos bcemit_jmp(FuncState *fs)

685. {

686. BCPos jpc = fs->jpc;

687. BCPos j = fs->pc - 1;

688. BCIns *ip = &fs->bcbase[j].ins;

689. fs->jpc = NO_JMP;

690. if ((int32_t)j >= (int32_t)fs->lasttarget && bc_op(*ip) == BC_UCLO) {

691. setbc_j(ip, NO_JMP);

692. fs->lasttarget = j+1;

693. } else {

694. j = bcemit_AJ(fs, BC_JMP, fs->freereg, NO_JMP);

695. }

696. jmp_append(fs, &j, jpc);

697. return j;

698. }

700. /* Invert branch condition of bytecode instruction. */

701. ‌static void invertcond(FuncState *fs, ExpDesc *e)

702. {

703. BCIns *ip = &fs->bcbase[e->u.s.info - 1].ins;

704. setbc_op(ip, bc_op(*ip)^1);

705. }

707. /* Emit conditional branch. */

708. ‌static BCPos bcemit_branch(FuncState *fs, ExpDesc *e, int cond)

709. {

710. BCPos pc;

711. if (e->k == VRELOCABLE) {

712. BCIns *ip = bcptr(fs, e);

713. if (bc_op(*ip) == BC_NOT) {

714. *ip = BCINS_AD(cond ? BC_ISF : BC_IST, 0, bc_d(*ip));

715. return bcemit_jmp(fs);

716. }

717. }

718. if (e->k != VNONRELOC) {

719. bcreg_reserve(fs, 1);

720. expr_toreg_nobranch(fs, e, fs->freereg-1);

721. }

722. bcemit_AD(fs, cond ? BC_ISTC : BC_ISFC, NO_REG, e->u.s.info);

723. pc = bcemit_jmp(fs);

724. expr_free(fs, e);

725. return pc;

726. }

728. /* Emit branch on true condition. */

729. ‌static void bcemit_branch_t(FuncState *fs, ExpDesc *e)

730. {

731. BCPos pc;

732. expr_discharge(fs, e);

733. if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)

734. pc = NO_JMP;  /* Never jump. */

735. else if (e->k == VJMP)

736. invertcond(fs, e), pc = e->u.s.info;

737. else if (e->k == VKFALSE || e->k == VKNIL)

738. expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);

739. else

740. pc = bcemit_branch(fs, e, 0);

741. jmp_append(fs, &e->f, pc);

742. jmp_tohere(fs, e->t);

743. e->t = NO_JMP;

744. }

746. /* Emit branch on false condition. */

747. ‌static void bcemit_branch_f(FuncState *fs, ExpDesc *e)

748. {

749. BCPos pc;

750. expr_discharge(fs, e);

751. if (e->k == VKNIL || e->k == VKFALSE)

752. pc = NO_JMP;  /* Never jump. */

753. else if (e->k == VJMP)

754. pc = e->u.s.info;

755. else if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)

756. expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);

757. else

758. pc = bcemit_branch(fs, e, 1);

759. jmp_append(fs, &e->t, pc);

760. jmp_tohere(fs, e->f);

761. e->f = NO_JMP;

762. }

764. /* -- Bytecode emitter for operators -------------------------------------- */

766. /* Try constant-folding of arithmetic operators. */

767. ‌static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2)

768. {

769. TValue o;

770. lua_Number n;

771. if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0;

772. n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD);

773. setnumV(&o, n);

774. if (tvisnan(&o) || tvismzero(&o)) return 0;  /* Avoid NaN and -0 as consts. */

775. if (LJ_DUALNUM) {

776. int32_t k = lj_num2int(n);

777. if ((lua_Number)k == n) {

778. setintV(&e1->u.nval, k);

779. return 1;

780. }

781. }

782. setnumV(&e1->u.nval, n);

783. return 1;

784. }

786. /* Emit arithmetic operator. */

787. ‌static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)

788. {

789. BCReg rb, rc, t;

790. uint32_t op;

791. if (foldarith(opr, e1, e2))

792. return;

793. if (opr == OPR_POW) {

794. op = BC_POW;

795. rc = expr_toanyreg(fs, e2);

796. rb = expr_toanyreg(fs, e1);

797. } else {

798. op = opr-OPR_ADD+BC_ADDVV;

799. /* Must discharge 2nd operand first since VINDEXED might free regs. */

800. expr_toval(fs, e2);

801. if (expr_isnumk(e2) && (rc = const_num(fs, e2)) <= BCMAX_C)

802. op -= BC_ADDVV-BC_ADDVN;

803. else

804. rc = expr_toanyreg(fs, e2);

805. /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */

806. lua_assert(expr_isnumk(e1) || e1->k == VNONRELOC);

807. expr_toval(fs, e1);

808. /* Avoid two consts to satisfy bytecode constraints. */

809. if (expr_isnumk(e1) && !expr_isnumk(e2) &&

810. (t = const_num(fs, e1)) <= BCMAX_B) {

811. rb = rc; rc = t; op -= BC_ADDVV-BC_ADDNV;

812. } else {

813. rb = expr_toanyreg(fs, e1);

814. }

815. }

816. /* Using expr_free might cause asserts if the order is wrong. */

817. if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;

818. if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;

819. e1->u.s.info = bcemit_ABC(fs, op, 0, rb, rc);

820. e1->k = VRELOCABLE;

821. }

823. /* Emit comparison operator. */

824. ‌static void bcemit_comp(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)

825. {

826. ExpDesc *eret = e1;

827. BCIns ins;

828. expr_toval(fs, e1);

829. if (opr == OPR_EQ || opr == OPR_NE) {

830. BCOp op = opr == OPR_EQ ? BC_ISEQV : BC_ISNEV;

831. BCReg ra;

832. if (expr_isk(e1)) { e1 = e2; e2 = eret; }  /* Need constant in 2nd arg. */

833. ra = expr_toanyreg(fs, e1);  /* First arg must be in a reg. */

834. expr_toval(fs, e2);

835. switch (e2->k) {

836. case VKNIL: case VKFALSE: case VKTRUE:

837. ins = BCINS_AD(op+(BC_ISEQP-BC_ISEQV), ra, const_pri(e2));

838. break;

839. case VKSTR:

840. ins = BCINS_AD(op+(BC_ISEQS-BC_ISEQV), ra, const_str(fs, e2));

841. break;

842. case VKNUM:

843. ins = BCINS_AD(op+(BC_ISEQN-BC_ISEQV), ra, const_num(fs, e2));

844. break;

845. default:

846. ins = BCINS_AD(op, ra, expr_toanyreg(fs, e2));

847. break;

848. }

849. } else {

850. uint32_t op = opr-OPR_LT+BC_ISLT;

851. BCReg ra, rd;

852. if ((op-BC_ISLT) & 1) {  /* GT -> LT, GE -> LE */

853. e1 = e2; e2 = eret;  /* Swap operands. */

854. op = ((op-BC_ISLT)^3)+BC_ISLT;

855. expr_toval(fs, e1);

856. }

857. rd = expr_toanyreg(fs, e2);

858. ra = expr_toanyreg(fs, e1);

859. ins = BCINS_AD(op, ra, rd);

860. }

861. /* Using expr_free might cause asserts if the order is wrong. */

862. if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;

863. if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;

864. bcemit_INS(fs, ins);

865. eret->u.s.info = bcemit_jmp(fs);

866. eret->k = VJMP;

867. }

869. /* Fixup left side of binary operator. */

870. ‌static void bcemit_binop_left(FuncState *fs, BinOpr op, ExpDesc *e)

871. {

872. if (op == OPR_AND) {

873. bcemit_branch_t(fs, e);

874. } else if (op == OPR_OR) {

875. bcemit_branch_f(fs, e);

876. } else if (op == OPR_CONCAT) {

877. expr_tonextreg(fs, e);

878. } else if (op == OPR_EQ || op == OPR_NE) {

879. if (!expr_isk_nojump(e)) expr_toanyreg(fs, e);

880. } else {

881. if (!expr_isnumk_nojump(e)) expr_toanyreg(fs, e);

882. }

883. }

885. /* Emit binary operator. */

886. ‌static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2)

887. {

888. if (op <= OPR_POW) {

889. bcemit_arith(fs, op, e1, e2);

890. } else if (op == OPR_AND) {

891. lua_assert(e1->t == NO_JMP);  /* List must be closed. */

892. expr_discharge(fs, e2);

893. jmp_append(fs, &e2->f, e1->f);

894. *e1 = *e2;

895. } else if (op == OPR_OR) {

896. lua_assert(e1->f == NO_JMP);  /* List must be closed. */

897. expr_discharge(fs, e2);

898. jmp_append(fs, &e2->t, e1->t);

899. *e1 = *e2;

900. } else if (op == OPR_CONCAT) {

901. expr_toval(fs, e2);

902. if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) {

903. lua_assert(e1->u.s.info == bc_b(*bcptr(fs, e2))-1);

904. expr_free(fs, e1);

905. setbc_b(bcptr(fs, e2), e1->u.s.info);

906. e1->u.s.info = e2->u.s.info;

907. } else {

908. expr_tonextreg(fs, e2);

909. expr_free(fs, e2);

910. expr_free(fs, e1);

911. e1->u.s.info = bcemit_ABC(fs, BC_CAT, 0, e1->u.s.info, e2->u.s.info);

912. }

913. e1->k = VRELOCABLE;

914. } else {

915. lua_assert(op == OPR_NE || op == OPR_EQ ||

916. op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT);

917. bcemit_comp(fs, op, e1, e2);

918. }

919. }

921. /* Emit unary operator. */

922. ‌static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e)

923. {

924. if (op == BC_NOT) {

925. /* Swap true and false lists. */

926. { BCPos temp = e->f; e->f = e->t; e->t = temp; }

927. jmp_dropval(fs, e->f);

928. jmp_dropval(fs, e->t);

929. expr_discharge(fs, e);

930. if (e->k == VKNIL || e->k == VKFALSE) {

931. e->k = VKTRUE;

932. return;

933. } else if (expr_isk(e) || (LJ_HASFFI && e->k == VKCDATA)) {

934. e->k = VKFALSE;

935. return;

936. } else if (e->k == VJMP) {

937. invertcond(fs, e);

938. return;

939. } else if (e->k == VRELOCABLE) {

940. bcreg_reserve(fs, 1);

941. setbc_a(bcptr(fs, e), fs->freereg-1);

942. e->u.s.info = fs->freereg-1;

943. e->k = VNONRELOC;

944. } else {

945. lua_assert(e->k == VNONRELOC);

946. }

947. } else {

948. lua_assert(op == BC_UNM || op == BC_LEN);

949. if (op == BC_UNM && !expr_hasjump(e)) {  /* Constant-fold negations. */

950. #if LJ_HASFFI

951. if (e->k == VKCDATA) {  /* Fold in-place since cdata is not interned. */

952. GCcdata *cd = cdataV(&e->u.nval);

953. int64_t *p = (int64_t *)cdataptr(cd);

954. if (cd->ctypeid == CTID_COMPLEX_DOUBLE)

955. p[1] ^= (int64_t)U64x(80000000,00000000);

956. else

957. *p = -*p;

958. return;

959. } else

960. #endif

961. if (expr_isnumk(e) && !expr_numiszero(e)) {  /* Avoid folding to -0. */

962. TValue *o = expr_numtv(e);

963. if (tvisint(o)) {

964. int32_t k = intV(o);

965. if (k == -k)

966. setnumV(o, -(lua_Number)k);

967. else

968. setintV(o, -k);

969. return;

970. } else {

971. o->u64 ^= U64x(80000000,00000000);

972. return;

973. }

974. }

975. }

976. expr_toanyreg(fs, e);

977. }

978. expr_free(fs, e);

979. e->u.s.info = bcemit_AD(fs, op, 0, e->u.s.info);

980. e->k = VRELOCABLE;

981. }

983. /* -- Lexer support ------------------------------------------------------- */

985. /* Check and consume optional token. */

986. ‌static int lex_opt(LexState *ls, LexToken tok)

987. {

988. if (ls->tok == tok) {

989. lj_lex_next(ls);

990. return 1;

991. }

992. return 0;

993. }

995. /* Check and consume token. */

996. ‌static void lex_check(LexState *ls, LexToken tok)

997. {

998. if (ls->tok != tok)

999. err_token(ls, tok);

1000. lj_lex_next(ls);

1001. }

1003. /* Check for matching token. */

1004. ‌static void lex_match(LexState *ls, LexToken what, LexToken who, BCLine line)

1005. {

1006. if (!lex_opt(ls, what)) {

1007. if (line == ls->linenumber) {

1008. err_token(ls, what);

1009. } else {

1010. const char *swhat = lj_lex_token2str(ls, what);

1011. const char *swho = lj_lex_token2str(ls, who);

1012. lj_lex_error(ls, ls->tok, LJ_ERR_XMATCH, swhat, swho, line);

1013. }

1014. }

1015. }

1017. /* Check for string token. */

1018. ‌static GCstr *lex_str(LexState *ls)

1019. {

1020. GCstr *s;

1021. if (ls->tok != TK_name && (LJ_52 || ls->tok != TK_goto))

1022. err_token(ls, TK_name);

1023. s = strV(&ls->tokval);

1024. lj_lex_next(ls);

1025. return s;

1026. }

1028. /* -- Variable handling --------------------------------------------------- */

1030. ‌#define var_get(ls, fs, i)        ((ls)->vstack[(fs)->varmap[(i)]])

1032. /* Define a new local variable. */

1033. ‌static void var_new(LexState *ls, BCReg n, GCstr *name)

1034. {

1035. FuncState *fs = ls->fs;

1036. MSize vtop = ls->vtop;

1037. checklimit(fs, fs->nactvar+n, LJ_MAX_LOCVAR, "local variables");

1038. if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {

1039. if (ls->sizevstack >= LJ_MAX_VSTACK)

1040. lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);

1041. lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);

1042. }

1043. lua_assert((uintptr_t)name < VARNAME__MAX ||

1044. lj_tab_getstr(fs->kt, name) != NULL);

1045. /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */

1046. setgcref(ls->vstack[vtop].name, obj2gco(name));

1047. fs->varmap[fs->nactvar+n] = (uint16_t)vtop;

1048. ls->vtop = vtop+1;

1049. }

1051. ‌#define var_new_lit(ls, n, v) \

1052. var_new(ls, (n), lj_parse_keepstr(ls, "" v, sizeof(v)-1))

1054. ‌#define var_new_fixed(ls, n, vn) \

1055. var_new(ls, (n), (GCstr *)(uintptr_t)(vn))

1057. /* Add local variables. */

1058. ‌static void var_add(LexState *ls, BCReg nvars)

1059. {

1060. FuncState *fs = ls->fs;

1061. BCReg nactvar = fs->nactvar;

1062. while (nvars--) {

1063. VarInfo *v = &var_get(ls, fs, nactvar);

1064. v->startpc = fs->pc;

1065. v->slot = nactvar++;

1066. v->info = 0;

1067. }

1068. fs->nactvar = nactvar;

1069. }

1071. /* Remove local variables. */

1072. ‌static void var_remove(LexState *ls, BCReg tolevel)

1073. {

1074. FuncState *fs = ls->fs;

1075. while (fs->nactvar > tolevel)

1076. var_get(ls, fs, --fs->nactvar).endpc = fs->pc;

1077. }

1079. /* Lookup local variable name. */

1080. ‌static BCReg var_lookup_local(FuncState *fs, GCstr *n)

1081. {

1082. int i;

1083. for (i = fs->nactvar-1; i >= 0; i--) {

1084. if (n == strref(var_get(fs->ls, fs, i).name))

1085. return (BCReg)i;

1086. }

1087. return (BCReg)-1;  /* Not found. */

1088. }

1090. /* Lookup or add upvalue index. */

1091. ‌static MSize var_lookup_uv(FuncState *fs, MSize vidx, ExpDesc *e)

1092. {

1093. MSize i, n = fs->nuv;

1094. for (i = 0; i < n; i++)

1095. if (fs->uvmap[i] == vidx)

1096. return i;  /* Already exists. */

1097. /* Otherwise create a new one. */

1098. checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues");

1099. lua_assert(e->k == VLOCAL || e->k == VUPVAL);

1100. fs->uvmap[n] = (uint16_t)vidx;

1101. fs->uvtmp[n] = (uint16_t)(e->k == VLOCAL ? vidx : LJ_MAX_VSTACK+e->u.s.info);

1102. fs->nuv = n+1;

1103. return n;

1104. }

1106. /* Forward declaration. */

1107. static void fscope_uvmark(FuncState *fs, BCReg level);

1109. /* Recursively lookup variables in enclosing functions. */

1110. ‌static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first)

1111. {

1112. if (fs) {

1113. BCReg reg = var_lookup_local(fs, name);

1114. if ((int32_t)reg >= 0) {  /* Local in this function? */

1115. expr_init(e, VLOCAL, reg);

1116. if (!first)

1117. fscope_uvmark(fs, reg);  /* Scope now has an upvalue. */

1118. return (MSize)(e->u.s.aux = (uint32_t)fs->varmap[reg]);

1119. } else {

1120. MSize vidx = var_lookup_(fs->prev, name, e, 0);  /* Var in outer func? */

1121. if ((int32_t)vidx >= 0) {  /* Yes, make it an upvalue here. */

1122. e->u.s.info = (uint8_t)var_lookup_uv(fs, vidx, e);

1123. e->k = VUPVAL;

1124. return vidx;

1125. }

1126. }

1127. } else {  /* Not found in any function, must be a global. */

1128. expr_init(e, VGLOBAL, 0);

1129. e->u.sval = name;

1130. }

1131. return (MSize)-1;  /* Global. */

1132. }

1134. /* Lookup variable name. */

1135. ‌#define var_lookup(ls, e) \

1136. var_lookup_((ls)->fs, lex_str(ls), (e), 1)

1138. /* -- Goto an label handling ---------------------------------------------- */

1140. /* Add a new goto or label. */

1141. ‌static MSize gola_new(LexState *ls, GCstr *name, uint8_t info, BCPos pc)

1142. {

1143. FuncState *fs = ls->fs;

1144. MSize vtop = ls->vtop;

1145. if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {

1146. if (ls->sizevstack >= LJ_MAX_VSTACK)

1147. lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);

1148. lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);

1149. }

1150. lua_assert(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL);

1151. /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */

1152. setgcref(ls->vstack[vtop].name, obj2gco(name));

1153. ls->vstack[vtop].startpc = pc;

1154. ls->vstack[vtop].slot = (uint8_t)fs->nactvar;

1155. ls->vstack[vtop].info = info;

1156. ls->vtop = vtop+1;

1157. return vtop;

1158. }

1160. ‌#define gola_isgoto(v)                ((v)->info & VSTACK_GOTO)

1161. ‌#define gola_islabel(v)                ((v)->info & VSTACK_LABEL)

1162. ‌#define gola_isgotolabel(v)        ((v)->info & (VSTACK_GOTO|VSTACK_LABEL))

1164. /* Patch goto to jump to label. */

1165. ‌static void gola_patch(LexState *ls, VarInfo *vg, VarInfo *vl)

1166. {

1167. FuncState *fs = ls->fs;

1168. BCPos pc = vg->startpc;

1169. setgcrefnull(vg->name);  /* Invalidate pending goto. */

1170. setbc_a(&fs->bcbase[pc].ins, vl->slot);

1171. jmp_patch(fs, pc, vl->startpc);

1172. }

1174. /* Patch goto to close upvalues. */

1175. ‌static void gola_close(LexState *ls, VarInfo *vg)

1176. {

1177. FuncState *fs = ls->fs;

1178. BCPos pc = vg->startpc;

1179. BCIns *ip = &fs->bcbase[pc].ins;

1180. lua_assert(gola_isgoto(vg));

1181. lua_assert(bc_op(*ip) == BC_JMP || bc_op(*ip) == BC_UCLO);

1182. setbc_a(ip, vg->slot);

1183. if (bc_op(*ip) == BC_JMP) {

1184. BCPos next = jmp_next(fs, pc);

1185. if (next != NO_JMP) jmp_patch(fs, next, pc);  /* Jump to UCLO. */

1186. setbc_op(ip, BC_UCLO);  /* Turn into UCLO. */

1187. setbc_j(ip, NO_JMP);

1188. }

1189. }

1191. /* Resolve pending forward gotos for label. */

1192. ‌static void gola_resolve(LexState *ls, FuncScope *bl, MSize idx)

1193. {

1194. VarInfo *vg = ls->vstack + bl->vstart;

1195. VarInfo *vl = ls->vstack + idx;

1196. for (; vg < vl; vg++)

1197. if (gcrefeq(vg->name, vl->name) && gola_isgoto(vg)) {

1198. if (vg->slot < vl->slot) {

1199. GCstr *name = strref(var_get(ls, ls->fs, vg->slot).name);

1200. lua_assert((uintptr_t)name >= VARNAME__MAX);

1201. ls->linenumber = ls->fs->bcbase[vg->startpc].line;

1202. lua_assert(strref(vg->name) != NAME_BREAK);

1203. lj_lex_error(ls, 0, LJ_ERR_XGSCOPE,

1204. strdata(strref(vg->name)), strdata(name));

1205. }

1206. gola_patch(ls, vg, vl);

1207. }

1208. }

1210. /* Fixup remaining gotos and labels for scope. */

1211. ‌static void gola_fixup(LexState *ls, FuncScope *bl)

1212. {

1213. VarInfo *v = ls->vstack + bl->vstart;

1214. VarInfo *ve = ls->vstack + ls->vtop;

1215. for (; v < ve; v++) {

1216. GCstr *name = strref(v->name);

1217. if (name != NULL) {  /* Only consider remaining valid gotos/labels. */

1218. if (gola_islabel(v)) {

1219. VarInfo *vg;

1220. setgcrefnull(v->name);  /* Invalidate label that goes out of scope. */

1221. for (vg = v+1; vg < ve; vg++)  /* Resolve pending backward gotos. */

1222. if (strref(vg->name) == name && gola_isgoto(vg)) {

1223. if ((bl->flags&FSCOPE_UPVAL) && vg->slot > v->slot)

1224. gola_close(ls, vg);

1225. gola_patch(ls, vg, v);

1226. }

1227. } else if (gola_isgoto(v)) {

1228. if (bl->prev) {  /* Propagate goto or break to outer scope. */

1229. bl->prev->flags |= name == NAME_BREAK ? FSCOPE_BREAK : FSCOPE_GOLA;

1230. v->slot = bl->nactvar;

1231. if ((bl->flags & FSCOPE_UPVAL))

1232. gola_close(ls, v);

1233. } else {  /* No outer scope: undefined goto label or no loop. */

1234. ls->linenumber = ls->fs->bcbase[v->startpc].line;

1235. if (name == NAME_BREAK)

1236. lj_lex_error(ls, 0, LJ_ERR_XBREAK);

1237. else

1238. lj_lex_error(ls, 0, LJ_ERR_XLUNDEF, strdata(name));

1239. }

1240. }

1241. }

1242. }

1243. }

1245. /* Find existing label. */

1246. ‌static VarInfo *gola_findlabel(LexState *ls, GCstr *name)

1247. {

1248. VarInfo *v = ls->vstack + ls->fs->bl->vstart;

1249. VarInfo *ve = ls->vstack + ls->vtop;

1250. for (; v < ve; v++)

1251. if (strref(v->name) == name && gola_islabel(v))

1252. return v;

1253. return NULL;

1254. }

1256. /* -- Scope handling ------------------------------------------------------ */

1258. /* Begin a scope. */

1259. ‌static void fscope_begin(FuncState *fs, FuncScope *bl, int flags)

1260. {

1261. bl->nactvar = (uint8_t)fs->nactvar;

1262. bl->flags = flags;

1263. bl->vstart = fs->ls->vtop;

1264. bl->prev = fs->bl;

1265. fs->bl = bl;

1266. lua_assert(fs->freereg == fs->nactvar);

1267. }

1269. /* End a scope. */

1270. ‌static void fscope_end(FuncState *fs)

1271. {

1272. FuncScope *bl = fs->bl;

1273. LexState *ls = fs->ls;

1274. fs->bl = bl->prev;

1275. var_remove(ls, bl->nactvar);

1276. fs->freereg = fs->nactvar;

1277. lua_assert(bl->nactvar == fs->nactvar);

1278. if ((bl->flags & (FSCOPE_UPVAL|FSCOPE_NOCLOSE)) == FSCOPE_UPVAL)

1279. bcemit_AJ(fs, BC_UCLO, bl->nactvar, 0);

1280. if ((bl->flags & FSCOPE_BREAK)) {

1281. if ((bl->flags & FSCOPE_LOOP)) {

1282. MSize idx = gola_new(ls, NAME_BREAK, VSTACK_LABEL, fs->pc);

1283. ls->vtop = idx;  /* Drop break label immediately. */

1284. gola_resolve(ls, bl, idx);

1285. return;

1286. }  /* else: need the fixup step to propagate the breaks. */

1287. } else if (!(bl->flags & FSCOPE_GOLA)) {

1288. return;

1289. }

1290. gola_fixup(ls, bl);

1291. }

1293. /* Mark scope as having an upvalue. */

1294. ‌static void fscope_uvmark(FuncState *fs, BCReg level)

1295. {

1296. FuncScope *bl;

1297. for (bl = fs->bl; bl && bl->nactvar > level; bl = bl->prev)

1298. ;

1299. if (bl)

1300. bl->flags |= FSCOPE_UPVAL;

1301. }

1303. /* -- Function state management ------------------------------------------- */

1305. /* Fixup bytecode for prototype. */

1306. ‌static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, MSize n)

1307. {

1308. BCInsLine *base = fs->bcbase;

1309. MSize i;

1310. pt->sizebc = n;

1311. bc[0] = BCINS_AD((fs->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF,

1312. fs->framesize, 0);

1313. for (i = 1; i < n; i++)

1314. bc[i] = base[i].ins;

1315. }

1317. /* Fixup upvalues for child prototype, step #2. */

1318. ‌static void fs_fixup_uv2(FuncState *fs, GCproto *pt)

1319. {

1320. VarInfo *vstack = fs->ls->vstack;

1321. uint16_t *uv = proto_uv(pt);

1322. MSize i, n = pt->sizeuv;

1323. for (i = 0; i < n; i++) {

1324. VarIndex vidx = uv[i];

1325. if (vidx >= LJ_MAX_VSTACK)

1326. uv[i] = vidx - LJ_MAX_VSTACK;

1327. else if ((vstack[vidx].info & VSTACK_VAR_RW))

1328. uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL;

1329. else

1330. uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL | PROTO_UV_IMMUTABLE;

1331. }

1332. }

1334. /* Fixup constants for prototype. */

1335. ‌static void fs_fixup_k(FuncState *fs, GCproto *pt, void *kptr)

1336. {

1337. GCtab *kt;

1338. TValue *array;

1339. Node *node;

1340. MSize i, hmask;

1341. checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants");

1342. checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants");

1343. setmref(pt->k, kptr);

1344. pt->sizekn = fs->nkn;

1345. pt->sizekgc = fs->nkgc;

1346. kt = fs->kt;

1347. array = tvref(kt->array);

1348. for (i = 0; i < kt->asize; i++)

1349. if (tvhaskslot(&array[i])) {

1350. TValue *tv = &((TValue *)kptr)[tvkslot(&array[i])];

1351. if (LJ_DUALNUM)

1352. setintV(tv, (int32_t)i);

1353. else

1354. setnumV(tv, (lua_Number)i);

1355. }

1356. node = noderef(kt->node);

1357. hmask = kt->hmask;

1358. for (i = 0; i <= hmask; i++) {

1359. Node *n = &node[i];

1360. if (tvhaskslot(&n->val)) {

1361. ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val);

1362. lua_assert(!tvisint(&n->key));

1363. if (tvisnum(&n->key)) {

1364. TValue *tv = &((TValue *)kptr)[kidx];

1365. if (LJ_DUALNUM) {

1366. lua_Number nn = numV(&n->key);

1367. int32_t k = lj_num2int(nn);

1368. lua_assert(!tvismzero(&n->key));

1369. if ((lua_Number)k == nn)

1370. setintV(tv, k);

1371. else

1372. *tv = n->key;

1373. } else {

1374. *tv = n->key;

1375. }

1376. } else {

1377. GCobj *o = gcV(&n->key);

1378. setgcref(((GCRef *)kptr)[~kidx], o);

1379. lj_gc_objbarrier(fs->L, pt, o);

1380. if (tvisproto(&n->key))

1381. fs_fixup_uv2(fs, gco2pt(o));

1382. }

1383. }

1384. }

1385. }

1387. /* Fixup upvalues for prototype, step #1. */

1388. ‌static void fs_fixup_uv1(FuncState *fs, GCproto *pt, uint16_t *uv)

1389. {

1390. setmref(pt->uv, uv);

1391. pt->sizeuv = fs->nuv;

1392. memcpy(uv, fs->uvtmp, fs->nuv*sizeof(VarIndex));

1393. }

1395. #ifndef LUAJIT_DISABLE_DEBUGINFO

1396. /* Prepare lineinfo for prototype. */

1397. ‌static size_t fs_prep_line(FuncState *fs, BCLine numline)

1398. {

1399. return (fs->pc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2);

1400. }

1402. /* Fixup lineinfo for prototype. */

1403. ‌static void fs_fixup_line(FuncState *fs, GCproto *pt,

1404. void *lineinfo, BCLine numline)

1405. {

1406. BCInsLine *base = fs->bcbase + 1;

1407. BCLine first = fs->linedefined;

1408. MSize i = 0, n = fs->pc-1;

1409. pt->firstline = fs->linedefined;

1410. pt->numline = numline;

1411. setmref(pt->lineinfo, lineinfo);

1412. if (LJ_LIKELY(numline < 256)) {

1413. uint8_t *li = (uint8_t *)lineinfo;

1414. do {

1415. BCLine delta = base[i].line - first;

1416. lua_assert(delta >= 0 && delta < 256);

1417. li[i] = (uint8_t)delta;

1418. } while (++i < n);

1419. } else if (LJ_LIKELY(numline < 65536)) {

1420. uint16_t *li = (uint16_t *)lineinfo;

1421. do {

1422. BCLine delta = base[i].line - first;

1423. lua_assert(delta >= 0 && delta < 65536);

1424. li[i] = (uint16_t)delta;

1425. } while (++i < n);

1426. } else {

1427. uint32_t *li = (uint32_t *)lineinfo;

1428. do {

1429. BCLine delta = base[i].line - first;

1430. lua_assert(delta >= 0);

1431. li[i] = (uint32_t)delta;

1432. } while (++i < n);

1433. }

1434. }

1436. /* Prepare variable info for prototype. */

1437. ‌static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar)

1438. {

1439. VarInfo *vs =ls->vstack, *ve;

1440. MSize i, n;

1441. BCPos lastpc;

1442. lj_buf_reset(&ls->sb);  /* Copy to temp. string buffer. */

1443. /* Store upvalue names. */

1444. for (i = 0, n = fs->nuv; i < n; i++) {

1445. GCstr *s = strref(vs[fs->uvmap[i]].name);

1446. MSize len = s->len+1;

1447. char *p = lj_buf_more(&ls->sb, len);

1448. p = lj_buf_wmem(p, strdata(s), len);

1449. setsbufP(&ls->sb, p);

1450. }

1451. *ofsvar = sbuflen(&ls->sb);

1452. lastpc = 0;

1453. /* Store local variable names and compressed ranges. */

1454. for (ve = vs + ls->vtop, vs += fs->vbase; vs < ve; vs++) {

1455. if (!gola_isgotolabel(vs)) {

1456. GCstr *s = strref(vs->name);

1457. BCPos startpc;

1458. char *p;

1459. if ((uintptr_t)s < VARNAME__MAX) {

1460. p = lj_buf_more(&ls->sb, 1 + 2*5);

1461. *p++ = (char)(uintptr_t)s;

1462. } else {

1463. MSize len = s->len+1;

1464. p = lj_buf_more(&ls->sb, len + 2*5);

1465. p = lj_buf_wmem(p, strdata(s), len);

1466. }

1467. startpc = vs->startpc;

1468. p = lj_strfmt_wuleb128(p, startpc-lastpc);

1469. p = lj_strfmt_wuleb128(p, vs->endpc-startpc);

1470. setsbufP(&ls->sb, p);

1471. lastpc = startpc;

1472. }

1473. }

1474. lj_buf_putb(&ls->sb, '\0');  /* Terminator for varinfo. */

1475. return sbuflen(&ls->sb);

1476. }

1478. /* Fixup variable info for prototype. */

1479. ‌static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar)

1480. {

1481. setmref(pt->uvinfo, p);

1482. setmref(pt->varinfo, (char *)p + ofsvar);

1483. memcpy(p, sbufB(&ls->sb), sbuflen(&ls->sb));  /* Copy from temp. buffer. */

1484. }

1485. #else

1487. /* Initialize with empty debug info, if disabled. */

1488. ‌#define fs_prep_line(fs, numline)                (UNUSED(numline), 0)

1489. ‌#define fs_fixup_line(fs, pt, li, numline) \

1490. pt->firstline = pt->numline = 0, setmref((pt)->lineinfo, NULL)

1491. ‌#define fs_prep_var(ls, fs, ofsvar)                (UNUSED(ofsvar), 0)

1492. ‌#define fs_fixup_var(ls, pt, p, ofsvar) \

1493. setmref((pt)->uvinfo, NULL), setmref((pt)->varinfo, NULL)

1495. #endif

1497. /* Check if bytecode op returns. */

1498. ‌static int bcopisret(BCOp op)

1499. {

1500. switch (op) {

1501. case BC_CALLMT: case BC_CALLT:

1502. case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:

1503. return 1;

1504. default:

1505. return 0;

1506. }

1507. }

1509. /* Fixup return instruction for prototype. */

1510. ‌static void fs_fixup_ret(FuncState *fs)

1511. {

1512. BCPos lastpc = fs->pc;

1513. if (lastpc <= fs->lasttarget || !bcopisret(bc_op(fs->bcbase[lastpc-1].ins))) {

1514. if ((fs->bl->flags & FSCOPE_UPVAL))

1515. bcemit_AJ(fs, BC_UCLO, 0, 0);

1516. bcemit_AD(fs, BC_RET0, 0, 1);  /* Need final return. */

1517. }

1518. fs->bl->flags |= FSCOPE_NOCLOSE;  /* Handled above. */

1519. fscope_end(fs);

1520. lua_assert(fs->bl == NULL);

1521. /* May need to fixup returns encoded before first function was created. */

1522. if (fs->flags & PROTO_FIXUP_RETURN) {

1523. BCPos pc;

1524. for (pc = 1; pc < lastpc; pc++) {

1525. BCIns ins = fs->bcbase[pc].ins;

1526. BCPos offset;

1527. switch (bc_op(ins)) {

1528. case BC_CALLMT: case BC_CALLT:

1529. case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:

1530. offset = bcemit_INS(fs, ins);  /* Copy original instruction. */

1531. fs->bcbase[offset].line = fs->bcbase[pc].line;

1532. offset = offset-(pc+1)+BCBIAS_J;

1533. if (offset > BCMAX_D)

1534. err_syntax(fs->ls, LJ_ERR_XFIXUP);

1535. /* Replace with UCLO plus branch. */

1536. fs->bcbase[pc].ins = BCINS_AD(BC_UCLO, 0, offset);

1537. break;

1538. case BC_UCLO:

1539. return;  /* We're done. */

1540. default:

1541. break;

1542. }

1543. }

1544. }

1545. }

1547. /* Finish a FuncState and return the new prototype. */

1548. ‌static GCproto *fs_finish(LexState *ls, BCLine line)

1549. {

1550. lua_State *L = ls->L;

1551. FuncState *fs = ls->fs;

1552. BCLine numline = line - fs->linedefined;

1553. size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar;

1554. GCproto *pt;

1556. /* Apply final fixups. */

1557. fs_fixup_ret(fs);

1559. /* Calculate total size of prototype including all colocated arrays. */

1560. sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef);

1561. sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1);

1562. ofsk = sizept; sizept += fs->nkn*sizeof(TValue);

1563. ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2;

1564. ofsli = sizept; sizept += fs_prep_line(fs, numline);

1565. ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar);

1567. /* Allocate prototype and initialize its fields. */

1568. pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept);

1569. pt->gct = ~LJ_TPROTO;

1570. pt->sizept = (MSize)sizept;

1571. pt->trace = 0;

1572. pt->flags = (uint8_t)(fs->flags & ~(PROTO_HAS_RETURN|PROTO_FIXUP_RETURN));

1573. pt->numparams = fs->numparams;

1574. pt->framesize = fs->framesize;

1575. setgcref(pt->chunkname, obj2gco(ls->chunkname));

1577. /* Close potentially uninitialized gap between bc and kgc. */

1578. *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(fs->nkgc+1)) = 0;

1579. fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), fs->pc);

1580. fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk));

1581. fs_fixup_uv1(fs, pt, (uint16_t *)((char *)pt + ofsuv));

1582. fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline);

1583. fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar);

1585. lj_vmevent_send(L, BC,

1586. setprotoV(L, L->top++, pt);

1587. );

1589. L->top--;  /* Pop table of constants. */

1590. ls->vtop = fs->vbase;  /* Reset variable stack. */

1591. ls->fs = fs->prev;

1592. lua_assert(ls->fs != NULL || ls->tok == TK_eof);

1593. return pt;

1594. }

1596. /* Initialize a new FuncState. */

1597. ‌static void fs_init(LexState *ls, FuncState *fs)

1598. {

1599. lua_State *L = ls->L;

1600. fs->prev = ls->fs; ls->fs = fs;  /* Append to list. */

1601. fs->ls = ls;

1602. fs->vbase = ls->vtop;

1603. fs->L = L;

1604. fs->pc = 0;

1605. fs->lasttarget = 0;

1606. fs->jpc = NO_JMP;

1607. fs->freereg = 0;

1608. fs->nkgc = 0;

1609. fs->nkn = 0;

1610. fs->nactvar = 0;

1611. fs->nuv = 0;

1612. fs->bl = NULL;

1613. fs->flags = 0;

1614. fs->framesize = 1;  /* Minimum frame size. */

1615. fs->kt = lj_tab_new(L, 0, 0);

1616. /* Anchor table of constants in stack to avoid being collected. */

1617. settabV(L, L->top, fs->kt);

1618. incr_top(L);

1619. }

1621. /* -- Expressions --------------------------------------------------------- */

1623. /* Forward declaration. */

1624. static void expr(LexState *ls, ExpDesc *v);

1626. /* Return string expression. */

1627. ‌static void expr_str(LexState *ls, ExpDesc *e)

1628. {

1629. expr_init(e, VKSTR, 0);

1630. e->u.sval = lex_str(ls);

1631. }

1633. /* Return index expression. */

1634. ‌static void expr_index(FuncState *fs, ExpDesc *t, ExpDesc *e)

1635. {

1636. /* Already called: expr_toval(fs, e). */

1637. t->k = VINDEXED;

1638. if (expr_isnumk(e)) {

1639. #if LJ_DUALNUM

1640. if (tvisint(expr_numtv(e))) {

1641. int32_t k = intV(expr_numtv(e));

1642. if (checku8(k)) {

1643. t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */

1644. return;

1645. }

1646. }

1647. #else

1648. lua_Number n = expr_numberV(e);

1649. int32_t k = lj_num2int(n);

1650. if (checku8(k) && n == (lua_Number)k) {

1651. t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */

1652. return;

1653. }

1654. #endif

1655. } else if (expr_isstrk(e)) {

1656. BCReg idx = const_str(fs, e);

1657. if (idx <= BCMAX_C) {

1658. t->u.s.aux = ~idx;  /* -256..-1: const string key */

1659. return;

1660. }

1661. }

1662. t->u.s.aux = expr_toanyreg(fs, e);  /* 0..255: register */

1663. }

1665. /* Parse index expression with named field. */

1666. ‌static void expr_field(LexState *ls, ExpDesc *v)

1667. {

1668. FuncState *fs = ls->fs;

1669. ExpDesc key;

1670. expr_toanyreg(fs, v);

1671. lj_lex_next(ls);  /* Skip dot or colon. */

1672. expr_str(ls, &key);

1673. expr_index(fs, v, &key);

1674. }

1676. /* Parse index expression with brackets. */

1677. ‌static void expr_bracket(LexState *ls, ExpDesc *v)

1678. {

1679. lj_lex_next(ls);  /* Skip '['. */

1680. expr(ls, v);

1681. expr_toval(ls->fs, v);

1682. lex_check(ls, ']');

1683. }

1685. /* Get value of constant expression. */

1686. ‌static void expr_kvalue(TValue *v, ExpDesc *e)

1687. {

1688. if (e->k <= VKTRUE) {

1689. setpriV(v, ~(uint32_t)e->k);

1690. } else if (e->k == VKSTR) {

1691. setgcVraw(v, obj2gco(e->u.sval), LJ_TSTR);

1692. } else {

1693. lua_assert(tvisnumber(expr_numtv(e)));

1694. *v = *expr_numtv(e);

1695. }

1696. }

1698. /* Parse table constructor expression. */

1699. ‌static void expr_table(LexState *ls, ExpDesc *e)

1700. {

1701. FuncState *fs = ls->fs;

1702. BCLine line = ls->linenumber;

1703. GCtab *t = NULL;

1704. int vcall = 0, needarr = 0, fixt = 0;

1705. uint32_t narr = 1;  /* First array index. */

1706. uint32_t nhash = 0;  /* Number of hash entries. */

1707. BCReg freg = fs->freereg;

1708. BCPos pc = bcemit_AD(fs, BC_TNEW, freg, 0);

1709. expr_init(e, VNONRELOC, freg);

1710. bcreg_reserve(fs, 1);

1711. freg++;

1712. lex_check(ls, '{');

1713. while (ls->tok != '}') {

1714. ExpDesc key, val;

1715. vcall = 0;

1716. if (ls->tok == '[') {

1717. expr_bracket(ls, &key);  /* Already calls expr_toval. */

1718. if (!expr_isk(&key)) expr_index(fs, e, &key);

1719. if (expr_isnumk(&key) && expr_numiszero(&key)) needarr = 1; else nhash++;

1720. lex_check(ls, '=');

1721. } else if ((ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) &&

1722. lj_lex_lookahead(ls) == '=') {

1723. expr_str(ls, &key);

1724. lex_check(ls, '=');

1725. nhash++;

1726. } else {

1727. expr_init(&key, VKNUM, 0);

1728. setintV(&key.u.nval, (int)narr);

1729. narr++;

1730. needarr = vcall = 1;

1731. }

1732. expr(ls, &val);

1733. if (expr_isk(&key) && key.k != VKNIL &&

1734. (key.k == VKSTR || expr_isk_nojump(&val))) {

1735. TValue k, *v;

1736. if (!t) {  /* Create template table on demand. */

1737. BCReg kidx;

1738. t = lj_tab_new(fs->L, needarr ? narr : 0, hsize2hbits(nhash));

1739. kidx = const_gc(fs, obj2gco(t), LJ_TTAB);

1740. fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx);

1741. }

1742. vcall = 0;

1743. expr_kvalue(&k, &key);

1744. v = lj_tab_set(fs->L, t, &k);

1745. lj_gc_anybarriert(fs->L, t);

1746. if (expr_isk_nojump(&val)) {  /* Add const key/value to template table. */

1747. expr_kvalue(v, &val);

1748. } else {  /* Otherwise create dummy string key (avoids lj_tab_newkey). */

1749. settabV(fs->L, v, t);  /* Preserve key with table itself as value. */

1750. fixt = 1;   /* Fix this later, after all resizes. */

1751. goto nonconst;

1752. }

1753. } else {

1754. nonconst:

1755. if (val.k != VCALL) { expr_toanyreg(fs, &val); vcall = 0; }

1756. if (expr_isk(&key)) expr_index(fs, e, &key);

1757. bcemit_store(fs, e, &val);

1758. }

1759. fs->freereg = freg;

1760. if (!lex_opt(ls, ',') && !lex_opt(ls, ';')) break;

1761. }

1762. lex_match(ls, '}', '{', line);

1763. if (vcall) {

1764. BCInsLine *ilp = &fs->bcbase[fs->pc-1];

1765. ExpDesc en;

1766. lua_assert(bc_a(ilp->ins) == freg &&

1767. bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB));

1768. expr_init(&en, VKNUM, 0);

1769. en.u.nval.u32.lo = narr-1;

1770. en.u.nval.u32.hi = 0x43300000;  /* Biased integer to avoid denormals. */

1771. if (narr > 256) { fs->pc--; ilp--; }

1772. ilp->ins = BCINS_AD(BC_TSETM, freg, const_num(fs, &en));

1773. setbc_b(&ilp[-1].ins, 0);

1774. }

1775. if (pc == fs->pc-1) {  /* Make expr relocable if possible. */

1776. e->u.s.info = pc;

1777. fs->freereg--;

1778. e->k = VRELOCABLE;

1779. } else {

1780. e->k = VNONRELOC;  /* May have been changed by expr_index. */

1781. }

1782. if (!t) {  /* Construct TNEW RD: hhhhhaaaaaaaaaaa. */

1783. BCIns *ip = &fs->bcbase[pc].ins;

1784. if (!needarr) narr = 0;

1785. else if (narr < 3) narr = 3;

1786. else if (narr > 0x7ff) narr = 0x7ff;

1787. setbc_d(ip, narr|(hsize2hbits(nhash)<<11));

1788. } else {

1789. if (needarr && t->asize < narr)

1790. lj_tab_reasize(fs->L, t, narr-1);

1791. if (fixt) {  /* Fix value for dummy keys in template table. */

1792. Node *node = noderef(t->node);

1793. uint32_t i, hmask = t->hmask;

1794. for (i = 0; i <= hmask; i++) {

1795. Node *n = &node[i];

1796. if (tvistab(&n->val)) {

1797. lua_assert(tabV(&n->val) == t);

1798. setnilV(&n->val);  /* Turn value into nil. */

1799. }

1800. }

1801. }

1802. lj_gc_check(fs->L);

1803. }

1804. }

1806. /* Parse function parameters. */

1807. ‌static BCReg parse_params(LexState *ls, int needself)

1808. {

1809. FuncState *fs = ls->fs;

1810. BCReg nparams = 0;

1811. lex_check(ls, '(');

1812. if (needself)

1813. var_new_lit(ls, nparams++, "self");

1814. if (ls->tok != ')') {

1815. do {

1816. if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {

1817. var_new(ls, nparams++, lex_str(ls));

1818. } else if (ls->tok == TK_dots) {

1819. lj_lex_next(ls);

1820. fs->flags |= PROTO_VARARG;

1821. break;

1822. } else {

1823. err_syntax(ls, LJ_ERR_XPARAM);

1824. }

1825. } while (lex_opt(ls, ','));

1826. }

1827. var_add(ls, nparams);

1828. lua_assert(fs->nactvar == nparams);

1829. bcreg_reserve(fs, nparams);

1830. lex_check(ls, ')');

1831. return nparams;

1832. }

1834. /* Forward declaration. */

1835. static void parse_chunk(LexState *ls);

1837. /* Parse body of a function. */

1838. ‌static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line)

1839. {

1840. FuncState fs, *pfs = ls->fs;

1841. FuncScope bl;

1842. GCproto *pt;

1843. ptrdiff_t oldbase = pfs->bcbase - ls->bcstack;

1844. fs_init(ls, &fs);

1845. fscope_begin(&fs, &bl, 0);

1846. fs.linedefined = line;

1847. fs.numparams = (uint8_t)parse_params(ls, needself);

1848. fs.bcbase = pfs->bcbase + pfs->pc;

1849. fs.bclim = pfs->bclim - pfs->pc;

1850. bcemit_AD(&fs, BC_FUNCF, 0, 0);  /* Placeholder. */

1851. parse_chunk(ls);

1852. if (ls->tok != TK_end) lex_match(ls, TK_end, TK_function, line);

1853. pt = fs_finish(ls, (ls->lastline = ls->linenumber));

1854. pfs->bcbase = ls->bcstack + oldbase;  /* May have been reallocated. */

1855. pfs->bclim = (BCPos)(ls->sizebcstack - oldbase);

1856. /* Store new prototype in the constant array of the parent. */

1857. expr_init(e, VRELOCABLE,

1858. bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO)));

1859. #if LJ_HASFFI

1860. pfs->flags |= (fs.flags & PROTO_FFI);

1861. #endif

1862. if (!(pfs->flags & PROTO_CHILD)) {

1863. if (pfs->flags & PROTO_HAS_RETURN)

1864. pfs->flags |= PROTO_FIXUP_RETURN;

1865. pfs->flags |= PROTO_CHILD;

1866. }

1867. lj_lex_next(ls);

1868. }

1870. /* Parse expression list. Last expression is left open. */

1871. ‌static BCReg expr_list(LexState *ls, ExpDesc *v)

1872. {

1873. BCReg n = 1;

1874. expr(ls, v);

1875. while (lex_opt(ls, ',')) {

1876. expr_tonextreg(ls->fs, v);

1877. expr(ls, v);

1878. n++;

1879. }

1880. return n;

1881. }

1883. /* Parse function argument list. */

1884. ‌static void parse_args(LexState *ls, ExpDesc *e)

1885. {

1886. FuncState *fs = ls->fs;

1887. ExpDesc args;

1888. BCIns ins;

1889. BCReg base;

1890. BCLine line = ls->linenumber;

1891. if (ls->tok == '(') {

1892. #if !LJ_52

1893. if (line != ls->lastline)

1894. err_syntax(ls, LJ_ERR_XAMBIG);

1895. #endif

1896. lj_lex_next(ls);

1897. if (ls->tok == ')') {  /* f(). */

1898. args.k = VVOID;

1899. } else {

1900. expr_list(ls, &args);

1901. if (args.k == VCALL)  /* f(a, b, g()) or f(a, b, ...). */

1902. setbc_b(bcptr(fs, &args), 0);  /* Pass on multiple results. */

1903. }

1904. lex_match(ls, ')', '(', line);

1905. } else if (ls->tok == '{') {

1906. expr_table(ls, &args);

1907. } else if (ls->tok == TK_string) {

1908. expr_init(&args, VKSTR, 0);

1909. args.u.sval = strV(&ls->tokval);

1910. lj_lex_next(ls);

1911. } else {

1912. err_syntax(ls, LJ_ERR_XFUNARG);

1913. return;  /* Silence compiler. */

1914. }

1915. lua_assert(e->k == VNONRELOC);

1916. base = e->u.s.info;  /* Base register for call. */

1917. if (args.k == VCALL) {

1918. ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1 - LJ_FR2);

1919. } else {

1920. if (args.k != VVOID)

1921. expr_tonextreg(fs, &args);

1922. ins = BCINS_ABC(BC_CALL, base, 2, fs->freereg - base - LJ_FR2);

1923. }

1924. expr_init(e, VCALL, bcemit_INS(fs, ins));

1925. e->u.s.aux = base;

1926. fs->bcbase[fs->pc - 1].line = line;

1927. fs->freereg = base+1;  /* Leave one result by default. */

1928. }

1930. /* Parse primary expression. */

1931. ‌static void expr_primary(LexState *ls, ExpDesc *v)

1932. {

1933. FuncState *fs = ls->fs;

1934. /* Parse prefix expression. */

1935. if (ls->tok == '(') {

1936. BCLine line = ls->linenumber;

1937. lj_lex_next(ls);

1938. expr(ls, v);

1939. lex_match(ls, ')', '(', line);

1940. expr_discharge(ls->fs, v);

1941. } else if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {

1942. var_lookup(ls, v);

1943. } else {

1944. err_syntax(ls, LJ_ERR_XSYMBOL);

1945. }

1946. for (;;) {  /* Parse multiple expression suffixes. */

1947. if (ls->tok == '.') {

1948. expr_field(ls, v);

1949. } else if (ls->tok == '[') {

1950. ExpDesc key;

1951. expr_toanyreg(fs, v);

1952. expr_bracket(ls, &key);

1953. expr_index(fs, v, &key);

1954. } else if (ls->tok == ':') {

1955. ExpDesc key;

1956. lj_lex_next(ls);

1957. expr_str(ls, &key);

1958. bcemit_method(fs, v, &key);

1959. parse_args(ls, v);

1960. } else if (ls->tok == '(' || ls->tok == TK_string || ls->tok == '{') {

1961. expr_tonextreg(fs, v);

1962. if (LJ_FR2) bcreg_reserve(fs, 1);

1963. parse_args(ls, v);

1964. } else {

1965. break;

1966. }

1967. }

1968. }

1970. /* Parse simple expression. */

1971. ‌static void expr_simple(LexState *ls, ExpDesc *v)

1972. {

1973. switch (ls->tok) {

1974. case TK_number:

1975. expr_init(v, (LJ_HASFFI && tviscdata(&ls->tokval)) ? VKCDATA : VKNUM, 0);

1976. copyTV(ls->L, &v->u.nval, &ls->tokval);

1977. break;

1978. case TK_string:

1979. expr_init(v, VKSTR, 0);

1980. v->u.sval = strV(&ls->tokval);

1981. break;

1982. case TK_nil:

1983. expr_init(v, VKNIL, 0);

1984. break;

1985. case TK_true:

1986. expr_init(v, VKTRUE, 0);

1987. break;

1988. case TK_false:

1989. expr_init(v, VKFALSE, 0);

1990. break;

1991. case TK_dots: {  /* Vararg. */

1992. FuncState *fs = ls->fs;

1993. BCReg base;

1994. checkcond(ls, fs->flags & PROTO_VARARG, LJ_ERR_XDOTS);

1995. bcreg_reserve(fs, 1);

1996. base = fs->freereg-1;

1997. expr_init(v, VCALL, bcemit_ABC(fs, BC_VARG, base, 2, fs->numparams));

1998. v->u.s.aux = base;

1999. break;

2000. }

2001. case '{':  /* Table constructor. */

2002. expr_table(ls, v);

2003. return;

2004. case TK_function:

2005. lj_lex_next(ls);

2006. parse_body(ls, v, 0, ls->linenumber);

2007. return;

2008. default:

2009. expr_primary(ls, v);

2010. return;

2011. }

2012. lj_lex_next(ls);

2013. }

2015. /* Manage syntactic levels to avoid blowing up the stack. */

2016. ‌static void synlevel_begin(LexState *ls)

2017. {

2018. if (++ls->level >= LJ_MAX_XLEVEL)

2019. lj_lex_error(ls, 0, LJ_ERR_XLEVELS);

2020. }

2022. ‌#define synlevel_end(ls)        ((ls)->level--)

2024. /* Convert token to binary operator. */

2025. ‌static BinOpr token2binop(LexToken tok)

2026. {

2027. switch (tok) {

2028. case '+':        return OPR_ADD;

2029. case '-':        return OPR_SUB;

2030. case '*':        return OPR_MUL;

2031. case '/':        return OPR_DIV;

2032. case '%':        return OPR_MOD;

2033. case '^':        return OPR_POW;

2034. case TK_concat: return OPR_CONCAT;

2035. case TK_ne:        return OPR_NE;

2036. case TK_eq:        return OPR_EQ;

2037. case '<':        return OPR_LT;

2038. case TK_le:        return OPR_LE;

2039. case '>':        return OPR_GT;

2040. case TK_ge:        return OPR_GE;

2041. case TK_and:        return OPR_AND;

2042. case TK_or:        return OPR_OR;

2043. default:        return OPR_NOBINOPR;

2044. }

2045. }

2047. /* Priorities for each binary operator. ORDER OPR. */

2048. static const struct {

2049. uint8_t left;                /* Left priority. */

2050. uint8_t right;        /* Right priority. */

2051. ‌} priority[] = {

2052. {6,6}, {6,6}, {7,7}, {7,7}, {7,7},        /* ADD SUB MUL DIV MOD */

2053. {10,9}, {5,4},                        /* POW CONCAT (right associative) */

2054. {3,3}, {3,3},                                /* EQ NE */

2055. {3,3}, {3,3}, {3,3}, {3,3},                /* LT GE GT LE */

2056. {2,2}, {1,1}                                /* AND OR */

2057. };

2059. ‌#define UNARY_PRIORITY                8  /* Priority for unary operators. */

2061. /* Forward declaration. */

2062. static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit);

2064. /* Parse unary expression. */

2065. ‌static void expr_unop(LexState *ls, ExpDesc *v)

2066. {

2067. BCOp op;

2068. if (ls->tok == TK_not) {

2069. op = BC_NOT;

2070. } else if (ls->tok == '-') {

2071. op = BC_UNM;

2072. } else if (ls->tok == '#') {

2073. op = BC_LEN;

2074. } else {

2075. expr_simple(ls, v);

2076. return;

2077. }

2078. lj_lex_next(ls);

2079. expr_binop(ls, v, UNARY_PRIORITY);

2080. bcemit_unop(ls->fs, op, v);

2081. }

2083. /* Parse binary expressions with priority higher than the limit. */

2084. ‌static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit)

2085. {

2086. BinOpr op;

2087. synlevel_begin(ls);

2088. expr_unop(ls, v);

2089. op = token2binop(ls->tok);

2090. while (op != OPR_NOBINOPR && priority[op].left > limit) {

2091. ExpDesc v2;

2092. BinOpr nextop;

2093. lj_lex_next(ls);

2094. bcemit_binop_left(ls->fs, op, v);

2095. /* Parse binary expression with higher priority. */

2096. nextop = expr_binop(ls, &v2, priority[op].right);

2097. bcemit_binop(ls->fs, op, v, &v2);

2098. op = nextop;

2099. }

2100. synlevel_end(ls);

2101. return op;  /* Return unconsumed binary operator (if any). */

2102. }

2104. /* Parse expression. */

2105. ‌static void expr(LexState *ls, ExpDesc *v)

2106. {

2107. expr_binop(ls, v, 0);  /* Priority 0: parse whole expression. */

2108. }

2110. /* Assign expression to the next register. */

2111. ‌static void expr_next(LexState *ls)

2112. {

2113. ExpDesc e;

2114. expr(ls, &e);

2115. expr_tonextreg(ls->fs, &e);

2116. }

2118. /* Parse conditional expression. */

2119. ‌static BCPos expr_cond(LexState *ls)

2120. {

2121. ExpDesc v;

2122. expr(ls, &v);

2123. if (v.k == VKNIL) v.k = VKFALSE;

2124. bcemit_branch_t(ls->fs, &v);

2125. return v.f;

2126. }

2128. /* -- Assignments --------------------------------------------------------- */

2130. /* List of LHS variables. */

2131. ‌typedef struct LHSVarList {

2132. ExpDesc v;                        /* LHS variable. */

2133. struct LHSVarList *prev;        /* Link to previous LHS variable. */

2134. ‌} LHSVarList;

2136. /* Eliminate write-after-read hazards for local variable assignment. */

2137. ‌static void assign_hazard(LexState *ls, LHSVarList *lh, const ExpDesc *v)

2138. {

2139. FuncState *fs = ls->fs;

2140. BCReg reg = v->u.s.info;  /* Check against this variable. */

2141. BCReg tmp = fs->freereg;  /* Rename to this temp. register (if needed). */

2142. int hazard = 0;

2143. for (; lh; lh = lh->prev) {

2144. if (lh->v.k == VINDEXED) {

2145. if (lh->v.u.s.info == reg) {  /* t[i], t = 1, 2 */

2146. hazard = 1;

2147. lh->v.u.s.info = tmp;

2148. }

2149. if (lh->v.u.s.aux == reg) {  /* t[i], i = 1, 2 */

2150. hazard = 1;

2151. lh->v.u.s.aux = tmp;

2152. }

2153. }

2154. }

2155. if (hazard) {

2156. bcemit_AD(fs, BC_MOV, tmp, reg);  /* Rename conflicting variable. */

2157. bcreg_reserve(fs, 1);

2158. }

2159. }

2161. /* Adjust LHS/RHS of an assignment. */

2162. ‌static void assign_adjust(LexState *ls, BCReg nvars, BCReg nexps, ExpDesc *e)

2163. {

2164. FuncState *fs = ls->fs;

2165. int32_t extra = (int32_t)nvars - (int32_t)nexps;

2166. if (e->k == VCALL) {

2167. extra++;  /* Compensate for the VCALL itself. */

2168. if (extra < 0) extra = 0;

2169. setbc_b(bcptr(fs, e), extra+1);  /* Fixup call results. */

2170. if (extra > 1) bcreg_reserve(fs, (BCReg)extra-1);

2171. } else {

2172. if (e->k != VVOID)

2173. expr_tonextreg(fs, e);  /* Close last expression. */

2174. if (extra > 0) {  /* Leftover LHS are set to nil. */

2175. BCReg reg = fs->freereg;

2176. bcreg_reserve(fs, (BCReg)extra);

2177. bcemit_nil(fs, reg, (BCReg)extra);

2178. }

2179. }

2180. }

2182. /* Recursively parse assignment statement. */

2183. ‌static void parse_assignment(LexState *ls, LHSVarList *lh, BCReg nvars)

2184. {

2185. ExpDesc e;

2186. checkcond(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, LJ_ERR_XSYNTAX);

2187. if (lex_opt(ls, ',')) {  /* Collect LHS list and recurse upwards. */

2188. LHSVarList vl;

2189. vl.prev = lh;

2190. expr_primary(ls, &vl.v);

2191. if (vl.v.k == VLOCAL)

2192. assign_hazard(ls, lh, &vl.v);

2193. checklimit(ls->fs, ls->level + nvars, LJ_MAX_XLEVEL, "variable names");

2194. parse_assignment(ls, &vl, nvars+1);

2195. } else {  /* Parse RHS. */

2196. BCReg nexps;

2197. lex_check(ls, '=');

2198. nexps = expr_list(ls, &e);

2199. if (nexps == nvars) {

2200. if (e.k == VCALL) {

2201. if (bc_op(*bcptr(ls->fs, &e)) == BC_VARG) {  /* Vararg assignment. */

2202. ls->fs->freereg--;

2203. e.k = VRELOCABLE;

2204. } else {  /* Multiple call results. */

2205. e.u.s.info = e.u.s.aux;  /* Base of call is not relocatable. */

2206. e.k = VNONRELOC;

2207. }

2208. }

2209. bcemit_store(ls->fs, &lh->v, &e);

2210. return;

2211. }

2212. assign_adjust(ls, nvars, nexps, &e);

2213. if (nexps > nvars)

2214. ls->fs->freereg -= nexps - nvars;  /* Drop leftover regs. */

2215. }

2216. /* Assign RHS to LHS and recurse downwards. */

2217. expr_init(&e, VNONRELOC, ls->fs->freereg-1);

2218. bcemit_store(ls->fs, &lh->v, &e);

2219. }

2221. /* Parse call statement or assignment. */

2222. ‌static void parse_call_assign(LexState *ls)

2223. {

2224. FuncState *fs = ls->fs;

2225. LHSVarList vl;

2226. expr_primary(ls, &vl.v);

2227. if (vl.v.k == VCALL) {  /* Function call statement. */

2228. setbc_b(bcptr(fs, &vl.v), 1);  /* No results. */

2229. } else {  /* Start of an assignment. */

2230. vl.prev = NULL;

2231. parse_assignment(ls, &vl, 1);

2232. }

2233. }

2235. /* Parse 'local' statement. */

2236. ‌static void parse_local(LexState *ls)

2237. {

2238. if (lex_opt(ls, TK_function)) {  /* Local function declaration. */

2239. ExpDesc v, b;

2240. FuncState *fs = ls->fs;

2241. var_new(ls, 0, lex_str(ls));

2242. expr_init(&v, VLOCAL, fs->freereg);

2243. v.u.s.aux = fs->varmap[fs->freereg];

2244. bcreg_reserve(fs, 1);

2245. var_add(ls, 1);

2246. parse_body(ls, &b, 0, ls->linenumber);

2247. /* bcemit_store(fs, &v, &b) without setting VSTACK_VAR_RW. */

2248. expr_free(fs, &b);

2249. expr_toreg(fs, &b, v.u.s.info);

2250. /* The upvalue is in scope, but the local is only valid after the store. */

2251. var_get(ls, fs, fs->nactvar - 1).startpc = fs->pc;

2252. } else {  /* Local variable declaration. */

2253. ExpDesc e;

2254. BCReg nexps, nvars = 0;

2255. do {  /* Collect LHS. */

2256. var_new(ls, nvars++, lex_str(ls));

2257. } while (lex_opt(ls, ','));

2258. if (lex_opt(ls, '=')) {  /* Optional RHS. */

2259. nexps = expr_list(ls, &e);

2260. } else {  /* Or implicitly set to nil. */

2261. e.k = VVOID;

2262. nexps = 0;

2263. }

2264. assign_adjust(ls, nvars, nexps, &e);

2265. var_add(ls, nvars);

2266. }

2267. }

2269. /* Parse 'function' statement. */

2270. ‌static void parse_func(LexState *ls, BCLine line)

2271. {

2272. FuncState *fs;

2273. ExpDesc v, b;

2274. int needself = 0;

2275. lj_lex_next(ls);  /* Skip 'function'. */

2276. /* Parse function name. */

2277. var_lookup(ls, &v);

2278. while (ls->tok == '.')  /* Multiple dot-separated fields. */

2279. expr_field(ls, &v);

2280. if (ls->tok == ':') {  /* Optional colon to signify method call. */

2281. needself = 1;

2282. expr_field(ls, &v);

2283. }

2284. parse_body(ls, &b, needself, line);

2285. fs = ls->fs;

2286. bcemit_store(fs, &v, &b);

2287. fs->bcbase[fs->pc - 1].line = line;  /* Set line for the store. */

2288. }

2290. /* -- Control transfer statements ----------------------------------------- */

2292. /* Check for end of block. */

2293. ‌static int parse_isend(LexToken tok)

2294. {

2295. switch (tok) {

2296. case TK_else: case TK_elseif: case TK_end: case TK_until: case TK_eof:

2297. return 1;

2298. default:

2299. return 0;

2300. }

2301. }

2303. /* Parse 'return' statement. */

2304. ‌static void parse_return(LexState *ls)

2305. {

2306. BCIns ins;

2307. FuncState *fs = ls->fs;

2308. lj_lex_next(ls);  /* Skip 'return'. */

2309. fs->flags |= PROTO_HAS_RETURN;

2310. if (parse_isend(ls->tok) || ls->tok == ';') {  /* Bare return. */

2311. ins = BCINS_AD(BC_RET0, 0, 1);

2312. } else {  /* Return with one or more values. */

2313. ExpDesc e;  /* Receives the _last_ expression in the list. */

2314. BCReg nret = expr_list(ls, &e);

2315. if (nret == 1) {  /* Return one result. */

2316. if (e.k == VCALL) {  /* Check for tail call. */

2317. BCIns *ip = bcptr(fs, &e);

2318. /* It doesn't pay off to add BC_VARGT just for 'return ...'. */

2319. if (bc_op(*ip) == BC_VARG) goto notailcall;

2320. fs->pc--;

2321. ins = BCINS_AD(bc_op(*ip)-BC_CALL+BC_CALLT, bc_a(*ip), bc_c(*ip));

2322. } else {  /* Can return the result from any register. */

2323. ins = BCINS_AD(BC_RET1, expr_toanyreg(fs, &e), 2);

2324. }

2325. } else {

2326. if (e.k == VCALL) {  /* Append all results from a call. */

2327. notailcall:

2328. setbc_b(bcptr(fs, &e), 0);

2329. ins = BCINS_AD(BC_RETM, fs->nactvar, e.u.s.aux - fs->nactvar);

2330. } else {

2331. expr_tonextreg(fs, &e);  /* Force contiguous registers. */

2332. ins = BCINS_AD(BC_RET, fs->nactvar, nret+1);

2333. }

2334. }

2335. }

2336. if (fs->flags & PROTO_CHILD)

2337. bcemit_AJ(fs, BC_UCLO, 0, 0);  /* May need to close upvalues first. */

2338. bcemit_INS(fs, ins);

2339. }

2341. /* Parse 'break' statement. */

2342. ‌static void parse_break(LexState *ls)

2343. {

2344. ls->fs->bl->flags |= FSCOPE_BREAK;

2345. gola_new(ls, NAME_BREAK, VSTACK_GOTO, bcemit_jmp(ls->fs));

2346. }

2348. /* Parse 'goto' statement. */

2349. ‌static void parse_goto(LexState *ls)

2350. {

2351. FuncState *fs = ls->fs;

2352. GCstr *name = lex_str(ls);

2353. VarInfo *vl = gola_findlabel(ls, name);

2354. if (vl)  /* Treat backwards goto within same scope like a loop. */

2355. bcemit_AJ(fs, BC_LOOP, vl->slot, -1);  /* No BC range check. */

2356. fs->bl->flags |= FSCOPE_GOLA;

2357. gola_new(ls, name, VSTACK_GOTO, bcemit_jmp(fs));

2358. }

2360. /* Parse label. */

2361. ‌static void parse_label(LexState *ls)

2362. {

2363. FuncState *fs = ls->fs;

2364. GCstr *name;

2365. MSize idx;

2366. fs->lasttarget = fs->pc;

2367. fs->bl->flags |= FSCOPE_GOLA;

2368. lj_lex_next(ls);  /* Skip '::'. */

2369. name = lex_str(ls);

2370. if (gola_findlabel(ls, name))

2371. lj_lex_error(ls, 0, LJ_ERR_XLDUP, strdata(name));

2372. idx = gola_new(ls, name, VSTACK_LABEL, fs->pc);

2373. lex_check(ls, TK_label);

2374. /* Recursively parse trailing statements: labels and ';' (Lua 5.2 only). */

2375. for (;;) {

2376. if (ls->tok == TK_label) {

2377. synlevel_begin(ls);

2378. parse_label(ls);

2379. synlevel_end(ls);

2380. } else if (LJ_52 && ls->tok == ';') {

2381. lj_lex_next(ls);

2382. } else {

2383. break;

2384. }

2385. }

2386. /* Trailing label is considered to be outside of scope. */

2387. if (parse_isend(ls->tok) && ls->tok != TK_until)

2388. ls->vstack[idx].slot = fs->bl->nactvar;

2389. gola_resolve(ls, fs->bl, idx);

2390. }

2392. /* -- Blocks, loops and conditional statements ---------------------------- */

2394. /* Parse a block. */

2395. ‌static void parse_block(LexState *ls)

2396. {

2397. FuncState *fs = ls->fs;

2398. FuncScope bl;

2399. fscope_begin(fs, &bl, 0);

2400. parse_chunk(ls);

2401. fscope_end(fs);

2402. }

2404. /* Parse 'while' statement. */

2405. ‌static void parse_while(LexState *ls, BCLine line)

2406. {

2407. FuncState *fs = ls->fs;

2408. BCPos start, loop, condexit;

2409. FuncScope bl;

2410. lj_lex_next(ls);  /* Skip 'while'. */

2411. start = fs->lasttarget = fs->pc;

2412. condexit = expr_cond(ls);

2413. fscope_begin(fs, &bl, FSCOPE_LOOP);

2414. lex_check(ls, TK_do);

2415. loop = bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);

2416. parse_block(ls);

2417. jmp_patch(fs, bcemit_jmp(fs), start);

2418. lex_match(ls, TK_end, TK_while, line);

2419. fscope_end(fs);

2420. jmp_tohere(fs, condexit);

2421. jmp_patchins(fs, loop, fs->pc);

2422. }

2424. /* Parse 'repeat' statement. */

2425. ‌static void parse_repeat(LexState *ls, BCLine line)

2426. {

2427. FuncState *fs = ls->fs;

2428. BCPos loop = fs->lasttarget = fs->pc;

2429. BCPos condexit;

2430. FuncScope bl1, bl2;

2431. fscope_begin(fs, &bl1, FSCOPE_LOOP);  /* Breakable loop scope. */

2432. fscope_begin(fs, &bl2, 0);  /* Inner scope. */

2433. lj_lex_next(ls);  /* Skip 'repeat'. */

2434. bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);

2435. parse_chunk(ls);

2436. lex_match(ls, TK_until, TK_repeat, line);

2437. condexit = expr_cond(ls);  /* Parse condition (still inside inner scope). */

2438. if (!(bl2.flags & FSCOPE_UPVAL)) {  /* No upvalues? Just end inner scope. */

2439. fscope_end(fs);

2440. } else {  /* Otherwise generate: cond: UCLO+JMP out, !cond: UCLO+JMP loop. */

2441. parse_break(ls);  /* Break from loop and close upvalues. */

2442. jmp_tohere(fs, condexit);

2443. fscope_end(fs);  /* End inner scope and close upvalues. */

2444. condexit = bcemit_jmp(fs);

2445. }

2446. jmp_patch(fs, condexit, loop);  /* Jump backwards if !cond. */

2447. jmp_patchins(fs, loop, fs->pc);

2448. fscope_end(fs);  /* End loop scope. */

2449. }

2451. /* Parse numeric 'for'. */

2452. ‌static void parse_for_num(LexState *ls, GCstr *varname, BCLine line)

2453. {

2454. FuncState *fs = ls->fs;

2455. BCReg base = fs->freereg;

2456. FuncScope bl;

2457. BCPos loop, loopend;

2458. /* Hidden control variables. */

2459. var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX);

2460. var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP);

2461. var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP);

2462. /* Visible copy of index variable. */

2463. var_new(ls, FORL_EXT, varname);

2464. lex_check(ls, '=');

2465. expr_next(ls);

2466. lex_check(ls, ',');

2467. expr_next(ls);

2468. if (lex_opt(ls, ',')) {

2469. expr_next(ls);

2470. } else {

2471. bcemit_AD(fs, BC_KSHORT, fs->freereg, 1);  /* Default step is 1. */

2472. bcreg_reserve(fs, 1);

2473. }

2474. var_add(ls, 3);  /* Hidden control variables. */

2475. lex_check(ls, TK_do);

2476. loop = bcemit_AJ(fs, BC_FORI, base, NO_JMP);

2477. fscope_begin(fs, &bl, 0);  /* Scope for visible variables. */

2478. var_add(ls, 1);

2479. bcreg_reserve(fs, 1);

2480. parse_block(ls);

2481. fscope_end(fs);

2482. /* Perform loop inversion. Loop control instructions are at the end. */

2483. loopend = bcemit_AJ(fs, BC_FORL, base, NO_JMP);

2484. fs->bcbase[loopend].line = line;  /* Fix line for control ins. */

2485. jmp_patchins(fs, loopend, loop+1);

2486. jmp_patchins(fs, loop, fs->pc);

2487. }

2489. /* Try to predict whether the iterator is next() and specialize the bytecode.

2490. ** Detecting next() and pairs() by name is simplistic, but quite effective.

2491. ** The interpreter backs off if the check for the closure fails at runtime.

2492. */

2493. ‌static int predict_next(LexState *ls, FuncState *fs, BCPos pc)

2494. {

2495. BCIns ins = fs->bcbase[pc].ins;

2496. GCstr *name;

2497. cTValue *o;

2498. switch (bc_op(ins)) {

2499. case BC_MOV:

2500. name = gco2str(gcref(var_get(ls, fs, bc_d(ins)).name));

2501. break;

2502. case BC_UGET:

2503. name = gco2str(gcref(ls->vstack[fs->uvmap[bc_d(ins)]].name));

2504. break;

2505. case BC_GGET:

2506. /* There's no inverse index (yet), so lookup the strings. */

2507. o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "pairs"));

2508. if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins))

2509. return 1;

2510. o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "next"));

2511. if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins))

2512. return 1;

2513. return 0;

2514. default:

2515. return 0;

2516. }

2517. return (name->len == 5 && !strcmp(strdata(name), "pairs")) ||

2518. (name->len == 4 && !strcmp(strdata(name), "next"));

2519. }

2521. /* Parse 'for' iterator. */

2522. ‌static void parse_for_iter(LexState *ls, GCstr *indexname)

2523. {

2524. FuncState *fs = ls->fs;

2525. ExpDesc e;

2526. BCReg nvars = 0;

2527. BCLine line;

2528. BCReg base = fs->freereg + 3;

2529. BCPos loop, loopend, exprpc = fs->pc;

2530. FuncScope bl;

2531. int isnext;

2532. /* Hidden control variables. */

2533. var_new_fixed(ls, nvars++, VARNAME_FOR_GEN);

2534. var_new_fixed(ls, nvars++, VARNAME_FOR_STATE);

2535. var_new_fixed(ls, nvars++, VARNAME_FOR_CTL);

2536. /* Visible variables returned from iterator. */

2537. var_new(ls, nvars++, indexname);

2538. while (lex_opt(ls, ','))

2539. var_new(ls, nvars++, lex_str(ls));

2540. lex_check(ls, TK_in);

2541. line = ls->linenumber;

2542. assign_adjust(ls, 3, expr_list(ls, &e), &e);

2543. /* The iterator needs another 3 [4] slots (func [pc] | state ctl). */

2544. bcreg_bump(fs, 3+LJ_FR2);

2545. isnext = (nvars <= 5 && predict_next(ls, fs, exprpc));

2546. var_add(ls, 3);  /* Hidden control variables. */

2547. lex_check(ls, TK_do);

2548. loop = bcemit_AJ(fs, isnext ? BC_ISNEXT : BC_JMP, base, NO_JMP);

2549. fscope_begin(fs, &bl, 0);  /* Scope for visible variables. */

2550. var_add(ls, nvars-3);

2551. bcreg_reserve(fs, nvars-3);

2552. parse_block(ls);

2553. fscope_end(fs);

2554. /* Perform loop inversion. Loop control instructions are at the end. */

2555. jmp_patchins(fs, loop, fs->pc);

2556. bcemit_ABC(fs, isnext ? BC_ITERN : BC_ITERC, base, nvars-3+1, 2+1);

2557. loopend = bcemit_AJ(fs, BC_ITERL, base, NO_JMP);

2558. fs->bcbase[loopend-1].line = line;  /* Fix line for control ins. */

2559. fs->bcbase[loopend].line = line;

2560. jmp_patchins(fs, loopend, loop+1);

2561. }

2563. /* Parse 'for' statement. */

2564. ‌static void parse_for(LexState *ls, BCLine line)

2565. {

2566. FuncState *fs = ls->fs;

2567. GCstr *varname;

2568. FuncScope bl;

2569. fscope_begin(fs, &bl, FSCOPE_LOOP);

2570. lj_lex_next(ls);  /* Skip 'for'. */

2571. varname = lex_str(ls);  /* Get first variable name. */

2572. if (ls->tok == '=')

2573. parse_for_num(ls, varname, line);

2574. else if (ls->tok == ',' || ls->tok == TK_in)

2575. parse_for_iter(ls, varname);

2576. else

2577. err_syntax(ls, LJ_ERR_XFOR);

2578. lex_match(ls, TK_end, TK_for, line);

2579. fscope_end(fs);  /* Resolve break list. */

2580. }

2582. /* Parse condition and 'then' block. */

2583. ‌static BCPos parse_then(LexState *ls)

2584. {

2585. BCPos condexit;

2586. lj_lex_next(ls);  /* Skip 'if' or 'elseif'. */

2587. condexit = expr_cond(ls);

2588. lex_check(ls, TK_then);

2589. parse_block(ls);

2590. return condexit;

2591. }

2593. /* Parse 'if' statement. */

2594. ‌static void parse_if(LexState *ls, BCLine line)

2595. {

2596. FuncState *fs = ls->fs;

2597. BCPos flist;

2598. BCPos escapelist = NO_JMP;

2599. flist = parse_then(ls);

2600. while (ls->tok == TK_elseif) {  /* Parse multiple 'elseif' blocks. */

2601. jmp_append(fs, &escapelist, bcemit_jmp(fs));

2602. jmp_tohere(fs, flist);

2603. flist = parse_then(ls);

2604. }

2605. if (ls->tok == TK_else) {  /* Parse optional 'else' block. */

2606. jmp_append(fs, &escapelist, bcemit_jmp(fs));

2607. jmp_tohere(fs, flist);

2608. lj_lex_next(ls);  /* Skip 'else'. */

2609. parse_block(ls);

2610. } else {

2611. jmp_append(fs, &escapelist, flist);

2612. }

2613. jmp_tohere(fs, escapelist);

2614. lex_match(ls, TK_end, TK_if, line);

2615. }

2617. /* -- Parse statements ---------------------------------------------------- */

2619. /* Parse a statement. Returns 1 if it must be the last one in a chunk. */

2620. ‌static int parse_stmt(LexState *ls)

2621. {

2622. BCLine line = ls->linenumber;

2623. switch (ls->tok) {

2624. case TK_if:

2625. parse_if(ls, line);

2626. break;

2627. case TK_while:

2628. parse_while(ls, line);

2629. break;

2630. case TK_do:

2631. lj_lex_next(ls);

2632. parse_block(ls);

2633. lex_match(ls, TK_end, TK_do, line);

2634. break;

2635. case TK_for:

2636. parse_for(ls, line);

2637. break;

2638. case TK_repeat:

2639. parse_repeat(ls, line);

2640. break;

2641. case TK_function:

2642. parse_func(ls, line);

2643. break;

2644. case TK_local:

2645. lj_lex_next(ls);

2646. parse_local(ls);

2647. break;

2648. case TK_return:

2649. parse_return(ls);

2650. return 1;  /* Must be last. */

2651. case TK_break:

2652. lj_lex_next(ls);

2653. parse_break(ls);

2654. return !LJ_52;  /* Must be last in Lua 5.1. */

2655. #if LJ_52

2656. case ';':

2657. lj_lex_next(ls);

2658. break;

2659. #endif

2660. case TK_label:

2661. parse_label(ls);

2662. break;

2663. case TK_goto:

2664. if (LJ_52 || lj_lex_lookahead(ls) == TK_name) {

2665. lj_lex_next(ls);

2666. parse_goto(ls);

2667. break;

2668. }  /* else: fallthrough */

2669. default:

2670. parse_call_assign(ls);

2671. break;

2672. }

2673. return 0;

2674. }

2676. /* A chunk is a list of statements optionally separated by semicolons. */

2677. ‌static void parse_chunk(LexState *ls)

2678. {

2679. int islast = 0;

2680. synlevel_begin(ls);

2681. while (!islast && !parse_isend(ls->tok)) {

2682. islast = parse_stmt(ls);

2683. lex_opt(ls, ';');

2684. lua_assert(ls->fs->framesize >= ls->fs->freereg &&

2685. ls->fs->freereg >= ls->fs->nactvar);

2686. ls->fs->freereg = ls->fs->nactvar;  /* Free registers after each stmt. */

2687. }

2688. synlevel_end(ls);

2689. }

2691. /* Entry point of bytecode parser. */

2692. ‌GCproto *lj_parse(LexState *ls)

2693. {

2694. FuncState fs;

2695. FuncScope bl;

2696. GCproto *pt;

2697. lua_State *L = ls->L;

2698. #ifdef LUAJIT_DISABLE_DEBUGINFO

2699. ls->chunkname = lj_str_newlit(L, "=");

2700. #else

2701. ls->chunkname = lj_str_newz(L, ls->chunkarg);

2702. #endif

2703. setstrV(L, L->top, ls->chunkname);  /* Anchor chunkname string. */

2704. incr_top(L);

2705. ls->level = 0;

2706. fs_init(ls, &fs);

2707. fs.linedefined = 0;

2708. fs.numparams = 0;

2709. fs.bcbase = NULL;

2710. fs.bclim = 0;

2711. fs.flags |= PROTO_VARARG;  /* Main chunk is always a vararg func. */

2712. fscope_begin(&fs, &bl, 0);

2713. bcemit_AD(&fs, BC_FUNCV, 0, 0);  /* Placeholder. */

2714. lj_lex_next(ls);  /* Read-ahead first token. */

2715. parse_chunk(ls);

2716. if (ls->tok != TK_eof)

2717. err_token(ls, TK_eof);

2718. pt = fs_finish(ls, ls->linenumber);

2719. L->top--;  /* Drop chunkname. */

2720. lua_assert(fs.prev == NULL);

2721. lua_assert(ls->fs == NULL);

2722. lua_assert(pt->sizeuv == 0);

2723. return pt;

2724. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_bc.h - luajit-2.0-src

Global variables defined

- lj_bc_mode

- lj_bc_ofs

Data types defined

- BCMode

- BCOp

Functions defined

- bc_isret

Macros defined

- BCBIAS_J

- BCDEF

- BCENUM

- BCENUM

- BCINS_ABC

- BCINS_AD

- BCINS_AJ

- BCMAX_A

- BCMAX_B

- BCMAX_C

- BCMAX_D

- BCMODE

- BCMODE_FF

- BCM___

- FF_next_N

- NO_JMP

- NO_REG

- _LJ_BC_H

- bc_a

- bc_b

- bc_c

- bc_d

- bc_j

- bc_op

- bcmode_a

- bcmode_b

- bcmode_c

- bcmode_d

- bcmode_hasd

- bcmode_mm

- setbc_a

- setbc_b

- setbc_byte

- setbc_c

- setbc_d

- setbc_j

- setbc_op

Source code

1. /*

2. ** Bytecode instruction format.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_BC_H

7. ‌#define _LJ_BC_H

9. #include "lj_def.h"

10. #include "lj_arch.h"

12. /* Bytecode instruction format, 32 bit wide, fields of 8 or 16 bit:

13. **

14. ** +----+----+----+----+

15. ** | B  | C  | A  | OP | Format ABC

16. ** +----+----+----+----+

17. ** |    D    | A  | OP | Format AD

18. ** +--------------------

19. ** MSB               LSB

20. **

21. ** In-memory instructions are always stored in host byte order.

22. */

24. /* Operand ranges and related constants. */

25. ‌#define BCMAX_A                0xff

26. ‌#define BCMAX_B                0xff

27. ‌#define BCMAX_C                0xff

28. ‌#define BCMAX_D                0xffff

29. ‌#define BCBIAS_J        0x8000

30. ‌#define NO_REG                BCMAX_A

31. ‌#define NO_JMP                (~(BCPos)0)

33. /* Macros to get instruction fields. */

34. ‌#define bc_op(i)        ((BCOp)((i)&0xff))

35. ‌#define bc_a(i)                ((BCReg)(((i)>>8)&0xff))

36. ‌#define bc_b(i)                ((BCReg)((i)>>24))

37. ‌#define bc_c(i)                ((BCReg)(((i)>>16)&0xff))

38. ‌#define bc_d(i)                ((BCReg)((i)>>16))

39. ‌#define bc_j(i)                ((ptrdiff_t)bc_d(i)-BCBIAS_J)

41. /* Macros to set instruction fields. */

42. ‌#define setbc_byte(p, x, ofs) \

43. ((uint8_t *)(p))[LJ_ENDIAN_SELECT(ofs, 3-ofs)] = (uint8_t)(x)

44. ‌#define setbc_op(p, x)        setbc_byte(p, (x), 0)

45. ‌#define setbc_a(p, x)        setbc_byte(p, (x), 1)

46. ‌#define setbc_b(p, x)        setbc_byte(p, (x), 3)

47. ‌#define setbc_c(p, x)        setbc_byte(p, (x), 2)

48. ‌#define setbc_d(p, x) \

49. ((uint16_t *)(p))[LJ_ENDIAN_SELECT(1, 0)] = (uint16_t)(x)

50. ‌#define setbc_j(p, x)        setbc_d(p, (BCPos)((int32_t)(x)+BCBIAS_J))

52. /* Macros to compose instructions. */

53. ‌#define BCINS_ABC(o, a, b, c) \

54. (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(b)<<24)|((BCIns)(c)<<16))

55. ‌#define BCINS_AD(o, a, d) \

56. (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(d)<<16))

57. ‌#define BCINS_AJ(o, a, j)        BCINS_AD(o, a, (BCPos)((int32_t)(j)+BCBIAS_J))

59. /* Bytecode instruction definition. Order matters, see below.

60. **

61. ** (name, filler, Amode, Bmode, Cmode or Dmode, metamethod)

62. **

63. ** The opcode name suffixes specify the type for RB/RC or RD:

64. ** V = variable slot

65. ** S = string const

66. ** N = number const

67. ** P = primitive type (~itype)

68. ** B = unsigned byte literal

69. ** M = multiple args/results

70. */

71. ‌#define BCDEF(_) \

72. /* Comparison ops. ORDER OPR. */ \

73. _(ISLT,        var,        ___,        var,        lt) \

74. _(ISGE,        var,        ___,        var,        lt) \

75. _(ISLE,        var,        ___,        var,        le) \

76. _(ISGT,        var,        ___,        var,        le) \

77. \

78. _(ISEQV,        var,        ___,        var,        eq) \

79. _(ISNEV,        var,        ___,        var,        eq) \

80. _(ISEQS,        var,        ___,        str,        eq) \

81. _(ISNES,        var,        ___,        str,        eq) \

82. _(ISEQN,        var,        ___,        num,        eq) \

83. _(ISNEN,        var,        ___,        num,        eq) \

84. _(ISEQP,        var,        ___,        pri,        eq) \

85. _(ISNEP,        var,        ___,        pri,        eq) \

86. \

87. /* Unary test and copy ops. */ \

88. _(ISTC,        dst,        ___,        var,        ___) \

89. _(ISFC,        dst,        ___,        var,        ___) \

90. _(IST,        ___,        ___,        var,        ___) \

91. _(ISF,        ___,        ___,        var,        ___) \

92. _(ISTYPE,        var,        ___,        lit,        ___) \

93. _(ISNUM,        var,        ___,        lit,        ___) \

94. \

95. /* Unary ops. */ \

96. _(MOV,        dst,        ___,        var,        ___) \

97. _(NOT,        dst,        ___,        var,        ___) \

98. _(UNM,        dst,        ___,        var,        unm) \

99. _(LEN,        dst,        ___,        var,        len) \

100. \

101. /* Binary ops. ORDER OPR. VV last, POW must be next. */ \

102. _(ADDVN,        dst,        var,        num,        add) \

103. _(SUBVN,        dst,        var,        num,        sub) \

104. _(MULVN,        dst,        var,        num,        mul) \

105. _(DIVVN,        dst,        var,        num,        div) \

106. _(MODVN,        dst,        var,        num,        mod) \

107. \

108. _(ADDNV,        dst,        var,        num,        add) \

109. _(SUBNV,        dst,        var,        num,        sub) \

110. _(MULNV,        dst,        var,        num,        mul) \

111. _(DIVNV,        dst,        var,        num,        div) \

112. _(MODNV,        dst,        var,        num,        mod) \

113. \

114. _(ADDVV,        dst,        var,        var,        add) \

115. _(SUBVV,        dst,        var,        var,        sub) \

116. _(MULVV,        dst,        var,        var,        mul) \

117. _(DIVVV,        dst,        var,        var,        div) \

118. _(MODVV,        dst,        var,        var,        mod) \

119. \

120. _(POW,        dst,        var,        var,        pow) \

121. _(CAT,        dst,        rbase,        rbase,        concat) \

122. \

123. /* Constant ops. */ \

124. _(KSTR,        dst,        ___,        str,        ___) \

125. _(KCDATA,        dst,        ___,        cdata,        ___) \

126. _(KSHORT,        dst,        ___,        lits,        ___) \

127. _(KNUM,        dst,        ___,        num,        ___) \

128. _(KPRI,        dst,        ___,        pri,        ___) \

129. _(KNIL,        base,        ___,        base,        ___) \

130. \

131. /* Upvalue and function ops. */ \

132. _(UGET,        dst,        ___,        uv,        ___) \

133. _(USETV,        uv,        ___,        var,        ___) \

134. _(USETS,        uv,        ___,        str,        ___) \

135. _(USETN,        uv,        ___,        num,        ___) \

136. _(USETP,        uv,        ___,        pri,        ___) \

137. _(UCLO,        rbase,        ___,        jump,        ___) \

138. _(FNEW,        dst,        ___,        func,        gc) \

139. \

140. /* Table ops. */ \

141. _(TNEW,        dst,        ___,        lit,        gc) \

142. _(TDUP,        dst,        ___,        tab,        gc) \

143. _(GGET,        dst,        ___,        str,        index) \

144. _(GSET,        var,        ___,        str,        newindex) \

145. _(TGETV,        dst,        var,        var,        index) \

146. _(TGETS,        dst,        var,        str,        index) \

147. _(TGETB,        dst,        var,        lit,        index) \

148. _(TGETR,        dst,        var,        var,        index) \

149. _(TSETV,        var,        var,        var,        newindex) \

150. _(TSETS,        var,        var,        str,        newindex) \

151. _(TSETB,        var,        var,        lit,        newindex) \

152. _(TSETM,        base,        ___,        num,        newindex) \

153. _(TSETR,        var,        var,        var,        newindex) \

154. \

155. /* Calls and vararg handling. T = tail call. */ \

156. _(CALLM,        base,        lit,        lit,        call) \

157. _(CALL,        base,        lit,        lit,        call) \

158. _(CALLMT,        base,        ___,        lit,        call) \

159. _(CALLT,        base,        ___,        lit,        call) \

160. _(ITERC,        base,        lit,        lit,        call) \

161. _(ITERN,        base,        lit,        lit,        call) \

162. _(VARG,        base,        lit,        lit,        ___) \

163. _(ISNEXT,        base,        ___,        jump,        ___) \

164. \

165. /* Returns. */ \

166. _(RETM,        base,        ___,        lit,        ___) \

167. _(RET,        rbase,        ___,        lit,        ___) \

168. _(RET0,        rbase,        ___,        lit,        ___) \

169. _(RET1,        rbase,        ___,        lit,        ___) \

170. \

171. /* Loops and branches. I/J = interp/JIT, I/C/L = init/call/loop. */ \

172. _(FORI,        base,        ___,        jump,        ___) \

173. _(JFORI,        base,        ___,        jump,        ___) \

174. \

175. _(FORL,        base,        ___,        jump,        ___) \

176. _(IFORL,        base,        ___,        jump,        ___) \

177. _(JFORL,        base,        ___,        lit,        ___) \

178. \

179. _(ITERL,        base,        ___,        jump,        ___) \

180. _(IITERL,        base,        ___,        jump,        ___) \

181. _(JITERL,        base,        ___,        lit,        ___) \

182. \

183. _(LOOP,        rbase,        ___,        jump,        ___) \

184. _(ILOOP,        rbase,        ___,        jump,        ___) \

185. _(JLOOP,        rbase,        ___,        lit,        ___) \

186. \

187. _(JMP,        rbase,        ___,        jump,        ___) \

188. \

189. /* Function headers. I/J = interp/JIT, F/V/C = fixarg/vararg/C func. */ \

190. _(FUNCF,        rbase,        ___,        ___,        ___) \

191. _(IFUNCF,        rbase,        ___,        ___,        ___) \

192. _(JFUNCF,        rbase,        ___,        lit,        ___) \

193. _(FUNCV,        rbase,        ___,        ___,        ___) \

194. _(IFUNCV,        rbase,        ___,        ___,        ___) \

195. _(JFUNCV,        rbase,        ___,        lit,        ___) \

196. _(FUNCC,        rbase,        ___,        ___,        ___) \

197. _(FUNCCW,        rbase,        ___,        ___,        ___)

199. /* Bytecode opcode numbers. */

200. typedef enum {

201. ‌#define BCENUM(name, ma, mb, mc, mt)        BC_##name,

202. BCDEF(BCENUM)

203. ‌#undef BCENUM

204. BC__MAX

205. ‌} BCOp;

207. LJ_STATIC_ASSERT((int)BC_ISEQV+1 == (int)BC_ISNEV);

208. LJ_STATIC_ASSERT(((int)BC_ISEQV^1) == (int)BC_ISNEV);

209. LJ_STATIC_ASSERT(((int)BC_ISEQS^1) == (int)BC_ISNES);

210. LJ_STATIC_ASSERT(((int)BC_ISEQN^1) == (int)BC_ISNEN);

211. LJ_STATIC_ASSERT(((int)BC_ISEQP^1) == (int)BC_ISNEP);

212. LJ_STATIC_ASSERT(((int)BC_ISLT^1) == (int)BC_ISGE);

213. LJ_STATIC_ASSERT(((int)BC_ISLE^1) == (int)BC_ISGT);

214. LJ_STATIC_ASSERT(((int)BC_ISLT^3) == (int)BC_ISGT);

215. LJ_STATIC_ASSERT((int)BC_IST-(int)BC_ISTC == (int)BC_ISF-(int)BC_ISFC);

216. LJ_STATIC_ASSERT((int)BC_CALLT-(int)BC_CALL == (int)BC_CALLMT-(int)BC_CALLM);

217. LJ_STATIC_ASSERT((int)BC_CALLMT + 1 == (int)BC_CALLT);

218. LJ_STATIC_ASSERT((int)BC_RETM + 1 == (int)BC_RET);

219. LJ_STATIC_ASSERT((int)BC_FORL + 1 == (int)BC_IFORL);

220. LJ_STATIC_ASSERT((int)BC_FORL + 2 == (int)BC_JFORL);

221. LJ_STATIC_ASSERT((int)BC_ITERL + 1 == (int)BC_IITERL);

222. LJ_STATIC_ASSERT((int)BC_ITERL + 2 == (int)BC_JITERL);

223. LJ_STATIC_ASSERT((int)BC_LOOP + 1 == (int)BC_ILOOP);

224. LJ_STATIC_ASSERT((int)BC_LOOP + 2 == (int)BC_JLOOP);

225. LJ_STATIC_ASSERT((int)BC_FUNCF + 1 == (int)BC_IFUNCF);

226. LJ_STATIC_ASSERT((int)BC_FUNCF + 2 == (int)BC_JFUNCF);

227. LJ_STATIC_ASSERT((int)BC_FUNCV + 1 == (int)BC_IFUNCV);

228. LJ_STATIC_ASSERT((int)BC_FUNCV + 2 == (int)BC_JFUNCV);

230. /* This solves a circular dependency problem, change as needed. */

231. ‌#define FF_next_N        4

233. /* Stack slots used by FORI/FORL, relative to operand A. */

234. enum {

235. FORL_IDX, FORL_STOP, FORL_STEP, FORL_EXT

236. };

238. /* Bytecode operand modes. ORDER BCMode */

239. typedef enum {

240. BCMnone, BCMdst, BCMbase, BCMvar, BCMrbase, BCMuv,  /* Mode A must be <= 7 */

241. BCMlit, BCMlits, BCMpri, BCMnum, BCMstr, BCMtab, BCMfunc, BCMjump, BCMcdata,

242. BCM_max

243. ‌} BCMode;

244. ‌#define BCM___                BCMnone

246. ‌#define bcmode_a(op)        ((BCMode)(lj_bc_mode[op] & 7))

247. ‌#define bcmode_b(op)        ((BCMode)((lj_bc_mode[op]>>3) & 15))

248. ‌#define bcmode_c(op)        ((BCMode)((lj_bc_mode[op]>>7) & 15))

249. ‌#define bcmode_d(op)        bcmode_c(op)

250. ‌#define bcmode_hasd(op)        ((lj_bc_mode[op] & (15<<3)) == (BCMnone<<3))

251. ‌#define bcmode_mm(op)        ((MMS)(lj_bc_mode[op]>>11))

253. ‌#define BCMODE(name, ma, mb, mc, mm) \

254. (BCM##ma|(BCM##mb<<3)|(BCM##mc<<7)|(MM_##mm<<11)),

255. ‌#define BCMODE_FF        0

257. ‌static LJ_AINLINE int bc_isret(BCOp op)

258. {

259. return (op == BC_RETM || op == BC_RET || op == BC_RET0 || op == BC_RET1);

260. }

262. ‌LJ_DATA const uint16_t lj_bc_mode[];

263. ‌LJ_DATA const uint16_t lj_bc_ofs[];

265. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_lex.h - luajit-2.0-src

Data types defined

- BCInsLine

- BCInsLine

- LexChar

- LexState

- LexState

- LexToken

- VarInfo

- VarInfo

Macros defined

- TKDEF

- TKENUM1

- TKENUM1

- TKENUM2

- TKENUM2

- _LJ_LEX_H

Source code

1. /*

2. ** Lexical analyzer.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_LEX_H

7. ‌#define _LJ_LEX_H

9. #include <stdarg.h>

11. #include "lj_obj.h"

12. #include "lj_err.h"

14. /* Lua lexer tokens. */

15. ‌#define TKDEF(_, __) \

16. _(and) _(break) _(do) _(else) _(elseif) _(end) _(false) \

17. _(for) _(function) _(goto) _(if) _(in) _(local) _(nil) _(not) _(or) \

18. _(repeat) _(return) _(then) _(true) _(until) _(while) \

19. __(concat, ..) __(dots, ...) __(eq, ==) __(ge, >=) __(le, <=) __(ne, ~=) \

20. __(label, ::) __(number, <number>) __(name, <name>) __(string, <string>) \

21. __(eof, <eof>)

23. enum {

24. TK_OFS = 256,

25. ‌#define TKENUM1(name)                TK_##name,

26. ‌#define TKENUM2(name, sym)        TK_##name,

27. TKDEF(TKENUM1, TKENUM2)

28. ‌#undef TKENUM1

29. ‌#undef TKENUM2

30. TK_RESERVED = TK_while - TK_OFS

31. };

33. ‌typedef int LexChar;        /* Lexical character. Unsigned ext. from char. */

34. ‌typedef int LexToken;        /* Lexical token. */

36. /* Combined bytecode ins/line. Only used during bytecode generation. */

37. ‌typedef struct BCInsLine {

38. BCIns ins;                /* Bytecode instruction. */

39. BCLine line;                /* Line number for this bytecode. */

40. ‌} BCInsLine;

42. /* Info for local variables. Only used during bytecode generation. */

43. ‌typedef struct VarInfo {

44. GCRef name;                /* Local variable name or goto/label name. */

45. BCPos startpc;        /* First point where the local variable is active. */

46. BCPos endpc;                /* First point where the local variable is dead. */

47. uint8_t slot;                /* Variable slot. */

48. uint8_t info;                /* Variable/goto/label info. */

49. ‌} VarInfo;

51. /* Lua lexer state. */

52. ‌typedef struct LexState {

53. struct FuncState *fs;        /* Current FuncState. Defined in lj_parse.c. */

54. struct lua_State *L;        /* Lua state. */

55. TValue tokval;        /* Current token value. */

56. TValue lookaheadval;        /* Lookahead token value. */

57. const char *p;        /* Current position in input buffer. */

58. const char *pe;        /* End of input buffer. */

59. LexChar c;                /* Current character. */

60. LexToken tok;                /* Current token. */

61. LexToken lookahead;        /* Lookahead token. */

62. SBuf sb;                /* String buffer for tokens. */

63. lua_Reader rfunc;        /* Reader callback. */

64. void *rdata;                /* Reader callback data. */

65. BCLine linenumber;        /* Input line counter. */

66. BCLine lastline;        /* Line of last token. */

67. GCstr *chunkname;        /* Current chunk name (interned string). */

68. const char *chunkarg;        /* Chunk name argument. */

69. const char *mode;        /* Allow loading bytecode (b) and/or source text (t). */

70. VarInfo *vstack;        /* Stack for names and extents of local variables. */

71. MSize sizevstack;        /* Size of variable stack. */

72. MSize vtop;                /* Top of variable stack. */

73. BCInsLine *bcstack;        /* Stack for bytecode instructions/line numbers. */

74. MSize sizebcstack;        /* Size of bytecode stack. */

75. uint32_t level;        /* Syntactical nesting level. */

76. ‌} LexState;

78. LJ_FUNC int lj_lex_setup(lua_State *L, LexState *ls);

79. LJ_FUNC void lj_lex_cleanup(lua_State *L, LexState *ls);

80. LJ_FUNC void lj_lex_next(LexState *ls);

81. LJ_FUNC LexToken lj_lex_lookahead(LexState *ls);

82. LJ_FUNC const char *lj_lex_token2str(LexState *ls, LexToken tok);

83. LJ_FUNC_NORET void lj_lex_error(LexState *ls, LexToken tok, ErrMsg em, ...);

84. LJ_FUNC void lj_lex_init(lua_State *L);

86. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_lex.c - luajit-2.0-src

Global variables defined

- tokennames

Functions defined

- lex_longstring

- lex_more

- lex_newline

- lex_next

- lex_number

- lex_save

- lex_savenext

- lex_scan

- lex_skipeq

- lex_string

- lj_lex_cleanup

- lj_lex_error

- lj_lex_init

- lj_lex_lookahead

- lj_lex_next

- lj_lex_setup

- lj_lex_token2str

Macros defined

- LEX_EOF

- LUA_CORE

- TKSTR1

- TKSTR1

- TKSTR2

- TKSTR2

- lex_iseol

- lj_lex_c

Source code

1. /*

2. ** Lexical analyzer.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lj_lex_c

10. ‌#define LUA_CORE

12. #include "lj_obj.h"

13. #include "lj_gc.h"

14. #include "lj_err.h"

15. #include "lj_buf.h"

16. #include "lj_str.h"

17. #if LJ_HASFFI

18. #include "lj_tab.h"

19. #include "lj_ctype.h"

20. #include "lj_cdata.h"

21. #include "lualib.h"

22. #endif

23. #include "lj_state.h"

24. #include "lj_lex.h"

25. #include "lj_parse.h"

26. #include "lj_char.h"

27. #include "lj_strscan.h"

28. #include "lj_strfmt.h"

30. /* Lua lexer token names. */

31. ‌static const char *const tokennames[] = {

32. ‌#define TKSTR1(name)                #name,

33. ‌#define TKSTR2(name, sym)        #sym,

34. TKDEF(TKSTR1, TKSTR2)

35. ‌#undef TKSTR1

36. ‌#undef TKSTR2

37. NULL

38. };

40. /* -- Buffer handling ----------------------------------------------------- */

42. ‌#define LEX_EOF                        (-1)

43. ‌#define lex_iseol(ls)                (ls->c == '\n' || ls->c == '\r')

45. /* Get more input from reader. */

46. ‌static LJ_NOINLINE LexChar lex_more(LexState *ls)

47. {

48. size_t sz;

49. const char *p = ls->rfunc(ls->L, ls->rdata, &sz);

50. if (p == NULL || sz == 0) return LEX_EOF;

51. ls->pe = p + sz;

52. ls->p = p + 1;

53. return (LexChar)(uint8_t)p[0];

54. }

56. /* Get next character. */

57. ‌static LJ_AINLINE LexChar lex_next(LexState *ls)

58. {

59. return (ls->c = ls->p < ls->pe ? (LexChar)(uint8_t)*ls->p++ : lex_more(ls));

60. }

62. /* Save character. */

63. ‌static LJ_AINLINE void lex_save(LexState *ls, LexChar c)

64. {

65. lj_buf_putb(&ls->sb, c);

66. }

68. /* Save previous character and get next character. */

69. ‌static LJ_AINLINE LexChar lex_savenext(LexState *ls)

70. {

71. lex_save(ls, ls->c);

72. return lex_next(ls);

73. }

75. /* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */

76. ‌static void lex_newline(LexState *ls)

77. {

78. LexChar old = ls->c;

79. lua_assert(lex_iseol(ls));

80. lex_next(ls);  /* Skip "\n" or "\r". */

81. if (lex_iseol(ls) && ls->c != old) lex_next(ls);  /* Skip "\n\r" or "\r\n". */

82. if (++ls->linenumber >= LJ_MAX_LINE)

83. lj_lex_error(ls, ls->tok, LJ_ERR_XLINES);

84. }

86. /* -- Scanner for terminals ----------------------------------------------- */

88. /* Parse a number literal. */

89. ‌static void lex_number(LexState *ls, TValue *tv)

90. {

91. StrScanFmt fmt;

92. LexChar c, xp = 'e';

93. lua_assert(lj_char_isdigit(ls->c));

94. if ((c = ls->c) == '0' && (lex_savenext(ls) | 0x20) == 'x')

95. xp = 'p';

96. while (lj_char_isident(ls->c) || ls->c == '.' ||

97. ((ls->c == '-' || ls->c == '+') && (c | 0x20) == xp)) {

98. c = ls->c;

99. lex_savenext(ls);

100. }

101. lex_save(ls, '\0');

102. fmt = lj_strscan_scan((const uint8_t *)sbufB(&ls->sb), tv,

103. (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) |

104. (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0));

105. if (LJ_DUALNUM && fmt == STRSCAN_INT) {

106. setitype(tv, LJ_TISNUM);

107. } else if (fmt == STRSCAN_NUM) {

108. /* Already in correct format. */

109. #if LJ_HASFFI

110. } else if (fmt != STRSCAN_ERROR) {

111. lua_State *L = ls->L;

112. GCcdata *cd;

113. lua_assert(fmt == STRSCAN_I64 || fmt == STRSCAN_U64 || fmt == STRSCAN_IMAG);

114. if (!ctype_ctsG(G(L))) {

115. ptrdiff_t oldtop = savestack(L, L->top);

116. luaopen_ffi(L);  /* Load FFI library on-demand. */

117. L->top = restorestack(L, oldtop);

118. }

119. if (fmt == STRSCAN_IMAG) {

120. cd = lj_cdata_new_(L, CTID_COMPLEX_DOUBLE, 2*sizeof(double));

121. ((double *)cdataptr(cd))[0] = 0;

122. ((double *)cdataptr(cd))[1] = numV(tv);

123. } else {

124. cd = lj_cdata_new_(L, fmt==STRSCAN_I64 ? CTID_INT64 : CTID_UINT64, 8);

125. *(uint64_t *)cdataptr(cd) = tv->u64;

126. }

127. lj_parse_keepcdata(ls, tv, cd);

128. #endif

129. } else {

130. lua_assert(fmt == STRSCAN_ERROR);

131. lj_lex_error(ls, TK_number, LJ_ERR_XNUMBER);

132. }

133. }

135. /* Skip equal signs for "[=...=[" and "]=...=]" and return their count. */

136. ‌static int lex_skipeq(LexState *ls)

137. {

138. int count = 0;

139. LexChar s = ls->c;

140. lua_assert(s == '[' || s == ']');

141. while (lex_savenext(ls) == '=')

142. count++;

143. return (ls->c == s) ? count : (-count) - 1;

144. }

146. /* Parse a long string or long comment (tv set to NULL). */

147. ‌static void lex_longstring(LexState *ls, TValue *tv, int sep)

148. {

149. lex_savenext(ls);  /* Skip second '['. */

150. if (lex_iseol(ls))  /* Skip initial newline. */

151. lex_newline(ls);

152. for (;;) {

153. switch (ls->c) {

154. case LEX_EOF:

155. lj_lex_error(ls, TK_eof, tv ? LJ_ERR_XLSTR : LJ_ERR_XLCOM);

156. break;

157. case ']':

158. if (lex_skipeq(ls) == sep) {

159. lex_savenext(ls);  /* Skip second ']'. */

160. goto endloop;

161. }

162. break;

163. case '\n':

164. case '\r':

165. lex_save(ls, '\n');

166. lex_newline(ls);

167. if (!tv) lj_buf_reset(&ls->sb);  /* Don't waste space for comments. */

168. break;

169. default:

170. lex_savenext(ls);

171. break;

172. }

173. } endloop:

174. if (tv) {

175. GCstr *str = lj_parse_keepstr(ls, sbufB(&ls->sb) + (2 + (MSize)sep),

176. sbuflen(&ls->sb) - 2*(2 + (MSize)sep));

177. setstrV(ls->L, tv, str);

178. }

179. }

181. /* Parse a string. */

182. ‌static void lex_string(LexState *ls, TValue *tv)

183. {

184. LexChar delim = ls->c;  /* Delimiter is '\'' or '"'. */

185. lex_savenext(ls);

186. while (ls->c != delim) {

187. switch (ls->c) {

188. case LEX_EOF:

189. lj_lex_error(ls, TK_eof, LJ_ERR_XSTR);

190. continue;

191. case '\n':

192. case '\r':

193. lj_lex_error(ls, TK_string, LJ_ERR_XSTR);

194. continue;

195. case '\\': {

196. LexChar c = lex_next(ls);  /* Skip the '\\'. */

197. switch (c) {

198. case 'a': c = '\a'; break;

199. case 'b': c = '\b'; break;

200. case 'f': c = '\f'; break;

201. case 'n': c = '\n'; break;

202. case 'r': c = '\r'; break;

203. case 't': c = '\t'; break;

204. case 'v': c = '\v'; break;

205. case 'x':  /* Hexadecimal escape '\xXX'. */

206. c = (lex_next(ls) & 15u) << 4;

207. if (!lj_char_isdigit(ls->c)) {

208. if (!lj_char_isxdigit(ls->c)) goto err_xesc;

209. c += 9 << 4;

210. }

211. c += (lex_next(ls) & 15u);

212. if (!lj_char_isdigit(ls->c)) {

213. if (!lj_char_isxdigit(ls->c)) goto err_xesc;

214. c += 9;

215. }

216. break;

217. case 'z':  /* Skip whitespace. */

218. lex_next(ls);

219. while (lj_char_isspace(ls->c))

220. if (lex_iseol(ls)) lex_newline(ls); else lex_next(ls);

221. continue;

222. case '\n': case '\r': lex_save(ls, '\n'); lex_newline(ls); continue;

223. case '\\': case '\"': case '\'': break;

224. case LEX_EOF: continue;

225. default:

226. if (!lj_char_isdigit(c))

227. goto err_xesc;

228. c -= '0';  /* Decimal escape '\ddd'. */

229. if (lj_char_isdigit(lex_next(ls))) {

230. c = c*10 + (ls->c - '0');

231. if (lj_char_isdigit(lex_next(ls))) {

232. c = c*10 + (ls->c - '0');

233. if (c > 255) {

234. err_xesc:

235. lj_lex_error(ls, TK_string, LJ_ERR_XESC);

236. }

237. lex_next(ls);

238. }

239. }

240. lex_save(ls, c);

241. continue;

242. }

243. lex_save(ls, c);

244. lex_next(ls);

245. continue;

246. }

247. default:

248. lex_savenext(ls);

249. break;

250. }

251. }

252. lex_savenext(ls);  /* Skip trailing delimiter. */

253. setstrV(ls->L, tv,

254. lj_parse_keepstr(ls, sbufB(&ls->sb)+1, sbuflen(&ls->sb)-2));

255. }

257. /* -- Main lexical scanner ------------------------------------------------ */

259. /* Get next lexical token. */

260. ‌static LexToken lex_scan(LexState *ls, TValue *tv)

261. {

262. lj_buf_reset(&ls->sb);

263. for (;;) {

264. if (lj_char_isident(ls->c)) {

265. GCstr *s;

266. if (lj_char_isdigit(ls->c)) {  /* Numeric literal. */

267. lex_number(ls, tv);

268. return TK_number;

269. }

270. /* Identifier or reserved word. */

271. do {

272. lex_savenext(ls);

273. } while (lj_char_isident(ls->c));

274. s = lj_parse_keepstr(ls, sbufB(&ls->sb), sbuflen(&ls->sb));

275. setstrV(ls->L, tv, s);

276. if (s->reserved > 0)  /* Reserved word? */

277. return TK_OFS + s->reserved;

278. return TK_name;

279. }

280. switch (ls->c) {

281. case '\n':

282. case '\r':

283. lex_newline(ls);

284. continue;

285. case ' ':

286. case '\t':

287. case '\v':

288. case '\f':

289. lex_next(ls);

290. continue;

291. case '-':

292. lex_next(ls);

293. if (ls->c != '-') return '-';

294. lex_next(ls);

295. if (ls->c == '[') {  /* Long comment "--[=*[...]=*]". */

296. int sep = lex_skipeq(ls);

297. lj_buf_reset(&ls->sb);  /* `lex_skipeq' may dirty the buffer */

298. if (sep >= 0) {

299. lex_longstring(ls, NULL, sep);

300. lj_buf_reset(&ls->sb);

301. continue;

302. }

303. }

304. /* Short comment "--.*\n". */

305. while (!lex_iseol(ls) && ls->c != LEX_EOF)

306. lex_next(ls);

307. continue;

308. case '[': {

309. int sep = lex_skipeq(ls);

310. if (sep >= 0) {

311. lex_longstring(ls, tv, sep);

312. return TK_string;

313. } else if (sep == -1) {

314. return '[';

315. } else {

316. lj_lex_error(ls, TK_string, LJ_ERR_XLDELIM);

317. continue;

318. }

319. }

320. case '=':

321. lex_next(ls);

322. if (ls->c != '=') return '='; else { lex_next(ls); return TK_eq; }

323. case '<':

324. lex_next(ls);

325. if (ls->c != '=') return '<'; else { lex_next(ls); return TK_le; }

326. case '>':

327. lex_next(ls);

328. if (ls->c != '=') return '>'; else { lex_next(ls); return TK_ge; }

329. case '~':

330. lex_next(ls);

331. if (ls->c != '=') return '~'; else { lex_next(ls); return TK_ne; }

332. case ':':

333. lex_next(ls);

334. if (ls->c != ':') return ':'; else { lex_next(ls); return TK_label; }

335. case '"':

336. case '\'':

337. lex_string(ls, tv);

338. return TK_string;

339. case '.':

340. if (lex_savenext(ls) == '.') {

341. lex_next(ls);

342. if (ls->c == '.') {

343. lex_next(ls);

344. return TK_dots;   /* ... */

345. }

346. return TK_concat;   /* .. */

347. } else if (!lj_char_isdigit(ls->c)) {

348. return '.';

349. } else {

350. lex_number(ls, tv);

351. return TK_number;

352. }

353. case LEX_EOF:

354. return TK_eof;

355. default: {

356. LexChar c = ls->c;

357. lex_next(ls);

358. return c;  /* Single-char tokens (+ - / ...). */

359. }

360. }

361. }

362. }

364. /* -- Lexer API ----------------------------------------------------------- */

366. /* Setup lexer state. */

367. ‌int lj_lex_setup(lua_State *L, LexState *ls)

368. {

369. int header = 0;

370. ls->L = L;

371. ls->fs = NULL;

372. ls->pe = ls->p = NULL;

373. ls->vstack = NULL;

374. ls->sizevstack = 0;

375. ls->vtop = 0;

376. ls->bcstack = NULL;

377. ls->sizebcstack = 0;

378. ls->tok = 0;

379. ls->lookahead = TK_eof;  /* No look-ahead token. */

380. ls->linenumber = 1;

381. ls->lastline = 1;

382. lex_next(ls);  /* Read-ahead first char. */

383. if (ls->c == 0xef && ls->p + 2 <= ls->pe && (uint8_t)ls->p[0] == 0xbb &&

384. (uint8_t)ls->p[1] == 0xbf) {  /* Skip UTF-8 BOM (if buffered). */

385. ls->p += 2;

386. lex_next(ls);

387. header = 1;

388. }

389. if (ls->c == '#') {  /* Skip POSIX #! header line. */

390. do {

391. lex_next(ls);

392. if (ls->c == LEX_EOF) return 0;

393. } while (!lex_iseol(ls));

394. lex_newline(ls);

395. header = 1;

396. }

397. if (ls->c == LUA_SIGNATURE[0]) {  /* Bytecode dump. */

398. if (header) {

399. /*

400. ** Loading bytecode with an extra header is disabled for security

401. ** reasons. This may circumvent the usual check for bytecode vs.

402. ** Lua code by looking at the first char. Since this is a potential

403. ** security violation no attempt is made to echo the chunkname either.

404. */

405. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_BCBAD));

406. lj_err_throw(L, LUA_ERRSYNTAX);

407. }

408. return 1;

409. }

410. return 0;

411. }

413. /* Cleanup lexer state. */

414. ‌void lj_lex_cleanup(lua_State *L, LexState *ls)

415. {

416. global_State *g = G(L);

417. lj_mem_freevec(g, ls->bcstack, ls->sizebcstack, BCInsLine);

418. lj_mem_freevec(g, ls->vstack, ls->sizevstack, VarInfo);

419. lj_buf_free(g, &ls->sb);

420. }

422. /* Return next lexical token. */

423. ‌void lj_lex_next(LexState *ls)

424. {

425. ls->lastline = ls->linenumber;

426. if (LJ_LIKELY(ls->lookahead == TK_eof)) {  /* No lookahead token? */

427. ls->tok = lex_scan(ls, &ls->tokval);  /* Get next token. */

428. } else {  /* Otherwise return lookahead token. */

429. ls->tok = ls->lookahead;

430. ls->lookahead = TK_eof;

431. ls->tokval = ls->lookaheadval;

432. }

433. }

435. /* Look ahead for the next token. */

436. ‌LexToken lj_lex_lookahead(LexState *ls)

437. {

438. lua_assert(ls->lookahead == TK_eof);

439. ls->lookahead = lex_scan(ls, &ls->lookaheadval);

440. return ls->lookahead;

441. }

443. /* Convert token to string. */

444. ‌const char *lj_lex_token2str(LexState *ls, LexToken tok)

445. {

446. if (tok > TK_OFS)

447. return tokennames[tok-TK_OFS-1];

448. else if (!lj_char_iscntrl(tok))

449. return lj_strfmt_pushf(ls->L, "%c", tok);

450. else

451. return lj_strfmt_pushf(ls->L, "char(%d)", tok);

452. }

454. /* Lexer error. */

455. ‌void lj_lex_error(LexState *ls, LexToken tok, ErrMsg em, ...)

456. {

457. const char *tokstr;

458. va_list argp;

459. if (tok == 0) {

460. tokstr = NULL;

461. } else if (tok == TK_name || tok == TK_string || tok == TK_number) {

462. lex_save(ls, '\0');

463. tokstr = sbufB(&ls->sb);

464. } else {

465. tokstr = lj_lex_token2str(ls, tok);

466. }

467. va_start(argp, em);

468. lj_err_lex(ls->L, ls->chunkname, tokstr, ls->linenumber, em, argp);

469. va_end(argp);

470. }

472. /* Initialize strings for reserved words. */

473. ‌void lj_lex_init(lua_State *L)

474. {

475. uint32_t i;

476. for (i = 0; i < TK_RESERVED; i++) {

477. GCstr *s = lj_str_newz(L, tokennames[i]);

478. fixstring(s);  /* Reserved words are never collected. */

479. s->reserved = (uint8_t)(i+1);

480. }

481. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_buf.h - luajit-2.0-src

Functions defined

- lj_buf_free

- lj_buf_init

- lj_buf_more

- lj_buf_need

- lj_buf_putb

- lj_buf_reset

- lj_buf_str

- lj_buf_tmp_

- lj_buf_wmem

Macros defined

- _LJ_BUF_H

- sbufB

- sbufE

- sbufL

- sbufP

- sbufleft

- sbuflen

- sbufsz

- setsbufL

- setsbufP

Source code

1. /*

2. ** Buffer handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_BUF_H

7. ‌#define _LJ_BUF_H

9. #include "lj_obj.h"

10. #include "lj_gc.h"

11. #include "lj_str.h"

13. /* Resizable string buffers. Struct definition in lj_obj.h. */

14. ‌#define sbufB(sb)        (mref((sb)->b, char))

15. ‌#define sbufP(sb)        (mref((sb)->p, char))

16. ‌#define sbufE(sb)        (mref((sb)->e, char))

17. ‌#define sbufL(sb)        (mref((sb)->L, lua_State))

18. ‌#define sbufsz(sb)        ((MSize)(sbufE((sb)) - sbufB((sb))))

19. ‌#define sbuflen(sb)        ((MSize)(sbufP((sb)) - sbufB((sb))))

20. ‌#define sbufleft(sb)        ((MSize)(sbufE((sb)) - sbufP((sb))))

21. ‌#define setsbufP(sb, q)        (setmref((sb)->p, (q)))

22. ‌#define setsbufL(sb, l)        (setmref((sb)->L, (l)))

24. /* Buffer management */

25. LJ_FUNC char *LJ_FASTCALL lj_buf_need2(SBuf *sb, MSize sz);

26. LJ_FUNC char *LJ_FASTCALL lj_buf_more2(SBuf *sb, MSize sz);

27. LJ_FUNC void LJ_FASTCALL lj_buf_shrink(lua_State *L, SBuf *sb);

28. LJ_FUNC char * LJ_FASTCALL lj_buf_tmp(lua_State *L, MSize sz);

30. ‌static LJ_AINLINE void lj_buf_init(lua_State *L, SBuf *sb)

31. {

32. setsbufL(sb, L);

33. setmref(sb->p, NULL); setmref(sb->e, NULL); setmref(sb->b, NULL);

34. }

36. ‌static LJ_AINLINE void lj_buf_reset(SBuf *sb)

37. {

38. setmrefr(sb->p, sb->b);

39. }

41. ‌static LJ_AINLINE SBuf *lj_buf_tmp_(lua_State *L)

42. {

43. SBuf *sb = &G(L)->tmpbuf;

44. setsbufL(sb, L);

45. lj_buf_reset(sb);

46. return sb;

47. }

49. ‌static LJ_AINLINE void lj_buf_free(global_State *g, SBuf *sb)

50. {

51. lj_mem_free(g, sbufB(sb), sbufsz(sb));

52. }

54. ‌static LJ_AINLINE char *lj_buf_need(SBuf *sb, MSize sz)

55. {

56. if (LJ_UNLIKELY(sz > sbufsz(sb)))

57. return lj_buf_need2(sb, sz);

58. return sbufB(sb);

59. }

61. ‌static LJ_AINLINE char *lj_buf_more(SBuf *sb, MSize sz)

62. {

63. if (LJ_UNLIKELY(sz > sbufleft(sb)))

64. return lj_buf_more2(sb, sz);

65. return sbufP(sb);

66. }

68. /* Low-level buffer put operations */

69. LJ_FUNC SBuf *lj_buf_putmem(SBuf *sb, const void *q, MSize len);

70. #if LJ_HASJIT

71. LJ_FUNC SBuf * LJ_FASTCALL lj_buf_putchar(SBuf *sb, int c);

72. #endif

73. LJ_FUNC SBuf * LJ_FASTCALL lj_buf_putstr(SBuf *sb, GCstr *s);

75. ‌static LJ_AINLINE char *lj_buf_wmem(char *p, const void *q, MSize len)

76. {

77. return (char *)memcpy(p, q, len) + len;

78. }

80. ‌static LJ_AINLINE void lj_buf_putb(SBuf *sb, int c)

81. {

82. char *p = lj_buf_more(sb, 1);

83. *p++ = (char)c;

84. setsbufP(sb, p);

85. }

87. /* High-level buffer put operations */

88. LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_reverse(SBuf *sb, GCstr *s);

89. LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_lower(SBuf *sb, GCstr *s);

90. LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_upper(SBuf *sb, GCstr *s);

91. LJ_FUNC SBuf *lj_buf_putstr_rep(SBuf *sb, GCstr *s, int32_t rep);

92. LJ_FUNC SBuf *lj_buf_puttab(SBuf *sb, GCtab *t, GCstr *sep,

93. int32_t i, int32_t e);

95. /* Miscellaneous buffer operations */

96. LJ_FUNCA GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb);

97. LJ_FUNC GCstr *lj_buf_cat2str(lua_State *L, GCstr *s1, GCstr *s2);

98. LJ_FUNC uint32_t LJ_FASTCALL lj_buf_ruleb128(const char **pp);

100. ‌static LJ_AINLINE GCstr *lj_buf_str(lua_State *L, SBuf *sb)

101. {

102. return lj_str_new(L, sbufB(sb), sbuflen(sb));

103. }

105. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_buf.c - luajit-2.0-src

Functions defined

- buf_grow

- lj_buf_cat2str

- lj_buf_more2

- lj_buf_need2

- lj_buf_putchar

- lj_buf_putmem

- lj_buf_putstr

- lj_buf_putstr_lower

- lj_buf_putstr_rep

- lj_buf_putstr_reverse

- lj_buf_putstr_upper

- lj_buf_puttab

- lj_buf_ruleb128

- lj_buf_shrink

- lj_buf_tmp

- lj_buf_tostr

Macros defined

- LUA_CORE

- lj_buf_c

Source code

1. /*

2. ** Buffer handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_buf_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_buf.h"

13. #include "lj_str.h"

14. #include "lj_tab.h"

15. #include "lj_strfmt.h"

17. /* -- Buffer management --------------------------------------------------- */

19. ‌static void buf_grow(SBuf *sb, MSize sz)

20. {

21. MSize osz = sbufsz(sb), len = sbuflen(sb), nsz = osz;

22. char *b;

23. if (nsz < LJ_MIN_SBUF) nsz = LJ_MIN_SBUF;

24. while (nsz < sz) nsz += nsz;

25. b = (char *)lj_mem_realloc(sbufL(sb), sbufB(sb), osz, nsz);

26. setmref(sb->b, b);

27. setmref(sb->p, b + len);

28. setmref(sb->e, b + nsz);

29. }

31. ‌LJ_NOINLINE char *LJ_FASTCALL lj_buf_need2(SBuf *sb, MSize sz)

32. {

33. lua_assert(sz > sbufsz(sb));

34. if (LJ_UNLIKELY(sz > LJ_MAX_BUF))

35. lj_err_mem(sbufL(sb));

36. buf_grow(sb, sz);

37. return sbufB(sb);

38. }

40. ‌LJ_NOINLINE char *LJ_FASTCALL lj_buf_more2(SBuf *sb, MSize sz)

41. {

42. MSize len = sbuflen(sb);

43. lua_assert(sz > sbufleft(sb));

44. if (LJ_UNLIKELY(sz > LJ_MAX_BUF || len + sz > LJ_MAX_BUF))

45. lj_err_mem(sbufL(sb));

46. buf_grow(sb, len + sz);

47. return sbufP(sb);

48. }

50. ‌void LJ_FASTCALL lj_buf_shrink(lua_State *L, SBuf *sb)

51. {

52. char *b = sbufB(sb);

53. MSize osz = (MSize)(sbufE(sb) - b);

54. if (osz > 2*LJ_MIN_SBUF) {

55. MSize n = (MSize)(sbufP(sb) - b);

56. b = lj_mem_realloc(L, b, osz, (osz >> 1));

57. setmref(sb->b, b);

58. setmref(sb->p, b + n);

59. setmref(sb->e, b + (osz >> 1));

60. }

61. }

63. ‌char * LJ_FASTCALL lj_buf_tmp(lua_State *L, MSize sz)

64. {

65. SBuf *sb = &G(L)->tmpbuf;

66. setsbufL(sb, L);

67. return lj_buf_need(sb, sz);

68. }

70. /* -- Low-level buffer put operations ------------------------------------- */

72. ‌SBuf *lj_buf_putmem(SBuf *sb, const void *q, MSize len)

73. {

74. char *p = lj_buf_more(sb, len);

75. p = lj_buf_wmem(p, q, len);

76. setsbufP(sb, p);

77. return sb;

78. }

80. #if LJ_HASJIT

81. ‌SBuf * LJ_FASTCALL lj_buf_putchar(SBuf *sb, int c)

82. {

83. char *p = lj_buf_more(sb, 1);

84. *p++ = (char)c;

85. setsbufP(sb, p);

86. return sb;

87. }

88. #endif

90. ‌SBuf * LJ_FASTCALL lj_buf_putstr(SBuf *sb, GCstr *s)

91. {

92. MSize len = s->len;

93. char *p = lj_buf_more(sb, len);

94. p = lj_buf_wmem(p, strdata(s), len);

95. setsbufP(sb, p);

96. return sb;

97. }

99. /* -- High-level buffer put operations ------------------------------------ */

101. ‌SBuf * LJ_FASTCALL lj_buf_putstr_reverse(SBuf *sb, GCstr *s)

102. {

103. MSize len = s->len;

104. char *p = lj_buf_more(sb, len), *e = p+len;

105. const char *q = strdata(s)+len-1;

106. while (p < e)

107. *p++ = *q--;

108. setsbufP(sb, p);

109. return sb;

110. }

112. ‌SBuf * LJ_FASTCALL lj_buf_putstr_lower(SBuf *sb, GCstr *s)

113. {

114. MSize len = s->len;

115. char *p = lj_buf_more(sb, len), *e = p+len;

116. const char *q = strdata(s);

117. for (; p < e; p++, q++) {

118. uint32_t c = *(unsigned char *)q;

119. #if LJ_TARGET_PPC

120. *p = c + ((c >= 'A' && c <= 'Z') << 5);

121. #else

122. if (c >= 'A' && c <= 'Z') c += 0x20;

123. *p = c;

124. #endif

125. }

126. setsbufP(sb, p);

127. return sb;

128. }

130. ‌SBuf * LJ_FASTCALL lj_buf_putstr_upper(SBuf *sb, GCstr *s)

131. {

132. MSize len = s->len;

133. char *p = lj_buf_more(sb, len), *e = p+len;

134. const char *q = strdata(s);

135. for (; p < e; p++, q++) {

136. uint32_t c = *(unsigned char *)q;

137. #if LJ_TARGET_PPC

138. *p = c - ((c >= 'a' && c <= 'z') << 5);

139. #else

140. if (c >= 'a' && c <= 'z') c -= 0x20;

141. *p = c;

142. #endif

143. }

144. setsbufP(sb, p);

145. return sb;

146. }

148. ‌SBuf *lj_buf_putstr_rep(SBuf *sb, GCstr *s, int32_t rep)

149. {

150. MSize len = s->len;

151. if (rep > 0 && len) {

152. uint64_t tlen = (uint64_t)rep * len;

153. char *p;

154. if (LJ_UNLIKELY(tlen > LJ_MAX_STR))

155. lj_err_mem(sbufL(sb));

156. p = lj_buf_more(sb, (MSize)tlen);

157. if (len == 1) {  /* Optimize a common case. */

158. uint32_t c = strdata(s)[0];

159. do { *p++ = c; } while (--rep > 0);

160. } else {

161. const char *e = strdata(s) + len;

162. do {

163. const char *q = strdata(s);

164. do { *p++ = *q++; } while (q < e);

165. } while (--rep > 0);

166. }

167. setsbufP(sb, p);

168. }

169. return sb;

170. }

172. ‌SBuf *lj_buf_puttab(SBuf *sb, GCtab *t, GCstr *sep, int32_t i, int32_t e)

173. {

174. MSize seplen = sep ? sep->len : 0;

175. if (i <= e) {

176. for (;;) {

177. cTValue *o = lj_tab_getint(t, i);

178. char *p;

179. if (!o) {

180. badtype:  /* Error: bad element type. */

181. setsbufP(sb, (void *)(intptr_t)i);  /* Store failing index. */

182. return NULL;

183. } else if (tvisstr(o)) {

184. MSize len = strV(o)->len;

185. p = lj_buf_wmem(lj_buf_more(sb, len + seplen), strVdata(o), len);

186. } else if (tvisint(o)) {

187. p = lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT+seplen), intV(o));

188. } else if (tvisnum(o)) {

189. p = lj_strfmt_wnum(lj_buf_more(sb, STRFMT_MAXBUF_NUM+seplen), o);

190. } else {

191. goto badtype;

192. }

193. if (i++ == e) {

194. setsbufP(sb, p);

195. break;

196. }

197. if (seplen) p = lj_buf_wmem(p, strdata(sep), seplen);

198. setsbufP(sb, p);

199. }

200. }

201. return sb;

202. }

204. /* -- Miscellaneous buffer operations ------------------------------------- */

206. ‌GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb)

207. {

208. return lj_str_new(sbufL(sb), sbufB(sb), sbuflen(sb));

209. }

211. /* Concatenate two strings. */

212. ‌GCstr *lj_buf_cat2str(lua_State *L, GCstr *s1, GCstr *s2)

213. {

214. MSize len1 = s1->len, len2 = s2->len;

215. char *buf = lj_buf_tmp(L, len1 + len2);

216. memcpy(buf, strdata(s1), len1);

217. memcpy(buf+len1, strdata(s2), len2);

218. return lj_str_new(L, buf, len1 + len2);

219. }

221. /* Read ULEB128 from buffer. */

222. ‌uint32_t LJ_FASTCALL lj_buf_ruleb128(const char **pp)

223. {

224. const uint8_t *p = (const uint8_t *)*pp;

225. uint32_t v = *p++;

226. if (LJ_UNLIKELY(v >= 0x80)) {

227. int sh = 0;

228. v &= 0x7f;

229. do { v |= ((*p & 0x7f) << (sh += 7)); } while (*p++ >= 0x80);

230. }

231. *pp = (const char *)p;

232. return v;

233. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_strfmt.c - luajit-2.0-src

Global variables defined

- strfmt_map

Functions defined

- lj_strfmt_char

- lj_strfmt_int

- lj_strfmt_num

- lj_strfmt_number

- lj_strfmt_obj

- lj_strfmt_parse

- lj_strfmt_pushf

- lj_strfmt_pushvf

- lj_strfmt_putfchar

- lj_strfmt_putfnum

- lj_strfmt_putfnum_int

- lj_strfmt_putfnum_uint

- lj_strfmt_putfstr

- lj_strfmt_putfxint

- lj_strfmt_putint

- lj_strfmt_putnum

- lj_strfmt_putptr

- lj_strfmt_putquoted

- lj_strfmt_wint

- lj_strfmt_wnum

- lj_strfmt_wptr

- lj_strfmt_wstrnum

- lj_strfmt_wuleb128

Macros defined

- LUA_CORE

- STRFMT_FMTNUMBUF

- WINT_R

- WINT_R

- lj_strfmt_c

Source code

1. /*

2. ** String formatting.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include <stdio.h>

8. ‌#define lj_strfmt_c

9. ‌#define LUA_CORE

11. #include "lj_obj.h"

12. #include "lj_buf.h"

13. #include "lj_str.h"

14. #include "lj_state.h"

15. #include "lj_char.h"

16. #include "lj_strfmt.h"

18. /* -- Format parser ------------------------------------------------------- */

20. ‌static const uint8_t strfmt_map[('x'-'A')+1] = {

21. STRFMT_A,0,0,0,STRFMT_E,0,STRFMT_G,0,0,0,0,0,0,

22. 0,0,0,0,0,0,0,0,0,0,STRFMT_X,0,0,

23. 0,0,0,0,0,0,

24. STRFMT_A,0,STRFMT_C,STRFMT_D,STRFMT_E,STRFMT_F,STRFMT_G,0,STRFMT_I,0,0,0,0,

25. 0,STRFMT_O,STRFMT_P,STRFMT_Q,0,STRFMT_S,0,STRFMT_U,0,0,STRFMT_X

26. };

28. ‌SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs)

29. {

30. const uint8_t *p = fs->p, *e = fs->e;

31. fs->str = (const char *)p;

32. for (; p < e; p++) {

33. if (*p == '%') {  /* Escape char? */

34. if (p[1] == '%') {  /* '%%'? */

35. fs->p = ++p+1;

36. goto retlit;

37. } else {

38. SFormat sf = 0;

39. uint32_t c;

40. if (p != (const uint8_t *)fs->str)

41. break;

42. for (p++; (uint32_t)*p - ' ' <= (uint32_t)('0' - ' '); p++) {

43. /* Parse flags. */

44. if (*p == '-') sf |= STRFMT_F_LEFT;

45. else if (*p == '+') sf |= STRFMT_F_PLUS;

46. else if (*p == '0') sf |= STRFMT_F_ZERO;

47. else if (*p == ' ') sf |= STRFMT_F_SPACE;

48. else if (*p == '#') sf |= STRFMT_F_ALT;

49. else break;

50. }

51. if ((uint32_t)*p - '0' < 10) {  /* Parse width. */

52. uint32_t width = (uint32_t)*p++ - '0';

53. if ((uint32_t)*p - '0' < 10)

54. width = (uint32_t)*p++ - '0' + width*10;

55. sf |= (width << STRFMT_SH_WIDTH);

56. }

57. if (*p == '.') {  /* Parse precision. */

58. uint32_t prec = 0;

59. p++;

60. if ((uint32_t)*p - '0' < 10) {

61. prec = (uint32_t)*p++ - '0';

62. if ((uint32_t)*p - '0' < 10)

63. prec = (uint32_t)*p++ - '0' + prec*10;

64. }

65. sf |= ((prec+1) << STRFMT_SH_PREC);

66. }

67. /* Parse conversion. */

68. c = (uint32_t)*p - 'A';

69. if (LJ_LIKELY(c <= (uint32_t)('x' - 'A'))) {

70. uint32_t sx = strfmt_map[c];

71. if (sx) {

72. fs->p = p+1;

73. return (sf | sx | ((c & 0x20) ? 0 : STRFMT_F_UPPER));

74. }

75. }

76. /* Return error location. */

77. if (*p >= 32) p++;

78. fs->len = (MSize)(p - (const uint8_t *)fs->str);

79. fs->p = fs->e;

80. return STRFMT_ERR;

81. }

82. }

83. }

84. fs->p = p;

85. retlit:

86. fs->len = (MSize)(p - (const uint8_t *)fs->str);

87. return fs->len ? STRFMT_LIT : STRFMT_EOF;

88. }

90. /* -- Raw conversions ----------------------------------------------------- */

92. /* Write number to bufer. */

93. ‌char * LJ_FASTCALL lj_strfmt_wnum(char *p, cTValue *o)

94. {

95. if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) {  /* Finite? */

96. #if __BIONIC__

97. if (tvismzero(o)) { *p++ = '-'; *p++ = '0'; return p; }

98. #endif

99. return p + lua_number2str(p, o->n);

100. } else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) {

101. *p++ = 'n'; *p++ = 'a'; *p++ = 'n';

102. } else if ((o->u32.hi & 0x80000000) == 0) {

103. *p++ = 'i'; *p++ = 'n'; *p++ = 'f';

104. } else {

105. *p++ = '-'; *p++ = 'i'; *p++ = 'n'; *p++ = 'f';

106. }

107. return p;

108. }

110. ‌#define WINT_R(x, sh, sc) \

111. { uint32_t d = (x*(((1<<sh)+sc-1)/sc))>>sh; x -= d*sc; *p++ = (char)('0'+d); }

113. /* Write integer to buffer. */

114. ‌char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k)

115. {

116. uint32_t u = (uint32_t)k;

117. if (k < 0) { u = (uint32_t)-k; *p++ = '-'; }

118. if (u < 10000) {

119. if (u < 10) goto dig1; if (u < 100) goto dig2; if (u < 1000) goto dig3;

120. } else {

121. uint32_t v = u / 10000; u -= v * 10000;

122. if (v < 10000) {

123. if (v < 10) goto dig5; if (v < 100) goto dig6; if (v < 1000) goto dig7;

124. } else {

125. uint32_t w = v / 10000; v -= w * 10000;

126. if (w >= 10) WINT_R(w, 10, 10)

127. *p++ = (char)('0'+w);

128. }

129. WINT_R(v, 23, 1000)

130. dig7: WINT_R(v, 12, 100)

131. dig6: WINT_R(v, 10, 10)

132. dig5: *p++ = (char)('0'+v);

133. }

134. WINT_R(u, 23, 1000)

135. dig3: WINT_R(u, 12, 100)

136. dig2: WINT_R(u, 10, 10)

137. dig1: *p++ = (char)('0'+u);

138. return p;

139. }

140. ‌#undef WINT_R

142. /* Write pointer to buffer. */

143. ‌char * LJ_FASTCALL lj_strfmt_wptr(char *p, const void *v)

144. {

145. ptrdiff_t x = (ptrdiff_t)v;

146. MSize i, n = STRFMT_MAXBUF_PTR;

147. if (x == 0) {

148. *p++ = 'N'; *p++ = 'U'; *p++ = 'L'; *p++ = 'L';

149. return p;

150. }

151. #if LJ_64

152. /* Shorten output for 64 bit pointers. */

153. n = 2+2*4+((x >> 32) ? 2+2*(lj_fls((uint32_t)(x >> 32))>>3) : 0);

154. #endif

155. p[0] = '0';

156. p[1] = 'x';

157. for (i = n-1; i >= 2; i--, x >>= 4)

158. p[i] = "0123456789abcdef"[(x & 15)];

159. return p+n;

160. }

162. /* Write ULEB128 to buffer. */

163. ‌char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v)

164. {

165. for (; v >= 0x80; v >>= 7)

166. *p++ = (char)((v & 0x7f) | 0x80);

167. *p++ = (char)v;

168. return p;

169. }

171. /* Return string or write number to buffer and return pointer to start. */

172. ‌const char *lj_strfmt_wstrnum(char *buf, cTValue *o, MSize *lenp)

173. {

174. if (tvisstr(o)) {

175. *lenp = strV(o)->len;

176. return strVdata(o);

177. } else if (tvisint(o)) {

178. *lenp = (MSize)(lj_strfmt_wint(buf, intV(o)) - buf);

179. return buf;

180. } else if (tvisnum(o)) {

181. *lenp = (MSize)(lj_strfmt_wnum(buf, o) - buf);

182. return buf;

183. } else {

184. return NULL;

185. }

186. }

188. /* -- Unformatted conversions to buffer ----------------------------------- */

190. /* Add integer to buffer. */

191. ‌SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k)

192. {

193. setsbufP(sb, lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT), k));

194. return sb;

195. }

197. #if LJ_HASJIT

198. /* Add number to buffer. */

199. ‌SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o)

200. {

201. setsbufP(sb, lj_strfmt_wnum(lj_buf_more(sb, STRFMT_MAXBUF_NUM), o));

202. return sb;

203. }

204. #endif

206. ‌SBuf * LJ_FASTCALL lj_strfmt_putptr(SBuf *sb, const void *v)

207. {

208. setsbufP(sb, lj_strfmt_wptr(lj_buf_more(sb, STRFMT_MAXBUF_PTR), v));

209. return sb;

210. }

212. /* Add quoted string to buffer. */

213. ‌SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str)

214. {

215. const char *s = strdata(str);

216. MSize len = str->len;

217. lj_buf_putb(sb, '"');

218. while (len--) {

219. uint32_t c = (uint32_t)(uint8_t)*s++;

220. char *p = lj_buf_more(sb, 4);

221. if (c == '"' || c == '\\' || c == '\n') {

222. *p++ = '\\';

223. } else if (lj_char_iscntrl(c)) {  /* This can only be 0-31 or 127. */

224. uint32_t d;

225. *p++ = '\\';

226. if (c >= 100 || lj_char_isdigit((uint8_t)*s)) {

227. *p++ = (char)('0'+(c >= 100)); if (c >= 100) c -= 100;

228. goto tens;

229. } else if (c >= 10) {

230. tens:

231. d = (c * 205) >> 11; c -= d * 10; *p++ = (char)('0'+d);

232. }

233. c += '0';

234. }

235. *p++ = (char)c;

236. setsbufP(sb, p);

237. }

238. lj_buf_putb(sb, '"');

239. return sb;

240. }

242. /* -- Formatted conversions to buffer ------------------------------------- */

244. /* Add formatted char to buffer. */

245. ‌SBuf *lj_strfmt_putfchar(SBuf *sb, SFormat sf, int32_t c)

246. {

247. MSize width = STRFMT_WIDTH(sf);

248. char *p = lj_buf_more(sb, width > 1 ? width : 1);

249. if ((sf & STRFMT_F_LEFT)) *p++ = (char)c;

250. while (width-- > 1) *p++ = ' ';

251. if (!(sf & STRFMT_F_LEFT)) *p++ = (char)c;

252. setsbufP(sb, p);

253. return sb;

254. }

256. /* Add formatted string to buffer. */

257. ‌SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat sf, GCstr *str)

258. {

259. MSize len = str->len <= STRFMT_PREC(sf) ? str->len : STRFMT_PREC(sf);

260. MSize width = STRFMT_WIDTH(sf);

261. char *p = lj_buf_more(sb, width > len ? width : len);

262. if ((sf & STRFMT_F_LEFT)) p = lj_buf_wmem(p, strdata(str), len);

263. while (width-- > len) *p++ = ' ';

264. if (!(sf & STRFMT_F_LEFT)) p = lj_buf_wmem(p, strdata(str), len);

265. setsbufP(sb, p);

266. return sb;

267. }

269. /* Add formatted signed/unsigned integer to buffer. */

270. ‌SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k)

271. {

272. char buf[STRFMT_MAXBUF_XINT], *q = buf + sizeof(buf), *p;

273. #ifdef LUA_USE_ASSERT

274. char *ps;

275. #endif

276. MSize prefix = 0, len, prec, pprec, width, need;

278. /* Figure out signed prefixes. */

279. if (STRFMT_TYPE(sf) == STRFMT_INT) {

280. if ((int64_t)k < 0) {

281. k = (uint64_t)-(int64_t)k;

282. prefix = 256 + '-';

283. } else if ((sf & STRFMT_F_PLUS)) {

284. prefix = 256 + '+';

285. } else if ((sf & STRFMT_F_SPACE)) {

286. prefix = 256 + ' ';

287. }

288. }

290. /* Convert number and store to fixed-size buffer in reverse order. */

291. prec = STRFMT_PREC(sf);

292. if ((int32_t)prec >= 0) sf &= ~STRFMT_F_ZERO;

293. if (k == 0) {  /* Special-case zero argument. */

294. if (prec != 0 ||

295. (sf & (STRFMT_T_OCT|STRFMT_F_ALT)) == (STRFMT_T_OCT|STRFMT_F_ALT))

296. *--q = '0';

297. } else if (!(sf & (STRFMT_T_HEX|STRFMT_T_OCT))) {  /* Decimal. */

298. uint32_t k2;

299. while ((k >> 32)) { *--q = (char)('0' + k % 10); k /= 10; }

300. k2 = (uint32_t)k;

301. do { *--q = (char)('0' + k2 % 10); k2 /= 10; } while (k2);

302. } else if ((sf & STRFMT_T_HEX)) {  /* Hex. */

303. const char *hexdig = (sf & STRFMT_F_UPPER) ? "0123456789ABCDEF" :

304. "0123456789abcdef";

305. do { *--q = hexdig[(k & 15)]; k >>= 4; } while (k);

306. if ((sf & STRFMT_F_ALT)) prefix = 512 + ((sf & STRFMT_F_UPPER) ? 'X' : 'x');

307. } else {  /* Octal. */

308. do { *--q = (char)('0' + (uint32_t)(k & 7)); k >>= 3; } while (k);

309. if ((sf & STRFMT_F_ALT)) *--q = '0';

310. }

312. /* Calculate sizes. */

313. len = (MSize)(buf + sizeof(buf) - q);

314. if ((int32_t)len >= (int32_t)prec) prec = len;

315. width = STRFMT_WIDTH(sf);

316. pprec = prec + (prefix >> 8);

317. need = width > pprec ? width : pprec;

318. p = lj_buf_more(sb, need);

319. #ifdef LUA_USE_ASSERT

320. ps = p;

321. #endif

323. /* Format number with leading/trailing whitespace and zeros. */

324. if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == 0)

325. while (width-- > pprec) *p++ = ' ';

326. if (prefix) {

327. if ((char)prefix >= 'X') *p++ = '0';

328. *p++ = (char)prefix;

329. }

330. if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == STRFMT_F_ZERO)

331. while (width-- > pprec) *p++ = '0';

332. while (prec-- > len) *p++ = '0';

333. while (q < buf + sizeof(buf)) *p++ = *q++;  /* Add number itself. */

334. if ((sf & STRFMT_F_LEFT))

335. while (width-- > pprec) *p++ = ' ';

337. lua_assert(need == (MSize)(p - ps));

338. setsbufP(sb, p);

339. return sb;

340. }

342. /* Add number formatted as signed integer to buffer. */

343. ‌SBuf *lj_strfmt_putfnum_int(SBuf *sb, SFormat sf, lua_Number n)

344. {

345. int64_t k = (int64_t)n;

346. if (checki32(k) && sf == STRFMT_INT)

347. return lj_strfmt_putint(sb, (int32_t)k);  /* Shortcut for plain %d. */

348. else

349. return lj_strfmt_putfxint(sb, sf, (uint64_t)k);

350. }

352. /* Add number formatted as unsigned integer to buffer. */

353. ‌SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n)

354. {

355. int64_t k;

356. if (n >= 9223372036854775808.0)

357. k = (int64_t)(n - 18446744073709551616.0);

358. else

359. k = (int64_t)n;

360. return lj_strfmt_putfxint(sb, sf, (uint64_t)k);

361. }

363. /* Max. sprintf buffer size needed. At least #string.format("%.99f", -1e308). */

364. ‌#define STRFMT_FMTNUMBUF        512

366. /* Add formatted floating-point number to buffer. */

367. ‌SBuf *lj_strfmt_putfnum(SBuf *sb, SFormat sf, lua_Number n)

368. {

369. TValue tv;

370. tv.n = n;

371. if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) {

372. /* Canonicalize output of non-finite values. */

373. MSize width = STRFMT_WIDTH(sf), len = 3;

374. int prefix = 0, ch = (sf & STRFMT_F_UPPER) ? 0x202020 : 0;

375. char *p;

376. if (((tv.u32.hi & 0x000fffff) | tv.u32.lo) != 0) {

377. ch ^= ('n' << 16) | ('a' << 8) | 'n';

378. if ((sf & STRFMT_F_SPACE)) prefix = ' ';

379. } else {

380. ch ^= ('i' << 16) | ('n' << 8) | 'f';

381. if ((tv.u32.hi & 0x80000000)) prefix = '-';

382. else if ((sf & STRFMT_F_PLUS)) prefix = '+';

383. else if ((sf & STRFMT_F_SPACE)) prefix = ' ';

384. }

385. if (prefix) len = 4;

386. p = lj_buf_more(sb, width > len ? width : len);

387. if (!(sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' ';

388. if (prefix) *p++ = prefix;

389. *p++ = (char)(ch >> 16); *p++ = (char)(ch >> 8); *p++ = (char)ch;

390. if ((sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' ';

391. setsbufP(sb, p);

392. } else {  /* Delegate to sprintf() for now. */

393. uint8_t width = (uint8_t)STRFMT_WIDTH(sf), prec = (uint8_t)STRFMT_PREC(sf);

394. char fmt[1+5+2+3+1+1], *p = fmt;

395. *p++ = '%';

396. if ((sf & STRFMT_F_LEFT)) *p++ = '-';

397. if ((sf & STRFMT_F_PLUS)) *p++ = '+';

398. if ((sf & STRFMT_F_ZERO)) *p++ = '0';

399. if ((sf & STRFMT_F_SPACE)) *p++ = ' ';

400. if ((sf & STRFMT_F_ALT)) *p++ = '#';

401. if (width) {

402. uint8_t x = width / 10, y = width % 10;

403. if (x) *p++ = '0' + x;

404. *p++ = '0' + y;

405. }

406. if (prec != 255) {

407. uint8_t x = prec / 10, y = prec % 10;

408. *p++ = '.';

409. if (x) *p++ = '0' + x;

410. *p++ = '0' + y;

411. }

412. *p++ = (0x67666561 >> (STRFMT_FP(sf)<<3)) ^ ((sf & STRFMT_F_UPPER)?0x20:0);

413. *p = '\0';

414. p = lj_buf_more(sb, STRFMT_FMTNUMBUF);

415. setsbufP(sb, p + sprintf(p, fmt, n));

416. }

417. return sb;

418. }

420. /* -- Conversions to strings ---------------------------------------------- */

422. /* Convert integer to string. */

423. ‌GCstr * LJ_FASTCALL lj_strfmt_int(lua_State *L, int32_t k)

424. {

425. char buf[STRFMT_MAXBUF_INT];

426. MSize len = (MSize)(lj_strfmt_wint(buf, k) - buf);

427. return lj_str_new(L, buf, len);

428. }

430. /* Convert number to string. */

431. ‌GCstr * LJ_FASTCALL lj_strfmt_num(lua_State *L, cTValue *o)

432. {

433. char buf[STRFMT_MAXBUF_NUM];

434. MSize len = (MSize)(lj_strfmt_wnum(buf, o) - buf);

435. return lj_str_new(L, buf, len);

436. }

438. /* Convert integer or number to string. */

439. ‌GCstr * LJ_FASTCALL lj_strfmt_number(lua_State *L, cTValue *o)

440. {

441. return tvisint(o) ? lj_strfmt_int(L, intV(o)) : lj_strfmt_num(L, o);

442. }

444. #if LJ_HASJIT

445. /* Convert char value to string. */

446. ‌GCstr * LJ_FASTCALL lj_strfmt_char(lua_State *L, int c)

447. {

448. char buf[1];

449. buf[0] = c;

450. return lj_str_new(L, buf, 1);

451. }

452. #endif

454. /* Raw conversion of object to string. */

455. ‌GCstr * LJ_FASTCALL lj_strfmt_obj(lua_State *L, cTValue *o)

456. {

457. if (tvisstr(o)) {

458. return strV(o);

459. } else if (tvisnumber(o)) {

460. return lj_strfmt_number(L, o);

461. } else if (tvisnil(o)) {

462. return lj_str_newlit(L, "nil");

463. } else if (tvisfalse(o)) {

464. return lj_str_newlit(L, "false");

465. } else if (tvistrue(o)) {

466. return lj_str_newlit(L, "true");

467. } else {

468. char buf[8+2+2+16], *p = buf;

469. p = lj_buf_wmem(p, lj_typename(o), (MSize)strlen(lj_typename(o)));

470. *p++ = ':'; *p++ = ' ';

471. if (tvisfunc(o) && isffunc(funcV(o))) {

472. p = lj_buf_wmem(p, "builtin#", 8);

473. p = lj_strfmt_wint(p, funcV(o)->c.ffid);

474. } else {

475. p = lj_strfmt_wptr(p, lj_obj_ptr(o));

476. }

477. return lj_str_new(L, buf, (size_t)(p - buf));

478. }

479. }

481. /* -- Internal string formatting ------------------------------------------ */

483. /*

484. ** These functions are only used for lua_pushfstring(), lua_pushvfstring()

485. ** and for internal string formatting (e.g. error messages). Caveat: unlike

486. ** string.format(), only a limited subset of formats and flags are supported!

487. **

488. ** LuaJIT has support for a couple more formats than Lua 5.1/5.2:

489. ** - %d %u %o %x with full formatting, 32 bit integers only.

490. ** - %f and other FP formats are really %.14g.

491. ** - %s %c %p without formatting.

492. */

494. /* Push formatted message as a string object to Lua stack. va_list variant. */

495. ‌const char *lj_strfmt_pushvf(lua_State *L, const char *fmt, va_list argp)

496. {

497. SBuf *sb = lj_buf_tmp_(L);

498. FormatState fs;

499. SFormat sf;

500. GCstr *str;

501. lj_strfmt_init(&fs, fmt, (MSize)strlen(fmt));

502. while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) {

503. switch (STRFMT_TYPE(sf)) {

504. case STRFMT_LIT:

505. lj_buf_putmem(sb, fs.str, fs.len);

506. break;

507. case STRFMT_INT:

508. lj_strfmt_putfxint(sb, sf, va_arg(argp, int32_t));

509. break;

510. case STRFMT_UINT:

511. lj_strfmt_putfxint(sb, sf, va_arg(argp, uint32_t));

512. break;

513. case STRFMT_NUM: {

514. TValue tv;

515. tv.n = va_arg(argp, lua_Number);

516. setsbufP(sb, lj_strfmt_wnum(lj_buf_more(sb, STRFMT_MAXBUF_NUM), &tv));

517. break;

518. }

519. case STRFMT_STR: {

520. const char *s = va_arg(argp, char *);

521. if (s == NULL) s = "(null)";

522. lj_buf_putmem(sb, s, (MSize)strlen(s));

523. break;

524. }

525. case STRFMT_CHAR:

526. lj_buf_putb(sb, va_arg(argp, int));

527. break;

528. case STRFMT_PTR:

529. lj_strfmt_putptr(sb, va_arg(argp, void *));

530. break;

531. case STRFMT_ERR:

532. default:

533. lj_buf_putb(sb, '?');

534. lua_assert(0);

535. break;

536. }

537. }

538. str = lj_buf_str(L, sb);

539. setstrV(L, L->top, str);

540. incr_top(L);

541. return strdata(str);

542. }

544. /* Push formatted message as a string object to Lua stack. Vararg variant. */

545. ‌const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...)

546. {

547. const char *msg;

548. va_list argp;

549. va_start(argp, fmt);

550. msg = lj_strfmt_pushvf(L, fmt, argp);

551. va_end(argp);

552. return msg;

553. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_strfmt.h - luajit-2.0-src

Data types defined

- FormatState

- FormatState

- FormatType

- FormatType

- SFormat

Functions defined

- lj_strfmt_init

Macros defined

- STRFMT_A

- STRFMT_C

- STRFMT_D

- STRFMT_E

- STRFMT_F

- STRFMT_FP

- STRFMT_F_ALT

- STRFMT_F_LEFT

- STRFMT_F_PLUS

- STRFMT_F_SPACE

- STRFMT_F_UPPER

- STRFMT_F_ZERO

- STRFMT_G

- STRFMT_I

- STRFMT_MAXBUF_INT

- STRFMT_MAXBUF_NUM

- STRFMT_MAXBUF_PTR

- STRFMT_MAXBUF_XINT

- STRFMT_O

- STRFMT_P

- STRFMT_PREC

- STRFMT_Q

- STRFMT_S

- STRFMT_SH_PREC

- STRFMT_SH_WIDTH

- STRFMT_TYPE

- STRFMT_T_FP_A

- STRFMT_T_FP_E

- STRFMT_T_FP_F

- STRFMT_T_FP_G

- STRFMT_T_HEX

- STRFMT_T_OCT

- STRFMT_T_QUOTED

- STRFMT_U

- STRFMT_WIDTH

- STRFMT_X

- _LJ_STRFMT_H

Source code

1. /*

2. ** String formatting.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_STRFMT_H

7. ‌#define _LJ_STRFMT_H

9. #include "lj_obj.h"

11. ‌typedef uint32_t SFormat;  /* Format indicator. */

13. /* Format parser state. */

14. ‌typedef struct FormatState {

15. const uint8_t *p;        /* Current format string pointer. */

16. const uint8_t *e;        /* End of format string. */

17. const char *str;        /* Returned literal string. */

18. MSize len;                /* Size of literal string. */

19. ‌} FormatState;

21. /* Format types (max. 16). */

22. ‌typedef enum FormatType {

23. STRFMT_EOF, STRFMT_ERR, STRFMT_LIT,

24. STRFMT_INT, STRFMT_UINT, STRFMT_NUM, STRFMT_STR, STRFMT_CHAR, STRFMT_PTR

25. ‌} FormatType;

27. /* Format subtypes (bits are reused). */

28. ‌#define STRFMT_T_HEX        0x0010        /* STRFMT_UINT */

29. ‌#define STRFMT_T_OCT        0x0020        /* STRFMT_UINT */

30. ‌#define STRFMT_T_FP_A        0x0000        /* STRFMT_NUM */

31. ‌#define STRFMT_T_FP_E        0x0010        /* STRFMT_NUM */

32. ‌#define STRFMT_T_FP_F        0x0020        /* STRFMT_NUM */

33. ‌#define STRFMT_T_FP_G        0x0030        /* STRFMT_NUM */

34. ‌#define STRFMT_T_QUOTED        0x0010        /* STRFMT_STR */

36. /* Format flags. */

37. ‌#define STRFMT_F_LEFT        0x0100

38. ‌#define STRFMT_F_PLUS        0x0200

39. ‌#define STRFMT_F_ZERO        0x0400

40. ‌#define STRFMT_F_SPACE        0x0800

41. ‌#define STRFMT_F_ALT        0x1000

42. ‌#define STRFMT_F_UPPER        0x2000

44. /* Format indicator fields. */

45. ‌#define STRFMT_SH_WIDTH        16

46. ‌#define STRFMT_SH_PREC        24

48. ‌#define STRFMT_TYPE(sf)                ((FormatType)((sf) & 15))

49. ‌#define STRFMT_WIDTH(sf)        (((sf) >> STRFMT_SH_WIDTH) & 255u)

50. ‌#define STRFMT_PREC(sf)                ((((sf) >> STRFMT_SH_PREC) & 255u) - 1u)

51. ‌#define STRFMT_FP(sf)                (((sf) >> 4) & 3)

53. /* Formats for conversion characters. */

54. ‌#define STRFMT_A        (STRFMT_NUM|STRFMT_T_FP_A)

55. ‌#define STRFMT_C        (STRFMT_CHAR)

56. ‌#define STRFMT_D        (STRFMT_INT)

57. ‌#define STRFMT_E        (STRFMT_NUM|STRFMT_T_FP_E)

58. ‌#define STRFMT_F        (STRFMT_NUM|STRFMT_T_FP_F)

59. ‌#define STRFMT_G        (STRFMT_NUM|STRFMT_T_FP_G)

60. ‌#define STRFMT_I        STRFMT_D

61. ‌#define STRFMT_O        (STRFMT_UINT|STRFMT_T_OCT)

62. ‌#define STRFMT_P        (STRFMT_PTR)

63. ‌#define STRFMT_Q        (STRFMT_STR|STRFMT_T_QUOTED)

64. ‌#define STRFMT_S        (STRFMT_STR)

65. ‌#define STRFMT_U        (STRFMT_UINT)

66. ‌#define STRFMT_X        (STRFMT_UINT|STRFMT_T_HEX)

68. /* Maximum buffer sizes for conversions. */

69. ‌#define STRFMT_MAXBUF_XINT        (1+22)  /* '0' prefix + uint64_t in octal. */

70. ‌#define STRFMT_MAXBUF_INT        (1+10)  /* Sign + int32_t in decimal. */

71. ‌#define STRFMT_MAXBUF_NUM        LUAI_MAXNUMBER2STR

72. ‌#define STRFMT_MAXBUF_PTR        (2+2*sizeof(ptrdiff_t))  /* "0x" + hex ptr. */

74. /* Format parser. */

75. LJ_FUNC SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs);

77. ‌static LJ_AINLINE void lj_strfmt_init(FormatState *fs, const char *p, MSize len)

78. {

79. fs->p = (const uint8_t *)p;

80. fs->e = (const uint8_t *)p + len;

81. lua_assert(*fs->e == 0);  /* Must be NUL-terminated (may have NULs inside). */

82. }

84. /* Raw conversions. */

85. LJ_FUNC char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k);

86. LJ_FUNC char * LJ_FASTCALL lj_strfmt_wnum(char *p, cTValue *o);

87. LJ_FUNC char * LJ_FASTCALL lj_strfmt_wptr(char *p, const void *v);

88. LJ_FUNC char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v);

89. LJ_FUNC const char *lj_strfmt_wstrnum(char *buf, cTValue *o, MSize *lenp);

91. /* Unformatted conversions to buffer. */

92. LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k);

93. #if LJ_HASJIT

94. LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o);

95. #endif

96. LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putptr(SBuf *sb, const void *v);

97. LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str);

99. /* Formatted conversions to buffer. */

100. LJ_FUNC SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k);

101. LJ_FUNC SBuf *lj_strfmt_putfnum_int(SBuf *sb, SFormat sf, lua_Number n);

102. LJ_FUNC SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n);

103. LJ_FUNC SBuf *lj_strfmt_putfnum(SBuf *sb, SFormat, lua_Number n);

104. LJ_FUNC SBuf *lj_strfmt_putfchar(SBuf *sb, SFormat, int32_t c);

105. LJ_FUNC SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat, GCstr *str);

107. /* Conversions to strings. */

108. LJ_FUNC GCstr * LJ_FASTCALL lj_strfmt_int(lua_State *L, int32_t k);

109. LJ_FUNCA GCstr * LJ_FASTCALL lj_strfmt_num(lua_State *L, cTValue *o);

110. LJ_FUNCA GCstr * LJ_FASTCALL lj_strfmt_number(lua_State *L, cTValue *o);

111. #if LJ_HASJIT

112. LJ_FUNC GCstr * LJ_FASTCALL lj_strfmt_char(lua_State *L, int c);

113. #endif

114. LJ_FUNC GCstr * LJ_FASTCALL lj_strfmt_obj(lua_State *L, cTValue *o);

116. /* Internal string formatting. */

117. LJ_FUNC const char *lj_strfmt_pushvf(lua_State *L, const char *fmt,

118. va_list argp);

119. LJ_FUNC const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...)

120. #ifdef __GNUC__

121. __attribute__ ((format (printf, 2, 3)))

122. #endif

123. ;

125. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_str.h - luajit-2.0-src

Macros defined

- _LJ_STR_H

- lj_str_newlit

- lj_str_newz

Source code

1. /*

2. ** String handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_STR_H

7. ‌#define _LJ_STR_H

9. #include <stdarg.h>

11. #include "lj_obj.h"

13. /* String helpers. */

14. LJ_FUNC int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b);

15. LJ_FUNC const char *lj_str_find(const char *s, const char *f,

16. MSize slen, MSize flen);

17. LJ_FUNC int lj_str_haspattern(GCstr *s);

19. /* String interning. */

20. LJ_FUNC void lj_str_resize(lua_State *L, MSize newmask);

21. LJ_FUNCA GCstr *lj_str_new(lua_State *L, const char *str, size_t len);

22. LJ_FUNC void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s);

24. ‌#define lj_str_newz(L, s)        (lj_str_new(L, s, strlen(s)))

25. ‌#define lj_str_newlit(L, s)        (lj_str_new(L, "" s, sizeof(s)-1))

27. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/host/buildvm_lib.c - luajit-2.0-src

Global variables defined

- ffasmfunc

- ffid

- funcname

- libdef_handlers

- modname

- modnamelen

- modstate

- obuf

- optr

- recffid

- regfunc

Data types defined

- LibDefFunc

- LibDefHandler

- LibDefHandler

Functions defined

- emit_lib

- find_ffofs

- find_rec

- libdef_endmodule

- libdef_fixupbc

- libdef_func

- libdef_lua

- libdef_module

- libdef_name

- libdef_push

- libdef_rec

- libdef_regfunc

- libdef_set

- libdef_uleb128

- memcpy_endian

Source code

1. /*

2. ** LuaJIT VM builder: library definition compiler.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "buildvm.h"

7. #include "lj_obj.h"

8. #include "lj_bc.h"

9. #include "lj_lib.h"

10. #include "buildvm_libbc.h"

12. /* Context for library definitions. */

13. ‌static uint8_t obuf[8192];

14. ‌static uint8_t *optr;

15. ‌static char modname[80];

16. ‌static size_t modnamelen;

17. ‌static char funcname[80];

18. ‌‌static int modstate, regfunc;

19. ‌‌‌static int ffid, recffid, ffasmfunc;

21. enum {

22. REGFUNC_OK,

23. REGFUNC_NOREG,

24. REGFUNC_NOREGUV

25. };

27. ‌static void libdef_name(const char *p, int kind)

28. {

29. size_t n = strlen(p);

30. if (kind != LIBINIT_STRING) {

31. if (n > modnamelen && p[modnamelen] == '_' &&

32. !strncmp(p, modname, modnamelen)) {

33. p += modnamelen+1;

34. n -= modnamelen+1;

35. }

36. }

37. if (n > LIBINIT_MAXSTR) {

38. fprintf(stderr, "Error: string too long: '%s'\n",  p);

39. exit(1);

40. }

41. if (optr+1+n+2 > obuf+sizeof(obuf)) {  /* +2 for caller. */

42. fprintf(stderr, "Error: output buffer overflow\n");

43. exit(1);

44. }

45. *optr++ = (uint8_t)(n | kind);

46. memcpy(optr, p, n);

47. optr += n;

48. }

50. ‌static void libdef_endmodule(BuildCtx *ctx)

51. {

52. if (modstate != 0) {

53. char line[80];

54. const uint8_t *p;

55. int n;

56. if (modstate == 1)

57. fprintf(ctx->fp, "  (lua_CFunction)0");

58. fprintf(ctx->fp, "\n};\n");

59. fprintf(ctx->fp, "static const uint8_t %s%s[] = {\n",

60. LABEL_PREFIX_LIBINIT, modname);

61. line[0] = '\0';

62. for (n = 0, p = obuf; p < optr; p++) {

63. n += sprintf(line+n, "%d,", *p);

64. if (n >= 75) {

65. fprintf(ctx->fp, "%s\n", line);

66. n = 0;

67. line[0] = '\0';

68. }

69. }

70. fprintf(ctx->fp, "%s%d\n};\n#endif\n\n", line, LIBINIT_END);

71. }

72. }

74. ‌static void libdef_module(BuildCtx *ctx, char *p, int arg)

75. {

76. UNUSED(arg);

77. if (ctx->mode == BUILD_libdef) {

78. libdef_endmodule(ctx);

79. optr = obuf;

80. *optr++ = (uint8_t)ffid;

81. *optr++ = (uint8_t)ffasmfunc;

82. *optr++ = 0;  /* Hash table size. */

83. modstate = 1;

84. fprintf(ctx->fp, "#ifdef %sMODULE_%s\n", LIBDEF_PREFIX, p);

85. fprintf(ctx->fp, "#undef %sMODULE_%s\n", LIBDEF_PREFIX, p);

86. fprintf(ctx->fp, "static const lua_CFunction %s%s[] = {\n",

87. LABEL_PREFIX_LIBCF, p);

88. }

89. modnamelen = strlen(p);

90. if (modnamelen > sizeof(modname)-1) {

91. fprintf(stderr, "Error: module name too long: '%s'\n", p);

92. exit(1);

93. }

94. strcpy(modname, p);

95. }

97. ‌static int find_ffofs(BuildCtx *ctx, const char *name)

98. {

99. int i;

100. for (i = 0; i < ctx->nglob; i++) {

101. const char *gl = ctx->globnames[i];

102. if (gl[0] == 'f' && gl[1] == 'f' && gl[2] == '_' && !strcmp(gl+3, name)) {

103. return (int)((uint8_t *)ctx->glob[i] - ctx->code);

104. }

105. }

106. fprintf(stderr, "Error: undefined fast function %s%s\n",

107. LABEL_PREFIX_FF, name);

108. exit(1);

109. }

111. ‌static void libdef_func(BuildCtx *ctx, char *p, int arg)

112. {

113. if (arg != LIBINIT_CF)

114. ffasmfunc++;

115. if (ctx->mode == BUILD_libdef) {

116. if (modstate == 0) {

117. fprintf(stderr, "Error: no module for function definition %s\n", p);

118. exit(1);

119. }

120. if (regfunc == REGFUNC_NOREG) {

121. if (optr+1 > obuf+sizeof(obuf)) {

122. fprintf(stderr, "Error: output buffer overflow\n");

123. exit(1);

124. }

125. *optr++ = LIBINIT_FFID;

126. } else {

127. if (arg != LIBINIT_ASM_) {

128. if (modstate != 1) fprintf(ctx->fp, ",\n");

129. modstate = 2;

130. fprintf(ctx->fp, "  %s%s", arg ? LABEL_PREFIX_FFH : LABEL_PREFIX_CF, p);

131. }

132. if (regfunc != REGFUNC_NOREGUV) obuf[2]++;  /* Bump hash table size. */

133. libdef_name(regfunc == REGFUNC_NOREGUV ? "" : p, arg);

134. }

135. } else if (ctx->mode == BUILD_ffdef) {

136. fprintf(ctx->fp, "FFDEF(%s)\n", p);

137. } else if (ctx->mode == BUILD_recdef) {

138. if (strlen(p) > sizeof(funcname)-1) {

139. fprintf(stderr, "Error: function name too long: '%s'\n", p);

140. exit(1);

141. }

142. strcpy(funcname, p);

143. } else if (ctx->mode == BUILD_vmdef) {

144. int i;

145. for (i = 1; p[i] && modname[i-1]; i++)

146. if (p[i] == '_') p[i] = '.';

147. fprintf(ctx->fp, "\"%s\",\n", p);

148. } else if (ctx->mode == BUILD_bcdef) {

149. if (arg != LIBINIT_CF)

150. fprintf(ctx->fp, ",\n%d", find_ffofs(ctx, p));

151. }

152. ffid++;

153. regfunc = REGFUNC_OK;

154. }

156. ‌static uint8_t *libdef_uleb128(uint8_t *p, uint32_t *vv)

157. {

158. uint32_t v = *p++;

159. if (v >= 0x80) {

160. int sh = 0; v &= 0x7f;

161. do { v |= ((*p & 0x7f) << (sh += 7)); } while (*p++ >= 0x80);

162. }

163. *vv = v;

164. return p;

165. }

167. ‌static void libdef_fixupbc(uint8_t *p)

168. {

169. uint32_t i, sizebc;

170. p += 4;

171. p = libdef_uleb128(p, &sizebc);

172. p = libdef_uleb128(p, &sizebc);

173. p = libdef_uleb128(p, &sizebc);

174. for (i = 0; i < sizebc; i++, p += 4) {

175. uint8_t op = p[libbc_endian ? 3 : 0];

176. uint8_t ra = p[libbc_endian ? 2 : 1];

177. uint8_t rc = p[libbc_endian ? 1 : 2];

178. uint8_t rb = p[libbc_endian ? 0 : 3];

179. if (!LJ_DUALNUM && op == BC_ISTYPE && rc == ~LJ_TNUMX+1) {

180. op = BC_ISNUM; rc++;

181. }

182. p[LJ_ENDIAN_SELECT(0, 3)] = op;

183. p[LJ_ENDIAN_SELECT(1, 2)] = ra;

184. p[LJ_ENDIAN_SELECT(2, 1)] = rc;

185. p[LJ_ENDIAN_SELECT(3, 0)] = rb;

186. }

187. }

189. ‌static void libdef_lua(BuildCtx *ctx, char *p, int arg)

190. {

191. UNUSED(arg);

192. if (ctx->mode == BUILD_libdef) {

193. int i;

194. for (i = 0; libbc_map[i].name != NULL; i++) {

195. if (!strcmp(libbc_map[i].name, p)) {

196. int ofs = libbc_map[i].ofs;

197. int len = libbc_map[i+1].ofs - ofs;

198. obuf[2]++;  /* Bump hash table size. */

199. *optr++ = LIBINIT_LUA;

200. libdef_name(p, 0);

201. memcpy(optr, libbc_code + ofs, len);

202. libdef_fixupbc(optr);

203. optr += len;

204. return;

205. }

206. }

207. fprintf(stderr, "Error: missing libbc definition for %s\n", p);

208. exit(1);

209. }

210. }

212. ‌static uint32_t find_rec(char *name)

213. {

214. char *p = (char *)obuf;

215. uint32_t n;

216. for (n = 2; *p; n++) {

217. if (strcmp(p, name) == 0)

218. return n;

219. p += strlen(p)+1;

220. }

221. if (p+strlen(name)+1 >= (char *)obuf+sizeof(obuf)) {

222. fprintf(stderr, "Error: output buffer overflow\n");

223. exit(1);

224. }

225. strcpy(p, name);

226. return n;

227. }

229. ‌static void libdef_rec(BuildCtx *ctx, char *p, int arg)

230. {

231. UNUSED(arg);

232. if (ctx->mode == BUILD_recdef) {

233. char *q;

234. uint32_t n;

235. for (; recffid+1 < ffid; recffid++)

236. fprintf(ctx->fp, ",\n0");

237. recffid = ffid;

238. if (*p == '.') p = funcname;

239. q = strchr(p, ' ');

240. if (q) *q++ = '\0';

241. n = find_rec(p);

242. if (q)

243. fprintf(ctx->fp, ",\n0x%02x00+(%s)", n, q);

244. else

245. fprintf(ctx->fp, ",\n0x%02x00", n);

246. }

247. }

249. ‌static void memcpy_endian(void *dst, void *src, size_t n)

250. {

251. union { uint8_t b; uint32_t u; } host_endian;

252. host_endian.u = 1;

253. if (host_endian.b == LJ_ENDIAN_SELECT(1, 0)) {

254. memcpy(dst, src, n);

255. } else {

256. size_t i;

257. for (i = 0; i < n; i++)

258. ((uint8_t *)dst)[i] = ((uint8_t *)src)[n-i-1];

259. }

260. }

262. ‌static void libdef_push(BuildCtx *ctx, char *p, int arg)

263. {

264. UNUSED(arg);

265. if (ctx->mode == BUILD_libdef) {

266. int len = (int)strlen(p);

267. if (*p == '"') {

268. if (len > 1 && p[len-1] == '"') {

269. p[len-1] = '\0';

270. libdef_name(p+1, LIBINIT_STRING);

271. return;

272. }

273. } else if (*p >= '0' && *p <= '9') {

274. char *ep;

275. double d = strtod(p, &ep);

276. if (*ep == '\0') {

277. if (optr+1+sizeof(double) > obuf+sizeof(obuf)) {

278. fprintf(stderr, "Error: output buffer overflow\n");

279. exit(1);

280. }

281. *optr++ = LIBINIT_NUMBER;

282. memcpy_endian(optr, &d, sizeof(double));

283. optr += sizeof(double);

284. return;

285. }

286. } else if (!strcmp(p, "lastcl")) {

287. if (optr+1 > obuf+sizeof(obuf)) {

288. fprintf(stderr, "Error: output buffer overflow\n");

289. exit(1);

290. }

291. *optr++ = LIBINIT_LASTCL;

292. return;

293. } else if (len > 4 && !strncmp(p, "top-", 4)) {

294. if (optr+2 > obuf+sizeof(obuf)) {

295. fprintf(stderr, "Error: output buffer overflow\n");

296. exit(1);

297. }

298. *optr++ = LIBINIT_COPY;

299. *optr++ = (uint8_t)atoi(p+4);

300. return;

301. }

302. fprintf(stderr, "Error: bad value for %sPUSH(%s)\n", LIBDEF_PREFIX, p);

303. exit(1);

304. }

305. }

307. ‌static void libdef_set(BuildCtx *ctx, char *p, int arg)

308. {

309. UNUSED(arg);

310. if (ctx->mode == BUILD_libdef) {

311. if (p[0] == '!' && p[1] == '\0') p[0] = '\0';  /* Set env. */

312. libdef_name(p, LIBINIT_STRING);

313. *optr++ = LIBINIT_SET;

314. obuf[2]++;  /* Bump hash table size. */

315. }

316. }

318. ‌static void libdef_regfunc(BuildCtx *ctx, char *p, int arg)

319. {

320. UNUSED(ctx); UNUSED(p);

321. regfunc = arg;

322. }

324. ‌typedef void (*LibDefFunc)(BuildCtx *ctx, char *p, int arg);

326. ‌typedef struct LibDefHandler {

327. const char *suffix;

328. const char *stop;

329. const LibDefFunc func;

330. const int arg;

331. ‌} LibDefHandler;

333. ‌static const LibDefHandler libdef_handlers[] = {

334. { "MODULE_",        " \t\r\n",        libdef_module,                0 },

335. { "CF(",        ")",                libdef_func,                LIBINIT_CF },

336. { "ASM(",        ")",                libdef_func,                LIBINIT_ASM },

337. { "ASM_(",        ")",                libdef_func,                LIBINIT_ASM_ },

338. { "LUA(",        ")",                libdef_lua,                0 },

339. { "REC(",        ")",                libdef_rec,                0 },

340. { "PUSH(",        ")",                libdef_push,                0 },

341. { "SET(",        ")",                libdef_set,                0 },

342. { "NOREGUV",        NULL,                libdef_regfunc,                REGFUNC_NOREGUV },

343. { "NOREG",        NULL,                libdef_regfunc,                REGFUNC_NOREG },

344. { NULL,        NULL,                (LibDefFunc)0,                0 }

345. };

347. /* Emit C source code for library function definitions. */

348. ‌void emit_lib(BuildCtx *ctx)

349. {

350. const char *fname;

352. if (ctx->mode == BUILD_ffdef || ctx->mode == BUILD_libdef ||

353. ctx->mode == BUILD_recdef)

354. fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n");

355. else if (ctx->mode == BUILD_vmdef)

356. fprintf(ctx->fp, "ffnames = {\n[0]=\"Lua\",\n\"C\",\n");

357. if (ctx->mode == BUILD_recdef)

358. fprintf(ctx->fp, "static const uint16_t recff_idmap[] = {\n0,\n0x0100");

359. recffid = ffid = FF_C+1;

360. ffasmfunc = 0;

362. while ((fname = *ctx->args++)) {

363. char buf[256];  /* We don't care about analyzing lines longer than that. */

364. FILE *fp;

365. if (fname[0] == '-' && fname[1] == '\0') {

366. fp = stdin;

367. } else {

368. fp = fopen(fname, "r");

369. if (!fp) {

370. fprintf(stderr, "Error: cannot open input file '%s': %s\n",

371. fname, strerror(errno));

372. exit(1);

373. }

374. }

375. modstate = 0;

376. regfunc = REGFUNC_OK;

377. while (fgets(buf, sizeof(buf), fp) != NULL) {

378. char *p;

379. /* Simplistic pre-processor. Only handles top-level #if/#endif. */

380. if (buf[0] == '#' && buf[1] == 'i' && buf[2] == 'f') {

381. int ok = 1;

382. if (!strcmp(buf, "#if LJ_52\n"))

383. ok = LJ_52;

384. else if (!strcmp(buf, "#if LJ_HASJIT\n"))

385. ok = LJ_HASJIT;

386. else if (!strcmp(buf, "#if LJ_HASFFI\n"))

387. ok = LJ_HASFFI;

388. if (!ok) {

389. int lvl = 1;

390. while (fgets(buf, sizeof(buf), fp) != NULL) {

391. if (buf[0] == '#' && buf[1] == 'e' && buf[2] == 'n') {

392. if (--lvl == 0) break;

393. } else if (buf[0] == '#' && buf[1] == 'i' && buf[2] == 'f') {

394. lvl++;

395. }

396. }

397. continue;

398. }

399. }

400. for (p = buf; (p = strstr(p, LIBDEF_PREFIX)) != NULL; ) {

401. const LibDefHandler *ldh;

402. p += sizeof(LIBDEF_PREFIX)-1;

403. for (ldh = libdef_handlers; ldh->suffix != NULL; ldh++) {

404. size_t n, len = strlen(ldh->suffix);

405. if (!strncmp(p, ldh->suffix, len)) {

406. p += len;

407. n = ldh->stop ? strcspn(p, ldh->stop) : 0;

408. if (!p[n]) break;

409. p[n] = '\0';

410. ldh->func(ctx, p, ldh->arg);

411. p += n+1;

412. break;

413. }

414. }

415. if (ldh->suffix == NULL) {

416. buf[strlen(buf)-1] = '\0';

417. fprintf(stderr, "Error: unknown library definition tag %s%s\n",

418. LIBDEF_PREFIX, p);

419. exit(1);

420. }

421. }

422. }

423. fclose(fp);

424. if (ctx->mode == BUILD_libdef) {

425. libdef_endmodule(ctx);

426. }

427. }

429. if (ctx->mode == BUILD_ffdef) {

430. fprintf(ctx->fp, "\n#undef FFDEF\n\n");

431. fprintf(ctx->fp,

432. "#ifndef FF_NUM_ASMFUNC\n#define FF_NUM_ASMFUNC %d\n#endif\n\n",

433. ffasmfunc);

434. } else if (ctx->mode == BUILD_vmdef) {

435. fprintf(ctx->fp, "},\n\n");

436. } else if (ctx->mode == BUILD_bcdef) {

437. int i;

438. fprintf(ctx->fp, "\n};\n\n");

439. fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_mode[] = {\n");

440. fprintf(ctx->fp, "BCDEF(BCMODE)\n");

441. for (i = ffasmfunc-1; i > 0; i--)

442. fprintf(ctx->fp, "BCMODE_FF,\n");

443. fprintf(ctx->fp, "BCMODE_FF\n};\n\n");

444. } else if (ctx->mode == BUILD_recdef) {

445. char *p = (char *)obuf;

446. fprintf(ctx->fp, "\n};\n\n");

447. fprintf(ctx->fp, "static const RecordFunc recff_func[] = {\n"

448. "recff_nyi,\n"

449. "recff_c");

450. while (*p) {

451. fprintf(ctx->fp, ",\nrecff_%s", p);

452. p += strlen(p)+1;

453. }

454. fprintf(ctx->fp, "\n};\n\n");

455. }

456. }

One Level Up                               Top Level

One Level Up                               Top Level

src/host/buildvm_arch.h - luajit-2.0-src

Global variables defined

- build_actionlist

- extnames

- globnames

Functions defined

- build_backend

- build_ins

- build_subroutines

- emit_asm_debug

Macros defined

- BSZPTR

- DASM_MAXSECTION

- DASM_SECTION_CODE_OP

- DASM_SECTION_CODE_SUB

- DISPATCH_GL

- DISPATCH_J

- Dt1

- Dt10

- Dt2

- Dt3

- Dt4

- Dt5

- Dt6

- Dt7

- Dt8

- Dt9

- DtA

- DtB

- DtC

- DtD

- DtE

- DtF

- PC2PROTO

- REG_RA

- REG_SP

- SZPTR

- TV2MARKOFS

- TV2MARKOFS

Source code

1. /*

2. ** This file has been pre-processed with DynASM.

3. ** http://luajit.org/dynasm.html

4. ** DynASM version 1.3.0, DynASM x64 version 1.3.0

5. ** DO NOT EDIT! The original file is in "vm_x86.dasc".

6. */

8. #line 1 "vm_x86.dasc"

9. //|// Low-level VM code for x86 CPUs.

10. //|// Bytecode interpreter, fast functions and helper functions.

11. //|// Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

12. //|

13. //|.if P64

14. //|.arch x64

15. #if DASM_VERSION != 10300

16. #error "Version mismatch between DynASM and included encoding engine"

17. #endif

18. #line 7 "vm_x86.dasc"

19. //|.else

20. //|.arch x86

21. //|.endif

22. //|.section code_op, code_sub

23. ‌#define DASM_SECTION_CODE_OP        0

24. ‌#define DASM_SECTION_CODE_SUB        1

25. ‌#define DASM_MAXSECTION                2

26. #line 11 "vm_x86.dasc"

27. //|

28. //|.actionlist build_actionlist

29. ‌static const unsigned char build_actionlist[14864] = {

30. 254,1,248,10,252,247,195,237,15,132,244,11,131,227,252,248,41,218,72,141,

31. 76,25,252,248,139,90,252,252,199,68,10,4,237,248,12,131,192,1,15,132,244,

32. 13,137,68,36,4,252,247,195,237,15,132,244,14,248,15,129,252,243,239,252,247,

33. 195,237,15,133,244,10,65,199,134,233,237,131,227,252,248,41,211,252,247,219,

34. 131,232,1,15,132,244,248,248,1,255,72,139,44,10,72,137,106,252,248,131,194,

35. 8,131,232,1,15,133,244,1,248,2,139,108,36,24,137,157,233,248,3,139,68,36,

36. 4,139,76,36,16,248,4,57,193,15,133,244,252,248,5,131,252,234,8,137,149,233,

37. 248,16,72,139,76,36,32,72,137,141,233,49,192,248,17,72,131,196,40,65,94,65,

38. 95,91,93,195,248,6,15,130,244,253,59,149,233,15,135,244,254,199,66,252,252,

39. 237,255,131,194,8,131,192,1,252,233,244,4,248,7,133,201,15,132,244,5,41,193,

40. 141,20,202,252,233,244,5,248,8,137,149,233,137,68,36,4,137,206,137,252,239,

41. 232,251,1,0,139,149,233,252,233,244,3,248,13,176,235,252,233,244,18,248,19,

42. 137,252,240,72,137,252,252,248,18,139,108,36,24,139,173,233,199,133,233,237,

43. 255,252,233,244,17,248,20,139,124,36,24,137,198,72,131,196,40,65,94,65,95,

44. 91,93,252,233,251,1,1,248,21,72,129,231,239,72,137,252,252,248,22,139,108,

45. 36,24,72,199,193,252,248,252,255,252,255,252,255,184,237,139,149,233,68,139,

46. 181,233,65,129,198,239,139,90,252,252,199,66,252,252,237,65,199,134,233,237,

47. 252,233,244,12,248,23,190,237,252,233,244,248,248,24,255,131,232,8,252,233,

48. 244,247,248,25,141,68,194,252,248,248,1,15,182,139,233,131,195,4,137,149,

49. 233,137,133,233,137,92,36,28,137,206,248,2,137,252,239,232,251,1,0,139,149,

50. 233,139,133,233,139,106,252,248,41,208,193,232,3,131,192,1,139,157,233,139,

51. 11,15,182,252,233,15,182,205,131,195,4,65,252,255,36,252,238,248,26,85,83,

52. 65,87,65,86,72,131,252,236,40,137,252,253,137,124,36,24,137,252,241,187,237,

53. 49,192,76,141,188,253,36,233,68,139,181,233,65,129,198,239,137,68,36,28,72,

54. 137,68,36,32,137,68,36,16,137,68,36,20,76,137,189,233,56,133,233,15,132,244,

55. 248,255,65,137,174,233,65,199,134,233,237,136,133,233,139,149,233,139,133,

56. 233,41,200,193,232,3,131,192,1,41,209,139,90,252,252,137,68,36,4,252,247,

57. 195,237,15,132,244,14,252,233,244,15,248,27,85,83,65,87,65,86,72,131,252,

58. 236,40,187,237,137,76,36,20,252,233,244,247,248,28,85,83,65,87,65,86,72,131,

59. 252,236,40,187,237,248,1,137,84,36,16,137,252,253,137,124,36,24,137,252,241,

60. 68,139,181,233,76,139,189,233,255,76,137,124,36,32,137,108,36,28,65,129,198,

61. 239,72,137,165,233,248,2,65,137,174,233,65,199,134,233,237,139,149,233,1,

62. 203,41,211,139,133,233,41,200,193,232,3,131,192,1,248,29,139,105,252,248,

63. 129,121,253,252,252,239,15,133,244,30,248,31,137,202,137,90,252,252,139,157,

64. 233,139,11,15,182,252,233,15,182,205,131,195,4,65,252,255,36,252,238,248,

65. 32,85,83,65,87,65,86,72,131,252,236,40,137,252,253,137,124,36,24,137,108,

66. 36,28,68,139,189,233,68,43,189,233,68,139,181,233,199,68,36,20,0,0,0,0,68,

67. 137,124,36,16,65,129,198,239,76,139,189,233,255,76,137,124,36,32,72,137,165,

68. 233,65,137,174,233,252,255,209,133,192,15,132,244,16,137,193,187,237,252,

69. 233,244,2,248,11,1,209,131,227,252,248,137,213,41,218,199,68,193,252,252,

70. 237,137,200,139,93,252,244,72,99,77,252,240,131,252,249,1,15,134,244,247,

71. 76,141,61,245,76,1,252,249,68,139,122,252,248,69,139,191,233,69,139,191,233,

72. 252,255,225,248,1,15,132,244,33,41,213,193,252,237,3,141,69,252,255,252,233,

73. 244,34,248,35,255,15,182,75,252,255,131,252,237,16,141,12,202,41,252,233,

74. 15,132,244,36,252,247,217,193,252,233,3,139,124,36,24,137,151,233,137,202,

75. 72,139,8,72,137,77,0,137,252,238,252,233,244,37,248,38,137,4,36,199,68,36,

76. 4,237,72,141,4,36,128,123,252,252,235,15,133,244,247,65,141,142,233,137,41,

77. 199,65,4,237,137,205,252,233,244,248,248,39,15,182,67,252,254,199,68,36,4,

78. 237,137,4,36,72,141,4,36,252,233,244,247,248,40,15,182,67,252,254,141,4,194,

79. 248,1,255,15,182,107,252,255,141,44,252,234,248,2,139,124,36,24,137,151,233,

80. 137,252,238,72,137,194,137,252,253,137,92,36,28,232,251,1,2,139,149,233,133,

81. 192,15,132,244,249,248,36,15,182,75,252,253,72,139,40,72,137,44,202,139,3,

82. 15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,248,3,139,

83. 141,233,137,89,252,244,141,153,233,41,211,139,105,252,248,184,237,252,233,

84. 244,31,248,41,137,252,239,137,213,137,198,232,251,1,3,15,182,75,252,253,137,

85. 252,234,133,192,15,133,244,42,199,68,202,4,237,252,233,244,43,248,44,137,

86. 4,36,199,68,36,4,237,255,72,141,4,36,128,123,252,252,235,15,133,244,247,65,

87. 141,142,233,137,41,199,65,4,237,137,205,252,233,244,248,248,45,15,182,67,

88. 252,254,199,68,36,4,237,137,4,36,72,141,4,36,252,233,244,247,248,46,15,182,

89. 67,252,254,141,4,194,248,1,15,182,107,252,255,141,44,252,234,248,2,139,124,

90. 36,24,137,151,233,137,252,238,72,137,194,137,252,253,137,92,36,28,232,251,

91. 1,4,139,149,233,133,192,15,132,244,249,15,182,75,252,253,72,139,44,202,72,

92. 137,40,248,47,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,

93. 36,252,238,248,3,255,139,141,233,137,89,252,244,15,182,67,252,253,72,139,

94. 44,194,72,137,105,16,141,153,233,41,211,139,105,252,248,184,237,252,233,244,

95. 31,248,48,139,124,36,24,137,252,238,137,151,233,137,213,137,194,137,92,36,

96. 28,232,251,1,5,15,182,75,252,253,137,252,234,252,233,244,49,248,50,139,108,

97. 36,24,137,149,233,141,52,202,141,20,194,137,252,239,15,182,75,252,252,137,

98. 92,36,28,232,251,1,6,248,3,139,149,233,131,252,248,1,15,135,244,51,248,4,

99. 141,91,4,15,130,244,252,248,5,15,183,67,252,254,141,156,253,131,233,248,6,

100. 255,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,

101. 248,52,131,195,4,129,120,253,4,239,15,130,244,5,252,233,244,6,248,53,129,

102. 120,253,4,239,252,233,244,4,248,54,131,252,235,4,137,206,137,252,233,139,

103. 108,36,24,137,149,233,137,194,137,252,239,137,92,36,28,232,251,1,7,252,233,

104. 244,3,248,55,131,252,235,4,139,108,36,24,137,149,233,137,252,239,139,115,

105. 252,252,137,92,36,28,232,251,1,8,252,233,244,3,248,56,139,108,36,24,137,149,

106. 233,255,137,206,15,183,83,252,254,137,252,239,137,92,36,28,232,251,1,9,139,

107. 149,233,252,233,244,6,248,57,15,182,107,252,255,248,58,65,141,4,199,252,233,

108. 244,247,248,59,15,182,67,252,254,248,60,65,141,4,199,141,44,252,234,149,252,

109. 233,244,248,248,61,141,4,194,137,197,252,233,244,248,248,62,15,182,107,252,

110. 255,248,63,141,4,194,248,1,141,44,252,234,248,2,141,12,202,68,15,182,67,252,

111. 252,137,206,137,193,139,124,36,24,137,151,233,137,252,234,137,252,253,137,

112. 92,36,28,232,251,1,10,139,149,233,255,133,192,15,132,244,47,248,51,137,193,

113. 41,208,137,89,252,244,141,152,233,184,237,252,233,244,29,248,64,139,108,36,

114. 24,137,149,233,141,52,194,137,252,239,137,92,36,28,232,251,1,11,139,149,233,

115. 255,133,192,15,133,244,51,15,183,67,252,254,139,60,194,252,233,244,65,255,

116. 252,233,244,51,255,248,66,141,76,202,8,248,30,137,76,36,4,137,4,36,131,252,

117. 233,8,139,108,36,24,137,149,233,137,206,141,20,193,137,252,239,137,92,36,

118. 28,232,251,1,12,139,149,233,139,76,36,4,139,4,36,139,105,252,248,131,192,

119. 1,65,57,215,15,132,244,67,137,202,137,90,252,252,139,157,233,139,11,15,182,

120. 252,233,15,182,205,131,195,4,65,252,255,36,252,238,248,68,139,108,36,24,137,

121. 149,233,137,206,137,252,239,137,92,36,28,232,251,1,13,139,149,233,139,67,

122. 252,252,15,182,204,15,182,232,193,232,16,65,252,255,164,253,252,238,233,248,

123. 69,129,252,248,239,15,130,244,70,139,106,4,129,252,253,239,15,131,244,70,

124. 139,90,252,252,137,68,36,4,137,106,252,252,139,42,137,106,252,248,131,232,

125. 2,15,132,244,248,255,137,209,248,1,131,193,8,72,139,41,72,137,105,252,248,

126. 131,232,1,15,133,244,1,248,2,139,68,36,4,252,233,244,71,248,72,129,252,248,

127. 239,15,130,244,70,139,106,4,137,252,233,193,252,249,15,131,252,249,252,254,

128. 15,132,244,249,184,237,252,247,213,57,232,15,71,197,248,2,139,106,252,248,

129. 139,132,253,197,233,139,90,252,252,199,66,252,252,237,137,66,252,248,252,

130. 233,244,73,248,3,184,237,255,252,233,244,2,248,74,129,252,248,239,15,130,

131. 244,70,139,106,4,139,90,252,252,129,252,253,239,15,133,244,252,248,1,139,

132. 42,139,173,233,248,2,133,252,237,199,66,252,252,237,15,132,244,73,65,139,

133. 134,233,199,66,252,252,237,137,106,252,248,139,141,233,35,136,233,105,201,

134. 239,255,3,141,233,248,3,129,185,233,239,15,133,244,250,57,129,233,15,132,

135. 244,251,248,4,139,137,233,133,201,15,133,244,3,252,233,244,73,248,5,139,105,

136. 4,129,252,253,239,15,132,244,73,139,1,137,106,252,252,137,66,252,248,252,

137. 233,244,73,248,6,255,129,252,253,239,15,132,244,1,129,252,253,239,15,135,

138. 244,254,129,252,253,239,15,134,244,253,189,237,252,233,244,254,248,7,189,

139. 237,248,8,252,247,213,65,139,172,253,174,233,252,233,244,2,248,75,129,252,

140. 248,239,255,15,130,244,70,129,122,253,4,239,15,133,244,70,139,42,131,189,

141. 233,0,15,133,244,70,129,122,253,12,239,15,133,244,70,139,66,8,137,133,233,

142. 139,90,252,252,199,66,252,252,237,137,106,252,248,252,246,133,233,235,15,

143. 132,244,247,128,165,233,235,65,139,134,233,255,65,137,174,233,137,133,233,

144. 248,1,252,233,244,73,248,76,129,252,248,239,15,130,244,70,129,122,253,4,239,

145. 15,133,244,70,137,213,139,50,141,82,8,139,124,36,24,232,251,1,14,137,252,

146. 234,72,139,40,139,90,252,252,72,137,106,252,248,252,233,244,73,248,77,129,

147. 252,248,239,15,133,244,70,129,122,253,4,239,15,133,244,247,255,139,42,252,

148. 233,244,78,248,1,15,135,244,70,252,242,15,16,2,252,233,244,79,248,80,129,

149. 252,248,239,15,130,244,70,139,90,252,252,129,122,253,4,239,15,133,244,249,

150. 139,2,248,2,199,66,252,252,237,137,66,252,248,252,233,244,73,248,3,129,122,

151. 253,4,239,255,15,135,244,70,65,131,190,233,0,15,133,244,70,65,139,174,233,

152. 65,59,174,233,15,130,244,247,232,244,81,248,1,139,108,36,24,137,149,233,137,

153. 92,36,28,137,214,137,252,239,232,251,1,15,139,149,233,252,233,244,2,248,82,

154. 129,252,248,239,15,130,244,70,255,15,132,244,248,248,1,129,122,253,4,239,

155. 15,133,244,70,139,108,36,24,137,149,233,137,149,233,139,90,252,252,139,50,

156. 141,82,8,137,252,239,137,92,36,28,232,251,1,16,139,149,233,133,192,15,132,

157. 244,249,72,139,106,8,72,139,66,16,72,137,106,252,248,72,137,2,248,83,184,

158. 237,252,233,244,84,248,2,199,66,12,237,252,233,244,1,248,3,255,199,66,252,

159. 252,237,252,233,244,73,248,85,129,252,248,239,15,130,244,70,139,42,129,122,

160. 253,4,239,15,133,244,70,255,131,189,233,0,15,133,244,70,255,139,106,252,248,

161. 139,133,233,139,90,252,252,199,66,252,252,237,137,66,252,248,199,66,12,237,

162. 184,237,252,233,244,84,248,86,129,252,248,239,15,130,244,70,129,122,253,4,

163. 239,15,133,244,70,129,122,253,12,239,15,133,244,70,139,90,252,252,139,66,

164. 8,131,192,1,199,66,252,252,237,137,66,252,248,139,42,59,133,233,15,131,244,

165. 248,255,193,224,3,3,133,233,248,1,129,120,253,4,239,15,132,244,87,72,139,

166. 40,72,137,42,252,233,244,83,248,2,131,189,233,0,15,132,244,87,137,252,239,

167. 137,213,137,198,232,251,1,3,137,252,234,133,192,15,133,244,1,248,87,184,237,

168. 252,233,244,84,248,88,129,252,248,239,15,130,244,70,255,139,106,252,248,139,

169. 133,233,139,90,252,252,199,66,252,252,237,137,66,252,248,199,66,12,237,199,

170. 66,8,0,0,0,0,184,237,252,233,244,84,248,89,129,252,248,239,15,130,244,70,

171. 141,74,8,131,232,1,187,237,248,1,65,15,182,174,233,193,252,237,235,131,229,

172. 1,1,252,235,252,233,244,29,248,90,129,252,248,239,15,130,244,70,255,129,122,

173. 253,12,239,15,133,244,70,139,106,4,137,106,12,199,66,4,237,139,42,139,90,

174. 8,137,106,8,137,26,141,74,16,131,232,2,187,237,252,233,244,1,248,91,129,252,

175. 248,239,15,130,244,70,139,42,139,90,252,252,137,92,36,28,137,44,36,129,122,

176. 253,4,239,15,133,244,70,72,131,189,233,0,15,133,244,70,128,189,233,235,15,

177. 135,244,70,255,139,141,233,15,132,244,247,59,141,233,15,132,244,70,248,1,

178. 141,92,193,252,240,59,157,233,15,135,244,70,137,157,233,139,108,36,24,137,

179. 149,233,131,194,8,137,149,233,141,108,194,232,72,41,221,57,203,15,132,244,

180. 249,248,2,72,139,4,43,72,137,67,252,248,131,252,235,8,57,203,15,133,244,2,

181. 248,3,137,206,139,60,36,232,244,26,255,139,108,36,24,139,28,36,139,149,233,

182. 65,137,174,233,65,199,134,233,237,129,252,248,239,15,135,244,254,248,4,139,

183. 139,233,68,139,187,233,137,139,233,68,137,252,251,41,203,15,132,244,252,141,

184. 4,26,193,252,235,3,59,133,233,15,135,244,255,137,213,72,41,205,248,5,72,139,

185. 1,72,137,4,41,131,193,8,68,57,252,249,15,133,244,5,248,6,255,141,67,2,199,

186. 66,252,252,237,248,7,139,92,36,28,137,68,36,4,72,199,193,252,248,252,255,

187. 252,255,252,255,252,247,195,237,15,132,244,14,252,233,244,15,248,8,199,66,

188. 252,252,237,139,139,233,131,252,233,8,137,139,233,72,139,1,72,137,2,184,237,

189. 252,233,244,7,248,9,139,12,36,68,137,185,233,137,222,137,252,239,232,251,

190. 1,0,139,28,36,139,149,233,252,233,244,4,248,92,255,139,106,252,248,139,173,

191. 233,139,90,252,252,137,92,36,28,137,44,36,72,131,189,233,0,15,133,244,70,

192. 128,189,233,235,15,135,244,70,139,141,233,15,132,244,247,59,141,233,15,132,

193. 244,70,248,1,141,92,193,252,248,59,157,233,15,135,244,70,137,157,233,139,

194. 108,36,24,137,149,233,255,137,149,233,141,108,194,252,240,72,41,221,57,203,

195. 15,132,244,249,248,2,72,139,4,43,72,137,67,252,248,131,252,235,8,57,203,15,

196. 133,244,2,248,3,137,206,139,60,36,232,244,26,139,108,36,24,139,28,36,139,

197. 149,233,65,137,174,233,65,199,134,233,237,129,252,248,239,15,135,244,254,

198. 248,4,139,139,233,68,139,187,233,137,139,233,255,68,137,252,251,41,203,15,

199. 132,244,252,141,4,26,193,252,235,3,59,133,233,15,135,244,255,137,213,72,41,

200. 205,248,5,72,139,1,72,137,4,41,131,193,8,68,57,252,249,15,133,244,5,248,6,

201. 141,67,1,248,7,139,92,36,28,137,68,36,4,49,201,252,247,195,237,15,132,244,

202. 14,252,233,244,15,248,8,137,222,137,252,239,232,251,1,17,248,9,139,12,36,

203. 68,137,185,233,137,222,137,252,239,232,251,1,0,139,28,36,139,149,233,252,

204. 233,244,4,248,93,255,139,108,36,24,72,252,247,133,233,237,15,132,244,70,137,

205. 149,233,141,68,194,252,248,137,133,233,49,192,72,137,133,233,176,235,136,

206. 133,233,252,233,244,17,248,94,139,90,252,252,221,90,252,248,252,233,244,73,

207. 248,95,129,252,248,239,15,130,244,70,129,122,253,4,239,15,133,244,248,255,

208. 139,42,131,252,253,0,15,137,244,78,252,247,221,15,136,244,247,248,96,248,

209. 78,139,90,252,252,199,66,252,252,237,137,106,252,248,252,233,244,73,248,1,

210. 139,90,252,252,199,66,252,252,0,0,224,65,199,66,252,248,0,0,0,0,252,233,244,

211. 73,248,2,15,135,244,70,252,242,15,16,2,72,184,237,237,102,72,15,110,200,15,

212. 84,193,248,79,139,90,252,252,252,242,15,17,66,252,248,248,73,184,237,248,

213. 84,255,137,68,36,4,248,71,252,247,195,237,15,133,244,253,248,5,56,67,252,

214. 255,15,135,244,252,15,182,75,252,253,72,252,247,209,141,20,202,139,3,15,182,

215. 204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,248,6,199,68,194,

216. 252,244,237,131,192,1,252,233,244,5,248,7,72,199,193,252,248,252,255,252,

217. 255,252,255,252,233,244,15,248,97,129,122,253,4,239,15,133,244,247,139,42,

218. 252,233,244,78,248,1,255,15,135,244,70,252,242,15,16,2,232,244,98,252,242,

219. 15,44,232,129,252,253,0,0,0,128,15,133,244,78,252,242,15,42,205,102,15,46,

220. 193,15,138,244,79,15,132,244,78,252,233,244,79,248,99,129,122,253,4,239,15,

221. 133,244,247,139,42,252,233,244,78,248,1,15,135,244,70,255,252,242,15,16,2,

222. 232,244,100,252,242,15,44,232,129,252,253,0,0,0,128,15,133,244,78,252,242,

223. 15,42,205,102,15,46,193,15,138,244,79,15,132,244,78,252,233,244,79,248,101,

224. 129,252,248,239,15,130,244,70,129,122,253,4,239,15,131,244,70,252,242,15,

225. 81,2,252,233,244,79,248,102,255,129,252,248,239,15,133,244,70,129,122,253,

226. 4,239,15,131,244,70,252,242,15,16,2,137,213,232,251,1,18,137,252,234,252,

227. 233,244,79,248,103,129,252,248,239,15,130,244,70,129,122,253,4,239,15,131,

228. 244,70,252,242,15,16,2,137,213,232,251,1,19,137,252,234,252,233,244,79,248,

229. 104,129,252,248,239,15,130,244,70,255,129,122,253,4,239,15,131,244,70,252,

230. 242,15,16,2,137,213,232,251,1,20,137,252,234,252,233,244,79,248,105,129,252,

231. 248,239,15,130,244,70,129,122,253,4,239,15,131,244,70,252,242,15,16,2,137,

232. 213,232,251,1,21,137,252,234,252,233,244,79,248,106,129,252,248,239,15,130,

233. 244,70,129,122,253,4,239,15,131,244,70,255,252,242,15,16,2,137,213,232,251,

234. 1,22,137,252,234,252,233,244,79,248,107,129,252,248,239,15,130,244,70,129,

235. 122,253,4,239,15,131,244,70,252,242,15,16,2,137,213,232,251,1,23,137,252,

236. 234,252,233,244,79,248,108,129,252,248,239,15,130,244,70,129,122,253,4,239,

237. 15,131,244,70,252,242,15,16,2,137,213,232,251,1,24,137,252,234,252,233,244,

238. 79,248,109,255,129,252,248,239,15,130,244,70,129,122,253,4,239,15,131,244,

239. 70,252,242,15,16,2,137,213,232,251,1,25,137,252,234,252,233,244,79,248,110,

240. 129,252,248,239,15,130,244,70,129,122,253,4,239,15,131,244,70,252,242,15,

241. 16,2,137,213,232,251,1,26,137,252,234,252,233,244,79,248,111,129,252,248,

242. 239,15,130,244,70,255,129,122,253,4,239,15,131,244,70,252,242,15,16,2,137,

243. 213,232,251,1,27,137,252,234,252,233,244,79,248,112,129,252,248,239,15,130,

244. 244,70,129,122,253,4,239,15,131,244,70,252,242,15,16,2,137,213,232,251,1,

245. 28,137,252,234,252,233,244,79,248,113,129,252,248,239,15,130,244,70,129,122,

246. 253,4,239,15,131,244,70,255,252,242,15,16,2,137,213,232,251,1,29,137,252,

247. 234,252,233,244,79,248,114,129,252,248,239,15,130,244,70,129,122,253,4,239,

248. 15,131,244,70,129,122,253,12,239,15,131,244,70,252,242,15,16,2,252,242,15,

249. 16,74,8,137,213,232,251,1,30,137,252,234,252,233,244,79,248,115,129,252,248,

250. 239,15,130,244,70,129,122,253,4,239,15,131,244,70,255,129,122,253,12,239,

251. 15,131,244,70,252,242,15,16,2,252,242,15,16,74,8,137,213,232,251,1,31,137,

252. 252,234,252,233,244,79,248,116,129,252,248,239,15,130,244,70,129,122,253,

253. 4,239,15,131,244,70,129,122,253,12,239,15,131,244,70,252,242,15,16,2,252,

254. 242,15,16,74,8,137,213,232,251,1,32,137,252,234,252,233,244,79,248,117,129,

255. 252,248,239,15,130,244,70,255,129,122,253,4,239,15,131,244,70,129,122,253,

256. 12,239,15,131,244,70,221,66,8,221,2,217,252,253,221,217,252,233,244,94,248,

257. 118,129,252,248,239,15,130,244,70,139,106,4,129,252,253,239,15,131,244,70,

258. 139,90,252,252,139,2,137,106,252,252,137,66,252,248,209,229,129,252,253,0,

259. 0,224,252,255,15,131,244,249,9,232,15,132,244,249,184,252,254,3,0,0,129,252,

260. 253,0,0,32,0,15,130,244,250,248,1,255,193,252,237,21,41,197,252,242,15,42,

261. 197,139,106,252,252,129,229,252,255,252,255,15,128,129,205,0,0,224,63,137,

262. 106,252,252,248,2,252,242,15,17,2,184,237,252,233,244,84,248,3,15,87,192,

263. 252,233,244,2,248,4,252,242,15,16,2,72,189,237,237,102,72,15,110,205,252,

264. 242,15,89,193,252,242,15,17,66,252,248,139,106,252,252,184,52,4,0,0,209,229,

265. 252,233,244,1,248,119,129,252,248,239,15,130,244,70,129,122,253,4,239,15,

266. 131,244,70,252,242,15,16,2,139,106,4,139,90,252,252,209,229,129,252,253,0,

267. 0,224,252,255,15,132,244,250,255,15,40,224,232,244,120,252,242,15,92,224,

268. 248,1,252,242,15,17,66,252,248,252,242,15,17,34,139,66,252,252,139,106,4,

269. 49,232,15,136,244,249,248,2,184,237,252,233,244,84,248,3,129,252,245,0,0,

270. 0,128,137,106,4,252,233,244,2,248,4,15,87,228,252,233,244,1,248,121,185,2,

271. 0,0,0,129,122,253,4,239,15,133,244,250,139,42,248,1,255,57,193,15,131,244,

272. 78,129,124,253,202,252,252,239,15,133,244,249,59,108,202,252,248,15,79,108,

273. 202,252,248,131,193,1,252,233,244,1,248,3,15,135,244,70,252,242,15,42,197,

274. 252,233,244,252,248,4,15,135,244,70,252,242,15,16,2,248,5,57,193,15,131,244,

275. 79,129,124,253,202,252,252,239,15,130,244,252,255,15,135,244,70,252,242,15,

276. 42,76,202,252,248,252,233,244,253,248,6,252,242,15,16,76,202,252,248,248,

277. 7,252,242,15,93,193,131,193,1,252,233,244,5,248,122,185,2,0,0,0,129,122,253,

278. 4,239,15,133,244,250,139,42,248,1,57,193,15,131,244,78,129,124,253,202,252,

279. 252,239,15,133,244,249,59,108,202,252,248,15,76,108,202,252,248,131,193,1,

280. 252,233,244,1,248,3,255,15,135,244,70,252,242,15,42,197,252,233,244,252,248,

281. 4,15,135,244,70,252,242,15,16,2,248,5,57,193,15,131,244,79,129,124,253,202,

282. 252,252,239,15,130,244,252,15,135,244,70,252,242,15,42,76,202,252,248,252,

283. 233,244,253,248,6,252,242,15,16,76,202,252,248,248,7,252,242,15,95,193,131,

284. 193,1,252,233,244,5,248,123,255,129,252,248,239,15,133,244,70,129,122,253,

285. 4,239,15,133,244,70,139,42,139,90,252,252,131,189,233,1,15,130,244,87,15,

286. 182,173,233,252,233,244,78,248,124,65,139,174,233,65,59,174,233,15,130,244,

287. 247,232,244,81,248,1,255,129,252,248,239,15,133,244,70,129,122,253,4,239,

288. 15,133,244,70,139,42,129,252,253,252,255,0,0,0,15,135,244,70,137,108,36,4,

289. 199,68,36,8,1,0,0,0,72,141,68,36,4,248,125,139,108,36,24,137,149,233,139,

290. 84,36,8,72,137,198,137,252,239,137,92,36,28,232,251,1,33,248,126,139,149,

291. 233,139,90,252,252,199,66,252,252,237,137,66,252,248,252,233,244,73,248,127,

292. 65,139,174,233,65,59,174,233,15,130,244,247,255,232,244,81,248,1,199,68,36,

293. 4,252,255,252,255,252,255,252,255,129,252,248,239,15,130,244,70,15,134,244,

294. 247,129,122,253,20,239,15,133,244,70,139,106,16,137,108,36,4,248,1,129,122,

295. 253,4,239,15,133,244,70,129,122,253,12,239,15,133,244,70,139,42,137,108,36,

296. 8,139,173,233,139,74,8,139,68,36,4,57,197,15,130,244,251,248,2,255,133,201,

297. 15,142,244,253,248,3,139,108,36,8,41,200,15,140,244,128,141,172,253,13,233,

298. 131,192,1,248,4,137,68,36,8,137,232,252,233,244,125,248,5,15,140,244,252,

299. 141,68,40,1,252,233,244,2,248,6,137,232,252,233,244,2,248,7,15,132,244,254,

300. 255,1,252,233,131,193,1,15,143,244,3,248,8,185,1,0,0,0,252,233,244,3,248,

301. 128,49,192,252,233,244,4,248,129,129,252,248,239,15,130,244,70,65,139,174,

302. 233,65,59,174,233,15,130,244,247,232,244,81,248,1,129,122,253,4,239,255,15,

303. 133,244,70,139,108,36,24,65,141,190,233,137,149,233,139,50,139,135,233,137,

304. 175,233,137,135,233,137,92,36,28,232,251,1,34,137,199,232,251,1,35,252,233,

305. 244,126,248,130,129,252,248,239,15,130,244,70,65,139,174,233,65,59,174,233,

306. 15,130,244,247,232,244,81,248,1,255,129,122,253,4,239,15,133,244,70,139,108,

307. 36,24,65,141,190,233,137,149,233,139,50,139,135,233,137,175,233,137,135,233,

308. 137,92,36,28,232,251,1,36,137,199,232,251,1,35,252,233,244,126,248,131,129,

309. 252,248,239,15,130,244,70,65,139,174,233,65,59,174,233,15,130,244,247,232,

310. 244,81,248,1,255,129,122,253,4,239,15,133,244,70,139,108,36,24,65,141,190,

311. 233,137,149,233,139,50,139,135,233,137,175,233,137,135,233,137,92,36,28,232,

312. 251,1,37,137,199,232,251,1,35,252,233,244,126,248,132,129,252,248,239,15,

313. 130,244,70,129,122,253,4,239,15,133,244,247,139,42,252,233,244,96,248,1,255,

314. 15,135,244,70,252,242,15,16,2,72,189,237,237,102,72,15,110,205,252,242,15,

315. 88,193,102,15,126,197,248,2,252,233,244,96,248,133,129,252,248,239,15,130,

316. 244,70,72,189,237,237,102,72,15,110,205,129,122,253,4,239,15,133,244,247,

317. 139,42,252,233,244,248,248,1,15,135,244,70,255,252,242,15,16,2,252,242,15,

318. 88,193,102,15,126,197,248,2,137,68,36,4,141,68,194,252,240,248,1,57,208,15,

319. 134,244,96,129,120,253,4,239,15,133,244,248,35,40,131,232,8,252,233,244,1,

320. 248,2,15,135,244,134,252,242,15,16,0,252,242,15,88,193,102,15,126,193,33,

321. 205,131,232,8,252,233,244,1,248,135,129,252,248,239,15,130,244,70,72,189,

322. 237,237,255,102,72,15,110,205,129,122,253,4,239,15,133,244,247,139,42,252,

323. 233,244,248,248,1,15,135,244,70,252,242,15,16,2,252,242,15,88,193,102,15,

324. 126,197,248,2,137,68,36,4,141,68,194,252,240,248,1,57,208,15,134,244,96,129,

325. 120,253,4,239,15,133,244,248,11,40,131,232,8,252,233,244,1,248,2,15,135,244,

326. 134,255,252,242,15,16,0,252,242,15,88,193,102,15,126,193,9,205,131,232,8,

327. 252,233,244,1,248,136,129,252,248,239,15,130,244,70,72,189,237,237,102,72,

328. 15,110,205,129,122,253,4,239,15,133,244,247,139,42,252,233,244,248,248,1,

329. 15,135,244,70,252,242,15,16,2,252,242,15,88,193,102,15,126,197,248,2,137,

330. 68,36,4,141,68,194,252,240,248,1,57,208,15,134,244,96,255,129,120,253,4,239,

331. 15,133,244,248,51,40,131,232,8,252,233,244,1,248,2,15,135,244,134,252,242,

332. 15,16,0,252,242,15,88,193,102,15,126,193,49,205,131,232,8,252,233,244,1,248,

333. 137,129,252,248,239,15,130,244,70,129,122,253,4,239,15,133,244,247,139,42,

334. 252,233,244,248,248,1,255,15,135,244,70,252,242,15,16,2,72,189,237,237,102,

335. 72,15,110,205,252,242,15,88,193,102,15,126,197,248,2,15,205,252,233,244,96,

336. 248,138,129,252,248,239,15,130,244,70,129,122,253,4,239,15,133,244,247,139,

337. 42,252,233,244,248,248,1,15,135,244,70,252,242,15,16,2,72,189,237,237,255,

338. 102,72,15,110,205,252,242,15,88,193,102,15,126,197,248,2,252,247,213,252,

339. 233,244,96,248,134,139,68,36,4,252,233,244,70,248,139,129,252,248,239,15,

340. 130,244,70,129,122,253,4,239,15,133,244,247,139,42,252,233,244,248,248,1,

341. 15,135,244,70,252,242,15,16,2,72,189,237,237,255,102,72,15,110,205,252,242,

342. 15,88,193,102,15,126,197,248,2,129,122,253,12,239,15,133,244,70,139,74,8,

343. 211,229,252,233,244,96,248,140,129,252,248,239,15,130,244,70,129,122,253,

344. 4,239,15,133,244,247,139,42,252,233,244,248,248,1,15,135,244,70,252,242,15,

345. 16,2,72,189,237,237,255,102,72,15,110,205,252,242,15,88,193,102,15,126,197,

346. 248,2,129,122,253,12,239,15,133,244,70,139,74,8,211,252,237,252,233,244,96,

347. 248,141,129,252,248,239,15,130,244,70,129,122,253,4,239,15,133,244,247,139,

348. 42,252,233,244,248,248,1,15,135,244,70,252,242,15,16,2,72,189,237,237,255,

349. 102,72,15,110,205,252,242,15,88,193,102,15,126,197,248,2,129,122,253,12,239,

350. 15,133,244,70,139,74,8,211,252,253,252,233,244,96,248,142,129,252,248,239,

351. 15,130,244,70,129,122,253,4,239,15,133,244,247,139,42,252,233,244,248,248,

352. 1,15,135,244,70,252,242,15,16,2,72,189,237,237,255,102,72,15,110,205,252,

353. 242,15,88,193,102,15,126,197,248,2,129,122,253,12,239,15,133,244,70,139,74,

354. 8,211,197,252,233,244,96,248,143,129,252,248,239,15,130,244,70,129,122,253,

355. 4,239,15,133,244,247,139,42,252,233,244,248,248,1,15,135,244,70,252,242,15,

356. 16,2,72,189,237,237,255,102,72,15,110,205,252,242,15,88,193,102,15,126,197,

357. 248,2,129,122,253,12,239,15,133,244,70,139,74,8,211,205,252,233,244,96,248,

358. 144,184,237,252,233,244,70,248,145,184,237,248,70,139,108,36,24,139,90,252,

359. 252,137,92,36,28,137,149,233,141,68,194,252,248,141,136,233,137,133,233,139,

360. 66,252,248,59,141,233,15,135,244,251,137,252,239,252,255,144,233,255,139,

361. 149,233,133,192,15,143,244,84,248,1,139,141,233,41,209,193,252,233,3,133,

362. 192,141,65,1,139,106,252,248,15,133,244,34,139,157,233,139,11,15,182,252,

363. 233,15,182,205,131,195,4,65,252,255,36,252,238,248,34,137,209,252,247,195,

364. 237,15,133,244,249,15,182,107,252,253,72,252,247,213,141,20,252,234,252,233,

365. 244,29,248,3,137,221,131,229,252,248,41,252,234,252,233,244,29,248,5,190,

366. 237,137,252,239,232,251,1,0,139,149,233,255,49,192,252,233,244,1,248,81,93,

367. 72,137,108,36,8,139,108,36,24,137,92,36,28,137,149,233,141,68,194,252,248,

368. 137,252,239,137,133,233,232,251,1,38,139,149,233,139,133,233,41,208,193,232,

369. 3,131,192,1,72,139,108,36,8,85,195,248,146,65,15,182,134,233,168,235,15,133,

370. 244,251,168,235,15,133,244,247,168,235,15,132,244,247,65,252,255,142,233,

371. 252,233,244,247,248,147,255,65,15,182,134,233,168,235,15,133,244,251,252,

372. 233,244,247,248,148,65,15,182,134,233,168,235,15,133,244,251,168,235,15,132,

373. 244,251,65,252,255,142,233,15,132,244,247,168,235,15,132,244,251,248,1,255,

374. 139,108,36,24,137,149,233,137,222,137,252,239,232,251,1,39,248,3,139,149,

375. 233,248,4,15,182,75,252,253,248,5,15,182,107,252,252,15,183,67,252,254,65,

376. 252,255,164,253,252,238,233,248,149,131,195,4,139,77,232,137,76,36,4,252,

377. 233,244,4,248,150,139,106,252,248,139,173,233,15,182,133,233,141,4,194,139,

378. 108,36,24,137,149,233,137,133,233,137,222,65,141,190,233,73,137,174,233,137,

379. 92,36,28,232,251,1,40,252,233,244,3,248,151,137,92,36,28,252,233,244,247,

380. 248,152,255,137,92,36,28,131,203,1,248,1,141,68,194,252,248,139,108,36,24,

381. 137,149,233,137,133,233,137,222,137,252,239,232,251,1,41,199,68,36,28,0,0,

382. 0,0,131,227,252,254,139,149,233,72,137,193,139,133,233,41,208,72,137,205,

383. 15,182,75,252,253,193,232,3,131,192,1,252,255,229,248,153,139,77,232,137,

384. 12,36,68,137,116,36,8,68,139,116,36,4,15,182,75,252,253,141,12,202,65,131,

385. 252,238,1,15,132,244,248,248,1,72,139,40,72,137,41,131,192,8,131,193,8,65,

386. 131,252,238,1,15,133,244,1,248,2,15,182,67,252,253,15,182,107,252,255,1,232,

387. 141,68,194,252,248,248,3,57,200,15,135,244,255,68,139,116,36,8,139,44,36,

388. 65,139,142,233,139,4,169,133,192,15,132,244,47,15,183,128,233,57,232,15,132,

389. 244,47,255,133,192,15,133,245,65,137,174,233,139,108,36,24,137,149,233,137,

390. 222,65,141,190,233,73,137,174,233,232,251,1,42,139,149,233,252,233,244,47,

391. 248,9,199,65,4,237,131,193,8,252,233,244,3,248,154,255,139,108,36,24,137,

392. 149,233,137,222,137,252,239,232,251,1,43,139,149,233,131,252,235,4,252,233,

393. 244,47,255,248,155,65,85,65,84,65,83,65,82,65,81,65,80,87,86,85,72,141,108,

394. 36,88,85,83,82,81,80,15,182,69,252,248,138,101,252,240,76,137,125,252,248,

395. 76,137,117,252,240,68,139,117,0,65,139,142,233,65,199,134,233,237,65,137,

396. 134,233,65,137,142,233,72,129,252,236,239,72,131,197,128,252,242,68,15,17,

397. 125,252,248,252,242,68,15,17,117,252,240,252,242,68,15,17,109,232,252,242,

398. 68,15,17,101,224,252,242,68,15,17,93,216,252,242,68,15,17,85,208,252,242,

399. 68,15,17,77,200,252,242,68,15,17,69,192,252,242,15,17,125,184,252,242,15,

400. 17,117,176,252,242,15,17,109,168,252,242,15,17,101,160,252,242,15,17,93,152,

401. 252,242,15,17,85,144,252,242,15,17,77,136,252,242,15,17,69,128,65,139,174,

402. 233,65,139,150,233,73,137,174,233,137,149,233,72,137,230,65,141,190,233,65,

403. 199,134,233,0,0,0,0,232,251,1,44,72,139,141,233,72,129,225,239,72,137,204,

404. 137,169,233,139,149,233,139,153,233,252,233,244,247,248,156,255,72,131,196,

405. 16,248,1,76,139,108,36,8,76,139,36,36,255,139,124,36,24,137,151,233,137,197,

406. 137,28,36,137,218,65,139,182,233,232,251,1,45,139,28,36,137,232,139,108,36,

407. 24,139,149,233,255,133,192,15,136,244,255,139,108,36,24,137,68,36,4,68,139,

408. 122,252,248,69,139,191,233,69,139,191,233,137,149,233,65,199,134,233,0,0,

409. 0,0,65,199,134,233,237,139,3,15,182,204,15,182,232,131,195,4,193,232,16,129,

410. 252,253,239,15,130,244,249,129,252,253,239,15,131,244,250,248,2,139,68,36,

411. 4,248,3,65,252,255,36,252,238,248,4,139,66,252,252,169,237,15,133,244,2,255,

412. 15,182,64,252,253,72,252,247,208,68,139,124,194,252,248,69,139,191,233,69,

413. 139,191,233,252,233,244,2,248,9,252,247,216,137,252,239,137,198,232,251,1,

414. 1,248,157,248,98,72,184,237,237,102,72,15,110,208,72,184,237,237,102,72,15,

415. 110,216,15,40,200,102,15,84,202,102,15,46,217,15,134,244,247,102,15,85,208,

416. 252,242,15,88,203,252,242,15,92,203,102,15,86,202,72,184,237,237,102,72,15,

417. 110,208,252,242,15,194,193,1,102,15,84,194,252,242,15,92,200,15,40,193,248,

418. 1,195,248,158,248,100,72,184,237,237,255,102,72,15,110,208,72,184,237,237,

419. 102,72,15,110,216,15,40,200,102,15,84,202,102,15,46,217,15,134,244,247,102,

420. 15,85,208,252,242,15,88,203,252,242,15,92,203,102,15,86,202,72,184,237,237,

421. 102,72,15,110,208,252,242,15,194,193,6,102,15,84,194,252,242,15,92,200,15,

422. 40,193,248,1,195,248,159,248,120,72,184,237,237,102,72,15,110,208,72,184,

423. 237,237,102,72,15,110,216,15,40,200,102,15,84,202,102,15,46,217,15,134,244,

424. 247,102,15,85,208,15,40,193,252,242,15,88,203,252,242,15,92,203,72,184,237,

425. 237,102,72,15,110,216,252,242,15,194,193,1,102,15,84,195,252,242,15,92,200,

426. 102,15,86,202,15,40,193,248,1,195,248,160,15,40,232,252,242,15,94,193,72,

427. 184,237,237,255,102,72,15,110,208,72,184,237,237,102,72,15,110,216,15,40,

428. 224,102,15,84,226,102,15,46,220,15,134,244,247,102,15,85,208,252,242,15,88,

429. 227,252,242,15,92,227,102,15,86,226,72,184,237,237,102,72,15,110,208,252,

430. 242,15,194,196,1,102,15,84,194,252,242,15,92,224,15,40,197,252,242,15,89,

431. 204,252,242,15,92,193,195,248,1,252,242,15,89,200,15,40,197,252,242,15,92,

432. 193,195,248,161,131,252,248,1,15,142,244,252,248,1,169,1,0,0,0,15,133,244,

433. 248,252,242,15,89,192,209,232,252,233,244,1,248,2,209,232,15,132,244,251,

434. 15,40,200,248,3,252,242,15,89,192,209,232,15,132,244,250,255,15,131,244,3,

435. 252,242,15,89,200,252,233,244,3,248,4,252,242,15,89,193,248,5,195,248,6,15,

436. 132,244,5,15,130,244,253,252,247,216,232,244,1,72,184,237,237,102,72,15,110,

437. 200,252,242,15,94,200,15,40,193,195,248,7,72,184,237,237,102,72,15,110,192,

438. 195,248,162,137,252,248,83,15,162,137,6,137,94,4,137,78,8,137,86,12,91,195,

439. 248,163,255,204,255,204,248,164,83,65,87,65,86,72,131,252,236,40,68,141,181,

440. 233,139,157,233,15,183,192,137,131,233,72,137,187,233,72,137,179,233,72,137,

441. 147,233,72,137,139,233,252,242,15,17,131,233,252,242,15,17,139,233,252,242,

442. 15,17,147,233,252,242,15,17,155,233,72,141,132,253,36,233,76,137,131,233,

443. 76,137,139,233,252,242,15,17,163,233,252,242,15,17,171,233,252,242,15,17,

444. 179,233,252,242,15,17,187,233,72,137,131,233,255,72,137,230,137,92,36,28,

445. 137,223,232,251,1,46,65,199,134,233,237,139,144,233,139,128,233,41,208,139,

446. 106,252,248,193,232,3,131,192,1,139,157,233,139,11,15,182,252,233,15,182,

447. 205,131,195,4,65,252,255,36,252,238,248,33,139,76,36,24,65,139,158,233,72,

448. 137,139,233,137,145,233,137,169,233,137,223,137,198,232,251,1,47,72,139,131,

449. 233,252,242,15,16,131,233,252,233,244,17,248,165,85,72,137,229,83,72,137,

450. 252,251,139,131,233,72,41,196,15,182,139,233,131,252,233,1,15,136,244,248,

451. 248,1,255,72,139,132,253,203,233,72,137,132,253,204,233,131,252,233,1,15,

452. 137,244,1,248,2,15,182,131,233,72,139,187,233,72,139,179,233,72,139,147,233,

453. 72,139,139,233,76,139,131,233,76,139,139,233,133,192,15,132,244,251,15,40,

454. 131,233,15,40,139,233,15,40,147,233,15,40,155,233,131,252,248,4,15,134,244,

455. 251,255,15,40,163,233,15,40,171,233,15,40,179,233,15,40,187,233,248,5,252,

456. 255,147,233,72,137,131,233,15,41,131,233,72,137,147,233,15,41,139,233,72,

457. 139,93,252,248,201,195,255,249,255,129,124,253,202,4,239,15,133,244,253,129,

458. 124,253,194,4,239,15,133,244,254,139,44,202,131,195,4,59,44,194,255,15,141,

459. 244,255,255,15,140,244,255,255,15,143,244,255,255,15,142,244,255,255,248,

460. 6,15,183,67,252,254,141,156,253,131,233,248,9,139,3,15,182,204,15,182,232,

461. 131,195,4,193,232,16,65,252,255,36,252,238,248,7,15,135,244,50,129,124,253,

462. 194,4,239,15,130,244,247,15,133,244,50,252,242,15,42,4,194,252,233,244,248,

463. 248,8,15,135,244,50,252,242,15,42,12,202,252,242,15,16,4,194,131,195,4,102,

464. 15,46,193,255,15,134,244,9,255,15,135,244,9,255,15,130,244,9,255,15,131,244,

465. 9,255,252,233,244,6,248,1,252,242,15,16,4,194,248,2,131,195,4,102,15,46,4,

466. 202,248,3,255,252,233,244,6,255,139,108,194,4,131,195,4,129,252,253,239,15,

467. 133,244,253,129,124,253,202,4,239,15,133,244,254,139,44,194,59,44,202,255,

468. 15,133,244,255,255,15,132,244,255,255,15,183,67,252,254,141,156,253,131,233,

469. 248,9,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,

470. 238,248,7,15,135,244,251,129,124,253,202,4,239,15,130,244,247,15,133,244,

471. 251,252,242,15,42,4,202,252,233,244,248,248,8,15,135,244,251,252,242,15,42,

472. 4,194,102,15,46,4,202,252,233,244,250,248,1,252,242,15,16,4,202,248,2,102,

473. 15,46,4,194,248,4,255,15,138,244,248,15,133,244,248,255,15,138,244,248,15,

474. 132,244,247,255,248,1,15,183,67,252,254,141,156,253,131,233,248,2,255,248,

475. 2,15,183,67,252,254,141,156,253,131,233,248,1,255,252,233,244,9,255,139,3,

476. 15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,255,248,

477. 5,129,252,253,239,15,132,244,55,129,124,253,202,4,239,15,132,244,55,57,108,

478. 202,4,15,133,244,2,129,252,253,239,15,131,244,1,139,12,202,139,4,194,57,193,

479. 15,132,244,1,129,252,253,239,15,135,244,2,129,252,253,239,15,130,244,2,255,

480. 139,169,233,133,252,237,15,132,244,2,252,246,133,233,235,15,133,244,2,255,

481. 49,252,237,255,189,1,0,0,0,255,252,233,244,54,255,248,3,129,252,253,239,255,

482. 15,133,244,9,255,252,233,244,55,255,72,252,247,208,139,108,202,4,131,195,

483. 4,129,252,253,239,15,133,244,249,139,12,202,65,59,12,135,255,139,108,202,

484. 4,131,195,4,129,252,253,239,15,133,244,253,65,129,124,253,199,4,239,15,133,

485. 244,254,65,139,44,199,59,44,202,255,15,183,67,252,254,141,156,253,131,233,

486. 248,9,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,

487. 238,248,7,15,135,244,249,65,129,124,253,199,4,239,15,130,244,247,252,242,

488. 65,15,42,4,199,252,233,244,248,248,8,252,242,15,42,4,202,102,65,15,46,4,199,

489. 252,233,244,250,248,1,252,242,65,15,16,4,199,248,2,102,15,46,4,202,248,4,

490. 255,72,252,247,208,139,108,202,4,131,195,4,57,197,255,15,133,244,249,15,183,

491. 67,252,254,141,156,253,131,233,248,2,139,3,15,182,204,15,182,232,131,195,

492. 4,193,232,16,65,252,255,36,252,238,248,3,129,252,253,239,15,133,244,2,252,

493. 233,244,55,255,15,132,244,248,129,252,253,239,15,132,244,55,15,183,67,252,

494. 254,141,156,253,131,233,248,2,139,3,15,182,204,15,182,232,131,195,4,193,232,

495. 16,65,252,255,36,252,238,255,139,108,194,4,131,195,4,129,252,253,239,255,

496. 15,131,244,247,255,137,108,202,4,139,44,194,137,44,202,255,15,183,67,252,

497. 254,141,156,253,131,233,248,1,139,3,15,182,204,15,182,232,131,195,4,193,232,

498. 16,65,252,255,36,252,238,255,3,68,202,4,15,133,244,56,139,3,15,182,204,15,

499. 182,232,131,195,4,193,232,16,65,252,255,36,252,238,255,129,124,253,202,4,

500. 239,15,131,244,56,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,

501. 255,36,252,238,255,72,139,44,194,72,137,44,202,139,3,15,182,204,15,182,232,

502. 131,195,4,193,232,16,65,252,255,36,252,238,255,49,252,237,129,124,253,194,

503. 4,239,129,213,239,137,108,202,4,139,3,15,182,204,15,182,232,131,195,4,193,

504. 232,16,65,252,255,36,252,238,255,129,124,253,194,4,239,15,133,244,251,139,

505. 44,194,252,247,221,15,128,244,250,199,68,202,4,237,137,44,202,248,9,139,3,

506. 15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,248,4,199,

507. 68,202,4,0,0,224,65,199,4,202,0,0,0,0,252,233,244,9,248,5,15,135,244,61,252,

508. 242,15,16,4,194,72,184,237,237,102,72,15,110,200,15,87,193,252,242,15,17,

509. 4,202,252,233,244,9,255,129,124,253,194,4,239,15,133,244,248,139,4,194,139,

510. 128,233,248,1,199,68,202,4,237,137,4,202,139,3,15,182,204,15,182,232,131,

511. 195,4,193,232,16,65,252,255,36,252,238,248,2,129,124,253,194,4,239,15,133,

512. 244,64,139,60,194,255,139,175,233,131,252,253,0,15,133,244,255,248,3,255,

513. 248,65,137,213,232,251,1,48,137,252,234,15,182,75,252,253,252,233,244,1,255,

514. 248,9,252,246,133,233,235,15,133,244,3,252,233,244,64,255,15,182,252,236,

515. 15,182,192,255,129,124,253,252,234,4,239,15,133,244,58,65,129,124,253,199,

516. 4,239,15,133,244,58,139,44,252,234,65,3,44,199,15,128,244,57,255,129,124,

517. 253,252,234,4,239,15,133,244,60,65,129,124,253,199,4,239,15,133,244,60,65,

518. 139,4,199,3,4,252,234,15,128,244,59,255,129,124,253,252,234,4,239,15,133,

519. 244,63,129,124,253,194,4,239,15,133,244,63,139,44,252,234,3,44,194,15,128,

520. 244,62,255,199,68,202,4,237,255,137,4,202,255,129,124,253,252,234,4,239,15,

521. 133,244,58,65,129,124,253,199,4,239,15,133,244,58,139,44,252,234,65,43,44,

522. 199,15,128,244,57,255,129,124,253,252,234,4,239,15,133,244,60,65,129,124,

523. 253,199,4,239,15,133,244,60,65,139,4,199,43,4,252,234,15,128,244,59,255,129,

524. 124,253,252,234,4,239,15,133,244,63,129,124,253,194,4,239,15,133,244,63,139,

525. 44,252,234,43,44,194,15,128,244,62,255,129,124,253,252,234,4,239,15,133,244,

526. 58,65,129,124,253,199,4,239,15,133,244,58,139,44,252,234,65,15,175,44,199,

527. 15,128,244,57,255,129,124,253,252,234,4,239,15,133,244,60,65,129,124,253,

528. 199,4,239,15,133,244,60,65,139,4,199,15,175,4,252,234,15,128,244,59,255,129,

529. 124,253,252,234,4,239,15,133,244,63,129,124,253,194,4,239,15,133,244,63,139,

530. 44,252,234,15,175,44,194,15,128,244,62,255,129,124,253,252,234,4,239,15,131,

531. 244,58,65,129,124,253,199,4,239,15,131,244,58,252,242,15,16,4,252,234,252,

532. 242,65,15,94,4,199,255,129,124,253,252,234,4,239,15,131,244,60,65,129,124,

533. 253,199,4,239,15,131,244,60,252,242,65,15,16,4,199,252,242,15,94,4,252,234,

534. 255,129,124,253,252,234,4,239,15,131,244,63,129,124,253,194,4,239,15,131,

535. 244,63,252,242,15,16,4,252,234,252,242,15,94,4,194,255,252,242,15,17,4,202,

536. 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,255,

537. 129,124,253,252,234,4,239,15,131,244,58,65,129,124,253,199,4,239,15,131,244,

538. 58,252,242,15,16,4,252,234,252,242,65,15,16,12,199,255,129,124,253,252,234,

539. 4,239,15,131,244,60,65,129,124,253,199,4,239,15,131,244,60,252,242,65,15,

540. 16,4,199,252,242,15,16,12,252,234,255,129,124,253,252,234,4,239,15,131,244,

541. 63,129,124,253,194,4,239,15,131,244,63,252,242,15,16,4,252,234,252,242,15,

542. 16,12,194,255,248,166,232,244,160,252,242,15,17,4,202,139,3,15,182,204,15,

543. 182,232,131,195,4,193,232,16,65,252,255,36,252,238,255,252,233,244,166,255,

544. 137,213,232,251,1,30,15,182,75,252,253,137,252,234,252,242,15,17,4,202,139,

545. 3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,255,15,

546. 182,252,236,15,182,192,139,124,36,24,137,151,233,141,52,194,137,194,41,252,

547. 234,248,37,137,252,253,137,92,36,28,232,251,1,49,139,149,233,133,192,15,133,

548. 244,51,15,182,107,252,255,15,182,75,252,253,72,139,4,252,234,72,137,4,202,

549. 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,255,

550. 72,252,247,208,65,139,4,135,199,68,202,4,237,137,4,202,139,3,15,182,204,15,

551. 182,232,131,195,4,193,232,16,65,252,255,36,252,238,255,15,191,192,199,68,

552. 202,4,237,137,4,202,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,

553. 255,36,252,238,255,252,242,65,15,16,4,199,252,242,15,17,4,202,139,3,15,182,

554. 204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,255,72,252,247,

555. 208,137,68,202,4,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,

556. 255,36,252,238,255,141,76,202,12,141,68,194,4,189,237,137,105,252,248,248,

557. 1,137,41,131,193,8,57,193,15,134,244,1,139,3,15,182,204,15,182,232,131,195,

558. 4,193,232,16,65,252,255,36,252,238,255,139,106,252,248,139,172,253,133,233,

559. 139,173,233,72,139,69,0,72,137,4,202,139,3,15,182,204,15,182,232,131,195,

560. 4,193,232,16,65,252,255,36,252,238,255,139,106,252,248,139,172,253,141,233,

561. 128,189,233,0,139,173,233,139,12,194,139,68,194,4,137,77,0,137,69,4,15,132,

562. 244,247,252,246,133,233,235,15,133,244,248,248,1,139,3,15,182,204,15,182,

563. 232,131,195,4,193,232,16,65,252,255,36,252,238,248,2,129,232,239,129,252,

564. 248,239,15,134,244,1,252,246,129,233,235,15,132,244,1,137,252,238,137,213,

565. 65,141,190,233,255,232,251,1,50,137,252,234,252,233,244,1,255,72,252,247,

566. 208,139,106,252,248,139,172,253,141,233,65,139,12,135,139,133,233,137,8,199,

567. 64,4,237,252,246,133,233,235,15,133,244,248,248,1,139,3,15,182,204,15,182,

568. 232,131,195,4,193,232,16,65,252,255,36,252,238,248,2,252,246,129,233,235,

569. 15,132,244,1,128,189,233,0,15,132,244,1,137,213,137,198,65,141,190,233,232,

570. 251,1,50,137,252,234,252,233,244,1,255,139,106,252,248,252,242,65,15,16,4,

571. 199,139,172,253,141,233,139,141,233,252,242,15,17,1,139,3,15,182,204,15,182,

572. 232,131,195,4,193,232,16,65,252,255,36,252,238,255,72,252,247,208,139,106,

573. 252,248,139,172,253,141,233,139,141,233,137,65,4,139,3,15,182,204,15,182,

574. 232,131,195,4,193,232,16,65,252,255,36,252,238,255,141,156,253,131,233,139,

575. 108,36,24,131,189,233,0,15,132,244,247,137,149,233,141,52,202,137,252,239,

576. 232,251,1,51,139,149,233,248,1,139,3,15,182,204,15,182,232,131,195,4,193,

577. 232,16,65,252,255,36,252,238,255,72,252,247,208,139,108,36,24,137,149,233,

578. 139,82,252,248,65,139,52,135,137,252,239,137,92,36,28,232,251,1,52,139,149,

579. 233,15,182,75,252,253,137,4,202,199,68,202,4,237,139,3,15,182,204,15,182,

580. 232,131,195,4,193,232,16,65,252,255,36,252,238,255,139,108,36,24,137,149,

581. 233,65,139,142,233,65,59,142,233,137,92,36,28,15,131,244,251,248,1,137,194,

582. 37,252,255,7,0,0,193,252,234,11,61,252,255,7,0,0,15,132,244,249,248,2,137,

583. 252,239,137,198,232,251,1,53,139,149,233,15,182,75,252,253,137,4,202,199,

584. 68,202,4,237,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,

585. 36,252,238,248,3,184,1,8,0,0,252,233,244,2,248,5,137,252,239,232,251,1,54,

586. 15,183,67,252,254,252,233,244,1,255,72,252,247,208,139,108,36,24,65,139,142,

587. 233,137,92,36,28,65,59,142,233,137,149,233,15,131,244,249,248,2,65,139,52,

588. 135,137,252,239,232,251,1,55,139,149,233,15,182,75,252,253,137,4,202,199,

589. 68,202,4,237,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,

590. 36,252,238,248,3,137,252,239,232,251,1,54,15,183,67,252,254,72,252,247,208,

591. 252,233,244,2,255,72,252,247,208,139,106,252,248,139,173,233,65,139,4,135,

592. 252,233,244,167,255,72,252,247,208,139,106,252,248,139,173,233,65,139,4,135,

593. 252,233,244,168,255,15,182,252,236,15,182,192,129,124,253,252,234,4,239,15,

594. 133,244,40,139,44,252,234,129,124,253,194,4,239,15,133,244,251,139,4,194,

595. 59,133,233,15,131,244,40,193,224,3,3,133,233,129,120,253,4,239,15,132,244,

596. 248,72,139,40,72,137,44,202,248,1,139,3,15,182,204,15,182,232,131,195,4,193,

597. 232,16,65,252,255,36,252,238,248,2,131,189,233,0,15,132,244,249,139,141,233,

598. 252,246,129,233,235,255,15,132,244,40,15,182,75,252,253,248,3,199,68,202,

599. 4,237,252,233,244,1,248,5,129,124,253,194,4,239,15,133,244,40,139,4,194,252,

600. 233,244,167,255,15,182,252,236,15,182,192,72,252,247,208,65,139,4,135,129,

601. 124,253,252,234,4,239,15,133,244,38,139,44,252,234,248,167,139,141,233,35,

602. 136,233,105,201,239,3,141,233,248,1,129,185,233,239,15,133,244,250,57,129,

603. 233,15,133,244,250,129,121,253,4,239,15,132,244,251,15,182,67,252,253,72,

604. 139,41,72,137,44,194,248,2,255,139,3,15,182,204,15,182,232,131,195,4,193,

605. 232,16,65,252,255,36,252,238,248,3,15,182,67,252,253,199,68,194,4,237,252,

606. 233,244,2,248,4,139,137,233,133,201,15,133,244,1,248,5,139,141,233,133,201,

607. 15,132,244,3,252,246,129,233,235,15,133,244,3,252,233,244,38,255,15,182,252,

608. 236,15,182,192,129,124,253,252,234,4,239,15,133,244,39,139,44,252,234,59,

609. 133,233,15,131,244,39,193,224,3,3,133,233,129,120,253,4,239,15,132,244,248,

610. 72,139,40,72,137,44,202,248,1,139,3,15,182,204,15,182,232,131,195,4,193,232,

611. 16,65,252,255,36,252,238,248,2,131,189,233,0,15,132,244,249,139,141,233,252,

612. 246,129,233,235,15,132,244,39,255,15,182,75,252,253,248,3,199,68,202,4,237,

613. 252,233,244,1,255,15,182,252,236,15,182,192,139,44,252,234,139,4,194,59,133,

614. 233,15,131,244,41,193,224,3,3,133,233,248,42,72,139,40,72,137,44,202,248,

615. 43,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,

616. 255,15,182,252,236,15,182,192,129,124,253,252,234,4,239,15,133,244,46,139,

617. 44,252,234,129,124,253,194,4,239,15,133,244,251,139,4,194,59,133,233,15,131,

618. 244,46,193,224,3,3,133,233,129,120,253,4,239,15,132,244,249,248,1,252,246,

619. 133,233,235,15,133,244,253,248,2,72,139,44,202,72,137,40,139,3,15,182,204,

620. 15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,248,3,255,131,189,233,

621. 0,15,132,244,1,139,141,233,252,246,129,233,235,15,132,244,46,15,182,75,252,

622. 253,252,233,244,1,248,5,129,124,253,194,4,239,15,133,244,46,139,4,194,252,

623. 233,244,168,248,7,128,165,233,235,65,139,142,233,255,65,137,174,233,137,141,

624. 233,15,182,75,252,253,252,233,244,2,255,15,182,252,236,15,182,192,72,252,

625. 247,208,65,139,4,135,129,124,253,252,234,4,239,15,133,244,44,139,44,252,234,

626. 248,168,139,141,233,35,136,233,105,201,239,198,133,233,0,3,141,233,248,1,

627. 129,185,233,239,15,133,244,251,57,129,233,15,133,244,251,129,121,253,4,239,

628. 15,132,244,250,248,2,255,252,246,133,233,235,15,133,244,253,248,3,15,182,

629. 67,252,253,72,139,44,194,72,137,41,139,3,15,182,204,15,182,232,131,195,4,

630. 193,232,16,65,252,255,36,252,238,248,4,131,189,233,0,15,132,244,2,137,12,

631. 36,139,141,233,252,246,129,233,235,15,132,244,44,139,12,36,252,233,244,2,

632. 248,5,139,137,233,133,201,15,133,244,1,255,139,141,233,133,201,15,132,244,

633. 252,252,246,129,233,235,15,132,244,44,248,6,137,4,36,199,68,36,4,237,137,

634. 108,36,8,139,124,36,24,137,151,233,72,141,20,36,137,252,238,137,252,253,137,

635. 92,36,28,232,251,1,56,139,149,233,139,108,36,8,137,193,252,233,244,2,248,

636. 7,128,165,233,235,65,139,134,233,65,137,174,233,137,133,233,252,233,244,3,

637. 255,15,182,252,236,15,182,192,129,124,253,252,234,4,239,15,133,244,45,139,

638. 44,252,234,59,133,233,15,131,244,45,193,224,3,3,133,233,129,120,253,4,239,

639. 15,132,244,249,248,1,252,246,133,233,235,15,133,244,253,248,2,72,139,12,202,

640. 72,137,8,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,

641. 238,248,3,131,189,233,0,15,132,244,1,255,139,141,233,252,246,129,233,235,

642. 15,132,244,45,15,182,75,252,253,252,233,244,1,248,7,128,165,233,235,65,139,

643. 142,233,65,137,174,233,137,141,233,15,182,75,252,253,252,233,244,2,255,15,

644. 182,252,236,15,182,192,139,44,252,234,139,4,194,252,246,133,233,235,15,133,

645. 244,253,248,2,59,133,233,15,131,244,48,193,224,3,3,133,233,248,49,72,139,

646. 44,202,72,137,40,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,

647. 255,36,252,238,248,7,128,165,233,235,65,139,142,233,65,137,174,233,137,141,

648. 233,15,182,75,252,253,252,233,244,2,255,68,137,60,36,69,139,60,199,248,1,

649. 141,12,202,139,105,252,248,252,246,133,233,235,15,133,244,253,248,2,139,68,

650. 36,4,131,232,1,15,132,244,250,68,1,252,248,59,133,233,15,135,244,251,68,41,

651. 252,248,65,193,231,3,68,3,189,233,248,3,72,139,41,131,193,8,73,137,47,65,

652. 131,199,8,131,232,1,15,133,244,3,248,4,68,139,60,36,139,3,15,182,204,15,182,

653. 232,131,195,4,193,232,16,65,252,255,36,252,238,248,5,139,124,36,24,137,151,

654. 233,137,252,238,137,194,137,252,253,137,92,36,28,232,251,1,57,139,149,233,

655. 15,182,75,252,253,252,233,244,1,248,7,255,128,165,233,235,65,139,134,233,

656. 65,137,174,233,137,133,233,252,233,244,2,255,3,68,36,4,255,129,124,253,202,

657. 4,239,139,44,202,15,133,244,66,141,84,202,8,137,90,252,252,139,157,233,139,

658. 11,15,182,252,233,15,182,205,131,195,4,65,252,255,36,252,238,255,141,76,202,

659. 8,65,137,215,139,105,252,248,129,121,253,252,252,239,15,133,244,30,248,67,

660. 139,90,252,252,252,247,195,237,15,133,244,253,248,1,137,106,252,248,137,68,

661. 36,4,131,232,1,15,132,244,249,248,2,72,139,41,131,193,8,73,137,47,65,131,

662. 199,8,131,232,1,15,133,244,2,139,106,252,248,248,3,139,68,36,4,128,189,233,

663. 1,15,135,244,251,248,4,139,157,233,139,11,15,182,252,233,15,182,205,131,195,

664. 4,65,252,255,36,252,238,248,5,255,252,247,195,237,15,133,244,4,15,182,75,

665. 252,253,72,252,247,209,68,139,124,202,252,248,69,139,191,233,69,139,191,233,

666. 252,233,244,4,248,7,129,252,235,239,252,247,195,237,15,133,244,254,41,218,

667. 65,137,215,139,90,252,252,252,233,244,1,248,8,129,195,239,252,233,244,1,255,

668. 141,76,202,8,72,139,105,232,72,139,65,252,240,72,137,41,72,137,65,8,139,105,

669. 224,139,65,228,137,105,252,248,137,65,252,252,129,252,248,239,184,237,15,

670. 133,244,30,137,202,137,90,252,252,139,157,233,139,11,15,182,252,233,15,182,

671. 205,131,195,4,65,252,255,36,252,238,255,68,137,60,36,68,137,116,36,4,139,

672. 108,202,252,240,139,68,202,252,248,68,139,181,233,131,195,4,68,139,189,233,

673. 248,1,68,57,252,240,15,131,244,251,65,129,124,253,199,4,239,15,132,244,250,

674. 199,68,202,4,237,137,4,202,73,139,44,199,72,137,108,202,8,131,192,1,137,68,

675. 202,252,248,248,2,15,183,67,252,254,141,156,253,131,233,248,3,68,139,116,

676. 36,4,68,139,60,36,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,

677. 255,36,252,238,248,4,131,192,1,252,233,244,1,248,5,68,41,252,240,248,6,59,

678. 133,233,15,135,244,3,255,68,105,252,248,239,68,3,189,233,65,129,191,233,239,

679. 15,132,244,253,70,141,116,48,1,73,139,175,233,73,139,135,233,72,137,44,202,

680. 72,137,68,202,8,68,137,116,202,252,248,252,233,244,2,248,7,131,192,1,252,

681. 233,244,6,255,129,124,253,202,252,236,239,15,133,244,251,139,108,202,232,

682. 129,124,253,202,252,244,239,15,133,244,251,129,124,253,202,252,252,239,15,

683. 133,244,251,128,189,233,235,15,133,244,251,141,156,253,131,233,199,68,202,

684. 252,248,0,0,0,0,199,68,202,252,252,252,255,127,252,254,252,255,248,1,139,

685. 3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,248,5,

686. 198,67,252,252,235,141,156,253,131,233,198,3,235,252,233,244,1,255,15,182,

687. 252,236,15,182,192,68,137,60,36,68,141,188,253,194,233,141,12,202,68,43,122,

688. 252,252,133,252,237,15,132,244,251,141,108,252,233,252,248,65,57,215,15,131,

689. 244,248,248,1,73,139,71,252,248,65,131,199,8,72,137,1,131,193,8,57,252,233,

690. 15,131,244,249,65,57,215,15,130,244,1,248,2,199,65,4,237,131,193,8,57,252,

691. 233,15,130,244,2,248,3,68,139,60,36,139,3,15,182,204,15,182,232,131,195,4,

692. 193,232,16,65,252,255,36,252,238,248,5,199,68,36,4,1,0,0,0,137,208,68,41,

693. 252,248,15,134,244,3,137,197,193,252,237,3,131,197,1,137,108,36,4,139,108,

694. 36,24,1,200,59,133,233,15,135,244,253,248,6,255,73,139,71,252,248,65,131,

695. 199,8,72,137,1,131,193,8,65,57,215,15,130,244,6,252,233,244,3,248,7,137,149,

696. 233,137,141,233,137,92,36,28,65,41,215,139,116,36,4,131,252,238,1,137,252,

697. 239,232,251,1,0,139,149,233,139,141,233,65,1,215,252,233,244,6,255,193,225,

698. 3,255,248,1,139,90,252,252,137,68,36,4,252,247,195,237,15,133,244,253,255,

699. 248,14,65,137,215,131,232,1,15,132,244,249,248,2,73,139,44,15,73,137,111,

700. 252,248,65,131,199,8,131,232,1,15,133,244,2,248,3,139,68,36,4,15,182,107,

701. 252,255,248,5,57,197,15,135,244,252,255,72,139,44,10,72,137,106,252,248,255,

702. 248,5,56,67,252,255,15,135,244,252,255,15,182,75,252,253,72,252,247,209,141,

703. 20,202,68,139,122,252,248,69,139,191,233,69,139,191,233,139,3,15,182,204,

704. 15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,248,6,255,65,199,71,

705. 252,252,237,65,131,199,8,255,199,68,194,252,244,237,255,131,192,1,252,233,

706. 244,5,248,7,141,171,233,252,247,197,237,15,133,244,15,41,252,234,255,1,252,

707. 233,255,137,221,209,252,237,129,229,239,102,65,129,172,253,46,233,238,15,

708. 130,244,150,255,141,12,202,255,129,121,253,4,239,15,133,244,255,255,129,121,

709. 253,12,239,15,133,244,68,129,121,253,20,239,15,133,244,68,139,41,131,121,

710. 16,0,15,140,244,251,255,129,121,253,12,239,15,133,244,163,129,121,253,20,

711. 239,15,133,244,163,255,139,105,16,133,252,237,15,136,244,251,3,41,15,128,

712. 244,247,137,41,255,59,105,8,199,65,28,237,137,105,24,255,15,142,244,253,248,

713. 1,248,6,141,156,253,131,233,255,141,156,253,131,233,15,183,67,252,254,15,

714. 142,245,248,1,248,6,255,15,143,244,253,248,6,141,156,253,131,233,248,1,255,

715. 248,7,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,252,255,36,252,

716. 238,248,5,255,3,41,15,128,244,1,137,41,255,15,141,244,7,255,141,156,253,131,

717. 233,15,183,67,252,254,15,141,245,255,15,140,244,7,255,252,233,244,6,248,9,

718. 255,129,121,253,4,239,255,15,131,244,68,129,121,253,12,239,15,131,244,68,

719. 255,129,121,253,12,239,15,131,244,163,129,121,253,20,239,15,131,244,163,255,

720. 139,105,20,255,129,252,253,239,15,131,244,68,255,252,242,15,16,1,252,242,

721. 15,16,73,8,255,252,242,15,88,65,16,252,242,15,17,1,133,252,237,15,136,244,

722. 249,255,15,140,244,249,255,102,15,46,200,248,1,252,242,15,17,65,24,255,15,

723. 131,244,7,255,141,156,253,131,233,15,183,67,252,254,15,131,245,255,15,130,

724. 244,7,255,252,233,244,6,248,3,102,15,46,193,252,233,244,1,255,141,12,202,

725. 139,105,4,129,252,253,239,15,132,244,247,255,137,105,252,252,139,41,137,105,

726. 252,248,252,233,245,255,141,156,253,131,233,139,1,137,105,252,252,137,65,

727. 252,248,255,139,108,36,24,137,149,233,137,4,36,137,218,137,198,137,252,239,

728. 232,251,1,58,139,4,36,139,149,233,255,65,139,142,233,139,4,129,72,139,128,

729. 233,139,108,36,24,65,137,150,233,65,137,174,233,76,137,36,36,76,137,108,36,

730. 8,72,131,252,236,16,252,255,224,255,141,156,253,131,233,139,3,15,182,204,

731. 15,182,232,131,195,4,193,232,16,65,252,255,36,252,238,255,137,221,209,252,

732. 237,129,229,239,102,65,129,172,253,46,233,238,15,130,244,152,255,68,139,187,

733. 233,139,108,36,24,141,12,202,59,141,233,15,135,244,25,15,182,139,233,57,200,

734. 15,134,244,249,248,2,255,15,183,67,252,254,252,233,245,255,248,3,199,68,194,

735. 252,252,237,131,192,1,57,200,15,134,244,3,252,233,244,2,255,141,44,197,237,

736. 141,4,194,68,139,122,252,248,137,104,252,252,68,137,120,252,248,139,108,36,

737. 24,141,12,200,59,141,233,15,135,244,24,137,209,137,194,15,182,171,233,133,

738. 252,237,15,132,244,248,248,1,131,193,8,57,209,15,131,244,249,68,139,121,252,

739. 248,68,137,56,68,139,121,252,252,68,137,120,4,131,192,8,199,65,252,252,237,

740. 131,252,237,1,15,133,244,1,248,2,255,68,139,187,233,139,3,15,182,204,15,182,

741. 232,131,195,4,193,232,16,65,252,255,36,252,238,255,248,3,199,64,4,237,131,

742. 192,8,131,252,237,1,15,133,244,3,252,233,244,2,255,139,106,252,248,76,139,

743. 189,233,139,108,36,24,141,68,194,252,248,137,149,233,141,136,233,59,141,233,

744. 137,133,233,255,137,252,239,255,76,137,252,254,137,252,239,255,15,135,244,

745. 23,65,199,134,233,237,255,65,252,255,215,255,65,252,255,150,233,255,139,149,

746. 233,65,137,174,233,65,199,134,233,237,141,12,194,252,247,217,3,141,233,139,

747. 90,252,252,252,233,244,12,255,254,0

748. };

750. #line 13 "vm_x86.dasc"

751. //|.globals GLOB_

752. enum {

753. GLOB_vm_returnp,

754. GLOB_cont_dispatch,

755. GLOB_vm_returnc,

756. GLOB_vm_unwind_yield,

757. GLOB_BC_RET_Z,

758. GLOB_vm_return,

759. GLOB_vm_leave_cp,

760. GLOB_vm_leave_unw,

761. GLOB_vm_unwind_c_eh,

762. GLOB_vm_unwind_c,

763. GLOB_vm_unwind_rethrow,

764. GLOB_vm_unwind_ff,

765. GLOB_vm_unwind_ff_eh,

766. GLOB_vm_growstack_c,

767. GLOB_vm_growstack_v,

768. GLOB_vm_growstack_f,

769. GLOB_vm_resume,

770. GLOB_vm_pcall,

771. GLOB_vm_call,

772. GLOB_vm_call_dispatch,

773. GLOB_vmeta_call,

774. GLOB_vm_call_dispatch_f,

775. GLOB_vm_cpcall,

776. GLOB_cont_ffi_callback,

777. GLOB_vm_call_tail,

778. GLOB_cont_cat,

779. GLOB_cont_ra,

780. GLOB_BC_CAT_Z,

781. GLOB_vmeta_tgets,

782. GLOB_vmeta_tgetb,

783. GLOB_vmeta_tgetv,

784. GLOB_vmeta_tgetr,

785. GLOB_BC_TGETR_Z,

786. GLOB_BC_TGETR2_Z,

787. GLOB_vmeta_tsets,

788. GLOB_vmeta_tsetb,

789. GLOB_vmeta_tsetv,

790. GLOB_cont_nop,

791. GLOB_vmeta_tsetr,

792. GLOB_BC_TSETR_Z,

793. GLOB_vmeta_comp,

794. GLOB_vmeta_binop,

795. GLOB_cont_condt,

796. GLOB_cont_condf,

797. GLOB_vmeta_equal,

798. GLOB_vmeta_equal_cd,

799. GLOB_vmeta_istype,

800. GLOB_vmeta_arith_vno,

801. GLOB_vmeta_arith_vn,

802. GLOB_vmeta_arith_nvo,

803. GLOB_vmeta_arith_nv,

804. GLOB_vmeta_unm,

805. GLOB_vmeta_arith_vvo,

806. GLOB_vmeta_arith_vv,

807. GLOB_vmeta_len,

808. GLOB_BC_LEN_Z,

809. GLOB_vmeta_call_ra,

810. GLOB_BC_CALLT_Z,

811. GLOB_vmeta_for,

812. GLOB_ff_assert,

813. GLOB_fff_fallback,

814. GLOB_fff_res_,

815. GLOB_ff_type,

816. GLOB_fff_res1,

817. GLOB_ff_getmetatable,

818. GLOB_ff_setmetatable,

819. GLOB_ff_rawget,

820. GLOB_ff_tonumber,

821. GLOB_fff_resi,

822. GLOB_fff_resxmm0,

823. GLOB_ff_tostring,

824. GLOB_fff_gcstep,

825. GLOB_ff_next,

826. GLOB_fff_res2,

827. GLOB_fff_res,

828. GLOB_ff_pairs,

829. GLOB_ff_ipairs_aux,

830. GLOB_fff_res0,

831. GLOB_ff_ipairs,

832. GLOB_ff_pcall,

833. GLOB_ff_xpcall,

834. GLOB_ff_coroutine_resume,

835. GLOB_ff_coroutine_wrap_aux,

836. GLOB_ff_coroutine_yield,

837. GLOB_fff_resn,

838. GLOB_ff_math_abs,

839. GLOB_fff_resbit,

840. GLOB_ff_math_floor,

841. GLOB_vm_floor_sse,

842. GLOB_ff_math_ceil,

843. GLOB_vm_ceil_sse,

844. GLOB_ff_math_sqrt,

845. GLOB_ff_math_log,

846. GLOB_ff_math_log10,

847. GLOB_ff_math_exp,

848. GLOB_ff_math_sin,

849. GLOB_ff_math_cos,

850. GLOB_ff_math_tan,

851. GLOB_ff_math_asin,

852. GLOB_ff_math_acos,

853. GLOB_ff_math_atan,

854. GLOB_ff_math_sinh,

855. GLOB_ff_math_cosh,

856. GLOB_ff_math_tanh,

857. GLOB_ff_math_pow,

858. GLOB_ff_math_atan2,

859. GLOB_ff_math_fmod,

860. GLOB_ff_math_ldexp,

861. GLOB_ff_math_frexp,

862. GLOB_ff_math_modf,

863. GLOB_vm_trunc_sse,

864. GLOB_ff_math_min,

865. GLOB_ff_math_max,

866. GLOB_ff_string_byte,

867. GLOB_ff_string_char,

868. GLOB_fff_newstr,

869. GLOB_fff_resstr,

870. GLOB_ff_string_sub,

871. GLOB_fff_emptystr,

872. GLOB_ff_string_reverse,

873. GLOB_ff_string_lower,

874. GLOB_ff_string_upper,

875. GLOB_ff_bit_tobit,

876. GLOB_ff_bit_band,

877. GLOB_fff_fallback_bit_op,

878. GLOB_ff_bit_bor,

879. GLOB_ff_bit_bxor,

880. GLOB_ff_bit_bswap,

881. GLOB_ff_bit_bnot,

882. GLOB_ff_bit_lshift,

883. GLOB_ff_bit_rshift,

884. GLOB_ff_bit_arshift,

885. GLOB_ff_bit_rol,

886. GLOB_ff_bit_ror,

887. GLOB_fff_fallback_2,

888. GLOB_fff_fallback_1,

889. GLOB_vm_record,

890. GLOB_vm_rethook,

891. GLOB_vm_inshook,

892. GLOB_cont_hook,

893. GLOB_vm_hotloop,

894. GLOB_vm_callhook,

895. GLOB_vm_hotcall,

896. GLOB_cont_stitch,

897. GLOB_vm_profhook,

898. GLOB_vm_exit_handler,

899. GLOB_vm_exit_interp,

900. GLOB_vm_floor,

901. GLOB_vm_ceil,

902. GLOB_vm_trunc,

903. GLOB_vm_mod,

904. GLOB_vm_powi_sse,

905. GLOB_vm_cpuid,

906. GLOB_assert_bad_for_arg_type,

907. GLOB_vm_ffi_callback,

908. GLOB_vm_ffi_call,

909. GLOB_BC_MODVN_Z,

910. GLOB_BC_TGETS_Z,

911. GLOB_BC_TSETS_Z,

912. GLOB__MAX

913. };

914. #line 14 "vm_x86.dasc"

915. //|.globalnames globnames

916. ‌static const char *const globnames[] = {

917. "vm_returnp",

918. "cont_dispatch",

919. "vm_returnc",

920. "vm_unwind_yield",

921. "BC_RET_Z",

922. "vm_return",

923. "vm_leave_cp",

924. "vm_leave_unw",

925. "vm_unwind_c_eh",

926. "vm_unwind_c@8",

927. "vm_unwind_rethrow",

928. "vm_unwind_ff@4",

929. "vm_unwind_ff_eh",

930. "vm_growstack_c",

931. "vm_growstack_v",

932. "vm_growstack_f",

933. "vm_resume",

934. "vm_pcall",

935. "vm_call",

936. "vm_call_dispatch",

937. "vmeta_call",

938. "vm_call_dispatch_f",

939. "vm_cpcall",

940. "cont_ffi_callback",

941. "vm_call_tail",

942. "cont_cat",

943. "cont_ra",

944. "BC_CAT_Z",

945. "vmeta_tgets",

946. "vmeta_tgetb",

947. "vmeta_tgetv",

948. "vmeta_tgetr",

949. "BC_TGETR_Z",

950. "BC_TGETR2_Z",

951. "vmeta_tsets",

952. "vmeta_tsetb",

953. "vmeta_tsetv",

954. "cont_nop",

955. "vmeta_tsetr",

956. "BC_TSETR_Z",

957. "vmeta_comp",

958. "vmeta_binop",

959. "cont_condt",

960. "cont_condf",

961. "vmeta_equal",

962. "vmeta_equal_cd",

963. "vmeta_istype",

964. "vmeta_arith_vno",

965. "vmeta_arith_vn",

966. "vmeta_arith_nvo",

967. "vmeta_arith_nv",

968. "vmeta_unm",

969. "vmeta_arith_vvo",

970. "vmeta_arith_vv",

971. "vmeta_len",

972. "BC_LEN_Z",

973. "vmeta_call_ra",

974. "BC_CALLT_Z",

975. "vmeta_for",

976. "ff_assert",

977. "fff_fallback",

978. "fff_res_",

979. "ff_type",

980. "fff_res1",

981. "ff_getmetatable",

982. "ff_setmetatable",

983. "ff_rawget",

984. "ff_tonumber",

985. "fff_resi",

986. "fff_resxmm0",

987. "ff_tostring",

988. "fff_gcstep",

989. "ff_next",

990. "fff_res2",

991. "fff_res",

992. "ff_pairs",

993. "ff_ipairs_aux",

994. "fff_res0",

995. "ff_ipairs",

996. "ff_pcall",

997. "ff_xpcall",

998. "ff_coroutine_resume",

999. "ff_coroutine_wrap_aux",

1000. "ff_coroutine_yield",

1001. "fff_resn",

1002. "ff_math_abs",

1003. "fff_resbit",

1004. "ff_math_floor",

1005. "vm_floor_sse",

1006. "ff_math_ceil",

1007. "vm_ceil_sse",

1008. "ff_math_sqrt",

1009. "ff_math_log",

1010. "ff_math_log10",

1011. "ff_math_exp",

1012. "ff_math_sin",

1013. "ff_math_cos",

1014. "ff_math_tan",

1015. "ff_math_asin",

1016. "ff_math_acos",

1017. "ff_math_atan",

1018. "ff_math_sinh",

1019. "ff_math_cosh",

1020. "ff_math_tanh",

1021. "ff_math_pow",

1022. "ff_math_atan2",

1023. "ff_math_fmod",

1024. "ff_math_ldexp",

1025. "ff_math_frexp",

1026. "ff_math_modf",

1027. "vm_trunc_sse",

1028. "ff_math_min",

1029. "ff_math_max",

1030. "ff_string_byte",

1031. "ff_string_char",

1032. "fff_newstr",

1033. "fff_resstr",

1034. "ff_string_sub",

1035. "fff_emptystr",

1036. "ff_string_reverse",

1037. "ff_string_lower",

1038. "ff_string_upper",

1039. "ff_bit_tobit",

1040. "ff_bit_band",

1041. "fff_fallback_bit_op",

1042. "ff_bit_bor",

1043. "ff_bit_bxor",

1044. "ff_bit_bswap",

1045. "ff_bit_bnot",

1046. "ff_bit_lshift",

1047. "ff_bit_rshift",

1048. "ff_bit_arshift",

1049. "ff_bit_rol",

1050. "ff_bit_ror",

1051. "fff_fallback_2",

1052. "fff_fallback_1",

1053. "vm_record",

1054. "vm_rethook",

1055. "vm_inshook",

1056. "cont_hook",

1057. "vm_hotloop",

1058. "vm_callhook",

1059. "vm_hotcall",

1060. "cont_stitch",

1061. "vm_profhook",

1062. "vm_exit_handler",

1063. "vm_exit_interp",

1064. "vm_floor",

1065. "vm_ceil",

1066. "vm_trunc",

1067. "vm_mod",

1068. "vm_powi_sse",

1069. "vm_cpuid",

1070. "assert_bad_for_arg_type",

1071. "vm_ffi_callback",

1072. "vm_ffi_call@4",

1073. "BC_MODVN_Z",

1074. "BC_TGETS_Z",

1075. "BC_TSETS_Z",

1076. (const char *)0

1077. };

1078. #line 15 "vm_x86.dasc"

1079. //|.externnames extnames

1080. ‌static const char *const extnames[] = {

1081. "lj_state_growstack@8",

1082. "lj_err_throw@8",

1083. "lj_meta_tget",

1084. "lj_tab_getinth@8",

1085. "lj_meta_tset",

1086. "lj_tab_setinth",

1087. "lj_meta_comp",

1088. "lj_meta_equal",

1089. "lj_meta_equal_cd@8",

1090. "lj_meta_istype",

1091. "lj_meta_arith",

1092. "lj_meta_len@8",

1093. "lj_meta_call",

1094. "lj_meta_for@8",

1095. "lj_tab_get",

1096. "lj_strfmt_number@8",

1097. "lj_tab_next",

1098. "lj_ffh_coroutine_wrap_err@8",

1099. "log",

1100. "log10",

1101. "exp",

1102. "sin",

1103. "cos",

1104. "tan",

1105. "asin",

1106. "acos",

1107. "atan",

1108. "sinh",

1109. "cosh",

1110. "tanh",

1111. "pow",

1112. "atan2",

1113. "fmod",

1114. "lj_str_new",

1115. "lj_buf_putstr_reverse@8",

1116. "lj_buf_tostr@4",

1117. "lj_buf_putstr_lower@8",

1118. "lj_buf_putstr_upper@8",

1119. "lj_gc_step@4",

1120. "lj_dispatch_ins@8",

1121. "lj_trace_hot@8",

1122. "lj_dispatch_call@8",

1123. "lj_dispatch_stitch@8",

1124. "lj_dispatch_profile@8",

1125. "lj_trace_exit@8",

1126. "lj_log_trace_direct_exit@8",

1127. "lj_ccallback_enter@8",

1128. "lj_ccallback_leave@8",

1129. "lj_tab_len@4",

1130. "lj_meta_cat",

1131. "lj_gc_barrieruv@8",

1132. "lj_func_closeuv@8",

1133. "lj_func_newL_gc",

1134. "lj_tab_new",

1135. "lj_gc_step_fixtop@4",

1136. "lj_tab_dup@8",

1137. "lj_tab_newkey",

1138. "lj_tab_reasize",

1139. "lj_log_trace_entry@8",

1140. (const char *)0

1141. };

1142. #line 16 "vm_x86.dasc"

1143. //|

1144. //|//-----------------------------------------------------------------------

1145. //|

1146. //|.if P64

1147. //|.define X64, 1

1148. //|.if WIN

1149. //|.define X64WIN, 1

1150. //|.endif

1151. //|.endif

1152. //|

1153. //|// Fixed register assignments for the interpreter.

1154. //|// This is very fragile and has many dependencies. Caveat emptor.

1155. //|.define BASE,                edx                // Not C callee-save, refetched anyway.

1156. //|.if not X64

1157. //|.define KBASE,                edi                // Must be C callee-save.

1158. //|.define KBASEa,        KBASE

1159. //|.define PC,                esi                // Must be C callee-save.

1160. //|.define PCa,                PC

1161. //|.define DISPATCH,        ebx                // Must be C callee-save.

1162. //|.elif X64WIN

1163. //|.define KBASE,                edi                // Must be C callee-save.

1164. //|.define KBASEa,        rdi

1165. //|.define PC,                esi                // Must be C callee-save.

1166. //|.define PCa,                rsi

1167. //|.define DISPATCH,        ebx                // Must be C callee-save.

1168. //|.else

1169. //|.define KBASE,                r15d                // Must be C callee-save.

1170. //|.define KBASEa,        r15

1171. //|.define PC,                ebx                // Must be C callee-save.

1172. //|.define PCa,                rbx

1173. //|.define DISPATCH,        r14d                // Must be C callee-save.

1174. //|.endif

1175. //|

1176. //|.define RA,                ecx

1177. //|.define RAH,                ch

1178. //|.define RAL,                cl

1179. //|.define RB,                ebp                // Must be ebp (C callee-save).

1180. //|.define RC,                eax                // Must be eax.

1181. //|.define RCW,                ax

1182. //|.define RCH,                ah

1183. //|.define RCL,                al

1184. //|.define OP,                RB

1185. //|.define RD,                RC

1186. //|.define RDW,                RCW

1187. //|.define RDL,                RCL

1188. //|.if X64

1189. //|.define RAa, rcx

1190. //|.define RBa, rbp

1191. //|.define RCa, rax

1192. //|.define RDa, rax

1193. //|.else

1194. //|.define RAa, RA

1195. //|.define RBa, RB

1196. //|.define RCa, RC

1197. //|.define RDa, RD

1198. //|.endif

1199. //|

1200. //|.if not X64

1201. //|.define FCARG1,        ecx                // x86 fastcall arguments.

1202. //|.define FCARG2,        edx

1203. //|.elif X64WIN

1204. //|.define CARG1,                rcx                // x64/WIN64 C call arguments.

1205. //|.define CARG2,                rdx

1206. //|.define CARG3,                r8

1207. //|.define CARG4,                r9

1208. //|.define CARG1d,        ecx

1209. //|.define CARG2d,        edx

1210. //|.define CARG3d,        r8d

1211. //|.define CARG4d,        r9d

1212. //|.define FCARG1,        CARG1d                // Upwards compatible to x86 fastcall.

1213. //|.define FCARG2,        CARG2d

1214. //|.else

1215. //|.define CARG1,                rdi                // x64/POSIX C call arguments.

1216. //|.define CARG2,                rsi

1217. //|.define CARG3,                rdx

1218. //|.define CARG4,                rcx

1219. //|.define CARG5,                r8

1220. //|.define CARG6,                r9

1221. //|.define CARG1d,        edi

1222. //|.define CARG2d,        esi

1223. //|.define CARG3d,        edx

1224. //|.define CARG4d,        ecx

1225. //|.define CARG5d,        r8d

1226. //|.define CARG6d,        r9d

1227. //|.define FCARG1,        CARG1d                // Simulate x86 fastcall.

1228. //|.define FCARG2,        CARG2d

1229. //|.endif

1230. //|

1231. //|// Type definitions. Some of these are only used for documentation.

1232. //|.type L,                lua_State

1233. ‌#define Dt1(_V) (int)(ptrdiff_t)&(((lua_State *)0)_V)

1234. #line 106 "vm_x86.dasc"

1235. //|.type GL,                global_State

1236. ‌#define Dt2(_V) (int)(ptrdiff_t)&(((global_State *)0)_V)

1237. #line 107 "vm_x86.dasc"

1238. //|.type TVALUE,                TValue

1239. ‌#define Dt3(_V) (int)(ptrdiff_t)&(((TValue *)0)_V)

1240. #line 108 "vm_x86.dasc"

1241. //|.type GCOBJ,                GCobj

1242. ‌#define Dt4(_V) (int)(ptrdiff_t)&(((GCobj *)0)_V)

1243. #line 109 "vm_x86.dasc"

1244. //|.type STR,                GCstr

1245. ‌#define Dt5(_V) (int)(ptrdiff_t)&(((GCstr *)0)_V)

1246. #line 110 "vm_x86.dasc"

1247. //|.type TAB,                GCtab

1248. ‌#define Dt6(_V) (int)(ptrdiff_t)&(((GCtab *)0)_V)

1249. #line 111 "vm_x86.dasc"

1250. //|.type LFUNC,                GCfuncL

1251. ‌#define Dt7(_V) (int)(ptrdiff_t)&(((GCfuncL *)0)_V)

1252. #line 112 "vm_x86.dasc"

1253. //|.type CFUNC,                GCfuncC

1254. ‌#define Dt8(_V) (int)(ptrdiff_t)&(((GCfuncC *)0)_V)

1255. #line 113 "vm_x86.dasc"

1256. //|.type PROTO,                GCproto

1257. ‌#define Dt9(_V) (int)(ptrdiff_t)&(((GCproto *)0)_V)

1258. #line 114 "vm_x86.dasc"

1259. //|.type UPVAL,                GCupval

1260. ‌#define DtA(_V) (int)(ptrdiff_t)&(((GCupval *)0)_V)

1261. #line 115 "vm_x86.dasc"

1262. //|.type NODE,                Node

1263. ‌#define DtB(_V) (int)(ptrdiff_t)&(((Node *)0)_V)

1264. #line 116 "vm_x86.dasc"

1265. //|.type NARGS,                int

1266. ‌#define DtC(_V) (int)(ptrdiff_t)&(((int *)0)_V)

1267. #line 117 "vm_x86.dasc"

1268. //|.type TRACE,                GCtrace

1269. ‌#define DtD(_V) (int)(ptrdiff_t)&(((GCtrace *)0)_V)

1270. #line 118 "vm_x86.dasc"

1271. //|.type SBUF,                SBuf

1272. ‌#define DtE(_V) (int)(ptrdiff_t)&(((SBuf *)0)_V)

1273. #line 119 "vm_x86.dasc"

1274. //|

1275. //|// Stack layout while in interpreter. Must match with lj_frame.h.

1276. //|//-----------------------------------------------------------------------

1277. //|.if not X64                // x86 stack layout.

1278. //|

1279. //|.define CFRAME_SPACE,        aword*7                        // Delta for esp (see <--).

1280. //|.macro saveregs_

1281. //|  push edi; push esi; push ebx

1282. //|  sub esp, CFRAME_SPACE

1283. //|.endmacro

1284. //|.macro saveregs

1285. //|  push ebp; saveregs_

1286. //|.endmacro

1287. //|.macro restoreregs

1288. //|  add esp, CFRAME_SPACE

1289. //|  pop ebx; pop esi; pop edi; pop ebp

1290. //|.endmacro

1291. //|

1292. //|.define SAVE_ERRF,        aword [esp+aword*15]        // vm_pcall/vm_cpcall only.

1293. //|.define SAVE_NRES,        aword [esp+aword*14]

1294. //|.define SAVE_CFRAME,        aword [esp+aword*13]

1295. //|.define SAVE_L,        aword [esp+aword*12]

1296. //|//----- 16 byte aligned, ^^^ arguments from C caller

1297. //|.define SAVE_RET,        aword [esp+aword*11]        //<-- esp entering interpreter.

1298. //|.define SAVE_R4,        aword [esp+aword*10]

1299. //|.define SAVE_R3,        aword [esp+aword*9]

1300. //|.define SAVE_R2,        aword [esp+aword*8]

1301. //|//----- 16 byte aligned

1302. //|.define SAVE_R1,        aword [esp+aword*7]        //<-- esp after register saves.

1303. //|.define SAVE_PC,        aword [esp+aword*6]

1304. //|.define TMP2,                aword [esp+aword*5]

1305. //|.define TMP1,                aword [esp+aword*4]

1306. //|//----- 16 byte aligned

1307. //|.define ARG4,                aword [esp+aword*3]

1308. //|.define ARG3,                aword [esp+aword*2]

1309. //|.define ARG2,                aword [esp+aword*1]

1310. //|.define ARG1,                aword [esp]                //<-- esp while in interpreter.

1311. //|//----- 16 byte aligned, ^^^ arguments for C callee

1312. //|

1313. //|// FPARGx overlaps ARGx and ARG(x+1) on x86.

1314. //|.define FPARG3,        qword [esp+qword*1]

1315. //|.define FPARG1,        qword [esp]

1316. //|// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ).

1317. //|.define TMPQ,                qword [esp+aword*4]

1318. //|.define TMP3,                ARG4

1319. //|.define ARG5,                TMP1

1320. //|.define TMPa,                TMP1

1321. //|.define MULTRES,        TMP2

1322. //|

1323. //|// Arguments for vm_call and vm_pcall.

1324. //|.define INARG_BASE,        SAVE_CFRAME                // Overwritten by SAVE_CFRAME!

1325. //|

1326. //|// Arguments for vm_cpcall.

1327. //|.define INARG_CP_CALL,        SAVE_ERRF

1328. //|.define INARG_CP_UD,        SAVE_NRES

1329. //|.define INARG_CP_FUNC,        SAVE_CFRAME

1330. //|

1331. //|//-----------------------------------------------------------------------

1332. //|.elif X64WIN                // x64/Windows stack layout

1333. //|

1334. //|.define CFRAME_SPACE,        aword*5                        // Delta for rsp (see <--).

1335. //|.macro saveregs_

1336. //|  push rdi; push rsi; push rbx

1337. //|  sub rsp, CFRAME_SPACE

1338. //|.endmacro

1339. //|.macro saveregs

1340. //|  push rbp; saveregs_

1341. //|.endmacro

1342. //|.macro restoreregs

1343. //|  add rsp, CFRAME_SPACE

1344. //|  pop rbx; pop rsi; pop rdi; pop rbp

1345. //|.endmacro

1346. //|

1347. //|.define SAVE_CFRAME,        aword [rsp+aword*13]

1348. //|.define SAVE_PC,        dword [rsp+dword*25]

1349. //|.define SAVE_L,        dword [rsp+dword*24]

1350. //|.define SAVE_ERRF,        dword [rsp+dword*23]

1351. //|.define SAVE_NRES,        dword [rsp+dword*22]

1352. //|.define TMP2,                dword [rsp+dword*21]

1353. //|.define TMP1,                dword [rsp+dword*20]

1354. //|//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter

1355. //|.define SAVE_RET,        aword [rsp+aword*9]        //<-- rsp entering interpreter.

1356. //|.define SAVE_R4,        aword [rsp+aword*8]

1357. //|.define SAVE_R3,        aword [rsp+aword*7]

1358. //|.define SAVE_R2,        aword [rsp+aword*6]

1359. //|.define SAVE_R1,        aword [rsp+aword*5]        //<-- rsp after register saves.

1360. //|.define ARG5,                aword [rsp+aword*4]

1361. //|.define CSAVE_4,        aword [rsp+aword*3]

1362. //|.define CSAVE_3,        aword [rsp+aword*2]

1363. //|.define CSAVE_2,        aword [rsp+aword*1]

1364. //|.define CSAVE_1,        aword [rsp]                //<-- rsp while in interpreter.

1365. //|//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee

1366. //|

1367. //|// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).

1368. //|.define TMPQ,                qword [rsp+aword*10]

1369. //|.define MULTRES,        TMP2

1370. //|.define TMPa,                ARG5

1371. //|.define ARG5d,                dword [rsp+aword*4]

1372. //|.define TMP3,                ARG5d

1373. //|

1374. //|//-----------------------------------------------------------------------

1375. //|.else                        // x64/POSIX stack layout

1376. //|

1377. //|.define CFRAME_SPACE,        aword*5                        // Delta for rsp (see <--).

1378. //|.macro saveregs_

1379. //|  push rbx; push r15; push r14

1380. //|  sub rsp, CFRAME_SPACE

1381. //|.endmacro

1382. //|.macro saveregs

1383. //|  push rbp; saveregs_

1384. //|.endmacro

1385. //|.macro restoreregs

1386. //|  add rsp, CFRAME_SPACE

1387. //|  pop r14; pop r15; pop rbx; pop rbp

1388. //|.endmacro

1389. //|

1390. //|//----- 16 byte aligned,

1391. //|.define SAVE_RET,        aword [rsp+aword*9]        //<-- rsp entering interpreter.

1392. //|.define SAVE_R4,        aword [rsp+aword*8]

1393. //|.define SAVE_R3,        aword [rsp+aword*7]

1394. //|.define SAVE_R2,        aword [rsp+aword*6]

1395. //|.define SAVE_R1,        aword [rsp+aword*5]        //<-- rsp after register saves.

1396. //|.define SAVE_CFRAME,        aword [rsp+aword*4]

1397. //|.define SAVE_PC,        dword [rsp+dword*7]

1398. //|.define SAVE_L,        dword [rsp+dword*6]

1399. //|.define SAVE_ERRF,        dword [rsp+dword*5]

1400. //|.define SAVE_NRES,        dword [rsp+dword*4]

1401. //|.define TMPa,                aword [rsp+aword*1]

1402. //|.define TMP2,                dword [rsp+dword*1]

1403. //|.define TMP1,                dword [rsp]                //<-- rsp while in interpreter.

1404. //|//----- 16 byte aligned

1405. //|

1406. //|// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).

1407. //|.define TMPQ,                qword [rsp]

1408. //|.define TMP3,                dword [rsp+aword*1]

1409. //|.define MULTRES,        TMP2

1410. //|

1411. //|.endif

1412. //|

1413. //|//-----------------------------------------------------------------------

1414. //|

1415. //|// Instruction headers.

1416. //|.macro ins_A; .endmacro

1417. //|.macro ins_AD; .endmacro

1418. //|.macro ins_AJ; .endmacro

1419. //|.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro

1420. //|.macro ins_AB_; movzx RB, RCH; .endmacro

1421. //|.macro ins_A_C; movzx RC, RCL; .endmacro

1422. //|.macro ins_AND; not RDa; .endmacro

1423. //|

1424. //|// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).

1425. //|.macro ins_NEXT

1426. //|  mov RC, [PC]

1427. //|  movzx RA, RCH

1428. //|  movzx OP, RCL

1429. //|  add PC, 4

1430. //|  shr RC, 16

1431. //|.if X64

1432. //|  jmp aword [DISPATCH+OP*8]

1433. //|.else

1434. //|  jmp aword [DISPATCH+OP*4]

1435. //|.endif

1436. //|.endmacro

1437. //|

1438. //|// Instruction footer.

1439. //|.if 1

1440. //|  // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.

1441. //|  .define ins_next, ins_NEXT

1442. //|  .define ins_next_, ins_NEXT

1443. //|.else

1444. //|  // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.

1445. //|  // Affects only certain kinds of benchmarks (and only with -j off).

1446. //|  // Around 10%-30% slower on Core2, a lot more slower on P4.

1447. //|  .macro ins_next

1448. //|    jmp ->ins_next

1449. //|  .endmacro

1450. //|  .macro ins_next_

1451. //|  ->ins_next:

1452. //|    ins_NEXT

1453. //|  .endmacro

1454. //|.endif

1455. //|

1456. //|// Call decode and dispatch.

1457. //|.macro ins_callt

1458. //|  // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC

1459. //|  mov PC, LFUNC:RB->pc

1460. //|  mov RA, [PC]

1461. //|  movzx OP, RAL

1462. //|  movzx RA, RAH

1463. //|  add PC, 4

1464. //|.if X64

1465. //|  jmp aword [DISPATCH+OP*8]

1466. //|.else

1467. //|  jmp aword [DISPATCH+OP*4]

1468. //|.endif

1469. //|.endmacro

1470. //|

1471. //|.macro ins_call

1472. //|  // BASE = new base, RB = LFUNC, RD = nargs+1

1473. //|  mov [BASE-4], PC

1474. //|  ins_callt

1475. //|.endmacro

1476. //|

1477. //|//-----------------------------------------------------------------------

1478. //|

1479. //|// Macros to test operand types.

1480. //|.macro checktp, reg, tp;  cmp dword [BASE+reg*8+4], tp; .endmacro

1481. //|.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro

1482. //|.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro

1483. //|.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro

1484. //|.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro

1485. //|

1486. //|// These operands must be used with movzx.

1487. //|.define PC_OP, byte [PC-4]

1488. //|.define PC_RA, byte [PC-3]

1489. //|.define PC_RB, byte [PC-1]

1490. //|.define PC_RC, byte [PC-2]

1491. //|.define PC_RD, word [PC-2]

1492. //|

1493. //|.macro branchPC, reg

1494. //|  lea PC, [PC+reg*4-BCBIAS_J*4]

1495. //|.endmacro

1496. //|

1497. //|// Assumes DISPATCH is relative to GL.

1498. ‌#define DISPATCH_GL(field)        (GG_DISP2G + (int)offsetof(global_State, field))

1499. ‌#define DISPATCH_J(field)        (GG_DISP2J + (int)offsetof(jit_State, field))

1500. //|

1501. ‌#define PC2PROTO(field)  ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))

1502. //|

1503. //|// Decrement hashed hotcount and trigger trace recorder if zero.

1504. //|.macro hotloop, reg

1505. //|  mov reg, PC

1506. //|  shr reg, 1

1507. //|  and reg, HOTCOUNT_PCMASK

1508. //|  sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP

1509. //|  jb ->vm_hotloop

1510. //|.endmacro

1511. //|

1512. //|.macro hotcall, reg

1513. //|  mov reg, PC

1514. //|  shr reg, 1

1515. //|  and reg, HOTCOUNT_PCMASK

1516. //|  sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL

1517. //|  jb ->vm_hotcall

1518. //|.endmacro

1519. //|

1520. //|// Set current VM state.

1521. //|.macro set_vmstate, st

1522. //|  mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st

1523. //|.endmacro

1524. //|

1525. //|// x87 compares.

1526. //|.macro fcomparepp                        // Compare and pop st0 >< st1.

1527. //|  fucomip st1

1528. //|  fpop

1529. //|.endmacro

1530. //|

1531. //|.macro fpop1; fstp st1; .endmacro

1532. //|

1533. //|// Synthesize SSE FP constants.

1534. //|.macro sseconst_abs, reg, tmp                // Synthesize abs mask.

1535. //|.if X64

1536. //|  mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp

1537. //|.else

1538. //|  pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1

1539. //|.endif

1540. //|.endmacro

1541. //|

1542. //|.macro sseconst_hi, reg, tmp, val        // Synthesize hi-32 bit const.

1543. //|.if X64

1544. //|  mov64 tmp, U64x(val,00000000); movd reg, tmp

1545. //|.else

1546. //|  mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51

1547. //|.endif

1548. //|.endmacro

1549. //|

1550. //|.macro sseconst_sign, reg, tmp                // Synthesize sign mask.

1551. //|  sseconst_hi reg, tmp, 80000000

1552. //|.endmacro

1553. //|.macro sseconst_1, reg, tmp                // Synthesize 1.0.

1554. //|  sseconst_hi reg, tmp, 3ff00000

1555. //|.endmacro

1556. //|.macro sseconst_m1, reg, tmp                // Synthesize -1.0.

1557. //|  sseconst_hi reg, tmp, bff00000

1558. //|.endmacro

1559. //|.macro sseconst_2p52, reg, tmp                // Synthesize 2^52.

1560. //|  sseconst_hi reg, tmp, 43300000

1561. //|.endmacro

1562. //|.macro sseconst_tobit, reg, tmp        // Synthesize 2^52 + 2^51.

1563. //|  sseconst_hi reg, tmp, 43380000

1564. //|.endmacro

1565. //|

1566. //|// Move table write barrier back. Overwrites reg.

1567. //|.macro barrierback, tab, reg

1568. //|  and byte tab->marked, (uint8_t)~LJ_GC_BLACK        // black2gray(tab)

1569. //|  mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]

1570. //|  mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab

1571. //|  mov tab->gclist, reg

1572. //|.endmacro

1573. //|

1574. //|//-----------------------------------------------------------------------

1576. /* Generate subroutines used by opcodes and other parts of the VM. */

1577. /* The .code_sub section should be last to help static branch prediction. */

1578. ‌static void build_subroutines(BuildCtx *ctx)

1579. {

1580. //|.code_sub

1581. dasm_put(Dst, 0);

1582. #line 426 "vm_x86.dasc"

1583. //|

1584. //|//-----------------------------------------------------------------------

1585. //|//-- Return handling ----------------------------------------------------

1586. //|//-----------------------------------------------------------------------

1587. //|

1588. //|->vm_returnp:

1589. //|  test PC, FRAME_P

1590. //|  jz ->cont_dispatch

1591. //|

1592. //|  // Return from pcall or xpcall fast func.

1593. //|  and PC, -8

1594. //|  sub BASE, PC                        // Restore caller base.

1595. //|  lea RAa, [RA+PC-8]                        // Rebase RA and prepend one result.

1596. //|  mov PC, [BASE-4]                        // Fetch PC of previous frame.

1597. //|  // Prepending may overwrite the pcall frame, so do it at the end.

1598. //|  mov dword [BASE+RA+4], LJ_TTRUE        // Prepend true to results.

1599. //|

1600. //|->vm_returnc:

1601. //|  add RD, 1                                // RD = nresults+1

1602. //|  jz ->vm_unwind_yield

1603. //|  mov MULTRES, RD

1604. //|  test PC, FRAME_TYPE

1605. //|  jz ->BC_RET_Z                        // Handle regular return to Lua.

1606. //|

1607. //|->vm_return:

1608. //|  // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return

1609. //|  xor PC, FRAME_C

1610. //|  test PC, FRAME_TYPE

1611. //|  jnz ->vm_returnp

1612. //|

1613. //|  // Return to C.

1614. //|  set_vmstate C

1615. //|  and PC, -8

1616. //|  sub PC, BASE

1617. //|  neg PC                                // Previous base = BASE - delta.

1618. //|

1619. //|  sub RD, 1

1620. //|  jz >2

1621. //|1:  // Move results down.

1622. //|.if X64

1623. //|  mov RBa, [BASE+RA]

1624. dasm_put(Dst, 2, FRAME_P, LJ_TTRUE, FRAME_TYPE, FRAME_C, FRAME_TYPE, DISPATCH_GL(vmstate), ~LJ_VMST_C);

1625. #line 467 "vm_x86.dasc"

1626. //|  mov [BASE-8], RBa

1627. //|.else

1628. //|  mov RB, [BASE+RA]

1629. //|  mov [BASE-8], RB

1630. //|  mov RB, [BASE+RA+4]

1631. //|  mov [BASE-4], RB

1632. //|.endif

1633. //|  add BASE, 8

1634. //|  sub RD, 1

1635. //|  jnz <1

1636. //|2:

1637. //|  mov L:RB, SAVE_L

1638. //|  mov L:RB->base, PC

1639. //|3:

1640. //|  mov RD, MULTRES

1641. //|  mov RA, SAVE_NRES                        // RA = wanted nresults+1

1642. //|4:

1643. //|  cmp RA, RD

1644. //|  jne >6                                // More/less results wanted?

1645. //|5:

1646. //|  sub BASE, 8

1647. //|  mov L:RB->top, BASE

1648. //|

1649. //|->vm_leave_cp:

1650. //|  mov RAa, SAVE_CFRAME                // Restore previous C frame.

1651. //|  mov L:RB->cframe, RAa

1652. //|  xor eax, eax                        // Ok return status for vm_pcall.

1653. //|

1654. //|->vm_leave_unw:

1655. //|  restoreregs

1656. //|  ret

1657. //|

1658. //|6:

1659. //|  jb >7                                // Less results wanted?

1660. //|  // More results wanted. Check stack size and fill up results with nil.

1661. //|  cmp BASE, L:RB->maxstack

1662. //|  ja >8

1663. //|  mov dword [BASE-4], LJ_TNIL

1664. //|  add BASE, 8

1665. dasm_put(Dst, 92, Dt1(->base), Dt1(->top), Dt1(->cframe), Dt1(->maxstack), LJ_TNIL);

1666. #line 506 "vm_x86.dasc"

1667. //|  add RD, 1

1668. //|  jmp <4

1669. //|

1670. //|7:  // Less results wanted.

1671. //|  test RA, RA

1672. //|  jz <5                                // But check for LUA_MULTRET+1.

1673. //|  sub RA, RD                                // Negative result!

1674. //|  lea BASE, [BASE+RA*8]                // Correct top.

1675. //|  jmp <5

1676. //|

1677. //|8:  // Corner case: need to grow stack for filling up results.

1678. //|  // This can happen if:

1679. //|  // - A C function grows the stack (a lot).

1680. //|  // - The GC shrinks the stack in between.

1681. //|  // - A return back from a lua_call() with (high) nresults adjustment.

1682. //|  mov L:RB->top, BASE                // Save current top held in BASE (yes).

1683. //|  mov MULTRES, RD                        // Need to fill only remainder with nil.

1684. //|  mov FCARG2, RA

1685. //|  mov FCARG1, L:RB

1686. //|  call extern lj_state_growstack@8        // (lua_State *L, int n)

1687. //|  mov BASE, L:RB->top                // Need the (realloced) L->top in BASE.

1688. //|  jmp <3

1689. //|

1690. //|->vm_unwind_yield:

1691. //|  mov al, LUA_YIELD

1692. //|  jmp ->vm_unwind_c_eh

1693. //|

1694. //|->vm_unwind_c@8:                        // Unwind C stack, return from vm_pcall.

1695. //|  // (void *cframe, int errcode)

1696. //|.if X64

1697. //|  mov eax, CARG2d                        // Error return status for vm_pcall.

1698. //|  mov rsp, CARG1

1699. //|.else

1700. //|  mov eax, FCARG2                        // Error return status for vm_pcall.

1701. //|  mov esp, FCARG1

1702. //|.endif

1703. //|->vm_unwind_c_eh:                        // Landing pad for external unwinder.

1704. //|  mov L:RB, SAVE_L

1705. //|  mov GL:RB, L:RB->glref

1706. //|  mov dword GL:RB->vmstate, ~LJ_VMST_C

1707. //|  jmp ->vm_leave_unw

1708. dasm_put(Dst, 192, Dt1(->top), Dt1(->top), LUA_YIELD, Dt1(->glref), Dt2(->vmstate), ~LJ_VMST_C);

1709. #line 547 "vm_x86.dasc"

1710. //|

1711. //|->vm_unwind_rethrow:

1712. //|.if X64 and not X64WIN

1713. //|  mov FCARG1, SAVE_L

1714. //|  mov FCARG2, eax

1715. //|  restoreregs

1716. //|  jmp extern lj_err_throw@8                // (lua_State *L, int errcode)

1717. //|.endif

1718. //|

1719. //|->vm_unwind_ff@4:                        // Unwind C stack, return from ff pcall.

1720. //|  // (void *cframe)

1721. //|.if X64

1722. //|  and CARG1, CFRAME_RAWMASK

1723. //|  mov rsp, CARG1

1724. //|.else

1725. //|  and FCARG1, CFRAME_RAWMASK

1726. //|  mov esp, FCARG1

1727. //|.endif

1728. //|->vm_unwind_ff_eh:                        // Landing pad for external unwinder.

1729. //|  mov L:RB, SAVE_L

1730. //|  mov RAa, -8                        // Results start at BASE+RA = BASE-8.

1731. //|  mov RD, 1+1                        // Really 1+2 results, incr. later.

1732. //|  mov BASE, L:RB->base

1733. //|  mov DISPATCH, L:RB->glref                // Setup pointer to dispatch table.

1734. //|  add DISPATCH, GG_G2DISP

1735. //|  mov PC, [BASE-4]                        // Fetch PC of previous frame.

1736. //|  mov dword [BASE-4], LJ_TFALSE        // Prepend false to error message.

1737. //|  set_vmstate INTERP

1738. //|  jmp ->vm_returnc                        // Increments RD/MULTRES and returns.

1739. //|

1740. //|//-----------------------------------------------------------------------

1741. //|//-- Grow stack for calls -----------------------------------------------

1742. //|//-----------------------------------------------------------------------

1743. //|

1744. //|->vm_growstack_c:                        // Grow stack for C function.

1745. //|  mov FCARG2, LUA_MINSTACK

1746. //|  jmp >2

1747. //|

1748. //|->vm_growstack_v:                        // Grow stack for vararg Lua function.

1749. //|  sub RD, 8

1750. dasm_put(Dst, 275, CFRAME_RAWMASK, 1+1, Dt1(->base), Dt1(->glref), GG_G2DISP, LJ_TFALSE, DISPATCH_GL(vmstate), ~LJ_VMST_INTERP, LUA_MINSTACK);

1751. #line 587 "vm_x86.dasc"

1752. //|  jmp >1

1753. //|

1754. //|->vm_growstack_f:                        // Grow stack for fixarg Lua function.

1755. //|  // BASE = new base, RD = nargs+1, RB = L, PC = first PC

1756. //|  lea RD, [BASE+NARGS:RD*8-8]

1757. //|1:

1758. //|  movzx RA, byte [PC-4+PC2PROTO(framesize)]

1759. //|  add PC, 4                                // Must point after first instruction.

1760. //|  mov L:RB->base, BASE

1761. //|  mov L:RB->top, RD

1762. //|  mov SAVE_PC, PC

1763. //|  mov FCARG2, RA

1764. //|2:

1765. //|  // RB = L, L->base = new base, L->top = top

1766. //|  mov FCARG1, L:RB

1767. //|  call extern lj_state_growstack@8        // (lua_State *L, int n)

1768. //|  mov BASE, L:RB->base

1769. //|  mov RD, L:RB->top

1770. //|  mov LFUNC:RB, [BASE-8]

1771. //|  sub RD, BASE

1772. //|  shr RD, 3

1773. //|  add NARGS:RD, 1

1774. //|  // BASE = new base, RB = LFUNC, RD = nargs+1

1775. //|  ins_callt                                // Just retry the call.

1776. //|

1777. //|//-----------------------------------------------------------------------

1778. //|//-- Entry points into the assembler VM ---------------------------------

1779. //|//-----------------------------------------------------------------------

1780. //|

1781. //|->vm_resume:                                // Setup C frame and resume thread.

1782. //|  // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)

1783. //|  saveregs

1784. //|.if X64

1785. //|  mov L:RB, CARG1d                        // Caveat: CARG1d may be RA.

1786. //|  mov SAVE_L, CARG1d

1787. //|  mov RA, CARG2d

1788. //|.else

1789. //|  mov L:RB, SAVE_L

1790. //|  mov RA, INARG_BASE                        // Caveat: overlaps SAVE_CFRAME!

1791. //|.endif

1792. //|  mov PC, FRAME_CP

1793. //|  xor RD, RD

1794. //|  lea KBASEa, [esp+CFRAME_RESUME]

1795. //|  mov DISPATCH, L:RB->glref                // Setup pointer to dispatch table.

1796. //|  add DISPATCH, GG_G2DISP

1797. //|  mov SAVE_PC, RD                        // Any value outside of bytecode is ok.

1798. //|  mov SAVE_CFRAME, RDa

1799. //|.if X64

1800. //|  mov SAVE_NRES, RD

1801. //|  mov SAVE_ERRF, RD

1802. //|.endif

1803. //|  mov L:RB->cframe, KBASEa

1804. //|  cmp byte L:RB->status, RDL

1805. //|  je >2                                // Initial resume (like a call).

1806. //|

1807. //|  // Resume after yield (like a return).

1808. //|  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB

1809. dasm_put(Dst, 371, -4+PC2PROTO(framesize), Dt1(->base), Dt1(->top), Dt1(->base), Dt1(->top), Dt7(->pc), FRAME_CP, CFRAME_RESUME, Dt1(->glref), GG_G2DISP, Dt1(->cframe), Dt1(->status));

1810. #line 644 "vm_x86.dasc"

1811. //|  set_vmstate INTERP

1812. //|  mov byte L:RB->status, RDL

1813. //|  mov BASE, L:RB->base

1814. //|  mov RD, L:RB->top

1815. //|  sub RD, RA

1816. //|  shr RD, 3

1817. //|  add RD, 1                                // RD = nresults+1

1818. //|  sub RA, BASE                        // RA = resultofs

1819. //|  mov PC, [BASE-4]

1820. //|  mov MULTRES, RD

1821. //|  test PC, FRAME_TYPE

1822. //|  jz ->BC_RET_Z

1823. //|  jmp ->vm_return

1824. //|

1825. //|->vm_pcall:                                // Setup protected C frame and enter VM.

1826. //|  // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)

1827. //|  saveregs

1828. //|  mov PC, FRAME_CP

1829. //|.if X64

1830. //|  mov SAVE_ERRF, CARG4d

1831. //|.endif

1832. //|  jmp >1

1833. //|

1834. //|->vm_call:                                // Setup C frame and enter VM.

1835. //|  // (lua_State *L, TValue *base, int nres1)

1836. //|  saveregs

1837. //|  mov PC, FRAME_C

1838. //|

1839. //|1:  // Entry point for vm_pcall above (PC = ftype).

1840. //|.if X64

1841. //|  mov SAVE_NRES, CARG3d

1842. //|  mov L:RB, CARG1d                        // Caveat: CARG1d may be RA.

1843. //|  mov SAVE_L, CARG1d

1844. //|  mov RA, CARG2d

1845. //|.else

1846. //|  mov L:RB, SAVE_L

1847. //|  mov RA, INARG_BASE                        // Caveat: overlaps SAVE_CFRAME!

1848. //|.endif

1849. //|

1850. //|  mov DISPATCH, L:RB->glref                // Setup pointer to dispatch table.

1851. //|  mov KBASEa, L:RB->cframe                // Add our C frame to cframe chain.

1852. //|  mov SAVE_CFRAME, KBASEa

1853. dasm_put(Dst, 524, DISPATCH_GL(cur_L), DISPATCH_GL(vmstate), ~LJ_VMST_INTERP, Dt1(->status), Dt1(->base), Dt1(->top), FRAME_TYPE, FRAME_CP, FRAME_C, Dt1(->glref), Dt1(->cframe));

1854. #line 686 "vm_x86.dasc"

1855. //|  mov SAVE_PC, L:RB                        // Any value outside of bytecode is ok.

1856. //|  add DISPATCH, GG_G2DISP

1857. //|.if X64

1858. //|  mov L:RB->cframe, rsp

1859. //|.else

1860. //|  mov L:RB->cframe, esp

1861. //|.endif

1862. //|

1863. //|2:  // Entry point for vm_resume/vm_cpcall (RA = base, RB = L, PC = ftype).

1864. //|  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB

1865. //|  set_vmstate INTERP

1866. //|  mov BASE, L:RB->base                // BASE = old base (used in vmeta_call).

1867. //|  add PC, RA

1868. //|  sub PC, BASE                        // PC = frame delta + frame type

1869. //|

1870. //|  mov RD, L:RB->top

1871. //|  sub RD, RA

1872. //|  shr NARGS:RD, 3

1873. //|  add NARGS:RD, 1                        // RD = nargs+1

1874. //|

1875. //|->vm_call_dispatch:

1876. //|  mov LFUNC:RB, [RA-8]

1877. //|  cmp dword [RA-4], LJ_TFUNC

1878. //|  jne ->vmeta_call                        // Ensure KBASE defined and != BASE.

1879. //|

1880. //|->vm_call_dispatch_f:

1881. //|  mov BASE, RA

1882. //|  ins_call

1883. //|  // BASE = new base, RB = func, RD = nargs+1, PC = caller PC

1884. //|

1885. //|->vm_cpcall:                                // Setup protected C frame, call C.

1886. //|  // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)

1887. //|  saveregs

1888. //|.if X64

1889. //|  mov L:RB, CARG1d                        // Caveat: CARG1d may be RA.

1890. //|  mov SAVE_L, CARG1d

1891. //|.else

1892. //|  mov L:RB, SAVE_L

1893. //|  // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap!

1894. //|  mov RC, INARG_CP_UD                // Get args before they are overwritten.

1895. //|  mov RA, INARG_CP_FUNC

1896. //|  mov BASE, INARG_CP_CALL

1897. //|.endif

1898. //|  mov SAVE_PC, L:RB                        // Any value outside of bytecode is ok.

1899. //|

1900. //|  mov KBASE, L:RB->stack                // Compute -savestack(L, L->top).

1901. //|  sub KBASE, L:RB->top

1902. //|   mov DISPATCH, L:RB->glref                // Setup pointer to dispatch table.

1903. //|  mov SAVE_ERRF, 0                        // No error function.

1904. //|  mov SAVE_NRES, KBASE                // Neg. delta means cframe w/o frame.

1905. //|   add DISPATCH, GG_G2DISP

1906. //|  // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).

1907. //|

1908. //|.if X64

1909. //|  mov KBASEa, L:RB->cframe                // Add our C frame to cframe chain.

1910. //|  mov SAVE_CFRAME, KBASEa

1911. dasm_put(Dst, 635, GG_G2DISP, Dt1(->cframe), DISPATCH_GL(cur_L), DISPATCH_GL(vmstate), ~LJ_VMST_INTERP, Dt1(->base), Dt1(->top), LJ_TFUNC, Dt7(->pc), Dt1(->stack), Dt1(->top), Dt1(->glref), GG_G2DISP, Dt1(->cframe));

1912. #line 742 "vm_x86.dasc"

1913. //|  mov L:RB->cframe, rsp

1914. //|  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB

1915. //|

1916. //|  call CARG4                        // (lua_State *L, lua_CFunction func, void *ud)

1917. //|.else

1918. //|  mov ARG3, RC                        // Have to copy args downwards.

1919. //|  mov ARG2, RA

1920. //|  mov ARG1, L:RB

1921. //|

1922. //|  mov KBASE, L:RB->cframe                // Add our C frame to cframe chain.

1923. //|  mov SAVE_CFRAME, KBASE

1924. //|  mov L:RB->cframe, esp

1925. //|  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB

1926. //|

1927. //|  call BASE                        // (lua_State *L, lua_CFunction func, void *ud)

1928. //|.endif

1929. //|  // TValue * (new base) or NULL returned in eax (RC).

1930. //|  test RC, RC

1931. //|  jz ->vm_leave_cp                        // No base? Just remove C frame.

1932. //|  mov RA, RC

1933. //|  mov PC, FRAME_CP

1934. //|  jmp <2                                // Else continue with the call.

1935. //|

1936. //|//-----------------------------------------------------------------------

1937. //|//-- Metamethod handling ------------------------------------------------

1938. //|//-----------------------------------------------------------------------

1939. //|

1940. //|//-- Continuation dispatch ----------------------------------------------

1941. //|

1942. //|->cont_dispatch:

1943. //|  // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)

1944. //|  add RA, BASE

1945. //|  and PC, -8

1946. //|  mov RB, BASE

1947. //|  sub BASE, PC                        // Restore caller BASE.

1948. //|  mov dword [RA+RD*8-4], LJ_TNIL        // Ensure one valid arg.

1949. //|  mov RC, RA                                // ... in [RC]

1950. //|  mov PC, [RB-12]                        // Restore PC from [cont|PC].

1951. //|.if X64

1952. //|  movsxd RAa, dword [RB-16]                // May be negative on WIN64 with debug.

1953. //|.if FFI

1954. //|  cmp RA, 1

1955. //|  jbe >1

1956. //|.endif

1957. //|  lea KBASEa, qword [=>0]

1958. //|  add RAa, KBASEa

1959. //|.else

1960. //|  mov RA, dword [RB-16]

1961. //|.if FFI

1962. //|  cmp RA, 1

1963. //|  jbe >1

1964. //|.endif

1965. //|.endif

1966. //|  mov LFUNC:KBASE, [BASE-8]

1967. //|  mov KBASE, LFUNC:KBASE->pc

1968. //|  mov KBASE, [KBASE+PC2PROTO(k)]

1969. //|  // BASE = base, RC = result, RB = meta base

1970. //|  jmp RAa                                // Jump to continuation.

1971. //|

1972. //|.if FFI

1973. //|1:

1974. //|  je ->cont_ffi_callback                // cont = 1: return from FFI callback.

1975. //|  // cont = 0: Tail call from C function.

1976. //|  sub RB, BASE

1977. //|  shr RB, 3

1978. //|  lea RD, [RB-1]

1979. //|  jmp ->vm_call_tail

1980. //|.endif

1981. //|

1982. //|->cont_cat:                                // BASE = base, RC = result, RB = mbase

1983. //|  movzx RA, PC_RB

1984. dasm_put(Dst, 784, Dt1(->cframe), DISPATCH_GL(cur_L), FRAME_CP, LJ_TNIL, 0, Dt7(->pc), PC2PROTO(k));

1985. #line 813 "vm_x86.dasc"

1986. //|  sub RB, 16

1987. //|  lea RA, [BASE+RA*8]

1988. //|  sub RA, RB

1989. //|  je ->cont_ra

1990. //|  neg RA

1991. //|  shr RA, 3

1992. //|.if X64WIN

1993. //|  mov CARG3d, RA

1994. //|  mov L:CARG1d, SAVE_L

1995. //|  mov L:CARG1d->base, BASE

1996. //|  mov RCa, [RC]

1997. //|  mov [RB], RCa

1998. //|  mov CARG2d, RB

1999. //|.elif X64

2000. //|  mov L:CARG1d, SAVE_L

2001. //|  mov L:CARG1d->base, BASE

2002. //|  mov CARG3d, RA

2003. //|  mov RAa, [RC]

2004. //|  mov [RB], RAa

2005. //|  mov CARG2d, RB

2006. //|.else

2007. //|  mov ARG3, RA

2008. //|  mov RA, [RC+4]

2009. //|  mov RC, [RC]

2010. //|  mov [RB+4], RA

2011. //|  mov [RB], RC

2012. //|  mov ARG2, RB

2013. //|.endif

2014. //|  jmp ->BC_CAT_Z

2015. //|

2016. //|//-- Table indexing metamethods -----------------------------------------

2017. //|

2018. //|->vmeta_tgets:

2019. //|  mov TMP1, RC                        // RC = GCstr *

2020. //|  mov TMP2, LJ_TSTR

2021. //|  lea RCa, TMP1                        // Store temp. TValue in TMP1/TMP2.

2022. //|  cmp PC_OP, BC_GGET

2023. //|  jne >1

2024. //|  lea RA, [DISPATCH+DISPATCH_GL(tmptv)]  // Store fn->l.env in g->tmptv.

2025. //|  mov [RA], TAB:RB                        // RB = GCtab *

2026. //|  mov dword [RA+4], LJ_TTAB

2027. //|  mov RB, RA

2028. //|  jmp >2

2029. //|

2030. //|->vmeta_tgetb:

2031. //|  movzx RC, PC_RC

2032. //|.if DUALNUM

2033. //|  mov TMP2, LJ_TISNUM

2034. //|  mov TMP1, RC

2035. //|.else

2036. //|  cvtsi2sd xmm0, RC

2037. //|  movsd TMPQ, xmm0

2038. //|.endif

2039. //|  lea RCa, TMPQ                        // Store temp. TValue in TMPQ.

2040. //|  jmp >1

2041. //|

2042. //|->vmeta_tgetv:

2043. //|  movzx RC, PC_RC                        // Reload TValue *k from RC.

2044. //|  lea RC, [BASE+RC*8]

2045. //|1:

2046. //|  movzx RB, PC_RB                        // Reload TValue *t from RB.

2047. dasm_put(Dst, 898, Dt1(->base), LJ_TSTR, BC_GGET, DISPATCH_GL(tmptv), LJ_TTAB, LJ_TISNUM);

2048. #line 874 "vm_x86.dasc"

2049. //|  lea RB, [BASE+RB*8]

2050. //|2:

2051. //|.if X64

2052. //|  mov L:CARG1d, SAVE_L

2053. //|  mov L:CARG1d->base, BASE                // Caveat: CARG2d/CARG3d may be BASE.

2054. //|  mov CARG2d, RB

2055. //|  mov CARG3, RCa                        // May be 64 bit ptr to stack.

2056. //|  mov L:RB, L:CARG1d

2057. //|.else

2058. //|  mov ARG2, RB

2059. //|  mov L:RB, SAVE_L

2060. //|  mov ARG3, RC

2061. //|  mov ARG1, L:RB

2062. //|  mov L:RB->base, BASE

2063. //|.endif

2064. //|  mov SAVE_PC, PC

2065. //|  call extern lj_meta_tget                // (lua_State *L, TValue *o, TValue *k)

2066. //|  // TValue * (finished) or NULL (metamethod) returned in eax (RC).

2067. //|  mov BASE, L:RB->base

2068. //|  test RC, RC

2069. //|  jz >3

2070. //|->cont_ra:                                // BASE = base, RC = result

2071. //|  movzx RA, PC_RA

2072. //|.if X64

2073. //|  mov RBa, [RC]

2074. //|  mov [BASE+RA*8], RBa

2075. //|.else

2076. //|  mov RB, [RC+4]

2077. //|  mov RC, [RC]

2078. //|  mov [BASE+RA*8+4], RB

2079. //|  mov [BASE+RA*8], RC

2080. //|.endif

2081. //|  ins_next

2082. //|

2083. //|3:  // Call __index metamethod.

2084. //|  // BASE = base, L->top = new base, stack = cont/func/t/k

2085. //|  mov RA, L:RB->top

2086. //|  mov [RA-12], PC                        // [cont|PC]

2087. //|  lea PC, [RA+FRAME_CONT]

2088. //|  sub PC, BASE

2089. //|  mov LFUNC:RB, [RA-8]                // Guaranteed to be a function here.

2090. //|  mov NARGS:RD, 2+1                        // 2 args for func(t, k).

2091. //|  jmp ->vm_call_dispatch_f

2092. //|

2093. //|->vmeta_tgetr:

2094. //|  mov FCARG1, TAB:RB

2095. //|  mov RB, BASE                        // Save BASE.

2096. //|  mov FCARG2, RC                        // Caveat: FCARG2 == BASE

2097. //|  call extern lj_tab_getinth@8        // (GCtab *t, int32_t key)

2098. //|  // cTValue * or NULL returned in eax (RC).

2099. //|  movzx RA, PC_RA

2100. //|  mov BASE, RB                        // Restore BASE.

2101. //|  test RC, RC

2102. //|  jnz ->BC_TGETR_Z

2103. //|  mov dword [BASE+RA*8+4], LJ_TNIL

2104. //|  jmp ->BC_TGETR2_Z

2105. //|

2106. //|//-----------------------------------------------------------------------

2107. //|

2108. //|->vmeta_tsets:

2109. //|  mov TMP1, RC                        // RC = GCstr *

2110. //|  mov TMP2, LJ_TSTR

2111. //|  lea RCa, TMP1                        // Store temp. TValue in TMP1/TMP2.

2112. dasm_put(Dst, 1022, Dt1(->base), Dt1(->base), Dt1(->top), FRAME_CONT, 2+1, LJ_TNIL, LJ_TSTR);

2113. #line 937 "vm_x86.dasc"

2114. //|  cmp PC_OP, BC_GSET

2115. //|  jne >1

2116. //|  lea RA, [DISPATCH+DISPATCH_GL(tmptv)]  // Store fn->l.env in g->tmptv.

2117. //|  mov [RA], TAB:RB                        // RB = GCtab *

2118. //|  mov dword [RA+4], LJ_TTAB

2119. //|  mov RB, RA

2120. //|  jmp >2

2121. //|

2122. //|->vmeta_tsetb:

2123. //|  movzx RC, PC_RC

2124. //|.if DUALNUM

2125. //|  mov TMP2, LJ_TISNUM

2126. //|  mov TMP1, RC

2127. //|.else

2128. //|  cvtsi2sd xmm0, RC

2129. //|  movsd TMPQ, xmm0

2130. //|.endif

2131. //|  lea RCa, TMPQ                        // Store temp. TValue in TMPQ.

2132. //|  jmp >1

2133. //|

2134. //|->vmeta_tsetv:

2135. //|  movzx RC, PC_RC                        // Reload TValue *k from RC.

2136. //|  lea RC, [BASE+RC*8]

2137. //|1:

2138. //|  movzx RB, PC_RB                        // Reload TValue *t from RB.

2139. //|  lea RB, [BASE+RB*8]

2140. //|2:

2141. //|.if X64

2142. //|  mov L:CARG1d, SAVE_L

2143. //|  mov L:CARG1d->base, BASE                // Caveat: CARG2d/CARG3d may be BASE.

2144. //|  mov CARG2d, RB

2145. //|  mov CARG3, RCa                        // May be 64 bit ptr to stack.

2146. //|  mov L:RB, L:CARG1d

2147. //|.else

2148. //|  mov ARG2, RB

2149. //|  mov L:RB, SAVE_L

2150. //|  mov ARG3, RC

2151. //|  mov ARG1, L:RB

2152. //|  mov L:RB->base, BASE

2153. //|.endif

2154. //|  mov SAVE_PC, PC

2155. //|  call extern lj_meta_tset                // (lua_State *L, TValue *o, TValue *k)

2156. //|  // TValue * (finished) or NULL (metamethod) returned in eax (RC).

2157. //|  mov BASE, L:RB->base

2158. //|  test RC, RC

2159. //|  jz >3

2160. //|  // NOBARRIER: lj_meta_tset ensures the table is not black.

2161. //|  movzx RA, PC_RA

2162. //|.if X64

2163. //|  mov RBa, [BASE+RA*8]

2164. //|  mov [RC], RBa

2165. //|.else

2166. //|  mov RB, [BASE+RA*8+4]

2167. //|  mov RA, [BASE+RA*8]

2168. //|  mov [RC+4], RB

2169. //|  mov [RC], RA

2170. //|.endif

2171. //|->cont_nop:                                // BASE = base, (RC = result)

2172. //|  ins_next

2173. //|

2174. //|3:  // Call __newindex metamethod.

2175. //|  // BASE = base, L->top = new base, stack = cont/func/t/k/(v)

2176. //|  mov RA, L:RB->top

2177. dasm_put(Dst, 1171, BC_GSET, DISPATCH_GL(tmptv), LJ_TTAB, LJ_TISNUM, Dt1(->base), Dt1(->base));

2178. #line 1000 "vm_x86.dasc"

2179. //|  mov [RA-12], PC                        // [cont|PC]

2180. //|  movzx RC, PC_RA

2181. //|  // Copy value to third argument.

2182. //|.if X64

2183. //|  mov RBa, [BASE+RC*8]

2184. //|  mov [RA+16], RBa

2185. //|.else

2186. //|  mov RB, [BASE+RC*8+4]

2187. //|  mov RC, [BASE+RC*8]

2188. //|  mov [RA+20], RB

2189. //|  mov [RA+16], RC

2190. //|.endif

2191. //|  lea PC, [RA+FRAME_CONT]

2192. //|  sub PC, BASE

2193. //|  mov LFUNC:RB, [RA-8]                // Guaranteed to be a function here.

2194. //|  mov NARGS:RD, 3+1                        // 3 args for func(t, k, v).

2195. //|  jmp ->vm_call_dispatch_f

2196. //|

2197. //|->vmeta_tsetr:

2198. //|.if X64WIN

2199. //|  mov L:CARG1d, SAVE_L

2200. //|  mov CARG3d, RC

2201. //|  mov L:CARG1d->base, BASE

2202. //|  xchg CARG2d, TAB:RB                // Caveat: CARG2d == BASE.

2203. //|.elif X64

2204. //|  mov L:CARG1d, SAVE_L

2205. //|  mov CARG2d, TAB:RB

2206. //|  mov L:CARG1d->base, BASE

2207. //|  mov RB, BASE                        // Save BASE.

2208. //|  mov CARG3d, RC                        // Caveat: CARG3d == BASE.

2209. //|.else

2210. //|  mov L:RA, SAVE_L

2211. //|  mov ARG2, TAB:RB

2212. //|  mov RB, BASE                        // Save BASE.

2213. //|  mov ARG3, RC

2214. //|  mov ARG1, L:RA

2215. //|  mov L:RA->base, BASE

2216. //|.endif

2217. //|  mov SAVE_PC, PC

2218. //|  call extern lj_tab_setinth  // (lua_State *L, GCtab *t, int32_t key)

2219. //|  // TValue * returned in eax (RC).

2220. //|  movzx RA, PC_RA

2221. //|  mov BASE, RB                        // Restore BASE.

2222. //|  jmp ->BC_TSETR_Z

2223. //|

2224. //|//-- Comparison metamethods ---------------------------------------------

2225. //|

2226. //|->vmeta_comp:

2227. //|.if X64

2228. //|  mov L:RB, SAVE_L

2229. //|  mov L:RB->base, BASE                // Caveat: CARG2d/CARG3d == BASE.

2230. //|.if X64WIN

2231. //|  lea CARG3d, [BASE+RD*8]

2232. //|  lea CARG2d, [BASE+RA*8]

2233. //|.else

2234. //|  lea CARG2d, [BASE+RA*8]

2235. //|  lea CARG3d, [BASE+RD*8]

2236. //|.endif

2237. //|  mov CARG1d, L:RB                        // Caveat: CARG1d/CARG4d == RA.

2238. //|  movzx CARG4d, PC_OP

2239. //|.else

2240. //|  movzx RB, PC_OP

2241. //|  lea RD, [BASE+RD*8]

2242. //|  lea RA, [BASE+RA*8]

2243. //|  mov ARG4, RB

2244. //|  mov L:RB, SAVE_L

2245. //|  mov ARG3, RD

2246. //|  mov ARG2, RA

2247. //|  mov ARG1, L:RB

2248. //|  mov L:RB->base, BASE

2249. //|.endif

2250. //|  mov SAVE_PC, PC

2251. //|  call extern lj_meta_comp        // (lua_State *L, TValue *o1, *o2, int op)

2252. //|  // 0/1 or TValue * (metamethod) returned in eax (RC).

2253. //|3:

2254. //|  mov BASE, L:RB->base

2255. //|  cmp RC, 1

2256. //|  ja ->vmeta_binop

2257. //|4:

2258. //|  lea PC, [PC+4]

2259. //|  jb >6

2260. //|5:

2261. //|  movzx RD, PC_RD

2262. //|  branchPC RD

2263. //|6:

2264. //|  ins_next

2265. dasm_put(Dst, 1316, Dt1(->top), FRAME_CONT, 3+1, Dt1(->base), Dt1(->base), Dt1(->base), -BCBIAS_J*4);

2266. #line 1086 "vm_x86.dasc"

2267. //|

2268. //|->cont_condt:                        // BASE = base, RC = result

2269. //|  add PC, 4

2270. //|  cmp dword [RC+4], LJ_TISTRUECOND        // Branch if result is true.

2271. //|  jb <5

2272. //|  jmp <6

2273. //|

2274. //|->cont_condf:                        // BASE = base, RC = result

2275. //|  cmp dword [RC+4], LJ_TISTRUECOND        // Branch if result is false.

2276. //|  jmp <4

2277. //|

2278. //|->vmeta_equal:

2279. //|  sub PC, 4

2280. //|.if X64WIN

2281. //|  mov CARG3d, RD

2282. //|  mov CARG4d, RB

2283. //|  mov L:RB, SAVE_L

2284. //|  mov L:RB->base, BASE                // Caveat: CARG2d == BASE.

2285. //|  mov CARG2d, RA

2286. //|  mov CARG1d, L:RB                        // Caveat: CARG1d == RA.

2287. //|.elif X64

2288. //|  mov CARG2d, RA

2289. //|  mov CARG4d, RB                        // Caveat: CARG4d == RA.

2290. //|  mov L:RB, SAVE_L

2291. //|  mov L:RB->base, BASE                // Caveat: CARG3d == BASE.

2292. //|  mov CARG3d, RD

2293. //|  mov CARG1d, L:RB

2294. //|.else

2295. //|  mov ARG4, RB

2296. //|  mov L:RB, SAVE_L

2297. //|  mov ARG3, RD

2298. //|  mov ARG2, RA

2299. //|  mov ARG1, L:RB

2300. //|  mov L:RB->base, BASE

2301. //|.endif

2302. //|  mov SAVE_PC, PC

2303. //|  call extern lj_meta_equal        // (lua_State *L, GCobj *o1, *o2, int ne)

2304. //|  // 0/1 or TValue * (metamethod) returned in eax (RC).

2305. //|  jmp <3

2306. //|

2307. //|->vmeta_equal_cd:

2308. //|.if FFI

2309. //|  sub PC, 4

2310. //|  mov L:RB, SAVE_L

2311. //|  mov L:RB->base, BASE

2312. //|  mov FCARG1, L:RB

2313. //|  mov FCARG2, dword [PC-4]

2314. //|  mov SAVE_PC, PC

2315. //|  call extern lj_meta_equal_cd@8        // (lua_State *L, BCIns ins)

2316. //|  // 0/1 or TValue * (metamethod) returned in eax (RC).

2317. //|  jmp <3

2318. //|.endif

2319. //|

2320. //|->vmeta_istype:

2321. //|.if X64

2322. //|  mov L:RB, SAVE_L

2323. //|  mov L:RB->base, BASE                // Caveat: CARG2d/CARG3d may be BASE.

2324. //|  mov CARG2d, RA

2325. dasm_put(Dst, 1455, LJ_TISTRUECOND, LJ_TISTRUECOND, Dt1(->base), Dt1(->base), Dt1(->base));

2326. #line 1144 "vm_x86.dasc"

2327. //|  movzx CARG3d, PC_RD

2328. //|  mov L:CARG1d, L:RB

2329. //|.else

2330. //|  movzx RD, PC_RD

2331. //|  mov ARG2, RA

2332. //|  mov L:RB, SAVE_L

2333. //|  mov ARG3, RD

2334. //|  mov ARG1, L:RB

2335. //|  mov L:RB->base, BASE

2336. //|.endif

2337. //|  mov SAVE_PC, PC

2338. //|  call extern lj_meta_istype  // (lua_State *L, BCReg ra, BCReg tp)

2339. //|  mov BASE, L:RB->base

2340. //|  jmp <6

2341. //|

2342. //|//-- Arithmetic metamethods ---------------------------------------------

2343. //|

2344. //|->vmeta_arith_vno:

2345. //|.if DUALNUM

2346. //|  movzx RB, PC_RB

2347. //|.endif

2348. //|->vmeta_arith_vn:

2349. //|  lea RC, [KBASE+RC*8]

2350. //|  jmp >1

2351. //|

2352. //|->vmeta_arith_nvo:

2353. //|.if DUALNUM

2354. //|  movzx RC, PC_RC

2355. //|.endif

2356. //|->vmeta_arith_nv:

2357. //|  lea RC, [KBASE+RC*8]

2358. //|  lea RB, [BASE+RB*8]

2359. //|  xchg RB, RC

2360. //|  jmp >2

2361. //|

2362. //|->vmeta_unm:

2363. //|  lea RC, [BASE+RD*8]

2364. //|  mov RB, RC

2365. //|  jmp >2

2366. //|

2367. //|->vmeta_arith_vvo:

2368. //|.if DUALNUM

2369. //|  movzx RB, PC_RB

2370. //|.endif

2371. //|->vmeta_arith_vv:

2372. //|  lea RC, [BASE+RC*8]

2373. //|1:

2374. //|  lea RB, [BASE+RB*8]

2375. //|2:

2376. //|  lea RA, [BASE+RA*8]

2377. //|.if X64WIN

2378. //|  mov CARG3d, RB

2379. //|  mov CARG4d, RC

2380. //|  movzx RC, PC_OP

2381. //|  mov ARG5d, RC

2382. //|  mov L:RB, SAVE_L

2383. //|  mov L:RB->base, BASE                // Caveat: CARG2d == BASE.

2384. //|  mov CARG2d, RA

2385. //|  mov CARG1d, L:RB                        // Caveat: CARG1d == RA.

2386. //|.elif X64

2387. //|  movzx CARG5d, PC_OP

2388. //|  mov CARG2d, RA

2389. //|  mov CARG4d, RC                        // Caveat: CARG4d == RA.

2390. //|  mov L:CARG1d, SAVE_L

2391. //|  mov L:CARG1d->base, BASE                // Caveat: CARG3d == BASE.

2392. //|  mov CARG3d, RB

2393. //|  mov L:RB, L:CARG1d

2394. //|.else

2395. //|  mov ARG3, RB

2396. //|  mov L:RB, SAVE_L

2397. //|  mov ARG4, RC

2398. //|  movzx RC, PC_OP

2399. //|  mov ARG2, RA

2400. //|  mov ARG5, RC

2401. //|  mov ARG1, L:RB

2402. //|  mov L:RB->base, BASE

2403. //|.endif

2404. //|  mov SAVE_PC, PC

2405. //|  call extern lj_meta_arith        // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)

2406. //|  // NULL (finished) or TValue * (metamethod) returned in eax (RC).

2407. //|  mov BASE, L:RB->base

2408. //|  test RC, RC

2409. dasm_put(Dst, 1581, Dt1(->base), Dt1(->base), Dt1(->base));

2410. #line 1226 "vm_x86.dasc"

2411. //|  jz ->cont_nop

2412. //|

2413. //|  // Call metamethod for binary op.

2414. //|->vmeta_binop:

2415. //|  // BASE = base, RC = new base, stack = cont/func/o1/o2

2416. //|  mov RA, RC

2417. //|  sub RC, BASE

2418. //|  mov [RA-12], PC                        // [cont|PC]

2419. //|  lea PC, [RC+FRAME_CONT]

2420. //|  mov NARGS:RD, 2+1                        // 2 args for func(o1, o2).

2421. //|  jmp ->vm_call_dispatch

2422. //|

2423. //|->vmeta_len:

2424. //|  mov L:RB, SAVE_L

2425. //|  mov L:RB->base, BASE

2426. //|  lea FCARG2, [BASE+RD*8]                // Caveat: FCARG2 == BASE

2427. //|  mov L:FCARG1, L:RB

2428. //|  mov SAVE_PC, PC

2429. //|  call extern lj_meta_len@8                // (lua_State *L, TValue *o)

2430. //|  // NULL (retry) or TValue * (metamethod) returned in eax (RC).

2431. //|  mov BASE, L:RB->base

2432. dasm_put(Dst, 1714, FRAME_CONT, 2+1, Dt1(->base), Dt1(->base));

2433. #line 1247 "vm_x86.dasc"

2434. #if LJ_52

2435. //|  test RC, RC

2436. //|  jne ->vmeta_binop                        // Binop call for compatibility.

2437. //|  movzx RD, PC_RD

2438. //|  mov TAB:FCARG1, [BASE+RD*8]

2439. //|  jmp ->BC_LEN_Z

2440. dasm_put(Dst, 1766);

2441. #line 1253 "vm_x86.dasc"

2442. #else

2443. //|  jmp ->vmeta_binop                        // Binop call for compatibility.

2444. dasm_put(Dst, 1785);

2445. #line 1255 "vm_x86.dasc"

2446. #endif

2447. //|

2448. //|//-- Call metamethod ----------------------------------------------------

2449. //|

2450. //|->vmeta_call_ra:

2451. //|  lea RA, [BASE+RA*8+8]

2452. //|->vmeta_call:                        // Resolve and call __call metamethod.

2453. //|  // BASE = old base, RA = new base, RC = nargs+1, PC = return

2454. //|  mov TMP2, RA                        // Save RA, RC for us.

2455. //|  mov TMP1, NARGS:RD

2456. //|  sub RA, 8

2457. //|.if X64

2458. //|  mov L:RB, SAVE_L

2459. //|  mov L:RB->base, BASE                // Caveat: CARG2d/CARG3d may be BASE.

2460. //|  mov CARG2d, RA

2461. //|  lea CARG3d, [RA+NARGS:RD*8]

2462. //|  mov CARG1d, L:RB                        // Caveat: CARG1d may be RA.

2463. //|.else

2464. //|  lea RC, [RA+NARGS:RD*8]

2465. //|  mov L:RB, SAVE_L

2466. //|  mov ARG2, RA

2467. //|  mov ARG3, RC

2468. //|  mov ARG1, L:RB

2469. //|  mov L:RB->base, BASE                // This is the callers base!

2470. //|.endif

2471. //|  mov SAVE_PC, PC

2472. //|  call extern lj_meta_call        // (lua_State *L, TValue *func, TValue *top)

2473. //|  mov BASE, L:RB->base

2474. //|  mov RA, TMP2

2475. //|  mov NARGS:RD, TMP1

2476. //|  mov LFUNC:RB, [RA-8]

2477. //|  add NARGS:RD, 1

2478. //|  // This is fragile. L->base must not move, KBASE must always be defined.

2479. //|  cmp KBASE, BASE                        // Continue with CALLT if flag set.

2480. //|  je ->BC_CALLT_Z

2481. //|  mov BASE, RA

2482. //|  ins_call                                // Otherwise call resolved metamethod.

2483. //|

2484. //|//-- Argument coercion for 'for' statement ------------------------------

2485. //|

2486. //|->vmeta_for:

2487. //|  mov L:RB, SAVE_L

2488. //|  mov L:RB->base, BASE

2489. //|  mov FCARG2, RA                        // Caveat: FCARG2 == BASE

2490. //|  mov L:FCARG1, L:RB                        // Caveat: FCARG1 == RA

2491. //|  mov SAVE_PC, PC

2492. //|  call extern lj_meta_for@8        // (lua_State *L, TValue *base)

2493. //|  mov BASE, L:RB->base

2494. //|  mov RC, [PC-4]

2495. //|  movzx RA, RCH

2496. //|  movzx OP, RCL

2497. //|  shr RC, 16

2498. //|.if X64

2499. //|  jmp aword [DISPATCH+OP*8+GG_DISP2STATIC]        // Retry FORI or JFORI.

2500. //|.else

2501. //|  jmp aword [DISPATCH+OP*4+GG_DISP2STATIC]        // Retry FORI or JFORI.

2502. //|.endif

2503. //|

2504. //|//-----------------------------------------------------------------------

2505. //|//-- Fast functions -----------------------------------------------------

2506. //|//-----------------------------------------------------------------------

2507. //|

2508. //|.macro .ffunc, name

2509. //|->ff_ .. name:

2510. //|.endmacro

2511. //|

2512. //|.macro .ffunc_1, name

2513. //|->ff_ .. name:

2514. //|  cmp NARGS:RD, 1+1;  jb ->fff_fallback

2515. //|.endmacro

2516. //|

2517. //|.macro .ffunc_2, name

2518. //|->ff_ .. name:

2519. //|  cmp NARGS:RD, 2+1;  jb ->fff_fallback

2520. //|.endmacro

2521. //|

2522. //|.macro .ffunc_nsse, name, op

2523. //|  .ffunc_1 name

2524. //|  cmp dword [BASE+4], LJ_TISNUM;  jae ->fff_fallback

2525. //|  op xmm0, qword [BASE]

2526. //|.endmacro

2527. //|

2528. //|.macro .ffunc_nsse, name

2529. //|  .ffunc_nsse name, movsd

2530. //|.endmacro

2531. //|

2532. //|.macro .ffunc_nnsse, name

2533. //|  .ffunc_2 name

2534. //|  cmp dword [BASE+4], LJ_TISNUM;  jae ->fff_fallback

2535. //|  cmp dword [BASE+12], LJ_TISNUM;  jae ->fff_fallback

2536. //|  movsd xmm0, qword [BASE]

2537. //|  movsd xmm1, qword [BASE+8]

2538. //|.endmacro

2539. //|

2540. //|.macro .ffunc_nnr, name

2541. //|  .ffunc_2 name

2542. //|  cmp dword [BASE+4], LJ_TISNUM;  jae ->fff_fallback

2543. //|  cmp dword [BASE+12], LJ_TISNUM;  jae ->fff_fallback

2544. //|  fld qword [BASE+8]

2545. //|  fld qword [BASE]

2546. //|.endmacro

2547. //|

2548. //|// Inlined GC threshold check. Caveat: uses label 1.

2549. //|.macro ffgccheck

2550. //|  mov RB, [DISPATCH+DISPATCH_GL(gc.total)]

2551. //|  cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]

2552. //|  jb >1

2553. //|  call ->fff_gcstep

2554. //|1:

2555. //|.endmacro

2556. //|

2557. //|//-- Base library: checks -----------------------------------------------

2558. //|

2559. //|.ffunc_1 assert

2560. //|  mov RB, [BASE+4]

2561. //|  cmp RB, LJ_TISTRUECOND;  jae ->fff_fallback

2562. //|  mov PC, [BASE-4]

2563. //|  mov MULTRES, RD

2564. //|  mov [BASE-4], RB

2565. //|  mov RB, [BASE]

2566. //|  mov [BASE-8], RB

2567. //|  sub RD, 2

2568. //|  jz >2

2569. //|  mov RA, BASE

2570. dasm_put(Dst, 1790, Dt1(->base), Dt1(->base), Dt7(->pc), Dt1(->base), Dt1(->base), GG_DISP2STATIC, 1+1, LJ_TISTRUECOND);

2571. #line 1379 "vm_x86.dasc"

2572. //|1:

2573. //|  add RA, 8

2574. //|.if X64

2575. //|  mov RBa, [RA]

2576. //|  mov [RA-8], RBa

2577. //|.else

2578. //|  mov RB, [RA+4]

2579. //|  mov [RA-4], RB

2580. //|  mov RB, [RA]

2581. //|  mov [RA-8], RB

2582. //|.endif

2583. //|  sub RD, 1

2584. //|  jnz <1

2585. //|2:

2586. //|  mov RD, MULTRES

2587. //|  jmp ->fff_res_

2588. //|

2589. //|.ffunc_1 type

2590. //|  mov RB, [BASE+4]

2591. //|.if X64

2592. //|  mov RA, RB

2593. //|  sar RA, 15

2594. //|  cmp RA, -2

2595. //|  je >3

2596. //|.endif

2597. //|  mov RC, ~LJ_TNUMX

2598. //|  not RB

2599. //|  cmp RC, RB

2600. //|  cmova RC, RB

2601. //|2:

2602. //|  mov CFUNC:RB, [BASE-8]

2603. //|  mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]

2604. //|  mov PC, [BASE-4]

2605. //|  mov dword [BASE-4], LJ_TSTR

2606. //|  mov [BASE-8], STR:RC

2607. //|  jmp ->fff_res1

2608. //|.if X64

2609. //|3:

2610. //|  mov RC, ~LJ_TLIGHTUD

2611. //|  jmp <2

2612. dasm_put(Dst, 1976, 1+1, ~LJ_TNUMX, ((char *)(&((GCfuncC *)0)->upvalue)), LJ_TSTR, ~LJ_TLIGHTUD);

2613. #line 1419 "vm_x86.dasc"

2614. //|.endif

2615. //|

2616. //|//-- Base library: getters and setters ---------------------------------

2617. //|

2618. //|.ffunc_1 getmetatable

2619. //|  mov RB, [BASE+4]

2620. //|  mov PC, [BASE-4]

2621. //|  cmp RB, LJ_TTAB;  jne >6

2622. //|1:  // Field metatable must be at same offset for GCtab and GCudata!

2623. //|  mov TAB:RB, [BASE]

2624. //|  mov TAB:RB, TAB:RB->metatable

2625. //|2:

2626. //|  test TAB:RB, TAB:RB

2627. //|  mov dword [BASE-4], LJ_TNIL

2628. //|  jz ->fff_res1

2629. //|  mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)]

2630. //|  mov dword [BASE-4], LJ_TTAB        // Store metatable as default result.

2631. //|  mov [BASE-8], TAB:RB

2632. //|  mov RA, TAB:RB->hmask

2633. //|  and RA, STR:RC->hash

2634. //|  imul RA, #NODE

2635. //|  add NODE:RA, TAB:RB->node

2636. dasm_put(Dst, 2080, 1+1, LJ_TTAB, Dt6(->metatable), LJ_TNIL, DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable), LJ_TTAB, Dt6(->hmask), Dt5(->hash), sizeof(Node));

2637. #line 1441 "vm_x86.dasc"

2638. //|3:  // Rearranged logic, because we expect _not_ to find the key.

2639. //|  cmp dword NODE:RA->key.it, LJ_TSTR

2640. //|  jne >4

2641. //|  cmp dword NODE:RA->key.gcr, STR:RC

2642. //|  je >5

2643. //|4:

2644. //|  mov NODE:RA, NODE:RA->next

2645. //|  test NODE:RA, NODE:RA

2646. //|  jnz <3

2647. //|  jmp ->fff_res1                        // Not found, keep default result.

2648. //|5:

2649. //|  mov RB, [RA+4]

2650. //|  cmp RB, LJ_TNIL;  je ->fff_res1        // Ditto for nil value.

2651. //|  mov RC, [RA]

2652. //|  mov [BASE-4], RB                        // Return value of mt.__metatable.

2653. //|  mov [BASE-8], RC

2654. //|  jmp ->fff_res1

2655. //|

2656. //|6:

2657. //|  cmp RB, LJ_TUDATA;  je <1

2658. dasm_put(Dst, 2153, Dt6(->node), DtB(->key.it), LJ_TSTR, DtB(->key.gcr), DtB(->next), LJ_TNIL);

2659. #line 1461 "vm_x86.dasc"

2660. //|.if X64

2661. //|  cmp RB, LJ_TNUMX;  ja >8

2662. //|  cmp RB, LJ_TISNUM;  jbe >7

2663. //|  mov RB, LJ_TLIGHTUD

2664. //|  jmp >8

2665. //|7:

2666. //|.else

2667. //|  cmp RB, LJ_TISNUM;  ja >8

2668. //|.endif

2669. //|  mov RB, LJ_TNUMX

2670. //|8:

2671. //|  not RB

2672. //|  mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])]

2673. //|  jmp <2

2674. //|

2675. //|.ffunc_2 setmetatable

2676. dasm_put(Dst, 2218, LJ_TUDATA, LJ_TNUMX, LJ_TISNUM, LJ_TLIGHTUD, LJ_TNUMX, DISPATCH_GL(gcroot[GCROOT_BASEMT]), 2+1);

2677. #line 1477 "vm_x86.dasc"

2678. //|  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback

2679. //|  // Fast path: no mt for table yet and not clearing the mt.

2680. //|  mov TAB:RB, [BASE]

2681. //|  cmp dword TAB:RB->metatable, 0;  jne ->fff_fallback

2682. //|  cmp dword [BASE+12], LJ_TTAB;  jne ->fff_fallback

2683. //|  mov TAB:RC, [BASE+8]

2684. //|  mov TAB:RB->metatable, TAB:RC

2685. //|  mov PC, [BASE-4]

2686. //|  mov dword [BASE-4], LJ_TTAB                // Return original table.

2687. //|  mov [BASE-8], TAB:RB

2688. //|  test byte TAB:RB->marked, LJ_GC_BLACK        // isblack(table)

2689. //|  jz >1

2690. //|  // Possible write barrier. Table is black, but skip iswhite(mt) check.

2691. //|  barrierback TAB:RB, RC

2692. dasm_put(Dst, 2274, LJ_TTAB, Dt6(->metatable), LJ_TTAB, Dt6(->metatable), LJ_TTAB, Dt6(->marked), LJ_GC_BLACK, Dt6(->marked), (uint8_t)~LJ_GC_BLACK, DISPATCH_GL(gc.grayagain));

2693. #line 1491 "vm_x86.dasc"

2694. //|1:

2695. //|  jmp ->fff_res1

2696. //|

2697. //|.ffunc_2 rawget

2698. //|  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback

2699. //|.if X64WIN

2700. //|  mov RB, BASE                        // Save BASE.

2701. //|  lea CARG3d, [BASE+8]

2702. //|  mov CARG2d, [BASE]                        // Caveat: CARG2d == BASE.

2703. //|  mov CARG1d, SAVE_L

2704. //|.elif X64

2705. //|  mov RB, BASE                        // Save BASE.

2706. //|  mov CARG2d, [BASE]

2707. //|  lea CARG3d, [BASE+8]                // Caveat: CARG3d == BASE.

2708. //|  mov CARG1d, SAVE_L

2709. //|.else

2710. //|  mov TAB:RD, [BASE]

2711. //|  mov L:RB, SAVE_L

2712. //|  mov ARG2, TAB:RD

2713. //|  mov ARG1, L:RB

2714. //|  mov RB, BASE                        // Save BASE.

2715. //|  add BASE, 8

2716. //|  mov ARG3, BASE

2717. //|.endif

2718. //|  call extern lj_tab_get        // (lua_State *L, GCtab *t, cTValue *key)

2719. //|  // cTValue * returned in eax (RD).

2720. //|  mov BASE, RB                        // Restore BASE.

2721. //|  // Copy table slot.

2722. //|.if X64

2723. //|  mov RBa, [RD]

2724. //|  mov PC, [BASE-4]

2725. //|  mov [BASE-8], RBa

2726. //|.else

2727. //|  mov RB, [RD]

2728. //|  mov RD, [RD+4]

2729. //|  mov PC, [BASE-4]

2730. //|  mov [BASE-8], RB

2731. //|  mov [BASE-4], RD

2732. //|.endif

2733. //|  jmp ->fff_res1

2734. //|

2735. //|//-- Base library: conversions ------------------------------------------

2736. //|

2737. //|.ffunc tonumber

2738. //|  // Only handles the number case inline (without a base argument).

2739. //|  cmp NARGS:RD, 1+1;  jne ->fff_fallback        // Exactly one argument.

2740. //|  cmp dword [BASE+4], LJ_TISNUM

2741. //|.if DUALNUM

2742. //|  jne >1

2743. //|  mov RB, dword [BASE]; jmp ->fff_resi

2744. dasm_put(Dst, 2343, DISPATCH_GL(gc.grayagain), Dt6(->gclist), 2+1, LJ_TTAB, 1+1, LJ_TISNUM);

2745. #line 1541 "vm_x86.dasc"

2746. //|1:

2747. //|  ja ->fff_fallback

2748. //|.else

2749. //|  jae ->fff_fallback

2750. //|.endif

2751. //|  movsd xmm0, qword [BASE]; jmp ->fff_resxmm0

2752. //|

2753. //|.ffunc_1 tostring

2754. //|  // Only handles the string or number case inline.

2755. //|  mov PC, [BASE-4]

2756. //|  cmp dword [BASE+4], LJ_TSTR;  jne >3

2757. //|  // A __tostring method in the string base metatable is ignored.

2758. //|  mov STR:RD, [BASE]

2759. //|2:

2760. //|  mov dword [BASE-4], LJ_TSTR

2761. //|  mov [BASE-8], STR:RD

2762. //|  jmp ->fff_res1

2763. //|3:  // Handle numbers inline, unless a number base metatable is present.

2764. //|  cmp dword [BASE+4], LJ_TISNUM;  ja ->fff_fallback

2765. dasm_put(Dst, 2429, 1+1, LJ_TSTR, LJ_TSTR, LJ_TISNUM);

2766. #line 1560 "vm_x86.dasc"

2767. //|  cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0

2768. //|  jne ->fff_fallback

2769. //|  ffgccheck                                // Caveat: uses label 1.

2770. //|  mov L:RB, SAVE_L

2771. //|  mov L:RB->base, BASE                // Add frame since C call can throw.

2772. //|  mov SAVE_PC, PC                        // Redundant (but a defined value).

2773. //|.if X64 and not X64WIN

2774. //|  mov FCARG2, BASE                        // Otherwise: FCARG2 == BASE

2775. //|.endif

2776. //|  mov L:FCARG1, L:RB

2777. //|.if DUALNUM

2778. //|  call extern lj_strfmt_number@8        // (lua_State *L, cTValue *o)

2779. //|.else

2780. //|  call extern lj_strfmt_num@8        // (lua_State *L, lua_Number *np)

2781. //|.endif

2782. //|  // GCstr returned in eax (RD).

2783. //|  mov BASE, L:RB->base

2784. //|  jmp <2

2785. //|

2786. //|//-- Base library: iterators -------------------------------------------

2787. //|

2788. //|.ffunc_1 next

2789. //|  je >2                                // Missing 2nd arg?

2790. dasm_put(Dst, 2498, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM]), DISPATCH_GL(gc.total), DISPATCH_GL(gc.threshold), Dt1(->base), Dt1(->base), 1+1);

2791. #line 1583 "vm_x86.dasc"

2792. //|1:

2793. //|  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback

2794. //|  mov L:RB, SAVE_L

2795. //|  mov L:RB->base, BASE                // Add frame since C call can throw.

2796. //|  mov L:RB->top, BASE                // Dummy frame length is ok.

2797. //|  mov PC, [BASE-4]

2798. //|.if X64WIN

2799. //|  lea CARG3d, [BASE+8]

2800. //|  mov CARG2d, [BASE]                        // Caveat: CARG2d == BASE.

2801. //|  mov CARG1d, L:RB

2802. //|.elif X64

2803. //|  mov CARG2d, [BASE]

2804. //|  lea CARG3d, [BASE+8]                // Caveat: CARG3d == BASE.

2805. //|  mov CARG1d, L:RB

2806. //|.else

2807. //|  mov TAB:RD, [BASE]

2808. //|  mov ARG2, TAB:RD

2809. //|  mov ARG1, L:RB

2810. //|  add BASE, 8

2811. //|  mov ARG3, BASE

2812. //|.endif

2813. //|  mov SAVE_PC, PC                        // Needed for ITERN fallback.

2814. //|  call extern lj_tab_next        // (lua_State *L, GCtab *t, TValue *key)

2815. //|  // Flag returned in eax (RD).

2816. //|  mov BASE, L:RB->base

2817. //|  test RD, RD;  jz >3                // End of traversal?

2818. //|  // Copy key and value to results.

2819. //|.if X64

2820. //|  mov RBa, [BASE+8]

2821. //|  mov RDa, [BASE+16]

2822. //|  mov [BASE-8], RBa

2823. //|  mov [BASE], RDa

2824. //|.else

2825. //|  mov RB, [BASE+8]

2826. //|  mov RD, [BASE+12]

2827. //|  mov [BASE-8], RB

2828. //|  mov [BASE-4], RD

2829. //|  mov RB, [BASE+16]

2830. //|  mov RD, [BASE+20]

2831. //|  mov [BASE], RB

2832. //|  mov [BASE+4], RD

2833. //|.endif

2834. //|->fff_res2:

2835. //|  mov RD, 1+2

2836. //|  jmp ->fff_res

2837. //|2:  // Set missing 2nd arg to nil.

2838. //|  mov dword [BASE+12], LJ_TNIL

2839. //|  jmp <1

2840. //|3:  // End of traversal: return nil.

2841. //|  mov dword [BASE-4], LJ_TNIL

2842. dasm_put(Dst, 2566, LJ_TTAB, Dt1(->base), Dt1(->top), Dt1(->base), 1+2, LJ_TNIL);

2843. #line 1633 "vm_x86.dasc"

2844. //|  jmp ->fff_res1

2845. //|

2846. //|.ffunc_1 pairs

2847. //|  mov TAB:RB, [BASE]

2848. //|  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback

2849. dasm_put(Dst, 2657, LJ_TNIL, 1+1, LJ_TTAB);

2850. #line 1638 "vm_x86.dasc"

2851. #if LJ_52

2852. //|  cmp dword TAB:RB->metatable, 0; jne ->fff_fallback

2853. dasm_put(Dst, 2688, Dt6(->metatable));

2854. #line 1640 "vm_x86.dasc"

2855. #endif

2856. //|  mov CFUNC:RB, [BASE-8]

2857. //|  mov CFUNC:RD, CFUNC:RB->upvalue[0]

2858. //|  mov PC, [BASE-4]

2859. //|  mov dword [BASE-4], LJ_TFUNC

2860. //|  mov [BASE-8], CFUNC:RD

2861. //|  mov dword [BASE+12], LJ_TNIL

2862. //|  mov RD, 1+3

2863. //|  jmp ->fff_res

2864. //|

2865. //|.ffunc_2 ipairs_aux

2866. //|  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback

2867. //|  cmp dword [BASE+12], LJ_TISNUM

2868. //|.if DUALNUM

2869. //|  jne ->fff_fallback

2870. //|.else

2871. //|  jae ->fff_fallback

2872. //|.endif

2873. //|  mov PC, [BASE-4]

2874. //|.if DUALNUM

2875. //|  mov RD, dword [BASE+8]

2876. //|  add RD, 1

2877. //|  mov dword [BASE-4], LJ_TISNUM

2878. //|  mov dword [BASE-8], RD

2879. //|.else

2880. //|  movsd xmm0, qword [BASE+8]

2881. //|  sseconst_1 xmm1, RBa

2882. //|  addsd xmm0, xmm1

2883. //|  cvttsd2si RD, xmm0

2884. //|  movsd qword [BASE-8], xmm0

2885. //|.endif

2886. //|  mov TAB:RB, [BASE]

2887. //|  cmp RD, TAB:RB->asize;  jae >2        // Not in array part?

2888. //|  shl RD, 3

2889. dasm_put(Dst, 2697, Dt8(->upvalue[0]), LJ_TFUNC, LJ_TNIL, 1+3, 2+1, LJ_TTAB, LJ_TISNUM, LJ_TISNUM, Dt6(->asize));

2890. #line 1674 "vm_x86.dasc"

2891. //|  add RD, TAB:RB->array

2892. //|1:

2893. //|  cmp dword [RD+4], LJ_TNIL;  je ->fff_res0

2894. //|  // Copy array slot.

2895. //|.if X64

2896. //|  mov RBa, [RD]

2897. //|  mov [BASE], RBa

2898. //|.else

2899. //|  mov RB, [RD]

2900. //|  mov RD, [RD+4]

2901. //|  mov [BASE], RB

2902. //|  mov [BASE+4], RD

2903. //|.endif

2904. //|  jmp ->fff_res2

2905. //|2:  // Check for empty hash part first. Otherwise call C function.

2906. //|  cmp dword TAB:RB->hmask, 0; je ->fff_res0

2907. //|  mov FCARG1, TAB:RB

2908. //|  mov RB, BASE                        // Save BASE.

2909. //|  mov FCARG2, RD                        // Caveat: FCARG2 == BASE

2910. //|  call extern lj_tab_getinth@8        // (GCtab *t, int32_t key)

2911. //|  // cTValue * or NULL returned in eax (RD).

2912. //|  mov BASE, RB

2913. //|  test RD, RD

2914. //|  jnz <1

2915. //|->fff_res0:

2916. //|  mov RD, 1+0

2917. //|  jmp ->fff_res

2918. //|

2919. //|.ffunc_1 ipairs

2920. //|  mov TAB:RB, [BASE]

2921. dasm_put(Dst, 2784, Dt6(->array), LJ_TNIL, Dt6(->hmask), 1+0, 1+1);

2922. #line 1704 "vm_x86.dasc"

2923. //|  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback

2924. dasm_put(Dst, 2676, LJ_TTAB);

2925. #line 1705 "vm_x86.dasc"

2926. #if LJ_52

2927. //|  cmp dword TAB:RB->metatable, 0; jne ->fff_fallback

2928. dasm_put(Dst, 2688, Dt6(->metatable));

2929. #line 1707 "vm_x86.dasc"

2930. #endif

2931. //|  mov CFUNC:RB, [BASE-8]

2932. //|  mov CFUNC:RD, CFUNC:RB->upvalue[0]

2933. //|  mov PC, [BASE-4]

2934. //|  mov dword [BASE-4], LJ_TFUNC

2935. //|  mov [BASE-8], CFUNC:RD

2936. //|.if DUALNUM

2937. //|  mov dword [BASE+12], LJ_TISNUM

2938. //|  mov dword [BASE+8], 0

2939. //|.else

2940. //|  xorps xmm0, xmm0

2941. //|  movsd qword [BASE+8], xmm0

2942. //|.endif

2943. //|  mov RD, 1+3

2944. //|  jmp ->fff_res

2945. //|

2946. //|//-- Base library: catch errors ----------------------------------------

2947. //|

2948. //|.ffunc_1 pcall

2949. //|  lea RA, [BASE+8]

2950. //|  sub NARGS:RD, 1

2951. //|  mov PC, 8+FRAME_PCALL

2952. //|1:

2953. //|  movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)]

2954. //|  shr RB, HOOK_ACTIVE_SHIFT

2955. //|  and RB, 1

2956. //|  add PC, RB                                // Remember active hook before pcall.

2957. //|  jmp ->vm_call_dispatch

2958. //|

2959. //|.ffunc_2 xpcall

2960. //|  cmp dword [BASE+12], LJ_TFUNC;  jne ->fff_fallback

2961. dasm_put(Dst, 2860, Dt8(->upvalue[0]), LJ_TFUNC, LJ_TISNUM, 1+3, 1+1, 8+FRAME_PCALL, DISPATCH_GL(hookmask), HOOK_ACTIVE_SHIFT, 2+1);

2962. #line 1738 "vm_x86.dasc"

2963. //|  mov RB, [BASE+4]                        // Swap function and traceback.

2964. //|  mov [BASE+12], RB

2965. //|  mov dword [BASE+4], LJ_TFUNC

2966. //|  mov LFUNC:RB, [BASE]

2967. //|  mov PC, [BASE+8]

2968. //|  mov [BASE+8], LFUNC:RB

2969. //|  mov [BASE], PC

2970. //|  lea RA, [BASE+16]

2971. //|  sub NARGS:RD, 2

2972. //|  mov PC, 16+FRAME_PCALL

2973. //|  jmp <1

2974. //|

2975. //|//-- Coroutine library --------------------------------------------------

2976. //|

2977. //|.macro coroutine_resume_wrap, resume

2978. //|.if resume

2979. //|.ffunc_1 coroutine_resume

2980. //|  mov L:RB, [BASE]

2981. //|.else

2982. //|.ffunc coroutine_wrap_aux

2983. //|  mov CFUNC:RB, [BASE-8]

2984. //|  mov L:RB, CFUNC:RB->upvalue[0].gcr

2985. //|.endif

2986. //|  mov PC, [BASE-4]

2987. //|  mov SAVE_PC, PC

2988. //|.if X64

2989. //|  mov TMP1, L:RB

2990. //|.else

2991. //|  mov ARG1, L:RB

2992. //|.endif

2993. //|.if resume

2994. //|  cmp dword [BASE+4], LJ_TTHREAD;  jne ->fff_fallback

2995. //|.endif

2996. //|  cmp aword L:RB->cframe, 0; jne ->fff_fallback

2997. //|  cmp byte L:RB->status, LUA_YIELD;  ja ->fff_fallback

2998. //|  mov RA, L:RB->top

2999. //|  je >1                                // Status != LUA_YIELD (i.e. 0)?

3000. //|  cmp RA, L:RB->base                        // Check for presence of initial func.

3001. //|  je ->fff_fallback

3002. //|1:

3003. //|.if resume

3004. //|  lea PC, [RA+NARGS:RD*8-16]                // Check stack space (-1-thread).

3005. //|.else

3006. //|  lea PC, [RA+NARGS:RD*8-8]                // Check stack space (-1).

3007. //|.endif

3008. //|  cmp PC, L:RB->maxstack; ja ->fff_fallback

3009. //|  mov L:RB->top, PC

3010. //|

3011. //|  mov L:RB, SAVE_L

3012. //|  mov L:RB->base, BASE

3013. //|.if resume

3014. //|  add BASE, 8                        // Keep resumed thread in stack for GC.

3015. //|.endif

3016. //|  mov L:RB->top, BASE

3017. //|.if resume

3018. //|  lea RB, [BASE+NARGS:RD*8-24]        // RB = end of source for stack move.

3019. //|.else

3020. //|  lea RB, [BASE+NARGS:RD*8-16]        // RB = end of source for stack move.

3021. //|.endif

3022. //|  sub RBa, PCa                        // Relative to PC.

3023. //|

3024. //|  cmp PC, RA

3025. //|  je >3

3026. //|2:  // Move args to coroutine.

3027. //|.if X64

3028. //|  mov RCa, [PC+RB]

3029. //|  mov [PC-8], RCa

3030. //|.else

3031. //|  mov RC, [PC+RB+4]

3032. //|  mov [PC-4], RC

3033. //|  mov RC, [PC+RB]

3034. //|  mov [PC-8], RC

3035. //|.endif

3036. //|  sub PC, 8

3037. //|  cmp PC, RA

3038. //|  jne <2

3039. //|3:

3040. //|.if X64

3041. //|  mov CARG2d, RA

3042. //|  mov CARG1d, TMP1

3043. //|.else

3044. //|  mov ARG2, RA

3045. //|  xor RA, RA

3046. //|  mov ARG4, RA

3047. //|  mov ARG3, RA

3048. //|.endif

3049. //|  call ->vm_resume                        // (lua_State *L, TValue *base, 0, 0)

3050. //|

3051. //|  mov L:RB, SAVE_L

3052. //|.if X64

3053. //|  mov L:PC, TMP1

3054. //|.else

3055. //|  mov L:PC, ARG1                        // The callee doesn't modify SAVE_L.

3056. //|.endif

3057. //|  mov BASE, L:RB->base

3058. //|  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB

3059. //|  set_vmstate INTERP

3060. //|

3061. //|  cmp eax, LUA_YIELD

3062. //|  ja >8

3063. //|4:

3064. //|  mov RA, L:PC->base

3065. //|  mov KBASE, L:PC->top

3066. //|  mov L:PC->top, RA                        // Clear coroutine stack.

3067. //|  mov PC, KBASE

3068. //|  sub PC, RA

3069. //|  je >6                                // No results?

3070. //|  lea RD, [BASE+PC]

3071. //|  shr PC, 3

3072. //|  cmp RD, L:RB->maxstack

3073. //|  ja >9                                // Need to grow stack?

3074. //|

3075. //|  mov RB, BASE

3076. //|  sub RBa, RAa

3077. //|5:  // Move results from coroutine.

3078. //|.if X64

3079. //|  mov RDa, [RA]

3080. //|  mov [RA+RB], RDa

3081. //|.else

3082. //|  mov RD, [RA]

3083. //|  mov [RA+RB], RD

3084. //|  mov RD, [RA+4]

3085. //|  mov [RA+RB+4], RD

3086. //|.endif

3087. //|  add RA, 8

3088. //|  cmp RA, KBASE

3089. //|  jne <5

3090. //|6:

3091. //|.if resume

3092. //|  lea RD, [PC+2]                        // nresults+1 = 1 + true + results.

3093. //|  mov dword [BASE-4], LJ_TTRUE        // Prepend true to results.

3094. //|.else

3095. //|  lea RD, [PC+1]                        // nresults+1 = 1 + results.

3096. //|.endif

3097. //|7:

3098. //|  mov PC, SAVE_PC

3099. //|  mov MULTRES, RD

3100. //|.if resume

3101. //|  mov RAa, -8

3102. //|.else

3103. //|  xor RA, RA

3104. //|.endif

3105. //|  test PC, FRAME_TYPE

3106. //|  jz ->BC_RET_Z

3107. //|  jmp ->vm_return

3108. //|

3109. //|8:  // Coroutine returned with error (at co->top-1).

3110. //|.if resume

3111. //|  mov dword [BASE-4], LJ_TFALSE        // Prepend false to results.

3112. //|  mov RA, L:PC->top

3113. //|  sub RA, 8

3114. //|  mov L:PC->top, RA                        // Clear error from coroutine stack.

3115. //|  // Copy error message.

3116. //|.if X64

3117. //|  mov RDa, [RA]

3118. //|  mov [BASE], RDa

3119. //|.else

3120. //|  mov RD, [RA]

3121. //|  mov [BASE], RD

3122. //|  mov RD, [RA+4]

3123. //|  mov [BASE+4], RD

3124. //|.endif

3125. //|  mov RD, 1+2                        // nresults+1 = 1 + false + error.

3126. //|  jmp <7

3127. //|.else

3128. //|  mov FCARG2, L:PC

3129. //|  mov FCARG1, L:RB

3130. //|  call extern lj_ffh_coroutine_wrap_err@8  // (lua_State *L, lua_State *co)

3131. //|  // Error function does not return.

3132. //|.endif

3133. //|

3134. //|9:  // Handle stack expansion on return from yield.

3135. //|.if X64

3136. //|  mov L:RA, TMP1

3137. //|.else

3138. //|  mov L:RA, ARG1                        // The callee doesn't modify SAVE_L.

3139. //|.endif

3140. //|  mov L:RA->top, KBASE                // Undo coroutine stack clearing.

3141. //|  mov FCARG2, PC

3142. //|  mov FCARG1, L:RB

3143. //|  call extern lj_state_growstack@8        // (lua_State *L, int n)

3144. //|.if X64

3145. //|  mov L:PC, TMP1

3146. //|.else

3147. //|  mov L:PC, ARG1

3148. //|.endif

3149. //|  mov BASE, L:RB->base

3150. //|  jmp <4                                // Retry the stack move.

3151. //|.endmacro

3152. //|

3153. //|  coroutine_resume_wrap 1                // coroutine.resume

3154. dasm_put(Dst, 2947, LJ_TFUNC, LJ_TFUNC, 16+FRAME_PCALL, 1+1, LJ_TTHREAD, Dt1(->cframe), Dt1(->status), LUA_YIELD);

3155. dasm_put(Dst, 3038, Dt1(->top), Dt1(->base), Dt1(->maxstack), Dt1(->top), Dt1(->base), Dt1(->top));

3156. dasm_put(Dst, 3127, Dt1(->base), DISPATCH_GL(cur_L), DISPATCH_GL(vmstate), ~LJ_VMST_INTERP, LUA_YIELD, Dt1(->base), Dt1(->top), Dt1(->top), Dt1(->maxstack));

3157. #line 1929 "vm_x86.dasc"

3158. //|  coroutine_resume_wrap 0                // coroutine.wrap

3159. dasm_put(Dst, 3218, LJ_TTRUE, FRAME_TYPE, LJ_TFALSE, Dt1(->top), Dt1(->top), 1+2, Dt1(->top), Dt1(->base));

3160. dasm_put(Dst, 3319, Dt8(->upvalue[0].gcr), Dt1(->cframe), Dt1(->status), LUA_YIELD, Dt1(->top), Dt1(->base), Dt1(->maxstack), Dt1(->top), Dt1(->base));

3161. dasm_put(Dst, 3393, Dt1(->top), Dt1(->base), DISPATCH_GL(cur_L), DISPATCH_GL(vmstate), ~LJ_VMST_INTERP, LUA_YIELD, Dt1(->base), Dt1(->top), Dt1(->top));

3162. #line 1930 "vm_x86.dasc"

3163. //|

3164. //|.ffunc coroutine_yield

3165. //|  mov L:RB, SAVE_L

3166. dasm_put(Dst, 3481, Dt1(->maxstack), FRAME_TYPE, Dt1(->top), Dt1(->base));

3167. #line 1933 "vm_x86.dasc"

3168. //|  test aword L:RB->cframe, CFRAME_RESUME

3169. //|  jz ->fff_fallback

3170. //|  mov L:RB->base, BASE

3171. //|  lea RD, [BASE+NARGS:RD*8-8]

3172. //|  mov L:RB->top, RD

3173. //|  xor RD, RD

3174. //|  mov aword L:RB->cframe, RDa

3175. //|  mov al, LUA_YIELD

3176. //|  mov byte L:RB->status, al

3177. //|  jmp ->vm_leave_unw

3178. //|

3179. //|//-- Math library -------------------------------------------------------

3180. //|

3181. //|.if not DUALNUM

3182. //|->fff_resi:  // Dummy.

3183. //|.endif

3184. //|

3185. //|->fff_resn:

3186. //|  mov PC, [BASE-4]

3187. //|  fstp qword [BASE-8]

3188. //|  jmp ->fff_res1

3189. //|

3190. //|  .ffunc_1 math_abs

3191. //|.if DUALNUM

3192. //|  cmp dword [BASE+4], LJ_TISNUM; jne >2

3193. //|  mov RB, dword [BASE]

3194. dasm_put(Dst, 3601, Dt1(->cframe), CFRAME_RESUME, Dt1(->base), Dt1(->top), Dt1(->cframe), LUA_YIELD, Dt1(->status), 1+1, LJ_TISNUM);

3195. #line 1959 "vm_x86.dasc"

3196. //|  cmp RB, 0; jns ->fff_resi

3197. //|  neg RB; js >1

3198. //|->fff_resbit:

3199. //|->fff_resi:

3200. //|  mov PC, [BASE-4]

3201. //|  mov dword [BASE-4], LJ_TISNUM

3202. //|  mov dword [BASE-8], RB

3203. //|  jmp ->fff_res1

3204. //|1:

3205. //|  mov PC, [BASE-4]

3206. //|  mov dword [BASE-4], 0x41e00000  // 2^31.

3207. //|  mov dword [BASE-8], 0

3208. //|  jmp ->fff_res1

3209. //|2:

3210. //|  ja ->fff_fallback

3211. //|.else

3212. //|  cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback

3213. //|.endif

3214. //|  movsd xmm0, qword [BASE]

3215. //|  sseconst_abs xmm1, RDa

3216. //|  andps xmm0, xmm1

3217. //|->fff_resxmm0:

3218. //|  mov PC, [BASE-4]

3219. //|  movsd qword [BASE-8], xmm0

3220. //|  // fallthrough

3221. //|

3222. //|->fff_res1:

3223. //|  mov RD, 1+1

3224. //|->fff_res:

3225. //|  mov MULTRES, RD

3226. dasm_put(Dst, 3675, LJ_TISNUM, (unsigned int)(U64x(7fffffff,ffffffff)), (unsigned int)((U64x(7fffffff,ffffffff))>>32), 1+1);

3227. #line 1989 "vm_x86.dasc"

3228. //|->fff_res_:

3229. //|  test PC, FRAME_TYPE

3230. //|  jnz >7

3231. //|5:

3232. //|  cmp PC_RB, RDL                        // More results expected?

3233. //|  ja >6

3234. //|  // Adjust BASE. KBASE is assumed to be set for the calling frame.

3235. //|  movzx RA, PC_RA

3236. //|  not RAa                                // Note: ~RA = -(RA+1)

3237. //|  lea BASE, [BASE+RA*8]                // base = base - (RA+1)*8

3238. //|  ins_next

3239. //|

3240. //|6:  // Fill up results with nil.

3241. //|  mov dword [BASE+RD*8-12], LJ_TNIL

3242. //|  add RD, 1

3243. //|  jmp <5

3244. //|

3245. //|7:  // Non-standard return case.

3246. //|  mov RAa, -8                        // Results start at BASE+RA = BASE-8.

3247. //|  jmp ->vm_return

3248. //|

3249. //|.if X64

3250. //|.define fff_resfp, fff_resxmm0

3251. //|.else

3252. //|.define fff_resfp, fff_resn

3253. //|.endif

3254. //|

3255. //|.macro math_round, func

3256. //|  .ffunc math_ .. func

3257. //|.if DUALNUM

3258. //|  cmp dword [BASE+4], LJ_TISNUM; jne >1

3259. //|  mov RB, dword [BASE]; jmp ->fff_resi

3260. //|1:

3261. //|  ja ->fff_fallback

3262. //|.else

3263. //|  cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback

3264. //|.endif

3265. //|  movsd xmm0, qword [BASE]

3266. //|  call ->vm_ .. func .. _sse

3267. //|.if DUALNUM

3268. //|  cvttsd2si RB, xmm0

3269. //|  cmp RB, 0x80000000

3270. //|  jne ->fff_resi

3271. //|  cvtsi2sd xmm1, RB

3272. //|  ucomisd xmm0, xmm1

3273. //|  jp ->fff_resxmm0

3274. //|  je ->fff_resi

3275. //|.endif

3276. //|  jmp ->fff_resxmm0

3277. //|.endmacro

3278. //|

3279. //|  math_round floor

3280. dasm_put(Dst, 3782, FRAME_TYPE, LJ_TNIL, LJ_TISNUM);

3281. #line 2041 "vm_x86.dasc"

3282. //|  math_round ceil

3283. dasm_put(Dst, 3890, LJ_TISNUM);

3284. #line 2042 "vm_x86.dasc"

3285. //|

3286. //|.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0

3287. //|

3288. //|.ffunc math_log

3289. //|  cmp NARGS:RD, 1+1; jne ->fff_fallback        // Exactly one argument.

3290. dasm_put(Dst, 3963, 1+1, LJ_TISNUM);

3291. #line 2047 "vm_x86.dasc"

3292. //|  cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback

3293. //|  movsd xmm0, qword [BASE]

3294. //|.if not X64

3295. //|  movsd FPARG1, xmm0

3296. //|.endif

3297. //|  mov RB, BASE

3298. //|  call extern log

3299. //|  mov BASE, RB

3300. //|  jmp ->fff_resfp

3301. //|

3302. //|.macro math_extern, func

3303. //|  .ffunc_nsse math_ .. func

3304. //|.if not X64

3305. //|  movsd FPARG1, xmm0

3306. //|.endif

3307. //|  mov RB, BASE

3308. //|  call extern func

3309. //|  mov BASE, RB

3310. //|  jmp ->fff_resfp

3311. //|.endmacro

3312. //|

3313. //|.macro math_extern2, func

3314. //|  .ffunc_nnsse math_ .. func

3315. //|.if not X64

3316. //|  movsd FPARG1, xmm0

3317. //|  movsd FPARG3, xmm1

3318. //|.endif

3319. //|  mov RB, BASE

3320. //|  call extern func

3321. //|  mov BASE, RB

3322. //|  jmp ->fff_resfp

3323. //|.endmacro

3324. //|

3325. //|  math_extern log10

3326. //|  math_extern exp

3327. dasm_put(Dst, 4039, 1+1, LJ_TISNUM, 1+1, LJ_TISNUM, 1+1);

3328. #line 2082 "vm_x86.dasc"

3329. //|  math_extern sin

3330. //|  math_extern cos

3331. dasm_put(Dst, 4122, LJ_TISNUM, 1+1, LJ_TISNUM, 1+1, LJ_TISNUM);

3332. #line 2084 "vm_x86.dasc"

3333. //|  math_extern tan

3334. //|  math_extern asin

3335. //|  math_extern acos

3336. dasm_put(Dst, 4206, 1+1, LJ_TISNUM, 1+1, LJ_TISNUM);

3337. #line 2087 "vm_x86.dasc"

3338. //|  math_extern atan

3339. //|  math_extern sinh

3340. dasm_put(Dst, 4301, 1+1, LJ_TISNUM, 1+1, LJ_TISNUM, 1+1);

3341. #line 2089 "vm_x86.dasc"

3342. //|  math_extern cosh

3343. //|  math_extern tanh

3344. dasm_put(Dst, 4384, LJ_TISNUM, 1+1, LJ_TISNUM, 1+1, LJ_TISNUM);

3345. #line 2091 "vm_x86.dasc"

3346. //|  math_extern2 pow

3347. //|  math_extern2 atan2

3348. dasm_put(Dst, 4468, 2+1, LJ_TISNUM, LJ_TISNUM, 2+1, LJ_TISNUM);

3349. #line 2093 "vm_x86.dasc"

3350. //|  math_extern2 fmod

3351. //|

3352. //|.ffunc_nnr math_ldexp;        fscale; fpop1;        jmp ->fff_resn

3353. dasm_put(Dst, 4558, LJ_TISNUM, 2+1, LJ_TISNUM, LJ_TISNUM, 2+1);

3354. #line 2096 "vm_x86.dasc"

3355. //|

3356. //|.ffunc_1 math_frexp

3357. //|  mov RB, [BASE+4]

3358. //|  cmp RB, LJ_TISNUM;  jae ->fff_fallback

3359. //|  mov PC, [BASE-4]

3360. //|  mov RC, [BASE]

3361. //|  mov [BASE-4], RB; mov [BASE-8], RC

3362. //|  shl RB, 1; cmp RB, 0xffe00000; jae >3

3363. //|  or RC, RB; jz >3

3364. //|  mov RC, 1022

3365. //|  cmp RB, 0x00200000; jb >4

3366. //|1:

3367. //|  shr RB, 21; sub RB, RC                // Extract and unbias exponent.

3368. dasm_put(Dst, 4654, LJ_TISNUM, LJ_TISNUM, 1+1, LJ_TISNUM);

3369. #line 2109 "vm_x86.dasc"

3370. //|  cvtsi2sd xmm0, RB

3371. //|  mov RB, [BASE-4]

3372. //|  and RB, 0x800fffff                        // Mask off exponent.

3373. //|  or RB, 0x3fe00000                        // Put mantissa in range [0.5,1) or 0.

3374. //|  mov [BASE-4], RB

3375. //|2:

3376. //|  movsd qword [BASE], xmm0

3377. //|  mov RD, 1+2

3378. //|  jmp ->fff_res

3379. //|3:  // Return +-0, +-Inf, NaN unmodified and an exponent of 0.

3380. //|  xorps xmm0, xmm0; jmp <2

3381. //|4:  // Handle denormals by multiplying with 2^54 and adjusting the bias.

3382. //|  movsd xmm0, qword [BASE]

3383. //|  sseconst_hi xmm1, RBa, 43500000  // 2^54.

3384. //|  mulsd xmm0, xmm1

3385. //|  movsd qword [BASE-8], xmm0

3386. //|  mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1

3387. //|

3388. //|.ffunc_nsse math_modf

3389. //|  mov RB, [BASE+4]

3390. //|  mov PC, [BASE-4]

3391. //|  shl RB, 1; cmp RB, 0xffe00000; je >4        // +-Inf?

3392. //|  movaps xmm4, xmm0

3393. dasm_put(Dst, 4761, 1+2, (unsigned int)(U64x(43500000,00000000)), (unsigned int)((U64x(43500000,00000000))>>32), 1+1, LJ_TISNUM);

3394. #line 2132 "vm_x86.dasc"

3395. //|  call ->vm_trunc_sse

3396. //|  subsd xmm4, xmm0

3397. //|1:

3398. //|  movsd qword [BASE-8], xmm0

3399. //|  movsd qword [BASE], xmm4

3400. //|  mov RC, [BASE-4]; mov RB, [BASE+4]

3401. //|  xor RC, RB; js >3                                // Need to adjust sign?

3402. //|2:

3403. //|  mov RD, 1+2

3404. //|  jmp ->fff_res

3405. //|3:

3406. //|  xor RB, 0x80000000; mov [BASE+4], RB        // Flip sign of fraction.

3407. //|  jmp <2

3408. //|4:

3409. //|  xorps xmm4, xmm4; jmp <1                        // Return +-Inf and +-0.

3410. //|

3411. //|.macro math_minmax, name, cmovop, sseop

3412. //|  .ffunc name

3413. //|  mov RA, 2

3414. //|  cmp dword [BASE+4], LJ_TISNUM

3415. //|.if DUALNUM

3416. //|  jne >4

3417. //|  mov RB, dword [BASE]

3418. //|1:  // Handle integers.

3419. //|  cmp RA, RD; jae ->fff_resi

3420. //|  cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3

3421. //|  cmp RB, dword [BASE+RA*8-8]

3422. //|  cmovop RB, dword [BASE+RA*8-8]

3423. //|  add RA, 1

3424. //|  jmp <1

3425. //|3:

3426. //|  ja ->fff_fallback

3427. //|  // Convert intermediate result to number and continue below.

3428. //|  cvtsi2sd xmm0, RB

3429. //|  jmp >6

3430. //|4:

3431. //|  ja ->fff_fallback

3432. //|.else

3433. //|  jae ->fff_fallback

3434. //|.endif

3435. //|

3436. //|  movsd xmm0, qword [BASE]

3437. //|5:  // Handle numbers or integers.

3438. //|  cmp RA, RD; jae ->fff_resxmm0

3439. //|  cmp dword [BASE+RA*8-4], LJ_TISNUM

3440. //|.if DUALNUM

3441. //|  jb >6

3442. //|  ja ->fff_fallback

3443. //|  cvtsi2sd xmm1, dword [BASE+RA*8-8]

3444. //|  jmp >7

3445. //|.else

3446. //|  jae ->fff_fallback

3447. //|.endif

3448. //|6:

3449. //|  movsd xmm1, qword [BASE+RA*8-8]

3450. //|7:

3451. //|  sseop xmm0, xmm1

3452. //|  add RA, 1

3453. //|  jmp <5

3454. //|.endmacro

3455. //|

3456. //|  math_minmax math_min, cmovg, minsd

3457. dasm_put(Dst, 4905, 1+2, LJ_TISNUM);

3458. dasm_put(Dst, 4997, LJ_TISNUM, LJ_TISNUM);

3459. #line 2194 "vm_x86.dasc"

3460. //|  math_minmax math_max, cmovl, maxsd

3461. dasm_put(Dst, 5078, LJ_TISNUM, LJ_TISNUM);

3462. #line 2195 "vm_x86.dasc"

3463. //|

3464. //|//-- String library -----------------------------------------------------

3465. //|

3466. //|.ffunc string_byte                        // Only handle the 1-arg case here.

3467. //|  cmp NARGS:RD, 1+1;  jne ->fff_fallback

3468. dasm_put(Dst, 5176, LJ_TISNUM);

3469. #line 2200 "vm_x86.dasc"

3470. //|  cmp dword [BASE+4], LJ_TSTR;  jne ->fff_fallback

3471. //|  mov STR:RB, [BASE]

3472. //|  mov PC, [BASE-4]

3473. //|  cmp dword STR:RB->len, 1

3474. //|  jb ->fff_res0                        // Return no results for empty string.

3475. //|  movzx RB, byte STR:RB[1]

3476. //|.if DUALNUM

3477. //|  jmp ->fff_resi

3478. //|.else

3479. //|  cvtsi2sd xmm0, RB; jmp ->fff_resxmm0

3480. //|.endif

3481. //|

3482. //|.ffunc string_char                        // Only handle the 1-arg case here.

3483. //|  ffgccheck

3484. //|  cmp NARGS:RD, 1+1;  jne ->fff_fallback        // *Exactly* 1 arg.

3485. dasm_put(Dst, 5262, 1+1, LJ_TSTR, Dt5(->len), Dt5([1]), DISPATCH_GL(gc.total), DISPATCH_GL(gc.threshold));

3486. #line 2215 "vm_x86.dasc"

3487. //|  cmp dword [BASE+4], LJ_TISNUM

3488. //|.if DUALNUM

3489. //|  jne ->fff_fallback

3490. //|  mov RB, dword [BASE]

3491. //|  cmp RB, 255;  ja ->fff_fallback

3492. //|  mov TMP2, RB

3493. //|.else

3494. //|  jae ->fff_fallback

3495. //|  cvttsd2si RB, qword [BASE]

3496. //|  cmp RB, 255;  ja ->fff_fallback

3497. //|  mov TMP2, RB

3498. //|.endif

3499. //|.if X64

3500. //|  mov TMP3, 1

3501. //|.else

3502. //|  mov ARG3, 1

3503. //|.endif

3504. //|  lea RDa, TMP2                        // Points to stack. Little-endian.

3505. //|->fff_newstr:

3506. //|  mov L:RB, SAVE_L

3507. //|  mov L:RB->base, BASE

3508. //|.if X64

3509. //|  mov CARG3d, TMP3                        // Zero-extended to size_t.

3510. //|  mov CARG2, RDa                        // May be 64 bit ptr to stack.

3511. //|  mov CARG1d, L:RB

3512. //|.else

3513. //|  mov ARG2, RD

3514. //|  mov ARG1, L:RB

3515. //|.endif

3516. //|  mov SAVE_PC, PC

3517. //|  call extern lj_str_new                // (lua_State *L, char *str, size_t l)

3518. //|->fff_resstr:

3519. //|  // GCstr * returned in eax (RD).

3520. //|  mov BASE, L:RB->base

3521. //|  mov PC, [BASE-4]

3522. //|  mov dword [BASE-4], LJ_TSTR

3523. //|  mov [BASE-8], STR:RD

3524. //|  jmp ->fff_res1

3525. //|

3526. //|.ffunc string_sub

3527. //|  ffgccheck

3528. dasm_put(Dst, 5321, 1+1, LJ_TISNUM, Dt1(->base), Dt1(->base), LJ_TSTR, DISPATCH_GL(gc.total), DISPATCH_GL(gc.threshold));

3529. #line 2256 "vm_x86.dasc"

3530. //|  mov TMP2, -1

3531. //|  cmp NARGS:RD, 1+2;  jb ->fff_fallback

3532. //|  jna >1

3533. //|  cmp dword [BASE+20], LJ_TISNUM

3534. //|.if DUALNUM

3535. //|  jne ->fff_fallback

3536. //|  mov RB, dword [BASE+16]

3537. //|  mov TMP2, RB

3538. //|.else

3539. //|  jae ->fff_fallback

3540. //|  cvttsd2si RB, qword [BASE+16]

3541. //|  mov TMP2, RB

3542. //|.endif

3543. //|1:

3544. //|  cmp dword [BASE+4], LJ_TSTR;  jne ->fff_fallback

3545. //|  cmp dword [BASE+12], LJ_TISNUM

3546. //|.if DUALNUM

3547. //|  jne ->fff_fallback

3548. //|.else

3549. //|  jae ->fff_fallback

3550. //|.endif

3551. //|  mov STR:RB, [BASE]

3552. //|  mov TMP3, STR:RB

3553. //|  mov RB, STR:RB->len

3554. //|.if DUALNUM

3555. //|  mov RA, dword [BASE+8]

3556. //|.else

3557. //|  cvttsd2si RA, qword [BASE+8]

3558. //|.endif

3559. //|  mov RC, TMP2

3560. //|  cmp RB, RC                                // len < end? (unsigned compare)

3561. //|  jb >5

3562. //|2:

3563. //|  test RA, RA                        // start <= 0?

3564. dasm_put(Dst, 5433, 1+2, LJ_TISNUM, LJ_TSTR, LJ_TISNUM, Dt5(->len));

3565. #line 2290 "vm_x86.dasc"

3566. //|  jle >7

3567. //|3:

3568. //|  mov STR:RB, TMP3

3569. //|  sub RC, RA                                // start > end?

3570. //|  jl ->fff_emptystr

3571. //|  lea RB, [STR:RB+RA+#STR-1]

3572. //|  add RC, 1

3573. //|4:

3574. //|.if X64

3575. //|  mov TMP3, RC

3576. //|.else

3577. //|  mov ARG3, RC

3578. //|.endif

3579. //|  mov RD, RB

3580. //|  jmp ->fff_newstr

3581. //|

3582. //|5:  // Negative end or overflow.

3583. //|  jl >6

3584. //|  lea RC, [RC+RB+1]                        // end = end+(len+1)

3585. //|  jmp <2

3586. //|6:  // Overflow.

3587. //|  mov RC, RB                                // end = len

3588. //|  jmp <2

3589. //|

3590. //|7:  // Negative start or underflow.

3591. //|  je >8

3592. //|  add RA, RB                                // start = start+(len+1)

3593. dasm_put(Dst, 5523, sizeof(GCstr)-1);

3594. #line 2317 "vm_x86.dasc"

3595. //|  add RA, 1

3596. //|  jg <3                                // start > 0?

3597. //|8:  // Underflow.

3598. //|  mov RA, 1                                // start = 1

3599. //|  jmp <3

3600. //|

3601. //|->fff_emptystr:  // Range underflow.

3602. //|  xor RC, RC                                // Zero length. Any ptr in RB is ok.

3603. //|  jmp <4

3604. //|

3605. //|.macro ffstring_op, name

3606. //|  .ffunc_1 string_ .. name

3607. //|  ffgccheck

3608. //|  cmp dword [BASE+4], LJ_TSTR;  jne ->fff_fallback

3609. //|  mov L:RB, SAVE_L

3610. //|   lea SBUF:FCARG1, [DISPATCH+DISPATCH_GL(tmpbuf)]

3611. //|  mov L:RB->base, BASE

3612. //|  mov STR:FCARG2, [BASE]                // Caveat: FCARG2 == BASE

3613. //|   mov RC, SBUF:FCARG1->b

3614. //|   mov SBUF:FCARG1->L, L:RB

3615. //|   mov SBUF:FCARG1->p, RC

3616. //|  mov SAVE_PC, PC

3617. //|  call extern lj_buf_putstr_ .. name .. @8

3618. //|  mov FCARG1, eax

3619. //|  call extern lj_buf_tostr@4

3620. //|  jmp ->fff_resstr

3621. //|.endmacro

3622. //|

3623. //|ffstring_op reverse

3624. dasm_put(Dst, 5590, 1+1, DISPATCH_GL(gc.total), DISPATCH_GL(gc.threshold), LJ_TSTR);

3625. #line 2346 "vm_x86.dasc"

3626. //|ffstring_op lower

3627. dasm_put(Dst, 5652, DISPATCH_GL(tmpbuf), Dt1(->base), DtE(->b), DtE(->L), DtE(->p), 1+1, DISPATCH_GL(gc.total), DISPATCH_GL(gc.threshold));

3628. #line 2347 "vm_x86.dasc"

3629. //|ffstring_op upper

3630. dasm_put(Dst, 5724, LJ_TSTR, DISPATCH_GL(tmpbuf), Dt1(->base), DtE(->b), DtE(->L), DtE(->p), 1+1, DISPATCH_GL(gc.total), DISPATCH_GL(gc.threshold));

3631. #line 2348 "vm_x86.dasc"

3632. //|

3633. //|//-- Bit library --------------------------------------------------------

3634. //|

3635. //|.define TOBIT_BIAS, 0x59c00000        // 2^52 + 2^51 (float, not double!).

3636. //|

3637. //|.macro .ffunc_bit, name, kind

3638. //|  .ffunc_1 name

3639. //|.if kind == 2

3640. //|  sseconst_tobit xmm1, RBa

3641. //|.endif

3642. //|  cmp dword [BASE+4], LJ_TISNUM

3643. //|.if DUALNUM

3644. //|  jne >1

3645. //|  mov RB, dword [BASE]

3646. //|.if kind > 0

3647. //|  jmp >2

3648. //|.else

3649. //|  jmp ->fff_resbit

3650. //|.endif

3651. //|1:

3652. //|  ja ->fff_fallback

3653. //|.else

3654. //|  jae ->fff_fallback

3655. //|.endif

3656. //|  movsd xmm0, qword [BASE]

3657. //|.if kind < 2

3658. //|  sseconst_tobit xmm1, RBa

3659. //|.endif

3660. //|  addsd xmm0, xmm1

3661. //|  movd RB, xmm0

3662. //|2:

3663. //|.endmacro

3664. //|

3665. //|.ffunc_bit bit_tobit, 0

3666. dasm_put(Dst, 5801, LJ_TSTR, DISPATCH_GL(tmpbuf), Dt1(->base), DtE(->b), DtE(->L), DtE(->p), 1+1, LJ_TISNUM);

3667. #line 2382 "vm_x86.dasc"

3668. //|  jmp ->fff_resbit

3669. //|

3670. //|.macro .ffunc_bit_op, name, ins

3671. //|  .ffunc_bit name, 2

3672. //|  mov TMP2, NARGS:RD                        // Save for fallback.

3673. //|  lea RD, [BASE+NARGS:RD*8-16]

3674. //|1:

3675. //|  cmp RD, BASE

3676. //|  jbe ->fff_resbit

3677. //|  cmp dword [RD+4], LJ_TISNUM

3678. //|.if DUALNUM

3679. //|  jne >2

3680. //|  ins RB, dword [RD]

3681. //|  sub RD, 8

3682. //|  jmp <1

3683. //|2:

3684. //|  ja ->fff_fallback_bit_op

3685. //|.else

3686. //|  jae ->fff_fallback_bit_op

3687. //|.endif

3688. //|  movsd xmm0, qword [RD]

3689. //|  addsd xmm0, xmm1

3690. //|  movd RA, xmm0

3691. //|  ins RB, RA

3692. //|  sub RD, 8

3693. //|  jmp <1

3694. //|.endmacro

3695. //|

3696. //|.ffunc_bit_op bit_band, and

3697. dasm_put(Dst, 5878, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32), 1+1, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32), LJ_TISNUM);

3698. #line 2411 "vm_x86.dasc"

3699. //|.ffunc_bit_op bit_bor, or

3700. dasm_put(Dst, 5952, LJ_TISNUM, 1+1, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32));

3701. dasm_put(Dst, 6047, LJ_TISNUM, LJ_TISNUM);

3702. #line 2412 "vm_x86.dasc"

3703. //|.ffunc_bit_op bit_bxor, xor

3704. dasm_put(Dst, 6131, 1+1, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32), LJ_TISNUM);

3705. #line 2413 "vm_x86.dasc"

3706. //|

3707. //|.ffunc_bit bit_bswap, 1

3708. dasm_put(Dst, 6228, LJ_TISNUM, 1+1, LJ_TISNUM);

3709. #line 2415 "vm_x86.dasc"

3710. //|  bswap RB

3711. //|  jmp ->fff_resbit

3712. //|

3713. //|.ffunc_bit bit_bnot, 1

3714. dasm_put(Dst, 6303, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32), 1+1, LJ_TISNUM, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32));

3715. #line 2419 "vm_x86.dasc"

3716. //|  not RB

3717. //|.if DUALNUM

3718. //|  jmp ->fff_resbit

3719. //|.else

3720. //|->fff_resbit:

3721. //|  cvtsi2sd xmm0, RB

3722. //|  jmp ->fff_resxmm0

3723. //|.endif

3724. //|

3725. //|->fff_fallback_bit_op:

3726. //|  mov NARGS:RD, TMP2                        // Restore for fallback

3727. //|  jmp ->fff_fallback

3728. //|

3729. //|.macro .ffunc_bit_sh, name, ins

3730. //|.if DUALNUM

3731. //|  .ffunc_bit name, 1

3732. //|  // Note: no inline conversion from number for 2nd argument!

3733. //|  cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback

3734. //|  mov RA, dword [BASE+8]

3735. //|.else

3736. //|  .ffunc_nnsse name

3737. //|  sseconst_tobit xmm2, RBa

3738. //|  addsd xmm0, xmm2

3739. //|  addsd xmm1, xmm2

3740. //|  movd RB, xmm0

3741. //|  movd RA, xmm1

3742. //|.endif

3743. //|  ins RB, cl                                // Assumes RA is ecx.

3744. //|  jmp ->fff_resbit

3745. //|.endmacro

3746. //|

3747. //|.ffunc_bit_sh bit_lshift, shl

3748. dasm_put(Dst, 6379, 1+1, LJ_TISNUM, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32));

3749. #line 2451 "vm_x86.dasc"

3750. //|.ffunc_bit_sh bit_rshift, shr

3751. dasm_put(Dst, 6453, LJ_TISNUM, 1+1, LJ_TISNUM, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32));

3752. #line 2452 "vm_x86.dasc"

3753. //|.ffunc_bit_sh bit_arshift, sar

3754. dasm_put(Dst, 6528, LJ_TISNUM, 1+1, LJ_TISNUM, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32));

3755. #line 2453 "vm_x86.dasc"

3756. //|.ffunc_bit_sh bit_rol, rol

3757. dasm_put(Dst, 6604, LJ_TISNUM, 1+1, LJ_TISNUM, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32));

3758. #line 2454 "vm_x86.dasc"

3759. //|.ffunc_bit_sh bit_ror, ror

3760. dasm_put(Dst, 6680, LJ_TISNUM, 1+1, LJ_TISNUM, (unsigned int)(U64x(43380000,00000000)), (unsigned int)((U64x(43380000,00000000))>>32));

3761. #line 2455 "vm_x86.dasc"

3762. //|

3763. //|//-----------------------------------------------------------------------

3764. //|

3765. //|->fff_fallback_2:

3766. //|  mov NARGS:RD, 1+2                        // Other args are ignored, anyway.

3767. //|  jmp ->fff_fallback

3768. //|->fff_fallback_1:

3769. //|  mov NARGS:RD, 1+1                        // Other args are ignored, anyway.

3770. //|->fff_fallback:                        // Call fast function fallback handler.

3771. //|  // BASE = new base, RD = nargs+1

3772. //|  mov L:RB, SAVE_L

3773. //|  mov PC, [BASE-4]                        // Fallback may overwrite PC.

3774. //|  mov SAVE_PC, PC                        // Redundant (but a defined value).

3775. //|  mov L:RB->base, BASE

3776. //|  lea RD, [BASE+NARGS:RD*8-8]

3777. //|  lea RA, [RD+8*LUA_MINSTACK]        // Ensure enough space for handler.

3778. //|  mov L:RB->top, RD

3779. //|  mov CFUNC:RD, [BASE-8]

3780. //|  cmp RA, L:RB->maxstack

3781. //|  ja >5                                // Need to grow stack.

3782. //|.if X64

3783. //|  mov CARG1d, L:RB

3784. //|.else

3785. //|  mov ARG1, L:RB

3786. //|.endif

3787. //|  call aword CFUNC:RD->f                // (lua_State *L)

3788. //|  mov BASE, L:RB->base

3789. dasm_put(Dst, 6755, LJ_TISNUM, 1+2, 1+1, Dt1(->base), 8*LUA_MINSTACK, Dt1(->top), Dt1(->maxstack), Dt8(->f));

3790. #line 2482 "vm_x86.dasc"

3791. //|  // Either throws an error, or recovers and returns -1, 0 or nresults+1.

3792. //|  test RD, RD;  jg ->fff_res                // Returned nresults+1?

3793. //|1:

3794. //|  mov RA, L:RB->top

3795. //|  sub RA, BASE

3796. //|  shr RA, 3

3797. //|  test RD, RD

3798. //|  lea NARGS:RD, [RA+1]

3799. //|  mov LFUNC:RB, [BASE-8]

3800. //|  jne ->vm_call_tail                        // Returned -1?

3801. //|  ins_callt                                // Returned 0: retry fast path.

3802. //|

3803. //|// Reconstruct previous base for vmeta_call during tailcall.

3804. //|->vm_call_tail:

3805. //|  mov RA, BASE

3806. //|  test PC, FRAME_TYPE

3807. //|  jnz >3

3808. //|  movzx RB, PC_RA

3809. //|  not RBa                                // Note: ~RB = -(RB+1)

3810. //|  lea BASE, [BASE+RB*8]                // base = base - (RB+1)*8

3811. //|  jmp ->vm_call_dispatch                // Resolve again for tailcall.

3812. //|3:

3813. //|  mov RB, PC

3814. //|  and RB, -8

3815. //|  sub BASE, RB

3816. //|  jmp ->vm_call_dispatch                // Resolve again for tailcall.

3817. //|

3818. //|5:  // Grow stack for fallback handler.

3819. //|  mov FCARG2, LUA_MINSTACK

3820. //|  mov FCARG1, L:RB

3821. //|  call extern lj_state_growstack@8        // (lua_State *L, int n)

3822. //|  mov BASE, L:RB->base

3823. //|  xor RD, RD                                // Simulate a return 0.

3824. dasm_put(Dst, 6848, Dt1(->base), Dt1(->top), Dt7(->pc), FRAME_TYPE, LUA_MINSTACK, Dt1(->base));

3825. #line 2515 "vm_x86.dasc"

3826. //|  jmp <1                                // Dumb retry (goes through ff first).

3827. //|

3828. //|->fff_gcstep:                        // Call GC step function.

3829. //|  // BASE = new base, RD = nargs+1

3830. //|  pop RBa                                // Must keep stack at same level.

3831. //|  mov TMPa, RBa                        // Save return address

3832. //|  mov L:RB, SAVE_L

3833. //|  mov SAVE_PC, PC                        // Redundant (but a defined value).

3834. //|  mov L:RB->base, BASE

3835. //|  lea RD, [BASE+NARGS:RD*8-8]

3836. //|  mov FCARG1, L:RB

3837. //|  mov L:RB->top, RD

3838. //|  call extern lj_gc_step@4                // (lua_State *L)

3839. //|  mov BASE, L:RB->base

3840. //|  mov RD, L:RB->top

3841. //|  sub RD, BASE

3842. //|  shr RD, 3

3843. //|  add NARGS:RD, 1

3844. //|  mov RBa, TMPa

3845. //|  push RBa                                // Restore return address.

3846. //|  ret

3847. //|

3848. //|//-----------------------------------------------------------------------

3849. //|//-- Special dispatch targets -------------------------------------------

3850. //|//-----------------------------------------------------------------------

3851. //|

3852. //|->vm_record:                                // Dispatch target for recording phase.

3853. //|.if JIT

3854. //|  movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]

3855. //|  test RDL, HOOK_VMEVENT                // No recording while in vmevent.

3856. //|  jnz >5

3857. //|  // Decrement the hookcount for consistency, but always do the call.

3858. //|  test RDL, HOOK_ACTIVE

3859. //|  jnz >1

3860. //|  test RDL, LUA_MASKLINE|LUA_MASKCOUNT

3861. //|  jz >1

3862. //|  dec dword [DISPATCH+DISPATCH_GL(hookcount)]

3863. //|  jmp >1

3864. //|.endif

3865. //|

3866. //|->vm_rethook:                        // Dispatch target for return hooks.

3867. //|  movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]

3868. dasm_put(Dst, 6961, Dt1(->base), Dt1(->top), Dt1(->base), Dt1(->top), DISPATCH_GL(hookmask), HOOK_VMEVENT, HOOK_ACTIVE, LUA_MASKLINE|LUA_MASKCOUNT, DISPATCH_GL(hookcount));

3869. #line 2557 "vm_x86.dasc"

3870. //|  test RDL, HOOK_ACTIVE                // Hook already active?

3871. //|  jnz >5

3872. //|  jmp >1

3873. //|

3874. //|->vm_inshook:                        // Dispatch target for instr/line hooks.

3875. //|  movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]

3876. //|  test RDL, HOOK_ACTIVE                // Hook already active?

3877. //|  jnz >5

3878. //|

3879. //|  test RDL, LUA_MASKLINE|LUA_MASKCOUNT

3880. //|  jz >5

3881. //|  dec dword [DISPATCH+DISPATCH_GL(hookcount)]

3882. //|  jz >1

3883. //|  test RDL, LUA_MASKLINE

3884. //|  jz >5

3885. //|1:

3886. //|  mov L:RB, SAVE_L

3887. dasm_put(Dst, 7059, DISPATCH_GL(hookmask), HOOK_ACTIVE, DISPATCH_GL(hookmask), HOOK_ACTIVE, LUA_MASKLINE|LUA_MASKCOUNT, DISPATCH_GL(hookcount), LUA_MASKLINE);

3888. #line 2574 "vm_x86.dasc"

3889. //|  mov L:RB->base, BASE

3890. //|  mov FCARG2, PC                        // Caveat: FCARG2 == BASE

3891. //|  mov FCARG1, L:RB

3892. //|  // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.

3893. //|  call extern lj_dispatch_ins@8        // (lua_State *L, const BCIns *pc)

3894. //|3:

3895. //|  mov BASE, L:RB->base

3896. //|4:

3897. //|  movzx RA, PC_RA

3898. //|5:

3899. //|  movzx OP, PC_OP

3900. //|  movzx RD, PC_RD

3901. //|.if X64

3902. //|  jmp aword [DISPATCH+OP*8+GG_DISP2STATIC]        // Re-dispatch to static ins.

3903. //|.else

3904. //|  jmp aword [DISPATCH+OP*4+GG_DISP2STATIC]        // Re-dispatch to static ins.

3905. //|.endif

3906. //|

3907. //|->cont_hook:                                // Continue from hook yield.

3908. //|  add PC, 4

3909. //|  mov RA, [RB-24]

3910. //|  mov MULTRES, RA                        // Restore MULTRES for *M ins.

3911. //|  jmp <4

3912. //|

3913. //|->vm_hotloop:                        // Hot loop counter underflow.

3914. //|.if JIT

3915. //|  mov LFUNC:RB, [BASE-8]                // Same as curr_topL(L).

3916. //|  mov RB, LFUNC:RB->pc

3917. //|  movzx RD, byte [RB+PC2PROTO(framesize)]

3918. //|  lea RD, [BASE+RD*8]

3919. //|  mov L:RB, SAVE_L

3920. //|  mov L:RB->base, BASE

3921. //|  mov L:RB->top, RD

3922. //|  mov FCARG2, PC

3923. //|  lea FCARG1, [DISPATCH+GG_DISP2J]

3924. //|  mov aword [DISPATCH+DISPATCH_J(L)], L:RBa

3925. //|  mov SAVE_PC, PC

3926. //|  call extern lj_trace_hot@8                // (jit_State *J, const BCIns *pc)

3927. //|  jmp <3

3928. //|.endif

3929. //|

3930. //|->vm_callhook:                        // Dispatch target for call hooks.

3931. //|  mov SAVE_PC, PC

3932. //|.if JIT

3933. //|  jmp >1

3934. //|.endif

3935. //|

3936. //|->vm_hotcall:                        // Hot call counter underflow.

3937. //|.if JIT

3938. //|  mov SAVE_PC, PC

3939. dasm_put(Dst, 7111, Dt1(->base), Dt1(->base), GG_DISP2STATIC, Dt7(->pc), PC2PROTO(framesize), Dt1(->base), Dt1(->top), GG_DISP2J, DISPATCH_J(L));

3940. #line 2624 "vm_x86.dasc"

3941. //|  or PC, 1                                // Marker for hot call.

3942. //|1:

3943. //|.endif

3944. //|  lea RD, [BASE+NARGS:RD*8-8]

3945. //|  mov L:RB, SAVE_L

3946. //|  mov L:RB->base, BASE

3947. //|  mov L:RB->top, RD

3948. //|  mov FCARG2, PC

3949. //|  mov FCARG1, L:RB

3950. //|  call extern lj_dispatch_call@8        // (lua_State *L, const BCIns *pc)

3951. //|  // ASMFunction returned in eax/rax (RDa).

3952. //|  mov SAVE_PC, 0                        // Invalidate for subsequent line hook.

3953. //|.if JIT

3954. //|  and PC, -2

3955. //|.endif

3956. //|  mov BASE, L:RB->base

3957. //|  mov RAa, RDa

3958. //|  mov RD, L:RB->top

3959. //|  sub RD, BASE

3960. //|  mov RBa, RAa

3961. //|  movzx RA, PC_RA

3962. //|  shr RD, 3

3963. //|  add NARGS:RD, 1

3964. //|  jmp RBa

3965. //|

3966. //|->cont_stitch:                        // Trace stitching.

3967. //|.if JIT

3968. //|  // BASE = base, RC = result, RB = mbase

3969. //|  mov RA, [RB-24]                        // Save previous trace number.

3970. //|  mov TMP1, RA

3971. //|  mov TMP3, DISPATCH                        // Need one more register.

3972. //|  mov DISPATCH, MULTRES

3973. //|  movzx RA, PC_RA

3974. //|  lea RA, [BASE+RA*8]                // Call base.

3975. //|  sub DISPATCH, 1

3976. //|  jz >2

3977. //|1:  // Move results down.

3978. //|.if X64

3979. //|  mov RBa, [RC]

3980. //|  mov [RA], RBa

3981. //|.else

3982. //|  mov RB, [RC]

3983. //|  mov [RA], RB

3984. //|  mov RB, [RC+4]

3985. //|  mov [RA+4], RB

3986. //|.endif

3987. //|  add RC, 8

3988. //|  add RA, 8

3989. //|  sub DISPATCH, 1

3990. //|  jnz <1

3991. //|2:

3992. //|  movzx RC, PC_RA

3993. //|  movzx RB, PC_RB

3994. //|  add RC, RB

3995. //|  lea RC, [BASE+RC*8-8]

3996. //|3:

3997. //|  cmp RC, RA

3998. //|  ja >9                                // More results wanted?

3999. //|

4000. //|  mov DISPATCH, TMP3

4001. //|  mov RB, TMP1                        // Get previous trace number.

4002. //|  mov RA, [DISPATCH+DISPATCH_J(trace)]

4003. //|  mov TRACE:RD, [RA+RB*4]

4004. //|  test TRACE:RD, TRACE:RD

4005. //|  jz ->cont_nop

4006. //|  movzx RD, word TRACE:RD->link

4007. //|  cmp RD, RB

4008. //|  je ->cont_nop                        // Blacklisted.

4009. //|  test RD, RD

4010. dasm_put(Dst, 7236, Dt1(->base), Dt1(->top), Dt1(->base), Dt1(->top), DISPATCH_J(trace), DtD(->link));

4011. #line 2693 "vm_x86.dasc"

4012. //|  jne =>BC_JLOOP                        // Jump to stitched trace.

4013. //|

4014. //|  // Stitch a new trace to the previous trace.

4015. //|  mov [DISPATCH+DISPATCH_J(exitno)], RB

4016. //|  mov L:RB, SAVE_L

4017. //|  mov L:RB->base, BASE

4018. //|  mov FCARG2, PC

4019. //|  lea FCARG1, [DISPATCH+GG_DISP2J]

4020. //|  mov aword [DISPATCH+DISPATCH_J(L)], L:RBa

4021. //|  call extern lj_dispatch_stitch@8        // (jit_State *J, const BCIns *pc)

4022. //|  mov BASE, L:RB->base

4023. //|  jmp ->cont_nop

4024. //|

4025. //|9:  // Fill up results with nil.

4026. //|  mov dword [RA+4], LJ_TNIL

4027. //|  add RA, 8

4028. //|  jmp <3

4029. //|.endif

4030. //|

4031. //|->vm_profhook:                        // Dispatch target for profiler hook.

4032. dasm_put(Dst, 7426, BC_JLOOP, DISPATCH_J(exitno), Dt1(->base), GG_DISP2J, DISPATCH_J(L), Dt1(->base), LJ_TNIL);

4033. #line 2713 "vm_x86.dasc"

4034. #if LJ_HASPROFILE

4035. //|  mov L:RB, SAVE_L

4036. //|  mov L:RB->base, BASE

4037. //|  mov FCARG2, PC                        // Caveat: FCARG2 == BASE

4038. //|  mov FCARG1, L:RB

4039. //|  call extern lj_dispatch_profile@8        // (lua_State *L, const BCIns *pc)

4040. //|  mov BASE, L:RB->base

4041. //|  // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.

4042. //|  sub PC, 4

4043. //|  jmp ->cont_nop

4044. dasm_put(Dst, 7479, Dt1(->base), Dt1(->base));

4045. #line 2723 "vm_x86.dasc"

4046. #endif

4047. //|

4048. //|//-----------------------------------------------------------------------

4049. //|//-- Trace exit handler -------------------------------------------------

4050. //|//-----------------------------------------------------------------------

4051. //|

4052. //|// Called from an exit stub with the exit number on the stack.

4053. //|// The 16 bit exit number is stored with two (sign-extended) push imm8.

4054. //|->vm_exit_handler:

4055. //|.if JIT

4056. //|.if X64

4057. //|  push r13; push r12

4058. //|  push r11; push r10; push r9; push r8

4059. //|  push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp

4060. //|  push rbx; push rdx; push rcx; push rax

4061. //|  movzx RC, byte [rbp-8]                // Reconstruct exit number.

4062. //|  mov RCH, byte [rbp-16]

4063. //|  mov [rbp-8], r15; mov [rbp-16], r14

4064. //|.else

4065. //|  push ebp; lea ebp, [esp+12]; push ebp

4066. //|  push ebx; push edx; push ecx; push eax

4067. //|  movzx RC, byte [ebp-4]                // Reconstruct exit number.

4068. //|  mov RCH, byte [ebp-8]

4069. //|  mov [ebp-4], edi; mov [ebp-8], esi

4070. //|.endif

4071. //|  // Caveat: DISPATCH is ebx.

4072. //|  mov DISPATCH, [ebp]

4073. //|  mov RA, [DISPATCH+DISPATCH_GL(vmstate)]        // Get trace number.

4074. //|  set_vmstate EXIT

4075. //|  mov [DISPATCH+DISPATCH_J(exitno)], RC

4076. //|  mov [DISPATCH+DISPATCH_J(parent)], RA

4077. //|.if X64

4078. //|.if X64WIN

4079. //|  sub rsp, 16*8+4*8                        // Room for SSE regs + save area.

4080. //|.else

4081. //|  sub rsp, 16*8                        // Room for SSE regs.

4082. //|.endif

4083. //|  add rbp, -128

4084. //|  movsd qword [rbp-8],   xmm15; movsd qword [rbp-16],  xmm14

4085. //|  movsd qword [rbp-24],  xmm13; movsd qword [rbp-32],  xmm12

4086. //|  movsd qword [rbp-40],  xmm11; movsd qword [rbp-48],  xmm10

4087. //|  movsd qword [rbp-56],  xmm9;  movsd qword [rbp-64],  xmm8

4088. //|  movsd qword [rbp-72],  xmm7;  movsd qword [rbp-80],  xmm6

4089. //|  movsd qword [rbp-88],  xmm5;  movsd qword [rbp-96],  xmm4

4090. //|  movsd qword [rbp-104], xmm3;  movsd qword [rbp-112], xmm2

4091. //|  movsd qword [rbp-120], xmm1;  movsd qword [rbp-128], xmm0

4092. //|.else

4093. //|  sub esp, 8*8+16                        // Room for SSE regs + args.

4094. //|  movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6

4095. //|  movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4

4096. //|  movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2

4097. //|  movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0

4098. //|.endif

4099. //|  // Caveat: RB is ebp.

4100. //|  mov L:RB, [DISPATCH+DISPATCH_GL(cur_L)]

4101. //|  mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]

4102. //|  mov aword [DISPATCH+DISPATCH_J(L)], L:RBa

4103. //|  mov L:RB->base, BASE

4104. //|.if X64WIN

4105. //|  lea CARG2, [rsp+4*8]

4106. //|.elif X64

4107. //|  mov CARG2, rsp

4108. //|.else

4109. //|  lea FCARG2, [esp+16]

4110. //|.endif

4111. //|  lea FCARG1, [DISPATCH+GG_DISP2J]

4112. //|  mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0

4113. //|  call extern lj_trace_exit@8        // (jit_State *J, ExitState *ex)

4114. //|  // MULTRES or negated error code returned in eax (RD).

4115. //|  mov RAa, L:RB->cframe

4116. //|  and RAa, CFRAME_RAWMASK

4117. //|.if X64WIN

4118. //|  // Reposition stack later.

4119. //|.elif X64

4120. //|  mov rsp, RAa                        // Reposition stack to C frame.

4121. //|.else

4122. //|  mov esp, RAa                        // Reposition stack to C frame.

4123. //|.endif

4124. //|  mov [RAa+CFRAME_OFS_L], L:RB        // Set SAVE_L (on-trace resume/yield).

4125. //|  mov BASE, L:RB->base

4126. //|  mov PC, [RAa+CFRAME_OFS_PC]        // Get SAVE_PC.

4127. //|.if X64

4128. //|  jmp >1

4129. //|.endif

4130. //|.endif

4131. //|->vm_exit_interp:

4132. //|  // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.

4133. //|.if JIT

4134. //|.if X64

4135. //|  // Restore additional callee-save registers only used in compiled code.

4136. //|.if X64WIN

4137. //|  lea RAa, [rsp+9*16+4*8]

4138. //|1:

4139. //|  movdqa xmm15, [RAa-9*16]

4140. //|  movdqa xmm14, [RAa-8*16]

4141. //|  movdqa xmm13, [RAa-7*16]

4142. //|  movdqa xmm12, [RAa-6*16]

4143. //|  movdqa xmm11, [RAa-5*16]

4144. //|  movdqa xmm10, [RAa-4*16]

4145. //|  movdqa xmm9, [RAa-3*16]

4146. //|  movdqa xmm8, [RAa-2*16]

4147. //|  movdqa xmm7, [RAa-1*16]

4148. //|  mov rsp, RAa                        // Reposition stack to C frame.

4149. //|  movdqa xmm6, [RAa]

4150. //|  mov r15, CSAVE_3

4151. //|  mov r14, CSAVE_4

4152. //|.else

4153. //|  add rsp, 16                        // Reposition stack to C frame.

4154. dasm_put(Dst, 7507, DISPATCH_GL(vmstate), DISPATCH_GL(vmstate), ~LJ_VMST_EXIT, DISPATCH_J(exitno), DISPATCH_J(parent), 16*8, DISPATCH_GL(cur_L), DISPATCH_GL(jit_base), DISPATCH_J(L), Dt1(->base), GG_DISP2J, DISPATCH_GL(jit_base), Dt1(->cframe), CFRAME_RAWMASK, CFRAME_OFS_L, Dt1(->base), CFRAME_OFS_PC);

4155. #line 2831 "vm_x86.dasc"

4156. //|1:

4157. //|.endif

4158. //|  mov r13, TMPa

4159. //|  mov r12, TMPQ

4160. //|.endif

4161. dasm_put(Dst, 7750);

4162. #line 2836 "vm_x86.dasc"

4163. #ifdef LUA_USE_TRACE_LOGS

4164. //|.if X64

4165. //|  mov FCARG1, SAVE_L

4166. //|  mov L:FCARG1->base, BASE

4167. //|  mov RB, RD     // Save RD

4168. //|  mov TMP1, PC  // Save PC

4169. //|  mov CARG3d, PC   // CARG3d == BASE

4170. //|  mov FCARG2, dword [DISPATCH+DISPATCH_GL(vmstate)]

4171. //|  call extern lj_log_trace_direct_exit@8

4172. //|  mov PC, TMP1

4173. //|  mov RD, RB

4174. //|  mov RB, SAVE_L

4175. //|  mov BASE, L:RB->base

4176. //|.endif

4177. dasm_put(Dst, 7766, Dt1(->base), DISPATCH_GL(vmstate), Dt1(->base));

4178. #line 2850 "vm_x86.dasc"

4179. #endif

4180. //|  test RD, RD; js >9                        // Check for error from exit.

4181. //|  mov L:RB, SAVE_L

4182. //|  mov MULTRES, RD

4183. //|  mov LFUNC:KBASE, [BASE-8]

4184. //|  mov KBASE, LFUNC:KBASE->pc

4185. //|  mov KBASE, [KBASE+PC2PROTO(k)]

4186. //|  mov L:RB->base, BASE

4187. //|  mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0

4188. //|  set_vmstate INTERP

4189. //|  // Modified copy of ins_next which handles function header dispatch, too.

4190. //|  mov RC, [PC]

4191. //|  movzx RA, RCH

4192. //|  movzx OP, RCL

4193. //|  add PC, 4

4194. //|  shr RC, 16

4195. //|  cmp OP, BC_FUNCF                        // Function header?

4196. //|  jb >3

4197. //|  cmp OP, BC_FUNCC+2                        // Fast function?

4198. //|  jae >4

4199. //|2:

4200. //|  mov RC, MULTRES                        // RC/RD holds nres+1.

4201. //|3:

4202. //|.if X64

4203. //|  jmp aword [DISPATCH+OP*8]

4204. //|.else

4205. //|  jmp aword [DISPATCH+OP*4]

4206. //|.endif

4207. //|

4208. //|4:  // Check frame below fast function.

4209. //|  mov RC, [BASE-4]

4210. //|  test RC, FRAME_TYPE

4211. //|  jnz <2                                // Trace stitching continuation?

4212. //|  // Otherwise set KBASE for Lua function below fast function.

4213. //|  movzx RC, byte [RC-3]

4214. dasm_put(Dst, 7801, Dt7(->pc), PC2PROTO(k), Dt1(->base), DISPATCH_GL(jit_base), DISPATCH_GL(vmstate), ~LJ_VMST_INTERP, BC_FUNCF, BC_FUNCC+2, FRAME_TYPE);

4215. #line 2885 "vm_x86.dasc"

4216. //|  not RCa

4217. //|  mov LFUNC:KBASE, [BASE+RC*8-8]

4218. //|  mov KBASE, LFUNC:KBASE->pc

4219. //|  mov KBASE, [KBASE+PC2PROTO(k)]

4220. //|  jmp <2

4221. //|

4222. //|9:  // Rethrow error from the right C frame.

4223. //|  neg RD

4224. //|  mov FCARG1, L:RB

4225. //|  mov FCARG2, RD

4226. //|  call extern lj_err_throw@8                // (lua_State *L, int errcode)

4227. //|.endif

4228. //|

4229. //|//-----------------------------------------------------------------------

4230. //|//-- Math helper functions ----------------------------------------------

4231. //|//-----------------------------------------------------------------------

4232. //|

4233. //|// FP value rounding. Called by math.floor/math.ceil fast functions

4234. //|// and from JIT code. arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.

4235. //|.macro vm_round, name, mode, cond

4236. //|->name:

4237. //|.if not X64 and cond

4238. //|  movsd xmm0, qword [esp+4]

4239. //|  call ->name .. _sse

4240. //|  movsd qword [esp+4], xmm0  // Overwrite callee-owned arg.

4241. //|  fld qword [esp+4]

4242. //|  ret

4243. //|.endif

4244. //|

4245. //|->name .. _sse:

4246. //|  sseconst_abs xmm2, RDa

4247. //|  sseconst_2p52 xmm3, RDa

4248. //|  movaps xmm1, xmm0

4249. //|  andpd xmm1, xmm2                        // |x|

4250. //|  ucomisd xmm3, xmm1                        // No truncation if 2^52 <= |x|.

4251. //|  jbe >1

4252. //|  andnpd xmm2, xmm0                        // Isolate sign bit.

4253. //|.if mode == 2                // trunc(x)?

4254. //|  movaps xmm0, xmm1

4255. //|  addsd xmm1, xmm3                        // (|x| + 2^52) - 2^52

4256. //|  subsd xmm1, xmm3

4257. //|  sseconst_1 xmm3, RDa

4258. //|  cmpsd xmm0, xmm1, 1                // |x| < result?

4259. //|  andpd xmm0, xmm3

4260. //|  subsd xmm1, xmm0                        // If yes, subtract -1.

4261. //|  orpd xmm1, xmm2                        // Merge sign bit back in.

4262. //|.else

4263. //|  addsd xmm1, xmm3                        // (|x| + 2^52) - 2^52

4264. //|  subsd xmm1, xmm3

4265. //|  orpd xmm1, xmm2                        // Merge sign bit back in.

4266. //|  .if mode == 1                // ceil(x)?

4267. //|    sseconst_m1 xmm2, RDa                // Must subtract -1 to preserve -0.

4268. //|    cmpsd xmm0, xmm1, 6                // x > result?

4269. //|  .else                        // floor(x)?

4270. //|    sseconst_1 xmm2, RDa

4271. //|    cmpsd xmm0, xmm1, 1                // x < result?

4272. //|  .endif

4273. //|  andpd xmm0, xmm2

4274. //|  subsd xmm1, xmm0                        // If yes, subtract +-1.

4275. //|.endif

4276. //|  movaps xmm0, xmm1

4277. //|1:

4278. //|  ret

4279. //|.endmacro

4280. //|

4281. //|  vm_round vm_floor, 0, 1

4282. //|  vm_round vm_ceil,  1, JIT

4283. dasm_put(Dst, 7901, Dt7(->pc), PC2PROTO(k), (unsigned int)(U64x(7fffffff,ffffffff)), (unsigned int)((U64x(7fffffff,ffffffff))>>32), (unsigned int)(U64x(43300000,00000000)), (unsigned int)((U64x(43300000,00000000))>>32), (unsigned int)(U64x(3ff00000,00000000)), (unsigned int)((U64x(3ff00000,00000000))>>32), (unsigned int)(U64x(7fffffff,ffffffff)), (unsigned int)((U64x(7fffffff,ffffffff))>>32));

4284. #line 2952 "vm_x86.dasc"

4285. //|  vm_round vm_trunc, 2, JIT

4286. //|

4287. //|// FP modulo x%y. Called by BC_MOD* and vm_arith.

4288. //|->vm_mod:

4289. //|// Args in xmm0/xmm1, return value in xmm0.

4290. //|// Caveat: xmm0-xmm5 and RC (eax) modified!

4291. //|  movaps xmm5, xmm0

4292. //|  divsd xmm0, xmm1

4293. //|  sseconst_abs xmm2, RDa

4294. dasm_put(Dst, 8036, (unsigned int)(U64x(43300000,00000000)), (unsigned int)((U64x(43300000,00000000))>>32), (unsigned int)(U64x(bff00000,00000000)), (unsigned int)((U64x(bff00000,00000000))>>32), (unsigned int)(U64x(7fffffff,ffffffff)), (unsigned int)((U64x(7fffffff,ffffffff))>>32), (unsigned int)(U64x(43300000,00000000)), (unsigned int)((U64x(43300000,00000000))>>32), (unsigned int)(U64x(3ff00000,00000000)), (unsigned int)((U64x(3ff00000,00000000))>>32), (unsigned int)(U64x(7fffffff,ffffffff)), (unsigned int)((U64x(7fffffff,ffffffff))>>32));

4295. #line 2961 "vm_x86.dasc"

4296. //|  sseconst_2p52 xmm3, RDa

4297. //|  movaps xmm4, xmm0

4298. //|  andpd xmm4, xmm2                        // |x/y|

4299. //|  ucomisd xmm3, xmm4                        // No truncation if 2^52 <= |x/y|.

4300. //|  jbe >1

4301. //|  andnpd xmm2, xmm0                        // Isolate sign bit.

4302. //|  addsd xmm4, xmm3                        // (|x/y| + 2^52) - 2^52

4303. //|  subsd xmm4, xmm3

4304. //|  orpd xmm4, xmm2                        // Merge sign bit back in.

4305. //|  sseconst_1 xmm2, RDa

4306. //|  cmpsd xmm0, xmm4, 1                // x/y < result?

4307. //|  andpd xmm0, xmm2

4308. //|  subsd xmm4, xmm0                        // If yes, subtract 1.0.

4309. //|  movaps xmm0, xmm5

4310. //|  mulsd xmm1, xmm4

4311. //|  subsd xmm0, xmm1

4312. //|  ret

4313. //|1:

4314. //|  mulsd xmm1, xmm0

4315. //|  movaps xmm0, xmm5

4316. //|  subsd xmm0, xmm1

4317. //|  ret

4318. //|

4319. //|// Args in xmm0/eax. Ret in xmm0. xmm0-xmm1 and eax modified.

4320. //|->vm_powi_sse:

4321. //|  cmp eax, 1; jle >6                        // i<=1?

4322. //|  // Now 1 < (unsigned)i <= 0x80000000.

4323. //|1:  // Handle leading zeros.

4324. //|  test eax, 1; jnz >2

4325. //|  mulsd xmm0, xmm0

4326. //|  shr eax, 1

4327. //|  jmp <1

4328. //|2:

4329. //|  shr eax, 1; jz >5

4330. //|  movaps xmm1, xmm0

4331. //|3:  // Handle trailing bits.

4332. //|  mulsd xmm0, xmm0

4333. //|  shr eax, 1; jz >4

4334. //|  jnc <3

4335. dasm_put(Dst, 8216, (unsigned int)(U64x(43300000,00000000)), (unsigned int)((U64x(43300000,00000000))>>32), (unsigned int)(U64x(3ff00000,00000000)), (unsigned int)((U64x(3ff00000,00000000))>>32));

4336. #line 3000 "vm_x86.dasc"

4337. //|  mulsd xmm1, xmm0

4338. //|  jmp <3

4339. //|4:

4340. //|  mulsd xmm0, xmm1

4341. //|5:

4342. //|  ret

4343. //|6:

4344. //|  je <5                                // x^1 ==> x

4345. //|  jb >7                                // x^0 ==> 1

4346. //|  neg eax

4347. //|  call <1

4348. //|  sseconst_1 xmm1, RDa

4349. //|  divsd xmm1, xmm0

4350. //|  movaps xmm0, xmm1

4351. //|  ret

4352. //|7:

4353. //|  sseconst_1 xmm0, RDa

4354. //|  ret

4355. //|

4356. //|//-----------------------------------------------------------------------

4357. //|//-- Miscellaneous functions --------------------------------------------

4358. //|//-----------------------------------------------------------------------

4359. //|

4360. //|// int lj_vm_cpuid(uint32_t f, uint32_t res[4])

4361. //|->vm_cpuid:

4362. //|.if X64

4363. //|  mov eax, CARG1d

4364. //|  .if X64WIN; push rsi; mov rsi, CARG2; .endif

4365. //|  push rbx

4366. //|  cpuid

4367. //|  mov [rsi], eax

4368. //|  mov [rsi+4], ebx

4369. //|  mov [rsi+8], ecx

4370. //|  mov [rsi+12], edx

4371. //|  pop rbx

4372. //|  .if X64WIN; pop rsi; .endif

4373. //|  ret

4374. //|.else

4375. //|  pushfd

4376. //|  pop edx

4377. //|  mov ecx, edx

4378. //|  xor edx, 0x00200000                // Toggle ID bit in flags.

4379. //|  push edx

4380. //|  popfd

4381. //|  pushfd

4382. //|  pop edx

4383. //|  xor eax, eax                        // Zero means no features supported.

4384. //|  cmp ecx, edx

4385. //|  jz >1                                // No ID toggle means no CPUID support.

4386. //|  mov eax, [esp+4]                        // Argument 1 is function number.

4387. //|  push edi

4388. //|  push ebx

4389. //|  cpuid

4390. //|  mov edi, [esp+16]                        // Argument 2 is result area.

4391. //|  mov [edi], eax

4392. //|  mov [edi+4], ebx

4393. //|  mov [edi+8], ecx

4394. //|  mov [edi+12], edx

4395. //|  pop ebx

4396. //|  pop edi

4397. //|1:

4398. //|  ret

4399. //|.endif

4400. //|

4401. //|//-----------------------------------------------------------------------

4402. //|//-- Assertions ---------------------------------------------------------

4403. //|//-----------------------------------------------------------------------

4404. //|

4405. //|->assert_bad_for_arg_type:

4406. dasm_put(Dst, 8374, (unsigned int)(U64x(3ff00000,00000000)), (unsigned int)((U64x(3ff00000,00000000))>>32), (unsigned int)(U64x(3ff00000,00000000)), (unsigned int)((U64x(3ff00000,00000000))>>32));

4407. #line 3069 "vm_x86.dasc"

4408. #ifdef LUA_USE_ASSERT

4409. //|  int3

4410. dasm_put(Dst, 8467);

4411. #line 3071 "vm_x86.dasc"

4412. #endif

4413. //|  int3

4414. //|

4415. //|//-----------------------------------------------------------------------

4416. //|//-- FFI helper functions -----------------------------------------------

4417. //|//-----------------------------------------------------------------------

4418. //|

4419. //|// Handler for callback functions. Callback slot number in ah/al.

4420. //|->vm_ffi_callback:

4421. //|.if FFI

4422. //|.type CTSTATE, CTState, PC

4423. ‌#define DtF(_V) (int)(ptrdiff_t)&(((CTState *)0)_V)

4424. #line 3082 "vm_x86.dasc"

4425. //|.if not X64

4426. //|  sub esp, 16                        // Leave room for SAVE_ERRF etc.

4427. //|.endif

4428. //|  saveregs_        // ebp/rbp already saved. ebp now holds global_State *.

4429. //|  lea DISPATCH, [ebp+GG_G2DISP]

4430. //|  mov CTSTATE, GL:ebp->ctype_state

4431. //|  movzx eax, ax

4432. //|  mov CTSTATE->cb.slot, eax

4433. //|.if X64

4434. //|  mov CTSTATE->cb.gpr[0], CARG1

4435. //|  mov CTSTATE->cb.gpr[1], CARG2

4436. //|  mov CTSTATE->cb.gpr[2], CARG3

4437. //|  mov CTSTATE->cb.gpr[3], CARG4

4438. //|  movsd qword CTSTATE->cb.fpr[0], xmm0

4439. //|  movsd qword CTSTATE->cb.fpr[1], xmm1

4440. //|  movsd qword CTSTATE->cb.fpr[2], xmm2

4441. //|  movsd qword CTSTATE->cb.fpr[3], xmm3

4442. //|.if X64WIN

4443. //|  lea rax, [rsp+CFRAME_SIZE+4*8]

4444. //|.else

4445. //|  lea rax, [rsp+CFRAME_SIZE]

4446. //|  mov CTSTATE->cb.gpr[4], CARG5

4447. //|  mov CTSTATE->cb.gpr[5], CARG6

4448. //|  movsd qword CTSTATE->cb.fpr[4], xmm4

4449. //|  movsd qword CTSTATE->cb.fpr[5], xmm5

4450. //|  movsd qword CTSTATE->cb.fpr[6], xmm6

4451. //|  movsd qword CTSTATE->cb.fpr[7], xmm7

4452. //|.endif

4453. //|  mov CTSTATE->cb.stack, rax

4454. //|  mov CARG2, rsp

4455. dasm_put(Dst, 8469, GG_G2DISP, Dt2(->ctype_state), DtF(->cb.slot), DtF(->cb.gpr[0]), DtF(->cb.gpr[1]), DtF(->cb.gpr[2]), DtF(->cb.gpr[3]), DtF(->cb.fpr[0]), DtF(->cb.fpr[1]), DtF(->cb.fpr[2]), DtF(->cb.fpr[3]), CFRAME_SIZE, DtF(->cb.gpr[4]), DtF(->cb.gpr[5]), DtF(->cb.fpr[4]), DtF(->cb.fpr[5]), DtF(->cb.fpr[6]), DtF(->cb.fpr[7]), DtF(->cb.stack));

4456. #line 3112 "vm_x86.dasc"

4457. //|.else

4458. //|  lea eax, [esp+CFRAME_SIZE+16]

4459. //|  mov CTSTATE->cb.gpr[0], FCARG1

4460. //|  mov CTSTATE->cb.gpr[1], FCARG2

4461. //|  mov CTSTATE->cb.stack, eax

4462. //|  mov FCARG1, [esp+CFRAME_SIZE+12]        // Move around misplaced retaddr/ebp.

4463. //|  mov FCARG2, [esp+CFRAME_SIZE+8]

4464. //|  mov SAVE_RET, FCARG1

4465. //|  mov SAVE_R4, FCARG2

4466. //|  mov FCARG2, esp

4467. //|.endif

4468. //|  mov SAVE_PC, CTSTATE                // Any value outside of bytecode is ok.

4469. //|  mov FCARG1, CTSTATE

4470. //|  call extern lj_ccallback_enter@8        // (CTState *cts, void *cf)

4471. //|  // lua_State * returned in eax (RD).

4472. //|  set_vmstate INTERP

4473. //|  mov BASE, L:RD->base

4474. //|  mov RD, L:RD->top

4475. //|  sub RD, BASE

4476. //|  mov LFUNC:RB, [BASE-8]

4477. //|  shr RD, 3

4478. //|  add RD, 1

4479. //|  ins_callt

4480. //|.endif

4481. //|

4482. //|->cont_ffi_callback:                        // Return from FFI callback.

4483. //|.if FFI

4484. //|  mov L:RA, SAVE_L

4485. //|  mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)]

4486. //|  mov aword CTSTATE->L, L:RAa

4487. //|  mov L:RA->base, BASE

4488. //|  mov L:RA->top, RB

4489. //|  mov FCARG1, CTSTATE

4490. //|  mov FCARG2, RC

4491. //|  call extern lj_ccallback_leave@8        // (CTState *cts, TValue *o)

4492. //|.if X64

4493. //|  mov rax, CTSTATE->cb.gpr[0]

4494. //|  movsd xmm0, qword CTSTATE->cb.fpr[0]

4495. //|  jmp ->vm_leave_unw

4496. //|.else

4497. //|  mov L:RB, SAVE_L

4498. //|  mov eax, CTSTATE->cb.gpr[0]

4499. //|  mov edx, CTSTATE->cb.gpr[1]

4500. //|  cmp dword CTSTATE->cb.gpr[2], 1

4501. //|  jb >7

4502. //|  je >6

4503. //|  fld qword CTSTATE->cb.fpr[0].d

4504. //|  jmp >7

4505. //|6:

4506. //|  fld dword CTSTATE->cb.fpr[0].f

4507. //|7:

4508. //|  mov ecx, L:RB->top

4509. //|  movzx ecx, word [ecx+6]                // Get stack adjustment and copy up.

4510. //|  mov SAVE_L, ecx                        // Must be one slot above SAVE_RET

4511. //|  restoreregs

4512. //|  pop ecx                                // Move return addr from SAVE_RET.

4513. //|  add esp, [esp]                        // Adjust stack.

4514. //|  add esp, 16

4515. //|  push ecx

4516. //|  ret

4517. //|.endif

4518. //|.endif

4519. //|

4520. //|->vm_ffi_call@4:                        // Call C function via FFI.

4521. //|  // Caveat: needs special frame unwinding, see below.

4522. //|.if FFI

4523. //|.if X64

4524. //|  .type CCSTATE, CCallState, rbx

4525. ‌#define Dt10(_V) (int)(ptrdiff_t)&(((CCallState *)0)_V)

4526. #line 3180 "vm_x86.dasc"

4527. //|  push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1

4528. //|.else

4529. //|  .type CCSTATE, CCallState, ebx

4530. //|  push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1

4531. //|.endif

4532. //|

4533. //|  // Readjust stack.

4534. //|.if X64

4535. //|  mov eax, CCSTATE->spadj

4536. //|  sub rsp, rax

4537. //|.else

4538. //|  sub esp, CCSTATE->spadj

4539. //|.if WIN

4540. //|  mov CCSTATE->spadj, esp

4541. //|.endif

4542. //|.endif

4543. //|

4544. //|  // Copy stack slots.

4545. //|  movzx ecx, byte CCSTATE->nsp

4546. //|  sub ecx, 1

4547. //|  js >2

4548. //|1:

4549. //|.if X64

4550. //|  mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)]

4551. dasm_put(Dst, 8578, DISPATCH_GL(vmstate), ~LJ_VMST_INTERP, Dt1(->base), Dt1(->top), Dt7(->pc), DISPATCH_GL(ctype_state), DtF(->L), Dt1(->base), Dt1(->top), DtF(->cb.gpr[0]), DtF(->cb.fpr[0]), Dt10(->spadj), Dt10(->nsp));

4552. #line 3204 "vm_x86.dasc"

4553. //|  mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax

4554. //|.else

4555. //|  mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)]

4556. //|  mov [esp+ecx*4], eax

4557. //|.endif

4558. //|  sub ecx, 1

4559. //|  jns <1

4560. //|2:

4561. //|

4562. //|.if X64

4563. //|  movzx eax, byte CCSTATE->nfpr

4564. //|  mov CARG1, CCSTATE->gpr[0]

4565. //|  mov CARG2, CCSTATE->gpr[1]

4566. //|  mov CARG3, CCSTATE->gpr[2]

4567. //|  mov CARG4, CCSTATE->gpr[3]

4568. //|.if not X64WIN

4569. //|  mov CARG5, CCSTATE->gpr[4]

4570. //|  mov CARG6, CCSTATE->gpr[5]

4571. //|.endif

4572. //|  test eax, eax; jz >5

4573. //|  movaps xmm0, CCSTATE->fpr[0]

4574. //|  movaps xmm1, CCSTATE->fpr[1]

4575. //|  movaps xmm2, CCSTATE->fpr[2]

4576. //|  movaps xmm3, CCSTATE->fpr[3]

4577. //|.if not X64WIN

4578. //|  cmp eax, 4; jbe >5

4579. //|  movaps xmm4, CCSTATE->fpr[4]

4580. dasm_put(Dst, 8709, offsetof(CCallState, stack), CCALL_SPS_EXTRA*8, Dt10(->nfpr), Dt10(->gpr[0]), Dt10(->gpr[1]), Dt10(->gpr[2]), Dt10(->gpr[3]), Dt10(->gpr[4]), Dt10(->gpr[5]), Dt10(->fpr[0]), Dt10(->fpr[1]), Dt10(->fpr[2]), Dt10(->fpr[3]));

4581. #line 3231 "vm_x86.dasc"

4582. //|  movaps xmm5, CCSTATE->fpr[5]

4583. //|  movaps xmm6, CCSTATE->fpr[6]

4584. //|  movaps xmm7, CCSTATE->fpr[7]

4585. //|.endif

4586. //|5:

4587. //|.else

4588. //|  mov FCARG1, CCSTATE->gpr[0]

4589. //|  mov FCARG2, CCSTATE->gpr[1]

4590. //|.endif

4591. //|

4592. //|  call aword CCSTATE->func

4593. //|

4594. //|.if X64

4595. //|  mov CCSTATE->gpr[0], rax

4596. //|  movaps CCSTATE->fpr[0], xmm0

4597. //|.if not X64WIN

4598. //|  mov CCSTATE->gpr[1], rdx

4599. //|  movaps CCSTATE->fpr[1], xmm1

4600. //|.endif

4601. //|.else

4602. //|  mov CCSTATE->gpr[0], eax

4603. //|  mov CCSTATE->gpr[1], edx

4604. //|  cmp byte CCSTATE->resx87, 1

4605. //|  jb >7

4606. //|  je >6

4607. //|  fstp qword CCSTATE->fpr[0].d[0]

4608. //|  jmp >7

4609. //|6:

4610. //|  fstp dword CCSTATE->fpr[0].f[0]

4611. //|7:

4612. //|.if WIN

4613. //|  sub CCSTATE->spadj, esp

4614. //|.endif

4615. //|.endif

4616. //|

4617. //|.if X64

4618. //|  mov rbx, [rbp-8]; leave; ret

4619. //|.else

4620. //|  mov ebx, [ebp-4]; leave; ret

4621. //|.endif

4622. //|.endif

4623. //|// Note: vm_ffi_call must be the last function in this object file!

4624. //|

4625. //|//-----------------------------------------------------------------------

4626. dasm_put(Dst, 8790, Dt10(->fpr[4]), Dt10(->fpr[5]), Dt10(->fpr[6]), Dt10(->fpr[7]), Dt10(->func), Dt10(->gpr[0]), Dt10(->fpr[0]), Dt10(->gpr[1]), Dt10(->fpr[1]));

4627. #line 3275 "vm_x86.dasc"

4628. }

4630. /* Generate the code for a single instruction. */

4631. ‌static void build_ins(BuildCtx *ctx, BCOp op, int defop)

4632. {

4633. int vk = 0;

4634. //|// Note: aligning all instructions does not pay off.

4635. //|=>defop:

4636. dasm_put(Dst, 8836, defop);

4637. #line 3283 "vm_x86.dasc"

4639. switch (op) {

4641. /* -- Comparison ops ---------------------------------------------------- */

4643. /* Remember: all ops branch for a true comparison, fall through otherwise. */

4645. //|.macro jmp_comp, lt, ge, le, gt, target

4646. //||switch (op) {

4647. //||case BC_ISLT:

4648. //|   lt target

4649. //||break;

4650. //||case BC_ISGE:

4651. //|   ge target

4652. //||break;

4653. //||case BC_ISLE:

4654. //|   le target

4655. //||break;

4656. //||case BC_ISGT:

4657. //|   gt target

4658. //||break;

4659. //||default: break;  /* Shut up GCC. */

4660. //||}

4661. //|.endmacro

4663. case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:

4664. //|  // RA = src1, RD = src2, JMP with RD = target

4665. //|  ins_AD

4666. //|.if DUALNUM

4667. //|  checkint RA, >7

4668. //|  checkint RD, >8

4669. //|  mov RB, dword [BASE+RA*8]

4670. //|  add PC, 4

4671. //|  cmp RB, dword [BASE+RD*8]

4672. //|  jmp_comp jge, jl, jg, jle, >9

4673. dasm_put(Dst, 8838, LJ_TISNUM, LJ_TISNUM);

4674. switch (op) {

4675. case BC_ISLT:

4676. dasm_put(Dst, 8868);

4677. break;

4678. case BC_ISGE:

4679. dasm_put(Dst, 8873);

4680. break;

4681. case BC_ISLE:

4682. dasm_put(Dst, 8878);

4683. break;

4684. case BC_ISGT:

4685. dasm_put(Dst, 8883);

4686. break;

4687. default: break;  /* Shut up GCC. */

4688. }

4689. #line 3318 "vm_x86.dasc"

4690. //|6:

4691. //|  movzx RD, PC_RD

4692. //|  branchPC RD

4693. //|9:

4694. //|  ins_next

4695. //|

4696. //|7:  // RA is not an integer.

4697. //|  ja ->vmeta_comp

4698. //|  // RA is a number.

4699. //|  cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp

4700. //|  // RA is a number, RD is an integer.

4701. //|  cvtsi2sd xmm0, dword [BASE+RD*8]

4702. //|  jmp >2

4703. //|

4704. //|8:  // RA is an integer, RD is not an integer.

4705. //|  ja ->vmeta_comp

4706. //|  // RA is an integer, RD is a number.

4707. //|  cvtsi2sd xmm1, dword [BASE+RA*8]

4708. //|  movsd xmm0, qword [BASE+RD*8]

4709. //|  add PC, 4

4710. //|  ucomisd xmm0, xmm1

4711. //|  jmp_comp jbe, ja, jb, jae, <9

4712. dasm_put(Dst, 8888, -BCBIAS_J*4, LJ_TISNUM);

4713. switch (op) {

4714. case BC_ISLT:

4715. dasm_put(Dst, 8978);

4716. break;

4717. case BC_ISGE:

4718. dasm_put(Dst, 8983);

4719. break;

4720. case BC_ISLE:

4721. dasm_put(Dst, 8988);

4722. break;

4723. case BC_ISGT:

4724. dasm_put(Dst, 8993);

4725. break;

4726. default: break;  /* Shut up GCC. */

4727. }

4728. #line 3340 "vm_x86.dasc"

4729. //|  jmp <6

4730. //|.else

4731. //|  checknum RA, ->vmeta_comp

4732. //|  checknum RD, ->vmeta_comp

4733. //|.endif

4734. //|1:

4735. //|  movsd xmm0, qword [BASE+RD*8]

4736. //|2:

4737. //|  add PC, 4

4738. //|  ucomisd xmm0, qword [BASE+RA*8]

4739. //|3:

4740. //|  // Unordered: all of ZF CF PF set, ordered: PF clear.

4741. //|  // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.

4742. //|.if DUALNUM

4743. //|  jmp_comp jbe, ja, jb, jae, <9

4744. dasm_put(Dst, 8998);

4745. switch (op) {

4746. case BC_ISLT:

4747. dasm_put(Dst, 8978);

4748. break;

4749. case BC_ISGE:

4750. dasm_put(Dst, 8983);

4751. break;

4752. case BC_ISLE:

4753. dasm_put(Dst, 8988);

4754. break;

4755. case BC_ISGT:

4756. dasm_put(Dst, 8993);

4757. break;

4758. default: break;  /* Shut up GCC. */

4759. }

4760. #line 3355 "vm_x86.dasc"

4761. //|  jmp <6

4762. //|.else

4763. //|  jmp_comp jbe, ja, jb, jae, >1

4764. //|  movzx RD, PC_RD

4765. //|  branchPC RD

4766. //|1:

4767. //|  ins_next

4768. //|.endif

4769. dasm_put(Dst, 9023);

4770. #line 3363 "vm_x86.dasc"

4771. break;

4773. case BC_ISEQV: case BC_ISNEV:

4774. vk = op == BC_ISEQV;

4775. //|  ins_AD        // RA = src1, RD = src2, JMP with RD = target

4776. //|  mov RB, [BASE+RD*8+4]

4777. //|  add PC, 4

4778. //|.if DUALNUM

4779. //|  cmp RB, LJ_TISNUM; jne >7

4780. //|  checkint RA, >8

4781. //|  mov RB, dword [BASE+RD*8]

4782. //|  cmp RB, dword [BASE+RA*8]

4783. dasm_put(Dst, 9028, LJ_TISNUM, LJ_TISNUM);

4784. #line 3375 "vm_x86.dasc"

4785. if (vk) {

4786. //|  jne >9

4787. dasm_put(Dst, 9060);

4788. #line 3377 "vm_x86.dasc"

4789. } else {

4790. //|  je >9

4791. dasm_put(Dst, 9065);

4792. #line 3379 "vm_x86.dasc"

4793. }

4794. //|  movzx RD, PC_RD

4795. //|  branchPC RD

4796. //|9:

4797. //|  ins_next

4798. //|

4799. //|7:  // RD is not an integer.

4800. //|  ja >5

4801. //|  // RD is a number.

4802. //|  cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5

4803. //|  // RD is a number, RA is an integer.

4804. //|  cvtsi2sd xmm0, dword [BASE+RA*8]

4805. //|  jmp >2

4806. //|

4807. //|8:  // RD is an integer, RA is not an integer.

4808. //|  ja >5

4809. //|  // RD is an integer, RA is a number.

4810. //|  cvtsi2sd xmm0, dword [BASE+RD*8]

4811. //|  ucomisd xmm0, qword [BASE+RA*8]

4812. //|  jmp >4

4813. //|

4814. //|.else

4815. //|  cmp RB, LJ_TISNUM; jae >5

4816. //|  checknum RA, >5

4817. //|.endif

4818. //|1:

4819. //|  movsd xmm0, qword [BASE+RA*8]

4820. //|2:

4821. //|  ucomisd xmm0, qword [BASE+RD*8]

4822. //|4:

4823. dasm_put(Dst, 9070, -BCBIAS_J*4, LJ_TISNUM);

4824. #line 3409 "vm_x86.dasc"

4825. iseqne_fp:

4826. if (vk) {

4827. //|  jp >2                                // Unordered means not equal.

4828. //|  jne >2

4829. dasm_put(Dst, 9171);

4830. #line 3413 "vm_x86.dasc"

4831. } else {

4832. //|  jp >2                                // Unordered means not equal.

4833. //|  je >1

4834. dasm_put(Dst, 9180);

4835. #line 3416 "vm_x86.dasc"

4836. }

4837. iseqne_end:

4838. if (vk) {

4839. //|1:                                // EQ: Branch to the target.

4840. //|  movzx RD, PC_RD

4841. //|  branchPC RD

4842. //|2:                                // NE: Fallthrough to next instruction.

4843. //|.if not FFI

4844. //|3:

4845. //|.endif

4846. dasm_put(Dst, 9189, -BCBIAS_J*4);

4847. #line 3426 "vm_x86.dasc"

4848. } else {

4849. //|.if not FFI

4850. //|3:

4851. //|.endif

4852. //|2:                                // NE: Branch to the target.

4853. //|  movzx RD, PC_RD

4854. //|  branchPC RD

4855. //|1:                                // EQ: Fallthrough to next instruction.

4856. dasm_put(Dst, 9204, -BCBIAS_J*4);

4857. #line 3434 "vm_x86.dasc"

4858. }

4859. if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||

4860. op == BC_ISEQN || op == BC_ISNEN)) {

4861. //|  jmp <9

4862. dasm_put(Dst, 9219);

4863. #line 3438 "vm_x86.dasc"

4864. } else {

4865. //|  ins_next

4866. dasm_put(Dst, 9224);

4867. #line 3440 "vm_x86.dasc"

4868. }

4869. //|

4870. if (op == BC_ISEQV || op == BC_ISNEV) {

4871. //|5:  // Either or both types are not numbers.

4872. //|.if FFI

4873. //|  cmp RB, LJ_TCDATA; je ->vmeta_equal_cd

4874. //|  checktp RA, LJ_TCDATA; je ->vmeta_equal_cd

4875. //|.endif

4876. //|  checktp RA, RB                        // Compare types.

4877. //|  jne <2                                // Not the same type?

4878. //|  cmp RB, LJ_TISPRI

4879. //|  jae <1                                // Same type and primitive type?

4880. //|

4881. //|  // Same types and not a primitive type. Compare GCobj or pvalue.

4882. //|  mov RA, [BASE+RA*8]

4883. //|  mov RD, [BASE+RD*8]

4884. //|  cmp RA, RD

4885. //|  je <1                                // Same GCobjs or pvalues?

4886. //|  cmp RB, LJ_TISTABUD

4887. //|  ja <2                                // Different objects and not table/ud?

4888. //|.if X64

4889. //|  cmp RB, LJ_TUDATA                // And not 64 bit lightuserdata.

4890. //|  jb <2

4891. //|.endif

4892. //|

4893. //|  // Different tables or userdatas. Need to check __eq metamethod.

4894. //|  // Field metatable must be at same offset for GCtab and GCudata!

4895. //|  mov TAB:RB, TAB:RA->metatable

4896. dasm_put(Dst, 9245, LJ_TCDATA, LJ_TCDATA, LJ_TISPRI, LJ_TISTABUD, LJ_TUDATA);

4897. #line 3468 "vm_x86.dasc"

4898. //|  test TAB:RB, TAB:RB

4899. //|  jz <2                                // No metatable?

4900. //|  test byte TAB:RB->nomm, 1<<MM_eq

4901. //|  jnz <2                                // Or 'no __eq' flag set?

4902. dasm_put(Dst, 9310, Dt6(->metatable), Dt6(->nomm), 1<<MM_eq);

4903. #line 3472 "vm_x86.dasc"

4904. if (vk) {

4905. //|  xor RB, RB                        // ne = 0

4906. dasm_put(Dst, 9330);

4907. #line 3474 "vm_x86.dasc"

4908. } else {

4909. //|  mov RB, 1                        // ne = 1

4910. dasm_put(Dst, 9334);

4911. #line 3476 "vm_x86.dasc"

4912. }

4913. //|  jmp ->vmeta_equal                // Handle __eq metamethod.

4914. dasm_put(Dst, 9340);

4915. #line 3478 "vm_x86.dasc"

4916. } else {

4917. //|.if FFI

4918. //|3:

4919. //|  cmp RB, LJ_TCDATA

4920. dasm_put(Dst, 9345, LJ_TCDATA);

4921. #line 3482 "vm_x86.dasc"

4922. if (LJ_DUALNUM && vk) {

4923. //|  jne <9

4924. dasm_put(Dst, 9352);

4925. #line 3484 "vm_x86.dasc"

4926. } else {

4927. //|  jne <2

4928. dasm_put(Dst, 7896);

4929. #line 3486 "vm_x86.dasc"

4930. }

4931. //|  jmp ->vmeta_equal_cd

4932. //|.endif

4933. dasm_put(Dst, 9357);

4934. #line 3489 "vm_x86.dasc"

4935. }

4936. break;

4937. case BC_ISEQS: case BC_ISNES:

4938. vk = op == BC_ISEQS;

4939. //|  ins_AND        // RA = src, RD = str const, JMP with RD = target

4940. //|  mov RB, [BASE+RA*8+4]

4941. //|  add PC, 4

4942. //|  cmp RB, LJ_TSTR; jne >3

4943. //|  mov RA, [BASE+RA*8]

4944. //|  cmp RA, [KBASE+RD*4]

4945. dasm_put(Dst, 9362, LJ_TSTR);

4946. #line 3499 "vm_x86.dasc"

4947. iseqne_test:

4948. if (vk) {

4949. //|  jne >2

4950. dasm_put(Dst, 3670);

4951. #line 3502 "vm_x86.dasc"

4952. } else {

4953. //|  je >1

4954. dasm_put(Dst, 9184);

4955. #line 3504 "vm_x86.dasc"

4956. }

4957. goto iseqne_end;

4958. case BC_ISEQN: case BC_ISNEN:

4959. vk = op == BC_ISEQN;

4960. //|  ins_AD        // RA = src, RD = num const, JMP with RD = target

4961. //|  mov RB, [BASE+RA*8+4]

4962. //|  add PC, 4

4963. //|.if DUALNUM

4964. //|  cmp RB, LJ_TISNUM; jne >7

4965. //|  cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8

4966. //|  mov RB, dword [KBASE+RD*8]

4967. //|  cmp RB, dword [BASE+RA*8]

4968. dasm_put(Dst, 9389, LJ_TISNUM, LJ_TISNUM);

4969. #line 3516 "vm_x86.dasc"

4970. if (vk) {

4971. //|  jne >9

4972. dasm_put(Dst, 9060);

4973. #line 3518 "vm_x86.dasc"

4974. } else {

4975. //|  je >9

4976. dasm_put(Dst, 9065);

4977. #line 3520 "vm_x86.dasc"

4978. }

4979. //|  movzx RD, PC_RD

4980. //|  branchPC RD

4981. //|9:

4982. //|  ins_next

4983. //|

4984. //|7:  // RA is not an integer.

4985. //|  ja >3

4986. //|  // RA is a number.

4987. //|  cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1

4988. //|  // RA is a number, RD is an integer.

4989. //|  cvtsi2sd xmm0, dword [KBASE+RD*8]

4990. //|  jmp >2

4991. //|

4992. //|8:  // RA is an integer, RD is a number.

4993. //|  cvtsi2sd xmm0, dword [BASE+RA*8]

4994. //|  ucomisd xmm0, qword [KBASE+RD*8]

4995. //|  jmp >4

4996. //|.else

4997. //|  cmp RB, LJ_TISNUM; jae >3

4998. //|.endif

4999. //|1:

5000. //|  movsd xmm0, qword [KBASE+RD*8]

5001. //|2:

5002. //|  ucomisd xmm0, qword [BASE+RA*8]

5003. //|4:

5004. dasm_put(Dst, 9423, -BCBIAS_J*4, LJ_TISNUM);

5005. #line 3546 "vm_x86.dasc"

5006. goto iseqne_fp;

5007. case BC_ISEQP: case BC_ISNEP:

5008. vk = op == BC_ISEQP;

5009. //|  ins_AND        // RA = src, RD = primitive type (~), JMP with RD = target

5010. //|  mov RB, [BASE+RA*8+4]

5011. //|  add PC, 4

5012. //|  cmp RB, RD

5013. dasm_put(Dst, 9520);

5014. #line 3553 "vm_x86.dasc"

5015. if (!LJ_HASFFI) goto iseqne_test;

5016. if (vk) {

5017. //|  jne >3

5018. //|  movzx RD, PC_RD

5019. //|  branchPC RD

5020. //|2:

5021. //|  ins_next

5022. //|3:

5023. //|  cmp RB, LJ_TCDATA; jne <2

5024. //|  jmp ->vmeta_equal_cd

5025. dasm_put(Dst, 9534, -BCBIAS_J*4, LJ_TCDATA);

5026. #line 3563 "vm_x86.dasc"

5027. } else {

5028. //|  je >2

5029. //|  cmp RB, LJ_TCDATA; je ->vmeta_equal_cd

5030. //|  movzx RD, PC_RD

5031. //|  branchPC RD

5032. //|2:

5033. //|  ins_next

5034. dasm_put(Dst, 9585, LJ_TCDATA, -BCBIAS_J*4);

5035. #line 3570 "vm_x86.dasc"

5036. }

5037. break;

5039. /* -- Unary test and copy ops ------------------------------------------- */

5041. case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:

5042. //|  ins_AD        // RA = dst or unused, RD = src, JMP with RD = target

5043. //|  mov RB, [BASE+RD*8+4]

5044. //|  add PC, 4

5045. //|  cmp RB, LJ_TISTRUECOND

5046. dasm_put(Dst, 9630, LJ_TISTRUECOND);

5047. #line 3580 "vm_x86.dasc"

5048. if (op == BC_IST || op == BC_ISTC) {

5049. //|  jae >1

5050. dasm_put(Dst, 9642);

5051. #line 3582 "vm_x86.dasc"

5052. } else {

5053. //|  jb >1

5054. dasm_put(Dst, 5428);

5055. #line 3584 "vm_x86.dasc"

5056. }

5057. if (op == BC_ISTC || op == BC_ISFC) {

5058. //|  mov [BASE+RA*8+4], RB

5059. //|  mov RB, [BASE+RD*8]

5060. //|  mov [BASE+RA*8], RB

5061. dasm_put(Dst, 9647);

5062. #line 3589 "vm_x86.dasc"

5063. }

5064. //|  movzx RD, PC_RD

5065. //|  branchPC RD

5066. //|1:                                        // Fallthrough to the next instruction.

5067. //|  ins_next

5068. dasm_put(Dst, 9658, -BCBIAS_J*4);

5069. #line 3594 "vm_x86.dasc"

5070. break;

5072. case BC_ISTYPE:

5073. //|  ins_AD        // RA = src, RD = -type

5074. //|  add RD, [BASE+RA*8+4]

5075. //|  jne ->vmeta_istype

5076. //|  ins_next

5077. dasm_put(Dst, 9691);

5078. #line 3601 "vm_x86.dasc"

5079. break;

5080. case BC_ISNUM:

5081. //|  ins_AD        // RA = src, RD = -(TISNUM-1)

5082. //|  checknum RA, ->vmeta_istype

5083. //|  ins_next

5084. dasm_put(Dst, 9720, LJ_TISNUM);

5085. #line 3606 "vm_x86.dasc"

5086. break;

5088. /* -- Unary ops --------------------------------------------------------- */

5090. case BC_MOV:

5091. //|  ins_AD        // RA = dst, RD = src

5092. //|.if X64

5093. //|  mov RBa, [BASE+RD*8]

5094. //|  mov [BASE+RA*8], RBa

5095. //|.else

5096. //|  mov RB, [BASE+RD*8+4]

5097. //|  mov RD, [BASE+RD*8]

5098. //|  mov [BASE+RA*8+4], RB

5099. //|  mov [BASE+RA*8], RD

5100. //|.endif

5101. //|  ins_next_

5102. dasm_put(Dst, 9751);

5103. #line 3622 "vm_x86.dasc"

5104. break;

5105. case BC_NOT:

5106. //|  ins_AD        // RA = dst, RD = src

5107. //|  xor RB, RB

5108. //|  checktp RD, LJ_TISTRUECOND

5109. //|  adc RB, LJ_TTRUE

5110. //|  mov [BASE+RA*8+4], RB

5111. //|  ins_next

5112. dasm_put(Dst, 9780, LJ_TISTRUECOND, LJ_TTRUE);

5113. #line 3630 "vm_x86.dasc"

5114. break;

5115. case BC_UNM:

5116. //|  ins_AD        // RA = dst, RD = src

5117. //|.if DUALNUM

5118. //|  checkint RD, >5

5119. //|  mov RB, [BASE+RD*8]

5120. //|  neg RB

5121. //|  jo >4

5122. //|  mov dword [BASE+RA*8+4], LJ_TISNUM

5123. //|  mov dword [BASE+RA*8], RB

5124. //|9:

5125. //|  ins_next

5126. //|4:

5127. //|  mov dword [BASE+RA*8+4], 0x41e00000  // 2^31.

5128. //|  mov dword [BASE+RA*8], 0

5129. //|  jmp <9

5130. //|5:

5131. //|  ja ->vmeta_unm

5132. //|.else

5133. //|  checknum RD, ->vmeta_unm

5134. //|.endif

5135. //|  movsd xmm0, qword [BASE+RD*8]

5136. //|  sseconst_sign xmm1, RDa

5137. //|  xorps xmm0, xmm1

5138. //|  movsd qword [BASE+RA*8], xmm0

5139. //|.if DUALNUM

5140. //|  jmp <9

5141. //|.else

5142. //|  ins_next

5143. //|.endif

5144. dasm_put(Dst, 9817, LJ_TISNUM, LJ_TISNUM, (unsigned int)(U64x(80000000,00000000)), (unsigned int)((U64x(80000000,00000000))>>32));

5145. #line 3660 "vm_x86.dasc"

5146. break;

5147. case BC_LEN:

5148. //|  ins_AD        // RA = dst, RD = src

5149. //|  checkstr RD, >2

5150. //|  mov STR:RD, [BASE+RD*8]

5151. //|.if DUALNUM

5152. //|  mov RD, dword STR:RD->len

5153. //|1:

5154. //|  mov dword [BASE+RA*8+4], LJ_TISNUM

5155. //|  mov dword [BASE+RA*8], RD

5156. //|.else

5157. //|  xorps xmm0, xmm0

5158. //|  cvtsi2sd xmm0, dword STR:RD->len

5159. //|1:

5160. //|  movsd qword [BASE+RA*8], xmm0

5161. //|.endif

5162. //|  ins_next

5163. //|2:

5164. //|  checktab RD, ->vmeta_len

5165. //|  mov TAB:FCARG1, [BASE+RD*8]

5166. dasm_put(Dst, 9923, LJ_TSTR, Dt5(->len), LJ_TISNUM, LJ_TTAB);

5167. #line 3680 "vm_x86.dasc"

5168. #if LJ_52

5169. //|  mov TAB:RB, TAB:FCARG1->metatable

5170. //|  cmp TAB:RB, 0

5171. //|  jnz >9

5172. //|3:

5173. dasm_put(Dst, 9985, Dt6(->metatable));

5174. #line 3685 "vm_x86.dasc"

5175. #endif

5176. //|->BC_LEN_Z:

5177. //|  mov RB, BASE                        // Save BASE.

5178. //|  call extern lj_tab_len@4                // (GCtab *t)

5179. //|  // Length of table returned in eax (RD).

5180. //|.if DUALNUM

5181. //|  // Nothing to do.

5182. //|.else

5183. //|  cvtsi2sd xmm0, RD

5184. //|.endif

5185. //|  mov BASE, RB                        // Restore BASE.

5186. //|  movzx RA, PC_RA

5187. //|  jmp <1

5188. dasm_put(Dst, 9999);

5189. #line 3698 "vm_x86.dasc"

5190. #if LJ_52

5191. //|9:  // Check for __len.

5192. //|  test byte TAB:RB->nomm, 1<<MM_len

5193. //|  jnz <3

5194. //|  jmp ->vmeta_len                        // 'no __len' flag NOT set: check.

5195. dasm_put(Dst, 10020, Dt6(->nomm), 1<<MM_len);

5196. #line 3703 "vm_x86.dasc"

5197. #endif

5198. break;

5200. /* -- Binary ops -------------------------------------------------------- */

5202. //|.macro ins_arithpre, sseins, ssereg

5203. //|  ins_ABC

5204. //||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);

5205. //||switch (vk) {

5206. //||case 0:

5207. //|   checknum RB, ->vmeta_arith_vn

5208. //|   .if DUALNUM

5209. //|     cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn

5210. //|   .endif

5211. //|   movsd xmm0, qword [BASE+RB*8]

5212. //|   sseins ssereg, qword [KBASE+RC*8]

5213. //||  break;

5214. //||case 1:

5215. //|   checknum RB, ->vmeta_arith_nv

5216. //|   .if DUALNUM

5217. //|     cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv

5218. //|   .endif

5219. //|   movsd xmm0, qword [KBASE+RC*8]

5220. //|   sseins ssereg, qword [BASE+RB*8]

5221. //||  break;

5222. //||default:

5223. //|   checknum RB, ->vmeta_arith_vv

5224. //|   checknum RC, ->vmeta_arith_vv

5225. //|   movsd xmm0, qword [BASE+RB*8]

5226. //|   sseins ssereg, qword [BASE+RC*8]

5227. //||  break;

5228. //||}

5229. //|.endmacro

5230. //|

5231. //|.macro ins_arithdn, intins

5232. //|  ins_ABC

5233. //||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);

5234. //||switch (vk) {

5235. //||case 0:

5236. //|   checkint RB, ->vmeta_arith_vn

5237. //|   cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn

5238. //|   mov RB, [BASE+RB*8]

5239. //|   intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno

5240. //||  break;

5241. //||case 1:

5242. //|   checkint RB, ->vmeta_arith_nv

5243. //|   cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv

5244. //|   mov RC, [KBASE+RC*8]

5245. //|   intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo

5246. //||  break;

5247. //||default:

5248. //|   checkint RB, ->vmeta_arith_vv

5249. //|   checkint RC, ->vmeta_arith_vv

5250. //|   mov RB, [BASE+RB*8]

5251. //|   intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo

5252. //||  break;

5253. //||}

5254. //|  mov dword [BASE+RA*8+4], LJ_TISNUM

5255. //||if (vk == 1) {

5256. //|   mov dword [BASE+RA*8], RC

5257. //||} else {

5258. //|   mov dword [BASE+RA*8], RB

5259. //||}

5260. //|  ins_next

5261. //|.endmacro

5262. //|

5263. //|.macro ins_arithpost

5264. //|  movsd qword [BASE+RA*8], xmm0

5265. //|.endmacro

5266. //|

5267. //|.macro ins_arith, sseins

5268. //|  ins_arithpre sseins, xmm0

5269. //|  ins_arithpost

5270. //|  ins_next

5271. //|.endmacro

5272. //|

5273. //|.macro ins_arith, intins, sseins

5274. //|.if DUALNUM

5275. //|  ins_arithdn intins

5276. //|.else

5277. //|  ins_arith, sseins

5278. //|.endif

5279. //|.endmacro

5281. //|  // RA = dst, RB = src1 or num const, RC = src2 or num const

5282. case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:

5283. //|  ins_arith add, addsd

5284. dasm_put(Dst, 10036);

5285. vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);

5286. switch (vk) {

5287. case 0:

5288. dasm_put(Dst, 10044, LJ_TISNUM, LJ_TISNUM);

5289. break;

5290. case 1:

5291. dasm_put(Dst, 10079, LJ_TISNUM, LJ_TISNUM);

5292. break;

5293. default:

5294. dasm_put(Dst, 10114, LJ_TISNUM, LJ_TISNUM);

5295. break;

5296. }

5297. dasm_put(Dst, 10147, LJ_TISNUM);

5298. if (vk == 1) {

5299. dasm_put(Dst, 10153);

5300. } else {

5301. dasm_put(Dst, 9654);

5302. }

5303. dasm_put(Dst, 9224);

5304. #line 3790 "vm_x86.dasc"

5305. break;

5306. case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:

5307. //|  ins_arith sub, subsd

5308. dasm_put(Dst, 10036);

5309. vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);

5310. switch (vk) {

5311. case 0:

5312. dasm_put(Dst, 10157, LJ_TISNUM, LJ_TISNUM);

5313. break;

5314. case 1:

5315. dasm_put(Dst, 10192, LJ_TISNUM, LJ_TISNUM);

5316. break;

5317. default:

5318. dasm_put(Dst, 10227, LJ_TISNUM, LJ_TISNUM);

5319. break;

5320. }

5321. dasm_put(Dst, 10147, LJ_TISNUM);

5322. if (vk == 1) {

5323. dasm_put(Dst, 10153);

5324. } else {

5325. dasm_put(Dst, 9654);

5326. }

5327. dasm_put(Dst, 9224);

5328. #line 3793 "vm_x86.dasc"

5329. break;

5330. case BC_MULVN: case BC_MULNV: case BC_MULVV:

5331. //|  ins_arith imul, mulsd

5332. dasm_put(Dst, 10036);

5333. vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);

5334. switch (vk) {

5335. case 0:

5336. dasm_put(Dst, 10260, LJ_TISNUM, LJ_TISNUM);

5337. break;

5338. case 1:

5339. dasm_put(Dst, 10296, LJ_TISNUM, LJ_TISNUM);

5340. break;

5341. default:

5342. dasm_put(Dst, 10332, LJ_TISNUM, LJ_TISNUM);

5343. break;

5344. }

5345. dasm_put(Dst, 10147, LJ_TISNUM);

5346. if (vk == 1) {

5347. dasm_put(Dst, 10153);

5348. } else {

5349. dasm_put(Dst, 9654);

5350. }

5351. dasm_put(Dst, 9224);

5352. #line 3796 "vm_x86.dasc"

5353. break;

5354. case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:

5355. //|  ins_arith divsd

5356. dasm_put(Dst, 10036);

5357. vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);

5358. switch (vk) {

5359. case 0:

5360. dasm_put(Dst, 10366, LJ_TISNUM, LJ_TISNUM);

5361. break;

5362. case 1:

5363. dasm_put(Dst, 10403, LJ_TISNUM, LJ_TISNUM);

5364. break;

5365. default:

5366. dasm_put(Dst, 10440, LJ_TISNUM, LJ_TISNUM);

5367. break;

5368. }

5369. dasm_put(Dst, 10475);

5370. #line 3799 "vm_x86.dasc"

5371. break;

5372. case BC_MODVN:

5373. //|  ins_arithpre movsd, xmm1

5374. dasm_put(Dst, 10036);

5375. vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);

5376. switch (vk) {

5377. case 0:

5378. dasm_put(Dst, 10502, LJ_TISNUM, LJ_TISNUM);

5379. break;

5380. case 1:

5381. dasm_put(Dst, 10539, LJ_TISNUM, LJ_TISNUM);

5382. break;

5383. default:

5384. dasm_put(Dst, 10576, LJ_TISNUM, LJ_TISNUM);

5385. break;

5386. }

5387. #line 3802 "vm_x86.dasc"

5388. //|->BC_MODVN_Z:

5389. //|  call ->vm_mod

5390. //|  ins_arithpost

5391. //|  ins_next

5392. dasm_put(Dst, 10611);

5393. #line 3806 "vm_x86.dasc"

5394. break;

5395. case BC_MODNV: case BC_MODVV:

5396. //|  ins_arithpre movsd, xmm1

5397. dasm_put(Dst, 10036);

5398. vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);

5399. switch (vk) {

5400. case 0:

5401. dasm_put(Dst, 10502, LJ_TISNUM, LJ_TISNUM);

5402. break;

5403. case 1:

5404. dasm_put(Dst, 10539, LJ_TISNUM, LJ_TISNUM);

5405. break;

5406. default:

5407. dasm_put(Dst, 10576, LJ_TISNUM, LJ_TISNUM);

5408. break;

5409. }

5410. #line 3809 "vm_x86.dasc"

5411. //|  jmp ->BC_MODVN_Z                        // Avoid 3 copies. It's slow anyway.

5412. dasm_put(Dst, 10643);

5413. #line 3810 "vm_x86.dasc"

5414. break;

5415. case BC_POW:

5416. //|  ins_arithpre movsd, xmm1

5417. dasm_put(Dst, 10036);

5418. vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);

5419. switch (vk) {

5420. case 0:

5421. dasm_put(Dst, 10502, LJ_TISNUM, LJ_TISNUM);

5422. break;

5423. case 1:

5424. dasm_put(Dst, 10539, LJ_TISNUM, LJ_TISNUM);

5425. break;

5426. default:

5427. dasm_put(Dst, 10576, LJ_TISNUM, LJ_TISNUM);

5428. break;

5429. }

5430. #line 3813 "vm_x86.dasc"

5431. //|  mov RB, BASE

5432. //|.if not X64

5433. //|  movsd FPARG1, xmm0

5434. //|  movsd FPARG3, xmm1

5435. //|.endif

5436. //|  call extern pow

5437. //|  movzx RA, PC_RA

5438. //|  mov BASE, RB

5439. //|.if X64

5440. //|  ins_arithpost

5441. //|.else

5442. //|  fstp qword [BASE+RA*8]

5443. //|.endif

5444. //|  ins_next

5445. dasm_put(Dst, 10648);

5446. #line 3827 "vm_x86.dasc"

5447. break;

5449. case BC_CAT:

5450. //|  ins_ABC        // RA = dst, RB = src_start, RC = src_end

5451. //|.if X64

5452. //|  mov L:CARG1d, SAVE_L

5453. //|  mov L:CARG1d->base, BASE

5454. //|  lea CARG2d, [BASE+RC*8]

5455. //|  mov CARG3d, RC

5456. //|  sub CARG3d, RB

5457. //|->BC_CAT_Z:

5458. //|  mov L:RB, L:CARG1d

5459. //|.else

5460. //|  lea RA, [BASE+RC*8]

5461. //|  sub RC, RB

5462. //|  mov ARG2, RA

5463. //|  mov ARG3, RC

5464. //|->BC_CAT_Z:

5465. //|  mov L:RB, SAVE_L

5466. //|  mov ARG1, L:RB

5467. //|  mov L:RB->base, BASE

5468. //|.endif

5469. //|  mov SAVE_PC, PC

5470. //|  call extern lj_meta_cat                // (lua_State *L, TValue *top, int left)

5471. //|  // NULL (finished) or TValue * (metamethod) returned in eax (RC).

5472. //|  mov BASE, L:RB->base

5473. //|  test RC, RC

5474. //|  jnz ->vmeta_binop

5475. //|  movzx RB, PC_RB                        // Copy result to Stk[RA] from Stk[RB].

5476. //|  movzx RA, PC_RA

5477. //|.if X64

5478. //|  mov RCa, [BASE+RB*8]

5479. //|  mov [BASE+RA*8], RCa

5480. //|.else

5481. //|  mov RC, [BASE+RB*8+4]

5482. //|  mov RB, [BASE+RB*8]

5483. //|  mov [BASE+RA*8+4], RC

5484. //|  mov [BASE+RA*8], RB

5485. //|.endif

5486. //|  ins_next

5487. dasm_put(Dst, 10689, Dt1(->base), Dt1(->base));

5488. #line 3867 "vm_x86.dasc"

5489. break;

5491. /* -- Constant ops ------------------------------------------------------ */

5493. case BC_KSTR:

5494. //|  ins_AND        // RA = dst, RD = str const (~)

5495. //|  mov RD, [KBASE+RD*4]

5496. //|  mov dword [BASE+RA*8+4], LJ_TSTR

5497. //|  mov [BASE+RA*8], RD

5498. //|  ins_next

5499. dasm_put(Dst, 10773, LJ_TSTR);

5500. #line 3877 "vm_x86.dasc"

5501. break;

5502. case BC_KCDATA:

5503. //|.if FFI

5504. //|  ins_AND        // RA = dst, RD = cdata const (~)

5505. //|  mov RD, [KBASE+RD*4]

5506. //|  mov dword [BASE+RA*8+4], LJ_TCDATA

5507. //|  mov [BASE+RA*8], RD

5508. //|  ins_next

5509. //|.endif

5510. dasm_put(Dst, 10773, LJ_TCDATA);

5511. #line 3886 "vm_x86.dasc"

5512. break;

5513. case BC_KSHORT:

5514. //|  ins_AD        // RA = dst, RD = signed int16 literal

5515. //|.if DUALNUM

5516. //|  movsx RD, RDW

5517. //|  mov dword [BASE+RA*8+4], LJ_TISNUM

5518. //|  mov dword [BASE+RA*8], RD

5519. //|.else

5520. //|  movsx RD, RDW                        // Sign-extend literal.

5521. //|  cvtsi2sd xmm0, RD

5522. //|  movsd qword [BASE+RA*8], xmm0

5523. //|.endif

5524. //|  ins_next

5525. dasm_put(Dst, 10810, LJ_TISNUM);

5526. #line 3899 "vm_x86.dasc"

5527. break;

5528. case BC_KNUM:

5529. //|  ins_AD        // RA = dst, RD = num const

5530. //|  movsd xmm0, qword [KBASE+RD*8]

5531. //|  movsd qword [BASE+RA*8], xmm0

5532. //|  ins_next

5533. dasm_put(Dst, 10842);

5534. #line 3905 "vm_x86.dasc"

5535. break;

5536. case BC_KPRI:

5537. //|  ins_AND        // RA = dst, RD = primitive type (~)

5538. //|  mov [BASE+RA*8+4], RD

5539. //|  ins_next

5540. dasm_put(Dst, 10876);

5541. #line 3910 "vm_x86.dasc"

5542. break;

5543. case BC_KNIL:

5544. //|  ins_AD        // RA = dst_start, RD = dst_end

5545. //|  lea RA, [BASE+RA*8+12]

5546. //|  lea RD, [BASE+RD*8+4]

5547. //|  mov RB, LJ_TNIL

5548. //|  mov [RA-8], RB                        // Sets minimum 2 slots.

5549. //|1:

5550. //|  mov [RA], RB

5551. //|  add RA, 8

5552. //|  cmp RA, RD

5553. //|  jbe <1

5554. //|  ins_next

5555. dasm_put(Dst, 10905, LJ_TNIL);

5556. #line 3923 "vm_x86.dasc"

5557. break;

5559. /* -- Upvalue and function ops ------------------------------------------ */

5561. case BC_UGET:

5562. //|  ins_AD        // RA = dst, RD = upvalue #

5563. //|  mov LFUNC:RB, [BASE-8]

5564. //|  mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)]

5565. //|  mov RB, UPVAL:RB->v

5566. //|.if X64

5567. //|  mov RDa, [RB]

5568. //|  mov [BASE+RA*8], RDa

5569. //|.else

5570. //|  mov RD, [RB+4]

5571. //|  mov RB, [RB]

5572. //|  mov [BASE+RA*8+4], RD

5573. //|  mov [BASE+RA*8], RB

5574. //|.endif

5575. //|  ins_next

5576. dasm_put(Dst, 10953, offsetof(GCfuncL, uvptr), DtA(->v));

5577. #line 3942 "vm_x86.dasc"

5578. break;

5579. case BC_USETV:

5580. ‌#define TV2MARKOFS \

5581. ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))

5582. //|  ins_AD        // RA = upvalue #, RD = src

5583. //|  mov LFUNC:RB, [BASE-8]

5584. //|  mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]

5585. //|  cmp byte UPVAL:RB->closed, 0

5586. //|  mov RB, UPVAL:RB->v

5587. //|  mov RA, [BASE+RD*8]

5588. //|  mov RD, [BASE+RD*8+4]

5589. //|  mov [RB], RA

5590. //|  mov [RB+4], RD

5591. //|  jz >1

5592. //|  // Check barrier for closed upvalue.

5593. //|  test byte [RB+TV2MARKOFS], LJ_GC_BLACK                // isblack(uv)

5594. //|  jnz >2

5595. //|1:

5596. //|  ins_next

5597. //|

5598. //|2:  // Upvalue is black. Check if new value is collectable and white.

5599. //|  sub RD, LJ_TISGCV

5600. //|  cmp RD, LJ_TNUMX - LJ_TISGCV                        // tvisgcv(v)

5601. //|  jbe <1

5602. //|  test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES        // iswhite(v)

5603. //|  jz <1

5604. //|  // Crossed a write barrier. Move the barrier forward.

5605. //|.if X64 and not X64WIN

5606. //|  mov FCARG2, RB

5607. //|  mov RB, BASE                        // Save BASE.

5608. //|.else

5609. //|  xchg FCARG2, RB                        // Save BASE (FCARG2 == BASE).

5610. //|.endif

5611. //|  lea GL:FCARG1, [DISPATCH+GG_DISP2G]

5612. //|  call extern lj_gc_barrieruv@8        // (global_State *g, TValue *tv)

5613. dasm_put(Dst, 10994, offsetof(GCfuncL, uvptr), DtA(->closed), DtA(->v), TV2MARKOFS, LJ_GC_BLACK, LJ_TISGCV, LJ_TNUMX - LJ_TISGCV, Dt4(->gch.marked), LJ_GC_WHITES, GG_DISP2G);

5614. #line 3977 "vm_x86.dasc"

5615. //|  mov BASE, RB                        // Restore BASE.

5616. //|  jmp <1

5617. dasm_put(Dst, 11090);

5618. #line 3979 "vm_x86.dasc"

5619. break;

5620. ‌#undef TV2MARKOFS

5621. case BC_USETS:

5622. //|  ins_AND        // RA = upvalue #, RD = str const (~)

5623. //|  mov LFUNC:RB, [BASE-8]

5624. //|  mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]

5625. //|  mov GCOBJ:RA, [KBASE+RD*4]

5626. //|  mov RD, UPVAL:RB->v

5627. //|  mov [RD], GCOBJ:RA

5628. //|  mov dword [RD+4], LJ_TSTR

5629. //|  test byte UPVAL:RB->marked, LJ_GC_BLACK                // isblack(uv)

5630. //|  jnz >2

5631. //|1:

5632. //|  ins_next

5633. //|

5634. //|2:  // Check if string is white and ensure upvalue is closed.

5635. //|  test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES        // iswhite(str)

5636. //|  jz <1

5637. //|  cmp byte UPVAL:RB->closed, 0

5638. //|  jz <1

5639. //|  // Crossed a write barrier. Move the barrier forward.

5640. //|  mov RB, BASE                        // Save BASE (FCARG2 == BASE).

5641. //|  mov FCARG2, RD

5642. //|  lea GL:FCARG1, [DISPATCH+GG_DISP2G]

5643. //|  call extern lj_gc_barrieruv@8        // (global_State *g, TValue *tv)

5644. //|  mov BASE, RB                        // Restore BASE.

5645. //|  jmp <1

5646. dasm_put(Dst, 11102, offsetof(GCfuncL, uvptr), DtA(->v), LJ_TSTR, DtA(->marked), LJ_GC_BLACK, Dt4(->gch.marked), LJ_GC_WHITES, DtA(->closed), GG_DISP2G);

5647. #line 4006 "vm_x86.dasc"

5648. break;

5649. case BC_USETN:

5650. //|  ins_AD        // RA = upvalue #, RD = num const

5651. //|  mov LFUNC:RB, [BASE-8]

5652. //|  movsd xmm0, qword [KBASE+RD*8]

5653. //|  mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]

5654. //|  mov RA, UPVAL:RB->v

5655. //|  movsd qword [RA], xmm0

5656. //|  ins_next

5657. dasm_put(Dst, 11198, offsetof(GCfuncL, uvptr), DtA(->v));

5658. #line 4015 "vm_x86.dasc"

5659. break;

5660. case BC_USETP:

5661. //|  ins_AND        // RA = upvalue #, RD = primitive type (~)

5662. //|  mov LFUNC:RB, [BASE-8]

5663. //|  mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]

5664. //|  mov RA, UPVAL:RB->v

5665. //|  mov [RA+4], RD

5666. //|  ins_next

5667. dasm_put(Dst, 11243, offsetof(GCfuncL, uvptr), DtA(->v));

5668. #line 4023 "vm_x86.dasc"

5669. break;

5670. case BC_UCLO:

5671. //|  ins_AD        // RA = level, RD = target

5672. //|  branchPC RD                        // Do this first to free RD.

5673. //|  mov L:RB, SAVE_L

5674. //|  cmp dword L:RB->openupval, 0

5675. //|  je >1

5676. //|  mov L:RB->base, BASE

5677. //|  lea FCARG2, [BASE+RA*8]                // Caveat: FCARG2 == BASE

5678. //|  mov L:FCARG1, L:RB                // Caveat: FCARG1 == RA

5679. //|  call extern lj_func_closeuv@8        // (lua_State *L, TValue *level)

5680. //|  mov BASE, L:RB->base

5681. //|1:

5682. //|  ins_next

5683. dasm_put(Dst, 11283, -BCBIAS_J*4, Dt1(->openupval), Dt1(->base), Dt1(->base));

5684. #line 4037 "vm_x86.dasc"

5685. break;

5687. case BC_FNEW:

5688. //|  ins_AND        // RA = dst, RD = proto const (~) (holding function prototype)

5689. //|.if X64

5690. //|  mov L:RB, SAVE_L

5691. //|  mov L:RB->base, BASE                // Caveat: CARG2d/CARG3d may be BASE.

5692. //|  mov CARG3d, [BASE-8]

5693. //|  mov CARG2d, [KBASE+RD*4]                // Fetch GCproto *.

5694. //|  mov CARG1d, L:RB

5695. //|.else

5696. //|  mov LFUNC:RA, [BASE-8]

5697. //|  mov PROTO:RD, [KBASE+RD*4]        // Fetch GCproto *.

5698. //|  mov L:RB, SAVE_L

5699. //|  mov ARG3, LFUNC:RA

5700. //|  mov ARG2, PROTO:RD

5701. //|  mov ARG1, L:RB

5702. //|  mov L:RB->base, BASE

5703. //|.endif

5704. //|  mov SAVE_PC, PC

5705. //|  // (lua_State *L, GCproto *pt, GCfuncL *parent)

5706. //|  call extern lj_func_newL_gc

5707. //|  // GCfuncL * returned in eax (RC).

5708. //|  mov BASE, L:RB->base

5709. //|  movzx RA, PC_RA

5710. //|  mov [BASE+RA*8], LFUNC:RC

5711. //|  mov dword [BASE+RA*8+4], LJ_TFUNC

5712. //|  ins_next

5713. dasm_put(Dst, 11339, Dt1(->base), Dt1(->base), LJ_TFUNC);

5714. #line 4065 "vm_x86.dasc"

5715. break;

5717. /* -- Table ops --------------------------------------------------------- */

5719. case BC_TNEW:

5720. //|  ins_AD        // RA = dst, RD = hbits|asize

5721. //|  mov L:RB, SAVE_L

5722. //|  mov L:RB->base, BASE

5723. //|  mov RA, [DISPATCH+DISPATCH_GL(gc.total)]

5724. //|  cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]

5725. //|  mov SAVE_PC, PC

5726. //|  jae >5

5727. //|1:

5728. //|.if X64

5729. //|  mov CARG3d, RD

5730. //|  and RD, 0x7ff

5731. //|  shr CARG3d, 11

5732. //|.else

5733. //|  mov RA, RD

5734. //|  and RD, 0x7ff

5735. //|  shr RA, 11

5736. //|  mov ARG3, RA

5737. //|.endif

5738. //|  cmp RD, 0x7ff

5739. //|  je >3

5740. //|2:

5741. //|.if X64

5742. //|  mov L:CARG1d, L:RB

5743. //|  mov CARG2d, RD

5744. //|.else

5745. //|  mov ARG1, L:RB

5746. //|  mov ARG2, RD

5747. //|.endif

5748. //|  call extern lj_tab_new  // (lua_State *L, int32_t asize, uint32_t hbits)

5749. //|  // Table * returned in eax (RC).

5750. //|  mov BASE, L:RB->base

5751. //|  movzx RA, PC_RA

5752. //|  mov [BASE+RA*8], TAB:RC

5753. //|  mov dword [BASE+RA*8+4], LJ_TTAB

5754. //|  ins_next

5755. //|3:  // Turn 0x7ff into 0x801.

5756. //|  mov RD, 0x801

5757. //|  jmp <2

5758. //|5:

5759. //|  mov L:FCARG1, L:RB

5760. //|  call extern lj_gc_step_fixtop@4        // (lua_State *L)

5761. //|  movzx RD, PC_RD

5762. //|  jmp <1

5763. dasm_put(Dst, 11406, Dt1(->base), DISPATCH_GL(gc.total), DISPATCH_GL(gc.threshold), Dt1(->base), LJ_TTAB);

5764. #line 4113 "vm_x86.dasc"

5765. break;

5766. case BC_TDUP:

5767. //|  ins_AND        // RA = dst, RD = table const (~) (holding template table)

5768. //|  mov L:RB, SAVE_L

5769. //|  mov RA, [DISPATCH+DISPATCH_GL(gc.total)]

5770. //|  mov SAVE_PC, PC

5771. //|  cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]

5772. //|  mov L:RB->base, BASE

5773. //|  jae >3

5774. //|2:

5775. //|  mov TAB:FCARG2, [KBASE+RD*4]        // Caveat: FCARG2 == BASE

5776. //|  mov L:FCARG1, L:RB                // Caveat: FCARG1 == RA

5777. //|  call extern lj_tab_dup@8                // (lua_State *L, Table *kt)

5778. //|  // Table * returned in eax (RC).

5779. //|  mov BASE, L:RB->base

5780. //|  movzx RA, PC_RA

5781. //|  mov [BASE+RA*8], TAB:RC

5782. //|  mov dword [BASE+RA*8+4], LJ_TTAB

5783. //|  ins_next

5784. //|3:

5785. //|  mov L:FCARG1, L:RB

5786. //|  call extern lj_gc_step_fixtop@4        // (lua_State *L)

5787. //|  movzx RD, PC_RD                        // Need to reload RD.

5788. //|  not RDa

5789. //|  jmp <2

5790. dasm_put(Dst, 11530, DISPATCH_GL(gc.total), DISPATCH_GL(gc.threshold), Dt1(->base), Dt1(->base), LJ_TTAB);

5791. #line 4138 "vm_x86.dasc"

5792. break;

5794. case BC_GGET:

5795. //|  ins_AND        // RA = dst, RD = str const (~)

5796. //|  mov LFUNC:RB, [BASE-8]

5797. //|  mov TAB:RB, LFUNC:RB->env

5798. //|  mov STR:RC, [KBASE+RD*4]

5799. //|  jmp ->BC_TGETS_Z

5800. dasm_put(Dst, 11629, Dt7(->env));

5801. #line 4146 "vm_x86.dasc"

5802. break;

5803. case BC_GSET:

5804. //|  ins_AND        // RA = src, RD = str const (~)

5805. //|  mov LFUNC:RB, [BASE-8]

5806. //|  mov TAB:RB, LFUNC:RB->env

5807. //|  mov STR:RC, [KBASE+RD*4]

5808. //|  jmp ->BC_TSETS_Z

5809. dasm_put(Dst, 11649, Dt7(->env));

5810. #line 4153 "vm_x86.dasc"

5811. break;

5813. case BC_TGETV:

5814. //|  ins_ABC        // RA = dst, RB = table, RC = key

5815. //|  checktab RB, ->vmeta_tgetv

5816. //|  mov TAB:RB, [BASE+RB*8]

5817. //|

5818. //|  // Integer key?

5819. //|.if DUALNUM

5820. //|  checkint RC, >5

5821. //|  mov RC, dword [BASE+RC*8]

5822. //|.else

5823. //|  // Convert number to int and back and compare.

5824. //|  checknum RC, >5

5825. //|  movsd xmm0, qword [BASE+RC*8]

5826. //|  cvttsd2si RC, xmm0

5827. //|  cvtsi2sd xmm1, RC

5828. //|  ucomisd xmm0, xmm1

5829. //|  jne ->vmeta_tgetv                // Generic numeric key? Use fallback.

5830. //|.endif

5831. //|  cmp RC, TAB:RB->asize        // Takes care of unordered, too.

5832. //|  jae ->vmeta_tgetv                // Not in array part? Use fallback.

5833. //|  shl RC, 3

5834. //|  add RC, TAB:RB->array

5835. //|  cmp dword [RC+4], LJ_TNIL        // Avoid overwriting RB in fastpath.

5836. //|  je >2

5837. //|  // Get array slot.

5838. //|.if X64

5839. //|  mov RBa, [RC]

5840. //|  mov [BASE+RA*8], RBa

5841. //|.else

5842. //|  mov RB, [RC]

5843. //|  mov RC, [RC+4]

5844. //|  mov [BASE+RA*8], RB

5845. //|  mov [BASE+RA*8+4], RC

5846. //|.endif

5847. //|1:

5848. //|  ins_next

5849. //|

5850. //|2:  // Check for __index if table value is nil.

5851. //|  cmp dword TAB:RB->metatable, 0        // Shouldn't overwrite RA for fastpath.

5852. //|  jz >3

5853. //|  mov TAB:RA, TAB:RB->metatable

5854. //|  test byte TAB:RA->nomm, 1<<MM_index

5855. //|  jz ->vmeta_tgetv                        // 'no __index' flag NOT set: check.

5856. dasm_put(Dst, 11669, LJ_TTAB, LJ_TISNUM, Dt6(->asize), Dt6(->array), LJ_TNIL, Dt6(->metatable), Dt6(->metatable), Dt6(->nomm), 1<<MM_index);

5857. #line 4198 "vm_x86.dasc"

5858. //|  movzx RA, PC_RA                        // Restore RA.

5859. //|3:

5860. //|  mov dword [BASE+RA*8+4], LJ_TNIL

5861. //|  jmp <1

5862. //|

5863. //|5:  // String key?

5864. //|  checkstr RC, ->vmeta_tgetv

5865. //|  mov STR:RC, [BASE+RC*8]

5866. //|  jmp ->BC_TGETS_Z

5867. dasm_put(Dst, 11774, LJ_TNIL, LJ_TSTR);

5868. #line 4207 "vm_x86.dasc"

5869. break;

5870. case BC_TGETS:

5871. //|  ins_ABC        // RA = dst, RB = table, RC = str const (~)

5872. //|  not RCa

5873. //|  mov STR:RC, [KBASE+RC*4]

5874. //|  checktab RB, ->vmeta_tgets

5875. //|  mov TAB:RB, [BASE+RB*8]

5876. //|->BC_TGETS_Z:        // RB = GCtab *, RC = GCstr *, refetches PC_RA.

5877. //|  mov RA, TAB:RB->hmask

5878. //|  and RA, STR:RC->hash

5879. //|  imul RA, #NODE

5880. //|  add NODE:RA, TAB:RB->node

5881. //|1:

5882. //|  cmp dword NODE:RA->key.it, LJ_TSTR

5883. //|  jne >4

5884. //|  cmp dword NODE:RA->key.gcr, STR:RC

5885. //|  jne >4

5886. //|  // Ok, key found. Assumes: offsetof(Node, val) == 0

5887. //|  cmp dword [RA+4], LJ_TNIL        // Avoid overwriting RB in fastpath.

5888. //|  je >5                                // Key found, but nil value?

5889. //|  movzx RC, PC_RA

5890. //|  // Get node value.

5891. //|.if X64

5892. //|  mov RBa, [RA]

5893. //|  mov [BASE+RC*8], RBa

5894. //|.else

5895. //|  mov RB, [RA]

5896. //|  mov RA, [RA+4]

5897. //|  mov [BASE+RC*8], RB

5898. //|  mov [BASE+RC*8+4], RA

5899. //|.endif

5900. //|2:

5901. //|  ins_next

5902. dasm_put(Dst, 11814, LJ_TTAB, Dt6(->hmask), Dt5(->hash), sizeof(Node), Dt6(->node), DtB(->key.it), LJ_TSTR, DtB(->key.gcr), LJ_TNIL);

5903. #line 4240 "vm_x86.dasc"

5904. //|

5905. //|3:

5906. //|  movzx RC, PC_RA

5907. //|  mov dword [BASE+RC*8+4], LJ_TNIL

5908. //|  jmp <2

5909. //|

5910. //|4:  // Follow hash chain.

5911. //|  mov NODE:RA, NODE:RA->next

5912. //|  test NODE:RA, NODE:RA

5913. //|  jnz <1

5914. //|  // End of hash chain: key not found, nil result.

5915. //|

5916. //|5:  // Check for __index if table value is nil.

5917. //|  mov TAB:RA, TAB:RB->metatable

5918. //|  test TAB:RA, TAB:RA

5919. //|  jz <3                                // No metatable: done.

5920. //|  test byte TAB:RA->nomm, 1<<MM_index

5921. //|  jnz <3                                // 'no __index' flag set: done.

5922. //|  jmp ->vmeta_tgets                // Caveat: preserve STR:RC.

5923. dasm_put(Dst, 11899, LJ_TNIL, DtB(->next), Dt6(->metatable), Dt6(->nomm), 1<<MM_index);

5924. #line 4259 "vm_x86.dasc"

5925. break;

5926. case BC_TGETB:

5927. //|  ins_ABC        // RA = dst, RB = table, RC = byte literal

5928. //|  checktab RB, ->vmeta_tgetb

5929. //|  mov TAB:RB, [BASE+RB*8]

5930. //|  cmp RC, TAB:RB->asize

5931. //|  jae ->vmeta_tgetb

5932. //|  shl RC, 3

5933. //|  add RC, TAB:RB->array

5934. //|  cmp dword [RC+4], LJ_TNIL        // Avoid overwriting RB in fastpath.

5935. //|  je >2

5936. //|  // Get array slot.

5937. //|.if X64

5938. //|  mov RBa, [RC]

5939. //|  mov [BASE+RA*8], RBa

5940. //|.else

5941. //|  mov RB, [RC]

5942. //|  mov RC, [RC+4]

5943. //|  mov [BASE+RA*8], RB

5944. //|  mov [BASE+RA*8+4], RC

5945. //|.endif

5946. //|1:

5947. //|  ins_next

5948. //|

5949. //|2:  // Check for __index if table value is nil.

5950. //|  cmp dword TAB:RB->metatable, 0        // Shouldn't overwrite RA for fastpath.

5951. //|  jz >3

5952. //|  mov TAB:RA, TAB:RB->metatable

5953. //|  test byte TAB:RA->nomm, 1<<MM_index

5954. //|  jz ->vmeta_tgetb                        // 'no __index' flag NOT set: check.

5955. //|  movzx RA, PC_RA                        // Restore RA.

5956. dasm_put(Dst, 11971, LJ_TTAB, Dt6(->asize), Dt6(->array), LJ_TNIL, Dt6(->metatable), Dt6(->metatable), Dt6(->nomm), 1<<MM_index);

5957. #line 4290 "vm_x86.dasc"

5958. //|3:

5959. //|  mov dword [BASE+RA*8+4], LJ_TNIL

5960. //|  jmp <1

5961. dasm_put(Dst, 12067, LJ_TNIL);

5962. #line 4293 "vm_x86.dasc"

5963. break;

5964. case BC_TGETR:

5965. //|  ins_ABC        // RA = dst, RB = table, RC = key

5966. //|  mov TAB:RB, [BASE+RB*8]

5967. //|.if DUALNUM

5968. //|  mov RC, dword [BASE+RC*8]

5969. //|.else

5970. //|  cvttsd2si RC, qword [BASE+RC*8]

5971. //|.endif

5972. //|  cmp RC, TAB:RB->asize

5973. //|  jae ->vmeta_tgetr                // Not in array part? Use fallback.

5974. //|  shl RC, 3

5975. //|  add RC, TAB:RB->array

5976. //|  // Get array slot.

5977. //|->BC_TGETR_Z:

5978. //|.if X64

5979. //|  mov RBa, [RC]

5980. //|  mov [BASE+RA*8], RBa

5981. //|.else

5982. //|  mov RB, [RC]

5983. //|  mov RC, [RC+4]

5984. //|  mov [BASE+RA*8], RB

5985. //|  mov [BASE+RA*8+4], RC

5986. //|.endif

5987. //|->BC_TGETR2_Z:

5988. //|  ins_next

5989. dasm_put(Dst, 12084, Dt6(->asize), Dt6(->array));

5990. #line 4319 "vm_x86.dasc"

5991. break;

5993. case BC_TSETV:

5994. //|  ins_ABC        // RA = src, RB = table, RC = key

5995. //|  checktab RB, ->vmeta_tsetv

5996. //|  mov TAB:RB, [BASE+RB*8]

5997. //|

5998. //|  // Integer key?

5999. //|.if DUALNUM

6000. //|  checkint RC, >5

6001. //|  mov RC, dword [BASE+RC*8]

6002. //|.else

6003. //|  // Convert number to int and back and compare.

6004. //|  checknum RC, >5

6005. //|  movsd xmm0, qword [BASE+RC*8]

6006. //|  cvttsd2si RC, xmm0

6007. //|  cvtsi2sd xmm1, RC

6008. //|  ucomisd xmm0, xmm1

6009. //|  jne ->vmeta_tsetv                // Generic numeric key? Use fallback.

6010. //|.endif

6011. //|  cmp RC, TAB:RB->asize                // Takes care of unordered, too.

6012. //|  jae ->vmeta_tsetv

6013. //|  shl RC, 3

6014. //|  add RC, TAB:RB->array

6015. //|  cmp dword [RC+4], LJ_TNIL

6016. //|  je >3                                // Previous value is nil?

6017. //|1:

6018. //|  test byte TAB:RB->marked, LJ_GC_BLACK        // isblack(table)

6019. //|  jnz >7

6020. //|2:  // Set array slot.

6021. //|.if X64

6022. //|  mov RBa, [BASE+RA*8]

6023. //|  mov [RC], RBa

6024. //|.else

6025. //|  mov RB, [BASE+RA*8+4]

6026. //|  mov RA, [BASE+RA*8]

6027. //|  mov [RC+4], RB

6028. //|  mov [RC], RA

6029. //|.endif

6030. //|  ins_next

6031. //|

6032. //|3:  // Check for __newindex if previous value is nil.

6033. //|  cmp dword TAB:RB->metatable, 0        // Shouldn't overwrite RA for fastpath.

6034. dasm_put(Dst, 12143, LJ_TTAB, LJ_TISNUM, Dt6(->asize), Dt6(->array), LJ_TNIL, Dt6(->marked), LJ_GC_BLACK);

6035. #line 4362 "vm_x86.dasc"

6036. //|  jz <1

6037. //|  mov TAB:RA, TAB:RB->metatable

6038. //|  test byte TAB:RA->nomm, 1<<MM_newindex

6039. //|  jz ->vmeta_tsetv                        // 'no __newindex' flag NOT set: check.

6040. //|  movzx RA, PC_RA                        // Restore RA.

6041. //|  jmp <1

6042. //|

6043. //|5:  // String key?

6044. //|  checkstr RC, ->vmeta_tsetv

6045. //|  mov STR:RC, [BASE+RC*8]

6046. //|  jmp ->BC_TSETS_Z

6047. //|

6048. //|7:  // Possible table write barrier for the value. Skip valiswhite check.

6049. //|  barrierback TAB:RB, RA

6050. dasm_put(Dst, 12243, Dt6(->metatable), Dt6(->metatable), Dt6(->nomm), 1<<MM_newindex, LJ_TSTR, Dt6(->marked), (uint8_t)~LJ_GC_BLACK, DISPATCH_GL(gc.grayagain));

6051. #line 4376 "vm_x86.dasc"

6052. //|  movzx RA, PC_RA                        // Restore RA.

6053. //|  jmp <2

6054. dasm_put(Dst, 12302, DISPATCH_GL(gc.grayagain), Dt6(->gclist));

6055. #line 4378 "vm_x86.dasc"

6056. break;

6057. case BC_TSETS:

6058. //|  ins_ABC        // RA = src, RB = table, RC = str const (~)

6059. //|  not RCa

6060. //|  mov STR:RC, [KBASE+RC*4]

6061. //|  checktab RB, ->vmeta_tsets

6062. //|  mov TAB:RB, [BASE+RB*8]

6063. //|->BC_TSETS_Z:        // RB = GCtab *, RC = GCstr *, refetches PC_RA.

6064. //|  mov RA, TAB:RB->hmask

6065. //|  and RA, STR:RC->hash

6066. //|  imul RA, #NODE

6067. //|  mov byte TAB:RB->nomm, 0                // Clear metamethod cache.

6068. //|  add NODE:RA, TAB:RB->node

6069. //|1:

6070. //|  cmp dword NODE:RA->key.it, LJ_TSTR

6071. //|  jne >5

6072. //|  cmp dword NODE:RA->key.gcr, STR:RC

6073. //|  jne >5

6074. //|  // Ok, key found. Assumes: offsetof(Node, val) == 0

6075. //|  cmp dword [RA+4], LJ_TNIL

6076. //|  je >4                                // Previous value is nil?

6077. //|2:

6078. //|  test byte TAB:RB->marked, LJ_GC_BLACK        // isblack(table)

6079. dasm_put(Dst, 12319, LJ_TTAB, Dt6(->hmask), Dt5(->hash), sizeof(Node), Dt6(->nomm), Dt6(->node), DtB(->key.it), LJ_TSTR, DtB(->key.gcr), LJ_TNIL);

6080. #line 4401 "vm_x86.dasc"

6081. //|  jnz >7

6082. //|3:  // Set node value.

6083. //|  movzx RC, PC_RA

6084. //|.if X64

6085. //|  mov RBa, [BASE+RC*8]

6086. //|  mov [RA], RBa

6087. //|.else

6088. //|  mov RB, [BASE+RC*8+4]

6089. //|  mov RC, [BASE+RC*8]

6090. //|  mov [RA+4], RB

6091. //|  mov [RA], RC

6092. //|.endif

6093. //|  ins_next

6094. //|

6095. //|4:  // Check for __newindex if previous value is nil.

6096. //|  cmp dword TAB:RB->metatable, 0        // Shouldn't overwrite RA for fastpath.

6097. //|  jz <2

6098. //|  mov TMP1, RA                        // Save RA.

6099. //|  mov TAB:RA, TAB:RB->metatable

6100. //|  test byte TAB:RA->nomm, 1<<MM_newindex

6101. //|  jz ->vmeta_tsets                        // 'no __newindex' flag NOT set: check.

6102. //|  mov RA, TMP1                        // Restore RA.

6103. //|  jmp <2

6104. //|

6105. //|5:  // Follow hash chain.

6106. //|  mov NODE:RA, NODE:RA->next

6107. //|  test NODE:RA, NODE:RA

6108. //|  jnz <1

6109. //|  // End of hash chain: key not found, add a new one.

6110. //|

6111. //|  // But check for __newindex first.

6112. //|  mov TAB:RA, TAB:RB->metatable

6113. dasm_put(Dst, 12396, Dt6(->marked), LJ_GC_BLACK, Dt6(->metatable), Dt6(->metatable), Dt6(->nomm), 1<<MM_newindex, DtB(->next));

6114. #line 4433 "vm_x86.dasc"

6115. //|  test TAB:RA, TAB:RA

6116. //|  jz >6                                // No metatable: continue.

6117. //|  test byte TAB:RA->nomm, 1<<MM_newindex

6118. //|  jz ->vmeta_tsets                        // 'no __newindex' flag NOT set: check.

6119. //|6:

6120. //|  mov TMP1, STR:RC

6121. //|  mov TMP2, LJ_TSTR

6122. //|  mov TMP3, TAB:RB                        // Save TAB:RB for us.

6123. //|.if X64

6124. //|  mov L:CARG1d, SAVE_L

6125. //|  mov L:CARG1d->base, BASE

6126. //|  lea CARG3, TMP1

6127. //|  mov CARG2d, TAB:RB

6128. //|  mov L:RB, L:CARG1d

6129. //|.else

6130. //|  lea RC, TMP1                        // Store temp. TValue in TMP1/TMP2.

6131. //|  mov ARG2, TAB:RB

6132. //|  mov L:RB, SAVE_L

6133. //|  mov ARG3, RC

6134. //|  mov ARG1, L:RB

6135. //|  mov L:RB->base, BASE

6136. //|.endif

6137. //|  mov SAVE_PC, PC

6138. //|  call extern lj_tab_newkey        // (lua_State *L, GCtab *t, TValue *k)

6139. //|  // Handles write barrier for the new key. TValue * returned in eax (RC).

6140. //|  mov BASE, L:RB->base

6141. //|  mov TAB:RB, TMP3                        // Need TAB:RB for barrier.

6142. //|  mov RA, eax

6143. //|  jmp <2                                // Must check write barrier for value.

6144. //|

6145. //|7:  // Possible table write barrier for the value. Skip valiswhite check.

6146. //|  barrierback TAB:RB, RC                // Destroys STR:RC.

6147. //|  jmp <3

6148. dasm_put(Dst, 12483, Dt6(->metatable), Dt6(->nomm), 1<<MM_newindex, LJ_TSTR, Dt1(->base), Dt1(->base), Dt6(->marked), (uint8_t)~LJ_GC_BLACK, DISPATCH_GL(gc.grayagain), DISPATCH_GL(gc.grayagain), Dt6(->gclist));

6149. #line 4466 "vm_x86.dasc"

6150. break;

6151. case BC_TSETB:

6152. //|  ins_ABC        // RA = src, RB = table, RC = byte literal

6153. //|  checktab RB, ->vmeta_tsetb

6154. //|  mov TAB:RB, [BASE+RB*8]

6155. //|  cmp RC, TAB:RB->asize

6156. //|  jae ->vmeta_tsetb

6157. //|  shl RC, 3

6158. //|  add RC, TAB:RB->array

6159. //|  cmp dword [RC+4], LJ_TNIL

6160. //|  je >3                                // Previous value is nil?

6161. //|1:

6162. //|  test byte TAB:RB->marked, LJ_GC_BLACK        // isblack(table)

6163. //|  jnz >7

6164. //|2:         // Set array slot.

6165. //|.if X64

6166. //|  mov RAa, [BASE+RA*8]

6167. //|  mov [RC], RAa

6168. //|.else

6169. //|  mov RB, [BASE+RA*8+4]

6170. //|  mov RA, [BASE+RA*8]

6171. //|  mov [RC+4], RB

6172. //|  mov [RC], RA

6173. //|.endif

6174. //|  ins_next

6175. //|

6176. //|3:  // Check for __newindex if previous value is nil.

6177. //|  cmp dword TAB:RB->metatable, 0        // Shouldn't overwrite RA for fastpath.

6178. //|  jz <1

6179. //|  mov TAB:RA, TAB:RB->metatable

6180. dasm_put(Dst, 12575, LJ_TTAB, Dt6(->asize), Dt6(->array), LJ_TNIL, Dt6(->marked), LJ_GC_BLACK, Dt6(->metatable));

6181. #line 4496 "vm_x86.dasc"

6182. //|  test byte TAB:RA->nomm, 1<<MM_newindex

6183. //|  jz ->vmeta_tsetb                        // 'no __newindex' flag NOT set: check.

6184. //|  movzx RA, PC_RA                        // Restore RA.

6185. //|  jmp <1

6186. //|

6187. //|7:  // Possible table write barrier for the value. Skip valiswhite check.

6188. //|  barrierback TAB:RB, RA

6189. //|  movzx RA, PC_RA                        // Restore RA.

6190. //|  jmp <2

6191. dasm_put(Dst, 12670, Dt6(->metatable), Dt6(->nomm), 1<<MM_newindex, Dt6(->marked), (uint8_t)~LJ_GC_BLACK, DISPATCH_GL(gc.grayagain), DISPATCH_GL(gc.grayagain), Dt6(->gclist));

6192. #line 4505 "vm_x86.dasc"

6193. break;

6194. case BC_TSETR:

6195. //|  ins_ABC        // RA = src, RB = table, RC = key

6196. //|  mov TAB:RB, [BASE+RB*8]

6197. //|.if DUALNUM

6198. //|  mov RC, dword [BASE+RC*8]

6199. //|.else

6200. //|  cvttsd2si RC, qword [BASE+RC*8]

6201. //|.endif

6202. //|  test byte TAB:RB->marked, LJ_GC_BLACK        // isblack(table)

6203. //|  jnz >7

6204. //|2:

6205. //|  cmp RC, TAB:RB->asize

6206. //|  jae ->vmeta_tsetr

6207. //|  shl RC, 3

6208. //|  add RC, TAB:RB->array

6209. //|  // Set array slot.

6210. //|->BC_TSETR_Z:

6211. //|.if X64

6212. //|  mov RBa, [BASE+RA*8]

6213. //|  mov [RC], RBa

6214. //|.else

6215. //|  mov RB, [BASE+RA*8+4]

6216. //|  mov RA, [BASE+RA*8]

6217. //|  mov [RC+4], RB

6218. //|  mov [RC], RA

6219. //|.endif

6220. //|  ins_next

6221. //|

6222. //|7:  // Possible table write barrier for the value. Skip valiswhite check.

6223. //|  barrierback TAB:RB, RA

6224. //|  movzx RA, PC_RA                        // Restore RA.

6225. //|  jmp <2

6226. dasm_put(Dst, 12718, Dt6(->marked), LJ_GC_BLACK, Dt6(->asize), Dt6(->array), Dt6(->marked), (uint8_t)~LJ_GC_BLACK, DISPATCH_GL(gc.grayagain), DISPATCH_GL(gc.grayagain), Dt6(->gclist));

6227. #line 4538 "vm_x86.dasc"

6228. break;

6230. case BC_TSETM:

6231. //|  ins_AD        // RA = base (table at base-1), RD = num const (start index)

6232. //|  mov TMP1, KBASE                        // Need one more free register.

6233. //|  mov KBASE, dword [KBASE+RD*8]        // Integer constant is in lo-word.

6234. //|1:

6235. //|  lea RA, [BASE+RA*8]

6236. //|  mov TAB:RB, [RA-8]                // Guaranteed to be a table.

6237. //|  test byte TAB:RB->marked, LJ_GC_BLACK        // isblack(table)

6238. //|  jnz >7

6239. //|2:

6240. //|  mov RD, MULTRES

6241. //|  sub RD, 1

6242. //|  jz >4                                // Nothing to copy?

6243. //|  add RD, KBASE                        // Compute needed size.

6244. //|  cmp RD, TAB:RB->asize

6245. //|  ja >5                                // Doesn't fit into array part?

6246. //|  sub RD, KBASE

6247. //|  shl KBASE, 3

6248. //|  add KBASE, TAB:RB->array

6249. //|3:  // Copy result slots to table.

6250. //|.if X64

6251. //|  mov RBa, [RA]

6252. //|  add RA, 8

6253. //|  mov [KBASE], RBa

6254. //|.else

6255. //|  mov RB, [RA]

6256. //|  mov [KBASE], RB

6257. //|  mov RB, [RA+4]

6258. //|  add RA, 8

6259. //|  mov [KBASE+4], RB

6260. //|.endif

6261. //|  add KBASE, 8

6262. //|  sub RD, 1

6263. //|  jnz <3

6264. //|4:

6265. //|  mov KBASE, TMP1

6266. //|  ins_next

6267. //|

6268. //|5:  // Need to resize array part.

6269. //|.if X64

6270. //|  mov L:CARG1d, SAVE_L

6271. //|  mov L:CARG1d->base, BASE                // Caveat: CARG2d/CARG3d may be BASE.

6272. //|  mov CARG2d, TAB:RB

6273. //|  mov CARG3d, RD

6274. //|  mov L:RB, L:CARG1d

6275. //|.else

6276. //|  mov ARG2, TAB:RB

6277. //|  mov L:RB, SAVE_L

6278. //|  mov L:RB->base, BASE

6279. //|  mov ARG3, RD

6280. //|  mov ARG1, L:RB

6281. //|.endif

6282. //|  mov SAVE_PC, PC

6283. //|  call extern lj_tab_reasize        // (lua_State *L, GCtab *t, int nasize)

6284. //|  mov BASE, L:RB->base

6285. //|  movzx RA, PC_RA                        // Restore RA.

6286. //|  jmp <1                                // Retry.

6287. //|

6288. //|7:  // Possible table write barrier for any value. Skip valiswhite check.

6289. //|  barrierback TAB:RB, RD

6290. dasm_put(Dst, 12812, Dt6(->marked), LJ_GC_BLACK, Dt6(->asize), Dt6(->array), Dt1(->base), Dt1(->base));

6291. #line 4600 "vm_x86.dasc"

6292. //|  jmp <2

6293. dasm_put(Dst, 12962, Dt6(->marked), (uint8_t)~LJ_GC_BLACK, DISPATCH_GL(gc.grayagain), DISPATCH_GL(gc.grayagain), Dt6(->gclist));

6294. #line 4601 "vm_x86.dasc"

6295. break;

6297. /* -- Calls and vararg handling ----------------------------------------- */

6299. case BC_CALL: case BC_CALLM:

6300. //|  ins_A_C        // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs

6301. dasm_put(Dst, 10040);

6302. #line 4607 "vm_x86.dasc"

6303. if (op == BC_CALLM) {

6304. //|  add NARGS:RD, MULTRES

6305. dasm_put(Dst, 12982);

6306. #line 4609 "vm_x86.dasc"

6307. }

6308. //|  cmp dword [BASE+RA*8+4], LJ_TFUNC

6309. //|  mov LFUNC:RB, [BASE+RA*8]

6310. //|  jne ->vmeta_call_ra

6311. //|  lea BASE, [BASE+RA*8+8]

6312. //|  ins_call

6313. dasm_put(Dst, 12987, LJ_TFUNC, Dt7(->pc));

6314. #line 4615 "vm_x86.dasc"

6315. break;

6317. case BC_CALLMT:

6318. //|  ins_AD        // RA = base, RD = extra_nargs

6319. //|  add NARGS:RD, MULTRES

6320. //|  // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.

6321. dasm_put(Dst, 12982);

6322. #line 4621 "vm_x86.dasc"

6323. break;

6324. case BC_CALLT:

6325. //|  ins_AD        // RA = base, RD = nargs+1

6326. //|  lea RA, [BASE+RA*8+8]

6327. //|  mov KBASE, BASE                        // Use KBASE for move + vmeta_call hint.

6328. //|  mov LFUNC:RB, [RA-8]

6329. //|  cmp dword [RA-4], LJ_TFUNC

6330. //|  jne ->vmeta_call

6331. //|->BC_CALLT_Z:

6332. //|  mov PC, [BASE-4]

6333. //|  test PC, FRAME_TYPE

6334. //|  jnz >7

6335. //|1:

6336. //|  mov [BASE-8], LFUNC:RB                // Copy function down, reloaded below.

6337. //|  mov MULTRES, NARGS:RD

6338. //|  sub NARGS:RD, 1

6339. //|  jz >3

6340. //|2:  // Move args down.

6341. //|.if X64

6342. //|  mov RBa, [RA]

6343. //|  add RA, 8

6344. //|  mov [KBASE], RBa

6345. //|.else

6346. //|  mov RB, [RA]

6347. //|  mov [KBASE], RB

6348. //|  mov RB, [RA+4]

6349. //|  add RA, 8

6350. //|  mov [KBASE+4], RB

6351. //|.endif

6352. //|  add KBASE, 8

6353. //|  sub NARGS:RD, 1

6354. //|  jnz <2

6355. //|

6356. //|  mov LFUNC:RB, [BASE-8]

6357. //|3:

6358. //|  mov NARGS:RD, MULTRES

6359. //|  cmp byte LFUNC:RB->ffid, 1        // (> FF_C) Calling a fast function?

6360. //|  ja >5

6361. //|4:

6362. //|  ins_callt

6363. //|

6364. //|5:  // Tailcall to a fast function.

6365. //|  test PC, FRAME_TYPE                // Lua frame below?

6366. dasm_put(Dst, 13030, LJ_TFUNC, FRAME_TYPE, Dt7(->ffid), Dt7(->pc));

6367. #line 4664 "vm_x86.dasc"

6368. //|  jnz <4

6369. //|  movzx RA, PC_RA

6370. //|  not RAa

6371. //|  mov LFUNC:KBASE, [BASE+RA*8-8]        // Need to prepare KBASE.

6372. //|  mov KBASE, LFUNC:KBASE->pc

6373. //|  mov KBASE, [KBASE+PC2PROTO(k)]

6374. //|  jmp <4

6375. //|

6376. //|7:  // Tailcall from a vararg function.

6377. //|  sub PC, FRAME_VARG

6378. //|  test PC, FRAME_TYPEP

6379. //|  jnz >8                                // Vararg frame below?

6380. //|  sub BASE, PC                        // Need to relocate BASE/KBASE down.

6381. //|  mov KBASE, BASE

6382. //|  mov PC, [BASE-4]

6383. //|  jmp <1

6384. //|8:

6385. //|  add PC, FRAME_VARG

6386. //|  jmp <1

6387. dasm_put(Dst, 13148, FRAME_TYPE, Dt7(->pc), PC2PROTO(k), FRAME_VARG, FRAME_TYPEP, FRAME_VARG);

6388. #line 4683 "vm_x86.dasc"

6389. break;

6391. case BC_ITERC:

6392. //|  ins_A        // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)

6393. //|  lea RA, [BASE+RA*8+8]                // fb = base+1

6394. //|.if X64

6395. //|  mov RBa, [RA-24]                        // Copy state. fb[0] = fb[-3].

6396. //|  mov RCa, [RA-16]                        // Copy control var. fb[1] = fb[-2].

6397. //|  mov [RA], RBa

6398. //|  mov [RA+8], RCa

6399. //|.else

6400. //|  mov RB, [RA-24]                        // Copy state. fb[0] = fb[-3].

6401. //|  mov RC, [RA-20]

6402. //|  mov [RA], RB

6403. //|  mov [RA+4], RC

6404. //|  mov RB, [RA-16]                        // Copy control var. fb[1] = fb[-2].

6405. //|  mov RC, [RA-12]

6406. //|  mov [RA+8], RB

6407. //|  mov [RA+12], RC

6408. //|.endif

6409. //|  mov LFUNC:RB, [RA-32]                // Copy callable. fb[-1] = fb[-4]

6410. //|  mov RC, [RA-28]

6411. //|  mov [RA-8], LFUNC:RB

6412. //|  mov [RA-4], RC

6413. //|  cmp RC, LJ_TFUNC                        // Handle like a regular 2-arg call.

6414. //|  mov NARGS:RD, 2+1

6415. //|  jne ->vmeta_call

6416. //|  mov BASE, RA

6417. //|  ins_call

6418. dasm_put(Dst, 13220, LJ_TFUNC, 2+1, Dt7(->pc));

6419. #line 4712 "vm_x86.dasc"

6420. break;

6422. case BC_ITERN:

6423. //|  ins_A        // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))

6424. //|.if JIT

6425. //|  // NYI: add hotloop, record BC_ITERN.

6426. //|.endif

6427. //|  mov TMP1, KBASE                        // Need two more free registers.

6428. //|  mov TMP2, DISPATCH

6429. //|  mov TAB:RB, [BASE+RA*8-16]

6430. //|  mov RC, [BASE+RA*8-8]                // Get index from control var.

6431. //|  mov DISPATCH, TAB:RB->asize

6432. //|  add PC, 4

6433. //|  mov KBASE, TAB:RB->array

6434. //|1:  // Traverse array part.

6435. //|  cmp RC, DISPATCH; jae >5                // Index points after array part?

6436. //|  cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4

6437. //|.if DUALNUM

6438. //|  mov dword [BASE+RA*8+4], LJ_TISNUM

6439. //|  mov dword [BASE+RA*8], RC

6440. //|.else

6441. //|  cvtsi2sd xmm0, RC

6442. //|.endif

6443. //|  // Copy array slot to returned value.

6444. //|.if X64

6445. //|  mov RBa, [KBASE+RC*8]

6446. //|  mov [BASE+RA*8+8], RBa

6447. //|.else

6448. //|  mov RB, [KBASE+RC*8+4]

6449. //|  mov [BASE+RA*8+12], RB

6450. //|  mov RB, [KBASE+RC*8]

6451. //|  mov [BASE+RA*8+8], RB

6452. //|.endif

6453. //|  add RC, 1

6454. //|  // Return array index as a numeric key.

6455. //|.if DUALNUM

6456. //|  // See above.

6457. //|.else

6458. //|  movsd qword [BASE+RA*8], xmm0

6459. //|.endif

6460. //|  mov [BASE+RA*8-8], RC                // Update control var.

6461. //|2:

6462. //|  movzx RD, PC_RD                        // Get target from ITERL.

6463. //|  branchPC RD

6464. //|3:

6465. //|  mov DISPATCH, TMP2

6466. //|  mov KBASE, TMP1

6467. //|  ins_next

6468. //|

6469. //|4:  // Skip holes in array part.

6470. //|  add RC, 1

6471. //|  jmp <1

6472. //|

6473. //|5:  // Traverse hash part.

6474. //|  sub RC, DISPATCH

6475. //|6:

6476. //|  cmp RC, TAB:RB->hmask; ja <3        // End of iteration? Branch to ITERL+1.

6477. //|  imul KBASE, RC, #NODE

6478. dasm_put(Dst, 13292, Dt6(->asize), Dt6(->array), LJ_TNIL, LJ_TISNUM, -BCBIAS_J*4, Dt6(->hmask));

6479. #line 4770 "vm_x86.dasc"

6480. //|  add NODE:KBASE, TAB:RB->node

6481. //|  cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7

6482. //|  lea DISPATCH, [RC+DISPATCH+1]

6483. //|  // Copy key and value from hash slot.

6484. //|.if X64

6485. //|  mov RBa, NODE:KBASE->key

6486. //|  mov RCa, NODE:KBASE->val

6487. //|  mov [BASE+RA*8], RBa

6488. //|  mov [BASE+RA*8+8], RCa

6489. //|.else

6490. //|  mov RB, NODE:KBASE->key.gcr

6491. //|  mov RC, NODE:KBASE->key.it

6492. //|  mov [BASE+RA*8], RB

6493. //|  mov [BASE+RA*8+4], RC

6494. //|  mov RB, NODE:KBASE->val.gcr

6495. //|  mov RC, NODE:KBASE->val.it

6496. //|  mov [BASE+RA*8+8], RB

6497. //|  mov [BASE+RA*8+12], RC

6498. //|.endif

6499. //|  mov [BASE+RA*8-8], DISPATCH

6500. //|  jmp <2

6501. //|

6502. //|7:  // Skip holes in hash part.

6503. //|  add RC, 1

6504. //|  jmp <6

6505. dasm_put(Dst, 13436, sizeof(Node), Dt6(->node), DtB(->val.it), LJ_TNIL, DtB(->key), DtB(->val));

6506. #line 4795 "vm_x86.dasc"

6507. break;

6509. case BC_ISNEXT:

6510. //|  ins_AD        // RA = base, RD = target (points to ITERN)

6511. //|  cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5

6512. //|  mov CFUNC:RB, [BASE+RA*8-24]

6513. //|  cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5

6514. //|  cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5

6515. //|  cmp byte CFUNC:RB->ffid, FF_next_N; jne >5

6516. //|  branchPC RD

6517. //|  mov dword [BASE+RA*8-8], 0        // Initialize control var.

6518. //|  mov dword [BASE+RA*8-4], 0xfffe7fff

6519. //|1:

6520. //|  ins_next

6521. //|5:  // Despecialize bytecode if any of the checks fail.

6522. //|  mov PC_OP, BC_JMP

6523. //|  branchPC RD

6524. //|  mov byte [PC], BC_ITERC

6525. //|  jmp <1

6526. dasm_put(Dst, 13496, LJ_TFUNC, LJ_TTAB, LJ_TNIL, Dt8(->ffid), FF_next_N, -BCBIAS_J*4, BC_JMP, -BCBIAS_J*4, BC_ITERC);

6527. #line 4814 "vm_x86.dasc"

6528. break;

6530. case BC_VARG:

6531. //|  ins_ABC        // RA = base, RB = nresults+1, RC = numparams

6532. //|  mov TMP1, KBASE                        // Need one more free register.

6533. //|  lea KBASE, [BASE+RC*8+(8+FRAME_VARG)]

6534. //|  lea RA, [BASE+RA*8]

6535. //|  sub KBASE, [BASE-4]

6536. //|  // Note: KBASE may now be even _above_ BASE if nargs was < numparams.

6537. //|  test RB, RB

6538. //|  jz >5                                // Copy all varargs?

6539. //|  lea RB, [RA+RB*8-8]

6540. //|  cmp KBASE, BASE                        // No vararg slots?

6541. //|  jnb >2

6542. //|1:  // Copy vararg slots to destination slots.

6543. //|.if X64

6544. //|  mov RCa, [KBASE-8]

6545. //|  add KBASE, 8

6546. //|  mov [RA], RCa

6547. //|.else

6548. //|  mov RC, [KBASE-8]

6549. //|  mov [RA], RC

6550. //|  mov RC, [KBASE-4]

6551. //|  add KBASE, 8

6552. //|  mov [RA+4], RC

6553. //|.endif

6554. //|  add RA, 8

6555. //|  cmp RA, RB                        // All destination slots filled?

6556. //|  jnb >3

6557. //|  cmp KBASE, BASE                        // No more vararg slots?

6558. //|  jb <1

6559. //|2:  // Fill up remainder with nil.

6560. //|  mov dword [RA+4], LJ_TNIL

6561. //|  add RA, 8

6562. //|  cmp RA, RB

6563. //|  jb <2

6564. //|3:

6565. //|  mov KBASE, TMP1

6566. //|  ins_next

6567. //|

6568. //|5:  // Copy all varargs.

6569. //|  mov MULTRES, 1                        // MULTRES = 0+1

6570. //|  mov RC, BASE

6571. //|  sub RC, KBASE

6572. //|  jbe <3                                // No vararg slots?

6573. //|  mov RB, RC

6574. //|  shr RB, 3

6575. //|  add RB, 1

6576. //|  mov MULTRES, RB                        // MULTRES = #varargs+1

6577. //|  mov L:RB, SAVE_L

6578. //|  add RC, RA

6579. //|  cmp RC, L:RB->maxstack

6580. //|  ja >7                                // Need to grow stack?

6581. //|6:  // Copy all vararg slots.

6582. //|.if X64

6583. //|  mov RCa, [KBASE-8]

6584. dasm_put(Dst, 13609, (8+FRAME_VARG), LJ_TNIL, Dt1(->maxstack));

6585. #line 4870 "vm_x86.dasc"

6586. //|  add KBASE, 8

6587. //|  mov [RA], RCa

6588. //|.else

6589. //|  mov RC, [KBASE-8]

6590. //|  mov [RA], RC

6591. //|  mov RC, [KBASE-4]

6592. //|  add KBASE, 8

6593. //|  mov [RA+4], RC

6594. //|.endif

6595. //|  add RA, 8

6596. //|  cmp KBASE, BASE                        // No more vararg slots?

6597. //|  jb <6

6598. //|  jmp <3

6599. //|

6600. //|7:  // Grow stack for varargs.

6601. //|  mov L:RB->base, BASE

6602. //|  mov L:RB->top, RA

6603. //|  mov SAVE_PC, PC

6604. //|  sub KBASE, BASE                        // Need delta, because BASE may change.

6605. //|  mov FCARG2, MULTRES

6606. //|  sub FCARG2, 1

6607. //|  mov FCARG1, L:RB

6608. //|  call extern lj_state_growstack@8        // (lua_State *L, int n)

6609. //|  mov BASE, L:RB->base

6610. //|  mov RA, L:RB->top

6611. //|  add KBASE, BASE

6612. //|  jmp <6

6613. dasm_put(Dst, 13776, Dt1(->base), Dt1(->top), Dt1(->base), Dt1(->top));

6614. #line 4897 "vm_x86.dasc"

6615. break;

6617. /* -- Returns ----------------------------------------------------------- */

6619. case BC_RETM:

6620. //|  ins_AD        // RA = results, RD = extra_nresults

6621. //|  add RD, MULTRES                        // MULTRES >=1, so RD >=1.

6622. //|  // Fall through. Assumes BC_RET follows and ins_AD is a no-op.

6623. dasm_put(Dst, 12982);

6624. #line 4905 "vm_x86.dasc"

6625. break;

6627. case BC_RET: case BC_RET0: case BC_RET1:

6628. //|  ins_AD        // RA = results, RD = nresults+1

6629. if (op != BC_RET0) {

6630. //|  shl RA, 3

6631. dasm_put(Dst, 13846);

6632. #line 4911 "vm_x86.dasc"

6633. }

6634. //|1:

6635. //|  mov PC, [BASE-4]

6636. //|  mov MULTRES, RD                        // Save nresults+1.

6637. //|  test PC, FRAME_TYPE                // Check frame type marker.

6638. //|  jnz >7                                // Not returning to a fixarg Lua func?

6639. dasm_put(Dst, 13850, FRAME_TYPE);

6640. #line 4917 "vm_x86.dasc"

6641. switch (op) {

6642. case BC_RET:

6643. //|->BC_RET_Z:

6644. //|  mov KBASE, BASE                // Use KBASE for result move.

6645. //|  sub RD, 1

6646. //|  jz >3

6647. //|2:  // Move results down.

6648. //|.if X64

6649. //|  mov RBa, [KBASE+RA]

6650. //|  mov [KBASE-8], RBa

6651. //|.else

6652. //|  mov RB, [KBASE+RA]

6653. //|  mov [KBASE-8], RB

6654. //|  mov RB, [KBASE+RA+4]

6655. //|  mov [KBASE-4], RB

6656. //|.endif

6657. //|  add KBASE, 8

6658. //|  sub RD, 1

6659. //|  jnz <2

6660. //|3:

6661. //|  mov RD, MULTRES                // Note: MULTRES may be >255.

6662. //|  movzx RB, PC_RB                // So cannot compare with RDL!

6663. //|5:

6664. //|  cmp RB, RD                        // More results expected?

6665. //|  ja >6

6666. dasm_put(Dst, 13869);

6667. #line 4942 "vm_x86.dasc"

6668. break;

6669. case BC_RET1:

6670. //|.if X64

6671. //|  mov RBa, [BASE+RA]

6672. //|  mov [BASE-8], RBa

6673. //|.else

6674. //|  mov RB, [BASE+RA+4]

6675. //|  mov [BASE-4], RB

6676. //|  mov RB, [BASE+RA]

6677. //|  mov [BASE-8], RB

6678. //|.endif

6679. dasm_put(Dst, 13923);

6680. #line 4953 "vm_x86.dasc"

6681. /* fallthrough */

6682. case BC_RET0:

6683. //|5:

6684. //|  cmp PC_RB, RDL                        // More results expected?

6685. //|  ja >6

6686. dasm_put(Dst, 13933);

6687. #line 4958 "vm_x86.dasc"

6688. default:

6689. break;

6690. }

6691. //|  movzx RA, PC_RA

6692. //|  not RAa                                // Note: ~RA = -(RA+1)

6693. //|  lea BASE, [BASE+RA*8]                // base = base - (RA+1)*8

6694. //|  mov LFUNC:KBASE, [BASE-8]

6695. //|  mov KBASE, LFUNC:KBASE->pc

6696. //|  mov KBASE, [KBASE+PC2PROTO(k)]

6697. //|  ins_next

6698. //|

6699. //|6:  // Fill up results with nil.

6700. dasm_put(Dst, 13944, Dt7(->pc), PC2PROTO(k));

6701. #line 4970 "vm_x86.dasc"

6702. if (op == BC_RET) {

6703. //|  mov dword [KBASE-4], LJ_TNIL        // Note: relies on shifted base.

6704. //|  add KBASE, 8

6705. dasm_put(Dst, 13992, LJ_TNIL);

6706. #line 4973 "vm_x86.dasc"

6707. } else {

6708. //|  mov dword [BASE+RD*8-12], LJ_TNIL

6709. dasm_put(Dst, 14003, LJ_TNIL);

6710. #line 4975 "vm_x86.dasc"

6711. }

6712. //|  add RD, 1

6713. //|  jmp <5

6714. //|

6715. //|7:  // Non-standard return case.

6716. //|  lea RB, [PC-FRAME_VARG]

6717. //|  test RB, FRAME_TYPEP

6718. //|  jnz ->vm_return

6719. //|  // Return from vararg function: relocate BASE down and RA up.

6720. //|  sub BASE, RB

6721. dasm_put(Dst, 14010, -FRAME_VARG, FRAME_TYPEP);

6722. #line 4985 "vm_x86.dasc"

6723. if (op != BC_RET0) {

6724. //|  add RA, RB

6725. dasm_put(Dst, 14034);

6726. #line 4987 "vm_x86.dasc"

6727. }

6728. //|  jmp <1

6729. dasm_put(Dst, 10015);

6730. #line 4989 "vm_x86.dasc"

6731. break;

6733. /* -- Loops and branches ------------------------------------------------ */

6735. //|.define FOR_IDX,  [RA];    .define FOR_TIDX,  dword [RA+4]

6736. //|.define FOR_STOP, [RA+8];  .define FOR_TSTOP, dword [RA+12]

6737. //|.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20]

6738. //|.define FOR_EXT,  [RA+24]; .define FOR_TEXT,  dword [RA+28]

6740. case BC_FORL:

6741. //|.if JIT

6742. //|  hotloop RB

6743. //|.endif

6744. //| // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.

6745. dasm_put(Dst, 14038, HOTCOUNT_PCMASK, GG_DISP2HOT, HOTCOUNT_LOOP);

6746. #line 5003 "vm_x86.dasc"

6747. break;

6749. case BC_JFORI:

6750. case BC_JFORL:

6751. #if !LJ_HASJIT

6752. break;

6753. #endif

6754. case BC_FORI:

6755. case BC_IFORL:

6756. vk = (op == BC_IFORL || op == BC_JFORL);

6757. //|  ins_AJ        // RA = base, RD = target (after end of loop or start of loop)

6758. //|  lea RA, [BASE+RA*8]

6759. dasm_put(Dst, 14059);

6760. #line 5015 "vm_x86.dasc"

6761. if (LJ_DUALNUM) {

6762. //|  cmp FOR_TIDX, LJ_TISNUM; jne >9

6763. dasm_put(Dst, 14063, LJ_TISNUM);

6764. #line 5017 "vm_x86.dasc"

6765. if (!vk) {

6766. //|  cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for

6767. //|  cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for

6768. //|  mov RB, dword FOR_IDX

6769. //|  cmp dword FOR_STEP, 0; jl >5

6770. dasm_put(Dst, 14073, LJ_TISNUM, LJ_TISNUM);

6771. #line 5022 "vm_x86.dasc"

6772. } else {

6773. #ifdef LUA_USE_ASSERT

6774. //|  cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type

6775. //|  cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type

6776. dasm_put(Dst, 14102, LJ_TISNUM, LJ_TISNUM);

6777. #line 5026 "vm_x86.dasc"

6778. #endif

6779. //|  mov RB, dword FOR_STEP

6780. //|  test RB, RB; js >5

6781. //|  add RB, dword FOR_IDX; jo >1

6782. //|  mov dword FOR_IDX, RB

6783. dasm_put(Dst, 14121);

6784. #line 5031 "vm_x86.dasc"

6785. }

6786. //|  cmp RB, dword FOR_STOP

6787. //|  mov FOR_TEXT, LJ_TISNUM

6788. //|  mov dword FOR_EXT, RB

6789. dasm_put(Dst, 14140, LJ_TISNUM);

6790. #line 5035 "vm_x86.dasc"

6791. if (op == BC_FORI) {

6792. //|  jle >7

6793. //|1:

6794. //|6:

6795. //|  branchPC RD

6796. dasm_put(Dst, 14151, -BCBIAS_J*4);

6797. #line 5040 "vm_x86.dasc"

6798. } else if (op == BC_JFORI) {

6799. //|  branchPC RD

6800. //|  movzx RD, PC_RD

6801. //|  jle =>BC_JLOOP

6802. //|1:

6803. //|6:

6804. dasm_put(Dst, 14165, -BCBIAS_J*4, BC_JLOOP);

6805. #line 5046 "vm_x86.dasc"

6806. } else if (op == BC_IFORL) {

6807. //|  jg >7

6808. //|6:

6809. //|  branchPC RD

6810. //|1:

6811. dasm_put(Dst, 14183, -BCBIAS_J*4);

6812. #line 5051 "vm_x86.dasc"

6813. } else {

6814. //|  jle =>BC_JLOOP

6815. //|1:

6816. //|6:

6817. dasm_put(Dst, 14175, BC_JLOOP);

6818. #line 5055 "vm_x86.dasc"

6819. }

6820. //|7:

6821. //|  ins_next

6822. //|

6823. //|5:  // Invert check for negative step.

6824. dasm_put(Dst, 14197);

6825. #line 5060 "vm_x86.dasc"

6826. if (vk) {

6827. //|  add RB, dword FOR_IDX; jo <1

6828. //|  mov dword FOR_IDX, RB

6829. dasm_put(Dst, 14222);

6830. #line 5063 "vm_x86.dasc"

6831. }

6832. //|  cmp RB, dword FOR_STOP

6833. //|  mov FOR_TEXT, LJ_TISNUM

6834. //|  mov dword FOR_EXT, RB

6835. dasm_put(Dst, 14140, LJ_TISNUM);

6836. #line 5067 "vm_x86.dasc"

6837. if (op == BC_FORI) {

6838. //|  jge <7

6839. dasm_put(Dst, 14231);

6840. #line 5069 "vm_x86.dasc"

6841. } else if (op == BC_JFORI) {

6842. //|  branchPC RD

6843. //|  movzx RD, PC_RD

6844. //|  jge =>BC_JLOOP

6845. dasm_put(Dst, 14236, -BCBIAS_J*4, BC_JLOOP);

6846. #line 5073 "vm_x86.dasc"

6847. } else if (op == BC_IFORL) {

6848. //|  jl <7

6849. dasm_put(Dst, 14250);

6850. #line 5075 "vm_x86.dasc"

6851. } else {

6852. //|  jge =>BC_JLOOP

6853. dasm_put(Dst, 14246, BC_JLOOP);

6854. #line 5077 "vm_x86.dasc"

6855. }

6856. //|  jmp <6

6857. //|9:  // Fallback to FP variant.

6858. dasm_put(Dst, 14255);

6859. #line 5080 "vm_x86.dasc"

6860. } else if (!vk) {

6861. //|  cmp FOR_TIDX, LJ_TISNUM

6862. dasm_put(Dst, 14262, LJ_TISNUM);

6863. #line 5082 "vm_x86.dasc"

6864. }

6865. if (!vk) {

6866. //|  jae ->vmeta_for

6867. //|  cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for

6868. dasm_put(Dst, 14268, LJ_TISNUM);

6869. #line 5086 "vm_x86.dasc"

6870. } else {

6871. #ifdef LUA_USE_ASSERT

6872. //|  cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type

6873. //|  cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type

6874. dasm_put(Dst, 14282, LJ_TISNUM, LJ_TISNUM);

6875. #line 5090 "vm_x86.dasc"

6876. #endif

6877. }

6878. //|  mov RB, FOR_TSTEP                // Load type/hiword of for step.

6879. dasm_put(Dst, 14301);

6880. #line 5093 "vm_x86.dasc"

6881. if (!vk) {

6882. //|  cmp RB, LJ_TISNUM; jae ->vmeta_for

6883. dasm_put(Dst, 14305, LJ_TISNUM);

6884. #line 5095 "vm_x86.dasc"

6885. }

6886. //|  movsd xmm0, qword FOR_IDX

6887. //|  movsd xmm1, qword FOR_STOP

6888. dasm_put(Dst, 14314);

6889. #line 5098 "vm_x86.dasc"

6890. if (vk) {

6891. //|  addsd xmm0, qword FOR_STEP

6892. //|  movsd qword FOR_IDX, xmm0

6893. //|  test RB, RB; js >3

6894. dasm_put(Dst, 14326);

6895. #line 5102 "vm_x86.dasc"

6896. } else {

6897. //|  jl >3

6898. dasm_put(Dst, 14345);

6899. #line 5104 "vm_x86.dasc"

6900. }

6901. //|  ucomisd xmm1, xmm0

6902. //|1:

6903. //|  movsd qword FOR_EXT, xmm0

6904. dasm_put(Dst, 14350);

6905. #line 5108 "vm_x86.dasc"

6906. if (op == BC_FORI) {

6907. //|.if DUALNUM

6908. //|  jnb <7

6909. //|.else

6910. //|  jnb >2

6911. //|  branchPC RD

6912. //|.endif

6913. dasm_put(Dst, 14363);

6914. #line 5115 "vm_x86.dasc"

6915. } else if (op == BC_JFORI) {

6916. //|  branchPC RD

6917. //|  movzx RD, PC_RD

6918. //|  jnb =>BC_JLOOP

6919. dasm_put(Dst, 14368, -BCBIAS_J*4, BC_JLOOP);

6920. #line 5119 "vm_x86.dasc"

6921. } else if (op == BC_IFORL) {

6922. //|.if DUALNUM

6923. //|  jb <7

6924. //|.else

6925. //|  jb >2

6926. //|  branchPC RD

6927. //|.endif

6928. dasm_put(Dst, 14382);

6929. #line 5126 "vm_x86.dasc"

6930. } else {

6931. //|  jnb =>BC_JLOOP

6932. dasm_put(Dst, 14378, BC_JLOOP);

6933. #line 5128 "vm_x86.dasc"

6934. }

6935. //|.if DUALNUM

6936. //|  jmp <6

6937. //|.else

6938. //|2:

6939. //|  ins_next

6940. //|.endif

6941. //|

6942. //|3:  // Invert comparison if step is negative.

6943. //|  ucomisd xmm0, xmm1

6944. //|  jmp <1

6945. dasm_put(Dst, 14387);

6946. #line 5139 "vm_x86.dasc"

6947. break;

6949. case BC_ITERL:

6950. //|.if JIT

6951. //|  hotloop RB

6952. //|.endif

6953. //| // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.

6954. dasm_put(Dst, 14038, HOTCOUNT_PCMASK, GG_DISP2HOT, HOTCOUNT_LOOP);

6955. #line 5146 "vm_x86.dasc"

6956. break;

6958. case BC_JITERL:

6959. #if !LJ_HASJIT

6960. break;

6961. #endif

6962. case BC_IITERL:

6963. //|  ins_AJ        // RA = base, RD = target

6964. //|  lea RA, [BASE+RA*8]

6965. //|  mov RB, [RA+4]

6966. //|  cmp RB, LJ_TNIL; je >1                // Stop if iterator returned nil.

6967. dasm_put(Dst, 14402, LJ_TNIL);

6968. #line 5157 "vm_x86.dasc"

6969. if (op == BC_JITERL) {

6970. //|  mov [RA-4], RB

6971. //|  mov RB, [RA]

6972. //|  mov [RA-8], RB

6973. //|  jmp =>BC_JLOOP

6974. dasm_put(Dst, 14417, BC_JLOOP);

6975. #line 5162 "vm_x86.dasc"

6976. } else {

6977. //|  branchPC RD                        // Otherwise save control var + branch.

6978. //|  mov RD, [RA]

6979. //|  mov [RA-4], RB

6980. //|  mov [RA-8], RD

6981. dasm_put(Dst, 14431, -BCBIAS_J*4);

6982. #line 5167 "vm_x86.dasc"

6983. }

6984. //|1:

6985. //|  ins_next

6986. dasm_put(Dst, 9668);

6987. #line 5170 "vm_x86.dasc"

6988. break;

6990. case BC_LOOP:

6991. //|  ins_A        // RA = base, RD = target (loop extent)

6992. //|  // Note: RA/RD is only used by trace recorder to determine scope/extent

6993. //|  // This opcode does NOT jump, it's only purpose is to detect a hot loop.

6994. //|.if JIT

6995. //|  hotloop RB

6996. //|.endif

6997. //| // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.

6998. dasm_put(Dst, 14038, HOTCOUNT_PCMASK, GG_DISP2HOT, HOTCOUNT_LOOP);

6999. #line 5180 "vm_x86.dasc"

7000. break;

7002. case BC_ILOOP:

7003. //|  ins_A        // RA = base, RD = target (loop extent)

7004. //|  ins_next

7005. dasm_put(Dst, 9224);

7006. #line 5185 "vm_x86.dasc"

7007. break;

7009. case BC_JLOOP:

7010. //|.if JIT

7011. //|  ins_AD        // RA = base (ignored), RD = traceno

7012. #ifdef LUA_USE_TRACE_LOGS

7013. //|.if X64

7014. //|  mov L:RB, SAVE_L

7015. //|  mov L:RB->base, BASE  // Save BASE

7016. //|  mov TMP1, RD     // Save RD

7017. //|  mov CARG3d, PC  // CARG3d == BASE

7018. //|  mov FCARG2, RD

7019. //|  mov FCARG1, RB

7020. //|  call extern lj_log_trace_entry@8

7021. //|  mov RD, TMP1

7022. //|  mov BASE, L:RB->base

7023. //|.endif

7024. dasm_put(Dst, 14447, Dt1(->base), Dt1(->base));

7025. #line 5202 "vm_x86.dasc"

7026. #endif

7027. //|  mov RA, [DISPATCH+DISPATCH_J(trace)]

7028. //|  mov TRACE:RD, [RA+RD*4]

7029. //|  mov RDa, TRACE:RD->mcode

7030. //|  mov L:RB, SAVE_L

7031. //|  mov [DISPATCH+DISPATCH_GL(jit_base)], BASE

7032. //|  mov [DISPATCH+DISPATCH_GL(tmpbuf.L)], L:RB

7033. //|  // Save additional callee-save registers only used in compiled code.

7034. //|.if X64WIN

7035. //|  mov TMPQ, r12

7036. //|  mov TMPa, r13

7037. //|  mov CSAVE_4, r14

7038. //|  mov CSAVE_3, r15

7039. //|  mov RAa, rsp

7040. //|  sub rsp, 9*16+4*8

7041. //|  movdqa [RAa], xmm6

7042. //|  movdqa [RAa-1*16], xmm7

7043. //|  movdqa [RAa-2*16], xmm8

7044. //|  movdqa [RAa-3*16], xmm9

7045. //|  movdqa [RAa-4*16], xmm10

7046. //|  movdqa [RAa-5*16], xmm11

7047. //|  movdqa [RAa-6*16], xmm12

7048. //|  movdqa [RAa-7*16], xmm13

7049. //|  movdqa [RAa-8*16], xmm14

7050. //|  movdqa [RAa-9*16], xmm15

7051. //|.elif X64

7052. //|  mov TMPQ, r12

7053. //|  mov TMPa, r13

7054. //|  sub rsp, 16

7055. //|.endif

7056. //|  jmp RDa

7057. //|.endif

7058. dasm_put(Dst, 14475, DISPATCH_J(trace), DtD(->mcode), DISPATCH_GL(jit_base), DISPATCH_GL(tmpbuf.L));

7059. #line 5234 "vm_x86.dasc"

7060. break;

7062. case BC_JMP:

7063. //|  ins_AJ        // RA = unused, RD = target

7064. //|  branchPC RD

7065. //|  ins_next

7066. dasm_put(Dst, 14516, -BCBIAS_J*4);

7067. #line 5240 "vm_x86.dasc"

7068. break;

7070. /* -- Function headers -------------------------------------------------- */

7072. /*

7073. ** Reminder: A function may be called with func/args above L->maxstack,

7074. ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,

7075. ** too. This means all FUNC* ops (including fast functions) must check

7076. ** for stack overflow _before_ adding more slots!

7077. */

7079. case BC_FUNCF:

7080. //|.if JIT

7081. //|  hotcall RB

7082. //|.endif

7083. dasm_put(Dst, 14542, HOTCOUNT_PCMASK, GG_DISP2HOT, HOTCOUNT_CALL);

7084. #line 5255 "vm_x86.dasc"

7085. case BC_FUNCV:  /* NYI: compiled vararg functions. */

7086. //| // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.

7087. break;

7089. case BC_JFUNCF:

7090. #if !LJ_HASJIT

7091. break;

7092. #endif

7093. case BC_IFUNCF:

7094. //|  ins_AD  // BASE = new base, RA = framesize, RD = nargs+1

7095. //|  mov KBASE, [PC-4+PC2PROTO(k)]

7096. //|  mov L:RB, SAVE_L

7097. //|  lea RA, [BASE+RA*8]                // Top of frame.

7098. //|  cmp RA, L:RB->maxstack

7099. //|  ja ->vm_growstack_f

7100. //|  movzx RA, byte [PC-4+PC2PROTO(numparams)]

7101. //|  cmp NARGS:RD, RA                        // Check for missing parameters.

7102. //|  jbe >3

7103. //|2:

7104. dasm_put(Dst, 14563, -4+PC2PROTO(k), Dt1(->maxstack), -4+PC2PROTO(numparams));

7105. #line 5274 "vm_x86.dasc"

7106. if (op == BC_JFUNCF) {

7107. //|  movzx RD, PC_RD

7108. //|  jmp =>BC_JLOOP

7109. dasm_put(Dst, 14594, BC_JLOOP);

7110. #line 5277 "vm_x86.dasc"

7111. } else {

7112. //|  ins_next

7113. dasm_put(Dst, 9224);

7114. #line 5279 "vm_x86.dasc"

7115. }

7116. //|

7117. //|3:  // Clear missing parameters.

7118. //|  mov dword [BASE+NARGS:RD*8-4], LJ_TNIL

7119. //|  add NARGS:RD, 1

7120. //|  cmp NARGS:RD, RA

7121. //|  jbe <3

7122. //|  jmp <2

7123. dasm_put(Dst, 14603, LJ_TNIL);

7124. #line 5287 "vm_x86.dasc"

7125. break;

7127. case BC_JFUNCV:

7128. #if !LJ_HASJIT

7129. break;

7130. #endif

7131. //| int3  // NYI: compiled vararg functions

7132. dasm_put(Dst, 8467);

7133. #line 5294 "vm_x86.dasc"

7134. break;  /* NYI: compiled vararg functions. */

7136. case BC_IFUNCV:

7137. //|  ins_AD  // BASE = new base, RA = framesize, RD = nargs+1

7138. //|  lea RB, [NARGS:RD*8+FRAME_VARG]

7139. //|  lea RD, [BASE+NARGS:RD*8]

7140. //|  mov LFUNC:KBASE, [BASE-8]

7141. //|  mov [RD-4], RB                        // Store delta + FRAME_VARG.

7142. //|  mov [RD-8], LFUNC:KBASE                // Store copy of LFUNC.

7143. //|  mov L:RB, SAVE_L

7144. //|  lea RA, [RD+RA*8]

7145. //|  cmp RA, L:RB->maxstack

7146. //|  ja ->vm_growstack_v                // Need to grow stack.

7147. //|  mov RA, BASE

7148. //|  mov BASE, RD

7149. //|  movzx RB, byte [PC-4+PC2PROTO(numparams)]

7150. //|  test RB, RB

7151. //|  jz >2

7152. //|1:  // Copy fixarg slots up to new frame.

7153. //|  add RA, 8

7154. //|  cmp RA, BASE

7155. //|  jnb >3                                // Less args than parameters?

7156. //|  mov KBASE, [RA-8]

7157. //|  mov [RD], KBASE

7158. //|  mov KBASE, [RA-4]

7159. //|  mov [RD+4], KBASE

7160. //|  add RD, 8

7161. //|  mov dword [RA-4], LJ_TNIL        // Clear old fixarg slot (help the GC).

7162. //|  sub RB, 1

7163. //|  jnz <1

7164. //|2:

7165. dasm_put(Dst, 14625, FRAME_VARG, Dt1(->maxstack), -4+PC2PROTO(numparams), LJ_TNIL);

7166. #line 5325 "vm_x86.dasc"

7167. if (op == BC_JFUNCV) {

7168. //|  movzx RD, PC_RD

7169. //|  jmp =>BC_JLOOP

7170. dasm_put(Dst, 14594, BC_JLOOP);

7171. #line 5328 "vm_x86.dasc"

7172. } else {

7173. //|  mov KBASE, [PC-4+PC2PROTO(k)]

7174. //|  ins_next

7175. dasm_put(Dst, 14722, -4+PC2PROTO(k));

7176. #line 5331 "vm_x86.dasc"

7177. }

7178. //|

7179. //|3:  // Clear missing parameters.

7180. //|  mov dword [RD+4], LJ_TNIL

7181. //|  add RD, 8

7182. //|  sub RB, 1

7183. //|  jnz <3

7184. //|  jmp <2

7185. dasm_put(Dst, 14747, LJ_TNIL);

7186. #line 5339 "vm_x86.dasc"

7187. break;

7189. case BC_FUNCC:

7190. case BC_FUNCCW:

7191. //|  ins_AD  // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1

7192. //|  mov CFUNC:RB, [BASE-8]

7193. //|  mov KBASEa, CFUNC:RB->f

7194. //|  mov L:RB, SAVE_L

7195. //|  lea RD, [BASE+NARGS:RD*8-8]

7196. //|  mov L:RB->base, BASE

7197. //|  lea RA, [RD+8*LUA_MINSTACK]

7198. //|  cmp RA, L:RB->maxstack

7199. //|  mov L:RB->top, RD

7200. dasm_put(Dst, 14769, Dt8(->f), Dt1(->base), 8*LUA_MINSTACK, Dt1(->maxstack), Dt1(->top));

7201. #line 5352 "vm_x86.dasc"

7202. if (op == BC_FUNCC) {

7203. //|.if X64

7204. //|  mov CARG1d, L:RB                        // Caveat: CARG1d may be RA.

7205. //|.else

7206. //|  mov ARG1, L:RB

7207. //|.endif

7208. dasm_put(Dst, 14799);

7209. #line 5358 "vm_x86.dasc"

7210. } else {

7211. //|.if X64

7212. //|  mov CARG2, KBASEa

7213. //|  mov CARG1d, L:RB                        // Caveat: CARG1d may be RA.

7214. //|.else

7215. //|  mov ARG2, KBASEa

7216. //|  mov ARG1, L:RB

7217. //|.endif

7218. dasm_put(Dst, 14803);

7219. #line 5366 "vm_x86.dasc"

7220. }

7221. //|  ja ->vm_growstack_c                // Need to grow stack.

7222. //|  set_vmstate C

7223. dasm_put(Dst, 14811, DISPATCH_GL(vmstate), ~LJ_VMST_C);

7224. #line 5369 "vm_x86.dasc"

7225. if (op == BC_FUNCC) {

7226. //|  call KBASEa                        // (lua_State *L)

7227. dasm_put(Dst, 14821);

7228. #line 5371 "vm_x86.dasc"

7229. } else {

7230. //|  // (lua_State *L, lua_CFunction f)

7231. //|  call aword [DISPATCH+DISPATCH_GL(wrapf)]

7232. dasm_put(Dst, 14826, DISPATCH_GL(wrapf));

7233. #line 5374 "vm_x86.dasc"

7234. }

7235. //|  // nresults returned in eax (RD).

7236. //|  mov BASE, L:RB->base

7237. //|  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB

7238. //|  set_vmstate INTERP

7239. //|  lea RA, [BASE+RD*8]

7240. //|  neg RA

7241. //|  add RA, L:RB->top                // RA = (L->top-(L->base+nresults))*8

7242. //|  mov PC, [BASE-4]                        // Fetch PC of caller.

7243. //|  jmp ->vm_returnc

7244. dasm_put(Dst, 14832, Dt1(->base), DISPATCH_GL(cur_L), DISPATCH_GL(vmstate), ~LJ_VMST_INTERP, Dt1(->top));

7245. #line 5384 "vm_x86.dasc"

7246. break;

7248. /* ---------------------------------------------------------------------- */

7250. default:

7251. fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);

7252. exit(2);

7253. break;

7254. }

7255. }

7257. ‌static int build_backend(BuildCtx *ctx)

7258. {

7259. int op;

7260. dasm_growpc(Dst, BC__MAX);

7261. build_subroutines(ctx);

7262. //|.code_op

7263. dasm_put(Dst, 14862);

7264. #line 5401 "vm_x86.dasc"

7265. for (op = 0; op < BC__MAX; op++)

7266. build_ins(ctx, (BCOp)op, op);

7267. return BC__MAX;

7268. }

7270. /* Emit pseudo frame-info for all assembler functions. */

7271. ‌static void emit_asm_debug(BuildCtx *ctx)

7272. {

7273. int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);

7274. #if LJ_64

7275. ‌#define SZPTR        "8"

7276. ‌#define BSZPTR        "3"

7277. ‌#define REG_SP        "0x7"

7278. ‌#define REG_RA        "0x10"

7279. #else

7280. #define SZPTR        "4"

7281. #define BSZPTR        "2"

7282. #define REG_SP        "0x4"

7283. #define REG_RA        "0x8"

7284. #endif

7285. switch (ctx->mode) {

7286. case BUILD_elfasm:

7287. fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");

7288. fprintf(ctx->fp,

7289. ".Lframe0:\n"

7290. "\t.long .LECIE0-.LSCIE0\n"

7291. ".LSCIE0:\n"

7292. "\t.long 0xffffffff\n"

7293. "\t.byte 0x1\n"

7294. "\t.string \"\"\n"

7295. "\t.uleb128 0x1\n"

7296. "\t.sleb128 -" SZPTR "\n"

7297. "\t.byte " REG_RA "\n"

7298. "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"

7299. "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"

7300. "\t.align " SZPTR "\n"

7301. ".LECIE0:\n\n");

7302. fprintf(ctx->fp,

7303. ".LSFDE0:\n"

7304. "\t.long .LEFDE0-.LASFDE0\n"

7305. ".LASFDE0:\n"

7306. "\t.long .Lframe0\n"

7307. #if LJ_64

7308. "\t.quad .Lbegin\n"

7309. "\t.quad %d\n"

7310. "\t.byte 0xe\n\t.uleb128 %d\n"                /* def_cfa_offset */

7311. "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */

7312. "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */

7313. "\t.byte 0x8f\n\t.uleb128 0x4\n"        /* offset r15 */

7314. "\t.byte 0x8e\n\t.uleb128 0x5\n"        /* offset r14 */

7315. #else

7316. "\t.long .Lbegin\n"

7317. "\t.long %d\n"

7318. "\t.byte 0xe\n\t.uleb128 %d\n"                /* def_cfa_offset */

7319. "\t.byte 0x85\n\t.uleb128 0x2\n"        /* offset ebp */

7320. "\t.byte 0x87\n\t.uleb128 0x3\n"        /* offset edi */

7321. "\t.byte 0x86\n\t.uleb128 0x4\n"        /* offset esi */

7322. "\t.byte 0x83\n\t.uleb128 0x5\n"        /* offset ebx */

7323. #endif

7324. "\t.align " SZPTR "\n"

7325. ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);

7326. #if LJ_HASFFI

7327. fprintf(ctx->fp,

7328. ".LSFDE1:\n"

7329. "\t.long .LEFDE1-.LASFDE1\n"

7330. ".LASFDE1:\n"

7331. "\t.long .Lframe0\n"

7332. #if LJ_64

7333. "\t.quad lj_vm_ffi_call\n"

7334. "\t.quad %d\n"

7335. "\t.byte 0xe\n\t.uleb128 16\n"                /* def_cfa_offset */

7336. "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */

7337. "\t.byte 0xd\n\t.uleb128 0x6\n"                /* def_cfa_register rbp */

7338. "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */

7339. #else

7340. "\t.long lj_vm_ffi_call\n"

7341. "\t.long %d\n"

7342. "\t.byte 0xe\n\t.uleb128 8\n"                /* def_cfa_offset */

7343. "\t.byte 0x85\n\t.uleb128 0x2\n"        /* offset ebp */

7344. "\t.byte 0xd\n\t.uleb128 0x5\n"                /* def_cfa_register ebp */

7345. "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset ebx */

7346. #endif

7347. "\t.align " SZPTR "\n"

7348. ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);

7349. #endif

7350. #if (defined(__sun__) && defined(__svr4__))

7351. #if LJ_64

7352. fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@unwind\n");

7353. #else

7354. fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n");

7355. #endif

7356. #else

7357. fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");

7358. #endif

7359. fprintf(ctx->fp,

7360. ".Lframe1:\n"

7361. "\t.long .LECIE1-.LSCIE1\n"

7362. ".LSCIE1:\n"

7363. "\t.long 0\n"

7364. "\t.byte 0x1\n"

7365. "\t.string \"zPR\"\n"

7366. "\t.uleb128 0x1\n"

7367. "\t.sleb128 -" SZPTR "\n"

7368. "\t.byte " REG_RA "\n"

7369. "\t.uleb128 6\n"                        /* augmentation length */

7370. "\t.byte 0x1b\n"                        /* pcrel|sdata4 */

7371. "\t.long lj_err_unwind_dwarf-.\n"

7372. "\t.byte 0x1b\n"                        /* pcrel|sdata4 */

7373. "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"

7374. "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"

7375. "\t.align " SZPTR "\n"

7376. ".LECIE1:\n\n");

7377. fprintf(ctx->fp,

7378. ".LSFDE2:\n"

7379. "\t.long .LEFDE2-.LASFDE2\n"

7380. ".LASFDE2:\n"

7381. "\t.long .LASFDE2-.Lframe1\n"

7382. "\t.long .Lbegin-.\n"

7383. "\t.long %d\n"

7384. "\t.uleb128 0\n"                        /* augmentation length */

7385. "\t.byte 0xe\n\t.uleb128 %d\n"                /* def_cfa_offset */

7386. #if LJ_64

7387. "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */

7388. "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */

7389. "\t.byte 0x8f\n\t.uleb128 0x4\n"        /* offset r15 */

7390. "\t.byte 0x8e\n\t.uleb128 0x5\n"        /* offset r14 */

7391. #else

7392. "\t.byte 0x85\n\t.uleb128 0x2\n"        /* offset ebp */

7393. "\t.byte 0x87\n\t.uleb128 0x3\n"        /* offset edi */

7394. "\t.byte 0x86\n\t.uleb128 0x4\n"        /* offset esi */

7395. "\t.byte 0x83\n\t.uleb128 0x5\n"        /* offset ebx */

7396. #endif

7397. "\t.align " SZPTR "\n"

7398. ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);

7399. #if LJ_HASFFI

7400. fprintf(ctx->fp,

7401. ".Lframe2:\n"

7402. "\t.long .LECIE2-.LSCIE2\n"

7403. ".LSCIE2:\n"

7404. "\t.long 0\n"

7405. "\t.byte 0x1\n"

7406. "\t.string \"zR\"\n"

7407. "\t.uleb128 0x1\n"

7408. "\t.sleb128 -" SZPTR "\n"

7409. "\t.byte " REG_RA "\n"

7410. "\t.uleb128 1\n"                        /* augmentation length */

7411. "\t.byte 0x1b\n"                        /* pcrel|sdata4 */

7412. "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"

7413. "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"

7414. "\t.align " SZPTR "\n"

7415. ".LECIE2:\n\n");

7416. fprintf(ctx->fp,

7417. ".LSFDE3:\n"

7418. "\t.long .LEFDE3-.LASFDE3\n"

7419. ".LASFDE3:\n"

7420. "\t.long .LASFDE3-.Lframe2\n"

7421. "\t.long lj_vm_ffi_call-.\n"

7422. "\t.long %d\n"

7423. "\t.uleb128 0\n"                        /* augmentation length */

7424. #if LJ_64

7425. "\t.byte 0xe\n\t.uleb128 16\n"                /* def_cfa_offset */

7426. "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */

7427. "\t.byte 0xd\n\t.uleb128 0x6\n"                /* def_cfa_register rbp */

7428. "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */

7429. #else

7430. "\t.byte 0xe\n\t.uleb128 8\n"                /* def_cfa_offset */

7431. "\t.byte 0x85\n\t.uleb128 0x2\n"        /* offset ebp */

7432. "\t.byte 0xd\n\t.uleb128 0x5\n"                /* def_cfa_register ebp */

7433. "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset ebx */

7434. #endif

7435. "\t.align " SZPTR "\n"

7436. ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);

7437. #endif

7438. break;

7439. /* Mental note: never let Apple design an assembler.

7440. ** Or a linker. Or a plastic case. But I digress.

7441. */

7442. case BUILD_machasm: {

7443. #if LJ_HASFFI

7444. int fcsize = 0;

7445. #endif

7446. int i;

7447. fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");

7448. fprintf(ctx->fp,

7449. "EH_frame1:\n"

7450. "\t.set L$set$x,LECIEX-LSCIEX\n"

7451. "\t.long L$set$x\n"

7452. "LSCIEX:\n"

7453. "\t.long 0\n"

7454. "\t.byte 0x1\n"

7455. "\t.ascii \"zPR\\0\"\n"

7456. "\t.byte 0x1\n"

7457. "\t.byte 128-" SZPTR "\n"

7458. "\t.byte " REG_RA "\n"

7459. "\t.byte 6\n"                                /* augmentation length */

7460. "\t.byte 0x9b\n"                        /* indirect|pcrel|sdata4 */

7461. #if LJ_64

7462. "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"

7463. "\t.byte 0x1b\n"                        /* pcrel|sdata4 */

7464. "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"

7465. #else

7466. "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n"

7467. "\t.byte 0x1b\n"                        /* pcrel|sdata4 */

7468. "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n"  /* esp=5 on 32 bit MACH-O. */

7469. #endif

7470. "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"

7471. "\t.align " BSZPTR "\n"

7472. "LECIEX:\n\n");

7473. for (i = 0; i < ctx->nsym; i++) {

7474. const char *name = ctx->sym[i].name;

7475. int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;

7476. if (size == 0) continue;

7477. #if LJ_HASFFI

7478. if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }

7479. #endif

7480. fprintf(ctx->fp,

7481. "%s.eh:\n"

7482. "LSFDE%d:\n"

7483. "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"

7484. "\t.long L$set$%d\n"

7485. "LASFDE%d:\n"

7486. "\t.long LASFDE%d-EH_frame1\n"

7487. "\t.long %s-.\n"

7488. "\t.long %d\n"

7489. "\t.byte 0\n"                                /* augmentation length */

7490. "\t.byte 0xe\n\t.byte %d\n"                /* def_cfa_offset */

7491. #if LJ_64

7492. "\t.byte 0x86\n\t.byte 0x2\n"                /* offset rbp */

7493. "\t.byte 0x83\n\t.byte 0x3\n"                /* offset rbx */

7494. "\t.byte 0x8f\n\t.byte 0x4\n"                /* offset r15 */

7495. "\t.byte 0x8e\n\t.byte 0x5\n"                /* offset r14 */

7496. #else

7497. "\t.byte 0x84\n\t.byte 0x2\n"                /* offset ebp (4 for MACH-O)*/

7498. "\t.byte 0x87\n\t.byte 0x3\n"                /* offset edi */

7499. "\t.byte 0x86\n\t.byte 0x4\n"                /* offset esi */

7500. "\t.byte 0x83\n\t.byte 0x5\n"                /* offset ebx */

7501. #endif

7502. "\t.align " BSZPTR "\n"

7503. "LEFDE%d:\n\n",

7504. name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);

7505. }

7506. #if LJ_HASFFI

7507. if (fcsize) {

7508. fprintf(ctx->fp,

7509. "EH_frame2:\n"

7510. "\t.set L$set$y,LECIEY-LSCIEY\n"

7511. "\t.long L$set$y\n"

7512. "LSCIEY:\n"

7513. "\t.long 0\n"

7514. "\t.byte 0x1\n"

7515. "\t.ascii \"zR\\0\"\n"

7516. "\t.byte 0x1\n"

7517. "\t.byte 128-" SZPTR "\n"

7518. "\t.byte " REG_RA "\n"

7519. "\t.byte 1\n"                                /* augmentation length */

7520. #if LJ_64

7521. "\t.byte 0x1b\n"                        /* pcrel|sdata4 */

7522. "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"

7523. #else

7524. "\t.byte 0x1b\n"                        /* pcrel|sdata4 */

7525. "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n"  /* esp=5 on 32 bit MACH. */

7526. #endif

7527. "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"

7528. "\t.align " BSZPTR "\n"

7529. "LECIEY:\n\n");

7530. fprintf(ctx->fp,

7531. "_lj_vm_ffi_call.eh:\n"

7532. "LSFDEY:\n"

7533. "\t.set L$set$yy,LEFDEY-LASFDEY\n"

7534. "\t.long L$set$yy\n"

7535. "LASFDEY:\n"

7536. "\t.long LASFDEY-EH_frame2\n"

7537. "\t.long _lj_vm_ffi_call-.\n"

7538. "\t.long %d\n"

7539. "\t.byte 0\n"                                /* augmentation length */

7540. #if LJ_64

7541. "\t.byte 0xe\n\t.byte 16\n"                /* def_cfa_offset */

7542. "\t.byte 0x86\n\t.byte 0x2\n"                /* offset rbp */

7543. "\t.byte 0xd\n\t.byte 0x6\n"                /* def_cfa_register rbp */

7544. "\t.byte 0x83\n\t.byte 0x3\n"                /* offset rbx */

7545. #else

7546. "\t.byte 0xe\n\t.byte 8\n"                /* def_cfa_offset */

7547. "\t.byte 0x84\n\t.byte 0x2\n"                /* offset ebp (4 for MACH-O)*/

7548. "\t.byte 0xd\n\t.byte 0x4\n"                /* def_cfa_register ebp */

7549. "\t.byte 0x83\n\t.byte 0x3\n"                /* offset ebx */

7550. #endif

7551. "\t.align " BSZPTR "\n"

7552. "LEFDEY:\n\n", fcsize);

7553. }

7554. #endif

7555. #if !LJ_64

7556. fprintf(ctx->fp,

7557. "\t.non_lazy_symbol_pointer\n"

7558. "L_lj_err_unwind_dwarf$non_lazy_ptr:\n"

7559. ".indirect_symbol _lj_err_unwind_dwarf\n"

7560. ".long 0\n\n");

7561. fprintf(ctx->fp, "\t.section __IMPORT,__jump_table,symbol_stubs,pure_instructions+self_modifying_code,5\n");

7562. {

7563. const char *const *xn;

7564. for (xn = ctx->extnames; *xn; xn++)

7565. if (strncmp(*xn, LABEL_PREFIX, sizeof(LABEL_PREFIX)-1))

7566. fprintf(ctx->fp, "L_%s$stub:\n\t.indirect_symbol _%s\n\t.ascii \"\\364\\364\\364\\364\\364\"\n", *xn, *xn);

7567. }

7568. #endif

7569. fprintf(ctx->fp, ".subsections_via_symbols\n");

7570. }

7571. break;

7572. default:  /* Difficult for other modes. */

7573. break;

7574. }

7575. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_state.c - luajit-2.0-src

Functions defined

- close_state

- cpfinalize

- cpluaopen

- lj_state_free

- lj_state_growstack

- lj_state_growstack1

- lj_state_new

- lj_state_newstate

- lj_state_relimitstack

- lj_state_shrinkstack

- lua_close

- resizestack

- stack_init

Macros defined

- LJ_STACK_MAX

- LJ_STACK_MAXEX

- LJ_STACK_MIN

- LJ_STACK_START

- LUA_CORE

- lj_state_c

Source code

1. /*

2. ** State and stack handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lj_state_c

10. ‌#define LUA_CORE

12. #include "lj_obj.h"

13. #include "lj_gc.h"

14. #include "lj_err.h"

15. #include "lj_buf.h"

16. #include "lj_str.h"

17. #include "lj_tab.h"

18. #include "lj_func.h"

19. #include "lj_meta.h"

20. #include "lj_state.h"

21. #include "lj_frame.h"

22. #if LJ_HASFFI

23. #include "lj_ctype.h"

24. #endif

25. #include "lj_trace.h"

26. #include "lj_dispatch.h"

27. #include "lj_vm.h"

28. #include "lj_lex.h"

29. #include "lj_alloc.h"

30. #include "luajit.h"

32. /* -- Stack handling ------------------------------------------------------ */

34. /* Stack sizes. */

35. ‌#define LJ_STACK_MIN        LUA_MINSTACK        /* Min. stack size. */

36. ‌#define LJ_STACK_MAX        LUAI_MAXSTACK        /* Max. stack size. */

37. ‌#define LJ_STACK_START        (2*LJ_STACK_MIN)        /* Starting stack size. */

38. ‌#define LJ_STACK_MAXEX        (LJ_STACK_MAX + 1 + LJ_STACK_EXTRA)

40. /* Explanation of LJ_STACK_EXTRA:

41. **

42. ** Calls to metamethods store their arguments beyond the current top

43. ** without checking for the stack limit. This avoids stack resizes which

44. ** would invalidate passed TValue pointers. The stack check is performed

45. ** later by the function header. This can safely resize the stack or raise

46. ** an error. Thus we need some extra slots beyond the current stack limit.

47. **

48. ** Most metamethods need 4 slots above top (cont, mobj, arg1, arg2) plus

49. ** one extra slot if mobj is not a function. Only lj_meta_tset needs 5

50. ** slots above top, but then mobj is always a function. So we can get by

51. ** with 5 extra slots.

52. ** LJ_FR2: We need 2 more slots for the frame PC and the continuation PC.

53. */

55. /* Resize stack slots and adjust pointers in state. */

56. ‌static void resizestack(lua_State *L, MSize n)

57. {

58. TValue *st, *oldst = tvref(L->stack);

59. ptrdiff_t delta;

60. MSize oldsize = L->stacksize;

61. MSize realsize = n + 1 + LJ_STACK_EXTRA;

62. GCobj *up;

63. lua_assert((MSize)(tvref(L->maxstack)-oldst)==L->stacksize-LJ_STACK_EXTRA-1);

64. st = (TValue *)lj_mem_realloc(L, tvref(L->stack),

65. (MSize)(oldsize*sizeof(TValue)),

66. (MSize)(realsize*sizeof(TValue)));

67. setmref(L->stack, st);

68. delta = (char *)st - (char *)oldst;

69. setmref(L->maxstack, st + n);

70. while (oldsize < realsize)  /* Clear new slots. */

71. setnilV(st + oldsize++);

72. L->stacksize = realsize;

73. if ((size_t)(mref(G(L)->jit_base, char) - (char *)oldst) < oldsize)

74. setmref(G(L)->jit_base, mref(G(L)->jit_base, char) + delta);

75. L->base = (TValue *)((char *)L->base + delta);

76. L->top = (TValue *)((char *)L->top + delta);

77. for (up = gcref(L->openupval); up != NULL; up = gcnext(up))

78. setmref(gco2uv(up)->v, (TValue *)((char *)uvval(gco2uv(up)) + delta));

79. }

81. /* Relimit stack after error, in case the limit was overdrawn. */

82. ‌void lj_state_relimitstack(lua_State *L)

83. {

84. if (L->stacksize > LJ_STACK_MAXEX && L->top-tvref(L->stack) < LJ_STACK_MAX-1)

85. resizestack(L, LJ_STACK_MAX);

86. }

88. /* Try to shrink the stack (called from GC). */

89. ‌void lj_state_shrinkstack(lua_State *L, MSize used)

90. {

91. if (L->stacksize > LJ_STACK_MAXEX)

92. return;  /* Avoid stack shrinking while handling stack overflow. */

93. if (4*used < L->stacksize &&

94. 2*(LJ_STACK_START+LJ_STACK_EXTRA) < L->stacksize &&

95. /* Don't shrink stack of live trace. */

96. (tvref(G(L)->jit_base) == NULL || obj2gco(L) != gcref(G(L)->cur_L)))

97. resizestack(L, L->stacksize >> 1);

98. }

100. /* Try to grow stack. */

101. ‌void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need)

102. {

103. MSize n;

104. if (L->stacksize > LJ_STACK_MAXEX)  /* Overflow while handling overflow? */

105. lj_err_throw(L, LUA_ERRERR);

106. n = L->stacksize + need;

107. if (n > LJ_STACK_MAX) {

108. n += 2*LUA_MINSTACK;

109. } else if (n < 2*L->stacksize) {

110. n = 2*L->stacksize;

111. if (n >= LJ_STACK_MAX)

112. n = LJ_STACK_MAX;

113. }

114. resizestack(L, n);

115. if (L->stacksize > LJ_STACK_MAXEX)

116. lj_err_msg(L, LJ_ERR_STKOV);

117. }

119. ‌void LJ_FASTCALL lj_state_growstack1(lua_State *L)

120. {

121. lj_state_growstack(L, 1);

122. }

124. /* Allocate basic stack for new state. */

125. ‌static void stack_init(lua_State *L1, lua_State *L)

126. {

127. TValue *stend, *st = lj_mem_newvec(L, LJ_STACK_START+LJ_STACK_EXTRA, TValue);

128. setmref(L1->stack, st);

129. L1->stacksize = LJ_STACK_START + LJ_STACK_EXTRA;

130. stend = st + L1->stacksize;

131. setmref(L1->maxstack, stend - LJ_STACK_EXTRA - 1);

132. setthreadV(L1, st++, L1);  /* Needed for curr_funcisL() on empty stack. */

133. if (LJ_FR2) setnilV(st++);

134. L1->base = L1->top = st;

135. while (st < stend)  /* Clear new slots. */

136. setnilV(st++);

137. }

139. /* -- State handling ------------------------------------------------------ */

141. /* Open parts that may cause memory-allocation errors. */

142. ‌static TValue *cpluaopen(lua_State *L, lua_CFunction dummy, void *ud)

143. {

144. global_State *g = G(L);

145. UNUSED(dummy);

146. UNUSED(ud);

147. stack_init(L, L);

148. /* NOBARRIER: State initialization, all objects are white. */

149. setgcref(L->env, obj2gco(lj_tab_new(L, 0, LJ_MIN_GLOBAL)));

150. settabV(L, registry(L), lj_tab_new(L, 0, LJ_MIN_REGISTRY));

151. lj_str_resize(L, LJ_MIN_STRTAB-1);

152. lj_meta_init(L);

153. lj_lex_init(L);

154. fixstring(lj_err_str(L, LJ_ERR_ERRMEM));  /* Preallocate memory error msg. */

155. g->gc.threshold = 4*g->gc.total;

156. lj_trace_initstate(g);

157. return NULL;

158. }

160. ‌static void close_state(lua_State *L)

161. {

162. global_State *g = G(L);

163. lj_func_closeuv(L, tvref(L->stack));

164. lj_gc_freeall(g);

165. lua_assert(gcref(g->gc.root) == obj2gco(L));

166. lua_assert(g->strnum == 0);

167. lj_trace_freestate(g);

168. #if LJ_HASFFI

169. lj_ctype_freestate(g);

170. #endif

171. lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef);

172. lj_buf_free(g, &g->tmpbuf);

173. lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);

174. lua_assert(g->gc.total == sizeof(GG_State));

175. #ifndef LUAJIT_USE_SYSMALLOC

176. if (g->allocf == lj_alloc_f)

177. lj_alloc_destroy(g->allocd);

178. else

179. #endif

180. g->allocf(g->allocd, G2GG(g), sizeof(GG_State), 0);

181. }

183. #if LJ_64 && !(defined(LUAJIT_USE_VALGRIND) && defined(LUAJIT_USE_SYSMALLOC))

184. ‌lua_State *lj_state_newstate(lua_Alloc f, void *ud)

185. #else

186. LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)

187. #endif

188. {

189. GG_State *GG = (GG_State *)f(ud, NULL, 0, sizeof(GG_State));

190. lua_State *L = &GG->L;

191. global_State *g = &GG->g;

192. if (GG == NULL || !checkptrGC(GG)) return NULL;

193. memset(GG, 0, sizeof(GG_State));

194. L->gct = ~LJ_TTHREAD;

195. L->marked = LJ_GC_WHITE0 | LJ_GC_FIXED | LJ_GC_SFIXED;  /* Prevent free. */

196. L->dummy_ffid = FF_C;

197. setmref(L->glref, g);

198. g->gc.currentwhite = LJ_GC_WHITE0 | LJ_GC_FIXED;

199. g->strempty.marked = LJ_GC_WHITE0;

200. g->strempty.gct = ~LJ_TSTR;

201. g->allocf = f;

202. g->allocd = ud;

203. setgcref(g->mainthref, obj2gco(L));

204. setgcref(g->uvhead.prev, obj2gco(&g->uvhead));

205. setgcref(g->uvhead.next, obj2gco(&g->uvhead));

206. g->strmask = ~(MSize)0;

207. setnilV(registry(L));

208. setnilV(&g->nilnode.val);

209. setnilV(&g->nilnode.key);

210. #if !LJ_GC64

211. setmref(g->nilnode.freetop, &g->nilnode);

212. #endif

213. lj_buf_init(NULL, &g->tmpbuf);

214. g->gc.state = GCSpause;

215. setgcref(g->gc.root, obj2gco(L));

216. setmref(g->gc.sweep, &g->gc.root);

217. g->gc.total = sizeof(GG_State);

218. g->gc.pause = LUAI_GCPAUSE;

219. g->gc.stepmul = LUAI_GCMUL;

220. lj_dispatch_init((GG_State *)L);

221. L->status = LUA_ERRERR+1;  /* Avoid touching the stack upon memory error. */

222. if (lj_vm_cpcall(L, NULL, NULL, cpluaopen) != 0) {

223. /* Memory allocation error: free partial state. */

224. close_state(L);

225. return NULL;

226. }

227. L->status = 0;

228. return L;

229. }

231. ‌static TValue *cpfinalize(lua_State *L, lua_CFunction dummy, void *ud)

232. {

233. UNUSED(dummy);

234. UNUSED(ud);

235. lj_gc_finalize_cdata(L);

236. lj_gc_finalize_udata(L);

237. /* Frame pop omitted. */

238. return NULL;

239. }

241. ‌LUA_API void lua_close(lua_State *L)

242. {

243. global_State *g = G(L);

244. int i;

245. L = mainthread(g);  /* Only the main thread can be closed. */

246. #if LJ_HASPROFILE

247. luaJIT_profile_stop(L);

248. #endif

249. setgcrefnull(g->cur_L);

250. lj_func_closeuv(L, tvref(L->stack));

251. lj_gc_separateudata(g, 1);  /* Separate udata which have GC metamethods. */

252. #if LJ_HASJIT

253. G2J(g)->flags &= ~JIT_F_ON;

254. G2J(g)->state = LJ_TRACE_IDLE;

255. lj_dispatch_update(g);

256. #endif

257. for (i = 0;;) {

258. hook_enter(g);

259. L->status = 0;

260. L->base = L->top = tvref(L->stack) + 1 + LJ_FR2;

261. L->cframe = NULL;

262. if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == 0) {

263. if (++i >= 10) break;

264. lj_gc_separateudata(g, 1);  /* Separate udata again. */

265. if (gcref(g->gc.mmudata) == NULL)  /* Until nothing is left to do. */

266. break;

267. }

268. }

269. close_state(L);

270. }

272. ‌lua_State *lj_state_new(lua_State *L)

273. {

274. lua_State *L1 = lj_mem_newobj(L, lua_State);

275. L1->gct = ~LJ_TTHREAD;

276. L1->dummy_ffid = FF_C;

277. L1->status = 0;

278. L1->stacksize = 0;

279. setmref(L1->stack, NULL);

280. L1->cframe = NULL;

281. /* NOBARRIER: The lua_State is new (marked white). */

282. setgcrefnull(L1->openupval);

283. setmrefr(L1->glref, L->glref);

284. setgcrefr(L1->env, L->env);

285. stack_init(L1, L);  /* init stack */

286. lua_assert(iswhite(obj2gco(L1)));

287. return L1;

288. }

290. ‌void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L)

291. {

292. lua_assert(L != mainthread(g));

293. if (obj2gco(L) == gcref(g->cur_L))

294. setgcrefnull(g->cur_L);

295. lj_func_closeuv(L, tvref(L->stack));

296. lua_assert(gcref(L->openupval) == NULL);

297. lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);

298. lj_mem_freet(g, L);

299. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ctype.c - luajit-2.0-src

Global variables defined

- lj_ctype_typeinfo

- lj_ctype_typenames

Data types defined

- CTRepr

- CTRepr

Functions defined

- ctype_addtype

- ctype_appc

- ctype_appnum

- ctype_prepc

- ctype_prepnum

- ctype_prepqual

- ctype_prepstr

- ctype_preptype

- ctype_repr

- lj_ctype_addname

- lj_ctype_freestate

- lj_ctype_getfieldq

- lj_ctype_getname

- lj_ctype_info

- lj_ctype_init

- lj_ctype_intern

- lj_ctype_meta

- lj_ctype_new

- lj_ctype_rawref

- lj_ctype_repr

- lj_ctype_repr_complex

- lj_ctype_repr_int64

- lj_ctype_size

- lj_ctype_vlsize

Macros defined

- CTKWDEF

- CTKWINFODEF

- CTKWINFODEF

- CTKWNAMEDEF

- CTKWNAMEDEF

- CTREPR_MAX

- CTTDDEF

- CTTDINFODEF

- CTTDINFODEF

- CTTDNAMEDEF

- CTTDNAMEDEF

- CTTYINFODEF

- CTTYINFODEF

- CTTYPEINFO_NUM

- CTTYPETAB_MIN

- CTTYPETAB_MIN

- ct_hashname

- ct_hashtype

- ctype_preplit

Source code

1. /*

2. ** C type management.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "lj_obj.h"

8. #if LJ_HASFFI

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_str.h"

13. #include "lj_tab.h"

14. #include "lj_strfmt.h"

15. #include "lj_ctype.h"

16. #include "lj_ccallback.h"

18. /* -- C type definitions -------------------------------------------------- */

20. /* Predefined typedefs. */

21. ‌#define CTTDDEF(_) \

22. /* Vararg handling. */ \

23. _("va_list",                        P_VOID) \

24. _("__builtin_va_list",        P_VOID) \

25. _("__gnuc_va_list",                P_VOID) \

26. /* From stddef.h. */ \

27. _("ptrdiff_t",                INT_PSZ) \

28. _("size_t",                        UINT_PSZ) \

29. _("wchar_t",                        WCHAR) \

30. /* Subset of stdint.h. */ \

31. _("int8_t",                        INT8) \

32. _("int16_t",                        INT16) \

33. _("int32_t",                        INT32) \

34. _("int64_t",                        INT64) \

35. _("uint8_t",                        UINT8) \

36. _("uint16_t",                        UINT16) \

37. _("uint32_t",                        UINT32) \

38. _("uint64_t",                        UINT64) \

39. _("intptr_t",                        INT_PSZ) \

40. _("uintptr_t",                UINT_PSZ) \

41. /* End of typedef list. */

43. /* Keywords (only the ones we actually care for). */

44. ‌#define CTKWDEF(_) \

45. /* Type specifiers. */ \

46. _("void",                -1,        CTOK_VOID) \

47. _("_Bool",                0,        CTOK_BOOL) \

48. _("bool",                1,        CTOK_BOOL) \

49. _("char",                1,        CTOK_CHAR) \

50. _("int",                4,        CTOK_INT) \

51. _("__int8",                1,        CTOK_INT) \

52. _("__int16",                2,        CTOK_INT) \

53. _("__int32",                4,        CTOK_INT) \

54. _("__int64",                8,        CTOK_INT) \

55. _("float",                4,        CTOK_FP) \

56. _("double",                8,        CTOK_FP) \

57. _("long",                0,        CTOK_LONG) \

58. _("short",                0,        CTOK_SHORT) \

59. _("_Complex",                0,        CTOK_COMPLEX) \

60. _("complex",                0,        CTOK_COMPLEX) \

61. _("__complex",        0,        CTOK_COMPLEX) \

62. _("__complex__",        0,        CTOK_COMPLEX) \

63. _("signed",                0,        CTOK_SIGNED) \

64. _("__signed",                0,        CTOK_SIGNED) \

65. _("__signed__",        0,        CTOK_SIGNED) \

66. _("unsigned",                0,        CTOK_UNSIGNED) \

67. /* Type qualifiers. */ \

68. _("const",                0,        CTOK_CONST) \

69. _("__const",                0,        CTOK_CONST) \

70. _("__const__",        0,        CTOK_CONST) \

71. _("volatile",                0,        CTOK_VOLATILE) \

72. _("__volatile",        0,        CTOK_VOLATILE) \

73. _("__volatile__",        0,        CTOK_VOLATILE) \

74. _("restrict",                0,        CTOK_RESTRICT) \

75. _("__restrict",        0,        CTOK_RESTRICT) \

76. _("__restrict__",        0,        CTOK_RESTRICT) \

77. _("inline",                0,        CTOK_INLINE) \

78. _("__inline",                0,        CTOK_INLINE) \

79. _("__inline__",        0,        CTOK_INLINE) \

80. /* Storage class specifiers. */ \

81. _("typedef",                0,        CTOK_TYPEDEF) \

82. _("extern",                0,        CTOK_EXTERN) \

83. _("static",                0,        CTOK_STATIC) \

84. _("auto",                0,        CTOK_AUTO) \

85. _("register",                0,        CTOK_REGISTER) \

86. /* GCC Attributes. */ \

87. _("__extension__",        0,        CTOK_EXTENSION) \

88. _("__attribute",        0,        CTOK_ATTRIBUTE) \

89. _("__attribute__",        0,        CTOK_ATTRIBUTE) \

90. _("asm",                0,        CTOK_ASM) \

91. _("__asm",                0,        CTOK_ASM) \

92. _("__asm__",                0,        CTOK_ASM) \

93. /* MSVC Attributes. */ \

94. _("__declspec",        0,        CTOK_DECLSPEC) \

95. _("__cdecl",                CTCC_CDECL,        CTOK_CCDECL) \

96. _("__thiscall",        CTCC_THISCALL,        CTOK_CCDECL) \

97. _("__fastcall",        CTCC_FASTCALL,        CTOK_CCDECL) \

98. _("__stdcall",        CTCC_STDCALL,        CTOK_CCDECL) \

99. _("__ptr32",                4,        CTOK_PTRSZ) \

100. _("__ptr64",                8,        CTOK_PTRSZ) \

101. /* Other type specifiers. */ \

102. _("struct",                0,        CTOK_STRUCT) \

103. _("union",                0,        CTOK_UNION) \

104. _("enum",                0,        CTOK_ENUM) \

105. /* Operators. */ \

106. _("sizeof",                0,        CTOK_SIZEOF) \

107. _("__alignof",        0,        CTOK_ALIGNOF) \

108. _("__alignof__",        0,        CTOK_ALIGNOF) \

109. /* End of keyword list. */

111. /* Type info for predefined types. Size merged in. */

112. ‌static CTInfo lj_ctype_typeinfo[] = {

113. ‌#define CTTYINFODEF(id, sz, ct, info)        CTINFO((ct),(((sz)&0x3fu)<<10)+(info)),

114. ‌#define CTTDINFODEF(name, id)                CTINFO(CT_TYPEDEF, CTID_##id),

115. ‌#define CTKWINFODEF(name, sz, kw)        CTINFO(CT_KW,(((sz)&0x3fu)<<10)+(kw)),

116. CTTYDEF(CTTYINFODEF)

117. CTTDDEF(CTTDINFODEF)

118. CTKWDEF(CTKWINFODEF)

119. ‌#undef CTTYINFODEF

120. ‌#undef CTTDINFODEF

121. ‌#undef CTKWINFODEF

122. 0

123. };

125. /* Predefined type names collected in a single string. */

126. ‌static const char * const lj_ctype_typenames =

127. ‌#define CTTDNAMEDEF(name, id)                name "\0"

128. ‌#define CTKWNAMEDEF(name, sz, cds)        name "\0"

129. CTTDDEF(CTTDNAMEDEF)

130. CTKWDEF(CTKWNAMEDEF)

131. ‌#undef CTTDNAMEDEF

132. ‌#undef CTKWNAMEDEF

133. ;

135. ‌#define CTTYPEINFO_NUM                (sizeof(lj_ctype_typeinfo)/sizeof(CTInfo)-1)

136. #ifdef LUAJIT_CTYPE_CHECK_ANCHOR

137. ‌#define CTTYPETAB_MIN                CTTYPEINFO_NUM

138. #else

139. ‌#define CTTYPETAB_MIN                128

140. #endif

142. /* -- C type interning ---------------------------------------------------- */

144. ‌#define ct_hashtype(info, size)        (hashrot(info, size) & CTHASH_MASK)

145. ‌#define ct_hashname(name) \

146. (hashrot(u32ptr(name), u32ptr(name) + HASH_BIAS) & CTHASH_MASK)

148. /* Create new type element. */

149. ‌CTypeID lj_ctype_new(CTState *cts, CType **ctp)

150. {

151. CTypeID id = cts->top;

152. CType *ct;

153. lua_assert(cts->L);

154. if (LJ_UNLIKELY(id >= cts->sizetab)) {

155. if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV);

156. #ifdef LUAJIT_CTYPE_CHECK_ANCHOR

157. ct = lj_mem_newvec(cts->L, id+1, CType);

158. memcpy(ct, cts->tab, id*sizeof(CType));

159. memset(cts->tab, 0, id*sizeof(CType));

160. lj_mem_freevec(cts->g, cts->tab, cts->sizetab, CType);

161. cts->tab = ct;

162. cts->sizetab = id+1;

163. #else

164. lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType);

165. #endif

166. }

167. cts->top = id+1;

168. *ctp = ct = &cts->tab[id];

169. ct->info = 0;

170. ct->size = 0;

171. ct->sib = 0;

172. ct->next = 0;

173. setgcrefnull(ct->name);

174. return id;

175. }

177. /* Intern a type element. */

178. ‌CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size)

179. {

180. uint32_t h = ct_hashtype(info, size);

181. CTypeID id = cts->hash[h];

182. lua_assert(cts->L);

183. while (id) {

184. CType *ct = ctype_get(cts, id);

185. if (ct->info == info && ct->size == size)

186. return id;

187. id = ct->next;

188. }

189. id = cts->top;

190. if (LJ_UNLIKELY(id >= cts->sizetab)) {

191. if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV);

192. lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType);

193. }

194. cts->top = id+1;

195. cts->tab[id].info = info;

196. cts->tab[id].size = size;

197. cts->tab[id].sib = 0;

198. cts->tab[id].next = cts->hash[h];

199. setgcrefnull(cts->tab[id].name);

200. cts->hash[h] = (CTypeID1)id;

201. return id;

202. }

204. /* Add type element to hash table. */

205. ‌static void ctype_addtype(CTState *cts, CType *ct, CTypeID id)

206. {

207. uint32_t h = ct_hashtype(ct->info, ct->size);

208. ct->next = cts->hash[h];

209. cts->hash[h] = (CTypeID1)id;

210. }

212. /* Add named element to hash table. */

213. ‌void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id)

214. {

215. uint32_t h = ct_hashname(gcref(ct->name));

216. ct->next = cts->hash[h];

217. cts->hash[h] = (CTypeID1)id;

218. }

220. /* Get a C type by name, matching the type mask. */

221. ‌CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, uint32_t tmask)

222. {

223. CTypeID id = cts->hash[ct_hashname(name)];

224. while (id) {

225. CType *ct = ctype_get(cts, id);

226. if (gcref(ct->name) == obj2gco(name) &&

227. ((tmask >> ctype_type(ct->info)) & 1)) {

228. *ctp = ct;

229. return id;

230. }

231. id = ct->next;

232. }

233. *ctp = &cts->tab[0];  /* Simplify caller logic. ctype_get() would assert. */

234. return 0;

235. }

237. /* Get a struct/union/enum/function field by name. */

238. ‌CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, CTSize *ofs,

239. CTInfo *qual)

240. {

241. while (ct->sib) {

242. ct = ctype_get(cts, ct->sib);

243. if (gcref(ct->name) == obj2gco(name)) {

244. *ofs = ct->size;

245. return ct;

246. }

247. if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {

248. CType *fct, *cct = ctype_child(cts, ct);

249. CTInfo q = 0;

250. while (ctype_isattrib(cct->info)) {

251. if (ctype_attrib(cct->info) == CTA_QUAL) q |= cct->size;

252. cct = ctype_child(cts, cct);

253. }

254. fct = lj_ctype_getfieldq(cts, cct, name, ofs, qual);

255. if (fct) {

256. if (qual) *qual |= q;

257. *ofs += ct->size;

258. return fct;

259. }

260. }

261. }

262. return NULL;  /* Not found. */

263. }

265. /* -- C type information -------------------------------------------------- */

267. /* Follow references and get raw type for a C type ID. */

268. ‌CType *lj_ctype_rawref(CTState *cts, CTypeID id)

269. {

270. CType *ct = ctype_get(cts, id);

271. while (ctype_isattrib(ct->info) || ctype_isref(ct->info))

272. ct = ctype_child(cts, ct);

273. return ct;

274. }

276. /* Get size for a C type ID. Does NOT support VLA/VLS. */

277. ‌CTSize lj_ctype_size(CTState *cts, CTypeID id)

278. {

279. CType *ct = ctype_raw(cts, id);

280. return ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID;

281. }

283. /* Get size for a variable-length C type. Does NOT support other C types. */

284. ‌CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem)

285. {

286. uint64_t xsz = 0;

287. if (ctype_isstruct(ct->info)) {

288. CTypeID arrid = 0, fid = ct->sib;

289. xsz = ct->size;  /* Add the struct size. */

290. while (fid) {

291. CType *ctf = ctype_get(cts, fid);

292. if (ctype_type(ctf->info) == CT_FIELD)

293. arrid = ctype_cid(ctf->info);  /* Remember last field of VLS. */

294. fid = ctf->sib;

295. }

296. ct = ctype_raw(cts, arrid);

297. }

298. lua_assert(ctype_isvlarray(ct->info));  /* Must be a VLA. */

299. ct = ctype_rawchild(cts, ct);  /* Get array element. */

300. lua_assert(ctype_hassize(ct->info));

301. /* Calculate actual size of VLA and check for overflow. */

302. xsz += (uint64_t)ct->size * nelem;

303. return xsz < 0x80000000u ? (CTSize)xsz : CTSIZE_INVALID;

304. }

306. /* Get type, qualifiers, size and alignment for a C type ID. */

307. ‌CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp)

308. {

309. CTInfo qual = 0;

310. CType *ct = ctype_get(cts, id);

311. for (;;) {

312. CTInfo info = ct->info;

313. if (ctype_isenum(info)) {

314. /* Follow child. Need to look at its attributes, too. */

315. } else if (ctype_isattrib(info)) {

316. if (ctype_isxattrib(info, CTA_QUAL))

317. qual |= ct->size;

318. else if (ctype_isxattrib(info, CTA_ALIGN) && !(qual & CTFP_ALIGNED))

319. qual |= CTFP_ALIGNED + CTALIGN(ct->size);

320. } else {

321. if (!(qual & CTFP_ALIGNED)) qual |= (info & CTF_ALIGN);

322. qual |= (info & ~(CTF_ALIGN|CTMASK_CID));

323. lua_assert(ctype_hassize(info) || ctype_isfunc(info));

324. *szp = ctype_isfunc(info) ? CTSIZE_INVALID : ct->size;

325. break;

326. }

327. ct = ctype_get(cts, ctype_cid(info));

328. }

329. return qual;

330. }

332. /* Get ctype metamethod. */

333. ‌cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm)

334. {

335. CType *ct = ctype_get(cts, id);

336. cTValue *tv;

337. while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) {

338. id = ctype_cid(ct->info);

339. ct = ctype_get(cts, id);

340. }

341. if (ctype_isptr(ct->info) &&

342. ctype_isfunc(ctype_get(cts, ctype_cid(ct->info))->info))

343. tv = lj_tab_getstr(cts->miscmap, &cts->g->strempty);

344. else

345. tv = lj_tab_getinth(cts->miscmap, -(int32_t)id);

346. if (tv && tvistab(tv) &&

347. (tv = lj_tab_getstr(tabV(tv), mmname_str(cts->g, mm))) && !tvisnil(tv))

348. return tv;

349. return NULL;

350. }

352. /* -- C type representation ----------------------------------------------- */

354. /* Fixed max. length of a C type representation. */

355. ‌#define CTREPR_MAX                512

357. ‌typedef struct CTRepr {

358. char *pb, *pe;

359. CTState *cts;

360. lua_State *L;

361. int needsp;

362. int ok;

363. char buf[CTREPR_MAX];

364. ‌} CTRepr;

366. /* Prepend string. */

367. ‌static void ctype_prepstr(CTRepr *ctr, const char *str, MSize len)

368. {

369. char *p = ctr->pb;

370. if (ctr->buf + len+1 > p) { ctr->ok = 0; return; }

371. if (ctr->needsp) *--p = ' ';

372. ctr->needsp = 1;

373. p -= len;

374. while (len-- > 0) p[len] = str[len];

375. ctr->pb = p;

376. }

378. ‌#define ctype_preplit(ctr, str)        ctype_prepstr((ctr), "" str, sizeof(str)-1)

380. /* Prepend char. */

381. ‌static void ctype_prepc(CTRepr *ctr, int c)

382. {

383. if (ctr->buf >= ctr->pb) { ctr->ok = 0; return; }

384. *--ctr->pb = c;

385. }

387. /* Prepend number. */

388. ‌static void ctype_prepnum(CTRepr *ctr, uint32_t n)

389. {

390. char *p = ctr->pb;

391. if (ctr->buf + 10+1 > p) { ctr->ok = 0; return; }

392. do { *--p = (char)('0' + n % 10); } while (n /= 10);

393. ctr->pb = p;

394. ctr->needsp = 0;

395. }

397. /* Append char. */

398. ‌static void ctype_appc(CTRepr *ctr, int c)

399. {

400. if (ctr->pe >= ctr->buf + CTREPR_MAX) { ctr->ok = 0; return; }

401. *ctr->pe++ = c;

402. }

404. /* Append number. */

405. ‌static void ctype_appnum(CTRepr *ctr, uint32_t n)

406. {

407. char buf[10];

408. char *p = buf+sizeof(buf);

409. char *q = ctr->pe;

410. if (q > ctr->buf + CTREPR_MAX - 10) { ctr->ok = 0; return; }

411. do { *--p = (char)('0' + n % 10); } while (n /= 10);

412. do { *q++ = *p++; } while (p < buf+sizeof(buf));

413. ctr->pe = q;

414. }

416. /* Prepend qualifiers. */

417. ‌static void ctype_prepqual(CTRepr *ctr, CTInfo info)

418. {

419. if ((info & CTF_VOLATILE)) ctype_preplit(ctr, "volatile");

420. if ((info & CTF_CONST)) ctype_preplit(ctr, "const");

421. }

423. /* Prepend named type. */

424. ‌static void ctype_preptype(CTRepr *ctr, CType *ct, CTInfo qual, const char *t)

425. {

426. if (gcref(ct->name)) {

427. GCstr *str = gco2str(gcref(ct->name));

428. ctype_prepstr(ctr, strdata(str), str->len);

429. } else {

430. if (ctr->needsp) ctype_prepc(ctr, ' ');

431. ctype_prepnum(ctr, ctype_typeid(ctr->cts, ct));

432. ctr->needsp = 1;

433. }

434. ctype_prepstr(ctr, t, (MSize)strlen(t));

435. ctype_prepqual(ctr, qual);

436. }

438. ‌static void ctype_repr(CTRepr *ctr, CTypeID id)

439. {

440. CType *ct = ctype_get(ctr->cts, id);

441. CTInfo qual = 0;

442. int ptrto = 0;

443. for (;;) {

444. CTInfo info = ct->info;

445. CTSize size = ct->size;

446. switch (ctype_type(info)) {

447. case CT_NUM:

448. if ((info & CTF_BOOL)) {

449. ctype_preplit(ctr, "bool");

450. } else if ((info & CTF_FP)) {

451. if (size == sizeof(double)) ctype_preplit(ctr, "double");

452. else if (size == sizeof(float)) ctype_preplit(ctr, "float");

453. else ctype_preplit(ctr, "long double");

454. } else if (size == 1) {

455. if (!((info ^ CTF_UCHAR) & CTF_UNSIGNED)) ctype_preplit(ctr, "char");

456. else if (CTF_UCHAR) ctype_preplit(ctr, "signed char");

457. else ctype_preplit(ctr, "unsigned char");

458. } else if (size < 8) {

459. if (size == 4) ctype_preplit(ctr, "int");

460. else ctype_preplit(ctr, "short");

461. if ((info & CTF_UNSIGNED)) ctype_preplit(ctr, "unsigned");

462. } else {

463. ctype_preplit(ctr, "_t");

464. ctype_prepnum(ctr, size*8);

465. ctype_preplit(ctr, "int");

466. if ((info & CTF_UNSIGNED)) ctype_prepc(ctr, 'u');

467. }

468. ctype_prepqual(ctr, (qual|info));

469. return;

470. case CT_VOID:

471. ctype_preplit(ctr, "void");

472. ctype_prepqual(ctr, (qual|info));

473. return;

474. case CT_STRUCT:

475. ctype_preptype(ctr, ct, qual, (info & CTF_UNION) ? "union" : "struct");

476. return;

477. case CT_ENUM:

478. if (id == CTID_CTYPEID) {

479. ctype_preplit(ctr, "ctype");

480. return;

481. }

482. ctype_preptype(ctr, ct, qual, "enum");

483. return;

484. case CT_ATTRIB:

485. if (ctype_attrib(info) == CTA_QUAL) qual |= size;

486. break;

487. case CT_PTR:

488. if ((info & CTF_REF)) {

489. ctype_prepc(ctr, '&');

490. } else {

491. ctype_prepqual(ctr, (qual|info));

492. if (LJ_64 && size == 4) ctype_preplit(ctr, "__ptr32");

493. ctype_prepc(ctr, '*');

494. }

495. qual = 0;

496. ptrto = 1;

497. ctr->needsp = 1;

498. break;

499. case CT_ARRAY:

500. if (ctype_isrefarray(info)) {

501. ctr->needsp = 1;

502. if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); }

503. ctype_appc(ctr, '[');

504. if (size != CTSIZE_INVALID) {

505. CTSize csize = ctype_child(ctr->cts, ct)->size;

506. ctype_appnum(ctr, csize ? size/csize : 0);

507. } else if ((info & CTF_VLA)) {

508. ctype_appc(ctr, '?');

509. }

510. ctype_appc(ctr, ']');

511. } else if ((info & CTF_COMPLEX)) {

512. if (size == 2*sizeof(float)) ctype_preplit(ctr, "float");

513. ctype_preplit(ctr, "complex");

514. return;

515. } else {

516. ctype_preplit(ctr, ")))");

517. ctype_prepnum(ctr, size);

518. ctype_preplit(ctr, "__attribute__((vector_size(");

519. }

520. break;

521. case CT_FUNC:

522. ctr->needsp = 1;

523. if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); }

524. ctype_appc(ctr, '(');

525. ctype_appc(ctr, ')');

526. break;

527. default:

528. lua_assert(0);

529. break;

530. }

531. ct = ctype_get(ctr->cts, ctype_cid(info));

532. }

533. }

535. /* Return a printable representation of a C type. */

536. ‌GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name)

537. {

538. global_State *g = G(L);

539. CTRepr ctr;

540. ctr.pb = ctr.pe = &ctr.buf[CTREPR_MAX/2];

541. ctr.cts = ctype_ctsG(g);

542. ctr.L = L;

543. ctr.ok = 1;

544. ctr.needsp = 0;

545. if (name) ctype_prepstr(&ctr, strdata(name), name->len);

546. ctype_repr(&ctr, id);

547. if (LJ_UNLIKELY(!ctr.ok)) return lj_str_newlit(L, "?");

548. return lj_str_new(L, ctr.pb, ctr.pe - ctr.pb);

549. }

551. /* Convert int64_t/uint64_t to string with 'LL' or 'ULL' suffix. */

552. ‌GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned)

553. {

554. char buf[1+20+3];

555. char *p = buf+sizeof(buf);

556. int sign = 0;

557. *--p = 'L'; *--p = 'L';

558. if (isunsigned) {

559. *--p = 'U';

560. } else if ((int64_t)n < 0) {

561. n = (uint64_t)-(int64_t)n;

562. sign = 1;

563. }

564. do { *--p = (char)('0' + n % 10); } while (n /= 10);

565. if (sign) *--p = '-';

566. return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p));

567. }

569. /* Convert complex to string with 'i' or 'I' suffix. */

570. ‌GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size)

571. {

572. char buf[2*STRFMT_MAXBUF_NUM+2+1], *p = buf;

573. TValue re, im;

574. if (size == 2*sizeof(double)) {

575. re.n = *(double *)sp; im.n = ((double *)sp)[1];

576. } else {

577. re.n = (double)*(float *)sp; im.n = (double)((float *)sp)[1];

578. }

579. p = lj_strfmt_wnum(p, &re);

580. if (!(im.u32.hi & 0x80000000u) || im.n != im.n) *p++ = '+';

581. p = lj_strfmt_wnum(p, &im);

582. *p = *(p-1) >= 'a' ? 'I' : 'i';

583. p++;

584. return lj_str_new(L, buf, p-buf);

585. }

587. /* -- C type state -------------------------------------------------------- */

589. /* Initialize C type table and state. */

590. ‌CTState *lj_ctype_init(lua_State *L)

591. {

592. CTState *cts = lj_mem_newt(L, sizeof(CTState), CTState);

593. CType *ct = lj_mem_newvec(L, CTTYPETAB_MIN, CType);

594. const char *name = lj_ctype_typenames;

595. CTypeID id;

596. memset(cts, 0, sizeof(CTState));

597. cts->tab = ct;

598. cts->sizetab = CTTYPETAB_MIN;

599. cts->top = CTTYPEINFO_NUM;

600. cts->L = NULL;

601. cts->g = G(L);

602. for (id = 0; id < CTTYPEINFO_NUM; id++, ct++) {

603. CTInfo info = lj_ctype_typeinfo[id];

604. ct->size = (CTSize)((int32_t)(info << 16) >> 26);

605. ct->info = info & 0xffff03ffu;

606. ct->sib = 0;

607. if (ctype_type(info) == CT_KW || ctype_istypedef(info)) {

608. size_t len = strlen(name);

609. GCstr *str = lj_str_new(L, name, len);

610. ctype_setname(ct, str);

611. name += len+1;

612. lj_ctype_addname(cts, ct, id);

613. } else {

614. setgcrefnull(ct->name);

615. ct->next = 0;

616. if (!ctype_isenum(info)) ctype_addtype(cts, ct, id);

617. }

618. }

619. setmref(G(L)->ctype_state, cts);

620. return cts;

621. }

623. /* Free C type table and state. */

624. ‌void lj_ctype_freestate(global_State *g)

625. {

626. CTState *cts = ctype_ctsG(g);

627. if (cts) {

628. lj_ccallback_mcode_free(cts);

629. lj_mem_freevec(g, cts->tab, cts->sizetab, CType);

630. lj_mem_freevec(g, cts->cb.cbid, cts->cb.sizeid, CTypeID1);

631. lj_mem_freet(g, cts);

632. }

633. }

635. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ctype.h - luajit-2.0-src

Global variables defined

- CCallback

- FPRCBArg

Data types defined

- CCallback

- CTInfo

- CTSize

- CTState

- CTState

- CType

- CType

- CTypeID

- CTypeID1

- FPRCBArg

Functions defined

- ctype_check

- ctype_child

- ctype_cts

- ctype_get

- ctype_raw

- ctype_rawchild

- ctype_setname

Macros defined

- CCALL_MAX_FPR

- CCALL_MAX_GPR

- CDF_SCL

- CDSDEF

- CDSFLAG

- CDSFLAG

- CKWDEF

- CKWNUM

- CKWNUM

- CTALIGN

- CTALIGN_PTR

- CTALIGN_PTR

- CTATTRIB

- CTBSZ_FIELD

- CTBSZ_MAX

- CTFP_ALIGNED

- CTFP_CCONV

- CTFP_PACKED

- CTF_ALIGN

- CTF_BOOL

- CTF_COMPLEX

- CTF_CONST

- CTF_FP

- CTF_INSERT

- CTF_LONG

- CTF_QUAL

- CTF_REF

- CTF_SSEREGPARM

- CTF_UCHAR

- CTF_UNION

- CTF_UNSIGNED

- CTF_VARARG

- CTF_VECTOR

- CTF_VLA

- CTF_VOLATILE

- CTHASH_MASK

- CTHASH_SIZE

- CTID_INT_PSZ

- CTID_INT_PSZ

- CTID_UINT_PSZ

- CTID_UINT_PSZ

- CTID_WCHAR

- CTID_WCHAR

- CTID_WCHAR

- CTINFO

- CTINFO_REF

- CTMASK_ALIGN

- CTMASK_ATTRIB

- CTMASK_BITBSZ

- CTMASK_BITCSZ

- CTMASK_BITPOS

- CTMASK_CCONV

- CTMASK_CID

- CTMASK_MSIZEP

- CTMASK_NUM

- CTMASK_REGPARM

- CTMASK_VSIZEP

- CTOKDEF

- CTOKNUM

- CTOKNUM

- CTSHIFT_ALIGN

- CTSHIFT_ATTRIB

- CTSHIFT_BITBSZ

- CTSHIFT_BITCSZ

- CTSHIFT_BITPOS

- CTSHIFT_CCONV

- CTSHIFT_MSIZEP

- CTSHIFT_NUM

- CTSHIFT_REGPARM

- CTSHIFT_VSIZEP

- CTSIZE_INVALID

- CTSIZE_PTR

- CTSIZE_PTR

- CTTYDEF

- CTTYDEFP

- CTTYDEFP

- CTTYIDDEF

- CTTYIDDEF

- CT_MEMALIGN

- LJ_CTYPE_RESTORE

- LJ_CTYPE_SAVE

- _LJ_CTYPE_H

- ctype_align

- ctype_attrib

- ctype_bitbsz

- ctype_bitcsz

- ctype_bitpos

- ctype_cconv

- ctype_cid

- ctype_ctsG

- ctype_hassize

- ctype_isarray

- ctype_isattrib

- ctype_isbitfield

- ctype_isbool

- ctype_iscomplex

- ctype_isconstval

- ctype_isenum

- ctype_isextern

- ctype_isfield

- ctype_isfp

- ctype_isfunc

- ctype_isinteger

- ctype_isinteger_or_bool

- ctype_isnum

- ctype_ispointer

- ctype_isptr

- ctype_isref

- ctype_isrefarray

- ctype_isstruct

- ctype_istypedef

- ctype_isvector

- ctype_isvlarray

- ctype_isvltype

- ctype_isvoid

- ctype_isxattrib

- ctype_msizeP

- ctype_type

- ctype_typeid

- ctype_vsizeP

- lj_ctype_getfield

Source code

1. /*

2. ** C type management.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_CTYPE_H

7. ‌#define _LJ_CTYPE_H

9. #include "lj_obj.h"

10. #include "lj_gc.h"

12. #if LJ_HASFFI

14. /* -- C type definitions -------------------------------------------------- */

16. /* C type numbers. Highest 4 bits of C type info. ORDER CT. */

17. enum {

18. /* Externally visible types. */

19. CT_NUM,                /* Integer or floating-point numbers. */

20. CT_STRUCT,                /* Struct or union. */

21. CT_PTR,                /* Pointer or reference. */

22. CT_ARRAY,                /* Array or complex type. */

23. CT_MAYCONVERT = CT_ARRAY,

24. CT_VOID,                /* Void type. */

25. CT_ENUM,                /* Enumeration. */

26. CT_HASSIZE = CT_ENUM,  /* Last type where ct->size holds the actual size. */

27. CT_FUNC,                /* Function. */

28. CT_TYPEDEF,                /* Typedef. */

29. CT_ATTRIB,                /* Miscellaneous attributes. */

30. /* Internal element types. */

31. CT_FIELD,                /* Struct/union field or function parameter. */

32. CT_BITFIELD,                /* Struct/union bitfield. */

33. CT_CONSTVAL,                /* Constant value. */

34. CT_EXTERN,                /* External reference. */

35. CT_KW                        /* Keyword. */

36. };

38. LJ_STATIC_ASSERT(((int)CT_PTR & (int)CT_ARRAY) == CT_PTR);

39. LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT);

41. /*

42. **  ---------- info ------------

43. ** |type      flags...  A   cid | size   |  sib  | next  | name  |

44. ** +----------------------------+--------+-------+-------+-------+--

45. ** |NUM       BFvcUL..  A       | size   |       | type  |       |

46. ** |STRUCT    ..vcU..V  A       | size   | field | name? | name? |

47. ** |PTR       ..vcR...  A   cid | size   |       | type  |       |

48. ** |ARRAY     VCvc...V  A   cid | size   |       | type  |       |

49. ** |VOID      ..vc....  A       | size   |       | type  |       |

50. ** |ENUM                A   cid | size   | const | name? | name? |

51. ** |FUNC      ....VS.. cc   cid | nargs  | field | name? | name? |

52. ** |TYPEDEF                 cid |        |       | name  | name  |

53. ** |ATTRIB        attrnum   cid | attr   | sib?  | type? |       |

54. ** |FIELD                   cid | offset | field |       | name? |

55. ** |BITFIELD  B.vcU csz bsz pos | offset | field |       | name? |

56. ** |CONSTVAL     c          cid | value  | const | name  | name  |

57. ** |EXTERN                  cid |        | sib?  | name  | name  |

58. ** |KW                      tok | size   |       | name  | name  |

59. ** +----------------------------+--------+-------+-------+-------+--

60. **        ^^  ^^--- bits used for C type conversion dispatch

61. */

63. /* C type info flags.     TFFArrrr  */

64. ‌#define CTF_BOOL        0x08000000u        /* Boolean: NUM, BITFIELD. */

65. ‌#define CTF_FP                0x04000000u        /* Floating-point: NUM. */

66. ‌#define CTF_CONST        0x02000000u        /* Const qualifier. */

67. ‌#define CTF_VOLATILE        0x01000000u        /* Volatile qualifier. */

68. ‌#define CTF_UNSIGNED        0x00800000u        /* Unsigned: NUM, BITFIELD. */

69. ‌#define CTF_LONG        0x00400000u        /* Long: NUM. */

70. ‌#define CTF_VLA                0x00100000u        /* Variable-length: ARRAY, STRUCT. */

71. ‌#define CTF_REF                0x00800000u        /* Reference: PTR. */

72. ‌#define CTF_VECTOR        0x08000000u        /* Vector: ARRAY. */

73. ‌#define CTF_COMPLEX        0x04000000u        /* Complex: ARRAY. */

74. ‌#define CTF_UNION        0x00800000u        /* Union: STRUCT. */

75. ‌#define CTF_VARARG        0x00800000u        /* Vararg: FUNC. */

76. ‌#define CTF_SSEREGPARM        0x00400000u        /* SSE register parameters: FUNC. */

78. ‌#define CTF_QUAL        (CTF_CONST|CTF_VOLATILE)

79. ‌#define CTF_ALIGN        (CTMASK_ALIGN<<CTSHIFT_ALIGN)

80. ‌#define CTF_UCHAR        ((char)-1 > 0 ? CTF_UNSIGNED : 0)

82. /* Flags used in parser.  .F.Ammvf   cp->attr  */

83. ‌#define CTFP_ALIGNED        0x00000001u        /* cp->attr + ALIGN */

84. ‌#define CTFP_PACKED        0x00000002u        /* cp->attr */

85. /*                        ...C...f   cp->fattr */

86. ‌#define CTFP_CCONV        0x00000001u        /* cp->fattr + CCONV/[SSE]REGPARM */

88. /* C type info bitfields. */

89. ‌#define CTMASK_CID        0x0000ffffu        /* Max. 65536 type IDs. */

90. ‌#define CTMASK_NUM        0xf0000000u        /* Max. 16 type numbers. */

91. ‌#define CTSHIFT_NUM        28

92. ‌#define CTMASK_ALIGN        15                /* Max. alignment is 2^15. */

93. ‌#define CTSHIFT_ALIGN        16

94. ‌#define CTMASK_ATTRIB        255                /* Max. 256 attributes. */

95. ‌#define CTSHIFT_ATTRIB        16

96. ‌#define CTMASK_CCONV        3                /* Max. 4 calling conventions. */

97. ‌#define CTSHIFT_CCONV        16

98. ‌#define CTMASK_REGPARM        3                /* Max. 0-3 regparms. */

99. ‌#define CTSHIFT_REGPARM        18

100. /* Bitfields only used in parser. */

101. ‌#define CTMASK_VSIZEP        15                /* Max. vector size is 2^15. */

102. ‌#define CTSHIFT_VSIZEP        4

103. ‌#define CTMASK_MSIZEP        255                /* Max. type size (via mode) is 128. */

104. ‌#define CTSHIFT_MSIZEP        8

106. /* Info bits for BITFIELD. Max. size of bitfield is 64 bits. */

107. ‌#define CTBSZ_MAX        32                /* Max. size of bitfield is 32 bit. */

108. ‌#define CTBSZ_FIELD        127                /* Temp. marker for regular field. */

109. ‌#define CTMASK_BITPOS        127

110. ‌#define CTMASK_BITBSZ        127

111. ‌#define CTMASK_BITCSZ        127

112. ‌#define CTSHIFT_BITPOS        0

113. ‌#define CTSHIFT_BITBSZ        8

114. ‌#define CTSHIFT_BITCSZ        16

116. ‌#define CTF_INSERT(info, field, val) \

117. info = (info & ~(CTMASK_##field<<CTSHIFT_##field)) | \

118. (((CTSize)(val) & CTMASK_##field) << CTSHIFT_##field)

120. /* Calling conventions. ORDER CC */

121. enum { CTCC_CDECL, CTCC_THISCALL, CTCC_FASTCALL, CTCC_STDCALL };

123. /* Attribute numbers. */

124. enum {

125. CTA_NONE,                /* Ignored attribute. Must be zero. */

126. CTA_QUAL,                /* Unmerged qualifiers. */

127. CTA_ALIGN,                /* Alignment override. */

128. CTA_SUBTYPE,                /* Transparent sub-type. */

129. CTA_REDIR,                /* Redirected symbol name. */

130. CTA_BAD,                /* To catch bad IDs. */

131. CTA__MAX

132. };

134. /* Special sizes. */

135. ‌#define CTSIZE_INVALID        0xffffffffu

137. ‌typedef uint32_t CTInfo;        /* Type info. */

138. ‌typedef uint32_t CTSize;        /* Type size. */

139. ‌typedef uint32_t CTypeID;        /* Type ID. */

140. ‌typedef uint16_t CTypeID1;        /* Minimum-sized type ID. */

142. /* C type table element. */

143. ‌typedef struct CType {

144. CTInfo info;                /* Type info. */

145. CTSize size;                /* Type size or other info. */

146. CTypeID1 sib;                /* Sibling element. */

147. CTypeID1 next;        /* Next element in hash chain. */

148. GCRef name;                /* Element name (GCstr). */

149. ‌} CType;

151. ‌#define CTHASH_SIZE        128        /* Number of hash anchors. */

152. ‌#define CTHASH_MASK        (CTHASH_SIZE-1)

154. /* Simplify target-specific configuration. Checked in lj_ccall.h. */

155. ‌#define CCALL_MAX_GPR                8

156. ‌#define CCALL_MAX_FPR                8

158. ‌‌typedef LJ_ALIGN(8) union FPRCBArg { double d; float f[2]; } FPRCBArg;

160. /* C callback state. Defined here, to avoid dragging in lj_ccall.h. */

162. ‌typedef LJ_ALIGN(8) struct CCallback {

163. FPRCBArg fpr[CCALL_MAX_FPR];        /* Arguments/results in FPRs. */

164. intptr_t gpr[CCALL_MAX_GPR];        /* Arguments/results in GPRs. */

165. intptr_t *stack;                /* Pointer to arguments on stack. */

166. void *mcode;                        /* Machine code for callback func. pointers. */

167. CTypeID1 *cbid;                /* Callback type table. */

168. MSize sizeid;                        /* Size of callback type table. */

169. MSize topid;                        /* Highest unused callback type table slot. */

170. MSize slot;                        /* Current callback slot. */

171. ‌} CCallback;

173. /* C type state. */

174. ‌typedef struct CTState {

175. CType *tab;                /* C type table. */

176. CTypeID top;                /* Current top of C type table. */

177. MSize sizetab;        /* Size of C type table. */

178. lua_State *L;                /* Lua state (needed for errors and allocations). */

179. global_State *g;        /* Global state. */

180. GCtab *finalizer;        /* Map of cdata to finalizer. */

181. GCtab *miscmap;        /* Map of -CTypeID to metatable and cb slot to func. */

182. CCallback cb;                /* Temporary callback state. */

183. CTypeID1 hash[CTHASH_SIZE];  /* Hash anchors for C type table. */

184. ‌} CTState;

186. ‌#define CTINFO(ct, flags)        (((CTInfo)(ct) << CTSHIFT_NUM) + (flags))

187. ‌#define CTALIGN(al)                ((CTSize)(al) << CTSHIFT_ALIGN)

188. ‌#define CTATTRIB(at)                ((CTInfo)(at) << CTSHIFT_ATTRIB)

190. ‌#define ctype_type(info)        ((info) >> CTSHIFT_NUM)

191. ‌#define ctype_cid(info)                ((CTypeID)((info) & CTMASK_CID))

192. ‌#define ctype_align(info)        (((info) >> CTSHIFT_ALIGN) & CTMASK_ALIGN)

193. ‌#define ctype_attrib(info)        (((info) >> CTSHIFT_ATTRIB) & CTMASK_ATTRIB)

194. ‌#define ctype_bitpos(info)        (((info) >> CTSHIFT_BITPOS) & CTMASK_BITPOS)

195. ‌#define ctype_bitbsz(info)        (((info) >> CTSHIFT_BITBSZ) & CTMASK_BITBSZ)

196. ‌#define ctype_bitcsz(info)        (((info) >> CTSHIFT_BITCSZ) & CTMASK_BITCSZ)

197. ‌#define ctype_vsizeP(info)        (((info) >> CTSHIFT_VSIZEP) & CTMASK_VSIZEP)

198. ‌#define ctype_msizeP(info)        (((info) >> CTSHIFT_MSIZEP) & CTMASK_MSIZEP)

199. ‌#define ctype_cconv(info)        (((info) >> CTSHIFT_CCONV) & CTMASK_CCONV)

201. /* Simple type checks. */

202. ‌#define ctype_isnum(info)        (ctype_type((info)) == CT_NUM)

203. ‌#define ctype_isvoid(info)        (ctype_type((info)) == CT_VOID)

204. ‌#define ctype_isptr(info)        (ctype_type((info)) == CT_PTR)

205. ‌#define ctype_isarray(info)        (ctype_type((info)) == CT_ARRAY)

206. ‌#define ctype_isstruct(info)        (ctype_type((info)) == CT_STRUCT)

207. ‌#define ctype_isfunc(info)        (ctype_type((info)) == CT_FUNC)

208. ‌#define ctype_isenum(info)        (ctype_type((info)) == CT_ENUM)

209. ‌#define ctype_istypedef(info)        (ctype_type((info)) == CT_TYPEDEF)

210. ‌#define ctype_isattrib(info)        (ctype_type((info)) == CT_ATTRIB)

211. ‌#define ctype_isfield(info)        (ctype_type((info)) == CT_FIELD)

212. ‌#define ctype_isbitfield(info)        (ctype_type((info)) == CT_BITFIELD)

213. ‌#define ctype_isconstval(info)        (ctype_type((info)) == CT_CONSTVAL)

214. ‌#define ctype_isextern(info)        (ctype_type((info)) == CT_EXTERN)

215. ‌#define ctype_hassize(info)        (ctype_type((info)) <= CT_HASSIZE)

217. /* Combined type and flag checks. */

218. ‌#define ctype_isinteger(info) \

219. (((info) & (CTMASK_NUM|CTF_BOOL|CTF_FP)) == CTINFO(CT_NUM, 0))

220. ‌#define ctype_isinteger_or_bool(info) \

221. (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0))

222. ‌#define ctype_isbool(info) \

223. (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL))

224. ‌#define ctype_isfp(info) \

225. (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, CTF_FP))

227. ‌#define ctype_ispointer(info) \

228. ((ctype_type(info) >> 1) == (CT_PTR >> 1))  /* Pointer or array. */

229. ‌#define ctype_isref(info) \

230. (((info) & (CTMASK_NUM|CTF_REF)) == CTINFO(CT_PTR, CTF_REF))

232. ‌#define ctype_isrefarray(info) \

233. (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0))

234. ‌#define ctype_isvector(info) \

235. (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR))

236. ‌#define ctype_iscomplex(info) \

237. (((info) & (CTMASK_NUM|CTF_COMPLEX)) == CTINFO(CT_ARRAY, CTF_COMPLEX))

239. ‌#define ctype_isvltype(info) \

240. (((info) & ((CTMASK_NUM|CTF_VLA) - (2u<<CTSHIFT_NUM))) == \

241. CTINFO(CT_STRUCT, CTF_VLA))  /* VL array or VL struct. */

242. ‌#define ctype_isvlarray(info) \

243. (((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_ARRAY, CTF_VLA))

245. ‌#define ctype_isxattrib(info, at) \

246. (((info) & (CTMASK_NUM|CTATTRIB(CTMASK_ATTRIB))) == \

247. CTINFO(CT_ATTRIB, CTATTRIB(at)))

249. /* Target-dependent sizes and alignments. */

250. #if LJ_64

251. ‌#define CTSIZE_PTR        8

252. ‌#define CTALIGN_PTR        CTALIGN(3)

253. #else

254. ‌#define CTSIZE_PTR        4

255. ‌#define CTALIGN_PTR        CTALIGN(2)

256. #endif

258. ‌#define CTINFO_REF(ref) \

259. CTINFO(CT_PTR, (CTF_CONST|CTF_REF|CTALIGN_PTR) + (ref))

261. ‌#define CT_MEMALIGN        3        /* Alignment guaranteed by memory allocator. */

263. /* -- Predefined types ---------------------------------------------------- */

265. /* Target-dependent types. */

266. #if LJ_TARGET_PPC

267. ‌#define CTTYDEFP(_) \

268. _(LINT32,                4,        CT_NUM, CTF_LONG|CTALIGN(2))

269. #else

270. ‌#define CTTYDEFP(_)

271. #endif

273. /* Common types. */

274. ‌#define CTTYDEF(_) \

275. _(NONE,                0,        CT_ATTRIB, CTATTRIB(CTA_BAD)) \

276. _(VOID,                -1,        CT_VOID, CTALIGN(0)) \

277. _(CVOID,                -1,        CT_VOID, CTF_CONST|CTALIGN(0)) \

278. _(BOOL,                1,        CT_NUM, CTF_BOOL|CTF_UNSIGNED|CTALIGN(0)) \

279. _(CCHAR,                1,        CT_NUM, CTF_CONST|CTF_UCHAR|CTALIGN(0)) \

280. _(INT8,                1,        CT_NUM, CTALIGN(0)) \

281. _(UINT8,                1,        CT_NUM, CTF_UNSIGNED|CTALIGN(0)) \

282. _(INT16,                2,        CT_NUM, CTALIGN(1)) \

283. _(UINT16,                2,        CT_NUM, CTF_UNSIGNED|CTALIGN(1)) \

284. _(INT32,                4,        CT_NUM, CTALIGN(2)) \

285. _(UINT32,                4,        CT_NUM, CTF_UNSIGNED|CTALIGN(2)) \

286. _(INT64,                8,        CT_NUM, CTF_LONG|CTALIGN(3)) \

287. _(UINT64,                8,        CT_NUM, CTF_UNSIGNED|CTF_LONG|CTALIGN(3)) \

288. _(FLOAT,                4,        CT_NUM, CTF_FP|CTALIGN(2)) \

289. _(DOUBLE,                8,        CT_NUM, CTF_FP|CTALIGN(3)) \

290. _(COMPLEX_FLOAT,        8,        CT_ARRAY, CTF_COMPLEX|CTALIGN(2)|CTID_FLOAT) \

291. _(COMPLEX_DOUBLE,        16,        CT_ARRAY, CTF_COMPLEX|CTALIGN(3)|CTID_DOUBLE) \

292. _(P_VOID,        CTSIZE_PTR,        CT_PTR, CTALIGN_PTR|CTID_VOID) \

293. _(P_CVOID,        CTSIZE_PTR,        CT_PTR, CTALIGN_PTR|CTID_CVOID) \

294. _(P_CCHAR,        CTSIZE_PTR,        CT_PTR, CTALIGN_PTR|CTID_CCHAR) \

295. _(A_CCHAR,                -1,        CT_ARRAY, CTF_CONST|CTALIGN(0)|CTID_CCHAR) \

296. _(CTYPEID,                4,        CT_ENUM, CTALIGN(2)|CTID_INT32) \

297. CTTYDEFP(_) \

298. /* End of type list. */

300. /* Public predefined type IDs. */

301. enum {

302. ‌#define CTTYIDDEF(id, sz, ct, info)        CTID_##id,

303. CTTYDEF(CTTYIDDEF)

304. ‌#undef CTTYIDDEF

305. /* Predefined typedefs and keywords follow. */

306. CTID_MAX = 65536

307. };

309. /* Target-dependent type IDs. */

310. #if LJ_64

311. ‌#define CTID_INT_PSZ        CTID_INT64

312. ‌#define CTID_UINT_PSZ        CTID_UINT64

313. #else

314. ‌#define CTID_INT_PSZ        CTID_INT32

315. ‌#define CTID_UINT_PSZ        CTID_UINT32

316. #endif

318. #if LJ_ABI_WIN

319. ‌#define CTID_WCHAR        CTID_UINT16

320. #elif LJ_TARGET_PPC

321. ‌#define CTID_WCHAR        CTID_LINT32

322. #else

323. ‌#define CTID_WCHAR        CTID_INT32

324. #endif

326. /* -- C tokens and keywords ----------------------------------------------- */

328. /* C lexer keywords. */

329. ‌#define CTOKDEF(_) \

330. _(IDENT, "<identifier>") _(STRING, "<string>") \

331. _(INTEGER, "<integer>") _(EOF, "<eof>") \

332. _(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \

333. _(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->")

335. /* Simple declaration specifiers. */

336. ‌#define CDSDEF(_) \

337. _(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \

338. _(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \

339. _(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \

340. _(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER)

342. /* C keywords. */

343. ‌#define CKWDEF(_) \

344. CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \

345. _(DECLSPEC) _(CCDECL) _(PTRSZ) \

346. _(STRUCT) _(UNION) _(ENUM) \

347. _(SIZEOF) _(ALIGNOF)

349. /* C token numbers. */

350. enum {

351. CTOK_OFS = 255,

352. ‌#define CTOKNUM(name, sym)        CTOK_##name,

353. ‌#define CKWNUM(name)                CTOK_##name,

354. CTOKDEF(CTOKNUM)

355. CKWDEF(CKWNUM)

356. ‌#undef CTOKNUM

357. ‌#undef CKWNUM

358. CTOK_FIRSTDECL = CTOK_VOID,

359. CTOK_FIRSTSCL = CTOK_TYPEDEF,

360. CTOK_LASTDECLFLAG = CTOK_REGISTER,

361. CTOK_LASTDECL = CTOK_ENUM

362. };

364. /* Declaration specifier flags. */

365. enum {

366. ‌#define CDSFLAG(name)        CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)),

367. CDSDEF(CDSFLAG)

368. ‌#undef CDSFLAG

369. CDF__END

370. };

372. ‌#define CDF_SCL  (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER)

374. /* -- C type management --------------------------------------------------- */

376. ‌#define ctype_ctsG(g)                (mref((g)->ctype_state, CTState))

378. /* Get C type state. */

379. ‌static LJ_AINLINE CTState *ctype_cts(lua_State *L)

380. {

381. CTState *cts = ctype_ctsG(G(L));

382. cts->L = L;  /* Save L for errors and allocations. */

383. return cts;

384. }

386. /* Save and restore state of C type table. */

387. ‌#define LJ_CTYPE_SAVE(cts)        CTState savects_ = *(cts)

388. ‌#define LJ_CTYPE_RESTORE(cts) \

389. ((cts)->top = savects_.top, \

390. memcpy((cts)->hash, savects_.hash, sizeof(savects_.hash)))

392. /* Check C type ID for validity when assertions are enabled. */

393. ‌static LJ_AINLINE CTypeID ctype_check(CTState *cts, CTypeID id)

394. {

395. lua_assert(id > 0 && id < cts->top); UNUSED(cts);

396. return id;

397. }

399. /* Get C type for C type ID. */

400. ‌static LJ_AINLINE CType *ctype_get(CTState *cts, CTypeID id)

401. {

402. return &cts->tab[ctype_check(cts, id)];

403. }

405. /* Get C type ID for a C type. */

406. ‌#define ctype_typeid(cts, ct)        ((CTypeID)((ct) - (cts)->tab))

408. /* Get child C type. */

409. ‌static LJ_AINLINE CType *ctype_child(CTState *cts, CType *ct)

410. {

411. lua_assert(!(ctype_isvoid(ct->info) || ctype_isstruct(ct->info) ||

412. ctype_isbitfield(ct->info)));  /* These don't have children. */

413. return ctype_get(cts, ctype_cid(ct->info));

414. }

416. /* Get raw type for a C type ID. */

417. ‌static LJ_AINLINE CType *ctype_raw(CTState *cts, CTypeID id)

418. {

419. CType *ct = ctype_get(cts, id);

420. while (ctype_isattrib(ct->info)) ct = ctype_child(cts, ct);

421. return ct;

422. }

424. /* Get raw type of the child of a C type. */

425. ‌static LJ_AINLINE CType *ctype_rawchild(CTState *cts, CType *ct)

426. {

427. do { ct = ctype_child(cts, ct); } while (ctype_isattrib(ct->info));

428. return ct;

429. }

431. /* Set the name of a C type table element. */

432. ‌static LJ_AINLINE void ctype_setname(CType *ct, GCstr *s)

433. {

434. /* NOBARRIER: mark string as fixed -- the C type table is never collected. */

435. fixstring(s);

436. setgcref(ct->name, obj2gco(s));

437. }

439. LJ_FUNC CTypeID lj_ctype_new(CTState *cts, CType **ctp);

440. LJ_FUNC CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size);

441. LJ_FUNC void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id);

442. LJ_FUNC CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name,

443. uint32_t tmask);

444. LJ_FUNC CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name,

445. CTSize *ofs, CTInfo *qual);

446. ‌#define lj_ctype_getfield(cts, ct, name, ofs) \

447. lj_ctype_getfieldq((cts), (ct), (name), (ofs), NULL)

448. LJ_FUNC CType *lj_ctype_rawref(CTState *cts, CTypeID id);

449. LJ_FUNC CTSize lj_ctype_size(CTState *cts, CTypeID id);

450. LJ_FUNC CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem);

451. LJ_FUNC CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp);

452. LJ_FUNC cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm);

453. LJ_FUNC GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name);

454. LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned);

455. LJ_FUNC GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size);

456. LJ_FUNC CTState *lj_ctype_init(lua_State *L);

457. LJ_FUNC void lj_ctype_freestate(global_State *g);

459. #endif

461. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ccallback.c - luajit-2.0-src

Functions defined

- CALLBACK_OFS2SLOT

- callback_checkfunc

- callback_conv_args

- callback_conv_result

- callback_mcode_init

- callback_mcode_init

- callback_mcode_init

- callback_mcode_init

- callback_mcode_init

- callback_mcode_new

- callback_slot2ptr

- callback_slot_new

- lj_ccallback_enter

- lj_ccallback_leave

- lj_ccallback_mcode_free

- lj_ccallback_new

- lj_ccallback_ptr2slot

Macros defined

- CALLBACK_HANDLE_REGARG

- CALLBACK_HANDLE_REGARG

- CALLBACK_HANDLE_REGARG

- CALLBACK_HANDLE_REGARG

- CALLBACK_HANDLE_REGARG

- CALLBACK_HANDLE_REGARG

- CALLBACK_HANDLE_REGARG

- CALLBACK_HANDLE_REGARG_FP1

- CALLBACK_HANDLE_REGARG_FP1

- CALLBACK_HANDLE_REGARG_FP2

- CALLBACK_HANDLE_REGARG_FP2

- CALLBACK_HANDLE_RET

- CALLBACK_HANDLE_RET

- CALLBACK_MAX_SLOT

- CALLBACK_MAX_SLOT

- CALLBACK_MAX_SLOT

- CALLBACK_MAX_SLOT

- CALLBACK_MCODE_GROUP

- CALLBACK_MCODE_HEAD

- CALLBACK_MCODE_HEAD

- CALLBACK_MCODE_HEAD

- CALLBACK_MCODE_HEAD

- CALLBACK_MCODE_HEAD

- CALLBACK_MCODE_SIZE

- CALLBACK_OFS2SLOT

- CALLBACK_OFS2SLOT

- CALLBACK_OFS2SLOT

- CALLBACK_SLOT2OFS

- CALLBACK_SLOT2OFS

- CALLBACK_SLOT2OFS

- CALLBACK_SLOT2OFS

- MAP_ANONYMOUS

- WIN32_LEAN_AND_MEAN

- callback_mcode_init

- callback_mcode_init

Source code

1. /*

2. ** FFI C callback handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "lj_obj.h"

8. #if LJ_HASFFI

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_tab.h"

13. #include "lj_state.h"

14. #include "lj_frame.h"

15. #include "lj_ctype.h"

16. #include "lj_cconv.h"

17. #include "lj_ccall.h"

18. #include "lj_ccallback.h"

19. #include "lj_target.h"

20. #include "lj_mcode.h"

21. #include "lj_trace.h"

22. #include "lj_vm.h"

24. /* -- Target-specific handling of callback slots -------------------------- */

26. ‌#define CALLBACK_MCODE_SIZE        (LJ_PAGESIZE * LJ_NUM_CBPAGE)

28. #if LJ_OS_NOJIT

30. /* Callbacks disabled. */

31. ‌#define CALLBACK_SLOT2OFS(slot)        (0*(slot))

32. ‌#define CALLBACK_OFS2SLOT(ofs)        (0*(ofs))

33. ‌#define CALLBACK_MAX_SLOT        0

35. #elif LJ_TARGET_X86ORX64

37. ‌#define CALLBACK_MCODE_HEAD        (LJ_64 ? 8 : 0)

38. ‌#define CALLBACK_MCODE_GROUP        (-2+1+2+5+(LJ_64 ? 6 : 5))

40. ‌#define CALLBACK_SLOT2OFS(slot) \

41. (CALLBACK_MCODE_HEAD + CALLBACK_MCODE_GROUP*((slot)/32) + 4*(slot))

43. ‌static MSize CALLBACK_OFS2SLOT(MSize ofs)

44. {

45. MSize group;

46. ofs -= CALLBACK_MCODE_HEAD;

47. group = ofs / (32*4 + CALLBACK_MCODE_GROUP);

48. return (ofs % (32*4 + CALLBACK_MCODE_GROUP))/4 + group*32;

49. }

51. ‌#define CALLBACK_MAX_SLOT \

52. (((CALLBACK_MCODE_SIZE-CALLBACK_MCODE_HEAD)/(CALLBACK_MCODE_GROUP+4*32))*32)

54. #elif LJ_TARGET_ARM

56. ‌#define CALLBACK_MCODE_HEAD                32

58. #elif LJ_TARGET_ARM64

60. ‌#define CALLBACK_MCODE_HEAD                32

62. #elif LJ_TARGET_PPC

64. ‌#define CALLBACK_MCODE_HEAD                24

66. #elif LJ_TARGET_MIPS

68. ‌#define CALLBACK_MCODE_HEAD                24

70. #else

72. /* Missing support for this architecture. */

73. ‌#define CALLBACK_SLOT2OFS(slot)        (0*(slot))

74. ‌#define CALLBACK_OFS2SLOT(ofs)        (0*(ofs))

75. ‌#define CALLBACK_MAX_SLOT        0

77. #endif

79. #ifndef CALLBACK_SLOT2OFS

80. ‌#define CALLBACK_SLOT2OFS(slot)                (CALLBACK_MCODE_HEAD + 8*(slot))

81. ‌#define CALLBACK_OFS2SLOT(ofs)                (((ofs)-CALLBACK_MCODE_HEAD)/8)

82. ‌#define CALLBACK_MAX_SLOT                (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE))

83. #endif

85. /* Convert callback slot number to callback function pointer. */

86. ‌static void *callback_slot2ptr(CTState *cts, MSize slot)

87. {

88. return (uint8_t *)cts->cb.mcode + CALLBACK_SLOT2OFS(slot);

89. }

91. /* Convert callback function pointer to slot number. */

92. ‌MSize lj_ccallback_ptr2slot(CTState *cts, void *p)

93. {

94. uintptr_t ofs = (uintptr_t)((uint8_t *)p -(uint8_t *)cts->cb.mcode);

95. if (ofs < CALLBACK_MCODE_SIZE) {

96. MSize slot = CALLBACK_OFS2SLOT((MSize)ofs);

97. if (CALLBACK_SLOT2OFS(slot) == (MSize)ofs)

98. return slot;

99. }

100. return ~0u;  /* Not a known callback function pointer. */

101. }

103. /* Initialize machine code for callback function pointers. */

104. #if LJ_OS_NOJIT

105. /* Disabled callback support. */

106. ‌#define callback_mcode_init(g, p)        UNUSED(p)

107. #elif LJ_TARGET_X86ORX64

108. ‌static void callback_mcode_init(global_State *g, uint8_t *page)

109. {

110. uint8_t *p = page;

111. uint8_t *target = (uint8_t *)(void *)lj_vm_ffi_callback;

112. MSize slot;

113. #if LJ_64

114. *(void **)p = target; p += 8;

115. #endif

116. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {

117. /* mov al, slot; jmp group */

118. *p++ = XI_MOVrib | RID_EAX; *p++ = (uint8_t)slot;

119. if ((slot & 31) == 31 || slot == CALLBACK_MAX_SLOT-1) {

120. /* push ebp/rbp; mov ah, slot>>8; mov ebp, &g. */

121. *p++ = XI_PUSH + RID_EBP;

122. *p++ = XI_MOVrib | (RID_EAX+4); *p++ = (uint8_t)(slot >> 8);

123. *p++ = XI_MOVri | RID_EBP;

124. *(int32_t *)p = i32ptr(g); p += 4;

125. #if LJ_64

126. /* jmp [rip-pageofs] where lj_vm_ffi_callback is stored. */

127. *p++ = XI_GROUP5; *p++ = XM_OFS0 + (XOg_JMP<<3) + RID_EBP;

128. *(int32_t *)p = (int32_t)(page-(p+4)); p += 4;

129. #else

130. /* jmp lj_vm_ffi_callback. */

131. *p++ = XI_JMP; *(int32_t *)p = target-(p+4); p += 4;

132. #endif

133. } else {

134. *p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2);

135. }

136. }

137. lua_assert(p - page <= CALLBACK_MCODE_SIZE);

138. }

139. #elif LJ_TARGET_ARM

140. ‌static void callback_mcode_init(global_State *g, uint32_t *page)

141. {

142. uint32_t *p = page;

143. void *target = (void *)lj_vm_ffi_callback;

144. MSize slot;

145. /* This must match with the saveregs macro in buildvm_arm.dasc. */

146. *p++ = ARMI_SUB|ARMF_D(RID_R12)|ARMF_N(RID_R12)|ARMF_M(RID_PC);

147. *p++ = ARMI_PUSH|ARMF_N(RID_SP)|RSET_RANGE(RID_R4,RID_R11+1)|RID2RSET(RID_LR);

148. *p++ = ARMI_SUB|ARMI_K12|ARMF_D(RID_R12)|ARMF_N(RID_R12)|CALLBACK_MCODE_HEAD;

149. *p++ = ARMI_STR|ARMI_LS_P|ARMI_LS_W|ARMF_D(RID_R12)|ARMF_N(RID_SP)|(CFRAME_SIZE-4*9);

150. *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_R12)|ARMF_N(RID_PC);

151. *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_PC)|ARMF_N(RID_PC);

152. *p++ = u32ptr(g);

153. *p++ = u32ptr(target);

154. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {

155. *p++ = ARMI_MOV|ARMF_D(RID_R12)|ARMF_M(RID_PC);

156. *p = ARMI_B | ((page-p-2) & 0x00ffffffu);

157. p++;

158. }

159. lua_assert(p - page <= CALLBACK_MCODE_SIZE);

160. }

161. #elif LJ_TARGET_ARM64

162. ‌static void callback_mcode_init(global_State *g, uint32_t *page)

163. {

164. uint32_t *p = page;

165. void *target = (void *)lj_vm_ffi_callback;

166. MSize slot;

167. *p++ = A64I_LDRLx | A64F_D(RID_X11) | A64F_S19(4);

168. *p++ = A64I_LDRLx | A64F_D(RID_X10) | A64F_S19(5);

169. *p++ = A64I_BR | A64F_N(RID_X11);

170. *p++ = A64I_NOP;

171. ((void **)p)[0] = target;

172. ((void **)p)[1] = g;

173. p += 4;

174. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {

175. *p++ = A64I_MOVZw | A64F_D(RID_X9) | A64F_U16(slot);

176. *p = A64I_B | A64F_S26((page-p) & 0x03ffffffu);

177. p++;

178. }

179. lua_assert(p - page <= CALLBACK_MCODE_SIZE);

180. }

181. #elif LJ_TARGET_PPC

182. ‌static void callback_mcode_init(global_State *g, uint32_t *page)

183. {

184. uint32_t *p = page;

185. void *target = (void *)lj_vm_ffi_callback;

186. MSize slot;

187. *p++ = PPCI_LIS | PPCF_T(RID_TMP) | (u32ptr(target) >> 16);

188. *p++ = PPCI_LIS | PPCF_T(RID_R12) | (u32ptr(g) >> 16);

189. *p++ = PPCI_ORI | PPCF_A(RID_TMP)|PPCF_T(RID_TMP) | (u32ptr(target) & 0xffff);

190. *p++ = PPCI_ORI | PPCF_A(RID_R12)|PPCF_T(RID_R12) | (u32ptr(g) & 0xffff);

191. *p++ = PPCI_MTCTR | PPCF_T(RID_TMP);

192. *p++ = PPCI_BCTR;

193. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {

194. *p++ = PPCI_LI | PPCF_T(RID_R11) | slot;

195. *p = PPCI_B | (((page-p) & 0x00ffffffu) << 2);

196. p++;

197. }

198. lua_assert(p - page <= CALLBACK_MCODE_SIZE);

199. }

200. #elif LJ_TARGET_MIPS

201. ‌static void callback_mcode_init(global_State *g, uint32_t *page)

202. {

203. uint32_t *p = page;

204. void *target = (void *)lj_vm_ffi_callback;

205. MSize slot;

206. *p++ = MIPSI_SW | MIPSF_T(RID_R1)|MIPSF_S(RID_SP) | 0;

207. *p++ = MIPSI_LUI | MIPSF_T(RID_R3) | (u32ptr(target) >> 16);

208. *p++ = MIPSI_LUI | MIPSF_T(RID_R2) | (u32ptr(g) >> 16);

209. *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) |(u32ptr(target)&0xffff);

210. *p++ = MIPSI_JR | MIPSF_S(RID_R3);

211. *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | (u32ptr(g)&0xffff);

212. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {

213. *p = MIPSI_B | ((page-p-1) & 0x0000ffffu);

214. p++;

215. *p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot;

216. }

217. lua_assert(p - page <= CALLBACK_MCODE_SIZE);

218. }

219. #else

220. /* Missing support for this architecture. */

221. ‌#define callback_mcode_init(g, p)        UNUSED(p)

222. #endif

224. /* -- Machine code management --------------------------------------------- */

226. #if LJ_TARGET_WINDOWS

228. ‌#define WIN32_LEAN_AND_MEAN

229. #include <windows.h>

231. #elif LJ_TARGET_POSIX

233. #include <sys/mman.h>

234. #ifndef MAP_ANONYMOUS

235. ‌#define MAP_ANONYMOUS   MAP_ANON

236. #endif

238. #endif

240. /* Allocate and initialize area for callback function pointers. */

241. ‌static void callback_mcode_new(CTState *cts)

242. {

243. size_t sz = (size_t)CALLBACK_MCODE_SIZE;

244. void *p;

245. if (CALLBACK_MAX_SLOT == 0)

246. lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);

247. #if LJ_TARGET_WINDOWS

248. p = VirtualAlloc(NULL, sz, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);

249. if (!p)

250. lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);

251. #elif LJ_TARGET_POSIX

252. p = mmap(NULL, sz, (PROT_READ|PROT_WRITE), MAP_PRIVATE|MAP_ANONYMOUS,

253. -1, 0);

254. if (p == MAP_FAILED)

255. lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);

256. #else

257. /* Fallback allocator. Fails if memory is not executable by default. */

258. p = lj_mem_new(cts->L, sz);

259. #endif

260. cts->cb.mcode = p;

261. callback_mcode_init(cts->g, p);

262. lj_mcode_sync(p, (char *)p + sz);

263. #if LJ_TARGET_WINDOWS

264. {

265. DWORD oprot;

266. VirtualProtect(p, sz, PAGE_EXECUTE_READ, &oprot);

267. }

268. #elif LJ_TARGET_POSIX

269. mprotect(p, sz, (PROT_READ|PROT_EXEC));

270. #endif

271. }

273. /* Free area for callback function pointers. */

274. ‌void lj_ccallback_mcode_free(CTState *cts)

275. {

276. size_t sz = (size_t)CALLBACK_MCODE_SIZE;

277. void *p = cts->cb.mcode;

278. if (p == NULL) return;

279. #if LJ_TARGET_WINDOWS

280. VirtualFree(p, 0, MEM_RELEASE);

281. UNUSED(sz);

282. #elif LJ_TARGET_POSIX

283. munmap(p, sz);

284. #else

285. lj_mem_free(cts->g, p, sz);

286. #endif

287. }

289. /* -- C callback entry ---------------------------------------------------- */

291. /* Target-specific handling of register arguments. Similar to lj_ccall.c. */

292. #if LJ_TARGET_X86

294. ‌#define CALLBACK_HANDLE_REGARG \

295. if (!isfp) {  /* Only non-FP values may be passed in registers. */ \

296. if (n > 1) {  /* Anything > 32 bit is passed on the stack. */ \

297. if (!LJ_ABI_WIN) ngpr = maxgpr;  /* Prevent reordering. */ \

298. } else if (ngpr + 1 <= maxgpr) { \

299. sp = &cts->cb.gpr[ngpr]; \

300. ngpr += n; \

301. goto done; \

302. } \

303. }

305. #elif LJ_TARGET_X64 && LJ_ABI_WIN

307. /* Windows/x64 argument registers are strictly positional (use ngpr). */

308. ‌#define CALLBACK_HANDLE_REGARG \

309. if (isfp) { \

310. if (ngpr < maxgpr) { sp = &cts->cb.fpr[ngpr++]; UNUSED(nfpr); goto done; } \

311. } else { \

312. if (ngpr < maxgpr) { sp = &cts->cb.gpr[ngpr++]; goto done; } \

313. }

315. #elif LJ_TARGET_X64

317. ‌#define CALLBACK_HANDLE_REGARG \

318. if (isfp) { \

319. if (nfpr + n <= CCALL_NARG_FPR) { \

320. sp = &cts->cb.fpr[nfpr]; \

321. nfpr += n; \

322. goto done; \

323. } \

324. } else { \

325. if (ngpr + n <= maxgpr) { \

326. sp = &cts->cb.gpr[ngpr]; \

327. ngpr += n; \

328. goto done; \

329. } \

330. }

332. #elif LJ_TARGET_ARM

334. #if LJ_ABI_SOFTFP

336. ‌#define CALLBACK_HANDLE_REGARG_FP1        UNUSED(isfp);

337. ‌#define CALLBACK_HANDLE_REGARG_FP2

339. #else

341. ‌#define CALLBACK_HANDLE_REGARG_FP1 \

342. if (isfp) { \

343. if (n == 1) { \

344. if (fprodd) { \

345. sp = &cts->cb.fpr[fprodd-1]; \

346. fprodd = 0; \

347. goto done; \

348. } else if (nfpr + 1 <= CCALL_NARG_FPR) { \

349. sp = &cts->cb.fpr[nfpr++]; \

350. fprodd = nfpr; \

351. goto done; \

352. } \

353. } else { \

354. if (nfpr + 1 <= CCALL_NARG_FPR) { \

355. sp = &cts->cb.fpr[nfpr++]; \

356. goto done; \

357. } \

358. } \

359. fprodd = 0;  /* No reordering after the first FP value is on stack. */ \

360. } else {

362. ‌#define CALLBACK_HANDLE_REGARG_FP2        }

364. #endif

366. ‌#define CALLBACK_HANDLE_REGARG \

367. CALLBACK_HANDLE_REGARG_FP1 \

368. if (n > 1) ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \

369. if (ngpr + n <= maxgpr) { \

370. sp = &cts->cb.gpr[ngpr]; \

371. ngpr += n; \

372. goto done; \

373. } CALLBACK_HANDLE_REGARG_FP2

375. #elif LJ_TARGET_ARM64

377. ‌#define CALLBACK_HANDLE_REGARG \

378. if (isfp) { \

379. if (nfpr + n <= CCALL_NARG_FPR) { \

380. sp = &cts->cb.fpr[nfpr]; \

381. nfpr += n; \

382. goto done; \

383. } else { \

384. nfpr = CCALL_NARG_FPR;  /* Prevent reordering. */ \

385. } \

386. } else { \

387. if (!LJ_TARGET_IOS && n > 1) \

388. ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \

389. if (ngpr + n <= maxgpr) { \

390. sp = &cts->cb.gpr[ngpr]; \

391. ngpr += n; \

392. goto done; \

393. } else { \

394. ngpr = CCALL_NARG_GPR;  /* Prevent reordering. */ \

395. } \

396. }

398. #elif LJ_TARGET_PPC

400. ‌#define CALLBACK_HANDLE_REGARG \

401. if (isfp) { \

402. if (nfpr + 1 <= CCALL_NARG_FPR) { \

403. sp = &cts->cb.fpr[nfpr++]; \

404. cta = ctype_get(cts, CTID_DOUBLE);  /* FPRs always hold doubles. */ \

405. goto done; \

406. } \

407. } else {  /* Try to pass argument in GPRs. */ \

408. if (n > 1) { \

409. lua_assert(ctype_isinteger(cta->info) && n == 2);  /* int64_t. */ \

410. ngpr = (ngpr + 1u) & ~1u;  /* Align int64_t to regpair. */ \

411. } \

412. if (ngpr + n <= maxgpr) { \

413. sp = &cts->cb.gpr[ngpr]; \

414. ngpr += n; \

415. goto done; \

416. } \

417. }

419. ‌#define CALLBACK_HANDLE_RET \

420. if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \

421. *(double *)dp = *(float *)dp;  /* FPRs always hold doubles. */

423. #elif LJ_TARGET_MIPS

425. ‌#define CALLBACK_HANDLE_REGARG \

426. if (isfp && nfpr < CCALL_NARG_FPR) {  /* Try to pass argument in FPRs. */ \

427. sp = (void *)((uint8_t *)&cts->cb.fpr[nfpr] + ((LJ_BE && n==1) ? 4 : 0)); \

428. nfpr++; ngpr += n; \

429. goto done; \

430. } else {  /* Try to pass argument in GPRs. */ \

431. nfpr = CCALL_NARG_FPR; \

432. if (n > 1) ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \

433. if (ngpr + n <= maxgpr) { \

434. sp = &cts->cb.gpr[ngpr]; \

435. ngpr += n; \

436. goto done; \

437. } \

438. }

440. ‌#define CALLBACK_HANDLE_RET \

441. if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \

442. ((float *)dp)[1] = *(float *)dp;

444. #else

445. #error "Missing calling convention definitions for this architecture"

446. #endif

448. /* Convert and push callback arguments to Lua stack. */

449. ‌static void callback_conv_args(CTState *cts, lua_State *L)

450. {

451. TValue *o = L->top;

452. intptr_t *stack = cts->cb.stack;

453. MSize slot = cts->cb.slot;

454. CTypeID id = 0, rid, fid;

455. int gcsteps = 0;

456. CType *ct;

457. GCfunc *fn;

458. int fntp;

459. MSize ngpr = 0, nsp = 0, maxgpr = CCALL_NARG_GPR;

460. #if CCALL_NARG_FPR

461. MSize nfpr = 0;

462. #if LJ_TARGET_ARM

463. MSize fprodd = 0;

464. #endif

465. #endif

467. if (slot < cts->cb.sizeid && (id = cts->cb.cbid[slot]) != 0) {

468. ct = ctype_get(cts, id);

469. rid = ctype_cid(ct->info);  /* Return type. x86: +(spadj<<16). */

470. fn = funcV(lj_tab_getint(cts->miscmap, (int32_t)slot));

471. fntp = LJ_TFUNC;

472. } else {  /* Must set up frame first, before throwing the error. */

473. ct = NULL;

474. rid = 0;

475. fn = (GCfunc *)L;

476. fntp = LJ_TTHREAD;

477. }

478. /* Continuation returns from callback. */

479. if (LJ_FR2) {

480. (o++)->u64 = LJ_CONT_FFI_CALLBACK;

481. (o++)->u64 = rid;

482. o++;

483. } else {

484. o->u32.lo = LJ_CONT_FFI_CALLBACK;

485. o->u32.hi = rid;

486. o++;

487. }

488. setframe_gc(o, obj2gco(fn), fntp);

489. setframe_ftsz(o, ((char *)(o+1) - (char *)L->base) + FRAME_CONT);

490. L->top = L->base = ++o;

491. if (!ct)

492. lj_err_caller(cts->L, LJ_ERR_FFI_BADCBACK);

493. if (isluafunc(fn))

494. setcframe_pc(L->cframe, proto_bc(funcproto(fn))+1);

495. lj_state_checkstack(L, LUA_MINSTACK);  /* May throw. */

496. o = L->base;  /* Might have been reallocated. */

498. #if LJ_TARGET_X86

499. /* x86 has several different calling conventions. */

500. switch (ctype_cconv(ct->info)) {

501. case CTCC_FASTCALL: maxgpr = 2; break;

502. case CTCC_THISCALL: maxgpr = 1; break;

503. default: maxgpr = 0; break;

504. }

505. #endif

507. fid = ct->sib;

508. while (fid) {

509. CType *ctf = ctype_get(cts, fid);

510. if (!ctype_isattrib(ctf->info)) {

511. CType *cta;

512. void *sp;

513. CTSize sz;

514. int isfp;

515. MSize n;

516. lua_assert(ctype_isfield(ctf->info));

517. cta = ctype_rawchild(cts, ctf);

518. isfp = ctype_isfp(cta->info);

519. sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);

520. n = sz / CTSIZE_PTR;  /* Number of GPRs or stack slots needed. */

522. CALLBACK_HANDLE_REGARG  /* Handle register arguments. */

524. /* Otherwise pass argument on stack. */

525. if (CCALL_ALIGN_STACKARG && LJ_32 && sz == 8)

526. nsp = (nsp + 1) & ~1u;  /* Align 64 bit argument on stack. */

527. sp = &stack[nsp];

528. nsp += n;

530. done:

531. if (LJ_BE && cta->size < CTSIZE_PTR)

532. sp = (void *)((uint8_t *)sp + CTSIZE_PTR-cta->size);

533. gcsteps += lj_cconv_tv_ct(cts, cta, 0, o++, sp);

534. }

535. fid = ctf->sib;

536. }

537. L->top = o;

538. #if LJ_TARGET_X86

539. /* Store stack adjustment for returns from non-cdecl callbacks. */

540. if (ctype_cconv(ct->info) != CTCC_CDECL) {

541. #if LJ_FR2

542. (L->base-3)->u64 |= (nsp << (16+2));

543. #else

544. (L->base-2)->u32.hi |= (nsp << (16+2));

545. #endif

546. }

547. #endif

548. while (gcsteps-- > 0)

549. lj_gc_check(L);

550. }

552. /* Convert Lua object to callback result. */

553. ‌static void callback_conv_result(CTState *cts, lua_State *L, TValue *o)

554. {

555. #if LJ_FR2

556. CType *ctr = ctype_raw(cts, (uint16_t)(L->base-3)->u64);

557. #else

558. CType *ctr = ctype_raw(cts, (uint16_t)(L->base-2)->u32.hi);

559. #endif

560. #if LJ_TARGET_X86

561. cts->cb.gpr[2] = 0;

562. #endif

563. if (!ctype_isvoid(ctr->info)) {

564. uint8_t *dp = (uint8_t *)&cts->cb.gpr[0];

565. #if CCALL_NUM_FPR

566. if (ctype_isfp(ctr->info))

567. dp = (uint8_t *)&cts->cb.fpr[0];

568. #endif

569. lj_cconv_ct_tv(cts, ctr, dp, o, 0);

570. #ifdef CALLBACK_HANDLE_RET

571. CALLBACK_HANDLE_RET

572. #endif

573. /* Extend returned integers to (at least) 32 bits. */

574. if (ctype_isinteger_or_bool(ctr->info) && ctr->size < 4) {

575. if (ctr->info & CTF_UNSIGNED)

576. *(uint32_t *)dp = ctr->size == 1 ? (uint32_t)*(uint8_t *)dp :

577. (uint32_t)*(uint16_t *)dp;

578. else

579. *(int32_t *)dp = ctr->size == 1 ? (int32_t)*(int8_t *)dp :

580. (int32_t)*(int16_t *)dp;

581. }

582. #if LJ_TARGET_X86

583. if (ctype_isfp(ctr->info))

584. cts->cb.gpr[2] = ctr->size == sizeof(float) ? 1 : 2;

585. #endif

586. }

587. }

589. /* Enter callback. */

590. ‌lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf)

591. {

592. lua_State *L = cts->L;

593. global_State *g = cts->g;

594. lua_assert(L != NULL);

595. if (tvref(g->jit_base)) {

596. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK));

597. if (g->panic) g->panic(L);

598. exit(EXIT_FAILURE);

599. }

600. lj_trace_abort(g);  /* Never record across callback. */

601. /* Setup C frame. */

602. cframe_prev(cf) = L->cframe;

603. setcframe_L(cf, L);

604. cframe_errfunc(cf) = -1;

605. cframe_nres(cf) = 0;

606. L->cframe = cf;

607. callback_conv_args(cts, L);

608. return L;  /* Now call the function on this stack. */

609. }

611. /* Leave callback. */

612. ‌void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o)

613. {

614. lua_State *L = cts->L;

615. GCfunc *fn;

616. TValue *obase = L->base;

617. L->base = L->top;  /* Keep continuation frame for throwing errors. */

618. if (o >= L->base) {

619. /* PC of RET* is lost. Point to last line for result conv. errors. */

620. fn = curr_func(L);

621. if (isluafunc(fn)) {

622. GCproto *pt = funcproto(fn);

623. setcframe_pc(L->cframe, proto_bc(pt)+pt->sizebc+1);

624. }

625. }

626. callback_conv_result(cts, L, o);

627. /* Finally drop C frame and continuation frame. */

628. L->top -= 2+2*LJ_FR2;

629. L->base = obase;

630. L->cframe = cframe_prev(L->cframe);

631. cts->cb.slot = 0;  /* Blacklist C function that called the callback. */

632. }

634. /* -- C callback management ----------------------------------------------- */

636. /* Get an unused slot in the callback slot table. */

637. ‌static MSize callback_slot_new(CTState *cts, CType *ct)

638. {

639. CTypeID id = ctype_typeid(cts, ct);

640. CTypeID1 *cbid = cts->cb.cbid;

641. MSize top;

642. for (top = cts->cb.topid; top < cts->cb.sizeid; top++)

643. if (LJ_LIKELY(cbid[top] == 0))

644. goto found;

645. #if CALLBACK_MAX_SLOT

646. if (top >= CALLBACK_MAX_SLOT)

647. #endif

648. lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);

649. if (!cts->cb.mcode)

650. callback_mcode_new(cts);

651. lj_mem_growvec(cts->L, cbid, cts->cb.sizeid, CALLBACK_MAX_SLOT, CTypeID1);

652. cts->cb.cbid = cbid;

653. memset(cbid+top, 0, (cts->cb.sizeid-top)*sizeof(CTypeID1));

654. found:

655. cbid[top] = id;

656. cts->cb.topid = top+1;

657. return top;

658. }

660. /* Check for function pointer and supported argument/result types. */

661. ‌static CType *callback_checkfunc(CTState *cts, CType *ct)

662. {

663. int narg = 0;

664. if (!ctype_isptr(ct->info) || (LJ_64 && ct->size != CTSIZE_PTR))

665. return NULL;

666. ct = ctype_rawchild(cts, ct);

667. if (ctype_isfunc(ct->info)) {

668. CType *ctr = ctype_rawchild(cts, ct);

669. CTypeID fid = ct->sib;

670. if (!(ctype_isvoid(ctr->info) || ctype_isenum(ctr->info) ||

671. ctype_isptr(ctr->info) || (ctype_isnum(ctr->info) && ctr->size <= 8)))

672. return NULL;

673. if ((ct->info & CTF_VARARG))

674. return NULL;

675. while (fid) {

676. CType *ctf = ctype_get(cts, fid);

677. if (!ctype_isattrib(ctf->info)) {

678. CType *cta;

679. lua_assert(ctype_isfield(ctf->info));

680. cta = ctype_rawchild(cts, ctf);

681. if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) ||

682. (ctype_isnum(cta->info) && cta->size <= 8)) ||

683. ++narg >= LUA_MINSTACK-3)

684. return NULL;

685. }

686. fid = ctf->sib;

687. }

688. return ct;

689. }

690. return NULL;

691. }

693. /* Create a new callback and return the callback function pointer. */

694. ‌void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn)

695. {

696. ct = callback_checkfunc(cts, ct);

697. if (ct) {

698. MSize slot = callback_slot_new(cts, ct);

699. GCtab *t = cts->miscmap;

700. setfuncV(cts->L, lj_tab_setint(cts->L, t, (int32_t)slot), fn);

701. lj_gc_anybarriert(cts->L, t);

702. return callback_slot2ptr(cts, slot);

703. }

704. return NULL;  /* Bad conversion. */

705. }

707. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_target_arm.h - luajit-2.0-src

Data types defined

- ARMCC

- ARMCC

- ARMIns

- ARMIns

- ARMShift

- ARMShift

- ExitState

Macros defined

- ARMF_CC

- ARMF_D

- ARMF_M

- ARMF_N

- ARMF_RSH

- ARMF_S

- ARMF_SH

- EXITSTATE_CHECKEXIT

- EXITSTATE_PCREG

- EXITSTUBS_PER_GROUP

- EXITSTUB_SPACING

- FPRDEF

- FPRDEF

- GPRDEF

- REGARG_FIRSTFPR

- REGARG_FIRSTFPR

- REGARG_FIRSTGPR

- REGARG_LASTFPR

- REGARG_LASTFPR

- REGARG_LASTGPR

- REGARG_NUMFPR

- REGARG_NUMFPR

- REGARG_NUMGPR

- RIDENUM

- RID_MIN_KREF

- RID_NUM_KREF

- RSET_ALL

- RSET_FPR

- RSET_FPR

- RSET_GPR

- RSET_GPREVEN

- RSET_GPRODD

- RSET_INIT

- RSET_SCRATCH

- RSET_SCRATCH_FPR

- RSET_SCRATCH_FPR

- RSET_SCRATCH_GPR

- RSET_SCRATCH_GPR

- RSET_SCRATCH_GPR_

- SPOFS_TMP

- SPS_FIRST

- SPS_FIXED

- VRIDDEF

- _LJ_TARGET_ARM_H

- sps_align

- sps_scale

Source code

1. /*

2. ** Definitions for ARM CPUs.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_TARGET_ARM_H

7. ‌#define _LJ_TARGET_ARM_H

9. /* -- Registers IDs ------------------------------------------------------- */

11. ‌#define GPRDEF(_) \

12. _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \

13. _(R8) _(R9) _(R10) _(R11) _(R12) _(SP) _(LR) _(PC)

14. #if LJ_SOFTFP

15. ‌#define FPRDEF(_)

16. #else

17. ‌#define FPRDEF(_) \

18. _(D0) _(D1) _(D2) _(D3) _(D4) _(D5) _(D6) _(D7) \

19. _(D8) _(D9) _(D10) _(D11) _(D12) _(D13) _(D14) _(D15)

20. #endif

21. ‌#define VRIDDEF(_)

23. ‌#define RIDENUM(name)        RID_##name,

25. enum {

26. GPRDEF(RIDENUM)                /* General-purpose registers (GPRs). */

27. FPRDEF(RIDENUM)                /* Floating-point registers (FPRs). */

28. RID_MAX,

29. RID_TMP = RID_LR,

31. /* Calling conventions. */

32. RID_RET = RID_R0,

33. RID_RETLO = RID_R0,

34. RID_RETHI = RID_R1,

35. #if LJ_SOFTFP

36. RID_FPRET = RID_R0,

37. #else

38. RID_FPRET = RID_D0,

39. #endif

41. /* These definitions must match with the *.dasc file(s): */

42. RID_BASE = RID_R9,                /* Interpreter BASE. */

43. RID_LPC = RID_R6,                /* Interpreter PC. */

44. RID_DISPATCH = RID_R7,        /* Interpreter DISPATCH table. */

45. RID_LREG = RID_R8,                /* Interpreter L. */

47. /* Register ranges [min, max) and number of registers. */

48. RID_MIN_GPR = RID_R0,

49. RID_MAX_GPR = RID_PC+1,

50. RID_MIN_FPR = RID_MAX_GPR,

51. #if LJ_SOFTFP

52. RID_MAX_FPR = RID_MIN_FPR,

53. #else

54. RID_MAX_FPR = RID_D15+1,

55. #endif

56. RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR,

57. RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR

58. };

60. ‌#define RID_NUM_KREF                RID_NUM_GPR

61. ‌#define RID_MIN_KREF                RID_R0

63. /* -- Register sets ------------------------------------------------------- */

65. /* Make use of all registers, except sp, lr and pc. */

66. ‌#define RSET_GPR                (RSET_RANGE(RID_MIN_GPR, RID_R12+1))

67. ‌#define RSET_GPREVEN \

68. (RID2RSET(RID_R0)|RID2RSET(RID_R2)|RID2RSET(RID_R4)|RID2RSET(RID_R6)| \

69. RID2RSET(RID_R8)|RID2RSET(RID_R10))

70. ‌#define RSET_GPRODD \

71. (RID2RSET(RID_R1)|RID2RSET(RID_R3)|RID2RSET(RID_R5)|RID2RSET(RID_R7)| \

72. RID2RSET(RID_R9)|RID2RSET(RID_R11))

73. #if LJ_SOFTFP

74. ‌#define RSET_FPR                0

75. #else

76. ‌#define RSET_FPR                (RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR))

77. #endif

78. ‌#define RSET_ALL                (RSET_GPR|RSET_FPR)

79. ‌#define RSET_INIT                RSET_ALL

81. /* ABI-specific register sets. lr is an implicit scratch register. */

82. ‌#define RSET_SCRATCH_GPR_        (RSET_RANGE(RID_R0, RID_R3+1)|RID2RSET(RID_R12))

83. #ifdef __APPLE__

84. ‌#define RSET_SCRATCH_GPR        (RSET_SCRATCH_GPR_|RID2RSET(RID_R9))

85. #else

86. ‌#define RSET_SCRATCH_GPR        RSET_SCRATCH_GPR_

87. #endif

88. #if LJ_SOFTFP

89. ‌#define RSET_SCRATCH_FPR        0

90. #else

91. ‌#define RSET_SCRATCH_FPR        (RSET_RANGE(RID_D0, RID_D7+1))

92. #endif

93. ‌#define RSET_SCRATCH                (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR)

94. ‌#define REGARG_FIRSTGPR                RID_R0

95. ‌#define REGARG_LASTGPR                RID_R3

96. ‌#define REGARG_NUMGPR                4

97. #if LJ_ABI_SOFTFP

98. ‌#define REGARG_FIRSTFPR                0

99. ‌#define REGARG_LASTFPR                0

100. ‌#define REGARG_NUMFPR                0

101. #else

102. ‌#define REGARG_FIRSTFPR                RID_D0

103. ‌#define REGARG_LASTFPR                RID_D7

104. ‌#define REGARG_NUMFPR                8

105. #endif

107. /* -- Spill slots --------------------------------------------------------- */

109. /* Spill slots are 32 bit wide. An even/odd pair is used for FPRs.

110. **

111. ** SPS_FIXED: Available fixed spill slots in interpreter frame.

112. ** This definition must match with the *.dasc file(s).

113. **

114. ** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots.

115. */

116. ‌#define SPS_FIXED        2

117. ‌#define SPS_FIRST        2

119. ‌#define SPOFS_TMP        0

121. ‌#define sps_scale(slot)                (4 * (int32_t)(slot))

122. ‌#define sps_align(slot)                (((slot) - SPS_FIXED + 1) & ~1)

124. /* -- Exit state ---------------------------------------------------------- */

126. /* This definition must match with the *.dasc file(s). */

127. typedef struct {

128. #if !LJ_SOFTFP

129. lua_Number fpr[RID_NUM_FPR];        /* Floating-point registers. */

130. #endif

131. int32_t gpr[RID_NUM_GPR];        /* General-purpose registers. */

132. int32_t spill[256];                /* Spill slots. */

133. ‌} ExitState;

135. /* PC after instruction that caused an exit. Used to find the trace number. */

136. ‌#define EXITSTATE_PCREG                RID_PC

137. /* Highest exit + 1 indicates stack check. */

138. ‌#define EXITSTATE_CHECKEXIT        1

140. ‌#define EXITSTUB_SPACING        4

141. ‌#define EXITSTUBS_PER_GROUP     32

143. /* -- Instructions -------------------------------------------------------- */

145. /* Instruction fields. */

146. ‌#define ARMF_CC(ai, cc)        (((ai) ^ ARMI_CCAL) | ((cc) << 28))

147. ‌#define ARMF_N(r)        ((r) << 16)

148. ‌#define ARMF_D(r)        ((r) << 12)

149. ‌#define ARMF_S(r)        ((r) << 8)

150. ‌#define ARMF_M(r)        (r)

151. ‌#define ARMF_SH(sh, n)        (((sh) << 5) | ((n) << 7))

152. ‌#define ARMF_RSH(sh, r)        (0x10 | ((sh) << 5) | ARMF_S(r))

154. ‌typedef enum ARMIns {

155. ARMI_CCAL = 0xe0000000,

156. ARMI_S = 0x000100000,

157. ARMI_K12 = 0x02000000,

158. ARMI_KNEG = 0x00200000,

159. ARMI_LS_W = 0x00200000,

160. ARMI_LS_U = 0x00800000,

161. ARMI_LS_P = 0x01000000,

162. ARMI_LS_R = 0x02000000,

163. ARMI_LSX_I = 0x00400000,

165. ARMI_AND = 0xe0000000,

166. ARMI_EOR = 0xe0200000,

167. ARMI_SUB = 0xe0400000,

168. ARMI_RSB = 0xe0600000,

169. ARMI_ADD = 0xe0800000,

170. ARMI_ADC = 0xe0a00000,

171. ARMI_SBC = 0xe0c00000,

172. ARMI_RSC = 0xe0e00000,

173. ARMI_TST = 0xe1100000,

174. ARMI_TEQ = 0xe1300000,

175. ARMI_CMP = 0xe1500000,

176. ARMI_CMN = 0xe1700000,

177. ARMI_ORR = 0xe1800000,

178. ARMI_MOV = 0xe1a00000,

179. ARMI_BIC = 0xe1c00000,

180. ARMI_MVN = 0xe1e00000,

182. ARMI_NOP = 0xe1a00000,

184. ARMI_MUL = 0xe0000090,

185. ARMI_SMULL = 0xe0c00090,

187. ARMI_LDR = 0xe4100000,

188. ARMI_LDRB = 0xe4500000,

189. ARMI_LDRH = 0xe01000b0,

190. ARMI_LDRSB = 0xe01000d0,

191. ARMI_LDRSH = 0xe01000f0,

192. ARMI_LDRD = 0xe00000d0,

193. ARMI_STR = 0xe4000000,

194. ARMI_STRB = 0xe4400000,

195. ARMI_STRH = 0xe00000b0,

196. ARMI_STRD = 0xe00000f0,

197. ARMI_PUSH = 0xe92d0000,

199. ARMI_B = 0xea000000,

200. ARMI_BL = 0xeb000000,

201. ARMI_BLX = 0xfa000000,

202. ARMI_BLXr = 0xe12fff30,

204. /* ARMv6 */

205. ARMI_REV = 0xe6bf0f30,

206. ARMI_SXTB = 0xe6af0070,

207. ARMI_SXTH = 0xe6bf0070,

208. ARMI_UXTB = 0xe6ef0070,

209. ARMI_UXTH = 0xe6ff0070,

211. /* ARMv6T2 */

212. ARMI_MOVW = 0xe3000000,

213. ARMI_MOVT = 0xe3400000,

215. /* VFP */

216. ARMI_VMOV_D = 0xeeb00b40,

217. ARMI_VMOV_S = 0xeeb00a40,

218. ARMI_VMOVI_D = 0xeeb00b00,

220. ARMI_VMOV_R_S = 0xee100a10,

221. ARMI_VMOV_S_R = 0xee000a10,

222. ARMI_VMOV_RR_D = 0xec500b10,

223. ARMI_VMOV_D_RR = 0xec400b10,

225. ARMI_VADD_D = 0xee300b00,

226. ARMI_VSUB_D = 0xee300b40,

227. ARMI_VMUL_D = 0xee200b00,

228. ARMI_VMLA_D = 0xee000b00,

229. ARMI_VMLS_D = 0xee000b40,

230. ARMI_VNMLS_D = 0xee100b00,

231. ARMI_VDIV_D = 0xee800b00,

233. ARMI_VABS_D = 0xeeb00bc0,

234. ARMI_VNEG_D = 0xeeb10b40,

235. ARMI_VSQRT_D = 0xeeb10bc0,

237. ARMI_VCMP_D = 0xeeb40b40,

238. ARMI_VCMPZ_D = 0xeeb50b40,

240. ARMI_VMRS = 0xeef1fa10,

242. ARMI_VCVT_S32_F32 = 0xeebd0ac0,

243. ARMI_VCVT_S32_F64 = 0xeebd0bc0,

244. ARMI_VCVT_U32_F32 = 0xeebc0ac0,

245. ARMI_VCVT_U32_F64 = 0xeebc0bc0,

246. ARMI_VCVT_F32_S32 = 0xeeb80ac0,

247. ARMI_VCVT_F64_S32 = 0xeeb80bc0,

248. ARMI_VCVT_F32_U32 = 0xeeb80a40,

249. ARMI_VCVT_F64_U32 = 0xeeb80b40,

250. ARMI_VCVT_F32_F64 = 0xeeb70bc0,

251. ARMI_VCVT_F64_F32 = 0xeeb70ac0,

253. ARMI_VLDR_S = 0xed100a00,

254. ARMI_VLDR_D = 0xed100b00,

255. ARMI_VSTR_S = 0xed000a00,

256. ARMI_VSTR_D = 0xed000b00,

257. ‌} ARMIns;

259. ‌typedef enum ARMShift {

260. ARMSH_LSL, ARMSH_LSR, ARMSH_ASR, ARMSH_ROR

261. ‌} ARMShift;

263. /* ARM condition codes. */

264. ‌typedef enum ARMCC {

265. CC_EQ, CC_NE, CC_CS, CC_CC, CC_MI, CC_PL, CC_VS, CC_VC,

266. CC_HI, CC_LS, CC_GE, CC_LT, CC_GT, CC_LE, CC_AL,

267. CC_HS = CC_CS, CC_LO = CC_CC

268. ‌} ARMCC;

270. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_target_x86.h - luajit-2.0-src

Data types defined

- ExitState

- x86Arith

- x86CC

- x86Group

- x86Group3

- x86Group5

- x86ModRM

- x86Mode

- x86Op

- x86Shift

Macros defined

- EXITSTUBS_PER_GROUP

- EXITSTUB_SPACING

- FPRDEF

- FPRDEF

- GPRDEF

- GPRDEF

- REGARG_FIRSTFPR

- REGARG_FIRSTFPR

- REGARG_GPRS

- REGARG_GPRS

- REGARG_GPRS

- REGARG_LASTFPR

- REGARG_LASTFPR

- REGARG_NUMFPR

- REGARG_NUMFPR

- REGARG_NUMFPR

- REGARG_NUMGPR

- REGARG_NUMGPR

- REGARG_NUMGPR

- RIDENUM

- RSET_ACD

- RSET_ALL

- RSET_FPR

- RSET_GPR

- RSET_GPR8

- RSET_GPR8

- RSET_INIT

- RSET_SCRATCH

- RSET_SCRATCH

- RSET_SCRATCH

- SPOFS_TMP

- SPS_FIRST

- SPS_FIRST

- SPS_FIRST

- SPS_FIXED

- SPS_FIXED

- SPS_FIXED

- STACKARG_OFS

- STACKARG_OFS

- STACKARG_OFS

- VRIDDEF

- XG_

- XG_ARITHi

- XG_TOXOi

- XG_TOXOi8

- XO_

- XO_0f

- XO_66

- XO_660f

- XO_ARITH

- XO_ARITHw

- XO_FPU

- XO_f20f

- XO_f30f

- _LJ_TARGET_X86_H

- rset_picktop

- rset_picktop

- sps_align

- sps_scale

Source code

1. /*

2. ** Definitions for x86 and x64 CPUs.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_TARGET_X86_H

7. ‌#define _LJ_TARGET_X86_H

9. /* -- Registers IDs ------------------------------------------------------- */

11. #if LJ_64

12. ‌#define GPRDEF(_) \

13. _(EAX) _(ECX) _(EDX) _(EBX) _(ESP) _(EBP) _(ESI) _(EDI) \

14. _(R8D) _(R9D) _(R10D) _(R11D) _(R12D) _(R13D) _(R14D) _(R15D)

15. ‌#define FPRDEF(_) \

16. _(XMM0) _(XMM1) _(XMM2) _(XMM3) _(XMM4) _(XMM5) _(XMM6) _(XMM7) \

17. _(XMM8) _(XMM9) _(XMM10) _(XMM11) _(XMM12) _(XMM13) _(XMM14) _(XMM15)

18. #else

19. ‌#define GPRDEF(_) \

20. _(EAX) _(ECX) _(EDX) _(EBX) _(ESP) _(EBP) _(ESI) _(EDI)

21. ‌#define FPRDEF(_) \

22. _(XMM0) _(XMM1) _(XMM2) _(XMM3) _(XMM4) _(XMM5) _(XMM6) _(XMM7)

23. #endif

24. ‌#define VRIDDEF(_) \

25. _(MRM)

27. ‌#define RIDENUM(name)        RID_##name,

29. enum {

30. GPRDEF(RIDENUM)                /* General-purpose registers (GPRs). */

31. FPRDEF(RIDENUM)                /* Floating-point registers (FPRs). */

32. RID_MAX,

33. RID_MRM = RID_MAX,                /* Pseudo-id for ModRM operand. */

35. /* Calling conventions. */

36. RID_SP = RID_ESP,

37. RID_RET = RID_EAX,

38. #if LJ_64

39. RID_FPRET = RID_XMM0,

40. #else

41. RID_RETLO = RID_EAX,

42. RID_RETHI = RID_EDX,

43. #endif

45. /* These definitions must match with the *.dasc file(s): */

46. RID_BASE = RID_EDX,                /* Interpreter BASE. */

47. #if LJ_64 && !LJ_ABI_WIN

48. RID_LPC = RID_EBX,                /* Interpreter PC. */

49. RID_DISPATCH = RID_R14D,        /* Interpreter DISPATCH table. */

50. #else

51. RID_LPC = RID_ESI,                /* Interpreter PC. */

52. RID_DISPATCH = RID_EBX,        /* Interpreter DISPATCH table. */

53. #endif

55. /* Register ranges [min, max) and number of registers. */

56. RID_MIN_GPR = RID_EAX,

57. RID_MIN_FPR = RID_XMM0,

58. RID_MAX_GPR = RID_MIN_FPR,

59. RID_MAX_FPR = RID_MAX,

60. RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR,

61. RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR,

62. };

64. /* -- Register sets ------------------------------------------------------- */

66. /* Make use of all registers, except the stack pointer. */

67. ‌#define RSET_GPR        (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR)-RID2RSET(RID_ESP))

68. ‌#define RSET_FPR        (RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR))

69. ‌#define RSET_ALL        (RSET_GPR|RSET_FPR)

70. ‌#define RSET_INIT        RSET_ALL

72. #if LJ_64

73. /* Note: this requires the use of FORCE_REX! */

74. ‌#define RSET_GPR8        RSET_GPR

75. #else

76. ‌#define RSET_GPR8        (RSET_RANGE(RID_EAX, RID_EBX+1))

77. #endif

79. /* ABI-specific register sets. */

80. ‌#define RSET_ACD        (RID2RSET(RID_EAX)|RID2RSET(RID_ECX)|RID2RSET(RID_EDX))

81. #if LJ_64

82. #if LJ_ABI_WIN

83. /* Windows x64 ABI. */

84. ‌#define RSET_SCRATCH \

85. (RSET_ACD|RSET_RANGE(RID_R8D, RID_R11D+1)|RSET_RANGE(RID_XMM0, RID_XMM5+1))

86. ‌#define REGARG_GPRS \

87. (RID_ECX|((RID_EDX|((RID_R8D|(RID_R9D<<5))<<5))<<5))

88. ‌#define REGARG_NUMGPR        4

89. ‌#define REGARG_NUMFPR        4

90. ‌#define REGARG_FIRSTFPR        RID_XMM0

91. ‌#define REGARG_LASTFPR        RID_XMM3

92. ‌#define STACKARG_OFS        (4*8)

93. #else

94. /* The rest of the civilized x64 world has a common ABI. */

95. ‌#define RSET_SCRATCH \

96. (RSET_ACD|RSET_RANGE(RID_ESI, RID_R11D+1)|RSET_FPR)

97. ‌#define REGARG_GPRS \

98. (RID_EDI|((RID_ESI|((RID_EDX|((RID_ECX|((RID_R8D|(RID_R9D \

99. <<5))<<5))<<5))<<5))<<5))

100. ‌#define REGARG_NUMGPR        6

101. ‌#define REGARG_NUMFPR        8

102. ‌#define REGARG_FIRSTFPR        RID_XMM0

103. ‌#define REGARG_LASTFPR        RID_XMM7

104. ‌#define STACKARG_OFS        0

105. #endif

106. #else

107. /* Common x86 ABI. */

108. ‌#define RSET_SCRATCH        (RSET_ACD|RSET_FPR)

109. ‌#define REGARG_GPRS        (RID_ECX|(RID_EDX<<5))  /* Fastcall only. */

110. ‌#define REGARG_NUMGPR        2  /* Fastcall only. */

111. ‌#define REGARG_NUMFPR        0

112. ‌#define STACKARG_OFS        0

113. #endif

115. #if LJ_64

116. /* Prefer the low 8 regs of each type to reduce REX prefixes. */

117. ‌#undef rset_picktop

118. ‌#define rset_picktop(rs)        (lj_fls(lj_bswap(rs)) ^ 0x18)

119. #endif

121. /* -- Spill slots --------------------------------------------------------- */

123. /* Spill slots are 32 bit wide. An even/odd pair is used for FPRs.

124. **

125. ** SPS_FIXED: Available fixed spill slots in interpreter frame.

126. ** This definition must match with the *.dasc file(s).

127. **

128. ** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots.

129. */

130. #if LJ_64

131. #if LJ_ABI_WIN

132. ‌#define SPS_FIXED        (4*2)

133. ‌#define SPS_FIRST        (4*2)        /* Don't use callee register save area. */

134. #else

135. ‌#define SPS_FIXED        4

136. ‌#define SPS_FIRST        2

137. #endif

138. #else

139. ‌#define SPS_FIXED        6

140. ‌#define SPS_FIRST        2

141. #endif

143. ‌#define SPOFS_TMP        0

145. ‌#define sps_scale(slot)                (4 * (int32_t)(slot))

146. ‌#define sps_align(slot)                (((slot) - SPS_FIXED + 3) & ~3)

148. /* -- Exit state ---------------------------------------------------------- */

150. /* This definition must match with the *.dasc file(s). */

151. typedef struct {

152. lua_Number fpr[RID_NUM_FPR];        /* Floating-point registers. */

153. intptr_t gpr[RID_NUM_GPR];        /* General-purpose registers. */

154. int32_t spill[256];                /* Spill slots. */

155. ‌} ExitState;

157. /* Limited by the range of a short fwd jump (127): (2+2)*(32-1)-2 = 122. */

158. ‌#define EXITSTUB_SPACING        (2+2)

159. ‌#define EXITSTUBS_PER_GROUP        32

161. /* -- x86 ModRM operand encoding ------------------------------------------ */

163. typedef enum {

164. XM_OFS0 = 0x00, XM_OFS8 = 0x40, XM_OFS32 = 0x80, XM_REG = 0xc0,

165. XM_SCALE1 = 0x00, XM_SCALE2 = 0x40, XM_SCALE4 = 0x80, XM_SCALE8 = 0xc0,

166. XM_MASK = 0xc0

167. ‌} x86Mode;

169. /* Structure to hold variable ModRM operand. */

170. typedef struct {

171. int32_t ofs;                /* Offset. */

172. uint8_t base;                /* Base register or RID_NONE. */

173. uint8_t idx;                /* Index register or RID_NONE. */

174. uint8_t scale;        /* Index scale (XM_SCALE1 .. XM_SCALE8). */

175. ‌} x86ModRM;

177. /* -- Opcodes ------------------------------------------------------------- */

179. /* Macros to construct variable-length x86 opcodes. -(len+1) is in LSB. */

180. ‌#define XO_(o)                ((uint32_t)(0x0000fe + (0x##o<<24)))

181. ‌#define XO_FPU(a,b)        ((uint32_t)(0x00fd + (0x##a<<16)+(0x##b<<24)))

182. ‌#define XO_0f(o)        ((uint32_t)(0x0f00fd + (0x##o<<24)))

183. ‌#define XO_66(o)        ((uint32_t)(0x6600fd + (0x##o<<24)))

184. ‌#define XO_660f(o)        ((uint32_t)(0x0f66fc + (0x##o<<24)))

185. ‌#define XO_f20f(o)        ((uint32_t)(0x0ff2fc + (0x##o<<24)))

186. ‌#define XO_f30f(o)        ((uint32_t)(0x0ff3fc + (0x##o<<24)))

188. /* This list of x86 opcodes is not intended to be complete. Opcodes are only

189. ** included when needed. Take a look at DynASM or jit.dis_x86 to see the

190. ** whole mess.

191. */

192. typedef enum {

193. /* Fixed length opcodes. XI_* prefix. */

194. XI_NOP =        0x90,

195. XI_XCHGa =        0x90,

196. XI_CALL =        0xe8,

197. XI_JMP =        0xe9,

198. XI_JMPs =        0xeb,

199. XI_PUSH =        0x50, /* Really 50+r. */

200. XI_JCCs =        0x70, /* Really 7x. */

201. XI_JCCn =        0x80, /* Really 0f8x. */

202. XI_LEA =        0x8d,

203. XI_MOVrib =        0xb0, /* Really b0+r. */

204. XI_MOVri =        0xb8, /* Really b8+r. */

205. XI_ARITHib =        0x80,

206. XI_ARITHi =        0x81,

207. XI_ARITHi8 =        0x83,

208. XI_PUSHi8 =        0x6a,

209. XI_TESTb =        0x84,

210. XI_TEST =        0x85,

211. XI_MOVmi =        0xc7,

212. XI_GROUP5 =        0xff,

214. /* Note: little-endian byte-order! */

215. XI_FLDZ =        0xeed9,

216. XI_FLD1 =        0xe8d9,

217. XI_FLDLG2 =        0xecd9,

218. XI_FLDLN2 =        0xedd9,

219. XI_FDUP =        0xc0d9,  /* Really fld st0. */

220. XI_FPOP =        0xd8dd,  /* Really fstp st0. */

221. XI_FPOP1 =        0xd9dd,  /* Really fstp st1. */

222. XI_FRNDINT =        0xfcd9,

223. XI_FSIN =        0xfed9,

224. XI_FCOS =        0xffd9,

225. XI_FPTAN =        0xf2d9,

226. XI_FPATAN =        0xf3d9,

227. XI_FSCALE =        0xfdd9,

228. XI_FYL2X =        0xf1d9,

230. /* Variable-length opcodes. XO_* prefix. */

231. XO_MOV =        XO_(8b),

232. XO_MOVto =        XO_(89),

233. XO_MOVtow =        XO_66(89),

234. XO_MOVtob =        XO_(88),

235. XO_MOVmi =        XO_(c7),

236. XO_MOVmib =        XO_(c6),

237. XO_LEA =        XO_(8d),

238. XO_ARITHib =        XO_(80),

239. XO_ARITHi =        XO_(81),

240. XO_ARITHi8 =        XO_(83),

241. XO_ARITHiw8 =        XO_66(83),

242. XO_SHIFTi =        XO_(c1),

243. XO_SHIFT1 =        XO_(d1),

244. XO_SHIFTcl =        XO_(d3),

245. XO_IMUL =        XO_0f(af),

246. XO_IMULi =        XO_(69),

247. XO_IMULi8 =        XO_(6b),

248. XO_CMP =        XO_(3b),

249. XO_TESTb =        XO_(84),

250. XO_TEST =        XO_(85),

251. XO_GROUP3b =        XO_(f6),

252. XO_GROUP3 =        XO_(f7),

253. XO_GROUP5b =        XO_(fe),

254. XO_GROUP5 =        XO_(ff),

255. XO_MOVZXb =        XO_0f(b6),

256. XO_MOVZXw =        XO_0f(b7),

257. XO_MOVSXb =        XO_0f(be),

258. XO_MOVSXw =        XO_0f(bf),

259. XO_MOVSXd =        XO_(63),

260. XO_BSWAP =        XO_0f(c8),

261. XO_CMOV =        XO_0f(40),

263. XO_MOVSD =        XO_f20f(10),

264. XO_MOVSDto =        XO_f20f(11),

265. XO_MOVSS =        XO_f30f(10),

266. XO_MOVSSto =        XO_f30f(11),

267. XO_MOVLPD =        XO_660f(12),

268. XO_MOVAPS =        XO_0f(28),

269. XO_XORPS =        XO_0f(57),

270. XO_ANDPS =        XO_0f(54),

271. XO_ADDSD =        XO_f20f(58),

272. XO_SUBSD =        XO_f20f(5c),

273. XO_MULSD =        XO_f20f(59),

274. XO_DIVSD =        XO_f20f(5e),

275. XO_SQRTSD =        XO_f20f(51),

276. XO_MINSD =        XO_f20f(5d),

277. XO_MAXSD =        XO_f20f(5f),

278. XO_ROUNDSD =        0x0b3a0ffc,  /* Really 66 0f 3a 0b. See asm_fpmath. */

279. XO_UCOMISD =        XO_660f(2e),

280. XO_CVTSI2SD =        XO_f20f(2a),

281. XO_CVTTSD2SI=        XO_f20f(2c),

282. XO_CVTSI2SS =        XO_f30f(2a),

283. XO_CVTTSS2SI=        XO_f30f(2c),

284. XO_CVTSS2SD =        XO_f30f(5a),

285. XO_CVTSD2SS =        XO_f20f(5a),

286. XO_ADDSS =        XO_f30f(58),

287. XO_MOVD =        XO_660f(6e),

288. XO_MOVDto =        XO_660f(7e),

290. XO_FLDd =        XO_(d9), XOg_FLDd = 0,

291. XO_FLDq =        XO_(dd), XOg_FLDq = 0,

292. XO_FILDd =        XO_(db), XOg_FILDd = 0,

293. XO_FILDq =        XO_(df), XOg_FILDq = 5,

294. XO_FSTPd =        XO_(d9), XOg_FSTPd = 3,

295. XO_FSTPq =        XO_(dd), XOg_FSTPq = 3,

296. XO_FISTPq =        XO_(df), XOg_FISTPq = 7,

297. XO_FISTTPq =        XO_(dd), XOg_FISTTPq = 1,

298. XO_FADDq =        XO_(dc), XOg_FADDq = 0,

299. XO_FLDCW =        XO_(d9), XOg_FLDCW = 5,

300. XO_FNSTCW =        XO_(d9), XOg_FNSTCW = 7

301. ‌} x86Op;

303. /* x86 opcode groups. */

304. ‌typedef uint32_t x86Group;

306. ‌#define XG_(i8, i, g)        ((x86Group)(((i8) << 16) + ((i) << 8) + (g)))

307. ‌#define XG_ARITHi(g)        XG_(XI_ARITHi8, XI_ARITHi, g)

308. ‌#define XG_TOXOi(xg)        ((x86Op)(0x000000fe + (((xg)<<16) & 0xff000000)))

309. ‌#define XG_TOXOi8(xg)        ((x86Op)(0x000000fe + (((xg)<<8) & 0xff000000)))

311. ‌#define XO_ARITH(a)        ((x86Op)(0x030000fe + ((a)<<27)))

312. ‌#define XO_ARITHw(a)        ((x86Op)(0x036600fd + ((a)<<27)))

314. typedef enum {

315. XOg_ADD, XOg_OR, XOg_ADC, XOg_SBB, XOg_AND, XOg_SUB, XOg_XOR, XOg_CMP,

316. XOg_X_IMUL

317. ‌} x86Arith;

319. typedef enum {

320. XOg_ROL, XOg_ROR, XOg_RCL, XOg_RCR, XOg_SHL, XOg_SHR, XOg_SAL, XOg_SAR

321. ‌} x86Shift;

323. typedef enum {

324. XOg_TEST, XOg_TEST_, XOg_NOT, XOg_NEG, XOg_MUL, XOg_IMUL, XOg_DIV, XOg_IDIV

325. ‌} x86Group3;

327. typedef enum {

328. XOg_INC, XOg_DEC, XOg_CALL, XOg_CALLfar, XOg_JMP, XOg_JMPfar, XOg_PUSH

329. ‌} x86Group5;

331. /* x86 condition codes. */

332. typedef enum {

333. CC_O, CC_NO, CC_B, CC_NB, CC_E, CC_NE, CC_BE, CC_NBE,

334. CC_S, CC_NS, CC_P, CC_NP, CC_L, CC_NL, CC_LE, CC_NLE,

335. CC_C = CC_B, CC_NAE = CC_C, CC_NC = CC_NB, CC_AE = CC_NB,

336. CC_Z = CC_E, CC_NZ = CC_NE, CC_NA = CC_BE, CC_A = CC_NBE,

337. CC_PE = CC_P, CC_PO = CC_NP, CC_NGE = CC_L, CC_GE = CC_NL,

338. CC_NG = CC_LE, CC_G = CC_NLE

339. ‌} x86CC;

341. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_target.h - luajit-2.0-src

Data types defined

- Reg

- RegCost

- RegSP

- RegSet

- RegSet

Functions defined

- exitstub_addr_

Macros defined

- REGCOST

- REGCOST_PHI_WEIGHT

- REGCOST_REF_T

- REGCOST_T

- REGSP

- REGSP_HINT

- REGSP_INIT

- RID2RSET

- RID_INIT

- RID_MASK

- RID_NONE

- RID_SINK

- RID_SUNK

- RSET_EMPTY

- RSET_RANGE

- SPS_NONE

- _LJ_TARGET_H

- exitstub_addr

- ra_gethint

- ra_hashint

- ra_hasreg

- ra_hasspill

- ra_noreg

- ra_samehint

- ra_sethint

- regcost_ref

- regsp_reg

- regsp_spill

- regsp_used

- rset_clear

- rset_exclude

- rset_pickbot

- rset_pickbot

- rset_picktop

- rset_picktop

- rset_set

- rset_test

Source code

1. /*

2. ** Definitions for target CPU.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_TARGET_H

7. ‌#define _LJ_TARGET_H

9. #include "lj_def.h"

10. #include "lj_arch.h"

12. /* -- Registers and spill slots ------------------------------------------- */

14. /* Register type (uint8_t in ir->r). */

15. ‌typedef uint32_t Reg;

17. /* The hi-bit is NOT set for an allocated register. This means the value

18. ** can be directly used without masking. The hi-bit is set for a register

19. ** allocation hint or for RID_INIT, RID_SINK or RID_SUNK.

20. */

21. ‌#define RID_NONE                0x80

22. ‌#define RID_MASK                0x7f

23. ‌#define RID_INIT                (RID_NONE|RID_MASK)

24. ‌#define RID_SINK                (RID_INIT-1)

25. ‌#define RID_SUNK                (RID_INIT-2)

27. ‌#define ra_noreg(r)                ((r) & RID_NONE)

28. ‌#define ra_hasreg(r)                (!((r) & RID_NONE))

30. /* The ra_hashint() macro assumes a previous test for ra_noreg(). */

31. ‌#define ra_hashint(r)                ((r) < RID_SUNK)

32. ‌#define ra_gethint(r)                ((Reg)((r) & RID_MASK))

33. ‌#define ra_sethint(rr, r)        rr = (uint8_t)((r)|RID_NONE)

34. ‌#define ra_samehint(r1, r2)        (ra_gethint((r1)^(r2)) == 0)

36. /* Spill slot 0 means no spill slot has been allocated. */

37. ‌#define SPS_NONE                0

39. ‌#define ra_hasspill(s)                ((s) != SPS_NONE)

41. /* Combined register and spill slot (uint16_t in ir->prev). */

42. ‌typedef uint32_t RegSP;

44. ‌#define REGSP(r, s)                ((r) + ((s) << 8))

45. ‌#define REGSP_HINT(r)                ((r)|RID_NONE)

46. ‌#define REGSP_INIT                REGSP(RID_INIT, 0)

48. ‌#define regsp_reg(rs)                ((rs) & 255)

49. ‌#define regsp_spill(rs)                ((rs) >> 8)

50. ‌#define regsp_used(rs) \

51. (((rs) & ~REGSP(RID_MASK, 0)) != REGSP(RID_NONE, 0))

53. /* -- Register sets ------------------------------------------------------- */

55. /* Bitset for registers. 32 registers suffice for most architectures.

56. ** Note that one set holds bits for both GPRs and FPRs.

57. */

58. #if LJ_TARGET_PPC || LJ_TARGET_MIPS

59. ‌typedef uint64_t RegSet;

60. #else

61. ‌typedef uint32_t RegSet;

62. #endif

64. ‌#define RID2RSET(r)                (((RegSet)1) << (r))

65. ‌#define RSET_EMPTY                ((RegSet)0)

66. ‌#define RSET_RANGE(lo, hi)        ((RID2RSET((hi)-(lo))-1) << (lo))

68. ‌#define rset_test(rs, r)        ((int)((rs) >> (r)) & 1)

69. ‌#define rset_set(rs, r)                (rs |= RID2RSET(r))

70. ‌#define rset_clear(rs, r)        (rs &= ~RID2RSET(r))

71. ‌#define rset_exclude(rs, r)        (rs & ~RID2RSET(r))

72. #if LJ_TARGET_PPC || LJ_TARGET_MIPS

73. ‌#define rset_picktop(rs)        ((Reg)(__builtin_clzll(rs)^63))

74. ‌#define rset_pickbot(rs)        ((Reg)__builtin_ctzll(rs))

75. #else

76. ‌#define rset_picktop(rs)        ((Reg)lj_fls(rs))

77. ‌#define rset_pickbot(rs)        ((Reg)lj_ffs(rs))

78. #endif

80. /* -- Register allocation cost -------------------------------------------- */

82. /* The register allocation heuristic keeps track of the cost for allocating

83. ** a specific register:

84. **

85. ** A free register (obviously) has a cost of 0 and a 1-bit in the free mask.

86. **

87. ** An already allocated register has the (non-zero) IR reference in the lowest

88. ** bits and the result of a blended cost-model in the higher bits.

89. **

90. ** The allocator first checks the free mask for a hit. Otherwise an (unrolled)

91. ** linear search for the minimum cost is used. The search doesn't need to

92. ** keep track of the position of the minimum, which makes it very fast.

93. ** The lowest bits of the minimum cost show the desired IR reference whose

94. ** register is the one to evict.

95. **

96. ** Without the cost-model this degenerates to the standard heuristics for

97. ** (reverse) linear-scan register allocation. Since code generation is done

98. ** in reverse, a live interval extends from the last use to the first def.

99. ** For an SSA IR the IR reference is the first (and only) def and thus

100. ** trivially marks the end of the interval. The LSRA heuristics says to pick

101. ** the register whose live interval has the furthest extent, i.e. the lowest

102. ** IR reference in our case.

103. **

104. ** A cost-model should take into account other factors, like spill-cost and

105. ** restore- or rematerialization-cost, which depend on the kind of instruction.

106. ** E.g. constants have zero spill costs, variant instructions have higher

107. ** costs than invariants and PHIs should preferably never be spilled.

108. **

109. ** Here's a first cut at simple, but effective blended cost-model for R-LSRA:

110. ** - Due to careful design of the IR, constants already have lower IR

111. **   references than invariants and invariants have lower IR references

112. **   than variants.

113. ** - The cost in the upper 16 bits is the sum of the IR reference and a

114. **   weighted score. The score currently only takes into account whether

115. **   the IRT_ISPHI bit is set in the instruction type.

116. ** - The PHI weight is the minimum distance (in IR instructions) a PHI

117. **   reference has to be further apart from a non-PHI reference to be spilled.

118. ** - It should be a power of two (for speed) and must be between 2 and 32768.

119. **   Good values for the PHI weight seem to be between 40 and 150.

120. ** - Further study is required.

121. */

122. ‌#define REGCOST_PHI_WEIGHT        64

124. /* Cost for allocating a specific register. */

125. ‌typedef uint32_t RegCost;

127. /* Note: assumes 16 bit IRRef1. */

128. ‌#define REGCOST(cost, ref)        ((RegCost)(ref) + ((RegCost)(cost) << 16))

129. ‌#define regcost_ref(rc)                ((IRRef1)(rc))

131. ‌#define REGCOST_T(t) \

132. ((RegCost)((t)&IRT_ISPHI) * (((RegCost)(REGCOST_PHI_WEIGHT)<<16)/IRT_ISPHI))

133. ‌#define REGCOST_REF_T(ref, t)        (REGCOST((ref), (ref)) + REGCOST_T((t)))

135. /* -- Target-specific definitions ----------------------------------------- */

137. #if LJ_TARGET_X86ORX64

138. #include "lj_target_x86.h"

139. #elif LJ_TARGET_ARM

140. #include "lj_target_arm.h"

141. #elif LJ_TARGET_ARM64

142. #include "lj_target_arm64.h"

143. #elif LJ_TARGET_PPC

144. #include "lj_target_ppc.h"

145. #elif LJ_TARGET_MIPS

146. #include "lj_target_mips.h"

147. #else

148. #error "Missing include for target CPU"

149. #endif

151. #ifdef EXITSTUBS_PER_GROUP

152. /* Return the address of an exit stub. */

153. ‌static LJ_AINLINE char *exitstub_addr_(char **group, uint32_t exitno)

154. {

155. lua_assert(group[exitno / EXITSTUBS_PER_GROUP] != NULL);

156. return (char *)group[exitno / EXITSTUBS_PER_GROUP] +

157. EXITSTUB_SPACING*(exitno % EXITSTUBS_PER_GROUP);

158. }

159. /* Avoid dependence on lj_jit.h if only including lj_target.h. */

160. ‌#define exitstub_addr(J, exitno) \

161. ((MCode *)exitstub_addr_((char **)((J)->exitstubgroup), (exitno)))

162. #endif

164. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_asm.c - luajit-2.0-src

Global variables defined

- field_ofs

- ra_dbg_buf

- ra_dbg_mcp

- ra_dbg_merge

- ra_dbg_p

- ra_regname

Data types defined

- ASMState

- ASMState

Functions defined

- asm_baseslot

- asm_bufhdr

- asm_bufput

- asm_bufstr

- asm_call

- asm_callid

- asm_callx_flags

- asm_collectargs

- asm_conv64

- asm_fpjoin_pow

- asm_fppow

- asm_gcstep

- asm_head_root

- asm_head_side

- asm_ir

- asm_loop

- asm_lref

- asm_mclimit

- asm_newref

- asm_phi

- asm_phi_break

- asm_phi_copyspill

- asm_phi_fixup

- asm_phi_shuffle

- asm_setup_regsp

- asm_snap_alloc

- asm_snap_alloc1

- asm_snap_canremat

- asm_snap_checkrename

- asm_snap_prep

- asm_snew

- asm_stack_adjust

- asm_sunk_store

- asm_tail_link

- asm_tdup

- asm_tnew

- asm_tostr

- checkmclim

- ir_khash

- lj_asm_trace

- ra_alloc1

- ra_allock

- ra_allockreg

- ra_allocref

- ra_dest

- ra_destpair

- ra_destreg

- ra_dflush

- ra_dprintf

- ra_dstart

- ra_evict

- ra_evictk

- ra_evictset

- ra_left

- ra_leftov

- ra_pick

- ra_releasetmp

- ra_rematk

- ra_rename

- ra_restore

- ra_save

- ra_scratch

- ra_setkref

- ra_setup

- ra_spill

Macros defined

- ASMREF_L

- ASMREF_TMP1

- ASMREF_TMP2

- FLOFS

- FLOFS

- FUSE_DISABLED

- IR

- IR

- LUA_CORE

- MCLIM_REDZONE

- MINCOST

- RA_DBGX

- RA_DBGX

- RA_DBG_FLUSH

- RA_DBG_FLUSH

- RA_DBG_REF

- RA_DBG_REF

- RA_DBG_START

- RA_DBG_START

- RIDNAME

- RIDNAME

- canfuse

- emit_spload

- emit_spstore

- iscrossref

- lj_asm_c

- mayfuse

- neverfuse

- opisfusableload

- ra_allockreg

- ra_free

- ra_iskref

- ra_iskref

- ra_krefk

- ra_krefk

- ra_krefreg

- ra_krefreg

- ra_modified

- ra_noweak

- ra_used

- ra_weak

Source code

1. /*

2. ** IR assembler (SSA IR -> machine code).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_asm_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT

13. #include "lj_gc.h"

14. #include "lj_str.h"

15. #include "lj_tab.h"

16. #include "lj_frame.h"

17. #if LJ_HASFFI

18. #include "lj_ctype.h"

19. #endif

20. #include "lj_ir.h"

21. #include "lj_jit.h"

22. #include "lj_ircall.h"

23. #include "lj_iropt.h"

24. #include "lj_mcode.h"

25. #include "lj_iropt.h"

26. #include "lj_trace.h"

27. #include "lj_snap.h"

28. #include "lj_asm.h"

29. #include "lj_dispatch.h"

30. #include "lj_vm.h"

31. #include "lj_target.h"

33. #ifdef LUA_USE_ASSERT

34. #include <stdio.h>

35. #endif

37. /* -- Assembler state and common macros ----------------------------------- */

39. /* Assembler state. */

40. ‌typedef struct ASMState {

41. RegCost cost[RID_MAX];  /* Reference and blended allocation cost for regs. */

43. MCode *mcp;                /* Current MCode pointer (grows down). */

44. MCode *mclim;                /* Lower limit for MCode memory + red zone. */

45. #ifdef LUA_USE_ASSERT

46. MCode *mcp_prev;        /* Red zone overflow check. */

47. #endif

49. IRIns *ir;                /* Copy of pointer to IR instructions/constants. */

50. jit_State *J;                /* JIT compiler state. */

52. #if LJ_TARGET_X86ORX64

53. x86ModRM mrm;                /* Fused x86 address operand. */

54. #endif

56. RegSet freeset;        /* Set of free registers. */

57. RegSet modset;        /* Set of registers modified inside the loop. */

58. RegSet weakset;        /* Set of weakly referenced registers. */

59. RegSet phiset;        /* Set of PHI registers. */

61. uint32_t flags;        /* Copy of JIT compiler flags. */

62. int loopinv;                /* Loop branch inversion (0:no, 1:yes, 2:yes+CC_P). */

64. int32_t evenspill;        /* Next even spill slot. */

65. int32_t oddspill;        /* Next odd spill slot (or 0). */

67. IRRef curins;                /* Reference of current instruction. */

68. IRRef stopins;        /* Stop assembly before hitting this instruction. */

69. IRRef orignins;        /* Original T->nins. */

71. IRRef snapref;        /* Current snapshot is active after this reference. */

72. IRRef snaprename;        /* Rename highwater mark for snapshot check. */

73. SnapNo snapno;        /* Current snapshot number. */

74. SnapNo loopsnapno;        /* Loop snapshot number. */

76. IRRef fuseref;        /* Fusion limit (loopref, 0 or FUSE_DISABLED). */

77. IRRef sectref;        /* Section base reference (loopref or 0). */

78. IRRef loopref;        /* Reference of LOOP instruction (or 0). */

80. BCReg topslot;        /* Number of slots for stack check (unless 0). */

81. int32_t gcsteps;        /* Accumulated number of GC steps (per section). */

83. GCtrace *T;                /* Trace to assemble. */

84. GCtrace *parent;        /* Parent trace (or NULL). */

86. MCode *mcbot;                /* Bottom of reserved MCode. */

87. MCode *mctop;                /* Top of generated MCode. */

88. MCode *mcloop;        /* Pointer to loop MCode (or NULL). */

89. MCode *invmcp;        /* Points to invertible loop branch (or NULL). */

90. MCode *flagmcp;        /* Pending opportunity to merge flag setting ins. */

91. MCode *realign;        /* Realign loop if not NULL. */

93. #ifdef RID_NUM_KREF

94. int32_t krefk[RID_NUM_KREF];

95. #endif

96. IRRef1 phireg[RID_MAX];  /* PHI register references. */

97. uint16_t parentmap[LJ_MAX_JSLOTS];  /* Parent instruction to RegSP map. */

98. ‌} ASMState;

100. ‌#define IR(ref)                        (&as->ir[(ref)])

102. ‌#define ASMREF_TMP1                REF_TRUE        /* Temp. register. */

103. ‌#define ASMREF_TMP2                REF_FALSE        /* Temp. register. */

104. ‌#define ASMREF_L                REF_NIL                /* Stores register for L. */

106. /* Check for variant to invariant references. */

107. ‌#define iscrossref(as, ref)        ((ref) < as->sectref)

109. /* Inhibit memory op fusion from variant to invariant references. */

110. ‌#define FUSE_DISABLED                (~(IRRef)0)

111. ‌#define mayfuse(as, ref)        ((ref) > as->fuseref)

112. ‌#define neverfuse(as)                (as->fuseref == FUSE_DISABLED)

113. ‌#define canfuse(as, ir)                (!neverfuse(as) && !irt_isphi((ir)->t))

114. ‌#define opisfusableload(o) \

115. ((o) == IR_ALOAD || (o) == IR_HLOAD || (o) == IR_ULOAD || \

116. (o) == IR_FLOAD || (o) == IR_XLOAD || (o) == IR_SLOAD || (o) == IR_VLOAD)

118. /* Sparse limit checks using a red zone before the actual limit. */

119. ‌#define MCLIM_REDZONE        64

121. ‌static LJ_NORET LJ_NOINLINE void asm_mclimit(ASMState *as)

122. {

123. lj_mcode_limiterr(as->J, (size_t)(as->mctop - as->mcp + 4*MCLIM_REDZONE));

124. }

126. ‌static LJ_AINLINE void checkmclim(ASMState *as)

127. {

128. #ifdef LUA_USE_ASSERT

129. if (as->mcp + MCLIM_REDZONE < as->mcp_prev) {

130. IRIns *ir = IR(as->curins+1);

131. fprintf(stderr, "RED ZONE OVERFLOW: %p IR %04d  %02d %04d %04d\n", as->mcp,

132. as->curins+1-REF_BIAS, ir->o, ir->op1-REF_BIAS, ir->op2-REF_BIAS);

133. lua_assert(0);

134. }

135. #endif

136. if (LJ_UNLIKELY(as->mcp < as->mclim)) asm_mclimit(as);

137. #ifdef LUA_USE_ASSERT

138. as->mcp_prev = as->mcp;

139. #endif

140. }

142. #ifdef RID_NUM_KREF

143. ‌#define ra_iskref(ref)                ((ref) < RID_NUM_KREF)

144. ‌#define ra_krefreg(ref)                ((Reg)(RID_MIN_KREF + (Reg)(ref)))

145. ‌#define ra_krefk(as, ref)        (as->krefk[(ref)])

147. ‌static LJ_AINLINE void ra_setkref(ASMState *as, Reg r, int32_t k)

148. {

149. IRRef ref = (IRRef)(r - RID_MIN_KREF);

150. as->krefk[ref] = k;

151. as->cost[r] = REGCOST(ref, ref);

152. }

154. #else

155. ‌#define ra_iskref(ref)                0

156. ‌#define ra_krefreg(ref)                RID_MIN_GPR

157. ‌#define ra_krefk(as, ref)        0

158. #endif

160. /* Arch-specific field offsets. */

161. ‌static const uint8_t field_ofs[IRFL__MAX+1] = {

162. ‌#define FLOFS(name, ofs)        (uint8_t)(ofs),

163. IRFLDEF(FLOFS)

164. ‌#undef FLOFS

165. 0

166. };

168. /* -- Target-specific instruction emitter --------------------------------- */

170. #if LJ_TARGET_X86ORX64

171. #include "lj_emit_x86.h"

172. #elif LJ_TARGET_ARM

173. #include "lj_emit_arm.h"

174. #elif LJ_TARGET_PPC

175. #include "lj_emit_ppc.h"

176. #elif LJ_TARGET_MIPS

177. #include "lj_emit_mips.h"

178. #else

179. #error "Missing instruction emitter for target CPU"

180. #endif

182. /* Generic load/store of register from/to stack slot. */

183. ‌#define emit_spload(as, ir, r, ofs) \

184. emit_loadofs(as, ir, (r), RID_SP, (ofs))

185. ‌#define emit_spstore(as, ir, r, ofs) \

186. emit_storeofs(as, ir, (r), RID_SP, (ofs))

188. /* -- Register allocator debugging ---------------------------------------- */

190. /* #define LUAJIT_DEBUG_RA */

192. #ifdef LUAJIT_DEBUG_RA

194. #include <stdio.h>

195. #include <stdarg.h>

197. ‌#define RIDNAME(name)        #name,

198. ‌static const char *const ra_regname[] = {

199. GPRDEF(RIDNAME)

200. FPRDEF(RIDNAME)

201. VRIDDEF(RIDNAME)

202. NULL

203. };

204. ‌#undef RIDNAME

206. ‌static char ra_dbg_buf[65536];

207. ‌static char *ra_dbg_p;

208. ‌static char *ra_dbg_merge;

209. ‌static MCode *ra_dbg_mcp;

211. ‌static void ra_dstart(void)

212. {

213. ra_dbg_p = ra_dbg_buf;

214. ra_dbg_merge = NULL;

215. ra_dbg_mcp = NULL;

216. }

218. ‌static void ra_dflush(void)

219. {

220. fwrite(ra_dbg_buf, 1, (size_t)(ra_dbg_p-ra_dbg_buf), stdout);

221. ra_dstart();

222. }

224. ‌static void ra_dprintf(ASMState *as, const char *fmt, ...)

225. {

226. char *p;

227. va_list argp;

228. va_start(argp, fmt);

229. p = ra_dbg_mcp == as->mcp ? ra_dbg_merge : ra_dbg_p;

230. ra_dbg_mcp = NULL;

231. p += sprintf(p, "%08x  \e[36m%04d ", (uintptr_t)as->mcp, as->curins-REF_BIAS);

232. for (;;) {

233. const char *e = strchr(fmt, '$');

234. if (e == NULL) break;

235. memcpy(p, fmt, (size_t)(e-fmt));

236. p += e-fmt;

237. if (e[1] == 'r') {

238. Reg r = va_arg(argp, Reg) & RID_MASK;

239. if (r <= RID_MAX) {

240. const char *q;

241. for (q = ra_regname[r]; *q; q++)

242. *p++ = *q >= 'A' && *q <= 'Z' ? *q + 0x20 : *q;

243. } else {

244. *p++ = '?';

245. lua_assert(0);

246. }

247. } else if (e[1] == 'f' || e[1] == 'i') {

248. IRRef ref;

249. if (e[1] == 'f')

250. ref = va_arg(argp, IRRef);

251. else

252. ref = va_arg(argp, IRIns *) - as->ir;

253. if (ref >= REF_BIAS)

254. p += sprintf(p, "%04d", ref - REF_BIAS);

255. else

256. p += sprintf(p, "K%03d", REF_BIAS - ref);

257. } else if (e[1] == 's') {

258. uint32_t slot = va_arg(argp, uint32_t);

259. p += sprintf(p, "[sp+0x%x]", sps_scale(slot));

260. } else if (e[1] == 'x') {

261. p += sprintf(p, "%08x", va_arg(argp, int32_t));

262. } else {

263. lua_assert(0);

264. }

265. fmt = e+2;

266. }

267. va_end(argp);

268. while (*fmt)

269. *p++ = *fmt++;

270. *p++ = '\e'; *p++ = '['; *p++ = 'm'; *p++ = '\n';

271. if (p > ra_dbg_buf+sizeof(ra_dbg_buf)-256) {

272. fwrite(ra_dbg_buf, 1, (size_t)(p-ra_dbg_buf), stdout);

273. p = ra_dbg_buf;

274. }

275. ra_dbg_p = p;

276. }

278. ‌#define RA_DBG_START()        ra_dstart()

279. ‌#define RA_DBG_FLUSH()        ra_dflush()

280. ‌#define RA_DBG_REF() \

281. do { char *_p = ra_dbg_p; ra_dprintf(as, ""); \

282. ra_dbg_merge = _p; ra_dbg_mcp = as->mcp; } while (0)

283. ‌#define RA_DBGX(x)        ra_dprintf x

285. #else

286. ‌#define RA_DBG_START()        ((void)0)

287. ‌#define RA_DBG_FLUSH()        ((void)0)

288. ‌#define RA_DBG_REF()        ((void)0)

289. ‌#define RA_DBGX(x)        ((void)0)

290. #endif

292. /* -- Register allocator -------------------------------------------------- */

294. ‌#define ra_free(as, r)                rset_set(as->freeset, (r))

295. ‌#define ra_modified(as, r)        rset_set(as->modset, (r))

296. ‌#define ra_weak(as, r)                rset_set(as->weakset, (r))

297. ‌#define ra_noweak(as, r)        rset_clear(as->weakset, (r))

299. ‌#define ra_used(ir)                (ra_hasreg((ir)->r) || ra_hasspill((ir)->s))

301. /* Setup register allocator. */

302. ‌static void ra_setup(ASMState *as)

303. {

304. Reg r;

305. /* Initially all regs (except the stack pointer) are free for use. */

306. as->freeset = RSET_INIT;

307. as->modset = RSET_EMPTY;

308. as->weakset = RSET_EMPTY;

309. as->phiset = RSET_EMPTY;

310. memset(as->phireg, 0, sizeof(as->phireg));

311. for (r = RID_MIN_GPR; r < RID_MAX; r++)

312. as->cost[r] = REGCOST(~0u, 0u);

313. }

315. /* Rematerialize constants. */

316. ‌static Reg ra_rematk(ASMState *as, IRRef ref)

317. {

318. IRIns *ir;

319. Reg r;

320. if (ra_iskref(ref)) {

321. r = ra_krefreg(ref);

322. lua_assert(!rset_test(as->freeset, r));

323. ra_free(as, r);

324. ra_modified(as, r);

325. emit_loadi(as, r, ra_krefk(as, ref));

326. return r;

327. }

328. ir = IR(ref);

329. r = ir->r;

330. lua_assert(ra_hasreg(r) && !ra_hasspill(ir->s));

331. ra_free(as, r);

332. ra_modified(as, r);

333. ir->r = RID_INIT;  /* Do not keep any hint. */

334. RA_DBGX((as, "remat     $i $r", ir, r));

335. #if !LJ_SOFTFP

336. if (ir->o == IR_KNUM) {

337. emit_loadn(as, r, ir_knum(ir));

338. } else

339. #endif

340. if (emit_canremat(REF_BASE) && ir->o == IR_BASE) {

341. ra_sethint(ir->r, RID_BASE);  /* Restore BASE register hint. */

342. emit_getgl(as, r, jit_base);

343. } else if (emit_canremat(ASMREF_L) && ir->o == IR_KPRI) {

344. lua_assert(irt_isnil(ir->t));  /* REF_NIL stores ASMREF_L register. */

345. emit_getgl(as, r, cur_L);

346. #if LJ_64

347. } else if (ir->o == IR_KINT64) {

348. emit_loadu64(as, r, ir_kint64(ir)->u64);

349. #endif

350. } else {

351. lua_assert(ir->o == IR_KINT || ir->o == IR_KGC ||

352. ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL);

353. emit_loadi(as, r, ir->i);

354. }

355. return r;

356. }

358. /* Force a spill. Allocate a new spill slot if needed. */

359. ‌static int32_t ra_spill(ASMState *as, IRIns *ir)

360. {

361. int32_t slot = ir->s;

362. lua_assert(ir >= as->ir + REF_TRUE);

363. if (!ra_hasspill(slot)) {

364. if (irt_is64(ir->t)) {

365. slot = as->evenspill;

366. as->evenspill += 2;

367. } else if (as->oddspill) {

368. slot = as->oddspill;

369. as->oddspill = 0;

370. } else {

371. slot = as->evenspill;

372. as->oddspill = slot+1;

373. as->evenspill += 2;

374. }

375. if (as->evenspill > 256)

376. lj_trace_err(as->J, LJ_TRERR_SPILLOV);

377. ir->s = (uint8_t)slot;

378. }

379. return sps_scale(slot);

380. }

382. /* Release the temporarily allocated register in ASMREF_TMP1/ASMREF_TMP2. */

383. ‌static Reg ra_releasetmp(ASMState *as, IRRef ref)

384. {

385. IRIns *ir = IR(ref);

386. Reg r = ir->r;

387. lua_assert(ra_hasreg(r) && !ra_hasspill(ir->s));

388. ra_free(as, r);

389. ra_modified(as, r);

390. ir->r = RID_INIT;

391. return r;

392. }

394. /* Restore a register (marked as free). Rematerialize or force a spill. */

395. ‌static Reg ra_restore(ASMState *as, IRRef ref)

396. {

397. if (emit_canremat(ref)) {

398. return ra_rematk(as, ref);

399. } else {

400. IRIns *ir = IR(ref);

401. int32_t ofs = ra_spill(as, ir);  /* Force a spill slot. */

402. Reg r = ir->r;

403. lua_assert(ra_hasreg(r));

404. ra_sethint(ir->r, r);  /* Keep hint. */

405. ra_free(as, r);

406. if (!rset_test(as->weakset, r)) {  /* Only restore non-weak references. */

407. ra_modified(as, r);

408. RA_DBGX((as, "restore   $i $r", ir, r));

409. emit_spload(as, ir, r, ofs);

410. }

411. return r;

412. }

413. }

415. /* Save a register to a spill slot. */

416. ‌static void ra_save(ASMState *as, IRIns *ir, Reg r)

417. {

418. RA_DBGX((as, "save      $i $r", ir, r));

419. emit_spstore(as, ir, r, sps_scale(ir->s));

420. }

422. ‌#define MINCOST(name) \

423. if (rset_test(RSET_ALL, RID_##name) && \

424. LJ_LIKELY(allow&RID2RSET(RID_##name)) && as->cost[RID_##name] < cost) \

425. cost = as->cost[RID_##name];

427. /* Evict the register with the lowest cost, forcing a restore. */

428. ‌static Reg ra_evict(ASMState *as, RegSet allow)

429. {

430. IRRef ref;

431. RegCost cost = ~(RegCost)0;

432. lua_assert(allow != RSET_EMPTY);

433. if (RID_NUM_FPR == 0 || allow < RID2RSET(RID_MAX_GPR)) {

434. GPRDEF(MINCOST)

435. } else {

436. FPRDEF(MINCOST)

437. }

438. ref = regcost_ref(cost);

439. lua_assert(ra_iskref(ref) || (ref >= as->T->nk && ref < as->T->nins));

440. /* Preferably pick any weak ref instead of a non-weak, non-const ref. */

441. if (!irref_isk(ref) && (as->weakset & allow)) {

442. IRIns *ir = IR(ref);

443. if (!rset_test(as->weakset, ir->r))

444. ref = regcost_ref(as->cost[rset_pickbot((as->weakset & allow))]);

445. }

446. return ra_restore(as, ref);

447. }

449. /* Pick any register (marked as free). Evict on-demand. */

450. ‌static Reg ra_pick(ASMState *as, RegSet allow)

451. {

452. RegSet pick = as->freeset & allow;

453. if (!pick)

454. return ra_evict(as, allow);

455. else

456. return rset_picktop(pick);

457. }

459. /* Get a scratch register (marked as free). */

460. ‌static Reg ra_scratch(ASMState *as, RegSet allow)

461. {

462. Reg r = ra_pick(as, allow);

463. ra_modified(as, r);

464. RA_DBGX((as, "scratch        $r", r));

465. return r;

466. }

468. /* Evict all registers from a set (if not free). */

469. ‌static void ra_evictset(ASMState *as, RegSet drop)

470. {

471. RegSet work;

472. as->modset |= drop;

473. #if !LJ_SOFTFP

474. work = (drop & ~as->freeset) & RSET_FPR;

475. while (work) {

476. Reg r = rset_pickbot(work);

477. ra_restore(as, regcost_ref(as->cost[r]));

478. rset_clear(work, r);

479. checkmclim(as);

480. }

481. #endif

482. work = (drop & ~as->freeset);

483. while (work) {

484. Reg r = rset_pickbot(work);

485. ra_restore(as, regcost_ref(as->cost[r]));

486. rset_clear(work, r);

487. checkmclim(as);

488. }

489. }

491. /* Evict (rematerialize) all registers allocated to constants. */

492. ‌static void ra_evictk(ASMState *as)

493. {

494. RegSet work;

495. #if !LJ_SOFTFP

496. work = ~as->freeset & RSET_FPR;

497. while (work) {

498. Reg r = rset_pickbot(work);

499. IRRef ref = regcost_ref(as->cost[r]);

500. if (emit_canremat(ref) && irref_isk(ref)) {

501. ra_rematk(as, ref);

502. checkmclim(as);

503. }

504. rset_clear(work, r);

505. }

506. #endif

507. work = ~as->freeset & RSET_GPR;

508. while (work) {

509. Reg r = rset_pickbot(work);

510. IRRef ref = regcost_ref(as->cost[r]);

511. if (emit_canremat(ref) && irref_isk(ref)) {

512. ra_rematk(as, ref);

513. checkmclim(as);

514. }

515. rset_clear(work, r);

516. }

517. }

519. #ifdef RID_NUM_KREF

520. /* Allocate a register for a constant. */

521. ‌static Reg ra_allock(ASMState *as, int32_t k, RegSet allow)

522. {

523. /* First try to find a register which already holds the same constant. */

524. RegSet pick, work = ~as->freeset & RSET_GPR;

525. Reg r;

526. while (work) {

527. IRRef ref;

528. r = rset_pickbot(work);

529. ref = regcost_ref(as->cost[r]);

530. if (ref < ASMREF_L &&

531. k == (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i))

532. return r;

533. rset_clear(work, r);

534. }

535. pick = as->freeset & allow;

536. if (pick) {

537. /* Constants should preferably get unmodified registers. */

538. if ((pick & ~as->modset))

539. pick &= ~as->modset;

540. r = rset_pickbot(pick);  /* Reduce conflicts with inverse allocation. */

541. } else {

542. r = ra_evict(as, allow);

543. }

544. RA_DBGX((as, "allock    $x $r", k, r));

545. ra_setkref(as, r, k);

546. rset_clear(as->freeset, r);

547. ra_noweak(as, r);

548. return r;

549. }

551. /* Allocate a specific register for a constant. */

552. ‌static void ra_allockreg(ASMState *as, int32_t k, Reg r)

553. {

554. Reg kr = ra_allock(as, k, RID2RSET(r));

555. if (kr != r) {

556. IRIns irdummy;

557. irdummy.t.irt = IRT_INT;

558. ra_scratch(as, RID2RSET(r));

559. emit_movrr(as, &irdummy, r, kr);

560. }

561. }

562. #else

563. ‌#define ra_allockreg(as, k, r)                emit_loadi(as, (r), (k))

564. #endif

566. /* Allocate a register for ref from the allowed set of registers.

567. ** Note: this function assumes the ref does NOT have a register yet!

568. ** Picks an optimal register, sets the cost and marks the register as non-free.

569. */

570. ‌static Reg ra_allocref(ASMState *as, IRRef ref, RegSet allow)

571. {

572. IRIns *ir = IR(ref);

573. RegSet pick = as->freeset & allow;

574. Reg r;

575. lua_assert(ra_noreg(ir->r));

576. if (pick) {

577. /* First check register hint from propagation or PHI. */

578. if (ra_hashint(ir->r)) {

579. r = ra_gethint(ir->r);

580. if (rset_test(pick, r))  /* Use hint register if possible. */

581. goto found;

582. /* Rematerialization is cheaper than missing a hint. */

583. if (rset_test(allow, r) && emit_canremat(regcost_ref(as->cost[r]))) {

584. ra_rematk(as, regcost_ref(as->cost[r]));

585. goto found;

586. }

587. RA_DBGX((as, "hintmiss  $f $r", ref, r));

588. }

589. /* Invariants should preferably get unmodified registers. */

590. if (ref < as->loopref && !irt_isphi(ir->t)) {

591. if ((pick & ~as->modset))

592. pick &= ~as->modset;

593. r = rset_pickbot(pick);  /* Reduce conflicts with inverse allocation. */

594. } else {

595. /* We've got plenty of regs, so get callee-save regs if possible. */

596. if (RID_NUM_GPR > 8 && (pick & ~RSET_SCRATCH))

597. pick &= ~RSET_SCRATCH;

598. r = rset_picktop(pick);

599. }

600. } else {

601. r = ra_evict(as, allow);

602. }

603. found:

604. RA_DBGX((as, "alloc     $f $r", ref, r));

605. ir->r = (uint8_t)r;

606. rset_clear(as->freeset, r);

607. ra_noweak(as, r);

608. as->cost[r] = REGCOST_REF_T(ref, irt_t(ir->t));

609. return r;

610. }

612. /* Allocate a register on-demand. */

613. ‌static Reg ra_alloc1(ASMState *as, IRRef ref, RegSet allow)

614. {

615. Reg r = IR(ref)->r;

616. /* Note: allow is ignored if the register is already allocated. */

617. if (ra_noreg(r)) r = ra_allocref(as, ref, allow);

618. ra_noweak(as, r);

619. return r;

620. }

622. /* Rename register allocation and emit move. */

623. ‌static void ra_rename(ASMState *as, Reg down, Reg up)

624. {

625. IRRef ren, ref = regcost_ref(as->cost[up] = as->cost[down]);

626. IRIns *ir = IR(ref);

627. ir->r = (uint8_t)up;

628. as->cost[down] = 0;

629. lua_assert((down < RID_MAX_GPR) == (up < RID_MAX_GPR));

630. lua_assert(!rset_test(as->freeset, down) && rset_test(as->freeset, up));

631. ra_free(as, down);  /* 'down' is free ... */

632. ra_modified(as, down);

633. rset_clear(as->freeset, up);  /* ... and 'up' is now allocated. */

634. ra_noweak(as, up);

635. RA_DBGX((as, "rename    $f $r $r", regcost_ref(as->cost[up]), down, up));

636. emit_movrr(as, ir, down, up);  /* Backwards codegen needs inverse move. */

637. if (!ra_hasspill(IR(ref)->s)) {  /* Add the rename to the IR. */

638. lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), ref, as->snapno);

639. ren = tref_ref(lj_ir_emit(as->J));

640. as->ir = as->T->ir;  /* The IR may have been reallocated. */

641. IR(ren)->r = (uint8_t)down;

642. IR(ren)->s = SPS_NONE;

643. }

644. }

646. /* Pick a destination register (marked as free).

647. ** Caveat: allow is ignored if there's already a destination register.

648. ** Use ra_destreg() to get a specific register.

649. */

650. ‌static Reg ra_dest(ASMState *as, IRIns *ir, RegSet allow)

651. {

652. Reg dest = ir->r;

653. if (ra_hasreg(dest)) {

654. ra_free(as, dest);

655. ra_modified(as, dest);

656. } else {

657. if (ra_hashint(dest) && rset_test((as->freeset&allow), ra_gethint(dest))) {

658. dest = ra_gethint(dest);

659. ra_modified(as, dest);

660. RA_DBGX((as, "dest           $r", dest));

661. } else {

662. dest = ra_scratch(as, allow);

663. }

664. ir->r = dest;

665. }

666. if (LJ_UNLIKELY(ra_hasspill(ir->s))) ra_save(as, ir, dest);

667. return dest;

668. }

670. /* Force a specific destination register (marked as free). */

671. ‌static void ra_destreg(ASMState *as, IRIns *ir, Reg r)

672. {

673. Reg dest = ra_dest(as, ir, RID2RSET(r));

674. if (dest != r) {

675. lua_assert(rset_test(as->freeset, r));

676. ra_modified(as, r);

677. emit_movrr(as, ir, dest, r);

678. }

679. }

681. #if LJ_TARGET_X86ORX64

682. /* Propagate dest register to left reference. Emit moves as needed.

683. ** This is a required fixup step for all 2-operand machine instructions.

684. */

685. ‌static void ra_left(ASMState *as, Reg dest, IRRef lref)

686. {

687. IRIns *ir = IR(lref);

688. Reg left = ir->r;

689. if (ra_noreg(left)) {

690. if (irref_isk(lref)) {

691. if (ir->o == IR_KNUM) {

692. cTValue *tv = ir_knum(ir);

693. /* FP remat needs a load except for +0. Still better than eviction. */

694. if (tvispzero(tv) || !(as->freeset & RSET_FPR)) {

695. emit_loadn(as, dest, tv);

696. return;

697. }

698. #if LJ_64

699. } else if (ir->o == IR_KINT64) {

700. emit_loadu64(as, dest, ir_kint64(ir)->u64);

701. return;

702. #endif

703. } else if (ir->o != IR_KPRI) {

704. lua_assert(ir->o == IR_KINT || ir->o == IR_KGC ||

705. ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL);

706. emit_loadi(as, dest, ir->i);

707. return;

708. }

709. }

710. if (!ra_hashint(left) && !iscrossref(as, lref))

711. ra_sethint(ir->r, dest);  /* Propagate register hint. */

712. left = ra_allocref(as, lref, dest < RID_MAX_GPR ? RSET_GPR : RSET_FPR);

713. }

714. ra_noweak(as, left);

715. /* Move needed for true 3-operand instruction: y=a+b ==> y=a; y+=b. */

716. if (dest != left) {

717. /* Use register renaming if dest is the PHI reg. */

718. if (irt_isphi(ir->t) && as->phireg[dest] == lref) {

719. ra_modified(as, left);

720. ra_rename(as, left, dest);

721. } else {

722. emit_movrr(as, ir, dest, left);

723. }

724. }

725. }

726. #else

727. /* Similar to ra_left, except we override any hints. */

728. ‌static void ra_leftov(ASMState *as, Reg dest, IRRef lref)

729. {

730. IRIns *ir = IR(lref);

731. Reg left = ir->r;

732. if (ra_noreg(left)) {

733. ra_sethint(ir->r, dest);  /* Propagate register hint. */

734. left = ra_allocref(as, lref,

735. (LJ_SOFTFP || dest < RID_MAX_GPR) ? RSET_GPR : RSET_FPR);

736. }

737. ra_noweak(as, left);

738. if (dest != left) {

739. /* Use register renaming if dest is the PHI reg. */

740. if (irt_isphi(ir->t) && as->phireg[dest] == lref) {

741. ra_modified(as, left);

742. ra_rename(as, left, dest);

743. } else {

744. emit_movrr(as, ir, dest, left);

745. }

746. }

747. }

748. #endif

750. #if !LJ_64

751. /* Force a RID_RETLO/RID_RETHI destination register pair (marked as free). */

752. ‌static void ra_destpair(ASMState *as, IRIns *ir)

753. {

754. Reg destlo = ir->r, desthi = (ir+1)->r;

755. /* First spill unrelated refs blocking the destination registers. */

756. if (!rset_test(as->freeset, RID_RETLO) &&

757. destlo != RID_RETLO && desthi != RID_RETLO)

758. ra_restore(as, regcost_ref(as->cost[RID_RETLO]));

759. if (!rset_test(as->freeset, RID_RETHI) &&

760. destlo != RID_RETHI && desthi != RID_RETHI)

761. ra_restore(as, regcost_ref(as->cost[RID_RETHI]));

762. /* Next free the destination registers (if any). */

763. if (ra_hasreg(destlo)) {

764. ra_free(as, destlo);

765. ra_modified(as, destlo);

766. } else {

767. destlo = RID_RETLO;

768. }

769. if (ra_hasreg(desthi)) {

770. ra_free(as, desthi);

771. ra_modified(as, desthi);

772. } else {

773. desthi = RID_RETHI;

774. }

775. /* Check for conflicts and shuffle the registers as needed. */

776. if (destlo == RID_RETHI) {

777. if (desthi == RID_RETLO) {

778. #if LJ_TARGET_X86

779. *--as->mcp = XI_XCHGa + RID_RETHI;

780. #else

781. emit_movrr(as, ir, RID_RETHI, RID_TMP);

782. emit_movrr(as, ir, RID_RETLO, RID_RETHI);

783. emit_movrr(as, ir, RID_TMP, RID_RETLO);

784. #endif

785. } else {

786. emit_movrr(as, ir, RID_RETHI, RID_RETLO);

787. if (desthi != RID_RETHI) emit_movrr(as, ir, desthi, RID_RETHI);

788. }

789. } else if (desthi == RID_RETLO) {

790. emit_movrr(as, ir, RID_RETLO, RID_RETHI);

791. if (destlo != RID_RETLO) emit_movrr(as, ir, destlo, RID_RETLO);

792. } else {

793. if (desthi != RID_RETHI) emit_movrr(as, ir, desthi, RID_RETHI);

794. if (destlo != RID_RETLO) emit_movrr(as, ir, destlo, RID_RETLO);

795. }

796. /* Restore spill slots (if any). */

797. if (ra_hasspill((ir+1)->s)) ra_save(as, ir+1, RID_RETHI);

798. if (ra_hasspill(ir->s)) ra_save(as, ir, RID_RETLO);

799. }

800. #endif

802. /* -- Snapshot handling --------- ----------------------------------------- */

804. /* Can we rematerialize a KNUM instead of forcing a spill? */

805. ‌static int asm_snap_canremat(ASMState *as)

806. {

807. Reg r;

808. for (r = RID_MIN_FPR; r < RID_MAX_FPR; r++)

809. if (irref_isk(regcost_ref(as->cost[r])))

810. return 1;

811. return 0;

812. }

814. /* Check whether a sunk store corresponds to an allocation. */

815. ‌static int asm_sunk_store(ASMState *as, IRIns *ira, IRIns *irs)

816. {

817. if (irs->s == 255) {

818. if (irs->o == IR_ASTORE || irs->o == IR_HSTORE ||

819. irs->o == IR_FSTORE || irs->o == IR_XSTORE) {

820. IRIns *irk = IR(irs->op1);

821. if (irk->o == IR_AREF || irk->o == IR_HREFK)

822. irk = IR(irk->op1);

823. return (IR(irk->op1) == ira);

824. }

825. return 0;

826. } else {

827. return (ira + irs->s == irs);  /* Quick check. */

828. }

829. }

831. /* Allocate register or spill slot for a ref that escapes to a snapshot. */

832. ‌static void asm_snap_alloc1(ASMState *as, IRRef ref)

833. {

834. IRIns *ir = IR(ref);

835. if (!irref_isk(ref) && (!(ra_used(ir) || ir->r == RID_SUNK))) {

836. if (ir->r == RID_SINK) {

837. ir->r = RID_SUNK;

838. #if LJ_HASFFI

839. if (ir->o == IR_CNEWI) {  /* Allocate CNEWI value. */

840. asm_snap_alloc1(as, ir->op2);

841. if (LJ_32 && (ir+1)->o == IR_HIOP)

842. asm_snap_alloc1(as, (ir+1)->op2);

843. } else

844. #endif

845. {  /* Allocate stored values for TNEW, TDUP and CNEW. */

846. IRIns *irs;

847. lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW);

848. for (irs = IR(as->snapref-1); irs > ir; irs--)

849. if (irs->r == RID_SINK && asm_sunk_store(as, ir, irs)) {

850. lua_assert(irs->o == IR_ASTORE || irs->o == IR_HSTORE ||

851. irs->o == IR_FSTORE || irs->o == IR_XSTORE);

852. asm_snap_alloc1(as, irs->op2);

853. if (LJ_32 && (irs+1)->o == IR_HIOP)

854. asm_snap_alloc1(as, (irs+1)->op2);

855. }

856. }

857. } else {

858. RegSet allow;

859. if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT) {

860. IRIns *irc;

861. for (irc = IR(as->curins); irc > ir; irc--)

862. if ((irc->op1 == ref || irc->op2 == ref) &&

863. !(irc->r == RID_SINK || irc->r == RID_SUNK))

864. goto nosink;  /* Don't sink conversion if result is used. */

865. asm_snap_alloc1(as, ir->op1);

866. return;

867. }

868. nosink:

869. allow = (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR;

870. if ((as->freeset & allow) ||

871. (allow == RSET_FPR && asm_snap_canremat(as))) {

872. /* Get a weak register if we have a free one or can rematerialize. */

873. Reg r = ra_allocref(as, ref, allow);  /* Allocate a register. */

874. if (!irt_isphi(ir->t))

875. ra_weak(as, r);  /* But mark it as weakly referenced. */

876. checkmclim(as);

877. RA_DBGX((as, "snapreg   $f $r", ref, ir->r));

878. } else {

879. ra_spill(as, ir);  /* Otherwise force a spill slot. */

880. RA_DBGX((as, "snapspill $f $s", ref, ir->s));

881. }

882. }

883. }

884. }

886. /* Allocate refs escaping to a snapshot. */

887. ‌static void asm_snap_alloc(ASMState *as)

888. {

889. SnapShot *snap = &as->T->snap[as->snapno];

890. SnapEntry *map = &as->T->snapmap[snap->mapofs];

891. MSize n, nent = snap->nent;

892. for (n = 0; n < nent; n++) {

893. SnapEntry sn = map[n];

894. IRRef ref = snap_ref(sn);

895. if (!irref_isk(ref)) {

896. asm_snap_alloc1(as, ref);

897. if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) {

898. lua_assert(irt_type(IR(ref+1)->t) == IRT_SOFTFP);

899. asm_snap_alloc1(as, ref+1);

900. }

901. }

902. }

903. }

905. /* All guards for a snapshot use the same exitno. This is currently the

906. ** same as the snapshot number. Since the exact origin of the exit cannot

907. ** be determined, all guards for the same snapshot must exit with the same

908. ** RegSP mapping.

909. ** A renamed ref which has been used in a prior guard for the same snapshot

910. ** would cause an inconsistency. The easy way out is to force a spill slot.

911. */

912. ‌static int asm_snap_checkrename(ASMState *as, IRRef ren)

913. {

914. SnapShot *snap = &as->T->snap[as->snapno];

915. SnapEntry *map = &as->T->snapmap[snap->mapofs];

916. MSize n, nent = snap->nent;

917. for (n = 0; n < nent; n++) {

918. SnapEntry sn = map[n];

919. IRRef ref = snap_ref(sn);

920. if (ref == ren || (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && ++ref == ren)) {

921. IRIns *ir = IR(ref);

922. ra_spill(as, ir);  /* Register renamed, so force a spill slot. */

923. RA_DBGX((as, "snaprensp $f $s", ref, ir->s));

924. return 1;  /* Found. */

925. }

926. }

927. return 0;  /* Not found. */

928. }

930. /* Prepare snapshot for next guard instruction. */

931. ‌static void asm_snap_prep(ASMState *as)

932. {

933. if (as->curins < as->snapref) {

934. do {

935. if (as->snapno == 0) return;  /* Called by sunk stores before snap #0. */

936. as->snapno--;

937. as->snapref = as->T->snap[as->snapno].ref;

938. } while (as->curins < as->snapref);

939. asm_snap_alloc(as);

940. as->snaprename = as->T->nins;

941. } else {

942. /* Process any renames above the highwater mark. */

943. for (; as->snaprename < as->T->nins; as->snaprename++) {

944. IRIns *ir = IR(as->snaprename);

945. if (asm_snap_checkrename(as, ir->op1))

946. ir->op2 = REF_BIAS-1;  /* Kill rename. */

947. }

948. }

949. }

951. /* -- Miscellaneous helpers ----------------------------------------------- */

953. /* Calculate stack adjustment. */

954. ‌static int32_t asm_stack_adjust(ASMState *as)

955. {

956. if (as->evenspill <= SPS_FIXED)

957. return 0;

958. return sps_scale(sps_align(as->evenspill));

959. }

961. /* Must match with hash*() in lj_tab.c. */

962. ‌static uint32_t ir_khash(IRIns *ir)

963. {

964. uint32_t lo, hi;

965. if (irt_isstr(ir->t)) {

966. return ir_kstr(ir)->hash;

967. } else if (irt_isnum(ir->t)) {

968. lo = ir_knum(ir)->u32.lo;

969. hi = ir_knum(ir)->u32.hi << 1;

970. } else if (irt_ispri(ir->t)) {

971. lua_assert(!irt_isnil(ir->t));

972. return irt_type(ir->t)-IRT_FALSE;

973. } else {

974. lua_assert(irt_isgcv(ir->t));

975. lo = u32ptr(ir_kgc(ir));

976. hi = lo + HASH_BIAS;

977. }

978. return hashrot(lo, hi);

979. }

981. /* -- Allocations --------------------------------------------------------- */

983. static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args);

984. static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci);

986. ‌static void asm_snew(ASMState *as, IRIns *ir)

987. {

988. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_new];

989. IRRef args[3];

990. args[0] = ASMREF_L;  /* lua_State *L    */

991. args[1] = ir->op1;   /* const char *str */

992. args[2] = ir->op2;   /* size_t len      */

993. as->gcsteps++;

994. asm_setupresult(as, ir, ci);  /* GCstr * */

995. asm_gencall(as, ci, args);

996. }

998. ‌static void asm_tnew(ASMState *as, IRIns *ir)

999. {

1000. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_new1];

1001. IRRef args[2];

1002. args[0] = ASMREF_L;     /* lua_State *L    */

1003. args[1] = ASMREF_TMP1;  /* uint32_t ahsize */

1004. as->gcsteps++;

1005. asm_setupresult(as, ir, ci);  /* GCtab * */

1006. asm_gencall(as, ci, args);

1007. ra_allockreg(as, ir->op1 | (ir->op2 << 24), ra_releasetmp(as, ASMREF_TMP1));

1008. }

1010. ‌static void asm_tdup(ASMState *as, IRIns *ir)

1011. {

1012. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_dup];

1013. IRRef args[2];

1014. args[0] = ASMREF_L;  /* lua_State *L    */

1015. args[1] = ir->op1;   /* const GCtab *kt */

1016. as->gcsteps++;

1017. asm_setupresult(as, ir, ci);  /* GCtab * */

1018. asm_gencall(as, ci, args);

1019. }

1021. static void asm_gc_check(ASMState *as);

1023. /* Explicit GC step. */

1024. ‌static void asm_gcstep(ASMState *as, IRIns *ir)

1025. {

1026. IRIns *ira;

1027. for (ira = IR(as->stopins+1); ira < ir; ira++)

1028. if ((ira->o == IR_TNEW || ira->o == IR_TDUP ||

1029. (LJ_HASFFI && (ira->o == IR_CNEW || ira->o == IR_CNEWI))) &&

1030. ra_used(ira))

1031. as->gcsteps++;

1032. if (as->gcsteps)

1033. asm_gc_check(as);

1034. as->gcsteps = 0x80000000;  /* Prevent implicit GC check further up. */

1035. }

1037. /* -- Buffer operations --------------------------------------------------- */

1039. static void asm_tvptr(ASMState *as, Reg dest, IRRef ref);

1041. ‌static void asm_bufhdr(ASMState *as, IRIns *ir)

1042. {

1043. Reg sb = ra_dest(as, ir, RSET_GPR);

1044. if ((ir->op2 & IRBUFHDR_APPEND)) {

1045. /* Rematerialize const buffer pointer instead of likely spill. */

1046. IRIns *irp = IR(ir->op1);

1047. if (!(ra_hasreg(irp->r) || irp == ir-1 ||

1048. (irp == ir-2 && !ra_used(ir-1)))) {

1049. while (!(irp->o == IR_BUFHDR && !(irp->op2 & IRBUFHDR_APPEND)))

1050. irp = IR(irp->op1);

1051. if (irref_isk(irp->op1)) {

1052. ra_weak(as, ra_allocref(as, ir->op1, RSET_GPR));

1053. ir = irp;

1054. }

1055. }

1056. } else {

1057. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb));

1058. /* Passing ir isn't strictly correct, but it's an IRT_P32, too. */

1059. emit_storeofs(as, ir, tmp, sb, offsetof(SBuf, p));

1060. emit_loadofs(as, ir, tmp, sb, offsetof(SBuf, b));

1061. }

1062. #if LJ_TARGET_X86ORX64

1063. ra_left(as, sb, ir->op1);

1064. #else

1065. ra_leftov(as, sb, ir->op1);

1066. #endif

1067. }

1069. ‌static void asm_bufput(ASMState *as, IRIns *ir)

1070. {

1071. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_buf_putstr];

1072. IRRef args[3];

1073. IRIns *irs;

1074. int kchar = -1;

1075. args[0] = ir->op1;  /* SBuf * */

1076. args[1] = ir->op2;  /* GCstr * */

1077. irs = IR(ir->op2);

1078. lua_assert(irt_isstr(irs->t));

1079. if (irs->o == IR_KGC) {

1080. GCstr *s = ir_kstr(irs);

1081. if (s->len == 1) {  /* Optimize put of single-char string constant. */

1082. kchar = strdata(s)[0];

1083. args[1] = ASMREF_TMP1;  /* int, truncated to char */

1084. ci = &lj_ir_callinfo[IRCALL_lj_buf_putchar];

1085. }

1086. } else if (mayfuse(as, ir->op2) && ra_noreg(irs->r)) {

1087. if (irs->o == IR_TOSTR) {  /* Fuse number to string conversions. */

1088. if (irs->op2 == IRTOSTR_NUM) {

1089. args[1] = ASMREF_TMP1;  /* TValue * */

1090. ci = &lj_ir_callinfo[IRCALL_lj_strfmt_putnum];

1091. } else {

1092. lua_assert(irt_isinteger(IR(irs->op1)->t));

1093. args[1] = irs->op1;  /* int */

1094. if (irs->op2 == IRTOSTR_INT)

1095. ci = &lj_ir_callinfo[IRCALL_lj_strfmt_putint];

1096. else

1097. ci = &lj_ir_callinfo[IRCALL_lj_buf_putchar];

1098. }

1099. } else if (irs->o == IR_SNEW) {  /* Fuse string allocation. */

1100. args[1] = irs->op1;  /* const void * */

1101. args[2] = irs->op2;  /* MSize */

1102. ci = &lj_ir_callinfo[IRCALL_lj_buf_putmem];

1103. }

1104. }

1105. asm_setupresult(as, ir, ci);  /* SBuf * */

1106. asm_gencall(as, ci, args);

1107. if (args[1] == ASMREF_TMP1) {

1108. Reg tmp = ra_releasetmp(as, ASMREF_TMP1);

1109. if (kchar == -1)

1110. asm_tvptr(as, tmp, irs->op1);

1111. else

1112. ra_allockreg(as, kchar, tmp);

1113. }

1114. }

1116. ‌static void asm_bufstr(ASMState *as, IRIns *ir)

1117. {

1118. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_buf_tostr];

1119. IRRef args[1];

1120. args[0] = ir->op1;  /* SBuf *sb */

1121. as->gcsteps++;

1122. asm_setupresult(as, ir, ci);  /* GCstr * */

1123. asm_gencall(as, ci, args);

1124. }

1126. /* -- Type conversions ---------------------------------------------------- */

1128. ‌static void asm_tostr(ASMState *as, IRIns *ir)

1129. {

1130. const CCallInfo *ci;

1131. IRRef args[2];

1132. args[0] = ASMREF_L;

1133. as->gcsteps++;

1134. if (ir->op2 == IRTOSTR_NUM) {

1135. args[1] = ASMREF_TMP1;  /* cTValue * */

1136. ci = &lj_ir_callinfo[IRCALL_lj_strfmt_num];

1137. } else {

1138. args[1] = ir->op1;  /* int32_t k */

1139. if (ir->op2 == IRTOSTR_INT)

1140. ci = &lj_ir_callinfo[IRCALL_lj_strfmt_int];

1141. else

1142. ci = &lj_ir_callinfo[IRCALL_lj_strfmt_char];

1143. }

1144. asm_setupresult(as, ir, ci);  /* GCstr * */

1145. asm_gencall(as, ci, args);

1146. if (ir->op2 == IRTOSTR_NUM)

1147. asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1);

1148. }

1150. #if LJ_32 && LJ_HASFFI && !LJ_SOFTFP && !LJ_TARGET_X86

1151. ‌static void asm_conv64(ASMState *as, IRIns *ir)

1152. {

1153. IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK);

1154. IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH);

1155. IRCallID id;

1156. IRRef args[2];

1157. lua_assert((ir-1)->o == IR_CONV && ir->o == IR_HIOP);

1158. args[LJ_BE] = (ir-1)->op1;

1159. args[LJ_LE] = ir->op1;

1160. if (st == IRT_NUM || st == IRT_FLOAT) {

1161. id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64);

1162. ir--;

1163. } else {

1164. id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64);

1165. }

1166. {

1167. #if LJ_TARGET_ARM && !LJ_ABI_SOFTFP

1168. CCallInfo cim = lj_ir_callinfo[id], *ci = &cim;

1169. cim.flags |= CCI_VARARG;  /* These calls don't use the hard-float ABI! */

1170. #else

1171. const CCallInfo *ci = &lj_ir_callinfo[id];

1172. #endif

1173. asm_setupresult(as, ir, ci);

1174. asm_gencall(as, ci, args);

1175. }

1176. }

1177. #endif

1179. /* -- Memory references --------------------------------------------------- */

1181. ‌static void asm_newref(ASMState *as, IRIns *ir)

1182. {

1183. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey];

1184. IRRef args[3];

1185. if (ir->r == RID_SINK)

1186. return;

1187. args[0] = ASMREF_L;     /* lua_State *L */

1188. args[1] = ir->op1;      /* GCtab *t     */

1189. args[2] = ASMREF_TMP1;  /* cTValue *key */

1190. asm_setupresult(as, ir, ci);  /* TValue * */

1191. asm_gencall(as, ci, args);

1192. asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2);

1193. }

1195. ‌static void asm_lref(ASMState *as, IRIns *ir)

1196. {

1197. Reg r = ra_dest(as, ir, RSET_GPR);

1198. #if LJ_TARGET_X86ORX64

1199. ra_left(as, r, ASMREF_L);

1200. #else

1201. ra_leftov(as, r, ASMREF_L);

1202. #endif

1203. }

1205. /* -- Calls --------------------------------------------------------------- */

1207. /* Collect arguments from CALL* and CARG instructions. */

1208. ‌static void asm_collectargs(ASMState *as, IRIns *ir,

1209. const CCallInfo *ci, IRRef *args)

1210. {

1211. uint32_t n = CCI_XNARGS(ci);

1212. lua_assert(n <= CCI_NARGS_MAX*2);  /* Account for split args. */

1213. if ((ci->flags & CCI_L)) { *args++ = ASMREF_L; n--; }

1214. while (n-- > 1) {

1215. ir = IR(ir->op1);

1216. lua_assert(ir->o == IR_CARG);

1217. args[n] = ir->op2 == REF_NIL ? 0 : ir->op2;

1218. }

1219. args[0] = ir->op1 == REF_NIL ? 0 : ir->op1;

1220. lua_assert(IR(ir->op1)->o != IR_CARG);

1221. }

1223. /* Reconstruct CCallInfo flags for CALLX*. */

1224. ‌static uint32_t asm_callx_flags(ASMState *as, IRIns *ir)

1225. {

1226. uint32_t nargs = 0;

1227. if (ir->op1 != REF_NIL) {  /* Count number of arguments first. */

1228. IRIns *ira = IR(ir->op1);

1229. nargs++;

1230. while (ira->o == IR_CARG) { nargs++; ira = IR(ira->op1); }

1231. }

1232. #if LJ_HASFFI

1233. if (IR(ir->op2)->o == IR_CARG) {  /* Copy calling convention info. */

1234. CTypeID id = (CTypeID)IR(IR(ir->op2)->op2)->i;

1235. CType *ct = ctype_get(ctype_ctsG(J2G(as->J)), id);

1236. nargs |= ((ct->info & CTF_VARARG) ? CCI_VARARG : 0);

1237. #if LJ_TARGET_X86

1238. nargs |= (ctype_cconv(ct->info) << CCI_CC_SHIFT);

1239. #endif

1240. }

1241. #endif

1242. return (nargs | (ir->t.irt << CCI_OTSHIFT));

1243. }

1245. ‌static void asm_callid(ASMState *as, IRIns *ir, IRCallID id)

1246. {

1247. const CCallInfo *ci = &lj_ir_callinfo[id];

1248. IRRef args[2];

1249. args[0] = ir->op1;

1250. args[1] = ir->op2;

1251. asm_setupresult(as, ir, ci);

1252. asm_gencall(as, ci, args);

1253. }

1255. ‌static void asm_call(ASMState *as, IRIns *ir)

1256. {

1257. IRRef args[CCI_NARGS_MAX];

1258. const CCallInfo *ci = &lj_ir_callinfo[ir->op2];

1259. asm_collectargs(as, ir, ci, args);

1260. asm_setupresult(as, ir, ci);

1261. asm_gencall(as, ci, args);

1262. }

1264. #if !LJ_SOFTFP

1265. ‌static void asm_fppow(ASMState *as, IRIns *ir, IRRef lref, IRRef rref)

1266. {

1267. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow];

1268. IRRef args[2];

1269. args[0] = lref;

1270. args[1] = rref;

1271. asm_setupresult(as, ir, ci);

1272. asm_gencall(as, ci, args);

1273. }

1275. ‌static int asm_fpjoin_pow(ASMState *as, IRIns *ir)

1276. {

1277. IRIns *irp = IR(ir->op1);

1278. if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) {

1279. IRIns *irpp = IR(irp->op1);

1280. if (irpp == ir-2 && irpp->o == IR_FPMATH &&

1281. irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) {

1282. asm_fppow(as, ir, irpp->op1, irp->op2);

1283. return 1;

1284. }

1285. }

1286. return 0;

1287. }

1288. #endif

1290. /* -- PHI and loop handling ----------------------------------------------- */

1292. /* Break a PHI cycle by renaming to a free register (evict if needed). */

1293. ‌static void asm_phi_break(ASMState *as, RegSet blocked, RegSet blockedby,

1294. RegSet allow)

1295. {

1296. RegSet candidates = blocked & allow;

1297. if (candidates) {  /* If this register file has candidates. */

1298. /* Note: the set for ra_pick cannot be empty, since each register file

1299. ** has some registers never allocated to PHIs.

1300. */

1301. Reg down, up = ra_pick(as, ~blocked & allow);  /* Get a free register. */

1302. if (candidates & ~blockedby)  /* Optimize shifts, else it's a cycle. */

1303. candidates = candidates & ~blockedby;

1304. down = rset_picktop(candidates);  /* Pick candidate PHI register. */

1305. ra_rename(as, down, up);  /* And rename it to the free register. */

1306. }

1307. }

1309. /* PHI register shuffling.

1310. **

1311. ** The allocator tries hard to preserve PHI register assignments across

1312. ** the loop body. Most of the time this loop does nothing, since there

1313. ** are no register mismatches.

1314. **

1315. ** If a register mismatch is detected and ...

1316. ** - the register is currently free: rename it.

1317. ** - the register is blocked by an invariant: restore/remat and rename it.

1318. ** - Otherwise the register is used by another PHI, so mark it as blocked.

1319. **

1320. ** The renames are order-sensitive, so just retry the loop if a register

1321. ** is marked as blocked, but has been freed in the meantime. A cycle is

1322. ** detected if all of the blocked registers are allocated. To break the

1323. ** cycle rename one of them to a free register and retry.

1324. **

1325. ** Note that PHI spill slots are kept in sync and don't need to be shuffled.

1326. */

1327. ‌static void asm_phi_shuffle(ASMState *as)

1328. {

1329. RegSet work;

1331. /* Find and resolve PHI register mismatches. */

1332. for (;;) {

1333. RegSet blocked = RSET_EMPTY;

1334. RegSet blockedby = RSET_EMPTY;

1335. RegSet phiset = as->phiset;

1336. while (phiset) {  /* Check all left PHI operand registers. */

1337. Reg r = rset_pickbot(phiset);

1338. IRIns *irl = IR(as->phireg[r]);

1339. Reg left = irl->r;

1340. if (r != left) {  /* Mismatch? */

1341. if (!rset_test(as->freeset, r)) {  /* PHI register blocked? */

1342. IRRef ref = regcost_ref(as->cost[r]);

1343. /* Blocked by other PHI (w/reg)? */

1344. if (!ra_iskref(ref) && irt_ismarked(IR(ref)->t)) {

1345. rset_set(blocked, r);

1346. if (ra_hasreg(left))

1347. rset_set(blockedby, left);

1348. left = RID_NONE;

1349. } else {  /* Otherwise grab register from invariant. */

1350. ra_restore(as, ref);

1351. checkmclim(as);

1352. }

1353. }

1354. if (ra_hasreg(left)) {

1355. ra_rename(as, left, r);

1356. checkmclim(as);

1357. }

1358. }

1359. rset_clear(phiset, r);

1360. }

1361. if (!blocked) break;  /* Finished. */

1362. if (!(as->freeset & blocked)) {  /* Break cycles if none are free. */

1363. asm_phi_break(as, blocked, blockedby, RSET_GPR);

1364. if (!LJ_SOFTFP) asm_phi_break(as, blocked, blockedby, RSET_FPR);

1365. checkmclim(as);

1366. }  /* Else retry some more renames. */

1367. }

1369. /* Restore/remat invariants whose registers are modified inside the loop. */

1370. #if !LJ_SOFTFP

1371. work = as->modset & ~(as->freeset | as->phiset) & RSET_FPR;

1372. while (work) {

1373. Reg r = rset_pickbot(work);

1374. ra_restore(as, regcost_ref(as->cost[r]));

1375. rset_clear(work, r);

1376. checkmclim(as);

1377. }

1378. #endif

1379. work = as->modset & ~(as->freeset | as->phiset);

1380. while (work) {

1381. Reg r = rset_pickbot(work);

1382. ra_restore(as, regcost_ref(as->cost[r]));

1383. rset_clear(work, r);

1384. checkmclim(as);

1385. }

1387. /* Allocate and save all unsaved PHI regs and clear marks. */

1388. work = as->phiset;

1389. while (work) {

1390. Reg r = rset_picktop(work);

1391. IRRef lref = as->phireg[r];

1392. IRIns *ir = IR(lref);

1393. if (ra_hasspill(ir->s)) {  /* Left PHI gained a spill slot? */

1394. irt_clearmark(ir->t);  /* Handled here, so clear marker now. */

1395. ra_alloc1(as, lref, RID2RSET(r));

1396. ra_save(as, ir, r);  /* Save to spill slot inside the loop. */

1397. checkmclim(as);

1398. }

1399. rset_clear(work, r);

1400. }

1401. }

1403. /* Copy unsynced left/right PHI spill slots. Rarely needed. */

1404. ‌static void asm_phi_copyspill(ASMState *as)

1405. {

1406. int need = 0;

1407. IRIns *ir;

1408. for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--)

1409. if (ra_hasspill(ir->s) && ra_hasspill(IR(ir->op1)->s))

1410. need |= irt_isfp(ir->t) ? 2 : 1;  /* Unsynced spill slot? */

1411. if ((need & 1)) {  /* Copy integer spill slots. */

1412. #if !LJ_TARGET_X86ORX64

1413. Reg r = RID_TMP;

1414. #else

1415. Reg r = RID_RET;

1416. if ((as->freeset & RSET_GPR))

1417. r = rset_pickbot((as->freeset & RSET_GPR));

1418. else

1419. emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP);

1420. #endif

1421. for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) {

1422. if (ra_hasspill(ir->s)) {

1423. IRIns *irl = IR(ir->op1);

1424. if (ra_hasspill(irl->s) && !irt_isfp(ir->t)) {

1425. emit_spstore(as, irl, r, sps_scale(irl->s));

1426. emit_spload(as, ir, r, sps_scale(ir->s));

1427. checkmclim(as);

1428. }

1429. }

1430. }

1431. #if LJ_TARGET_X86ORX64

1432. if (!rset_test(as->freeset, r))

1433. emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP);

1434. #endif

1435. }

1436. #if !LJ_SOFTFP

1437. if ((need & 2)) {  /* Copy FP spill slots. */

1438. #if LJ_TARGET_X86

1439. Reg r = RID_XMM0;

1440. #else

1441. Reg r = RID_FPRET;

1442. #endif

1443. if ((as->freeset & RSET_FPR))

1444. r = rset_pickbot((as->freeset & RSET_FPR));

1445. if (!rset_test(as->freeset, r))

1446. emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP);

1447. for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) {

1448. if (ra_hasspill(ir->s)) {

1449. IRIns *irl = IR(ir->op1);

1450. if (ra_hasspill(irl->s) && irt_isfp(ir->t)) {

1451. emit_spstore(as, irl, r, sps_scale(irl->s));

1452. emit_spload(as, ir, r, sps_scale(ir->s));

1453. checkmclim(as);

1454. }

1455. }

1456. }

1457. if (!rset_test(as->freeset, r))

1458. emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP);

1459. }

1460. #endif

1461. }

1463. /* Emit renames for left PHIs which are only spilled outside the loop. */

1464. ‌static void asm_phi_fixup(ASMState *as)

1465. {

1466. RegSet work = as->phiset;

1467. while (work) {

1468. Reg r = rset_picktop(work);

1469. IRRef lref = as->phireg[r];

1470. IRIns *ir = IR(lref);

1471. if (irt_ismarked(ir->t)) {

1472. irt_clearmark(ir->t);

1473. /* Left PHI gained a spill slot before the loop? */

1474. if (ra_hasspill(ir->s)) {

1475. IRRef ren;

1476. lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), lref, as->loopsnapno);

1477. ren = tref_ref(lj_ir_emit(as->J));

1478. as->ir = as->T->ir;  /* The IR may have been reallocated. */

1479. IR(ren)->r = (uint8_t)r;

1480. IR(ren)->s = SPS_NONE;

1481. }

1482. }

1483. rset_clear(work, r);

1484. }

1485. }

1487. /* Setup right PHI reference. */

1488. ‌static void asm_phi(ASMState *as, IRIns *ir)

1489. {

1490. RegSet allow = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) &

1491. ~as->phiset;

1492. RegSet afree = (as->freeset & allow);

1493. IRIns *irl = IR(ir->op1);

1494. IRIns *irr = IR(ir->op2);

1495. if (ir->r == RID_SINK)  /* Sink PHI. */

1496. return;

1497. /* Spill slot shuffling is not implemented yet (but rarely needed). */

1498. if (ra_hasspill(irl->s) || ra_hasspill(irr->s))

1499. lj_trace_err(as->J, LJ_TRERR_NYIPHI);

1500. /* Leave at least one register free for non-PHIs (and PHI cycle breaking). */

1501. if ((afree & (afree-1))) {  /* Two or more free registers? */

1502. Reg r;

1503. if (ra_noreg(irr->r)) {  /* Get a register for the right PHI. */

1504. r = ra_allocref(as, ir->op2, allow);

1505. } else {  /* Duplicate right PHI, need a copy (rare). */

1506. r = ra_scratch(as, allow);

1507. emit_movrr(as, irr, r, irr->r);

1508. }

1509. ir->r = (uint8_t)r;

1510. rset_set(as->phiset, r);

1511. as->phireg[r] = (IRRef1)ir->op1;

1512. irt_setmark(irl->t);  /* Marks left PHIs _with_ register. */

1513. if (ra_noreg(irl->r))

1514. ra_sethint(irl->r, r); /* Set register hint for left PHI. */

1515. } else {  /* Otherwise allocate a spill slot. */

1516. /* This is overly restrictive, but it triggers only on synthetic code. */

1517. if (ra_hasreg(irl->r) || ra_hasreg(irr->r))

1518. lj_trace_err(as->J, LJ_TRERR_NYIPHI);

1519. ra_spill(as, ir);

1520. irr->s = ir->s;  /* Set right PHI spill slot. Sync left slot later. */

1521. }

1522. }

1524. static void asm_loop_fixup(ASMState *as);

1526. /* Middle part of a loop. */

1527. ‌static void asm_loop(ASMState *as)

1528. {

1529. MCode *mcspill;

1530. /* LOOP is a guard, so the snapno is up to date. */

1531. as->loopsnapno = as->snapno;

1532. if (as->gcsteps)

1533. asm_gc_check(as);

1534. /* LOOP marks the transition from the variant to the invariant part. */

1535. as->flagmcp = as->invmcp = NULL;

1536. as->sectref = 0;

1537. if (!neverfuse(as)) as->fuseref = 0;

1538. asm_phi_shuffle(as);

1539. mcspill = as->mcp;

1540. asm_phi_copyspill(as);

1541. asm_loop_fixup(as);

1542. as->mcloop = as->mcp;

1543. RA_DBGX((as, "===== LOOP ====="));

1544. if (!as->realign) RA_DBG_FLUSH();

1545. if (as->mcp != mcspill)

1546. emit_jmp(as, mcspill);

1547. }

1549. /* -- Target-specific assembler ------------------------------------------- */

1551. #if LJ_TARGET_X86ORX64

1552. #include "lj_asm_x86.h"

1553. #elif LJ_TARGET_ARM

1554. #include "lj_asm_arm.h"

1555. #elif LJ_TARGET_PPC

1556. #include "lj_asm_ppc.h"

1557. #elif LJ_TARGET_MIPS

1558. #include "lj_asm_mips.h"

1559. #else

1560. #error "Missing assembler for target CPU"

1561. #endif

1563. /* -- Instruction dispatch ------------------------------------------------ */

1565. /* Assemble a single instruction. */

1566. ‌static void asm_ir(ASMState *as, IRIns *ir)

1567. {

1568. switch ((IROp)ir->o) {

1569. /* Miscellaneous ops. */

1570. case IR_LOOP: asm_loop(as); break;

1571. case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break;

1572. case IR_USE:

1573. ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break;

1574. case IR_PHI: asm_phi(as, ir); break;

1575. case IR_HIOP: asm_hiop(as, ir); break;

1576. case IR_GCSTEP: asm_gcstep(as, ir); break;

1577. case IR_PROF: asm_prof(as, ir); break;

1579. /* Guarded assertions. */

1580. case IR_LT: case IR_GE: case IR_LE: case IR_GT:

1581. case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT:

1582. case IR_ABC:

1583. asm_comp(as, ir);

1584. break;

1585. case IR_EQ: case IR_NE:

1586. if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) {

1587. as->curins--;

1588. asm_href(as, ir-1, (IROp)ir->o);

1589. } else {

1590. asm_equal(as, ir);

1591. }

1592. break;

1594. case IR_RETF: asm_retf(as, ir); break;

1596. /* Bit ops. */

1597. case IR_BNOT: asm_bnot(as, ir); break;

1598. case IR_BSWAP: asm_bswap(as, ir); break;

1599. case IR_BAND: asm_band(as, ir); break;

1600. case IR_BOR: asm_bor(as, ir); break;

1601. case IR_BXOR: asm_bxor(as, ir); break;

1602. case IR_BSHL: asm_bshl(as, ir); break;

1603. case IR_BSHR: asm_bshr(as, ir); break;

1604. case IR_BSAR: asm_bsar(as, ir); break;

1605. case IR_BROL: asm_brol(as, ir); break;

1606. case IR_BROR: asm_bror(as, ir); break;

1608. /* Arithmetic ops. */

1609. case IR_ADD: asm_add(as, ir); break;

1610. case IR_SUB: asm_sub(as, ir); break;

1611. case IR_MUL: asm_mul(as, ir); break;

1612. case IR_DIV: asm_div(as, ir); break;

1613. case IR_MOD: asm_mod(as, ir); break;

1614. case IR_POW: asm_pow(as, ir); break;

1615. case IR_NEG: asm_neg(as, ir); break;

1616. case IR_ABS: asm_abs(as, ir); break;

1617. case IR_ATAN2: asm_atan2(as, ir); break;

1618. case IR_LDEXP: asm_ldexp(as, ir); break;

1619. case IR_MIN: asm_min(as, ir); break;

1620. case IR_MAX: asm_max(as, ir); break;

1621. case IR_FPMATH: asm_fpmath(as, ir); break;

1623. /* Overflow-checking arithmetic ops. */

1624. case IR_ADDOV: asm_addov(as, ir); break;

1625. case IR_SUBOV: asm_subov(as, ir); break;

1626. case IR_MULOV: asm_mulov(as, ir); break;

1628. /* Memory references. */

1629. case IR_AREF: asm_aref(as, ir); break;

1630. case IR_HREF: asm_href(as, ir, 0); break;

1631. case IR_HREFK: asm_hrefk(as, ir); break;

1632. case IR_NEWREF: asm_newref(as, ir); break;

1633. case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break;

1634. case IR_FREF: asm_fref(as, ir); break;

1635. case IR_STRREF: asm_strref(as, ir); break;

1636. case IR_LREF: asm_lref(as, ir); break;

1638. /* Loads and stores. */

1639. case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:

1640. asm_ahuvload(as, ir);

1641. break;

1642. case IR_FLOAD: asm_fload(as, ir); break;

1643. case IR_XLOAD: asm_xload(as, ir); break;

1644. case IR_SLOAD: asm_sload(as, ir); break;

1646. case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break;

1647. case IR_FSTORE: asm_fstore(as, ir); break;

1648. case IR_XSTORE: asm_xstore(as, ir); break;

1650. /* Allocations. */

1651. case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break;

1652. case IR_TNEW: asm_tnew(as, ir); break;

1653. case IR_TDUP: asm_tdup(as, ir); break;

1654. case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break;

1656. /* Buffer operations. */

1657. case IR_BUFHDR: asm_bufhdr(as, ir); break;

1658. case IR_BUFPUT: asm_bufput(as, ir); break;

1659. case IR_BUFSTR: asm_bufstr(as, ir); break;

1661. /* Write barriers. */

1662. case IR_TBAR: asm_tbar(as, ir); break;

1663. case IR_OBAR: asm_obar(as, ir); break;

1665. /* Type conversions. */

1666. case IR_TOBIT: asm_tobit(as, ir); break;

1667. case IR_CONV: asm_conv(as, ir); break;

1668. case IR_TOSTR: asm_tostr(as, ir); break;

1669. case IR_STRTO: asm_strto(as, ir); break;

1671. /* Calls. */

1672. case IR_CALLA:

1673. as->gcsteps++;

1674. /* fallthrough */

1675. case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break;

1676. case IR_CALLXS: asm_callx(as, ir); break;

1677. case IR_CARG: break;

1679. default:

1680. setintV(&as->J->errinfo, ir->o);

1681. lj_trace_err_info(as->J, LJ_TRERR_NYIIR);

1682. break;

1683. }

1684. }

1686. /* -- Head of trace ------------------------------------------------------- */

1688. /* Head of a root trace. */

1689. ‌static void asm_head_root(ASMState *as)

1690. {

1691. int32_t spadj;

1692. asm_head_root_base(as);

1693. emit_setvmstate(as, (int32_t)as->T->traceno);

1694. spadj = asm_stack_adjust(as);

1695. as->T->spadjust = (uint16_t)spadj;

1696. emit_spsub(as, spadj);

1697. /* Root traces assume a checked stack for the starting proto. */

1698. as->T->topslot = gcref(as->T->startpt)->pt.framesize;

1699. }

1701. /* Head of a side trace.

1702. **

1703. ** The current simplistic algorithm requires that all slots inherited

1704. ** from the parent are live in a register between pass 2 and pass 3. This

1705. ** avoids the complexity of stack slot shuffling. But of course this may

1706. ** overflow the register set in some cases and cause the dreaded error:

1707. ** "NYI: register coalescing too complex". A refined algorithm is needed.

1708. */

1709. ‌static void asm_head_side(ASMState *as)

1710. {

1711. IRRef1 sloadins[RID_MAX];

1712. RegSet allow = RSET_ALL;  /* Inverse of all coalesced registers. */

1713. RegSet live = RSET_EMPTY;  /* Live parent registers. */

1714. IRIns *irp = &as->parent->ir[REF_BASE];  /* Parent base. */

1715. int32_t spadj, spdelta;

1716. int pass2 = 0;

1717. int pass3 = 0;

1718. IRRef i;

1720. if (as->snapno && as->topslot > as->parent->topslot) {

1721. /* Force snap #0 alloc to prevent register overwrite in stack check. */

1722. as->snapno = 0;

1723. asm_snap_alloc(as);

1724. }

1725. allow = asm_head_side_base(as, irp, allow);

1727. /* Scan all parent SLOADs and collect register dependencies. */

1728. for (i = as->stopins; i > REF_BASE; i--) {

1729. IRIns *ir = IR(i);

1730. RegSP rs;

1731. lua_assert((ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_PARENT)) ||

1732. (LJ_SOFTFP && ir->o == IR_HIOP) || ir->o == IR_PVAL);

1733. rs = as->parentmap[i - REF_FIRST];

1734. if (ra_hasreg(ir->r)) {

1735. rset_clear(allow, ir->r);

1736. if (ra_hasspill(ir->s)) {

1737. ra_save(as, ir, ir->r);

1738. checkmclim(as);

1739. }

1740. } else if (ra_hasspill(ir->s)) {

1741. irt_setmark(ir->t);

1742. pass2 = 1;

1743. }

1744. if (ir->r == rs) {  /* Coalesce matching registers right now. */

1745. ra_free(as, ir->r);

1746. } else if (ra_hasspill(regsp_spill(rs))) {

1747. if (ra_hasreg(ir->r))

1748. pass3 = 1;

1749. } else if (ra_used(ir)) {

1750. sloadins[rs] = (IRRef1)i;

1751. rset_set(live, rs);  /* Block live parent register. */

1752. }

1753. }

1755. /* Calculate stack frame adjustment. */

1756. spadj = asm_stack_adjust(as);

1757. spdelta = spadj - (int32_t)as->parent->spadjust;

1758. if (spdelta < 0) {  /* Don't shrink the stack frame. */

1759. spadj = (int32_t)as->parent->spadjust;

1760. spdelta = 0;

1761. }

1762. as->T->spadjust = (uint16_t)spadj;

1764. /* Reload spilled target registers. */

1765. if (pass2) {

1766. for (i = as->stopins; i > REF_BASE; i--) {

1767. IRIns *ir = IR(i);

1768. if (irt_ismarked(ir->t)) {

1769. RegSet mask;

1770. Reg r;

1771. RegSP rs;

1772. irt_clearmark(ir->t);

1773. rs = as->parentmap[i - REF_FIRST];

1774. if (!ra_hasspill(regsp_spill(rs)))

1775. ra_sethint(ir->r, rs);  /* Hint may be gone, set it again. */

1776. else if (sps_scale(regsp_spill(rs))+spdelta == sps_scale(ir->s))

1777. continue;  /* Same spill slot, do nothing. */

1778. mask = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) & allow;

1779. if (mask == RSET_EMPTY)

1780. lj_trace_err(as->J, LJ_TRERR_NYICOAL);

1781. r = ra_allocref(as, i, mask);

1782. ra_save(as, ir, r);

1783. rset_clear(allow, r);

1784. if (r == rs) {  /* Coalesce matching registers right now. */

1785. ra_free(as, r);

1786. rset_clear(live, r);

1787. } else if (ra_hasspill(regsp_spill(rs))) {

1788. pass3 = 1;

1789. }

1790. checkmclim(as);

1791. }

1792. }

1793. }

1795. /* Store trace number and adjust stack frame relative to the parent. */

1796. emit_setvmstate(as, (int32_t)as->T->traceno);

1797. emit_spsub(as, spdelta);

1799. #if !LJ_TARGET_X86ORX64

1800. /* Restore BASE register from parent spill slot. */

1801. if (ra_hasspill(irp->s))

1802. emit_spload(as, IR(REF_BASE), IR(REF_BASE)->r, sps_scale(irp->s));

1803. #endif

1805. /* Restore target registers from parent spill slots. */

1806. if (pass3) {

1807. RegSet work = ~as->freeset & RSET_ALL;

1808. while (work) {

1809. Reg r = rset_pickbot(work);

1810. IRRef ref = regcost_ref(as->cost[r]);

1811. RegSP rs = as->parentmap[ref - REF_FIRST];

1812. rset_clear(work, r);

1813. if (ra_hasspill(regsp_spill(rs))) {

1814. int32_t ofs = sps_scale(regsp_spill(rs));

1815. ra_free(as, r);

1816. emit_spload(as, IR(ref), r, ofs);

1817. checkmclim(as);

1818. }

1819. }

1820. }

1822. /* Shuffle registers to match up target regs with parent regs. */

1823. for (;;) {

1824. RegSet work;

1826. /* Repeatedly coalesce free live registers by moving to their target. */

1827. while ((work = as->freeset & live) != RSET_EMPTY) {

1828. Reg rp = rset_pickbot(work);

1829. IRIns *ir = IR(sloadins[rp]);

1830. rset_clear(live, rp);

1831. rset_clear(allow, rp);

1832. ra_free(as, ir->r);

1833. emit_movrr(as, ir, ir->r, rp);

1834. checkmclim(as);

1835. }

1837. /* We're done if no live registers remain. */

1838. if (live == RSET_EMPTY)

1839. break;

1841. /* Break cycles by renaming one target to a temp. register. */

1842. if (live & RSET_GPR) {

1843. RegSet tmpset = as->freeset & ~live & allow & RSET_GPR;

1844. if (tmpset == RSET_EMPTY)

1845. lj_trace_err(as->J, LJ_TRERR_NYICOAL);

1846. ra_rename(as, rset_pickbot(live & RSET_GPR), rset_pickbot(tmpset));

1847. }

1848. if (!LJ_SOFTFP && (live & RSET_FPR)) {

1849. RegSet tmpset = as->freeset & ~live & allow & RSET_FPR;

1850. if (tmpset == RSET_EMPTY)

1851. lj_trace_err(as->J, LJ_TRERR_NYICOAL);

1852. ra_rename(as, rset_pickbot(live & RSET_FPR), rset_pickbot(tmpset));

1853. }

1854. checkmclim(as);

1855. /* Continue with coalescing to fix up the broken cycle(s). */

1856. }

1858. /* Inherit top stack slot already checked by parent trace. */

1859. as->T->topslot = as->parent->topslot;

1860. if (as->topslot > as->T->topslot) {  /* Need to check for higher slot? */

1861. #ifdef EXITSTATE_CHECKEXIT

1862. /* Highest exit + 1 indicates stack check. */

1863. ExitNo exitno = as->T->nsnap;

1864. #else

1865. /* Reuse the parent exit in the context of the parent trace. */

1866. ExitNo exitno = as->J->exitno;

1867. #endif

1868. as->T->topslot = (uint8_t)as->topslot;  /* Remember for child traces. */

1869. asm_stack_check(as, as->topslot, irp, allow & RSET_GPR, exitno);

1870. }

1871. }

1873. /* -- Tail of trace ------------------------------------------------------- */

1875. /* Get base slot for a snapshot. */

1876. ‌static BCReg asm_baseslot(ASMState *as, SnapShot *snap, int *gotframe)

1877. {

1878. SnapEntry *map = &as->T->snapmap[snap->mapofs];

1879. MSize n;

1880. for (n = snap->nent; n > 0; n--) {

1881. SnapEntry sn = map[n-1];

1882. if ((sn & SNAP_FRAME)) {

1883. *gotframe = 1;

1884. return snap_slot(sn);

1885. }

1886. }

1887. return 0;

1888. }

1890. /* Link to another trace. */

1891. ‌static void asm_tail_link(ASMState *as)

1892. {

1893. SnapNo snapno = as->T->nsnap-1;  /* Last snapshot. */

1894. SnapShot *snap = &as->T->snap[snapno];

1895. int gotframe = 0;

1896. BCReg baseslot = asm_baseslot(as, snap, &gotframe);

1898. as->topslot = snap->topslot;

1899. checkmclim(as);

1900. ra_allocref(as, REF_BASE, RID2RSET(RID_BASE));

1902. if (as->T->link == 0) {

1903. /* Setup fixed registers for exit to interpreter. */

1904. const BCIns *pc = snap_pc(as->T->snapmap[snap->mapofs + snap->nent]);

1905. int32_t mres;

1906. if (bc_op(*pc) == BC_JLOOP) {  /* NYI: find a better way to do this. */

1907. BCIns *retpc = &traceref(as->J, bc_d(*pc))->startins;

1908. if (bc_isret(bc_op(*retpc)))

1909. pc = retpc;

1910. }

1911. ra_allockreg(as, i32ptr(J2GG(as->J)->dispatch), RID_DISPATCH);

1912. ra_allockreg(as, i32ptr(pc), RID_LPC);

1913. mres = (int32_t)(snap->nslots - baseslot);

1914. switch (bc_op(*pc)) {

1915. case BC_CALLM: case BC_CALLMT:

1916. mres -= (int32_t)(1 + LJ_FR2 + bc_a(*pc) + bc_c(*pc)); break;

1917. case BC_RETM: mres -= (int32_t)(bc_a(*pc) + bc_d(*pc)); break;

1918. case BC_TSETM: mres -= (int32_t)bc_a(*pc); break;

1919. default: if (bc_op(*pc) < BC_FUNCF) mres = 0; break;

1920. }

1921. ra_allockreg(as, mres, RID_RET);  /* Return MULTRES or 0. */

1922. } else if (baseslot) {

1923. /* Save modified BASE for linking to trace with higher start frame. */

1924. emit_setgl(as, RID_BASE, jit_base);

1925. }

1926. emit_addptr(as, RID_BASE, 8*(int32_t)baseslot);

1928. /* Sync the interpreter state with the on-trace state. */

1929. asm_stack_restore(as, snap);

1931. /* Root traces that add frames need to check the stack at the end. */

1932. if (!as->parent && gotframe)

1933. asm_stack_check(as, as->topslot, NULL, as->freeset & RSET_GPR, snapno);

1934. }

1936. /* -- Trace setup --------------------------------------------------------- */

1938. /* Clear reg/sp for all instructions and add register hints. */

1939. ‌static void asm_setup_regsp(ASMState *as)

1940. {

1941. GCtrace *T = as->T;

1942. int sink = T->sinktags;

1943. IRRef nins = T->nins;

1944. IRIns *ir, *lastir;

1945. int inloop;

1946. #if LJ_TARGET_ARM

1947. uint32_t rload = 0xa6402a64;

1948. #endif

1950. ra_setup(as);

1952. /* Clear reg/sp for constants. */

1953. for (ir = IR(T->nk), lastir = IR(REF_BASE); ir < lastir; ir++)

1954. ir->prev = REGSP_INIT;

1956. /* REF_BASE is used for implicit references to the BASE register. */

1957. lastir->prev = REGSP_HINT(RID_BASE);

1959. ir = IR(nins-1);

1960. if (ir->o == IR_RENAME) {

1961. do { ir--; nins--; } while (ir->o == IR_RENAME);

1962. T->nins = nins;  /* Remove any renames left over from ASM restart. */

1963. }

1964. as->snaprename = nins;

1965. as->snapref = nins;

1966. as->snapno = T->nsnap;

1968. as->stopins = REF_BASE;

1969. as->orignins = nins;

1970. as->curins = nins;

1972. /* Setup register hints for parent link instructions. */

1973. ir = IR(REF_FIRST);

1974. if (as->parent) {

1975. uint16_t *p;

1976. lastir = lj_snap_regspmap(as->parent, as->J->exitno, ir);

1977. if (lastir - ir > LJ_MAX_JSLOTS)

1978. lj_trace_err(as->J, LJ_TRERR_NYICOAL);

1979. as->stopins = (IRRef)((lastir-1) - as->ir);

1980. for (p = as->parentmap; ir < lastir; ir++) {

1981. RegSP rs = ir->prev;

1982. *p++ = (uint16_t)rs;  /* Copy original parent RegSP to parentmap. */

1983. if (!ra_hasspill(regsp_spill(rs)))

1984. ir->prev = (uint16_t)REGSP_HINT(regsp_reg(rs));

1985. else

1986. ir->prev = REGSP_INIT;

1987. }

1988. }

1990. inloop = 0;

1991. as->evenspill = SPS_FIRST;

1992. for (lastir = IR(nins); ir < lastir; ir++) {

1993. if (sink) {

1994. if (ir->r == RID_SINK)

1995. continue;

1996. if (ir->r == RID_SUNK) {  /* Revert after ASM restart. */

1997. ir->r = RID_SINK;

1998. continue;

1999. }

2000. }

2001. switch (ir->o) {

2002. case IR_LOOP:

2003. inloop = 1;

2004. break;

2005. #if LJ_TARGET_ARM

2006. case IR_SLOAD:

2007. if (!((ir->op2 & IRSLOAD_TYPECHECK) || (ir+1)->o == IR_HIOP))

2008. break;

2009. /* fallthrough */

2010. case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:

2011. if (!LJ_SOFTFP && irt_isnum(ir->t)) break;

2012. ir->prev = (uint16_t)REGSP_HINT((rload & 15));

2013. rload = lj_ror(rload, 4);

2014. continue;

2015. #endif

2016. case IR_CALLXS: {

2017. CCallInfo ci;

2018. ci.flags = asm_callx_flags(as, ir);

2019. ir->prev = asm_setup_call_slots(as, ir, &ci);

2020. if (inloop)

2021. as->modset |= RSET_SCRATCH;

2022. continue;

2023. }

2024. case IR_CALLN: case IR_CALLA: case IR_CALLL: case IR_CALLS: {

2025. const CCallInfo *ci = &lj_ir_callinfo[ir->op2];

2026. ir->prev = asm_setup_call_slots(as, ir, ci);

2027. if (inloop)

2028. as->modset |= (ci->flags & CCI_NOFPRCLOBBER) ?

2029. (RSET_SCRATCH & ~RSET_FPR) : RSET_SCRATCH;

2030. continue;

2031. }

2032. #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)

2033. case IR_HIOP:

2034. switch ((ir-1)->o) {

2035. #if LJ_SOFTFP && LJ_TARGET_ARM

2036. case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:

2037. if (ra_hashint((ir-1)->r)) {

2038. ir->prev = (ir-1)->prev + 1;

2039. continue;

2040. }

2041. break;

2042. #endif

2043. #if !LJ_SOFTFP && LJ_NEED_FP64

2044. case IR_CONV:

2045. if (irt_isfp((ir-1)->t)) {

2046. ir->prev = REGSP_HINT(RID_FPRET);

2047. continue;

2048. }

2049. /* fallthrough */

2050. #endif

2051. case IR_CALLN: case IR_CALLXS:

2052. #if LJ_SOFTFP

2053. case IR_MIN: case IR_MAX:

2054. #endif

2055. (ir-1)->prev = REGSP_HINT(RID_RETLO);

2056. ir->prev = REGSP_HINT(RID_RETHI);

2057. continue;

2058. default:

2059. break;

2060. }

2061. break;

2062. #endif

2063. #if LJ_SOFTFP

2064. case IR_MIN: case IR_MAX:

2065. if ((ir+1)->o != IR_HIOP) break;

2066. /* fallthrough */

2067. #endif

2068. /* C calls evict all scratch regs and return results in RID_RET. */

2069. case IR_SNEW: case IR_XSNEW: case IR_NEWREF: case IR_BUFPUT:

2070. if (REGARG_NUMGPR < 3 && as->evenspill < 3)

2071. as->evenspill = 3;  /* lj_str_new and lj_tab_newkey need 3 args. */

2072. #if LJ_TARGET_X86 && LJ_HASFFI

2073. if (0) {

2074. case IR_CNEW:

2075. if (ir->op2 != REF_NIL && as->evenspill < 4)

2076. as->evenspill = 4;  /* lj_cdata_newv needs 4 args. */

2077. }

2078. #else

2079. case IR_CNEW:

2080. #endif

2081. case IR_TNEW: case IR_TDUP: case IR_CNEWI: case IR_TOSTR:

2082. case IR_BUFSTR:

2083. ir->prev = REGSP_HINT(RID_RET);

2084. if (inloop)

2085. as->modset = RSET_SCRATCH;

2086. continue;

2087. case IR_STRTO: case IR_OBAR:

2088. if (inloop)

2089. as->modset = RSET_SCRATCH;

2090. break;

2091. #if !LJ_SOFTFP

2092. case IR_ATAN2:

2093. #if LJ_TARGET_X86

2094. if (as->evenspill < 4)  /* Leave room to call atan2(). */

2095. as->evenspill = 4;

2096. #endif

2097. #if !LJ_TARGET_X86ORX64

2098. case IR_LDEXP:

2099. #endif

2100. #endif

2101. case IR_POW:

2102. if (!LJ_SOFTFP && irt_isnum(ir->t)) {

2103. if (inloop)

2104. as->modset |= RSET_SCRATCH;

2105. #if LJ_TARGET_X86

2106. break;

2107. #else

2108. ir->prev = REGSP_HINT(RID_FPRET);

2109. continue;

2110. #endif

2111. }

2112. /* fallthrough for integer POW */

2113. case IR_DIV: case IR_MOD:

2114. if (!irt_isnum(ir->t)) {

2115. ir->prev = REGSP_HINT(RID_RET);

2116. if (inloop)

2117. as->modset |= (RSET_SCRATCH & RSET_GPR);

2118. continue;

2119. }

2120. break;

2121. case IR_FPMATH:

2122. #if LJ_TARGET_X86ORX64

2123. if (ir->op2 <= IRFPM_TRUNC) {

2124. if (!(as->flags & JIT_F_SSE4_1)) {

2125. ir->prev = REGSP_HINT(RID_XMM0);

2126. if (inloop)

2127. as->modset |= RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX);

2128. continue;

2129. }

2130. break;

2131. } else if (ir->op2 == IRFPM_EXP2 && !LJ_64) {

2132. if (as->evenspill < 4)  /* Leave room to call pow(). */

2133. as->evenspill = 4;

2134. }

2135. #endif

2136. if (inloop)

2137. as->modset |= RSET_SCRATCH;

2138. #if LJ_TARGET_X86

2139. break;

2140. #else

2141. ir->prev = REGSP_HINT(RID_FPRET);

2142. continue;

2143. #endif

2144. #if LJ_TARGET_X86ORX64

2145. /* Non-constant shift counts need to be in RID_ECX on x86/x64. */

2146. case IR_BSHL: case IR_BSHR: case IR_BSAR: case IR_BROL: case IR_BROR:

2147. if (!irref_isk(ir->op2) && !ra_hashint(IR(ir->op2)->r)) {

2148. IR(ir->op2)->r = REGSP_HINT(RID_ECX);

2149. if (inloop)

2150. rset_set(as->modset, RID_ECX);

2151. }

2152. break;

2153. #endif

2154. /* Do not propagate hints across type conversions or loads. */

2155. case IR_TOBIT:

2156. case IR_XLOAD:

2157. #if !LJ_TARGET_ARM

2158. case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:

2159. #endif

2160. break;

2161. case IR_CONV:

2162. if (irt_isfp(ir->t) || (ir->op2 & IRCONV_SRCMASK) == IRT_NUM ||

2163. (ir->op2 & IRCONV_SRCMASK) == IRT_FLOAT)

2164. break;

2165. /* fallthrough */

2166. default:

2167. /* Propagate hints across likely 'op reg, imm' or 'op reg'. */

2168. if (irref_isk(ir->op2) && !irref_isk(ir->op1) &&

2169. ra_hashint(regsp_reg(IR(ir->op1)->prev))) {

2170. ir->prev = IR(ir->op1)->prev;

2171. continue;

2172. }

2173. break;

2174. }

2175. ir->prev = REGSP_INIT;

2176. }

2177. if ((as->evenspill & 1))

2178. as->oddspill = as->evenspill++;

2179. else

2180. as->oddspill = 0;

2181. }

2183. /* -- Assembler core ------------------------------------------------------ */

2185. /* Assemble a trace. */

2186. ‌void lj_asm_trace(jit_State *J, GCtrace *T)

2187. {

2188. ASMState as_;

2189. ASMState *as = &as_;

2190. MCode *origtop;

2192. /* Ensure an initialized instruction beyond the last one for HIOP checks. */

2193. J->cur.nins = lj_ir_nextins(J);

2194. J->cur.ir[J->cur.nins].o = IR_NOP;

2196. /* Setup initial state. Copy some fields to reduce indirections. */

2197. as->J = J;

2198. as->T = T;

2199. as->ir = T->ir;

2200. as->flags = J->flags;

2201. as->loopref = J->loopref;

2202. as->realign = NULL;

2203. as->loopinv = 0;

2204. as->parent = J->parent ? traceref(J, J->parent) : NULL;

2206. /* Reserve MCode memory. */

2207. as->mctop = origtop = lj_mcode_reserve(J, &as->mcbot);

2208. as->mcp = as->mctop;

2209. as->mclim = as->mcbot + MCLIM_REDZONE;

2210. asm_setup_target(as);

2212. do {

2213. as->mcp = as->mctop;

2214. #ifdef LUA_USE_ASSERT

2215. as->mcp_prev = as->mcp;

2216. #endif

2217. as->curins = T->nins;

2218. RA_DBG_START();

2219. RA_DBGX((as, "===== STOP ====="));

2221. /* General trace setup. Emit tail of trace. */

2222. asm_tail_prep(as);

2223. as->mcloop = NULL;

2224. as->flagmcp = NULL;

2225. as->topslot = 0;

2226. as->gcsteps = 0;

2227. as->sectref = as->loopref;

2228. as->fuseref = (as->flags & JIT_F_OPT_FUSE) ? as->loopref : FUSE_DISABLED;

2229. asm_setup_regsp(as);

2230. if (!as->loopref)

2231. asm_tail_link(as);

2233. /* Assemble a trace in linear backwards order. */

2234. for (as->curins--; as->curins > as->stopins; as->curins--) {

2235. IRIns *ir = IR(as->curins);

2236. lua_assert(!(LJ_32 && irt_isint64(ir->t)));  /* Handled by SPLIT. */

2237. if (!ra_used(ir) && !ir_sideeff(ir) && (as->flags & JIT_F_OPT_DCE))

2238. continue;  /* Dead-code elimination can be soooo easy. */

2239. if (irt_isguard(ir->t))

2240. asm_snap_prep(as);

2241. RA_DBG_REF();

2242. checkmclim(as);

2243. asm_ir(as, ir);

2244. }

2245. } while (as->realign);  /* Retry in case the MCode needs to be realigned. */

2247. /* Emit head of trace. */

2248. RA_DBG_REF();

2249. checkmclim(as);

2250. if (as->gcsteps > 0) {

2251. as->curins = as->T->snap[0].ref;

2252. asm_snap_prep(as);  /* The GC check is a guard. */

2253. asm_gc_check(as);

2254. }

2255. ra_evictk(as);

2256. if (as->parent)

2257. asm_head_side(as);

2258. else

2259. asm_head_root(as);

2260. asm_phi_fixup(as);

2262. RA_DBGX((as, "===== START ===="));

2263. RA_DBG_FLUSH();

2264. if (as->freeset != RSET_ALL)

2265. lj_trace_err(as->J, LJ_TRERR_BADRA);  /* Ouch! Should never happen. */

2267. /* Set trace entry point before fixing up tail to allow link to self. */

2268. T->mcode = as->mcp;

2269. T->mcloop = as->mcloop ? (MSize)((char *)as->mcloop - (char *)as->mcp) : 0;

2270. if (!as->loopref)

2271. asm_tail_fixup(as, T->link);  /* Note: this may change as->mctop! */

2272. T->szmcode = (MSize)((char *)as->mctop - (char *)as->mcp);

2273. lj_mcode_sync(T->mcode, origtop);

2274. }

2276. ‌#undef IR

2278. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ir.h - luajit-2.0-src

Global variables defined

- lj_ir_mode

- lj_ir_type_size

Data types defined

- IRFPMathOp

- IRFieldID

- IRIns

- IRIns

- IRMode

- IROp

- IROp1

- IROpT

- IRRef

- IRRef1

- IRRef2

- IRType

- IRType1

- IRType1

- TRef

Functions defined

- ir_sideeff

- irt_toitype_

- itype2irt

Macros defined

- FLENUM

- FLENUM

- FPMENUM

- FPMENUM

- IRBUFHDR_APPEND

- IRBUFHDR_RESET

- IRCONV_ANY

- IRCONV_CHECK

- IRCONV_CONVMASK

- IRCONV_CSH

- IRCONV_DSH

- IRCONV_DSTMASK

- IRCONV_INDEX

- IRCONV_INT_NUM

- IRCONV_MODEMASK

- IRCONV_NUM_INT

- IRCONV_SEXT

- IRCONV_SRCMASK

- IRCONV_TOBIT

- IRDEF

- IRDELTA_L2S

- IRENUM

- IRENUM

- IRFLDEF

- IRFPMDEF

- IRMODE

- IRM_A

- IRM_AW

- IRM_C

- IRM_CW

- IRM_L

- IRM_LW

- IRM_N

- IRM_NW

- IRM_R

- IRM_S

- IRM_W

- IRM___

- IRREF2

- IRSLOAD_CONVERT

- IRSLOAD_FRAME

- IRSLOAD_INHERIT

- IRSLOAD_PARENT

- IRSLOAD_READONLY

- IRSLOAD_TYPECHECK

- IRT

- IRTDEF

- IRTENUM

- IRTENUM

- IRTG

- IRTGI

- IRTI

- IRTN

- IRTOSTR_CHAR

- IRTOSTR_INT

- IRTOSTR_NUM

- IRT_IS64

- IRT_IS64

- IRT_IS64

- IRXLOAD_READONLY

- IRXLOAD_UNALIGNED

- IRXLOAD_VOLATILE

- TREF

- TREF_CONT

- TREF_FALSE

- TREF_FRAME

- TREF_NIL

- TREF_PRI

- TREF_REFMASK

- TREF_TRUE

- _LJ_IR_H

- ir_k64

- ir_kcdata

- ir_kfunc

- ir_kgc

- ir_kint64

- ir_knum

- ir_kptr

- ir_kstr

- ir_ktab

- irm_iscomm

- irm_kind

- irm_op1

- irm_op2

- irref_isk

- irt_clearmark

- irt_clearphi

- irt_is64

- irt_is64orfp

- irt_isaddr

- irt_iscdata

- irt_isfloat

- irt_isfp

- irt_isgcv

- irt_isguard

- irt_isi16

- irt_isi64

- irt_isi8

- irt_isint

- irt_isint64

- irt_isinteger

- irt_islightud

- irt_ismarked

- irt_isnil

- irt_isnum

- irt_isphi

- irt_ispri

- irt_isstr

- irt_istab

- irt_isu16

- irt_isu32

- irt_isu64

- irt_isu8

- irt_sametype

- irt_setmark

- irt_setphi

- irt_size

- irt_t

- irt_toitype

- irt_type

- irt_typerange

- irtype_ispri

- tref_isbool

- tref_iscdata

- tref_isfalse

- tref_isfunc

- tref_isgcv

- tref_isint

- tref_isinteger

- tref_isk

- tref_isk2

- tref_islightud

- tref_isnil

- tref_isnum

- tref_isnumber

- tref_isnumber_str

- tref_ispri

- tref_isstr

- tref_istab

- tref_istrue

- tref_istruecond

- tref_istype

- tref_isudata

- tref_ref

- tref_t

- tref_type

- tref_typerange

Source code

1. /*

2. ** SSA IR (Intermediate Representation) format.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_IR_H

7. ‌#define _LJ_IR_H

9. #include "lj_obj.h"

11. /* -- IR instructions ----------------------------------------------------- */

13. /* IR instruction definition. Order matters, see below. ORDER IR */

14. ‌#define IRDEF(_) \

15. /* Guarded assertions. */ \

16. /* Must be properly aligned to flip opposites (^1) and (un)ordered (^4). */ \

17. _(LT,                N , ref, ref) \

18. _(GE,                N , ref, ref) \

19. _(LE,                N , ref, ref) \

20. _(GT,                N , ref, ref) \

21. \

22. _(ULT,        N , ref, ref) \

23. _(UGE,        N , ref, ref) \

24. _(ULE,        N , ref, ref) \

25. _(UGT,        N , ref, ref) \

26. \

27. _(EQ,                C , ref, ref) \

28. _(NE,                C , ref, ref) \

29. \

30. _(ABC,        N , ref, ref) \

31. _(RETF,        S , ref, ref) \

32. \

33. /* Miscellaneous ops. */ \

34. _(NOP,        N , ___, ___) \

35. _(BASE,        N , lit, lit) \

36. _(PVAL,        N , lit, ___) \

37. _(GCSTEP,        S , ___, ___) \

38. _(HIOP,        S , ref, ref) \

39. _(LOOP,        S , ___, ___) \

40. _(USE,        S , ref, ___) \

41. _(PHI,        S , ref, ref) \

42. _(RENAME,        S , ref, lit) \

43. _(PROF,        S , ___, ___) \

44. \

45. /* Constants. */ \

46. _(KPRI,        N , ___, ___) \

47. _(KINT,        N , cst, ___) \

48. _(KGC,        N , cst, ___) \

49. _(KPTR,        N , cst, ___) \

50. _(KKPTR,        N , cst, ___) \

51. _(KNULL,        N , cst, ___) \

52. _(KNUM,        N , cst, ___) \

53. _(KINT64,        N , cst, ___) \

54. _(KSLOT,        N , ref, lit) \

55. \

56. /* Bit ops. */ \

57. _(BNOT,        N , ref, ___) \

58. _(BSWAP,        N , ref, ___) \

59. _(BAND,        C , ref, ref) \

60. _(BOR,        C , ref, ref) \

61. _(BXOR,        C , ref, ref) \

62. _(BSHL,        N , ref, ref) \

63. _(BSHR,        N , ref, ref) \

64. _(BSAR,        N , ref, ref) \

65. _(BROL,        N , ref, ref) \

66. _(BROR,        N , ref, ref) \

67. \

68. /* Arithmetic ops. ORDER ARITH */ \

69. _(ADD,        C , ref, ref) \

70. _(SUB,        N , ref, ref) \

71. _(MUL,        C , ref, ref) \

72. _(DIV,        N , ref, ref) \

73. _(MOD,        N , ref, ref) \

74. _(POW,        N , ref, ref) \

75. _(NEG,        N , ref, ref) \

76. \

77. _(ABS,        N , ref, ref) \

78. _(ATAN2,        N , ref, ref) \

79. _(LDEXP,        N , ref, ref) \

80. _(MIN,        C , ref, ref) \

81. _(MAX,        C , ref, ref) \

82. _(FPMATH,        N , ref, lit) \

83. \

84. /* Overflow-checking arithmetic ops. */ \

85. _(ADDOV,        CW, ref, ref) \

86. _(SUBOV,        NW, ref, ref) \

87. _(MULOV,        CW, ref, ref) \

88. \

89. /* Memory ops. A = array, H = hash, U = upvalue, F = field, S = stack. */ \

90. \

91. /* Memory references. */ \

92. _(AREF,        R , ref, ref) \

93. _(HREFK,        R , ref, ref) \

94. _(HREF,        L , ref, ref) \

95. _(NEWREF,        S , ref, ref) \

96. _(UREFO,        LW, ref, lit) \

97. _(UREFC,        LW, ref, lit) \

98. _(FREF,        R , ref, lit) \

99. _(STRREF,        N , ref, ref) \

100. _(LREF,        L , ___, ___) \

101. \

102. /* Loads and Stores. These must be in the same order. */ \

103. _(ALOAD,        L , ref, ___) \

104. _(HLOAD,        L , ref, ___) \

105. _(ULOAD,        L , ref, ___) \

106. _(FLOAD,        L , ref, lit) \

107. _(XLOAD,        L , ref, lit) \

108. _(SLOAD,        L , lit, lit) \

109. _(VLOAD,        L , ref, ___) \

110. \

111. _(ASTORE,        S , ref, ref) \

112. _(HSTORE,        S , ref, ref) \

113. _(USTORE,        S , ref, ref) \

114. _(FSTORE,        S , ref, ref) \

115. _(XSTORE,        S , ref, ref) \

116. \

117. /* Allocations. */ \

118. _(SNEW,        N , ref, ref)  /* CSE is ok, not marked as A. */ \

119. _(XSNEW,        A , ref, ref) \

120. _(TNEW,        AW, lit, lit) \

121. _(TDUP,        AW, ref, ___) \

122. _(CNEW,        AW, ref, ref) \

123. _(CNEWI,        NW, ref, ref)  /* CSE is ok, not marked as A. */ \

124. \

125. /* Buffer operations. */ \

126. _(BUFHDR,        L , ref, lit) \

127. _(BUFPUT,        L , ref, ref) \

128. _(BUFSTR,        A , ref, ref) \

129. \

130. /* Barriers. */ \

131. _(TBAR,        S , ref, ___) \

132. _(OBAR,        S , ref, ref) \

133. _(XBAR,        S , ___, ___) \

134. \

135. /* Type conversions. */ \

136. _(CONV,        NW, ref, lit) \

137. _(TOBIT,        N , ref, ref) \

138. _(TOSTR,        N , ref, lit) \

139. _(STRTO,        N , ref, ___) \

140. \

141. /* Calls. */ \

142. _(CALLN,        N , ref, lit) \

143. _(CALLA,        A , ref, lit) \

144. _(CALLL,        L , ref, lit) \

145. _(CALLS,        S , ref, lit) \

146. _(CALLXS,        S , ref, ref) \

147. _(CARG,        N , ref, ref) \

148. \

149. /* End of list. */

151. /* IR opcodes (max. 256). */

152. typedef enum {

153. ‌#define IRENUM(name, m, m1, m2)        IR_##name,

154. IRDEF(IRENUM)

155. ‌#undef IRENUM

156. IR__MAX

157. ‌} IROp;

159. /* Stored opcode. */

160. ‌typedef uint8_t IROp1;

162. LJ_STATIC_ASSERT(((int)IR_EQ^1) == (int)IR_NE);

163. LJ_STATIC_ASSERT(((int)IR_LT^1) == (int)IR_GE);

164. LJ_STATIC_ASSERT(((int)IR_LE^1) == (int)IR_GT);

165. LJ_STATIC_ASSERT(((int)IR_LT^3) == (int)IR_GT);

166. LJ_STATIC_ASSERT(((int)IR_LT^4) == (int)IR_ULT);

168. /* Delta between xLOAD and xSTORE. */

169. ‌#define IRDELTA_L2S                ((int)IR_ASTORE - (int)IR_ALOAD)

171. LJ_STATIC_ASSERT((int)IR_HLOAD + IRDELTA_L2S == (int)IR_HSTORE);

172. LJ_STATIC_ASSERT((int)IR_ULOAD + IRDELTA_L2S == (int)IR_USTORE);

173. LJ_STATIC_ASSERT((int)IR_FLOAD + IRDELTA_L2S == (int)IR_FSTORE);

174. LJ_STATIC_ASSERT((int)IR_XLOAD + IRDELTA_L2S == (int)IR_XSTORE);

176. /* -- Named IR literals --------------------------------------------------- */

178. /* FPMATH sub-functions. ORDER FPM. */

179. ‌#define IRFPMDEF(_) \

180. _(FLOOR) _(CEIL) _(TRUNC)  /* Must be first and in this order. */ \

181. _(SQRT) _(EXP) _(EXP2) _(LOG) _(LOG2) _(LOG10) \

182. _(SIN) _(COS) _(TAN) \

183. _(OTHER)

185. typedef enum {

186. ‌#define FPMENUM(name)                IRFPM_##name,

187. IRFPMDEF(FPMENUM)

188. ‌#undef FPMENUM

189. IRFPM__MAX

190. ‌} IRFPMathOp;

192. /* FLOAD fields. */

193. ‌#define IRFLDEF(_) \

194. _(STR_LEN,        offsetof(GCstr, len)) \

195. _(FUNC_ENV,        offsetof(GCfunc, l.env)) \

196. _(FUNC_PC,        offsetof(GCfunc, l.pc)) \

197. _(FUNC_FFID,        offsetof(GCfunc, l.ffid)) \

198. _(THREAD_ENV,        offsetof(lua_State, env)) \

199. _(TAB_META,        offsetof(GCtab, metatable)) \

200. _(TAB_ARRAY,        offsetof(GCtab, array)) \

201. _(TAB_NODE,        offsetof(GCtab, node)) \

202. _(TAB_ASIZE,        offsetof(GCtab, asize)) \

203. _(TAB_HMASK,        offsetof(GCtab, hmask)) \

204. _(TAB_NOMM,        offsetof(GCtab, nomm)) \

205. _(UDATA_META,        offsetof(GCudata, metatable)) \

206. _(UDATA_UDTYPE, offsetof(GCudata, udtype)) \

207. _(UDATA_FILE,        sizeof(GCudata)) \

208. _(CDATA_CTYPEID, offsetof(GCcdata, ctypeid)) \

209. _(CDATA_PTR,        sizeof(GCcdata)) \

210. _(CDATA_INT, sizeof(GCcdata)) \

211. _(CDATA_INT64, sizeof(GCcdata)) \

212. _(CDATA_INT64_4, sizeof(GCcdata) + 4)

214. typedef enum {

215. ‌#define FLENUM(name, ofs)        IRFL_##name,

216. IRFLDEF(FLENUM)

217. ‌#undef FLENUM

218. IRFL__MAX

219. ‌} IRFieldID;

221. /* SLOAD mode bits, stored in op2. */

222. ‌#define IRSLOAD_PARENT                0x01        /* Coalesce with parent trace. */

223. ‌#define IRSLOAD_FRAME                0x02        /* Load hiword of frame. */

224. ‌#define IRSLOAD_TYPECHECK        0x04        /* Needs type check. */

225. ‌#define IRSLOAD_CONVERT                0x08        /* Number to integer conversion. */

226. ‌#define IRSLOAD_READONLY        0x10        /* Read-only, omit slot store. */

227. ‌#define IRSLOAD_INHERIT                0x20        /* Inherited by exits/side traces. */

229. /* XLOAD mode, stored in op2. */

230. ‌#define IRXLOAD_READONLY        1        /* Load from read-only data. */

231. ‌#define IRXLOAD_VOLATILE        2        /* Load from volatile data. */

232. ‌#define IRXLOAD_UNALIGNED        4        /* Unaligned load. */

234. /* BUFHDR mode, stored in op2. */

235. ‌#define IRBUFHDR_RESET                0        /* Reset buffer. */

236. ‌#define IRBUFHDR_APPEND                1        /* Append to buffer. */

238. /* CONV mode, stored in op2. */

239. ‌#define IRCONV_SRCMASK                0x001f        /* Source IRType. */

240. ‌#define IRCONV_DSTMASK                0x03e0        /* Dest. IRType (also in ir->t). */

241. ‌#define IRCONV_DSH                5

242. ‌#define IRCONV_NUM_INT                ((IRT_NUM<<IRCONV_DSH)|IRT_INT)

243. ‌#define IRCONV_INT_NUM                ((IRT_INT<<IRCONV_DSH)|IRT_NUM)

244. ‌#define IRCONV_SEXT                0x0800        /* Sign-extend integer to integer. */

245. ‌#define IRCONV_MODEMASK                0x0fff

246. ‌#define IRCONV_CONVMASK                0xf000

247. ‌#define IRCONV_CSH                12

248. /* Number to integer conversion mode. Ordered by strength of the checks. */

249. ‌#define IRCONV_TOBIT  (0<<IRCONV_CSH)        /* None. Cache only: TOBIT conv. */

250. ‌#define IRCONV_ANY    (1<<IRCONV_CSH)        /* Any FP number is ok. */

251. ‌#define IRCONV_INDEX  (2<<IRCONV_CSH)        /* Check + special backprop rules. */

252. ‌#define IRCONV_CHECK  (3<<IRCONV_CSH)        /* Number checked for integerness. */

254. /* TOSTR mode, stored in op2. */

255. ‌#define IRTOSTR_INT                0        /* Convert integer to string. */

256. ‌#define IRTOSTR_NUM                1        /* Convert number to string. */

257. ‌#define IRTOSTR_CHAR                2        /* Convert char value to string. */

259. /* -- IR operands --------------------------------------------------------- */

261. /* IR operand mode (2 bit). */

262. typedef enum {

263. IRMref,                /* IR reference. */

264. IRMlit,                /* 16 bit unsigned literal. */

265. IRMcst,                /* Constant literal: i, gcr or ptr. */

266. IRMnone                /* Unused operand. */

267. ‌} IRMode;

268. ‌#define IRM___                IRMnone

270. /* Mode bits: Commutative, {Normal/Ref, Alloc, Load, Store}, Non-weak guard. */

271. ‌#define IRM_C                        0x10

273. ‌#define IRM_N                        0x00

274. ‌#define IRM_R                        IRM_N

275. ‌#define IRM_A                        0x20

276. ‌#define IRM_L                        0x40

277. ‌#define IRM_S                        0x60

279. ‌#define IRM_W                        0x80

281. ‌#define IRM_NW                        (IRM_N|IRM_W)

282. ‌#define IRM_CW                        (IRM_C|IRM_W)

283. ‌#define IRM_AW                        (IRM_A|IRM_W)

284. ‌#define IRM_LW                        (IRM_L|IRM_W)

286. ‌#define irm_op1(m)                ((IRMode)((m)&3))

287. ‌#define irm_op2(m)                ((IRMode)(((m)>>2)&3))

288. ‌#define irm_iscomm(m)                ((m) & IRM_C)

289. ‌#define irm_kind(m)                ((m) & IRM_S)

291. ‌#define IRMODE(name, m, m1, m2)        (((IRM##m1)|((IRM##m2)<<2)|(IRM_##m))^IRM_W),

293. ‌LJ_DATA const uint8_t lj_ir_mode[IR__MAX+1];

295. /* -- IR instruction types ------------------------------------------------ */

297. /* Map of itypes to non-negative numbers. ORDER LJ_T.

298. ** LJ_TUPVAL/LJ_TTRACE never appear in a TValue. Use these itypes for

299. ** IRT_P32 and IRT_P64, which never escape the IR.

300. ** The various integers are only used in the IR and can only escape to

301. ** a TValue after implicit or explicit conversion. Their types must be

302. ** contiguous and next to IRT_NUM (see the typerange macros below).

303. */

304. ‌#define IRTDEF(_) \

305. _(NIL, 4) _(FALSE, 4) _(TRUE, 4) _(LIGHTUD, LJ_64 ? 8 : 4) _(STR, 4) \

306. _(P32, 4) _(THREAD, 4) _(PROTO, 4) _(FUNC, 4) _(P64, 8) _(CDATA, 4) \

307. _(TAB, 4) _(UDATA, 4) \

308. _(FLOAT, 4) _(NUM, 8) _(I8, 1) _(U8, 1) _(I16, 2) _(U16, 2) \

309. _(INT, 4) _(U32, 4) _(I64, 8) _(U64, 8) \

310. _(SOFTFP, 4)  /* There is room for 9 more types. */

312. /* IR result type and flags (8 bit). */

313. typedef enum {

314. ‌#define IRTENUM(name, size)        IRT_##name,

315. IRTDEF(IRTENUM)

316. ‌#undef IRTENUM

317. IRT__MAX,

319. /* Native pointer type and the corresponding integer type. */

320. IRT_PTR = LJ_64 ? IRT_P64 : IRT_P32,

321. IRT_INTP = LJ_64 ? IRT_I64 : IRT_INT,

322. IRT_UINTP = LJ_64 ? IRT_U64 : IRT_U32,

323. /* TODO_GC64: major changes required for all uses of IRT_P32. */

325. /* Additional flags. */

326. IRT_MARK = 0x20,        /* Marker for misc. purposes. */

327. IRT_ISPHI = 0x40,        /* Instruction is left or right PHI operand. */

328. IRT_GUARD = 0x80,        /* Instruction is a guard. */

330. /* Masks. */

331. IRT_TYPE = 0x1f,

332. IRT_T = 0xff

333. ‌} IRType;

335. ‌#define irtype_ispri(irt)        ((uint32_t)(irt) <= IRT_TRUE)

337. /* Stored IRType. */

338. ‌‌typedef struct IRType1 { uint8_t irt; } IRType1;

340. ‌#define IRT(o, t)                ((uint32_t)(((o)<<8) | (t)))

341. ‌#define IRTI(o)                        (IRT((o), IRT_INT))

342. ‌#define IRTN(o)                        (IRT((o), IRT_NUM))

343. ‌#define IRTG(o, t)                (IRT((o), IRT_GUARD|(t)))

344. ‌#define IRTGI(o)                (IRT((o), IRT_GUARD|IRT_INT))

346. ‌#define irt_t(t)                ((IRType)(t).irt)

347. ‌#define irt_type(t)                ((IRType)((t).irt & IRT_TYPE))

348. ‌#define irt_sametype(t1, t2)        ((((t1).irt ^ (t2).irt) & IRT_TYPE) == 0)

349. ‌#define irt_typerange(t, first, last) \

350. ((uint32_t)((t).irt & IRT_TYPE) - (uint32_t)(first) <= (uint32_t)(last-first))

352. ‌#define irt_isnil(t)                (irt_type(t) == IRT_NIL)

353. ‌#define irt_ispri(t)                ((uint32_t)irt_type(t) <= IRT_TRUE)

354. ‌#define irt_islightud(t)        (irt_type(t) == IRT_LIGHTUD)

355. ‌#define irt_isstr(t)                (irt_type(t) == IRT_STR)

356. ‌#define irt_istab(t)                (irt_type(t) == IRT_TAB)

357. ‌#define irt_iscdata(t)                (irt_type(t) == IRT_CDATA)

358. ‌#define irt_isfloat(t)                (irt_type(t) == IRT_FLOAT)

359. ‌#define irt_isnum(t)                (irt_type(t) == IRT_NUM)

360. ‌#define irt_isint(t)                (irt_type(t) == IRT_INT)

361. ‌#define irt_isi8(t)                (irt_type(t) == IRT_I8)

362. ‌#define irt_isu8(t)                (irt_type(t) == IRT_U8)

363. ‌#define irt_isi16(t)                (irt_type(t) == IRT_I16)

364. ‌#define irt_isu16(t)                (irt_type(t) == IRT_U16)

365. ‌#define irt_isu32(t)                (irt_type(t) == IRT_U32)

366. ‌#define irt_isi64(t)                (irt_type(t) == IRT_I64)

367. ‌#define irt_isu64(t)                (irt_type(t) == IRT_U64)

369. ‌#define irt_isfp(t)                (irt_isnum(t) || irt_isfloat(t))

370. ‌#define irt_isinteger(t)        (irt_typerange((t), IRT_I8, IRT_INT))

371. ‌#define irt_isgcv(t)                (irt_typerange((t), IRT_STR, IRT_UDATA))

372. ‌#define irt_isaddr(t)                (irt_typerange((t), IRT_LIGHTUD, IRT_UDATA))

373. ‌#define irt_isint64(t)                (irt_typerange((t), IRT_I64, IRT_U64))

375. #if LJ_GC64

376. ‌#define IRT_IS64 \

377. ((1u<<IRT_NUM)|(1u<<IRT_I64)|(1u<<IRT_U64)|(1u<<IRT_P64)|\

378. (1u<<IRT_LIGHTUD)|(1u<<IRT_STR)|(1u<<IRT_THREAD)|(1u<<IRT_PROTO)|\

379. (1u<<IRT_FUNC)|(1u<<IRT_CDATA)|(1u<<IRT_TAB)|(1u<<IRT_UDATA))

380. #elif LJ_64

381. ‌#define IRT_IS64 \

382. ((1u<<IRT_NUM)|(1u<<IRT_I64)|(1u<<IRT_U64)|(1u<<IRT_P64)|(1u<<IRT_LIGHTUD))

383. #else

384. ‌#define IRT_IS64 \

385. ((1u<<IRT_NUM)|(1u<<IRT_I64)|(1u<<IRT_U64))

386. #endif

388. ‌#define irt_is64(t)                ((IRT_IS64 >> irt_type(t)) & 1)

389. ‌#define irt_is64orfp(t)                (((IRT_IS64|(1u<<IRT_FLOAT))>>irt_type(t)) & 1)

391. ‌#define irt_size(t)                (lj_ir_type_size[irt_t((t))])

393. ‌LJ_DATA const uint8_t lj_ir_type_size[];

395. ‌static LJ_AINLINE IRType itype2irt(const TValue *tv)

396. {

397. if (tvisint(tv))

398. return IRT_INT;

399. else if (tvisnum(tv))

400. return IRT_NUM;

401. #if LJ_64 && !LJ_GC64

402. else if (tvislightud(tv))

403. return IRT_LIGHTUD;

404. #endif

405. else

406. return (IRType)~itype(tv);

407. }

409. ‌static LJ_AINLINE uint32_t irt_toitype_(IRType t)

410. {

411. lua_assert(!LJ_64 || t != IRT_LIGHTUD);

412. if (LJ_DUALNUM && t > IRT_NUM) {

413. return LJ_TISNUM;

414. } else {

415. lua_assert(t <= IRT_NUM);

416. return ~(uint32_t)t;

417. }

418. }

420. ‌#define irt_toitype(t)                irt_toitype_(irt_type((t)))

422. ‌#define irt_isguard(t)                ((t).irt & IRT_GUARD)

423. ‌#define irt_ismarked(t)                ((t).irt & IRT_MARK)

424. ‌#define irt_setmark(t)                ((t).irt |= IRT_MARK)

425. ‌#define irt_clearmark(t)        ((t).irt &= ~IRT_MARK)

426. ‌#define irt_isphi(t)                ((t).irt & IRT_ISPHI)

427. ‌#define irt_setphi(t)                ((t).irt |= IRT_ISPHI)

428. ‌#define irt_clearphi(t)                ((t).irt &= ~IRT_ISPHI)

430. /* Stored combined IR opcode and type. */

431. ‌typedef uint16_t IROpT;

433. /* -- IR references ------------------------------------------------------- */

435. /* IR references. */

436. ‌typedef uint16_t IRRef1;        /* One stored reference. */

437. ‌typedef uint32_t IRRef2;        /* Two stored references. */

438. ‌typedef uint32_t IRRef;                /* Used to pass around references. */

440. /* Fixed references. */

441. enum {

442. REF_BIAS =        0x8000,

443. REF_TRUE =        REF_BIAS-3,

444. REF_FALSE =        REF_BIAS-2,

445. REF_NIL =        REF_BIAS-1,        /* \--- Constants grow downwards. */

446. REF_BASE =        REF_BIAS,        /* /--- IR grows upwards. */

447. REF_FIRST =        REF_BIAS+1,

448. REF_DROP =        0xffff

449. };

451. /* Note: IRMlit operands must be < REF_BIAS, too!

452. ** This allows for fast and uniform manipulation of all operands

453. ** without looking up the operand mode in lj_ir_mode:

454. ** - CSE calculates the maximum reference of two operands.

455. **   This must work with mixed reference/literal operands, too.

456. ** - DCE marking only checks for operand >= REF_BIAS.

457. ** - LOOP needs to substitute reference operands.

458. **   Constant references and literals must not be modified.

459. */

461. ‌#define IRREF2(lo, hi)                ((IRRef2)(lo) | ((IRRef2)(hi) << 16))

463. ‌#define irref_isk(ref)                ((ref) < REF_BIAS)

465. /* Tagged IR references (32 bit).

466. **

467. ** +-------+-------+---------------+

468. ** |  irt  | flags |      ref      |

469. ** +-------+-------+---------------+

470. **

471. ** The tag holds a copy of the IRType and speeds up IR type checks.

472. */

473. ‌typedef uint32_t TRef;

475. ‌#define TREF_REFMASK                0x0000ffff

476. ‌#define TREF_FRAME                0x00010000

477. ‌#define TREF_CONT                0x00020000

479. ‌#define TREF(ref, t)                ((TRef)((ref) + ((t)<<24)))

481. ‌#define tref_ref(tr)                ((IRRef1)(tr))

482. ‌#define tref_t(tr)                ((IRType)((tr)>>24))

483. ‌#define tref_type(tr)                ((IRType)(((tr)>>24) & IRT_TYPE))

484. ‌#define tref_typerange(tr, first, last) \

485. ((((tr)>>24) & IRT_TYPE) - (TRef)(first) <= (TRef)(last-first))

487. ‌#define tref_istype(tr, t)        (((tr) & (IRT_TYPE<<24)) == ((t)<<24))

488. ‌#define tref_isnil(tr)                (tref_istype((tr), IRT_NIL))

489. ‌#define tref_isfalse(tr)        (tref_istype((tr), IRT_FALSE))

490. ‌#define tref_istrue(tr)                (tref_istype((tr), IRT_TRUE))

491. ‌#define tref_islightud(tr)        (tref_istype((tr), IRT_LIGHTUD))

492. ‌#define tref_isstr(tr)                (tref_istype((tr), IRT_STR))

493. ‌#define tref_isfunc(tr)                (tref_istype((tr), IRT_FUNC))

494. ‌#define tref_iscdata(tr)        (tref_istype((tr), IRT_CDATA))

495. ‌#define tref_istab(tr)                (tref_istype((tr), IRT_TAB))

496. ‌#define tref_isudata(tr)        (tref_istype((tr), IRT_UDATA))

497. ‌#define tref_isnum(tr)                (tref_istype((tr), IRT_NUM))

498. ‌#define tref_isint(tr)                (tref_istype((tr), IRT_INT))

500. ‌#define tref_isbool(tr)                (tref_typerange((tr), IRT_FALSE, IRT_TRUE))

501. ‌#define tref_ispri(tr)                (tref_typerange((tr), IRT_NIL, IRT_TRUE))

502. ‌#define tref_istruecond(tr)        (!tref_typerange((tr), IRT_NIL, IRT_FALSE))

503. ‌#define tref_isinteger(tr)        (tref_typerange((tr), IRT_I8, IRT_INT))

504. ‌#define tref_isnumber(tr)        (tref_typerange((tr), IRT_NUM, IRT_INT))

505. ‌#define tref_isnumber_str(tr)        (tref_isnumber((tr)) || tref_isstr((tr)))

506. ‌#define tref_isgcv(tr)                (tref_typerange((tr), IRT_STR, IRT_UDATA))

508. ‌#define tref_isk(tr)                (irref_isk(tref_ref((tr))))

509. ‌#define tref_isk2(tr1, tr2)        (irref_isk(tref_ref((tr1) | (tr2))))

511. ‌#define TREF_PRI(t)                (TREF(REF_NIL-(t), (t)))

512. ‌#define TREF_NIL                (TREF_PRI(IRT_NIL))

513. ‌#define TREF_FALSE                (TREF_PRI(IRT_FALSE))

514. ‌#define TREF_TRUE                (TREF_PRI(IRT_TRUE))

516. /* -- IR format ----------------------------------------------------------- */

518. /* IR instruction format (64 bit).

519. **

520. **    16      16     8   8   8   8

521. ** +-------+-------+---+---+---+---+

522. ** |  op1  |  op2  | t | o | r | s |

523. ** +-------+-------+---+---+---+---+

524. ** |  op12/i/gco   |   ot  | prev  | (alternative fields in union)

525. ** +---------------+-------+-------+

526. **        32           16      16

527. **

528. ** prev is only valid prior to register allocation and then reused for r + s.

529. */

531. ‌typedef union IRIns {

532. struct {

533. LJ_ENDIAN_LOHI(

534. IRRef1 op1;        /* IR operand 1. */

535. , IRRef1 op2;        /* IR operand 2. */

536. )

537. IROpT ot;                /* IR opcode and type (overlaps t and o). */

538. IRRef1 prev;        /* Previous ins in same chain (overlaps r and s). */

539. };

540. struct {

541. IRRef2 op12;        /* IR operand 1 and 2 (overlaps op1 and op2). */

542. LJ_ENDIAN_LOHI(

543. IRType1 t;        /* IR type. */

544. , IROp1 o;                /* IR opcode. */

545. )

546. LJ_ENDIAN_LOHI(

547. uint8_t r;        /* Register allocation (overlaps prev). */

548. , uint8_t s;        /* Spill slot allocation (overlaps prev). */

549. )

550. };

551. int32_t i;                /* 32 bit signed integer literal (overlaps op12). */

552. GCRef gcr;                /* GCobj constant (overlaps op12). */

553. MRef ptr;                /* Pointer constant (overlaps op12). */

554. ‌} IRIns;

556. /* TODO_GC64: major changes required. */

557. ‌#define ir_kgc(ir)        check_exp((ir)->o == IR_KGC, gcref((ir)->gcr))

558. ‌#define ir_kstr(ir)        (gco2str(ir_kgc((ir))))

559. ‌#define ir_ktab(ir)        (gco2tab(ir_kgc((ir))))

560. ‌#define ir_kfunc(ir)        (gco2func(ir_kgc((ir))))

561. ‌#define ir_kcdata(ir)        (gco2cd(ir_kgc((ir))))

562. ‌#define ir_knum(ir)        check_exp((ir)->o == IR_KNUM, mref((ir)->ptr, cTValue))

563. ‌#define ir_kint64(ir)        check_exp((ir)->o == IR_KINT64, mref((ir)->ptr,cTValue))

564. ‌#define ir_k64(ir) \

565. check_exp((ir)->o == IR_KNUM || (ir)->o == IR_KINT64, mref((ir)->ptr,cTValue))

566. ‌#define ir_kptr(ir) \

567. check_exp((ir)->o == IR_KPTR || (ir)->o == IR_KKPTR, mref((ir)->ptr, void))

569. /* A store or any other op with a non-weak guard has a side-effect. */

570. ‌static LJ_AINLINE int ir_sideeff(IRIns *ir)

571. {

572. return (((ir->t.irt | ~IRT_GUARD) & lj_ir_mode[ir->o]) >= IRM_S);

573. }

575. LJ_STATIC_ASSERT((int)IRT_GUARD == (int)IRM_W);

577. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_target_arm64.h - luajit-2.0-src

Data types defined

- A64Ins

- A64Ins

Macros defined

- A64F_A

- A64F_D

- A64F_M

- A64F_N

- A64F_S19

- A64F_S26

- A64F_U16

- FPRDEF

- GPRDEF

- REGARG_FIRSTFPR

- REGARG_FIRSTGPR

- REGARG_LASTFPR

- REGARG_LASTGPR

- REGARG_NUMFPR

- REGARG_NUMGPR

- RIDENUM

- RID_MIN_KREF

- RID_NUM_KREF

- RSET_ALL

- RSET_FIXED

- RSET_FPR

- RSET_GPR

- RSET_INIT

- RSET_SCRATCH

- RSET_SCRATCH_FPR

- RSET_SCRATCH_GPR

- VRIDDEF

- _LJ_TARGET_ARM64_H

Source code

1. /*

2. ** Definitions for ARM64 CPUs.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_TARGET_ARM64_H

7. ‌#define _LJ_TARGET_ARM64_H

9. /* -- Registers IDs ------------------------------------------------------- */

11. ‌#define GPRDEF(_) \

12. _(X0) _(X1) _(X2) _(X3) _(X4) _(X5) _(X6) _(X7) \

13. _(X8) _(X9) _(X10) _(X11) _(X12) _(X13) _(X14) _(X15) \

14. _(X16) _(X17) _(X18) _(X19) _(X20) _(X21) _(X22) _(X23) \

15. _(X24) _(X25) _(X26) _(X27) _(X28) _(FP) _(LR) _(SP)

16. ‌#define FPRDEF(_) \

17. _(D0) _(D1) _(D2) _(D3) _(D4) _(D5) _(D6) _(D7) \

18. _(D8) _(D9) _(D10) _(D11) _(D12) _(D13) _(D14) _(D15) \

19. _(D16) _(D17) _(D18) _(D19) _(D20) _(D21) _(D22) _(D23) \

20. _(D24) _(D25) _(D26) _(D27) _(D28) _(D29) _(D30) _(D31)

21. ‌#define VRIDDEF(_)

23. ‌#define RIDENUM(name)        RID_##name,

25. enum {

26. GPRDEF(RIDENUM)                /* General-purpose registers (GPRs). */

27. FPRDEF(RIDENUM)                /* Floating-point registers (FPRs). */

28. RID_MAX,

29. RID_TMP = RID_LR,

30. RID_ZERO = RID_SP,

32. /* Calling conventions. */

33. RID_RET = RID_X0,

34. RID_FPRET = RID_D0,

36. /* These definitions must match with the *.dasc file(s): */

37. RID_BASE = RID_X19,                /* Interpreter BASE. */

38. RID_LPC = RID_X21,                /* Interpreter PC. */

39. RID_GL = RID_X22,                /* Interpreter GL. */

40. RID_LREG = RID_X23,                /* Interpreter L. */

42. /* Register ranges [min, max) and number of registers. */

43. RID_MIN_GPR = RID_X0,

44. RID_MAX_GPR = RID_SP+1,

45. RID_MIN_FPR = RID_MAX_GPR,

46. RID_MAX_FPR = RID_D31+1,

47. RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR,

48. RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR

49. };

51. ‌#define RID_NUM_KREF                RID_NUM_GPR

52. ‌#define RID_MIN_KREF                RID_X0

54. /* -- Register sets ------------------------------------------------------- */

56. /* Make use of all registers, except for x18, fp, lr and sp. */

57. ‌#define RSET_FIXED \

58. (RID2RSET(RID_X18)|RID2RSET(RID_FP)|RID2RSET(RID_LR)|RID2RSET(RID_SP))

59. ‌#define RSET_GPR        (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED)

60. ‌#define RSET_FPR        RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)

61. ‌#define RSET_ALL        (RSET_GPR|RSET_FPR)

62. ‌#define RSET_INIT        RSET_ALL

64. /* lr is an implicit scratch register. */

65. ‌#define RSET_SCRATCH_GPR        (RSET_RANGE(RID_X0, RID_X17+1))

66. ‌#define RSET_SCRATCH_FPR \

67. (RSET_RANGE(RID_D0, RID_D7+1)|RSET_RANGE(RID_D16, RID_D31+1))

68. ‌#define RSET_SCRATCH                (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR)

69. ‌#define REGARG_FIRSTGPR                RID_X0

70. ‌#define REGARG_LASTGPR                RID_X7

71. ‌#define REGARG_NUMGPR                8

72. ‌#define REGARG_FIRSTFPR                RID_D0

73. ‌#define REGARG_LASTFPR                RID_D7

74. ‌#define REGARG_NUMFPR                8

76. /* -- Instructions -------------------------------------------------------- */

78. /* Instruction fields. */

79. ‌#define A64F_D(r)        (r)

80. ‌#define A64F_N(r)       ((r) << 5)

81. ‌#define A64F_A(r)       ((r) << 10)

82. ‌#define A64F_M(r)       ((r) << 16)

83. ‌#define A64F_U16(x)        ((x) << 5)

84. ‌#define A64F_S26(x)        (x)

85. ‌#define A64F_S19(x)        ((x) << 5)

87. ‌typedef enum A64Ins {

88. A64I_MOVZw = 0x52800000,

89. A64I_MOVZx = 0xd2800000,

90. A64I_LDRLw = 0x18000000,

91. A64I_LDRLx = 0x58000000,

92. A64I_NOP = 0xd503201f,

93. A64I_B = 0x14000000,

94. A64I_BR = 0xd61f0000,

95. ‌} A64Ins;

97. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_mcode.c - luajit-2.0-src

Data types defined

- MCLink

- MCLink

Functions defined

- lj_mcode_abort

- lj_mcode_commit

- lj_mcode_free

- lj_mcode_limiterr

- lj_mcode_patch

- lj_mcode_reserve

- lj_mcode_sync

- mcode_alloc

- mcode_alloc

- mcode_alloc_at

- mcode_alloc_at

- mcode_alloc_at

- mcode_allocarea

- mcode_free

- mcode_free

- mcode_free

- mcode_protect

- mcode_protect

- mcode_protfail

- mcode_setprot

- mcode_setprot

Macros defined

- LUAJIT_UNPROTECT_MCODE

- LUA_CORE

- MAP_ANONYMOUS

- MCPROT_GEN

- MCPROT_GEN

- MCPROT_RUN

- MCPROT_RUN

- MCPROT_RW

- MCPROT_RW

- MCPROT_RW

- MCPROT_RWX

- MCPROT_RWX

- MCPROT_RWX

- MCPROT_RX

- MCPROT_RX

- MCPROT_RX

- WIN32_LEAN_AND_MEAN

- lj_mcode_c

- mcode_validptr

- mcode_validptr

Source code

1. /*

2. ** Machine code management.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_mcode_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #if LJ_HASJIT

11. #include "lj_gc.h"

12. #include "lj_err.h"

13. #include "lj_jit.h"

14. #include "lj_mcode.h"

15. #include "lj_trace.h"

16. #include "lj_dispatch.h"

17. #endif

18. #if LJ_HASJIT || LJ_HASFFI

19. #include "lj_vm.h"

20. #endif

22. /* -- OS-specific functions ----------------------------------------------- */

24. #if LJ_HASJIT || LJ_HASFFI

26. /* Define this if you want to run LuaJIT with Valgrind. */

27. #ifdef LUAJIT_USE_VALGRIND

28. #include <valgrind/valgrind.h>

29. #endif

31. #if LJ_TARGET_IOS

32. void sys_icache_invalidate(void *start, size_t len);

33. #endif

35. /* Synchronize data/instruction cache. */

36. ‌void lj_mcode_sync(void *start, void *end)

37. {

38. #ifdef LUAJIT_USE_VALGRIND

39. VALGRIND_DISCARD_TRANSLATIONS(start, (char *)end-(char *)start);

40. #endif

41. #if LJ_TARGET_X86ORX64

42. UNUSED(start); UNUSED(end);

43. #elif LJ_TARGET_IOS

44. sys_icache_invalidate(start, (char *)end-(char *)start);

45. #elif LJ_TARGET_PPC

46. lj_vm_cachesync(start, end);

47. #elif defined(__GNUC__)

48. __clear_cache(start, end);

49. #else

50. #error "Missing builtin to flush instruction cache"

51. #endif

52. }

54. #endif

56. #if LJ_HASJIT

58. #if LJ_TARGET_WINDOWS

60. ‌#define WIN32_LEAN_AND_MEAN

61. #include <windows.h>

63. ‌#define MCPROT_RW        PAGE_READWRITE

64. ‌#define MCPROT_RX        PAGE_EXECUTE_READ

65. ‌#define MCPROT_RWX        PAGE_EXECUTE_READWRITE

67. ‌static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, DWORD prot)

68. {

69. void *p = VirtualAlloc((void *)hint, sz,

70. MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, prot);

71. if (!p && !hint)

72. lj_trace_err(J, LJ_TRERR_MCODEAL);

73. return p;

74. }

76. ‌static void mcode_free(jit_State *J, void *p, size_t sz)

77. {

78. UNUSED(J); UNUSED(sz);

79. VirtualFree(p, 0, MEM_RELEASE);

80. }

82. ‌static int mcode_setprot(void *p, size_t sz, DWORD prot)

83. {

84. DWORD oprot;

85. return !VirtualProtect(p, sz, prot, &oprot);

86. }

88. #elif LJ_TARGET_POSIX

90. #include <sys/mman.h>

92. #ifndef MAP_ANONYMOUS

93. ‌#define MAP_ANONYMOUS        MAP_ANON

94. #endif

96. ‌#define MCPROT_RW        (PROT_READ|PROT_WRITE)

97. ‌#define MCPROT_RX        (PROT_READ|PROT_EXEC)

98. ‌#define MCPROT_RWX        (PROT_READ|PROT_WRITE|PROT_EXEC)

100. ‌static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot)

101. {

102. void *p = mmap((void *)hint, sz, prot, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);

103. if (p == MAP_FAILED) {

104. if (!hint) lj_trace_err(J, LJ_TRERR_MCODEAL);

105. p = NULL;

106. }

107. return p;

108. }

110. ‌static void mcode_free(jit_State *J, void *p, size_t sz)

111. {

112. UNUSED(J);

113. munmap(p, sz);

114. }

116. ‌static int mcode_setprot(void *p, size_t sz, int prot)

117. {

118. return mprotect(p, sz, prot);

119. }

121. #elif LJ_64

123. #error "Missing OS support for explicit placement of executable memory"

125. #else

127. /* Fallback allocator. This will fail if memory is not executable by default. */

128. ‌#define LUAJIT_UNPROTECT_MCODE

129. ‌#define MCPROT_RW        0

130. ‌#define MCPROT_RX        0

131. ‌#define MCPROT_RWX        0

133. ‌static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot)

134. {

135. UNUSED(hint); UNUSED(prot);

136. return lj_mem_new(J->L, sz);

137. }

139. ‌static void mcode_free(jit_State *J, void *p, size_t sz)

140. {

141. lj_mem_free(J2G(J), p, sz);

142. }

144. #endif

146. /* -- MCode area protection ----------------------------------------------- */

148. /* Define this ONLY if page protection twiddling becomes a bottleneck. */

149. #ifdef LUAJIT_UNPROTECT_MCODE

151. /* It's generally considered to be a potential security risk to have

152. ** pages with simultaneous write *and* execute access in a process.

153. **

154. ** Do not even think about using this mode for server processes or

155. ** apps handling untrusted external data (such as a browser).

156. **

157. ** The security risk is not in LuaJIT itself -- but if an adversary finds

158. ** any *other* flaw in your C application logic, then any RWX memory page

159. ** simplifies writing an exploit considerably.

160. */

161. ‌#define MCPROT_GEN        MCPROT_RWX

162. ‌#define MCPROT_RUN        MCPROT_RWX

164. ‌static void mcode_protect(jit_State *J, int prot)

165. {

166. UNUSED(J); UNUSED(prot);

167. }

169. #else

171. /* This is the default behaviour and much safer:

172. **

173. ** Most of the time the memory pages holding machine code are executable,

174. ** but NONE of them is writable.

175. **

176. ** The current memory area is marked read-write (but NOT executable) only

177. ** during the short time window while the assembler generates machine code.

178. */

179. ‌#define MCPROT_GEN        MCPROT_RW

180. ‌#define MCPROT_RUN        MCPROT_RX

182. /* Protection twiddling failed. Probably due to kernel security. */

183. ‌static LJ_NOINLINE void mcode_protfail(jit_State *J)

184. {

185. lua_CFunction panic = J2G(J)->panic;

186. if (panic) {

187. lua_State *L = J->L;

188. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITPROT));

189. panic(L);

190. }

191. }

193. /* Change protection of MCode area. */

194. ‌static void mcode_protect(jit_State *J, int prot)

195. {

196. if (J->mcprot != prot) {

197. if (LJ_UNLIKELY(mcode_setprot(J->mcarea, J->szmcarea, prot)))

198. mcode_protfail(J);

199. J->mcprot = prot;

200. }

201. }

203. #endif

205. /* -- MCode area allocation ----------------------------------------------- */

207. #if LJ_TARGET_X64

208. ‌#define mcode_validptr(p)        ((p) && (uintptr_t)(p) < (uintptr_t)1<<47)

209. #else

210. ‌#define mcode_validptr(p)        ((p) && (uintptr_t)(p) < 0xffff0000)

211. #endif

213. #ifdef LJ_TARGET_JUMPRANGE

215. /* Get memory within relative jump distance of our code in 64 bit mode. */

216. ‌static void *mcode_alloc(jit_State *J, size_t sz)

217. {

218. /* Target an address in the static assembler code (64K aligned).

219. ** Try addresses within a distance of target-range/2+1MB..target+range/2-1MB.

220. ** Use half the jump range so every address in the range can reach any other.

221. */

222. #if LJ_TARGET_MIPS

223. /* Use the middle of the 256MB-aligned region. */

224. uintptr_t target = ((uintptr_t)(void *)lj_vm_exit_handler & 0xf0000000u) +

225. 0x08000000u;

226. #else

227. uintptr_t target = (uintptr_t)(void *)lj_vm_exit_handler & ~(uintptr_t)0xffff;

228. #endif

229. const uintptr_t range = (1u << (LJ_TARGET_JUMPRANGE-1)) - (1u << 21);

230. /* First try a contiguous area below the last one. */

231. uintptr_t hint = J->mcarea ? (uintptr_t)J->mcarea - sz : 0;

232. int i;

233. for (i = 0; i < 32; i++) {  /* 32 attempts ought to be enough ... */

234. if (mcode_validptr(hint)) {

235. void *p = mcode_alloc_at(J, hint, sz, MCPROT_GEN);

237. if (mcode_validptr(p) &&

238. ((uintptr_t)p + sz - target < range || target - (uintptr_t)p < range))

239. return p;

240. if (p) mcode_free(J, p, sz);  /* Free badly placed area. */

241. }

242. /* Next try probing pseudo-random addresses. */

243. do {

244. hint = (0x78fb ^ LJ_PRNG_BITS(J, 15)) << 16;  /* 64K aligned. */

245. } while (!(hint + sz < range));

246. hint = target + hint - (range>>1);

247. }

248. lj_trace_err(J, LJ_TRERR_MCODEAL);  /* Give up. OS probably ignores hints? */

249. return NULL;

250. }

252. #else

254. /* All memory addresses are reachable by relative jumps. */

255. ‌static void *mcode_alloc(jit_State *J, size_t sz)

256. {

257. #ifdef __OpenBSD__

258. /* Allow better executable memory allocation for OpenBSD W^X mode. */

259. void *p = mcode_alloc_at(J, 0, sz, MCPROT_RUN);

260. if (p && mcode_setprot(p, sz, MCPROT_GEN)) {

261. mcode_free(J, p, sz);

262. return NULL;

263. }

264. return p;

265. #else

266. return mcode_alloc_at(J, 0, sz, MCPROT_GEN);

267. #endif

268. }

270. #endif

272. /* -- MCode area management ----------------------------------------------- */

274. /* Linked list of MCode areas. */

275. ‌typedef struct MCLink {

276. MCode *next;                /* Next area. */

277. size_t size;                /* Size of current area. */

278. ‌} MCLink;

280. /* Allocate a new MCode area. */

281. ‌static void mcode_allocarea(jit_State *J)

282. {

283. MCode *oldarea = J->mcarea;

284. size_t sz = (size_t)J->param[JIT_P_sizemcode] << 10;

285. sz = (sz + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1);

286. J->mcarea = (MCode *)mcode_alloc(J, sz);

287. J->szmcarea = sz;

288. J->mcprot = MCPROT_GEN;

289. J->mctop = (MCode *)((char *)J->mcarea + J->szmcarea);

290. J->mcbot = (MCode *)((char *)J->mcarea + sizeof(MCLink));

291. ((MCLink *)J->mcarea)->next = oldarea;

292. ((MCLink *)J->mcarea)->size = sz;

293. J->szallmcarea += sz;

294. }

296. /* Free all MCode areas. */

297. ‌void lj_mcode_free(jit_State *J)

298. {

299. MCode *mc = J->mcarea;

300. J->mcarea = NULL;

301. J->szallmcarea = 0;

302. while (mc) {

303. MCode *next = ((MCLink *)mc)->next;

304. mcode_free(J, mc, ((MCLink *)mc)->size);

305. mc = next;

306. }

307. }

309. /* -- MCode transactions -------------------------------------------------- */

311. /* Reserve the remainder of the current MCode area. */

312. ‌MCode *lj_mcode_reserve(jit_State *J, MCode **lim)

313. {

314. if (!J->mcarea)

315. mcode_allocarea(J);

316. else

317. mcode_protect(J, MCPROT_GEN);

318. *lim = J->mcbot;

319. return J->mctop;

320. }

322. /* Commit the top part of the current MCode area. */

323. ‌void lj_mcode_commit(jit_State *J, MCode *top)

324. {

325. J->mctop = top;

326. mcode_protect(J, MCPROT_RUN);

327. }

329. /* Abort the reservation. */

330. ‌void lj_mcode_abort(jit_State *J)

331. {

332. if (J->mcarea)

333. mcode_protect(J, MCPROT_RUN);

334. }

336. /* Set/reset protection to allow patching of MCode areas. */

337. ‌MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish)

338. {

339. #ifdef LUAJIT_UNPROTECT_MCODE

340. UNUSED(J); UNUSED(ptr); UNUSED(finish);

341. return NULL;

342. #else

343. if (finish) {

344. if (J->mcarea == ptr)

345. mcode_protect(J, MCPROT_RUN);

346. else if (LJ_UNLIKELY(mcode_setprot(ptr, ((MCLink *)ptr)->size, MCPROT_RUN)))

347. mcode_protfail(J);

348. return NULL;

349. } else {

350. MCode *mc = J->mcarea;

351. /* Try current area first to use the protection cache. */

352. if (ptr >= mc && ptr < (MCode *)((char *)mc + J->szmcarea)) {

353. mcode_protect(J, MCPROT_GEN);

354. return mc;

355. }

356. /* Otherwise search through the list of MCode areas. */

357. for (;;) {

358. mc = ((MCLink *)mc)->next;

359. lua_assert(mc != NULL);

360. if (ptr >= mc && ptr < (MCode *)((char *)mc + ((MCLink *)mc)->size)) {

361. if (LJ_UNLIKELY(mcode_setprot(mc, ((MCLink *)mc)->size, MCPROT_GEN)))

362. mcode_protfail(J);

363. return mc;

364. }

365. }

366. }

367. #endif

368. }

370. /* Limit of MCode reservation reached. */

371. ‌void lj_mcode_limiterr(jit_State *J, size_t need)

372. {

373. size_t sizemcode, maxmcode;

374. lj_mcode_abort(J);

375. sizemcode = (size_t)J->param[JIT_P_sizemcode] << 10;

376. sizemcode = (sizemcode + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1);

377. maxmcode = (size_t)J->param[JIT_P_maxmcode] << 10;

378. if ((size_t)need > sizemcode)

379. lj_trace_err(J, LJ_TRERR_MCODEOV);  /* Too long for any area. */

380. if (J->szallmcarea + sizemcode > maxmcode)

381. lj_trace_err(J, LJ_TRERR_MCODEAL);

382. mcode_allocarea(J);

383. lj_trace_err(J, LJ_TRERR_MCODELM);  /* Retry with new area. */

384. }

386. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_emit_mips.h - luajit-2.0-src

Data types defined

- MCLabel

Functions defined

- emit_addptr

- emit_branch

- emit_call

- emit_dst

- emit_dta

- emit_fgh

- emit_jmp

- emit_kdelta1

- emit_loadi

- emit_loadofs

- emit_lsglptr

- emit_lsptr

- emit_movrr

- emit_rotr

- emit_storeofs

- emit_tsi

Macros defined

- emit_canremat

- emit_ds

- emit_fg

- emit_getgl

- emit_hsi

- emit_label

- emit_loada

- emit_loadn

- emit_move

- emit_setgl

- emit_setvmstate

- emit_spsub

- emit_tg

- emit_ti

Source code

1. /*

2. ** MIPS instruction emitter.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* -- Emit basic instructions --------------------------------------------- */

8. ‌static void emit_dst(ASMState *as, MIPSIns mi, Reg rd, Reg rs, Reg rt)

9. {

10. *--as->mcp = mi | MIPSF_D(rd) | MIPSF_S(rs) | MIPSF_T(rt);

11. }

13. ‌static void emit_dta(ASMState *as, MIPSIns mi, Reg rd, Reg rt, uint32_t a)

14. {

15. *--as->mcp = mi | MIPSF_D(rd) | MIPSF_T(rt) | MIPSF_A(a);

16. }

18. ‌#define emit_ds(as, mi, rd, rs)                emit_dst(as, (mi), (rd), (rs), 0)

19. ‌#define emit_tg(as, mi, rt, rg)                emit_dst(as, (mi), (rg)&31, 0, (rt))

21. ‌static void emit_tsi(ASMState *as, MIPSIns mi, Reg rt, Reg rs, int32_t i)

22. {

23. *--as->mcp = mi | MIPSF_T(rt) | MIPSF_S(rs) | (i & 0xffff);

24. }

26. ‌#define emit_ti(as, mi, rt, i)                emit_tsi(as, (mi), (rt), 0, (i))

27. ‌#define emit_hsi(as, mi, rh, rs, i)        emit_tsi(as, (mi), (rh) & 31, (rs), (i))

29. ‌static void emit_fgh(ASMState *as, MIPSIns mi, Reg rf, Reg rg, Reg rh)

30. {

31. *--as->mcp = mi | MIPSF_F(rf&31) | MIPSF_G(rg&31) | MIPSF_H(rh&31);

32. }

34. ‌#define emit_fg(as, mi, rf, rg)                emit_fgh(as, (mi), (rf), (rg), 0)

36. ‌static void emit_rotr(ASMState *as, Reg dest, Reg src, Reg tmp, uint32_t shift)

37. {

38. if ((as->flags & JIT_F_MIPS32R2)) {

39. emit_dta(as, MIPSI_ROTR, dest, src, shift);

40. } else {

41. emit_dst(as, MIPSI_OR, dest, dest, tmp);

42. emit_dta(as, MIPSI_SLL, dest, src, (-shift)&31);

43. emit_dta(as, MIPSI_SRL, tmp, src, shift);

44. }

45. }

47. /* -- Emit loads/stores --------------------------------------------------- */

49. /* Prefer rematerialization of BASE/L from global_State over spills. */

50. ‌#define emit_canremat(ref)        ((ref) <= REF_BASE)

52. /* Try to find a one step delta relative to another constant. */

53. ‌static int emit_kdelta1(ASMState *as, Reg t, int32_t i)

54. {

55. RegSet work = ~as->freeset & RSET_GPR;

56. while (work) {

57. Reg r = rset_picktop(work);

58. IRRef ref = regcost_ref(as->cost[r]);

59. lua_assert(r != t);

60. if (ref < ASMREF_L) {

61. int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i);

62. if (checki16(delta)) {

63. emit_tsi(as, MIPSI_ADDIU, t, r, delta);

64. return 1;

65. }

66. }

67. rset_clear(work, r);

68. }

69. return 0;  /* Failed. */

70. }

72. /* Load a 32 bit constant into a GPR. */

73. ‌static void emit_loadi(ASMState *as, Reg r, int32_t i)

74. {

75. if (checki16(i)) {

76. emit_ti(as, MIPSI_LI, r, i);

77. } else {

78. if ((i & 0xffff)) {

79. int32_t jgl = i32ptr(J2G(as->J));

80. if ((uint32_t)(i-jgl) < 65536) {

81. emit_tsi(as, MIPSI_ADDIU, r, RID_JGL, i-jgl-32768);

82. return;

83. } else if (emit_kdelta1(as, r, i)) {

84. return;

85. } else if ((i >> 16) == 0) {

86. emit_tsi(as, MIPSI_ORI, r, RID_ZERO, i);

87. return;

88. }

89. emit_tsi(as, MIPSI_ORI, r, r, i);

90. }

91. emit_ti(as, MIPSI_LUI, r, (i >> 16));

92. }

93. }

95. ‌#define emit_loada(as, r, addr)                emit_loadi(as, (r), i32ptr((addr)))

97. static Reg ra_allock(ASMState *as, int32_t k, RegSet allow);

98. static void ra_allockreg(ASMState *as, int32_t k, Reg r);

100. /* Get/set from constant pointer. */

101. ‌static void emit_lsptr(ASMState *as, MIPSIns mi, Reg r, void *p, RegSet allow)

102. {

103. int32_t jgl = i32ptr(J2G(as->J));

104. int32_t i = i32ptr(p);

105. Reg base;

106. if ((uint32_t)(i-jgl) < 65536) {

107. i = i-jgl-32768;

108. base = RID_JGL;

109. } else {

110. base = ra_allock(as, i-(int16_t)i, allow);

111. }

112. emit_tsi(as, mi, r, base, i);

113. }

115. ‌#define emit_loadn(as, r, tv) \

116. emit_lsptr(as, MIPSI_LDC1, ((r) & 31), (void *)(tv), RSET_GPR)

118. /* Get/set global_State fields. */

119. ‌static void emit_lsglptr(ASMState *as, MIPSIns mi, Reg r, int32_t ofs)

120. {

121. emit_tsi(as, mi, r, RID_JGL, ofs-32768);

122. }

124. ‌#define emit_getgl(as, r, field) \

125. emit_lsglptr(as, MIPSI_LW, (r), (int32_t)offsetof(global_State, field))

126. ‌#define emit_setgl(as, r, field) \

127. emit_lsglptr(as, MIPSI_SW, (r), (int32_t)offsetof(global_State, field))

129. /* Trace number is determined from per-trace exit stubs. */

130. ‌#define emit_setvmstate(as, i)                UNUSED(i)

132. /* -- Emit control-flow instructions -------------------------------------- */

134. /* Label for internal jumps. */

135. ‌typedef MCode *MCLabel;

137. /* Return label pointing to current PC. */

138. ‌#define emit_label(as)                ((as)->mcp)

140. ‌static void emit_branch(ASMState *as, MIPSIns mi, Reg rs, Reg rt, MCode *target)

141. {

142. MCode *p = as->mcp;

143. ptrdiff_t delta = target - p;

144. lua_assert(((delta + 0x8000) >> 16) == 0);

145. *--p = mi | MIPSF_S(rs) | MIPSF_T(rt) | ((uint32_t)delta & 0xffffu);

146. as->mcp = p;

147. }

149. ‌static void emit_jmp(ASMState *as, MCode *target)

150. {

151. *--as->mcp = MIPSI_NOP;

152. emit_branch(as, MIPSI_B, RID_ZERO, RID_ZERO, (target));

153. }

155. ‌static void emit_call(ASMState *as, void *target)

156. {

157. MCode *p = as->mcp;

158. *--p = MIPSI_NOP;

159. if ((((uintptr_t)target ^ (uintptr_t)p) >> 28) == 0)

160. *--p = MIPSI_JAL | (((uintptr_t)target >>2) & 0x03ffffffu);

161. else  /* Target out of range: need indirect call. */

162. *--p = MIPSI_JALR | MIPSF_S(RID_CFUNCADDR);

163. as->mcp = p;

164. ra_allockreg(as, i32ptr(target), RID_CFUNCADDR);

165. }

167. /* -- Emit generic operations --------------------------------------------- */

169. ‌#define emit_move(as, dst, src) \

170. emit_ds(as, MIPSI_MOVE, (dst), (src))

172. /* Generic move between two regs. */

173. ‌static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src)

174. {

175. if (dst < RID_MAX_GPR)

176. emit_move(as, dst, src);

177. else

178. emit_fg(as, irt_isnum(ir->t) ? MIPSI_MOV_D : MIPSI_MOV_S, dst, src);

179. }

181. /* Generic load of register with base and (small) offset address. */

182. ‌static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)

183. {

184. if (r < RID_MAX_GPR)

185. emit_tsi(as, MIPSI_LW, r, base, ofs);

186. else

187. emit_tsi(as, irt_isnum(ir->t) ? MIPSI_LDC1 : MIPSI_LWC1,

188. (r & 31), base, ofs);

189. }

191. /* Generic store of register with base and (small) offset address. */

192. ‌static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)

193. {

194. if (r < RID_MAX_GPR)

195. emit_tsi(as, MIPSI_SW, r, base, ofs);

196. else

197. emit_tsi(as, irt_isnum(ir->t) ? MIPSI_SDC1 : MIPSI_SWC1,

198. (r&31), base, ofs);

199. }

201. /* Add offset to pointer. */

202. ‌static void emit_addptr(ASMState *as, Reg r, int32_t ofs)

203. {

204. if (ofs) {

205. lua_assert(checki16(ofs));

206. emit_tsi(as, MIPSI_ADDIU, r, r, ofs);

207. }

208. }

210. ‌#define emit_spsub(as, ofs)        emit_addptr(as, RID_SP, -(ofs))

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_target_mips.h - luajit-2.0-src

Data types defined

- ExitState

- MIPSIns

- MIPSIns

Functions defined

- exitstub_trace_addr_

Macros defined

- EXITSTATE_CHECKEXIT

- FPRDEF

- GPRDEF

- MIPSF_A

- MIPSF_D

- MIPSF_F

- MIPSF_G

- MIPSF_H

- MIPSF_M

- MIPSF_R

- MIPSF_S

- MIPSF_T

- REGARG_FIRSTFPR

- REGARG_FIRSTGPR

- REGARG_LASTFPR

- REGARG_LASTGPR

- REGARG_NUMFPR

- REGARG_NUMGPR

- RIDENUM

- RID_MIN_KREF

- RID_NUM_KREF

- RSET_ALL

- RSET_FIXED

- RSET_FPR

- RSET_GPR

- RSET_INIT

- RSET_SCRATCH

- RSET_SCRATCH_FPR

- RSET_SCRATCH_GPR

- SPOFS_TMP

- SPS_FIRST

- SPS_FIXED

- VRIDDEF

- _LJ_TARGET_MIPS_H

- exitstub_trace_addr

- sps_align

- sps_scale

Source code

1. /*

2. ** Definitions for MIPS CPUs.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_TARGET_MIPS_H

7. ‌#define _LJ_TARGET_MIPS_H

9. /* -- Registers IDs ------------------------------------------------------- */

11. ‌#define GPRDEF(_) \

12. _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \

13. _(R8) _(R9) _(R10) _(R11) _(R12) _(R13) _(R14) _(R15) \

14. _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \

15. _(R24) _(R25) _(SYS1) _(SYS2) _(R28) _(SP) _(R30) _(RA)

16. ‌#define FPRDEF(_) \

17. _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \

18. _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \

19. _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \

20. _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31)

21. ‌#define VRIDDEF(_)

23. ‌#define RIDENUM(name)        RID_##name,

25. enum {

26. GPRDEF(RIDENUM)                /* General-purpose registers (GPRs). */

27. FPRDEF(RIDENUM)                /* Floating-point registers (FPRs). */

28. RID_MAX,

29. RID_ZERO = RID_R0,

30. RID_TMP = RID_RA,

32. /* Calling conventions. */

33. RID_RET = RID_R2,

34. #if LJ_LE

35. RID_RETHI = RID_R3,

36. RID_RETLO = RID_R2,

37. #else

38. RID_RETHI = RID_R2,

39. RID_RETLO = RID_R3,

40. #endif

41. RID_FPRET = RID_F0,

42. RID_CFUNCADDR = RID_R25,

44. /* These definitions must match with the *.dasc file(s): */

45. RID_BASE = RID_R16,                /* Interpreter BASE. */

46. RID_LPC = RID_R18,                /* Interpreter PC. */

47. RID_DISPATCH = RID_R19,        /* Interpreter DISPATCH table. */

48. RID_LREG = RID_R20,                /* Interpreter L. */

49. RID_JGL = RID_R30,                /* On-trace: global_State + 32768. */

51. /* Register ranges [min, max) and number of registers. */

52. RID_MIN_GPR = RID_R0,

53. RID_MAX_GPR = RID_RA+1,

54. RID_MIN_FPR = RID_F0,

55. RID_MAX_FPR = RID_F31+1,

56. RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR,

57. RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR        /* Only even regs are used. */

58. };

60. ‌#define RID_NUM_KREF                RID_NUM_GPR

61. ‌#define RID_MIN_KREF                RID_R0

63. /* -- Register sets ------------------------------------------------------- */

65. /* Make use of all registers, except ZERO, TMP, SP, SYS1, SYS2 and JGL. */

66. ‌#define RSET_FIXED \

67. (RID2RSET(RID_ZERO)|RID2RSET(RID_TMP)|RID2RSET(RID_SP)|\

68. RID2RSET(RID_SYS1)|RID2RSET(RID_SYS2)|RID2RSET(RID_JGL))

69. ‌#define RSET_GPR        (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED)

70. ‌#define RSET_FPR \

71. (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\

72. RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\

73. RID2RSET(RID_F16)|RID2RSET(RID_F18)|RID2RSET(RID_F20)|RID2RSET(RID_F22)|\

74. RID2RSET(RID_F24)|RID2RSET(RID_F26)|RID2RSET(RID_F28)|RID2RSET(RID_F30))

75. ‌#define RSET_ALL        (RSET_GPR|RSET_FPR)

76. ‌#define RSET_INIT        RSET_ALL

78. ‌#define RSET_SCRATCH_GPR \

79. (RSET_RANGE(RID_R1, RID_R15+1)|\

80. RID2RSET(RID_R24)|RID2RSET(RID_R25)|RID2RSET(RID_R28))

81. ‌#define RSET_SCRATCH_FPR \

82. (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\

83. RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\

84. RID2RSET(RID_F16)|RID2RSET(RID_F18))

85. ‌#define RSET_SCRATCH                (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR)

86. ‌#define REGARG_FIRSTGPR                RID_R4

87. ‌#define REGARG_LASTGPR                RID_R7

88. ‌#define REGARG_NUMGPR                4

89. ‌#define REGARG_FIRSTFPR                RID_F12

90. ‌#define REGARG_LASTFPR                RID_F14

91. ‌#define REGARG_NUMFPR                2

93. /* -- Spill slots --------------------------------------------------------- */

95. /* Spill slots are 32 bit wide. An even/odd pair is used for FPRs.

96. **

97. ** SPS_FIXED: Available fixed spill slots in interpreter frame.

98. ** This definition must match with the *.dasc file(s).

99. **

100. ** SPS_FIRST: First spill slot for general use.

101. */

102. ‌#define SPS_FIXED        5

103. ‌#define SPS_FIRST        4

105. ‌#define SPOFS_TMP        0

107. ‌#define sps_scale(slot)                (4 * (int32_t)(slot))

108. ‌#define sps_align(slot)                (((slot) - SPS_FIXED + 1) & ~1)

110. /* -- Exit state ---------------------------------------------------------- */

112. /* This definition must match with the *.dasc file(s). */

113. typedef struct {

114. lua_Number fpr[RID_NUM_FPR];        /* Floating-point registers. */

115. int32_t gpr[RID_NUM_GPR];        /* General-purpose registers. */

116. int32_t spill[256];                /* Spill slots. */

117. ‌} ExitState;

119. /* Highest exit + 1 indicates stack check. */

120. ‌#define EXITSTATE_CHECKEXIT        1

122. /* Return the address of a per-trace exit stub. */

123. ‌static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p)

124. {

125. while (*p == 0x00000000) p++;  /* Skip MIPSI_NOP. */

126. return p;

127. }

128. /* Avoid dependence on lj_jit.h if only including lj_target.h. */

129. ‌#define exitstub_trace_addr(T, exitno) \

130. exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode))

132. /* -- Instructions -------------------------------------------------------- */

134. /* Instruction fields. */

135. ‌#define MIPSF_S(r)        ((r) << 21)

136. ‌#define MIPSF_T(r)        ((r) << 16)

137. ‌#define MIPSF_D(r)        ((r) << 11)

138. ‌#define MIPSF_R(r)        ((r) << 21)

139. ‌#define MIPSF_H(r)        ((r) << 16)

140. ‌#define MIPSF_G(r)        ((r) << 11)

141. ‌#define MIPSF_F(r)        ((r) << 6)

142. ‌#define MIPSF_A(n)        ((n) << 6)

143. ‌#define MIPSF_M(n)        ((n) << 11)

145. ‌typedef enum MIPSIns {

146. /* Integer instructions. */

147. MIPSI_MOVE = 0x00000021,

148. MIPSI_NOP = 0x00000000,

150. MIPSI_LI = 0x24000000,

151. MIPSI_LU = 0x34000000,

152. MIPSI_LUI = 0x3c000000,

154. MIPSI_ADDIU = 0x24000000,

155. MIPSI_ANDI = 0x30000000,

156. MIPSI_ORI = 0x34000000,

157. MIPSI_XORI = 0x38000000,

158. MIPSI_SLTI = 0x28000000,

159. MIPSI_SLTIU = 0x2c000000,

161. MIPSI_ADDU = 0x00000021,

162. MIPSI_SUBU = 0x00000023,

163. MIPSI_MUL = 0x70000002,

164. MIPSI_AND = 0x00000024,

165. MIPSI_OR = 0x00000025,

166. MIPSI_XOR = 0x00000026,

167. MIPSI_NOR = 0x00000027,

168. MIPSI_SLT = 0x0000002a,

169. MIPSI_SLTU = 0x0000002b,

170. MIPSI_MOVZ = 0x0000000a,

171. MIPSI_MOVN = 0x0000000b,

172. MIPSI_MFHI = 0x00000010,

173. MIPSI_MFLO = 0x00000012,

174. MIPSI_MULT = 0x00000018,

176. MIPSI_SLL = 0x00000000,

177. MIPSI_SRL = 0x00000002,

178. MIPSI_SRA = 0x00000003,

179. MIPSI_ROTR = 0x00200002,        /* MIPS32R2 */

180. MIPSI_SLLV = 0x00000004,

181. MIPSI_SRLV = 0x00000006,

182. MIPSI_SRAV = 0x00000007,

183. MIPSI_ROTRV = 0x00000046,        /* MIPS32R2 */

185. MIPSI_SEB = 0x7c000420,        /* MIPS32R2 */

186. MIPSI_SEH = 0x7c000620,        /* MIPS32R2 */

187. MIPSI_WSBH = 0x7c0000a0,        /* MIPS32R2 */

189. MIPSI_B = 0x10000000,

190. MIPSI_J = 0x08000000,

191. MIPSI_JAL = 0x0c000000,

192. MIPSI_JR = 0x00000008,

193. MIPSI_JALR = 0x0000f809,

195. MIPSI_BEQ = 0x10000000,

196. MIPSI_BNE = 0x14000000,

197. MIPSI_BLEZ = 0x18000000,

198. MIPSI_BGTZ = 0x1c000000,

199. MIPSI_BLTZ = 0x04000000,

200. MIPSI_BGEZ = 0x04010000,

202. /* Load/store instructions. */

203. MIPSI_LW = 0x8c000000,

204. MIPSI_SW = 0xac000000,

205. MIPSI_LB = 0x80000000,

206. MIPSI_SB = 0xa0000000,

207. MIPSI_LH = 0x84000000,

208. MIPSI_SH = 0xa4000000,

209. MIPSI_LBU = 0x90000000,

210. MIPSI_LHU = 0x94000000,

211. MIPSI_LWC1 = 0xc4000000,

212. MIPSI_SWC1 = 0xe4000000,

213. MIPSI_LDC1 = 0xd4000000,

214. MIPSI_SDC1 = 0xf4000000,

216. /* FP instructions. */

217. MIPSI_MOV_S = 0x46000006,

218. MIPSI_MOV_D = 0x46200006,

219. MIPSI_MOVT_D = 0x46210011,

220. MIPSI_MOVF_D = 0x46200011,

222. MIPSI_ABS_D = 0x46200005,

223. MIPSI_NEG_D = 0x46200007,

225. MIPSI_ADD_D = 0x46200000,

226. MIPSI_SUB_D = 0x46200001,

227. MIPSI_MUL_D = 0x46200002,

228. MIPSI_DIV_D = 0x46200003,

229. MIPSI_SQRT_D = 0x46200004,

231. MIPSI_ADD_S = 0x46000000,

232. MIPSI_SUB_S = 0x46000001,

234. MIPSI_CVT_D_S = 0x46000021,

235. MIPSI_CVT_W_S = 0x46000024,

236. MIPSI_CVT_S_D = 0x46200020,

237. MIPSI_CVT_W_D = 0x46200024,

238. MIPSI_CVT_S_W = 0x46800020,

239. MIPSI_CVT_D_W = 0x46800021,

241. MIPSI_TRUNC_W_S = 0x4600000d,

242. MIPSI_TRUNC_W_D = 0x4620000d,

243. MIPSI_FLOOR_W_S = 0x4600000f,

244. MIPSI_FLOOR_W_D = 0x4620000f,

246. MIPSI_MFC1 = 0x44000000,

247. MIPSI_MTC1 = 0x44800000,

249. MIPSI_BC1F = 0x45000000,

250. MIPSI_BC1T = 0x45010000,

252. MIPSI_C_EQ_D = 0x46200032,

253. MIPSI_C_OLT_D = 0x46200034,

254. MIPSI_C_ULT_D = 0x46200035,

255. MIPSI_C_OLE_D = 0x46200036,

256. MIPSI_C_ULE_D = 0x46200037,

258. ‌} MIPSIns;

260. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_emit_x86.h - luajit-2.0-src

Data types defined

- MCLabel

Functions defined

- emit_addptr

- emit_call_

- emit_gmrmi

- emit_gmroi

- emit_gri

- emit_jcc

- emit_jmp

- emit_loadi

- emit_loadn

- emit_loadofs

- emit_loadu64

- emit_movmroi

- emit_movrr

- emit_mrm

- emit_op

- emit_rma

- emit_rmro

- emit_rmrxo

- emit_rr

- emit_sfixup

- emit_sjcc

- emit_sjcc_label

- emit_sjmp

- emit_storeofs

- jmprel

- ptr2addr

Macros defined

- FORCE_REX

- FORCE_REX

- MODRM

- REXRB

- REXRB

- REX_64

- REX_64

- REX_64IR

- REX_64IR

- emit_call

- emit_canremat

- emit_getgl

- emit_i32

- emit_i8

- emit_label

- emit_loada

- emit_movtomro

- emit_opgl

- emit_opm

- emit_opmx

- emit_setgl

- emit_setvmstate

- emit_shifti

- emit_spsub

- emit_u32

- emit_x87op

- ptr2addr

Source code

1. /*

2. ** x86/x64 instruction emitter.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* -- Emit basic instructions --------------------------------------------- */

8. ‌#define MODRM(mode, r1, r2)        ((MCode)((mode)+(((r1)&7)<<3)+((r2)&7)))

10. #if LJ_64

11. ‌#define REXRB(p, rr, rb) \

12. { MCode rex = 0x40 + (((rr)>>1)&4) + (((rb)>>3)&1); \

13. if (rex != 0x40) *--(p) = rex; }

14. ‌#define FORCE_REX                0x200

15. ‌#define REX_64                        (FORCE_REX|0x080000)

16. #else

17. ‌#define REXRB(p, rr, rb)        ((void)0)

18. ‌#define FORCE_REX                0

19. ‌#define REX_64                        0

20. #endif

22. ‌#define emit_i8(as, i)                (*--as->mcp = (MCode)(i))

23. ‌#define emit_i32(as, i)                (*(int32_t *)(as->mcp-4) = (i), as->mcp -= 4)

24. ‌#define emit_u32(as, u)                (*(uint32_t *)(as->mcp-4) = (u), as->mcp -= 4)

26. ‌#define emit_x87op(as, xo) \

27. (*(uint16_t *)(as->mcp-2) = (uint16_t)(xo), as->mcp -= 2)

29. /* op */

30. ‌static LJ_AINLINE MCode *emit_op(x86Op xo, Reg rr, Reg rb, Reg rx,

31. MCode *p, int delta)

32. {

33. int n = (int8_t)xo;

34. #if defined(__GNUC__)

35. if (__builtin_constant_p(xo) && n == -2)

36. p[delta-2] = (MCode)(xo >> 24);

37. else if (__builtin_constant_p(xo) && n == -3)

38. *(uint16_t *)(p+delta-3) = (uint16_t)(xo >> 16);

39. else

40. #endif

41. *(uint32_t *)(p+delta-5) = (uint32_t)xo;

42. p += n + delta;

43. #if LJ_64

44. {

45. uint32_t rex = 0x40 + ((rr>>1)&(4+(FORCE_REX>>1)))+((rx>>2)&2)+((rb>>3)&1);

46. if (rex != 0x40) {

47. rex |= (rr >> 16);

48. if (n == -4) { *p = (MCode)rex; rex = (MCode)(xo >> 8); }

49. else if ((xo & 0xffffff) == 0x6600fd) { *p = (MCode)rex; rex = 0x66; }

50. *--p = (MCode)rex;

51. }

52. }

53. #else

54. UNUSED(rr); UNUSED(rb); UNUSED(rx);

55. #endif

56. return p;

57. }

59. /* op + modrm */

60. ‌#define emit_opm(xo, mode, rr, rb, p, delta) \

61. (p[(delta)-1] = MODRM((mode), (rr), (rb)), \

62. emit_op((xo), (rr), (rb), 0, (p), (delta)))

64. /* op + modrm + sib */

65. ‌#define emit_opmx(xo, mode, scale, rr, rb, rx, p) \

66. (p[-1] = MODRM((scale), (rx), (rb)), \

67. p[-2] = MODRM((mode), (rr), RID_ESP), \

68. emit_op((xo), (rr), (rb), (rx), (p), -1))

70. /* op r1, r2 */

71. ‌static void emit_rr(ASMState *as, x86Op xo, Reg r1, Reg r2)

72. {

73. MCode *p = as->mcp;

74. as->mcp = emit_opm(xo, XM_REG, r1, r2, p, 0);

75. }

77. #if LJ_64 && defined(LUA_USE_ASSERT)

78. /* [addr] is sign-extended in x64 and must be in lower 2G (not 4G). */

79. ‌static int32_t ptr2addr(const void *p)

80. {

81. lua_assert((uintptr_t)p < (uintptr_t)0x80000000);

82. return i32ptr(p);

83. }

84. #else

85. ‌#define ptr2addr(p)        (i32ptr((p)))

86. #endif

88. /* op r, [addr] */

89. ‌static void emit_rma(ASMState *as, x86Op xo, Reg rr, const void *addr)

90. {

91. MCode *p = as->mcp;

92. *(int32_t *)(p-4) = ptr2addr(addr);

93. #if LJ_64

94. p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP);

95. as->mcp = emit_opm(xo, XM_OFS0, rr, RID_ESP, p, -5);

96. #else

97. as->mcp = emit_opm(xo, XM_OFS0, rr, RID_EBP, p, -4);

98. #endif

99. }

101. /* op r, [base+ofs] */

102. ‌static void emit_rmro(ASMState *as, x86Op xo, Reg rr, Reg rb, int32_t ofs)

103. {

104. MCode *p = as->mcp;

105. x86Mode mode;

106. if (ra_hasreg(rb)) {

107. if (ofs == 0 && (rb&7) != RID_EBP) {

108. mode = XM_OFS0;

109. } else if (checki8(ofs)) {

110. *--p = (MCode)ofs;

111. mode = XM_OFS8;

112. } else {

113. p -= 4;

114. *(int32_t *)p = ofs;

115. mode = XM_OFS32;

116. }

117. if ((rb&7) == RID_ESP)

118. *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP);

119. } else {

120. *(int32_t *)(p-4) = ofs;

121. #if LJ_64

122. p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP);

123. p -= 5;

124. rb = RID_ESP;

125. #else

126. p -= 4;

127. rb = RID_EBP;

128. #endif

129. mode = XM_OFS0;

130. }

131. as->mcp = emit_opm(xo, mode, rr, rb, p, 0);

132. }

134. /* op r, [base+idx*scale+ofs] */

135. ‌static void emit_rmrxo(ASMState *as, x86Op xo, Reg rr, Reg rb, Reg rx,

136. x86Mode scale, int32_t ofs)

137. {

138. MCode *p = as->mcp;

139. x86Mode mode;

140. if (ofs == 0 && (rb&7) != RID_EBP) {

141. mode = XM_OFS0;

142. } else if (checki8(ofs)) {

143. mode = XM_OFS8;

144. *--p = (MCode)ofs;

145. } else {

146. mode = XM_OFS32;

147. p -= 4;

148. *(int32_t *)p = ofs;

149. }

150. as->mcp = emit_opmx(xo, mode, scale, rr, rb, rx, p);

151. }

153. /* op r, i */

154. ‌static void emit_gri(ASMState *as, x86Group xg, Reg rb, int32_t i)

155. {

156. MCode *p = as->mcp;

157. x86Op xo;

158. if (checki8(i)) {

159. *--p = (MCode)i;

160. xo = XG_TOXOi8(xg);

161. } else {

162. p -= 4;

163. *(int32_t *)p = i;

164. xo = XG_TOXOi(xg);

165. }

166. as->mcp = emit_opm(xo, XM_REG, (Reg)(xg & 7) | (rb & REX_64), rb, p, 0);

167. }

169. /* op [base+ofs], i */

170. ‌static void emit_gmroi(ASMState *as, x86Group xg, Reg rb, int32_t ofs,

171. int32_t i)

172. {

173. x86Op xo;

174. if (checki8(i)) {

175. emit_i8(as, i);

176. xo = XG_TOXOi8(xg);

177. } else {

178. emit_i32(as, i);

179. xo = XG_TOXOi(xg);

180. }

181. emit_rmro(as, xo, (Reg)(xg & 7), rb, ofs);

182. }

184. ‌#define emit_shifti(as, xg, r, i) \

185. (emit_i8(as, (i)), emit_rr(as, XO_SHIFTi, (Reg)(xg), (r)))

187. /* op r, rm/mrm */

188. ‌static void emit_mrm(ASMState *as, x86Op xo, Reg rr, Reg rb)

189. {

190. MCode *p = as->mcp;

191. x86Mode mode = XM_REG;

192. if (rb == RID_MRM) {

193. rb = as->mrm.base;

194. if (rb == RID_NONE) {

195. rb = RID_EBP;

196. mode = XM_OFS0;

197. p -= 4;

198. *(int32_t *)p = as->mrm.ofs;

199. if (as->mrm.idx != RID_NONE)

200. goto mrmidx;

201. #if LJ_64

202. *--p = MODRM(XM_SCALE1, RID_ESP, RID_EBP);

203. rb = RID_ESP;

204. #endif

205. } else {

206. if (as->mrm.ofs == 0 && (rb&7) != RID_EBP) {

207. mode = XM_OFS0;

208. } else if (checki8(as->mrm.ofs)) {

209. *--p = (MCode)as->mrm.ofs;

210. mode = XM_OFS8;

211. } else {

212. p -= 4;

213. *(int32_t *)p = as->mrm.ofs;

214. mode = XM_OFS32;

215. }

216. if (as->mrm.idx != RID_NONE) {

217. mrmidx:

218. as->mcp = emit_opmx(xo, mode, as->mrm.scale, rr, rb, as->mrm.idx, p);

219. return;

220. }

221. if ((rb&7) == RID_ESP)

222. *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP);

223. }

224. }

225. as->mcp = emit_opm(xo, mode, rr, rb, p, 0);

226. }

228. /* op rm/mrm, i */

229. ‌static void emit_gmrmi(ASMState *as, x86Group xg, Reg rb, int32_t i)

230. {

231. x86Op xo;

232. if (checki8(i)) {

233. emit_i8(as, i);

234. xo = XG_TOXOi8(xg);

235. } else {

236. emit_i32(as, i);

237. xo = XG_TOXOi(xg);

238. }

239. emit_mrm(as, xo, (Reg)(xg & 7) | (rb & REX_64), (rb & ~REX_64));

240. }

242. /* -- Emit loads/stores --------------------------------------------------- */

244. /* mov [base+ofs], i */

245. ‌static void emit_movmroi(ASMState *as, Reg base, int32_t ofs, int32_t i)

246. {

247. emit_i32(as, i);

248. emit_rmro(as, XO_MOVmi, 0, base, ofs);

249. }

251. /* mov [base+ofs], r */

252. ‌#define emit_movtomro(as, r, base, ofs) \

253. emit_rmro(as, XO_MOVto, (r), (base), (ofs))

255. /* Get/set global_State fields. */

256. ‌#define emit_opgl(as, xo, r, field) \

257. emit_rma(as, (xo), (r), (void *)&J2G(as->J)->field)

258. ‌#define emit_getgl(as, r, field)        emit_opgl(as, XO_MOV, (r), field)

259. ‌#define emit_setgl(as, r, field)        emit_opgl(as, XO_MOVto, (r), field)

261. ‌#define emit_setvmstate(as, i) \

262. (emit_i32(as, i), emit_opgl(as, XO_MOVmi, 0, vmstate))

264. /* mov r, i / xor r, r */

265. ‌static void emit_loadi(ASMState *as, Reg r, int32_t i)

266. {

267. /* XOR r,r is shorter, but modifies the flags. This is bad for HIOP. */

268. if (i == 0 && !(LJ_32 && (IR(as->curins)->o == IR_HIOP ||

269. (as->curins+1 < as->T->nins &&

270. IR(as->curins+1)->o == IR_HIOP)))) {

271. emit_rr(as, XO_ARITH(XOg_XOR), r, r);

272. } else {

273. MCode *p = as->mcp;

274. *(int32_t *)(p-4) = i;

275. p[-5] = (MCode)(XI_MOVri+(r&7));

276. p -= 5;

277. REXRB(p, 0, r);

278. as->mcp = p;

279. }

280. }

282. /* mov r, addr */

283. ‌#define emit_loada(as, r, addr) \

284. emit_loadi(as, (r), ptr2addr((addr)))

286. #if LJ_64

287. /* mov r, imm64 or shorter 32 bit extended load. */

288. ‌static void emit_loadu64(ASMState *as, Reg r, uint64_t u64)

289. {

290. if (checku32(u64)) {  /* 32 bit load clears upper 32 bits. */

291. emit_loadi(as, r, (int32_t)u64);

292. } else if (checki32((int64_t)u64)) {  /* Sign-extended 32 bit load. */

293. MCode *p = as->mcp;

294. *(int32_t *)(p-4) = (int32_t)u64;

295. as->mcp = emit_opm(XO_MOVmi, XM_REG, REX_64, r, p, -4);

296. } else {  /* Full-size 64 bit load. */

297. MCode *p = as->mcp;

298. *(uint64_t *)(p-8) = u64;

299. p[-9] = (MCode)(XI_MOVri+(r&7));

300. p[-10] = 0x48 + ((r>>3)&1);

301. p -= 10;

302. as->mcp = p;

303. }

304. }

305. #endif

307. /* movsd r, [&tv->n] / xorps r, r */

308. ‌static void emit_loadn(ASMState *as, Reg r, cTValue *tv)

309. {

310. if (tvispzero(tv))  /* Use xor only for +0. */

311. emit_rr(as, XO_XORPS, r, r);

312. else

313. emit_rma(as, XO_MOVSD, r, &tv->n);

314. }

316. /* -- Emit control-flow instructions -------------------------------------- */

318. /* Label for short jumps. */

319. ‌typedef MCode *MCLabel;

321. #if LJ_32 && LJ_HASFFI

322. /* jmp short target */

323. ‌static void emit_sjmp(ASMState *as, MCLabel target)

324. {

325. MCode *p = as->mcp;

326. ptrdiff_t delta = target - p;

327. lua_assert(delta == (int8_t)delta);

328. p[-1] = (MCode)(int8_t)delta;

329. p[-2] = XI_JMPs;

330. as->mcp = p - 2;

331. }

332. #endif

334. /* jcc short target */

335. ‌static void emit_sjcc(ASMState *as, int cc, MCLabel target)

336. {

337. MCode *p = as->mcp;

338. ptrdiff_t delta = target - p;

339. lua_assert(delta == (int8_t)delta);

340. p[-1] = (MCode)(int8_t)delta;

341. p[-2] = (MCode)(XI_JCCs+(cc&15));

342. as->mcp = p - 2;

343. }

345. /* jcc short (pending target) */

346. ‌static MCLabel emit_sjcc_label(ASMState *as, int cc)

347. {

348. MCode *p = as->mcp;

349. p[-1] = 0;

350. p[-2] = (MCode)(XI_JCCs+(cc&15));

351. as->mcp = p - 2;

352. return p;

353. }

355. /* Fixup jcc short target. */

356. ‌static void emit_sfixup(ASMState *as, MCLabel source)

357. {

358. source[-1] = (MCode)(as->mcp-source);

359. }

361. /* Return label pointing to current PC. */

362. ‌#define emit_label(as)                ((as)->mcp)

364. /* Compute relative 32 bit offset for jump and call instructions. */

365. ‌static LJ_AINLINE int32_t jmprel(MCode *p, MCode *target)

366. {

367. ptrdiff_t delta = target - p;

368. lua_assert(delta == (int32_t)delta);

369. return (int32_t)delta;

370. }

372. /* jcc target */

373. ‌static void emit_jcc(ASMState *as, int cc, MCode *target)

374. {

375. MCode *p = as->mcp;

376. *(int32_t *)(p-4) = jmprel(p, target);

377. p[-5] = (MCode)(XI_JCCn+(cc&15));

378. p[-6] = 0x0f;

379. as->mcp = p - 6;

380. }

382. /* jmp target */

383. ‌static void emit_jmp(ASMState *as, MCode *target)

384. {

385. MCode *p = as->mcp;

386. *(int32_t *)(p-4) = jmprel(p, target);

387. p[-5] = XI_JMP;

388. as->mcp = p - 5;

389. }

391. /* call target */

392. ‌static void emit_call_(ASMState *as, MCode *target)

393. {

394. MCode *p = as->mcp;

395. #if LJ_64

396. if (target-p != (int32_t)(target-p)) {

397. /* Assumes RID_RET is never an argument to calls and always clobbered. */

398. emit_rr(as, XO_GROUP5, XOg_CALL, RID_RET);

399. emit_loadu64(as, RID_RET, (uint64_t)target);

400. return;

401. }

402. #endif

403. *(int32_t *)(p-4) = jmprel(p, target);

404. p[-5] = XI_CALL;

405. as->mcp = p - 5;

406. }

408. ‌#define emit_call(as, f)        emit_call_(as, (MCode *)(void *)(f))

410. /* -- Emit generic operations --------------------------------------------- */

412. /* Use 64 bit operations to handle 64 bit IR types. */

413. #if LJ_64

414. ‌#define REX_64IR(ir, r)                ((r) + (irt_is64((ir)->t) ? REX_64 : 0))

415. #else

416. ‌#define REX_64IR(ir, r)                (r)

417. #endif

419. /* Generic move between two regs. */

420. ‌static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src)

421. {

422. UNUSED(ir);

423. if (dst < RID_MAX_GPR)

424. emit_rr(as, XO_MOV, REX_64IR(ir, dst), src);

425. else

426. emit_rr(as, XO_MOVAPS, dst, src);

427. }

429. /* Generic load of register with base and (small) offset address. */

430. ‌static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)

431. {

432. if (r < RID_MAX_GPR)

433. emit_rmro(as, XO_MOV, REX_64IR(ir, r), base, ofs);

434. else

435. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSD : XO_MOVSS, r, base, ofs);

436. }

438. /* Generic store of register with base and (small) offset address. */

439. ‌static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)

440. {

441. if (r < RID_MAX_GPR)

442. emit_rmro(as, XO_MOVto, REX_64IR(ir, r), base, ofs);

443. else

444. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto, r, base, ofs);

445. }

447. /* Add offset to pointer. */

448. ‌static void emit_addptr(ASMState *as, Reg r, int32_t ofs)

449. {

450. if (ofs) {

451. if ((as->flags & JIT_F_LEA_AGU))

452. emit_rmro(as, XO_LEA, r, r, ofs);

453. else

454. emit_gri(as, XG_ARITHi(XOg_ADD), r, ofs);

455. }

456. }

458. ‌#define emit_spsub(as, ofs)        emit_addptr(as, RID_ESP|REX_64, -(ofs))

460. /* Prefer rematerialization of BASE/L from global_State over spills. */

461. ‌#define emit_canremat(ref)        ((ref) <= REF_BASE)

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_iropt.h - luajit-2.0-src

Functions defined

- lj_ir_knum

- lj_ir_nextins

- lj_ir_set_

Macros defined

- CONDFOLD

- CSEFOLD

- EMITFOLD

- INT64FOLD

- INTFOLD

- LEFTFOLD

- RIGHTFOLD

- _LJ_IROPT_H

- lj_ir_kfunc

- lj_ir_kintp

- lj_ir_kintp

- lj_ir_kkptr

- lj_ir_knum_abs

- lj_ir_knum_neg

- lj_ir_knum_one

- lj_ir_knum_tobit

- lj_ir_knum_zero

- lj_ir_kptr

- lj_ir_kstr

- lj_ir_ktab

- lj_ir_set

- lj_opt_split

Source code

1. /*

2. ** Common header for IR emitter and optimizations.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_IROPT_H

7. ‌#define _LJ_IROPT_H

9. #include <stdarg.h>

11. #include "lj_obj.h"

12. #include "lj_jit.h"

14. #if LJ_HASJIT

15. /* IR emitter. */

16. LJ_FUNC void LJ_FASTCALL lj_ir_growtop(jit_State *J);

17. LJ_FUNC TRef LJ_FASTCALL lj_ir_emit(jit_State *J);

19. /* Save current IR in J->fold.ins, but do not emit it (yet). */

20. ‌static LJ_AINLINE void lj_ir_set_(jit_State *J, uint16_t ot, IRRef1 a, IRRef1 b)

21. {

22. J->fold.ins.ot = ot; J->fold.ins.op1 = a; J->fold.ins.op2 = b;

23. }

25. ‌#define lj_ir_set(J, ot, a, b) \

26. lj_ir_set_(J, (uint16_t)(ot), (IRRef1)(a), (IRRef1)(b))

28. /* Get ref of next IR instruction and optionally grow IR.

29. ** Note: this may invalidate all IRIns*!

30. */

31. ‌static LJ_AINLINE IRRef lj_ir_nextins(jit_State *J)

32. {

33. IRRef ref = J->cur.nins;

34. if (LJ_UNLIKELY(ref >= J->irtoplim)) lj_ir_growtop(J);

35. J->cur.nins = ref + 1;

36. return ref;

37. }

39. /* Interning of constants. */

40. LJ_FUNC TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k);

41. LJ_FUNC void lj_ir_k64_freeall(jit_State *J);

42. LJ_FUNC TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv);

43. LJ_FUNC cTValue *lj_ir_k64_find(jit_State *J, uint64_t u64);

44. LJ_FUNC TRef lj_ir_knum_u64(jit_State *J, uint64_t u64);

45. LJ_FUNC TRef lj_ir_knumint(jit_State *J, lua_Number n);

46. LJ_FUNC TRef lj_ir_kint64(jit_State *J, uint64_t u64);

47. LJ_FUNC TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t);

48. LJ_FUNC TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr);

49. LJ_FUNC TRef lj_ir_knull(jit_State *J, IRType t);

50. LJ_FUNC TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot);

52. #if LJ_64

53. ‌#define lj_ir_kintp(J, k)        lj_ir_kint64(J, (uint64_t)(k))

54. #else

55. ‌#define lj_ir_kintp(J, k)        lj_ir_kint(J, (int32_t)(k))

56. #endif

58. ‌static LJ_AINLINE TRef lj_ir_knum(jit_State *J, lua_Number n)

59. {

60. TValue tv;

61. tv.n = n;

62. return lj_ir_knum_u64(J, tv.u64);

63. }

65. ‌#define lj_ir_kstr(J, str)        lj_ir_kgc(J, obj2gco((str)), IRT_STR)

66. ‌#define lj_ir_ktab(J, tab)        lj_ir_kgc(J, obj2gco((tab)), IRT_TAB)

67. ‌#define lj_ir_kfunc(J, func)        lj_ir_kgc(J, obj2gco((func)), IRT_FUNC)

68. ‌#define lj_ir_kptr(J, ptr)        lj_ir_kptr_(J, IR_KPTR, (ptr))

69. ‌#define lj_ir_kkptr(J, ptr)        lj_ir_kptr_(J, IR_KKPTR, (ptr))

71. /* Special FP constants. */

72. ‌#define lj_ir_knum_zero(J)        lj_ir_knum_u64(J, U64x(00000000,00000000))

73. ‌#define lj_ir_knum_one(J)        lj_ir_knum_u64(J, U64x(3ff00000,00000000))

74. ‌#define lj_ir_knum_tobit(J)        lj_ir_knum_u64(J, U64x(43380000,00000000))

76. /* Special 128 bit SIMD constants. */

77. ‌#define lj_ir_knum_abs(J)        lj_ir_k64(J, IR_KNUM, LJ_KSIMD(J, LJ_KSIMD_ABS))

78. ‌#define lj_ir_knum_neg(J)        lj_ir_k64(J, IR_KNUM, LJ_KSIMD(J, LJ_KSIMD_NEG))

80. /* Access to constants. */

81. LJ_FUNC void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir);

83. /* Convert IR operand types. */

84. LJ_FUNC TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr);

85. LJ_FUNC TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr);

86. LJ_FUNC TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr);

88. /* Miscellaneous IR ops. */

89. LJ_FUNC int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op);

90. LJ_FUNC int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op);

91. LJ_FUNC void lj_ir_rollback(jit_State *J, IRRef ref);

93. /* Emit IR instructions with on-the-fly optimizations. */

94. LJ_FUNC TRef LJ_FASTCALL lj_opt_fold(jit_State *J);

95. LJ_FUNC TRef LJ_FASTCALL lj_opt_cse(jit_State *J);

96. LJ_FUNC TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim);

98. /* Special return values for the fold functions. */

99. enum {

100. NEXTFOLD,                /* Couldn't fold, pass on. */

101. RETRYFOLD,                /* Retry fold with modified fins. */

102. KINTFOLD,                /* Return ref for int constant in fins->i. */

103. FAILFOLD,                /* Guard would always fail. */

104. DROPFOLD,                /* Guard eliminated. */

105. MAX_FOLD

106. };

108. ‌#define INTFOLD(k)        ((J->fold.ins.i = (k)), (TRef)KINTFOLD)

109. ‌#define INT64FOLD(k)        (lj_ir_kint64(J, (k)))

110. ‌#define CONDFOLD(cond)        ((TRef)FAILFOLD + (TRef)(cond))

111. ‌#define LEFTFOLD        (J->fold.ins.op1)

112. ‌#define RIGHTFOLD        (J->fold.ins.op2)

113. ‌#define CSEFOLD                (lj_opt_cse(J))

114. ‌#define EMITFOLD        (lj_ir_emit(J))

116. /* Load/store forwarding. */

117. LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J);

118. LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J);

119. LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J);

120. LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J);

121. LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J);

122. LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_tab_len(jit_State *J);

123. LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J);

124. LJ_FUNC int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J);

125. LJ_FUNC int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim);

126. LJ_FUNC int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref);

128. /* Dead-store elimination. */

129. LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J);

130. LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J);

131. LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J);

132. LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J);

134. /* Narrowing. */

135. LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_convert(jit_State *J);

136. LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_index(jit_State *J, TRef key);

137. LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_toint(jit_State *J, TRef tr);

138. LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_tobit(jit_State *J, TRef tr);

139. #if LJ_HASFFI

140. LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_cindex(jit_State *J, TRef key);

141. #endif

142. LJ_FUNC TRef lj_opt_narrow_arith(jit_State *J, TRef rb, TRef rc,

143. TValue *vb, TValue *vc, IROp op);

144. LJ_FUNC TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc);

145. LJ_FUNC TRef lj_opt_narrow_mod(jit_State *J, TRef rb, TRef rc, TValue *vc);

146. LJ_FUNC TRef lj_opt_narrow_pow(jit_State *J, TRef rb, TRef rc, TValue *vc);

147. LJ_FUNC IRType lj_opt_narrow_forl(jit_State *J, cTValue *forbase);

149. /* Optimization passes. */

150. LJ_FUNC void lj_opt_dce(jit_State *J);

151. LJ_FUNC int lj_opt_loop(jit_State *J);

152. #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)

153. LJ_FUNC void lj_opt_split(jit_State *J);

154. #else

155. ‌#define lj_opt_split(J)                UNUSED(J)

156. #endif

157. LJ_FUNC void lj_opt_sink(jit_State *J);

159. #endif

161. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ir.c - luajit-2.0-src

Global variables defined

- lj_ir_callinfo

- lj_ir_mode

- lj_ir_type_size

Data types defined

- K64Array

- K64Array

Functions defined

- ir_nextk

- lj_ir_call

- lj_ir_emit

- lj_ir_growbot

- lj_ir_growtop

- lj_ir_k64

- lj_ir_k64_find

- lj_ir_k64_freeall

- lj_ir_kgc

- lj_ir_kint

- lj_ir_kint64

- lj_ir_knull

- lj_ir_knum_u64

- lj_ir_knumint

- lj_ir_kptr_

- lj_ir_kslot

- lj_ir_kvalue

- lj_ir_numcmp

- lj_ir_rollback

- lj_ir_strcmp

- lj_ir_tonum

- lj_ir_tonumber

- lj_ir_tostr

- numistrueint

Macros defined

- IR

- IR

- IRCALLCI

- IRCALLCI

- IRTSIZE

- IRTSIZE

- LUA_CORE

- emitir

- emitir

- fins

- fins

- lj_ir_c

Source code

1. /*

2. ** SSA IR (Intermediate Representation) emitter.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_ir_c

7. ‌#define LUA_CORE

9. /* For pointers to libc/libm functions. */

10. #include <stdio.h>

11. #include <math.h>

13. #include "lj_obj.h"

15. #if LJ_HASJIT

17. #include "lj_gc.h"

18. #include "lj_buf.h"

19. #include "lj_str.h"

20. #include "lj_tab.h"

21. #include "lj_ir.h"

22. #include "lj_jit.h"

23. #include "lj_ircall.h"

24. #include "lj_iropt.h"

25. #include "lj_trace.h"

26. #if LJ_HASFFI

27. #include "lj_ctype.h"

28. #include "lj_cdata.h"

29. #include "lj_carith.h"

30. #endif

31. #include "lj_vm.h"

32. #include "lj_strscan.h"

33. #include "lj_strfmt.h"

34. #include "lj_lib.h"

36. /* Some local macros to save typing. Undef'd at the end. */

37. ‌#define IR(ref)                        (&J->cur.ir[(ref)])

38. ‌#define fins                        (&J->fold.ins)

40. /* Pass IR on to next optimization in chain (FOLD). */

41. ‌#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))

43. /* -- IR tables ----------------------------------------------------------- */

45. /* IR instruction modes. */

46. ‌LJ_DATADEF const uint8_t lj_ir_mode[IR__MAX+1] = {

47. IRDEF(IRMODE)

48. 0

49. };

51. /* IR type sizes. */

52. ‌LJ_DATADEF const uint8_t lj_ir_type_size[IRT__MAX+1] = {

53. ‌#define IRTSIZE(name, size)        size,

54. IRTDEF(IRTSIZE)

55. ‌#undef IRTSIZE

56. 0

57. };

59. /* C call info for CALL* instructions. */

60. ‌LJ_DATADEF const CCallInfo lj_ir_callinfo[] = {

61. ‌#define IRCALLCI(cond, name, nargs, kind, type, flags) \

62. { (ASMFunction)IRCALLCOND_##cond(name), \

63. (nargs)|(CCI_CALL_##kind)|(IRT_##type<<CCI_OTSHIFT)|(flags) },

64. IRCALLDEF(IRCALLCI)

65. ‌#undef IRCALLCI

66. { NULL, 0 }

67. };

69. /* -- IR emitter ---------------------------------------------------------- */

71. /* Grow IR buffer at the top. */

72. ‌void LJ_FASTCALL lj_ir_growtop(jit_State *J)

73. {

74. IRIns *baseir = J->irbuf + J->irbotlim;

75. MSize szins = J->irtoplim - J->irbotlim;

76. if (szins) {

77. baseir = (IRIns *)lj_mem_realloc(J->L, baseir, szins*sizeof(IRIns),

78. 2*szins*sizeof(IRIns));

79. J->irtoplim = J->irbotlim + 2*szins;

80. } else {

81. baseir = (IRIns *)lj_mem_realloc(J->L, NULL, 0, LJ_MIN_IRSZ*sizeof(IRIns));

82. J->irbotlim = REF_BASE - LJ_MIN_IRSZ/4;

83. J->irtoplim = J->irbotlim + LJ_MIN_IRSZ;

84. }

85. J->cur.ir = J->irbuf = baseir - J->irbotlim;

86. }

88. /* Grow IR buffer at the bottom or shift it up. */

89. ‌static void lj_ir_growbot(jit_State *J)

90. {

91. IRIns *baseir = J->irbuf + J->irbotlim;

92. MSize szins = J->irtoplim - J->irbotlim;

93. lua_assert(szins != 0);

94. lua_assert(J->cur.nk == J->irbotlim);

95. if (J->cur.nins + (szins >> 1) < J->irtoplim) {

96. /* More than half of the buffer is free on top: shift up by a quarter. */

97. MSize ofs = szins >> 2;

98. memmove(baseir + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns));

99. J->irbotlim -= ofs;

100. J->irtoplim -= ofs;

101. J->cur.ir = J->irbuf = baseir - J->irbotlim;

102. } else {

103. /* Double the buffer size, but split the growth amongst top/bottom. */

104. IRIns *newbase = lj_mem_newt(J->L, 2*szins*sizeof(IRIns), IRIns);

105. MSize ofs = szins >= 256 ? 128 : (szins >> 1);  /* Limit bottom growth. */

106. memcpy(newbase + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns));

107. lj_mem_free(G(J->L), baseir, szins*sizeof(IRIns));

108. J->irbotlim -= ofs;

109. J->irtoplim = J->irbotlim + 2*szins;

110. J->cur.ir = J->irbuf = newbase - J->irbotlim;

111. }

112. }

114. /* Emit IR without any optimizations. */

115. ‌TRef LJ_FASTCALL lj_ir_emit(jit_State *J)

116. {

117. IRRef ref = lj_ir_nextins(J);

118. IRIns *ir = IR(ref);

119. IROp op = fins->o;

120. ir->prev = J->chain[op];

121. J->chain[op] = (IRRef1)ref;

122. ir->o = op;

123. ir->op1 = fins->op1;

124. ir->op2 = fins->op2;

125. J->guardemit.irt |= fins->t.irt;

126. return TREF(ref, irt_t((ir->t = fins->t)));

127. }

129. /* Emit call to a C function. */

130. ‌TRef lj_ir_call(jit_State *J, IRCallID id, ...)

131. {

132. const CCallInfo *ci = &lj_ir_callinfo[id];

133. uint32_t n = CCI_NARGS(ci);

134. TRef tr = TREF_NIL;

135. va_list argp;

136. va_start(argp, id);

137. if ((ci->flags & CCI_L)) n--;

138. if (n > 0)

139. tr = va_arg(argp, IRRef);

140. while (n-- > 1)

141. tr = emitir(IRT(IR_CARG, IRT_NIL), tr, va_arg(argp, IRRef));

142. va_end(argp);

143. if (CCI_OP(ci) == IR_CALLS)

144. J->needsnap = 1;  /* Need snapshot after call with side effect. */

145. return emitir(CCI_OPTYPE(ci), tr, id);

146. }

148. /* -- Interning of constants ---------------------------------------------- */

150. /*

151. ** IR instructions for constants are kept between J->cur.nk >= ref < REF_BIAS.

152. ** They are chained like all other instructions, but grow downwards.

153. ** The are interned (like strings in the VM) to facilitate reference

154. ** comparisons. The same constant must get the same reference.

155. */

157. /* Get ref of next IR constant and optionally grow IR.

158. ** Note: this may invalidate all IRIns *!

159. */

160. ‌static LJ_AINLINE IRRef ir_nextk(jit_State *J)

161. {

162. IRRef ref = J->cur.nk;

163. if (LJ_UNLIKELY(ref <= J->irbotlim)) lj_ir_growbot(J);

164. J->cur.nk = --ref;

165. return ref;

166. }

168. /* Intern int32_t constant. */

169. ‌TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k)

170. {

171. IRIns *ir, *cir = J->cur.ir;

172. IRRef ref;

173. for (ref = J->chain[IR_KINT]; ref; ref = cir[ref].prev)

174. if (cir[ref].i == k)

175. goto found;

176. ref = ir_nextk(J);

177. ir = IR(ref);

178. ir->i = k;

179. ir->t.irt = IRT_INT;

180. ir->o = IR_KINT;

181. ir->prev = J->chain[IR_KINT];

182. J->chain[IR_KINT] = (IRRef1)ref;

183. found:

184. return TREF(ref, IRT_INT);

185. }

187. /* The MRef inside the KNUM/KINT64 IR instructions holds the address of the

188. ** 64 bit constant. The constants themselves are stored in a chained array

189. ** and shared across traces.

190. **

191. ** Rationale for choosing this data structure:

192. ** - The address of the constants is embedded in the generated machine code

193. **   and must never move. A resizable array or hash table wouldn't work.

194. ** - Most apps need very few non-32 bit integer constants (less than a dozen).

195. ** - Linear search is hard to beat in terms of speed and low complexity.

196. */

197. ‌typedef struct K64Array {

198. MRef next;                        /* Pointer to next list. */

199. MSize numk;                        /* Number of used elements in this array. */

200. TValue k[LJ_MIN_K64SZ];        /* Array of constants. */

201. ‌} K64Array;

203. /* Free all chained arrays. */

204. ‌void lj_ir_k64_freeall(jit_State *J)

205. {

206. K64Array *k;

207. for (k = mref(J->k64, K64Array); k; ) {

208. K64Array *next = mref(k->next, K64Array);

209. lj_mem_free(J2G(J), k, sizeof(K64Array));

210. k = next;

211. }

212. }

214. /* Find 64 bit constant in chained array or add it. */

215. ‌cTValue *lj_ir_k64_find(jit_State *J, uint64_t u64)

216. {

217. K64Array *k, *kp = NULL;

218. TValue *ntv;

219. MSize idx;

220. /* Search for the constant in the whole chain of arrays. */

221. for (k = mref(J->k64, K64Array); k; k = mref(k->next, K64Array)) {

222. kp = k;  /* Remember previous element in list. */

223. for (idx = 0; idx < k->numk; idx++) {  /* Search one array. */

224. TValue *tv = &k->k[idx];

225. if (tv->u64 == u64)  /* Needed for +-0/NaN/absmask. */

226. return tv;

227. }

228. }

229. /* Constant was not found, need to add it. */

230. if (!(kp && kp->numk < LJ_MIN_K64SZ)) {  /* Allocate a new array. */

231. K64Array *kn = lj_mem_newt(J->L, sizeof(K64Array), K64Array);

232. setmref(kn->next, NULL);

233. kn->numk = 0;

234. if (kp)

235. setmref(kp->next, kn);  /* Chain to the end of the list. */

236. else

237. setmref(J->k64, kn);  /* Link first array. */

238. kp = kn;

239. }

240. ntv = &kp->k[kp->numk++];  /* Add to current array. */

241. ntv->u64 = u64;

242. return ntv;

243. }

245. /* Intern 64 bit constant, given by its address. */

246. ‌TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv)

247. {

248. IRIns *ir, *cir = J->cur.ir;

249. IRRef ref;

250. IRType t = op == IR_KNUM ? IRT_NUM : IRT_I64;

251. for (ref = J->chain[op]; ref; ref = cir[ref].prev)

252. if (ir_k64(&cir[ref]) == tv)

253. goto found;

254. ref = ir_nextk(J);

255. ir = IR(ref);

256. lua_assert(checkptrGC(tv));

257. setmref(ir->ptr, tv);

258. ir->t.irt = t;

259. ir->o = op;

260. ir->prev = J->chain[op];

261. J->chain[op] = (IRRef1)ref;

262. found:

263. return TREF(ref, t);

264. }

266. /* Intern FP constant, given by its 64 bit pattern. */

267. ‌TRef lj_ir_knum_u64(jit_State *J, uint64_t u64)

268. {

269. return lj_ir_k64(J, IR_KNUM, lj_ir_k64_find(J, u64));

270. }

272. /* Intern 64 bit integer constant. */

273. ‌TRef lj_ir_kint64(jit_State *J, uint64_t u64)

274. {

275. return lj_ir_k64(J, IR_KINT64, lj_ir_k64_find(J, u64));

276. }

278. /* Check whether a number is int and return it. -0 is NOT considered an int. */

279. ‌static int numistrueint(lua_Number n, int32_t *kp)

280. {

281. int32_t k = lj_num2int(n);

282. if (n == (lua_Number)k) {

283. if (kp) *kp = k;

284. if (k == 0) {  /* Special check for -0. */

285. TValue tv;

286. setnumV(&tv, n);

287. if (tv.u32.hi != 0)

288. return 0;

289. }

290. return 1;

291. }

292. return 0;

293. }

295. /* Intern number as int32_t constant if possible, otherwise as FP constant. */

296. ‌TRef lj_ir_knumint(jit_State *J, lua_Number n)

297. {

298. int32_t k;

299. if (numistrueint(n, &k))

300. return lj_ir_kint(J, k);

301. else

302. return lj_ir_knum(J, n);

303. }

305. /* Intern GC object "constant". */

306. ‌TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t)

307. {

308. IRIns *ir, *cir = J->cur.ir;

309. IRRef ref;

310. lua_assert(!LJ_GC64);  /* TODO_GC64: major changes required. */

311. lua_assert(!isdead(J2G(J), o));

312. for (ref = J->chain[IR_KGC]; ref; ref = cir[ref].prev)

313. if (ir_kgc(&cir[ref]) == o)

314. goto found;

315. ref = ir_nextk(J);

316. ir = IR(ref);

317. /* NOBARRIER: Current trace is a GC root. */

318. setgcref(ir->gcr, o);

319. ir->t.irt = (uint8_t)t;

320. ir->o = IR_KGC;

321. ir->prev = J->chain[IR_KGC];

322. J->chain[IR_KGC] = (IRRef1)ref;

323. found:

324. return TREF(ref, t);

325. }

327. /* Intern 32 bit pointer constant. */

328. ‌TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr)

329. {

330. IRIns *ir, *cir = J->cur.ir;

331. IRRef ref;

332. lua_assert((void *)(intptr_t)i32ptr(ptr) == ptr);

333. for (ref = J->chain[op]; ref; ref = cir[ref].prev)

334. if (mref(cir[ref].ptr, void) == ptr)

335. goto found;

336. ref = ir_nextk(J);

337. ir = IR(ref);

338. setmref(ir->ptr, ptr);

339. ir->t.irt = IRT_P32;

340. ir->o = op;

341. ir->prev = J->chain[op];

342. J->chain[op] = (IRRef1)ref;

343. found:

344. return TREF(ref, IRT_P32);

345. }

347. /* Intern typed NULL constant. */

348. ‌TRef lj_ir_knull(jit_State *J, IRType t)

349. {

350. IRIns *ir, *cir = J->cur.ir;

351. IRRef ref;

352. for (ref = J->chain[IR_KNULL]; ref; ref = cir[ref].prev)

353. if (irt_t(cir[ref].t) == t)

354. goto found;

355. ref = ir_nextk(J);

356. ir = IR(ref);

357. ir->i = 0;

358. ir->t.irt = (uint8_t)t;

359. ir->o = IR_KNULL;

360. ir->prev = J->chain[IR_KNULL];

361. J->chain[IR_KNULL] = (IRRef1)ref;

362. found:

363. return TREF(ref, t);

364. }

366. /* Intern key slot. */

367. ‌TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot)

368. {

369. IRIns *ir, *cir = J->cur.ir;

370. IRRef2 op12 = IRREF2((IRRef1)key, (IRRef1)slot);

371. IRRef ref;

372. /* Const part is not touched by CSE/DCE, so 0-65535 is ok for IRMlit here. */

373. lua_assert(tref_isk(key) && slot == (IRRef)(IRRef1)slot);

374. for (ref = J->chain[IR_KSLOT]; ref; ref = cir[ref].prev)

375. if (cir[ref].op12 == op12)

376. goto found;

377. ref = ir_nextk(J);

378. ir = IR(ref);

379. ir->op12 = op12;

380. ir->t.irt = IRT_P32;

381. ir->o = IR_KSLOT;

382. ir->prev = J->chain[IR_KSLOT];

383. J->chain[IR_KSLOT] = (IRRef1)ref;

384. found:

385. return TREF(ref, IRT_P32);

386. }

388. /* -- Access to IR constants ---------------------------------------------- */

390. /* Copy value of IR constant. */

391. ‌void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir)

392. {

393. UNUSED(L);

394. lua_assert(ir->o != IR_KSLOT);  /* Common mistake. */

395. switch (ir->o) {

396. case IR_KPRI: setpriV(tv, irt_toitype(ir->t)); break;

397. case IR_KINT: setintV(tv, ir->i); break;

398. case IR_KGC: setgcV(L, tv, ir_kgc(ir), irt_toitype(ir->t)); break;

399. case IR_KPTR: case IR_KKPTR: case IR_KNULL:

400. setlightudV(tv, mref(ir->ptr, void));

401. break;

402. case IR_KNUM: setnumV(tv, ir_knum(ir)->n); break;

403. #if LJ_HASFFI

404. case IR_KINT64: {

405. GCcdata *cd = lj_cdata_new_(L, CTID_INT64, 8);

406. *(uint64_t *)cdataptr(cd) = ir_kint64(ir)->u64;

407. setcdataV(L, tv, cd);

408. break;

409. }

410. #endif

411. default: lua_assert(0); break;

412. }

413. }

415. /* -- Convert IR operand types -------------------------------------------- */

417. /* Convert from string to number. */

418. ‌TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr)

419. {

420. if (!tref_isnumber(tr)) {

421. if (tref_isstr(tr))

422. tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);

423. else

424. lj_trace_err(J, LJ_TRERR_BADTYPE);

425. }

426. return tr;

427. }

429. /* Convert from integer or string to number. */

430. ‌TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr)

431. {

432. if (!tref_isnum(tr)) {

433. if (tref_isinteger(tr))

434. tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT);

435. else if (tref_isstr(tr))

436. tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);

437. else

438. lj_trace_err(J, LJ_TRERR_BADTYPE);

439. }

440. return tr;

441. }

443. /* Convert from integer or number to string. */

444. ‌TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr)

445. {

446. if (!tref_isstr(tr)) {

447. if (!tref_isnumber(tr))

448. lj_trace_err(J, LJ_TRERR_BADTYPE);

449. tr = emitir(IRT(IR_TOSTR, IRT_STR), tr,

450. tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT);

451. }

452. return tr;

453. }

455. /* -- Miscellaneous IR ops ------------------------------------------------ */

457. /* Evaluate numeric comparison. */

458. ‌int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op)

459. {

460. switch (op) {

461. case IR_EQ: return (a == b);

462. case IR_NE: return (a != b);

463. case IR_LT: return (a < b);

464. case IR_GE: return (a >= b);

465. case IR_LE: return (a <= b);

466. case IR_GT: return (a > b);

467. case IR_ULT: return !(a >= b);

468. case IR_UGE: return !(a < b);

469. case IR_ULE: return !(a > b);

470. case IR_UGT: return !(a <= b);

471. default: lua_assert(0); return 0;

472. }

473. }

475. /* Evaluate string comparison. */

476. ‌int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op)

477. {

478. int res = lj_str_cmp(a, b);

479. switch (op) {

480. case IR_LT: return (res < 0);

481. case IR_GE: return (res >= 0);

482. case IR_LE: return (res <= 0);

483. case IR_GT: return (res > 0);

484. default: lua_assert(0); return 0;

485. }

486. }

488. /* Rollback IR to previous state. */

489. ‌void lj_ir_rollback(jit_State *J, IRRef ref)

490. {

491. IRRef nins = J->cur.nins;

492. while (nins > ref) {

493. IRIns *ir;

494. nins--;

495. ir = IR(nins);

496. J->chain[ir->o] = ir->prev;

497. }

498. J->cur.nins = nins;

499. }

501. ‌#undef IR

502. ‌#undef fins

503. ‌#undef emitir

505. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ircall.h - luajit-2.0-src

Global variables defined

- lj_ir_callinfo

Data types defined

- CCallInfo

- CCallInfo

- IRCallID

Macros defined

- CCI_CALL_A

- CCI_CALL_FL

- CCI_CALL_FN

- CCI_CALL_FS

- CCI_CALL_L

- CCI_CALL_N

- CCI_CALL_S

- CCI_CASTU64

- CCI_CC_CDECL

- CCI_CC_FASTCALL

- CCI_CC_MASK

- CCI_CC_SHIFT

- CCI_CC_STDCALL

- CCI_CC_THISCALL

- CCI_L

- CCI_NARGS

- CCI_NARGS_MAX

- CCI_NOFPRCLOBBER

- CCI_OP

- CCI_OPSHIFT

- CCI_OPTYPE

- CCI_OTSHIFT

- CCI_RANDFPR

- CCI_RANDFPR

- CCI_VARARG

- CCI_XA

- CCI_XARGS

- CCI_XARGS_SHIFT

- CCI_XNARGS

- CCI_XNARGS

- IRCALLCOND_ANY

- IRCALLCOND_FFI

- IRCALLCOND_FFI

- IRCALLCOND_FFI32

- IRCALLCOND_FFI32

- IRCALLCOND_FFI32

- IRCALLCOND_FP64_FFI

- IRCALLCOND_FP64_FFI

- IRCALLCOND_FPMATH

- IRCALLCOND_FPMATH

- IRCALLCOND_SOFTFP

- IRCALLCOND_SOFTFP

- IRCALLCOND_SOFTFP_FFI

- IRCALLCOND_SOFTFP_FFI

- IRCALLCOND_SOFTFP_FFI

- IRCALLDEF

- IRCALLENUM

- IRCALLENUM

- LJ_NEED_FP64

- XA2_64

- XA2_64

- XA2_FP

- XA2_FP

- XA_64

- XA_64

- XA_FP

- XA_FP

- _LJ_IRCALL_H

- fp64_d2l

- fp64_d2l

- fp64_d2l

- fp64_d2ul

- fp64_d2ul

- fp64_d2ul

- fp64_f2l

- fp64_f2l

- fp64_f2l

- fp64_f2ul

- fp64_f2ul

- fp64_f2ul

- fp64_l2d

- fp64_l2d

- fp64_l2f

- fp64_l2f

- fp64_ul2d

- fp64_ul2d

- fp64_ul2f

- fp64_ul2f

- softfp_add

- softfp_cmp

- softfp_d2f

- softfp_d2i

- softfp_d2ui

- softfp_div

- softfp_f2d

- softfp_f2i

- softfp_f2ui

- softfp_i2d

- softfp_i2f

- softfp_mul

- softfp_sub

- softfp_ui2d

- softfp_ui2f

Source code

1. /*

2. ** IR CALL* instruction definitions.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_IRCALL_H

7. ‌#define _LJ_IRCALL_H

9. #include "lj_obj.h"

10. #include "lj_ir.h"

11. #include "lj_jit.h"

13. /* C call info for CALL* instructions. */

14. ‌typedef struct CCallInfo {

15. ASMFunction func;                /* Function pointer. */

16. uint32_t flags;                /* Number of arguments and flags. */

17. ‌} CCallInfo;

19. ‌#define CCI_NARGS(ci)                ((ci)->flags & 0xff)        /* # of args. */

20. ‌#define CCI_NARGS_MAX                32                        /* Max. # of args. */

22. ‌#define CCI_OTSHIFT                16

23. ‌#define CCI_OPTYPE(ci)                ((ci)->flags >> CCI_OTSHIFT)  /* Get op/type. */

24. ‌#define CCI_OPSHIFT                24

25. ‌#define CCI_OP(ci)                ((ci)->flags >> CCI_OPSHIFT)  /* Get op. */

27. ‌#define CCI_CALL_N                (IR_CALLN << CCI_OPSHIFT)

28. ‌#define CCI_CALL_A                (IR_CALLA << CCI_OPSHIFT)

29. ‌#define CCI_CALL_L                (IR_CALLL << CCI_OPSHIFT)

30. ‌#define CCI_CALL_S                (IR_CALLS << CCI_OPSHIFT)

31. ‌#define CCI_CALL_FN                (CCI_CALL_N|CCI_CC_FASTCALL)

32. ‌#define CCI_CALL_FL                (CCI_CALL_L|CCI_CC_FASTCALL)

33. ‌#define CCI_CALL_FS                (CCI_CALL_S|CCI_CC_FASTCALL)

35. /* C call info flags. */

36. ‌#define CCI_L                        0x0100        /* Implicit L arg. */

37. ‌#define CCI_CASTU64                0x0200        /* Cast u64 result to number. */

38. ‌#define CCI_NOFPRCLOBBER        0x0400        /* Does not clobber any FPRs. */

39. ‌#define CCI_VARARG                0x0800        /* Vararg function. */

41. ‌#define CCI_CC_MASK                0x3000        /* Calling convention mask. */

42. ‌#define CCI_CC_SHIFT                12

43. /* ORDER CC */

44. ‌#define CCI_CC_CDECL                0x0000        /* Default cdecl calling convention. */

45. ‌#define CCI_CC_THISCALL                0x1000        /* Thiscall calling convention. */

46. ‌#define CCI_CC_FASTCALL                0x2000        /* Fastcall calling convention. */

47. ‌#define CCI_CC_STDCALL                0x3000        /* Stdcall calling convention. */

49. /* Extra args for SOFTFP, SPLIT 64 bit. */

50. ‌#define CCI_XARGS_SHIFT                14

51. ‌#define CCI_XARGS(ci)                (((ci)->flags >> CCI_XARGS_SHIFT) & 3)

52. ‌#define CCI_XA                        (1u << CCI_XARGS_SHIFT)

54. #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)

55. ‌#define CCI_XNARGS(ci)                (CCI_NARGS((ci)) + CCI_XARGS((ci)))

56. #else

57. ‌#define CCI_XNARGS(ci)                CCI_NARGS((ci))

58. #endif

60. /* Helpers for conditional function definitions. */

61. ‌#define IRCALLCOND_ANY(x)                x

63. #if LJ_TARGET_X86ORX64

64. ‌#define IRCALLCOND_FPMATH(x)                NULL

65. #else

66. ‌#define IRCALLCOND_FPMATH(x)                x

67. #endif

69. #if LJ_SOFTFP

70. ‌#define IRCALLCOND_SOFTFP(x)                x

71. #if LJ_HASFFI

72. ‌#define IRCALLCOND_SOFTFP_FFI(x)        x

73. #else

74. ‌#define IRCALLCOND_SOFTFP_FFI(x)        NULL

75. #endif

76. #else

77. ‌#define IRCALLCOND_SOFTFP(x)                NULL

78. ‌#define IRCALLCOND_SOFTFP_FFI(x)        NULL

79. #endif

81. ‌#define LJ_NEED_FP64        (LJ_TARGET_ARM || LJ_TARGET_PPC || LJ_TARGET_MIPS)

83. #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64)

84. ‌#define IRCALLCOND_FP64_FFI(x)                x

85. #else

86. ‌#define IRCALLCOND_FP64_FFI(x)                NULL

87. #endif

89. #if LJ_HASFFI

90. ‌#define IRCALLCOND_FFI(x)                x

91. #if LJ_32

92. ‌#define IRCALLCOND_FFI32(x)                x

93. #else

94. ‌#define IRCALLCOND_FFI32(x)                NULL

95. #endif

96. #else

97. ‌#define IRCALLCOND_FFI(x)                NULL

98. ‌#define IRCALLCOND_FFI32(x)                NULL

99. #endif

101. #if LJ_TARGET_X86

102. ‌#define CCI_RANDFPR        0        /* Clang on OSX/x86 is overzealous. */

103. #else

104. ‌#define CCI_RANDFPR        CCI_NOFPRCLOBBER

105. #endif

107. #if LJ_SOFTFP

108. ‌#define XA_FP                CCI_XA

109. ‌#define XA2_FP                (CCI_XA+CCI_XA)

110. #else

111. ‌#define XA_FP                0

112. ‌#define XA2_FP                0

113. #endif

115. #if LJ_32

116. ‌#define XA_64                CCI_XA

117. ‌#define XA2_64                (CCI_XA+CCI_XA)

118. #else

119. ‌#define XA_64                0

120. ‌#define XA2_64                0

121. #endif

123. /* Function definitions for CALL* instructions. */

124. ‌#define IRCALLDEF(_) \

125. _(ANY,        lj_str_cmp,                2,  FN, INT, CCI_NOFPRCLOBBER) \

126. _(ANY,        lj_str_find,                4,   N, P32, 0) \

127. _(ANY,        lj_str_new,                3,   S, STR, CCI_L) \

128. _(ANY,        lj_strscan_num,                2,  FN, INT, 0) \

129. _(ANY,        lj_strfmt_int,                2,  FN, STR, CCI_L) \

130. _(ANY,        lj_strfmt_num,                2,  FN, STR, CCI_L) \

131. _(ANY,        lj_strfmt_char,                2,  FN, STR, CCI_L) \

132. _(ANY,        lj_strfmt_putint,        2,  FL, P32, 0) \

133. _(ANY,        lj_strfmt_putnum,        2,  FL, P32, 0) \

134. _(ANY,        lj_strfmt_putquoted,        2,  FL, P32, 0) \

135. _(ANY,        lj_strfmt_putfxint,        3,   L, P32, XA_64) \

136. _(ANY,        lj_strfmt_putfnum_int,        3,   L, P32, XA_FP) \

137. _(ANY,        lj_strfmt_putfnum_uint,        3,   L, P32, XA_FP) \

138. _(ANY,        lj_strfmt_putfnum,        3,   L, P32, XA_FP) \

139. _(ANY,        lj_strfmt_putfstr,        3,   L, P32, 0) \

140. _(ANY,        lj_strfmt_putfchar,        3,   L, P32, 0) \

141. _(ANY,        lj_buf_putmem,                3,   S, P32, 0) \

142. _(ANY,        lj_buf_putstr,                2,  FL, P32, 0) \

143. _(ANY,        lj_buf_putchar,                2,  FL, P32, 0) \

144. _(ANY,        lj_buf_putstr_reverse,        2,  FL, P32, 0) \

145. _(ANY,        lj_buf_putstr_lower,        2,  FL, P32, 0) \

146. _(ANY,        lj_buf_putstr_upper,        2,  FL, P32, 0) \

147. _(ANY,        lj_buf_putstr_rep,        3,   L, P32, 0) \

148. _(ANY,        lj_buf_puttab,                5,   L, P32, 0) \

149. _(ANY,        lj_buf_tostr,                1,  FL, STR, 0) \

150. _(ANY,        lj_tab_new_ah,                3,   A, TAB, CCI_L) \

151. _(ANY,        lj_tab_new1,                2,  FS, TAB, CCI_L) \

152. _(ANY,        lj_tab_dup,                2,  FS, TAB, CCI_L) \

153. _(ANY,        lj_tab_clear,                1,  FS, NIL, 0) \

154. _(ANY,        lj_tab_newkey,                3,   S, P32, CCI_L) \

155. _(ANY,        lj_tab_len,                1,  FL, INT, 0) \

156. _(ANY,        lj_gc_step_jit,                2,  FS, NIL, CCI_L) \

157. _(ANY,        lj_gc_barrieruv,        2,  FS, NIL, 0) \

158. _(ANY,        lj_mem_newgco,                2,  FS, P32, CCI_L) \

159. _(ANY,        lj_math_random_step, 1, FS, NUM, CCI_CASTU64|CCI_RANDFPR)\

160. _(ANY,        lj_vm_modi,                2,  FN, INT, 0) \

161. _(ANY,        sinh,                        1,   N, NUM, XA_FP) \

162. _(ANY,        cosh,                        1,   N, NUM, XA_FP) \

163. _(ANY,        tanh,                        1,   N, NUM, XA_FP) \

164. _(ANY,        fputc,                        2,   S, INT, 0) \

165. _(ANY,        fwrite,                        4,   S, INT, 0) \

166. _(ANY,        fflush,                        1,   S, INT, 0) \

167. /* ORDER FPM */ \

168. _(FPMATH,        lj_vm_floor,                1,   N, NUM, XA_FP) \

169. _(FPMATH,        lj_vm_ceil,                1,   N, NUM, XA_FP) \

170. _(FPMATH,        lj_vm_trunc,                1,   N, NUM, XA_FP) \

171. _(FPMATH,        sqrt,                        1,   N, NUM, XA_FP) \

172. _(ANY,        exp,                        1,   N, NUM, XA_FP) \

173. _(ANY,        lj_vm_exp2,                1,   N, NUM, XA_FP) \

174. _(ANY,        log,                        1,   N, NUM, XA_FP) \

175. _(ANY,        lj_vm_log2,                1,   N, NUM, XA_FP) \

176. _(ANY,        log10,                        1,   N, NUM, XA_FP) \

177. _(ANY,        sin,                        1,   N, NUM, XA_FP) \

178. _(ANY,        cos,                        1,   N, NUM, XA_FP) \

179. _(ANY,        tan,                        1,   N, NUM, XA_FP) \

180. _(ANY,        lj_vm_powi,                2,   N, NUM, XA_FP) \

181. _(ANY,        pow,                        2,   N, NUM, XA2_FP) \

182. _(ANY,        atan2,                        2,   N, NUM, XA2_FP) \

183. _(ANY,        ldexp,                        2,   N, NUM, XA_FP) \

184. _(SOFTFP,        lj_vm_tobit,                2,   N, INT, 0) \

185. _(SOFTFP,        softfp_add,                4,   N, NUM, 0) \

186. _(SOFTFP,        softfp_sub,                4,   N, NUM, 0) \

187. _(SOFTFP,        softfp_mul,                4,   N, NUM, 0) \

188. _(SOFTFP,        softfp_div,                4,   N, NUM, 0) \

189. _(SOFTFP,        softfp_cmp,                4,   N, NIL, 0) \

190. _(SOFTFP,        softfp_i2d,                1,   N, NUM, 0) \

191. _(SOFTFP,        softfp_d2i,                2,   N, INT, 0) \

192. _(SOFTFP_FFI,        softfp_ui2d,                1,   N, NUM, 0) \

193. _(SOFTFP_FFI,        softfp_f2d,                1,   N, NUM, 0) \

194. _(SOFTFP_FFI,        softfp_d2ui,                2,   N, INT, 0) \

195. _(SOFTFP_FFI,        softfp_d2f,                2,   N, FLOAT, 0) \

196. _(SOFTFP_FFI,        softfp_i2f,                1,   N, FLOAT, 0) \

197. _(SOFTFP_FFI,        softfp_ui2f,                1,   N, FLOAT, 0) \

198. _(SOFTFP_FFI,        softfp_f2i,                1,   N, INT, 0) \

199. _(SOFTFP_FFI,        softfp_f2ui,                1,   N, INT, 0) \

200. _(FP64_FFI,        fp64_l2d,                1,   N, NUM, XA_64) \

201. _(FP64_FFI,        fp64_ul2d,                1,   N, NUM, XA_64) \

202. _(FP64_FFI,        fp64_l2f,                1,   N, FLOAT, XA_64) \

203. _(FP64_FFI,        fp64_ul2f,                1,   N, FLOAT, XA_64) \

204. _(FP64_FFI,        fp64_d2l,                1,   N, I64, XA_FP) \

205. _(FP64_FFI,        fp64_d2ul,                1,   N, U64, XA_FP) \

206. _(FP64_FFI,        fp64_f2l,                1,   N, I64, 0) \

207. _(FP64_FFI,        fp64_f2ul,                1,   N, U64, 0) \

208. _(FFI,        lj_carith_divi64,        2,   N, I64, XA2_64|CCI_NOFPRCLOBBER) \

209. _(FFI,        lj_carith_divu64,        2,   N, U64, XA2_64|CCI_NOFPRCLOBBER) \

210. _(FFI,        lj_carith_modi64,        2,   N, I64, XA2_64|CCI_NOFPRCLOBBER) \

211. _(FFI,        lj_carith_modu64,        2,   N, U64, XA2_64|CCI_NOFPRCLOBBER) \

212. _(FFI,        lj_carith_powi64,        2,   N, I64, XA2_64|CCI_NOFPRCLOBBER) \

213. _(FFI,        lj_carith_powu64,        2,   N, U64, XA2_64|CCI_NOFPRCLOBBER) \

214. _(FFI,        lj_cdata_newv,                4,   S, CDATA, CCI_L) \

215. _(FFI,        lj_cdata_setfin,        4,   S, NIL, CCI_L) \

216. _(FFI,        strlen,                        1,   L, INTP, 0) \

217. _(FFI,        memcpy,                        3,   S, PTR, 0) \

218. _(FFI,        memset,                        3,   S, PTR, 0) \

219. _(FFI,        lj_vm_errno,                0,   S, INT, CCI_NOFPRCLOBBER) \

220. _(FFI32,        lj_carith_mul64,        2,   N, I64, XA2_64|CCI_NOFPRCLOBBER) \

221. _(FFI32,        lj_carith_shl64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \

222. _(FFI32,        lj_carith_shr64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \

223. _(FFI32,        lj_carith_sar64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \

224. _(FFI32,        lj_carith_rol64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \

225. _(FFI32,        lj_carith_ror64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \

226. \

227. /* End of list. */

229. typedef enum {

230. ‌#define IRCALLENUM(cond, name, nargs, kind, type, flags)        IRCALL_##name,

231. IRCALLDEF(IRCALLENUM)

232. ‌#undef IRCALLENUM

233. IRCALL__MAX

234. ‌} IRCallID;

236. LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...);

238. ‌LJ_DATA const CCallInfo lj_ir_callinfo[IRCALL__MAX+1];

240. /* Soft-float declarations. */

241. #if LJ_SOFTFP

242. #if LJ_TARGET_ARM

243. ‌#define softfp_add __aeabi_dadd

244. ‌#define softfp_sub __aeabi_dsub

245. ‌#define softfp_mul __aeabi_dmul

246. ‌#define softfp_div __aeabi_ddiv

247. ‌#define softfp_cmp __aeabi_cdcmple

248. ‌#define softfp_i2d __aeabi_i2d

249. ‌#define softfp_d2i __aeabi_d2iz

250. ‌#define softfp_ui2d __aeabi_ui2d

251. ‌#define softfp_f2d __aeabi_f2d

252. ‌#define softfp_d2ui __aeabi_d2uiz

253. ‌#define softfp_d2f __aeabi_d2f

254. ‌#define softfp_i2f __aeabi_i2f

255. ‌#define softfp_ui2f __aeabi_ui2f

256. ‌#define softfp_f2i __aeabi_f2iz

257. ‌#define softfp_f2ui __aeabi_f2uiz

258. ‌#define fp64_l2d __aeabi_l2d

259. ‌#define fp64_ul2d __aeabi_ul2d

260. ‌#define fp64_l2f __aeabi_l2f

261. ‌#define fp64_ul2f __aeabi_ul2f

262. #if LJ_TARGET_IOS

263. ‌#define fp64_d2l __fixdfdi

264. ‌#define fp64_d2ul __fixunsdfdi

265. ‌#define fp64_f2l __fixsfdi

266. ‌#define fp64_f2ul __fixunssfdi

267. #else

268. ‌#define fp64_d2l __aeabi_d2lz

269. ‌#define fp64_d2ul __aeabi_d2ulz

270. ‌#define fp64_f2l __aeabi_f2lz

271. ‌#define fp64_f2ul __aeabi_f2ulz

272. #endif

273. #else

274. #error "Missing soft-float definitions for target architecture"

275. #endif

276. extern double softfp_add(double a, double b);

277. extern double softfp_sub(double a, double b);

278. extern double softfp_mul(double a, double b);

279. extern double softfp_div(double a, double b);

280. extern void softfp_cmp(double a, double b);

281. extern double softfp_i2d(int32_t a);

282. extern int32_t softfp_d2i(double a);

283. #if LJ_HASFFI

284. extern double softfp_ui2d(uint32_t a);

285. extern double softfp_f2d(float a);

286. extern uint32_t softfp_d2ui(double a);

287. extern float softfp_d2f(double a);

288. extern float softfp_i2f(int32_t a);

289. extern float softfp_ui2f(uint32_t a);

290. extern int32_t softfp_f2i(float a);

291. extern uint32_t softfp_f2ui(float a);

292. #endif

293. #endif

295. #if LJ_HASFFI && LJ_NEED_FP64 && !(LJ_TARGET_ARM && LJ_SOFTFP)

296. #ifdef __GNUC__

297. ‌#define fp64_l2d __floatdidf

298. ‌#define fp64_ul2d __floatundidf

299. ‌#define fp64_l2f __floatdisf

300. ‌#define fp64_ul2f __floatundisf

301. ‌#define fp64_d2l __fixdfdi

302. ‌#define fp64_d2ul __fixunsdfdi

303. ‌#define fp64_f2l __fixsfdi

304. ‌#define fp64_f2ul __fixunssfdi

305. #else

306. #error "Missing fp64 helper definitions for this compiler"

307. #endif

308. #endif

310. #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64)

311. extern double fp64_l2d(int64_t a);

312. extern double fp64_ul2d(uint64_t a);

313. extern float fp64_l2f(int64_t a);

314. extern float fp64_ul2f(uint64_t a);

315. extern int64_t fp64_d2l(double a);

316. extern uint64_t fp64_d2ul(double a);

317. extern int64_t fp64_f2l(float a);

318. extern uint64_t fp64_f2ul(float a);

319. #endif

321. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_strscan.c - luajit-2.0-src

Functions defined

- lj_strscan_num

- lj_strscan_number

- lj_strscan_scan

- strscan_dec

- strscan_double

- strscan_hex

- strscan_oct

Macros defined

- DLEN

- DLEN

- DNEXT

- DNEXT

- DPREV

- DPREV

- LUA_CORE

- STRSCAN_DDIG

- STRSCAN_DIG

- STRSCAN_DMASK

- STRSCAN_MAXDIG

- casecmp

- lj_strscan_c

Source code

1. /*

2. ** String scanning.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include <math.h>

8. ‌#define lj_strscan_c

9. ‌#define LUA_CORE

11. #include "lj_obj.h"

12. #include "lj_char.h"

13. #include "lj_strscan.h"

15. /* -- Scanning numbers ---------------------------------------------------- */

17. /*

18. ** Rationale for the builtin string to number conversion library:

19. **

20. ** It removes a dependency on libc's strtod(), which is a true portability

21. ** nightmare. Mainly due to the plethora of supported OS and toolchain

22. ** combinations. Sadly, the various implementations

23. ** a) are often buggy, incomplete (no hex floats) and/or imprecise,

24. ** b) sometimes crash or hang on certain inputs,

25. ** c) return non-standard NaNs that need to be filtered out, and

26. ** d) fail if the locale-specific decimal separator is not a dot,

27. **    which can only be fixed with atrocious workarounds.

28. **

29. ** Also, most of the strtod() implementations are hopelessly bloated,

30. ** which is not just an I-cache hog, but a problem for static linkage

31. ** on embedded systems, too.

32. **

33. ** OTOH the builtin conversion function is very compact. Even though it

34. ** does a lot more, like parsing long longs, octal or imaginary numbers

35. ** and returning the result in different formats:

36. ** a) It needs less than 3 KB (!) of machine code (on x64 with -Os),

37. ** b) it doesn't perform any dynamic allocation and,

38. ** c) it needs only around 600 bytes of stack space.

39. **

40. ** The builtin function is faster than strtod() for typical inputs, e.g.

41. ** "123", "1.5" or "1e6". Arguably, it's slower for very large exponents,

42. ** which are not very common (this could be fixed, if needed).

43. **

44. ** And most importantly, the builtin function is equally precise on all

45. ** platforms. It correctly converts and rounds any input to a double.

46. ** If this is not the case, please send a bug report -- but PLEASE verify

47. ** that the implementation you're comparing to is not the culprit!

48. **

49. ** The implementation quickly pre-scans the entire string first and

50. ** handles simple integers on-the-fly. Otherwise, it dispatches to the

51. ** base-specific parser. Hex and octal is straightforward.

52. **

53. ** Decimal to binary conversion uses a fixed-length circular buffer in

54. ** base 100. Some simple cases are handled directly. For other cases, the

55. ** number in the buffer is up-scaled or down-scaled until the integer part

56. ** is in the proper range. Then the integer part is rounded and converted

57. ** to a double which is finally rescaled to the result. Denormals need

58. ** special treatment to prevent incorrect 'double rounding'.

59. */

61. /* Definitions for circular decimal digit buffer (base 100 = 2 digits/byte). */

62. ‌#define STRSCAN_DIG        1024

63. ‌#define STRSCAN_MAXDIG        800                /* 772 + extra are sufficient. */

64. ‌#define STRSCAN_DDIG        (STRSCAN_DIG/2)

65. ‌#define STRSCAN_DMASK        (STRSCAN_DDIG-1)

67. /* Helpers for circular buffer. */

68. ‌#define DNEXT(a)        (((a)+1) & STRSCAN_DMASK)

69. ‌#define DPREV(a)        (((a)-1) & STRSCAN_DMASK)

70. ‌#define DLEN(lo, hi)        ((int32_t)(((lo)-(hi)) & STRSCAN_DMASK))

72. ‌#define casecmp(c, k)        (((c) | 0x20) == k)

74. /* Final conversion to double. */

75. ‌static void strscan_double(uint64_t x, TValue *o, int32_t ex2, int32_t neg)

76. {

77. double n;

79. /* Avoid double rounding for denormals. */

80. if (LJ_UNLIKELY(ex2 <= -1075 && x != 0)) {

81. /* NYI: all of this generates way too much code on 32 bit CPUs. */

82. #if defined(__GNUC__) && LJ_64

83. int32_t b = (int32_t)(__builtin_clzll(x)^63);

84. #else

85. int32_t b = (x>>32) ? 32+(int32_t)lj_fls((uint32_t)(x>>32)) :

86. (int32_t)lj_fls((uint32_t)x);

87. #endif

88. if ((int32_t)b + ex2 <= -1023 && (int32_t)b + ex2 >= -1075) {

89. uint64_t rb = (uint64_t)1 << (-1075-ex2);

90. if ((x & rb) && ((x & (rb+rb+rb-1)))) x += rb+rb;

91. x = (x & ~(rb+rb-1));

92. }

93. }

95. /* Convert to double using a signed int64_t conversion, then rescale. */

96. lua_assert((int64_t)x >= 0);

97. n = (double)(int64_t)x;

98. if (neg) n = -n;

99. if (ex2) n = ldexp(n, ex2);

100. o->n = n;

101. }

103. /* Parse hexadecimal number. */

104. ‌static StrScanFmt strscan_hex(const uint8_t *p, TValue *o,

105. StrScanFmt fmt, uint32_t opt,

106. int32_t ex2, int32_t neg, uint32_t dig)

107. {

108. uint64_t x = 0;

109. uint32_t i;

111. /* Scan hex digits. */

112. for (i = dig > 16 ? 16 : dig ; i; i--, p++) {

113. uint32_t d = (*p != '.' ? *p : *++p); if (d > '9') d += 9;

114. x = (x << 4) + (d & 15);

115. }

117. /* Summarize rounding-effect of excess digits. */

118. for (i = 16; i < dig; i++, p++)

119. x |= ((*p != '.' ? *p : *++p) != '0'), ex2 += 4;

121. /* Format-specific handling. */

122. switch (fmt) {

123. case STRSCAN_INT:

124. if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) {

125. o->i = neg ? -(int32_t)x : (int32_t)x;

126. return STRSCAN_INT;  /* Fast path for 32 bit integers. */

127. }

128. if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; break; }

129. /* fallthrough */

130. case STRSCAN_U32:

131. if (dig > 8) return STRSCAN_ERROR;

132. o->i = neg ? -(int32_t)x : (int32_t)x;

133. return STRSCAN_U32;

134. case STRSCAN_I64:

135. case STRSCAN_U64:

136. if (dig > 16) return STRSCAN_ERROR;

137. o->u64 = neg ? (uint64_t)-(int64_t)x : x;

138. return fmt;

139. default:

140. break;

141. }

143. /* Reduce range then convert to double. */

144. if ((x & U64x(c0000000,0000000))) { x = (x >> 2) | (x & 3); ex2 += 2; }

145. strscan_double(x, o, ex2, neg);

146. return fmt;

147. }

149. /* Parse octal number. */

150. ‌static StrScanFmt strscan_oct(const uint8_t *p, TValue *o,

151. StrScanFmt fmt, int32_t neg, uint32_t dig)

152. {

153. uint64_t x = 0;

155. /* Scan octal digits. */

156. if (dig > 22 || (dig == 22 && *p > '1')) return STRSCAN_ERROR;

157. while (dig-- > 0) {

158. if (!(*p >= '0' && *p <= '7')) return STRSCAN_ERROR;

159. x = (x << 3) + (*p++ & 7);

160. }

162. /* Format-specific handling. */

163. switch (fmt) {

164. case STRSCAN_INT:

165. if (x >= 0x80000000u+neg) fmt = STRSCAN_U32;

166. /* fallthrough */

167. case STRSCAN_U32:

168. if ((x >> 32)) return STRSCAN_ERROR;

169. o->i = neg ? -(int32_t)x : (int32_t)x;

170. break;

171. default:

172. case STRSCAN_I64:

173. case STRSCAN_U64:

174. o->u64 = neg ? (uint64_t)-(int64_t)x : x;

175. break;

176. }

177. return fmt;

178. }

180. /* Parse decimal number. */

181. ‌static StrScanFmt strscan_dec(const uint8_t *p, TValue *o,

182. StrScanFmt fmt, uint32_t opt,

183. int32_t ex10, int32_t neg, uint32_t dig)

184. {

185. uint8_t xi[STRSCAN_DDIG], *xip = xi;

187. if (dig) {

188. uint32_t i = dig;

189. if (i > STRSCAN_MAXDIG) {

190. ex10 += (int32_t)(i - STRSCAN_MAXDIG);

191. i = STRSCAN_MAXDIG;

192. }

193. /* Scan unaligned leading digit. */

194. if (((ex10^i) & 1))

195. *xip++ = ((*p != '.' ? *p : *++p) & 15), i--, p++;

196. /* Scan aligned double-digits. */

197. for ( ; i > 1; i -= 2) {

198. uint32_t d = 10 * ((*p != '.' ? *p : *++p) & 15); p++;

199. *xip++ = d + ((*p != '.' ? *p : *++p) & 15); p++;

200. }

201. /* Scan and realign trailing digit. */

202. if (i) *xip++ = 10 * ((*p != '.' ? *p : *++p) & 15), ex10--, dig++, p++;

204. /* Summarize rounding-effect of excess digits. */

205. if (dig > STRSCAN_MAXDIG) {

206. do {

207. if ((*p != '.' ? *p : *++p) != '0') { xip[-1] |= 1; break; }

208. p++;

209. } while (--dig > STRSCAN_MAXDIG);

210. dig = STRSCAN_MAXDIG;

211. } else {  /* Simplify exponent. */

212. while (ex10 > 0 && dig <= 18) *xip++ = 0, ex10 -= 2, dig += 2;

213. }

214. } else {  /* Only got zeros. */

215. ex10 = 0;

216. xi[0] = 0;

217. }

219. /* Fast path for numbers in integer format (but handles e.g. 1e6, too). */

220. if (dig <= 20 && ex10 == 0) {

221. uint8_t *xis;

222. uint64_t x = xi[0];

223. double n;

224. for (xis = xi+1; xis < xip; xis++) x = x * 100 + *xis;

225. if (!(dig == 20 && (xi[0] > 18 || (int64_t)x >= 0))) {  /* No overflow? */

226. /* Format-specific handling. */

227. switch (fmt) {

228. case STRSCAN_INT:

229. if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) {

230. o->i = neg ? -(int32_t)x : (int32_t)x;

231. return STRSCAN_INT;  /* Fast path for 32 bit integers. */

232. }

233. if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; goto plainnumber; }

234. /* fallthrough */

235. case STRSCAN_U32:

236. if ((x >> 32) != 0) return STRSCAN_ERROR;

237. o->i = neg ? -(int32_t)x : (int32_t)x;

238. return STRSCAN_U32;

239. case STRSCAN_I64:

240. case STRSCAN_U64:

241. o->u64 = neg ? (uint64_t)-(int64_t)x : x;

242. return fmt;

243. default:

244. plainnumber:  /* Fast path for plain numbers < 2^63. */

245. if ((int64_t)x < 0) break;

246. n = (double)(int64_t)x;

247. if (neg) n = -n;

248. o->n = n;

249. return fmt;

250. }

251. }

252. }

254. /* Slow non-integer path. */

255. if (fmt == STRSCAN_INT) {

256. if ((opt & STRSCAN_OPT_C)) return STRSCAN_ERROR;

257. fmt = STRSCAN_NUM;

258. } else if (fmt > STRSCAN_INT) {

259. return STRSCAN_ERROR;

260. }

261. {

262. uint32_t hi = 0, lo = (uint32_t)(xip-xi);

263. int32_t ex2 = 0, idig = (int32_t)lo + (ex10 >> 1);

265. lua_assert(lo > 0 && (ex10 & 1) == 0);

267. /* Handle simple overflow/underflow. */

268. if (idig > 310/2) { if (neg) setminfV(o); else setpinfV(o); return fmt; }

269. else if (idig < -326/2) { o->n = neg ? -0.0 : 0.0; return fmt; }

271. /* Scale up until we have at least 17 or 18 integer part digits. */

272. while (idig < 9 && idig < DLEN(lo, hi)) {

273. uint32_t i, cy = 0;

274. ex2 -= 6;

275. for (i = DPREV(lo); ; i = DPREV(i)) {

276. uint32_t d = (xi[i] << 6) + cy;

277. cy = (((d >> 2) * 5243) >> 17); d = d - cy * 100;  /* Div/mod 100. */

278. xi[i] = (uint8_t)d;

279. if (i == hi) break;

280. if (d == 0 && i == DPREV(lo)) lo = i;

281. }

282. if (cy) {

283. hi = DPREV(hi);

284. if (xi[DPREV(lo)] == 0) lo = DPREV(lo);

285. else if (hi == lo) { lo = DPREV(lo); xi[DPREV(lo)] |= xi[lo]; }

286. xi[hi] = (uint8_t)cy; idig++;

287. }

288. }

290. /* Scale down until no more than 17 or 18 integer part digits remain. */

291. while (idig > 9) {

292. uint32_t i = hi, cy = 0;

293. ex2 += 6;

294. do {

295. cy += xi[i];

296. xi[i] = (cy >> 6);

297. cy = 100 * (cy & 0x3f);

298. if (xi[i] == 0 && i == hi) hi = DNEXT(hi), idig--;

299. i = DNEXT(i);

300. } while (i != lo);

301. while (cy) {

302. if (hi == lo) { xi[DPREV(lo)] |= 1; break; }

303. xi[lo] = (cy >> 6); lo = DNEXT(lo);

304. cy = 100 * (cy & 0x3f);

305. }

306. }

308. /* Collect integer part digits and convert to rescaled double. */

309. {

310. uint64_t x = xi[hi];

311. uint32_t i;

312. for (i = DNEXT(hi); --idig > 0 && i != lo; i = DNEXT(i))

313. x = x * 100 + xi[i];

314. if (i == lo) {

315. while (--idig >= 0) x = x * 100;

316. } else {  /* Gather round bit from remaining digits. */

317. x <<= 1; ex2--;

318. do {

319. if (xi[i]) { x |= 1; break; }

320. i = DNEXT(i);

321. } while (i != lo);

322. }

323. strscan_double(x, o, ex2, neg);

324. }

325. }

326. return fmt;

327. }

329. /* Scan string containing a number. Returns format. Returns value in o. */

330. ‌StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt)

331. {

332. int32_t neg = 0;

334. /* Remove leading space, parse sign and non-numbers. */

335. if (LJ_UNLIKELY(!lj_char_isdigit(*p))) {

336. while (lj_char_isspace(*p)) p++;

337. if (*p == '+' || *p == '-') neg = (*p++ == '-');

338. if (LJ_UNLIKELY(*p >= 'A')) {  /* Parse "inf", "infinity" or "nan". */

339. TValue tmp;

340. setnanV(&tmp);

341. if (casecmp(p[0],'i') && casecmp(p[1],'n') && casecmp(p[2],'f')) {

342. if (neg) setminfV(&tmp); else setpinfV(&tmp);

343. p += 3;

344. if (casecmp(p[0],'i') && casecmp(p[1],'n') && casecmp(p[2],'i') &&

345. casecmp(p[3],'t') && casecmp(p[4],'y')) p += 5;

346. } else if (casecmp(p[0],'n') && casecmp(p[1],'a') && casecmp(p[2],'n')) {

347. p += 3;

348. }

349. while (lj_char_isspace(*p)) p++;

350. if (*p) return STRSCAN_ERROR;

351. o->u64 = tmp.u64;

352. return STRSCAN_NUM;

353. }

354. }

356. /* Parse regular number. */

357. {

358. StrScanFmt fmt = STRSCAN_INT;

359. int cmask = LJ_CHAR_DIGIT;

360. int base = (opt & STRSCAN_OPT_C) && *p == '0' ? 0 : 10;

361. const uint8_t *sp, *dp = NULL;

362. uint32_t dig = 0, hasdig = 0, x = 0;

363. int32_t ex = 0;

365. /* Determine base and skip leading zeros. */

366. if (LJ_UNLIKELY(*p <= '0')) {

367. if (*p == '0' && casecmp(p[1], 'x'))

368. base = 16, cmask = LJ_CHAR_XDIGIT, p += 2;

369. for ( ; ; p++) {

370. if (*p == '0') {

371. hasdig = 1;

372. } else if (*p == '.') {

373. if (dp) return STRSCAN_ERROR;

374. dp = p;

375. } else {

376. break;

377. }

378. }

379. }

381. /* Preliminary digit and decimal point scan. */

382. for (sp = p; ; p++) {

383. if (LJ_LIKELY(lj_char_isa(*p, cmask))) {

384. x = x * 10 + (*p & 15);  /* For fast path below. */

385. dig++;

386. } else if (*p == '.') {

387. if (dp) return STRSCAN_ERROR;

388. dp = p;

389. } else {

390. break;

391. }

392. }

393. if (!(hasdig | dig)) return STRSCAN_ERROR;

395. /* Handle decimal point. */

396. if (dp) {

397. fmt = STRSCAN_NUM;

398. if (dig) {

399. ex = (int32_t)(dp-(p-1)); dp = p-1;

400. while (ex < 0 && *dp-- == '0') ex++, dig--;  /* Skip trailing zeros. */

401. if (base == 16) ex *= 4;

402. }

403. }

405. /* Parse exponent. */

406. if (casecmp(*p, (uint32_t)(base == 16 ? 'p' : 'e'))) {

407. uint32_t xx;

408. int negx = 0;

409. fmt = STRSCAN_NUM; p++;

410. if (*p == '+' || *p == '-') negx = (*p++ == '-');

411. if (!lj_char_isdigit(*p)) return STRSCAN_ERROR;

412. xx = (*p++ & 15);

413. while (lj_char_isdigit(*p)) {

414. if (xx < 65536) xx = xx * 10 + (*p & 15);

415. p++;

416. }

417. ex += negx ? -(int32_t)xx : (int32_t)xx;

418. }

420. /* Parse suffix. */

421. if (*p) {

422. /* I (IMAG), U (U32), LL (I64), ULL/LLU (U64), L (long), UL/LU (ulong). */

423. /* NYI: f (float). Not needed until cp_number() handles non-integers. */

424. if (casecmp(*p, 'i')) {

425. if (!(opt & STRSCAN_OPT_IMAG)) return STRSCAN_ERROR;

426. p++; fmt = STRSCAN_IMAG;

427. } else if (fmt == STRSCAN_INT) {

428. if (casecmp(*p, 'u')) p++, fmt = STRSCAN_U32;

429. if (casecmp(*p, 'l')) {

430. p++;

431. if (casecmp(*p, 'l')) p++, fmt += STRSCAN_I64 - STRSCAN_INT;

432. else if (!(opt & STRSCAN_OPT_C)) return STRSCAN_ERROR;

433. else if (sizeof(long) == 8) fmt += STRSCAN_I64 - STRSCAN_INT;

434. }

435. if (casecmp(*p, 'u') && (fmt == STRSCAN_INT || fmt == STRSCAN_I64))

436. p++, fmt += STRSCAN_U32 - STRSCAN_INT;

437. if ((fmt == STRSCAN_U32 && !(opt & STRSCAN_OPT_C)) ||

438. (fmt >= STRSCAN_I64 && !(opt & STRSCAN_OPT_LL)))

439. return STRSCAN_ERROR;

440. }

441. while (lj_char_isspace(*p)) p++;

442. if (*p) return STRSCAN_ERROR;

443. }

445. /* Fast path for decimal 32 bit integers. */

446. if (fmt == STRSCAN_INT && base == 10 &&

447. (dig < 10 || (dig == 10 && *sp <= '2' && x < 0x80000000u+neg))) {

448. int32_t y = neg ? -(int32_t)x : (int32_t)x;

449. if ((opt & STRSCAN_OPT_TONUM)) {

450. o->n = (double)y;

451. return STRSCAN_NUM;

452. } else {

453. o->i = y;

454. return STRSCAN_INT;

455. }

456. }

458. /* Dispatch to base-specific parser. */

459. if (base == 0 && !(fmt == STRSCAN_NUM || fmt == STRSCAN_IMAG))

460. return strscan_oct(sp, o, fmt, neg, dig);

461. if (base == 16)

462. fmt = strscan_hex(sp, o, fmt, opt, ex, neg, dig);

463. else

464. fmt = strscan_dec(sp, o, fmt, opt, ex, neg, dig);

466. /* Try to convert number to integer, if requested. */

467. if (fmt == STRSCAN_NUM && (opt & STRSCAN_OPT_TOINT)) {

468. double n = o->n;

469. int32_t i = lj_num2int(n);

470. if (n == (lua_Number)i) { o->i = i; return STRSCAN_INT; }

471. }

472. return fmt;

473. }

474. }

476. ‌int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o)

477. {

478. StrScanFmt fmt = lj_strscan_scan((const uint8_t *)strdata(str), o,

479. STRSCAN_OPT_TONUM);

480. lua_assert(fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM);

481. return (fmt != STRSCAN_ERROR);

482. }

484. #if LJ_DUALNUM

485. ‌int LJ_FASTCALL lj_strscan_number(GCstr *str, TValue *o)

486. {

487. StrScanFmt fmt = lj_strscan_scan((const uint8_t *)strdata(str), o,

488. STRSCAN_OPT_TOINT);

489. lua_assert(fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM || fmt == STRSCAN_INT);

490. if (fmt == STRSCAN_INT) setitype(o, LJ_TISNUM);

491. return (fmt != STRSCAN_ERROR);

492. }

493. #endif

495. ‌#undef DNEXT

496. ‌#undef DPREV

497. ‌#undef DLEN

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_strscan.h - luajit-2.0-src

Data types defined

- StrScanFmt

Functions defined

- lj_strscan_numberobj

Macros defined

- STRSCAN_OPT_C

- STRSCAN_OPT_IMAG

- STRSCAN_OPT_LL

- STRSCAN_OPT_TOINT

- STRSCAN_OPT_TONUM

- _LJ_STRSCAN_H

- lj_strscan_number

Source code

1. /*

2. ** String scanning.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_STRSCAN_H

7. ‌#define _LJ_STRSCAN_H

9. #include "lj_obj.h"

11. /* Options for accepted/returned formats. */

12. ‌#define STRSCAN_OPT_TOINT        0x01  /* Convert to int32_t, if possible. */

13. ‌#define STRSCAN_OPT_TONUM        0x02  /* Always convert to double. */

14. ‌#define STRSCAN_OPT_IMAG        0x04

15. ‌#define STRSCAN_OPT_LL                0x08

16. ‌#define STRSCAN_OPT_C                0x10

18. /* Returned format. */

19. typedef enum {

20. STRSCAN_ERROR,

21. STRSCAN_NUM, STRSCAN_IMAG,

22. STRSCAN_INT, STRSCAN_U32, STRSCAN_I64, STRSCAN_U64,

23. ‌} StrScanFmt;

25. LJ_FUNC StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt);

26. LJ_FUNC int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o);

27. #if LJ_DUALNUM

28. LJ_FUNC int LJ_FASTCALL lj_strscan_number(GCstr *str, TValue *o);

29. #else

30. ‌#define lj_strscan_number(s, o)                lj_strscan_num((s), (o))

31. #endif

33. /* Check for number or convert string to number/int in-place (!). */

34. ‌static LJ_AINLINE int lj_strscan_numberobj(TValue *o)

35. {

36. return tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), o));

37. }

39. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_cparse.c - luajit-2.0-src

Global variables defined

- ctoknames

Data types defined

- CPDecl

- CPDecl

- CPDeclIdx

- CPscl

Functions defined

- cp_add

- cp_check

- cp_cleanup

- cp_comment_c

- cp_comment_cpp

- cp_decl_abstract

- cp_decl_align

- cp_decl_array

- cp_decl_asm

- cp_decl_attributes

- cp_decl_constinit

- cp_decl_enum

- cp_decl_func

- cp_decl_gccattribute

- cp_decl_intern

- cp_decl_mode

- cp_decl_msvcattribute

- cp_decl_multi

- cp_decl_reset

- cp_decl_single

- cp_decl_sizeattr

- cp_decl_spec

- cp_decl_struct

- cp_declarator

- cp_err

- cp_err_badidx

- cp_err_token

- cp_errmsg

- cp_expr_comma

- cp_expr_infix

- cp_expr_kint

- cp_expr_ksize

- cp_expr_postfix

- cp_expr_prefix

- cp_expr_sizeof

- cp_expr_sub

- cp_expr_unary

- cp_field_align

- cp_get

- cp_get_bs

- cp_ident

- cp_init

- cp_iseol

- cp_istypedecl

- cp_newline

- cp_next

- cp_next_

- cp_number

- cp_opt

- cp_param

- cp_pragma

- cp_push

- cp_push_attributes

- cp_push_type

- cp_rawpeek

- cp_save

- cp_string

- cp_struct_layout

- cp_struct_name

- cp_tok2str

- cpcparser

- lj_cparse

Macros defined

- CPNS_DEFAULT

- CPNS_STRUCT

- CTOKSTR

- CTOKSTR

- H_

- H_

Source code

1. /*

2. ** C declaration parser.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "lj_obj.h"

8. #if LJ_HASFFI

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_buf.h"

13. #include "lj_ctype.h"

14. #include "lj_cparse.h"

15. #include "lj_frame.h"

16. #include "lj_vm.h"

17. #include "lj_char.h"

18. #include "lj_strscan.h"

19. #include "lj_strfmt.h"

21. /*

22. ** Important note: this is NOT a validating C parser! This is a minimal

23. ** C declaration parser, solely for use by the LuaJIT FFI.

24. **

25. ** It ought to return correct results for properly formed C declarations,

26. ** but it may accept some invalid declarations, too (and return nonsense).

27. ** Also, it shows rather generic error messages to avoid unnecessary bloat.

28. ** If in doubt, please check the input against your favorite C compiler.

29. */

31. /* -- C lexer ------------------------------------------------------------- */

33. /* C lexer token names. */

34. ‌static const char *const ctoknames[] = {

35. ‌#define CTOKSTR(name, str)        str,

36. CTOKDEF(CTOKSTR)

37. ‌#undef CTOKSTR

38. NULL

39. };

41. /* Forward declaration. */

42. LJ_NORET static void cp_err(CPState *cp, ErrMsg em);

44. ‌static const char *cp_tok2str(CPState *cp, CPToken tok)

45. {

46. lua_assert(tok < CTOK_FIRSTDECL);

47. if (tok > CTOK_OFS)

48. return ctoknames[tok-CTOK_OFS-1];

49. else if (!lj_char_iscntrl(tok))

50. return lj_strfmt_pushf(cp->L, "%c", tok);

51. else

52. return lj_strfmt_pushf(cp->L, "char(%d)", tok);

53. }

55. /* End-of-line? */

56. ‌static LJ_AINLINE int cp_iseol(CPChar c)

57. {

58. return (c == '\n' || c == '\r');

59. }

61. /* Peek next raw character. */

62. ‌static LJ_AINLINE CPChar cp_rawpeek(CPState *cp)

63. {

64. return (CPChar)(uint8_t)(*cp->p);

65. }

67. static LJ_NOINLINE CPChar cp_get_bs(CPState *cp);

69. /* Get next character. */

70. ‌static LJ_AINLINE CPChar cp_get(CPState *cp)

71. {

72. cp->c = (CPChar)(uint8_t)(*cp->p++);

73. if (LJ_LIKELY(cp->c != '\\')) return cp->c;

74. return cp_get_bs(cp);

75. }

77. /* Transparently skip backslash-escaped line breaks. */

78. ‌static LJ_NOINLINE CPChar cp_get_bs(CPState *cp)

79. {

80. CPChar c2, c = cp_rawpeek(cp);

81. if (!cp_iseol(c)) return cp->c;

82. cp->p++;

83. c2 = cp_rawpeek(cp);

84. if (cp_iseol(c2) && c2 != c) cp->p++;

85. cp->linenumber++;

86. return cp_get(cp);

87. }

89. /* Save character in buffer. */

90. ‌static LJ_AINLINE void cp_save(CPState *cp, CPChar c)

91. {

92. lj_buf_putb(&cp->sb, c);

93. }

95. /* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */

96. ‌static void cp_newline(CPState *cp)

97. {

98. CPChar c = cp_rawpeek(cp);

99. if (cp_iseol(c) && c != cp->c) cp->p++;

100. cp->linenumber++;

101. }

103. ‌LJ_NORET static void cp_errmsg(CPState *cp, CPToken tok, ErrMsg em, ...)

104. {

105. const char *msg, *tokstr;

106. lua_State *L;

107. va_list argp;

108. if (tok == 0) {

109. tokstr = NULL;

110. } else if (tok == CTOK_IDENT || tok == CTOK_INTEGER || tok == CTOK_STRING ||

111. tok >= CTOK_FIRSTDECL) {

112. if (sbufP(&cp->sb) == sbufB(&cp->sb)) cp_save(cp, '$');

113. cp_save(cp, '\0');

114. tokstr = sbufB(&cp->sb);

115. } else {

116. tokstr = cp_tok2str(cp, tok);

117. }

118. L = cp->L;

119. va_start(argp, em);

120. msg = lj_strfmt_pushvf(L, err2msg(em), argp);

121. va_end(argp);

122. if (tokstr)

123. msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tokstr);

124. if (cp->linenumber > 1)

125. msg = lj_strfmt_pushf(L, "%s at line %d", msg, cp->linenumber);

126. lj_err_callermsg(L, msg);

127. }

129. ‌LJ_NORET LJ_NOINLINE static void cp_err_token(CPState *cp, CPToken tok)

130. {

131. cp_errmsg(cp, cp->tok, LJ_ERR_XTOKEN, cp_tok2str(cp, tok));

132. }

134. ‌LJ_NORET LJ_NOINLINE static void cp_err_badidx(CPState *cp, CType *ct)

135. {

136. GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL);

137. cp_errmsg(cp, 0, LJ_ERR_FFI_BADIDX, strdata(s));

138. }

140. ‌LJ_NORET LJ_NOINLINE static void cp_err(CPState *cp, ErrMsg em)

141. {

142. cp_errmsg(cp, 0, em);

143. }

145. /* -- Main lexical scanner ------------------------------------------------ */

147. /* Parse number literal. Only handles int32_t/uint32_t right now. */

148. ‌static CPToken cp_number(CPState *cp)

149. {

150. StrScanFmt fmt;

151. TValue o;

152. do { cp_save(cp, cp->c); } while (lj_char_isident(cp_get(cp)));

153. cp_save(cp, '\0');

154. fmt = lj_strscan_scan((const uint8_t *)sbufB(&cp->sb), &o, STRSCAN_OPT_C);

155. if (fmt == STRSCAN_INT) cp->val.id = CTID_INT32;

156. else if (fmt == STRSCAN_U32) cp->val.id = CTID_UINT32;

157. else if (!(cp->mode & CPARSE_MODE_SKIP))

158. cp_errmsg(cp, CTOK_INTEGER, LJ_ERR_XNUMBER);

159. cp->val.u32 = (uint32_t)o.i;

160. return CTOK_INTEGER;

161. }

163. /* Parse identifier or keyword. */

164. ‌static CPToken cp_ident(CPState *cp)

165. {

166. do { cp_save(cp, cp->c); } while (lj_char_isident(cp_get(cp)));

167. cp->str = lj_buf_str(cp->L, &cp->sb);

168. cp->val.id = lj_ctype_getname(cp->cts, &cp->ct, cp->str, cp->tmask);

169. if (ctype_type(cp->ct->info) == CT_KW)

170. return ctype_cid(cp->ct->info);

171. return CTOK_IDENT;

172. }

174. /* Parse parameter. */

175. ‌static CPToken cp_param(CPState *cp)

176. {

177. CPChar c = cp_get(cp);

178. TValue *o = cp->param;

179. if (lj_char_isident(c) || c == '$')  /* Reserve $xyz for future extensions. */

180. cp_errmsg(cp, c, LJ_ERR_XSYNTAX);

181. if (!o || o >= cp->L->top)

182. cp_err(cp, LJ_ERR_FFI_NUMPARAM);

183. cp->param = o+1;

184. if (tvisstr(o)) {

185. cp->str = strV(o);

186. cp->val.id = 0;

187. cp->ct = &cp->cts->tab[0];

188. return CTOK_IDENT;

189. } else if (tvisnumber(o)) {

190. cp->val.i32 = numberVint(o);

191. cp->val.id = CTID_INT32;

192. return CTOK_INTEGER;

193. } else {

194. GCcdata *cd;

195. if (!tviscdata(o))

196. lj_err_argtype(cp->L, (int)(o-cp->L->base)+1, "type parameter");

197. cd = cdataV(o);

198. if (cd->ctypeid == CTID_CTYPEID)

199. cp->val.id = *(CTypeID *)cdataptr(cd);

200. else

201. cp->val.id = cd->ctypeid;

202. return '$';

203. }

204. }

206. /* Parse string or character constant. */

207. ‌static CPToken cp_string(CPState *cp)

208. {

209. CPChar delim = cp->c;

210. cp_get(cp);

211. while (cp->c != delim) {

212. CPChar c = cp->c;

213. if (c == '\0') cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR);

214. if (c == '\\') {

215. c = cp_get(cp);

216. switch (c) {

217. case '\0': cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR); break;

218. case 'a': c = '\a'; break;

219. case 'b': c = '\b'; break;

220. case 'f': c = '\f'; break;

221. case 'n': c = '\n'; break;

222. case 'r': c = '\r'; break;

223. case 't': c = '\t'; break;

224. case 'v': c = '\v'; break;

225. case 'e': c = 27; break;

226. case 'x':

227. c = 0;

228. while (lj_char_isxdigit(cp_get(cp)))

229. c = (c<<4) + (lj_char_isdigit(cp->c) ? cp->c-'0' : (cp->c&15)+9);

230. cp_save(cp, (c & 0xff));

231. continue;

232. default:

233. if (lj_char_isdigit(c)) {

234. c -= '0';

235. if (lj_char_isdigit(cp_get(cp))) {

236. c = c*8 + (cp->c - '0');

237. if (lj_char_isdigit(cp_get(cp))) {

238. c = c*8 + (cp->c - '0');

239. cp_get(cp);

240. }

241. }

242. cp_save(cp, (c & 0xff));

243. continue;

244. }

245. break;

246. }

247. }

248. cp_save(cp, c);

249. cp_get(cp);

250. }

251. cp_get(cp);

252. if (delim == '"') {

253. cp->str = lj_buf_str(cp->L, &cp->sb);

254. return CTOK_STRING;

255. } else {

256. if (sbuflen(&cp->sb) != 1) cp_err_token(cp, '\'');

257. cp->val.i32 = (int32_t)(char)*sbufB(&cp->sb);

258. cp->val.id = CTID_INT32;

259. return CTOK_INTEGER;

260. }

261. }

263. /* Skip C comment. */

264. ‌static void cp_comment_c(CPState *cp)

265. {

266. do {

267. if (cp_get(cp) == '*') {

268. do {

269. if (cp_get(cp) == '/') { cp_get(cp); return; }

270. } while (cp->c == '*');

271. }

272. if (cp_iseol(cp->c)) cp_newline(cp);

273. } while (cp->c != '\0');

274. }

276. /* Skip C++ comment. */

277. ‌static void cp_comment_cpp(CPState *cp)

278. {

279. while (!cp_iseol(cp_get(cp)) && cp->c != '\0')

280. ;

281. }

283. /* Lexical scanner for C. Only a minimal subset is implemented. */

284. ‌static CPToken cp_next_(CPState *cp)

285. {

286. lj_buf_reset(&cp->sb);

287. for (;;) {

288. if (lj_char_isident(cp->c))

289. return lj_char_isdigit(cp->c) ? cp_number(cp) : cp_ident(cp);

290. switch (cp->c) {

291. case '\n': case '\r': cp_newline(cp);  /* fallthrough. */

292. case ' ': case '\t': case '\v': case '\f': cp_get(cp); break;

293. case '"': case '\'': return cp_string(cp);

294. case '/':

295. if (cp_get(cp) == '*') cp_comment_c(cp);

296. else if (cp->c == '/') cp_comment_cpp(cp);

297. else return '/';

298. break;

299. case '|':

300. if (cp_get(cp) != '|') return '|'; cp_get(cp); return CTOK_OROR;

301. case '&':

302. if (cp_get(cp) != '&') return '&'; cp_get(cp); return CTOK_ANDAND;

303. case '=':

304. if (cp_get(cp) != '=') return '='; cp_get(cp); return CTOK_EQ;

305. case '!':

306. if (cp_get(cp) != '=') return '!'; cp_get(cp); return CTOK_NE;

307. case '<':

308. if (cp_get(cp) == '=') { cp_get(cp); return CTOK_LE; }

309. else if (cp->c == '<') { cp_get(cp); return CTOK_SHL; }

310. return '<';

311. case '>':

312. if (cp_get(cp) == '=') { cp_get(cp); return CTOK_GE; }

313. else if (cp->c == '>') { cp_get(cp); return CTOK_SHR; }

314. return '>';

315. case '-':

316. if (cp_get(cp) != '>') return '-'; cp_get(cp); return CTOK_DEREF;

317. case '$':

318. return cp_param(cp);

319. case '\0': return CTOK_EOF;

320. default: { CPToken c = cp->c; cp_get(cp); return c; }

321. }

322. }

323. }

325. ‌static LJ_NOINLINE CPToken cp_next(CPState *cp)

326. {

327. return (cp->tok = cp_next_(cp));

328. }

330. /* -- C parser ------------------------------------------------------------ */

332. /* Namespaces for resolving identifiers. */

333. ‌#define CPNS_DEFAULT \

334. ((1u<<CT_KW)|(1u<<CT_TYPEDEF)|(1u<<CT_FUNC)|(1u<<CT_EXTERN)|(1u<<CT_CONSTVAL))

335. ‌#define CPNS_STRUCT        ((1u<<CT_KW)|(1u<<CT_STRUCT)|(1u<<CT_ENUM))

337. ‌typedef CTypeID CPDeclIdx;        /* Index into declaration stack. */

338. ‌typedef uint32_t CPscl;                /* Storage class flags. */

340. /* Type declaration context. */

341. ‌typedef struct CPDecl {

342. CPDeclIdx top;        /* Top of declaration stack. */

343. CPDeclIdx pos;        /* Insertion position in declaration chain. */

344. CPDeclIdx specpos;        /* Saved position for declaration specifier. */

345. uint32_t mode;        /* Declarator mode. */

346. CPState *cp;                /* C parser state. */

347. GCstr *name;                /* Name of declared identifier (if direct). */

348. GCstr *redir;                /* Redirected symbol name. */

349. CTypeID nameid;        /* Existing typedef for declared identifier. */

350. CTInfo attr;                /* Attributes. */

351. CTInfo fattr;                /* Function attributes. */

352. CTInfo specattr;        /* Saved attributes. */

353. CTInfo specfattr;        /* Saved function attributes. */

354. CTSize bits;                /* Field size in bits (if any). */

355. CType stack[CPARSE_MAX_DECLSTACK];  /* Type declaration stack. */

356. ‌} CPDecl;

358. /* Forward declarations. */

359. static CPscl cp_decl_spec(CPState *cp, CPDecl *decl, CPscl scl);

360. static void cp_declarator(CPState *cp, CPDecl *decl);

361. static CTypeID cp_decl_abstract(CPState *cp);

363. /* Initialize C parser state. Caller must set up: L, p, srcname, mode. */

364. ‌static void cp_init(CPState *cp)

365. {

366. cp->linenumber = 1;

367. cp->depth = 0;

368. cp->curpack = 0;

369. cp->packstack[0] = 255;

370. lj_buf_init(cp->L, &cp->sb);

371. lua_assert(cp->p != NULL);

372. cp_get(cp);  /* Read-ahead first char. */

373. cp->tok = 0;

374. cp->tmask = CPNS_DEFAULT;

375. cp_next(cp);  /* Read-ahead first token. */

376. }

378. /* Cleanup C parser state. */

379. ‌static void cp_cleanup(CPState *cp)

380. {

381. global_State *g = G(cp->L);

382. lj_buf_free(g, &cp->sb);

383. }

385. /* Check and consume optional token. */

386. ‌static int cp_opt(CPState *cp, CPToken tok)

387. {

388. if (cp->tok == tok) { cp_next(cp); return 1; }

389. return 0;

390. }

392. /* Check and consume token. */

393. ‌static void cp_check(CPState *cp, CPToken tok)

394. {

395. if (cp->tok != tok) cp_err_token(cp, tok);

396. cp_next(cp);

397. }

399. /* Check if the next token may start a type declaration. */

400. ‌static int cp_istypedecl(CPState *cp)

401. {

402. if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECL) return 1;

403. if (cp->tok == CTOK_IDENT && ctype_istypedef(cp->ct->info)) return 1;

404. if (cp->tok == '$') return 1;

405. return 0;

406. }

408. /* -- Constant expression evaluator --------------------------------------- */

410. /* Forward declarations. */

411. static void cp_expr_unary(CPState *cp, CPValue *k);

412. static void cp_expr_sub(CPState *cp, CPValue *k, int pri);

414. /* Please note that type handling is very weak here. Most ops simply

415. ** assume integer operands. Accessors are only needed to compute types and

416. ** return synthetic values. The only purpose of the expression evaluator

417. ** is to compute the values of constant expressions one would typically

418. ** find in C header files. And again: this is NOT a validating C parser!

419. */

421. /* Parse comma separated expression and return last result. */

422. ‌static void cp_expr_comma(CPState *cp, CPValue *k)

423. {

424. do { cp_expr_sub(cp, k, 0); } while (cp_opt(cp, ','));

425. }

427. /* Parse sizeof/alignof operator. */

428. ‌static void cp_expr_sizeof(CPState *cp, CPValue *k, int wantsz)

429. {

430. CTSize sz;

431. CTInfo info;

432. if (cp_opt(cp, '(')) {

433. if (cp_istypedecl(cp))

434. k->id = cp_decl_abstract(cp);

435. else

436. cp_expr_comma(cp, k);

437. cp_check(cp, ')');

438. } else {

439. cp_expr_unary(cp, k);

440. }

441. info = lj_ctype_info(cp->cts, k->id, &sz);

442. if (wantsz) {

443. if (sz != CTSIZE_INVALID)

444. k->u32 = sz;

445. else if (k->id != CTID_A_CCHAR)  /* Special case for sizeof("string"). */

446. cp_err(cp, LJ_ERR_FFI_INVSIZE);

447. } else {

448. k->u32 = 1u << ctype_align(info);

449. }

450. k->id = CTID_UINT32;  /* Really size_t. */

451. }

453. /* Parse prefix operators. */

454. ‌static void cp_expr_prefix(CPState *cp, CPValue *k)

455. {

456. if (cp->tok == CTOK_INTEGER) {

457. *k = cp->val; cp_next(cp);

458. } else if (cp_opt(cp, '+')) {

459. cp_expr_unary(cp, k);  /* Nothing to do (well, integer promotion). */

460. } else if (cp_opt(cp, '-')) {

461. cp_expr_unary(cp, k); k->i32 = -k->i32;

462. } else if (cp_opt(cp, '~')) {

463. cp_expr_unary(cp, k); k->i32 = ~k->i32;

464. } else if (cp_opt(cp, '!')) {

465. cp_expr_unary(cp, k); k->i32 = !k->i32; k->id = CTID_INT32;

466. } else if (cp_opt(cp, '(')) {

467. if (cp_istypedecl(cp)) {  /* Cast operator. */

468. CTypeID id = cp_decl_abstract(cp);

469. cp_check(cp, ')');

470. cp_expr_unary(cp, k);

471. k->id = id;  /* No conversion performed. */

472. } else {  /* Sub-expression. */

473. cp_expr_comma(cp, k);

474. cp_check(cp, ')');

475. }

476. } else if (cp_opt(cp, '*')) {  /* Indirection. */

477. CType *ct;

478. cp_expr_unary(cp, k);

479. ct = lj_ctype_rawref(cp->cts, k->id);

480. if (!ctype_ispointer(ct->info))

481. cp_err_badidx(cp, ct);

482. k->u32 = 0; k->id = ctype_cid(ct->info);

483. } else if (cp_opt(cp, '&')) {  /* Address operator. */

484. cp_expr_unary(cp, k);

485. k->id = lj_ctype_intern(cp->cts, CTINFO(CT_PTR, CTALIGN_PTR+k->id),

486. CTSIZE_PTR);

487. } else if (cp_opt(cp, CTOK_SIZEOF)) {

488. cp_expr_sizeof(cp, k, 1);

489. } else if (cp_opt(cp, CTOK_ALIGNOF)) {

490. cp_expr_sizeof(cp, k, 0);

491. } else if (cp->tok == CTOK_IDENT) {

492. if (ctype_type(cp->ct->info) == CT_CONSTVAL) {

493. k->u32 = cp->ct->size; k->id = ctype_cid(cp->ct->info);

494. } else if (ctype_type(cp->ct->info) == CT_EXTERN) {

495. k->u32 = cp->val.id; k->id = ctype_cid(cp->ct->info);

496. } else if (ctype_type(cp->ct->info) == CT_FUNC) {

497. k->u32 = cp->val.id; k->id = cp->val.id;

498. } else {

499. goto err_expr;

500. }

501. cp_next(cp);

502. } else if (cp->tok == CTOK_STRING) {

503. CTSize sz = cp->str->len;

504. while (cp_next(cp) == CTOK_STRING)

505. sz += cp->str->len;

506. k->u32 = sz + 1;

507. k->id = CTID_A_CCHAR;

508. } else {

509. err_expr:

510. cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL);

511. }

512. }

514. /* Parse postfix operators. */

515. ‌static void cp_expr_postfix(CPState *cp, CPValue *k)

516. {

517. for (;;) {

518. CType *ct;

519. if (cp_opt(cp, '[')) {  /* Array/pointer index. */

520. CPValue k2;

521. cp_expr_comma(cp, &k2);

522. ct = lj_ctype_rawref(cp->cts, k->id);

523. if (!ctype_ispointer(ct->info)) {

524. ct = lj_ctype_rawref(cp->cts, k2.id);

525. if (!ctype_ispointer(ct->info))

526. cp_err_badidx(cp, ct);

527. }

528. cp_check(cp, ']');

529. k->u32 = 0;

530. } else if (cp->tok == '.' || cp->tok == CTOK_DEREF) {  /* Struct deref. */

531. CTSize ofs;

532. CType *fct;

533. ct = lj_ctype_rawref(cp->cts, k->id);

534. if (cp->tok == CTOK_DEREF) {

535. if (!ctype_ispointer(ct->info))

536. cp_err_badidx(cp, ct);

537. ct = lj_ctype_rawref(cp->cts, ctype_cid(ct->info));

538. }

539. cp_next(cp);

540. if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT);

541. if (!ctype_isstruct(ct->info) || ct->size == CTSIZE_INVALID ||

542. !(fct = lj_ctype_getfield(cp->cts, ct, cp->str, &ofs)) ||

543. ctype_isbitfield(fct->info)) {

544. GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL);

545. cp_errmsg(cp, 0, LJ_ERR_FFI_BADMEMBER, strdata(s), strdata(cp->str));

546. }

547. ct = fct;

548. k->u32 = ctype_isconstval(ct->info) ? ct->size : 0;

549. cp_next(cp);

550. } else {

551. return;

552. }

553. k->id = ctype_cid(ct->info);

554. }

555. }

557. /* Parse infix operators. */

558. ‌static void cp_expr_infix(CPState *cp, CPValue *k, int pri)

559. {

560. CPValue k2;

561. k2.u32 = 0; k2.id = 0;  /* Silence the compiler. */

562. for (;;) {

563. switch (pri) {

564. case 0:

565. if (cp_opt(cp, '?')) {

566. CPValue k3;

567. cp_expr_comma(cp, &k2);  /* Right-associative. */

568. cp_check(cp, ':');

569. cp_expr_sub(cp, &k3, 0);

570. k->u32 = k->u32 ? k2.u32 : k3.u32;

571. k->id = k2.id > k3.id ? k2.id : k3.id;

572. continue;

573. }

574. case 1:

575. if (cp_opt(cp, CTOK_OROR)) {

576. cp_expr_sub(cp, &k2, 2); k->i32 = k->u32 || k2.u32; k->id = CTID_INT32;

577. continue;

578. }

579. case 2:

580. if (cp_opt(cp, CTOK_ANDAND)) {

581. cp_expr_sub(cp, &k2, 3); k->i32 = k->u32 && k2.u32; k->id = CTID_INT32;

582. continue;

583. }

584. case 3:

585. if (cp_opt(cp, '|')) {

586. cp_expr_sub(cp, &k2, 4); k->u32 = k->u32 | k2.u32; goto arith_result;

587. }

588. case 4:

589. if (cp_opt(cp, '^')) {

590. cp_expr_sub(cp, &k2, 5); k->u32 = k->u32 ^ k2.u32; goto arith_result;

591. }

592. case 5:

593. if (cp_opt(cp, '&')) {

594. cp_expr_sub(cp, &k2, 6); k->u32 = k->u32 & k2.u32; goto arith_result;

595. }

596. case 6:

597. if (cp_opt(cp, CTOK_EQ)) {

598. cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 == k2.u32; k->id = CTID_INT32;

599. continue;

600. } else if (cp_opt(cp, CTOK_NE)) {

601. cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 != k2.u32; k->id = CTID_INT32;

602. continue;

603. }

604. case 7:

605. if (cp_opt(cp, '<')) {

606. cp_expr_sub(cp, &k2, 8);

607. if (k->id == CTID_INT32 && k2.id == CTID_INT32)

608. k->i32 = k->i32 < k2.i32;

609. else

610. k->i32 = k->u32 < k2.u32;

611. k->id = CTID_INT32;

612. continue;

613. } else if (cp_opt(cp, '>')) {

614. cp_expr_sub(cp, &k2, 8);

615. if (k->id == CTID_INT32 && k2.id == CTID_INT32)

616. k->i32 = k->i32 > k2.i32;

617. else

618. k->i32 = k->u32 > k2.u32;

619. k->id = CTID_INT32;

620. continue;

621. } else if (cp_opt(cp, CTOK_LE)) {

622. cp_expr_sub(cp, &k2, 8);

623. if (k->id == CTID_INT32 && k2.id == CTID_INT32)

624. k->i32 = k->i32 <= k2.i32;

625. else

626. k->i32 = k->u32 <= k2.u32;

627. k->id = CTID_INT32;

628. continue;

629. } else if (cp_opt(cp, CTOK_GE)) {

630. cp_expr_sub(cp, &k2, 8);

631. if (k->id == CTID_INT32 && k2.id == CTID_INT32)

632. k->i32 = k->i32 >= k2.i32;

633. else

634. k->i32 = k->u32 >= k2.u32;

635. k->id = CTID_INT32;

636. continue;

637. }

638. case 8:

639. if (cp_opt(cp, CTOK_SHL)) {

640. cp_expr_sub(cp, &k2, 9); k->u32 = k->u32 << k2.u32;

641. continue;

642. } else if (cp_opt(cp, CTOK_SHR)) {

643. cp_expr_sub(cp, &k2, 9);

644. if (k->id == CTID_INT32)

645. k->i32 = k->i32 >> k2.i32;

646. else

647. k->u32 = k->u32 >> k2.u32;

648. continue;

649. }

650. case 9:

651. if (cp_opt(cp, '+')) {

652. cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 + k2.u32;

653. arith_result:

654. if (k2.id > k->id) k->id = k2.id;  /* Trivial promotion to unsigned. */

655. continue;

656. } else if (cp_opt(cp, '-')) {

657. cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 - k2.u32; goto arith_result;

658. }

659. case 10:

660. if (cp_opt(cp, '*')) {

661. cp_expr_unary(cp, &k2); k->u32 = k->u32 * k2.u32; goto arith_result;

662. } else if (cp_opt(cp, '/')) {

663. cp_expr_unary(cp, &k2);

664. if (k2.id > k->id) k->id = k2.id;  /* Trivial promotion to unsigned. */

665. if (k2.u32 == 0 ||

666. (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1))

667. cp_err(cp, LJ_ERR_BADVAL);

668. if (k->id == CTID_INT32)

669. k->i32 = k->i32 / k2.i32;

670. else

671. k->u32 = k->u32 / k2.u32;

672. continue;

673. } else if (cp_opt(cp, '%')) {

674. cp_expr_unary(cp, &k2);

675. if (k2.id > k->id) k->id = k2.id;  /* Trivial promotion to unsigned. */

676. if (k2.u32 == 0 ||

677. (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1))

678. cp_err(cp, LJ_ERR_BADVAL);

679. if (k->id == CTID_INT32)

680. k->i32 = k->i32 % k2.i32;

681. else

682. k->u32 = k->u32 % k2.u32;

683. continue;

684. }

685. default:

686. return;

687. }

688. }

689. }

691. /* Parse and evaluate unary expression. */

692. ‌static void cp_expr_unary(CPState *cp, CPValue *k)

693. {

694. if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS);

695. cp_expr_prefix(cp, k);

696. cp_expr_postfix(cp, k);

697. cp->depth--;

698. }

700. /* Parse and evaluate sub-expression. */

701. ‌static void cp_expr_sub(CPState *cp, CPValue *k, int pri)

702. {

703. cp_expr_unary(cp, k);

704. cp_expr_infix(cp, k, pri);

705. }

707. /* Parse constant integer expression. */

708. ‌static void cp_expr_kint(CPState *cp, CPValue *k)

709. {

710. CType *ct;

711. cp_expr_sub(cp, k, 0);

712. ct = ctype_raw(cp->cts, k->id);

713. if (!ctype_isinteger(ct->info)) cp_err(cp, LJ_ERR_BADVAL);

714. }

716. /* Parse (non-negative) size expression. */

717. ‌static CTSize cp_expr_ksize(CPState *cp)

718. {

719. CPValue k;

720. cp_expr_kint(cp, &k);

721. if (k.u32 >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE);

722. return k.u32;

723. }

725. /* -- Type declaration stack management ----------------------------------- */

727. /* Add declaration element behind the insertion position. */

728. ‌static CPDeclIdx cp_add(CPDecl *decl, CTInfo info, CTSize size)

729. {

730. CPDeclIdx top = decl->top;

731. if (top >= CPARSE_MAX_DECLSTACK) cp_err(decl->cp, LJ_ERR_XLEVELS);

732. decl->stack[top].info = info;

733. decl->stack[top].size = size;

734. decl->stack[top].sib = 0;

735. setgcrefnull(decl->stack[top].name);

736. decl->stack[top].next = decl->stack[decl->pos].next;

737. decl->stack[decl->pos].next = (CTypeID1)top;

738. decl->top = top+1;

739. return top;

740. }

742. /* Push declaration element before the insertion position. */

743. ‌static CPDeclIdx cp_push(CPDecl *decl, CTInfo info, CTSize size)

744. {

745. return (decl->pos = cp_add(decl, info, size));

746. }

748. /* Push or merge attributes. */

749. ‌static void cp_push_attributes(CPDecl *decl)

750. {

751. CType *ct = &decl->stack[decl->pos];

752. if (ctype_isfunc(ct->info)) {  /* Ok to modify in-place. */

753. #if LJ_TARGET_X86

754. if ((decl->fattr & CTFP_CCONV))

755. ct->info = (ct->info & (CTMASK_NUM|CTF_VARARG|CTMASK_CID)) +

756. (decl->fattr & ~CTMASK_CID);

757. #endif

758. } else {

759. if ((decl->attr & CTFP_ALIGNED) && !(decl->mode & CPARSE_MODE_FIELD))

760. cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_ALIGN)),

761. ctype_align(decl->attr));

762. }

763. }

765. /* Push unrolled type to declaration stack and merge qualifiers. */

766. ‌static void cp_push_type(CPDecl *decl, CTypeID id)

767. {

768. CType *ct = ctype_get(decl->cp->cts, id);

769. CTInfo info = ct->info;

770. CTSize size = ct->size;

771. switch (ctype_type(info)) {

772. case CT_STRUCT: case CT_ENUM:

773. cp_push(decl, CTINFO(CT_TYPEDEF, id), 0);  /* Don't copy unique types. */

774. if ((decl->attr & CTF_QUAL)) {  /* Push unmerged qualifiers. */

775. cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_QUAL)),

776. (decl->attr & CTF_QUAL));

777. decl->attr &= ~CTF_QUAL;

778. }

779. break;

780. case CT_ATTRIB:

781. if (ctype_isxattrib(info, CTA_QUAL))

782. decl->attr &= ~size;  /* Remove redundant qualifiers. */

783. cp_push_type(decl, ctype_cid(info));  /* Unroll. */

784. cp_push(decl, info & ~CTMASK_CID, size);  /* Copy type. */

785. break;

786. case CT_ARRAY:

787. cp_push_type(decl, ctype_cid(info));  /* Unroll. */

788. cp_push(decl, info & ~CTMASK_CID, size);  /* Copy type. */

789. decl->stack[decl->pos].sib = 1;  /* Mark as already checked and sized. */

790. /* Note: this is not copied to the ct->sib in the C type table. */

791. break;

792. case CT_FUNC:

793. /* Copy type, link parameters (shared). */

794. decl->stack[cp_push(decl, info, size)].sib = ct->sib;

795. break;

796. default:

797. /* Copy type, merge common qualifiers. */

798. cp_push(decl, info|(decl->attr & CTF_QUAL), size);

799. decl->attr &= ~CTF_QUAL;

800. break;

801. }

802. }

804. /* Consume the declaration element chain and intern the C type. */

805. ‌static CTypeID cp_decl_intern(CPState *cp, CPDecl *decl)

806. {

807. CTypeID id = 0;

808. CPDeclIdx idx = 0;

809. CTSize csize = CTSIZE_INVALID;

810. CTSize cinfo = 0;

811. do {

812. CType *ct = &decl->stack[idx];

813. CTInfo info = ct->info;

814. CTInfo size = ct->size;

815. /* The cid is already part of info for copies of pointers/functions. */

816. idx = ct->next;

817. if (ctype_istypedef(info)) {

818. lua_assert(id == 0);

819. id = ctype_cid(info);

820. /* Always refetch info/size, since struct/enum may have been completed. */

821. cinfo = ctype_get(cp->cts, id)->info;

822. csize = ctype_get(cp->cts, id)->size;

823. lua_assert(ctype_isstruct(cinfo) || ctype_isenum(cinfo));

824. } else if (ctype_isfunc(info)) {  /* Intern function. */

825. CType *fct;

826. CTypeID fid;

827. CTypeID sib;

828. if (id) {

829. CType *refct = ctype_raw(cp->cts, id);

830. /* Reject function or refarray return types. */

831. if (ctype_isfunc(refct->info) || ctype_isrefarray(refct->info))

832. cp_err(cp, LJ_ERR_FFI_INVTYPE);

833. }

834. /* No intervening attributes allowed, skip forward. */

835. while (idx) {

836. CType *ctn = &decl->stack[idx];

837. if (!ctype_isattrib(ctn->info)) break;

838. idx = ctn->next;  /* Skip attribute. */

839. }

840. sib = ct->sib;  /* Next line may reallocate the C type table. */

841. fid = lj_ctype_new(cp->cts, &fct);

842. csize = CTSIZE_INVALID;

843. fct->info = cinfo = info + id;

844. fct->size = size;

845. fct->sib = sib;

846. id = fid;

847. } else if (ctype_isattrib(info)) {

848. if (ctype_isxattrib(info, CTA_QUAL))

849. cinfo |= size;

850. else if (ctype_isxattrib(info, CTA_ALIGN))

851. CTF_INSERT(cinfo, ALIGN, size);

852. id = lj_ctype_intern(cp->cts, info+id, size);

853. /* Inherit csize/cinfo from original type. */

854. } else {

855. if (ctype_isnum(info)) {  /* Handle mode/vector-size attributes. */

856. lua_assert(id == 0);

857. if (!(info & CTF_BOOL)) {

858. CTSize msize = ctype_msizeP(decl->attr);

859. CTSize vsize = ctype_vsizeP(decl->attr);

860. if (msize && (!(info & CTF_FP) || (msize == 4 || msize == 8))) {

861. CTSize malign = lj_fls(msize);

862. if (malign > 4) malign = 4;  /* Limit alignment. */

863. CTF_INSERT(info, ALIGN, malign);

864. size = msize;  /* Override size via mode. */

865. }

866. if (vsize) {  /* Vector size set? */

867. CTSize esize = lj_fls(size);

868. if (vsize >= esize) {

869. /* Intern the element type first. */

870. id = lj_ctype_intern(cp->cts, info, size);

871. /* Then create a vector (array) with vsize alignment. */

872. size = (1u << vsize);

873. if (vsize > 4) vsize = 4;  /* Limit alignment. */

874. if (ctype_align(info) > vsize) vsize = ctype_align(info);

875. info = CTINFO(CT_ARRAY, (info & CTF_QUAL) + CTF_VECTOR +

876. CTALIGN(vsize));

877. }

878. }

879. }

880. } else if (ctype_isptr(info)) {

881. /* Reject pointer/ref to ref. */

882. if (id && ctype_isref(ctype_raw(cp->cts, id)->info))

883. cp_err(cp, LJ_ERR_FFI_INVTYPE);

884. if (ctype_isref(info)) {

885. info &= ~CTF_VOLATILE;  /* Refs are always const, never volatile. */

886. /* No intervening attributes allowed, skip forward. */

887. while (idx) {

888. CType *ctn = &decl->stack[idx];

889. if (!ctype_isattrib(ctn->info)) break;

890. idx = ctn->next;  /* Skip attribute. */

891. }

892. }

893. } else if (ctype_isarray(info)) {  /* Check for valid array size etc. */

894. if (ct->sib == 0) {  /* Only check/size arrays not copied by unroll. */

895. if (ctype_isref(cinfo))  /* Reject arrays of refs. */

896. cp_err(cp, LJ_ERR_FFI_INVTYPE);

897. /* Reject VLS or unknown-sized types. */

898. if (ctype_isvltype(cinfo) || csize == CTSIZE_INVALID)

899. cp_err(cp, LJ_ERR_FFI_INVSIZE);

900. /* a[] and a[?] keep their invalid size. */

901. if (size != CTSIZE_INVALID) {

902. uint64_t xsz = (uint64_t)size * csize;

903. if (xsz >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE);

904. size = (CTSize)xsz;

905. }

906. }

907. if ((cinfo & CTF_ALIGN) > (info & CTF_ALIGN))  /* Find max. align. */

908. info = (info & ~CTF_ALIGN) | (cinfo & CTF_ALIGN);

909. info |= (cinfo & CTF_QUAL);  /* Inherit qual. */

910. } else {

911. lua_assert(ctype_isvoid(info));

912. }

913. csize = size;

914. cinfo = info+id;

915. id = lj_ctype_intern(cp->cts, info+id, size);

916. }

917. } while (idx);

918. return id;

919. }

921. /* -- C declaration parser ------------------------------------------------ */

923. ‌#define H_(le, be)        LJ_ENDIAN_SELECT(0x##le, 0x##be)

925. /* Reset declaration state to declaration specifier. */

926. ‌static void cp_decl_reset(CPDecl *decl)

927. {

928. decl->pos = decl->specpos;

929. decl->top = decl->specpos+1;

930. decl->stack[decl->specpos].next = 0;

931. decl->attr = decl->specattr;

932. decl->fattr = decl->specfattr;

933. decl->name = NULL;

934. decl->redir = NULL;

935. }

937. /* Parse constant initializer. */

938. /* NYI: FP constants and strings as initializers. */

939. ‌static CTypeID cp_decl_constinit(CPState *cp, CType **ctp, CTypeID ctypeid)

940. {

941. CType *ctt = ctype_get(cp->cts, ctypeid);

942. CTInfo info;

943. CTSize size;

944. CPValue k;

945. CTypeID constid;

946. while (ctype_isattrib(ctt->info)) {  /* Skip attributes. */

947. ctypeid = ctype_cid(ctt->info);  /* Update ID, too. */

948. ctt = ctype_get(cp->cts, ctypeid);

949. }

950. info = ctt->info;

951. size = ctt->size;

952. if (!ctype_isinteger(info) || !(info & CTF_CONST) || size > 4)

953. cp_err(cp, LJ_ERR_FFI_INVTYPE);

954. cp_check(cp, '=');

955. cp_expr_sub(cp, &k, 0);

956. constid = lj_ctype_new(cp->cts, ctp);

957. (*ctp)->info = CTINFO(CT_CONSTVAL, CTF_CONST|ctypeid);

958. k.u32 <<= 8*(4-size);

959. if ((info & CTF_UNSIGNED))

960. k.u32 >>= 8*(4-size);

961. else

962. k.u32 = (uint32_t)((int32_t)k.u32 >> 8*(4-size));

963. (*ctp)->size = k.u32;

964. return constid;

965. }

967. /* Parse size in parentheses as part of attribute. */

968. ‌static CTSize cp_decl_sizeattr(CPState *cp)

969. {

970. CTSize sz;

971. uint32_t oldtmask = cp->tmask;

972. cp->tmask = CPNS_DEFAULT;  /* Required for expression evaluator. */

973. cp_check(cp, '(');

974. sz = cp_expr_ksize(cp);

975. cp->tmask = oldtmask;

976. cp_check(cp, ')');

977. return sz;

978. }

980. /* Parse alignment attribute. */

981. ‌static void cp_decl_align(CPState *cp, CPDecl *decl)

982. {

983. CTSize al = 4;  /* Unspecified alignment is 16 bytes. */

984. if (cp->tok == '(') {

985. al = cp_decl_sizeattr(cp);

986. al = al ? lj_fls(al) : 0;

987. }

988. CTF_INSERT(decl->attr, ALIGN, al);

989. decl->attr |= CTFP_ALIGNED;

990. }

992. /* Parse GCC asm("name") redirect. */

993. ‌static void cp_decl_asm(CPState *cp, CPDecl *decl)

994. {

995. UNUSED(decl);

996. cp_next(cp);

997. cp_check(cp, '(');

998. if (cp->tok == CTOK_STRING) {

999. GCstr *str = cp->str;

1000. while (cp_next(cp) == CTOK_STRING) {

1001. lj_strfmt_pushf(cp->L, "%s%s", strdata(str), strdata(cp->str));

1002. cp->L->top--;

1003. str = strV(cp->L->top);

1004. }

1005. decl->redir = str;

1006. }

1007. cp_check(cp, ')');

1008. }

1010. /* Parse GCC __attribute__((mode(...))). */

1011. ‌static void cp_decl_mode(CPState *cp, CPDecl *decl)

1012. {

1013. cp_check(cp, '(');

1014. if (cp->tok == CTOK_IDENT) {

1015. const char *s = strdata(cp->str);

1016. CTSize sz = 0, vlen = 0;

1017. if (s[0] == '_' && s[1] == '_') s += 2;

1018. if (*s == 'V') {

1019. s++;

1020. vlen = *s++ - '0';

1021. if (*s >= '0' && *s <= '9')

1022. vlen = vlen*10 + (*s++ - '0');

1023. }

1024. switch (*s++) {

1025. case 'Q': sz = 1; break;

1026. case 'H': sz = 2; break;

1027. case 'S': sz = 4; break;

1028. case 'D': sz = 8; break;

1029. case 'T': sz = 16; break;

1030. case 'O': sz = 32; break;

1031. default: goto bad_size;

1032. }

1033. if (*s == 'I' || *s == 'F') {

1034. CTF_INSERT(decl->attr, MSIZEP, sz);

1035. if (vlen) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vlen*sz));

1036. }

1037. bad_size:

1038. cp_next(cp);

1039. }

1040. cp_check(cp, ')');

1041. }

1043. /* Parse GCC __attribute__((...)). */

1044. ‌static void cp_decl_gccattribute(CPState *cp, CPDecl *decl)

1045. {

1046. cp_next(cp);

1047. cp_check(cp, '(');

1048. cp_check(cp, '(');

1049. while (cp->tok != ')') {

1050. if (cp->tok == CTOK_IDENT) {

1051. GCstr *attrstr = cp->str;

1052. cp_next(cp);

1053. switch (attrstr->hash) {

1054. case H_(64a9208e,8ce14319): case H_(8e6331b2,95a282af):  /* aligned */

1055. cp_decl_align(cp, decl);

1056. break;

1057. case H_(42eb47de,f0ede26c): case H_(29f48a09,cf383e0c):  /* packed */

1058. decl->attr |= CTFP_PACKED;

1059. break;

1060. case H_(0a84eef6,8dfab04c): case H_(995cf92c,d5696591):  /* mode */

1061. cp_decl_mode(cp, decl);

1062. break;

1063. case H_(0ab31997,2d5213fa): case H_(bf875611,200e9990):  /* vector_size */

1064. {

1065. CTSize vsize = cp_decl_sizeattr(cp);

1066. if (vsize) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vsize));

1067. }

1068. break;

1069. #if LJ_TARGET_X86

1070. case H_(5ad22db8,c689b848): case H_(439150fa,65ea78cb):  /* regparm */

1071. CTF_INSERT(decl->fattr, REGPARM, cp_decl_sizeattr(cp));

1072. decl->fattr |= CTFP_CCONV;

1073. break;

1074. case H_(18fc0b98,7ff4c074): case H_(4e62abed,0a747424):  /* cdecl */

1075. CTF_INSERT(decl->fattr, CCONV, CTCC_CDECL);

1076. decl->fattr |= CTFP_CCONV;

1077. break;

1078. case H_(72b2e41b,494c5a44): case H_(f2356d59,f25fc9bd):  /* thiscall */

1079. CTF_INSERT(decl->fattr, CCONV, CTCC_THISCALL);

1080. decl->fattr |= CTFP_CCONV;

1081. break;

1082. case H_(0d0ffc42,ab746f88): case H_(21c54ba1,7f0ca7e3):  /* fastcall */

1083. CTF_INSERT(decl->fattr, CCONV, CTCC_FASTCALL);

1084. decl->fattr |= CTFP_CCONV;

1085. break;

1086. case H_(ef76b040,9412e06a): case H_(de56697b,c750e6e1):  /* stdcall */

1087. CTF_INSERT(decl->fattr, CCONV, CTCC_STDCALL);

1088. decl->fattr |= CTFP_CCONV;

1089. break;

1090. case H_(ea78b622,f234bd8e): case H_(252ffb06,8d50f34b):  /* sseregparm */

1091. decl->fattr |= CTF_SSEREGPARM;

1092. decl->fattr |= CTFP_CCONV;

1093. break;

1094. #endif

1095. default:  /* Skip all other attributes. */

1096. goto skip_attr;

1097. }

1098. } else if (cp->tok >= CTOK_FIRSTDECL) {  /* For __attribute((const)) etc. */

1099. cp_next(cp);

1100. skip_attr:

1101. if (cp_opt(cp, '(')) {

1102. while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp);

1103. cp_check(cp, ')');

1104. }

1105. } else {

1106. break;

1107. }

1108. if (!cp_opt(cp, ',')) break;

1109. }

1110. cp_check(cp, ')');

1111. cp_check(cp, ')');

1112. }

1114. /* Parse MSVC __declspec(...). */

1115. ‌static void cp_decl_msvcattribute(CPState *cp, CPDecl *decl)

1116. {

1117. cp_next(cp);

1118. cp_check(cp, '(');

1119. while (cp->tok == CTOK_IDENT) {

1120. GCstr *attrstr = cp->str;

1121. cp_next(cp);

1122. switch (attrstr->hash) {

1123. case H_(bc2395fa,98f267f8):  /* align */

1124. cp_decl_align(cp, decl);

1125. break;

1126. default:  /* Ignore all other attributes. */

1127. if (cp_opt(cp, '(')) {

1128. while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp);

1129. cp_check(cp, ')');

1130. }

1131. break;

1132. }

1133. }

1134. cp_check(cp, ')');

1135. }

1137. /* Parse declaration attributes (and common qualifiers). */

1138. ‌static void cp_decl_attributes(CPState *cp, CPDecl *decl)

1139. {

1140. for (;;) {

1141. switch (cp->tok) {

1142. case CTOK_CONST: decl->attr |= CTF_CONST; break;

1143. case CTOK_VOLATILE: decl->attr |= CTF_VOLATILE; break;

1144. case CTOK_RESTRICT: break;  /* Ignore. */

1145. case CTOK_EXTENSION: break;  /* Ignore. */

1146. case CTOK_ATTRIBUTE: cp_decl_gccattribute(cp, decl); continue;

1147. case CTOK_ASM: cp_decl_asm(cp, decl); continue;

1148. case CTOK_DECLSPEC: cp_decl_msvcattribute(cp, decl); continue;

1149. case CTOK_CCDECL:

1150. #if LJ_TARGET_X86

1151. CTF_INSERT(decl->fattr, CCONV, cp->ct->size);

1152. decl->fattr |= CTFP_CCONV;

1153. #endif

1154. break;

1155. case CTOK_PTRSZ:

1156. #if LJ_64

1157. CTF_INSERT(decl->attr, MSIZEP, cp->ct->size);

1158. #endif

1159. break;

1160. default: return;

1161. }

1162. cp_next(cp);

1163. }

1164. }

1166. /* Parse struct/union/enum name. */

1167. ‌static CTypeID cp_struct_name(CPState *cp, CPDecl *sdecl, CTInfo info)

1168. {

1169. CTypeID sid;

1170. CType *ct;

1171. cp->tmask = CPNS_STRUCT;

1172. cp_next(cp);

1173. cp_decl_attributes(cp, sdecl);

1174. cp->tmask = CPNS_DEFAULT;

1175. if (cp->tok != '{') {

1176. if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT);

1177. if (cp->val.id) {  /* Name of existing struct/union/enum. */

1178. sid = cp->val.id;

1179. ct = cp->ct;

1180. if ((ct->info ^ info) & (CTMASK_NUM|CTF_UNION))  /* Wrong type. */

1181. cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name))));

1182. } else {  /* Create named, incomplete struct/union/enum. */

1183. if ((cp->mode & CPARSE_MODE_NOIMPLICIT))

1184. cp_errmsg(cp, 0, LJ_ERR_FFI_BADTAG, strdata(cp->str));

1185. sid = lj_ctype_new(cp->cts, &ct);

1186. ct->info = info;

1187. ct->size = CTSIZE_INVALID;

1188. ctype_setname(ct, cp->str);

1189. lj_ctype_addname(cp->cts, ct, sid);

1190. }

1191. cp_next(cp);

1192. } else {  /* Create anonymous, incomplete struct/union/enum. */

1193. sid = lj_ctype_new(cp->cts, &ct);

1194. ct->info = info;

1195. ct->size = CTSIZE_INVALID;

1196. }

1197. if (cp->tok == '{') {

1198. if (ct->size != CTSIZE_INVALID || ct->sib)

1199. cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name))));

1200. ct->sib = 1;  /* Indicate the type is currently being defined. */

1201. }

1202. return sid;

1203. }

1205. /* Determine field alignment. */

1206. ‌static CTSize cp_field_align(CPState *cp, CType *ct, CTInfo info)

1207. {

1208. CTSize align = ctype_align(info);

1209. UNUSED(cp); UNUSED(ct);

1210. #if (LJ_TARGET_X86 && !LJ_ABI_WIN) || (LJ_TARGET_ARM && __APPLE__)

1211. /* The SYSV i386 and iOS ABIs limit alignment of non-vector fields to 2^2. */

1212. if (align > 2 && !(info & CTFP_ALIGNED)) {

1213. if (ctype_isarray(info) && !(info & CTF_VECTOR)) {

1214. do {

1215. ct = ctype_rawchild(cp->cts, ct);

1216. info = ct->info;

1217. } while (ctype_isarray(info) && !(info & CTF_VECTOR));

1218. }

1219. if (ctype_isnum(info) || ctype_isenum(info))

1220. align = 2;

1221. }

1222. #endif

1223. return align;

1224. }

1226. /* Layout struct/union fields. */

1227. ‌static void cp_struct_layout(CPState *cp, CTypeID sid, CTInfo sattr)

1228. {

1229. CTSize bofs = 0, bmaxofs = 0;  /* Bit offset and max. bit offset. */

1230. CTSize maxalign = ctype_align(sattr);

1231. CType *sct = ctype_get(cp->cts, sid);

1232. CTInfo sinfo = sct->info;

1233. CTypeID fieldid = sct->sib;

1234. while (fieldid) {

1235. CType *ct = ctype_get(cp->cts, fieldid);

1236. CTInfo attr = ct->size;  /* Field declaration attributes (temp.). */

1238. if (ctype_isfield(ct->info) ||

1239. (ctype_isxattrib(ct->info, CTA_SUBTYPE) && attr)) {

1240. CTSize align, amask;  /* Alignment (pow2) and alignment mask (bits). */

1241. CTSize sz;

1242. CTInfo info = lj_ctype_info(cp->cts, ctype_cid(ct->info), &sz);

1243. CTSize bsz, csz = 8*sz;  /* Field size and container size (in bits). */

1244. sinfo |= (info & (CTF_QUAL|CTF_VLA));  /* Merge pseudo-qualifiers. */

1246. /* Check for size overflow and determine alignment. */

1247. if (sz >= 0x20000000u || bofs + csz < bofs || (info & CTF_VLA)) {

1248. if (!(sz == CTSIZE_INVALID && ctype_isarray(info) &&

1249. !(sinfo & CTF_UNION)))

1250. cp_err(cp, LJ_ERR_FFI_INVSIZE);

1251. csz = sz = 0;  /* Treat a[] and a[?] as zero-sized. */

1252. }

1253. align = cp_field_align(cp, ct, info);

1254. if (((attr|sattr) & CTFP_PACKED) ||

1255. ((attr & CTFP_ALIGNED) && ctype_align(attr) > align))

1256. align = ctype_align(attr);

1257. if (cp->packstack[cp->curpack] < align)

1258. align = cp->packstack[cp->curpack];

1259. if (align > maxalign) maxalign = align;

1260. amask = (8u << align) - 1;

1262. bsz = ctype_bitcsz(ct->info);  /* Bitfield size (temp.). */

1263. if (bsz == CTBSZ_FIELD || !ctype_isfield(ct->info)) {

1264. bsz = csz;  /* Regular fields or subtypes always fill the container. */

1265. bofs = (bofs + amask) & ~amask;  /* Start new aligned field. */

1266. ct->size = (bofs >> 3);  /* Store field offset. */

1267. } else {  /* Bitfield. */

1268. if (bsz == 0 || (attr & CTFP_ALIGNED) ||

1269. (!((attr|sattr) & CTFP_PACKED) && (bofs & amask) + bsz > csz))

1270. bofs = (bofs + amask) & ~amask;  /* Start new aligned field. */

1272. /* Prefer regular field over bitfield. */

1273. if (bsz == csz && (bofs & amask) == 0) {

1274. ct->info = CTINFO(CT_FIELD, ctype_cid(ct->info));

1275. ct->size = (bofs >> 3);  /* Store field offset. */

1276. } else {

1277. ct->info = CTINFO(CT_BITFIELD,

1278. (info & (CTF_QUAL|CTF_UNSIGNED|CTF_BOOL)) +

1279. (csz << (CTSHIFT_BITCSZ-3)) + (bsz << CTSHIFT_BITBSZ));

1280. #if LJ_BE

1281. ct->info += ((csz - (bofs & (csz-1)) - bsz) << CTSHIFT_BITPOS);

1282. #else

1283. ct->info += ((bofs & (csz-1)) << CTSHIFT_BITPOS);

1284. #endif

1285. ct->size = ((bofs & ~(csz-1)) >> 3);  /* Store container offset. */

1286. }

1287. }

1289. /* Determine next offset or max. offset. */

1290. if ((sinfo & CTF_UNION)) {

1291. if (bsz > bmaxofs) bmaxofs = bsz;

1292. } else {

1293. bofs += bsz;

1294. }

1295. }  /* All other fields in the chain are already set up. */

1297. fieldid = ct->sib;

1298. }

1300. /* Complete struct/union. */

1301. sct->info = sinfo + CTALIGN(maxalign);

1302. bofs = (sinfo & CTF_UNION) ? bmaxofs : bofs;

1303. maxalign = (8u << maxalign) - 1;

1304. sct->size = (((bofs + maxalign) & ~maxalign) >> 3);

1305. }

1307. /* Parse struct/union declaration. */

1308. ‌static CTypeID cp_decl_struct(CPState *cp, CPDecl *sdecl, CTInfo sinfo)

1309. {

1310. CTypeID sid = cp_struct_name(cp, sdecl, sinfo);

1311. if (cp_opt(cp, '{')) {  /* Struct/union definition. */

1312. CTypeID lastid = sid;

1313. int lastdecl = 0;

1314. while (cp->tok != '}') {

1315. CPDecl decl;

1316. CPscl scl = cp_decl_spec(cp, &decl, CDF_STATIC);

1317. decl.mode = scl ? CPARSE_MODE_DIRECT :

1318. CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT|CPARSE_MODE_FIELD;

1320. for (;;) {

1321. CTypeID ctypeid;

1323. if (lastdecl) cp_err_token(cp, '}');

1325. /* Parse field declarator. */

1326. decl.bits = CTSIZE_INVALID;

1327. cp_declarator(cp, &decl);

1328. ctypeid = cp_decl_intern(cp, &decl);

1330. if ((scl & CDF_STATIC)) {  /* Static constant in struct namespace. */

1331. CType *ct;

1332. CTypeID fieldid = cp_decl_constinit(cp, &ct, ctypeid);

1333. ctype_get(cp->cts, lastid)->sib = fieldid;

1334. lastid = fieldid;

1335. ctype_setname(ct, decl.name);

1336. } else {

1337. CTSize bsz = CTBSZ_FIELD;  /* Temp. for layout phase. */

1338. CType *ct;

1339. CTypeID fieldid = lj_ctype_new(cp->cts, &ct);  /* Do this first. */

1340. CType *tct = ctype_raw(cp->cts, ctypeid);

1342. if (decl.bits == CTSIZE_INVALID) {  /* Regular field. */

1343. if (ctype_isarray(tct->info) && tct->size == CTSIZE_INVALID)

1344. lastdecl = 1;  /* a[] or a[?] must be the last declared field. */

1346. /* Accept transparent struct/union/enum. */

1347. if (!decl.name) {

1348. if (!((ctype_isstruct(tct->info) && !(tct->info & CTF_VLA)) ||

1349. ctype_isenum(tct->info)))

1350. cp_err_token(cp, CTOK_IDENT);

1351. ct->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_SUBTYPE) + ctypeid);

1352. ct->size = ctype_isstruct(tct->info) ?

1353. (decl.attr|0x80000000u) : 0;  /* For layout phase. */

1354. goto add_field;

1355. }

1356. } else {  /* Bitfield. */

1357. bsz = decl.bits;

1358. if (!ctype_isinteger_or_bool(tct->info) ||

1359. (bsz == 0 && decl.name) || 8*tct->size > CTBSZ_MAX ||

1360. bsz > ((tct->info & CTF_BOOL) ? 1 : 8*tct->size))

1361. cp_errmsg(cp, ':', LJ_ERR_BADVAL);

1362. }

1364. /* Create temporary field for layout phase. */

1365. ct->info = CTINFO(CT_FIELD, ctypeid + (bsz << CTSHIFT_BITCSZ));

1366. ct->size = decl.attr;

1367. if (decl.name) ctype_setname(ct, decl.name);

1369. add_field:

1370. ctype_get(cp->cts, lastid)->sib = fieldid;

1371. lastid = fieldid;

1372. }

1373. if (!cp_opt(cp, ',')) break;

1374. cp_decl_reset(&decl);

1375. }

1376. cp_check(cp, ';');

1377. }

1378. cp_check(cp, '}');

1379. ctype_get(cp->cts, lastid)->sib = 0;  /* Drop sib = 1 for empty structs. */

1380. cp_decl_attributes(cp, sdecl);  /* Layout phase needs postfix attributes. */

1381. cp_struct_layout(cp, sid, sdecl->attr);

1382. }

1383. return sid;

1384. }

1386. /* Parse enum declaration. */

1387. ‌static CTypeID cp_decl_enum(CPState *cp, CPDecl *sdecl)

1388. {

1389. CTypeID eid = cp_struct_name(cp, sdecl, CTINFO(CT_ENUM, CTID_VOID));

1390. CTInfo einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_UINT32);

1391. CTSize esize = 4;  /* Only 32 bit enums are supported. */

1392. if (cp_opt(cp, '{')) {  /* Enum definition. */

1393. CPValue k;

1394. CTypeID lastid = eid;

1395. k.u32 = 0;

1396. k.id = CTID_INT32;

1397. do {

1398. GCstr *name = cp->str;

1399. if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT);

1400. if (cp->val.id) cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(name));

1401. cp_next(cp);

1402. if (cp_opt(cp, '=')) {

1403. cp_expr_kint(cp, &k);

1404. if (k.id == CTID_UINT32) {

1405. /* C99 says that enum constants are always (signed) integers.

1406. ** But since unsigned constants like 0x80000000 are quite common,

1407. ** those are left as uint32_t.

1408. */

1409. if (k.i32 >= 0) k.id = CTID_INT32;

1410. } else {

1411. /* OTOH it's common practice and even mandated by some ABIs

1412. ** that the enum type itself is unsigned, unless there are any

1413. ** negative constants.

1414. */

1415. k.id = CTID_INT32;

1416. if (k.i32 < 0) einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_INT32);

1417. }

1418. }

1419. /* Add named enum constant. */

1420. {

1421. CType *ct;

1422. CTypeID constid = lj_ctype_new(cp->cts, &ct);

1423. ctype_get(cp->cts, lastid)->sib = constid;

1424. lastid = constid;

1425. ctype_setname(ct, name);

1426. ct->info = CTINFO(CT_CONSTVAL, CTF_CONST|k.id);

1427. ct->size = k.u32++;

1428. if (k.u32 == 0x80000000u) k.id = CTID_UINT32;

1429. lj_ctype_addname(cp->cts, ct, constid);

1430. }

1431. if (!cp_opt(cp, ',')) break;

1432. } while (cp->tok != '}');  /* Trailing ',' is ok. */

1433. cp_check(cp, '}');

1434. /* Complete enum. */

1435. ctype_get(cp->cts, eid)->info = einfo;

1436. ctype_get(cp->cts, eid)->size = esize;

1437. }

1438. return eid;

1439. }

1441. /* Parse declaration specifiers. */

1442. ‌static CPscl cp_decl_spec(CPState *cp, CPDecl *decl, CPscl scl)

1443. {

1444. uint32_t cds = 0, sz = 0;

1445. CTypeID tdef = 0;

1447. decl->cp = cp;

1448. decl->mode = cp->mode;

1449. decl->name = NULL;

1450. decl->redir = NULL;

1451. decl->attr = 0;

1452. decl->fattr = 0;

1453. decl->pos = decl->top = 0;

1454. decl->stack[0].next = 0;

1456. for (;;) {  /* Parse basic types. */

1457. cp_decl_attributes(cp, decl);

1458. if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECLFLAG) {

1459. uint32_t cbit;

1460. if (cp->ct->size) {

1461. if (sz) goto end_decl;

1462. sz = cp->ct->size;

1463. }

1464. cbit = (1u << (cp->tok - CTOK_FIRSTDECL));

1465. cds = cds | cbit | ((cbit & cds & CDF_LONG) << 1);

1466. if (cp->tok >= CTOK_FIRSTSCL) {

1467. if (!(scl & cbit)) cp_errmsg(cp, cp->tok, LJ_ERR_FFI_BADSCL);

1468. } else if (tdef) {

1469. goto end_decl;

1470. }

1471. cp_next(cp);

1472. continue;

1473. }

1474. if (sz || tdef ||

1475. (cds & (CDF_SHORT|CDF_LONG|CDF_SIGNED|CDF_UNSIGNED|CDF_COMPLEX)))

1476. break;

1477. switch (cp->tok) {

1478. case CTOK_STRUCT:

1479. tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, 0));

1480. continue;

1481. case CTOK_UNION:

1482. tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, CTF_UNION));

1483. continue;

1484. case CTOK_ENUM:

1485. tdef = cp_decl_enum(cp, decl);

1486. continue;

1487. case CTOK_IDENT:

1488. if (ctype_istypedef(cp->ct->info)) {

1489. tdef = ctype_cid(cp->ct->info);  /* Get typedef. */

1490. cp_next(cp);

1491. continue;

1492. }

1493. break;

1494. case '$':

1495. tdef = cp->val.id;

1496. cp_next(cp);

1497. continue;

1498. default:

1499. break;

1500. }

1501. break;

1502. }

1503. end_decl:

1505. if ((cds & CDF_COMPLEX))  /* Use predefined complex types. */

1506. tdef = sz == 4 ? CTID_COMPLEX_FLOAT : CTID_COMPLEX_DOUBLE;

1508. if (tdef) {

1509. cp_push_type(decl, tdef);

1510. } else if ((cds & CDF_VOID)) {

1511. cp_push(decl, CTINFO(CT_VOID, (decl->attr & CTF_QUAL)), CTSIZE_INVALID);

1512. decl->attr &= ~CTF_QUAL;

1513. } else {

1514. /* Determine type info and size. */

1515. CTInfo info = CTINFO(CT_NUM, (cds & CDF_UNSIGNED) ? CTF_UNSIGNED : 0);

1516. if ((cds & CDF_BOOL)) {

1517. if ((cds & ~(CDF_SCL|CDF_BOOL|CDF_INT|CDF_SIGNED|CDF_UNSIGNED)))

1518. cp_errmsg(cp, 0, LJ_ERR_FFI_INVTYPE);

1519. info |= CTF_BOOL;

1520. if (!(cds & CDF_SIGNED)) info |= CTF_UNSIGNED;

1521. if (!sz) {

1522. sz = 1;

1523. }

1524. } else if ((cds & CDF_FP)) {

1525. info = CTINFO(CT_NUM, CTF_FP);

1526. if ((cds & CDF_LONG)) sz = sizeof(long double);

1527. } else if ((cds & CDF_CHAR)) {

1528. if ((cds & (CDF_CHAR|CDF_SIGNED|CDF_UNSIGNED)) == CDF_CHAR)

1529. info |= CTF_UCHAR;  /* Handle platforms where char is unsigned. */

1530. } else if ((cds & CDF_SHORT)) {

1531. sz = sizeof(short);

1532. } else if ((cds & CDF_LONGLONG)) {

1533. sz = 8;

1534. } else if ((cds & CDF_LONG)) {

1535. info |= CTF_LONG;

1536. sz = sizeof(long);

1537. } else if (!sz) {

1538. if (!(cds & (CDF_SIGNED|CDF_UNSIGNED)))

1539. cp_errmsg(cp, cp->tok, LJ_ERR_FFI_DECLSPEC);

1540. sz = sizeof(int);

1541. }

1542. lua_assert(sz != 0);

1543. info += CTALIGN(lj_fls(sz));  /* Use natural alignment. */

1544. info += (decl->attr & CTF_QUAL);  /* Merge qualifiers. */

1545. cp_push(decl, info, sz);

1546. decl->attr &= ~CTF_QUAL;

1547. }

1548. decl->specpos = decl->pos;

1549. decl->specattr = decl->attr;

1550. decl->specfattr = decl->fattr;

1551. return (cds & CDF_SCL);  /* Return storage class. */

1552. }

1554. /* Parse array declaration. */

1555. ‌static void cp_decl_array(CPState *cp, CPDecl *decl)

1556. {

1557. CTInfo info = CTINFO(CT_ARRAY, 0);

1558. CTSize nelem = CTSIZE_INVALID;  /* Default size for a[] or a[?]. */

1559. cp_decl_attributes(cp, decl);

1560. if (cp_opt(cp, '?'))

1561. info |= CTF_VLA;  /* Create variable-length array a[?]. */

1562. else if (cp->tok != ']')

1563. nelem = cp_expr_ksize(cp);

1564. cp_check(cp, ']');

1565. cp_add(decl, info, nelem);

1566. }

1568. /* Parse function declaration. */

1569. ‌static void cp_decl_func(CPState *cp, CPDecl *fdecl)

1570. {

1571. CTSize nargs = 0;

1572. CTInfo info = CTINFO(CT_FUNC, 0);

1573. CTypeID lastid = 0, anchor = 0;

1574. if (cp->tok != ')') {

1575. do {

1576. CPDecl decl;

1577. CTypeID ctypeid, fieldid;

1578. CType *ct;

1579. if (cp_opt(cp, '.')) {  /* Vararg function. */

1580. cp_check(cp, '.');  /* Workaround for the minimalistic lexer. */

1581. cp_check(cp, '.');

1582. info |= CTF_VARARG;

1583. break;

1584. }

1585. cp_decl_spec(cp, &decl, CDF_REGISTER);

1586. decl.mode = CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT;

1587. cp_declarator(cp, &decl);

1588. ctypeid = cp_decl_intern(cp, &decl);

1589. ct = ctype_raw(cp->cts, ctypeid);

1590. if (ctype_isvoid(ct->info))

1591. break;

1592. else if (ctype_isrefarray(ct->info))

1593. ctypeid = lj_ctype_intern(cp->cts,

1594. CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ct->info)), CTSIZE_PTR);

1595. else if (ctype_isfunc(ct->info))

1596. ctypeid = lj_ctype_intern(cp->cts,

1597. CTINFO(CT_PTR, CTALIGN_PTR|ctypeid), CTSIZE_PTR);

1598. /* Add new parameter. */

1599. fieldid = lj_ctype_new(cp->cts, &ct);

1600. if (anchor)

1601. ctype_get(cp->cts, lastid)->sib = fieldid;

1602. else

1603. anchor = fieldid;

1604. lastid = fieldid;

1605. if (decl.name) ctype_setname(ct, decl.name);

1606. ct->info = CTINFO(CT_FIELD, ctypeid);

1607. ct->size = nargs++;

1608. } while (cp_opt(cp, ','));

1609. }

1610. cp_check(cp, ')');

1611. if (cp_opt(cp, '{')) {  /* Skip function definition. */

1612. int level = 1;

1613. cp->mode |= CPARSE_MODE_SKIP;

1614. for (;;) {

1615. if (cp->tok == '{') level++;

1616. else if (cp->tok == '}' && --level == 0) break;

1617. else if (cp->tok == CTOK_EOF) cp_err_token(cp, '}');

1618. cp_next(cp);

1619. }

1620. cp->mode &= ~CPARSE_MODE_SKIP;

1621. cp->tok = ';';  /* Ok for cp_decl_multi(), error in cp_decl_single(). */

1622. }

1623. info |= (fdecl->fattr & ~CTMASK_CID);

1624. fdecl->fattr = 0;

1625. fdecl->stack[cp_add(fdecl, info, nargs)].sib = anchor;

1626. }

1628. /* Parse declarator. */

1629. ‌static void cp_declarator(CPState *cp, CPDecl *decl)

1630. {

1631. if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS);

1633. for (;;) {  /* Head of declarator. */

1634. if (cp_opt(cp, '*')) {  /* Pointer. */

1635. CTSize sz;

1636. CTInfo info;

1637. cp_decl_attributes(cp, decl);

1638. sz = CTSIZE_PTR;

1639. info = CTINFO(CT_PTR, CTALIGN_PTR);

1640. #if LJ_64

1641. if (ctype_msizeP(decl->attr) == 4) {

1642. sz = 4;

1643. info = CTINFO(CT_PTR, CTALIGN(2));

1644. }

1645. #endif

1646. info += (decl->attr & (CTF_QUAL|CTF_REF));

1647. decl->attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<<CTSHIFT_MSIZEP));

1648. cp_push(decl, info, sz);

1649. } else if (cp_opt(cp, '&') || cp_opt(cp, CTOK_ANDAND)) {  /* Reference. */

1650. decl->attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<<CTSHIFT_MSIZEP));

1651. cp_push(decl, CTINFO_REF(0), CTSIZE_PTR);

1652. } else {

1653. break;

1654. }

1655. }

1657. if (cp_opt(cp, '(')) {  /* Inner declarator. */

1658. CPDeclIdx pos;

1659. cp_decl_attributes(cp, decl);

1660. /* Resolve ambiguity between inner declarator and 1st function parameter. */

1661. if ((decl->mode & CPARSE_MODE_ABSTRACT) &&

1662. (cp->tok == ')' || cp_istypedecl(cp))) goto func_decl;

1663. pos = decl->pos;

1664. cp_declarator(cp, decl);

1665. cp_check(cp, ')');

1666. decl->pos = pos;

1667. } else if (cp->tok == CTOK_IDENT) {  /* Direct declarator. */

1668. if (!(decl->mode & CPARSE_MODE_DIRECT)) cp_err_token(cp, CTOK_EOF);

1669. decl->name = cp->str;

1670. decl->nameid = cp->val.id;

1671. cp_next(cp);

1672. } else {  /* Abstract declarator. */

1673. if (!(decl->mode & CPARSE_MODE_ABSTRACT)) cp_err_token(cp, CTOK_IDENT);

1674. }

1676. for (;;) {  /* Tail of declarator. */

1677. if (cp_opt(cp, '[')) {  /* Array. */

1678. cp_decl_array(cp, decl);

1679. } else if (cp_opt(cp, '(')) {  /* Function. */

1680. func_decl:

1681. cp_decl_func(cp, decl);

1682. } else {

1683. break;

1684. }

1685. }

1687. if ((decl->mode & CPARSE_MODE_FIELD) && cp_opt(cp, ':'))  /* Field width. */

1688. decl->bits = cp_expr_ksize(cp);

1690. /* Process postfix attributes. */

1691. cp_decl_attributes(cp, decl);

1692. cp_push_attributes(decl);

1694. cp->depth--;

1695. }

1697. /* Parse an abstract type declaration and return it's C type ID. */

1698. ‌static CTypeID cp_decl_abstract(CPState *cp)

1699. {

1700. CPDecl decl;

1701. cp_decl_spec(cp, &decl, 0);

1702. decl.mode = CPARSE_MODE_ABSTRACT;

1703. cp_declarator(cp, &decl);

1704. return cp_decl_intern(cp, &decl);

1705. }

1707. /* Handle pragmas. */

1708. ‌static void cp_pragma(CPState *cp, BCLine pragmaline)

1709. {

1710. cp_next(cp);

1711. if (cp->tok == CTOK_IDENT &&

1712. cp->str->hash == H_(e79b999f,42ca3e85))  {  /* pack */

1713. cp_next(cp);

1714. cp_check(cp, '(');

1715. if (cp->tok == CTOK_IDENT) {

1716. if (cp->str->hash == H_(738e923c,a1b65954)) {  /* push */

1717. if (cp->curpack < CPARSE_MAX_PACKSTACK) {

1718. cp->packstack[cp->curpack+1] = cp->packstack[cp->curpack];

1719. cp->curpack++;

1720. }

1721. } else if (cp->str->hash == H_(6c71cf27,6c71cf27)) {  /* pop */

1722. if (cp->curpack > 0) cp->curpack--;

1723. } else {

1724. cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL);

1725. }

1726. cp_next(cp);

1727. if (!cp_opt(cp, ',')) goto end_pack;

1728. }

1729. if (cp->tok == CTOK_INTEGER) {

1730. cp->packstack[cp->curpack] = cp->val.u32 ? lj_fls(cp->val.u32) : 0;

1731. cp_next(cp);

1732. } else {

1733. cp->packstack[cp->curpack] = 255;

1734. }

1735. end_pack:

1736. cp_check(cp, ')');

1737. } else {  /* Ignore all other pragmas. */

1738. while (cp->tok != CTOK_EOF && cp->linenumber == pragmaline)

1739. cp_next(cp);

1740. }

1741. }

1743. /* Parse multiple C declarations of types or extern identifiers. */

1744. ‌static void cp_decl_multi(CPState *cp)

1745. {

1746. int first = 1;

1747. while (cp->tok != CTOK_EOF) {

1748. CPDecl decl;

1749. CPscl scl;

1750. if (cp_opt(cp, ';')) {  /* Skip empty statements. */

1751. first = 0;

1752. continue;

1753. }

1754. if (cp->tok == '#') {  /* Workaround, since we have no preprocessor, yet. */

1755. BCLine pragmaline = cp->linenumber;

1756. if (!(cp_next(cp) == CTOK_IDENT &&

1757. cp->str->hash == H_(f5e6b4f8,1d509107)))  /* pragma */

1758. cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL);

1759. cp_pragma(cp, pragmaline);

1760. continue;

1761. }

1762. scl = cp_decl_spec(cp, &decl, CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC);

1763. if ((cp->tok == ';' || cp->tok == CTOK_EOF) &&

1764. ctype_istypedef(decl.stack[0].info)) {

1765. CTInfo info = ctype_rawchild(cp->cts, &decl.stack[0])->info;

1766. if (ctype_isstruct(info) || ctype_isenum(info))

1767. goto decl_end;  /* Accept empty declaration of struct/union/enum. */

1768. }

1769. for (;;) {

1770. CTypeID ctypeid;

1771. cp_declarator(cp, &decl);

1772. ctypeid = cp_decl_intern(cp, &decl);

1773. if (decl.name && !decl.nameid) {  /* NYI: redeclarations are ignored. */

1774. CType *ct;

1775. CTypeID id;

1776. if ((scl & CDF_TYPEDEF)) {  /* Create new typedef. */

1777. id = lj_ctype_new(cp->cts, &ct);

1778. ct->info = CTINFO(CT_TYPEDEF, ctypeid);

1779. goto noredir;

1780. } else if (ctype_isfunc(ctype_get(cp->cts, ctypeid)->info)) {

1781. /* Treat both static and extern function declarations as extern. */

1782. ct = ctype_get(cp->cts, ctypeid);

1783. /* We always get new anonymous functions (typedefs are copied). */

1784. lua_assert(gcref(ct->name) == NULL);

1785. id = ctypeid;  /* Just name it. */

1786. } else if ((scl & CDF_STATIC)) {  /* Accept static constants. */

1787. id = cp_decl_constinit(cp, &ct, ctypeid);

1788. goto noredir;

1789. } else {  /* External references have extern or no storage class. */

1790. id = lj_ctype_new(cp->cts, &ct);

1791. ct->info = CTINFO(CT_EXTERN, ctypeid);

1792. }

1793. if (decl.redir) {  /* Add attribute for redirected symbol name. */

1794. CType *cta;

1795. CTypeID aid = lj_ctype_new(cp->cts, &cta);

1796. ct = ctype_get(cp->cts, id);  /* Table may have been reallocated. */

1797. cta->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_REDIR));

1798. cta->sib = ct->sib;

1799. ct->sib = aid;

1800. ctype_setname(cta, decl.redir);

1801. }

1802. noredir:

1803. ctype_setname(ct, decl.name);

1804. lj_ctype_addname(cp->cts, ct, id);

1805. }

1806. if (!cp_opt(cp, ',')) break;

1807. cp_decl_reset(&decl);

1808. }

1809. decl_end:

1810. if (cp->tok == CTOK_EOF && first) break;  /* May omit ';' for 1 decl. */

1811. first = 0;

1812. cp_check(cp, ';');

1813. }

1814. }

1816. /* Parse a single C type declaration. */

1817. ‌static void cp_decl_single(CPState *cp)

1818. {

1819. CPDecl decl;

1820. cp_decl_spec(cp, &decl, 0);

1821. cp_declarator(cp, &decl);

1822. cp->val.id = cp_decl_intern(cp, &decl);

1823. if (cp->tok != CTOK_EOF) cp_err_token(cp, CTOK_EOF);

1824. }

1826. ‌#undef H_

1828. /* ------------------------------------------------------------------------ */

1830. /* Protected callback for C parser. */

1831. ‌static TValue *cpcparser(lua_State *L, lua_CFunction dummy, void *ud)

1832. {

1833. CPState *cp = (CPState *)ud;

1834. UNUSED(dummy);

1835. cframe_errfunc(L->cframe) = -1;  /* Inherit error function. */

1836. cp_init(cp);

1837. if ((cp->mode & CPARSE_MODE_MULTI))

1838. cp_decl_multi(cp);

1839. else

1840. cp_decl_single(cp);

1841. if (cp->param && cp->param != cp->L->top)

1842. cp_err(cp, LJ_ERR_FFI_NUMPARAM);

1843. lua_assert(cp->depth == 0);

1844. return NULL;

1845. }

1847. /* C parser. */

1848. ‌int lj_cparse(CPState *cp)

1849. {

1850. LJ_CTYPE_SAVE(cp->cts);

1851. int errcode = lj_vm_cpcall(cp->L, NULL, cp, cpcparser);

1852. if (errcode)

1853. LJ_CTYPE_RESTORE(cp->cts);

1854. cp_cleanup(cp);

1855. return errcode;

1856. }

1858. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_cparse.h - luajit-2.0-src

Data types defined

- CPChar

- CPState

- CPState

- CPToken

- CPValue

- CPValue

Macros defined

- CPARSE_MAX_BUF

- CPARSE_MAX_DECLDEPTH

- CPARSE_MAX_DECLSTACK

- CPARSE_MAX_PACKSTACK

- CPARSE_MODE_ABSTRACT

- CPARSE_MODE_DIRECT

- CPARSE_MODE_FIELD

- CPARSE_MODE_MULTI

- CPARSE_MODE_NOIMPLICIT

- CPARSE_MODE_SKIP

- _LJ_CPARSE_H

Source code

1. /*

2. ** C declaration parser.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_CPARSE_H

7. ‌#define _LJ_CPARSE_H

9. #include "lj_obj.h"

10. #include "lj_ctype.h"

12. #if LJ_HASFFI

14. /* C parser limits. */

15. ‌#define CPARSE_MAX_BUF                32768        /* Max. token buffer size. */

16. ‌#define CPARSE_MAX_DECLSTACK        100        /* Max. declaration stack depth. */

17. ‌#define CPARSE_MAX_DECLDEPTH        20        /* Max. recursive declaration depth. */

18. ‌#define CPARSE_MAX_PACKSTACK        7        /* Max. pack pragma stack depth. */

20. /* Flags for C parser mode. */

21. ‌#define CPARSE_MODE_MULTI        1        /* Process multiple declarations. */

22. ‌#define CPARSE_MODE_ABSTRACT        2        /* Accept abstract declarators. */

23. ‌#define CPARSE_MODE_DIRECT        4        /* Accept direct declarators. */

24. ‌#define CPARSE_MODE_FIELD        8        /* Accept field width in bits, too. */

25. ‌#define CPARSE_MODE_NOIMPLICIT        16        /* Reject implicit declarations. */

26. ‌#define CPARSE_MODE_SKIP        32        /* Skip definitions, ignore errors. */

28. ‌typedef int CPChar;        /* C parser character. Unsigned ext. from char. */

29. ‌typedef int CPToken;        /* C parser token. */

31. /* C parser internal value representation. */

32. ‌typedef struct CPValue {

33. union {

34. int32_t i32;        /* Value for CTID_INT32. */

35. uint32_t u32;        /* Value for CTID_UINT32. */

36. };

37. CTypeID id;                /* C Type ID of the value. */

38. ‌} CPValue;

40. /* C parser state. */

41. ‌typedef struct CPState {

42. CPChar c;                /* Current character. */

43. CPToken tok;                /* Current token. */

44. CPValue val;                /* Token value. */

45. GCstr *str;                /* Interned string of identifier/keyword. */

46. CType *ct;                /* C type table entry. */

47. const char *p;        /* Current position in input buffer. */

48. SBuf sb;                /* String buffer for tokens. */

49. lua_State *L;                /* Lua state. */

50. CTState *cts;                /* C type state. */

51. TValue *param;        /* C type parameters. */

52. const char *srcname;        /* Current source name. */

53. BCLine linenumber;        /* Input line counter. */

54. int depth;                /* Recursive declaration depth. */

55. uint32_t tmask;        /* Type mask for next identifier. */

56. uint32_t mode;        /* C parser mode. */

57. uint8_t packstack[CPARSE_MAX_PACKSTACK];  /* Stack for pack pragmas. */

58. uint8_t curpack;        /* Current position in pack pragma stack. */

59. ‌} CPState;

61. LJ_FUNC int lj_cparse(CPState *cp);

63. #endif

65. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_err.h - luajit-2.0-src

Global variables defined

- lj_err_allmsg

Data types defined

- ErrMsg

Macros defined

- ERRDEF

- _LJ_ERR_H

- err2msg

Source code

1. /*

2. ** Error handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_ERR_H

7. ‌#define _LJ_ERR_H

9. #include <stdarg.h>

11. #include "lj_obj.h"

13. typedef enum {

14. ‌#define ERRDEF(name, msg) \

15. LJ_ERR_##name, LJ_ERR_##name##_ = LJ_ERR_##name + sizeof(msg)-1,

16. #include "lj_errmsg.h"

17. LJ_ERR__MAX

18. ‌} ErrMsg;

20. ‌LJ_DATA const char *lj_err_allmsg;

21. ‌#define err2msg(em)        (lj_err_allmsg+(int)(em))

23. LJ_FUNC GCstr *lj_err_str(lua_State *L, ErrMsg em);

24. LJ_FUNCA_NORET void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode);

25. LJ_FUNC_NORET void lj_err_mem(lua_State *L);

26. LJ_FUNC_NORET void lj_err_run(lua_State *L);

27. LJ_FUNC_NORET void lj_err_msg(lua_State *L, ErrMsg em);

28. LJ_FUNC_NORET void lj_err_lex(lua_State *L, GCstr *src, const char *tok,

29. BCLine line, ErrMsg em, va_list argp);

30. LJ_FUNC_NORET void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm);

31. LJ_FUNC_NORET void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2);

32. LJ_FUNC_NORET void lj_err_optype_call(lua_State *L, TValue *o);

33. LJ_FUNC_NORET void lj_err_callermsg(lua_State *L, const char *msg);

34. LJ_FUNC_NORET void lj_err_callerv(lua_State *L, ErrMsg em, ...);

35. LJ_FUNC_NORET void lj_err_caller(lua_State *L, ErrMsg em);

36. LJ_FUNC_NORET void lj_err_arg(lua_State *L, int narg, ErrMsg em);

37. LJ_FUNC_NORET void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...);

38. LJ_FUNC_NORET void lj_err_argtype(lua_State *L, int narg, const char *xname);

39. LJ_FUNC_NORET void lj_err_argt(lua_State *L, int narg, int tt);

41. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_math.c - luajit-2.0-src

Data types defined

- U64double

Functions defined

- LJLIB_ASM(math_abs) LJLIB_REC(.)

- LJLIB_ASM(math_min) LJLIB_REC(math_minmax IR_MIN)

- LJLIB_ASM_(math_floor) LJLIB_REC(math_round IRFPM_FLOOR)

- LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10)

- LJLIB_ASM_(math_pow) LJLIB_REC(.)

- LJLIB_ASM_(math_max) LJLIB_REC(math_minmax IR_MAX)

- LJLIB_CF(math_random) LJLIB_REC(.)

- LJLIB_CF(math_randomseed)

- LJLIB_LUA(math_deg) /* function(x) return x * 57.29577951308232 end */

- lj_math_random_step

- luaopen_math

- random_init

Macros defined

- LJLIB_MODULE_math

- LUA_LIB

- TW223_GEN

- lib_math_c

Source code

1. /*

2. ** Math library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include <math.h>

8. ‌#define lib_math_c

9. ‌#define LUA_LIB

11. #include "lua.h"

12. #include "lauxlib.h"

13. #include "lualib.h"

15. #include "lj_obj.h"

16. #include "lj_lib.h"

17. #include "lj_vm.h"

19. /* ------------------------------------------------------------------------ */

21. ‌#define LJLIB_MODULE_math

23. ‌LJLIB_ASM(math_abs)                LJLIB_REC(.)

24. {

25. lj_lib_checknumber(L, 1);

26. return FFH_RETRY;

27. }

28. ‌LJLIB_ASM_(math_floor)                LJLIB_REC(math_round IRFPM_FLOOR)

29. LJLIB_ASM_(math_ceil)                LJLIB_REC(math_round IRFPM_CEIL)

31. LJLIB_ASM(math_sqrt)                LJLIB_REC(math_unary IRFPM_SQRT)

32. {

33. lj_lib_checknum(L, 1);

34. return FFH_RETRY;

35. }

36. ‌LJLIB_ASM_(math_log10)                LJLIB_REC(math_unary IRFPM_LOG10)

37. LJLIB_ASM_(math_exp)                LJLIB_REC(math_unary IRFPM_EXP)

38. LJLIB_ASM_(math_sin)                LJLIB_REC(math_unary IRFPM_SIN)

39. LJLIB_ASM_(math_cos)                LJLIB_REC(math_unary IRFPM_COS)

40. LJLIB_ASM_(math_tan)                LJLIB_REC(math_unary IRFPM_TAN)

41. LJLIB_ASM_(math_asin)                LJLIB_REC(math_atrig FF_math_asin)

42. LJLIB_ASM_(math_acos)                LJLIB_REC(math_atrig FF_math_acos)

43. LJLIB_ASM_(math_atan)                LJLIB_REC(math_atrig FF_math_atan)

44. LJLIB_ASM_(math_sinh)                LJLIB_REC(math_htrig IRCALL_sinh)

45. LJLIB_ASM_(math_cosh)                LJLIB_REC(math_htrig IRCALL_cosh)

46. LJLIB_ASM_(math_tanh)                LJLIB_REC(math_htrig IRCALL_tanh)

47. LJLIB_ASM_(math_frexp)

48. LJLIB_ASM_(math_modf)                LJLIB_REC(.)

50. LJLIB_ASM(math_log)                LJLIB_REC(math_log)

51. {

52. double x = lj_lib_checknum(L, 1);

53. if (L->base+1 < L->top) {

54. double y = lj_lib_checknum(L, 2);

55. #ifdef LUAJIT_NO_LOG2

56. x = log(x); y = 1.0 / log(y);

57. #else

58. x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y);

59. #endif

60. setnumV(L->base-1-LJ_FR2, x*y);  /* Do NOT join the expression to x / y. */

61. return FFH_RES(1);

62. }

63. return FFH_RETRY;

64. }

66. ‌LJLIB_LUA(math_deg) /* function(x) return x * 57.29577951308232 end */

67. LJLIB_LUA(math_rad) /* function(x) return x * 0.017453292519943295 end */

69. LJLIB_ASM(math_atan2)                LJLIB_REC(.)

70. {

71. lj_lib_checknum(L, 1);

72. lj_lib_checknum(L, 2);

73. return FFH_RETRY;

74. }

75. ‌LJLIB_ASM_(math_pow)                LJLIB_REC(.)

76. LJLIB_ASM_(math_fmod)

78. LJLIB_ASM(math_ldexp)                LJLIB_REC(.)

79. {

80. lj_lib_checknum(L, 1);

81. #if LJ_DUALNUM && !LJ_TARGET_X86ORX64

82. lj_lib_checkint(L, 2);

83. #else

84. lj_lib_checknum(L, 2);

85. #endif

86. return FFH_RETRY;

87. }

89. ‌LJLIB_ASM(math_min)                LJLIB_REC(math_minmax IR_MIN)

90. {

91. int i = 0;

92. do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top);

93. return FFH_RETRY;

94. }

95. ‌LJLIB_ASM_(math_max)                LJLIB_REC(math_minmax IR_MAX)

97. LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi)

98. LJLIB_PUSH(1e310) LJLIB_SET(huge)

100. /* ------------------------------------------------------------------------ */

102. /* This implements a Tausworthe PRNG with period 2^223. Based on:

103. **   Tables of maximally-equidistributed combined LFSR generators,

104. **   Pierre L'Ecuyer, 1991, table 3, 1st entry.

105. ** Full-period ME-CF generator with L=64, J=4, k=223, N1=49.

106. */

108. /* PRNG state. */

109. struct RandomState {

110. uint64_t gen[4];        /* State of the 4 LFSR generators. */

111. int valid;                /* State is valid. */

112. };

114. /* Union needed for bit-pattern conversion between uint64_t and double. */

115. ‌typedef union { uint64_t u64; double d; } U64double;

117. /* Update generator i and compute a running xor of all states. */

118. ‌#define TW223_GEN(i, k, q, s) \

119. z = rs->gen[i]; \

120. z = (((z<<q)^z) >> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<<s); \

121. r ^= z; rs->gen[i] = z;

123. /* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */

124. ‌LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs)

125. {

126. uint64_t z, r = 0;

127. TW223_GEN(0, 63, 31, 18)

128. TW223_GEN(1, 58, 19, 28)

129. TW223_GEN(2, 55, 24,  7)

130. TW223_GEN(3, 47, 21,  8)

131. return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000);

132. }

134. /* PRNG initialization function. */

135. ‌static void random_init(RandomState *rs, double d)

136. {

137. uint32_t r = 0x11090601;  /* 64-k[i] as four 8 bit constants. */

138. int i;

139. for (i = 0; i < 4; i++) {

140. U64double u;

141. uint32_t m = 1u << (r&255);

142. r >>= 8;

143. u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354;

144. if (u.u64 < m) u.u64 += m;  /* Ensure k[i] MSB of gen[i] are non-zero. */

145. rs->gen[i] = u.u64;

146. }

147. rs->valid = 1;

148. for (i = 0; i < 10; i++)

149. lj_math_random_step(rs);

150. }

152. /* PRNG extract function. */

153. LJLIB_PUSH(top-2)  /* Upvalue holds userdata with RandomState. */

154. ‌LJLIB_CF(math_random)                LJLIB_REC(.)

155. {

156. int n = (int)(L->top - L->base);

157. RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));

158. U64double u;

159. double d;

160. if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0);

161. u.u64 = lj_math_random_step(rs);

162. d = u.d - 1.0;

163. if (n > 0) {

164. #if LJ_DUALNUM

165. int isint = 1;

166. double r1;

167. lj_lib_checknumber(L, 1);

168. if (tvisint(L->base)) {

169. r1 = (lua_Number)intV(L->base);

170. } else {

171. isint = 0;

172. r1 = numV(L->base);

173. }

174. #else

175. double r1 = lj_lib_checknum(L, 1);

176. #endif

177. if (n == 1) {

178. d = lj_vm_floor(d*r1) + 1.0;  /* d is an int in range [1, r1] */

179. } else {

180. #if LJ_DUALNUM

181. double r2;

182. lj_lib_checknumber(L, 2);

183. if (tvisint(L->base+1)) {

184. r2 = (lua_Number)intV(L->base+1);

185. } else {

186. isint = 0;

187. r2 = numV(L->base+1);

188. }

189. #else

190. double r2 = lj_lib_checknum(L, 2);

191. #endif

192. d = lj_vm_floor(d*(r2-r1+1.0)) + r1;  /* d is an int in range [r1, r2] */

193. }

194. #if LJ_DUALNUM

195. if (isint) {

196. setintV(L->top-1, lj_num2int(d));

197. return 1;

198. }

199. #endif

200. }  /* else: d is a double in range [0, 1] */

201. setnumV(L->top++, d);

202. return 1;

203. }

205. /* PRNG seed function. */

206. LJLIB_PUSH(top-2)  /* Upvalue holds userdata with RandomState. */

207. ‌LJLIB_CF(math_randomseed)

208. {

209. RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));

210. random_init(rs, lj_lib_checknum(L, 1));

211. return 0;

212. }

214. /* ------------------------------------------------------------------------ */

216. #include "lj_libdef.h"

218. ‌LUALIB_API int luaopen_math(lua_State *L)

219. {

220. RandomState *rs;

221. rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState));

222. rs->valid = 0;  /* Use lazy initialization to save some time on startup. */

223. LJ_LIB_REG(L, LUA_MATHLIBNAME, math);

224. #if defined(LUA_COMPAT_MOD) && !LJ_52

225. lua_getfield(L, -1, "fmod");

226. lua_setfield(L, -2, "mod");

227. #endif

228. return 1;

229. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_vmmath.c - luajit-2.0-src

Functions defined

- lj_vm_errno

- lj_vm_exp2

- lj_vm_foldarith

- lj_vm_foldfpm

- lj_vm_log2

- lj_vm_modi

- lj_vm_powi

- lj_vm_powui

- lj_wrap_acos

- lj_wrap_asin

- lj_wrap_atan

- lj_wrap_atan2

- lj_wrap_cos

- lj_wrap_cosh

- lj_wrap_exp

- lj_wrap_fmod

- lj_wrap_log

- lj_wrap_log10

- lj_wrap_pow

- lj_wrap_sin

- lj_wrap_sinh

- lj_wrap_tan

- lj_wrap_tanh

Macros defined

- LUA_CORE

- lj_vmmath_c

Source code

1. /*

2. ** Math helper functions for assembler VM.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_vmmath_c

7. ‌#define LUA_CORE

9. #include <errno.h>

10. #include <math.h>

12. #include "lj_obj.h"

13. #include "lj_ir.h"

14. #include "lj_vm.h"

16. /* -- Wrapper functions --------------------------------------------------- */

18. #if LJ_TARGET_X86 && __ELF__ && __PIC__

19. /* Wrapper functions to deal with the ELF/x86 PIC disaster. */

20. ‌LJ_FUNCA double lj_wrap_log(double x) { return log(x); }

21. ‌LJ_FUNCA double lj_wrap_log10(double x) { return log10(x); }

22. ‌LJ_FUNCA double lj_wrap_exp(double x) { return exp(x); }

23. ‌LJ_FUNCA double lj_wrap_sin(double x) { return sin(x); }

24. ‌LJ_FUNCA double lj_wrap_cos(double x) { return cos(x); }

25. ‌LJ_FUNCA double lj_wrap_tan(double x) { return tan(x); }

26. ‌LJ_FUNCA double lj_wrap_asin(double x) { return asin(x); }

27. ‌LJ_FUNCA double lj_wrap_acos(double x) { return acos(x); }

28. ‌LJ_FUNCA double lj_wrap_atan(double x) { return atan(x); }

29. ‌LJ_FUNCA double lj_wrap_sinh(double x) { return sinh(x); }

30. ‌LJ_FUNCA double lj_wrap_cosh(double x) { return cosh(x); }

31. ‌LJ_FUNCA double lj_wrap_tanh(double x) { return tanh(x); }

32. ‌LJ_FUNCA double lj_wrap_atan2(double x, double y) { return atan2(x, y); }

33. ‌LJ_FUNCA double lj_wrap_pow(double x, double y) { return pow(x, y); }

34. ‌LJ_FUNCA double lj_wrap_fmod(double x, double y) { return fmod(x, y); }

35. #endif

37. /* -- Helper functions for generated machine code ------------------------- */

39. ‌double lj_vm_foldarith(double x, double y, int op)

40. {

41. switch (op) {

42. case IR_ADD - IR_ADD: return x+y; break;

43. case IR_SUB - IR_ADD: return x-y; break;

44. case IR_MUL - IR_ADD: return x*y; break;

45. case IR_DIV - IR_ADD: return x/y; break;

46. case IR_MOD - IR_ADD: return x-lj_vm_floor(x/y)*y; break;

47. case IR_POW - IR_ADD: return pow(x, y); break;

48. case IR_NEG - IR_ADD: return -x; break;

49. case IR_ABS - IR_ADD: return fabs(x); break;

50. #if LJ_HASJIT

51. case IR_ATAN2 - IR_ADD: return atan2(x, y); break;

52. case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break;

53. case IR_MIN - IR_ADD: return x > y ? y : x; break;

54. case IR_MAX - IR_ADD: return x < y ? y : x; break;

55. #endif

56. default: return x;

57. }

58. }

60. #if LJ_HASJIT

62. #ifdef LUAJIT_NO_LOG2

63. ‌double lj_vm_log2(double a)

64. {

65. return log(a) * 1.4426950408889634074;

66. }

67. #endif

69. #ifdef LUAJIT_NO_EXP2

70. ‌double lj_vm_exp2(double a)

71. {

72. return exp(a * 0.6931471805599453);

73. }

74. #endif

76. #if !(LJ_TARGET_ARM || LJ_TARGET_ARM64 || LJ_TARGET_PPC)

77. ‌int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b)

78. {

79. uint32_t y, ua, ub;

80. lua_assert(b != 0);  /* This must be checked before using this function. */

81. ua = a < 0 ? (uint32_t)-a : (uint32_t)a;

82. ub = b < 0 ? (uint32_t)-b : (uint32_t)b;

83. y = ua % ub;

84. if (y != 0 && (a^b) < 0) y = y - ub;

85. if (((int32_t)y^b) < 0) y = (uint32_t)-(int32_t)y;

86. return (int32_t)y;

87. }

88. #endif

90. #if !LJ_TARGET_X86ORX64

91. /* Unsigned x^k. */

92. ‌static double lj_vm_powui(double x, uint32_t k)

93. {

94. double y;

95. lua_assert(k != 0);

96. for (; (k & 1) == 0; k >>= 1) x *= x;

97. y = x;

98. if ((k >>= 1) != 0) {

99. for (;;) {

100. x *= x;

101. if (k == 1) break;

102. if (k & 1) y *= x;

103. k >>= 1;

104. }

105. y *= x;

106. }

107. return y;

108. }

110. /* Signed x^k. */

111. ‌double lj_vm_powi(double x, int32_t k)

112. {

113. if (k > 1)

114. return lj_vm_powui(x, (uint32_t)k);

115. else if (k == 1)

116. return x;

117. else if (k == 0)

118. return 1.0;

119. else

120. return 1.0 / lj_vm_powui(x, (uint32_t)-k);

121. }

122. #endif

124. /* Computes fpm(x) for extended math functions. */

125. ‌double lj_vm_foldfpm(double x, int fpm)

126. {

127. switch (fpm) {

128. case IRFPM_FLOOR: return lj_vm_floor(x);

129. case IRFPM_CEIL: return lj_vm_ceil(x);

130. case IRFPM_TRUNC: return lj_vm_trunc(x);

131. case IRFPM_SQRT: return sqrt(x);

132. case IRFPM_EXP: return exp(x);

133. case IRFPM_EXP2: return lj_vm_exp2(x);

134. case IRFPM_LOG: return log(x);

135. case IRFPM_LOG2: return lj_vm_log2(x);

136. case IRFPM_LOG10: return log10(x);

137. case IRFPM_SIN: return sin(x);

138. case IRFPM_COS: return cos(x);

139. case IRFPM_TAN: return tan(x);

140. default: lua_assert(0);

141. }

142. return 0;

143. }

145. #if LJ_HASFFI

146. ‌int lj_vm_errno(void)

147. {

148. return errno;

149. }

150. #endif

152. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_vm.h - luajit-2.0-src

Global variables defined

- lj_vm_asm_begin

Data types defined

- lua_CPFunction

Macros defined

- _LJ_VM_H

- lj_vm_ceil

- lj_vm_exp2

- lj_vm_floor

- lj_vm_log2

- lj_vm_powi

- lj_vm_trunc

- makeasmfunc

Source code

1. /*

2. ** Assembler VM interface definitions.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_VM_H

7. ‌#define _LJ_VM_H

9. #include "lj_obj.h"

11. /* Entry points for ASM parts of VM. */

12. LJ_ASMF void lj_vm_call(lua_State *L, TValue *base, int nres1);

13. LJ_ASMF int lj_vm_pcall(lua_State *L, TValue *base, int nres1, ptrdiff_t ef);

14. ‌typedef TValue *(*lua_CPFunction)(lua_State *L, lua_CFunction func, void *ud);

15. LJ_ASMF int lj_vm_cpcall(lua_State *L, lua_CFunction func, void *ud,

16. lua_CPFunction cp);

17. LJ_ASMF int lj_vm_resume(lua_State *L, TValue *base, int nres1, ptrdiff_t ef);

18. LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_c(void *cframe, int errcode);

19. LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_ff(void *cframe);

20. LJ_ASMF void lj_vm_unwind_c_eh(void);

21. LJ_ASMF void lj_vm_unwind_ff_eh(void);

22. #if LJ_TARGET_X86ORX64

23. LJ_ASMF void lj_vm_unwind_rethrow(void);

24. #endif

26. /* Miscellaneous functions. */

27. #if LJ_TARGET_X86ORX64

28. LJ_ASMF int lj_vm_cpuid(uint32_t f, uint32_t res[4]);

29. #endif

30. #if LJ_TARGET_PPC

31. void lj_vm_cachesync(void *start, void *end);

32. #endif

33. LJ_ASMF double lj_vm_foldarith(double x, double y, int op);

34. #if LJ_HASJIT

35. LJ_ASMF double lj_vm_foldfpm(double x, int op);

36. #endif

37. #if !LJ_ARCH_HASFPU

38. /* Declared in lj_obj.h: LJ_ASMF int32_t lj_vm_tobit(double x); */

39. #endif

41. /* Dispatch targets for recording and hooks. */

42. LJ_ASMF void lj_vm_record(void);

43. LJ_ASMF void lj_vm_inshook(void);

44. LJ_ASMF void lj_vm_rethook(void);

45. LJ_ASMF void lj_vm_callhook(void);

46. LJ_ASMF void lj_vm_profhook(void);

48. /* Trace exit handling. */

49. LJ_ASMF void lj_vm_exit_handler(void);

50. LJ_ASMF void lj_vm_exit_interp(void);

52. /* Internal math helper functions. */

53. #if LJ_TARGET_PPC || LJ_TARGET_ARM64

54. ‌#define lj_vm_floor        floor

55. ‌#define lj_vm_ceil        ceil

56. #else

57. LJ_ASMF double lj_vm_floor(double);

58. LJ_ASMF double lj_vm_ceil(double);

59. #if LJ_TARGET_ARM

60. LJ_ASMF double lj_vm_floor_sf(double);

61. LJ_ASMF double lj_vm_ceil_sf(double);

62. #endif

63. #endif

64. #ifdef LUAJIT_NO_LOG2

65. LJ_ASMF double lj_vm_log2(double);

66. #else

67. ‌#define lj_vm_log2        log2

68. #endif

70. #if LJ_HASJIT

71. #if LJ_TARGET_X86ORX64

72. LJ_ASMF void lj_vm_floor_sse(void);

73. LJ_ASMF void lj_vm_ceil_sse(void);

74. LJ_ASMF void lj_vm_trunc_sse(void);

75. LJ_ASMF void lj_vm_powi_sse(void);

76. ‌#define lj_vm_powi        NULL

77. #else

78. LJ_ASMF double lj_vm_powi(double, int32_t);

79. #endif

80. #if LJ_TARGET_PPC || LJ_TARGET_ARM64

81. ‌#define lj_vm_trunc        trunc

82. #else

83. LJ_ASMF double lj_vm_trunc(double);

84. #if LJ_TARGET_ARM

85. LJ_ASMF double lj_vm_trunc_sf(double);

86. #endif

87. #endif

88. #ifdef LUAJIT_NO_EXP2

89. LJ_ASMF double lj_vm_exp2(double);

90. #else

91. ‌#define lj_vm_exp2        exp2

92. #endif

93. LJ_ASMF int32_t LJ_FASTCALL lj_vm_modi(int32_t, int32_t);

94. #if LJ_HASFFI

95. LJ_ASMF int lj_vm_errno(void);

96. #endif

97. #endif

99. /* Continuations for metamethods. */

100. LJ_ASMF void lj_cont_cat(void);  /* Continue with concatenation. */

101. LJ_ASMF void lj_cont_ra(void);  /* Store result in RA from instruction. */

102. LJ_ASMF void lj_cont_nop(void);  /* Do nothing, just continue execution. */

103. LJ_ASMF void lj_cont_condt(void);  /* Branch if result is true. */

104. LJ_ASMF void lj_cont_condf(void);  /* Branch if result is false. */

105. LJ_ASMF void lj_cont_hook(void);  /* Continue from hook yield. */

106. LJ_ASMF void lj_cont_stitch(void);  /* Trace stitching. */

108. /* Start of the ASM code. */

109. ‌LJ_ASMF char lj_vm_asm_begin[];

111. /* Bytecode offsets are relative to lj_vm_asm_begin. */

112. ‌#define makeasmfunc(ofs)        ((ASMFunction)(lj_vm_asm_begin + (ofs)))

114. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_io.c - luajit-2.0-src

Data types defined

- IOFileUD

- IOFileUD

Functions defined

- LJLIB_CF(io_method_close)

- LJLIB_CF(io_method_read)

- LJLIB_CF(io_method_write) LJLIB_REC(io_write 0)

- LJLIB_CF(io_method_flush) LJLIB_REC(io_flush 0)

- LJLIB_CF(io_method_seek)

- LJLIB_CF(io_method_setvbuf)

- LJLIB_CF(io_method_lines)

- LJLIB_CF(io_method___gc)

- LJLIB_CF(io_method___tostring)

- LJLIB_CF(io_popen)

- LJLIB_CF(io_tmpfile)

- LJLIB_CF(io_close)

- LJLIB_CF(io_read)

- LJLIB_CF(io_write) LJLIB_REC(io_write GCROOT_IO_OUTPUT)

- LJLIB_CF(io_flush) LJLIB_REC(io_flush GCROOT_IO_OUTPUT)

- LJLIB_CF(io_input)

- LJLIB_CF(io_output)

- LJLIB_CF(io_lines)

- LJLIB_CF(io_type)

- LJLIB_PUSH(top-1) LJLIB_SET(__index)

- io_file_close

- io_file_iter

- io_file_new

- io_file_open

- io_file_read

- io_file_readall

- io_file_readlen

- io_file_readline

- io_file_readnum

- io_file_write

- io_std_getset

- io_std_new

- io_stdfile

- io_tofile

- io_tofilep

- luaopen_io

Macros defined

- IOFILE_FLAG_CLOSE

- IOFILE_TYPE_FILE

- IOFILE_TYPE_MASK

- IOFILE_TYPE_PIPE

- IOFILE_TYPE_STDF

- IOSTDF_IOF

- IOSTDF_UD

- LJLIB_MODULE_io

- LJLIB_MODULE_io_method

- LUA_LIB

- lib_io_c

Source code

1. /*

2. ** I/O library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. #include <errno.h>

10. #include <stdio.h>

12. ‌#define lib_io_c

13. ‌#define LUA_LIB

15. #include "lua.h"

16. #include "lauxlib.h"

17. #include "lualib.h"

19. #include "lj_obj.h"

20. #include "lj_gc.h"

21. #include "lj_err.h"

22. #include "lj_buf.h"

23. #include "lj_str.h"

24. #include "lj_state.h"

25. #include "lj_strfmt.h"

26. #include "lj_ff.h"

27. #include "lj_lib.h"

29. /* Userdata payload for I/O file. */

30. ‌typedef struct IOFileUD {

31. FILE *fp;                /* File handle. */

32. uint32_t type;        /* File type. */

33. ‌} IOFileUD;

35. ‌#define IOFILE_TYPE_FILE        0        /* Regular file. */

36. ‌#define IOFILE_TYPE_PIPE        1        /* Pipe. */

37. ‌#define IOFILE_TYPE_STDF        2        /* Standard file handle. */

38. ‌#define IOFILE_TYPE_MASK        3

40. ‌#define IOFILE_FLAG_CLOSE        4        /* Close after io.lines() iterator. */

42. ‌#define IOSTDF_UD(L, id)        (&gcref(G(L)->gcroot[(id)])->ud)

43. ‌#define IOSTDF_IOF(L, id)        ((IOFileUD *)uddata(IOSTDF_UD(L, (id))))

45. /* -- Open/close helpers -------------------------------------------------- */

47. ‌static IOFileUD *io_tofilep(lua_State *L)

48. {

49. if (!(L->base < L->top && tvisudata(L->base) &&

50. udataV(L->base)->udtype == UDTYPE_IO_FILE))

51. lj_err_argtype(L, 1, "FILE*");

52. return (IOFileUD *)uddata(udataV(L->base));

53. }

55. ‌static IOFileUD *io_tofile(lua_State *L)

56. {

57. IOFileUD *iof = io_tofilep(L);

58. if (iof->fp == NULL)

59. lj_err_caller(L, LJ_ERR_IOCLFL);

60. return iof;

61. }

63. ‌static FILE *io_stdfile(lua_State *L, ptrdiff_t id)

64. {

65. IOFileUD *iof = IOSTDF_IOF(L, id);

66. if (iof->fp == NULL)

67. lj_err_caller(L, LJ_ERR_IOSTDCL);

68. return iof->fp;

69. }

71. ‌static IOFileUD *io_file_new(lua_State *L)

72. {

73. IOFileUD *iof = (IOFileUD *)lua_newuserdata(L, sizeof(IOFileUD));

74. GCudata *ud = udataV(L->top-1);

75. ud->udtype = UDTYPE_IO_FILE;

76. /* NOBARRIER: The GCudata is new (marked white). */

77. setgcrefr(ud->metatable, curr_func(L)->c.env);

78. iof->fp = NULL;

79. iof->type = IOFILE_TYPE_FILE;

80. return iof;

81. }

83. ‌static IOFileUD *io_file_open(lua_State *L, const char *mode)

84. {

85. const char *fname = strdata(lj_lib_checkstr(L, 1));

86. IOFileUD *iof = io_file_new(L);

87. iof->fp = fopen(fname, mode);

88. if (iof->fp == NULL)

89. luaL_argerror(L, 1, lj_strfmt_pushf(L, "%s: %s", fname, strerror(errno)));

90. return iof;

91. }

93. ‌static int io_file_close(lua_State *L, IOFileUD *iof)

94. {

95. int ok;

96. if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_FILE) {

97. ok = (fclose(iof->fp) == 0);

98. } else if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_PIPE) {

99. int stat = -1;

100. #if LJ_TARGET_POSIX

101. stat = pclose(iof->fp);

102. #elif LJ_TARGET_WINDOWS

103. stat = _pclose(iof->fp);

104. #else

105. lua_assert(0);

106. return 0;

107. #endif

108. #if LJ_52

109. iof->fp = NULL;

110. return luaL_execresult(L, stat);

111. #else

112. ok = (stat != -1);

113. #endif

114. } else {

115. lua_assert((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_STDF);

116. setnilV(L->top++);

117. lua_pushliteral(L, "cannot close standard file");

118. return 2;

119. }

120. iof->fp = NULL;

121. return luaL_fileresult(L, ok, NULL);

122. }

124. /* -- Read/write helpers -------------------------------------------------- */

126. ‌static int io_file_readnum(lua_State *L, FILE *fp)

127. {

128. lua_Number d;

129. if (fscanf(fp, LUA_NUMBER_SCAN, &d) == 1) {

130. if (LJ_DUALNUM) {

131. int32_t i = lj_num2int(d);

132. if (d == (lua_Number)i && !tvismzero((cTValue *)&d)) {

133. setintV(L->top++, i);

134. return 1;

135. }

136. }

137. setnumV(L->top++, d);

138. return 1;

139. } else {

140. setnilV(L->top++);

141. return 0;

142. }

143. }

145. ‌static int io_file_readline(lua_State *L, FILE *fp, MSize chop)

146. {

147. MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0;

148. char *buf;

149. for (;;) {

150. buf = lj_buf_tmp(L, m);

151. if (fgets(buf+n, m-n, fp) == NULL) break;

152. n += (MSize)strlen(buf+n);

153. ok |= n;

154. if (n && buf[n-1] == '\n') { n -= chop; break; }

155. if (n >= m - 64) m += m;

156. }

157. setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n));

158. lj_gc_check(L);

159. return (int)ok;

160. }

162. ‌static void io_file_readall(lua_State *L, FILE *fp)

163. {

164. MSize m, n;

165. for (m = LUAL_BUFFERSIZE, n = 0; ; m += m) {

166. char *buf = lj_buf_tmp(L, m);

167. n += (MSize)fread(buf+n, 1, m-n, fp);

168. if (n != m) {

169. setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n));

170. lj_gc_check(L);

171. return;

172. }

173. }

174. }

176. ‌static int io_file_readlen(lua_State *L, FILE *fp, MSize m)

177. {

178. if (m) {

179. char *buf = lj_buf_tmp(L, m);

180. MSize n = (MSize)fread(buf, 1, m, fp);

181. setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n));

182. lj_gc_check(L);

183. return (n > 0 || m == 0);

184. } else {

185. int c = getc(fp);

186. ungetc(c, fp);

187. setstrV(L, L->top++, &G(L)->strempty);

188. return (c != EOF);

189. }

190. }

192. ‌static int io_file_read(lua_State *L, FILE *fp, int start)

193. {

194. int ok, n, nargs = (int)(L->top - L->base) - start;

195. clearerr(fp);

196. if (nargs == 0) {

197. ok = io_file_readline(L, fp, 1);

198. n = start+1;  /* Return 1 result. */

199. } else {

200. /* The results plus the buffers go on top of the args. */

201. luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");

202. ok = 1;

203. for (n = start; nargs-- && ok; n++) {

204. if (tvisstr(L->base+n)) {

205. const char *p = strVdata(L->base+n);

206. if (p[0] != '*')

207. lj_err_arg(L, n+1, LJ_ERR_INVOPT);

208. if (p[1] == 'n')

209. ok = io_file_readnum(L, fp);

210. else if ((p[1] & ~0x20) == 'L')

211. ok = io_file_readline(L, fp, (p[1] == 'l'));

212. else if (p[1] == 'a')

213. io_file_readall(L, fp);

214. else

215. lj_err_arg(L, n+1, LJ_ERR_INVFMT);

216. } else if (tvisnumber(L->base+n)) {

217. ok = io_file_readlen(L, fp, (MSize)lj_lib_checkint(L, n+1));

218. } else {

219. lj_err_arg(L, n+1, LJ_ERR_INVOPT);

220. }

221. }

222. }

223. if (ferror(fp))

224. return luaL_fileresult(L, 0, NULL);

225. if (!ok)

226. setnilV(L->top-1);  /* Replace last result with nil. */

227. return n - start;

228. }

230. ‌static int io_file_write(lua_State *L, FILE *fp, int start)

231. {

232. cTValue *tv;

233. int status = 1;

234. for (tv = L->base+start; tv < L->top; tv++) {

235. char buf[STRFMT_MAXBUF_NUM];

236. MSize len;

237. const char *p = lj_strfmt_wstrnum(buf, tv, &len);

238. if (!p)

239. lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING);

240. status = status && (fwrite(p, 1, len, fp) == len);

241. }

242. if (LJ_52 && status) {

243. L->top = L->base+1;

244. if (start == 0)

245. setudataV(L, L->base, IOSTDF_UD(L, GCROOT_IO_OUTPUT));

246. return 1;

247. }

248. return luaL_fileresult(L, status, NULL);

249. }

251. ‌static int io_file_iter(lua_State *L)

252. {

253. GCfunc *fn = curr_func(L);

254. IOFileUD *iof = uddata(udataV(&fn->c.upvalue[0]));

255. int n = fn->c.nupvalues - 1;

256. if (iof->fp == NULL)

257. lj_err_caller(L, LJ_ERR_IOCLFL);

258. L->top = L->base;

259. if (n) {  /* Copy upvalues with options to stack. */

260. if (n > LUAI_MAXCSTACK)

261. lj_err_caller(L, LJ_ERR_STKOV);

262. lj_state_checkstack(L, (MSize)n);

263. memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue));

264. L->top += n;

265. }

266. n = io_file_read(L, iof->fp, 0);

267. if (ferror(iof->fp))

268. lj_err_callermsg(L, strVdata(L->top-2));

269. if (tvisnil(L->base) && (iof->type & IOFILE_FLAG_CLOSE)) {

270. io_file_close(L, iof);  /* Return values are ignored. */

271. return 0;

272. }

273. return n;

274. }

276. /* -- I/O file methods ---------------------------------------------------- */

278. ‌#define LJLIB_MODULE_io_method

280. ‌LJLIB_CF(io_method_close)

281. {

282. IOFileUD *iof = L->base < L->top ? io_tofile(L) :

283. IOSTDF_IOF(L, GCROOT_IO_OUTPUT);

284. return io_file_close(L, iof);

285. }

287. ‌LJLIB_CF(io_method_read)

288. {

289. return io_file_read(L, io_tofile(L)->fp, 1);

290. }

292. ‌LJLIB_CF(io_method_write)                LJLIB_REC(io_write 0)

293. {

294. return io_file_write(L, io_tofile(L)->fp, 1);

295. }

297. ‌LJLIB_CF(io_method_flush)                LJLIB_REC(io_flush 0)

298. {

299. return luaL_fileresult(L, fflush(io_tofile(L)->fp) == 0, NULL);

300. }

302. ‌LJLIB_CF(io_method_seek)

303. {

304. FILE *fp = io_tofile(L)->fp;

305. int opt = lj_lib_checkopt(L, 2, 1, "\3set\3cur\3end");

306. int64_t ofs = 0;

307. cTValue *o;

308. int res;

309. if (opt == 0) opt = SEEK_SET;

310. else if (opt == 1) opt = SEEK_CUR;

311. else if (opt == 2) opt = SEEK_END;

312. o = L->base+2;

313. if (o < L->top) {

314. if (tvisint(o))

315. ofs = (int64_t)intV(o);

316. else if (tvisnum(o))

317. ofs = (int64_t)numV(o);

318. else if (!tvisnil(o))

319. lj_err_argt(L, 3, LUA_TNUMBER);

320. }

321. #if LJ_TARGET_POSIX

322. res = fseeko(fp, ofs, opt);

323. #elif _MSC_VER >= 1400

324. res = _fseeki64(fp, ofs, opt);

325. #elif defined(__MINGW32__)

326. res = fseeko64(fp, ofs, opt);

327. #else

328. res = fseek(fp, (long)ofs, opt);

329. #endif

330. if (res)

331. return luaL_fileresult(L, 0, NULL);

332. #if LJ_TARGET_POSIX

333. ofs = ftello(fp);

334. #elif _MSC_VER >= 1400

335. ofs = _ftelli64(fp);

336. #elif defined(__MINGW32__)

337. ofs = ftello64(fp);

338. #else

339. ofs = (int64_t)ftell(fp);

340. #endif

341. setint64V(L->top-1, ofs);

342. return 1;

343. }

345. ‌LJLIB_CF(io_method_setvbuf)

346. {

347. FILE *fp = io_tofile(L)->fp;

348. int opt = lj_lib_checkopt(L, 2, -1, "\4full\4line\2no");

349. size_t sz = (size_t)lj_lib_optint(L, 3, LUAL_BUFFERSIZE);

350. if (opt == 0) opt = _IOFBF;

351. else if (opt == 1) opt = _IOLBF;

352. else if (opt == 2) opt = _IONBF;

353. return luaL_fileresult(L, setvbuf(fp, NULL, opt, sz) == 0, NULL);

354. }

356. ‌LJLIB_CF(io_method_lines)

357. {

358. io_tofile(L);

359. lua_pushcclosure(L, io_file_iter, (int)(L->top - L->base));

360. return 1;

361. }

363. ‌LJLIB_CF(io_method___gc)

364. {

365. IOFileUD *iof = io_tofilep(L);

366. if (iof->fp != NULL && (iof->type & IOFILE_TYPE_MASK) != IOFILE_TYPE_STDF)

367. io_file_close(L, iof);

368. return 0;

369. }

371. ‌LJLIB_CF(io_method___tostring)

372. {

373. IOFileUD *iof = io_tofilep(L);

374. if (iof->fp != NULL)

375. lua_pushfstring(L, "file (%p)", iof->fp);

376. else

377. lua_pushliteral(L, "file (closed)");

378. return 1;

379. }

381. ‌LJLIB_PUSH(top-1) LJLIB_SET(__index)

383. #include "lj_libdef.h"

385. /* -- I/O library functions ----------------------------------------------- */

387. ‌#define LJLIB_MODULE_io

389. LJLIB_PUSH(top-2) LJLIB_SET(!)  /* Set environment. */

391. LJLIB_CF(io_open)

392. {

393. const char *fname = strdata(lj_lib_checkstr(L, 1));

394. GCstr *s = lj_lib_optstr(L, 2);

395. const char *mode = s ? strdata(s) : "r";

396. IOFileUD *iof = io_file_new(L);

397. iof->fp = fopen(fname, mode);

398. return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname);

399. }

401. ‌LJLIB_CF(io_popen)

402. {

403. #if LJ_TARGET_POSIX || LJ_TARGET_WINDOWS

404. const char *fname = strdata(lj_lib_checkstr(L, 1));

405. GCstr *s = lj_lib_optstr(L, 2);

406. const char *mode = s ? strdata(s) : "r";

407. IOFileUD *iof = io_file_new(L);

408. iof->type = IOFILE_TYPE_PIPE;

409. #if LJ_TARGET_POSIX

410. fflush(NULL);

411. iof->fp = popen(fname, mode);

412. #else

413. iof->fp = _popen(fname, mode);

414. #endif

415. return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname);

416. #else

417. return luaL_error(L, LUA_QL("popen") " not supported");

418. #endif

419. }

421. ‌LJLIB_CF(io_tmpfile)

422. {

423. IOFileUD *iof = io_file_new(L);

424. #if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PSVITA

425. iof->fp = NULL; errno = ENOSYS;

426. #else

427. iof->fp = tmpfile();

428. #endif

429. return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, NULL);

430. }

432. ‌LJLIB_CF(io_close)

433. {

434. return lj_cf_io_method_close(L);

435. }

437. ‌LJLIB_CF(io_read)

438. {

439. return io_file_read(L, io_stdfile(L, GCROOT_IO_INPUT), 0);

440. }

442. ‌LJLIB_CF(io_write)                LJLIB_REC(io_write GCROOT_IO_OUTPUT)

443. {

444. return io_file_write(L, io_stdfile(L, GCROOT_IO_OUTPUT), 0);

445. }

447. ‌LJLIB_CF(io_flush)                LJLIB_REC(io_flush GCROOT_IO_OUTPUT)

448. {

449. return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)) == 0, NULL);

450. }

452. ‌static int io_std_getset(lua_State *L, ptrdiff_t id, const char *mode)

453. {

454. if (L->base < L->top && !tvisnil(L->base)) {

455. if (tvisudata(L->base)) {

456. io_tofile(L);

457. L->top = L->base+1;

458. } else {

459. io_file_open(L, mode);

460. }

461. /* NOBARRIER: The standard I/O handles are GC roots. */

462. setgcref(G(L)->gcroot[id], gcV(L->top-1));

463. } else {

464. setudataV(L, L->top++, IOSTDF_UD(L, id));

465. }

466. return 1;

467. }

469. ‌LJLIB_CF(io_input)

470. {

471. return io_std_getset(L, GCROOT_IO_INPUT, "r");

472. }

474. ‌LJLIB_CF(io_output)

475. {

476. return io_std_getset(L, GCROOT_IO_OUTPUT, "w");

477. }

479. ‌LJLIB_CF(io_lines)

480. {

481. if (L->base == L->top) setnilV(L->top++);

482. if (!tvisnil(L->base)) {  /* io.lines(fname) */

483. IOFileUD *iof = io_file_open(L, "r");

484. iof->type = IOFILE_TYPE_FILE|IOFILE_FLAG_CLOSE;

485. L->top--;

486. setudataV(L, L->base, udataV(L->top));

487. } else {  /* io.lines() iterates over stdin. */

488. setudataV(L, L->base, IOSTDF_UD(L, GCROOT_IO_INPUT));

489. }

490. lua_pushcclosure(L, io_file_iter, (int)(L->top - L->base));

491. return 1;

492. }

494. ‌LJLIB_CF(io_type)

495. {

496. cTValue *o = lj_lib_checkany(L, 1);

497. if (!(tvisudata(o) && udataV(o)->udtype == UDTYPE_IO_FILE))

498. setnilV(L->top++);

499. else if (((IOFileUD *)uddata(udataV(o)))->fp != NULL)

500. lua_pushliteral(L, "file");

501. else

502. lua_pushliteral(L, "closed file");

503. return 1;

504. }

506. #include "lj_libdef.h"

508. /* ------------------------------------------------------------------------ */

510. ‌static GCobj *io_std_new(lua_State *L, FILE *fp, const char *name)

511. {

512. IOFileUD *iof = (IOFileUD *)lua_newuserdata(L, sizeof(IOFileUD));

513. GCudata *ud = udataV(L->top-1);

514. ud->udtype = UDTYPE_IO_FILE;

515. /* NOBARRIER: The GCudata is new (marked white). */

516. setgcref(ud->metatable, gcV(L->top-3));

517. iof->fp = fp;

518. iof->type = IOFILE_TYPE_STDF;

519. lua_setfield(L, -2, name);

520. return obj2gco(ud);

521. }

523. ‌LUALIB_API int luaopen_io(lua_State *L)

524. {

525. LJ_LIB_REG(L, NULL, io_method);

526. copyTV(L, L->top, L->top-1); L->top++;

527. lua_setfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);

528. LJ_LIB_REG(L, LUA_IOLIBNAME, io);

529. setgcref(G(L)->gcroot[GCROOT_IO_INPUT], io_std_new(L, stdin, "stdin"));

530. setgcref(G(L)->gcroot[GCROOT_IO_OUTPUT], io_std_new(L, stdout, "stdout"));

531. io_std_new(L, stderr, "stderr");

532. return 1;

533. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_state.h - luajit-2.0-src

Functions defined

- lj_state_checkstack

Macros defined

- _LJ_STATE_H

- incr_top

- restorestack

- savestack

Source code

1. /*

2. ** State and stack handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_STATE_H

7. ‌#define _LJ_STATE_H

9. #include "lj_obj.h"

11. ‌#define incr_top(L) \

12. (++L->top >= tvref(L->maxstack) && (lj_state_growstack1(L), 0))

14. ‌#define savestack(L, p)                ((char *)(p) - mref(L->stack, char))

15. ‌#define restorestack(L, n)        ((TValue *)(mref(L->stack, char) + (n)))

17. LJ_FUNC void lj_state_relimitstack(lua_State *L);

18. LJ_FUNC void lj_state_shrinkstack(lua_State *L, MSize used);

19. LJ_FUNCA void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need);

20. LJ_FUNC void LJ_FASTCALL lj_state_growstack1(lua_State *L);

22. ‌static LJ_AINLINE void lj_state_checkstack(lua_State *L, MSize need)

23. {

24. if ((mref(L->maxstack, char) - (char *)L->top) <=

25. (ptrdiff_t)need*(ptrdiff_t)sizeof(TValue))

26. lj_state_growstack(L, need);

27. }

29. LJ_FUNC lua_State *lj_state_new(lua_State *L);

30. LJ_FUNC void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L);

31. #if LJ_64

32. LJ_FUNC lua_State *lj_state_newstate(lua_Alloc f, void *ud);

33. #endif

35. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lualib.h - luajit-2.0-src

Macros defined

- LUA_BITLIBNAME

- LUA_COLIBNAME

- LUA_DBLIBNAME

- LUA_FFILIBNAME

- LUA_FILEHANDLE

- LUA_IOLIBNAME

- LUA_JITLIBNAME

- LUA_LOADLIBNAME

- LUA_MATHLIBNAME

- LUA_OSLIBNAME

- LUA_STRLIBNAME

- LUA_TABLIBNAME

- _LUALIB_H

- lua_assert

Source code

1. /*

2. ** Standard library header.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LUALIB_H

7. ‌#define _LUALIB_H

9. #include "lua.h"

11. ‌#define LUA_FILEHANDLE        "FILE*"

13. ‌#define LUA_COLIBNAME        "coroutine"

14. ‌#define LUA_MATHLIBNAME        "math"

15. ‌#define LUA_STRLIBNAME        "string"

16. ‌#define LUA_TABLIBNAME        "table"

17. ‌#define LUA_IOLIBNAME        "io"

18. ‌#define LUA_OSLIBNAME        "os"

19. ‌#define LUA_LOADLIBNAME        "package"

20. ‌#define LUA_DBLIBNAME        "debug"

21. ‌#define LUA_BITLIBNAME        "bit"

22. ‌#define LUA_JITLIBNAME        "jit"

23. ‌#define LUA_FFILIBNAME        "ffi"

25. LUALIB_API int luaopen_base(lua_State *L);

26. LUALIB_API int luaopen_math(lua_State *L);

27. LUALIB_API int luaopen_string(lua_State *L);

28. LUALIB_API int luaopen_table(lua_State *L);

29. LUALIB_API int luaopen_io(lua_State *L);

30. LUALIB_API int luaopen_os(lua_State *L);

31. LUALIB_API int luaopen_package(lua_State *L);

32. LUALIB_API int luaopen_debug(lua_State *L);

33. LUALIB_API int luaopen_bit(lua_State *L);

34. LUALIB_API int luaopen_jit(lua_State *L);

35. LUALIB_API int luaopen_ffi(lua_State *L);

37. LUALIB_API void luaL_openlibs(lua_State *L);

39. #ifndef lua_assert

40. ‌#define lua_assert(x)        ((void)0)

41. #endif

43. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_package.c - luajit-2.0-src

Global variables defined

- package_global

- package_lib

- package_loaders

- sentinel_

Functions defined

- dooptions

- findfile

- lj_cf_package_loader_c

- lj_cf_package_loader_croot

- lj_cf_package_loader_lua

- lj_cf_package_loader_preload

- lj_cf_package_loadlib

- lj_cf_package_module

- lj_cf_package_require

- lj_cf_package_searchpath

- lj_cf_package_seeall

- lj_cf_package_unloadlib

- ll_bcsym

- ll_bcsym

- ll_bcsym

- ll_load

- ll_load

- ll_load

- ll_loadfunc

- ll_register

- ll_sym

- ll_sym

- ll_sym

- ll_unloadlib

- ll_unloadlib

- ll_unloadlib

- loaderror

- luaopen_package

- mksymname

- modinit

- pusherror

- pushnexttemplate

- readable

- searchpath

- setfenv

- setpath

- setprogdir

Macros defined

- AUXMARK

- DLMSG

- GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS

- GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT

- LUA_LIB

- PACKAGE_ERR_FUNC

- PACKAGE_ERR_LIB

- PACKAGE_ERR_LOAD

- PACKAGE_LIB_FAIL

- PACKAGE_LIB_FAIL

- PACKAGE_LIB_FAIL

- SYMPREFIX_BC

- SYMPREFIX_CF

- WIN32_LEAN_AND_MEAN

- lib_package_c

- sentinel

- setprogdir

- setprogdir

Source code

1. /*

2. ** Package library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2012 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lib_package_c

10. ‌#define LUA_LIB

12. #include "lua.h"

13. #include "lauxlib.h"

14. #include "lualib.h"

16. #include "lj_obj.h"

17. #include "lj_err.h"

18. #include "lj_lib.h"

20. /* ------------------------------------------------------------------------ */

22. /* Error codes for ll_loadfunc. */

23. ‌#define PACKAGE_ERR_LIB                1

24. ‌#define PACKAGE_ERR_FUNC        2

25. ‌#define PACKAGE_ERR_LOAD        3

27. /* Redefined in platform specific part. */

28. ‌#define PACKAGE_LIB_FAIL        "open"

29. ‌#define setprogdir(L)                ((void)0)

31. /* Symbol name prefixes. */

32. ‌#define SYMPREFIX_CF                "luaopen_%s"

33. ‌#define SYMPREFIX_BC                "luaJIT_BC_%s"

35. #if LJ_TARGET_DLOPEN

37. #include <dlfcn.h>

39. ‌static void ll_unloadlib(void *lib)

40. {

41. dlclose(lib);

42. }

44. ‌static void *ll_load(lua_State *L, const char *path, int gl)

45. {

46. void *lib = dlopen(path, RTLD_NOW | (gl ? RTLD_GLOBAL : RTLD_LOCAL));

47. if (lib == NULL) lua_pushstring(L, dlerror());

48. return lib;

49. }

51. ‌static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)

52. {

53. lua_CFunction f = (lua_CFunction)dlsym(lib, sym);

54. if (f == NULL) lua_pushstring(L, dlerror());

55. return f;

56. }

58. ‌static const char *ll_bcsym(void *lib, const char *sym)

59. {

60. #if defined(RTLD_DEFAULT)

61. if (lib == NULL) lib = RTLD_DEFAULT;

62. #elif LJ_TARGET_OSX || LJ_TARGET_BSD

63. if (lib == NULL) lib = (void *)(intptr_t)-2;

64. #endif

65. return (const char *)dlsym(lib, sym);

66. }

68. #elif LJ_TARGET_WINDOWS

70. ‌#define WIN32_LEAN_AND_MEAN

71. #include <windows.h>

73. #ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS

74. ‌#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS  4

75. ‌#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT  2

76. BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*);

77. #endif

79. ‌#undef setprogdir

81. ‌static void setprogdir(lua_State *L)

82. {

83. char buff[MAX_PATH + 1];

84. char *lb;

85. DWORD nsize = sizeof(buff);

86. DWORD n = GetModuleFileNameA(NULL, buff, nsize);

87. if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) {

88. luaL_error(L, "unable to get ModuleFileName");

89. } else {

90. *lb = '\0';

91. luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);

92. lua_remove(L, -2);  /* remove original string */

93. }

94. }

96. ‌static void pusherror(lua_State *L)

97. {

98. DWORD error = GetLastError();

99. char buffer[128];

100. if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,

101. NULL, error, 0, buffer, sizeof(buffer), NULL))

102. lua_pushstring(L, buffer);

103. else

104. lua_pushfstring(L, "system error %d\n", error);

105. }

107. ‌static void ll_unloadlib(void *lib)

108. {

109. FreeLibrary((HINSTANCE)lib);

110. }

112. ‌static void *ll_load(lua_State *L, const char *path, int gl)

113. {

114. HINSTANCE lib = LoadLibraryA(path);

115. if (lib == NULL) pusherror(L);

116. UNUSED(gl);

117. return lib;

118. }

120. ‌static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)

121. {

122. lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);

123. if (f == NULL) pusherror(L);

124. return f;

125. }

127. ‌static const char *ll_bcsym(void *lib, const char *sym)

128. {

129. if (lib) {

130. return (const char *)GetProcAddress((HINSTANCE)lib, sym);

131. } else {

132. HINSTANCE h = GetModuleHandleA(NULL);

133. const char *p = (const char *)GetProcAddress(h, sym);

134. if (p == NULL && GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,

135. (const char *)ll_bcsym, &h))

136. p = (const char *)GetProcAddress(h, sym);

137. return p;

138. }

139. }

141. #else

143. ‌#undef PACKAGE_LIB_FAIL

144. ‌#define PACKAGE_LIB_FAIL        "absent"

146. ‌#define DLMSG        "dynamic libraries not enabled; no support for target OS"

148. ‌static void ll_unloadlib(void *lib)

149. {

150. UNUSED(lib);

151. }

153. ‌static void *ll_load(lua_State *L, const char *path, int gl)

154. {

155. UNUSED(path); UNUSED(gl);

156. lua_pushliteral(L, DLMSG);

157. return NULL;

158. }

160. ‌static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)

161. {

162. UNUSED(lib); UNUSED(sym);

163. lua_pushliteral(L, DLMSG);

164. return NULL;

165. }

167. ‌static const char *ll_bcsym(void *lib, const char *sym)

168. {

169. UNUSED(lib); UNUSED(sym);

170. return NULL;

171. }

173. #endif

175. /* ------------------------------------------------------------------------ */

177. ‌static void **ll_register(lua_State *L, const char *path)

178. {

179. void **plib;

180. lua_pushfstring(L, "LOADLIB: %s", path);

181. lua_gettable(L, LUA_REGISTRYINDEX);  /* check library in registry? */

182. if (!lua_isnil(L, -1)) {  /* is there an entry? */

183. plib = (void **)lua_touserdata(L, -1);

184. } else {  /* no entry yet; create one */

185. lua_pop(L, 1);

186. plib = (void **)lua_newuserdata(L, sizeof(void *));

187. *plib = NULL;

188. luaL_getmetatable(L, "_LOADLIB");

189. lua_setmetatable(L, -2);

190. lua_pushfstring(L, "LOADLIB: %s", path);

191. lua_pushvalue(L, -2);

192. lua_settable(L, LUA_REGISTRYINDEX);

193. }

194. return plib;

195. }

197. ‌static const char *mksymname(lua_State *L, const char *modname,

198. const char *prefix)

199. {

200. const char *funcname;

201. const char *mark = strchr(modname, *LUA_IGMARK);

202. if (mark) modname = mark + 1;

203. funcname = luaL_gsub(L, modname, ".", "_");

204. funcname = lua_pushfstring(L, prefix, funcname);

205. lua_remove(L, -2);  /* remove 'gsub' result */

206. return funcname;

207. }

209. ‌static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r)

210. {

211. void **reg = ll_register(L, path);

212. if (*reg == NULL) *reg = ll_load(L, path, (*name == '*'));

213. if (*reg == NULL) {

214. return PACKAGE_ERR_LIB;  /* Unable to load library. */

215. } else if (*name == '*') {  /* Only load library into global namespace. */

216. lua_pushboolean(L, 1);

217. return 0;

218. } else {

219. const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF);

220. lua_CFunction f = ll_sym(L, *reg, sym);

221. if (f) {

222. lua_pushcfunction(L, f);

223. return 0;

224. }

225. if (!r) {

226. const char *bcdata = ll_bcsym(*reg, mksymname(L, name, SYMPREFIX_BC));

227. lua_pop(L, 1);

228. if (bcdata) {

229. if (luaL_loadbuffer(L, bcdata, LJ_MAX_BUF, name) != 0)

230. return PACKAGE_ERR_LOAD;

231. return 0;

232. }

233. }

234. return PACKAGE_ERR_FUNC;  /* Unable to find function. */

235. }

236. }

238. ‌static int lj_cf_package_loadlib(lua_State *L)

239. {

240. const char *path = luaL_checkstring(L, 1);

241. const char *init = luaL_checkstring(L, 2);

242. int st = ll_loadfunc(L, path, init, 1);

243. if (st == 0) {  /* no errors? */

244. return 1;  /* return the loaded function */

245. } else {  /* error; error message is on stack top */

246. lua_pushnil(L);

247. lua_insert(L, -2);

248. lua_pushstring(L, (st == PACKAGE_ERR_LIB) ?  PACKAGE_LIB_FAIL : "init");

249. return 3;  /* return nil, error message, and where */

250. }

251. }

253. ‌static int lj_cf_package_unloadlib(lua_State *L)

254. {

255. void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");

256. if (*lib) ll_unloadlib(*lib);

257. *lib = NULL;  /* mark library as closed */

258. return 0;

259. }

261. /* ------------------------------------------------------------------------ */

263. ‌static int readable(const char *filename)

264. {

265. FILE *f = fopen(filename, "r");  /* try to open file */

266. if (f == NULL) return 0;  /* open failed */

267. fclose(f);

268. return 1;

269. }

271. ‌static const char *pushnexttemplate(lua_State *L, const char *path)

272. {

273. const char *l;

274. while (*path == *LUA_PATHSEP) path++;  /* skip separators */

275. if (*path == '\0') return NULL;  /* no more templates */

276. l = strchr(path, *LUA_PATHSEP);  /* find next separator */

277. if (l == NULL) l = path + strlen(path);

278. lua_pushlstring(L, path, (size_t)(l - path));  /* template */

279. return l;

280. }

282. ‌static const char *searchpath (lua_State *L, const char *name,

283. const char *path, const char *sep,

284. const char *dirsep)

285. {

286. luaL_Buffer msg;  /* to build error message */

287. luaL_buffinit(L, &msg);

288. if (*sep != '\0')  /* non-empty separator? */

289. name = luaL_gsub(L, name, sep, dirsep);  /* replace it by 'dirsep' */

290. while ((path = pushnexttemplate(L, path)) != NULL) {

291. const char *filename = luaL_gsub(L, lua_tostring(L, -1),

292. LUA_PATH_MARK, name);

293. lua_remove(L, -2);  /* remove path template */

294. if (readable(filename))  /* does file exist and is readable? */

295. return filename;  /* return that file name */

296. lua_pushfstring(L, "\n\tno file " LUA_QS, filename);

297. lua_remove(L, -2);  /* remove file name */

298. luaL_addvalue(&msg);  /* concatenate error msg. entry */

299. }

300. luaL_pushresult(&msg);  /* create error message */

301. return NULL;  /* not found */

302. }

304. ‌static int lj_cf_package_searchpath(lua_State *L)

305. {

306. const char *f = searchpath(L, luaL_checkstring(L, 1),

307. luaL_checkstring(L, 2),

308. luaL_optstring(L, 3, "."),

309. luaL_optstring(L, 4, LUA_DIRSEP));

310. if (f != NULL) {

311. return 1;

312. } else {  /* error message is on top of the stack */

313. lua_pushnil(L);

314. lua_insert(L, -2);

315. return 2;  /* return nil + error message */

316. }

317. }

319. ‌static const char *findfile(lua_State *L, const char *name,

320. const char *pname)

321. {

322. const char *path;

323. lua_getfield(L, LUA_ENVIRONINDEX, pname);

324. path = lua_tostring(L, -1);

325. if (path == NULL)

326. luaL_error(L, LUA_QL("package.%s") " must be a string", pname);

327. return searchpath(L, name, path, ".", LUA_DIRSEP);

328. }

330. ‌static void loaderror(lua_State *L, const char *filename)

331. {

332. luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",

333. lua_tostring(L, 1), filename, lua_tostring(L, -1));

334. }

336. ‌static int lj_cf_package_loader_lua(lua_State *L)

337. {

338. const char *filename;

339. const char *name = luaL_checkstring(L, 1);

340. filename = findfile(L, name, "path");

341. if (filename == NULL) return 1;  /* library not found in this path */

342. if (luaL_loadfile(L, filename) != 0)

343. loaderror(L, filename);

344. return 1;  /* library loaded successfully */

345. }

347. ‌static int lj_cf_package_loader_c(lua_State *L)

348. {

349. const char *name = luaL_checkstring(L, 1);

350. const char *filename = findfile(L, name, "cpath");

351. if (filename == NULL) return 1;  /* library not found in this path */

352. if (ll_loadfunc(L, filename, name, 0) != 0)

353. loaderror(L, filename);

354. return 1;  /* library loaded successfully */

355. }

357. ‌static int lj_cf_package_loader_croot(lua_State *L)

358. {

359. const char *filename;

360. const char *name = luaL_checkstring(L, 1);

361. const char *p = strchr(name, '.');

362. int st;

363. if (p == NULL) return 0;  /* is root */

364. lua_pushlstring(L, name, (size_t)(p - name));

365. filename = findfile(L, lua_tostring(L, -1), "cpath");

366. if (filename == NULL) return 1;  /* root not found */

367. if ((st = ll_loadfunc(L, filename, name, 0)) != 0) {

368. if (st != PACKAGE_ERR_FUNC) loaderror(L, filename);  /* real error */

369. lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,

370. name, filename);

371. return 1;  /* function not found */

372. }

373. return 1;

374. }

376. ‌static int lj_cf_package_loader_preload(lua_State *L)

377. {

378. const char *name = luaL_checkstring(L, 1);

379. lua_getfield(L, LUA_ENVIRONINDEX, "preload");

380. if (!lua_istable(L, -1))

381. luaL_error(L, LUA_QL("package.preload") " must be a table");

382. lua_getfield(L, -1, name);

383. if (lua_isnil(L, -1)) {  /* Not found? */

384. const char *bcname = mksymname(L, name, SYMPREFIX_BC);

385. const char *bcdata = ll_bcsym(NULL, bcname);

386. if (bcdata == NULL || luaL_loadbuffer(L, bcdata, LJ_MAX_BUF, name) != 0)

387. lua_pushfstring(L, "\n\tno field package.preload['%s']", name);

388. }

389. return 1;

390. }

392. /* ------------------------------------------------------------------------ */

394. ‌static const int sentinel_ = 0;

395. ‌#define sentinel        ((void *)&sentinel_)

397. ‌static int lj_cf_package_require(lua_State *L)

398. {

399. const char *name = luaL_checkstring(L, 1);

400. int i;

401. lua_settop(L, 1);  /* _LOADED table will be at index 2 */

402. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");

403. lua_getfield(L, 2, name);

404. if (lua_toboolean(L, -1)) {  /* is it there? */

405. if (lua_touserdata(L, -1) == sentinel)  /* check loops */

406. luaL_error(L, "loop or previous error loading module " LUA_QS, name);

407. return 1;  /* package is already loaded */

408. }

409. /* else must load it; iterate over available loaders */

410. lua_getfield(L, LUA_ENVIRONINDEX, "loaders");

411. if (!lua_istable(L, -1))

412. luaL_error(L, LUA_QL("package.loaders") " must be a table");

413. lua_pushliteral(L, "");  /* error message accumulator */

414. for (i = 1; ; i++) {

415. lua_rawgeti(L, -2, i);  /* get a loader */

416. if (lua_isnil(L, -1))

417. luaL_error(L, "module " LUA_QS " not found:%s",

418. name, lua_tostring(L, -2));

419. lua_pushstring(L, name);

420. lua_call(L, 1, 1);  /* call it */

421. if (lua_isfunction(L, -1))  /* did it find module? */

422. break;  /* module loaded successfully */

423. else if (lua_isstring(L, -1))  /* loader returned error message? */

424. lua_concat(L, 2);  /* accumulate it */

425. else

426. lua_pop(L, 1);

427. }

428. lua_pushlightuserdata(L, sentinel);

429. lua_setfield(L, 2, name);  /* _LOADED[name] = sentinel */

430. lua_pushstring(L, name);  /* pass name as argument to module */

431. lua_call(L, 1, 1);  /* run loaded module */

432. if (!lua_isnil(L, -1))  /* non-nil return? */

433. lua_setfield(L, 2, name);  /* _LOADED[name] = returned value */

434. lua_getfield(L, 2, name);

435. if (lua_touserdata(L, -1) == sentinel) {   /* module did not set a value? */

436. lua_pushboolean(L, 1);  /* use true as result */

437. lua_pushvalue(L, -1);  /* extra copy to be returned */

438. lua_setfield(L, 2, name);  /* _LOADED[name] = true */

439. }

440. lj_lib_checkfpu(L);

441. return 1;

442. }

444. /* ------------------------------------------------------------------------ */

446. ‌static void setfenv(lua_State *L)

447. {

448. lua_Debug ar;

449. if (lua_getstack(L, 1, &ar) == 0 ||

450. lua_getinfo(L, "f", &ar) == 0 ||  /* get calling function */

451. lua_iscfunction(L, -1))

452. luaL_error(L, LUA_QL("module") " not called from a Lua function");

453. lua_pushvalue(L, -2);

454. lua_setfenv(L, -2);

455. lua_pop(L, 1);

456. }

458. ‌static void dooptions(lua_State *L, int n)

459. {

460. int i;

461. for (i = 2; i <= n; i++) {

462. lua_pushvalue(L, i);  /* get option (a function) */

463. lua_pushvalue(L, -2);  /* module */

464. lua_call(L, 1, 0);

465. }

466. }

468. ‌static void modinit(lua_State *L, const char *modname)

469. {

470. const char *dot;

471. lua_pushvalue(L, -1);

472. lua_setfield(L, -2, "_M");  /* module._M = module */

473. lua_pushstring(L, modname);

474. lua_setfield(L, -2, "_NAME");

475. dot = strrchr(modname, '.');  /* look for last dot in module name */

476. if (dot == NULL) dot = modname; else dot++;

477. /* set _PACKAGE as package name (full module name minus last part) */

478. lua_pushlstring(L, modname, (size_t)(dot - modname));

479. lua_setfield(L, -2, "_PACKAGE");

480. }

482. ‌static int lj_cf_package_module(lua_State *L)

483. {

484. const char *modname = luaL_checkstring(L, 1);

485. int loaded = lua_gettop(L) + 1;  /* index of _LOADED table */

486. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");

487. lua_getfield(L, loaded, modname);  /* get _LOADED[modname] */

488. if (!lua_istable(L, -1)) {  /* not found? */

489. lua_pop(L, 1);  /* remove previous result */

490. /* try global variable (and create one if it does not exist) */

491. if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)

492. lj_err_callerv(L, LJ_ERR_BADMODN, modname);

493. lua_pushvalue(L, -1);

494. lua_setfield(L, loaded, modname);  /* _LOADED[modname] = new table */

495. }

496. /* check whether table already has a _NAME field */

497. lua_getfield(L, -1, "_NAME");

498. if (!lua_isnil(L, -1)) {  /* is table an initialized module? */

499. lua_pop(L, 1);

500. } else {  /* no; initialize it */

501. lua_pop(L, 1);

502. modinit(L, modname);

503. }

504. lua_pushvalue(L, -1);

505. setfenv(L);

506. dooptions(L, loaded - 1);

507. return 0;

508. }

510. ‌static int lj_cf_package_seeall(lua_State *L)

511. {

512. luaL_checktype(L, 1, LUA_TTABLE);

513. if (!lua_getmetatable(L, 1)) {

514. lua_createtable(L, 0, 1); /* create new metatable */

515. lua_pushvalue(L, -1);

516. lua_setmetatable(L, 1);

517. }

518. lua_pushvalue(L, LUA_GLOBALSINDEX);

519. lua_setfield(L, -2, "__index");  /* mt.__index = _G */

520. return 0;

521. }

523. /* ------------------------------------------------------------------------ */

525. ‌#define AUXMARK                "\1"

527. ‌static void setpath(lua_State *L, const char *fieldname, const char *envname,

528. const char *def, int noenv)

529. {

530. #if LJ_TARGET_CONSOLE

531. const char *path = NULL;

532. UNUSED(envname);

533. #else

534. const char *path = getenv(envname);

535. #endif

536. if (path == NULL || noenv) {

537. lua_pushstring(L, def);

538. } else {

539. path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,

540. LUA_PATHSEP AUXMARK LUA_PATHSEP);

541. luaL_gsub(L, path, AUXMARK, def);

542. lua_remove(L, -2);

543. }

544. setprogdir(L);

545. lua_setfield(L, -2, fieldname);

546. }

548. ‌static const luaL_Reg package_lib[] = {

549. { "loadlib",        lj_cf_package_loadlib },

550. { "searchpath",  lj_cf_package_searchpath },

551. { "seeall",        lj_cf_package_seeall },

552. { NULL, NULL }

553. };

555. ‌static const luaL_Reg package_global[] = {

556. { "module",        lj_cf_package_module },

557. { "require",        lj_cf_package_require },

558. { NULL, NULL }

559. };

561. ‌static const lua_CFunction package_loaders[] =

562. {

563. lj_cf_package_loader_preload,

564. lj_cf_package_loader_lua,

565. lj_cf_package_loader_c,

566. lj_cf_package_loader_croot,

567. NULL

568. };

570. ‌LUALIB_API int luaopen_package(lua_State *L)

571. {

572. int i;

573. int noenv;

574. luaL_newmetatable(L, "_LOADLIB");

575. lj_lib_pushcf(L, lj_cf_package_unloadlib, 1);

576. lua_setfield(L, -2, "__gc");

577. luaL_register(L, LUA_LOADLIBNAME, package_lib);

578. lua_pushvalue(L, -1);

579. lua_replace(L, LUA_ENVIRONINDEX);

580. lua_createtable(L, sizeof(package_loaders)/sizeof(package_loaders[0])-1, 0);

581. for (i = 0; package_loaders[i] != NULL; i++) {

582. lj_lib_pushcf(L, package_loaders[i], 1);

583. lua_rawseti(L, -2, i+1);

584. }

585. lua_setfield(L, -2, "loaders");

586. lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");

587. noenv = lua_toboolean(L, -1);

588. lua_pop(L, 1);

589. setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv);

590. setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv);

591. lua_pushliteral(L, LUA_PATH_CONFIG);

592. lua_setfield(L, -2, "config");

593. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);

594. lua_setfield(L, -2, "loaded");

595. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);

596. lua_setfield(L, -2, "preload");

597. lua_pushvalue(L, LUA_GLOBALSINDEX);

598. luaL_register(L, NULL, package_global);

599. lua_pop(L, 1);

600. return 1;

601. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_clib.c - luajit-2.0-src

Global variables defined

- clib_def_handle

Functions defined

- clib_check_lds

- clib_error

- clib_error

- clib_error_

- clib_extname

- clib_extname

- clib_extsym

- clib_func_argsize

- clib_getsym

- clib_getsym

- clib_getsym

- clib_loadlib

- clib_loadlib

- clib_loadlib

- clib_needext

- clib_new

- clib_resolve_lds

- clib_unloadlib

- clib_unloadlib

- clib_unloadlib

- lj_clib_default

- lj_clib_index

- lj_clib_load

- lj_clib_unload

Macros defined

- CLIB_DEFHANDLE

- CLIB_DEFHANDLE

- CLIB_DEFHANDLE

- CLIB_DEFHANDLE

- CLIB_DEFHANDLE

- CLIB_SOEXT

- CLIB_SOEXT

- CLIB_SOEXT

- CLIB_SOPREFIX

- CLIB_SOPREFIX

- GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS

- GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT

- WIN32_LEAN_AND_MEAN

- clib_error

Source code

1. /*

2. ** FFI C library loader.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "lj_obj.h"

8. #if LJ_HASFFI

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_tab.h"

13. #include "lj_str.h"

14. #include "lj_udata.h"

15. #include "lj_ctype.h"

16. #include "lj_cconv.h"

17. #include "lj_cdata.h"

18. #include "lj_clib.h"

19. #include "lj_strfmt.h"

21. /* -- OS-specific functions ----------------------------------------------- */

23. #if LJ_TARGET_DLOPEN

25. #include <dlfcn.h>

26. #include <stdio.h>

28. #if defined(RTLD_DEFAULT)

29. ‌#define CLIB_DEFHANDLE        RTLD_DEFAULT

30. #elif LJ_TARGET_OSX || LJ_TARGET_BSD

31. ‌#define CLIB_DEFHANDLE        ((void *)(intptr_t)-2)

32. #else

33. ‌#define CLIB_DEFHANDLE        NULL

34. #endif

36. ‌LJ_NORET LJ_NOINLINE static void clib_error_(lua_State *L)

37. {

38. lj_err_callermsg(L, dlerror());

39. }

41. ‌#define clib_error(L, fmt, name)        clib_error_(L)

43. #if defined(__CYGWIN__)

44. ‌#define CLIB_SOPREFIX        "cyg"

45. #else

46. ‌#define CLIB_SOPREFIX        "lib"

47. #endif

49. #if LJ_TARGET_OSX

50. ‌#define CLIB_SOEXT        "%s.dylib"

51. #elif defined(__CYGWIN__)

52. ‌#define CLIB_SOEXT        "%s.dll"

53. #else

54. ‌#define CLIB_SOEXT        "%s.so"

55. #endif

57. ‌static const char *clib_extname(lua_State *L, const char *name)

58. {

59. if (!strchr(name, '/')

60. #ifdef __CYGWIN__

61. && !strchr(name, '\\')

62. #endif

63. ) {

64. if (!strchr(name, '.')) {

65. name = lj_strfmt_pushf(L, CLIB_SOEXT, name);

66. L->top--;

67. #ifdef __CYGWIN__

68. } else {

69. return name;

70. #endif

71. }

72. if (!(name[0] == CLIB_SOPREFIX[0] && name[1] == CLIB_SOPREFIX[1] &&

73. name[2] == CLIB_SOPREFIX[2])) {

74. name = lj_strfmt_pushf(L, CLIB_SOPREFIX "%s", name);

75. L->top--;

76. }

77. }

78. return name;

79. }

81. /* Check for a recognized ld script line. */

82. ‌static const char *clib_check_lds(lua_State *L, const char *buf)

83. {

84. char *p, *e;

85. if ((!strncmp(buf, "GROUP", 5) || !strncmp(buf, "INPUT", 5)) &&

86. (p = strchr(buf, '('))) {

87. while (*++p == ' ') ;

88. for (e = p; *e && *e != ' ' && *e != ')'; e++) ;

89. return strdata(lj_str_new(L, p, e-p));

90. }

91. return NULL;

92. }

94. /* Quick and dirty solution to resolve shared library name from ld script. */

95. ‌static const char *clib_resolve_lds(lua_State *L, const char *name)

96. {

97. FILE *fp = fopen(name, "r");

98. const char *p = NULL;

99. if (fp) {

100. char buf[256];

101. if (fgets(buf, sizeof(buf), fp)) {

102. if (!strncmp(buf, "/* GNU ld script", 16)) {  /* ld script magic? */

103. while (fgets(buf, sizeof(buf), fp)) {  /* Check all lines. */

104. p = clib_check_lds(L, buf);

105. if (p) break;

106. }

107. } else {  /* Otherwise check only the first line. */

108. p = clib_check_lds(L, buf);

109. }

110. }

111. fclose(fp);

112. }

113. return p;

114. }

116. ‌static void *clib_loadlib(lua_State *L, const char *name, int global)

117. {

118. void *h = dlopen(clib_extname(L, name),

119. RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL));

120. if (!h) {

121. const char *e, *err = dlerror();

122. if (*err == '/' && (e = strchr(err, ':')) &&

123. (name = clib_resolve_lds(L, strdata(lj_str_new(L, err, e-err))))) {

124. h = dlopen(name, RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL));

125. if (h) return h;

126. err = dlerror();

127. }

128. lj_err_callermsg(L, err);

129. }

130. return h;

131. }

133. ‌static void clib_unloadlib(CLibrary *cl)

134. {

135. if (cl->handle && cl->handle != CLIB_DEFHANDLE)

136. dlclose(cl->handle);

137. }

139. ‌static void *clib_getsym(CLibrary *cl, const char *name)

140. {

141. void *p = dlsym(cl->handle, name);

142. return p;

143. }

145. #elif LJ_TARGET_WINDOWS

147. ‌#define WIN32_LEAN_AND_MEAN

148. #include <windows.h>

150. #ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS

151. ‌#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS        4

152. ‌#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT        2

153. BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*);

154. #endif

156. ‌#define CLIB_DEFHANDLE        ((void *)-1)

158. /* Default libraries. */

159. enum {

160. CLIB_HANDLE_EXE,

161. CLIB_HANDLE_DLL,

162. CLIB_HANDLE_CRT,

163. CLIB_HANDLE_KERNEL32,

164. CLIB_HANDLE_USER32,

165. CLIB_HANDLE_GDI32,

166. CLIB_HANDLE_MAX

167. };

169. ‌static void *clib_def_handle[CLIB_HANDLE_MAX];

171. ‌LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt,

172. const char *name)

173. {

174. DWORD err = GetLastError();

175. char buf[128];

176. if (!FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,

177. NULL, err, 0, buf, sizeof(buf), NULL))

178. buf[0] = '\0';

179. lj_err_callermsg(L, lj_strfmt_pushf(L, fmt, name, buf));

180. }

182. ‌static int clib_needext(const char *s)

183. {

184. while (*s) {

185. if (*s == '/' || *s == '\\' || *s == '.') return 0;

186. s++;

187. }

188. return 1;

189. }

191. ‌static const char *clib_extname(lua_State *L, const char *name)

192. {

193. if (clib_needext(name)) {

194. name = lj_strfmt_pushf(L, "%s.dll", name);

195. L->top--;

196. }

197. return name;

198. }

200. ‌static void *clib_loadlib(lua_State *L, const char *name, int global)

201. {

202. DWORD oldwerr = GetLastError();

203. void *h = (void *)LoadLibraryA(clib_extname(L, name));

204. if (!h) clib_error(L, "cannot load module " LUA_QS ": %s", name);

205. SetLastError(oldwerr);

206. UNUSED(global);

207. return h;

208. }

210. ‌static void clib_unloadlib(CLibrary *cl)

211. {

212. if (cl->handle == CLIB_DEFHANDLE) {

213. MSize i;

214. for (i = CLIB_HANDLE_KERNEL32; i < CLIB_HANDLE_MAX; i++) {

215. void *h = clib_def_handle[i];

216. if (h) {

217. clib_def_handle[i] = NULL;

218. FreeLibrary((HINSTANCE)h);

219. }

220. }

221. } else if (cl->handle) {

222. FreeLibrary((HINSTANCE)cl->handle);

223. }

224. }

226. ‌static void *clib_getsym(CLibrary *cl, const char *name)

227. {

228. void *p = NULL;

229. if (cl->handle == CLIB_DEFHANDLE) {  /* Search default libraries. */

230. MSize i;

231. for (i = 0; i < CLIB_HANDLE_MAX; i++) {

232. HINSTANCE h = (HINSTANCE)clib_def_handle[i];

233. if (!(void *)h) {  /* Resolve default library handles (once). */

234. switch (i) {

235. case CLIB_HANDLE_EXE: GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, &h); break;

236. case CLIB_HANDLE_DLL:

237. GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,

238. (const char *)clib_def_handle, &h);

239. break;

240. case CLIB_HANDLE_CRT:

241. GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,

242. (const char *)&_fmode, &h);

243. break;

244. case CLIB_HANDLE_KERNEL32: h = LoadLibraryA("kernel32.dll"); break;

245. case CLIB_HANDLE_USER32: h = LoadLibraryA("user32.dll"); break;

246. case CLIB_HANDLE_GDI32: h = LoadLibraryA("gdi32.dll"); break;

247. }

248. if (!h) continue;

249. clib_def_handle[i] = (void *)h;

250. }

251. p = (void *)GetProcAddress(h, name);

252. if (p) break;

253. }

254. } else {

255. p = (void *)GetProcAddress((HINSTANCE)cl->handle, name);

256. }

257. return p;

258. }

260. #else

262. ‌#define CLIB_DEFHANDLE        NULL

264. ‌LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt,

265. const char *name)

266. {

267. lj_err_callermsg(L, lj_strfmt_pushf(L, fmt, name, "no support for this OS"));

268. }

270. ‌static void *clib_loadlib(lua_State *L, const char *name, int global)

271. {

272. lj_err_callermsg(L, "no support for loading dynamic libraries for this OS");

273. UNUSED(name); UNUSED(global);

274. return NULL;

275. }

277. ‌static void clib_unloadlib(CLibrary *cl)

278. {

279. UNUSED(cl);

280. }

282. ‌static void *clib_getsym(CLibrary *cl, const char *name)

283. {

284. UNUSED(cl); UNUSED(name);

285. return NULL;

286. }

288. #endif

290. /* -- C library indexing -------------------------------------------------- */

292. #if LJ_TARGET_X86 && LJ_ABI_WIN

293. /* Compute argument size for fastcall/stdcall functions. */

294. ‌static CTSize clib_func_argsize(CTState *cts, CType *ct)

295. {

296. CTSize n = 0;

297. while (ct->sib) {

298. CType *d;

299. ct = ctype_get(cts, ct->sib);

300. if (ctype_isfield(ct->info)) {

301. d = ctype_rawchild(cts, ct);

302. n += ((d->size + 3) & ~3);

303. }

304. }

305. return n;

306. }

307. #endif

309. /* Get redirected or mangled external symbol. */

310. ‌static const char *clib_extsym(CTState *cts, CType *ct, GCstr *name)

311. {

312. if (ct->sib) {

313. CType *ctf = ctype_get(cts, ct->sib);

314. if (ctype_isxattrib(ctf->info, CTA_REDIR))

315. return strdata(gco2str(gcref(ctf->name)));

316. }

317. return strdata(name);

318. }

320. /* Index a C library by name. */

321. ‌TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name)

322. {

323. TValue *tv = lj_tab_setstr(L, cl->cache, name);

324. if (LJ_UNLIKELY(tvisnil(tv))) {

325. CTState *cts = ctype_cts(L);

326. CType *ct;

327. CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX);

328. if (!id)

329. lj_err_callerv(L, LJ_ERR_FFI_NODECL, strdata(name));

330. if (ctype_isconstval(ct->info)) {

331. CType *ctt = ctype_child(cts, ct);

332. lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4);

333. if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0)

334. setnumV(tv, (lua_Number)(uint32_t)ct->size);

335. else

336. setintV(tv, (int32_t)ct->size);

337. } else {

338. const char *sym = clib_extsym(cts, ct, name);

339. #if LJ_TARGET_WINDOWS

340. DWORD oldwerr = GetLastError();

341. #endif

342. void *p = clib_getsym(cl, sym);

343. GCcdata *cd;

344. lua_assert(ctype_isfunc(ct->info) || ctype_isextern(ct->info));

345. #if LJ_TARGET_X86 && LJ_ABI_WIN

346. /* Retry with decorated name for fastcall/stdcall functions. */

347. if (!p && ctype_isfunc(ct->info)) {

348. CTInfo cconv = ctype_cconv(ct->info);

349. if (cconv == CTCC_FASTCALL || cconv == CTCC_STDCALL) {

350. CTSize sz = clib_func_argsize(cts, ct);

351. const char *symd = lj_strfmt_pushf(L,

352. cconv == CTCC_FASTCALL ? "@%s@%d" : "_%s@%d",

353. sym, sz);

354. L->top--;

355. p = clib_getsym(cl, symd);

356. }

357. }

358. #endif

359. if (!p)

360. clib_error(L, "cannot resolve symbol " LUA_QS ": %s", sym);

361. #if LJ_TARGET_WINDOWS

362. SetLastError(oldwerr);

363. #endif

364. cd = lj_cdata_new(cts, id, CTSIZE_PTR);

365. *(void **)cdataptr(cd) = p;

366. setcdataV(L, tv, cd);

367. }

368. }

369. return tv;

370. }

372. /* -- C library management ------------------------------------------------ */

374. /* Create a new CLibrary object and push it on the stack. */

375. ‌static CLibrary *clib_new(lua_State *L, GCtab *mt)

376. {

377. GCtab *t = lj_tab_new(L, 0, 0);

378. GCudata *ud = lj_udata_new(L, sizeof(CLibrary), t);

379. CLibrary *cl = (CLibrary *)uddata(ud);

380. cl->cache = t;

381. ud->udtype = UDTYPE_FFI_CLIB;

382. /* NOBARRIER: The GCudata is new (marked white). */

383. setgcref(ud->metatable, obj2gco(mt));

384. setudataV(L, L->top++, ud);

385. return cl;

386. }

388. /* Load a C library. */

389. ‌void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global)

390. {

391. void *handle = clib_loadlib(L, strdata(name), global);

392. CLibrary *cl = clib_new(L, mt);

393. cl->handle = handle;

394. }

396. /* Unload a C library. */

397. ‌void lj_clib_unload(CLibrary *cl)

398. {

399. clib_unloadlib(cl);

400. cl->handle = NULL;

401. }

403. /* Create the default C library object. */

404. ‌void lj_clib_default(lua_State *L, GCtab *mt)

405. {

406. CLibrary *cl = clib_new(L, mt);

407. cl->handle = CLIB_DEFHANDLE;

408. }

410. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_clib.h - luajit-2.0-src

Data types defined

- CLibrary

- CLibrary

Macros defined

- CLNS_INDEX

- _LJ_CLIB_H

Source code

1. /*

2. ** FFI C library loader.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_CLIB_H

7. ‌#define _LJ_CLIB_H

9. #include "lj_obj.h"

11. #if LJ_HASFFI

13. /* Namespace for C library indexing. */

14. ‌#define CLNS_INDEX        ((1u<<CT_FUNC)|(1u<<CT_EXTERN)|(1u<<CT_CONSTVAL))

16. /* C library namespace. */

17. ‌typedef struct CLibrary {

18. void *handle;                /* Opaque handle for dynamic library loader. */

19. GCtab *cache;                /* Cache for resolved symbols. Anchored in ud->env. */

20. ‌} CLibrary;

22. LJ_FUNC TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name);

23. LJ_FUNC void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global);

24. LJ_FUNC void lj_clib_unload(CLibrary *cl);

25. LJ_FUNC void lj_clib_default(lua_State *L, GCtab *mt);

27. #endif

29. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_cdata.h - luajit-2.0-src

Functions defined

- cdata_getptr

- cdata_setptr

- lj_cdata_new

- lj_cdata_new_

Macros defined

- _LJ_CDATA_H

Source code

1. /*

2. ** C data management.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_CDATA_H

7. ‌#define _LJ_CDATA_H

9. #include "lj_obj.h"

10. #include "lj_gc.h"

11. #include "lj_ctype.h"

13. #if LJ_HASFFI

15. /* Get C data pointer. */

16. ‌static LJ_AINLINE void *cdata_getptr(void *p, CTSize sz)

17. {

18. if (LJ_64 && sz == 4) {  /* Support 32 bit pointers on 64 bit targets. */

19. return ((void *)(uintptr_t)*(uint32_t *)p);

20. } else {

21. lua_assert(sz == CTSIZE_PTR);

22. return *(void **)p;

23. }

24. }

26. /* Set C data pointer. */

27. ‌static LJ_AINLINE void cdata_setptr(void *p, CTSize sz, const void *v)

28. {

29. if (LJ_64 && sz == 4) {  /* Support 32 bit pointers on 64 bit targets. */

30. *(uint32_t *)p = (uint32_t)(uintptr_t)v;

31. } else {

32. lua_assert(sz == CTSIZE_PTR);

33. *(void **)p = (void *)v;

34. }

35. }

37. /* Allocate fixed-size C data object. */

38. ‌static LJ_AINLINE GCcdata *lj_cdata_new(CTState *cts, CTypeID id, CTSize sz)

39. {

40. GCcdata *cd;

41. #ifdef LUA_USE_ASSERT

42. CType *ct = ctype_raw(cts, id);

43. lua_assert((ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR) == sz);

44. #endif

45. cd = (GCcdata *)lj_mem_newgco(cts->L, sizeof(GCcdata) + sz);

46. cd->gct = ~LJ_TCDATA;

47. cd->ctypeid = ctype_check(cts, id);

48. return cd;

49. }

51. /* Variant which works without a valid CTState. */

52. ‌static LJ_AINLINE GCcdata *lj_cdata_new_(lua_State *L, CTypeID id, CTSize sz)

53. {

54. GCcdata *cd = (GCcdata *)lj_mem_newgco(L, sizeof(GCcdata) + sz);

55. cd->gct = ~LJ_TCDATA;

56. cd->ctypeid = id;

57. return cd;

58. }

60. LJ_FUNC GCcdata *lj_cdata_newref(CTState *cts, const void *pp, CTypeID id);

61. LJ_FUNC GCcdata *lj_cdata_newv(lua_State *L, CTypeID id, CTSize sz,

62. CTSize align);

64. LJ_FUNC void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd);

65. LJ_FUNC void lj_cdata_setfin(lua_State *L, GCcdata *cd, GCobj *obj,

66. uint32_t it);

68. LJ_FUNC CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key,

69. uint8_t **pp, CTInfo *qual);

70. LJ_FUNC int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp);

71. LJ_FUNC void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o,

72. CTInfo qual);

74. #endif

76. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_cdata.c - luajit-2.0-src

Functions defined

- cdata_getconst

- lj_cdata_free

- lj_cdata_get

- lj_cdata_index

- lj_cdata_newref

- lj_cdata_newv

- lj_cdata_set

- lj_cdata_setfin

Source code

1. /*

2. ** C data management.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "lj_obj.h"

8. #if LJ_HASFFI

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_tab.h"

13. #include "lj_ctype.h"

14. #include "lj_cconv.h"

15. #include "lj_cdata.h"

17. /* -- C data allocation --------------------------------------------------- */

19. /* Allocate a new C data object holding a reference to another object. */

20. ‌GCcdata *lj_cdata_newref(CTState *cts, const void *p, CTypeID id)

21. {

22. CTypeID refid = lj_ctype_intern(cts, CTINFO_REF(id), CTSIZE_PTR);

23. GCcdata *cd = lj_cdata_new(cts, refid, CTSIZE_PTR);

24. *(const void **)cdataptr(cd) = p;

25. return cd;

26. }

28. /* Allocate variable-sized or specially aligned C data object. */

29. ‌GCcdata *lj_cdata_newv(lua_State *L, CTypeID id, CTSize sz, CTSize align)

30. {

31. global_State *g;

32. MSize extra = sizeof(GCcdataVar) + sizeof(GCcdata) +

33. (align > CT_MEMALIGN ? (1u<<align) - (1u<<CT_MEMALIGN) : 0);

34. char *p = lj_mem_newt(L, extra + sz, char);

35. uintptr_t adata = (uintptr_t)p + sizeof(GCcdataVar) + sizeof(GCcdata);

36. uintptr_t almask = (1u << align) - 1u;

37. GCcdata *cd = (GCcdata *)(((adata + almask) & ~almask) - sizeof(GCcdata));

38. lua_assert((char *)cd - p < 65536);

39. cdatav(cd)->offset = (uint16_t)((char *)cd - p);

40. cdatav(cd)->extra = extra;

41. cdatav(cd)->len = sz;

42. g = G(L);

43. setgcrefr(cd->nextgc, g->gc.root);

44. setgcref(g->gc.root, obj2gco(cd));

45. newwhite(g, obj2gco(cd));

46. cd->marked |= 0x80;

47. cd->gct = ~LJ_TCDATA;

48. cd->ctypeid = id;

49. return cd;

50. }

52. /* Free a C data object. */

53. ‌void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd)

54. {

55. if (LJ_UNLIKELY(cd->marked & LJ_GC_CDATA_FIN)) {

56. GCobj *root;

57. makewhite(g, obj2gco(cd));

58. markfinalized(obj2gco(cd));

59. if ((root = gcref(g->gc.mmudata)) != NULL) {

60. setgcrefr(cd->nextgc, root->gch.nextgc);

61. setgcref(root->gch.nextgc, obj2gco(cd));

62. setgcref(g->gc.mmudata, obj2gco(cd));

63. } else {

64. setgcref(cd->nextgc, obj2gco(cd));

65. setgcref(g->gc.mmudata, obj2gco(cd));

66. }

67. } else if (LJ_LIKELY(!cdataisv(cd))) {

68. CType *ct = ctype_raw(ctype_ctsG(g), cd->ctypeid);

69. CTSize sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR;

70. lua_assert(ctype_hassize(ct->info) || ctype_isfunc(ct->info) ||

71. ctype_isextern(ct->info));

72. lj_mem_free(g, cd, sizeof(GCcdata) + sz);

73. } else {

74. lj_mem_free(g, memcdatav(cd), sizecdatav(cd));

75. }

76. }

78. ‌void lj_cdata_setfin(lua_State *L, GCcdata *cd, GCobj *obj, uint32_t it)

79. {

80. GCtab *t = ctype_ctsG(G(L))->finalizer;

81. if (gcref(t->metatable)) {

82. /* Add cdata to finalizer table, if still enabled. */

83. TValue *tv, tmp;

84. setcdataV(L, &tmp, cd);

85. lj_gc_anybarriert(L, t);

86. tv = lj_tab_set(L, t, &tmp);

87. setgcV(L, tv, obj, it);

88. if (!tvisnil(tv))

89. cd->marked |= LJ_GC_CDATA_FIN;

90. else

91. cd->marked &= ~LJ_GC_CDATA_FIN;

92. }

93. }

95. /* -- C data indexing ----------------------------------------------------- */

97. /* Index C data by a TValue. Return CType and pointer. */

98. ‌CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp,

99. CTInfo *qual)

100. {

101. uint8_t *p = (uint8_t *)cdataptr(cd);

102. CType *ct = ctype_get(cts, cd->ctypeid);

103. ptrdiff_t idx;

105. /* Resolve reference for cdata object. */

106. if (ctype_isref(ct->info)) {

107. lua_assert(ct->size == CTSIZE_PTR);

108. p = *(uint8_t **)p;

109. ct = ctype_child(cts, ct);

110. }

112. collect_attrib:

113. /* Skip attributes and collect qualifiers. */

114. while (ctype_isattrib(ct->info)) {

115. if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size;

116. ct = ctype_child(cts, ct);

117. }

118. lua_assert(!ctype_isref(ct->info));  /* Interning rejects refs to refs. */

120. if (tvisint(key)) {

121. idx = (ptrdiff_t)intV(key);

122. goto integer_key;

123. } else if (tvisnum(key)) {  /* Numeric key. */

124. idx = LJ_64 ? (ptrdiff_t)numV(key) : (ptrdiff_t)lj_num2int(numV(key));

125. integer_key:

126. if (ctype_ispointer(ct->info)) {

127. CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info));  /* Element size. */

128. if (sz == CTSIZE_INVALID)

129. lj_err_caller(cts->L, LJ_ERR_FFI_INVSIZE);

130. if (ctype_isptr(ct->info)) {

131. p = (uint8_t *)cdata_getptr(p, ct->size);

132. } else if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) {

133. if ((ct->info & CTF_COMPLEX)) idx &= 1;

134. *qual |= CTF_CONST;  /* Valarray elements are constant. */

135. }

136. *pp = p + idx*(int32_t)sz;

137. return ct;

138. }

139. } else if (tviscdata(key)) {  /* Integer cdata key. */

140. GCcdata *cdk = cdataV(key);

141. CType *ctk = ctype_raw(cts, cdk->ctypeid);

142. if (ctype_isenum(ctk->info)) ctk = ctype_child(cts, ctk);

143. if (ctype_isinteger(ctk->info)) {

144. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ctk,

145. (uint8_t *)&idx, cdataptr(cdk), 0);

146. goto integer_key;

147. }

148. } else if (tvisstr(key)) {  /* String key. */

149. GCstr *name = strV(key);

150. if (ctype_isstruct(ct->info)) {

151. CTSize ofs;

152. CType *fct = lj_ctype_getfieldq(cts, ct, name, &ofs, qual);

153. if (fct) {

154. *pp = p + ofs;

155. return fct;

156. }

157. } else if (ctype_iscomplex(ct->info)) {

158. if (name->len == 2) {

159. *qual |= CTF_CONST;  /* Complex fields are constant. */

160. if (strdata(name)[0] == 'r' && strdata(name)[1] == 'e') {

161. *pp = p;

162. return ct;

163. } else if (strdata(name)[0] == 'i' && strdata(name)[1] == 'm') {

164. *pp = p + (ct->size >> 1);

165. return ct;

166. }

167. }

168. } else if (cd->ctypeid == CTID_CTYPEID) {

169. /* Allow indexing a (pointer to) struct constructor to get constants. */

170. CType *sct = ctype_raw(cts, *(CTypeID *)p);

171. if (ctype_isptr(sct->info))

172. sct = ctype_rawchild(cts, sct);

173. if (ctype_isstruct(sct->info)) {

174. CTSize ofs;

175. CType *fct = lj_ctype_getfield(cts, sct, name, &ofs);

176. if (fct && ctype_isconstval(fct->info))

177. return fct;

178. }

179. ct = sct;  /* Allow resolving metamethods for constructors, too. */

180. }

181. }

182. if (ctype_isptr(ct->info)) {  /* Automatically perform '->'. */

183. if (ctype_isstruct(ctype_rawchild(cts, ct)->info)) {

184. p = (uint8_t *)cdata_getptr(p, ct->size);

185. ct = ctype_child(cts, ct);

186. goto collect_attrib;

187. }

188. }

189. *qual |= 1;  /* Lookup failed. */

190. return ct;  /* But return the resolved raw type. */

191. }

193. /* -- C data getters ------------------------------------------------------ */

195. /* Get constant value and convert to TValue. */

196. ‌static void cdata_getconst(CTState *cts, TValue *o, CType *ct)

197. {

198. CType *ctt = ctype_child(cts, ct);

199. lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4);

200. /* Constants are already zero-extended/sign-extended to 32 bits. */

201. if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0)

202. setnumV(o, (lua_Number)(uint32_t)ct->size);

203. else

204. setintV(o, (int32_t)ct->size);

205. }

207. /* Get C data value and convert to TValue. */

208. ‌int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp)

209. {

210. CTypeID sid;

212. if (ctype_isconstval(s->info)) {

213. cdata_getconst(cts, o, s);

214. return 0;  /* No GC step needed. */

215. } else if (ctype_isbitfield(s->info)) {

216. return lj_cconv_tv_bf(cts, s, o, sp);

217. }

219. /* Get child type of pointer/array/field. */

220. lua_assert(ctype_ispointer(s->info) || ctype_isfield(s->info));

221. sid = ctype_cid(s->info);

222. s = ctype_get(cts, sid);

224. /* Resolve reference for field. */

225. if (ctype_isref(s->info)) {

226. lua_assert(s->size == CTSIZE_PTR);

227. sp = *(uint8_t **)sp;

228. sid = ctype_cid(s->info);

229. s = ctype_get(cts, sid);

230. }

232. /* Skip attributes. */

233. while (ctype_isattrib(s->info))

234. s = ctype_child(cts, s);

236. return lj_cconv_tv_ct(cts, s, sid, o, sp);

237. }

239. /* -- C data setters ------------------------------------------------------ */

241. /* Convert TValue and set C data value. */

242. ‌void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, CTInfo qual)

243. {

244. if (ctype_isconstval(d->info)) {

245. goto err_const;

246. } else if (ctype_isbitfield(d->info)) {

247. if (((d->info|qual) & CTF_CONST)) goto err_const;

248. lj_cconv_bf_tv(cts, d, dp, o);

249. return;

250. }

252. /* Get child type of pointer/array/field. */

253. lua_assert(ctype_ispointer(d->info) || ctype_isfield(d->info));

254. d = ctype_child(cts, d);

256. /* Resolve reference for field. */

257. if (ctype_isref(d->info)) {

258. lua_assert(d->size == CTSIZE_PTR);

259. dp = *(uint8_t **)dp;

260. d = ctype_child(cts, d);

261. }

263. /* Skip attributes and collect qualifiers. */

264. for (;;) {

265. if (ctype_isattrib(d->info)) {

266. if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size;

267. } else {

268. break;

269. }

270. d = ctype_child(cts, d);

271. }

273. lua_assert(ctype_hassize(d->info) && !ctype_isvoid(d->info));

275. if (((d->info|qual) & CTF_CONST)) {

276. err_const:

277. lj_err_caller(cts->L, LJ_ERR_FFI_WRCONST);

278. }

280. lj_cconv_ct_tv(cts, d, dp, o, 0);

281. }

283. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_cconv.c - luajit-2.0-src

Functions defined

- cconv_array_init

- cconv_array_tab

- cconv_childqual

- cconv_err_conv

- cconv_err_convtv

- cconv_err_initov

- cconv_struct_init

- cconv_struct_tab

- cconv_substruct_init

- cconv_substruct_tab

- lj_cconv_bf_tv

- lj_cconv_compatptr

- lj_cconv_ct_ct

- lj_cconv_ct_init

- lj_cconv_ct_tv

- lj_cconv_multi_init

- lj_cconv_tv_bf

- lj_cconv_tv_ct

Source code

1. /*

2. ** C type conversions.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "lj_obj.h"

8. #if LJ_HASFFI

10. #include "lj_err.h"

11. #include "lj_tab.h"

12. #include "lj_ctype.h"

13. #include "lj_cdata.h"

14. #include "lj_cconv.h"

15. #include "lj_ccallback.h"

17. /* -- Conversion errors --------------------------------------------------- */

19. /* Bad conversion. */

20. ‌LJ_NORET static void cconv_err_conv(CTState *cts, CType *d, CType *s,

21. CTInfo flags)

22. {

23. const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));

24. const char *src;

25. if ((flags & CCF_FROMTV))

26. src = lj_obj_typename[1+(ctype_isnum(s->info) ? LUA_TNUMBER :

27. ctype_isarray(s->info) ? LUA_TSTRING : LUA_TNIL)];

28. else

29. src = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, s), NULL));

30. if (CCF_GETARG(flags))

31. lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst);

32. else

33. lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst);

34. }

36. /* Bad conversion from TValue. */

37. ‌LJ_NORET static void cconv_err_convtv(CTState *cts, CType *d, TValue *o,

38. CTInfo flags)

39. {

40. const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));

41. const char *src = lj_typename(o);

42. if (CCF_GETARG(flags))

43. lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst);

44. else

45. lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst);

46. }

48. /* Initializer overflow. */

49. ‌LJ_NORET static void cconv_err_initov(CTState *cts, CType *d)

50. {

51. const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));

52. lj_err_callerv(cts->L, LJ_ERR_FFI_INITOV, dst);

53. }

55. /* -- C type compatibility checks ----------------------------------------- */

57. /* Get raw type and qualifiers for a child type. Resolves enums, too. */

58. ‌static CType *cconv_childqual(CTState *cts, CType *ct, CTInfo *qual)

59. {

60. ct = ctype_child(cts, ct);

61. for (;;) {

62. if (ctype_isattrib(ct->info)) {

63. if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size;

64. } else if (!ctype_isenum(ct->info)) {

65. break;

66. }

67. ct = ctype_child(cts, ct);

68. }

69. *qual |= (ct->info & CTF_QUAL);

70. return ct;

71. }

73. /* Check for compatible types when converting to a pointer.

74. ** Note: these checks are more relaxed than what C99 mandates.

75. */

76. ‌int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags)

77. {

78. if (!((flags & CCF_CAST) || d == s)) {

79. CTInfo dqual = 0, squal = 0;

80. d = cconv_childqual(cts, d, &dqual);

81. if (!ctype_isstruct(s->info))

82. s = cconv_childqual(cts, s, &squal);

83. if ((flags & CCF_SAME)) {

84. if (dqual != squal)

85. return 0;  /* Different qualifiers. */

86. } else if (!(flags & CCF_IGNQUAL)) {

87. if ((dqual & squal) != squal)

88. return 0;  /* Discarded qualifiers. */

89. if (ctype_isvoid(d->info) || ctype_isvoid(s->info))

90. return 1;  /* Converting to/from void * is always ok. */

91. }

92. if (ctype_type(d->info) != ctype_type(s->info) ||

93. d->size != s->size)

94. return 0;  /* Different type or different size. */

95. if (ctype_isnum(d->info)) {

96. if (((d->info ^ s->info) & (CTF_BOOL|CTF_FP)))

97. return 0;  /* Different numeric types. */

98. } else if (ctype_ispointer(d->info)) {

99. /* Check child types for compatibility. */

100. return lj_cconv_compatptr(cts, d, s, flags|CCF_SAME);

101. } else if (ctype_isstruct(d->info)) {

102. if (d != s)

103. return 0;  /* Must be exact same type for struct/union. */

104. } else if (ctype_isfunc(d->info)) {

105. /* NYI: structural equality of functions. */

106. }

107. }

108. return 1;  /* Types are compatible. */

109. }

111. /* -- C type to C type conversion ----------------------------------------- */

113. /* Convert C type to C type. Caveat: expects to get the raw CType!

114. **

115. ** Note: This is only used by the interpreter and not optimized at all.

116. ** The JIT compiler will do a much better job specializing for each case.

117. */

118. ‌void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s,

119. uint8_t *dp, uint8_t *sp, CTInfo flags)

120. {

121. CTSize dsize = d->size, ssize = s->size;

122. CTInfo dinfo = d->info, sinfo = s->info;

123. void *tmpptr;

125. lua_assert(!ctype_isenum(dinfo) && !ctype_isenum(sinfo));

126. lua_assert(!ctype_isattrib(dinfo) && !ctype_isattrib(sinfo));

128. if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)

129. goto err_conv;

131. /* Some basic sanity checks. */

132. lua_assert(!ctype_isnum(dinfo) || dsize > 0);

133. lua_assert(!ctype_isnum(sinfo) || ssize > 0);

134. lua_assert(!ctype_isbool(dinfo) || dsize == 1 || dsize == 4);

135. lua_assert(!ctype_isbool(sinfo) || ssize == 1 || ssize == 4);

136. lua_assert(!ctype_isinteger(dinfo) || (1u<<lj_fls(dsize)) == dsize);

137. lua_assert(!ctype_isinteger(sinfo) || (1u<<lj_fls(ssize)) == ssize);

139. switch (cconv_idx2(dinfo, sinfo)) {

140. /* Destination is a bool. */

141. case CCX(B, B):

142. /* Source operand is already normalized. */

143. if (dsize == 1) *dp = *sp; else *(int *)dp = *sp;

144. break;

145. case CCX(B, I): {

146. MSize i;

147. uint8_t b = 0;

148. for (i = 0; i < ssize; i++) b |= sp[i];

149. b = (b != 0);

150. if (dsize == 1) *dp = b; else *(int *)dp = b;

151. break;

152. }

153. case CCX(B, F): {

154. uint8_t b;

155. if (ssize == sizeof(double)) b = (*(double *)sp != 0);

156. else if (ssize == sizeof(float)) b = (*(float *)sp != 0);

157. else goto err_conv;  /* NYI: long double. */

158. if (dsize == 1) *dp = b; else *(int *)dp = b;

159. break;

160. }

162. /* Destination is an integer. */

163. case CCX(I, B):

164. case CCX(I, I):

165. conv_I_I:

166. if (dsize > ssize) {  /* Zero-extend or sign-extend LSB. */

167. #if LJ_LE

168. uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[ssize-1]&0x80)) ? 0xff : 0;

169. memcpy(dp, sp, ssize);

170. memset(dp + ssize, fill, dsize-ssize);

171. #else

172. uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[0]&0x80)) ? 0xff : 0;

173. memset(dp, fill, dsize-ssize);

174. memcpy(dp + (dsize-ssize), sp, ssize);

175. #endif

176. } else {  /* Copy LSB. */

177. #if LJ_LE

178. memcpy(dp, sp, dsize);

179. #else

180. memcpy(dp, sp + (ssize-dsize), dsize);

181. #endif

182. }

183. break;

184. case CCX(I, F): {

185. double n;  /* Always convert via double. */

186. conv_I_F:

187. /* Convert source to double. */

188. if (ssize == sizeof(double)) n = *(double *)sp;

189. else if (ssize == sizeof(float)) n = (double)*(float *)sp;

190. else goto err_conv;  /* NYI: long double. */

191. /* Then convert double to integer. */

192. /* The conversion must exactly match the semantics of JIT-compiled code! */

193. if (dsize < 4 || (dsize == 4 && !(dinfo & CTF_UNSIGNED))) {

194. int32_t i = (int32_t)n;

195. if (dsize == 4) *(int32_t *)dp = i;

196. else if (dsize == 2) *(int16_t *)dp = (int16_t)i;

197. else *(int8_t *)dp = (int8_t)i;

198. } else if (dsize == 4) {

199. *(uint32_t *)dp = (uint32_t)n;

200. } else if (dsize == 8) {

201. if (!(dinfo & CTF_UNSIGNED))

202. *(int64_t *)dp = (int64_t)n;

203. else

204. *(uint64_t *)dp = lj_num2u64(n);

205. } else {

206. goto err_conv;  /* NYI: conversion to >64 bit integers. */

207. }

208. break;

209. }

210. case CCX(I, C):

211. s = ctype_child(cts, s);

212. sinfo = s->info;

213. ssize = s->size;

214. goto conv_I_F;  /* Just convert re. */

215. case CCX(I, P):

216. if (!(flags & CCF_CAST)) goto err_conv;

217. sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);

218. goto conv_I_I;

219. case CCX(I, A):

220. if (!(flags & CCF_CAST)) goto err_conv;

221. sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);

222. ssize = CTSIZE_PTR;

223. tmpptr = sp;

224. sp = (uint8_t *)&tmpptr;

225. goto conv_I_I;

227. /* Destination is a floating-point number. */

228. case CCX(F, B):

229. case CCX(F, I): {

230. double n;  /* Always convert via double. */

231. conv_F_I:

232. /* First convert source to double. */

233. /* The conversion must exactly match the semantics of JIT-compiled code! */

234. if (ssize < 4 || (ssize == 4 && !(sinfo & CTF_UNSIGNED))) {

235. int32_t i;

236. if (ssize == 4) {

237. i = *(int32_t *)sp;

238. } else if (!(sinfo & CTF_UNSIGNED)) {

239. if (ssize == 2) i = *(int16_t *)sp;

240. else i = *(int8_t *)sp;

241. } else {

242. if (ssize == 2) i = *(uint16_t *)sp;

243. else i = *(uint8_t *)sp;

244. }

245. n = (double)i;

246. } else if (ssize == 4) {

247. n = (double)*(uint32_t *)sp;

248. } else if (ssize == 8) {

249. if (!(sinfo & CTF_UNSIGNED)) n = (double)*(int64_t *)sp;

250. else n = (double)*(uint64_t *)sp;

251. } else {

252. goto err_conv;  /* NYI: conversion from >64 bit integers. */

253. }

254. /* Convert double to destination. */

255. if (dsize == sizeof(double)) *(double *)dp = n;

256. else if (dsize == sizeof(float)) *(float *)dp = (float)n;

257. else goto err_conv;  /* NYI: long double. */

258. break;

259. }

260. case CCX(F, F): {

261. double n;  /* Always convert via double. */

262. conv_F_F:

263. if (ssize == dsize) goto copyval;

264. /* Convert source to double. */

265. if (ssize == sizeof(double)) n = *(double *)sp;

266. else if (ssize == sizeof(float)) n = (double)*(float *)sp;

267. else goto err_conv;  /* NYI: long double. */

268. /* Convert double to destination. */

269. if (dsize == sizeof(double)) *(double *)dp = n;

270. else if (dsize == sizeof(float)) *(float *)dp = (float)n;

271. else goto err_conv;  /* NYI: long double. */

272. break;

273. }

274. case CCX(F, C):

275. s = ctype_child(cts, s);

276. sinfo = s->info;

277. ssize = s->size;

278. goto conv_F_F;  /* Ignore im, and convert from re. */

280. /* Destination is a complex number. */

281. case CCX(C, I):

282. d = ctype_child(cts, d);

283. dinfo = d->info;

284. dsize = d->size;

285. memset(dp + dsize, 0, dsize);  /* Clear im. */

286. goto conv_F_I;  /* Convert to re. */

287. case CCX(C, F):

288. d = ctype_child(cts, d);

289. dinfo = d->info;

290. dsize = d->size;

291. memset(dp + dsize, 0, dsize);  /* Clear im. */

292. goto conv_F_F;  /* Convert to re. */

294. case CCX(C, C):

295. if (dsize != ssize) {  /* Different types: convert re/im separately. */

296. CType *dc = ctype_child(cts, d);

297. CType *sc = ctype_child(cts, s);

298. lj_cconv_ct_ct(cts, dc, sc, dp, sp, flags);

299. lj_cconv_ct_ct(cts, dc, sc, dp + dc->size, sp + sc->size, flags);

300. return;

301. }

302. goto copyval;  /* Otherwise this is easy. */

304. /* Destination is a vector. */

305. case CCX(V, I):

306. case CCX(V, F):

307. case CCX(V, C): {

308. CType *dc = ctype_child(cts, d);

309. CTSize esize;

310. /* First convert the scalar to the first element. */

311. lj_cconv_ct_ct(cts, dc, s, dp, sp, flags);

312. /* Then replicate it to the other elements (splat). */

313. for (sp = dp, esize = dc->size; dsize > esize; dsize -= esize) {

314. dp += esize;

315. memcpy(dp, sp, esize);

316. }

317. break;

318. }

320. case CCX(V, V):

321. /* Copy same-sized vectors, even for different lengths/element-types. */

322. if (dsize != ssize) goto err_conv;

323. goto copyval;

325. /* Destination is a pointer. */

326. case CCX(P, I):

327. if (!(flags & CCF_CAST)) goto err_conv;

328. dinfo = CTINFO(CT_NUM, CTF_UNSIGNED);

329. goto conv_I_I;

331. case CCX(P, F):

332. if (!(flags & CCF_CAST) || !(flags & CCF_FROMTV)) goto err_conv;

333. /* The signed conversion is cheaper. x64 really has 47 bit pointers. */

334. dinfo = CTINFO(CT_NUM, (LJ_64 && dsize == 8) ? 0 : CTF_UNSIGNED);

335. goto conv_I_F;

337. case CCX(P, P):

338. if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv;

339. cdata_setptr(dp, dsize, cdata_getptr(sp, ssize));

340. break;

342. case CCX(P, A):

343. case CCX(P, S):

344. if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv;

345. cdata_setptr(dp, dsize, sp);

346. break;

348. /* Destination is an array. */

349. case CCX(A, A):

350. if ((flags & CCF_CAST) || (d->info & CTF_VLA) || dsize != ssize ||

351. d->size == CTSIZE_INVALID || !lj_cconv_compatptr(cts, d, s, flags))

352. goto err_conv;

353. goto copyval;

355. /* Destination is a struct/union. */

356. case CCX(S, S):

357. if ((flags & CCF_CAST) || (d->info & CTF_VLA) || d != s)

358. goto err_conv;  /* Must be exact same type. */

359. copyval:  /* Copy value. */

360. lua_assert(dsize == ssize);

361. memcpy(dp, sp, dsize);

362. break;

364. default:

365. err_conv:

366. cconv_err_conv(cts, d, s, flags);

367. }

368. }

370. /* -- C type to TValue conversion ----------------------------------------- */

372. /* Convert C type to TValue. Caveat: expects to get the raw CType! */

373. ‌int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid,

374. TValue *o, uint8_t *sp)

375. {

376. CTInfo sinfo = s->info;

377. if (ctype_isnum(sinfo)) {

378. if (!ctype_isbool(sinfo)) {

379. if (ctype_isinteger(sinfo) && s->size > 4) goto copyval;

380. if (LJ_DUALNUM && ctype_isinteger(sinfo)) {

381. int32_t i;

382. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT32), s,

383. (uint8_t *)&i, sp, 0);

384. if ((sinfo & CTF_UNSIGNED) && i < 0)

385. setnumV(o, (lua_Number)(uint32_t)i);

386. else

387. setintV(o, i);

388. } else {

389. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_DOUBLE), s,

390. (uint8_t *)&o->n, sp, 0);

391. /* Numbers are NOT canonicalized here! Beware of uninitialized data. */

392. lua_assert(tvisnum(o));

393. }

394. } else {

395. uint32_t b = s->size == 1 ? (*sp != 0) : (*(int *)sp != 0);

396. setboolV(o, b);

397. setboolV(&cts->g->tmptv2, b);  /* Remember for trace recorder. */

398. }

399. return 0;

400. } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {

401. /* Create reference. */

402. setcdataV(cts->L, o, lj_cdata_newref(cts, sp, sid));

403. return 1;  /* Need GC step. */

404. } else {

405. GCcdata *cd;

406. CTSize sz;

407. copyval:  /* Copy value. */

408. sz = s->size;

409. lua_assert(sz != CTSIZE_INVALID);

410. /* Attributes are stripped, qualifiers are kept (but mostly ignored). */

411. cd = lj_cdata_new(cts, ctype_typeid(cts, s), sz);

412. setcdataV(cts->L, o, cd);

413. memcpy(cdataptr(cd), sp, sz);

414. return 1;  /* Need GC step. */

415. }

416. }

418. /* Convert bitfield to TValue. */

419. ‌int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp)

420. {

421. CTInfo info = s->info;

422. CTSize pos, bsz;

423. uint32_t val;

424. lua_assert(ctype_isbitfield(info));

425. /* NYI: packed bitfields may cause misaligned reads. */

426. switch (ctype_bitcsz(info)) {

427. case 4: val = *(uint32_t *)sp; break;

428. case 2: val = *(uint16_t *)sp; break;

429. case 1: val = *(uint8_t *)sp; break;

430. default: lua_assert(0); val = 0; break;

431. }

432. /* Check if a packed bitfield crosses a container boundary. */

433. pos = ctype_bitpos(info);

434. bsz = ctype_bitbsz(info);

435. lua_assert(pos < 8*ctype_bitcsz(info));

436. lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info));

437. if (pos + bsz > 8*ctype_bitcsz(info))

438. lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT);

439. if (!(info & CTF_BOOL)) {

440. CTSize shift = 32 - bsz;

441. if (!(info & CTF_UNSIGNED)) {

442. setintV(o, (int32_t)(val << (shift-pos)) >> shift);

443. } else {

444. val = (val << (shift-pos)) >> shift;

445. if (!LJ_DUALNUM || (int32_t)val < 0)

446. setnumV(o, (lua_Number)(uint32_t)val);

447. else

448. setintV(o, (int32_t)val);

449. }

450. } else {

451. lua_assert(bsz == 1);

452. setboolV(o, (val >> pos) & 1);

453. }

454. return 0;  /* No GC step needed. */

455. }

457. /* -- TValue to C type conversion ----------------------------------------- */

459. /* Convert table to array. */

460. ‌static void cconv_array_tab(CTState *cts, CType *d,

461. uint8_t *dp, GCtab *t, CTInfo flags)

462. {

463. int32_t i;

464. CType *dc = ctype_rawchild(cts, d);  /* Array element type. */

465. CTSize size = d->size, esize = dc->size, ofs = 0;

466. for (i = 0; ; i++) {

467. TValue *tv = (TValue *)lj_tab_getint(t, i);

468. if (!tv || tvisnil(tv)) {

469. if (i == 0) continue;  /* Try again for 1-based tables. */

470. break;  /* Stop at first nil. */

471. }

472. if (ofs >= size)

473. cconv_err_initov(cts, d);

474. lj_cconv_ct_tv(cts, dc, dp + ofs, tv, flags);

475. ofs += esize;

476. }

477. if (size != CTSIZE_INVALID) {  /* Only fill up arrays with known size. */

478. if (ofs == esize) {  /* Replicate a single element. */

479. for (; ofs < size; ofs += esize) memcpy(dp + ofs, dp, esize);

480. } else {  /* Otherwise fill the remainder with zero. */

481. memset(dp + ofs, 0, size - ofs);

482. }

483. }

484. }

486. /* Convert table to sub-struct/union. */

487. ‌static void cconv_substruct_tab(CTState *cts, CType *d, uint8_t *dp,

488. GCtab *t, int32_t *ip, CTInfo flags)

489. {

490. CTypeID id = d->sib;

491. while (id) {

492. CType *df = ctype_get(cts, id);

493. id = df->sib;

494. if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) {

495. TValue *tv;

496. int32_t i = *ip, iz = i;

497. if (!gcref(df->name)) continue;  /* Ignore unnamed fields. */

498. if (i >= 0) {

499. retry:

500. tv = (TValue *)lj_tab_getint(t, i);

501. if (!tv || tvisnil(tv)) {

502. if (i == 0) { i = 1; goto retry; }  /* 1-based tables. */

503. if (iz == 0) { *ip = i = -1; goto tryname; }  /* Init named fields. */

504. break;  /* Stop at first nil. */

505. }

506. *ip = i + 1;

507. } else {

508. tryname:

509. tv = (TValue *)lj_tab_getstr(t, gco2str(gcref(df->name)));

510. if (!tv || tvisnil(tv)) continue;

511. }

512. if (ctype_isfield(df->info))

513. lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, tv, flags);

514. else

515. lj_cconv_bf_tv(cts, df, dp+df->size, tv);

516. if ((d->info & CTF_UNION)) break;

517. } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) {

518. cconv_substruct_tab(cts, ctype_rawchild(cts, df),

519. dp+df->size, t, ip, flags);

520. }  /* Ignore all other entries in the chain. */

521. }

522. }

524. /* Convert table to struct/union. */

525. ‌static void cconv_struct_tab(CTState *cts, CType *d,

526. uint8_t *dp, GCtab *t, CTInfo flags)

527. {

528. int32_t i = 0;

529. memset(dp, 0, d->size);  /* Much simpler to clear the struct first. */

530. cconv_substruct_tab(cts, d, dp, t, &i, flags);

531. }

533. /* Convert TValue to C type. Caveat: expects to get the raw CType! */

534. ‌void lj_cconv_ct_tv(CTState *cts, CType *d,

535. uint8_t *dp, TValue *o, CTInfo flags)

536. {

537. CTypeID sid = CTID_P_VOID;

538. CType *s;

539. void *tmpptr;

540. uint8_t tmpbool, *sp = (uint8_t *)&tmpptr;

541. if (LJ_LIKELY(tvisint(o))) {

542. sp = (uint8_t *)&o->i;

543. sid = CTID_INT32;

544. flags |= CCF_FROMTV;

545. } else if (LJ_LIKELY(tvisnum(o))) {

546. sp = (uint8_t *)&o->n;

547. sid = CTID_DOUBLE;

548. flags |= CCF_FROMTV;

549. } else if (tviscdata(o)) {

550. sp = cdataptr(cdataV(o));

551. sid = cdataV(o)->ctypeid;

552. s = ctype_get(cts, sid);

553. if (ctype_isref(s->info)) {  /* Resolve reference for value. */

554. lua_assert(s->size == CTSIZE_PTR);

555. sp = *(void **)sp;

556. sid = ctype_cid(s->info);

557. }

558. s = ctype_raw(cts, sid);

559. if (ctype_isfunc(s->info)) {

560. sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR);

561. } else {

562. if (ctype_isenum(s->info)) s = ctype_child(cts, s);

563. goto doconv;

564. }

565. } else if (tvisstr(o)) {

566. GCstr *str = strV(o);

567. if (ctype_isenum(d->info)) {  /* Match string against enum constant. */

568. CTSize ofs;

569. CType *cct = lj_ctype_getfield(cts, d, str, &ofs);

570. if (!cct || !ctype_isconstval(cct->info))

571. goto err_conv;

572. lua_assert(d->size == 4);

573. sp = (uint8_t *)&cct->size;

574. sid = ctype_cid(cct->info);

575. } else if (ctype_isrefarray(d->info)) {  /* Copy string to array. */

576. CType *dc = ctype_rawchild(cts, d);

577. CTSize sz = str->len+1;

578. if (!ctype_isinteger(dc->info) || dc->size != 1)

579. goto err_conv;

580. if (d->size != 0 && d->size < sz)

581. sz = d->size;

582. memcpy(dp, strdata(str), sz);

583. return;

584. } else {  /* Otherwise pass it as a const char[]. */

585. sp = (uint8_t *)strdata(str);

586. sid = CTID_A_CCHAR;

587. flags |= CCF_FROMTV;

588. }

589. } else if (tvistab(o)) {

590. if (ctype_isarray(d->info)) {

591. cconv_array_tab(cts, d, dp, tabV(o), flags);

592. return;

593. } else if (ctype_isstruct(d->info)) {

594. cconv_struct_tab(cts, d, dp, tabV(o), flags);

595. return;

596. } else {

597. goto err_conv;

598. }

599. } else if (tvisbool(o)) {

600. tmpbool = boolV(o);

601. sp = &tmpbool;

602. sid = CTID_BOOL;

603. } else if (tvisnil(o)) {

604. tmpptr = (void *)0;

605. flags |= CCF_FROMTV;

606. } else if (tvisudata(o)) {

607. GCudata *ud = udataV(o);

608. tmpptr = uddata(ud);

609. if (ud->udtype == UDTYPE_IO_FILE)

610. tmpptr = *(void **)tmpptr;

611. } else if (tvislightud(o)) {

612. tmpptr = lightudV(o);

613. } else if (tvisfunc(o)) {

614. void *p = lj_ccallback_new(cts, d, funcV(o));

615. if (p) {

616. *(void **)dp = p;

617. return;

618. }

619. goto err_conv;

620. } else {

621. err_conv:

622. cconv_err_convtv(cts, d, o, flags);

623. }

624. s = ctype_get(cts, sid);

625. doconv:

626. if (ctype_isenum(d->info)) d = ctype_child(cts, d);

627. lj_cconv_ct_ct(cts, d, s, dp, sp, flags);

628. }

630. /* Convert TValue to bitfield. */

631. ‌void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o)

632. {

633. CTInfo info = d->info;

634. CTSize pos, bsz;

635. uint32_t val, mask;

636. lua_assert(ctype_isbitfield(info));

637. if ((info & CTF_BOOL)) {

638. uint8_t tmpbool;

639. lua_assert(ctype_bitbsz(info) == 1);

640. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_BOOL), &tmpbool, o, 0);

641. val = tmpbool;

642. } else {

643. CTypeID did = (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32;

644. lj_cconv_ct_tv(cts, ctype_get(cts, did), (uint8_t *)&val, o, 0);

645. }

646. pos = ctype_bitpos(info);

647. bsz = ctype_bitbsz(info);

648. lua_assert(pos < 8*ctype_bitcsz(info));

649. lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info));

650. /* Check if a packed bitfield crosses a container boundary. */

651. if (pos + bsz > 8*ctype_bitcsz(info))

652. lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT);

653. mask = ((1u << bsz) - 1u) << pos;

654. val = (val << pos) & mask;

655. /* NYI: packed bitfields may cause misaligned reads/writes. */

656. switch (ctype_bitcsz(info)) {

657. case 4: *(uint32_t *)dp = (*(uint32_t *)dp & ~mask) | (uint32_t)val; break;

658. case 2: *(uint16_t *)dp = (*(uint16_t *)dp & ~mask) | (uint16_t)val; break;

659. case 1: *(uint8_t *)dp = (*(uint8_t *)dp & ~mask) | (uint8_t)val; break;

660. default: lua_assert(0); break;

661. }

662. }

664. /* -- Initialize C type with TValues -------------------------------------- */

666. /* Initialize an array with TValues. */

667. ‌static void cconv_array_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,

668. TValue *o, MSize len)

669. {

670. CType *dc = ctype_rawchild(cts, d);  /* Array element type. */

671. CTSize ofs, esize = dc->size;

672. MSize i;

673. if (len*esize > sz)

674. cconv_err_initov(cts, d);

675. for (i = 0, ofs = 0; i < len; i++, ofs += esize)

676. lj_cconv_ct_tv(cts, dc, dp + ofs, o + i, 0);

677. if (ofs == esize) {  /* Replicate a single element. */

678. for (; ofs < sz; ofs += esize) memcpy(dp + ofs, dp, esize);

679. } else {  /* Otherwise fill the remainder with zero. */

680. memset(dp + ofs, 0, sz - ofs);

681. }

682. }

684. /* Initialize a sub-struct/union with TValues. */

685. ‌static void cconv_substruct_init(CTState *cts, CType *d, uint8_t *dp,

686. TValue *o, MSize len, MSize *ip)

687. {

688. CTypeID id = d->sib;

689. while (id) {

690. CType *df = ctype_get(cts, id);

691. id = df->sib;

692. if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) {

693. MSize i = *ip;

694. if (!gcref(df->name)) continue;  /* Ignore unnamed fields. */

695. if (i >= len) break;

696. *ip = i + 1;

697. if (ctype_isfield(df->info))

698. lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, o + i, 0);

699. else

700. lj_cconv_bf_tv(cts, df, dp+df->size, o + i);

701. if ((d->info & CTF_UNION)) break;

702. } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) {

703. cconv_substruct_init(cts, ctype_rawchild(cts, df),

704. dp+df->size, o, len, ip);

705. if ((d->info & CTF_UNION)) break;

706. }  /* Ignore all other entries in the chain. */

707. }

708. }

710. /* Initialize a struct/union with TValues. */

711. ‌static void cconv_struct_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,

712. TValue *o, MSize len)

713. {

714. MSize i = 0;

715. memset(dp, 0, sz);  /* Much simpler to clear the struct first. */

716. cconv_substruct_init(cts, d, dp, o, len, &i);

717. if (i < len)

718. cconv_err_initov(cts, d);

719. }

721. /* Check whether to use a multi-value initializer.

722. ** This is true if an aggregate is to be initialized with a value.

723. ** Valarrays are treated as values here so ct_tv handles (V|C, I|F).

724. */

725. ‌int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o)

726. {

727. if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info)))

728. return 0;  /* Destination is not an aggregate. */

729. if (tvistab(o) || (tvisstr(o) && !ctype_isstruct(d->info)))

730. return 0;  /* Initializer is not a value. */

731. if (tviscdata(o) && lj_ctype_rawref(cts, cdataV(o)->ctypeid) == d)

732. return 0;  /* Source and destination are identical aggregates. */

733. return 1;  /* Otherwise the initializer is a value. */

734. }

736. /* Initialize C type with TValues. Caveat: expects to get the raw CType! */

737. ‌void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,

738. uint8_t *dp, TValue *o, MSize len)

739. {

740. if (len == 0)

741. memset(dp, 0, sz);

742. else if (len == 1 && !lj_cconv_multi_init(cts, d, o))

743. lj_cconv_ct_tv(cts, d, dp, o, 0);

744. else if (ctype_isarray(d->info))  /* Also handles valarray init with len>1. */

745. cconv_array_init(cts, d, sz, dp, o, len);

746. else if (ctype_isstruct(d->info))

747. cconv_struct_init(cts, d, sz, dp, o, len);

748. else

749. cconv_err_initov(cts, d);

750. }

752. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_cconv.h - luajit-2.0-src

Functions defined

- cconv_idx

Macros defined

- CCF_ARG

- CCF_ARG_SHIFT

- CCF_CAST

- CCF_FROMTV

- CCF_GETARG

- CCF_IGNQUAL

- CCF_SAME

- CCX

- _LJ_CCONV_H

- cconv_idx2

Source code

1. /*

2. ** C type conversions.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_CCONV_H

7. ‌#define _LJ_CCONV_H

9. #include "lj_obj.h"

10. #include "lj_ctype.h"

12. #if LJ_HASFFI

14. /* Compressed C type index. ORDER CCX. */

15. enum {

16. CCX_B,        /* Bool. */

17. CCX_I,        /* Integer. */

18. CCX_F,        /* Floating-point number. */

19. CCX_C,        /* Complex. */

20. CCX_V,        /* Vector. */

21. CCX_P,        /* Pointer. */

22. CCX_A,        /* Refarray. */

23. CCX_S                /* Struct/union. */

24. };

26. /* Convert C type info to compressed C type index. ORDER CT. ORDER CCX. */

27. ‌static LJ_AINLINE uint32_t cconv_idx(CTInfo info)

28. {

29. uint32_t idx = ((info >> 26) & 15u);  /* Dispatch bits. */

30. lua_assert(ctype_type(info) <= CT_MAYCONVERT);

31. #if LJ_64

32. idx = ((uint32_t)(U64x(f436fff5,fff7f021) >> 4*idx) & 15u);

33. #else

34. idx = (((idx < 8 ? 0xfff7f021u : 0xf436fff5) >> 4*(idx & 7u)) & 15u);

35. #endif

36. lua_assert(idx < 8);

37. return idx;

38. }

40. ‌#define cconv_idx2(dinfo, sinfo) \

41. ((cconv_idx((dinfo)) << 3) + cconv_idx((sinfo)))

43. ‌#define CCX(dst, src)                ((CCX_##dst << 3) + CCX_##src)

45. /* Conversion flags. */

46. ‌#define CCF_CAST        0x00000001u

47. ‌#define CCF_FROMTV        0x00000002u

48. ‌#define CCF_SAME        0x00000004u

49. ‌#define CCF_IGNQUAL        0x00000008u

51. ‌#define CCF_ARG_SHIFT        8

52. ‌#define CCF_ARG(n)        ((n) << CCF_ARG_SHIFT)

53. ‌#define CCF_GETARG(f)        ((f) >> CCF_ARG_SHIFT)

55. LJ_FUNC int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags);

56. LJ_FUNC void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s,

57. uint8_t *dp, uint8_t *sp, CTInfo flags);

58. LJ_FUNC int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid,

59. TValue *o, uint8_t *sp);

60. LJ_FUNC int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp);

61. LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d,

62. uint8_t *dp, TValue *o, CTInfo flags);

63. LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o);

64. LJ_FUNC int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o);

65. LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,

66. uint8_t *dp, TValue *o, MSize len);

68. #endif

70. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_udata.c - luajit-2.0-src

Functions defined

- lj_udata_free

- lj_udata_new

Macros defined

- LUA_CORE

- lj_udata_c

Source code

1. /*

2. ** Userdata handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_udata_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #include "lj_gc.h"

11. #include "lj_udata.h"

13. ‌GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env)

14. {

15. GCudata *ud = lj_mem_newt(L, sizeof(GCudata) + sz, GCudata);

16. global_State *g = G(L);

17. newwhite(g, ud);  /* Not finalized. */

18. ud->gct = ~LJ_TUDATA;

19. ud->udtype = UDTYPE_USERDATA;

20. ud->len = sz;

21. /* NOBARRIER: The GCudata is new (marked white). */

22. setgcrefnull(ud->metatable);

23. setgcref(ud->env, obj2gco(env));

24. /* Chain to userdata list (after main thread). */

25. setgcrefr(ud->nextgc, mainthread(g)->nextgc);

26. setgcref(mainthread(g)->nextgc, obj2gco(ud));

27. return ud;

28. }

30. ‌void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud)

31. {

32. lj_mem_free(g, ud, sizeudata(ud));

33. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_debug.c - luajit-2.0-src

Functions defined

- debug_frameline

- debug_framepc

- debug_localname

- debug_putchunkname

- debug_varname

- lj_debug_addloc

- lj_debug_dumpstack

- lj_debug_frame

- lj_debug_funcname

- lj_debug_getinfo

- lj_debug_line

- lj_debug_pushloc

- lj_debug_shortname

- lj_debug_slotname

- lj_debug_uvname

- lj_debug_uvnamev

- luaL_traceback

- lua_getinfo

- lua_getlocal

- lua_getstack

- lua_setlocal

Macros defined

- LUA_CORE

- NO_BCPOS

- TRACEBACK_LEVELS1

- TRACEBACK_LEVELS2

- VARNAMESTR

- VARNAMESTR

- lj_debug_c

Source code

1. /*

2. ** Debugging and introspection.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_debug_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #include "lj_err.h"

11. #include "lj_debug.h"

12. #include "lj_buf.h"

13. #include "lj_tab.h"

14. #include "lj_state.h"

15. #include "lj_frame.h"

16. #include "lj_bc.h"

17. #include "lj_strfmt.h"

18. #if LJ_HASJIT

19. #include "lj_jit.h"

20. #endif

22. /* -- Frames -------------------------------------------------------------- */

24. /* Get frame corresponding to a level. */

25. ‌cTValue *lj_debug_frame(lua_State *L, int level, int *size)

26. {

27. cTValue *frame, *nextframe, *bot = tvref(L->stack)+LJ_FR2;

28. /* Traverse frames backwards. */

29. for (nextframe = frame = L->base-1; frame > bot; ) {

30. if (frame_gc(frame) == obj2gco(L))

31. level++;  /* Skip dummy frames. See lj_err_optype_call(). */

32. if (level-- == 0) {

33. *size = (int)(nextframe - frame);

34. return frame;  /* Level found. */

35. }

36. nextframe = frame;

37. if (frame_islua(frame)) {

38. frame = frame_prevl(frame);

39. } else {

40. if (frame_isvarg(frame))

41. level++;  /* Skip vararg pseudo-frame. */

42. frame = frame_prevd(frame);

43. }

44. }

45. *size = level;

46. return NULL;  /* Level not found. */

47. }

49. /* Invalid bytecode position. */

50. ‌#define NO_BCPOS        (~(BCPos)0)

52. /* Return bytecode position for function/frame or NO_BCPOS. */

53. ‌static BCPos debug_framepc(lua_State *L, GCfunc *fn, cTValue *nextframe)

54. {

55. const BCIns *ins;

56. GCproto *pt;

57. BCPos pos;

58. lua_assert(fn->c.gct == ~LJ_TFUNC || fn->c.gct == ~LJ_TTHREAD);

59. if (!isluafunc(fn)) {  /* Cannot derive a PC for non-Lua functions. */

60. return NO_BCPOS;

61. } else if (nextframe == NULL) {  /* Lua function on top. */

62. void *cf = cframe_raw(L->cframe);

63. if (cf == NULL || (char *)cframe_pc(cf) == (char *)cframe_L(cf))

64. return NO_BCPOS;

65. ins = cframe_pc(cf);  /* Only happens during error/hook handling. */

66. } else {

67. if (frame_islua(nextframe)) {

68. ins = frame_pc(nextframe);

69. } else if (frame_iscont(nextframe)) {

70. ins = frame_contpc(nextframe);

71. } else {

72. /* Lua function below errfunc/gc/hook: find cframe to get the PC. */

73. void *cf = cframe_raw(L->cframe);

74. TValue *f = L->base-1;

75. for (;;) {

76. if (cf == NULL)

77. return NO_BCPOS;

78. while (cframe_nres(cf) < 0) {

79. if (f >= restorestack(L, -cframe_nres(cf)))

80. break;

81. cf = cframe_raw(cframe_prev(cf));

82. if (cf == NULL)

83. return NO_BCPOS;

84. }

85. if (f < nextframe)

86. break;

87. if (frame_islua(f)) {

88. f = frame_prevl(f);

89. } else {

90. if (frame_isc(f) || (frame_iscont(f) && frame_iscont_fficb(f)))

91. cf = cframe_raw(cframe_prev(cf));

92. f = frame_prevd(f);

93. }

94. }

95. ins = cframe_pc(cf);

96. }

97. }

98. pt = funcproto(fn);

99. pos = proto_bcpos(pt, ins) - 1;

100. #if LJ_HASJIT

101. if (pos > pt->sizebc) {  /* Undo the effects of lj_trace_exit for JLOOP. */

102. GCtrace *T = (GCtrace *)((char *)(ins-1) - offsetof(GCtrace, startins));

103. lua_assert(bc_isret(bc_op(ins[-1])));

104. pos = proto_bcpos(pt, mref(T->startpc, const BCIns));

105. }

106. #endif

107. return pos;

108. }

110. /* -- Line numbers -------------------------------------------------------- */

112. /* Get line number for a bytecode position. */

113. ‌BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc)

114. {

115. const void *lineinfo = proto_lineinfo(pt);

116. if (pc <= pt->sizebc && lineinfo) {

117. BCLine first = pt->firstline;

118. if (pc == pt->sizebc) return first + pt->numline;

119. if (pc-- == 0) return first;

120. if (pt->numline < 256)

121. return first + (BCLine)((const uint8_t *)lineinfo)[pc];

122. else if (pt->numline < 65536)

123. return first + (BCLine)((const uint16_t *)lineinfo)[pc];

124. else

125. return first + (BCLine)((const uint32_t *)lineinfo)[pc];

126. }

127. return 0;

128. }

130. /* Get line number for function/frame. */

131. ‌static BCLine debug_frameline(lua_State *L, GCfunc *fn, cTValue *nextframe)

132. {

133. BCPos pc = debug_framepc(L, fn, nextframe);

134. if (pc != NO_BCPOS) {

135. GCproto *pt = funcproto(fn);

136. lua_assert(pc <= pt->sizebc);

137. return lj_debug_line(pt, pc);

138. }

139. return -1;

140. }

142. /* -- Variable names ------------------------------------------------------ */

144. /* Get name of a local variable from slot number and PC. */

145. ‌static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot)

146. {

147. const char *p = (const char *)proto_varinfo(pt);

148. if (p) {

149. BCPos lastpc = 0;

150. for (;;) {

151. const char *name = p;

152. uint32_t vn = *(const uint8_t *)p;

153. BCPos startpc, endpc;

154. if (vn < VARNAME__MAX) {

155. if (vn == VARNAME_END) break;  /* End of varinfo. */

156. } else {

157. do { p++; } while (*(const uint8_t *)p);  /* Skip over variable name. */

158. }

159. p++;

160. lastpc = startpc = lastpc + lj_buf_ruleb128(&p);

161. if (startpc > pc) break;

162. endpc = startpc + lj_buf_ruleb128(&p);

163. if (pc < endpc && slot-- == 0) {

164. if (vn < VARNAME__MAX) {

165. ‌#define VARNAMESTR(name, str)        str "\0"

166. name = VARNAMEDEF(VARNAMESTR);

167. ‌#undef VARNAMESTR

168. if (--vn) while (*name++ || --vn) ;

169. }

170. return name;

171. }

172. }

173. }

174. return NULL;

175. }

177. /* Get name of local variable from 1-based slot number and function/frame. */

178. ‌static TValue *debug_localname(lua_State *L, const lua_Debug *ar,

179. const char **name, BCReg slot1)

180. {

181. uint32_t offset = (uint32_t)ar->i_ci & 0xffff;

182. uint32_t size = (uint32_t)ar->i_ci >> 16;

183. TValue *frame = tvref(L->stack) + offset;

184. TValue *nextframe = size ? frame + size : NULL;

185. GCfunc *fn = frame_func(frame);

186. BCPos pc = debug_framepc(L, fn, nextframe);

187. if (!nextframe) nextframe = L->top+LJ_FR2;

188. if ((int)slot1 < 0) {  /* Negative slot number is for varargs. */

189. if (pc != NO_BCPOS) {

190. GCproto *pt = funcproto(fn);

191. if ((pt->flags & PROTO_VARARG)) {

192. slot1 = pt->numparams + (BCReg)(-(int)slot1);

193. if (frame_isvarg(frame)) {  /* Vararg frame has been set up? (pc!=0) */

194. nextframe = frame;

195. frame = frame_prevd(frame);

196. }

197. if (frame + slot1+LJ_FR2 < nextframe) {

198. *name = "(*vararg)";

199. return frame+slot1;

200. }

201. }

202. }

203. return NULL;

204. }

205. if (pc != NO_BCPOS &&

206. (*name = debug_varname(funcproto(fn), pc, slot1-1)) != NULL)

207. ;

208. else if (slot1 > 0 && frame + slot1+LJ_FR2 < nextframe)

209. *name = "(*temporary)";

210. return frame+slot1;

211. }

213. /* Get name of upvalue. */

214. ‌const char *lj_debug_uvname(GCproto *pt, uint32_t idx)

215. {

216. const uint8_t *p = proto_uvinfo(pt);

217. lua_assert(idx < pt->sizeuv);

218. if (!p) return "";

219. if (idx) while (*p++ || --idx) ;

220. return (const char *)p;

221. }

223. /* Get name and value of upvalue. */

224. ‌const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp)

225. {

226. if (tvisfunc(o)) {

227. GCfunc *fn = funcV(o);

228. if (isluafunc(fn)) {

229. GCproto *pt = funcproto(fn);

230. if (idx < pt->sizeuv) {

231. *tvp = uvval(&gcref(fn->l.uvptr[idx])->uv);

232. return lj_debug_uvname(pt, idx);

233. }

234. } else {

235. if (idx < fn->c.nupvalues) {

236. *tvp = &fn->c.upvalue[idx];

237. return "";

238. }

239. }

240. }

241. return NULL;

242. }

244. /* Deduce name of an object from slot number and PC. */

245. ‌const char *lj_debug_slotname(GCproto *pt, const BCIns *ip, BCReg slot,

246. const char **name)

247. {

248. const char *lname;

249. restart:

250. lname = debug_varname(pt, proto_bcpos(pt, ip), slot);

251. if (lname != NULL) { *name = lname; return "local"; }

252. while (--ip > proto_bc(pt)) {

253. BCIns ins = *ip;

254. BCOp op = bc_op(ins);

255. BCReg ra = bc_a(ins);

256. if (bcmode_a(op) == BCMbase) {

257. if (slot >= ra && (op != BC_KNIL || slot <= bc_d(ins)))

258. return NULL;

259. } else if (bcmode_a(op) == BCMdst && ra == slot) {

260. switch (bc_op(ins)) {

261. case BC_MOV:

262. if (ra == slot) { slot = bc_d(ins); goto restart; }

263. break;

264. case BC_GGET:

265. *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_d(ins))));

266. return "global";

267. case BC_TGETS:

268. *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_c(ins))));

269. if (ip > proto_bc(pt)) {

270. BCIns insp = ip[-1];

271. if (bc_op(insp) == BC_MOV && bc_a(insp) == ra+1+LJ_FR2 &&

272. bc_d(insp) == bc_b(ins))

273. return "method";

274. }

275. return "field";

276. case BC_UGET:

277. *name = lj_debug_uvname(pt, bc_d(ins));

278. return "upvalue";

279. default:

280. return NULL;

281. }

282. }

283. }

284. return NULL;

285. }

287. /* Deduce function name from caller of a frame. */

288. ‌const char *lj_debug_funcname(lua_State *L, cTValue *frame, const char **name)

289. {

290. cTValue *pframe;

291. GCfunc *fn;

292. BCPos pc;

293. if (frame <= tvref(L->stack)+LJ_FR2)

294. return NULL;

295. if (frame_isvarg(frame))

296. frame = frame_prevd(frame);

297. pframe = frame_prev(frame);

298. fn = frame_func(pframe);

299. pc = debug_framepc(L, fn, frame);

300. if (pc != NO_BCPOS) {

301. GCproto *pt = funcproto(fn);

302. const BCIns *ip = &proto_bc(pt)[check_exp(pc < pt->sizebc, pc)];

303. MMS mm = bcmode_mm(bc_op(*ip));

304. if (mm == MM_call) {

305. BCReg slot = bc_a(*ip);

306. if (bc_op(*ip) == BC_ITERC) slot -= 3;

307. return lj_debug_slotname(pt, ip, slot, name);

308. } else if (mm != MM__MAX) {

309. *name = strdata(mmname_str(G(L), mm));

310. return "metamethod";

311. }

312. }

313. return NULL;

314. }

316. /* -- Source code locations ----------------------------------------------- */

318. /* Generate shortened source name. */

319. ‌void lj_debug_shortname(char *out, GCstr *str, BCLine line)

320. {

321. const char *src = strdata(str);

322. if (*src == '=') {

323. strncpy(out, src+1, LUA_IDSIZE);  /* Remove first char. */

324. out[LUA_IDSIZE-1] = '\0';  /* Ensures null termination. */

325. } else if (*src == '@') {  /* Output "source", or "...source". */

326. size_t len = str->len-1;

327. src++;  /* Skip the `@' */

328. if (len >= LUA_IDSIZE) {

329. src += len-(LUA_IDSIZE-4);  /* Get last part of file name. */

330. *out++ = '.'; *out++ = '.'; *out++ = '.';

331. }

332. strcpy(out, src);

333. } else {  /* Output [string "string"] or [builtin:name]. */

334. size_t len;  /* Length, up to first control char. */

335. for (len = 0; len < LUA_IDSIZE-12; len++)

336. if (((const unsigned char *)src)[len] < ' ') break;

337. strcpy(out, line == ~(BCLine)0 ? "[builtin:" : "[string \""); out += 9;

338. if (src[len] != '\0') {  /* Must truncate? */

339. if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15;

340. strncpy(out, src, len); out += len;

341. strcpy(out, "..."); out += 3;

342. } else {

343. strcpy(out, src); out += len;

344. }

345. strcpy(out, line == ~(BCLine)0 ? "]" : "\"]");

346. }

347. }

349. /* Add current location of a frame to error message. */

350. ‌void lj_debug_addloc(lua_State *L, const char *msg,

351. cTValue *frame, cTValue *nextframe)

352. {

353. if (frame) {

354. GCfunc *fn = frame_func(frame);

355. if (isluafunc(fn)) {

356. BCLine line = debug_frameline(L, fn, nextframe);

357. if (line >= 0) {

358. GCproto *pt = funcproto(fn);

359. char buf[LUA_IDSIZE];

360. lj_debug_shortname(buf, proto_chunkname(pt), pt->firstline);

361. lj_strfmt_pushf(L, "%s:%d: %s", buf, line, msg);

362. return;

363. }

364. }

365. }

366. lj_strfmt_pushf(L, "%s", msg);

367. }

369. /* Push location string for a bytecode position to Lua stack. */

370. ‌void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc)

371. {

372. GCstr *name = proto_chunkname(pt);

373. const char *s = strdata(name);

374. MSize i, len = name->len;

375. BCLine line = lj_debug_line(pt, pc);

376. if (pt->firstline == ~(BCLine)0) {

377. lj_strfmt_pushf(L, "builtin:%s", s);

378. } else if (*s == '@') {

379. s++; len--;

380. for (i = len; i > 0; i--)

381. if (s[i] == '/' || s[i] == '\\') {

382. s += i+1;

383. break;

384. }

385. lj_strfmt_pushf(L, "%s:%d", s, line);

386. } else if (len > 40) {

387. lj_strfmt_pushf(L, "%p:%d", pt, line);

388. } else if (*s == '=') {

389. lj_strfmt_pushf(L, "%s:%d", s+1, line);

390. } else {

391. lj_strfmt_pushf(L, "\"%s\":%d", s, line);

392. }

393. }

395. /* -- Public debug API ---------------------------------------------------- */

397. /* lua_getupvalue() and lua_setupvalue() are in lj_api.c. */

399. ‌LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n)

400. {

401. const char *name = NULL;

402. if (ar) {

403. TValue *o = debug_localname(L, ar, &name, (BCReg)n);

404. if (name) {

405. copyTV(L, L->top, o);

406. incr_top(L);

407. }

408. } else if (tvisfunc(L->top-1) && isluafunc(funcV(L->top-1))) {

409. name = debug_varname(funcproto(funcV(L->top-1)), 0, (BCReg)n-1);

410. }

411. return name;

412. }

414. ‌LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n)

415. {

416. const char *name = NULL;

417. TValue *o = debug_localname(L, ar, &name, (BCReg)n);

418. if (name)

419. copyTV(L, o, L->top-1);

420. L->top--;

421. return name;

422. }

424. ‌int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, int ext)

425. {

426. int opt_f = 0, opt_L = 0;

427. TValue *frame = NULL;

428. TValue *nextframe = NULL;

429. GCfunc *fn;

430. if (*what == '>') {

431. TValue *func = L->top - 1;

432. api_check(L, tvisfunc(func));

433. fn = funcV(func);

434. L->top--;

435. what++;

436. } else {

437. uint32_t offset = (uint32_t)ar->i_ci & 0xffff;

438. uint32_t size = (uint32_t)ar->i_ci >> 16;

439. lua_assert(offset != 0);

440. frame = tvref(L->stack) + offset;

441. if (size) nextframe = frame + size;

442. lua_assert(frame <= tvref(L->maxstack) &&

443. (!nextframe || nextframe <= tvref(L->maxstack)));

444. fn = frame_func(frame);

445. lua_assert(fn->c.gct == ~LJ_TFUNC);

446. }

447. for (; *what; what++) {

448. if (*what == 'S') {

449. if (isluafunc(fn)) {

450. GCproto *pt = funcproto(fn);

451. BCLine firstline = pt->firstline;

452. GCstr *name = proto_chunkname(pt);

453. ar->source = strdata(name);

454. lj_debug_shortname(ar->short_src, name, pt->firstline);

455. ar->linedefined = (int)firstline;

456. ar->lastlinedefined = (int)(firstline + pt->numline);

457. ar->what = (firstline || !pt->numline) ? "Lua" : "main";

458. } else {

459. ar->source = "=[C]";

460. ar->short_src[0] = '[';

461. ar->short_src[1] = 'C';

462. ar->short_src[2] = ']';

463. ar->short_src[3] = '\0';

464. ar->linedefined = -1;

465. ar->lastlinedefined = -1;

466. ar->what = "C";

467. }

468. } else if (*what == 'l') {

469. ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1;

470. } else if (*what == 'u') {

471. ar->nups = fn->c.nupvalues;

472. if (ext) {

473. if (isluafunc(fn)) {

474. GCproto *pt = funcproto(fn);

475. ar->nparams = pt->numparams;

476. ar->isvararg = !!(pt->flags & PROTO_VARARG);

477. } else {

478. ar->nparams = 0;

479. ar->isvararg = 1;

480. }

481. }

482. } else if (*what == 'n') {

483. ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL;

484. if (ar->namewhat == NULL) {

485. ar->namewhat = "";

486. ar->name = NULL;

487. }

488. } else if (*what == 'f') {

489. opt_f = 1;

490. } else if (*what == 'L') {

491. opt_L = 1;

492. } else {

493. return 0;  /* Bad option. */

494. }

495. }

496. if (opt_f) {

497. setfuncV(L, L->top, fn);

498. incr_top(L);

499. }

500. if (opt_L) {

501. if (isluafunc(fn)) {

502. GCtab *t = lj_tab_new(L, 0, 0);

503. GCproto *pt = funcproto(fn);

504. const void *lineinfo = proto_lineinfo(pt);

505. if (lineinfo) {

506. BCLine first = pt->firstline;

507. int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4;

508. MSize i, szl = pt->sizebc-1;

509. for (i = 0; i < szl; i++) {

510. BCLine line = first +

511. (sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] :

512. sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] :

513. (BCLine)((const uint32_t *)lineinfo)[i]);

514. setboolV(lj_tab_setint(L, t, line), 1);

515. }

516. }

517. settabV(L, L->top, t);

518. } else {

519. setnilV(L->top);

520. }

521. incr_top(L);

522. }

523. return 1;  /* Ok. */

524. }

526. ‌LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)

527. {

528. return lj_debug_getinfo(L, what, (lj_Debug *)ar, 0);

529. }

531. ‌LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)

532. {

533. int size;

534. cTValue *frame = lj_debug_frame(L, level, &size);

535. if (frame) {

536. ar->i_ci = (size << 16) + (int)(frame - tvref(L->stack));

537. return 1;

538. } else {

539. ar->i_ci = level - size;

540. return 0;

541. }

542. }

544. #if LJ_HASPROFILE

545. /* Put the chunkname into a buffer. */

546. ‌static int debug_putchunkname(SBuf *sb, GCproto *pt, int pathstrip)

547. {

548. GCstr *name = proto_chunkname(pt);

549. const char *p = strdata(name);

550. if (pt->firstline == ~(BCLine)0) {

551. lj_buf_putmem(sb, "[builtin:", 9);

552. lj_buf_putstr(sb, name);

553. lj_buf_putb(sb, ']');

554. return 0;

555. }

556. if (*p == '=' || *p == '@') {

557. MSize len = name->len-1;

558. p++;

559. if (pathstrip) {

560. int i;

561. for (i = len-1; i >= 0; i--)

562. if (p[i] == '/' || p[i] == '\\') {

563. len -= i+1;

564. p = p+i+1;

565. break;

566. }

567. }

568. lj_buf_putmem(sb, p, len);

569. } else {

570. lj_buf_putmem(sb, "[string]", 8);

571. }

572. return 1;

573. }

575. /* Put a compact stack dump into a buffer. */

576. ‌void lj_debug_dumpstack(lua_State *L, SBuf *sb, const char *fmt, int depth)

577. {

578. int level = 0, dir = 1, pathstrip = 1;

579. MSize lastlen = 0;

580. if (depth < 0) { level = ~depth; depth = dir = -1; }  /* Reverse frames. */

581. while (level != depth) {  /* Loop through all frame. */

582. int size;

583. cTValue *frame = lj_debug_frame(L, level, &size);

584. if (frame) {

585. cTValue *nextframe = size ? frame+size : NULL;

586. GCfunc *fn = frame_func(frame);

587. const uint8_t *p = (const uint8_t *)fmt;

588. int c;

589. while ((c = *p++)) {

590. switch (c) {

591. case 'p':  /* Preserve full path. */

592. pathstrip = 0;

593. break;

594. case 'F': case 'f': {  /* Dump function name. */

595. const char *name;

596. const char *what = lj_debug_funcname(L, frame, &name);

597. if (what) {

598. if (c == 'F' && isluafunc(fn)) {  /* Dump module:name for 'F'. */

599. GCproto *pt = funcproto(fn);

600. if (pt->firstline != ~(BCLine)0) {  /* Not a bytecode builtin. */

601. debug_putchunkname(sb, pt, pathstrip);

602. lj_buf_putb(sb, ':');

603. }

604. }

605. lj_buf_putmem(sb, name, (MSize)strlen(name));

606. break;

607. }  /* else: can't derive a name, dump module:line. */

608. }

609. /* fallthrough */

610. case 'l':  /* Dump module:line. */

611. if (isluafunc(fn)) {

612. GCproto *pt = funcproto(fn);

613. if (debug_putchunkname(sb, pt, pathstrip)) {

614. /* Regular Lua function. */

615. BCLine line = c == 'l' ? debug_frameline(L, fn, nextframe) :

616. pt->firstline;

617. lj_buf_putb(sb, ':');

618. lj_strfmt_putint(sb, line >= 0 ? line : pt->firstline);

619. }

620. } else if (isffunc(fn)) {  /* Dump numbered builtins. */

621. lj_buf_putmem(sb, "[builtin#", 9);

622. lj_strfmt_putint(sb, fn->c.ffid);

623. lj_buf_putb(sb, ']');

624. } else {  /* Dump C function address. */

625. lj_buf_putb(sb, '@');

626. lj_strfmt_putptr(sb, fn->c.f);

627. }

628. break;

629. case 'Z':  /* Zap trailing separator. */

630. lastlen = sbuflen(sb);

631. break;

632. default:

633. lj_buf_putb(sb, c);

634. break;

635. }

636. }

637. } else if (dir == 1) {

638. break;

639. } else {

640. level -= size;  /* Reverse frame order: quickly skip missing level. */

641. }

642. level += dir;

643. }

644. if (lastlen)

645. setsbufP(sb, sbufB(sb) + lastlen);  /* Zap trailing separator. */

646. }

647. #endif

649. /* Number of frames for the leading and trailing part of a traceback. */

650. ‌#define TRACEBACK_LEVELS1        12

651. ‌#define TRACEBACK_LEVELS2        10

653. ‌LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,

654. int level)

655. {

656. int top = (int)(L->top - L->base);

657. int lim = TRACEBACK_LEVELS1;

658. lua_Debug ar;

659. if (msg) lua_pushfstring(L, "%s\n", msg);

660. lua_pushliteral(L, "stack traceback:");

661. while (lua_getstack(L1, level++, &ar)) {

662. GCfunc *fn;

663. if (level > lim) {

664. if (!lua_getstack(L1, level + TRACEBACK_LEVELS2, &ar)) {

665. level--;

666. } else {

667. lua_pushliteral(L, "\n\t...");

668. lua_getstack(L1, -10, &ar);

669. level = ar.i_ci - TRACEBACK_LEVELS2;

670. }

671. lim = 2147483647;

672. continue;

673. }

674. lua_getinfo(L1, "Snlf", &ar);

675. fn = funcV(L1->top-1); L1->top--;

676. if (isffunc(fn) && !*ar.namewhat)

677. lua_pushfstring(L, "\n\t[builtin#%d]:", fn->c.ffid);

678. else

679. lua_pushfstring(L, "\n\t%s:", ar.short_src);

680. if (ar.currentline > 0)

681. lua_pushfstring(L, "%d:", ar.currentline);

682. if (*ar.namewhat) {

683. lua_pushfstring(L, " in function " LUA_QS, ar.name);

684. } else {

685. if (*ar.what == 'm') {

686. lua_pushliteral(L, " in main chunk");

687. } else if (*ar.what == 'C') {

688. lua_pushfstring(L, " at %p", fn->c.f);

689. } else {

690. lua_pushfstring(L, " in function <%s:%d>",

691. ar.short_src, ar.linedefined);

692. }

693. }

694. if ((int)(L->top - L->base) - top >= 15)

695. lua_concat(L, (int)(L->top - L->base) - top);

696. }

697. lua_concat(L, (int)(L->top - L->base) - top);

698. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_debug.h - luajit-2.0-src

Data types defined

- lj_Debug

- lj_Debug

Macros defined

- VARNAMEDEF

- VARNAMEENUM

- VARNAMEENUM

- _LJ_DEBUG_H

Source code

1. /*

2. ** Debugging and introspection.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_DEBUG_H

7. ‌#define _LJ_DEBUG_H

9. #include "lj_obj.h"

11. ‌typedef struct lj_Debug {

12. /* Common fields. Must be in the same order as in lua.h. */

13. int event;

14. const char *name;

15. const char *namewhat;

16. const char *what;

17. const char *source;

18. int currentline;

19. int nups;

20. int linedefined;

21. int lastlinedefined;

22. char short_src[LUA_IDSIZE];

23. int i_ci;

24. /* Extended fields. Only valid if lj_debug_getinfo() is called with ext = 1.*/

25. int nparams;

26. int isvararg;

27. ‌} lj_Debug;

29. LJ_FUNC cTValue *lj_debug_frame(lua_State *L, int level, int *size);

30. LJ_FUNC BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc);

31. LJ_FUNC const char *lj_debug_uvname(GCproto *pt, uint32_t idx);

32. LJ_FUNC const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp);

33. LJ_FUNC const char *lj_debug_slotname(GCproto *pt, const BCIns *pc,

34. BCReg slot, const char **name);

35. LJ_FUNC const char *lj_debug_funcname(lua_State *L, cTValue *frame,

36. const char **name);

37. LJ_FUNC void lj_debug_shortname(char *out, GCstr *str, BCLine line);

38. LJ_FUNC void lj_debug_addloc(lua_State *L, const char *msg,

39. cTValue *frame, cTValue *nextframe);

40. LJ_FUNC void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc);

41. LJ_FUNC int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar,

42. int ext);

43. #if LJ_HASPROFILE

44. LJ_FUNC void lj_debug_dumpstack(lua_State *L, SBuf *sb, const char *fmt,

45. int depth);

46. #endif

48. /* Fixed internal variable names. */

49. ‌#define VARNAMEDEF(_) \

50. _(FOR_IDX, "(for index)") \

51. _(FOR_STOP, "(for limit)") \

52. _(FOR_STEP, "(for step)") \

53. _(FOR_GEN, "(for generator)") \

54. _(FOR_STATE, "(for state)") \

55. _(FOR_CTL, "(for control)")

57. enum {

58. VARNAME_END,

59. ‌#define VARNAMEENUM(name, str)        VARNAME_##name,

60. VARNAMEDEF(VARNAMEENUM)

61. ‌#undef VARNAMEENUM

62. VARNAME__MAX

63. };

65. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_debug.c - luajit-2.0-src

Global variables defined

- KEY_HOOK

Functions defined

- LJLIB_CF(debug_getregistry)

- LJLIB_CF(debug_getmetatable) LJLIB_REC(.)

- LJLIB_CF(debug_setmetatable)

- LJLIB_CF(debug_getfenv)

- LJLIB_CF(debug_setfenv)

- LJLIB_CF(debug_getinfo)

- LJLIB_CF(debug_getlocal)

- LJLIB_CF(debug_setlocal)

- LJLIB_CF(debug_getupvalue)

- LJLIB_CF(debug_setupvalue)

- LJLIB_CF(debug_upvalueid)

- LJLIB_CF(debug_upvaluejoin)

- LJLIB_CF(debug_getuservalue)

- LJLIB_CF(debug_setuservalue)

- LJLIB_CF(debug_sethook)

- LJLIB_CF(debug_gethook)

- LJLIB_CF(debug_debug)

- LJLIB_CF(debug_traceback)

- debug_getupvalue

- getthread

- hookf

- luaopen_debug

- makemask

- settabsb

- settabsi

- settabss

- treatstackoption

- unmakemask

Macros defined

- LEVELS1

- LEVELS2

- LJLIB_MODULE_debug

- LUA_LIB

- lib_debug_c

Source code

1. /*

2. ** Debug library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lib_debug_c

10. ‌#define LUA_LIB

12. #include "lua.h"

13. #include "lauxlib.h"

14. #include "lualib.h"

16. #include "lj_obj.h"

17. #include "lj_gc.h"

18. #include "lj_err.h"

19. #include "lj_debug.h"

20. #include "lj_lib.h"

22. /* ------------------------------------------------------------------------ */

24. ‌#define LJLIB_MODULE_debug

26. ‌LJLIB_CF(debug_getregistry)

27. {

28. copyTV(L, L->top++, registry(L));

29. return 1;

30. }

32. ‌LJLIB_CF(debug_getmetatable)        LJLIB_REC(.)

33. {

34. lj_lib_checkany(L, 1);

35. if (!lua_getmetatable(L, 1)) {

36. setnilV(L->top-1);

37. }

38. return 1;

39. }

41. ‌LJLIB_CF(debug_setmetatable)

42. {

43. lj_lib_checktabornil(L, 2);

44. L->top = L->base+2;

45. lua_setmetatable(L, 1);

46. #if !LJ_52

47. setboolV(L->top-1, 1);

48. #endif

49. return 1;

50. }

52. ‌LJLIB_CF(debug_getfenv)

53. {

54. lj_lib_checkany(L, 1);

55. lua_getfenv(L, 1);

56. return 1;

57. }

59. ‌LJLIB_CF(debug_setfenv)

60. {

61. lj_lib_checktab(L, 2);

62. L->top = L->base+2;

63. if (!lua_setfenv(L, 1))

64. lj_err_caller(L, LJ_ERR_SETFENV);

65. return 1;

66. }

68. /* ------------------------------------------------------------------------ */

70. ‌static void settabss(lua_State *L, const char *i, const char *v)

71. {

72. lua_pushstring(L, v);

73. lua_setfield(L, -2, i);

74. }

76. ‌static void settabsi(lua_State *L, const char *i, int v)

77. {

78. lua_pushinteger(L, v);

79. lua_setfield(L, -2, i);

80. }

82. ‌static void settabsb(lua_State *L, const char *i, int v)

83. {

84. lua_pushboolean(L, v);

85. lua_setfield(L, -2, i);

86. }

88. ‌static lua_State *getthread(lua_State *L, int *arg)

89. {

90. if (L->base < L->top && tvisthread(L->base)) {

91. *arg = 1;

92. return threadV(L->base);

93. } else {

94. *arg = 0;

95. return L;

96. }

97. }

99. ‌static void treatstackoption(lua_State *L, lua_State *L1, const char *fname)

100. {

101. if (L == L1) {

102. lua_pushvalue(L, -2);

103. lua_remove(L, -3);

104. }

105. else

106. lua_xmove(L1, L, 1);

107. lua_setfield(L, -2, fname);

108. }

110. ‌LJLIB_CF(debug_getinfo)

111. {

112. lj_Debug ar;

113. int arg, opt_f = 0, opt_L = 0;

114. lua_State *L1 = getthread(L, &arg);

115. const char *options = luaL_optstring(L, arg+2, "flnSu");

116. if (lua_isnumber(L, arg+1)) {

117. if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), (lua_Debug *)&ar)) {

118. setnilV(L->top-1);

119. return 1;

120. }

121. } else if (L->base+arg < L->top && tvisfunc(L->base+arg)) {

122. options = lua_pushfstring(L, ">%s", options);

123. setfuncV(L1, L1->top++, funcV(L->base+arg));

124. } else {

125. lj_err_arg(L, arg+1, LJ_ERR_NOFUNCL);

126. }

127. if (!lj_debug_getinfo(L1, options, &ar, 1))

128. lj_err_arg(L, arg+2, LJ_ERR_INVOPT);

129. lua_createtable(L, 0, 16);  /* Create result table. */

130. for (; *options; options++) {

131. switch (*options) {

132. case 'S':

133. settabss(L, "source", ar.source);

134. settabss(L, "short_src", ar.short_src);

135. settabsi(L, "linedefined", ar.linedefined);

136. settabsi(L, "lastlinedefined", ar.lastlinedefined);

137. settabss(L, "what", ar.what);

138. break;

139. case 'l':

140. settabsi(L, "currentline", ar.currentline);

141. break;

142. case 'u':

143. settabsi(L, "nups", ar.nups);

144. settabsi(L, "nparams", ar.nparams);

145. settabsb(L, "isvararg", ar.isvararg);

146. break;

147. case 'n':

148. settabss(L, "name", ar.name);

149. settabss(L, "namewhat", ar.namewhat);

150. break;

151. case 'f': opt_f = 1; break;

152. case 'L': opt_L = 1; break;

153. default: break;

154. }

155. }

156. if (opt_L) treatstackoption(L, L1, "activelines");

157. if (opt_f) treatstackoption(L, L1, "func");

158. return 1;  /* Return result table. */

159. }

161. ‌LJLIB_CF(debug_getlocal)

162. {

163. int arg;

164. lua_State *L1 = getthread(L, &arg);

165. lua_Debug ar;

166. const char *name;

167. int slot = lj_lib_checkint(L, arg+2);

168. if (tvisfunc(L->base+arg)) {

169. L->top = L->base+arg+1;

170. lua_pushstring(L, lua_getlocal(L, NULL, slot));

171. return 1;

172. }

173. if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar))

174. lj_err_arg(L, arg+1, LJ_ERR_LVLRNG);

175. name = lua_getlocal(L1, &ar, slot);

176. if (name) {

177. lua_xmove(L1, L, 1);

178. lua_pushstring(L, name);

179. lua_pushvalue(L, -2);

180. return 2;

181. } else {

182. setnilV(L->top-1);

183. return 1;

184. }

185. }

187. ‌LJLIB_CF(debug_setlocal)

188. {

189. int arg;

190. lua_State *L1 = getthread(L, &arg);

191. lua_Debug ar;

192. TValue *tv;

193. if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar))

194. lj_err_arg(L, arg+1, LJ_ERR_LVLRNG);

195. tv = lj_lib_checkany(L, arg+3);

196. copyTV(L1, L1->top++, tv);

197. lua_pushstring(L, lua_setlocal(L1, &ar, lj_lib_checkint(L, arg+2)));

198. return 1;

199. }

201. ‌static int debug_getupvalue(lua_State *L, int get)

202. {

203. int32_t n = lj_lib_checkint(L, 2);

204. const char *name;

205. lj_lib_checkfunc(L, 1);

206. name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);

207. if (name) {

208. lua_pushstring(L, name);

209. if (!get) return 1;

210. copyTV(L, L->top, L->top-2);

211. L->top++;

212. return 2;

213. }

214. return 0;

215. }

217. ‌LJLIB_CF(debug_getupvalue)

218. {

219. return debug_getupvalue(L, 1);

220. }

222. ‌LJLIB_CF(debug_setupvalue)

223. {

224. lj_lib_checkany(L, 3);

225. return debug_getupvalue(L, 0);

226. }

228. ‌LJLIB_CF(debug_upvalueid)

229. {

230. GCfunc *fn = lj_lib_checkfunc(L, 1);

231. int32_t n = lj_lib_checkint(L, 2) - 1;

232. if ((uint32_t)n >= fn->l.nupvalues)

233. lj_err_arg(L, 2, LJ_ERR_IDXRNG);

234. setlightudV(L->top-1, isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :

235. (void *)&fn->c.upvalue[n]);

236. return 1;

237. }

239. ‌LJLIB_CF(debug_upvaluejoin)

240. {

241. GCfunc *fn[2];

242. GCRef *p[2];

243. int i;

244. for (i = 0; i < 2; i++) {

245. int32_t n;

246. fn[i] = lj_lib_checkfunc(L, 2*i+1);

247. if (!isluafunc(fn[i]))

248. lj_err_arg(L, 2*i+1, LJ_ERR_NOLFUNC);

249. n = lj_lib_checkint(L, 2*i+2) - 1;

250. if ((uint32_t)n >= fn[i]->l.nupvalues)

251. lj_err_arg(L, 2*i+2, LJ_ERR_IDXRNG);

252. p[i] = &fn[i]->l.uvptr[n];

253. }

254. setgcrefr(*p[0], *p[1]);

255. lj_gc_objbarrier(L, fn[0], gcref(*p[1]));

256. return 0;

257. }

259. #if LJ_52

260. ‌LJLIB_CF(debug_getuservalue)

261. {

262. TValue *o = L->base;

263. if (o < L->top && tvisudata(o))

264. settabV(L, o, tabref(udataV(o)->env));

265. else

266. setnilV(o);

267. L->top = o+1;

268. return 1;

269. }

271. ‌LJLIB_CF(debug_setuservalue)

272. {

273. TValue *o = L->base;

274. if (!(o < L->top && tvisudata(o)))

275. lj_err_argt(L, 1, LUA_TUSERDATA);

276. if (!(o+1 < L->top && tvistab(o+1)))

277. lj_err_argt(L, 2, LUA_TTABLE);

278. L->top = o+2;

279. lua_setfenv(L, 1);

280. return 1;

281. }

282. #endif

284. /* ------------------------------------------------------------------------ */

286. ‌static const char KEY_HOOK = 'h';

288. ‌static void hookf(lua_State *L, lua_Debug *ar)

289. {

290. static const char *const hooknames[] =

291. {"call", "return", "line", "count", "tail return"};

292. lua_pushlightuserdata(L, (void *)&KEY_HOOK);

293. lua_rawget(L, LUA_REGISTRYINDEX);

294. if (lua_isfunction(L, -1)) {

295. lua_pushstring(L, hooknames[(int)ar->event]);

296. if (ar->currentline >= 0)

297. lua_pushinteger(L, ar->currentline);

298. else lua_pushnil(L);

299. lua_call(L, 2, 0);

300. }

301. }

303. ‌static int makemask(const char *smask, int count)

304. {

305. int mask = 0;

306. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;

307. if (strchr(smask, 'r')) mask |= LUA_MASKRET;

308. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;

309. if (count > 0) mask |= LUA_MASKCOUNT;

310. return mask;

311. }

313. ‌static char *unmakemask(int mask, char *smask)

314. {

315. int i = 0;

316. if (mask & LUA_MASKCALL) smask[i++] = 'c';

317. if (mask & LUA_MASKRET) smask[i++] = 'r';

318. if (mask & LUA_MASKLINE) smask[i++] = 'l';

319. smask[i] = '\0';

320. return smask;

321. }

323. ‌LJLIB_CF(debug_sethook)

324. {

325. int arg, mask, count;

326. lua_Hook func;

327. (void)getthread(L, &arg);

328. if (lua_isnoneornil(L, arg+1)) {

329. lua_settop(L, arg+1);

330. func = NULL; mask = 0; count = 0;  /* turn off hooks */

331. } else {

332. const char *smask = luaL_checkstring(L, arg+2);

333. luaL_checktype(L, arg+1, LUA_TFUNCTION);

334. count = luaL_optint(L, arg+3, 0);

335. func = hookf; mask = makemask(smask, count);

336. }

337. lua_pushlightuserdata(L, (void *)&KEY_HOOK);

338. lua_pushvalue(L, arg+1);

339. lua_rawset(L, LUA_REGISTRYINDEX);

340. lua_sethook(L, func, mask, count);

341. return 0;

342. }

344. ‌LJLIB_CF(debug_gethook)

345. {

346. char buff[5];

347. int mask = lua_gethookmask(L);

348. lua_Hook hook = lua_gethook(L);

349. if (hook != NULL && hook != hookf) {  /* external hook? */

350. lua_pushliteral(L, "external hook");

351. } else {

352. lua_pushlightuserdata(L, (void *)&KEY_HOOK);

353. lua_rawget(L, LUA_REGISTRYINDEX);   /* get hook */

354. }

355. lua_pushstring(L, unmakemask(mask, buff));

356. lua_pushinteger(L, lua_gethookcount(L));

357. return 3;

358. }

360. /* ------------------------------------------------------------------------ */

362. ‌LJLIB_CF(debug_debug)

363. {

364. for (;;) {

365. char buffer[250];

366. fputs("lua_debug> ", stderr);

367. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||

368. strcmp(buffer, "cont\n") == 0)

369. return 0;

370. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||

371. lua_pcall(L, 0, 0, 0)) {

372. fputs(lua_tostring(L, -1), stderr);

373. fputs("\n", stderr);

374. }

375. lua_settop(L, 0);  /* remove eventual returns */

376. }

377. }

379. /* ------------------------------------------------------------------------ */

381. ‌#define LEVELS1        12        /* size of the first part of the stack */

382. ‌#define LEVELS2        10        /* size of the second part of the stack */

384. ‌LJLIB_CF(debug_traceback)

385. {

386. int arg;

387. lua_State *L1 = getthread(L, &arg);

388. const char *msg = lua_tostring(L, arg+1);

389. if (msg == NULL && L->top > L->base+arg)

390. L->top = L->base+arg+1;

391. else

392. luaL_traceback(L, L1, msg, lj_lib_optint(L, arg+2, (L == L1)));

393. return 1;

394. }

396. /* ------------------------------------------------------------------------ */

398. #include "lj_libdef.h"

400. ‌LUALIB_API int luaopen_debug(lua_State *L)

401. {

402. LJ_LIB_REG(L, LUA_DBLIBNAME, debug);

403. return 1;

404. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_bit.c - luajit-2.0-src

Functions defined

- LJLIB_ASM(bit_tobit) LJLIB_REC(bit_tobit)

- LJLIB_ASM(bit_bnot) LJLIB_REC(bit_unary IR_BNOT)

- LJLIB_ASM(bit_bswap) LJLIB_REC(bit_unary IR_BSWAP)

- LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL)

- LJLIB_ASM_(bit_rshift) LJLIB_REC(bit_shift IR_BSHR)

- LJLIB_ASM_(bit_bor) LJLIB_REC(bit_nary IR_BOR)

- bit_checkbit

- bit_result64

- luaopen_bit

Macros defined

- LJLIB_MODULE_bit

- LUA_LIB

- lib_bit_c

Source code

1. /*

2. ** Bit manipulation library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lib_bit_c

7. ‌#define LUA_LIB

9. #include "lua.h"

10. #include "lauxlib.h"

11. #include "lualib.h"

13. #include "lj_obj.h"

14. #include "lj_err.h"

15. #include "lj_buf.h"

16. #include "lj_strscan.h"

17. #include "lj_strfmt.h"

18. #if LJ_HASFFI

19. #include "lj_ctype.h"

20. #include "lj_cdata.h"

21. #include "lj_cconv.h"

22. #include "lj_carith.h"

23. #endif

24. #include "lj_ff.h"

25. #include "lj_lib.h"

27. /* ------------------------------------------------------------------------ */

29. ‌#define LJLIB_MODULE_bit

31. #if LJ_HASFFI

32. ‌static int bit_result64(lua_State *L, CTypeID id, uint64_t x)

33. {

34. GCcdata *cd = lj_cdata_new_(L, id, 8);

35. *(uint64_t *)cdataptr(cd) = x;

36. setcdataV(L, L->base-1-LJ_FR2, cd);

37. return FFH_RES(1);

38. }

39. #else

40. ‌static int32_t bit_checkbit(lua_State *L, int narg)

41. {

42. TValue *o = L->base + narg-1;

43. if (!(o < L->top && lj_strscan_numberobj(o)))

44. lj_err_argt(L, narg, LUA_TNUMBER);

45. if (LJ_LIKELY(tvisint(o))) {

46. return intV(o);

47. } else {

48. int32_t i = lj_num2bit(numV(o));

49. if (LJ_DUALNUM) setintV(o, i);

50. return i;

51. }

52. }

53. #endif

55. ‌LJLIB_ASM(bit_tobit)                LJLIB_REC(bit_tobit)

56. {

57. #if LJ_HASFFI

58. CTypeID id = 0;

59. setintV(L->base-1-LJ_FR2, (int32_t)lj_carith_check64(L, 1, &id));

60. return FFH_RES(1);

61. #else

62. lj_lib_checknumber(L, 1);

63. return FFH_RETRY;

64. #endif

65. }

67. ‌LJLIB_ASM(bit_bnot)                LJLIB_REC(bit_unary IR_BNOT)

68. {

69. #if LJ_HASFFI

70. CTypeID id = 0;

71. uint64_t x = lj_carith_check64(L, 1, &id);

72. return id ? bit_result64(L, id, ~x) : FFH_RETRY;

73. #else

74. lj_lib_checknumber(L, 1);

75. return FFH_RETRY;

76. #endif

77. }

79. ‌LJLIB_ASM(bit_bswap)                LJLIB_REC(bit_unary IR_BSWAP)

80. {

81. #if LJ_HASFFI

82. CTypeID id = 0;

83. uint64_t x = lj_carith_check64(L, 1, &id);

84. return id ? bit_result64(L, id, lj_bswap64(x)) : FFH_RETRY;

85. #else

86. lj_lib_checknumber(L, 1);

87. return FFH_RETRY;

88. #endif

89. }

91. ‌LJLIB_ASM(bit_lshift)                LJLIB_REC(bit_shift IR_BSHL)

92. {

93. #if LJ_HASFFI

94. CTypeID id = 0, id2 = 0;

95. uint64_t x = lj_carith_check64(L, 1, &id);

96. int32_t sh = (int32_t)lj_carith_check64(L, 2, &id2);

97. if (id) {

98. x = lj_carith_shift64(x, sh, curr_func(L)->c.ffid - (int)FF_bit_lshift);

99. return bit_result64(L, id, x);

100. }

101. if (id2) setintV(L->base+1, sh);

102. return FFH_RETRY;

103. #else

104. lj_lib_checknumber(L, 1);

105. bit_checkbit(L, 2);

106. return FFH_RETRY;

107. #endif

108. }

109. ‌LJLIB_ASM_(bit_rshift)                LJLIB_REC(bit_shift IR_BSHR)

110. LJLIB_ASM_(bit_arshift)                LJLIB_REC(bit_shift IR_BSAR)

111. LJLIB_ASM_(bit_rol)                LJLIB_REC(bit_shift IR_BROL)

112. LJLIB_ASM_(bit_ror)                LJLIB_REC(bit_shift IR_BROR)

114. LJLIB_ASM(bit_band)                LJLIB_REC(bit_nary IR_BAND)

115. {

116. #if LJ_HASFFI

117. CTypeID id = 0;

118. TValue *o = L->base, *top = L->top;

119. int i = 0;

120. do { lj_carith_check64(L, ++i, &id); } while (++o < top);

121. if (id) {

122. CTState *cts = ctype_cts(L);

123. CType *ct = ctype_get(cts, id);

124. int op = curr_func(L)->c.ffid - (int)FF_bit_bor;

125. uint64_t x, y = op >= 0 ? 0 : ~(uint64_t)0;

126. o = L->base;

127. do {

128. lj_cconv_ct_tv(cts, ct, (uint8_t *)&x, o, 0);

129. if (op < 0) y &= x; else if (op == 0) y |= x; else y ^= x;

130. } while (++o < top);

131. return bit_result64(L, id, y);

132. }

133. return FFH_RETRY;

134. #else

135. int i = 0;

136. do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top);

137. return FFH_RETRY;

138. #endif

139. }

140. ‌LJLIB_ASM_(bit_bor)                LJLIB_REC(bit_nary IR_BOR)

141. LJLIB_ASM_(bit_bxor)                LJLIB_REC(bit_nary IR_BXOR)

143. /* ------------------------------------------------------------------------ */

145. LJLIB_CF(bit_tohex)                LJLIB_REC(.)

146. {

147. #if LJ_HASFFI

148. CTypeID id = 0, id2 = 0;

149. uint64_t b = lj_carith_check64(L, 1, &id);

150. int32_t n = L->base+1>=L->top ? (id ? 16 : 8) :

151. (int32_t)lj_carith_check64(L, 2, &id2);

152. #else

153. uint32_t b = (uint32_t)bit_checkbit(L, 1);

154. int32_t n = L->base+1>=L->top ? 8 : bit_checkbit(L, 2);

155. #endif

156. SBuf *sb = lj_buf_tmp_(L);

157. SFormat sf = (STRFMT_UINT|STRFMT_T_HEX);

158. if (n < 0) { n = -n; sf |= STRFMT_F_UPPER; }

159. sf |= ((SFormat)((n+1)&255) << STRFMT_SH_PREC);

160. #if LJ_HASFFI

161. if (n < 16) b &= ((uint64_t)1 << 4*n)-1;

162. #else

163. if (n < 8) b &= (1u << 4*n)-1;

164. #endif

165. sb = lj_strfmt_putfxint(sb, sf, b);

166. setstrV(L, L->top-1, lj_buf_str(L, sb));

167. lj_gc_check(L);

168. return 1;

169. }

171. /* ------------------------------------------------------------------------ */

173. #include "lj_libdef.h"

175. ‌LUALIB_API int luaopen_bit(lua_State *L)

176. {

177. LJ_LIB_REG(L, LUA_BITLIBNAME, bit);

178. return 1;

179. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_carith.c - luajit-2.0-src

Data types defined

- CDArith

- CDArith

Functions defined

- B64DEF

- B64DEF

- B64DEF

- B64DEF

- B64DEF

- carith_checkarg

- carith_int64

- carith_ptr

- lj_carith_check64

- lj_carith_divi64

- lj_carith_divu64

- lj_carith_meta

- lj_carith_modi64

- lj_carith_modu64

- lj_carith_mul64

- lj_carith_op

- lj_carith_powi64

- lj_carith_powu64

- lj_carith_shift64

Macros defined

- B64DEF

- B64DEF

- B64DEF

Source code

1. /*

2. ** C data arithmetic.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "lj_obj.h"

8. #if LJ_HASFFI

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_tab.h"

13. #include "lj_meta.h"

14. #include "lj_ir.h"

15. #include "lj_ctype.h"

16. #include "lj_cconv.h"

17. #include "lj_cdata.h"

18. #include "lj_carith.h"

19. #include "lj_strscan.h"

21. /* -- C data arithmetic --------------------------------------------------- */

23. /* Binary operands of an operator converted to ctypes. */

24. ‌typedef struct CDArith {

25. uint8_t *p[2];

26. CType *ct[2];

27. ‌} CDArith;

29. /* Check arguments for arithmetic metamethods. */

30. ‌static int carith_checkarg(lua_State *L, CTState *cts, CDArith *ca)

31. {

32. TValue *o = L->base;

33. int ok = 1;

34. MSize i;

35. if (o+1 >= L->top)

36. lj_err_argt(L, 1, LUA_TCDATA);

37. for (i = 0; i < 2; i++, o++) {

38. if (tviscdata(o)) {

39. GCcdata *cd = cdataV(o);

40. CTypeID id = (CTypeID)cd->ctypeid;

41. CType *ct = ctype_raw(cts, id);

42. uint8_t *p = (uint8_t *)cdataptr(cd);

43. if (ctype_isptr(ct->info)) {

44. p = (uint8_t *)cdata_getptr(p, ct->size);

45. if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);

46. } else if (ctype_isfunc(ct->info)) {

47. p = (uint8_t *)*(void **)p;

48. ct = ctype_get(cts,

49. lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR));

50. }

51. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);

52. ca->ct[i] = ct;

53. ca->p[i] = p;

54. } else if (tvisint(o)) {

55. ca->ct[i] = ctype_get(cts, CTID_INT32);

56. ca->p[i] = (uint8_t *)&o->i;

57. } else if (tvisnum(o)) {

58. ca->ct[i] = ctype_get(cts, CTID_DOUBLE);

59. ca->p[i] = (uint8_t *)&o->n;

60. } else if (tvisnil(o)) {

61. ca->ct[i] = ctype_get(cts, CTID_P_VOID);

62. ca->p[i] = (uint8_t *)0;

63. } else if (tvisstr(o)) {

64. TValue *o2 = i == 0 ? o+1 : o-1;

65. CType *ct = ctype_raw(cts, cdataV(o2)->ctypeid);

66. ca->ct[i] = NULL;

67. ca->p[i] = (uint8_t *)strVdata(o);

68. ok = 0;

69. if (ctype_isenum(ct->info)) {

70. CTSize ofs;

71. CType *cct = lj_ctype_getfield(cts, ct, strV(o), &ofs);

72. if (cct && ctype_isconstval(cct->info)) {

73. ca->ct[i] = ctype_child(cts, cct);

74. ca->p[i] = (uint8_t *)&cct->size;  /* Assumes ct does not grow. */

75. ok = 1;

76. } else {

77. ca->ct[1-i] = ct;  /* Use enum to improve error message. */

78. ca->p[1-i] = NULL;

79. break;

80. }

81. }

82. } else {

83. ca->ct[i] = NULL;

84. ca->p[i] = (void *)(intptr_t)1;  /* To make it unequal. */

85. ok = 0;

86. }

87. }

88. return ok;

89. }

91. /* Pointer arithmetic. */

92. ‌static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm)

93. {

94. CType *ctp = ca->ct[0];

95. uint8_t *pp = ca->p[0];

96. ptrdiff_t idx;

97. CTSize sz;

98. CTypeID id;

99. GCcdata *cd;

100. if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {

101. if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&

102. (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {

103. uint8_t *pp2 = ca->p[1];

104. if (mm == MM_eq) {  /* Pointer equality. Incompatible pointers are ok. */

105. setboolV(L->top-1, (pp == pp2));

106. return 1;

107. }

108. if (!lj_cconv_compatptr(cts, ctp, ca->ct[1], CCF_IGNQUAL))

109. return 0;

110. if (mm == MM_sub) {  /* Pointer difference. */

111. intptr_t diff;

112. sz = lj_ctype_size(cts, ctype_cid(ctp->info));  /* Element size. */

113. if (sz == 0 || sz == CTSIZE_INVALID)

114. return 0;

115. diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz;

116. /* All valid pointer differences on x64 are in (-2^47, +2^47),

117. ** which fits into a double without loss of precision.

118. */

119. setintptrV(L->top-1, (int32_t)diff);

120. return 1;

121. } else if (mm == MM_lt) {  /* Pointer comparison (unsigned). */

122. setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2));

123. return 1;

124. } else {

125. lua_assert(mm == MM_le);

126. setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2));

127. return 1;

128. }

129. }

130. if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(ca->ct[1]->info)))

131. return 0;

132. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[1],

133. (uint8_t *)&idx, ca->p[1], 0);

134. if (mm == MM_sub) idx = -idx;

135. } else if (mm == MM_add && ctype_isnum(ctp->info) &&

136. (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {

137. /* Swap pointer and index. */

138. ctp = ca->ct[1]; pp = ca->p[1];

139. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[0],

140. (uint8_t *)&idx, ca->p[0], 0);

141. } else {

142. return 0;

143. }

144. sz = lj_ctype_size(cts, ctype_cid(ctp->info));  /* Element size. */

145. if (sz == CTSIZE_INVALID)

146. return 0;

147. pp += idx*(int32_t)sz;  /* Compute pointer + index. */

148. id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),

149. CTSIZE_PTR);

150. cd = lj_cdata_new(cts, id, CTSIZE_PTR);

151. *(uint8_t **)cdataptr(cd) = pp;

152. setcdataV(L, L->top-1, cd);

153. lj_gc_check(L);

154. return 1;

155. }

157. /* 64 bit integer arithmetic. */

158. ‌static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm)

159. {

160. if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 &&

161. ctype_isnum(ca->ct[1]->info) && ca->ct[1]->size <= 8) {

162. CTypeID id = (((ca->ct[0]->info & CTF_UNSIGNED) && ca->ct[0]->size == 8) ||

163. ((ca->ct[1]->info & CTF_UNSIGNED) && ca->ct[1]->size == 8)) ?

164. CTID_UINT64 : CTID_INT64;

165. CType *ct = ctype_get(cts, id);

166. GCcdata *cd;

167. uint64_t u0, u1, *up;

168. lj_cconv_ct_ct(cts, ct, ca->ct[0], (uint8_t *)&u0, ca->p[0], 0);

169. if (mm != MM_unm)

170. lj_cconv_ct_ct(cts, ct, ca->ct[1], (uint8_t *)&u1, ca->p[1], 0);

171. switch (mm) {

172. case MM_eq:

173. setboolV(L->top-1, (u0 == u1));

174. return 1;

175. case MM_lt:

176. setboolV(L->top-1,

177. id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1));

178. return 1;

179. case MM_le:

180. setboolV(L->top-1,

181. id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1));

182. return 1;

183. default: break;

184. }

185. cd = lj_cdata_new(cts, id, 8);

186. up = (uint64_t *)cdataptr(cd);

187. setcdataV(L, L->top-1, cd);

188. switch (mm) {

189. case MM_add: *up = u0 + u1; break;

190. case MM_sub: *up = u0 - u1; break;

191. case MM_mul: *up = u0 * u1; break;

192. case MM_div:

193. if (id == CTID_INT64)

194. *up = (uint64_t)lj_carith_divi64((int64_t)u0, (int64_t)u1);

195. else

196. *up = lj_carith_divu64(u0, u1);

197. break;

198. case MM_mod:

199. if (id == CTID_INT64)

200. *up = (uint64_t)lj_carith_modi64((int64_t)u0, (int64_t)u1);

201. else

202. *up = lj_carith_modu64(u0, u1);

203. break;

204. case MM_pow:

205. if (id == CTID_INT64)

206. *up = (uint64_t)lj_carith_powi64((int64_t)u0, (int64_t)u1);

207. else

208. *up = lj_carith_powu64(u0, u1);

209. break;

210. case MM_unm: *up = (uint64_t)-(int64_t)u0; break;

211. default: lua_assert(0); break;

212. }

213. lj_gc_check(L);

214. return 1;

215. }

216. return 0;

217. }

219. /* Handle ctype arithmetic metamethods. */

220. ‌static int lj_carith_meta(lua_State *L, CTState *cts, CDArith *ca, MMS mm)

221. {

222. cTValue *tv = NULL;

223. if (tviscdata(L->base)) {

224. CTypeID id = cdataV(L->base)->ctypeid;

225. CType *ct = ctype_raw(cts, id);

226. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);

227. tv = lj_ctype_meta(cts, id, mm);

228. }

229. if (!tv && L->base+1 < L->top && tviscdata(L->base+1)) {

230. CTypeID id = cdataV(L->base+1)->ctypeid;

231. CType *ct = ctype_raw(cts, id);

232. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);

233. tv = lj_ctype_meta(cts, id, mm);

234. }

235. if (!tv) {

236. const char *repr[2];

237. int i, isenum = -1, isstr = -1;

238. if (mm == MM_eq) {  /* Equality checks never raise an error. */

239. int eq = ca->p[0] == ca->p[1];

240. setboolV(L->top-1, eq);

241. setboolV(&G(L)->tmptv2, eq);  /* Remember for trace recorder. */

242. return 1;

243. }

244. for (i = 0; i < 2; i++) {

245. if (ca->ct[i] && tviscdata(L->base+i)) {

246. if (ctype_isenum(ca->ct[i]->info)) isenum = i;

247. repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, ca->ct[i]), NULL));

248. } else {

249. if (tvisstr(&L->base[i])) isstr = i;

250. repr[i] = lj_typename(&L->base[i]);

251. }

252. }

253. if ((isenum ^ isstr) == 1)

254. lj_err_callerv(L, LJ_ERR_FFI_BADCONV, repr[isstr], repr[isenum]);

255. lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN :

256. mm == MM_concat ? LJ_ERR_FFI_BADCONCAT :

257. mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH,

258. repr[0], repr[1]);

259. }

260. return lj_meta_tailcall(L, tv);

261. }

263. /* Arithmetic operators for cdata. */

264. ‌int lj_carith_op(lua_State *L, MMS mm)

265. {

266. CTState *cts = ctype_cts(L);

267. CDArith ca;

268. if (carith_checkarg(L, cts, &ca)) {

269. if (carith_int64(L, cts, &ca, mm) || carith_ptr(L, cts, &ca, mm)) {

270. copyTV(L, &G(L)->tmptv2, L->top-1);  /* Remember for trace recorder. */

271. return 1;

272. }

273. }

274. return lj_carith_meta(L, cts, &ca, mm);

275. }

277. /* -- 64 bit bit operations helpers --------------------------------------- */

279. #if LJ_64

280. ‌#define B64DEF(name) \

281. static LJ_AINLINE uint64_t lj_carith_##name(uint64_t x, int32_t sh)

282. #else

283. /* Not inlined on 32 bit archs, since some of these are quite lengthy. */

284. ‌#define B64DEF(name) \

285. uint64_t LJ_NOINLINE lj_carith_##name(uint64_t x, int32_t sh)

286. #endif

288. ‌B64DEF(shl64) { return x << (sh&63); }

289. ‌B64DEF(shr64) { return x >> (sh&63); }

290. ‌B64DEF(sar64) { return (uint64_t)((int64_t)x >> (sh&63)); }

291. ‌B64DEF(rol64) { return lj_rol(x, (sh&63)); }

292. ‌B64DEF(ror64) { return lj_ror(x, (sh&63)); }

294. ‌#undef B64DEF

296. ‌uint64_t lj_carith_shift64(uint64_t x, int32_t sh, int op)

297. {

298. switch (op) {

299. case IR_BSHL-IR_BSHL: x = lj_carith_shl64(x, sh); break;

300. case IR_BSHR-IR_BSHL: x = lj_carith_shr64(x, sh); break;

301. case IR_BSAR-IR_BSHL: x = lj_carith_sar64(x, sh); break;

302. case IR_BROL-IR_BSHL: x = lj_carith_rol64(x, sh); break;

303. case IR_BROR-IR_BSHL: x = lj_carith_ror64(x, sh); break;

304. default: lua_assert(0); break;

305. }

306. return x;

307. }

309. /* Equivalent to lj_lib_checkbit(), but handles cdata. */

310. ‌uint64_t lj_carith_check64(lua_State *L, int narg, CTypeID *id)

311. {

312. TValue *o = L->base + narg-1;

313. if (o >= L->top) {

314. err:

315. lj_err_argt(L, narg, LUA_TNUMBER);

316. } else if (LJ_LIKELY(tvisnumber(o))) {

317. /* Handled below. */

318. } else if (tviscdata(o)) {

319. CTState *cts = ctype_cts(L);

320. uint8_t *sp = (uint8_t *)cdataptr(cdataV(o));

321. CTypeID sid = cdataV(o)->ctypeid;

322. CType *s = ctype_get(cts, sid);

323. uint64_t x;

324. if (ctype_isref(s->info)) {

325. sp = *(void **)sp;

326. sid = ctype_cid(s->info);

327. }

328. s = ctype_raw(cts, sid);

329. if (ctype_isenum(s->info)) s = ctype_child(cts, s);

330. if ((s->info & (CTMASK_NUM|CTF_BOOL|CTF_FP|CTF_UNSIGNED)) ==

331. CTINFO(CT_NUM, CTF_UNSIGNED) && s->size == 8)

332. *id = CTID_UINT64;  /* Use uint64_t, since it has the highest rank. */

333. else if (!*id)

334. *id = CTID_INT64;  /* Use int64_t, unless already set. */

335. lj_cconv_ct_ct(cts, ctype_get(cts, *id), s,

336. (uint8_t *)&x, sp, CCF_ARG(narg));

337. return x;

338. } else if (!(tvisstr(o) && lj_strscan_number(strV(o), o))) {

339. goto err;

340. }

341. if (LJ_LIKELY(tvisint(o))) {

342. return (uint32_t)intV(o);

343. } else {

344. int32_t i = lj_num2bit(numV(o));

345. if (LJ_DUALNUM) setintV(o, i);

346. return (uint32_t)i;

347. }

348. }

351. /* -- 64 bit integer arithmetic helpers ----------------------------------- */

353. #if LJ_32 && LJ_HASJIT

354. /* Signed/unsigned 64 bit multiplication. */

355. ‌int64_t lj_carith_mul64(int64_t a, int64_t b)

356. {

357. return a * b;

358. }

359. #endif

361. /* Unsigned 64 bit division. */

362. ‌uint64_t lj_carith_divu64(uint64_t a, uint64_t b)

363. {

364. if (b == 0) return U64x(80000000,00000000);

365. return a / b;

366. }

368. /* Signed 64 bit division. */

369. ‌int64_t lj_carith_divi64(int64_t a, int64_t b)

370. {

371. if (b == 0 || (a == (int64_t)U64x(80000000,00000000) && b == -1))

372. return U64x(80000000,00000000);

373. return a / b;

374. }

376. /* Unsigned 64 bit modulo. */

377. ‌uint64_t lj_carith_modu64(uint64_t a, uint64_t b)

378. {

379. if (b == 0) return U64x(80000000,00000000);

380. return a % b;

381. }

383. /* Signed 64 bit modulo. */

384. ‌int64_t lj_carith_modi64(int64_t a, int64_t b)

385. {

386. if (b == 0) return U64x(80000000,00000000);

387. if (a == (int64_t)U64x(80000000,00000000) && b == -1) return 0;

388. return a % b;

389. }

391. /* Unsigned 64 bit x^k. */

392. ‌uint64_t lj_carith_powu64(uint64_t x, uint64_t k)

393. {

394. uint64_t y;

395. if (k == 0)

396. return 1;

397. for (; (k & 1) == 0; k >>= 1) x *= x;

398. y = x;

399. if ((k >>= 1) != 0) {

400. for (;;) {

401. x *= x;

402. if (k == 1) break;

403. if (k & 1) y *= x;

404. k >>= 1;

405. }

406. y *= x;

407. }

408. return y;

409. }

411. /* Signed 64 bit x^k. */

412. ‌int64_t lj_carith_powi64(int64_t x, int64_t k)

413. {

414. if (k == 0)

415. return 1;

416. if (k < 0) {

417. if (x == 0)

418. return U64x(7fffffff,ffffffff);

419. else if (x == 1)

420. return 1;

421. else if (x == -1)

422. return (k & 1) ? -1 : 1;

423. else

424. return 0;

425. }

426. return (int64_t)lj_carith_powu64((uint64_t)x, (uint64_t)k);

427. }

429. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_jit.c - luajit-2.0-src

Global variables defined

- KEY_PROFILE_FUNC

- KEY_PROFILE_THREAD

- jit_param_default

- jit_trlinkname

Functions defined

- LJLIB_CF(jit_on)

- LJLIB_CF(jit_off)

- LJLIB_CF(jit_flush)

- LJLIB_CF(jit_status)

- LJLIB_CF(jit_attach)

- LJLIB_CF(jit_util_funcinfo)

- LJLIB_CF(jit_util_funcbc)

- LJLIB_CF(jit_util_funck)

- LJLIB_CF(jit_util_funcuvname)

- LJLIB_CF(jit_util_traceinfo)

- LJLIB_CF(jit_util_traceir)

- LJLIB_CF(jit_util_tracek)

- LJLIB_CF(jit_util_tracesnap)

- LJLIB_CF(jit_util_tracemc)

- LJLIB_CF(jit_util_traceexitstub)

- LJLIB_CF(jit_util_ircalladdr)

- LJLIB_CF(jit_opt_start)

- LJLIB_CF(jit_profile_start)

- LJLIB_CF(jit_profile_stop)

- LJLIB_CF(jit_profile_dumpstack)

- check_Lproto

- flagbits_to_strings

- jit_checktrace

- jit_cpudetect

- jit_init

- jit_profile_callback

- jitopt_flag

- jitopt_level

- jitopt_param

- luaopen_jit

- luaopen_jit_profile

- luaopen_jit_util

- setintfield

- setjitmode

Macros defined

- JIT_F_SSE2

- JIT_PARAMINIT

- JIT_PARAMINIT

- LJLIB_MODULE_jit

- LJLIB_MODULE_jit_opt

- LJLIB_MODULE_jit_profile

- LJLIB_MODULE_jit_util

- LUA_LIB

- lib_jit_c

Source code

1. /*

2. ** JIT library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lib_jit_c

7. ‌#define LUA_LIB

9. #include "lua.h"

10. #include "lauxlib.h"

11. #include "lualib.h"

13. #include "lj_obj.h"

14. #include "lj_gc.h"

15. #include "lj_err.h"

16. #include "lj_debug.h"

17. #include "lj_str.h"

18. #include "lj_tab.h"

19. #include "lj_state.h"

20. #include "lj_bc.h"

21. #if LJ_HASFFI

22. #include "lj_ctype.h"

23. #endif

24. #if LJ_HASJIT

25. #include "lj_ir.h"

26. #include "lj_jit.h"

27. #include "lj_ircall.h"

28. #include "lj_iropt.h"

29. #include "lj_target.h"

30. #endif

31. #include "lj_trace.h"

32. #include "lj_dispatch.h"

33. #include "lj_vm.h"

34. #include "lj_vmevent.h"

35. #include "lj_lib.h"

37. #include "luajit.h"

39. /* -- jit.* functions ----------------------------------------------------- */

41. ‌#define LJLIB_MODULE_jit

43. ‌static int setjitmode(lua_State *L, int mode)

44. {

45. int idx = 0;

46. if (L->base == L->top || tvisnil(L->base)) {  /* jit.on/off/flush([nil]) */

47. mode |= LUAJIT_MODE_ENGINE;

48. } else {

49. /* jit.on/off/flush(func|proto, nil|true|false) */

50. if (tvisfunc(L->base) || tvisproto(L->base))

51. idx = 1;

52. else if (!tvistrue(L->base))  /* jit.on/off/flush(true, nil|true|false) */

53. goto err;

54. if (L->base+1 < L->top && tvisbool(L->base+1))

55. mode |= boolV(L->base+1) ? LUAJIT_MODE_ALLFUNC : LUAJIT_MODE_ALLSUBFUNC;

56. else

57. mode |= LUAJIT_MODE_FUNC;

58. }

59. if (luaJIT_setmode(L, idx, mode) != 1) {

60. if ((mode & LUAJIT_MODE_MASK) == LUAJIT_MODE_ENGINE)

61. lj_err_caller(L, LJ_ERR_NOJIT);

62. err:

63. lj_err_argt(L, 1, LUA_TFUNCTION);

64. }

65. return 0;

66. }

68. ‌LJLIB_CF(jit_on)

69. {

70. return setjitmode(L, LUAJIT_MODE_ON);

71. }

73. ‌LJLIB_CF(jit_off)

74. {

75. return setjitmode(L, LUAJIT_MODE_OFF);

76. }

78. ‌LJLIB_CF(jit_flush)

79. {

80. #if LJ_HASJIT

81. if (L->base < L->top && tvisnumber(L->base)) {

82. int traceno = lj_lib_checkint(L, 1);

83. luaJIT_setmode(L, traceno, LUAJIT_MODE_FLUSH|LUAJIT_MODE_TRACE);

84. return 0;

85. }

86. #endif

87. return setjitmode(L, LUAJIT_MODE_FLUSH);

88. }

90. #if LJ_HASJIT

91. /* Push a string for every flag bit that is set. */

92. ‌static void flagbits_to_strings(lua_State *L, uint32_t flags, uint32_t base,

93. const char *str)

94. {

95. for (; *str; base <<= 1, str += 1+*str)

96. if (flags & base)

97. setstrV(L, L->top++, lj_str_new(L, str+1, *(uint8_t *)str));

98. }

99. #endif

101. ‌LJLIB_CF(jit_status)

102. {

103. #if LJ_HASJIT

104. jit_State *J = L2J(L);

105. L->top = L->base;

106. setboolV(L->top++, (J->flags & JIT_F_ON) ? 1 : 0);

107. flagbits_to_strings(L, J->flags, JIT_F_CPU_FIRST, JIT_F_CPUSTRING);

108. flagbits_to_strings(L, J->flags, JIT_F_OPT_FIRST, JIT_F_OPTSTRING);

109. return (int)(L->top - L->base);

110. #else

111. setboolV(L->top++, 0);

112. return 1;

113. #endif

114. }

116. ‌LJLIB_CF(jit_attach)

117. {

118. #ifdef LUAJIT_DISABLE_VMEVENT

119. luaL_error(L, "vmevent API disabled");

120. #else

121. GCfunc *fn = lj_lib_checkfunc(L, 1);

122. GCstr *s = lj_lib_optstr(L, 2);

123. luaL_findtable(L, LUA_REGISTRYINDEX, LJ_VMEVENTS_REGKEY, LJ_VMEVENTS_HSIZE);

124. if (s) {  /* Attach to given event. */

125. const uint8_t *p = (const uint8_t *)strdata(s);

126. uint32_t h = s->len;

127. while (*p) h = h ^ (lj_rol(h, 6) + *p++);

128. lua_pushvalue(L, 1);

129. lua_rawseti(L, -2, VMEVENT_HASHIDX(h));

130. G(L)->vmevmask = VMEVENT_NOCACHE;  /* Invalidate cache. */

131. } else {  /* Detach if no event given. */

132. setnilV(L->top++);

133. while (lua_next(L, -2)) {

134. L->top--;

135. if (tvisfunc(L->top) && funcV(L->top) == fn) {

136. setnilV(lj_tab_set(L, tabV(L->top-2), L->top-1));

137. }

138. }

139. }

140. #endif

141. return 0;

142. }

144. LJLIB_PUSH(top-5) LJLIB_SET(os)

145. LJLIB_PUSH(top-4) LJLIB_SET(arch)

146. LJLIB_PUSH(top-3) LJLIB_SET(version_num)

147. LJLIB_PUSH(top-2) LJLIB_SET(version)

149. #include "lj_libdef.h"

151. /* -- jit.util.* functions ------------------------------------------------ */

153. ‌#define LJLIB_MODULE_jit_util

155. /* -- Reflection API for Lua functions ------------------------------------ */

157. /* Return prototype of first argument (Lua function or prototype object) */

158. ‌static GCproto *check_Lproto(lua_State *L, int nolua)

159. {

160. TValue *o = L->base;

161. if (L->top > o) {

162. if (tvisproto(o)) {

163. return protoV(o);

164. } else if (tvisfunc(o)) {

165. if (isluafunc(funcV(o)))

166. return funcproto(funcV(o));

167. else if (nolua)

168. return NULL;

169. }

170. }

171. lj_err_argt(L, 1, LUA_TFUNCTION);

172. return NULL;  /* unreachable */

173. }

175. ‌static void setintfield(lua_State *L, GCtab *t, const char *name, int32_t val)

176. {

177. setintV(lj_tab_setstr(L, t, lj_str_newz(L, name)), val);

178. }

180. /* local info = jit.util.funcinfo(func [,pc]) */

181. ‌LJLIB_CF(jit_util_funcinfo)

182. {

183. GCproto *pt = check_Lproto(L, 1);

184. if (pt) {

185. BCPos pc = (BCPos)lj_lib_optint(L, 2, 0);

186. GCtab *t;

187. lua_createtable(L, 0, 16);  /* Increment hash size if fields are added. */

188. t = tabV(L->top-1);

189. setintfield(L, t, "linedefined", pt->firstline);

190. setintfield(L, t, "lastlinedefined", pt->firstline + pt->numline);

191. setintfield(L, t, "stackslots", pt->framesize);

192. setintfield(L, t, "params", pt->numparams);

193. setintfield(L, t, "bytecodes", (int32_t)pt->sizebc);

194. setintfield(L, t, "gcconsts", (int32_t)pt->sizekgc);

195. setintfield(L, t, "nconsts", (int32_t)pt->sizekn);

196. setintfield(L, t, "upvalues", (int32_t)pt->sizeuv);

197. if (pc < pt->sizebc)

198. setintfield(L, t, "currentline", lj_debug_line(pt, pc));

199. lua_pushboolean(L, (pt->flags & PROTO_VARARG));

200. lua_setfield(L, -2, "isvararg");

201. lua_pushboolean(L, (pt->flags & PROTO_CHILD));

202. lua_setfield(L, -2, "children");

203. setstrV(L, L->top++, proto_chunkname(pt));

204. lua_setfield(L, -2, "source");

205. lj_debug_pushloc(L, pt, pc);

206. lua_setfield(L, -2, "loc");

207. } else {

208. GCfunc *fn = funcV(L->base);

209. GCtab *t;

210. lua_createtable(L, 0, 4);  /* Increment hash size if fields are added. */

211. t = tabV(L->top-1);

212. if (!iscfunc(fn))

213. setintfield(L, t, "ffid", fn->c.ffid);

214. setintptrV(lj_tab_setstr(L, t, lj_str_newlit(L, "addr")),

215. (intptr_t)(void *)fn->c.f);

216. setintfield(L, t, "upvalues", fn->c.nupvalues);

217. }

218. return 1;

219. }

221. /* local ins, m = jit.util.funcbc(func, pc) */

222. ‌LJLIB_CF(jit_util_funcbc)

223. {

224. GCproto *pt = check_Lproto(L, 0);

225. BCPos pc = (BCPos)lj_lib_checkint(L, 2);

226. if (pc < pt->sizebc) {

227. BCIns ins = proto_bc(pt)[pc];

228. BCOp op = bc_op(ins);

229. lua_assert(op < BC__MAX);

230. setintV(L->top, ins);

231. setintV(L->top+1, lj_bc_mode[op]);

232. L->top += 2;

233. return 2;

234. }

235. return 0;

236. }

238. /* local k = jit.util.funck(func, idx) */

239. ‌LJLIB_CF(jit_util_funck)

240. {

241. GCproto *pt = check_Lproto(L, 0);

242. ptrdiff_t idx = (ptrdiff_t)lj_lib_checkint(L, 2);

243. if (idx >= 0) {

244. if (idx < (ptrdiff_t)pt->sizekn) {

245. copyTV(L, L->top-1, proto_knumtv(pt, idx));

246. return 1;

247. }

248. } else {

249. if (~idx < (ptrdiff_t)pt->sizekgc) {

250. GCobj *gc = proto_kgc(pt, idx);

251. setgcV(L, L->top-1, gc, ~gc->gch.gct);

252. return 1;

253. }

254. }

255. return 0;

256. }

258. /* local name = jit.util.funcuvname(func, idx) */

259. ‌LJLIB_CF(jit_util_funcuvname)

260. {

261. GCproto *pt = check_Lproto(L, 0);

262. uint32_t idx = (uint32_t)lj_lib_checkint(L, 2);

263. if (idx < pt->sizeuv) {

264. setstrV(L, L->top-1, lj_str_newz(L, lj_debug_uvname(pt, idx)));

265. return 1;

266. }

267. return 0;

268. }

270. /* -- Reflection API for traces ------------------------------------------- */

272. #if LJ_HASJIT

274. /* Check trace argument. Must not throw for non-existent trace numbers. */

275. ‌static GCtrace *jit_checktrace(lua_State *L)

276. {

277. TraceNo tr = (TraceNo)lj_lib_checkint(L, 1);

278. jit_State *J = L2J(L);

279. if (tr > 0 && tr < J->sizetrace)

280. return traceref(J, tr);

281. return NULL;

282. }

284. /* Names of link types. ORDER LJ_TRLINK */

285. ‌static const char *const jit_trlinkname[] = {

286. "none", "root", "loop", "tail-recursion", "up-recursion", "down-recursion",

287. "interpreter", "return", "stitch"

288. };

290. /* local info = jit.util.traceinfo(tr) */

291. ‌LJLIB_CF(jit_util_traceinfo)

292. {

293. GCtrace *T = jit_checktrace(L);

294. if (T) {

295. GCtab *t;

296. lua_createtable(L, 0, 8);  /* Increment hash size if fields are added. */

297. t = tabV(L->top-1);

298. setintfield(L, t, "nins", (int32_t)T->nins - REF_BIAS - 1);

299. setintfield(L, t, "nk", REF_BIAS - (int32_t)T->nk);

300. setintfield(L, t, "link", T->link);

301. setintfield(L, t, "nexit", T->nsnap);

302. setstrV(L, L->top++, lj_str_newz(L, jit_trlinkname[T->linktype]));

303. lua_setfield(L, -2, "linktype");

304. /* There are many more fields. Add them only when needed. */

305. return 1;

306. }

307. return 0;

308. }

310. /* local m, ot, op1, op2, prev = jit.util.traceir(tr, idx) */

311. ‌LJLIB_CF(jit_util_traceir)

312. {

313. GCtrace *T = jit_checktrace(L);

314. IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS;

315. if (T && ref >= REF_BIAS && ref < T->nins) {

316. IRIns *ir = &T->ir[ref];

317. int32_t m = lj_ir_mode[ir->o];

318. setintV(L->top-2, m);

319. setintV(L->top-1, ir->ot);

320. setintV(L->top++, (int32_t)ir->op1 - (irm_op1(m)==IRMref ? REF_BIAS : 0));

321. setintV(L->top++, (int32_t)ir->op2 - (irm_op2(m)==IRMref ? REF_BIAS : 0));

322. setintV(L->top++, ir->prev);

323. return 5;

324. }

325. return 0;

326. }

328. /* local k, t [, slot] = jit.util.tracek(tr, idx) */

329. ‌LJLIB_CF(jit_util_tracek)

330. {

331. GCtrace *T = jit_checktrace(L);

332. IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS;

333. if (T && ref >= T->nk && ref < REF_BIAS) {

334. IRIns *ir = &T->ir[ref];

335. int32_t slot = -1;

336. if (ir->o == IR_KSLOT) {

337. slot = ir->op2;

338. ir = &T->ir[ir->op1];

339. }

340. #if LJ_HASFFI

341. if (ir->o == IR_KINT64 && !ctype_ctsG(G(L))) {

342. ptrdiff_t oldtop = savestack(L, L->top);

343. luaopen_ffi(L);  /* Load FFI library on-demand. */

344. L->top = restorestack(L, oldtop);

345. }

346. #endif

347. lj_ir_kvalue(L, L->top-2, ir);

348. setintV(L->top-1, (int32_t)irt_type(ir->t));

349. if (slot == -1)

350. return 2;

351. setintV(L->top++, slot);

352. return 3;

353. }

354. return 0;

355. }

357. /* local snap = jit.util.tracesnap(tr, sn) */

358. ‌LJLIB_CF(jit_util_tracesnap)

359. {

360. GCtrace *T = jit_checktrace(L);

361. SnapNo sn = (SnapNo)lj_lib_checkint(L, 2);

362. if (T && sn < T->nsnap) {

363. SnapShot *snap = &T->snap[sn];

364. SnapEntry *map = &T->snapmap[snap->mapofs];

365. MSize n, nent = snap->nent;

366. GCtab *t;

367. lua_createtable(L, nent+2, 0);

368. t = tabV(L->top-1);

369. setintV(lj_tab_setint(L, t, 0), (int32_t)snap->ref - REF_BIAS);

370. setintV(lj_tab_setint(L, t, 1), (int32_t)snap->nslots);

371. for (n = 0; n < nent; n++)

372. setintV(lj_tab_setint(L, t, (int32_t)(n+2)), (int32_t)map[n]);

373. setintV(lj_tab_setint(L, t, (int32_t)(nent+2)), (int32_t)SNAP(255, 0, 0));

374. return 1;

375. }

376. return 0;

377. }

379. /* local mcode, addr, loop = jit.util.tracemc(tr) */

380. ‌LJLIB_CF(jit_util_tracemc)

381. {

382. GCtrace *T = jit_checktrace(L);

383. if (T && T->mcode != NULL) {

384. setstrV(L, L->top-1, lj_str_new(L, (const char *)T->mcode, T->szmcode));

385. setintptrV(L->top++, (intptr_t)(void *)T->mcode);

386. setintV(L->top++, T->mcloop);

387. return 3;

388. }

389. return 0;

390. }

392. /* local addr = jit.util.traceexitstub([tr,] exitno) */

393. ‌LJLIB_CF(jit_util_traceexitstub)

394. {

395. #ifdef EXITSTUBS_PER_GROUP

396. ExitNo exitno = (ExitNo)lj_lib_checkint(L, 1);

397. jit_State *J = L2J(L);

398. if (exitno < EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) {

399. setintptrV(L->top-1, (intptr_t)(void *)exitstub_addr(J, exitno));

400. return 1;

401. }

402. #else

403. if (L->top > L->base+1) {  /* Don't throw for one-argument variant. */

404. GCtrace *T = jit_checktrace(L);

405. ExitNo exitno = (ExitNo)lj_lib_checkint(L, 2);

406. ExitNo maxexit = T->root ? T->nsnap+1 : T->nsnap;

407. if (T && T->mcode != NULL && exitno < maxexit) {

408. setintptrV(L->top-1, (intptr_t)(void *)exitstub_trace_addr(T, exitno));

409. return 1;

410. }

411. }

412. #endif

413. return 0;

414. }

416. /* local addr = jit.util.ircalladdr(idx) */

417. ‌LJLIB_CF(jit_util_ircalladdr)

418. {

419. uint32_t idx = (uint32_t)lj_lib_checkint(L, 1);

420. if (idx < IRCALL__MAX) {

421. setintptrV(L->top-1, (intptr_t)(void *)lj_ir_callinfo[idx].func);

422. return 1;

423. }

424. return 0;

425. }

427. #endif

429. #include "lj_libdef.h"

431. ‌static int luaopen_jit_util(lua_State *L)

432. {

433. LJ_LIB_REG(L, NULL, jit_util);

434. return 1;

435. }

437. /* -- jit.opt module ------------------------------------------------------ */

439. #if LJ_HASJIT

441. ‌#define LJLIB_MODULE_jit_opt

443. /* Parse optimization level. */

444. ‌static int jitopt_level(jit_State *J, const char *str)

445. {

446. if (str[0] >= '0' && str[0] <= '9' && str[1] == '\0') {

447. uint32_t flags;

448. if (str[0] == '0') flags = JIT_F_OPT_0;

449. else if (str[0] == '1') flags = JIT_F_OPT_1;

450. else if (str[0] == '2') flags = JIT_F_OPT_2;

451. else flags = JIT_F_OPT_3;

452. J->flags = (J->flags & ~JIT_F_OPT_MASK) | flags;

453. return 1;  /* Ok. */

454. }

455. return 0;  /* No match. */

456. }

458. /* Parse optimization flag. */

459. ‌static int jitopt_flag(jit_State *J, const char *str)

460. {

461. const char *lst = JIT_F_OPTSTRING;

462. uint32_t opt;

463. int set = 1;

464. if (str[0] == '+') {

465. str++;

466. } else if (str[0] == '-') {

467. str++;

468. set = 0;

469. } else if (str[0] == 'n' && str[1] == 'o') {

470. str += str[2] == '-' ? 3 : 2;

471. set = 0;

472. }

473. for (opt = JIT_F_OPT_FIRST; ; opt <<= 1) {

474. size_t len = *(const uint8_t *)lst;

475. if (len == 0)

476. break;

477. if (strncmp(str, lst+1, len) == 0 && str[len] == '\0') {

478. if (set) J->flags |= opt; else J->flags &= ~opt;

479. return 1;  /* Ok. */

480. }

481. lst += 1+len;

482. }

483. return 0;  /* No match. */

484. }

486. /* Parse optimization parameter. */

487. ‌static int jitopt_param(jit_State *J, const char *str)

488. {

489. const char *lst = JIT_P_STRING;

490. int i;

491. for (i = 0; i < JIT_P__MAX; i++) {

492. size_t len = *(const uint8_t *)lst;

493. lua_assert(len != 0);

494. if (strncmp(str, lst+1, len) == 0 && str[len] == '=') {

495. int32_t n = 0;

496. const char *p = &str[len+1];

497. while (*p >= '0' && *p <= '9')

498. n = n*10 + (*p++ - '0');

499. if (*p) return 0;  /* Malformed number. */

500. J->param[i] = n;

501. if (i == JIT_P_hotloop)

502. lj_dispatch_init_hotcount(J2G(J));

503. return 1;  /* Ok. */

504. }

505. lst += 1+len;

506. }

507. return 0;  /* No match. */

508. }

510. /* jit.opt.start(flags...) */

511. ‌LJLIB_CF(jit_opt_start)

512. {

513. jit_State *J = L2J(L);

514. int nargs = (int)(L->top - L->base);

515. if (nargs == 0) {

516. J->flags = (J->flags & ~JIT_F_OPT_MASK) | JIT_F_OPT_DEFAULT;

517. } else {

518. int i;

519. for (i = 1; i <= nargs; i++) {

520. const char *str = strdata(lj_lib_checkstr(L, i));

521. if (!jitopt_level(J, str) &&

522. !jitopt_flag(J, str) &&

523. !jitopt_param(J, str))

524. lj_err_callerv(L, LJ_ERR_JITOPT, str);

525. }

526. }

527. return 0;

528. }

530. #include "lj_libdef.h"

532. #endif

534. /* -- jit.profile module -------------------------------------------------- */

536. #if LJ_HASPROFILE

538. ‌#define LJLIB_MODULE_jit_profile

540. /* Not loaded by default, use: local profile = require("jit.profile") */

542. ‌static const char KEY_PROFILE_THREAD = 't';

543. ‌static const char KEY_PROFILE_FUNC = 'f';

545. ‌static void jit_profile_callback(lua_State *L2, lua_State *L, int samples,

546. int vmstate)

547. {

548. TValue key;

549. cTValue *tv;

550. setlightudV(&key, (void *)&KEY_PROFILE_FUNC);

551. tv = lj_tab_get(L, tabV(registry(L)), &key);

552. if (tvisfunc(tv)) {

553. char vmst = (char)vmstate;

554. int status;

555. setfuncV(L2, L2->top++, funcV(tv));

556. setthreadV(L2, L2->top++, L);

557. setintV(L2->top++, samples);

558. setstrV(L2, L2->top++, lj_str_new(L2, &vmst, 1));

559. status = lua_pcall(L2, 3, 0, 0);  /* callback(thread, samples, vmstate) */

560. if (status) {

561. if (G(L2)->panic) G(L2)->panic(L2);

562. exit(EXIT_FAILURE);

563. }

564. lj_trace_abort(G(L2));

565. }

566. }

568. /* profile.start(mode, cb) */

569. ‌LJLIB_CF(jit_profile_start)

570. {

571. GCtab *registry = tabV(registry(L));

572. GCstr *mode = lj_lib_optstr(L, 1);

573. GCfunc *func = lj_lib_checkfunc(L, 2);

574. lua_State *L2 = lua_newthread(L);  /* Thread that runs profiler callback. */

575. TValue key;

576. /* Anchor thread and function in registry. */

577. setlightudV(&key, (void *)&KEY_PROFILE_THREAD);

578. setthreadV(L, lj_tab_set(L, registry, &key), L2);

579. setlightudV(&key, (void *)&KEY_PROFILE_FUNC);

580. setfuncV(L, lj_tab_set(L, registry, &key), func);

581. lj_gc_anybarriert(L, registry);

582. luaJIT_profile_start(L, mode ? strdata(mode) : "",

583. (luaJIT_profile_callback)jit_profile_callback, L2);

584. return 0;

585. }

587. /* profile.stop() */

588. ‌LJLIB_CF(jit_profile_stop)

589. {

590. GCtab *registry;

591. TValue key;

592. luaJIT_profile_stop(L);

593. registry = tabV(registry(L));

594. setlightudV(&key, (void *)&KEY_PROFILE_THREAD);

595. setnilV(lj_tab_set(L, registry, &key));

596. setlightudV(&key, (void *)&KEY_PROFILE_FUNC);

597. setnilV(lj_tab_set(L, registry, &key));

598. lj_gc_anybarriert(L, registry);

599. return 0;

600. }

602. /* dump = profile.dumpstack([thread,] fmt, depth) */

603. ‌LJLIB_CF(jit_profile_dumpstack)

604. {

605. lua_State *L2 = L;

606. int arg = 0;

607. size_t len;

608. int depth;

609. GCstr *fmt;

610. const char *p;

611. if (L->top > L->base && tvisthread(L->base)) {

612. L2 = threadV(L->base);

613. arg = 1;

614. }

615. fmt = lj_lib_checkstr(L, arg+1);

616. depth = lj_lib_checkint(L, arg+2);

617. p = luaJIT_profile_dumpstack(L2, strdata(fmt), depth, &len);

618. lua_pushlstring(L, p, len);

619. return 1;

620. }

622. #include "lj_libdef.h"

624. ‌static int luaopen_jit_profile(lua_State *L)

625. {

626. LJ_LIB_REG(L, NULL, jit_profile);

627. return 1;

628. }

630. #endif

632. /* -- JIT compiler initialization ----------------------------------------- */

634. #if LJ_HASJIT

635. /* Default values for JIT parameters. */

636. ‌static const int32_t jit_param_default[JIT_P__MAX+1] = {

637. ‌#define JIT_PARAMINIT(len, name, value)        (value),

638. JIT_PARAMDEF(JIT_PARAMINIT)

639. ‌#undef JIT_PARAMINIT

640. 0

641. };

642. #endif

644. #if LJ_TARGET_ARM && LJ_TARGET_LINUX

645. #include <sys/utsname.h>

646. #endif

648. /* Arch-dependent CPU detection. */

649. ‌static uint32_t jit_cpudetect(lua_State *L)

650. {

651. uint32_t flags = 0;

652. #if LJ_TARGET_X86ORX64

653. uint32_t vendor[4];

654. uint32_t features[4];

655. if (lj_vm_cpuid(0, vendor) && lj_vm_cpuid(1, features)) {

656. #if !LJ_HASJIT

657. ‌#define JIT_F_SSE2        2

658. #endif

659. flags |= ((features[3] >> 26)&1) * JIT_F_SSE2;

660. #if LJ_HASJIT

661. flags |= ((features[2] >> 0)&1) * JIT_F_SSE3;

662. flags |= ((features[2] >> 19)&1) * JIT_F_SSE4_1;

663. if (vendor[2] == 0x6c65746e) {  /* Intel. */

664. if ((features[0] & 0x0fff0ff0) == 0x000106c0)  /* Atom. */

665. flags |= JIT_F_LEA_AGU;

666. } else if (vendor[2] == 0x444d4163) {  /* AMD. */

667. uint32_t fam = (features[0] & 0x0ff00f00);

668. if (fam >= 0x00000f00)  /* K8, K10. */

669. flags |= JIT_F_PREFER_IMUL;

670. }

671. #endif

672. }

673. /* Check for required instruction set support on x86 (unnecessary on x64). */

674. #if LJ_TARGET_X86

675. if (!(flags & JIT_F_SSE2))

676. luaL_error(L, "CPU with SSE2 required");

677. #endif

678. #elif LJ_TARGET_ARM

679. #if LJ_HASJIT

680. int ver = LJ_ARCH_VERSION;  /* Compile-time ARM CPU detection. */

681. #if LJ_TARGET_LINUX

682. if (ver < 70) {  /* Runtime ARM CPU detection. */

683. struct utsname ut;

684. uname(&ut);

685. if (strncmp(ut.machine, "armv", 4) == 0) {

686. if (ut.machine[4] >= '7')

687. ver = 70;

688. else if (ut.machine[4] == '6')

689. ver = 60;

690. }

691. }

692. #endif

693. flags |= ver >= 70 ? JIT_F_ARMV7 :

694. ver >= 61 ? JIT_F_ARMV6T2_ :

695. ver >= 60 ? JIT_F_ARMV6_ : 0;

696. flags |= LJ_ARCH_HASFPU == 0 ? 0 : ver >= 70 ? JIT_F_VFPV3 : JIT_F_VFPV2;

697. #endif

698. #elif LJ_TARGET_ARM64

699. /* No optional CPU features to detect (for now). */

700. #elif LJ_TARGET_PPC

701. #if LJ_HASJIT

702. #if LJ_ARCH_SQRT

703. flags |= JIT_F_SQRT;

704. #endif

705. #if LJ_ARCH_ROUND

706. flags |= JIT_F_ROUND;

707. #endif

708. #endif

709. #elif LJ_TARGET_MIPS

710. #if LJ_HASJIT

711. /* Compile-time MIPS CPU detection. */

712. #if LJ_ARCH_VERSION >= 20

713. flags |= JIT_F_MIPS32R2;

714. #endif

715. /* Runtime MIPS CPU detection. */

716. #if defined(__GNUC__)

717. if (!(flags & JIT_F_MIPS32R2)) {

718. int x;

719. /* On MIPS32R1 rotr is treated as srl. rotr r2,r2,1 -> srl r2,r2,1. */

720. __asm__("li $2, 1\n\t.long 0x00221042\n\tmove %0, $2" : "=r"(x) : : "$2");

721. if (x) flags |= JIT_F_MIPS32R2;  /* Either 0x80000000 (R2) or 0 (R1). */

722. }

723. #endif

724. #endif

725. #else

726. #error "Missing CPU detection for this architecture"

727. #endif

728. UNUSED(L);

729. return flags;

730. }

732. /* Initialize JIT compiler. */

733. ‌static void jit_init(lua_State *L)

734. {

735. uint32_t flags = jit_cpudetect(L);

736. #if LJ_HASJIT

737. jit_State *J = L2J(L);

738. J->flags = flags | JIT_F_ON | JIT_F_OPT_DEFAULT;

739. memcpy(J->param, jit_param_default, sizeof(J->param));

740. lj_dispatch_update(G(L));

741. #else

742. UNUSED(flags);

743. #endif

744. }

746. ‌LUALIB_API int luaopen_jit(lua_State *L)

747. {

748. jit_init(L);

749. lua_pushliteral(L, LJ_OS_NAME);

750. lua_pushliteral(L, LJ_ARCH_NAME);

751. lua_pushinteger(L, LUAJIT_VERSION_NUM);

752. lua_pushliteral(L, LUAJIT_VERSION);

753. LJ_LIB_REG(L, LUA_JITLIBNAME, jit);

754. #if LJ_HASPROFILE

755. lj_lib_prereg(L, LUA_JITLIBNAME ".profile", luaopen_jit_profile,

756. tabref(L->env));

757. #endif

758. #ifndef LUAJIT_DISABLE_JITUTIL

759. lj_lib_prereg(L, LUA_JITLIBNAME ".util", luaopen_jit_util, tabref(L->env));

760. #endif

761. #if LJ_HASJIT

762. LJ_LIB_REG(L, "jit.opt", jit_opt);

763. #endif

764. L->top -= 2;

765. return 1;

766. }

One Level Up                               Top Level

One Level Up                               Top Level

src/luajit.h - luajit-2.0-src

Data types defined

- luaJIT_profile_callback

Macros defined

- LUAJIT_COPYRIGHT

- LUAJIT_MODE_FLUSH

- LUAJIT_MODE_MASK

- LUAJIT_MODE_OFF

- LUAJIT_MODE_ON

- LUAJIT_URL

- LUAJIT_VERSION

- LUAJIT_VERSION_NUM

- LUAJIT_VERSION_SYM

- _LUAJIT_H

Source code

1. /*

2. ** LuaJIT -- a Just-In-Time Compiler for Lua. http://luajit.org/

3. **

4. ** Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. **

6. ** Permission is hereby granted, free of charge, to any person obtaining

7. ** a copy of this software and associated documentation files (the

8. ** "Software"), to deal in the Software without restriction, including

9. ** without limitation the rights to use, copy, modify, merge, publish,

10. ** distribute, sublicense, and/or sell copies of the Software, and to

11. ** permit persons to whom the Software is furnished to do so, subject to

12. ** the following conditions:

13. **

14. ** The above copyright notice and this permission notice shall be

15. ** included in all copies or substantial portions of the Software.

16. **

17. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

18. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

19. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

20. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY

21. ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,

22. ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE

23. ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

24. **

25. ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]

26. */

28. #ifndef _LUAJIT_H

29. ‌#define _LUAJIT_H

31. #include "lua.h"

33. ‌#define LUAJIT_VERSION                "LuaJIT 2.1.0-alpha"

34. ‌#define LUAJIT_VERSION_NUM        20100  /* Version 2.1.0 = 02.01.00. */

35. ‌#define LUAJIT_VERSION_SYM        luaJIT_version_2_1_0_alpha

36. ‌#define LUAJIT_COPYRIGHT        "Copyright (C) 2005-2015 Mike Pall"

37. ‌#define LUAJIT_URL                "http://luajit.org/"

39. /* Modes for luaJIT_setmode. */

40. ‌#define LUAJIT_MODE_MASK        0x00ff

42. enum {

43. LUAJIT_MODE_ENGINE,                /* Set mode for whole JIT engine. */

44. LUAJIT_MODE_DEBUG,                /* Set debug mode (idx = level). */

46. LUAJIT_MODE_FUNC,                /* Change mode for a function. */

47. LUAJIT_MODE_ALLFUNC,                /* Recurse into subroutine protos. */

48. LUAJIT_MODE_ALLSUBFUNC,        /* Change only the subroutines. */

50. LUAJIT_MODE_TRACE,                /* Flush a compiled trace. */

52. LUAJIT_MODE_WRAPCFUNC = 0x10,        /* Set wrapper mode for C function calls. */

54. LUAJIT_MODE_MAX

55. };

57. /* Flags or'ed in to the mode. */

58. ‌#define LUAJIT_MODE_OFF                0x0000        /* Turn feature off. */

59. ‌#define LUAJIT_MODE_ON                0x0100        /* Turn feature on. */

60. ‌#define LUAJIT_MODE_FLUSH        0x0200        /* Flush JIT-compiled code. */

62. /* LuaJIT public C API. */

64. /* Control the JIT engine. */

65. LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);

67. /* Low-overhead profiling API. */

68. ‌typedef void (*luaJIT_profile_callback)(void *data, lua_State *L,

69. int samples, int vmstate);

70. LUA_API void luaJIT_profile_start(lua_State *L, const char *mode,

71. luaJIT_profile_callback cb, void *data);

72. LUA_API void luaJIT_profile_stop(lua_State *L);

73. LUA_API const char *luaJIT_profile_dumpstack(lua_State *L, const char *fmt,

74. int depth, size_t *len);

76. /* Enforce (dynamic) linker error for version mismatches. Call from main. */

77. LUA_API void LUAJIT_VERSION_SYM(void);

79. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_profile.c - luajit-2.0-src

Global variables defined

- profile_state

Data types defined

- ProfileState

- ProfileState

- WMM_TPFUNC

Functions defined

- lj_profile_hook_enter

- lj_profile_hook_leave

- lj_profile_interpreter

- luaJIT_profile_dumpstack

- luaJIT_profile_start

- luaJIT_profile_stop

- profile_signal

- profile_thread

- profile_thread

- profile_timer_start

- profile_timer_start

- profile_timer_start

- profile_timer_stop

- profile_timer_stop

- profile_timer_stop

- profile_trigger

Macros defined

- LJ_PROFILE_INTERVAL_DEFAULT

- LUA_CORE

- WIN32_LEAN_AND_MEAN

- lj_profile_c

- profile_lock

- profile_lock

- profile_lock

- profile_unlock

- profile_unlock

- profile_unlock

Source code

1. /*

2. ** Low-overhead profiling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_profile_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASPROFILE

13. #include "lj_buf.h"

14. #include "lj_frame.h"

15. #include "lj_debug.h"

16. #include "lj_dispatch.h"

17. #if LJ_HASJIT

18. #include "lj_jit.h"

19. #include "lj_trace.h"

20. #endif

21. #include "lj_profile.h"

23. #include "luajit.h"

25. #if LJ_PROFILE_SIGPROF

27. #include <sys/time.h>

28. #include <signal.h>

29. ‌#define profile_lock(ps)        UNUSED(ps)

30. ‌#define profile_unlock(ps)        UNUSED(ps)

32. #elif LJ_PROFILE_PTHREAD

34. #include <pthread.h>

35. #include <time.h>

36. #if LJ_TARGET_PS3

37. #include <sys/timer.h>

38. #endif

39. ‌#define profile_lock(ps)        pthread_mutex_lock(&ps->lock)

40. ‌#define profile_unlock(ps)        pthread_mutex_unlock(&ps->lock)

42. #elif LJ_PROFILE_WTHREAD

44. ‌#define WIN32_LEAN_AND_MEAN

45. #if LJ_TARGET_XBOX360

46. #include <xtl.h>

47. #include <xbox.h>

48. #else

49. #include <windows.h>

50. #endif

51. ‌typedef unsigned int (WINAPI *WMM_TPFUNC)(unsigned int);

52. ‌#define profile_lock(ps)        EnterCriticalSection(&ps->lock)

53. ‌#define profile_unlock(ps)        LeaveCriticalSection(&ps->lock)

55. #endif

57. /* Profiler state. */

58. ‌typedef struct ProfileState {

59. global_State *g;                /* VM state that started the profiler. */

60. luaJIT_profile_callback cb;        /* Profiler callback. */

61. void *data;                        /* Profiler callback data. */

62. SBuf sb;                        /* String buffer for stack dumps. */

63. int interval;                        /* Sample interval in milliseconds. */

64. int samples;                        /* Number of samples for next callback. */

65. int vmstate;                        /* VM state when profile timer triggered. */

66. #if LJ_PROFILE_SIGPROF

67. struct sigaction oldsa;        /* Previous SIGPROF state. */

68. #elif LJ_PROFILE_PTHREAD

69. pthread_mutex_t lock;                /* g->hookmask update lock. */

70. pthread_t thread;                /* Timer thread. */

71. int abort;                        /* Abort timer thread. */

72. #elif LJ_PROFILE_WTHREAD

73. #if LJ_TARGET_WINDOWS

74. HINSTANCE wmm;                /* WinMM library handle. */

75. WMM_TPFUNC wmm_tbp;                /* WinMM timeBeginPeriod function. */

76. WMM_TPFUNC wmm_tep;                /* WinMM timeEndPeriod function. */

77. #endif

78. CRITICAL_SECTION lock;        /* g->hookmask update lock. */

79. HANDLE thread;                /* Timer thread. */

80. int abort;                        /* Abort timer thread. */

81. #endif

82. ‌} ProfileState;

84. /* Sadly, we have to use a static profiler state.

85. **

86. ** The SIGPROF variant needs a static pointer to the global state, anyway.

87. ** And it would be hard to extend for multiple threads. You can still use

88. ** multiple VMs in multiple threads, but only profile one at a time.

89. */

90. ‌static ProfileState profile_state;

92. /* Default sample interval in milliseconds. */

93. ‌#define LJ_PROFILE_INTERVAL_DEFAULT        10

95. /* -- Profiler/hook interaction ------------------------------------------- */

97. #if !LJ_PROFILE_SIGPROF

98. ‌void LJ_FASTCALL lj_profile_hook_enter(global_State *g)

99. {

100. ProfileState *ps = &profile_state;

101. if (ps->g) {

102. profile_lock(ps);

103. hook_enter(g);

104. profile_unlock(ps);

105. } else {

106. hook_enter(g);

107. }

108. }

110. ‌void LJ_FASTCALL lj_profile_hook_leave(global_State *g)

111. {

112. ProfileState *ps = &profile_state;

113. if (ps->g) {

114. profile_lock(ps);

115. hook_leave(g);

116. profile_unlock(ps);

117. } else {

118. hook_leave(g);

119. }

120. }

121. #endif

123. /* -- Profile callbacks --------------------------------------------------- */

125. /* Callback from profile hook (HOOK_PROFILE already cleared). */

126. ‌void LJ_FASTCALL lj_profile_interpreter(lua_State *L)

127. {

128. ProfileState *ps = &profile_state;

129. global_State *g = G(L);

130. uint8_t mask;

131. profile_lock(ps);

132. mask = (g->hookmask & ~HOOK_PROFILE);

133. if (!(mask & HOOK_VMEVENT)) {

134. int samples = ps->samples;

135. ps->samples = 0;

136. g->hookmask = HOOK_VMEVENT;

137. lj_dispatch_update(g);

138. profile_unlock(ps);

139. ps->cb(ps->data, L, samples, ps->vmstate);  /* Invoke user callback. */

140. profile_lock(ps);

141. mask |= (g->hookmask & HOOK_PROFILE);

142. }

143. g->hookmask = mask;

144. lj_dispatch_update(g);

145. profile_unlock(ps);

146. }

148. /* Trigger profile hook. Asynchronous call from OS-specific profile timer. */

149. ‌static void profile_trigger(ProfileState *ps)

150. {

151. global_State *g = ps->g;

152. uint8_t mask;

153. profile_lock(ps);

154. ps->samples++;  /* Always increment number of samples. */

155. mask = g->hookmask;

156. if (!(mask & (HOOK_PROFILE|HOOK_VMEVENT))) {  /* Set profile hook. */

157. int st = g->vmstate;

158. ps->vmstate = st >= 0 ? 'N' :

159. st == ~LJ_VMST_INTERP ? 'I' :

160. st == ~LJ_VMST_C ? 'C' :

161. st == ~LJ_VMST_GC ? 'G' : 'J';

162. g->hookmask = (mask | HOOK_PROFILE);

163. lj_dispatch_update(g);

164. }

165. profile_unlock(ps);

166. }

168. /* -- OS-specific profile timer handling ---------------------------------- */

170. #if LJ_PROFILE_SIGPROF

172. /* SIGPROF handler. */

173. ‌static void profile_signal(int sig)

174. {

175. UNUSED(sig);

176. profile_trigger(&profile_state);

177. }

179. /* Start profiling timer. */

180. ‌static void profile_timer_start(ProfileState *ps)

181. {

182. int interval = ps->interval;

183. struct itimerval tm;

184. struct sigaction sa;

185. tm.it_value.tv_sec = tm.it_interval.tv_sec = interval / 1000;

186. tm.it_value.tv_usec = tm.it_interval.tv_usec = (interval % 1000) * 1000;

187. setitimer(ITIMER_PROF, &tm, NULL);

188. sa.sa_flags = SA_RESTART;

189. sa.sa_handler = profile_signal;

190. sigemptyset(&sa.sa_mask);

191. sigaction(SIGPROF, &sa, &ps->oldsa);

192. }

194. /* Stop profiling timer. */

195. ‌static void profile_timer_stop(ProfileState *ps)

196. {

197. struct itimerval tm;

198. tm.it_value.tv_sec = tm.it_interval.tv_sec = 0;

199. tm.it_value.tv_usec = tm.it_interval.tv_usec = 0;

200. setitimer(ITIMER_PROF, &tm, NULL);

201. sigaction(SIGPROF, &ps->oldsa, NULL);

202. }

204. #elif LJ_PROFILE_PTHREAD

206. /* POSIX timer thread. */

207. ‌static void *profile_thread(ProfileState *ps)

208. {

209. int interval = ps->interval;

210. #if !LJ_TARGET_PS3

211. struct timespec ts;

212. ts.tv_sec = interval / 1000;

213. ts.tv_nsec = (interval % 1000) * 1000000;

214. #endif

215. while (1) {

216. #if LJ_TARGET_PS3

217. sys_timer_usleep(interval * 1000);

218. #else

219. nanosleep(&ts, NULL);

220. #endif

221. if (ps->abort) break;

222. profile_trigger(ps);

223. }

224. return NULL;

225. }

227. /* Start profiling timer thread. */

228. ‌static void profile_timer_start(ProfileState *ps)

229. {

230. pthread_mutex_init(&ps->lock, 0);

231. ps->abort = 0;

232. pthread_create(&ps->thread, NULL, (void *(*)(void *))profile_thread, ps);

233. }

235. /* Stop profiling timer thread. */

236. ‌static void profile_timer_stop(ProfileState *ps)

237. {

238. ps->abort = 1;

239. pthread_join(ps->thread, NULL);

240. pthread_mutex_destroy(&ps->lock);

241. }

243. #elif LJ_PROFILE_WTHREAD

245. /* Windows timer thread. */

246. ‌static DWORD WINAPI profile_thread(void *psx)

247. {

248. ProfileState *ps = (ProfileState *)psx;

249. int interval = ps->interval;

250. #if LJ_TARGET_WINDOWS

251. ps->wmm_tbp(interval);

252. #endif

253. while (1) {

254. Sleep(interval);

255. if (ps->abort) break;

256. profile_trigger(ps);

257. }

258. #if LJ_TARGET_WINDOWS

259. ps->wmm_tep(interval);

260. #endif

261. return 0;

262. }

264. /* Start profiling timer thread. */

265. ‌static void profile_timer_start(ProfileState *ps)

266. {

267. #if LJ_TARGET_WINDOWS

268. if (!ps->wmm) {  /* Load WinMM library on-demand. */

269. ps->wmm = LoadLibraryA("winmm.dll");

270. if (ps->wmm) {

271. ps->wmm_tbp = (WMM_TPFUNC)GetProcAddress(ps->wmm, "timeBeginPeriod");

272. ps->wmm_tep = (WMM_TPFUNC)GetProcAddress(ps->wmm, "timeEndPeriod");

273. if (!ps->wmm_tbp || !ps->wmm_tep) {

274. ps->wmm = NULL;

275. return;

276. }

277. }

278. }

279. #endif

280. InitializeCriticalSection(&ps->lock);

281. ps->abort = 0;

282. ps->thread = CreateThread(NULL, 0, profile_thread, ps, 0, NULL);

283. }

285. /* Stop profiling timer thread. */

286. ‌static void profile_timer_stop(ProfileState *ps)

287. {

288. ps->abort = 1;

289. WaitForSingleObject(ps->thread, INFINITE);

290. DeleteCriticalSection(&ps->lock);

291. }

293. #endif

295. /* -- Public profiling API ------------------------------------------------ */

297. /* Start profiling. */

298. ‌LUA_API void luaJIT_profile_start(lua_State *L, const char *mode,

299. luaJIT_profile_callback cb, void *data)

300. {

301. ProfileState *ps = &profile_state;

302. int interval = LJ_PROFILE_INTERVAL_DEFAULT;

303. while (*mode) {

304. int m = *mode++;

305. switch (m) {

306. case 'i':

307. interval = 0;

308. while (*mode >= '0' && *mode <= '9')

309. interval = interval * 10 + (*mode++ - '0');

310. if (interval <= 0) interval = 1;

311. break;

312. #if LJ_HASJIT

313. case 'l': case 'f':

314. L2J(L)->prof_mode = m;

315. lj_trace_flushall(L);

316. break;

317. #endif

318. default:  /* Ignore unknown mode chars. */

319. break;

320. }

321. }

322. if (ps->g) {

323. luaJIT_profile_stop(L);

324. if (ps->g) return;  /* Profiler in use by another VM. */

325. }

326. ps->g = G(L);

327. ps->interval = interval;

328. ps->cb = cb;

329. ps->data = data;

330. ps->samples = 0;

331. lj_buf_init(L, &ps->sb);

332. profile_timer_start(ps);

333. }

335. /* Stop profiling. */

336. ‌LUA_API void luaJIT_profile_stop(lua_State *L)

337. {

338. ProfileState *ps = &profile_state;

339. global_State *g = ps->g;

340. if (G(L) == g) {  /* Only stop profiler if started by this VM. */

341. profile_timer_stop(ps);

342. g->hookmask &= ~HOOK_PROFILE;

343. lj_dispatch_update(g);

344. #if LJ_HASJIT

345. G2J(g)->prof_mode = 0;

346. lj_trace_flushall(L);

347. #endif

348. lj_buf_free(g, &ps->sb);

349. setmref(ps->sb.b, NULL);

350. setmref(ps->sb.e, NULL);

351. ps->g = NULL;

352. }

353. }

355. /* Return a compact stack dump. */

356. ‌LUA_API const char *luaJIT_profile_dumpstack(lua_State *L, const char *fmt,

357. int depth, size_t *len)

358. {

359. ProfileState *ps = &profile_state;

360. SBuf *sb = &ps->sb;

361. setsbufL(sb, L);

362. lj_buf_reset(sb);

363. lj_debug_dumpstack(L, sb, fmt, depth);

364. *len = (size_t)sbuflen(sb);

365. return sbufB(sb);

366. }

368. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_vmevent.h - luajit-2.0-src

Data types defined

- VMEvent

Macros defined

- LJ_VMEVENTS_HSIZE

- LJ_VMEVENTS_REGKEY

- VMEVENT_DEF

- VMEVENT_HASH

- VMEVENT_HASHIDX

- VMEVENT_MASK

- VMEVENT_NOCACHE

- _LJ_VMEVENT_H

- lj_vmevent_send

- lj_vmevent_send

- lj_vmevent_send_

- lj_vmevent_send_

Source code

1. /*

2. ** VM event handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_VMEVENT_H

7. ‌#define _LJ_VMEVENT_H

9. #include "lj_obj.h"

11. /* Registry key for VM event handler table. */

12. ‌#define LJ_VMEVENTS_REGKEY        "_VMEVENTS"

13. ‌#define LJ_VMEVENTS_HSIZE        4

15. ‌#define VMEVENT_MASK(ev)        ((uint8_t)1 << ((int)(ev) & 7))

16. ‌#define VMEVENT_HASH(ev)        ((int)(ev) & ~7)

17. ‌#define VMEVENT_HASHIDX(h)        ((int)(h) << 3)

18. ‌#define VMEVENT_NOCACHE                255

20. ‌#define VMEVENT_DEF(name, hash) \

21. LJ_VMEVENT_##name##_, \

22. LJ_VMEVENT_##name = ((LJ_VMEVENT_##name##_) & 7)|((hash) << 3)

24. /* VM event IDs. */

25. typedef enum {

26. VMEVENT_DEF(BC,        0x00003883),

27. VMEVENT_DEF(TRACE,        0xb2d91467),

28. VMEVENT_DEF(RECORD,        0x9284bf4f),

29. VMEVENT_DEF(TEXIT,        0xb29df2b0),

30. LJ_VMEVENT__MAX

31. ‌} VMEvent;

33. #ifdef LUAJIT_DISABLE_VMEVENT

34. ‌#define lj_vmevent_send(L, ev, args)                UNUSED(L)

35. ‌#define lj_vmevent_send_(L, ev, args, post)        UNUSED(L)

36. #else

37. ‌#define lj_vmevent_send(L, ev, args) \

38. if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \

39. ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \

40. if (argbase) { \

41. args \

42. lj_vmevent_call(L, argbase); \

43. } \

44. }

45. ‌#define lj_vmevent_send_(L, ev, args, post) \

46. if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \

47. ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \

48. if (argbase) { \

49. args \

50. lj_vmevent_call(L, argbase); \

51. post \

52. } \

53. }

55. LJ_FUNC ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev);

56. LJ_FUNC void lj_vmevent_call(lua_State *L, ptrdiff_t argbase);

57. #endif

59. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_vmevent.c - luajit-2.0-src

Functions defined

- lj_vmevent_call

- lj_vmevent_prepare

Macros defined

- LUA_CORE

- lj_vmevent_c

Source code

1. /*

2. ** VM event handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include <stdio.h>

8. ‌#define lj_vmevent_c

9. ‌#define LUA_CORE

11. #include "lj_obj.h"

12. #include "lj_str.h"

13. #include "lj_tab.h"

14. #include "lj_state.h"

15. #include "lj_dispatch.h"

16. #include "lj_vm.h"

17. #include "lj_vmevent.h"

19. ‌ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev)

20. {

21. global_State *g = G(L);

22. GCstr *s = lj_str_newlit(L, LJ_VMEVENTS_REGKEY);

23. cTValue *tv = lj_tab_getstr(tabV(registry(L)), s);

24. if (tvistab(tv)) {

25. int hash = VMEVENT_HASH(ev);

26. tv = lj_tab_getint(tabV(tv), hash);

27. if (tv && tvisfunc(tv)) {

28. lj_state_checkstack(L, LUA_MINSTACK);

29. setfuncV(L, L->top++, funcV(tv));

30. if (LJ_FR2) setnilV(L->top++);

31. return savestack(L, L->top);

32. }

33. }

34. g->vmevmask &= ~VMEVENT_MASK(ev);  /* No handler: cache this fact. */

35. return 0;

36. }

38. ‌void lj_vmevent_call(lua_State *L, ptrdiff_t argbase)

39. {

40. global_State *g = G(L);

41. uint8_t oldmask = g->vmevmask;

42. uint8_t oldh = hook_save(g);

43. int status;

44. g->vmevmask = 0;  /* Disable all events. */

45. hook_vmevent(g);

46. status = lj_vm_pcall(L, restorestack(L, argbase), 0+1, 0);

47. if (LJ_UNLIKELY(status)) {

48. /* Really shouldn't use stderr here, but where else to complain? */

49. L->top--;

50. fputs("VM handler failed: ", stderr);

51. fputs(tvisstr(L->top) ? strVdata(L->top) : "?", stderr);

52. fputc('\n', stderr);

53. }

54. hook_restore(g, oldh);

55. if (g->vmevmask != VMEVENT_NOCACHE)

56. g->vmevmask = oldmask;  /* Restore event mask, but not if not modified. */

57. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_ffi.c - luajit-2.0-src

Functions defined

- LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0)

- LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)

- LJLIB_CF(ffi_meta___eq) LJLIB_REC(cdata_arith MM_eq)

- LJLIB_CF(ffi_meta___len) LJLIB_REC(cdata_arith MM_len)

- LJLIB_CF(ffi_meta___lt) LJLIB_REC(cdata_arith MM_lt)

- LJLIB_CF(ffi_meta___le) LJLIB_REC(cdata_arith MM_le)

- LJLIB_CF(ffi_meta___concat) LJLIB_REC(cdata_arith MM_concat)

- LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)

- LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add)

- LJLIB_CF(ffi_meta___sub) LJLIB_REC(cdata_arith MM_sub)

- LJLIB_CF(ffi_meta___mul) LJLIB_REC(cdata_arith MM_mul)

- LJLIB_CF(ffi_meta___div) LJLIB_REC(cdata_arith MM_div)

- LJLIB_CF(ffi_meta___mod) LJLIB_REC(cdata_arith MM_mod)

- LJLIB_CF(ffi_meta___pow) LJLIB_REC(cdata_arith MM_pow)

- LJLIB_CF(ffi_meta___unm) LJLIB_REC(cdata_arith MM_unm)

- LJLIB_CF(ffi_meta___tostring)

- LJLIB_CF(ffi_meta___pairs)

- LJLIB_CF(ffi_meta___ipairs)

- LJLIB_CF(ffi_clib___index) LJLIB_REC(clib_index 1)

- LJLIB_CF(ffi_clib___newindex) LJLIB_REC(clib_index 0)

- LJLIB_CF(ffi_clib___gc)

- LJLIB_CF(ffi_callback_free)

- LJLIB_CF(ffi_callback_set)

- LJLIB_CF(ffi_new) LJLIB_REC(.)

- LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new)

- LJLIB_CF(ffi_typeof) LJLIB_REC(.)

- LJLIB_CF(ffi_typeinfo)

- LJLIB_CF(ffi_istype) LJLIB_REC(.)

- LJLIB_CF(ffi_sizeof) LJLIB_REC(ffi_xof FF_ffi_sizeof)

- LJLIB_CF(ffi_alignof) LJLIB_REC(ffi_xof FF_ffi_alignof)

- LJLIB_CF(ffi_offsetof) LJLIB_REC(ffi_xof FF_ffi_offsetof)

- LJLIB_CF(ffi_errno) LJLIB_REC(.)

- LJLIB_CF(ffi_string) LJLIB_REC(.)

- LJLIB_CF(ffi_copy) LJLIB_REC(.)

- LJLIB_CF(ffi_fill) LJLIB_REC(.)

- LJLIB_CF(ffi_abi) LJLIB_REC(.)

- LJLIB_CF(ffi_metatype)

- LJLIB_CF(ffi_gc) LJLIB_REC(.)

- LJLIB_CF(ffi_load)

- LJLIB_PUSH(top-1) LJLIB_SET(__index)

- ffi_arith

- ffi_callback_set

- ffi_checkcdata

- ffi_checkctype

- ffi_checkint

- ffi_checkptr

- ffi_clib_index

- ffi_finalizer

- ffi_index_meta

- ffi_pairs

- ffi_register_module

- luaopen_ffi

Macros defined

- H_

- H_

- LJLIB_MODULE_ffi

- LJLIB_MODULE_ffi_callback

- LJLIB_MODULE_ffi_clib

- LJLIB_MODULE_ffi_meta

- LUA_LIB

- lib_ffi_c

Source code

1. /*

2. ** FFI library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lib_ffi_c

7. ‌#define LUA_LIB

9. #include <errno.h>

11. #include "lua.h"

12. #include "lauxlib.h"

13. #include "lualib.h"

15. #include "lj_obj.h"

17. #if LJ_HASFFI

19. #include "lj_gc.h"

20. #include "lj_err.h"

21. #include "lj_str.h"

22. #include "lj_tab.h"

23. #include "lj_meta.h"

24. #include "lj_ctype.h"

25. #include "lj_cparse.h"

26. #include "lj_cdata.h"

27. #include "lj_cconv.h"

28. #include "lj_carith.h"

29. #include "lj_ccall.h"

30. #include "lj_ccallback.h"

31. #include "lj_clib.h"

32. #include "lj_strfmt.h"

33. #include "lj_ff.h"

34. #include "lj_lib.h"

36. /* -- C type checks ------------------------------------------------------- */

38. /* Check first argument for a C type and returns its ID. */

39. ‌static CTypeID ffi_checkctype(lua_State *L, CTState *cts, TValue *param)

40. {

41. TValue *o = L->base;

42. if (!(o < L->top)) {

43. err_argtype:

44. lj_err_argtype(L, 1, "C type");

45. }

46. if (tvisstr(o)) {  /* Parse an abstract C type declaration. */

47. GCstr *s = strV(o);

48. CPState cp;

49. int errcode;

50. cp.L = L;

51. cp.cts = cts;

52. cp.srcname = strdata(s);

53. cp.p = strdata(s);

54. cp.param = param;

55. cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;

56. errcode = lj_cparse(&cp);

57. if (errcode) lj_err_throw(L, errcode);  /* Propagate errors. */

58. return cp.val.id;

59. } else {

60. GCcdata *cd;

61. if (!tviscdata(o)) goto err_argtype;

62. if (param && param < L->top) lj_err_arg(L, 1, LJ_ERR_FFI_NUMPARAM);

63. cd = cdataV(o);

64. return cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->ctypeid;

65. }

66. }

68. /* Check argument for C data and return it. */

69. ‌static GCcdata *ffi_checkcdata(lua_State *L, int narg)

70. {

71. TValue *o = L->base + narg-1;

72. if (!(o < L->top && tviscdata(o)))

73. lj_err_argt(L, narg, LUA_TCDATA);

74. return cdataV(o);

75. }

77. /* Convert argument to C pointer. */

78. ‌static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)

79. {

80. CTState *cts = ctype_cts(L);

81. TValue *o = L->base + narg-1;

82. void *p;

83. if (o >= L->top)

84. lj_err_arg(L, narg, LJ_ERR_NOVAL);

85. lj_cconv_ct_tv(cts, ctype_get(cts, id), (uint8_t *)&p, o, CCF_ARG(narg));

86. return p;

87. }

89. /* Convert argument to int32_t. */

90. ‌static int32_t ffi_checkint(lua_State *L, int narg)

91. {

92. CTState *cts = ctype_cts(L);

93. TValue *o = L->base + narg-1;

94. int32_t i;

95. if (o >= L->top)

96. lj_err_arg(L, narg, LJ_ERR_NOVAL);

97. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o,

98. CCF_ARG(narg));

99. return i;

100. }

102. /* -- C type metamethods -------------------------------------------------- */

104. ‌#define LJLIB_MODULE_ffi_meta

106. /* Handle ctype __index/__newindex metamethods. */

107. ‌static int ffi_index_meta(lua_State *L, CTState *cts, CType *ct, MMS mm)

108. {

109. CTypeID id = ctype_typeid(cts, ct);

110. cTValue *tv = lj_ctype_meta(cts, id, mm);

111. TValue *base = L->base;

112. if (!tv) {

113. const char *s;

114. err_index:

115. s = strdata(lj_ctype_repr(L, id, NULL));

116. if (tvisstr(L->base+1)) {

117. lj_err_callerv(L, LJ_ERR_FFI_BADMEMBER, s, strVdata(L->base+1));

118. } else {

119. const char *key = tviscdata(L->base+1) ?

120. strdata(lj_ctype_repr(L, cdataV(L->base+1)->ctypeid, NULL)) :

121. lj_typename(L->base+1);

122. lj_err_callerv(L, LJ_ERR_FFI_BADIDXW, s, key);

123. }

124. }

125. if (!tvisfunc(tv)) {

126. if (mm == MM_index) {

127. cTValue *o = lj_meta_tget(L, tv, base+1);

128. if (o) {

129. if (tvisnil(o)) goto err_index;

130. copyTV(L, L->top-1, o);

131. return 1;

132. }

133. } else {

134. TValue *o = lj_meta_tset(L, tv, base+1);

135. if (o) {

136. copyTV(L, o, base+2);

137. return 0;

138. }

139. }

140. copyTV(L, base, L->top);

141. tv = L->top-1-LJ_FR2;

142. }

143. return lj_meta_tailcall(L, tv);

144. }

146. ‌LJLIB_CF(ffi_meta___index)        LJLIB_REC(cdata_index 0)

147. {

148. CTState *cts = ctype_cts(L);

149. CTInfo qual = 0;

150. CType *ct;

151. uint8_t *p;

152. TValue *o = L->base;

153. if (!(o+1 < L->top && tviscdata(o)))  /* Also checks for presence of key. */

154. lj_err_argt(L, 1, LUA_TCDATA);

155. ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);

156. if ((qual & 1))

157. return ffi_index_meta(L, cts, ct, MM_index);

158. if (lj_cdata_get(cts, ct, L->top-1, p))

159. lj_gc_check(L);

160. return 1;

161. }

163. ‌LJLIB_CF(ffi_meta___newindex)        LJLIB_REC(cdata_index 1)

164. {

165. CTState *cts = ctype_cts(L);

166. CTInfo qual = 0;

167. CType *ct;

168. uint8_t *p;

169. TValue *o = L->base;

170. if (!(o+2 < L->top && tviscdata(o)))  /* Also checks for key and value. */

171. lj_err_argt(L, 1, LUA_TCDATA);

172. ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);

173. if ((qual & 1)) {

174. if ((qual & CTF_CONST))

175. lj_err_caller(L, LJ_ERR_FFI_WRCONST);

176. return ffi_index_meta(L, cts, ct, MM_newindex);

177. }

178. lj_cdata_set(cts, ct, p, o+2, qual);

179. return 0;

180. }

182. /* Common handler for cdata arithmetic. */

183. ‌static int ffi_arith(lua_State *L)

184. {

185. MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq);

186. return lj_carith_op(L, mm);

187. }

189. /* The following functions must be in contiguous ORDER MM. */

190. ‌LJLIB_CF(ffi_meta___eq)                LJLIB_REC(cdata_arith MM_eq)

191. {

192. return ffi_arith(L);

193. }

195. ‌LJLIB_CF(ffi_meta___len)        LJLIB_REC(cdata_arith MM_len)

196. {

197. return ffi_arith(L);

198. }

200. ‌LJLIB_CF(ffi_meta___lt)                LJLIB_REC(cdata_arith MM_lt)

201. {

202. return ffi_arith(L);

203. }

205. ‌LJLIB_CF(ffi_meta___le)                LJLIB_REC(cdata_arith MM_le)

206. {

207. return ffi_arith(L);

208. }

210. ‌LJLIB_CF(ffi_meta___concat)        LJLIB_REC(cdata_arith MM_concat)

211. {

212. return ffi_arith(L);

213. }

215. /* Forward declaration. */

216. static int lj_cf_ffi_new(lua_State *L);

218. ‌LJLIB_CF(ffi_meta___call)        LJLIB_REC(cdata_call)

219. {

220. CTState *cts = ctype_cts(L);

221. GCcdata *cd = ffi_checkcdata(L, 1);

222. CTypeID id = cd->ctypeid;

223. CType *ct;

224. cTValue *tv;

225. MMS mm = MM_call;

226. if (cd->ctypeid == CTID_CTYPEID) {

227. id = *(CTypeID *)cdataptr(cd);

228. mm = MM_new;

229. } else {

230. int ret = lj_ccall_func(L, cd);

231. if (ret >= 0)

232. return ret;

233. }

234. /* Handle ctype __call/__new metamethod. */

235. ct = ctype_raw(cts, id);

236. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);

237. tv = lj_ctype_meta(cts, id, mm);

238. if (tv)

239. return lj_meta_tailcall(L, tv);

240. else if (mm == MM_call)

241. lj_err_callerv(L, LJ_ERR_FFI_BADCALL, strdata(lj_ctype_repr(L, id, NULL)));

242. return lj_cf_ffi_new(L);

243. }

245. ‌LJLIB_CF(ffi_meta___add)        LJLIB_REC(cdata_arith MM_add)

246. {

247. return ffi_arith(L);

248. }

250. ‌LJLIB_CF(ffi_meta___sub)        LJLIB_REC(cdata_arith MM_sub)

251. {

252. return ffi_arith(L);

253. }

255. ‌LJLIB_CF(ffi_meta___mul)        LJLIB_REC(cdata_arith MM_mul)

256. {

257. return ffi_arith(L);

258. }

260. ‌LJLIB_CF(ffi_meta___div)        LJLIB_REC(cdata_arith MM_div)

261. {

262. return ffi_arith(L);

263. }

265. ‌LJLIB_CF(ffi_meta___mod)        LJLIB_REC(cdata_arith MM_mod)

266. {

267. return ffi_arith(L);

268. }

270. ‌LJLIB_CF(ffi_meta___pow)        LJLIB_REC(cdata_arith MM_pow)

271. {

272. return ffi_arith(L);

273. }

275. ‌LJLIB_CF(ffi_meta___unm)        LJLIB_REC(cdata_arith MM_unm)

276. {

277. return ffi_arith(L);

278. }

279. /* End of contiguous ORDER MM. */

281. ‌LJLIB_CF(ffi_meta___tostring)

282. {

283. GCcdata *cd = ffi_checkcdata(L, 1);

284. const char *msg = "cdata<%s>: %p";

285. CTypeID id = cd->ctypeid;

286. void *p = cdataptr(cd);

287. if (id == CTID_CTYPEID) {

288. msg = "ctype<%s>";

289. id = *(CTypeID *)p;

290. } else {

291. CTState *cts = ctype_cts(L);

292. CType *ct = ctype_raw(cts, id);

293. if (ctype_isref(ct->info)) {

294. p = *(void **)p;

295. ct = ctype_rawchild(cts, ct);

296. }

297. if (ctype_iscomplex(ct->info)) {

298. setstrV(L, L->top-1, lj_ctype_repr_complex(L, cdataptr(cd), ct->size));

299. goto checkgc;

300. } else if (ct->size == 8 && ctype_isinteger(ct->info)) {

301. setstrV(L, L->top-1, lj_ctype_repr_int64(L, *(uint64_t *)cdataptr(cd),

302. (ct->info & CTF_UNSIGNED)));

303. goto checkgc;

304. } else if (ctype_isfunc(ct->info)) {

305. p = *(void **)p;

306. } else if (ctype_isenum(ct->info)) {

307. msg = "cdata<%s>: %d";

308. p = (void *)(uintptr_t)*(uint32_t **)p;

309. } else {

310. if (ctype_isptr(ct->info)) {

311. p = cdata_getptr(p, ct->size);

312. ct = ctype_rawchild(cts, ct);

313. }

314. if (ctype_isstruct(ct->info) || ctype_isvector(ct->info)) {

315. /* Handle ctype __tostring metamethod. */

316. cTValue *tv = lj_ctype_meta(cts, ctype_typeid(cts, ct), MM_tostring);

317. if (tv)

318. return lj_meta_tailcall(L, tv);

319. }

320. }

321. }

322. lj_strfmt_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p);

323. checkgc:

324. lj_gc_check(L);

325. return 1;

326. }

328. ‌static int ffi_pairs(lua_State *L, MMS mm)

329. {

330. CTState *cts = ctype_cts(L);

331. CTypeID id = ffi_checkcdata(L, 1)->ctypeid;

332. CType *ct = ctype_raw(cts, id);

333. cTValue *tv;

334. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);

335. tv = lj_ctype_meta(cts, id, mm);

336. if (!tv)

337. lj_err_callerv(L, LJ_ERR_FFI_BADMM, strdata(lj_ctype_repr(L, id, NULL)),

338. strdata(mmname_str(G(L), mm)));

339. return lj_meta_tailcall(L, tv);

340. }

342. ‌LJLIB_CF(ffi_meta___pairs)

343. {

344. return ffi_pairs(L, MM_pairs);

345. }

347. ‌LJLIB_CF(ffi_meta___ipairs)

348. {

349. return ffi_pairs(L, MM_ipairs);

350. }

352. LJLIB_PUSH("ffi") LJLIB_SET(__metatable)

354. #include "lj_libdef.h"

356. /* -- C library metamethods ----------------------------------------------- */

358. ‌#define LJLIB_MODULE_ffi_clib

360. /* Index C library by a name. */

361. ‌static TValue *ffi_clib_index(lua_State *L)

362. {

363. TValue *o = L->base;

364. CLibrary *cl;

365. if (!(o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB))

366. lj_err_argt(L, 1, LUA_TUSERDATA);

367. cl = (CLibrary *)uddata(udataV(o));

368. if (!(o+1 < L->top && tvisstr(o+1)))

369. lj_err_argt(L, 2, LUA_TSTRING);

370. return lj_clib_index(L, cl, strV(o+1));

371. }

373. ‌LJLIB_CF(ffi_clib___index)        LJLIB_REC(clib_index 1)

374. {

375. TValue *tv = ffi_clib_index(L);

376. if (tviscdata(tv)) {

377. CTState *cts = ctype_cts(L);

378. GCcdata *cd = cdataV(tv);

379. CType *s = ctype_get(cts, cd->ctypeid);

380. if (ctype_isextern(s->info)) {

381. CTypeID sid = ctype_cid(s->info);

382. void *sp = *(void **)cdataptr(cd);

383. CType *ct = ctype_raw(cts, sid);

384. if (lj_cconv_tv_ct(cts, ct, sid, L->top-1, sp))

385. lj_gc_check(L);

386. return 1;

387. }

388. }

389. copyTV(L, L->top-1, tv);

390. return 1;

391. }

393. ‌LJLIB_CF(ffi_clib___newindex)        LJLIB_REC(clib_index 0)

394. {

395. TValue *tv = ffi_clib_index(L);

396. TValue *o = L->base+2;

397. if (o < L->top && tviscdata(tv)) {

398. CTState *cts = ctype_cts(L);

399. GCcdata *cd = cdataV(tv);

400. CType *d = ctype_get(cts, cd->ctypeid);

401. if (ctype_isextern(d->info)) {

402. CTInfo qual = 0;

403. for (;;) {  /* Skip attributes and collect qualifiers. */

404. d = ctype_child(cts, d);

405. if (!ctype_isattrib(d->info)) break;

406. if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size;

407. }

408. if (!((d->info|qual) & CTF_CONST)) {

409. lj_cconv_ct_tv(cts, d, *(void **)cdataptr(cd), o, 0);

410. return 0;

411. }

412. }

413. }

414. lj_err_caller(L, LJ_ERR_FFI_WRCONST);

415. return 0;  /* unreachable */

416. }

418. ‌LJLIB_CF(ffi_clib___gc)

419. {

420. TValue *o = L->base;

421. if (o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB)

422. lj_clib_unload((CLibrary *)uddata(udataV(o)));

423. return 0;

424. }

426. #include "lj_libdef.h"

428. /* -- Callback function metamethods --------------------------------------- */

430. ‌#define LJLIB_MODULE_ffi_callback

432. ‌static int ffi_callback_set(lua_State *L, GCfunc *fn)

433. {

434. GCcdata *cd = ffi_checkcdata(L, 1);

435. CTState *cts = ctype_cts(L);

436. CType *ct = ctype_raw(cts, cd->ctypeid);

437. if (ctype_isptr(ct->info) && (LJ_32 || ct->size == 8)) {

438. MSize slot = lj_ccallback_ptr2slot(cts, *(void **)cdataptr(cd));

439. if (slot < cts->cb.sizeid && cts->cb.cbid[slot] != 0) {

440. GCtab *t = cts->miscmap;

441. TValue *tv = lj_tab_setint(L, t, (int32_t)slot);

442. if (fn) {

443. setfuncV(L, tv, fn);

444. lj_gc_anybarriert(L, t);

445. } else {

446. setnilV(tv);

447. cts->cb.cbid[slot] = 0;

448. cts->cb.topid = slot < cts->cb.topid ? slot : cts->cb.topid;

449. }

450. return 0;

451. }

452. }

453. lj_err_caller(L, LJ_ERR_FFI_BADCBACK);

454. return 0;

455. }

457. ‌LJLIB_CF(ffi_callback_free)

458. {

459. return ffi_callback_set(L, NULL);

460. }

462. ‌LJLIB_CF(ffi_callback_set)

463. {

464. GCfunc *fn = lj_lib_checkfunc(L, 2);

465. return ffi_callback_set(L, fn);

466. }

468. ‌LJLIB_PUSH(top-1) LJLIB_SET(__index)

470. #include "lj_libdef.h"

472. /* -- FFI library functions ----------------------------------------------- */

474. ‌#define LJLIB_MODULE_ffi

476. LJLIB_CF(ffi_cdef)

477. {

478. GCstr *s = lj_lib_checkstr(L, 1);

479. CPState cp;

480. int errcode;

481. cp.L = L;

482. cp.cts = ctype_cts(L);

483. cp.srcname = strdata(s);

484. cp.p = strdata(s);

485. cp.param = L->base+1;

486. cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT;

487. errcode = lj_cparse(&cp);

488. if (errcode) lj_err_throw(L, errcode);  /* Propagate errors. */

489. lj_gc_check(L);

490. return 0;

491. }

493. ‌LJLIB_CF(ffi_new)        LJLIB_REC(.)

494. {

495. CTState *cts = ctype_cts(L);

496. CTypeID id = ffi_checkctype(L, cts, NULL);

497. CType *ct = ctype_raw(cts, id);

498. CTSize sz;

499. CTInfo info = lj_ctype_info(cts, id, &sz);

500. TValue *o = L->base+1;

501. GCcdata *cd;

502. if ((info & CTF_VLA)) {

503. o++;

504. sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));

505. }

506. if (sz == CTSIZE_INVALID)

507. lj_err_arg(L, 1, LJ_ERR_FFI_INVSIZE);

508. if (!(info & CTF_VLA) && ctype_align(info) <= CT_MEMALIGN)

509. cd = lj_cdata_new(cts, id, sz);

510. else

511. cd = lj_cdata_newv(L, id, sz, ctype_align(info));

512. setcdataV(L, o-1, cd);  /* Anchor the uninitialized cdata. */

513. lj_cconv_ct_init(cts, ct, sz, cdataptr(cd),

514. o, (MSize)(L->top - o));  /* Initialize cdata. */

515. if (ctype_isstruct(ct->info)) {

516. /* Handle ctype __gc metamethod. Use the fast lookup here. */

517. cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id);

518. if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) {

519. GCtab *t = cts->finalizer;

520. if (gcref(t->metatable)) {

521. /* Add to finalizer table, if still enabled. */

522. copyTV(L, lj_tab_set(L, t, o-1), tv);

523. lj_gc_anybarriert(L, t);

524. cd->marked |= LJ_GC_CDATA_FIN;

525. }

526. }

527. }

528. L->top = o;  /* Only return the cdata itself. */

529. lj_gc_check(L);

530. return 1;

531. }

533. ‌LJLIB_CF(ffi_cast)        LJLIB_REC(ffi_new)

534. {

535. CTState *cts = ctype_cts(L);

536. CTypeID id = ffi_checkctype(L, cts, NULL);

537. CType *d = ctype_raw(cts, id);

538. TValue *o = lj_lib_checkany(L, 2);

539. L->top = o+1;  /* Make sure this is the last item on the stack. */

540. if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info)))

541. lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);

542. if (!(tviscdata(o) && cdataV(o)->ctypeid == id)) {

543. GCcdata *cd = lj_cdata_new(cts, id, d->size);

544. lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST);

545. setcdataV(L, o, cd);

546. lj_gc_check(L);

547. }

548. return 1;

549. }

551. ‌LJLIB_CF(ffi_typeof)        LJLIB_REC(.)

552. {

553. CTState *cts = ctype_cts(L);

554. CTypeID id = ffi_checkctype(L, cts, L->base+1);

555. GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4);

556. *(CTypeID *)cdataptr(cd) = id;

557. setcdataV(L, L->top-1, cd);

558. lj_gc_check(L);

559. return 1;

560. }

562. /* Internal and unsupported API. */

563. ‌LJLIB_CF(ffi_typeinfo)

564. {

565. CTState *cts = ctype_cts(L);

566. CTypeID id = (CTypeID)ffi_checkint(L, 1);

567. if (id > 0 && id < cts->top) {

568. CType *ct = ctype_get(cts, id);

569. GCtab *t;

570. lua_createtable(L, 0, 4);  /* Increment hash size if fields are added. */

571. t = tabV(L->top-1);

572. setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "info")), (int32_t)ct->info);

573. if (ct->size != CTSIZE_INVALID)

574. setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "size")), (int32_t)ct->size);

575. if (ct->sib)

576. setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "sib")), (int32_t)ct->sib);

577. if (gcref(ct->name)) {

578. GCstr *s = gco2str(gcref(ct->name));

579. setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "name")), s);

580. }

581. lj_gc_check(L);

582. return 1;

583. }

584. return 0;

585. }

587. ‌LJLIB_CF(ffi_istype)        LJLIB_REC(.)

588. {

589. CTState *cts = ctype_cts(L);

590. CTypeID id1 = ffi_checkctype(L, cts, NULL);

591. TValue *o = lj_lib_checkany(L, 2);

592. int b = 0;

593. if (tviscdata(o)) {

594. GCcdata *cd = cdataV(o);

595. CTypeID id2 = cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) :

596. cd->ctypeid;

597. CType *ct1 = lj_ctype_rawref(cts, id1);

598. CType *ct2 = lj_ctype_rawref(cts, id2);

599. if (ct1 == ct2) {

600. b = 1;

601. } else if (ctype_type(ct1->info) == ctype_type(ct2->info) &&

602. ct1->size == ct2->size) {

603. if (ctype_ispointer(ct1->info))

604. b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL);

605. else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info))

606. b = (((ct1->info ^ ct2->info) & ~(CTF_QUAL|CTF_LONG)) == 0);

607. } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) &&

608. ct1 == ctype_rawchild(cts, ct2)) {

609. b = 1;

610. }

611. }

612. setboolV(L->top-1, b);

613. setboolV(&G(L)->tmptv2, b);  /* Remember for trace recorder. */

614. return 1;

615. }

617. ‌LJLIB_CF(ffi_sizeof)        LJLIB_REC(ffi_xof FF_ffi_sizeof)

618. {

619. CTState *cts = ctype_cts(L);

620. CTypeID id = ffi_checkctype(L, cts, NULL);

621. CTSize sz;

622. if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) {

623. sz = cdatavlen(cdataV(L->base));

624. } else {

625. CType *ct = lj_ctype_rawref(cts, id);

626. if (ctype_isvltype(ct->info))

627. sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));

628. else

629. sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID;

630. if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) {

631. setnilV(L->top-1);

632. return 1;

633. }

634. }

635. setintV(L->top-1, (int32_t)sz);

636. return 1;

637. }

639. ‌LJLIB_CF(ffi_alignof)        LJLIB_REC(ffi_xof FF_ffi_alignof)

640. {

641. CTState *cts = ctype_cts(L);

642. CTypeID id = ffi_checkctype(L, cts, NULL);

643. CTSize sz = 0;

644. CTInfo info = lj_ctype_info(cts, id, &sz);

645. setintV(L->top-1, 1 << ctype_align(info));

646. return 1;

647. }

649. ‌LJLIB_CF(ffi_offsetof)        LJLIB_REC(ffi_xof FF_ffi_offsetof)

650. {

651. CTState *cts = ctype_cts(L);

652. CTypeID id = ffi_checkctype(L, cts, NULL);

653. GCstr *name = lj_lib_checkstr(L, 2);

654. CType *ct = lj_ctype_rawref(cts, id);

655. CTSize ofs;

656. if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) {

657. CType *fct = lj_ctype_getfield(cts, ct, name, &ofs);

658. if (fct) {

659. setintV(L->top-1, ofs);

660. if (ctype_isfield(fct->info)) {

661. return 1;

662. } else if (ctype_isbitfield(fct->info)) {

663. setintV(L->top++, ctype_bitpos(fct->info));

664. setintV(L->top++, ctype_bitbsz(fct->info));

665. return 3;

666. }

667. }

668. }

669. return 0;

670. }

672. ‌LJLIB_CF(ffi_errno)        LJLIB_REC(.)

673. {

674. int err = errno;

675. if (L->top > L->base)

676. errno = ffi_checkint(L, 1);

677. setintV(L->top++, err);

678. return 1;

679. }

681. ‌LJLIB_CF(ffi_string)        LJLIB_REC(.)

682. {

683. CTState *cts = ctype_cts(L);

684. TValue *o = lj_lib_checkany(L, 1);

685. const char *p;

686. size_t len;

687. if (o+1 < L->top && !tvisnil(o+1)) {

688. len = (size_t)ffi_checkint(L, 2);

689. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o,

690. CCF_ARG(1));

691. } else {

692. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o,

693. CCF_ARG(1));

694. len = strlen(p);

695. }

696. L->top = o+1;  /* Make sure this is the last item on the stack. */

697. setstrV(L, o, lj_str_new(L, p, len));

698. lj_gc_check(L);

699. return 1;

700. }

702. ‌LJLIB_CF(ffi_copy)        LJLIB_REC(.)

703. {

704. void *dp = ffi_checkptr(L, 1, CTID_P_VOID);

705. void *sp = ffi_checkptr(L, 2, CTID_P_CVOID);

706. TValue *o = L->base+1;

707. CTSize len;

708. if (tvisstr(o) && o+1 >= L->top)

709. len = strV(o)->len+1;  /* Copy Lua string including trailing '\0'. */

710. else

711. len = (CTSize)ffi_checkint(L, 3);

712. memcpy(dp, sp, len);

713. return 0;

714. }

716. ‌LJLIB_CF(ffi_fill)        LJLIB_REC(.)

717. {

718. void *dp = ffi_checkptr(L, 1, CTID_P_VOID);

719. CTSize len = (CTSize)ffi_checkint(L, 2);

720. int32_t fill = 0;

721. if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3);

722. memset(dp, fill, len);

723. return 0;

724. }

726. ‌#define H_(le, be)        LJ_ENDIAN_SELECT(0x##le, 0x##be)

728. /* Test ABI string. */

729. ‌LJLIB_CF(ffi_abi)        LJLIB_REC(.)

730. {

731. GCstr *s = lj_lib_checkstr(L, 1);

732. int b = 0;

733. switch (s->hash) {

734. #if LJ_64

735. case H_(849858eb,ad35fd06): b = 1; break;  /* 64bit */

736. #else

737. case H_(662d3c79,d0e22477): b = 1; break;  /* 32bit */

738. #endif

739. #if LJ_ARCH_HASFPU

740. case H_(e33ee463,e33ee463): b = 1; break;  /* fpu */

741. #endif

742. #if LJ_ABI_SOFTFP

743. case H_(61211a23,c2e8c81c): b = 1; break;  /* softfp */

744. #else

745. case H_(539417a8,8ce0812f): b = 1; break;  /* hardfp */

746. #endif

747. #if LJ_ABI_EABI

748. case H_(2182df8f,f2ed1152): b = 1; break;  /* eabi */

749. #endif

750. #if LJ_ABI_WIN

751. case H_(4ab624a8,4ab624a8): b = 1; break;  /* win */

752. #endif

753. case H_(3af93066,1f001464): b = 1; break;  /* le/be */

754. #if LJ_GC64

755. case H_(9e89d2c9,13c83c92): b = 1; break;  /* gc64 */

756. #endif

757. default:

758. break;

759. }

760. setboolV(L->top-1, b);

761. setboolV(&G(L)->tmptv2, b);  /* Remember for trace recorder. */

762. return 1;

763. }

765. ‌#undef H_

767. LJLIB_PUSH(top-8) LJLIB_SET(!)  /* Store reference to miscmap table. */

769. ‌LJLIB_CF(ffi_metatype)

770. {

771. CTState *cts = ctype_cts(L);

772. CTypeID id = ffi_checkctype(L, cts, NULL);

773. GCtab *mt = lj_lib_checktab(L, 2);

774. GCtab *t = cts->miscmap;

775. CType *ct = ctype_get(cts, id);  /* Only allow raw types. */

776. TValue *tv;

777. GCcdata *cd;

778. if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) ||

779. ctype_isvector(ct->info)))

780. lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);

781. tv = lj_tab_setinth(L, t, -(int32_t)id);

782. if (!tvisnil(tv))

783. lj_err_caller(L, LJ_ERR_PROTMT);

784. settabV(L, tv, mt);

785. lj_gc_anybarriert(L, t);

786. cd = lj_cdata_new(cts, CTID_CTYPEID, 4);

787. *(CTypeID *)cdataptr(cd) = id;

788. setcdataV(L, L->top-1, cd);

789. lj_gc_check(L);

790. return 1;

791. }

793. LJLIB_PUSH(top-7) LJLIB_SET(!)  /* Store reference to finalizer table. */

795. ‌LJLIB_CF(ffi_gc)        LJLIB_REC(.)

796. {

797. GCcdata *cd = ffi_checkcdata(L, 1);

798. TValue *fin = lj_lib_checkany(L, 2);

799. CTState *cts = ctype_cts(L);

800. CType *ct = ctype_raw(cts, cd->ctypeid);

801. if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) ||

802. ctype_isrefarray(ct->info)))

803. lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);

804. lj_cdata_setfin(L, cd, gcval(fin), itype(fin));

805. L->top = L->base+1;  /* Pass through the cdata object. */

806. return 1;

807. }

809. LJLIB_PUSH(top-5) LJLIB_SET(!)  /* Store clib metatable in func environment. */

811. ‌LJLIB_CF(ffi_load)

812. {

813. GCstr *name = lj_lib_checkstr(L, 1);

814. int global = (L->base+1 < L->top && tvistruecond(L->base+1));

815. lj_clib_load(L, tabref(curr_func(L)->c.env), name, global);

816. return 1;

817. }

819. LJLIB_PUSH(top-4) LJLIB_SET(C)

820. LJLIB_PUSH(top-3) LJLIB_SET(os)

821. LJLIB_PUSH(top-2) LJLIB_SET(arch)

823. #include "lj_libdef.h"

825. /* ------------------------------------------------------------------------ */

827. /* Create special weak-keyed finalizer table. */

828. ‌static GCtab *ffi_finalizer(lua_State *L)

829. {

830. /* NOBARRIER: The table is new (marked white). */

831. GCtab *t = lj_tab_new(L, 0, 1);

832. settabV(L, L->top++, t);

833. setgcref(t->metatable, obj2gco(t));

834. setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),

835. lj_str_newlit(L, "K"));

836. t->nomm = (uint8_t)(~(1u<<MM_mode));

837. return t;

838. }

840. /* Register FFI module as loaded. */

841. ‌static void ffi_register_module(lua_State *L)

842. {

843. cTValue *tmp = lj_tab_getstr(tabV(registry(L)), lj_str_newlit(L, "_LOADED"));

844. if (tmp && tvistab(tmp)) {

845. GCtab *t = tabV(tmp);

846. copyTV(L, lj_tab_setstr(L, t, lj_str_newlit(L, LUA_FFILIBNAME)), L->top-1);

847. lj_gc_anybarriert(L, t);

848. }

849. }

851. ‌LUALIB_API int luaopen_ffi(lua_State *L)

852. {

853. CTState *cts = lj_ctype_init(L);

854. settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1)));

855. cts->finalizer = ffi_finalizer(L);

856. LJ_LIB_REG(L, NULL, ffi_meta);

857. /* NOBARRIER: basemt is a GC root. */

858. setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1)));

859. LJ_LIB_REG(L, NULL, ffi_clib);

860. LJ_LIB_REG(L, NULL, ffi_callback);

861. /* NOBARRIER: the key is new and lj_tab_newkey() handles the barrier. */

862. settabV(L, lj_tab_setstr(L, cts->miscmap, &cts->g->strempty), tabV(L->top-1));

863. L->top--;

864. lj_clib_default(L, tabV(L->top-1));  /* Create ffi.C default namespace. */

865. lua_pushliteral(L, LJ_OS_NAME);

866. lua_pushliteral(L, LJ_ARCH_NAME);

867. LJ_LIB_REG(L, NULL, ffi);  /* Note: no global "ffi" created! */

868. ffi_register_module(L);

869. return 1;

870. }

872. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ccall.c - luajit-2.0-src

Functions defined

- ccall_classify_ct

- ccall_classify_struct

- ccall_classify_struct

- ccall_classify_struct

- ccall_classify_struct

- ccall_get_results

- ccall_set_args

- ccall_struct_arg

- ccall_struct_reg

- ccall_struct_ret

- lj_ccall_ctid_vararg

- lj_ccall_func

Macros defined

- CCALL_HANDLE_COMPLEXARG

- CCALL_HANDLE_COMPLEXARG

- CCALL_HANDLE_COMPLEXARG

- CCALL_HANDLE_COMPLEXARG

- CCALL_HANDLE_COMPLEXARG

- CCALL_HANDLE_COMPLEXARG

- CCALL_HANDLE_COMPLEXARG

- CCALL_HANDLE_COMPLEXARG

- CCALL_HANDLE_COMPLEXRET

- CCALL_HANDLE_COMPLEXRET

- CCALL_HANDLE_COMPLEXRET

- CCALL_HANDLE_COMPLEXRET

- CCALL_HANDLE_COMPLEXRET

- CCALL_HANDLE_COMPLEXRET

- CCALL_HANDLE_COMPLEXRET

- CCALL_HANDLE_COMPLEXRET

- CCALL_HANDLE_COMPLEXRET

- CCALL_HANDLE_COMPLEXRET2

- CCALL_HANDLE_COMPLEXRET2

- CCALL_HANDLE_COMPLEXRET2

- CCALL_HANDLE_COMPLEXRET2

- CCALL_HANDLE_COMPLEXRET2

- CCALL_HANDLE_COMPLEXRET2

- CCALL_HANDLE_COMPLEXRET2

- CCALL_HANDLE_COMPLEXRET2

- CCALL_HANDLE_REGARG

- CCALL_HANDLE_REGARG

- CCALL_HANDLE_REGARG

- CCALL_HANDLE_REGARG

- CCALL_HANDLE_REGARG

- CCALL_HANDLE_REGARG

- CCALL_HANDLE_REGARG

- CCALL_HANDLE_REGARG_FP1

- CCALL_HANDLE_REGARG_FP1

- CCALL_HANDLE_REGARG_FP2

- CCALL_HANDLE_REGARG_FP2

- CCALL_HANDLE_RET

- CCALL_HANDLE_RET

- CCALL_HANDLE_RET

- CCALL_HANDLE_STRUCTARG

- CCALL_HANDLE_STRUCTARG

- CCALL_HANDLE_STRUCTARG

- CCALL_HANDLE_STRUCTARG

- CCALL_HANDLE_STRUCTARG

- CCALL_HANDLE_STRUCTARG

- CCALL_HANDLE_STRUCTARG

- CCALL_HANDLE_STRUCTARG

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET

- CCALL_HANDLE_STRUCTRET2

- CCALL_HANDLE_STRUCTRET2

- CCALL_HANDLE_STRUCTRET2

- CCALL_HANDLE_STRUCTRET2

- CCALL_HANDLE_STRUCTRET2

- CCALL_RCL_INT

- CCALL_RCL_MEM

- CCALL_RCL_SSE

Source code

1. /*

2. ** FFI C call handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "lj_obj.h"

8. #if LJ_HASFFI

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_tab.h"

13. #include "lj_ctype.h"

14. #include "lj_cconv.h"

15. #include "lj_cdata.h"

16. #include "lj_ccall.h"

17. #include "lj_trace.h"

19. /* Target-specific handling of register arguments. */

20. #if LJ_TARGET_X86

21. /* -- x86 calling conventions --------------------------------------------- */

23. #if LJ_ABI_WIN

25. ‌#define CCALL_HANDLE_STRUCTRET \

26. /* Return structs bigger than 8 by reference (on stack only). */ \

27. cc->retref = (sz > 8); \

28. if (cc->retref) cc->stack[nsp++] = (GPRArg)dp;

30. ‌#define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET

32. #else

34. #if LJ_TARGET_OSX

36. ‌#define CCALL_HANDLE_STRUCTRET \

37. /* Return structs of size 1, 2, 4 or 8 in registers. */ \

38. cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \

39. if (cc->retref) { \

40. if (ngpr < maxgpr) \

41. cc->gpr[ngpr++] = (GPRArg)dp; \

42. else \

43. cc->stack[nsp++] = (GPRArg)dp; \

44. } else {  /* Struct with single FP field ends up in FPR. */ \

45. cc->resx87 = ccall_classify_struct(cts, ctr); \

46. }

48. ‌#define CCALL_HANDLE_STRUCTRET2 \

49. if (cc->resx87) sp = (uint8_t *)&cc->fpr[0]; \

50. memcpy(dp, sp, ctr->size);

52. #else

54. ‌#define CCALL_HANDLE_STRUCTRET \

55. cc->retref = 1;  /* Return all structs by reference (in reg or on stack). */ \

56. if (ngpr < maxgpr) \

57. cc->gpr[ngpr++] = (GPRArg)dp; \

58. else \

59. cc->stack[nsp++] = (GPRArg)dp;

61. #endif

63. ‌#define CCALL_HANDLE_COMPLEXRET \

64. /* Return complex float in GPRs and complex double by reference. */ \

65. cc->retref = (sz > 8); \

66. if (cc->retref) { \

67. if (ngpr < maxgpr) \

68. cc->gpr[ngpr++] = (GPRArg)dp; \

69. else \

70. cc->stack[nsp++] = (GPRArg)dp; \

71. }

73. #endif

75. ‌#define CCALL_HANDLE_COMPLEXRET2 \

76. if (!cc->retref) \

77. *(int64_t *)dp = *(int64_t *)sp;  /* Copy complex float from GPRs. */

79. ‌#define CCALL_HANDLE_STRUCTARG \

80. ngpr = maxgpr;  /* Pass all structs by value on the stack. */

82. ‌#define CCALL_HANDLE_COMPLEXARG \

83. isfp = 1;  /* Pass complex by value on stack. */

85. ‌#define CCALL_HANDLE_REGARG \

86. if (!isfp) {  /* Only non-FP values may be passed in registers. */ \

87. if (n > 1) {  /* Anything > 32 bit is passed on the stack. */ \

88. if (!LJ_ABI_WIN) ngpr = maxgpr;  /* Prevent reordering. */ \

89. } else if (ngpr + 1 <= maxgpr) { \

90. dp = &cc->gpr[ngpr]; \

91. ngpr += n; \

92. goto done; \

93. } \

94. }

96. #elif LJ_TARGET_X64 && LJ_ABI_WIN

97. /* -- Windows/x64 calling conventions ------------------------------------- */

99. ‌#define CCALL_HANDLE_STRUCTRET \

100. /* Return structs of size 1, 2, 4 or 8 in a GPR. */ \

101. cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \

102. if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;

104. ‌#define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET

106. ‌#define CCALL_HANDLE_COMPLEXRET2 \

107. if (!cc->retref) \

108. *(int64_t *)dp = *(int64_t *)sp;  /* Copy complex float from GPRs. */

110. ‌#define CCALL_HANDLE_STRUCTARG \

111. /* Pass structs of size 1, 2, 4 or 8 in a GPR by value. */ \

112. if (!(sz == 1 || sz == 2 || sz == 4 || sz == 8)) { \

113. rp = cdataptr(lj_cdata_new(cts, did, sz)); \

114. sz = CTSIZE_PTR;  /* Pass all other structs by reference. */ \

115. }

117. ‌#define CCALL_HANDLE_COMPLEXARG \

118. /* Pass complex float in a GPR and complex double by reference. */ \

119. if (sz != 2*sizeof(float)) { \

120. rp = cdataptr(lj_cdata_new(cts, did, sz)); \

121. sz = CTSIZE_PTR; \

122. }

124. /* Windows/x64 argument registers are strictly positional (use ngpr). */

125. ‌#define CCALL_HANDLE_REGARG \

126. if (isfp) { \

127. if (ngpr < maxgpr) { dp = &cc->fpr[ngpr++]; nfpr = ngpr; goto done; } \

128. } else { \

129. if (ngpr < maxgpr) { dp = &cc->gpr[ngpr++]; goto done; } \

130. }

132. #elif LJ_TARGET_X64

133. /* -- POSIX/x64 calling conventions --------------------------------------- */

135. ‌#define CCALL_HANDLE_STRUCTRET \

136. int rcl[2]; rcl[0] = rcl[1] = 0; \

137. if (ccall_classify_struct(cts, ctr, rcl, 0)) { \

138. cc->retref = 1;  /* Return struct by reference. */ \

139. cc->gpr[ngpr++] = (GPRArg)dp; \

140. } else { \

141. cc->retref = 0;  /* Return small structs in registers. */ \

142. }

144. ‌#define CCALL_HANDLE_STRUCTRET2 \

145. int rcl[2]; rcl[0] = rcl[1] = 0; \

146. ccall_classify_struct(cts, ctr, rcl, 0); \

147. ccall_struct_ret(cc, rcl, dp, ctr->size);

149. ‌#define CCALL_HANDLE_COMPLEXRET \

150. /* Complex values are returned in one or two FPRs. */ \

151. cc->retref = 0;

153. ‌#define CCALL_HANDLE_COMPLEXRET2 \

154. if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from FPR. */ \

155. *(int64_t *)dp = cc->fpr[0].l[0]; \

156. } else {  /* Copy non-contiguous complex double from FPRs. */ \

157. ((int64_t *)dp)[0] = cc->fpr[0].l[0]; \

158. ((int64_t *)dp)[1] = cc->fpr[1].l[0]; \

159. }

161. ‌#define CCALL_HANDLE_STRUCTARG \

162. int rcl[2]; rcl[0] = rcl[1] = 0; \

163. if (!ccall_classify_struct(cts, d, rcl, 0)) { \

164. cc->nsp = nsp; cc->ngpr = ngpr; cc->nfpr = nfpr; \

165. if (ccall_struct_arg(cc, cts, d, rcl, o, narg)) goto err_nyi; \

166. nsp = cc->nsp; ngpr = cc->ngpr; nfpr = cc->nfpr; \

167. continue; \

168. }  /* Pass all other structs by value on stack. */

170. ‌#define CCALL_HANDLE_COMPLEXARG \

171. isfp = 2;  /* Pass complex in FPRs or on stack. Needs postprocessing. */

173. ‌#define CCALL_HANDLE_REGARG \

174. if (isfp) {  /* Try to pass argument in FPRs. */ \

175. int n2 = ctype_isvector(d->info) ? 1 : n; \

176. if (nfpr + n2 <= CCALL_NARG_FPR) { \

177. dp = &cc->fpr[nfpr]; \

178. nfpr += n2; \

179. goto done; \

180. } \

181. } else {  /* Try to pass argument in GPRs. */ \

182. /* Note that reordering is explicitly allowed in the x64 ABI. */ \

183. if (n <= 2 && ngpr + n <= maxgpr) { \

184. dp = &cc->gpr[ngpr]; \

185. ngpr += n; \

186. goto done; \

187. } \

188. }

190. #elif LJ_TARGET_ARM

191. /* -- ARM calling conventions --------------------------------------------- */

193. #if LJ_ABI_SOFTFP

195. ‌#define CCALL_HANDLE_STRUCTRET \

196. /* Return structs of size <= 4 in a GPR. */ \

197. cc->retref = !(sz <= 4); \

198. if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;

200. ‌#define CCALL_HANDLE_COMPLEXRET \

201. cc->retref = 1;  /* Return all complex values by reference. */ \

202. cc->gpr[ngpr++] = (GPRArg)dp;

204. ‌#define CCALL_HANDLE_COMPLEXRET2 \

205. UNUSED(dp); /* Nothing to do. */

207. ‌#define CCALL_HANDLE_STRUCTARG \

208. /* Pass all structs by value in registers and/or on the stack. */

210. ‌#define CCALL_HANDLE_COMPLEXARG \

211. /* Pass complex by value in 2 or 4 GPRs. */

213. ‌#define CCALL_HANDLE_REGARG_FP1

214. ‌#define CCALL_HANDLE_REGARG_FP2

216. #else

218. ‌#define CCALL_HANDLE_STRUCTRET \

219. cc->retref = !ccall_classify_struct(cts, ctr, ct); \

220. if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;

222. ‌#define CCALL_HANDLE_STRUCTRET2 \

223. if (ccall_classify_struct(cts, ctr, ct) > 1) sp = (uint8_t *)&cc->fpr[0]; \

224. memcpy(dp, sp, ctr->size);

226. ‌#define CCALL_HANDLE_COMPLEXRET \

227. if (!(ct->info & CTF_VARARG)) cc->retref = 0;  /* Return complex in FPRs. */

229. ‌#define CCALL_HANDLE_COMPLEXRET2 \

230. if (!(ct->info & CTF_VARARG)) memcpy(dp, &cc->fpr[0], ctr->size);

232. ‌#define CCALL_HANDLE_STRUCTARG \

233. isfp = (ccall_classify_struct(cts, d, ct) > 1);

234. /* Pass all structs by value in registers and/or on the stack. */

236. ‌#define CCALL_HANDLE_COMPLEXARG \

237. isfp = 1;  /* Pass complex by value in FPRs or on stack. */

239. ‌#define CCALL_HANDLE_REGARG_FP1 \

240. if (isfp && !(ct->info & CTF_VARARG)) { \

241. if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \

242. if (nfpr + (n >> 1) <= CCALL_NARG_FPR) { \

243. dp = &cc->fpr[nfpr]; \

244. nfpr += (n >> 1); \

245. goto done; \

246. } \

247. } else { \

248. if (sz > 1 && fprodd != nfpr) fprodd = 0; \

249. if (fprodd) { \

250. if (2*nfpr+n <= 2*CCALL_NARG_FPR+1) { \

251. dp = (void *)&cc->fpr[fprodd-1].f[1]; \

252. nfpr += (n >> 1); \

253. if ((n & 1)) fprodd = 0; else fprodd = nfpr-1; \

254. goto done; \

255. } \

256. } else { \

257. if (2*nfpr+n <= 2*CCALL_NARG_FPR) { \

258. dp = (void *)&cc->fpr[nfpr]; \

259. nfpr += (n >> 1); \

260. if ((n & 1)) fprodd = ++nfpr; else fprodd = 0; \

261. goto done; \

262. } \

263. } \

264. } \

265. fprodd = 0;  /* No reordering after the first FP value is on stack. */ \

266. } else {

268. ‌#define CCALL_HANDLE_REGARG_FP2        }

270. #endif

272. ‌#define CCALL_HANDLE_REGARG \

273. CCALL_HANDLE_REGARG_FP1 \

274. if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \

275. if (ngpr < maxgpr) \

276. ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \

277. } \

278. if (ngpr < maxgpr) { \

279. dp = &cc->gpr[ngpr]; \

280. if (ngpr + n > maxgpr) { \

281. nsp += ngpr + n - maxgpr;  /* Assumes contiguous gpr/stack fields. */ \

282. if (nsp > CCALL_MAXSTACK) goto err_nyi;  /* Too many arguments. */ \

283. ngpr = maxgpr; \

284. } else { \

285. ngpr += n; \

286. } \

287. goto done; \

288. } CCALL_HANDLE_REGARG_FP2

290. ‌#define CCALL_HANDLE_RET \

291. if ((ct->info & CTF_VARARG)) sp = (uint8_t *)&cc->gpr[0];

293. #elif LJ_TARGET_ARM64

294. /* -- ARM64 calling conventions ------------------------------------------- */

296. ‌#define CCALL_HANDLE_STRUCTRET \

297. cc->retref = !ccall_classify_struct(cts, ctr); \

298. if (cc->retref) cc->retp = dp;

300. ‌#define CCALL_HANDLE_STRUCTRET2 \

301. unsigned int cl = ccall_classify_struct(cts, ctr); \

302. if ((cl & 4)) { /* Combine float HFA from separate registers. */ \

303. CTSize i = (cl >> 8) - 1; \

304. do { ((uint32_t *)dp)[i] = cc->fpr[i].u32; } while (i--); \

305. } else { \

306. if (cl > 1) sp = (uint8_t *)&cc->fpr[0]; \

307. memcpy(dp, sp, ctr->size); \

308. }

310. ‌#define CCALL_HANDLE_COMPLEXRET \

311. /* Complex values are returned in one or two FPRs. */ \

312. cc->retref = 0;

314. ‌#define CCALL_HANDLE_COMPLEXRET2 \

315. if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from FPRs. */ \

316. ((float *)dp)[0] = cc->fpr[0].f; \

317. ((float *)dp)[1] = cc->fpr[1].f; \

318. } else {  /* Copy complex double from FPRs. */ \

319. ((double *)dp)[0] = cc->fpr[0].d; \

320. ((double *)dp)[1] = cc->fpr[1].d; \

321. }

323. ‌#define CCALL_HANDLE_STRUCTARG \

324. unsigned int cl = ccall_classify_struct(cts, d); \

325. if (cl == 0) {  /* Pass struct by reference. */ \

326. rp = cdataptr(lj_cdata_new(cts, did, sz)); \

327. sz = CTSIZE_PTR; \

328. } else if (cl > 1) {  /* Pass struct in FPRs or on stack. */ \

329. isfp = (cl & 4) ? 2 : 1; \

330. }  /* else: Pass struct in GPRs or on stack. */

332. ‌#define CCALL_HANDLE_COMPLEXARG \

333. /* Pass complex by value in separate (!) FPRs or on stack. */ \

334. isfp = ctr->size == 2*sizeof(float) ? 2 : 1;

336. ‌#define CCALL_HANDLE_REGARG \

337. if (LJ_TARGET_IOS && isva) { \

338. /* IOS: All variadic arguments are on the stack. */ \

339. } else if (isfp) {  /* Try to pass argument in FPRs. */ \

340. int n2 = ctype_isvector(d->info) ? 1 : n*isfp; \

341. if (nfpr + n2 <= CCALL_NARG_FPR) { \

342. dp = &cc->fpr[nfpr]; \

343. nfpr += n2; \

344. goto done; \

345. } else { \

346. nfpr = CCALL_NARG_FPR;  /* Prevent reordering. */ \

347. if (LJ_TARGET_IOS && d->size < 8) goto err_nyi; \

348. } \

349. } else {  /* Try to pass argument in GPRs. */ \

350. if (!LJ_TARGET_IOS && (d->info & CTF_ALIGN) > CTALIGN_PTR) \

351. ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \

352. if (ngpr + n <= maxgpr) { \

353. dp = &cc->gpr[ngpr]; \

354. ngpr += n; \

355. goto done; \

356. } else { \

357. ngpr = maxgpr;  /* Prevent reordering. */ \

358. if (LJ_TARGET_IOS && d->size < 8) goto err_nyi; \

359. } \

360. }

362. #elif LJ_TARGET_PPC

363. /* -- PPC calling conventions --------------------------------------------- */

365. ‌#define CCALL_HANDLE_STRUCTRET \

366. cc->retref = 1;  /* Return all structs by reference. */ \

367. cc->gpr[ngpr++] = (GPRArg)dp;

369. ‌#define CCALL_HANDLE_COMPLEXRET \

370. /* Complex values are returned in 2 or 4 GPRs. */ \

371. cc->retref = 0;

373. ‌#define CCALL_HANDLE_COMPLEXRET2 \

374. memcpy(dp, sp, ctr->size);  /* Copy complex from GPRs. */

376. ‌#define CCALL_HANDLE_STRUCTARG \

377. rp = cdataptr(lj_cdata_new(cts, did, sz)); \

378. sz = CTSIZE_PTR;  /* Pass all structs by reference. */

380. ‌#define CCALL_HANDLE_COMPLEXARG \

381. /* Pass complex by value in 2 or 4 GPRs. */

383. ‌#define CCALL_HANDLE_REGARG \

384. if (isfp) {  /* Try to pass argument in FPRs. */ \

385. if (nfpr + 1 <= CCALL_NARG_FPR) { \

386. dp = &cc->fpr[nfpr]; \

387. nfpr += 1; \

388. d = ctype_get(cts, CTID_DOUBLE);  /* FPRs always hold doubles. */ \

389. goto done; \

390. } \

391. } else {  /* Try to pass argument in GPRs. */ \

392. if (n > 1) { \

393. lua_assert(n == 2 || n == 4);  /* int64_t or complex (float). */ \

394. if (ctype_isinteger(d->info)) \

395. ngpr = (ngpr + 1u) & ~1u;  /* Align int64_t to regpair. */ \

396. else if (ngpr + n > maxgpr) \

397. ngpr = maxgpr;  /* Prevent reordering. */ \

398. } \

399. if (ngpr + n <= maxgpr) { \

400. dp = &cc->gpr[ngpr]; \

401. ngpr += n; \

402. goto done; \

403. } \

404. }

406. ‌#define CCALL_HANDLE_RET \

407. if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \

408. ctr = ctype_get(cts, CTID_DOUBLE);  /* FPRs always hold doubles. */

410. #elif LJ_TARGET_MIPS

411. /* -- MIPS calling conventions -------------------------------------------- */

413. ‌#define CCALL_HANDLE_STRUCTRET \

414. cc->retref = 1;  /* Return all structs by reference. */ \

415. cc->gpr[ngpr++] = (GPRArg)dp;

417. ‌#define CCALL_HANDLE_COMPLEXRET \

418. /* Complex values are returned in 1 or 2 FPRs. */ \

419. cc->retref = 0;

421. ‌#define CCALL_HANDLE_COMPLEXRET2 \

422. if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from FPRs. */ \

423. ((float *)dp)[0] = cc->fpr[0].f; \

424. ((float *)dp)[1] = cc->fpr[1].f; \

425. } else {  /* Copy complex double from FPRs. */ \

426. ((double *)dp)[0] = cc->fpr[0].d; \

427. ((double *)dp)[1] = cc->fpr[1].d; \

428. }

430. ‌#define CCALL_HANDLE_STRUCTARG \

431. /* Pass all structs by value in registers and/or on the stack. */

433. ‌#define CCALL_HANDLE_COMPLEXARG \

434. /* Pass complex by value in 2 or 4 GPRs. */

436. ‌#define CCALL_HANDLE_REGARG \

437. if (isfp && nfpr < CCALL_NARG_FPR && !(ct->info & CTF_VARARG)) { \

438. /* Try to pass argument in FPRs. */ \

439. dp = n == 1 ? (void *)&cc->fpr[nfpr].f : (void *)&cc->fpr[nfpr].d; \

440. nfpr++; ngpr += n; \

441. goto done; \

442. } else {  /* Try to pass argument in GPRs. */ \

443. nfpr = CCALL_NARG_FPR; \

444. if ((d->info & CTF_ALIGN) > CTALIGN_PTR) \

445. ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \

446. if (ngpr < maxgpr) { \

447. dp = &cc->gpr[ngpr]; \

448. if (ngpr + n > maxgpr) { \

449. nsp += ngpr + n - maxgpr;  /* Assumes contiguous gpr/stack fields. */ \

450. if (nsp > CCALL_MAXSTACK) goto err_nyi;  /* Too many arguments. */ \

451. ngpr = maxgpr; \

452. } else { \

453. ngpr += n; \

454. } \

455. goto done; \

456. } \

457. }

459. ‌#define CCALL_HANDLE_RET \

460. if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \

461. sp = (uint8_t *)&cc->fpr[0].f;

463. #else

464. #error "Missing calling convention definitions for this architecture"

465. #endif

467. #ifndef CCALL_HANDLE_STRUCTRET2

468. ‌#define CCALL_HANDLE_STRUCTRET2 \

469. memcpy(dp, sp, ctr->size);  /* Copy struct return value from GPRs. */

470. #endif

472. /* -- x86 OSX ABI struct classification ----------------------------------- */

474. #if LJ_TARGET_X86 && LJ_TARGET_OSX

476. /* Check for struct with single FP field. */

477. ‌static int ccall_classify_struct(CTState *cts, CType *ct)

478. {

479. CTSize sz = ct->size;

480. if (!(sz == sizeof(float) || sz == sizeof(double))) return 0;

481. if ((ct->info & CTF_UNION)) return 0;

482. while (ct->sib) {

483. ct = ctype_get(cts, ct->sib);

484. if (ctype_isfield(ct->info)) {

485. CType *sct = ctype_rawchild(cts, ct);

486. if (ctype_isfp(sct->info)) {

487. if (sct->size == sz)

488. return (sz >> 2);  /* Return 1 for float or 2 for double. */

489. } else if (ctype_isstruct(sct->info)) {

490. if (sct->size)

491. return ccall_classify_struct(cts, sct);

492. } else {

493. break;

494. }

495. } else if (ctype_isbitfield(ct->info)) {

496. break;

497. } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {

498. CType *sct = ctype_rawchild(cts, ct);

499. if (sct->size)

500. return ccall_classify_struct(cts, sct);

501. }

502. }

503. return 0;

504. }

506. #endif

508. /* -- x64 struct classification ------------------------------------------- */

510. #if LJ_TARGET_X64 && !LJ_ABI_WIN

512. /* Register classes for x64 struct classification. */

513. ‌#define CCALL_RCL_INT        1

514. ‌#define CCALL_RCL_SSE        2

515. ‌#define CCALL_RCL_MEM        4

516. /* NYI: classify vectors. */

518. static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs);

520. /* Classify a C type. */

521. ‌static void ccall_classify_ct(CTState *cts, CType *ct, int *rcl, CTSize ofs)

522. {

523. if (ctype_isarray(ct->info)) {

524. CType *cct = ctype_rawchild(cts, ct);

525. CTSize eofs, esz = cct->size, asz = ct->size;

526. for (eofs = 0; eofs < asz; eofs += esz)

527. ccall_classify_ct(cts, cct, rcl, ofs+eofs);

528. } else if (ctype_isstruct(ct->info)) {

529. ccall_classify_struct(cts, ct, rcl, ofs);

530. } else {

531. int cl = ctype_isfp(ct->info) ? CCALL_RCL_SSE : CCALL_RCL_INT;

532. lua_assert(ctype_hassize(ct->info));

533. if ((ofs & (ct->size-1))) cl = CCALL_RCL_MEM;  /* Unaligned. */

534. rcl[(ofs >= 8)] |= cl;

535. }

536. }

538. /* Recursively classify a struct based on its fields. */

539. ‌static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs)

540. {

541. if (ct->size > 16) return CCALL_RCL_MEM;  /* Too big, gets memory class. */

542. while (ct->sib) {

543. CTSize fofs;

544. ct = ctype_get(cts, ct->sib);

545. fofs = ofs+ct->size;

546. if (ctype_isfield(ct->info))

547. ccall_classify_ct(cts, ctype_rawchild(cts, ct), rcl, fofs);

548. else if (ctype_isbitfield(ct->info))

549. rcl[(fofs >= 8)] |= CCALL_RCL_INT;  /* NYI: unaligned bitfields? */

550. else if (ctype_isxattrib(ct->info, CTA_SUBTYPE))

551. ccall_classify_struct(cts, ctype_rawchild(cts, ct), rcl, fofs);

552. }

553. return ((rcl[0]|rcl[1]) & CCALL_RCL_MEM);  /* Memory class? */

554. }

556. /* Try to split up a small struct into registers. */

557. ‌static int ccall_struct_reg(CCallState *cc, GPRArg *dp, int *rcl)

558. {

559. MSize ngpr = cc->ngpr, nfpr = cc->nfpr;

560. uint32_t i;

561. for (i = 0; i < 2; i++) {

562. lua_assert(!(rcl[i] & CCALL_RCL_MEM));

563. if ((rcl[i] & CCALL_RCL_INT)) {  /* Integer class takes precedence. */

564. if (ngpr >= CCALL_NARG_GPR) return 1;  /* Register overflow. */

565. cc->gpr[ngpr++] = dp[i];

566. } else if ((rcl[i] & CCALL_RCL_SSE)) {

567. if (nfpr >= CCALL_NARG_FPR) return 1;  /* Register overflow. */

568. cc->fpr[nfpr++].l[0] = dp[i];

569. }

570. }

571. cc->ngpr = ngpr; cc->nfpr = nfpr;

572. return 0;  /* Ok. */

573. }

575. /* Pass a small struct argument. */

576. ‌static int ccall_struct_arg(CCallState *cc, CTState *cts, CType *d, int *rcl,

577. TValue *o, int narg)

578. {

579. GPRArg dp[2];

580. dp[0] = dp[1] = 0;

581. /* Convert to temp. struct. */

582. lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg));

583. if (ccall_struct_reg(cc, dp, rcl)) {  /* Register overflow? Pass on stack. */

584. MSize nsp = cc->nsp, n = rcl[1] ? 2 : 1;

585. if (nsp + n > CCALL_MAXSTACK) return 1;  /* Too many arguments. */

586. cc->nsp = nsp + n;

587. memcpy(&cc->stack[nsp], dp, n*CTSIZE_PTR);

588. }

589. return 0;  /* Ok. */

590. }

592. /* Combine returned small struct. */

593. ‌static void ccall_struct_ret(CCallState *cc, int *rcl, uint8_t *dp, CTSize sz)

594. {

595. GPRArg sp[2];

596. MSize ngpr = 0, nfpr = 0;

597. uint32_t i;

598. for (i = 0; i < 2; i++) {

599. if ((rcl[i] & CCALL_RCL_INT)) {  /* Integer class takes precedence. */

600. sp[i] = cc->gpr[ngpr++];

601. } else if ((rcl[i] & CCALL_RCL_SSE)) {

602. sp[i] = cc->fpr[nfpr++].l[0];

603. }

604. }

605. memcpy(dp, sp, sz);

606. }

607. #endif

609. /* -- ARM hard-float ABI struct classification ---------------------------- */

611. #if LJ_TARGET_ARM && !LJ_ABI_SOFTFP

613. /* Classify a struct based on its fields. */

614. ‌static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf)

615. {

616. CTSize sz = ct->size;

617. unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION);

618. if ((ctf->info & CTF_VARARG)) goto noth;

619. while (ct->sib) {

620. CType *sct;

621. ct = ctype_get(cts, ct->sib);

622. if (ctype_isfield(ct->info)) {

623. sct = ctype_rawchild(cts, ct);

624. if (ctype_isfp(sct->info)) {

625. r |= sct->size;

626. if (!isu) n++; else if (n == 0) n = 1;

627. } else if (ctype_iscomplex(sct->info)) {

628. r |= (sct->size >> 1);

629. if (!isu) n += 2; else if (n < 2) n = 2;

630. } else if (ctype_isstruct(sct->info)) {

631. goto substruct;

632. } else {

633. goto noth;

634. }

635. } else if (ctype_isbitfield(ct->info)) {

636. goto noth;

637. } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {

638. sct = ctype_rawchild(cts, ct);

639. substruct:

640. if (sct->size > 0) {

641. unsigned int s = ccall_classify_struct(cts, sct, ctf);

642. if (s <= 1) goto noth;

643. r |= (s & 255);

644. if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8);

645. }

646. }

647. }

648. if ((r == 4 || r == 8) && n <= 4)

649. return r + (n << 8);

650. noth:  /* Not a homogeneous float/double aggregate. */

651. return (sz <= 4);  /* Return structs of size <= 4 in a GPR. */

652. }

654. #endif

656. /* -- ARM64 ABI struct classification ------------------------------------- */

658. #if LJ_TARGET_ARM64

660. /* Classify a struct based on its fields. */

661. ‌static unsigned int ccall_classify_struct(CTState *cts, CType *ct)

662. {

663. CTSize sz = ct->size;

664. unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION);

665. while (ct->sib) {

666. CType *sct;

667. ct = ctype_get(cts, ct->sib);

668. if (ctype_isfield(ct->info)) {

669. sct = ctype_rawchild(cts, ct);

670. if (ctype_isfp(sct->info)) {

671. r |= sct->size;

672. if (!isu) n++; else if (n == 0) n = 1;

673. } else if (ctype_iscomplex(sct->info)) {

674. r |= (sct->size >> 1);

675. if (!isu) n += 2; else if (n < 2) n = 2;

676. } else if (ctype_isstruct(sct->info)) {

677. goto substruct;

678. } else {

679. goto noth;

680. }

681. } else if (ctype_isbitfield(ct->info)) {

682. goto noth;

683. } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {

684. sct = ctype_rawchild(cts, ct);

685. substruct:

686. if (sct->size > 0) {

687. unsigned int s = ccall_classify_struct(cts, sct);

688. if (s <= 1) goto noth;

689. r |= (s & 255);

690. if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8);

691. }

692. }

693. }

694. if ((r == 4 || r == 8) && n <= 4)

695. return r + (n << 8);

696. noth:  /* Not a homogeneous float/double aggregate. */

697. return (sz <= 16);  /* Return structs of size <= 16 in GPRs. */

698. }

700. #endif

702. /* -- Common C call handling ---------------------------------------------- */

704. /* Infer the destination CTypeID for a vararg argument. */

705. ‌CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o)

706. {

707. if (tvisnumber(o)) {

708. return CTID_DOUBLE;

709. } else if (tviscdata(o)) {

710. CTypeID id = cdataV(o)->ctypeid;

711. CType *s = ctype_get(cts, id);

712. if (ctype_isrefarray(s->info)) {

713. return lj_ctype_intern(cts,

714. CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(s->info)), CTSIZE_PTR);

715. } else if (ctype_isstruct(s->info) || ctype_isfunc(s->info)) {

716. /* NYI: how to pass a struct by value in a vararg argument? */

717. return lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR);

718. } else if (ctype_isfp(s->info) && s->size == sizeof(float)) {

719. return CTID_DOUBLE;

720. } else {

721. return id;

722. }

723. } else if (tvisstr(o)) {

724. return CTID_P_CCHAR;

725. } else if (tvisbool(o)) {

726. return CTID_BOOL;

727. } else {

728. return CTID_P_VOID;

729. }

730. }

732. /* Setup arguments for C call. */

733. ‌static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,

734. CCallState *cc)

735. {

736. int gcsteps = 0;

737. TValue *o, *top = L->top;

738. CTypeID fid;

739. CType *ctr;

740. MSize maxgpr, ngpr = 0, nsp = 0, narg;

741. #if CCALL_NARG_FPR

742. MSize nfpr = 0;

743. #if LJ_TARGET_ARM

744. MSize fprodd = 0;

745. #endif

746. #endif

748. /* Clear unused regs to get some determinism in case of misdeclaration. */

749. memset(cc->gpr, 0, sizeof(cc->gpr));

750. #if CCALL_NUM_FPR

751. memset(cc->fpr, 0, sizeof(cc->fpr));

752. #endif

754. #if LJ_TARGET_X86

755. /* x86 has several different calling conventions. */

756. cc->resx87 = 0;

757. switch (ctype_cconv(ct->info)) {

758. case CTCC_FASTCALL: maxgpr = 2; break;

759. case CTCC_THISCALL: maxgpr = 1; break;

760. default: maxgpr = 0; break;

761. }

762. #else

763. maxgpr = CCALL_NARG_GPR;

764. #endif

766. /* Perform required setup for some result types. */

767. ctr = ctype_rawchild(cts, ct);

768. if (ctype_isvector(ctr->info)) {

769. if (!(CCALL_VECTOR_REG && (ctr->size == 8 || ctr->size == 16)))

770. goto err_nyi;

771. } else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) {

772. /* Preallocate cdata object and anchor it after arguments. */

773. CTSize sz = ctr->size;

774. GCcdata *cd = lj_cdata_new(cts, ctype_cid(ct->info), sz);

775. void *dp = cdataptr(cd);

776. setcdataV(L, L->top++, cd);

777. if (ctype_isstruct(ctr->info)) {

778. CCALL_HANDLE_STRUCTRET

779. } else {

780. CCALL_HANDLE_COMPLEXRET

781. }

782. #if LJ_TARGET_X86

783. } else if (ctype_isfp(ctr->info)) {

784. cc->resx87 = ctr->size == sizeof(float) ? 1 : 2;

785. #endif

786. }

788. /* Skip initial attributes. */

789. fid = ct->sib;

790. while (fid) {

791. CType *ctf = ctype_get(cts, fid);

792. if (!ctype_isattrib(ctf->info)) break;

793. fid = ctf->sib;

794. }

796. /* Walk through all passed arguments. */

797. for (o = L->base+1, narg = 1; o < top; o++, narg++) {

798. CTypeID did;

799. CType *d;

800. CTSize sz;

801. MSize n, isfp = 0, isva = 0;

802. void *dp, *rp = NULL;

804. if (fid) {  /* Get argument type from field. */

805. CType *ctf = ctype_get(cts, fid);

806. fid = ctf->sib;

807. lua_assert(ctype_isfield(ctf->info));

808. did = ctype_cid(ctf->info);

809. } else {

810. if (!(ct->info & CTF_VARARG))

811. lj_err_caller(L, LJ_ERR_FFI_NUMARG);  /* Too many arguments. */

812. did = lj_ccall_ctid_vararg(cts, o);  /* Infer vararg type. */

813. isva = 1;

814. }

815. d = ctype_raw(cts, did);

816. sz = d->size;

818. /* Find out how (by value/ref) and where (GPR/FPR) to pass an argument. */

819. if (ctype_isnum(d->info)) {

820. if (sz > 8) goto err_nyi;

821. if ((d->info & CTF_FP))

822. isfp = 1;

823. } else if (ctype_isvector(d->info)) {

824. if (CCALL_VECTOR_REG && (sz == 8 || sz == 16))

825. isfp = 1;

826. else

827. goto err_nyi;

828. } else if (ctype_isstruct(d->info)) {

829. CCALL_HANDLE_STRUCTARG

830. } else if (ctype_iscomplex(d->info)) {

831. CCALL_HANDLE_COMPLEXARG

832. } else {

833. sz = CTSIZE_PTR;

834. }

835. sz = (sz + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);

836. n = sz / CTSIZE_PTR;  /* Number of GPRs or stack slots needed. */

838. CCALL_HANDLE_REGARG  /* Handle register arguments. */

840. /* Otherwise pass argument on stack. */

841. if (CCALL_ALIGN_STACKARG && !rp && (d->info & CTF_ALIGN) > CTALIGN_PTR) {

842. MSize align = (1u << ctype_align(d->info-CTALIGN_PTR)) -1;

843. nsp = (nsp + align) & ~align;  /* Align argument on stack. */

844. }

845. if (nsp + n > CCALL_MAXSTACK) {  /* Too many arguments. */

846. err_nyi:

847. lj_err_caller(L, LJ_ERR_FFI_NYICALL);

848. }

849. dp = &cc->stack[nsp];

850. nsp += n;

851. isva = 0;

853. done:

854. if (rp) {  /* Pass by reference. */

855. gcsteps++;

856. *(void **)dp = rp;

857. dp = rp;

858. }

859. lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg));

860. /* Extend passed integers to 32 bits at least. */

861. if (ctype_isinteger_or_bool(d->info) && d->size < 4) {

862. if (d->info & CTF_UNSIGNED)

863. *(uint32_t *)dp = d->size == 1 ? (uint32_t)*(uint8_t *)dp :

864. (uint32_t)*(uint16_t *)dp;

865. else

866. *(int32_t *)dp = d->size == 1 ? (int32_t)*(int8_t *)dp :

867. (int32_t)*(int16_t *)dp;

868. }

869. #if LJ_TARGET_X64 && LJ_ABI_WIN

870. if (isva) {  /* Windows/x64 mirrors varargs in both register sets. */

871. if (nfpr == ngpr)

872. cc->gpr[ngpr-1] = cc->fpr[ngpr-1].l[0];

873. else

874. cc->fpr[ngpr-1].l[0] = cc->gpr[ngpr-1];

875. }

876. #else

877. UNUSED(isva);

878. #endif

879. #if LJ_TARGET_X64 && !LJ_ABI_WIN

880. if (isfp == 2 && n == 2 && (uint8_t *)dp == (uint8_t *)&cc->fpr[nfpr-2]) {

881. cc->fpr[nfpr-1].d[0] = cc->fpr[nfpr-2].d[1];  /* Split complex double. */

882. cc->fpr[nfpr-2].d[1] = 0;

883. }

884. #elif LJ_TARGET_ARM64

885. if (isfp == 2 && (uint8_t *)dp < (uint8_t *)cc->stack) {

886. /* Split float HFA or complex float into separate registers. */

887. CTSize i = (sz >> 2) - 1;

888. do { ((uint64_t *)dp)[i] = ((uint32_t *)dp)[i]; } while (i--);

889. }

890. #else

891. UNUSED(isfp);

892. #endif

893. }

894. if (fid) lj_err_caller(L, LJ_ERR_FFI_NUMARG);  /* Too few arguments. */

896. #if LJ_TARGET_X64 || LJ_TARGET_PPC

897. cc->nfpr = nfpr;  /* Required for vararg functions. */

898. #endif

899. cc->nsp = nsp;

900. cc->spadj = (CCALL_SPS_FREE + CCALL_SPS_EXTRA)*CTSIZE_PTR;

901. if (nsp > CCALL_SPS_FREE)

902. cc->spadj += (((nsp-CCALL_SPS_FREE)*CTSIZE_PTR + 15u) & ~15u);

903. return gcsteps;

904. }

906. /* Get results from C call. */

907. ‌static int ccall_get_results(lua_State *L, CTState *cts, CType *ct,

908. CCallState *cc, int *ret)

909. {

910. CType *ctr = ctype_rawchild(cts, ct);

911. uint8_t *sp = (uint8_t *)&cc->gpr[0];

912. if (ctype_isvoid(ctr->info)) {

913. *ret = 0;  /* Zero results. */

914. return 0;  /* No additional GC step. */

915. }

916. *ret = 1;  /* One result. */

917. if (ctype_isstruct(ctr->info)) {

918. /* Return cdata object which is already on top of stack. */

919. if (!cc->retref) {

920. void *dp = cdataptr(cdataV(L->top-1));  /* Use preallocated object. */

921. CCALL_HANDLE_STRUCTRET2

922. }

923. return 1;  /* One GC step. */

924. }

925. if (ctype_iscomplex(ctr->info)) {

926. /* Return cdata object which is already on top of stack. */

927. void *dp = cdataptr(cdataV(L->top-1));  /* Use preallocated object. */

928. CCALL_HANDLE_COMPLEXRET2

929. return 1;  /* One GC step. */

930. }

931. if (LJ_BE && ctype_isinteger_or_bool(ctr->info) && ctr->size < CTSIZE_PTR)

932. sp += (CTSIZE_PTR - ctr->size);

933. #if CCALL_NUM_FPR

934. if (ctype_isfp(ctr->info) || ctype_isvector(ctr->info))

935. sp = (uint8_t *)&cc->fpr[0];

936. #endif

937. #ifdef CCALL_HANDLE_RET

938. CCALL_HANDLE_RET

939. #endif

940. /* No reference types end up here, so there's no need for the CTypeID. */

941. lua_assert(!(ctype_isrefarray(ctr->info) || ctype_isstruct(ctr->info)));

942. return lj_cconv_tv_ct(cts, ctr, 0, L->top-1, sp);

943. }

945. /* Call C function. */

946. ‌int lj_ccall_func(lua_State *L, GCcdata *cd)

947. {

948. CTState *cts = ctype_cts(L);

949. CType *ct = ctype_raw(cts, cd->ctypeid);

950. CTSize sz = CTSIZE_PTR;

951. if (ctype_isptr(ct->info)) {

952. sz = ct->size;

953. ct = ctype_rawchild(cts, ct);

954. }

955. if (ctype_isfunc(ct->info)) {

956. CCallState cc;

957. int gcsteps, ret;

958. cc.func = (void (*)(void))cdata_getptr(cdataptr(cd), sz);

959. gcsteps = ccall_set_args(L, cts, ct, &cc);

960. ct = (CType *)((intptr_t)ct-(intptr_t)cts->tab);

961. cts->cb.slot = ~0u;

962. lj_vm_ffi_call(&cc);

963. if (cts->cb.slot != ~0u) {  /* Blacklist function that called a callback. */

964. TValue tv;

965. setlightudV(&tv, (void *)cc.func);

966. setboolV(lj_tab_set(L, cts->miscmap, &tv), 1);

967. }

968. ct = (CType *)((intptr_t)ct+(intptr_t)cts->tab);  /* May be reallocated. */

969. gcsteps += ccall_get_results(L, cts, ct, &cc, &ret);

970. #if LJ_TARGET_X86 && LJ_ABI_WIN

971. /* Automatically detect __stdcall and fix up C function declaration. */

972. if (cc.spadj && ctype_cconv(ct->info) == CTCC_CDECL) {

973. CTF_INSERT(ct->info, CCONV, CTCC_STDCALL);

974. lj_trace_abort(G(L));

975. }

976. #endif

977. while (gcsteps-- > 0)

978. lj_gc_check(L);

979. return ret;

980. }

981. return -1;  /* Not a function. */

982. }

984. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ccall.h - luajit-2.0-src

Global variables defined

- FPRArg

Data types defined

- CCallState

- CCallState

- FPRArg

- FPRArg

- FPRArg

- FPRArg

- FPRArg

- FPRArg

- FPRArg

- FPRArg

- GPRArg

- GPRArg

- GPRArg

- GPRArg

- GPRArg

Macros defined

- CCALL_ALIGN_CALLSTATE

- CCALL_ALIGN_CALLSTATE

- CCALL_ALIGN_STACKARG

- CCALL_ALIGN_STACKARG

- CCALL_MAXSTACK

- CCALL_NARG_FPR

- CCALL_NARG_FPR

- CCALL_NARG_FPR

- CCALL_NARG_FPR

- CCALL_NARG_FPR

- CCALL_NARG_FPR

- CCALL_NARG_FPR

- CCALL_NARG_FPR

- CCALL_NARG_GPR

- CCALL_NARG_GPR

- CCALL_NARG_GPR

- CCALL_NARG_GPR

- CCALL_NARG_GPR

- CCALL_NARG_GPR

- CCALL_NARG_GPR

- CCALL_NRET_FPR

- CCALL_NRET_FPR

- CCALL_NRET_FPR

- CCALL_NRET_FPR

- CCALL_NRET_FPR

- CCALL_NRET_FPR

- CCALL_NRET_FPR

- CCALL_NRET_FPR

- CCALL_NRET_GPR

- CCALL_NRET_GPR

- CCALL_NRET_GPR

- CCALL_NRET_GPR

- CCALL_NRET_GPR

- CCALL_NRET_GPR

- CCALL_NRET_GPR

- CCALL_NUM_FPR

- CCALL_NUM_GPR

- CCALL_SPS_EXTRA

- CCALL_SPS_EXTRA

- CCALL_SPS_EXTRA

- CCALL_SPS_EXTRA

- CCALL_SPS_FREE

- CCALL_SPS_FREE

- CCALL_SPS_FREE

- CCALL_SPS_FREE

- CCALL_SPS_FREE

- CCALL_VECTOR_REG

- CCALL_VECTOR_REG

- _LJ_CCALL_H

Source code

1. /*

2. ** FFI C call handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_CCALL_H

7. ‌#define _LJ_CCALL_H

9. #include "lj_obj.h"

10. #include "lj_ctype.h"

12. #if LJ_HASFFI

14. /* -- C calling conventions ----------------------------------------------- */

16. #if LJ_TARGET_X86ORX64

18. #if LJ_TARGET_X86

19. ‌#define CCALL_NARG_GPR                2        /* For fastcall arguments. */

20. ‌#define CCALL_NARG_FPR                0

21. ‌#define CCALL_NRET_GPR                2

22. ‌#define CCALL_NRET_FPR                1        /* For FP results on x87 stack. */

23. ‌#define CCALL_ALIGN_STACKARG        0        /* Don't align argument on stack. */

24. #elif LJ_ABI_WIN

25. ‌#define CCALL_NARG_GPR                4

26. ‌#define CCALL_NARG_FPR                4

27. ‌#define CCALL_NRET_GPR                1

28. ‌#define CCALL_NRET_FPR                1

29. ‌#define CCALL_SPS_EXTRA                4

30. #else

31. ‌#define CCALL_NARG_GPR                6

32. ‌#define CCALL_NARG_FPR                8

33. ‌#define CCALL_NRET_GPR                2

34. ‌#define CCALL_NRET_FPR                2

35. ‌#define CCALL_VECTOR_REG        1        /* Pass vectors in registers. */

36. #endif

38. ‌#define CCALL_SPS_FREE                1

39. ‌#define CCALL_ALIGN_CALLSTATE        16

41. ‌typedef LJ_ALIGN(16) union FPRArg {

42. double d[2];

43. float f[4];

44. uint8_t b[16];

45. uint16_t s[8];

46. int i[4];

47. int64_t l[2];

48. ‌} FPRArg;

50. ‌typedef intptr_t GPRArg;

52. #elif LJ_TARGET_ARM

54. ‌#define CCALL_NARG_GPR                4

55. ‌#define CCALL_NRET_GPR                2        /* For softfp double. */

56. #if LJ_ABI_SOFTFP

57. ‌#define CCALL_NARG_FPR                0

58. ‌#define CCALL_NRET_FPR                0

59. #else

60. ‌#define CCALL_NARG_FPR                8

61. ‌#define CCALL_NRET_FPR                4

62. #endif

63. ‌#define CCALL_SPS_FREE                0

65. ‌typedef intptr_t GPRArg;

66. ‌typedef union FPRArg {

67. double d;

68. float f[2];

69. ‌} FPRArg;

71. #elif LJ_TARGET_ARM64

73. ‌#define CCALL_NARG_GPR                8

74. ‌#define CCALL_NRET_GPR                2

75. ‌#define CCALL_NARG_FPR                8

76. ‌#define CCALL_NRET_FPR                4

77. ‌#define CCALL_SPS_FREE                0

79. ‌typedef intptr_t GPRArg;

80. ‌typedef union FPRArg {

81. double d;

82. float f;

83. uint32_t u32;

84. ‌} FPRArg;

86. #elif LJ_TARGET_PPC

88. ‌#define CCALL_NARG_GPR                8

89. ‌#define CCALL_NARG_FPR                8

90. ‌#define CCALL_NRET_GPR                4        /* For complex double. */

91. ‌#define CCALL_NRET_FPR                1

92. ‌#define CCALL_SPS_EXTRA                4

93. ‌#define CCALL_SPS_FREE                0

95. ‌typedef intptr_t GPRArg;

96. ‌typedef double FPRArg;

98. #elif LJ_TARGET_MIPS

100. ‌#define CCALL_NARG_GPR                4

101. ‌#define CCALL_NARG_FPR                2

102. ‌#define CCALL_NRET_GPR                2

103. ‌#define CCALL_NRET_FPR                2

104. ‌#define CCALL_SPS_EXTRA                7

105. ‌#define CCALL_SPS_FREE                1

107. ‌typedef intptr_t GPRArg;

108. ‌typedef union FPRArg {

109. double d;

110. struct { LJ_ENDIAN_LOHI(float f; , float g;) };

111. ‌} FPRArg;

113. #else

114. #error "Missing calling convention definitions for this architecture"

115. #endif

117. #ifndef CCALL_SPS_EXTRA

118. ‌#define CCALL_SPS_EXTRA                0

119. #endif

120. #ifndef CCALL_VECTOR_REG

121. ‌#define CCALL_VECTOR_REG        0

122. #endif

123. #ifndef CCALL_ALIGN_STACKARG

124. ‌#define CCALL_ALIGN_STACKARG        1

125. #endif

126. #ifndef CCALL_ALIGN_CALLSTATE

127. ‌#define CCALL_ALIGN_CALLSTATE        8

128. #endif

130. ‌#define CCALL_NUM_GPR \

131. (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR)

132. ‌#define CCALL_NUM_FPR \

133. (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR)

135. /* Check against constants in lj_ctype.h. */

136. LJ_STATIC_ASSERT(CCALL_NUM_GPR <= CCALL_MAX_GPR);

137. LJ_STATIC_ASSERT(CCALL_NUM_FPR <= CCALL_MAX_FPR);

139. ‌#define CCALL_MAXSTACK                32

141. /* -- C call state -------------------------------------------------------- */

143. ‌typedef LJ_ALIGN(CCALL_ALIGN_CALLSTATE) struct CCallState {

144. void (*func)(void);                /* Pointer to called function. */

145. uint32_t spadj;                /* Stack pointer adjustment. */

146. uint8_t nsp;                        /* Number of stack slots. */

147. uint8_t retref;                /* Return value by reference. */

148. #if LJ_TARGET_X64

149. uint8_t ngpr;                        /* Number of arguments in GPRs. */

150. uint8_t nfpr;                        /* Number of arguments in FPRs. */

151. #elif LJ_TARGET_X86

152. uint8_t resx87;                /* Result on x87 stack: 1:float, 2:double. */

153. #elif LJ_TARGET_ARM64

154. void *retp;                        /* Aggregate return pointer in x8. */

155. #elif LJ_TARGET_PPC

156. uint8_t nfpr;                        /* Number of arguments in FPRs. */

157. #endif

158. #if LJ_32

159. int32_t align1;

160. #endif

161. #if CCALL_NUM_FPR

162. FPRArg fpr[CCALL_NUM_FPR];        /* Arguments/results in FPRs. */

163. #endif

164. GPRArg gpr[CCALL_NUM_GPR];        /* Arguments/results in GPRs. */

165. GPRArg stack[CCALL_MAXSTACK];        /* Stack slots. */

166. ‌} CCallState;

168. /* -- C call handling ----------------------------------------------------- */

170. /* Really belongs to lj_vm.h. */

171. LJ_ASMF void LJ_FASTCALL lj_vm_ffi_call(CCallState *cc);

173. LJ_FUNC CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o);

174. LJ_FUNC int lj_ccall_func(lua_State *L, GCcdata *cd);

176. #endif

178. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_init.c - luajit-2.0-src

Global variables defined

- lj_lib_load

- lj_lib_preload

Functions defined

- luaL_openlibs

Macros defined

- LUA_LIB

- lib_init_c

Source code

1. /*

2. ** Library initialization.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major parts taken verbatim from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lib_init_c

10. ‌#define LUA_LIB

12. #include "lua.h"

13. #include "lauxlib.h"

14. #include "lualib.h"

16. #include "lj_arch.h"

18. ‌static const luaL_Reg lj_lib_load[] = {

19. { "",                        luaopen_base },

20. { LUA_LOADLIBNAME,        luaopen_package },

21. { LUA_TABLIBNAME,        luaopen_table },

22. { LUA_IOLIBNAME,        luaopen_io },

23. { LUA_OSLIBNAME,        luaopen_os },

24. { LUA_STRLIBNAME,        luaopen_string },

25. { LUA_MATHLIBNAME,        luaopen_math },

26. { LUA_DBLIBNAME,        luaopen_debug },

27. { LUA_BITLIBNAME,        luaopen_bit },

28. { LUA_JITLIBNAME,        luaopen_jit },

29. { NULL,                NULL }

30. };

32. ‌static const luaL_Reg lj_lib_preload[] = {

33. #if LJ_HASFFI

34. { LUA_FFILIBNAME,        luaopen_ffi },

35. #endif

36. { NULL,                NULL }

37. };

39. ‌LUALIB_API void luaL_openlibs(lua_State *L)

40. {

41. const luaL_Reg *lib;

42. for (lib = lj_lib_load; lib->func; lib++) {

43. lua_pushcfunction(L, lib->func);

44. lua_pushstring(L, lib->name);

45. lua_call(L, 1, 0);

46. }

47. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD",

48. sizeof(lj_lib_preload)/sizeof(lj_lib_preload[0])-1);

49. for (lib = lj_lib_preload; lib->func; lib++) {

50. lua_pushcfunction(L, lib->func);

51. lua_setfield(L, -2, lib->name);

52. }

53. lua_pop(L, 1);

54. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_opt_loop.c - luajit-2.0-src

Data types defined

- LoopState

- LoopState

Functions defined

- cploop_opt

- lj_opt_loop

- loop_emit_phi

- loop_subst_snap

- loop_undo

- loop_unroll

Macros defined

- IR

- IR

- LUA_CORE

- emitir

- emitir

- emitir_raw

- emitir_raw

- lj_opt_loop_c

Source code

1. /*

2. ** LOOP: Loop Optimizations.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_opt_loop_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT

13. #include "lj_err.h"

14. #include "lj_buf.h"

15. #include "lj_ir.h"

16. #include "lj_jit.h"

17. #include "lj_iropt.h"

18. #include "lj_trace.h"

19. #include "lj_snap.h"

20. #include "lj_vm.h"

22. /* Loop optimization:

23. **

24. ** Traditional Loop-Invariant Code Motion (LICM) splits the instructions

25. ** of a loop into invariant and variant instructions. The invariant

26. ** instructions are hoisted out of the loop and only the variant

27. ** instructions remain inside the loop body.

28. **

29. ** Unfortunately LICM is mostly useless for compiling dynamic languages.

30. ** The IR has many guards and most of the subsequent instructions are

31. ** control-dependent on them. The first non-hoistable guard would

32. ** effectively prevent hoisting of all subsequent instructions.

33. **

34. ** That's why we use a special form of unrolling using copy-substitution,

35. ** combined with redundancy elimination:

36. **

37. ** The recorded instruction stream is re-emitted to the compiler pipeline

38. ** with substituted operands. The substitution table is filled with the

39. ** refs returned by re-emitting each instruction. This can be done

40. ** on-the-fly, because the IR is in strict SSA form, where every ref is

41. ** defined before its use.

42. **

43. ** This aproach generates two code sections, separated by the LOOP

44. ** instruction:

45. **

46. ** 1. The recorded instructions form a kind of pre-roll for the loop. It

47. ** contains a mix of invariant and variant instructions and performs

48. ** exactly one loop iteration (but not necessarily the 1st iteration).

49. **

50. ** 2. The loop body contains only the variant instructions and performs

51. ** all remaining loop iterations.

52. **

53. ** On first sight that looks like a waste of space, because the variant

54. ** instructions are present twice. But the key insight is that the

55. ** pre-roll honors the control-dependencies for *both* the pre-roll itself

56. ** *and* the loop body!

57. **

58. ** It also means one doesn't have to explicitly model control-dependencies

59. ** (which, BTW, wouldn't help LICM much). And it's much easier to

60. ** integrate sparse snapshotting with this approach.

61. **

62. ** One of the nicest aspects of this approach is that all of the

63. ** optimizations of the compiler pipeline (FOLD, CSE, FWD, etc.) can be

64. ** reused with only minor restrictions (e.g. one should not fold

65. ** instructions across loop-carried dependencies).

66. **

67. ** But in general all optimizations can be applied which only need to look

68. ** backwards into the generated instruction stream. At any point in time

69. ** during the copy-substitution process this contains both a static loop

70. ** iteration (the pre-roll) and a dynamic one (from the to-be-copied

71. ** instruction up to the end of the partial loop body).

72. **

73. ** Since control-dependencies are implicitly kept, CSE also applies to all

74. ** kinds of guards. The major advantage is that all invariant guards can

75. ** be hoisted, too.

76. **

77. ** Load/store forwarding works across loop iterations, too. This is

78. ** important if loop-carried dependencies are kept in upvalues or tables.

79. ** E.g. 'self.idx = self.idx + 1' deep down in some OO-style method may

80. ** become a forwarded loop-recurrence after inlining.

81. **

82. ** Since the IR is in SSA form, loop-carried dependencies have to be

83. ** modeled with PHI instructions. The potential candidates for PHIs are

84. ** collected on-the-fly during copy-substitution. After eliminating the

85. ** redundant ones, PHI instructions are emitted *below* the loop body.

86. **

87. ** Note that this departure from traditional SSA form doesn't change the

88. ** semantics of the PHI instructions themselves. But it greatly simplifies

89. ** on-the-fly generation of the IR and the machine code.

90. */

92. /* Some local macros to save typing. Undef'd at the end. */

93. ‌#define IR(ref)                (&J->cur.ir[(ref)])

95. /* Pass IR on to next optimization in chain (FOLD). */

96. ‌#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))

98. /* Emit raw IR without passing through optimizations. */

99. ‌#define emitir_raw(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J))

101. /* -- PHI elimination ----------------------------------------------------- */

103. /* Emit or eliminate collected PHIs. */

104. ‌static void loop_emit_phi(jit_State *J, IRRef1 *subst, IRRef1 *phi, IRRef nphi,

105. SnapNo onsnap)

106. {

107. int passx = 0;

108. IRRef i, j, nslots;

109. IRRef invar = J->chain[IR_LOOP];

110. /* Pass #1: mark redundant and potentially redundant PHIs. */

111. for (i = 0, j = 0; i < nphi; i++) {

112. IRRef lref = phi[i];

113. IRRef rref = subst[lref];

114. if (lref == rref || rref == REF_DROP) {  /* Invariants are redundant. */

115. irt_clearphi(IR(lref)->t);

116. } else {

117. phi[j++] = (IRRef1)lref;

118. if (!(IR(rref)->op1 == lref || IR(rref)->op2 == lref)) {

119. /* Quick check for simple recurrences failed, need pass2. */

120. irt_setmark(IR(lref)->t);

121. passx = 1;

122. }

123. }

124. }

125. nphi = j;

126. /* Pass #2: traverse variant part and clear marks of non-redundant PHIs. */

127. if (passx) {

128. SnapNo s;

129. for (i = J->cur.nins-1; i > invar; i--) {

130. IRIns *ir = IR(i);

131. if (!irref_isk(ir->op2)) irt_clearmark(IR(ir->op2)->t);

132. if (!irref_isk(ir->op1)) {

133. irt_clearmark(IR(ir->op1)->t);

134. if (ir->op1 < invar &&

135. ir->o >= IR_CALLN && ir->o <= IR_CARG) {  /* ORDER IR */

136. ir = IR(ir->op1);

137. while (ir->o == IR_CARG) {

138. if (!irref_isk(ir->op2)) irt_clearmark(IR(ir->op2)->t);

139. if (irref_isk(ir->op1)) break;

140. ir = IR(ir->op1);

141. irt_clearmark(ir->t);

142. }

143. }

144. }

145. }

146. for (s = J->cur.nsnap-1; s >= onsnap; s--) {

147. SnapShot *snap = &J->cur.snap[s];

148. SnapEntry *map = &J->cur.snapmap[snap->mapofs];

149. MSize n, nent = snap->nent;

150. for (n = 0; n < nent; n++) {

151. IRRef ref = snap_ref(map[n]);

152. if (!irref_isk(ref)) irt_clearmark(IR(ref)->t);

153. }

154. }

155. }

156. /* Pass #3: add PHIs for variant slots without a corresponding SLOAD. */

157. nslots = J->baseslot+J->maxslot;

158. for (i = 1; i < nslots; i++) {

159. IRRef ref = tref_ref(J->slot[i]);

160. while (!irref_isk(ref) && ref != subst[ref]) {

161. IRIns *ir = IR(ref);

162. irt_clearmark(ir->t);  /* Unmark potential uses, too. */

163. if (irt_isphi(ir->t) || irt_ispri(ir->t))

164. break;

165. irt_setphi(ir->t);

166. if (nphi >= LJ_MAX_PHI)

167. lj_trace_err(J, LJ_TRERR_PHIOV);

168. phi[nphi++] = (IRRef1)ref;

169. ref = subst[ref];

170. if (ref > invar)

171. break;

172. }

173. }

174. /* Pass #4: propagate non-redundant PHIs. */

175. while (passx) {

176. passx = 0;

177. for (i = 0; i < nphi; i++) {

178. IRRef lref = phi[i];

179. IRIns *ir = IR(lref);

180. if (!irt_ismarked(ir->t)) {  /* Propagate only from unmarked PHIs. */

181. IRIns *irr = IR(subst[lref]);

182. if (irt_ismarked(irr->t)) {  /* Right ref points to other PHI? */

183. irt_clearmark(irr->t);  /* Mark that PHI as non-redundant. */

184. passx = 1;  /* Retry. */

185. }

186. }

187. }

188. }

189. /* Pass #5: emit PHI instructions or eliminate PHIs. */

190. for (i = 0; i < nphi; i++) {

191. IRRef lref = phi[i];

192. IRIns *ir = IR(lref);

193. if (!irt_ismarked(ir->t)) {  /* Emit PHI if not marked. */

194. IRRef rref = subst[lref];

195. if (rref > invar)

196. irt_setphi(IR(rref)->t);

197. emitir_raw(IRT(IR_PHI, irt_type(ir->t)), lref, rref);

198. } else {  /* Otherwise eliminate PHI. */

199. irt_clearmark(ir->t);

200. irt_clearphi(ir->t);

201. }

202. }

203. }

205. /* -- Loop unrolling using copy-substitution ------------------------------ */

207. /* Copy-substitute snapshot. */

208. ‌static void loop_subst_snap(jit_State *J, SnapShot *osnap,

209. SnapEntry *loopmap, IRRef1 *subst)

210. {

211. SnapEntry *nmap, *omap = &J->cur.snapmap[osnap->mapofs];

212. SnapEntry *nextmap = &J->cur.snapmap[snap_nextofs(&J->cur, osnap)];

213. MSize nmapofs;

214. MSize on, ln, nn, onent = osnap->nent;

215. BCReg nslots = osnap->nslots;

216. SnapShot *snap = &J->cur.snap[J->cur.nsnap];

217. if (irt_isguard(J->guardemit)) {  /* Guard inbetween? */

218. nmapofs = J->cur.nsnapmap;

219. J->cur.nsnap++;  /* Add new snapshot. */

220. } else {  /* Otherwise overwrite previous snapshot. */

221. snap--;

222. nmapofs = snap->mapofs;

223. }

224. J->guardemit.irt = 0;

225. /* Setup new snapshot. */

226. snap->mapofs = (uint16_t)nmapofs;

227. snap->ref = (IRRef1)J->cur.nins;

228. snap->nslots = nslots;

229. snap->topslot = osnap->topslot;

230. snap->count = 0;

231. nmap = &J->cur.snapmap[nmapofs];

232. /* Substitute snapshot slots. */

233. on = ln = nn = 0;

234. while (on < onent) {

235. SnapEntry osn = omap[on], lsn = loopmap[ln];

236. if (snap_slot(lsn) < snap_slot(osn)) {  /* Copy slot from loop map. */

237. nmap[nn++] = lsn;

238. ln++;

239. } else {  /* Copy substituted slot from snapshot map. */

240. if (snap_slot(lsn) == snap_slot(osn)) ln++;  /* Shadowed loop slot. */

241. if (!irref_isk(snap_ref(osn)))

242. osn = snap_setref(osn, subst[snap_ref(osn)]);

243. nmap[nn++] = osn;

244. on++;

245. }

246. }

247. while (snap_slot(loopmap[ln]) < nslots)  /* Copy remaining loop slots. */

248. nmap[nn++] = loopmap[ln++];

249. snap->nent = (uint8_t)nn;

250. omap += onent;

251. nmap += nn;

252. while (omap < nextmap)  /* Copy PC + frame links. */

253. *nmap++ = *omap++;

254. J->cur.nsnapmap = (uint16_t)(nmap - J->cur.snapmap);

255. }

257. ‌typedef struct LoopState {

258. jit_State *J;

259. IRRef1 *subst;

260. MSize sizesubst;

261. ‌} LoopState;

263. /* Unroll loop. */

264. ‌static void loop_unroll(LoopState *lps)

265. {

266. jit_State *J = lps->J;

267. IRRef1 phi[LJ_MAX_PHI];

268. uint32_t nphi = 0;

269. IRRef1 *subst;

270. SnapNo onsnap;

271. SnapShot *osnap, *loopsnap;

272. SnapEntry *loopmap, *psentinel;

273. IRRef ins, invar;

275. /* Allocate substitution table.

276. ** Only non-constant refs in [REF_BIAS,invar) are valid indexes.

277. */

278. invar = J->cur.nins;

279. lps->sizesubst = invar - REF_BIAS;

280. lps->subst = lj_mem_newvec(J->L, lps->sizesubst, IRRef1);

281. subst = lps->subst - REF_BIAS;

282. subst[REF_BASE] = REF_BASE;

284. /* LOOP separates the pre-roll from the loop body. */

285. emitir_raw(IRTG(IR_LOOP, IRT_NIL), 0, 0);

287. /* Grow snapshot buffer and map for copy-substituted snapshots.

288. ** Need up to twice the number of snapshots minus #0 and loop snapshot.

289. ** Need up to twice the number of entries plus fallback substitutions

290. ** from the loop snapshot entries for each new snapshot.

291. ** Caveat: both calls may reallocate J->cur.snap and J->cur.snapmap!

292. */

293. onsnap = J->cur.nsnap;

294. lj_snap_grow_buf(J, 2*onsnap-2);

295. lj_snap_grow_map(J, J->cur.nsnapmap*2+(onsnap-2)*J->cur.snap[onsnap-1].nent);

297. /* The loop snapshot is used for fallback substitutions. */

298. loopsnap = &J->cur.snap[onsnap-1];

299. loopmap = &J->cur.snapmap[loopsnap->mapofs];

300. /* The PC of snapshot #0 and the loop snapshot must match. */

301. psentinel = &loopmap[loopsnap->nent];

302. lua_assert(*psentinel == J->cur.snapmap[J->cur.snap[0].nent]);

303. *psentinel = SNAP(255, 0, 0);  /* Replace PC with temporary sentinel. */

305. /* Start substitution with snapshot #1 (#0 is empty for root traces). */

306. osnap = &J->cur.snap[1];

308. /* Copy and substitute all recorded instructions and snapshots. */

309. for (ins = REF_FIRST; ins < invar; ins++) {

310. IRIns *ir;

311. IRRef op1, op2;

313. if (ins >= osnap->ref)  /* Instruction belongs to next snapshot? */

314. loop_subst_snap(J, osnap++, loopmap, subst);  /* Copy-substitute it. */

316. /* Substitute instruction operands. */

317. ir = IR(ins);

318. op1 = ir->op1;

319. if (!irref_isk(op1)) op1 = subst[op1];

320. op2 = ir->op2;

321. if (!irref_isk(op2)) op2 = subst[op2];

322. if (irm_kind(lj_ir_mode[ir->o]) == IRM_N &&

323. op1 == ir->op1 && op2 == ir->op2) {  /* Regular invariant ins? */

324. subst[ins] = (IRRef1)ins;  /* Shortcut. */

325. } else {

326. /* Re-emit substituted instruction to the FOLD/CSE/etc. pipeline. */

327. IRType1 t = ir->t;  /* Get this first, since emitir may invalidate ir. */

328. IRRef ref = tref_ref(emitir(ir->ot & ~IRT_ISPHI, op1, op2));

329. subst[ins] = (IRRef1)ref;

330. if (ref != ins) {

331. IRIns *irr = IR(ref);

332. if (ref < invar) {  /* Loop-carried dependency? */

333. /* Potential PHI? */

334. if (!irref_isk(ref) && !irt_isphi(irr->t) && !irt_ispri(irr->t)) {

335. irt_setphi(irr->t);

336. if (nphi >= LJ_MAX_PHI)

337. lj_trace_err(J, LJ_TRERR_PHIOV);

338. phi[nphi++] = (IRRef1)ref;

339. }

340. /* Check all loop-carried dependencies for type instability. */

341. if (!irt_sametype(t, irr->t)) {

342. if (irt_isinteger(t) && irt_isinteger(irr->t))

343. continue;

344. else if (irt_isnum(t) && irt_isinteger(irr->t))  /* Fix int->num. */

345. ref = tref_ref(emitir(IRTN(IR_CONV), ref, IRCONV_NUM_INT));

346. else if (irt_isnum(irr->t) && irt_isinteger(t))  /* Fix num->int. */

347. ref = tref_ref(emitir(IRTGI(IR_CONV), ref,

348. IRCONV_INT_NUM|IRCONV_CHECK));

349. else

350. lj_trace_err(J, LJ_TRERR_TYPEINS);

351. subst[ins] = (IRRef1)ref;

352. irr = IR(ref);

353. goto phiconv;

354. }

355. } else if (ref != REF_DROP && irr->o == IR_CONV &&

356. ref > invar && irr->op1 < invar) {

357. /* May need an extra PHI for a CONV. */

358. ref = irr->op1;

359. irr = IR(ref);

360. phiconv:

361. if (ref < invar && !irref_isk(ref) && !irt_isphi(irr->t)) {

362. irt_setphi(irr->t);

363. if (nphi >= LJ_MAX_PHI)

364. lj_trace_err(J, LJ_TRERR_PHIOV);

365. phi[nphi++] = (IRRef1)ref;

366. }

367. }

368. }

369. }

370. }

371. if (!irt_isguard(J->guardemit))  /* Drop redundant snapshot. */

372. J->cur.nsnapmap = (uint16_t)J->cur.snap[--J->cur.nsnap].mapofs;

373. lua_assert(J->cur.nsnapmap <= J->sizesnapmap);

374. *psentinel = J->cur.snapmap[J->cur.snap[0].nent];  /* Restore PC. */

376. loop_emit_phi(J, subst, phi, nphi, onsnap);

377. }

379. /* Undo any partial changes made by the loop optimization. */

380. ‌static void loop_undo(jit_State *J, IRRef ins, SnapNo nsnap, MSize nsnapmap)

381. {

382. ptrdiff_t i;

383. SnapShot *snap = &J->cur.snap[nsnap-1];

384. SnapEntry *map = J->cur.snapmap;

385. map[snap->mapofs + snap->nent] = map[J->cur.snap[0].nent];  /* Restore PC. */

386. J->cur.nsnapmap = (uint16_t)nsnapmap;

387. J->cur.nsnap = nsnap;

388. J->guardemit.irt = 0;

389. lj_ir_rollback(J, ins);

390. for (i = 0; i < BPROP_SLOTS; i++) {  /* Remove backprop. cache entries. */

391. BPropEntry *bp = &J->bpropcache[i];

392. if (bp->val >= ins)

393. bp->key = 0;

394. }

395. for (ins--; ins >= REF_FIRST; ins--) {  /* Remove flags. */

396. IRIns *ir = IR(ins);

397. irt_clearphi(ir->t);

398. irt_clearmark(ir->t);

399. }

400. }

402. /* Protected callback for loop optimization. */

403. ‌static TValue *cploop_opt(lua_State *L, lua_CFunction dummy, void *ud)

404. {

405. UNUSED(L); UNUSED(dummy);

406. loop_unroll((LoopState *)ud);

407. return NULL;

408. }

410. /* Loop optimization. */

411. ‌int lj_opt_loop(jit_State *J)

412. {

413. IRRef nins = J->cur.nins;

414. SnapNo nsnap = J->cur.nsnap;

415. MSize nsnapmap = J->cur.nsnapmap;

416. LoopState lps;

417. int errcode;

418. lps.J = J;

419. lps.subst = NULL;

420. lps.sizesubst = 0;

421. errcode = lj_vm_cpcall(J->L, NULL, &lps, cploop_opt);

422. lj_mem_freevec(J2G(J), lps.subst, lps.sizesubst, IRRef1);

423. if (LJ_UNLIKELY(errcode)) {

424. lua_State *L = J->L;

425. if (errcode == LUA_ERRRUN && tvisnumber(L->top-1)) {  /* Trace error? */

426. int32_t e = numberVint(L->top-1);

427. switch ((TraceError)e) {

428. case LJ_TRERR_TYPEINS:  /* Type instability. */

429. case LJ_TRERR_GFAIL:  /* Guard would always fail. */

430. /* Unrolling via recording fixes many cases, e.g. a flipped boolean. */

431. if (--J->instunroll < 0)  /* But do not unroll forever. */

432. break;

433. L->top--;  /* Remove error object. */

434. loop_undo(J, nins, nsnap, nsnapmap);

435. return 1;  /* Loop optimization failed, continue recording. */

436. default:

437. break;

438. }

439. }

440. lj_err_throw(L, errcode);  /* Propagate all other errors. */

441. }

442. return 0;  /* Loop optimization is ok. */

443. }

445. ‌#undef IR

446. ‌#undef emitir

447. ‌#undef emitir_raw

449. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_snap.h - luajit-2.0-src

Functions defined

- lj_snap_grow_buf

- lj_snap_grow_map

Macros defined

- _LJ_SNAP_H

Source code

1. /*

2. ** Snapshot handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_SNAP_H

7. ‌#define _LJ_SNAP_H

9. #include "lj_obj.h"

10. #include "lj_jit.h"

12. #if LJ_HASJIT

13. LJ_FUNC void lj_snap_add(jit_State *J);

14. LJ_FUNC void lj_snap_purge(jit_State *J);

15. LJ_FUNC void lj_snap_shrink(jit_State *J);

16. LJ_FUNC IRIns *lj_snap_regspmap(GCtrace *T, SnapNo snapno, IRIns *ir);

17. LJ_FUNC void lj_snap_replay(jit_State *J, GCtrace *T);

18. LJ_FUNC const BCIns *lj_snap_restore(jit_State *J, void *exptr);

19. LJ_FUNC void lj_snap_grow_buf_(jit_State *J, MSize need);

20. LJ_FUNC void lj_snap_grow_map_(jit_State *J, MSize need);

22. ‌static LJ_AINLINE void lj_snap_grow_buf(jit_State *J, MSize need)

23. {

24. if (LJ_UNLIKELY(need > J->sizesnap)) lj_snap_grow_buf_(J, need);

25. }

27. ‌static LJ_AINLINE void lj_snap_grow_map(jit_State *J, MSize need)

28. {

29. if (LJ_UNLIKELY(need > J->sizesnapmap)) lj_snap_grow_map_(J, need);

30. }

32. #endif

34. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_snap.c - luajit-2.0-src

Functions defined

- lj_snap_add

- lj_snap_grow_buf_

- lj_snap_grow_map_

- lj_snap_purge

- lj_snap_regspmap

- lj_snap_replay

- lj_snap_restore

- lj_snap_shrink

- snap_dedup

- snap_pref

- snap_renamefilter

- snap_renameref

- snap_replay_const

- snap_restoredata

- snap_restoreval

- snap_sunk_store

- snap_sunk_store2

- snap_unsink

- snap_usedef

- snapshot_framelinks

- snapshot_slots

- snapshot_stack

Macros defined

- DEF_SLOT

- DEF_SLOT

- IR

- IR

- LUA_CORE

- SNAP_USEDEF_SLOTS

- USE_SLOT

- USE_SLOT

- emitir

- emitir

- emitir_raw

- emitir_raw

- lj_snap_c

Source code

1. /*

2. ** Snapshot handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_snap_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT

13. #include "lj_gc.h"

14. #include "lj_tab.h"

15. #include "lj_state.h"

16. #include "lj_frame.h"

17. #include "lj_bc.h"

18. #include "lj_ir.h"

19. #include "lj_jit.h"

20. #include "lj_iropt.h"

21. #include "lj_trace.h"

22. #include "lj_snap.h"

23. #include "lj_target.h"

24. #if LJ_HASFFI

25. #include "lj_ctype.h"

26. #include "lj_cdata.h"

27. #endif

29. /* Some local macros to save typing. Undef'd at the end. */

30. ‌#define IR(ref)                (&J->cur.ir[(ref)])

32. /* Pass IR on to next optimization in chain (FOLD). */

33. ‌#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))

35. /* Emit raw IR without passing through optimizations. */

36. ‌#define emitir_raw(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J))

38. /* -- Snapshot buffer allocation ------------------------------------------ */

40. /* Grow snapshot buffer. */

41. ‌void lj_snap_grow_buf_(jit_State *J, MSize need)

42. {

43. MSize maxsnap = (MSize)J->param[JIT_P_maxsnap];

44. if (need > maxsnap)

45. lj_trace_err(J, LJ_TRERR_SNAPOV);

46. lj_mem_growvec(J->L, J->snapbuf, J->sizesnap, maxsnap, SnapShot);

47. J->cur.snap = J->snapbuf;

48. }

50. /* Grow snapshot map buffer. */

51. ‌void lj_snap_grow_map_(jit_State *J, MSize need)

52. {

53. if (need < 2*J->sizesnapmap)

54. need = 2*J->sizesnapmap;

55. else if (need < 64)

56. need = 64;

57. J->snapmapbuf = (SnapEntry *)lj_mem_realloc(J->L, J->snapmapbuf,

58. J->sizesnapmap*sizeof(SnapEntry), need*sizeof(SnapEntry));

59. J->cur.snapmap = J->snapmapbuf;

60. J->sizesnapmap = need;

61. }

63. /* -- Snapshot generation ------------------------------------------------- */

65. /* Add all modified slots to the snapshot. */

66. ‌static MSize snapshot_slots(jit_State *J, SnapEntry *map, BCReg nslots)

67. {

68. IRRef retf = J->chain[IR_RETF];  /* Limits SLOAD restore elimination. */

69. BCReg s;

70. MSize n = 0;

71. for (s = 0; s < nslots; s++) {

72. TRef tr = J->slot[s];

73. IRRef ref = tref_ref(tr);

74. if (ref) {

75. SnapEntry sn = SNAP_TR(s, tr);

76. IRIns *ir = IR(ref);

77. if (!(sn & (SNAP_CONT|SNAP_FRAME)) &&

78. ir->o == IR_SLOAD && ir->op1 == s && ref > retf) {

79. /* No need to snapshot unmodified non-inherited slots. */

80. if (!(ir->op2 & IRSLOAD_INHERIT))

81. continue;

82. /* No need to restore readonly slots and unmodified non-parent slots. */

83. if (!(LJ_DUALNUM && (ir->op2 & IRSLOAD_CONVERT)) &&

84. (ir->op2 & (IRSLOAD_READONLY|IRSLOAD_PARENT)) != IRSLOAD_PARENT)

85. sn |= SNAP_NORESTORE;

86. }

87. if (LJ_SOFTFP && irt_isnum(ir->t))

88. sn |= SNAP_SOFTFPNUM;

89. map[n++] = sn;

90. }

91. }

92. return n;

93. }

95. /* Add frame links at the end of the snapshot. */

96. ‌static BCReg snapshot_framelinks(jit_State *J, SnapEntry *map)

97. {

98. cTValue *frame = J->L->base - 1;

99. cTValue *lim = J->L->base - J->baseslot;

100. GCfunc *fn = frame_func(frame);

101. cTValue *ftop = isluafunc(fn) ? (frame+funcproto(fn)->framesize) : J->L->top;

102. MSize f = 0;

103. lua_assert(!LJ_FR2);  /* TODO_FR2: store 64 bit PCs. */

104. map[f++] = SNAP_MKPC(J->pc);  /* The current PC is always the first entry. */

105. while (frame > lim) {  /* Backwards traversal of all frames above base. */

106. if (frame_islua(frame)) {

107. map[f++] = SNAP_MKPC(frame_pc(frame));

108. frame = frame_prevl(frame);

109. } else if (frame_iscont(frame)) {

110. map[f++] = SNAP_MKFTSZ(frame_ftsz(frame));

111. map[f++] = SNAP_MKPC(frame_contpc(frame));

112. frame = frame_prevd(frame);

113. } else {

114. lua_assert(!frame_isc(frame));

115. map[f++] = SNAP_MKFTSZ(frame_ftsz(frame));

116. frame = frame_prevd(frame);

117. continue;

118. }

119. if (frame + funcproto(frame_func(frame))->framesize > ftop)

120. ftop = frame + funcproto(frame_func(frame))->framesize;

121. }

122. lua_assert(f == (MSize)(1 + J->framedepth));

123. return (BCReg)(ftop - lim);

124. }

126. /* Take a snapshot of the current stack. */

127. ‌static void snapshot_stack(jit_State *J, SnapShot *snap, MSize nsnapmap)

128. {

129. BCReg nslots = J->baseslot + J->maxslot;

130. MSize nent;

131. SnapEntry *p;

132. /* Conservative estimate. */

133. lj_snap_grow_map(J, nsnapmap + nslots + (MSize)J->framedepth+1);

134. p = &J->cur.snapmap[nsnapmap];

135. nent = snapshot_slots(J, p, nslots);

136. snap->topslot = (uint8_t)snapshot_framelinks(J, p + nent);

137. snap->mapofs = (uint16_t)nsnapmap;

138. snap->ref = (IRRef1)J->cur.nins;

139. snap->nent = (uint8_t)nent;

140. snap->nslots = (uint8_t)nslots;

141. snap->count = 0;

142. J->cur.nsnapmap = (uint16_t)(nsnapmap + nent + 1 + J->framedepth);

143. }

145. /* Add or merge a snapshot. */

146. ‌void lj_snap_add(jit_State *J)

147. {

148. MSize nsnap = J->cur.nsnap;

149. MSize nsnapmap = J->cur.nsnapmap;

150. /* Merge if no ins. inbetween or if requested and no guard inbetween. */

151. if (J->mergesnap ? !irt_isguard(J->guardemit) :

152. (nsnap > 0 && J->cur.snap[nsnap-1].ref == J->cur.nins)) {

153. if (nsnap == 1) {  /* But preserve snap #0 PC. */

154. emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0);

155. goto nomerge;

156. }

157. nsnapmap = J->cur.snap[--nsnap].mapofs;

158. } else {

159. nomerge:

160. lj_snap_grow_buf(J, nsnap+1);

161. J->cur.nsnap = (uint16_t)(nsnap+1);

162. }

163. J->mergesnap = 0;

164. J->guardemit.irt = 0;

165. snapshot_stack(J, &J->cur.snap[nsnap], nsnapmap);

166. }

168. /* -- Snapshot modification ----------------------------------------------- */

170. ‌#define SNAP_USEDEF_SLOTS        (LJ_MAX_JSLOTS+LJ_STACK_EXTRA)

172. /* Find unused slots with reaching-definitions bytecode data-flow analysis. */

173. ‌static BCReg snap_usedef(jit_State *J, uint8_t *udf,

174. const BCIns *pc, BCReg maxslot)

175. {

176. BCReg s;

177. GCobj *o;

179. if (maxslot == 0) return 0;

180. #ifdef LUAJIT_USE_VALGRIND

181. /* Avoid errors for harmless reads beyond maxslot. */

182. memset(udf, 1, SNAP_USEDEF_SLOTS);

183. #else

184. memset(udf, 1, maxslot);

185. #endif

187. /* Treat open upvalues as used. */

188. o = gcref(J->L->openupval);

189. while (o) {

190. if (uvval(gco2uv(o)) < J->L->base) break;

191. udf[uvval(gco2uv(o)) - J->L->base] = 0;

192. o = gcref(o->gch.nextgc);

193. }

195. ‌#define USE_SLOT(s)                udf[(s)] &= ~1

196. ‌#define DEF_SLOT(s)                udf[(s)] *= 3

198. /* Scan through following bytecode and check for uses/defs. */

199. lua_assert(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc);

200. for (;;) {

201. BCIns ins = *pc++;

202. BCOp op = bc_op(ins);

203. switch (bcmode_b(op)) {

204. case BCMvar: USE_SLOT(bc_b(ins)); break;

205. default: break;

206. }

207. switch (bcmode_c(op)) {

208. case BCMvar: USE_SLOT(bc_c(ins)); break;

209. case BCMrbase:

210. lua_assert(op == BC_CAT);

211. for (s = bc_b(ins); s <= bc_c(ins); s++) USE_SLOT(s);

212. for (; s < maxslot; s++) DEF_SLOT(s);

213. break;

214. case BCMjump:

215. handle_jump: {

216. BCReg minslot = bc_a(ins);

217. if (op >= BC_FORI && op <= BC_JFORL) minslot += FORL_EXT;

218. else if (op >= BC_ITERL && op <= BC_JITERL) minslot += bc_b(pc[-2])-1;

219. else if (op == BC_UCLO) { pc += bc_j(ins); break; }

220. for (s = minslot; s < maxslot; s++) DEF_SLOT(s);

221. return minslot < maxslot ? minslot : maxslot;

222. }

223. case BCMlit:

224. if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {

225. goto handle_jump;

226. } else if (bc_isret(op)) {

227. BCReg top = op == BC_RETM ? maxslot : (bc_a(ins) + bc_d(ins)-1);

228. for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s);

229. for (; s < top; s++) USE_SLOT(s);

230. for (; s < maxslot; s++) DEF_SLOT(s);

231. return 0;

232. }

233. break;

234. case BCMfunc: return maxslot;  /* NYI: will abort, anyway. */

235. default: break;

236. }

237. switch (bcmode_a(op)) {

238. case BCMvar: USE_SLOT(bc_a(ins)); break;

239. case BCMdst:

240. if (!(op == BC_ISTC || op == BC_ISFC)) DEF_SLOT(bc_a(ins));

241. break;

242. case BCMbase:

243. if (op >= BC_CALLM && op <= BC_VARG) {

244. BCReg top = (op == BC_CALLM || op == BC_CALLMT || bc_c(ins) == 0) ?

245. maxslot : (bc_a(ins) + bc_c(ins)+LJ_FR2);

246. if (LJ_FR2) DEF_SLOT(bc_a(ins)+1);

247. s = bc_a(ins) - ((op == BC_ITERC || op == BC_ITERN) ? 3 : 0);

248. for (; s < top; s++) USE_SLOT(s);

249. for (; s < maxslot; s++) DEF_SLOT(s);

250. if (op == BC_CALLT || op == BC_CALLMT) {

251. for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s);

252. return 0;

253. }

254. } else if (op == BC_KNIL) {

255. for (s = bc_a(ins); s <= bc_d(ins); s++) DEF_SLOT(s);

256. } else if (op == BC_TSETM) {

257. for (s = bc_a(ins)-1; s < maxslot; s++) USE_SLOT(s);

258. }

259. break;

260. default: break;

261. }

262. lua_assert(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc);

263. }

265. ‌#undef USE_SLOT

266. ‌#undef DEF_SLOT

268. return 0;  /* unreachable */

269. }

271. /* Purge dead slots before the next snapshot. */

272. ‌void lj_snap_purge(jit_State *J)

273. {

274. uint8_t udf[SNAP_USEDEF_SLOTS];

275. BCReg maxslot = J->maxslot;

276. BCReg s = snap_usedef(J, udf, J->pc, maxslot);

277. for (; s < maxslot; s++)

278. if (udf[s] != 0)

279. J->base[s] = 0;  /* Purge dead slots. */

280. }

282. /* Shrink last snapshot. */

283. ‌void lj_snap_shrink(jit_State *J)

284. {

285. SnapShot *snap = &J->cur.snap[J->cur.nsnap-1];

286. SnapEntry *map = &J->cur.snapmap[snap->mapofs];

287. MSize n, m, nlim, nent = snap->nent;

288. uint8_t udf[SNAP_USEDEF_SLOTS];

289. BCReg maxslot = J->maxslot;

290. BCReg minslot = snap_usedef(J, udf, snap_pc(map[nent]), maxslot);

291. BCReg baseslot = J->baseslot;

292. maxslot += baseslot;

293. minslot += baseslot;

294. snap->nslots = (uint8_t)maxslot;

295. for (n = m = 0; n < nent; n++) {  /* Remove unused slots from snapshot. */

296. BCReg s = snap_slot(map[n]);

297. if (s < minslot || (s < maxslot && udf[s-baseslot] == 0))

298. map[m++] = map[n];  /* Only copy used slots. */

299. }

300. snap->nent = (uint8_t)m;

301. nlim = J->cur.nsnapmap - snap->mapofs - 1;

302. while (n <= nlim) map[m++] = map[n++];  /* Move PC + frame links down. */

303. J->cur.nsnapmap = (uint16_t)(snap->mapofs + m);  /* Free up space in map. */

304. }

306. /* -- Snapshot access ----------------------------------------------------- */

308. /* Initialize a Bloom Filter with all renamed refs.

309. ** There are very few renames (often none), so the filter has

310. ** very few bits set. This makes it suitable for negative filtering.

311. */

312. ‌static BloomFilter snap_renamefilter(GCtrace *T, SnapNo lim)

313. {

314. BloomFilter rfilt = 0;

315. IRIns *ir;

316. for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--)

317. if (ir->op2 <= lim)

318. bloomset(rfilt, ir->op1);

319. return rfilt;

320. }

322. /* Process matching renames to find the original RegSP. */

323. ‌static RegSP snap_renameref(GCtrace *T, SnapNo lim, IRRef ref, RegSP rs)

324. {

325. IRIns *ir;

326. for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--)

327. if (ir->op1 == ref && ir->op2 <= lim)

328. rs = ir->prev;

329. return rs;

330. }

332. /* Copy RegSP from parent snapshot to the parent links of the IR. */

333. ‌IRIns *lj_snap_regspmap(GCtrace *T, SnapNo snapno, IRIns *ir)

334. {

335. SnapShot *snap = &T->snap[snapno];

336. SnapEntry *map = &T->snapmap[snap->mapofs];

337. BloomFilter rfilt = snap_renamefilter(T, snapno);

338. MSize n = 0;

339. IRRef ref = 0;

340. for ( ; ; ir++) {

341. uint32_t rs;

342. if (ir->o == IR_SLOAD) {

343. if (!(ir->op2 & IRSLOAD_PARENT)) break;

344. for ( ; ; n++) {

345. lua_assert(n < snap->nent);

346. if (snap_slot(map[n]) == ir->op1) {

347. ref = snap_ref(map[n++]);

348. break;

349. }

350. }

351. } else if (LJ_SOFTFP && ir->o == IR_HIOP) {

352. ref++;

353. } else if (ir->o == IR_PVAL) {

354. ref = ir->op1 + REF_BIAS;

355. } else {

356. break;

357. }

358. rs = T->ir[ref].prev;

359. if (bloomtest(rfilt, ref))

360. rs = snap_renameref(T, snapno, ref, rs);

361. ir->prev = (uint16_t)rs;

362. lua_assert(regsp_used(rs));

363. }

364. return ir;

365. }

367. /* -- Snapshot replay ----------------------------------------------------- */

369. /* Replay constant from parent trace. */

370. ‌static TRef snap_replay_const(jit_State *J, IRIns *ir)

371. {

372. /* Only have to deal with constants that can occur in stack slots. */

373. switch ((IROp)ir->o) {

374. case IR_KPRI: return TREF_PRI(irt_type(ir->t));

375. case IR_KINT: return lj_ir_kint(J, ir->i);

376. case IR_KGC: return lj_ir_kgc(J, ir_kgc(ir), irt_t(ir->t));

377. case IR_KNUM: return lj_ir_k64(J, IR_KNUM, ir_knum(ir));

378. case IR_KINT64: return lj_ir_k64(J, IR_KINT64, ir_kint64(ir));

379. case IR_KPTR: return lj_ir_kptr(J, ir_kptr(ir));  /* Continuation. */

380. default: lua_assert(0); return TREF_NIL; break;

381. }

382. }

384. /* De-duplicate parent reference. */

385. ‌static TRef snap_dedup(jit_State *J, SnapEntry *map, MSize nmax, IRRef ref)

386. {

387. MSize j;

388. for (j = 0; j < nmax; j++)

389. if (snap_ref(map[j]) == ref)

390. return J->slot[snap_slot(map[j])] & ~(SNAP_CONT|SNAP_FRAME);

391. return 0;

392. }

394. /* Emit parent reference with de-duplication. */

395. ‌static TRef snap_pref(jit_State *J, GCtrace *T, SnapEntry *map, MSize nmax,

396. BloomFilter seen, IRRef ref)

397. {

398. IRIns *ir = &T->ir[ref];

399. TRef tr;

400. if (irref_isk(ref))

401. tr = snap_replay_const(J, ir);

402. else if (!regsp_used(ir->prev))

403. tr = 0;

404. else if (!bloomtest(seen, ref) || (tr = snap_dedup(J, map, nmax, ref)) == 0)

405. tr = emitir(IRT(IR_PVAL, irt_type(ir->t)), ref - REF_BIAS, 0);

406. return tr;

407. }

409. /* Check whether a sunk store corresponds to an allocation. Slow path. */

410. ‌static int snap_sunk_store2(jit_State *J, IRIns *ira, IRIns *irs)

411. {

412. if (irs->o == IR_ASTORE || irs->o == IR_HSTORE ||

413. irs->o == IR_FSTORE || irs->o == IR_XSTORE) {

414. IRIns *irk = IR(irs->op1);

415. if (irk->o == IR_AREF || irk->o == IR_HREFK)

416. irk = IR(irk->op1);

417. return (IR(irk->op1) == ira);

418. }

419. return 0;

420. }

422. /* Check whether a sunk store corresponds to an allocation. Fast path. */

423. ‌static LJ_AINLINE int snap_sunk_store(jit_State *J, IRIns *ira, IRIns *irs)

424. {

425. if (irs->s != 255)

426. return (ira + irs->s == irs);  /* Fast check. */

427. return snap_sunk_store2(J, ira, irs);

428. }

430. /* Replay snapshot state to setup side trace. */

431. ‌void lj_snap_replay(jit_State *J, GCtrace *T)

432. {

433. SnapShot *snap = &T->snap[J->exitno];

434. SnapEntry *map = &T->snapmap[snap->mapofs];

435. MSize n, nent = snap->nent;

436. BloomFilter seen = 0;

437. int pass23 = 0;

438. J->framedepth = 0;

439. /* Emit IR for slots inherited from parent snapshot. */

440. for (n = 0; n < nent; n++) {

441. SnapEntry sn = map[n];

442. BCReg s = snap_slot(sn);

443. IRRef ref = snap_ref(sn);

444. IRIns *ir = &T->ir[ref];

445. TRef tr;

446. /* The bloom filter avoids O(nent^2) overhead for de-duping slots. */

447. if (bloomtest(seen, ref) && (tr = snap_dedup(J, map, n, ref)) != 0)

448. goto setslot;

449. bloomset(seen, ref);

450. if (irref_isk(ref)) {

451. tr = snap_replay_const(J, ir);

452. } else if (!regsp_used(ir->prev)) {

453. pass23 = 1;

454. lua_assert(s != 0);

455. tr = s;

456. } else {

457. IRType t = irt_type(ir->t);

458. uint32_t mode = IRSLOAD_INHERIT|IRSLOAD_PARENT;

459. if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) t = IRT_NUM;

460. if (ir->o == IR_SLOAD) mode |= (ir->op2 & IRSLOAD_READONLY);

461. tr = emitir_raw(IRT(IR_SLOAD, t), s, mode);

462. }

463. setslot:

464. J->slot[s] = tr | (sn&(SNAP_CONT|SNAP_FRAME));  /* Same as TREF_* flags. */

465. J->framedepth += ((sn & (SNAP_CONT|SNAP_FRAME)) && s);

466. if ((sn & SNAP_FRAME))

467. J->baseslot = s+1;

468. }

469. if (pass23) {

470. IRIns *irlast = &T->ir[snap->ref];

471. pass23 = 0;

472. /* Emit dependent PVALs. */

473. for (n = 0; n < nent; n++) {

474. SnapEntry sn = map[n];

475. IRRef refp = snap_ref(sn);

476. IRIns *ir = &T->ir[refp];

477. if (regsp_reg(ir->r) == RID_SUNK) {

478. if (J->slot[snap_slot(sn)] != snap_slot(sn)) continue;

479. pass23 = 1;

480. lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP ||

481. ir->o == IR_CNEW || ir->o == IR_CNEWI);

482. if (ir->op1 >= T->nk) snap_pref(J, T, map, nent, seen, ir->op1);

483. if (ir->op2 >= T->nk) snap_pref(J, T, map, nent, seen, ir->op2);

484. if (LJ_HASFFI && ir->o == IR_CNEWI) {

485. if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP)

486. snap_pref(J, T, map, nent, seen, (ir+1)->op2);

487. } else {

488. IRIns *irs;

489. for (irs = ir+1; irs < irlast; irs++)

490. if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) {

491. if (snap_pref(J, T, map, nent, seen, irs->op2) == 0)

492. snap_pref(J, T, map, nent, seen, T->ir[irs->op2].op1);

493. else if ((LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) &&

494. irs+1 < irlast && (irs+1)->o == IR_HIOP)

495. snap_pref(J, T, map, nent, seen, (irs+1)->op2);

496. }

497. }

498. } else if (!irref_isk(refp) && !regsp_used(ir->prev)) {

499. lua_assert(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT);

500. J->slot[snap_slot(sn)] = snap_pref(J, T, map, nent, seen, ir->op1);

501. }

502. }

503. /* Replay sunk instructions. */

504. for (n = 0; pass23 && n < nent; n++) {

505. SnapEntry sn = map[n];

506. IRRef refp = snap_ref(sn);

507. IRIns *ir = &T->ir[refp];

508. if (regsp_reg(ir->r) == RID_SUNK) {

509. TRef op1, op2;

510. if (J->slot[snap_slot(sn)] != snap_slot(sn)) {  /* De-dup allocs. */

511. J->slot[snap_slot(sn)] = J->slot[J->slot[snap_slot(sn)]];

512. continue;

513. }

514. op1 = ir->op1;

515. if (op1 >= T->nk) op1 = snap_pref(J, T, map, nent, seen, op1);

516. op2 = ir->op2;

517. if (op2 >= T->nk) op2 = snap_pref(J, T, map, nent, seen, op2);

518. if (LJ_HASFFI && ir->o == IR_CNEWI) {

519. if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP) {

520. lj_needsplit(J);  /* Emit joining HIOP. */

521. op2 = emitir_raw(IRT(IR_HIOP, IRT_I64), op2,

522. snap_pref(J, T, map, nent, seen, (ir+1)->op2));

523. }

524. J->slot[snap_slot(sn)] = emitir(ir->ot, op1, op2);

525. } else {

526. IRIns *irs;

527. TRef tr = emitir(ir->ot, op1, op2);

528. J->slot[snap_slot(sn)] = tr;

529. for (irs = ir+1; irs < irlast; irs++)

530. if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) {

531. IRIns *irr = &T->ir[irs->op1];

532. TRef val, key = irr->op2, tmp = tr;

533. if (irr->o != IR_FREF) {

534. IRIns *irk = &T->ir[key];

535. if (irr->o == IR_HREFK)

536. key = lj_ir_kslot(J, snap_replay_const(J, &T->ir[irk->op1]),

537. irk->op2);

538. else

539. key = snap_replay_const(J, irk);

540. if (irr->o == IR_HREFK || irr->o == IR_AREF) {

541. IRIns *irf = &T->ir[irr->op1];

542. tmp = emitir(irf->ot, tmp, irf->op2);

543. }

544. }

545. tmp = emitir(irr->ot, tmp, key);

546. val = snap_pref(J, T, map, nent, seen, irs->op2);

547. if (val == 0) {

548. IRIns *irc = &T->ir[irs->op2];

549. lua_assert(irc->o == IR_CONV && irc->op2 == IRCONV_NUM_INT);

550. val = snap_pref(J, T, map, nent, seen, irc->op1);

551. val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT);

552. } else if ((LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) &&

553. irs+1 < irlast && (irs+1)->o == IR_HIOP) {

554. IRType t = IRT_I64;

555. if (LJ_SOFTFP && irt_type((irs+1)->t) == IRT_SOFTFP)

556. t = IRT_NUM;

557. lj_needsplit(J);

558. if (irref_isk(irs->op2) && irref_isk((irs+1)->op2)) {

559. uint64_t k = (uint32_t)T->ir[irs->op2].i +

560. ((uint64_t)T->ir[(irs+1)->op2].i << 32);

561. val = lj_ir_k64(J, t == IRT_I64 ? IR_KINT64 : IR_KNUM,

562. lj_ir_k64_find(J, k));

563. } else {

564. val = emitir_raw(IRT(IR_HIOP, t), val,

565. snap_pref(J, T, map, nent, seen, (irs+1)->op2));

566. }

567. tmp = emitir(IRT(irs->o, t), tmp, val);

568. continue;

569. }

570. tmp = emitir(irs->ot, tmp, val);

571. } else if (LJ_HASFFI && irs->o == IR_XBAR && ir->o == IR_CNEW) {

572. emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);

573. }

574. }

575. }

576. }

577. }

578. J->base = J->slot + J->baseslot;

579. J->maxslot = snap->nslots - J->baseslot;

580. lj_snap_add(J);

581. if (pass23)  /* Need explicit GC step _after_ initial snapshot. */

582. emitir_raw(IRTG(IR_GCSTEP, IRT_NIL), 0, 0);

583. }

585. /* -- Snapshot restore ---------------------------------------------------- */

587. static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex,

588. SnapNo snapno, BloomFilter rfilt,

589. IRIns *ir, TValue *o);

591. /* Restore a value from the trace exit state. */

592. ‌static void snap_restoreval(jit_State *J, GCtrace *T, ExitState *ex,

593. SnapNo snapno, BloomFilter rfilt,

594. IRRef ref, TValue *o)

595. {

596. IRIns *ir = &T->ir[ref];

597. IRType1 t = ir->t;

598. RegSP rs = ir->prev;

599. if (irref_isk(ref)) {  /* Restore constant slot. */

600. lj_ir_kvalue(J->L, o, ir);

601. return;

602. }

603. if (LJ_UNLIKELY(bloomtest(rfilt, ref)))

604. rs = snap_renameref(T, snapno, ref, rs);

605. lua_assert(!LJ_GC64);  /* TODO_GC64: handle 64 bit references. */

606. if (ra_hasspill(regsp_spill(rs))) {  /* Restore from spill slot. */

607. int32_t *sps = &ex->spill[regsp_spill(rs)];

608. if (irt_isinteger(t)) {

609. setintV(o, *sps);

610. #if !LJ_SOFTFP

611. } else if (irt_isnum(t)) {

612. o->u64 = *(uint64_t *)sps;

613. #endif

614. } else if (LJ_64 && irt_islightud(t)) {

615. /* 64 bit lightuserdata which may escape already has the tag bits. */

616. o->u64 = *(uint64_t *)sps;

617. } else {

618. lua_assert(!irt_ispri(t));  /* PRI refs never have a spill slot. */

619. setgcV(J->L, o, (GCobj *)(uintptr_t)*(GCSize *)sps, irt_toitype(t));

620. }

621. } else {  /* Restore from register. */

622. Reg r = regsp_reg(rs);

623. if (ra_noreg(r)) {

624. lua_assert(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT);

625. snap_restoreval(J, T, ex, snapno, rfilt, ir->op1, o);

626. if (LJ_DUALNUM) setnumV(o, (lua_Number)intV(o));

627. return;

628. } else if (irt_isinteger(t)) {

629. setintV(o, (int32_t)ex->gpr[r-RID_MIN_GPR]);

630. #if !LJ_SOFTFP

631. } else if (irt_isnum(t)) {

632. setnumV(o, ex->fpr[r-RID_MIN_FPR]);

633. #endif

634. } else if (LJ_64 && irt_islightud(t)) {

635. /* 64 bit lightuserdata which may escape already has the tag bits. */

636. o->u64 = ex->gpr[r-RID_MIN_GPR];

637. } else if (irt_ispri(t)) {

638. setpriV(o, irt_toitype(t));

639. } else {

640. setgcV(J->L, o, (GCobj *)ex->gpr[r-RID_MIN_GPR], irt_toitype(t));

641. }

642. }

643. }

645. #if LJ_HASFFI

646. /* Restore raw data from the trace exit state. */

647. ‌static void snap_restoredata(GCtrace *T, ExitState *ex,

648. SnapNo snapno, BloomFilter rfilt,

649. IRRef ref, void *dst, CTSize sz)

650. {

651. IRIns *ir = &T->ir[ref];

652. RegSP rs = ir->prev;

653. int32_t *src;

654. uint64_t tmp;

655. if (irref_isk(ref)) {

656. if (ir->o == IR_KNUM || ir->o == IR_KINT64) {

657. src = mref(ir->ptr, int32_t);

658. } else if (sz == 8) {

659. tmp = (uint64_t)(uint32_t)ir->i;

660. src = (int32_t *)&tmp;

661. } else {

662. src = &ir->i;

663. }

664. } else {

665. if (LJ_UNLIKELY(bloomtest(rfilt, ref)))

666. rs = snap_renameref(T, snapno, ref, rs);

667. if (ra_hasspill(regsp_spill(rs))) {

668. src = &ex->spill[regsp_spill(rs)];

669. if (sz == 8 && !irt_is64(ir->t)) {

670. tmp = (uint64_t)(uint32_t)*src;

671. src = (int32_t *)&tmp;

672. }

673. } else {

674. Reg r = regsp_reg(rs);

675. if (ra_noreg(r)) {

676. /* Note: this assumes CNEWI is never used for SOFTFP split numbers. */

677. lua_assert(sz == 8 && ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT);

678. snap_restoredata(T, ex, snapno, rfilt, ir->op1, dst, 4);

679. *(lua_Number *)dst = (lua_Number)*(int32_t *)dst;

680. return;

681. }

682. src = (int32_t *)&ex->gpr[r-RID_MIN_GPR];

683. #if !LJ_SOFTFP

684. if (r >= RID_MAX_GPR) {

685. src = (int32_t *)&ex->fpr[r-RID_MIN_FPR];

686. #if LJ_TARGET_PPC

687. if (sz == 4) {  /* PPC FPRs are always doubles. */

688. *(float *)dst = (float)*(double *)src;

689. return;

690. }

691. #else

692. if (LJ_BE && sz == 4) src++;

693. #endif

694. }

695. #endif

696. }

697. }

698. lua_assert(sz == 1 || sz == 2 || sz == 4 || sz == 8);

699. if (sz == 4) *(int32_t *)dst = *src;

700. else if (sz == 8) *(int64_t *)dst = *(int64_t *)src;

701. else if (sz == 1) *(int8_t *)dst = (int8_t)*src;

702. else *(int16_t *)dst = (int16_t)*src;

703. }

704. #endif

706. /* Unsink allocation from the trace exit state. Unsink sunk stores. */

707. ‌static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex,

708. SnapNo snapno, BloomFilter rfilt,

709. IRIns *ir, TValue *o)

710. {

711. lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP ||

712. ir->o == IR_CNEW || ir->o == IR_CNEWI);

713. #if LJ_HASFFI

714. if (ir->o == IR_CNEW || ir->o == IR_CNEWI) {

715. CTState *cts = ctype_cts(J->L);

716. CTypeID id = (CTypeID)T->ir[ir->op1].i;

717. CTSize sz = lj_ctype_size(cts, id);

718. GCcdata *cd = lj_cdata_new(cts, id, sz);

719. setcdataV(J->L, o, cd);

720. if (ir->o == IR_CNEWI) {

721. uint8_t *p = (uint8_t *)cdataptr(cd);

722. lua_assert(sz == 4 || sz == 8);

723. if (LJ_32 && sz == 8 && ir+1 < T->ir + T->nins && (ir+1)->o == IR_HIOP) {

724. snap_restoredata(T, ex, snapno, rfilt, (ir+1)->op2, LJ_LE?p+4:p, 4);

725. if (LJ_BE) p += 4;

726. sz = 4;

727. }

728. snap_restoredata(T, ex, snapno, rfilt, ir->op2, p, sz);

729. } else {

730. IRIns *irs, *irlast = &T->ir[T->snap[snapno].ref];

731. for (irs = ir+1; irs < irlast; irs++)

732. if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) {

733. IRIns *iro = &T->ir[T->ir[irs->op1].op2];

734. uint8_t *p = (uint8_t *)cd;

735. CTSize szs;

736. lua_assert(irs->o == IR_XSTORE && T->ir[irs->op1].o == IR_ADD);

737. lua_assert(iro->o == IR_KINT || iro->o == IR_KINT64);

738. if (irt_is64(irs->t)) szs = 8;

739. else if (irt_isi8(irs->t) || irt_isu8(irs->t)) szs = 1;

740. else if (irt_isi16(irs->t) || irt_isu16(irs->t)) szs = 2;

741. else szs = 4;

742. if (LJ_64 && iro->o == IR_KINT64)

743. p += (int64_t)ir_k64(iro)->u64;

744. else

745. p += iro->i;

746. lua_assert(p >= (uint8_t *)cdataptr(cd) &&

747. p + szs <= (uint8_t *)cdataptr(cd) + sz);

748. if (LJ_32 && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) {

749. lua_assert(szs == 4);

750. snap_restoredata(T, ex, snapno, rfilt, (irs+1)->op2, LJ_LE?p+4:p,4);

751. if (LJ_BE) p += 4;

752. }

753. snap_restoredata(T, ex, snapno, rfilt, irs->op2, p, szs);

754. }

755. }

756. } else

757. #endif

758. {

759. IRIns *irs, *irlast;

760. GCtab *t = ir->o == IR_TNEW ? lj_tab_new(J->L, ir->op1, ir->op2) :

761. lj_tab_dup(J->L, ir_ktab(&T->ir[ir->op1]));

762. settabV(J->L, o, t);

763. irlast = &T->ir[T->snap[snapno].ref];

764. for (irs = ir+1; irs < irlast; irs++)

765. if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) {

766. IRIns *irk = &T->ir[irs->op1];

767. TValue tmp, *val;

768. lua_assert(irs->o == IR_ASTORE || irs->o == IR_HSTORE ||

769. irs->o == IR_FSTORE);

770. if (irk->o == IR_FREF) {

771. lua_assert(irk->op2 == IRFL_TAB_META);

772. snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, &tmp);

773. /* NOBARRIER: The table is new (marked white). */

774. setgcref(t->metatable, obj2gco(tabV(&tmp)));

775. } else {

776. irk = &T->ir[irk->op2];

777. if (irk->o == IR_KSLOT) irk = &T->ir[irk->op1];

778. lj_ir_kvalue(J->L, &tmp, irk);

779. val = lj_tab_set(J->L, t, &tmp);

780. /* NOBARRIER: The table is new (marked white). */

781. snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, val);

782. if (LJ_SOFTFP && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) {

783. snap_restoreval(J, T, ex, snapno, rfilt, (irs+1)->op2, &tmp);

784. val->u32.hi = tmp.u32.lo;

785. }

786. }

787. }

788. }

789. }

791. /* Restore interpreter state from exit state with the help of a snapshot. */

792. ‌const BCIns *lj_snap_restore(jit_State *J, void *exptr)

793. {

794. ExitState *ex = (ExitState *)exptr;

795. SnapNo snapno = J->exitno;  /* For now, snapno == exitno. */

796. GCtrace *T = traceref(J, J->parent);

797. SnapShot *snap = &T->snap[snapno];

798. MSize n, nent = snap->nent;

799. SnapEntry *map = &T->snapmap[snap->mapofs];

800. SnapEntry *flinks = &T->snapmap[snap_nextofs(T, snap)-1];

801. ptrdiff_t ftsz0;

802. TValue *frame;

803. BloomFilter rfilt = snap_renamefilter(T, snapno);

804. const BCIns *pc = snap_pc(map[nent]);

805. lua_State *L = J->L;

807. /* Set interpreter PC to the next PC to get correct error messages. */

808. setcframe_pc(cframe_raw(L->cframe), pc+1);

810. /* Make sure the stack is big enough for the slots from the snapshot. */

811. if (LJ_UNLIKELY(L->base + snap->topslot >= tvref(L->maxstack))) {

812. L->top = curr_topL(L);

813. lj_state_growstack(L, snap->topslot - curr_proto(L)->framesize);

814. }

816. /* Fill stack slots with data from the registers and spill slots. */

817. frame = L->base-1;

818. ftsz0 = frame_ftsz(frame);  /* Preserve link to previous frame in slot #0. */

819. for (n = 0; n < nent; n++) {

820. SnapEntry sn = map[n];

821. if (!(sn & SNAP_NORESTORE)) {

822. TValue *o = &frame[snap_slot(sn)];

823. IRRef ref = snap_ref(sn);

824. IRIns *ir = &T->ir[ref];

825. if (ir->r == RID_SUNK) {

826. MSize j;

827. for (j = 0; j < n; j++)

828. if (snap_ref(map[j]) == ref) {  /* De-duplicate sunk allocations. */

829. copyTV(L, o, &frame[snap_slot(map[j])]);

830. goto dupslot;

831. }

832. snap_unsink(J, T, ex, snapno, rfilt, ir, o);

833. dupslot:

834. continue;

835. }

836. snap_restoreval(J, T, ex, snapno, rfilt, ref, o);

837. if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && tvisint(o)) {

838. TValue tmp;

839. snap_restoreval(J, T, ex, snapno, rfilt, ref+1, &tmp);

840. o->u32.hi = tmp.u32.lo;

841. } else if ((sn & (SNAP_CONT|SNAP_FRAME))) {

842. lua_assert(!LJ_FR2);  /* TODO_FR2: store 64 bit PCs. */

843. /* Overwrite tag with frame link. */

844. setframe_ftsz(o, snap_slot(sn) != 0 ? (int32_t)*flinks-- : ftsz0);

845. L->base = o+1;

846. }

847. }

848. }

849. lua_assert(map + nent == flinks);

851. /* Compute current stack top. */

852. switch (bc_op(*pc)) {

853. default:

854. if (bc_op(*pc) < BC_FUNCF) {

855. L->top = curr_topL(L);

856. break;

857. }

858. /* fallthrough */

859. case BC_CALLM: case BC_CALLMT: case BC_RETM: case BC_TSETM:

860. L->top = frame + snap->nslots;

861. break;

862. }

863. return pc;

864. }

866. ‌#undef IR

867. ‌#undef emitir_raw

868. ‌#undef emitir

870. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_opt_mem.c - luajit-2.0-src

Data types defined

- AliasRet

Functions defined

- aa_ahref

- aa_cnew

- aa_escape

- aa_findcnew

- aa_fref

- aa_table

- aa_uref

- aa_xref

- fwd_aa_tab_clear

- fwd_ahload

- fwd_aload_reassoc

- lj_opt_dse_ahstore

- lj_opt_dse_fstore

- lj_opt_dse_ustore

- lj_opt_dse_xstore

- lj_opt_fwd_aload

- lj_opt_fwd_fload

- lj_opt_fwd_hload

- lj_opt_fwd_href_nokey

- lj_opt_fwd_hrefk

- lj_opt_fwd_tab_len

- lj_opt_fwd_tptr

- lj_opt_fwd_uload

- lj_opt_fwd_wasnonnil

- lj_opt_fwd_xload

- reassoc_trycse

- reassoc_xref

Macros defined

- IR

- IR

- LUA_CORE

- fins

- fins

- fleft

- fleft

- fright

- fright

- lj_opt_mem_c

Source code

1. /*

2. ** Memory access optimizations.

3. ** AA: Alias Analysis using high-level semantic disambiguation.

4. ** FWD: Load Forwarding (L2L) + Store Forwarding (S2L).

5. ** DSE: Dead-Store Elimination.

6. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

7. */

9. ‌#define lj_opt_mem_c

10. ‌#define LUA_CORE

12. #include "lj_obj.h"

14. #if LJ_HASJIT

16. #include "lj_tab.h"

17. #include "lj_ir.h"

18. #include "lj_jit.h"

19. #include "lj_iropt.h"

20. #include "lj_ircall.h"

22. /* Some local macros to save typing. Undef'd at the end. */

23. ‌#define IR(ref)                (&J->cur.ir[(ref)])

24. ‌#define fins                (&J->fold.ins)

25. ‌#define fleft                (&J->fold.left)

26. ‌#define fright                (&J->fold.right)

28. /*

29. ** Caveat #1: return value is not always a TRef -- only use with tref_ref().

30. ** Caveat #2: FWD relies on active CSE for xREF operands -- see lj_opt_fold().

31. */

33. /* Return values from alias analysis. */

34. typedef enum {

35. ALIAS_NO,        /* The two refs CANNOT alias (exact). */

36. ALIAS_MAY,        /* The two refs MAY alias (inexact). */

37. ALIAS_MUST        /* The two refs MUST alias (exact). */

38. ‌} AliasRet;

40. /* -- ALOAD/HLOAD forwarding and ASTORE/HSTORE elimination ---------------- */

42. /* Simplified escape analysis: check for intervening stores. */

43. ‌static AliasRet aa_escape(jit_State *J, IRIns *ir, IRIns *stop)

44. {

45. IRRef ref = (IRRef)(ir - J->cur.ir);  /* The ref that might be stored. */

46. for (ir++; ir < stop; ir++)

47. if (ir->op2 == ref &&

48. (ir->o == IR_ASTORE || ir->o == IR_HSTORE ||

49. ir->o == IR_USTORE || ir->o == IR_FSTORE))

50. return ALIAS_MAY;  /* Reference was stored and might alias. */

51. return ALIAS_NO;  /* Reference was not stored. */

52. }

54. /* Alias analysis for two different table references. */

55. ‌static AliasRet aa_table(jit_State *J, IRRef ta, IRRef tb)

56. {

57. IRIns *taba = IR(ta), *tabb = IR(tb);

58. int newa, newb;

59. lua_assert(ta != tb);

60. lua_assert(irt_istab(taba->t) && irt_istab(tabb->t));

61. /* Disambiguate new allocations. */

62. newa = (taba->o == IR_TNEW || taba->o == IR_TDUP);

63. newb = (tabb->o == IR_TNEW || tabb->o == IR_TDUP);

64. if (newa && newb)

65. return ALIAS_NO;  /* Two different allocations never alias. */

66. if (newb) {  /* At least one allocation? */

67. IRIns *tmp = taba; taba = tabb; tabb = tmp;

68. } else if (!newa) {

69. return ALIAS_MAY;  /* Anything else: we just don't know. */

70. }

71. return aa_escape(J, taba, tabb);

72. }

74. /* Alias analysis for array and hash access using key-based disambiguation. */

75. ‌static AliasRet aa_ahref(jit_State *J, IRIns *refa, IRIns *refb)

76. {

77. IRRef ka = refa->op2;

78. IRRef kb = refb->op2;

79. IRIns *keya, *keyb;

80. IRRef ta, tb;

81. if (refa == refb)

82. return ALIAS_MUST;  /* Shortcut for same refs. */

83. keya = IR(ka);

84. if (keya->o == IR_KSLOT) { ka = keya->op1; keya = IR(ka); }

85. keyb = IR(kb);

86. if (keyb->o == IR_KSLOT) { kb = keyb->op1; keyb = IR(kb); }

87. ta = (refa->o==IR_HREFK || refa->o==IR_AREF) ? IR(refa->op1)->op1 : refa->op1;

88. tb = (refb->o==IR_HREFK || refb->o==IR_AREF) ? IR(refb->op1)->op1 : refb->op1;

89. if (ka == kb) {

90. /* Same key. Check for same table with different ref (NEWREF vs. HREF). */

91. if (ta == tb)

92. return ALIAS_MUST;  /* Same key, same table. */

93. else

94. return aa_table(J, ta, tb);  /* Same key, possibly different table. */

95. }

96. if (irref_isk(ka) && irref_isk(kb))

97. return ALIAS_NO;  /* Different constant keys. */

98. if (refa->o == IR_AREF) {

99. /* Disambiguate array references based on index arithmetic. */

100. int32_t ofsa = 0, ofsb = 0;

101. IRRef basea = ka, baseb = kb;

102. lua_assert(refb->o == IR_AREF);

103. /* Gather base and offset from t[base] or t[base+-ofs]. */

104. if (keya->o == IR_ADD && irref_isk(keya->op2)) {

105. basea = keya->op1;

106. ofsa = IR(keya->op2)->i;

107. if (basea == kb && ofsa != 0)

108. return ALIAS_NO;  /* t[base+-ofs] vs. t[base]. */

109. }

110. if (keyb->o == IR_ADD && irref_isk(keyb->op2)) {

111. baseb = keyb->op1;

112. ofsb = IR(keyb->op2)->i;

113. if (ka == baseb && ofsb != 0)

114. return ALIAS_NO;  /* t[base] vs. t[base+-ofs]. */

115. }

116. if (basea == baseb && ofsa != ofsb)

117. return ALIAS_NO;  /* t[base+-o1] vs. t[base+-o2] and o1 != o2. */

118. } else {

119. /* Disambiguate hash references based on the type of their keys. */

120. lua_assert((refa->o==IR_HREF || refa->o==IR_HREFK || refa->o==IR_NEWREF) &&

121. (refb->o==IR_HREF || refb->o==IR_HREFK || refb->o==IR_NEWREF));

122. if (!irt_sametype(keya->t, keyb->t))

123. return ALIAS_NO;  /* Different key types. */

124. }

125. if (ta == tb)

126. return ALIAS_MAY;  /* Same table, cannot disambiguate keys. */

127. else

128. return aa_table(J, ta, tb);  /* Try to disambiguate tables. */

129. }

131. /* Array and hash load forwarding. */

132. ‌static TRef fwd_ahload(jit_State *J, IRRef xref)

133. {

134. IRIns *xr = IR(xref);

135. IRRef lim = xref;  /* Search limit. */

136. IRRef ref;

138. /* Search for conflicting stores. */

139. ref = J->chain[fins->o+IRDELTA_L2S];

140. while (ref > xref) {

141. IRIns *store = IR(ref);

142. switch (aa_ahref(J, xr, IR(store->op1))) {

143. case ALIAS_NO:   break;  /* Continue searching. */

144. case ALIAS_MAY:  lim = ref; goto cselim;  /* Limit search for load. */

145. case ALIAS_MUST: return store->op2;  /* Store forwarding. */

146. }

147. ref = store->prev;

148. }

150. /* No conflicting store (yet): const-fold loads from allocations. */

151. {

152. IRIns *ir = (xr->o == IR_HREFK || xr->o == IR_AREF) ? IR(xr->op1) : xr;

153. IRRef tab = ir->op1;

154. ir = IR(tab);

155. if (ir->o == IR_TNEW || (ir->o == IR_TDUP && irref_isk(xr->op2))) {

156. /* A NEWREF with a number key may end up pointing to the array part.

157. ** But it's referenced from HSTORE and not found in the ASTORE chain.

158. ** For now simply consider this a conflict without forwarding anything.

159. */

160. if (xr->o == IR_AREF) {

161. IRRef ref2 = J->chain[IR_NEWREF];

162. while (ref2 > tab) {

163. IRIns *newref = IR(ref2);

164. if (irt_isnum(IR(newref->op2)->t))

165. goto cselim;

166. ref2 = newref->prev;

167. }

168. }

169. /* NEWREF inhibits CSE for HREF, and dependent FLOADs from HREFK/AREF.

170. ** But the above search for conflicting stores was limited by xref.

171. ** So continue searching, limited by the TNEW/TDUP. Store forwarding

172. ** is ok, too. A conflict does NOT limit the search for a matching load.

173. */

174. while (ref > tab) {

175. IRIns *store = IR(ref);

176. switch (aa_ahref(J, xr, IR(store->op1))) {

177. case ALIAS_NO:   break;  /* Continue searching. */

178. case ALIAS_MAY:  goto cselim;  /* Conflicting store. */

179. case ALIAS_MUST: return store->op2;  /* Store forwarding. */

180. }

181. ref = store->prev;

182. }

183. lua_assert(ir->o != IR_TNEW || irt_isnil(fins->t));

184. if (irt_ispri(fins->t)) {

185. return TREF_PRI(irt_type(fins->t));

186. } else if (irt_isnum(fins->t) || (LJ_DUALNUM && irt_isint(fins->t)) ||

187. irt_isstr(fins->t)) {

188. TValue keyv;

189. cTValue *tv;

190. IRIns *key = IR(xr->op2);

191. if (key->o == IR_KSLOT) key = IR(key->op1);

192. lj_ir_kvalue(J->L, &keyv, key);

193. tv = lj_tab_get(J->L, ir_ktab(IR(ir->op1)), &keyv);

194. lua_assert(itype2irt(tv) == irt_type(fins->t));

195. if (irt_isnum(fins->t))

196. return lj_ir_knum_u64(J, tv->u64);

197. else if (LJ_DUALNUM && irt_isint(fins->t))

198. return lj_ir_kint(J, intV(tv));

199. else

200. return lj_ir_kstr(J, strV(tv));

201. }

202. /* Othwerwise: don't intern as a constant. */

203. }

204. }

206. cselim:

207. /* Try to find a matching load. Below the conflicting store, if any. */

208. ref = J->chain[fins->o];

209. while (ref > lim) {

210. IRIns *load = IR(ref);

211. if (load->op1 == xref)

212. return ref;  /* Load forwarding. */

213. ref = load->prev;

214. }

215. return 0;  /* Conflict or no match. */

216. }

218. /* Reassociate ALOAD across PHIs to handle t[i-1] forwarding case. */

219. ‌static TRef fwd_aload_reassoc(jit_State *J)

220. {

221. IRIns *irx = IR(fins->op1);

222. IRIns *key = IR(irx->op2);

223. if (key->o == IR_ADD && irref_isk(key->op2)) {

224. IRIns *add2 = IR(key->op1);

225. if (add2->o == IR_ADD && irref_isk(add2->op2) &&

226. IR(key->op2)->i == -IR(add2->op2)->i) {

227. IRRef ref = J->chain[IR_AREF];

228. IRRef lim = add2->op1;

229. if (irx->op1 > lim) lim = irx->op1;

230. while (ref > lim) {

231. IRIns *ir = IR(ref);

232. if (ir->op1 == irx->op1 && ir->op2 == add2->op1)

233. return fwd_ahload(J, ref);

234. ref = ir->prev;

235. }

236. }

237. }

238. return 0;

239. }

241. /* ALOAD forwarding. */

242. ‌TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J)

243. {

244. IRRef ref;

245. if ((ref = fwd_ahload(J, fins->op1)) ||

246. (ref = fwd_aload_reassoc(J)))

247. return ref;

248. return EMITFOLD;

249. }

251. /* HLOAD forwarding. */

252. ‌TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J)

253. {

254. IRRef ref = fwd_ahload(J, fins->op1);

255. if (ref)

256. return ref;

257. return EMITFOLD;

258. }

260. /* HREFK forwarding. */

261. ‌TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J)

262. {

263. IRRef tab = fleft->op1;

264. IRRef ref = J->chain[IR_NEWREF];

265. while (ref > tab) {

266. IRIns *newref = IR(ref);

267. if (tab == newref->op1) {

268. if (fright->op1 == newref->op2)

269. return ref;  /* Forward from NEWREF. */

270. else

271. goto docse;

272. } else if (aa_table(J, tab, newref->op1) != ALIAS_NO) {

273. goto docse;

274. }

275. ref = newref->prev;

276. }

277. /* No conflicting NEWREF: key location unchanged for HREFK of TDUP. */

278. if (IR(tab)->o == IR_TDUP)

279. fins->t.irt &= ~IRT_GUARD;  /* Drop HREFK guard. */

280. docse:

281. return CSEFOLD;

282. }

284. /* Check whether HREF of TNEW/TDUP can be folded to niltv. */

285. ‌int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J)

286. {

287. IRRef lim = fins->op1;  /* Search limit. */

288. IRRef ref;

290. /* The key for an ASTORE may end up in the hash part after a NEWREF. */

291. if (irt_isnum(fright->t) && J->chain[IR_NEWREF] > lim) {

292. ref = J->chain[IR_ASTORE];

293. while (ref > lim) {

294. if (ref < J->chain[IR_NEWREF])

295. return 0;  /* Conflict. */

296. ref = IR(ref)->prev;

297. }

298. }

300. /* Search for conflicting stores. */

301. ref = J->chain[IR_HSTORE];

302. while (ref > lim) {

303. IRIns *store = IR(ref);

304. if (aa_ahref(J, fins, IR(store->op1)) != ALIAS_NO)

305. return 0;  /* Conflict. */

306. ref = store->prev;

307. }

309. return 1;  /* No conflict. Can fold to niltv. */

310. }

312. /* Check whether there's no aliasing table.clear. */

313. ‌static int fwd_aa_tab_clear(jit_State *J, IRRef lim, IRRef ta)

314. {

315. IRRef ref = J->chain[IR_CALLS];

316. while (ref > lim) {

317. IRIns *calls = IR(ref);

318. if (calls->op2 == IRCALL_lj_tab_clear &&

319. (ta == calls->op1 || aa_table(J, ta, calls->op1) != ALIAS_NO))

320. return 0;  /* Conflict. */

321. ref = calls->prev;

322. }

323. return 1;  /* No conflict. Can safely FOLD/CSE. */

324. }

326. /* Check whether there's no aliasing NEWREF/table.clear for the left operand. */

327. ‌int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim)

328. {

329. IRRef ta = fins->op1;

330. IRRef ref = J->chain[IR_NEWREF];

331. while (ref > lim) {

332. IRIns *newref = IR(ref);

333. if (ta == newref->op1 || aa_table(J, ta, newref->op1) != ALIAS_NO)

334. return 0;  /* Conflict. */

335. ref = newref->prev;

336. }

337. return fwd_aa_tab_clear(J, lim, ta);

338. }

340. /* ASTORE/HSTORE elimination. */

341. ‌TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J)

342. {

343. IRRef xref = fins->op1;  /* xREF reference. */

344. IRRef val = fins->op2;  /* Stored value reference. */

345. IRIns *xr = IR(xref);

346. IRRef1 *refp = &J->chain[fins->o];

347. IRRef ref = *refp;

348. while (ref > xref) {  /* Search for redundant or conflicting stores. */

349. IRIns *store = IR(ref);

350. switch (aa_ahref(J, xr, IR(store->op1))) {

351. case ALIAS_NO:

352. break;  /* Continue searching. */

353. case ALIAS_MAY:        /* Store to MAYBE the same location. */

354. if (store->op2 != val)  /* Conflict if the value is different. */

355. goto doemit;

356. break;  /* Otherwise continue searching. */

357. case ALIAS_MUST:        /* Store to the same location. */

358. if (store->op2 == val)  /* Same value: drop the new store. */

359. return DROPFOLD;

360. /* Different value: try to eliminate the redundant store. */

361. if (ref > J->chain[IR_LOOP]) {  /* Quick check to avoid crossing LOOP. */

362. IRIns *ir;

363. /* Check for any intervening guards (includes conflicting loads). */

364. for (ir = IR(J->cur.nins-1); ir > store; ir--)

365. if (irt_isguard(ir->t) || ir->o == IR_CALLL)

366. goto doemit;  /* No elimination possible. */

367. /* Remove redundant store from chain and replace with NOP. */

368. *refp = store->prev;

369. store->o = IR_NOP;

370. store->t.irt = IRT_NIL;

371. store->op1 = store->op2 = 0;

372. store->prev = 0;

373. /* Now emit the new store instead. */

374. }

375. goto doemit;

376. }

377. ref = *(refp = &store->prev);

378. }

379. doemit:

380. return EMITFOLD;  /* Otherwise we have a conflict or simply no match. */

381. }

383. /* -- ULOAD forwarding ---------------------------------------------------- */

385. /* The current alias analysis for upvalues is very simplistic. It only

386. ** disambiguates between the unique upvalues of the same function.

387. ** This is good enough for now, since most upvalues are read-only.

388. **

389. ** A more precise analysis would be feasible with the help of the parser:

390. ** generate a unique key for every upvalue, even across all prototypes.

391. ** Lacking a realistic use-case, it's unclear whether this is beneficial.

392. */

393. ‌static AliasRet aa_uref(IRIns *refa, IRIns *refb)

394. {

395. if (refa->o != refb->o)

396. return ALIAS_NO;  /* Different UREFx type. */

397. if (refa->op1 == refb->op1) {  /* Same function. */

398. if (refa->op2 == refb->op2)

399. return ALIAS_MUST;  /* Same function, same upvalue idx. */

400. else

401. return ALIAS_NO;  /* Same function, different upvalue idx. */

402. } else {  /* Different functions, check disambiguation hash values. */

403. if (((refa->op2 ^ refb->op2) & 0xff))

404. return ALIAS_NO;  /* Upvalues with different hash values cannot alias. */

405. else

406. return ALIAS_MAY;  /* No conclusion can be drawn for same hash value. */

407. }

408. }

410. /* ULOAD forwarding. */

411. ‌TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J)

412. {

413. IRRef uref = fins->op1;

414. IRRef lim = REF_BASE;  /* Search limit. */

415. IRIns *xr = IR(uref);

416. IRRef ref;

418. /* Search for conflicting stores. */

419. ref = J->chain[IR_USTORE];

420. while (ref > lim) {

421. IRIns *store = IR(ref);

422. switch (aa_uref(xr, IR(store->op1))) {

423. case ALIAS_NO:   break;  /* Continue searching. */

424. case ALIAS_MAY:  lim = ref; goto cselim;  /* Limit search for load. */

425. case ALIAS_MUST: return store->op2;  /* Store forwarding. */

426. }

427. ref = store->prev;

428. }

430. cselim:

431. /* Try to find a matching load. Below the conflicting store, if any. */

433. ref = J->chain[IR_ULOAD];

434. while (ref > lim) {

435. IRIns *ir = IR(ref);

436. if (ir->op1 == uref ||

437. (IR(ir->op1)->op12 == IR(uref)->op12 && IR(ir->op1)->o == IR(uref)->o))

438. return ref;  /* Match for identical or equal UREFx (non-CSEable UREFO). */

439. ref = ir->prev;

440. }

441. return lj_ir_emit(J);

442. }

444. /* USTORE elimination. */

445. ‌TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J)

446. {

447. IRRef xref = fins->op1;  /* xREF reference. */

448. IRRef val = fins->op2;  /* Stored value reference. */

449. IRIns *xr = IR(xref);

450. IRRef1 *refp = &J->chain[IR_USTORE];

451. IRRef ref = *refp;

452. while (ref > xref) {  /* Search for redundant or conflicting stores. */

453. IRIns *store = IR(ref);

454. switch (aa_uref(xr, IR(store->op1))) {

455. case ALIAS_NO:

456. break;  /* Continue searching. */

457. case ALIAS_MAY:        /* Store to MAYBE the same location. */

458. if (store->op2 != val)  /* Conflict if the value is different. */

459. goto doemit;

460. break;  /* Otherwise continue searching. */

461. case ALIAS_MUST:        /* Store to the same location. */

462. if (store->op2 == val)  /* Same value: drop the new store. */

463. return DROPFOLD;

464. /* Different value: try to eliminate the redundant store. */

465. if (ref > J->chain[IR_LOOP]) {  /* Quick check to avoid crossing LOOP. */

466. IRIns *ir;

467. /* Check for any intervening guards (includes conflicting loads). */

468. for (ir = IR(J->cur.nins-1); ir > store; ir--)

469. if (irt_isguard(ir->t))

470. goto doemit;  /* No elimination possible. */

471. /* Remove redundant store from chain and replace with NOP. */

472. *refp = store->prev;

473. store->o = IR_NOP;

474. store->t.irt = IRT_NIL;

475. store->op1 = store->op2 = 0;

476. store->prev = 0;

477. if (ref+1 < J->cur.nins &&

478. store[1].o == IR_OBAR && store[1].op1 == xref) {

479. IRRef1 *bp = &J->chain[IR_OBAR];

480. IRIns *obar;

481. for (obar = IR(*bp); *bp > ref+1; obar = IR(*bp))

482. bp = &obar->prev;

483. /* Remove OBAR, too. */

484. *bp = obar->prev;

485. obar->o = IR_NOP;

486. obar->t.irt = IRT_NIL;

487. obar->op1 = obar->op2 = 0;

488. obar->prev = 0;

489. }

490. /* Now emit the new store instead. */

491. }

492. goto doemit;

493. }

494. ref = *(refp = &store->prev);

495. }

496. doemit:

497. return EMITFOLD;  /* Otherwise we have a conflict or simply no match. */

498. }

500. /* -- FLOAD forwarding and FSTORE elimination ----------------------------- */

502. /* Alias analysis for field access.

503. ** Field loads are cheap and field stores are rare.

504. ** Simple disambiguation based on field types is good enough.

505. */

506. ‌static AliasRet aa_fref(jit_State *J, IRIns *refa, IRIns *refb)

507. {

508. if (refa->op2 != refb->op2)

509. return ALIAS_NO;  /* Different fields. */

510. if (refa->op1 == refb->op1)

511. return ALIAS_MUST;  /* Same field, same object. */

512. else if (refa->op2 >= IRFL_TAB_META && refa->op2 <= IRFL_TAB_NOMM)

513. return aa_table(J, refa->op1, refb->op1);  /* Disambiguate tables. */

514. else

515. return ALIAS_MAY;  /* Same field, possibly different object. */

516. }

518. /* Only the loads for mutable fields end up here (see FOLD). */

519. ‌TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J)

520. {

521. IRRef oref = fins->op1;  /* Object reference. */

522. IRRef fid = fins->op2;  /* Field ID. */

523. IRRef lim = oref;  /* Search limit. */

524. IRRef ref;

526. /* Search for conflicting stores. */

527. ref = J->chain[IR_FSTORE];

528. while (ref > oref) {

529. IRIns *store = IR(ref);

530. switch (aa_fref(J, fins, IR(store->op1))) {

531. case ALIAS_NO:   break;  /* Continue searching. */

532. case ALIAS_MAY:  lim = ref; goto cselim;  /* Limit search for load. */

533. case ALIAS_MUST: return store->op2;  /* Store forwarding. */

534. }

535. ref = store->prev;

536. }

538. /* No conflicting store: const-fold field loads from allocations. */

539. if (fid == IRFL_TAB_META) {

540. IRIns *ir = IR(oref);

541. if (ir->o == IR_TNEW || ir->o == IR_TDUP)

542. return lj_ir_knull(J, IRT_TAB);

543. }

545. cselim:

546. /* Try to find a matching load. Below the conflicting store, if any. */

547. return lj_opt_cselim(J, lim);

548. }

550. /* FSTORE elimination. */

551. ‌TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J)

552. {

553. IRRef fref = fins->op1;  /* FREF reference. */

554. IRRef val = fins->op2;  /* Stored value reference. */

555. IRIns *xr = IR(fref);

556. IRRef1 *refp = &J->chain[IR_FSTORE];

557. IRRef ref = *refp;

558. while (ref > fref) {  /* Search for redundant or conflicting stores. */

559. IRIns *store = IR(ref);

560. switch (aa_fref(J, xr, IR(store->op1))) {

561. case ALIAS_NO:

562. break;  /* Continue searching. */

563. case ALIAS_MAY:

564. if (store->op2 != val)  /* Conflict if the value is different. */

565. goto doemit;

566. break;  /* Otherwise continue searching. */

567. case ALIAS_MUST:

568. if (store->op2 == val)  /* Same value: drop the new store. */

569. return DROPFOLD;

570. /* Different value: try to eliminate the redundant store. */

571. if (ref > J->chain[IR_LOOP]) {  /* Quick check to avoid crossing LOOP. */

572. IRIns *ir;

573. /* Check for any intervening guards or conflicting loads. */

574. for (ir = IR(J->cur.nins-1); ir > store; ir--)

575. if (irt_isguard(ir->t) || (ir->o == IR_FLOAD && ir->op2 == xr->op2))

576. goto doemit;  /* No elimination possible. */

577. /* Remove redundant store from chain and replace with NOP. */

578. *refp = store->prev;

579. store->o = IR_NOP;

580. store->t.irt = IRT_NIL;

581. store->op1 = store->op2 = 0;

582. store->prev = 0;

583. /* Now emit the new store instead. */

584. }

585. goto doemit;

586. }

587. ref = *(refp = &store->prev);

588. }

589. doemit:

590. return EMITFOLD;  /* Otherwise we have a conflict or simply no match. */

591. }

593. /* -- XLOAD forwarding and XSTORE elimination ----------------------------- */

595. /* Find cdata allocation for a reference (if any). */

596. ‌static IRIns *aa_findcnew(jit_State *J, IRIns *ir)

597. {

598. while (ir->o == IR_ADD) {

599. if (!irref_isk(ir->op1)) {

600. IRIns *ir1 = aa_findcnew(J, IR(ir->op1));  /* Left-recursion. */

601. if (ir1) return ir1;

602. }

603. if (irref_isk(ir->op2)) return NULL;

604. ir = IR(ir->op2);  /* Flatten right-recursion. */

605. }

606. return ir->o == IR_CNEW ? ir : NULL;

607. }

609. /* Alias analysis for two cdata allocations. */

610. ‌static AliasRet aa_cnew(jit_State *J, IRIns *refa, IRIns *refb)

611. {

612. IRIns *cnewa = aa_findcnew(J, refa);

613. IRIns *cnewb = aa_findcnew(J, refb);

614. if (cnewa == cnewb)

615. return ALIAS_MAY;  /* Same allocation or neither is an allocation. */

616. if (cnewa && cnewb)

617. return ALIAS_NO;  /* Two different allocations never alias. */

618. if (cnewb) { cnewa = cnewb; refb = refa; }

619. return aa_escape(J, cnewa, refb);

620. }

622. /* Alias analysis for XLOAD/XSTORE. */

623. ‌static AliasRet aa_xref(jit_State *J, IRIns *refa, IRIns *xa, IRIns *xb)

624. {

625. ptrdiff_t ofsa = 0, ofsb = 0;

626. IRIns *refb = IR(xb->op1);

627. IRIns *basea = refa, *baseb = refb;

628. if (refa == refb && irt_sametype(xa->t, xb->t))

629. return ALIAS_MUST;  /* Shortcut for same refs with identical type. */

630. /* Offset-based disambiguation. */

631. if (refa->o == IR_ADD && irref_isk(refa->op2)) {

632. IRIns *irk = IR(refa->op2);

633. basea = IR(refa->op1);

634. ofsa = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 :

635. (ptrdiff_t)irk->i;

636. }

637. if (refb->o == IR_ADD && irref_isk(refb->op2)) {

638. IRIns *irk = IR(refb->op2);

639. baseb = IR(refb->op1);

640. ofsb = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 :

641. (ptrdiff_t)irk->i;

642. }

643. /* Treat constified pointers like base vs. base+offset. */

644. if (basea->o == IR_KPTR && baseb->o == IR_KPTR) {

645. ofsb += (char *)ir_kptr(baseb) - (char *)ir_kptr(basea);

646. baseb = basea;

647. }

648. /* This implements (very) strict aliasing rules.

649. ** Different types do NOT alias, except for differences in signedness.

650. ** Type punning through unions is allowed (but forces a reload).

651. */

652. if (basea == baseb) {

653. ptrdiff_t sza = irt_size(xa->t), szb = irt_size(xb->t);

654. if (ofsa == ofsb) {

655. if (sza == szb && irt_isfp(xa->t) == irt_isfp(xb->t))

656. return ALIAS_MUST;  /* Same-sized, same-kind. May need to convert. */

657. } else if (ofsa + sza <= ofsb || ofsb + szb <= ofsa) {

658. return ALIAS_NO;  /* Non-overlapping base+-o1 vs. base+-o2. */

659. }

660. /* NYI: extract, extend or reinterpret bits (int <-> fp). */

661. return ALIAS_MAY;  /* Overlapping or type punning: force reload. */

662. }

663. if (!irt_sametype(xa->t, xb->t) &&

664. !(irt_typerange(xa->t, IRT_I8, IRT_U64) &&

665. ((xa->t.irt - IRT_I8) ^ (xb->t.irt - IRT_I8)) == 1))

666. return ALIAS_NO;

667. /* NYI: structural disambiguation. */

668. return aa_cnew(J, basea, baseb);  /* Try to disambiguate allocations. */

669. }

671. /* Return CSEd reference or 0. Caveat: swaps lower ref to the right! */

672. ‌static IRRef reassoc_trycse(jit_State *J, IROp op, IRRef op1, IRRef op2)

673. {

674. IRRef ref = J->chain[op];

675. IRRef lim = op1;

676. if (op2 > lim) { lim = op2; op2 = op1; op1 = lim; }

677. while (ref > lim) {

678. IRIns *ir = IR(ref);

679. if (ir->op1 == op1 && ir->op2 == op2)

680. return ref;

681. ref = ir->prev;

682. }

683. return 0;

684. }

686. /* Reassociate index references. */

687. ‌static IRRef reassoc_xref(jit_State *J, IRIns *ir)

688. {

689. ptrdiff_t ofs = 0;

690. if (ir->o == IR_ADD && irref_isk(ir->op2)) {  /* Get constant offset. */

691. IRIns *irk = IR(ir->op2);

692. ofs = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 :

693. (ptrdiff_t)irk->i;

694. ir = IR(ir->op1);

695. }

696. if (ir->o == IR_ADD) {  /* Add of base + index. */

697. /* Index ref > base ref for loop-carried dependences. Only check op1. */

698. IRIns *ir2, *ir1 = IR(ir->op1);

699. int32_t shift = 0;

700. IRRef idxref;

701. /* Determine index shifts. Don't bother with IR_MUL here. */

702. if (ir1->o == IR_BSHL && irref_isk(ir1->op2))

703. shift = IR(ir1->op2)->i;

704. else if (ir1->o == IR_ADD && ir1->op1 == ir1->op2)

705. shift = 1;

706. else

707. ir1 = ir;

708. ir2 = IR(ir1->op1);

709. /* A non-reassociated add. Must be a loop-carried dependence. */

710. if (ir2->o == IR_ADD && irt_isint(ir2->t) && irref_isk(ir2->op2))

711. ofs += (ptrdiff_t)IR(ir2->op2)->i << shift;

712. else

713. return 0;

714. idxref = ir2->op1;

715. /* Try to CSE the reassociated chain. Give up if not found. */

716. if (ir1 != ir &&

717. !(idxref = reassoc_trycse(J, ir1->o, idxref,

718. ir1->o == IR_BSHL ? ir1->op2 : idxref)))

719. return 0;

720. if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, ir->op2)))

721. return 0;

722. if (ofs != 0) {

723. IRRef refk = tref_ref(lj_ir_kintp(J, ofs));

724. if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, refk)))

725. return 0;

726. }

727. return idxref;  /* Success, found a reassociated index reference. Phew. */

728. }

729. return 0;  /* Failure. */

730. }

732. /* XLOAD forwarding. */

733. ‌TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J)

734. {

735. IRRef xref = fins->op1;

736. IRIns *xr = IR(xref);

737. IRRef lim = xref;  /* Search limit. */

738. IRRef ref;

740. if ((fins->op2 & IRXLOAD_READONLY))

741. goto cselim;

742. if ((fins->op2 & IRXLOAD_VOLATILE))

743. goto doemit;

745. /* Search for conflicting stores. */

746. ref = J->chain[IR_XSTORE];

747. retry:

748. if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS];

749. if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR];

750. while (ref > lim) {

751. IRIns *store = IR(ref);

752. switch (aa_xref(J, xr, fins, store)) {

753. case ALIAS_NO:   break;  /* Continue searching. */

754. case ALIAS_MAY:  lim = ref; goto cselim;  /* Limit search for load. */

755. case ALIAS_MUST:

756. /* Emit conversion if the loaded type doesn't match the forwarded type. */

757. if (!irt_sametype(fins->t, IR(store->op2)->t)) {

758. IRType dt = irt_type(fins->t), st = irt_type(IR(store->op2)->t);

759. if (dt == IRT_I8 || dt == IRT_I16) {  /* Trunc + sign-extend. */

760. st = dt | IRCONV_SEXT;

761. dt = IRT_INT;

762. } else if (dt == IRT_U8 || dt == IRT_U16) {  /* Trunc + zero-extend. */

763. st = dt;

764. dt = IRT_INT;

765. }

766. fins->ot = IRT(IR_CONV, dt);

767. fins->op1 = store->op2;

768. fins->op2 = (dt<<5)|st;

769. return RETRYFOLD;

770. }

771. return store->op2;  /* Store forwarding. */

772. }

773. ref = store->prev;

774. }

776. cselim:

777. /* Try to find a matching load. Below the conflicting store, if any. */

778. ref = J->chain[IR_XLOAD];

779. while (ref > lim) {

780. /* CSE for XLOAD depends on the type, but not on the IRXLOAD_* flags. */

781. if (IR(ref)->op1 == xref && irt_sametype(IR(ref)->t, fins->t))

782. return ref;

783. ref = IR(ref)->prev;

784. }

786. /* Reassociate XLOAD across PHIs to handle a[i-1] forwarding case. */

787. if (!(fins->op2 & IRXLOAD_READONLY) && J->chain[IR_LOOP] &&

788. xref == fins->op1 && (xref = reassoc_xref(J, xr)) != 0) {

789. ref = J->chain[IR_XSTORE];

790. while (ref > lim)  /* Skip stores that have already been checked. */

791. ref = IR(ref)->prev;

792. lim = xref;

793. xr = IR(xref);

794. goto retry;  /* Retry with the reassociated reference. */

795. }

796. doemit:

797. return EMITFOLD;

798. }

800. /* XSTORE elimination. */

801. ‌TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J)

802. {

803. IRRef xref = fins->op1;

804. IRIns *xr = IR(xref);

805. IRRef lim = xref;  /* Search limit. */

806. IRRef val = fins->op2;  /* Stored value reference. */

807. IRRef1 *refp = &J->chain[IR_XSTORE];

808. IRRef ref = *refp;

809. if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS];

810. if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR];

811. if (J->chain[IR_XSNEW] > lim) lim = J->chain[IR_XSNEW];

812. while (ref > lim) {  /* Search for redundant or conflicting stores. */

813. IRIns *store = IR(ref);

814. switch (aa_xref(J, xr, fins, store)) {

815. case ALIAS_NO:

816. break;  /* Continue searching. */

817. case ALIAS_MAY:

818. if (store->op2 != val)  /* Conflict if the value is different. */

819. goto doemit;

820. break;  /* Otherwise continue searching. */

821. case ALIAS_MUST:

822. if (store->op2 == val)  /* Same value: drop the new store. */

823. return DROPFOLD;

824. /* Different value: try to eliminate the redundant store. */

825. if (ref > J->chain[IR_LOOP]) {  /* Quick check to avoid crossing LOOP. */

826. IRIns *ir;

827. /* Check for any intervening guards or any XLOADs (no AA performed). */

828. for (ir = IR(J->cur.nins-1); ir > store; ir--)

829. if (irt_isguard(ir->t) || ir->o == IR_XLOAD)

830. goto doemit;  /* No elimination possible. */

831. /* Remove redundant store from chain and replace with NOP. */

832. *refp = store->prev;

833. store->o = IR_NOP;

834. store->t.irt = IRT_NIL;

835. store->op1 = store->op2 = 0;

836. store->prev = 0;

837. /* Now emit the new store instead. */

838. }

839. goto doemit;

840. }

841. ref = *(refp = &store->prev);

842. }

843. doemit:

844. return EMITFOLD;  /* Otherwise we have a conflict or simply no match. */

845. }

847. /* -- Forwarding of lj_tab_len -------------------------------------------- */

849. /* This is rather simplistic right now, but better than nothing. */

850. ‌TRef LJ_FASTCALL lj_opt_fwd_tab_len(jit_State *J)

851. {

852. IRRef tab = fins->op1;  /* Table reference. */

853. IRRef lim = tab;  /* Search limit. */

854. IRRef ref;

856. /* Any ASTORE is a conflict and limits the search. */

857. if (J->chain[IR_ASTORE] > lim) lim = J->chain[IR_ASTORE];

859. /* Search for conflicting HSTORE with numeric key. */

860. ref = J->chain[IR_HSTORE];

861. while (ref > lim) {

862. IRIns *store = IR(ref);

863. IRIns *href = IR(store->op1);

864. IRIns *key = IR(href->op2);

865. if (irt_isnum(key->o == IR_KSLOT ? IR(key->op1)->t : key->t)) {

866. lim = ref;  /* Conflicting store found, limits search for TLEN. */

867. break;

868. }

869. ref = store->prev;

870. }

872. /* Search for aliasing table.clear. */

873. if (!fwd_aa_tab_clear(J, lim, tab))

874. return lj_ir_emit(J);

876. /* Try to find a matching load. Below the conflicting store, if any. */

877. return lj_opt_cselim(J, lim);

878. }

880. /* -- ASTORE/HSTORE previous type analysis -------------------------------- */

882. /* Check whether the previous value for a table store is non-nil.

883. ** This can be derived either from a previous store or from a previous

884. ** load (because all loads from tables perform a type check).

885. **

886. ** The result of the analysis can be used to avoid the metatable check

887. ** and the guard against HREF returning niltv. Both of these are cheap,

888. ** so let's not spend too much effort on the analysis.

889. **

890. ** A result of 1 is exact: previous value CANNOT be nil.

891. ** A result of 0 is inexact: previous value MAY be nil.

892. */

893. ‌int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref)

894. {

895. /* First check stores. */

896. IRRef ref = J->chain[loadop+IRDELTA_L2S];

897. while (ref > xref) {

898. IRIns *store = IR(ref);

899. if (store->op1 == xref) {  /* Same xREF. */

900. /* A nil store MAY alias, but a non-nil store MUST alias. */

901. return !irt_isnil(store->t);

902. } else if (irt_isnil(store->t)) {  /* Must check any nil store. */

903. IRRef skref = IR(store->op1)->op2;

904. IRRef xkref = IR(xref)->op2;

905. /* Same key type MAY alias. Need ALOAD check due to multiple int types. */

906. if (loadop == IR_ALOAD || irt_sametype(IR(skref)->t, IR(xkref)->t)) {

907. if (skref == xkref || !irref_isk(skref) || !irref_isk(xkref))

908. return 0;  /* A nil store with same const key or var key MAY alias. */

909. /* Different const keys CANNOT alias. */

910. }  /* Different key types CANNOT alias. */

911. }  /* Other non-nil stores MAY alias. */

912. ref = store->prev;

913. }

915. /* Check loads since nothing could be derived from stores. */

916. ref = J->chain[loadop];

917. while (ref > xref) {

918. IRIns *load = IR(ref);

919. if (load->op1 == xref) {  /* Same xREF. */

920. /* A nil load MAY alias, but a non-nil load MUST alias. */

921. return !irt_isnil(load->t);

922. }  /* Other non-nil loads MAY alias. */

923. ref = load->prev;

924. }

925. return 0;  /* Nothing derived at all, previous value MAY be nil. */

926. }

928. /* ------------------------------------------------------------------------ */

930. ‌#undef IR

931. ‌#undef fins

932. ‌#undef fleft

933. ‌#undef fright

935. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_opt_dce.c - luajit-2.0-src

Functions defined

- dce_marksnap

- dce_propagate

- lj_opt_dce

Macros defined

- IR

- IR

- LUA_CORE

- lj_opt_dce_c

Source code

1. /*

2. ** DCE: Dead Code Elimination. Pre-LOOP only -- ASM already performs DCE.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_opt_dce_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT

13. #include "lj_ir.h"

14. #include "lj_jit.h"

15. #include "lj_iropt.h"

17. /* Some local macros to save typing. Undef'd at the end. */

18. ‌#define IR(ref)                (&J->cur.ir[(ref)])

20. /* Scan through all snapshots and mark all referenced instructions. */

21. ‌static void dce_marksnap(jit_State *J)

22. {

23. SnapNo i, nsnap = J->cur.nsnap;

24. for (i = 0; i < nsnap; i++) {

25. SnapShot *snap = &J->cur.snap[i];

26. SnapEntry *map = &J->cur.snapmap[snap->mapofs];

27. MSize n, nent = snap->nent;

28. for (n = 0; n < nent; n++) {

29. IRRef ref = snap_ref(map[n]);

30. if (ref >= REF_FIRST)

31. irt_setmark(IR(ref)->t);

32. }

33. }

34. }

36. /* Backwards propagate marks. Replace unused instructions with NOPs. */

37. ‌static void dce_propagate(jit_State *J)

38. {

39. IRRef1 *pchain[IR__MAX];

40. IRRef ins;

41. uint32_t i;

42. for (i = 0; i < IR__MAX; i++) pchain[i] = &J->chain[i];

43. for (ins = J->cur.nins-1; ins >= REF_FIRST; ins--) {

44. IRIns *ir = IR(ins);

45. if (irt_ismarked(ir->t)) {

46. irt_clearmark(ir->t);

47. pchain[ir->o] = &ir->prev;

48. } else if (!ir_sideeff(ir)) {

49. *pchain[ir->o] = ir->prev;  /* Reroute original instruction chain. */

50. ir->t.irt = IRT_NIL;

51. ir->o = IR_NOP;  /* Replace instruction with NOP. */

52. ir->op1 = ir->op2 = 0;

53. ir->prev = 0;

54. continue;

55. }

56. if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t);

57. if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t);

58. }

59. }

61. /* Dead Code Elimination.

62. **

63. ** First backpropagate marks for all used instructions. Then replace

64. ** the unused ones with a NOP. Note that compressing the IR to eliminate

65. ** the NOPs does not pay off.

66. */

67. ‌void lj_opt_dce(jit_State *J)

68. {

69. if ((J->flags & JIT_F_OPT_DCE)) {

70. dce_marksnap(J);

71. dce_propagate(J);

72. memset(J->bpropcache, 0, sizeof(J->bpropcache));  /* Invalidate cache. */

73. }

74. }

76. ‌#undef IR

78. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_opt_sink.c - luajit-2.0-src

Functions defined

- lj_opt_sink

- sink_checkalloc

- sink_checkphi

- sink_mark_ins

- sink_mark_snap

- sink_phidep

- sink_remark_phi

- sink_sweep_ins

Macros defined

- IR

- IR

- LUA_CORE

- lj_opt_sink_c

Source code

1. /*

2. ** SINK: Allocation Sinking and Store Sinking.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_opt_sink_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT

13. #include "lj_ir.h"

14. #include "lj_jit.h"

15. #include "lj_iropt.h"

16. #include "lj_target.h"

18. /* Some local macros to save typing. Undef'd at the end. */

19. ‌#define IR(ref)                (&J->cur.ir[(ref)])

21. /* Check whether the store ref points to an eligible allocation. */

22. ‌static IRIns *sink_checkalloc(jit_State *J, IRIns *irs)

23. {

24. IRIns *ir = IR(irs->op1);

25. if (!irref_isk(ir->op2))

26. return NULL;  /* Non-constant key. */

27. if (ir->o == IR_HREFK || ir->o == IR_AREF)

28. ir = IR(ir->op1);

29. else if (!(ir->o == IR_HREF || ir->o == IR_NEWREF ||

30. ir->o == IR_FREF || ir->o == IR_ADD))

31. return NULL;  /* Unhandled reference type (for XSTORE). */

32. ir = IR(ir->op1);

33. if (!(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW))

34. return NULL;  /* Not an allocation. */

35. return ir;  /* Return allocation. */

36. }

38. /* Recursively check whether a value depends on a PHI. */

39. ‌static int sink_phidep(jit_State *J, IRRef ref)

40. {

41. IRIns *ir = IR(ref);

42. if (irt_isphi(ir->t)) return 1;

43. if (ir->op1 >= REF_FIRST && sink_phidep(J, ir->op1)) return 1;

44. if (ir->op2 >= REF_FIRST && sink_phidep(J, ir->op2)) return 1;

45. return 0;

46. }

48. /* Check whether a value is a sinkable PHI or loop-invariant. */

49. ‌static int sink_checkphi(jit_State *J, IRIns *ira, IRRef ref)

50. {

51. if (ref >= REF_FIRST) {

52. IRIns *ir = IR(ref);

53. if (irt_isphi(ir->t) || (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT &&

54. irt_isphi(IR(ir->op1)->t))) {

55. ira->prev++;

56. return 1;  /* Sinkable PHI. */

57. }

58. /* Otherwise the value must be loop-invariant. */

59. return ref < J->loopref && !sink_phidep(J, ref);

60. }

61. return 1;  /* Constant (non-PHI). */

62. }

64. /* Mark non-sinkable allocations using single-pass backward propagation.

65. **

66. ** Roots for the marking process are:

67. ** - Some PHIs or snapshots (see below).

68. ** - Non-PHI, non-constant values stored to PHI allocations.

69. ** - All guards.

70. ** - Any remaining loads not eliminated by store-to-load forwarding.

71. ** - Stores with non-constant keys.

72. ** - All stored values.

73. */

74. ‌static void sink_mark_ins(jit_State *J)

75. {

76. IRIns *ir, *irlast = IR(J->cur.nins-1);

77. for (ir = irlast ; ; ir--) {

78. switch (ir->o) {

79. case IR_BASE:

80. return;  /* Finished. */

81. case IR_CALLL:  /* IRCALL_lj_tab_len */

82. case IR_ALOAD: case IR_HLOAD: case IR_XLOAD: case IR_TBAR:

83. irt_setmark(IR(ir->op1)->t);  /* Mark ref for remaining loads. */

84. break;

85. case IR_FLOAD:

86. if (irt_ismarked(ir->t) || ir->op2 == IRFL_TAB_META)

87. irt_setmark(IR(ir->op1)->t);  /* Mark table for remaining loads. */

88. break;

89. case IR_ASTORE: case IR_HSTORE: case IR_FSTORE: case IR_XSTORE: {

90. IRIns *ira = sink_checkalloc(J, ir);

91. if (!ira || (irt_isphi(ira->t) && !sink_checkphi(J, ira, ir->op2)))

92. irt_setmark(IR(ir->op1)->t);  /* Mark ineligible ref. */

93. irt_setmark(IR(ir->op2)->t);  /* Mark stored value. */

94. break;

95. }

96. #if LJ_HASFFI

97. case IR_CNEWI:

98. if (irt_isphi(ir->t) &&

99. (!sink_checkphi(J, ir, ir->op2) ||

100. (LJ_32 && ir+1 < irlast && (ir+1)->o == IR_HIOP &&

101. !sink_checkphi(J, ir, (ir+1)->op2))))

102. irt_setmark(ir->t);  /* Mark ineligible allocation. */

103. /* fallthrough */

104. #endif

105. case IR_USTORE:

106. irt_setmark(IR(ir->op2)->t);  /* Mark stored value. */

107. break;

108. #if LJ_HASFFI

109. case IR_CALLXS:

110. #endif

111. case IR_CALLS:

112. irt_setmark(IR(ir->op1)->t);  /* Mark (potentially) stored values. */

113. break;

114. case IR_PHI: {

115. IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);

116. irl->prev = irr->prev = 0;  /* Clear PHI value counts. */

117. if (irl->o == irr->o &&

118. (irl->o == IR_TNEW || irl->o == IR_TDUP ||

119. (LJ_HASFFI && (irl->o == IR_CNEW || irl->o == IR_CNEWI))))

120. break;

121. irt_setmark(irl->t);

122. irt_setmark(irr->t);

123. break;

124. }

125. default:

126. if (irt_ismarked(ir->t) || irt_isguard(ir->t)) {  /* Propagate mark. */

127. if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t);

128. if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t);

129. }

130. break;

131. }

132. }

133. }

135. /* Mark all instructions referenced by a snapshot. */

136. ‌static void sink_mark_snap(jit_State *J, SnapShot *snap)

137. {

138. SnapEntry *map = &J->cur.snapmap[snap->mapofs];

139. MSize n, nent = snap->nent;

140. for (n = 0; n < nent; n++) {

141. IRRef ref = snap_ref(map[n]);

142. if (!irref_isk(ref))

143. irt_setmark(IR(ref)->t);

144. }

145. }

147. /* Iteratively remark PHI refs with differing marks or PHI value counts. */

148. ‌static void sink_remark_phi(jit_State *J)

149. {

150. IRIns *ir;

151. int remark;

152. do {

153. remark = 0;

154. for (ir = IR(J->cur.nins-1); ir->o == IR_PHI; ir--) {

155. IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);

156. if (((irl->t.irt ^ irr->t.irt) & IRT_MARK))

157. remark = 1;

158. else if (irl->prev == irr->prev)

159. continue;

160. irt_setmark(IR(ir->op1)->t);

161. irt_setmark(IR(ir->op2)->t);

162. }

163. } while (remark);

164. }

166. /* Sweep instructions and tag sunken allocations and stores. */

167. ‌static void sink_sweep_ins(jit_State *J)

168. {

169. IRIns *ir, *irfirst = IR(J->cur.nk);

170. for (ir = IR(J->cur.nins-1) ; ir >= irfirst; ir--) {

171. switch (ir->o) {

172. case IR_ASTORE: case IR_HSTORE: case IR_FSTORE: case IR_XSTORE: {

173. IRIns *ira = sink_checkalloc(J, ir);

174. if (ira && !irt_ismarked(ira->t)) {

175. int delta = (int)(ir - ira);

176. ir->prev = REGSP(RID_SINK, delta > 255 ? 255 : delta);

177. } else {

178. ir->prev = REGSP_INIT;

179. }

180. break;

181. }

182. case IR_NEWREF:

183. if (!irt_ismarked(IR(ir->op1)->t)) {

184. ir->prev = REGSP(RID_SINK, 0);

185. } else {

186. irt_clearmark(ir->t);

187. ir->prev = REGSP_INIT;

188. }

189. break;

190. #if LJ_HASFFI

191. case IR_CNEW: case IR_CNEWI:

192. #endif

193. case IR_TNEW: case IR_TDUP:

194. if (!irt_ismarked(ir->t)) {

195. ir->t.irt &= ~IRT_GUARD;

196. ir->prev = REGSP(RID_SINK, 0);

197. J->cur.sinktags = 1;  /* Signal present SINK tags to assembler. */

198. } else {

199. irt_clearmark(ir->t);

200. ir->prev = REGSP_INIT;

201. }

202. break;

203. case IR_PHI: {

204. IRIns *ira = IR(ir->op2);

205. if (!irt_ismarked(ira->t) &&

206. (ira->o == IR_TNEW || ira->o == IR_TDUP ||

207. (LJ_HASFFI && (ira->o == IR_CNEW || ira->o == IR_CNEWI)))) {

208. ir->prev = REGSP(RID_SINK, 0);

209. } else {

210. ir->prev = REGSP_INIT;

211. }

212. break;

213. }

214. default:

215. irt_clearmark(ir->t);

216. ir->prev = REGSP_INIT;

217. break;

218. }

219. }

220. }

222. /* Allocation sinking and store sinking.

223. **

224. ** 1. Mark all non-sinkable allocations.

225. ** 2. Then sink all remaining allocations and the related stores.

226. */

227. ‌void lj_opt_sink(jit_State *J)

228. {

229. const uint32_t need = (JIT_F_OPT_SINK|JIT_F_OPT_FWD|

230. JIT_F_OPT_DCE|JIT_F_OPT_CSE|JIT_F_OPT_FOLD);

231. if ((J->flags & need) == need &&

232. (J->chain[IR_TNEW] || J->chain[IR_TDUP] ||

233. (LJ_HASFFI && (J->chain[IR_CNEW] || J->chain[IR_CNEWI])))) {

234. if (!J->loopref)

235. sink_mark_snap(J, &J->cur.snap[J->cur.nsnap-1]);

236. sink_mark_ins(J);

237. if (J->loopref)

238. sink_remark_phi(J);

239. sink_sweep_ins(J);

240. }

241. }

243. ‌#undef IR

245. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_asm_arm.h - luajit-2.0-src

Global variables defined

- asm_compmap

Functions defined

- asm_add

- asm_ahustore

- asm_ahuvload

- asm_aref

- asm_bitop

- asm_bitshift

- asm_bswap

- asm_callround

- asm_callx

- asm_cnew

- asm_comp

- asm_conv

- asm_exitstub_gen

- asm_exitstub_setup

- asm_fload

- asm_fparith

- asm_fpcomp

- asm_fpmath

- asm_fpmin_max

- asm_fpunary

- asm_fref

- asm_fstore

- asm_fuseabase

- asm_fuseahuref

- asm_fuselsl2

- asm_fusemadd

- asm_fuseopm

- asm_fusexref

- asm_fxloadins

- asm_fxstoreins

- asm_gc_check

- asm_gencall

- asm_guardcc

- asm_head_lreg

- asm_head_root_base

- asm_head_side_base

- asm_hiop

- asm_href

- asm_hrefk

- asm_int64comp

- asm_intcomp

- asm_intmin_max

- asm_intmul

- asm_intneg

- asm_intop

- asm_intop_s

- asm_loop_fixup

- asm_min_max

- asm_mul

- asm_neg

- asm_obar

- asm_prof

- asm_retf

- asm_setup_call_slots

- asm_setup_target

- asm_setupresult

- asm_sfpcomp

- asm_sfpmin_max

- asm_sload

- asm_stack_check

- asm_stack_restore

- asm_strref

- asm_strto

- asm_sub

- asm_swapops

- asm_tail_fixup

- asm_tail_prep

- asm_tbar

- asm_tobit

- asm_tointg

- asm_tvptr

- asm_uref

- asm_xload

- asm_xstore_

- lj_asm_patchexit

- noconflict

- ra_alloc2

- ra_hintalloc

- ra_scratchpair

Macros defined

- CONFLICT_SEARCH_LIM

- asm_abs

- asm_abs

- asm_addov

- asm_atan2

- asm_atan2

- asm_band

- asm_bnot

- asm_bor

- asm_brol

- asm_bror

- asm_bsar

- asm_bshl

- asm_bshr

- asm_bxor

- asm_cnew

- asm_div

- asm_div

- asm_equal

- asm_fpmath

- asm_ldexp

- asm_ldexp

- asm_max

- asm_min

- asm_mod

- asm_mulov

- asm_pow

- asm_pow

- asm_subov

- asm_tobit

- asm_xstore

Source code

1. /*

2. ** ARM IR assembler (SSA IR -> machine code).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* -- Register allocator extensions --------------------------------------- */

8. /* Allocate a register with a hint. */

9. ‌static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)

10. {

11. Reg r = IR(ref)->r;

12. if (ra_noreg(r)) {

13. if (!ra_hashint(r) && !iscrossref(as, ref))

14. ra_sethint(IR(ref)->r, hint);  /* Propagate register hint. */

15. r = ra_allocref(as, ref, allow);

16. }

17. ra_noweak(as, r);

18. return r;

19. }

21. /* Allocate a scratch register pair. */

22. ‌static Reg ra_scratchpair(ASMState *as, RegSet allow)

23. {

24. RegSet pick1 = as->freeset & allow;

25. RegSet pick2 = pick1 & (pick1 >> 1) & RSET_GPREVEN;

26. Reg r;

27. if (pick2) {

28. r = rset_picktop(pick2);

29. } else {

30. RegSet pick = pick1 & (allow >> 1) & RSET_GPREVEN;

31. if (pick) {

32. r = rset_picktop(pick);

33. ra_restore(as, regcost_ref(as->cost[r+1]));

34. } else {

35. pick = pick1 & (allow << 1) & RSET_GPRODD;

36. if (pick) {

37. r = ra_restore(as, regcost_ref(as->cost[rset_picktop(pick)-1]));

38. } else {

39. r = ra_evict(as, allow & (allow >> 1) & RSET_GPREVEN);

40. ra_restore(as, regcost_ref(as->cost[r+1]));

41. }

42. }

43. }

44. lua_assert(rset_test(RSET_GPREVEN, r));

45. ra_modified(as, r);

46. ra_modified(as, r+1);

47. RA_DBGX((as, "scratchpair    $r $r", r, r+1));

48. return r;

49. }

51. #if !LJ_SOFTFP

52. /* Allocate two source registers for three-operand instructions. */

53. ‌static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)

54. {

55. IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);

56. Reg left = irl->r, right = irr->r;

57. if (ra_hasreg(left)) {

58. ra_noweak(as, left);

59. if (ra_noreg(right))

60. right = ra_allocref(as, ir->op2, rset_exclude(allow, left));

61. else

62. ra_noweak(as, right);

63. } else if (ra_hasreg(right)) {

64. ra_noweak(as, right);

65. left = ra_allocref(as, ir->op1, rset_exclude(allow, right));

66. } else if (ra_hashint(right)) {

67. right = ra_allocref(as, ir->op2, allow);

68. left = ra_alloc1(as, ir->op1, rset_exclude(allow, right));

69. } else {

70. left = ra_allocref(as, ir->op1, allow);

71. right = ra_alloc1(as, ir->op2, rset_exclude(allow, left));

72. }

73. return left | (right << 8);

74. }

75. #endif

77. /* -- Guard handling ------------------------------------------------------ */

79. /* Generate an exit stub group at the bottom of the reserved MCode memory. */

80. ‌static MCode *asm_exitstub_gen(ASMState *as, ExitNo group)

81. {

82. MCode *mxp = as->mcbot;

83. int i;

84. if (mxp + 4*4+4*EXITSTUBS_PER_GROUP >= as->mctop)

85. asm_mclimit(as);

86. /* str lr, [sp]; bl ->vm_exit_handler; .long DISPATCH_address, group. */

87. *mxp++ = ARMI_STR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_LR)|ARMF_N(RID_SP);

88. *mxp = ARMI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)-2)&0x00ffffffu);

89. mxp++;

90. *mxp++ = (MCode)i32ptr(J2GG(as->J)->dispatch);  /* DISPATCH address */

91. *mxp++ = group*EXITSTUBS_PER_GROUP;

92. for (i = 0; i < EXITSTUBS_PER_GROUP; i++)

93. *mxp++ = ARMI_B|((-6-i)&0x00ffffffu);

94. lj_mcode_sync(as->mcbot, mxp);

95. lj_mcode_commitbot(as->J, mxp);

96. as->mcbot = mxp;

97. as->mclim = as->mcbot + MCLIM_REDZONE;

98. return mxp - EXITSTUBS_PER_GROUP;

99. }

101. /* Setup all needed exit stubs. */

102. ‌static void asm_exitstub_setup(ASMState *as, ExitNo nexits)

103. {

104. ExitNo i;

105. if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR)

106. lj_trace_err(as->J, LJ_TRERR_SNAPOV);

107. for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++)

108. if (as->J->exitstubgroup[i] == NULL)

109. as->J->exitstubgroup[i] = asm_exitstub_gen(as, i);

110. }

112. /* Emit conditional branch to exit for guard. */

113. ‌static void asm_guardcc(ASMState *as, ARMCC cc)

114. {

115. MCode *target = exitstub_addr(as->J, as->snapno);

116. MCode *p = as->mcp;

117. if (LJ_UNLIKELY(p == as->invmcp)) {

118. as->loopinv = 1;

119. *p = ARMI_BL | ((target-p-2) & 0x00ffffffu);

120. emit_branch(as, ARMF_CC(ARMI_B, cc^1), p+1);

121. return;

122. }

123. emit_branch(as, ARMF_CC(ARMI_BL, cc), target);

124. }

126. /* -- Operand fusion ------------------------------------------------------ */

128. /* Limit linear search to this distance. Avoids O(n^2) behavior. */

129. ‌#define CONFLICT_SEARCH_LIM        31

131. /* Check if there's no conflicting instruction between curins and ref. */

132. ‌static int noconflict(ASMState *as, IRRef ref, IROp conflict)

133. {

134. IRIns *ir = as->ir;

135. IRRef i = as->curins;

136. if (i > ref + CONFLICT_SEARCH_LIM)

137. return 0;  /* Give up, ref is too far away. */

138. while (--i > ref)

139. if (ir[i].o == conflict)

140. return 0;  /* Conflict found. */

141. return 1;  /* Ok, no conflict. */

142. }

144. /* Fuse the array base of colocated arrays. */

145. ‌static int32_t asm_fuseabase(ASMState *as, IRRef ref)

146. {

147. IRIns *ir = IR(ref);

148. if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&

149. !neverfuse(as) && noconflict(as, ref, IR_NEWREF))

150. return (int32_t)sizeof(GCtab);

151. return 0;

152. }

154. /* Fuse array/hash/upvalue reference into register+offset operand. */

155. ‌static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow,

156. int lim)

157. {

158. IRIns *ir = IR(ref);

159. if (ra_noreg(ir->r)) {

160. if (ir->o == IR_AREF) {

161. if (mayfuse(as, ref)) {

162. if (irref_isk(ir->op2)) {

163. IRRef tab = IR(ir->op1)->op1;

164. int32_t ofs = asm_fuseabase(as, tab);

165. IRRef refa = ofs ? tab : ir->op1;

166. ofs += 8*IR(ir->op2)->i;

167. if (ofs > -lim && ofs < lim) {

168. *ofsp = ofs;

169. return ra_alloc1(as, refa, allow);

170. }

171. }

172. }

173. } else if (ir->o == IR_HREFK) {

174. if (mayfuse(as, ref)) {

175. int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));

176. if (ofs < lim) {

177. *ofsp = ofs;

178. return ra_alloc1(as, ir->op1, allow);

179. }

180. }

181. } else if (ir->o == IR_UREFC) {

182. if (irref_isk(ir->op1)) {

183. GCfunc *fn = ir_kfunc(IR(ir->op1));

184. int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv);

185. *ofsp = (ofs & 255);  /* Mask out less bits to allow LDRD. */

186. return ra_allock(as, (ofs & ~255), allow);

187. }

188. }

189. }

190. *ofsp = 0;

191. return ra_alloc1(as, ref, allow);

192. }

194. /* Fuse m operand into arithmetic/logic instructions. */

195. ‌static uint32_t asm_fuseopm(ASMState *as, ARMIns ai, IRRef ref, RegSet allow)

196. {

197. IRIns *ir = IR(ref);

198. if (ra_hasreg(ir->r)) {

199. ra_noweak(as, ir->r);

200. return ARMF_M(ir->r);

201. } else if (irref_isk(ref)) {

202. uint32_t k = emit_isk12(ai, ir->i);

203. if (k)

204. return k;

205. } else if (mayfuse(as, ref)) {

206. if (ir->o >= IR_BSHL && ir->o <= IR_BROR) {

207. Reg m = ra_alloc1(as, ir->op1, allow);

208. ARMShift sh = ir->o == IR_BSHL ? ARMSH_LSL :

209. ir->o == IR_BSHR ? ARMSH_LSR :

210. ir->o == IR_BSAR ? ARMSH_ASR : ARMSH_ROR;

211. if (irref_isk(ir->op2)) {

212. return m | ARMF_SH(sh, (IR(ir->op2)->i & 31));

213. } else {

214. Reg s = ra_alloc1(as, ir->op2, rset_exclude(allow, m));

215. return m | ARMF_RSH(sh, s);

216. }

217. } else if (ir->o == IR_ADD && ir->op1 == ir->op2) {

218. Reg m = ra_alloc1(as, ir->op1, allow);

219. return m | ARMF_SH(ARMSH_LSL, 1);

220. }

221. }

222. return ra_allocref(as, ref, allow);

223. }

225. /* Fuse shifts into loads/stores. Only bother with BSHL 2 => lsl #2. */

226. ‌static IRRef asm_fuselsl2(ASMState *as, IRRef ref)

227. {

228. IRIns *ir = IR(ref);

229. if (ra_noreg(ir->r) && mayfuse(as, ref) && ir->o == IR_BSHL &&

230. irref_isk(ir->op2) && IR(ir->op2)->i == 2)

231. return ir->op1;

232. return 0;  /* No fusion. */

233. }

235. /* Fuse XLOAD/XSTORE reference into load/store operand. */

236. ‌static void asm_fusexref(ASMState *as, ARMIns ai, Reg rd, IRRef ref,

237. RegSet allow, int32_t ofs)

238. {

239. IRIns *ir = IR(ref);

240. Reg base;

241. if (ra_noreg(ir->r) && canfuse(as, ir)) {

242. int32_t lim = (!LJ_SOFTFP && (ai & 0x08000000)) ? 1024 :

243. (ai & 0x04000000) ? 4096 : 256;

244. if (ir->o == IR_ADD) {

245. int32_t ofs2;

246. if (irref_isk(ir->op2) &&

247. (ofs2 = ofs + IR(ir->op2)->i) > -lim && ofs2 < lim &&

248. (!(!LJ_SOFTFP && (ai & 0x08000000)) || !(ofs2 & 3))) {

249. ofs = ofs2;

250. ref = ir->op1;

251. } else if (ofs == 0 && !(!LJ_SOFTFP && (ai & 0x08000000))) {

252. IRRef lref = ir->op1, rref = ir->op2;

253. Reg rn, rm;

254. if ((ai & 0x04000000)) {

255. IRRef sref = asm_fuselsl2(as, rref);

256. if (sref) {

257. rref = sref;

258. ai |= ARMF_SH(ARMSH_LSL, 2);

259. } else if ((sref = asm_fuselsl2(as, lref)) != 0) {

260. lref = rref;

261. rref = sref;

262. ai |= ARMF_SH(ARMSH_LSL, 2);

263. }

264. }

265. rn = ra_alloc1(as, lref, allow);

266. rm = ra_alloc1(as, rref, rset_exclude(allow, rn));

267. if ((ai & 0x04000000)) ai |= ARMI_LS_R;

268. emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm);

269. return;

270. }

271. } else if (ir->o == IR_STRREF && !(!LJ_SOFTFP && (ai & 0x08000000))) {

272. lua_assert(ofs == 0);

273. ofs = (int32_t)sizeof(GCstr);

274. if (irref_isk(ir->op2)) {

275. ofs += IR(ir->op2)->i;

276. ref = ir->op1;

277. } else if (irref_isk(ir->op1)) {

278. ofs += IR(ir->op1)->i;

279. ref = ir->op2;

280. } else {

281. /* NYI: Fuse ADD with constant. */

282. Reg rn = ra_alloc1(as, ir->op1, allow);

283. uint32_t m = asm_fuseopm(as, 0, ir->op2, rset_exclude(allow, rn));

284. if ((ai & 0x04000000))

285. emit_lso(as, ai, rd, rd, ofs);

286. else

287. emit_lsox(as, ai, rd, rd, ofs);

288. emit_dn(as, ARMI_ADD^m, rd, rn);

289. return;

290. }

291. if (ofs <= -lim || ofs >= lim) {

292. Reg rn = ra_alloc1(as, ref, allow);

293. Reg rm = ra_allock(as, ofs, rset_exclude(allow, rn));

294. if ((ai & 0x04000000)) ai |= ARMI_LS_R;

295. emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm);

296. return;

297. }

298. }

299. }

300. base = ra_alloc1(as, ref, allow);

301. #if !LJ_SOFTFP

302. if ((ai & 0x08000000))

303. emit_vlso(as, ai, rd, base, ofs);

304. else

305. #endif

306. if ((ai & 0x04000000))

307. emit_lso(as, ai, rd, base, ofs);

308. else

309. emit_lsox(as, ai, rd, base, ofs);

310. }

312. #if !LJ_SOFTFP

313. /* Fuse to multiply-add/sub instruction. */

314. ‌static int asm_fusemadd(ASMState *as, IRIns *ir, ARMIns ai, ARMIns air)

315. {

316. IRRef lref = ir->op1, rref = ir->op2;

317. IRIns *irm;

318. if (lref != rref &&

319. ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) &&

320. ra_noreg(irm->r)) ||

321. (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) &&

322. (rref = lref, ai = air, ra_noreg(irm->r))))) {

323. Reg dest = ra_dest(as, ir, RSET_FPR);

324. Reg add = ra_hintalloc(as, rref, dest, RSET_FPR);

325. Reg right, left = ra_alloc2(as, irm,

326. rset_exclude(rset_exclude(RSET_FPR, dest), add));

327. right = (left >> 8); left &= 255;

328. emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15));

329. if (dest != add) emit_dm(as, ARMI_VMOV_D, (dest & 15), (add & 15));

330. return 1;

331. }

332. return 0;

333. }

334. #endif

336. /* -- Calls --------------------------------------------------------------- */

338. /* Generate a call to a C function. */

339. ‌static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)

340. {

341. uint32_t n, nargs = CCI_XNARGS(ci);

342. int32_t ofs = 0;

343. #if LJ_SOFTFP

344. Reg gpr = REGARG_FIRSTGPR;

345. #else

346. Reg gpr, fpr = REGARG_FIRSTFPR, fprodd = 0;

347. #endif

348. if ((void *)ci->func)

349. emit_call(as, (void *)ci->func);

350. #if !LJ_SOFTFP

351. for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++)

352. as->cost[gpr] = REGCOST(~0u, ASMREF_L);

353. gpr = REGARG_FIRSTGPR;

354. #endif

355. for (n = 0; n < nargs; n++) {  /* Setup args. */

356. IRRef ref = args[n];

357. IRIns *ir = IR(ref);

358. #if !LJ_SOFTFP

359. if (ref && irt_isfp(ir->t)) {

360. RegSet of = as->freeset;

361. Reg src;

362. if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) {

363. if (irt_isnum(ir->t)) {

364. if (fpr <= REGARG_LASTFPR) {

365. ra_leftov(as, fpr, ref);

366. fpr++;

367. continue;

368. }

369. } else if (fprodd) {  /* Ick. */

370. src = ra_alloc1(as, ref, RSET_FPR);

371. emit_dm(as, ARMI_VMOV_S, (fprodd & 15), (src & 15) | 0x00400000);

372. fprodd = 0;

373. continue;

374. } else if (fpr <= REGARG_LASTFPR) {

375. ra_leftov(as, fpr, ref);

376. fprodd = fpr++;

377. continue;

378. }

379. /* Workaround to protect argument GPRs from being used for remat. */

380. as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1);

381. src = ra_alloc1(as, ref, RSET_FPR);  /* May alloc GPR to remat FPR. */

382. as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1));

383. fprodd = 0;

384. goto stackfp;

385. }

386. /* Workaround to protect argument GPRs from being used for remat. */

387. as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1);

388. src = ra_alloc1(as, ref, RSET_FPR);  /* May alloc GPR to remat FPR. */

389. as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1));

390. if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1u;

391. if (gpr <= REGARG_LASTGPR) {

392. lua_assert(rset_test(as->freeset, gpr));  /* Must have been evicted. */

393. if (irt_isnum(ir->t)) {

394. lua_assert(rset_test(as->freeset, gpr+1));  /* Ditto. */

395. emit_dnm(as, ARMI_VMOV_RR_D, gpr, gpr+1, (src & 15));

396. gpr += 2;

397. } else {

398. emit_dn(as, ARMI_VMOV_R_S, gpr, (src & 15));

399. gpr++;

400. }

401. } else {

402. stackfp:

403. if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;

404. emit_spstore(as, ir, src, ofs);

405. ofs += irt_isnum(ir->t) ? 8 : 4;

406. }

407. } else

408. #endif

409. {

410. if (gpr <= REGARG_LASTGPR) {

411. lua_assert(rset_test(as->freeset, gpr));  /* Must have been evicted. */

412. if (ref) ra_leftov(as, gpr, ref);

413. gpr++;

414. } else {

415. if (ref) {

416. Reg r = ra_alloc1(as, ref, RSET_GPR);

417. emit_spstore(as, ir, r, ofs);

418. }

419. ofs += 4;

420. }

421. }

422. }

423. }

425. /* Setup result reg/sp for call. Evict scratch regs. */

426. ‌static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)

427. {

428. RegSet drop = RSET_SCRATCH;

429. int hiop = ((ir+1)->o == IR_HIOP);

430. if (ra_hasreg(ir->r))

431. rset_clear(drop, ir->r);  /* Dest reg handled below. */

432. if (hiop && ra_hasreg((ir+1)->r))

433. rset_clear(drop, (ir+1)->r);  /* Dest reg handled below. */

434. ra_evictset(as, drop);  /* Evictions must be performed first. */

435. if (ra_used(ir)) {

436. lua_assert(!irt_ispri(ir->t));

437. if (!LJ_SOFTFP && irt_isfp(ir->t)) {

438. if (LJ_ABI_SOFTFP || (ci->flags & (CCI_CASTU64|CCI_VARARG))) {

439. Reg dest = (ra_dest(as, ir, RSET_FPR) & 15);

440. if (irt_isnum(ir->t))

441. emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, dest);

442. else

443. emit_dn(as, ARMI_VMOV_S_R, RID_RET, dest);

444. } else {

445. ra_destreg(as, ir, RID_FPRET);

446. }

447. } else if (hiop) {

448. ra_destpair(as, ir);

449. } else {

450. ra_destreg(as, ir, RID_RET);

451. }

452. }

453. UNUSED(ci);

454. }

456. ‌static void asm_callx(ASMState *as, IRIns *ir)

457. {

458. IRRef args[CCI_NARGS_MAX*2];

459. CCallInfo ci;

460. IRRef func;

461. IRIns *irf;

462. ci.flags = asm_callx_flags(as, ir);

463. asm_collectargs(as, ir, &ci, args);

464. asm_setupresult(as, ir, &ci);

465. func = ir->op2; irf = IR(func);

466. if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }

467. if (irref_isk(func)) {  /* Call to constant address. */

468. ci.func = (ASMFunction)(void *)(irf->i);

469. } else {  /* Need a non-argument register for indirect calls. */

470. Reg freg = ra_alloc1(as, func, RSET_RANGE(RID_R4, RID_R12+1));

471. emit_m(as, ARMI_BLXr, freg);

472. ci.func = (ASMFunction)(void *)0;

473. }

474. asm_gencall(as, &ci, args);

475. }

477. /* -- Returns ------------------------------------------------------------- */

479. /* Return to lower frame. Guard that it goes to the right spot. */

480. ‌static void asm_retf(ASMState *as, IRIns *ir)

481. {

482. Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);

483. void *pc = ir_kptr(IR(ir->op2));

484. int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));

485. as->topslot -= (BCReg)delta;

486. if ((int32_t)as->topslot < 0) as->topslot = 0;

487. irt_setmark(IR(REF_BASE)->t);  /* Children must not coalesce with BASE reg. */

488. /* Need to force a spill on REF_BASE now to update the stack slot. */

489. emit_lso(as, ARMI_STR, base, RID_SP, ra_spill(as, IR(REF_BASE)));

490. emit_setgl(as, base, jit_base);

491. emit_addptr(as, base, -8*delta);

492. asm_guardcc(as, CC_NE);

493. emit_nm(as, ARMI_CMP, RID_TMP,

494. ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base)));

495. emit_lso(as, ARMI_LDR, RID_TMP, base, -4);

496. }

498. /* -- Type conversions ---------------------------------------------------- */

500. #if !LJ_SOFTFP

501. ‌static void asm_tointg(ASMState *as, IRIns *ir, Reg left)

502. {

503. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));

504. Reg dest = ra_dest(as, ir, RSET_GPR);

505. asm_guardcc(as, CC_NE);

506. emit_d(as, ARMI_VMRS, 0);

507. emit_dm(as, ARMI_VCMP_D, (tmp & 15), (left & 15));

508. emit_dm(as, ARMI_VCVT_F64_S32, (tmp & 15), (tmp & 15));

509. emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15));

510. emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (left & 15));

511. }

513. ‌static void asm_tobit(ASMState *as, IRIns *ir)

514. {

515. RegSet allow = RSET_FPR;

516. Reg left = ra_alloc1(as, ir->op1, allow);

517. Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));

518. Reg tmp = ra_scratch(as, rset_clear(allow, right));

519. Reg dest = ra_dest(as, ir, RSET_GPR);

520. emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15));

521. emit_dnm(as, ARMI_VADD_D, (tmp & 15), (left & 15), (right & 15));

522. }

523. #else

524. ‌#define asm_tobit(as, ir)        lua_assert(0)

525. #endif

527. ‌static void asm_conv(ASMState *as, IRIns *ir)

528. {

529. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);

530. #if !LJ_SOFTFP

531. int stfp = (st == IRT_NUM || st == IRT_FLOAT);

532. #endif

533. IRRef lref = ir->op1;

534. /* 64 bit integer conversions are handled by SPLIT. */

535. lua_assert(!irt_isint64(ir->t) && !(st == IRT_I64 || st == IRT_U64));

536. #if LJ_SOFTFP

537. /* FP conversions are handled by SPLIT. */

538. lua_assert(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT));

539. /* Can't check for same types: SPLIT uses CONV int.int + BXOR for sfp NEG. */

540. #else

541. lua_assert(irt_type(ir->t) != st);

542. if (irt_isfp(ir->t)) {

543. Reg dest = ra_dest(as, ir, RSET_FPR);

544. if (stfp) {  /* FP to FP conversion. */

545. emit_dm(as, st == IRT_NUM ? ARMI_VCVT_F32_F64 : ARMI_VCVT_F64_F32,

546. (dest & 15), (ra_alloc1(as, lref, RSET_FPR) & 15));

547. } else {  /* Integer to FP conversion. */

548. Reg left = ra_alloc1(as, lref, RSET_GPR);

549. ARMIns ai = irt_isfloat(ir->t) ?

550. (st == IRT_INT ? ARMI_VCVT_F32_S32 : ARMI_VCVT_F32_U32) :

551. (st == IRT_INT ? ARMI_VCVT_F64_S32 : ARMI_VCVT_F64_U32);

552. emit_dm(as, ai, (dest & 15), (dest & 15));

553. emit_dn(as, ARMI_VMOV_S_R, left, (dest & 15));

554. }

555. } else if (stfp) {  /* FP to integer conversion. */

556. if (irt_isguard(ir->t)) {

557. /* Checked conversions are only supported from number to int. */

558. lua_assert(irt_isint(ir->t) && st == IRT_NUM);

559. asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));

560. } else {

561. Reg left = ra_alloc1(as, lref, RSET_FPR);

562. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));

563. Reg dest = ra_dest(as, ir, RSET_GPR);

564. ARMIns ai;

565. emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15));

566. ai = irt_isint(ir->t) ?

567. (st == IRT_NUM ? ARMI_VCVT_S32_F64 : ARMI_VCVT_S32_F32) :

568. (st == IRT_NUM ? ARMI_VCVT_U32_F64 : ARMI_VCVT_U32_F32);

569. emit_dm(as, ai, (tmp & 15), (left & 15));

570. }

571. } else

572. #endif

573. {

574. Reg dest = ra_dest(as, ir, RSET_GPR);

575. if (st >= IRT_I8 && st <= IRT_U16) {  /* Extend to 32 bit integer. */

576. Reg left = ra_alloc1(as, lref, RSET_GPR);

577. lua_assert(irt_isint(ir->t) || irt_isu32(ir->t));

578. if ((as->flags & JIT_F_ARMV6)) {

579. ARMIns ai = st == IRT_I8 ? ARMI_SXTB :

580. st == IRT_U8 ? ARMI_UXTB :

581. st == IRT_I16 ? ARMI_SXTH : ARMI_UXTH;

582. emit_dm(as, ai, dest, left);

583. } else if (st == IRT_U8) {

584. emit_dn(as, ARMI_AND|ARMI_K12|255, dest, left);

585. } else {

586. uint32_t shift = st == IRT_I8 ? 24 : 16;

587. ARMShift sh = st == IRT_U16 ? ARMSH_LSR : ARMSH_ASR;

588. emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, RID_TMP);

589. emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_LSL, shift), RID_TMP, left);

590. }

591. } else {  /* Handle 32/32 bit no-op (cast). */

592. ra_leftov(as, dest, lref);  /* Do nothing, but may need to move regs. */

593. }

594. }

595. }

597. ‌static void asm_strto(ASMState *as, IRIns *ir)

598. {

599. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];

600. IRRef args[2];

601. Reg rlo = 0, rhi = 0, tmp;

602. int destused = ra_used(ir);

603. int32_t ofs = 0;

604. ra_evictset(as, RSET_SCRATCH);

605. #if LJ_SOFTFP

606. if (destused) {

607. if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) &&

608. (ir->s & 1) == 0 && ir->s + 1 == (ir+1)->s) {

609. int i;

610. for (i = 0; i < 2; i++) {

611. Reg r = (ir+i)->r;

612. if (ra_hasreg(r)) {

613. ra_free(as, r);

614. ra_modified(as, r);

615. emit_spload(as, ir+i, r, sps_scale((ir+i)->s));

616. }

617. }

618. ofs = sps_scale(ir->s);

619. destused = 0;

620. } else {

621. rhi = ra_dest(as, ir+1, RSET_GPR);

622. rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi));

623. }

624. }

625. asm_guardcc(as, CC_EQ);

626. if (destused) {

627. emit_lso(as, ARMI_LDR, rhi, RID_SP, 4);

628. emit_lso(as, ARMI_LDR, rlo, RID_SP, 0);

629. }

630. #else

631. UNUSED(rhi);

632. if (destused) {

633. if (ra_hasspill(ir->s)) {

634. ofs = sps_scale(ir->s);

635. destused = 0;

636. if (ra_hasreg(ir->r)) {

637. ra_free(as, ir->r);

638. ra_modified(as, ir->r);

639. emit_spload(as, ir, ir->r, ofs);

640. }

641. } else {

642. rlo = ra_dest(as, ir, RSET_FPR);

643. }

644. }

645. asm_guardcc(as, CC_EQ);

646. if (destused)

647. emit_vlso(as, ARMI_VLDR_D, rlo, RID_SP, 0);

648. #endif

649. emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET);  /* Test return status. */

650. args[0] = ir->op1;      /* GCstr *str */

651. args[1] = ASMREF_TMP1;  /* TValue *n  */

652. asm_gencall(as, ci, args);

653. tmp = ra_releasetmp(as, ASMREF_TMP1);

654. if (ofs == 0)

655. emit_dm(as, ARMI_MOV, tmp, RID_SP);

656. else

657. emit_opk(as, ARMI_ADD, tmp, RID_SP, ofs, RSET_GPR);

658. }

660. /* -- Memory references --------------------------------------------------- */

662. /* Get pointer to TValue. */

663. ‌static void asm_tvptr(ASMState *as, Reg dest, IRRef ref)

664. {

665. IRIns *ir = IR(ref);

666. if (irt_isnum(ir->t)) {

667. if (irref_isk(ref)) {

668. /* Use the number constant itself as a TValue. */

669. ra_allockreg(as, i32ptr(ir_knum(ir)), dest);

670. } else {

671. #if LJ_SOFTFP

672. lua_assert(0);

673. #else

674. /* Otherwise force a spill and use the spill slot. */

675. emit_opk(as, ARMI_ADD, dest, RID_SP, ra_spill(as, ir), RSET_GPR);

676. #endif

677. }

678. } else {

679. /* Otherwise use [sp] and [sp+4] to hold the TValue. */

680. RegSet allow = rset_exclude(RSET_GPR, dest);

681. Reg type;

682. emit_dm(as, ARMI_MOV, dest, RID_SP);

683. if (!irt_ispri(ir->t)) {

684. Reg src = ra_alloc1(as, ref, allow);

685. emit_lso(as, ARMI_STR, src, RID_SP, 0);

686. }

687. if (LJ_SOFTFP && (ir+1)->o == IR_HIOP)

688. type = ra_alloc1(as, ref+1, allow);

689. else

690. type = ra_allock(as, irt_toitype(ir->t), allow);

691. emit_lso(as, ARMI_STR, type, RID_SP, 4);

692. }

693. }

695. ‌static void asm_aref(ASMState *as, IRIns *ir)

696. {

697. Reg dest = ra_dest(as, ir, RSET_GPR);

698. Reg idx, base;

699. if (irref_isk(ir->op2)) {

700. IRRef tab = IR(ir->op1)->op1;

701. int32_t ofs = asm_fuseabase(as, tab);

702. IRRef refa = ofs ? tab : ir->op1;

703. uint32_t k = emit_isk12(ARMI_ADD, ofs + 8*IR(ir->op2)->i);

704. if (k) {

705. base = ra_alloc1(as, refa, RSET_GPR);

706. emit_dn(as, ARMI_ADD^k, dest, base);

707. return;

708. }

709. }

710. base = ra_alloc1(as, ir->op1, RSET_GPR);

711. idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));

712. emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, base, idx);

713. }

715. /* Inlined hash lookup. Specialized for key type and for const keys.

716. ** The equivalent C code is:

717. **   Node *n = hashkey(t, key);

718. **   do {

719. **     if (lj_obj_equal(&n->key, key)) return &n->val;

720. **   } while ((n = nextnode(n)));

721. **   return niltv(L);

722. */

723. ‌static void asm_href(ASMState *as, IRIns *ir, IROp merge)

724. {

725. RegSet allow = RSET_GPR;

726. int destused = ra_used(ir);

727. Reg dest = ra_dest(as, ir, allow);

728. Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));

729. Reg key = 0, keyhi = 0, keynumhi = RID_NONE, tmp = RID_TMP;

730. IRRef refkey = ir->op2;

731. IRIns *irkey = IR(refkey);

732. IRType1 kt = irkey->t;

733. int32_t k = 0, khi = emit_isk12(ARMI_CMP, irt_toitype(kt));

734. uint32_t khash;

735. MCLabel l_end, l_loop;

736. rset_clear(allow, tab);

737. if (!irref_isk(refkey) || irt_isstr(kt)) {

738. #if LJ_SOFTFP

739. key = ra_alloc1(as, refkey, allow);

740. rset_clear(allow, key);

741. if (irkey[1].o == IR_HIOP) {

742. if (ra_hasreg((irkey+1)->r)) {

743. keynumhi = (irkey+1)->r;

744. keyhi = RID_TMP;

745. ra_noweak(as, keynumhi);

746. } else {

747. keyhi = keynumhi = ra_allocref(as, refkey+1, allow);

748. }

749. rset_clear(allow, keynumhi);

750. khi = 0;

751. }

752. #else

753. if (irt_isnum(kt)) {

754. key = ra_scratch(as, allow);

755. rset_clear(allow, key);

756. keyhi = keynumhi = ra_scratch(as, allow);

757. rset_clear(allow, keyhi);

758. khi = 0;

759. } else {

760. key = ra_alloc1(as, refkey, allow);

761. rset_clear(allow, key);

762. }

763. #endif

764. } else if (irt_isnum(kt)) {

765. int32_t val = (int32_t)ir_knum(irkey)->u32.lo;

766. k = emit_isk12(ARMI_CMP, val);

767. if (!k) {

768. key = ra_allock(as, val, allow);

769. rset_clear(allow, key);

770. }

771. val = (int32_t)ir_knum(irkey)->u32.hi;

772. khi = emit_isk12(ARMI_CMP, val);

773. if (!khi) {

774. keyhi = ra_allock(as, val, allow);

775. rset_clear(allow, keyhi);

776. }

777. } else if (!irt_ispri(kt)) {

778. k = emit_isk12(ARMI_CMP, irkey->i);

779. if (!k) {

780. key = ra_alloc1(as, refkey, allow);

781. rset_clear(allow, key);

782. }

783. }

784. if (!irt_ispri(kt))

785. tmp = ra_scratchpair(as, allow);

787. /* Key not found in chain: jump to exit (if merged) or load niltv. */

788. l_end = emit_label(as);

789. as->invmcp = NULL;

790. if (merge == IR_NE)

791. asm_guardcc(as, CC_AL);

792. else if (destused)

793. emit_loada(as, dest, niltvg(J2G(as->J)));

795. /* Follow hash chain until the end. */

796. l_loop = --as->mcp;

797. emit_n(as, ARMI_CMP|ARMI_K12|0, dest);

798. emit_lso(as, ARMI_LDR, dest, dest, (int32_t)offsetof(Node, next));

800. /* Type and value comparison. */

801. if (merge == IR_EQ)

802. asm_guardcc(as, CC_EQ);

803. else

804. emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end);

805. if (!irt_ispri(kt)) {

806. emit_nm(as, ARMF_CC(ARMI_CMP, CC_EQ)^k, tmp, key);

807. emit_nm(as, ARMI_CMP^khi, tmp+1, keyhi);

808. emit_lsox(as, ARMI_LDRD, tmp, dest, (int32_t)offsetof(Node, key));

809. } else {

810. emit_n(as, ARMI_CMP^khi, tmp);

811. emit_lso(as, ARMI_LDR, tmp, dest, (int32_t)offsetof(Node, key.it));

812. }

813. *l_loop = ARMF_CC(ARMI_B, CC_NE) | ((as->mcp-l_loop-2) & 0x00ffffffu);

815. /* Load main position relative to tab->node into dest. */

816. khash = irref_isk(refkey) ? ir_khash(irkey) : 1;

817. if (khash == 0) {

818. emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node));

819. } else {

820. emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, dest, tmp);

821. emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 1), tmp, tmp, tmp);

822. if (irt_isstr(kt)) {  /* Fetch of str->hash is cheaper than ra_allock. */

823. emit_dnm(as, ARMI_AND, tmp, tmp+1, RID_TMP);

824. emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node));

825. emit_lso(as, ARMI_LDR, tmp+1, key, (int32_t)offsetof(GCstr, hash));

826. emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask));

827. } else if (irref_isk(refkey)) {

828. emit_opk(as, ARMI_AND, tmp, RID_TMP, (int32_t)khash,

829. rset_exclude(rset_exclude(RSET_GPR, tab), dest));

830. emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node));

831. emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask));

832. } else {  /* Must match with hash*() in lj_tab.c. */

833. if (ra_hasreg(keynumhi)) {  /* Canonicalize +-0.0 to 0.0. */

834. if (keyhi == RID_TMP)

835. emit_dm(as, ARMF_CC(ARMI_MOV, CC_NE), keyhi, keynumhi);

836. emit_d(as, ARMF_CC(ARMI_MOV, CC_EQ)|ARMI_K12|0, keyhi);

837. }

838. emit_dnm(as, ARMI_AND, tmp, tmp, RID_TMP);

839. emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT3), tmp, tmp, tmp+1);

840. emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node));

841. emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 32-((HASH_ROT2+HASH_ROT1)&31)),

842. tmp, tmp+1, tmp);

843. emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask));

844. emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT1), tmp+1, tmp+1, tmp);

845. if (ra_hasreg(keynumhi)) {

846. emit_dnm(as, ARMI_EOR, tmp+1, tmp, key);

847. emit_dnm(as, ARMI_ORR|ARMI_S, RID_TMP, tmp, key);  /* Test for +-0.0. */

848. emit_dnm(as, ARMI_ADD, tmp, keynumhi, keynumhi);

849. #if !LJ_SOFTFP

850. emit_dnm(as, ARMI_VMOV_RR_D, key, keynumhi,

851. (ra_alloc1(as, refkey, RSET_FPR) & 15));

852. #endif

853. } else {

854. emit_dnm(as, ARMI_EOR, tmp+1, tmp, key);

855. emit_opk(as, ARMI_ADD, tmp, key, (int32_t)HASH_BIAS,

856. rset_exclude(rset_exclude(RSET_GPR, tab), key));

857. }

858. }

859. }

860. }

862. ‌static void asm_hrefk(ASMState *as, IRIns *ir)

863. {

864. IRIns *kslot = IR(ir->op2);

865. IRIns *irkey = IR(kslot->op1);

866. int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));

867. int32_t kofs = ofs + (int32_t)offsetof(Node, key);

868. Reg dest = (ra_used(ir) || ofs > 4095) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;

869. Reg node = ra_alloc1(as, ir->op1, RSET_GPR);

870. Reg key = RID_NONE, type = RID_TMP, idx = node;

871. RegSet allow = rset_exclude(RSET_GPR, node);

872. lua_assert(ofs % sizeof(Node) == 0);

873. if (ofs > 4095) {

874. idx = dest;

875. rset_clear(allow, dest);

876. kofs = (int32_t)offsetof(Node, key);

877. } else if (ra_hasreg(dest)) {

878. emit_opk(as, ARMI_ADD, dest, node, ofs, allow);

879. }

880. asm_guardcc(as, CC_NE);

881. if (!irt_ispri(irkey->t)) {

882. RegSet even = (as->freeset & allow);

883. even = even & (even >> 1) & RSET_GPREVEN;

884. if (even) {

885. key = ra_scratch(as, even);

886. if (rset_test(as->freeset, key+1)) {

887. type = key+1;

888. ra_modified(as, type);

889. }

890. } else {

891. key = ra_scratch(as, allow);

892. }

893. rset_clear(allow, key);

894. }

895. rset_clear(allow, type);

896. if (irt_isnum(irkey->t)) {

897. emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, type,

898. (int32_t)ir_knum(irkey)->u32.hi, allow);

899. emit_opk(as, ARMI_CMP, 0, key,

900. (int32_t)ir_knum(irkey)->u32.lo, allow);

901. } else {

902. if (ra_hasreg(key))

903. emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, key, irkey->i, allow);

904. emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype(irkey->t), type);

905. }

906. emit_lso(as, ARMI_LDR, type, idx, kofs+4);

907. if (ra_hasreg(key)) emit_lso(as, ARMI_LDR, key, idx, kofs);

908. if (ofs > 4095)

909. emit_opk(as, ARMI_ADD, dest, node, ofs, RSET_GPR);

910. }

912. ‌static void asm_uref(ASMState *as, IRIns *ir)

913. {

914. /* NYI: Check that UREFO is still open and not aliasing a slot. */

915. Reg dest = ra_dest(as, ir, RSET_GPR);

916. if (irref_isk(ir->op1)) {

917. GCfunc *fn = ir_kfunc(IR(ir->op1));

918. MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;

919. emit_lsptr(as, ARMI_LDR, dest, v);

920. } else {

921. Reg uv = ra_scratch(as, RSET_GPR);

922. Reg func = ra_alloc1(as, ir->op1, RSET_GPR);

923. if (ir->o == IR_UREFC) {

924. asm_guardcc(as, CC_NE);

925. emit_n(as, ARMI_CMP|ARMI_K12|1, RID_TMP);

926. emit_opk(as, ARMI_ADD, dest, uv,

927. (int32_t)offsetof(GCupval, tv), RSET_GPR);

928. emit_lso(as, ARMI_LDRB, RID_TMP, uv, (int32_t)offsetof(GCupval, closed));

929. } else {

930. emit_lso(as, ARMI_LDR, dest, uv, (int32_t)offsetof(GCupval, v));

931. }

932. emit_lso(as, ARMI_LDR, uv, func,

933. (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8));

934. }

935. }

937. ‌static void asm_fref(ASMState *as, IRIns *ir)

938. {

939. UNUSED(as); UNUSED(ir);

940. lua_assert(!ra_used(ir));

941. }

943. ‌static void asm_strref(ASMState *as, IRIns *ir)

944. {

945. Reg dest = ra_dest(as, ir, RSET_GPR);

946. IRRef ref = ir->op2, refk = ir->op1;

947. Reg r;

948. if (irref_isk(ref)) {

949. IRRef tmp = refk; refk = ref; ref = tmp;

950. } else if (!irref_isk(refk)) {

951. uint32_t k, m = ARMI_K12|sizeof(GCstr);

952. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);

953. IRIns *irr = IR(ir->op2);

954. if (ra_hasreg(irr->r)) {

955. ra_noweak(as, irr->r);

956. right = irr->r;

957. } else if (mayfuse(as, irr->op2) &&

958. irr->o == IR_ADD && irref_isk(irr->op2) &&

959. (k = emit_isk12(ARMI_ADD,

960. (int32_t)sizeof(GCstr) + IR(irr->op2)->i))) {

961. m = k;

962. right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));

963. } else {

964. right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left));

965. }

966. emit_dn(as, ARMI_ADD^m, dest, dest);

967. emit_dnm(as, ARMI_ADD, dest, left, right);

968. return;

969. }

970. r = ra_alloc1(as, ref, RSET_GPR);

971. emit_opk(as, ARMI_ADD, dest, r,

972. sizeof(GCstr) + IR(refk)->i, rset_exclude(RSET_GPR, r));

973. }

975. /* -- Loads and stores ---------------------------------------------------- */

977. ‌static ARMIns asm_fxloadins(IRIns *ir)

978. {

979. switch (irt_type(ir->t)) {

980. case IRT_I8: return ARMI_LDRSB;

981. case IRT_U8: return ARMI_LDRB;

982. case IRT_I16: return ARMI_LDRSH;

983. case IRT_U16: return ARMI_LDRH;

984. case IRT_NUM: lua_assert(!LJ_SOFTFP); return ARMI_VLDR_D;

985. case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VLDR_S;

986. default: return ARMI_LDR;

987. }

988. }

990. ‌static ARMIns asm_fxstoreins(IRIns *ir)

991. {

992. switch (irt_type(ir->t)) {

993. case IRT_I8: case IRT_U8: return ARMI_STRB;

994. case IRT_I16: case IRT_U16: return ARMI_STRH;

995. case IRT_NUM: lua_assert(!LJ_SOFTFP); return ARMI_VSTR_D;

996. case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VSTR_S;

997. default: return ARMI_STR;

998. }

999. }

1001. ‌static void asm_fload(ASMState *as, IRIns *ir)

1002. {

1003. Reg dest = ra_dest(as, ir, RSET_GPR);

1004. Reg idx = ra_alloc1(as, ir->op1, RSET_GPR);

1005. ARMIns ai = asm_fxloadins(ir);

1006. int32_t ofs;

1007. if (ir->op2 == IRFL_TAB_ARRAY) {

1008. ofs = asm_fuseabase(as, ir->op1);

1009. if (ofs) {  /* Turn the t->array load into an add for colocated arrays. */

1010. emit_dn(as, ARMI_ADD|ARMI_K12|ofs, dest, idx);

1011. return;

1012. }

1013. }

1014. ofs = field_ofs[ir->op2];

1015. if ((ai & 0x04000000))

1016. emit_lso(as, ai, dest, idx, ofs);

1017. else

1018. emit_lsox(as, ai, dest, idx, ofs);

1019. }

1021. ‌static void asm_fstore(ASMState *as, IRIns *ir)

1022. {

1023. if (ir->r != RID_SINK) {

1024. Reg src = ra_alloc1(as, ir->op2, RSET_GPR);

1025. IRIns *irf = IR(ir->op1);

1026. Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));

1027. int32_t ofs = field_ofs[irf->op2];

1028. ARMIns ai = asm_fxstoreins(ir);

1029. if ((ai & 0x04000000))

1030. emit_lso(as, ai, src, idx, ofs);

1031. else

1032. emit_lsox(as, ai, src, idx, ofs);

1033. }

1034. }

1036. ‌static void asm_xload(ASMState *as, IRIns *ir)

1037. {

1038. Reg dest = ra_dest(as, ir,

1039. (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR);

1040. lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED));

1041. asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0);

1042. }

1044. ‌static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs)

1045. {

1046. if (ir->r != RID_SINK) {

1047. Reg src = ra_alloc1(as, ir->op2,

1048. (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR);

1049. asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,

1050. rset_exclude(RSET_GPR, src), ofs);

1051. }

1052. }

1054. ‌#define asm_xstore(as, ir)        asm_xstore_(as, ir, 0)

1056. ‌static void asm_ahuvload(ASMState *as, IRIns *ir)

1057. {

1058. int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP);

1059. IRType t = hiop ? IRT_NUM : irt_type(ir->t);

1060. Reg dest = RID_NONE, type = RID_NONE, idx;

1061. RegSet allow = RSET_GPR;

1062. int32_t ofs = 0;

1063. if (hiop && ra_used(ir+1)) {

1064. type = ra_dest(as, ir+1, allow);

1065. rset_clear(allow, type);

1066. }

1067. if (ra_used(ir)) {

1068. lua_assert((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) ||

1069. irt_isint(ir->t) || irt_isaddr(ir->t));

1070. dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow);

1071. rset_clear(allow, dest);

1072. }

1073. idx = asm_fuseahuref(as, ir->op1, &ofs, allow,

1074. (!LJ_SOFTFP && t == IRT_NUM) ? 1024 : 4096);

1075. if (!hiop || type == RID_NONE) {

1076. rset_clear(allow, idx);

1077. if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 &&

1078. rset_test((as->freeset & allow), dest+1)) {

1079. type = dest+1;

1080. ra_modified(as, type);

1081. } else {

1082. type = RID_TMP;

1083. }

1084. }

1085. asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE);

1086. emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type);

1087. if (ra_hasreg(dest)) {

1088. #if !LJ_SOFTFP

1089. if (t == IRT_NUM)

1090. emit_vlso(as, ARMI_VLDR_D, dest, idx, ofs);

1091. else

1092. #endif

1093. emit_lso(as, ARMI_LDR, dest, idx, ofs);

1094. }

1095. emit_lso(as, ARMI_LDR, type, idx, ofs+4);

1096. }

1098. ‌static void asm_ahustore(ASMState *as, IRIns *ir)

1099. {

1100. if (ir->r != RID_SINK) {

1101. RegSet allow = RSET_GPR;

1102. Reg idx, src = RID_NONE, type = RID_NONE;

1103. int32_t ofs = 0;

1104. #if !LJ_SOFTFP

1105. if (irt_isnum(ir->t)) {

1106. src = ra_alloc1(as, ir->op2, RSET_FPR);

1107. idx = asm_fuseahuref(as, ir->op1, &ofs, allow, 1024);

1108. emit_vlso(as, ARMI_VSTR_D, src, idx, ofs);

1109. } else

1110. #endif

1111. {

1112. int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP);

1113. if (!irt_ispri(ir->t)) {

1114. src = ra_alloc1(as, ir->op2, allow);

1115. rset_clear(allow, src);

1116. }

1117. if (hiop)

1118. type = ra_alloc1(as, (ir+1)->op2, allow);

1119. else

1120. type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);

1121. idx = asm_fuseahuref(as, ir->op1, &ofs, rset_exclude(allow, type), 4096);

1122. if (ra_hasreg(src)) emit_lso(as, ARMI_STR, src, idx, ofs);

1123. emit_lso(as, ARMI_STR, type, idx, ofs+4);

1124. }

1125. }

1126. }

1128. ‌static void asm_sload(ASMState *as, IRIns *ir)

1129. {

1130. int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0);

1131. int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP);

1132. IRType t = hiop ? IRT_NUM : irt_type(ir->t);

1133. Reg dest = RID_NONE, type = RID_NONE, base;

1134. RegSet allow = RSET_GPR;

1135. lua_assert(!(ir->op2 & IRSLOAD_PARENT));  /* Handled by asm_head_side(). */

1136. lua_assert(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK));

1137. #if LJ_SOFTFP

1138. lua_assert(!(ir->op2 & IRSLOAD_CONVERT));  /* Handled by LJ_SOFTFP SPLIT. */

1139. if (hiop && ra_used(ir+1)) {

1140. type = ra_dest(as, ir+1, allow);

1141. rset_clear(allow, type);

1142. }

1143. #else

1144. if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(ir->t) && t == IRT_INT) {

1145. dest = ra_scratch(as, RSET_FPR);

1146. asm_tointg(as, ir, dest);

1147. t = IRT_NUM;  /* Continue with a regular number type check. */

1148. } else

1149. #endif

1150. if (ra_used(ir)) {

1151. Reg tmp = RID_NONE;

1152. if ((ir->op2 & IRSLOAD_CONVERT))

1153. tmp = ra_scratch(as, t == IRT_INT ? RSET_FPR : RSET_GPR);

1154. lua_assert((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) ||

1155. irt_isint(ir->t) || irt_isaddr(ir->t));

1156. dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow);

1157. rset_clear(allow, dest);

1158. base = ra_alloc1(as, REF_BASE, allow);

1159. if ((ir->op2 & IRSLOAD_CONVERT)) {

1160. if (t == IRT_INT) {

1161. emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15));

1162. emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (tmp & 15));

1163. t = IRT_NUM;  /* Check for original type. */

1164. } else {

1165. emit_dm(as, ARMI_VCVT_F64_S32, (dest & 15), (dest & 15));

1166. emit_dn(as, ARMI_VMOV_S_R, tmp, (dest & 15));

1167. t = IRT_INT;  /* Check for original type. */

1168. }

1169. dest = tmp;

1170. }

1171. goto dotypecheck;

1172. }

1173. base = ra_alloc1(as, REF_BASE, allow);

1174. dotypecheck:

1175. rset_clear(allow, base);

1176. if ((ir->op2 & IRSLOAD_TYPECHECK)) {

1177. if (ra_noreg(type)) {

1178. if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 &&

1179. rset_test((as->freeset & allow), dest+1)) {

1180. type = dest+1;

1181. ra_modified(as, type);

1182. } else {

1183. type = RID_TMP;

1184. }

1185. }

1186. asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE);

1187. emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type);

1188. }

1189. if (ra_hasreg(dest)) {

1190. #if !LJ_SOFTFP

1191. if (t == IRT_NUM) {

1192. if (ofs < 1024) {

1193. emit_vlso(as, ARMI_VLDR_D, dest, base, ofs);

1194. } else {

1195. if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4);

1196. emit_vlso(as, ARMI_VLDR_D, dest, RID_TMP, 0);

1197. emit_opk(as, ARMI_ADD, RID_TMP, base, ofs, allow);

1198. return;

1199. }

1200. } else

1201. #endif

1202. emit_lso(as, ARMI_LDR, dest, base, ofs);

1203. }

1204. if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4);

1205. }

1207. /* -- Allocations --------------------------------------------------------- */

1209. #if LJ_HASFFI

1210. ‌static void asm_cnew(ASMState *as, IRIns *ir)

1211. {

1212. CTState *cts = ctype_ctsG(J2G(as->J));

1213. CTypeID id = (CTypeID)IR(ir->op1)->i;

1214. CTSize sz;

1215. CTInfo info = lj_ctype_info(cts, id, &sz);

1216. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];

1217. IRRef args[4];

1218. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);

1219. RegSet drop = RSET_SCRATCH;

1220. lua_assert(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL));

1222. as->gcsteps++;

1223. if (ra_hasreg(ir->r))

1224. rset_clear(drop, ir->r);  /* Dest reg handled below. */

1225. ra_evictset(as, drop);

1226. if (ra_used(ir))

1227. ra_destreg(as, ir, RID_RET);  /* GCcdata * */

1229. /* Initialize immutable cdata object. */

1230. if (ir->o == IR_CNEWI) {

1231. int32_t ofs = sizeof(GCcdata);

1232. lua_assert(sz == 4 || sz == 8);

1233. if (sz == 8) {

1234. ofs += 4; ir++;

1235. lua_assert(ir->o == IR_HIOP);

1236. }

1237. for (;;) {

1238. Reg r = ra_alloc1(as, ir->op2, allow);

1239. emit_lso(as, ARMI_STR, r, RID_RET, ofs);

1240. rset_clear(allow, r);

1241. if (ofs == sizeof(GCcdata)) break;

1242. ofs -= 4; ir--;

1243. }

1244. } else if (ir->op2 != REF_NIL) {  /* Create VLA/VLS/aligned cdata. */

1245. ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];

1246. args[0] = ASMREF_L;     /* lua_State *L */

1247. args[1] = ir->op1;      /* CTypeID id   */

1248. args[2] = ir->op2;      /* CTSize sz    */

1249. args[3] = ASMREF_TMP1;  /* CTSize align */

1250. asm_gencall(as, ci, args);

1251. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));

1252. return;

1253. }

1255. /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */

1256. {

1257. uint32_t k = emit_isk12(ARMI_MOV, id);

1258. Reg r = k ? RID_R1 : ra_allock(as, id, allow);

1259. emit_lso(as, ARMI_STRB, RID_TMP, RID_RET, offsetof(GCcdata, gct));

1260. emit_lsox(as, ARMI_STRH, r, RID_RET, offsetof(GCcdata, ctypeid));

1261. emit_d(as, ARMI_MOV|ARMI_K12|~LJ_TCDATA, RID_TMP);

1262. if (k) emit_d(as, ARMI_MOV^k, RID_R1);

1263. }

1264. args[0] = ASMREF_L;     /* lua_State *L */

1265. args[1] = ASMREF_TMP1;  /* MSize size   */

1266. asm_gencall(as, ci, args);

1267. ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),

1268. ra_releasetmp(as, ASMREF_TMP1));

1269. }

1270. #else

1271. ‌#define asm_cnew(as, ir)        ((void)0)

1272. #endif

1274. /* -- Write barriers ------------------------------------------------------ */

1276. ‌static void asm_tbar(ASMState *as, IRIns *ir)

1277. {

1278. Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);

1279. Reg link = ra_scratch(as, rset_exclude(RSET_GPR, tab));

1280. Reg gr = ra_allock(as, i32ptr(J2G(as->J)),

1281. rset_exclude(rset_exclude(RSET_GPR, tab), link));

1282. Reg mark = RID_TMP;

1283. MCLabel l_end = emit_label(as);

1284. emit_lso(as, ARMI_STR, link, tab, (int32_t)offsetof(GCtab, gclist));

1285. emit_lso(as, ARMI_STRB, mark, tab, (int32_t)offsetof(GCtab, marked));

1286. emit_lso(as, ARMI_STR, tab, gr,

1287. (int32_t)offsetof(global_State, gc.grayagain));

1288. emit_dn(as, ARMI_BIC|ARMI_K12|LJ_GC_BLACK, mark, mark);

1289. emit_lso(as, ARMI_LDR, link, gr,

1290. (int32_t)offsetof(global_State, gc.grayagain));

1291. emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end);

1292. emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_BLACK, mark);

1293. emit_lso(as, ARMI_LDRB, mark, tab, (int32_t)offsetof(GCtab, marked));

1294. }

1296. ‌static void asm_obar(ASMState *as, IRIns *ir)

1297. {

1298. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];

1299. IRRef args[2];

1300. MCLabel l_end;

1301. Reg obj, val, tmp;

1302. /* No need for other object barriers (yet). */

1303. lua_assert(IR(ir->op1)->o == IR_UREFC);

1304. ra_evictset(as, RSET_SCRATCH);

1305. l_end = emit_label(as);

1306. args[0] = ASMREF_TMP1;  /* global_State *g */

1307. args[1] = ir->op1;      /* TValue *tv      */

1308. asm_gencall(as, ci, args);

1309. if ((l_end[-1] >> 28) == CC_AL)

1310. l_end[-1] = ARMF_CC(l_end[-1], CC_NE);

1311. else

1312. emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end);

1313. ra_allockreg(as, i32ptr(J2G(as->J)), ra_releasetmp(as, ASMREF_TMP1));

1314. obj = IR(ir->op1)->r;

1315. tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));

1316. emit_n(as, ARMF_CC(ARMI_TST, CC_NE)|ARMI_K12|LJ_GC_BLACK, tmp);

1317. emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_WHITES, RID_TMP);

1318. val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));

1319. emit_lso(as, ARMI_LDRB, tmp, obj,

1320. (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));

1321. emit_lso(as, ARMI_LDRB, RID_TMP, val, (int32_t)offsetof(GChead, marked));

1322. }

1324. /* -- Arithmetic and logic operations ------------------------------------- */

1326. #if !LJ_SOFTFP

1327. ‌static void asm_fparith(ASMState *as, IRIns *ir, ARMIns ai)

1328. {

1329. Reg dest = ra_dest(as, ir, RSET_FPR);

1330. Reg right, left = ra_alloc2(as, ir, RSET_FPR);

1331. right = (left >> 8); left &= 255;

1332. emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15));

1333. }

1335. ‌static void asm_fpunary(ASMState *as, IRIns *ir, ARMIns ai)

1336. {

1337. Reg dest = ra_dest(as, ir, RSET_FPR);

1338. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);

1339. emit_dm(as, ai, (dest & 15), (left & 15));

1340. }

1342. ‌static void asm_callround(ASMState *as, IRIns *ir, int id)

1343. {

1344. /* The modified regs must match with the *.dasc implementation. */

1345. RegSet drop = RID2RSET(RID_R0)|RID2RSET(RID_R1)|RID2RSET(RID_R2)|

1346. RID2RSET(RID_R3)|RID2RSET(RID_R12);

1347. RegSet of;

1348. Reg dest, src;

1349. ra_evictset(as, drop);

1350. dest = ra_dest(as, ir, RSET_FPR);

1351. emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, (dest & 15));

1352. emit_call(as, id == IRFPM_FLOOR ? (void *)lj_vm_floor_sf :

1353. id == IRFPM_CEIL ? (void *)lj_vm_ceil_sf :

1354. (void *)lj_vm_trunc_sf);

1355. /* Workaround to protect argument GPRs from being used for remat. */

1356. of = as->freeset;

1357. as->freeset &= ~RSET_RANGE(RID_R0, RID_R1+1);

1358. as->cost[RID_R0] = as->cost[RID_R1] = REGCOST(~0u, ASMREF_L);

1359. src = ra_alloc1(as, ir->op1, RSET_FPR);  /* May alloc GPR to remat FPR. */

1360. as->freeset |= (of & RSET_RANGE(RID_R0, RID_R1+1));

1361. emit_dnm(as, ARMI_VMOV_RR_D, RID_R0, RID_R1, (src & 15));

1362. }

1364. ‌static void asm_fpmath(ASMState *as, IRIns *ir)

1365. {

1366. if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir))

1367. return;

1368. if (ir->op2 <= IRFPM_TRUNC)

1369. asm_callround(as, ir, ir->op2);

1370. else if (ir->op2 == IRFPM_SQRT)

1371. asm_fpunary(as, ir, ARMI_VSQRT_D);

1372. else

1373. asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);

1374. }

1375. #else

1376. ‌#define asm_fpmath(as, ir)        lua_assert(0)

1377. #endif

1379. ‌static int asm_swapops(ASMState *as, IRRef lref, IRRef rref)

1380. {

1381. IRIns *ir;

1382. if (irref_isk(rref))

1383. return 0;  /* Don't swap constants to the left. */

1384. if (irref_isk(lref))

1385. return 1;  /* But swap constants to the right. */

1386. ir = IR(rref);

1387. if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) ||

1388. (ir->o == IR_ADD && ir->op1 == ir->op2))

1389. return 0;  /* Don't swap fusable operands to the left. */

1390. ir = IR(lref);

1391. if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) ||

1392. (ir->o == IR_ADD && ir->op1 == ir->op2))

1393. return 1;  /* But swap fusable operands to the right. */

1394. return 0;  /* Otherwise don't swap. */

1395. }

1397. ‌static void asm_intop(ASMState *as, IRIns *ir, ARMIns ai)

1398. {

1399. IRRef lref = ir->op1, rref = ir->op2;

1400. Reg left, dest = ra_dest(as, ir, RSET_GPR);

1401. uint32_t m;

1402. if (asm_swapops(as, lref, rref)) {

1403. IRRef tmp = lref; lref = rref; rref = tmp;

1404. if ((ai & ~ARMI_S) == ARMI_SUB || (ai & ~ARMI_S) == ARMI_SBC)

1405. ai ^= (ARMI_SUB^ARMI_RSB);

1406. }

1407. left = ra_hintalloc(as, lref, dest, RSET_GPR);

1408. m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left));

1409. if (irt_isguard(ir->t)) {  /* For IR_ADDOV etc. */

1410. asm_guardcc(as, CC_VS);

1411. ai |= ARMI_S;

1412. }

1413. emit_dn(as, ai^m, dest, left);

1414. }

1416. ‌static void asm_intop_s(ASMState *as, IRIns *ir, ARMIns ai)

1417. {

1418. if (as->flagmcp == as->mcp) {  /* Drop cmp r, #0. */

1419. as->flagmcp = NULL;

1420. as->mcp++;

1421. ai |= ARMI_S;

1422. }

1423. asm_intop(as, ir, ai);

1424. }

1426. ‌static void asm_intneg(ASMState *as, IRIns *ir, ARMIns ai)

1427. {

1428. Reg dest = ra_dest(as, ir, RSET_GPR);

1429. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1430. emit_dn(as, ai|ARMI_K12|0, dest, left);

1431. }

1433. /* NYI: use add/shift for MUL(OV) with constants. FOLD only does 2^k. */

1434. ‌static void asm_intmul(ASMState *as, IRIns *ir)

1435. {

1436. Reg dest = ra_dest(as, ir, RSET_GPR);

1437. Reg left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, dest));

1438. Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1439. Reg tmp = RID_NONE;

1440. /* ARMv5 restriction: dest != left and dest_hi != left. */

1441. if (dest == left && left != right) { left = right; right = dest; }

1442. if (irt_isguard(ir->t)) {  /* IR_MULOV */

1443. if (!(as->flags & JIT_F_ARMV6) && dest == left)

1444. tmp = left = ra_scratch(as, rset_exclude(RSET_GPR, left));

1445. asm_guardcc(as, CC_NE);

1446. emit_nm(as, ARMI_TEQ|ARMF_SH(ARMSH_ASR, 31), RID_TMP, dest);

1447. emit_dnm(as, ARMI_SMULL|ARMF_S(right), dest, RID_TMP, left);

1448. } else {

1449. if (!(as->flags & JIT_F_ARMV6) && dest == left) tmp = left = RID_TMP;

1450. emit_nm(as, ARMI_MUL|ARMF_S(right), dest, left);

1451. }

1452. /* Only need this for the dest == left == right case. */

1453. if (ra_hasreg(tmp)) emit_dm(as, ARMI_MOV, tmp, right);

1454. }

1456. ‌static void asm_add(ASMState *as, IRIns *ir)

1457. {

1458. #if !LJ_SOFTFP

1459. if (irt_isnum(ir->t)) {

1460. if (!asm_fusemadd(as, ir, ARMI_VMLA_D, ARMI_VMLA_D))

1461. asm_fparith(as, ir, ARMI_VADD_D);

1462. return;

1463. }

1464. #endif

1465. asm_intop_s(as, ir, ARMI_ADD);

1466. }

1468. ‌static void asm_sub(ASMState *as, IRIns *ir)

1469. {

1470. #if !LJ_SOFTFP

1471. if (irt_isnum(ir->t)) {

1472. if (!asm_fusemadd(as, ir, ARMI_VNMLS_D, ARMI_VMLS_D))

1473. asm_fparith(as, ir, ARMI_VSUB_D);

1474. return;

1475. }

1476. #endif

1477. asm_intop_s(as, ir, ARMI_SUB);

1478. }

1480. ‌static void asm_mul(ASMState *as, IRIns *ir)

1481. {

1482. #if !LJ_SOFTFP

1483. if (irt_isnum(ir->t)) {

1484. asm_fparith(as, ir, ARMI_VMUL_D);

1485. return;

1486. }

1487. #endif

1488. asm_intmul(as, ir);

1489. }

1491. ‌#define asm_addov(as, ir)        asm_add(as, ir)

1492. ‌#define asm_subov(as, ir)        asm_sub(as, ir)

1493. ‌#define asm_mulov(as, ir)        asm_mul(as, ir)

1495. #if LJ_SOFTFP

1496. ‌#define asm_div(as, ir)                lua_assert(0)

1497. ‌#define asm_pow(as, ir)                lua_assert(0)

1498. ‌#define asm_abs(as, ir)                lua_assert(0)

1499. ‌#define asm_atan2(as, ir)        lua_assert(0)

1500. ‌#define asm_ldexp(as, ir)        lua_assert(0)

1501. #else

1502. ‌#define asm_div(as, ir)                asm_fparith(as, ir, ARMI_VDIV_D)

1503. ‌#define asm_pow(as, ir)                asm_callid(as, ir, IRCALL_lj_vm_powi)

1504. ‌#define asm_abs(as, ir)                asm_fpunary(as, ir, ARMI_VABS_D)

1505. ‌#define asm_atan2(as, ir)        asm_callid(as, ir, IRCALL_atan2)

1506. ‌#define asm_ldexp(as, ir)        asm_callid(as, ir, IRCALL_ldexp)

1507. #endif

1509. ‌#define asm_mod(as, ir)                asm_callid(as, ir, IRCALL_lj_vm_modi)

1511. ‌static void asm_neg(ASMState *as, IRIns *ir)

1512. {

1513. #if !LJ_SOFTFP

1514. if (irt_isnum(ir->t)) {

1515. asm_fpunary(as, ir, ARMI_VNEG_D);

1516. return;

1517. }

1518. #endif

1519. asm_intneg(as, ir, ARMI_RSB);

1520. }

1522. ‌static void asm_bitop(ASMState *as, IRIns *ir, ARMIns ai)

1523. {

1524. if (as->flagmcp == as->mcp) {  /* Try to drop cmp r, #0. */

1525. uint32_t cc = (as->mcp[1] >> 28);

1526. as->flagmcp = NULL;

1527. if (cc <= CC_NE) {

1528. as->mcp++;

1529. ai |= ARMI_S;

1530. } else if (cc == CC_GE) {

1531. *++as->mcp ^= ((CC_GE^CC_PL) << 28);

1532. ai |= ARMI_S;

1533. } else if (cc == CC_LT) {

1534. *++as->mcp ^= ((CC_LT^CC_MI) << 28);

1535. ai |= ARMI_S;

1536. }  /* else: other conds don't work with bit ops. */

1537. }

1538. if (ir->op2 == 0) {

1539. Reg dest = ra_dest(as, ir, RSET_GPR);

1540. uint32_t m = asm_fuseopm(as, ai, ir->op1, RSET_GPR);

1541. emit_d(as, ai^m, dest);

1542. } else {

1543. /* NYI: Turn BAND !k12 into uxtb, uxth or bfc or shl+shr. */

1544. asm_intop(as, ir, ai);

1545. }

1546. }

1548. ‌#define asm_bnot(as, ir)        asm_bitop(as, ir, ARMI_MVN)

1550. ‌static void asm_bswap(ASMState *as, IRIns *ir)

1551. {

1552. Reg dest = ra_dest(as, ir, RSET_GPR);

1553. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);

1554. if ((as->flags & JIT_F_ARMV6)) {

1555. emit_dm(as, ARMI_REV, dest, left);

1556. } else {

1557. Reg tmp2 = dest;

1558. if (tmp2 == left)

1559. tmp2 = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, dest), left));

1560. emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_LSR, 8), dest, tmp2, RID_TMP);

1561. emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_ROR, 8), tmp2, left);

1562. emit_dn(as, ARMI_BIC|ARMI_K12|256*8|255, RID_TMP, RID_TMP);

1563. emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 16), RID_TMP, left, left);

1564. }

1565. }

1567. ‌#define asm_band(as, ir)        asm_bitop(as, ir, ARMI_AND)

1568. ‌#define asm_bor(as, ir)                asm_bitop(as, ir, ARMI_ORR)

1569. ‌#define asm_bxor(as, ir)        asm_bitop(as, ir, ARMI_EOR)

1571. ‌static void asm_bitshift(ASMState *as, IRIns *ir, ARMShift sh)

1572. {

1573. if (irref_isk(ir->op2)) {  /* Constant shifts. */

1574. /* NYI: Turn SHL+SHR or BAND+SHR into uxtb, uxth or ubfx. */

1575. /* NYI: Turn SHL+ASR into sxtb, sxth or sbfx. */

1576. Reg dest = ra_dest(as, ir, RSET_GPR);

1577. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);

1578. int32_t shift = (IR(ir->op2)->i & 31);

1579. emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, left);

1580. } else {

1581. Reg dest = ra_dest(as, ir, RSET_GPR);

1582. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);

1583. Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1584. emit_dm(as, ARMI_MOV|ARMF_RSH(sh, right), dest, left);

1585. }

1586. }

1588. ‌#define asm_bshl(as, ir)        asm_bitshift(as, ir, ARMSH_LSL)

1589. ‌#define asm_bshr(as, ir)        asm_bitshift(as, ir, ARMSH_LSR)

1590. ‌#define asm_bsar(as, ir)        asm_bitshift(as, ir, ARMSH_ASR)

1591. ‌#define asm_bror(as, ir)        asm_bitshift(as, ir, ARMSH_ROR)

1592. ‌#define asm_brol(as, ir)        lua_assert(0)

1594. ‌static void asm_intmin_max(ASMState *as, IRIns *ir, int cc)

1595. {

1596. uint32_t kcmp = 0, kmov = 0;

1597. Reg dest = ra_dest(as, ir, RSET_GPR);

1598. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1599. Reg right = 0;

1600. if (irref_isk(ir->op2)) {

1601. kcmp = emit_isk12(ARMI_CMP, IR(ir->op2)->i);

1602. if (kcmp) kmov = emit_isk12(ARMI_MOV, IR(ir->op2)->i);

1603. }

1604. if (!kmov) {

1605. kcmp = 0;

1606. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1607. }

1608. if (kmov || dest != right) {

1609. emit_dm(as, ARMF_CC(ARMI_MOV, cc)^kmov, dest, right);

1610. cc ^= 1;  /* Must use opposite conditions for paired moves. */

1611. } else {

1612. cc ^= (CC_LT^CC_GT);  /* Otherwise may swap CC_LT <-> CC_GT. */

1613. }

1614. if (dest != left) emit_dm(as, ARMF_CC(ARMI_MOV, cc), dest, left);

1615. emit_nm(as, ARMI_CMP^kcmp, left, right);

1616. }

1618. #if LJ_SOFTFP

1619. ‌static void asm_sfpmin_max(ASMState *as, IRIns *ir, int cc)

1620. {

1621. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp];

1622. RegSet drop = RSET_SCRATCH;

1623. Reg r;

1624. IRRef args[4];

1625. args[0] = ir->op1; args[1] = (ir+1)->op1;

1626. args[2] = ir->op2; args[3] = (ir+1)->op2;

1627. /* __aeabi_cdcmple preserves r0-r3. */

1628. if (ra_hasreg(ir->r)) rset_clear(drop, ir->r);

1629. if (ra_hasreg((ir+1)->r)) rset_clear(drop, (ir+1)->r);

1630. if (!rset_test(as->freeset, RID_R2) &&

1631. regcost_ref(as->cost[RID_R2]) == args[2]) rset_clear(drop, RID_R2);

1632. if (!rset_test(as->freeset, RID_R3) &&

1633. regcost_ref(as->cost[RID_R3]) == args[3]) rset_clear(drop, RID_R3);

1634. ra_evictset(as, drop);

1635. ra_destpair(as, ir);

1636. emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETHI, RID_R3);

1637. emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETLO, RID_R2);

1638. emit_call(as, (void *)ci->func);

1639. for (r = RID_R0; r <= RID_R3; r++)

1640. ra_leftov(as, r, args[r-RID_R0]);

1641. }

1642. #else

1643. ‌static void asm_fpmin_max(ASMState *as, IRIns *ir, int cc)

1644. {

1645. Reg dest = (ra_dest(as, ir, RSET_FPR) & 15);

1646. Reg right, left = ra_alloc2(as, ir, RSET_FPR);

1647. right = ((left >> 8) & 15); left &= 15;

1648. if (dest != left) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc^1), dest, left);

1649. if (dest != right) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc), dest, right);

1650. emit_d(as, ARMI_VMRS, 0);

1651. emit_dm(as, ARMI_VCMP_D, left, right);

1652. }

1653. #endif

1655. ‌static void asm_min_max(ASMState *as, IRIns *ir, int cc, int fcc)

1656. {

1657. #if LJ_SOFTFP

1658. UNUSED(fcc);

1659. #else

1660. if (irt_isnum(ir->t))

1661. asm_fpmin_max(as, ir, fcc);

1662. else

1663. #endif

1664. asm_intmin_max(as, ir, cc);

1665. }

1667. ‌#define asm_min(as, ir)                asm_min_max(as, ir, CC_GT, CC_HI)

1668. ‌#define asm_max(as, ir)                asm_min_max(as, ir, CC_LT, CC_LO)

1670. /* -- Comparisons --------------------------------------------------------- */

1672. /* Map of comparisons to flags. ORDER IR. */

1673. ‌static const uint8_t asm_compmap[IR_ABC+1] = {

1674. /* op  FP swp  int cc   FP cc */

1675. /* LT       */ CC_GE + (CC_HS << 4),

1676. /* GE    x  */ CC_LT + (CC_HI << 4),

1677. /* LE       */ CC_GT + (CC_HI << 4),

1678. /* GT    x  */ CC_LE + (CC_HS << 4),

1679. /* ULT   x  */ CC_HS + (CC_LS << 4),

1680. /* UGE      */ CC_LO + (CC_LO << 4),

1681. /* ULE   x  */ CC_HI + (CC_LO << 4),

1682. /* UGT      */ CC_LS + (CC_LS << 4),

1683. /* EQ       */ CC_NE + (CC_NE << 4),

1684. /* NE       */ CC_EQ + (CC_EQ << 4),

1685. /* ABC      */ CC_LS + (CC_LS << 4)  /* Same as UGT. */

1686. };

1688. #if LJ_SOFTFP

1689. /* FP comparisons. */

1690. ‌static void asm_sfpcomp(ASMState *as, IRIns *ir)

1691. {

1692. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp];

1693. RegSet drop = RSET_SCRATCH;

1694. Reg r;

1695. IRRef args[4];

1696. int swp = (((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1) << 1);

1697. args[swp^0] = ir->op1; args[swp^1] = (ir+1)->op1;

1698. args[swp^2] = ir->op2; args[swp^3] = (ir+1)->op2;

1699. /* __aeabi_cdcmple preserves r0-r3. This helps to reduce spills. */

1700. for (r = RID_R0; r <= RID_R3; r++)

1701. if (!rset_test(as->freeset, r) &&

1702. regcost_ref(as->cost[r]) == args[r-RID_R0]) rset_clear(drop, r);

1703. ra_evictset(as, drop);

1704. asm_guardcc(as, (asm_compmap[ir->o] >> 4));

1705. emit_call(as, (void *)ci->func);

1706. for (r = RID_R0; r <= RID_R3; r++)

1707. ra_leftov(as, r, args[r-RID_R0]);

1708. }

1709. #else

1710. /* FP comparisons. */

1711. ‌static void asm_fpcomp(ASMState *as, IRIns *ir)

1712. {

1713. Reg left, right;

1714. ARMIns ai;

1715. int swp = ((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1);

1716. if (!swp && irref_isk(ir->op2) && ir_knum(IR(ir->op2))->u64 == 0) {

1717. left = (ra_alloc1(as, ir->op1, RSET_FPR) & 15);

1718. right = 0;

1719. ai = ARMI_VCMPZ_D;

1720. } else {

1721. left = ra_alloc2(as, ir, RSET_FPR);

1722. if (swp) {

1723. right = (left & 15); left = ((left >> 8) & 15);

1724. } else {

1725. right = ((left >> 8) & 15); left &= 15;

1726. }

1727. ai = ARMI_VCMP_D;

1728. }

1729. asm_guardcc(as, (asm_compmap[ir->o] >> 4));

1730. emit_d(as, ARMI_VMRS, 0);

1731. emit_dm(as, ai, left, right);

1732. }

1733. #endif

1735. /* Integer comparisons. */

1736. ‌static void asm_intcomp(ASMState *as, IRIns *ir)

1737. {

1738. ARMCC cc = (asm_compmap[ir->o] & 15);

1739. IRRef lref = ir->op1, rref = ir->op2;

1740. Reg left;

1741. uint32_t m;

1742. int cmpprev0 = 0;

1743. lua_assert(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t));

1744. if (asm_swapops(as, lref, rref)) {

1745. Reg tmp = lref; lref = rref; rref = tmp;

1746. if (cc >= CC_GE) cc ^= 7;  /* LT <-> GT, LE <-> GE */

1747. else if (cc > CC_NE) cc ^= 11;  /* LO <-> HI, LS <-> HS */

1748. }

1749. if (irref_isk(rref) && IR(rref)->i == 0) {

1750. IRIns *irl = IR(lref);

1751. cmpprev0 = (irl+1 == ir);

1752. /* Combine comp(BAND(left, right), 0) into tst left, right. */

1753. if (cmpprev0 && irl->o == IR_BAND && !ra_used(irl)) {

1754. IRRef blref = irl->op1, brref = irl->op2;

1755. uint32_t m2 = 0;

1756. Reg bleft;

1757. if (asm_swapops(as, blref, brref)) {

1758. Reg tmp = blref; blref = brref; brref = tmp;

1759. }

1760. if (irref_isk(brref)) {

1761. m2 = emit_isk12(ARMI_AND, IR(brref)->i);

1762. if ((m2 & (ARMI_AND^ARMI_BIC)))

1763. goto notst;  /* Not beneficial if we miss a constant operand. */

1764. }

1765. if (cc == CC_GE) cc = CC_PL;

1766. else if (cc == CC_LT) cc = CC_MI;

1767. else if (cc > CC_NE) goto notst;  /* Other conds don't work with tst. */

1768. bleft = ra_alloc1(as, blref, RSET_GPR);

1769. if (!m2) m2 = asm_fuseopm(as, 0, brref, rset_exclude(RSET_GPR, bleft));

1770. asm_guardcc(as, cc);

1771. emit_n(as, ARMI_TST^m2, bleft);

1772. return;

1773. }

1774. }

1775. notst:

1776. left = ra_alloc1(as, lref, RSET_GPR);

1777. m = asm_fuseopm(as, ARMI_CMP, rref, rset_exclude(RSET_GPR, left));

1778. asm_guardcc(as, cc);

1779. emit_n(as, ARMI_CMP^m, left);

1780. /* Signed comparison with zero and referencing previous ins? */

1781. if (cmpprev0 && (cc <= CC_NE || cc >= CC_GE))

1782. as->flagmcp = as->mcp;  /* Allow elimination of the compare. */

1783. }

1785. ‌static void asm_comp(ASMState *as, IRIns *ir)

1786. {

1787. #if !LJ_SOFTFP

1788. if (irt_isnum(ir->t))

1789. asm_fpcomp(as, ir);

1790. else

1791. #endif

1792. asm_intcomp(as, ir);

1793. }

1795. ‌#define asm_equal(as, ir)        asm_comp(as, ir)

1797. #if LJ_HASFFI

1798. /* 64 bit integer comparisons. */

1799. ‌static void asm_int64comp(ASMState *as, IRIns *ir)

1800. {

1801. int signedcomp = (ir->o <= IR_GT);

1802. ARMCC cclo, cchi;

1803. Reg leftlo, lefthi;

1804. uint32_t mlo, mhi;

1805. RegSet allow = RSET_GPR, oldfree;

1807. /* Always use unsigned comparison for loword. */

1808. cclo = asm_compmap[ir->o + (signedcomp ? 4 : 0)] & 15;

1809. leftlo = ra_alloc1(as, ir->op1, allow);

1810. oldfree = as->freeset;

1811. mlo = asm_fuseopm(as, ARMI_CMP, ir->op2, rset_clear(allow, leftlo));

1812. allow &= ~(oldfree & ~as->freeset);  /* Update for allocs of asm_fuseopm. */

1814. /* Use signed or unsigned comparison for hiword. */

1815. cchi = asm_compmap[ir->o] & 15;

1816. lefthi = ra_alloc1(as, (ir+1)->op1, allow);

1817. mhi = asm_fuseopm(as, ARMI_CMP, (ir+1)->op2, rset_clear(allow, lefthi));

1819. /* All register allocations must be performed _before_ this point. */

1820. if (signedcomp) {

1821. MCLabel l_around = emit_label(as);

1822. asm_guardcc(as, cclo);

1823. emit_n(as, ARMI_CMP^mlo, leftlo);

1824. emit_branch(as, ARMF_CC(ARMI_B, CC_NE), l_around);

1825. if (cchi == CC_GE || cchi == CC_LE) cchi ^= 6;  /* GE -> GT, LE -> LT */

1826. asm_guardcc(as, cchi);

1827. } else {

1828. asm_guardcc(as, cclo);

1829. emit_n(as, ARMF_CC(ARMI_CMP, CC_EQ)^mlo, leftlo);

1830. }

1831. emit_n(as, ARMI_CMP^mhi, lefthi);

1832. }

1833. #endif

1835. /* -- Support for 64 bit ops in 32 bit mode ------------------------------- */

1837. /* Hiword op of a split 64 bit op. Previous op must be the loword op. */

1838. ‌static void asm_hiop(ASMState *as, IRIns *ir)

1839. {

1840. #if LJ_HASFFI || LJ_SOFTFP

1841. /* HIOP is marked as a store because it needs its own DCE logic. */

1842. int uselo = ra_used(ir-1), usehi = ra_used(ir);  /* Loword/hiword used? */

1843. if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;

1844. if ((ir-1)->o <= IR_NE) {  /* 64 bit integer or FP comparisons. ORDER IR. */

1845. as->curins--;  /* Always skip the loword comparison. */

1846. #if LJ_SOFTFP

1847. if (!irt_isint(ir->t)) {

1848. asm_sfpcomp(as, ir-1);

1849. return;

1850. }

1851. #endif

1852. #if LJ_HASFFI

1853. asm_int64comp(as, ir-1);

1854. #endif

1855. return;

1856. #if LJ_SOFTFP

1857. } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) {

1858. as->curins--;  /* Always skip the loword min/max. */

1859. if (uselo || usehi)

1860. asm_sfpmin_max(as, ir-1, (ir-1)->o == IR_MIN ? CC_HI : CC_LO);

1861. return;

1862. #elif LJ_HASFFI

1863. } else if ((ir-1)->o == IR_CONV) {

1864. as->curins--;  /* Always skip the CONV. */

1865. if (usehi || uselo)

1866. asm_conv64(as, ir);

1867. return;

1868. #endif

1869. } else if ((ir-1)->o == IR_XSTORE) {

1870. if ((ir-1)->r != RID_SINK)

1871. asm_xstore_(as, ir, 4);

1872. return;

1873. }

1874. if (!usehi) return;  /* Skip unused hiword op for all remaining ops. */

1875. switch ((ir-1)->o) {

1876. #if LJ_HASFFI

1877. case IR_ADD:

1878. as->curins--;

1879. asm_intop(as, ir, ARMI_ADC);

1880. asm_intop(as, ir-1, ARMI_ADD|ARMI_S);

1881. break;

1882. case IR_SUB:

1883. as->curins--;

1884. asm_intop(as, ir, ARMI_SBC);

1885. asm_intop(as, ir-1, ARMI_SUB|ARMI_S);

1886. break;

1887. case IR_NEG:

1888. as->curins--;

1889. asm_intneg(as, ir, ARMI_RSC);

1890. asm_intneg(as, ir-1, ARMI_RSB|ARMI_S);

1891. break;

1892. #endif

1893. #if LJ_SOFTFP

1894. case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:

1895. case IR_STRTO:

1896. if (!uselo)

1897. ra_allocref(as, ir->op1, RSET_GPR);  /* Mark lo op as used. */

1898. break;

1899. #endif

1900. case IR_CALLN:

1901. case IR_CALLS:

1902. case IR_CALLXS:

1903. if (!uselo)

1904. ra_allocref(as, ir->op1, RID2RSET(RID_RETLO));  /* Mark lo op as used. */

1905. break;

1906. #if LJ_SOFTFP

1907. case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_TOSTR:

1908. #endif

1909. case IR_CNEWI:

1910. /* Nothing to do here. Handled by lo op itself. */

1911. break;

1912. default: lua_assert(0); break;

1913. }

1914. #else

1915. UNUSED(as); UNUSED(ir); lua_assert(0);

1916. #endif

1917. }

1919. /* -- Profiling ----------------------------------------------------------- */

1921. ‌static void asm_prof(ASMState *as, IRIns *ir)

1922. {

1923. UNUSED(ir);

1924. asm_guardcc(as, CC_NE);

1925. emit_n(as, ARMI_TST|ARMI_K12|HOOK_PROFILE, RID_TMP);

1926. emit_lsptr(as, ARMI_LDRB, RID_TMP, (void *)&J2G(as->J)->hookmask);

1927. }

1929. /* -- Stack handling ------------------------------------------------------ */

1931. /* Check Lua stack size for overflow. Use exit handler as fallback. */

1932. ‌static void asm_stack_check(ASMState *as, BCReg topslot,

1933. IRIns *irp, RegSet allow, ExitNo exitno)

1934. {

1935. Reg pbase;

1936. uint32_t k;

1937. if (irp) {

1938. if (!ra_hasspill(irp->s)) {

1939. pbase = irp->r;

1940. lua_assert(ra_hasreg(pbase));

1941. } else if (allow) {

1942. pbase = rset_pickbot(allow);

1943. } else {

1944. pbase = RID_RET;

1945. emit_lso(as, ARMI_LDR, RID_RET, RID_SP, 0);  /* Restore temp. register. */

1946. }

1947. } else {

1948. pbase = RID_BASE;

1949. }

1950. emit_branch(as, ARMF_CC(ARMI_BL, CC_LS), exitstub_addr(as->J, exitno));

1951. k = emit_isk12(0, (int32_t)(8*topslot));

1952. lua_assert(k);

1953. emit_n(as, ARMI_CMP^k, RID_TMP);

1954. emit_dnm(as, ARMI_SUB, RID_TMP, RID_TMP, pbase);

1955. emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP,

1956. (int32_t)offsetof(lua_State, maxstack));

1957. if (irp) {  /* Must not spill arbitrary registers in head of side trace. */

1958. int32_t i = i32ptr(&J2G(as->J)->cur_L);

1959. if (ra_hasspill(irp->s))

1960. emit_lso(as, ARMI_LDR, pbase, RID_SP, sps_scale(irp->s));

1961. emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, (i & 4095));

1962. if (ra_hasspill(irp->s) && !allow)

1963. emit_lso(as, ARMI_STR, RID_RET, RID_SP, 0);  /* Save temp. register. */

1964. emit_loadi(as, RID_TMP, (i & ~4095));

1965. } else {

1966. emit_getgl(as, RID_TMP, cur_L);

1967. }

1968. }

1970. /* Restore Lua stack from on-trace state. */

1971. ‌static void asm_stack_restore(ASMState *as, SnapShot *snap)

1972. {

1973. SnapEntry *map = &as->T->snapmap[snap->mapofs];

1974. SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1];

1975. MSize n, nent = snap->nent;

1976. /* Store the value of all modified slots to the Lua stack. */

1977. for (n = 0; n < nent; n++) {

1978. SnapEntry sn = map[n];

1979. BCReg s = snap_slot(sn);

1980. int32_t ofs = 8*((int32_t)s-1);

1981. IRRef ref = snap_ref(sn);

1982. IRIns *ir = IR(ref);

1983. if ((sn & SNAP_NORESTORE))

1984. continue;

1985. if (irt_isnum(ir->t)) {

1986. #if LJ_SOFTFP

1987. RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE);

1988. Reg tmp;

1989. lua_assert(irref_isk(ref));  /* LJ_SOFTFP: must be a number constant. */

1990. tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo,

1991. rset_exclude(RSET_GPREVEN, RID_BASE));

1992. emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs);

1993. if (rset_test(as->freeset, tmp+1)) odd = RID2RSET(tmp+1);

1994. tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, odd);

1995. emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs+4);

1996. #else

1997. Reg src = ra_alloc1(as, ref, RSET_FPR);

1998. emit_vlso(as, ARMI_VSTR_D, src, RID_BASE, ofs);

1999. #endif

2000. } else {

2001. RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE);

2002. Reg type;

2003. lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t));

2004. if (!irt_ispri(ir->t)) {

2005. Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPREVEN, RID_BASE));

2006. emit_lso(as, ARMI_STR, src, RID_BASE, ofs);

2007. if (rset_test(as->freeset, src+1)) odd = RID2RSET(src+1);

2008. }

2009. if ((sn & (SNAP_CONT|SNAP_FRAME))) {

2010. if (s == 0) continue;  /* Do not overwrite link to previous frame. */

2011. type = ra_allock(as, (int32_t)(*flinks--), odd);

2012. #if LJ_SOFTFP

2013. } else if ((sn & SNAP_SOFTFPNUM)) {

2014. type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPRODD, RID_BASE));

2015. #endif

2016. } else {

2017. type = ra_allock(as, (int32_t)irt_toitype(ir->t), odd);

2018. }

2019. emit_lso(as, ARMI_STR, type, RID_BASE, ofs+4);

2020. }

2021. checkmclim(as);

2022. }

2023. lua_assert(map + nent == flinks);

2024. }

2026. /* -- GC handling --------------------------------------------------------- */

2028. /* Check GC threshold and do one or more GC steps. */

2029. ‌static void asm_gc_check(ASMState *as)

2030. {

2031. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];

2032. IRRef args[2];

2033. MCLabel l_end;

2034. Reg tmp1, tmp2;

2035. ra_evictset(as, RSET_SCRATCH);

2036. l_end = emit_label(as);

2037. /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */

2038. asm_guardcc(as, CC_NE);  /* Assumes asm_snap_prep() already done. */

2039. emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET);

2040. args[0] = ASMREF_TMP1;  /* global_State *g */

2041. args[1] = ASMREF_TMP2;  /* MSize steps     */

2042. asm_gencall(as, ci, args);

2043. tmp1 = ra_releasetmp(as, ASMREF_TMP1);

2044. tmp2 = ra_releasetmp(as, ASMREF_TMP2);

2045. emit_loadi(as, tmp2, as->gcsteps);

2046. /* Jump around GC step if GC total < GC threshold. */

2047. emit_branch(as, ARMF_CC(ARMI_B, CC_LS), l_end);

2048. emit_nm(as, ARMI_CMP, RID_TMP, tmp2);

2049. emit_lso(as, ARMI_LDR, tmp2, tmp1,

2050. (int32_t)offsetof(global_State, gc.threshold));

2051. emit_lso(as, ARMI_LDR, RID_TMP, tmp1,

2052. (int32_t)offsetof(global_State, gc.total));

2053. ra_allockreg(as, i32ptr(J2G(as->J)), tmp1);

2054. as->gcsteps = 0;

2055. checkmclim(as);

2056. }

2058. /* -- Loop handling ------------------------------------------------------- */

2060. /* Fixup the loop branch. */

2061. ‌static void asm_loop_fixup(ASMState *as)

2062. {

2063. MCode *p = as->mctop;

2064. MCode *target = as->mcp;

2065. if (as->loopinv) {  /* Inverted loop branch? */

2066. /* asm_guardcc already inverted the bcc and patched the final bl. */

2067. p[-2] |= ((uint32_t)(target-p) & 0x00ffffffu);

2068. } else {

2069. p[-1] = ARMI_B | ((uint32_t)((target-p)-1) & 0x00ffffffu);

2070. }

2071. }

2073. /* -- Head of trace ------------------------------------------------------- */

2075. /* Reload L register from g->cur_L. */

2076. ‌static void asm_head_lreg(ASMState *as)

2077. {

2078. IRIns *ir = IR(ASMREF_L);

2079. if (ra_used(ir)) {

2080. Reg r = ra_dest(as, ir, RSET_GPR);

2081. emit_getgl(as, r, cur_L);

2082. ra_evictk(as);

2083. }

2084. }

2086. /* Coalesce BASE register for a root trace. */

2087. ‌static void asm_head_root_base(ASMState *as)

2088. {

2089. IRIns *ir;

2090. asm_head_lreg(as);

2091. ir = IR(REF_BASE);

2092. if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t)))

2093. ra_spill(as, ir);

2094. ra_destreg(as, ir, RID_BASE);

2095. }

2097. /* Coalesce BASE register for a side trace. */

2098. ‌static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow)

2099. {

2100. IRIns *ir;

2101. asm_head_lreg(as);

2102. ir = IR(REF_BASE);

2103. if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t)))

2104. ra_spill(as, ir);

2105. if (ra_hasspill(irp->s)) {

2106. rset_clear(allow, ra_dest(as, ir, allow));

2107. } else {

2108. Reg r = irp->r;

2109. lua_assert(ra_hasreg(r));

2110. rset_clear(allow, r);

2111. if (r != ir->r && !rset_test(as->freeset, r))

2112. ra_restore(as, regcost_ref(as->cost[r]));

2113. ra_destreg(as, ir, r);

2114. }

2115. return allow;

2116. }

2118. /* -- Tail of trace ------------------------------------------------------- */

2120. /* Fixup the tail code. */

2121. ‌static void asm_tail_fixup(ASMState *as, TraceNo lnk)

2122. {

2123. MCode *p = as->mctop;

2124. MCode *target;

2125. int32_t spadj = as->T->spadjust;

2126. if (spadj == 0) {

2127. as->mctop = --p;

2128. } else {

2129. /* Patch stack adjustment. */

2130. uint32_t k = emit_isk12(ARMI_ADD, spadj);

2131. lua_assert(k);

2132. p[-2] = (ARMI_ADD^k) | ARMF_D(RID_SP) | ARMF_N(RID_SP);

2133. }

2134. /* Patch exit branch. */

2135. target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp;

2136. p[-1] = ARMI_B|(((target-p)-1)&0x00ffffffu);

2137. }

2139. /* Prepare tail of code. */

2140. ‌static void asm_tail_prep(ASMState *as)

2141. {

2142. MCode *p = as->mctop - 1;  /* Leave room for exit branch. */

2143. if (as->loopref) {

2144. as->invmcp = as->mcp = p;

2145. } else {

2146. as->mcp = p-1;  /* Leave room for stack pointer adjustment. */

2147. as->invmcp = NULL;

2148. }

2149. *p = 0;  /* Prevent load/store merging. */

2150. }

2152. /* -- Trace setup --------------------------------------------------------- */

2154. /* Ensure there are enough stack slots for call arguments. */

2155. ‌static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)

2156. {

2157. IRRef args[CCI_NARGS_MAX*2];

2158. uint32_t i, nargs = CCI_XNARGS(ci);

2159. int nslots = 0, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR, fprodd = 0;

2160. asm_collectargs(as, ir, ci, args);

2161. for (i = 0; i < nargs; i++) {

2162. if (!LJ_SOFTFP && args[i] && irt_isfp(IR(args[i])->t)) {

2163. if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) {

2164. if (irt_isnum(IR(args[i])->t)) {

2165. if (nfpr > 0) nfpr--;

2166. else fprodd = 0, nslots = (nslots + 3) & ~1;

2167. } else {

2168. if (fprodd) fprodd--;

2169. else if (nfpr > 0) fprodd = 1, nfpr--;

2170. else nslots++;

2171. }

2172. } else if (irt_isnum(IR(args[i])->t)) {

2173. ngpr &= ~1;

2174. if (ngpr > 0) ngpr -= 2; else nslots += 2;

2175. } else {

2176. if (ngpr > 0) ngpr--; else nslots++;

2177. }

2178. } else {

2179. if (ngpr > 0) ngpr--; else nslots++;

2180. }

2181. }

2182. if (nslots > as->evenspill)  /* Leave room for args in stack slots. */

2183. as->evenspill = nslots;

2184. return REGSP_HINT(RID_RET);

2185. }

2187. ‌static void asm_setup_target(ASMState *as)

2188. {

2189. /* May need extra exit for asm_stack_check on side traces. */

2190. asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0));

2191. }

2193. /* -- Trace patching ------------------------------------------------------ */

2195. /* Patch exit jumps of existing machine code to a new target. */

2196. ‌void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)

2197. {

2198. MCode *p = T->mcode;

2199. MCode *pe = (MCode *)((char *)p + T->szmcode);

2200. MCode *cstart = NULL, *cend = p;

2201. MCode *mcarea = lj_mcode_patch(J, p, 0);

2202. MCode *px = exitstub_addr(J, exitno) - 2;

2203. for (; p < pe; p++) {

2204. /* Look for bl_cc exitstub, replace with b_cc target. */

2205. uint32_t ins = *p;

2206. if ((ins & 0x0f000000u) == 0x0b000000u && ins < 0xf0000000u &&

2207. ((ins ^ (px-p)) & 0x00ffffffu) == 0) {

2208. *p = (ins & 0xfe000000u) | (((target-p)-2) & 0x00ffffffu);

2209. cend = p+1;

2210. if (!cstart) cstart = p;

2211. }

2212. }

2213. lua_assert(cstart != NULL);

2214. lj_mcode_sync(cstart, cend);

2215. lj_mcode_patch(J, mcarea, 1);

2216. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_mcode.h - luajit-2.0-src

Macros defined

- _LJ_MCODE_H

- lj_mcode_commitbot

Source code

1. /*

2. ** Machine code management.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_MCODE_H

7. ‌#define _LJ_MCODE_H

9. #include "lj_obj.h"

11. #if LJ_HASJIT || LJ_HASFFI

12. LJ_FUNC void lj_mcode_sync(void *start, void *end);

13. #endif

15. #if LJ_HASJIT

17. #include "lj_jit.h"

19. LJ_FUNC void lj_mcode_free(jit_State *J);

20. LJ_FUNC MCode *lj_mcode_reserve(jit_State *J, MCode **lim);

21. LJ_FUNC void lj_mcode_commit(jit_State *J, MCode *m);

22. LJ_FUNC void lj_mcode_abort(jit_State *J);

23. LJ_FUNC MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish);

24. LJ_FUNC_NORET void lj_mcode_limiterr(jit_State *J, size_t need);

26. ‌#define lj_mcode_commitbot(J, m)        (J->mcbot = (m))

28. #endif

30. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_emit_arm.h - luajit-2.0-src

Global variables defined

- emit_invai

Data types defined

- MCLabel

Functions defined

- emit_addptr

- emit_branch

- emit_call

- emit_d

- emit_dm

- emit_dn

- emit_dnm

- emit_isk12

- emit_kdelta1

- emit_kdelta2

- emit_loadi

- emit_loadn

- emit_loadofs

- emit_lso

- emit_lsox

- emit_lsptr

- emit_m

- emit_movrr

- emit_n

- emit_nm

- emit_opk

- emit_storeofs

- emit_vlso

Macros defined

- emit_canremat

- emit_getgl

- emit_jmp

- emit_label

- emit_loada

- emit_setgl

- emit_setvmstate

- emit_spsub

Source code

1. /*

2. ** ARM instruction emitter.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* -- Constant encoding --------------------------------------------------- */

8. ‌static uint8_t emit_invai[16] = {

9. /* AND */ (ARMI_AND^ARMI_BIC) >> 21,

10. /* EOR */ 0,

11. /* SUB */ (ARMI_SUB^ARMI_ADD) >> 21,

12. /* RSB */ 0,

13. /* ADD */ (ARMI_ADD^ARMI_SUB) >> 21,

14. /* ADC */ (ARMI_ADC^ARMI_SBC) >> 21,

15. /* SBC */ (ARMI_SBC^ARMI_ADC) >> 21,

16. /* RSC */ 0,

17. /* TST */ 0,

18. /* TEQ */ 0,

19. /* CMP */ (ARMI_CMP^ARMI_CMN) >> 21,

20. /* CMN */ (ARMI_CMN^ARMI_CMP) >> 21,

21. /* ORR */ 0,

22. /* MOV */ (ARMI_MOV^ARMI_MVN) >> 21,

23. /* BIC */ (ARMI_BIC^ARMI_AND) >> 21,

24. /* MVN */ (ARMI_MVN^ARMI_MOV) >> 21

25. };

27. /* Encode constant in K12 format for data processing instructions. */

28. ‌static uint32_t emit_isk12(ARMIns ai, int32_t n)

29. {

30. uint32_t invai, i, m = (uint32_t)n;

31. /* K12: unsigned 8 bit value, rotated in steps of two bits. */

32. for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2))

33. if (m <= 255) return ARMI_K12|m|i;

34. /* Otherwise try negation/complement with the inverse instruction. */

35. invai = emit_invai[((ai >> 21) & 15)];

36. if (!invai) return 0;  /* Failed. No inverse instruction. */

37. m = ~(uint32_t)n;

38. if (invai == ((ARMI_SUB^ARMI_ADD) >> 21) ||

39. invai == (ARMI_CMP^ARMI_CMN) >> 21) m++;

40. for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2))

41. if (m <= 255) return ARMI_K12|(invai<<21)|m|i;

42. return 0;  /* Failed. */

43. }

45. /* -- Emit basic instructions --------------------------------------------- */

47. ‌static void emit_dnm(ASMState *as, ARMIns ai, Reg rd, Reg rn, Reg rm)

48. {

49. *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn) | ARMF_M(rm);

50. }

52. ‌static void emit_dm(ASMState *as, ARMIns ai, Reg rd, Reg rm)

53. {

54. *--as->mcp = ai | ARMF_D(rd) | ARMF_M(rm);

55. }

57. ‌static void emit_dn(ASMState *as, ARMIns ai, Reg rd, Reg rn)

58. {

59. *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn);

60. }

62. ‌static void emit_nm(ASMState *as, ARMIns ai, Reg rn, Reg rm)

63. {

64. *--as->mcp = ai | ARMF_N(rn) | ARMF_M(rm);

65. }

67. ‌static void emit_d(ASMState *as, ARMIns ai, Reg rd)

68. {

69. *--as->mcp = ai | ARMF_D(rd);

70. }

72. ‌static void emit_n(ASMState *as, ARMIns ai, Reg rn)

73. {

74. *--as->mcp = ai | ARMF_N(rn);

75. }

77. ‌static void emit_m(ASMState *as, ARMIns ai, Reg rm)

78. {

79. *--as->mcp = ai | ARMF_M(rm);

80. }

82. ‌static void emit_lsox(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs)

83. {

84. lua_assert(ofs >= -255 && ofs <= 255);

85. if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U;

86. *--as->mcp = ai | ARMI_LS_P | ARMI_LSX_I | ARMF_D(rd) | ARMF_N(rn) |

87. ((ofs & 0xf0) << 4) | (ofs & 0x0f);

88. }

90. ‌static void emit_lso(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs)

91. {

92. lua_assert(ofs >= -4095 && ofs <= 4095);

93. /* Combine LDR/STR pairs to LDRD/STRD. */

94. if (*as->mcp == (ai|ARMI_LS_P|ARMI_LS_U|ARMF_D(rd^1)|ARMF_N(rn)|(ofs^4)) &&

95. (ai & ~(ARMI_LDR^ARMI_STR)) == ARMI_STR && rd != rn &&

96. (uint32_t)ofs <= 252 && !(ofs & 3) && !((rd ^ (ofs >>2)) & 1) &&

97. as->mcp != as->mcloop) {

98. as->mcp++;

99. emit_lsox(as, ai == ARMI_LDR ? ARMI_LDRD : ARMI_STRD, rd&~1, rn, ofs&~4);

100. return;

101. }

102. if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U;

103. *--as->mcp = ai | ARMI_LS_P | ARMF_D(rd) | ARMF_N(rn) | ofs;

104. }

106. #if !LJ_SOFTFP

107. ‌static void emit_vlso(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs)

108. {

109. lua_assert(ofs >= -1020 && ofs <= 1020 && (ofs&3) == 0);

110. if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U;

111. *--as->mcp = ai | ARMI_LS_P | ARMF_D(rd & 15) | ARMF_N(rn) | (ofs >> 2);

112. }

113. #endif

115. /* -- Emit loads/stores --------------------------------------------------- */

117. /* Prefer spills of BASE/L. */

118. ‌#define emit_canremat(ref)        ((ref) < ASMREF_L)

120. /* Try to find a one step delta relative to another constant. */

121. ‌static int emit_kdelta1(ASMState *as, Reg d, int32_t i)

122. {

123. RegSet work = ~as->freeset & RSET_GPR;

124. while (work) {

125. Reg r = rset_picktop(work);

126. IRRef ref = regcost_ref(as->cost[r]);

127. lua_assert(r != d);

128. if (emit_canremat(ref)) {

129. int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i);

130. uint32_t k = emit_isk12(ARMI_ADD, delta);

131. if (k) {

132. if (k == ARMI_K12)

133. emit_dm(as, ARMI_MOV, d, r);

134. else

135. emit_dn(as, ARMI_ADD^k, d, r);

136. return 1;

137. }

138. }

139. rset_clear(work, r);

140. }

141. return 0;  /* Failed. */

142. }

144. /* Try to find a two step delta relative to another constant. */

145. ‌static int emit_kdelta2(ASMState *as, Reg d, int32_t i)

146. {

147. RegSet work = ~as->freeset & RSET_GPR;

148. while (work) {

149. Reg r = rset_picktop(work);

150. IRRef ref = regcost_ref(as->cost[r]);

151. lua_assert(r != d);

152. if (emit_canremat(ref)) {

153. int32_t other = ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i;

154. if (other) {

155. int32_t delta = i - other;

156. uint32_t sh, inv = 0, k2, k;

157. if (delta < 0) { delta = -delta; inv = ARMI_ADD^ARMI_SUB; }

158. sh = lj_ffs(delta) & ~1;

159. k2 = emit_isk12(0, delta & (255 << sh));

160. k = emit_isk12(0, delta & ~(255 << sh));

161. if (k) {

162. emit_dn(as, ARMI_ADD^k2^inv, d, d);

163. emit_dn(as, ARMI_ADD^k^inv, d, r);

164. return 1;

165. }

166. }

167. }

168. rset_clear(work, r);

169. }

170. return 0;  /* Failed. */

171. }

173. /* Load a 32 bit constant into a GPR. */

174. ‌static void emit_loadi(ASMState *as, Reg r, int32_t i)

175. {

176. uint32_t k = emit_isk12(ARMI_MOV, i);

177. lua_assert(rset_test(as->freeset, r) || r == RID_TMP);

178. if (k) {

179. /* Standard K12 constant. */

180. emit_d(as, ARMI_MOV^k, r);

181. } else if ((as->flags & JIT_F_ARMV6T2) && (uint32_t)i < 0x00010000u) {

182. /* 16 bit loword constant for ARMv6T2. */

183. emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), r);

184. } else if (emit_kdelta1(as, r, i)) {

185. /* One step delta relative to another constant. */

186. } else if ((as->flags & JIT_F_ARMV6T2)) {

187. /* 32 bit hiword/loword constant for ARMv6T2. */

188. emit_d(as, ARMI_MOVT|((i>>16) & 0x0fff)|(((i>>16) & 0xf000)<<4), r);

189. emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), r);

190. } else if (emit_kdelta2(as, r, i)) {

191. /* Two step delta relative to another constant. */

192. } else {

193. /* Otherwise construct the constant with up to 4 instructions. */

194. /* NYI: use mvn+bic, use pc-relative loads. */

195. for (;;) {

196. uint32_t sh = lj_ffs(i) & ~1;

197. int32_t m = i & (255 << sh);

198. i &= ~(255 << sh);

199. if (i == 0) {

200. emit_d(as, ARMI_MOV ^ emit_isk12(0, m), r);

201. break;

202. }

203. emit_dn(as, ARMI_ORR ^ emit_isk12(0, m), r, r);

204. }

205. }

206. }

208. ‌#define emit_loada(as, r, addr)                emit_loadi(as, (r), i32ptr((addr)))

210. static Reg ra_allock(ASMState *as, int32_t k, RegSet allow);

212. /* Get/set from constant pointer. */

213. ‌static void emit_lsptr(ASMState *as, ARMIns ai, Reg r, void *p)

214. {

215. int32_t i = i32ptr(p);

216. emit_lso(as, ai, r, ra_allock(as, (i & ~4095), rset_exclude(RSET_GPR, r)),

217. (i & 4095));

218. }

220. #if !LJ_SOFTFP

221. /* Load a number constant into an FPR. */

222. ‌static void emit_loadn(ASMState *as, Reg r, cTValue *tv)

223. {

224. int32_t i;

225. if ((as->flags & JIT_F_VFPV3) && !tv->u32.lo) {

226. uint32_t hi = tv->u32.hi;

227. uint32_t b = ((hi >> 22) & 0x1ff);

228. if (!(hi & 0xffff) && (b == 0x100 || b == 0x0ff)) {

229. *--as->mcp = ARMI_VMOVI_D | ARMF_D(r & 15) |

230. ((tv->u32.hi >> 12) & 0x00080000) |

231. ((tv->u32.hi >> 4) & 0x00070000) |

232. ((tv->u32.hi >> 16) & 0x0000000f);

233. return;

234. }

235. }

236. i = i32ptr(tv);

237. emit_vlso(as, ARMI_VLDR_D, r,

238. ra_allock(as, (i & ~1020), RSET_GPR), (i & 1020));

239. }

240. #endif

242. /* Get/set global_State fields. */

243. ‌#define emit_getgl(as, r, field) \

244. emit_lsptr(as, ARMI_LDR, (r), (void *)&J2G(as->J)->field)

245. ‌#define emit_setgl(as, r, field) \

246. emit_lsptr(as, ARMI_STR, (r), (void *)&J2G(as->J)->field)

248. /* Trace number is determined from pc of exit instruction. */

249. ‌#define emit_setvmstate(as, i)                UNUSED(i)

251. /* -- Emit control-flow instructions -------------------------------------- */

253. /* Label for internal jumps. */

254. ‌typedef MCode *MCLabel;

256. /* Return label pointing to current PC. */

257. ‌#define emit_label(as)                ((as)->mcp)

259. ‌static void emit_branch(ASMState *as, ARMIns ai, MCode *target)

260. {

261. MCode *p = as->mcp;

262. ptrdiff_t delta = (target - p) - 1;

263. lua_assert(((delta + 0x00800000) >> 24) == 0);

264. *--p = ai | ((uint32_t)delta & 0x00ffffffu);

265. as->mcp = p;

266. }

268. ‌#define emit_jmp(as, target) emit_branch(as, ARMI_B, (target))

270. ‌static void emit_call(ASMState *as, void *target)

271. {

272. MCode *p = --as->mcp;

273. ptrdiff_t delta = ((char *)target - (char *)p) - 8;

274. if ((((delta>>2) + 0x00800000) >> 24) == 0) {

275. if ((delta & 1))

276. *p = ARMI_BLX | ((uint32_t)(delta>>2) & 0x00ffffffu) | ((delta&2) << 27);

277. else

278. *p = ARMI_BL | ((uint32_t)(delta>>2) & 0x00ffffffu);

279. } else {  /* Target out of range: need indirect call. But don't use R0-R3. */

280. Reg r = ra_allock(as, i32ptr(target), RSET_RANGE(RID_R4, RID_R12+1));

281. *p = ARMI_BLXr | ARMF_M(r);

282. }

283. }

285. /* -- Emit generic operations --------------------------------------------- */

287. /* Generic move between two regs. */

288. ‌static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src)

289. {

290. #if LJ_SOFTFP

291. lua_assert(!irt_isnum(ir->t)); UNUSED(ir);

292. #else

293. if (dst >= RID_MAX_GPR) {

294. emit_dm(as, irt_isnum(ir->t) ? ARMI_VMOV_D : ARMI_VMOV_S,

295. (dst & 15), (src & 15));

296. return;

297. }

298. #endif

299. if (as->mcp != as->mcloop) {  /* Swap early registers for loads/stores. */

300. MCode ins = *as->mcp, swp = (src^dst);

301. if ((ins & 0x0c000000) == 0x04000000 && (ins & 0x02000010) != 0x02000010) {

302. if (!((ins ^ (dst << 16)) & 0x000f0000))

303. *as->mcp = ins ^ (swp << 16);  /* Swap N in load/store. */

304. if (!(ins & 0x00100000) && !((ins ^ (dst << 12)) & 0x0000f000))

305. *as->mcp = ins ^ (swp << 12);  /* Swap D in store. */

306. }

307. }

308. emit_dm(as, ARMI_MOV, dst, src);

309. }

311. /* Generic load of register with base and (small) offset address. */

312. ‌static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)

313. {

314. #if LJ_SOFTFP

315. lua_assert(!irt_isnum(ir->t)); UNUSED(ir);

316. #else

317. if (r >= RID_MAX_GPR)

318. emit_vlso(as, irt_isnum(ir->t) ? ARMI_VLDR_D : ARMI_VLDR_S, r, base, ofs);

319. else

320. #endif

321. emit_lso(as, ARMI_LDR, r, base, ofs);

322. }

324. /* Generic store of register with base and (small) offset address. */

325. ‌static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)

326. {

327. #if LJ_SOFTFP

328. lua_assert(!irt_isnum(ir->t)); UNUSED(ir);

329. #else

330. if (r >= RID_MAX_GPR)

331. emit_vlso(as, irt_isnum(ir->t) ? ARMI_VSTR_D : ARMI_VSTR_S, r, base, ofs);

332. else

333. #endif

334. emit_lso(as, ARMI_STR, r, base, ofs);

335. }

337. /* Emit an arithmetic/logic operation with a constant operand. */

338. ‌static void emit_opk(ASMState *as, ARMIns ai, Reg dest, Reg src,

339. int32_t i, RegSet allow)

340. {

341. uint32_t k = emit_isk12(ai, i);

342. if (k)

343. emit_dn(as, ai^k, dest, src);

344. else

345. emit_dnm(as, ai, dest, src, ra_allock(as, i, allow));

346. }

348. /* Add offset to pointer. */

349. ‌static void emit_addptr(ASMState *as, Reg r, int32_t ofs)

350. {

351. if (ofs)

352. emit_opk(as, ARMI_ADD, r, r, ofs, rset_exclude(RSET_GPR, r));

353. }

355. ‌#define emit_spsub(as, ofs)        emit_addptr(as, RID_SP, -(ofs))

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_obj.c - luajit-2.0-src

Global variables defined

- lj_obj_itypename

- lj_obj_typename

Functions defined

- lj_obj_equal

- lj_obj_ptr

Macros defined

- LUA_CORE

- lj_obj_c

Source code

1. /*

2. ** Miscellaneous object handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_obj_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. /* Object type names. */

12. ‌LJ_DATADEF const char *const lj_obj_typename[] = {  /* ORDER LUA_T */

13. "no value", "nil", "boolean", "userdata", "number", "string",

14. "table", "function", "userdata", "thread", "proto", "cdata"

15. };

17. ‌LJ_DATADEF const char *const lj_obj_itypename[] = {  /* ORDER LJ_T */

18. "nil", "boolean", "boolean", "userdata", "string", "upval", "thread",

19. "proto", "function", "trace", "cdata", "table", "userdata", "number"

20. };

22. /* Compare two objects without calling metamethods. */

23. ‌int LJ_FASTCALL lj_obj_equal(cTValue *o1, cTValue *o2)

24. {

25. if (itype(o1) == itype(o2)) {

26. if (tvispri(o1))

27. return 1;

28. if (!tvisnum(o1))

29. return gcrefeq(o1->gcr, o2->gcr);

30. } else if (!tvisnumber(o1) || !tvisnumber(o2)) {

31. return 0;

32. }

33. return numberVnum(o1) == numberVnum(o2);

34. }

36. /* Return pointer to object or its object data. */

37. ‌const void * LJ_FASTCALL lj_obj_ptr(cTValue *o)

38. {

39. if (tvisudata(o))

40. return uddata(udataV(o));

41. else if (tvislightud(o))

42. return lightudV(o);

43. else if (LJ_HASFFI && tviscdata(o))

44. return cdataptr(cdataV(o));

45. else if (tvisgcv(o))

46. return gcV(o);

47. else

48. return NULL;

49. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_target_ppc.h - luajit-2.0-src

Data types defined

- ExitState

- PPCCC

- PPCCC

- PPCIns

- PPCIns

Functions defined

- exitstub_trace_addr_

Macros defined

- EXITSTATE_CHECKEXIT

- FPRDEF

- GPRDEF

- PPCF_A

- PPCF_B

- PPCF_C

- PPCF_CC

- PPCF_DOT

- PPCF_MB

- PPCF_ME

- PPCF_T

- PPCF_Y

- REGARG_FIRSTFPR

- REGARG_FIRSTGPR

- REGARG_LASTFPR

- REGARG_LASTGPR

- REGARG_NUMFPR

- REGARG_NUMGPR

- RIDENUM

- RID_MIN_KREF

- RID_NUM_KREF

- RSET_ALL

- RSET_FIXED

- RSET_FPR

- RSET_GPR

- RSET_INIT

- RSET_SCRATCH

- RSET_SCRATCH_FPR

- RSET_SCRATCH_GPR

- SPOFS_TMP

- SPOFS_TMPHI

- SPOFS_TMPLO

- SPOFS_TMPW

- SPS_FIRST

- SPS_FIXED

- VRIDDEF

- _LJ_TARGET_PPC_H

- exitstub_trace_addr

- sps_align

- sps_scale

Source code

1. /*

2. ** Definitions for PPC CPUs.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_TARGET_PPC_H

7. ‌#define _LJ_TARGET_PPC_H

9. /* -- Registers IDs ------------------------------------------------------- */

11. ‌#define GPRDEF(_) \

12. _(R0) _(SP) _(SYS1) _(R3) _(R4) _(R5) _(R6) _(R7) \

13. _(R8) _(R9) _(R10) _(R11) _(R12) _(SYS2) _(R14) _(R15) \

14. _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \

15. _(R24) _(R25) _(R26) _(R27) _(R28) _(R29) _(R30) _(R31)

16. ‌#define FPRDEF(_) \

17. _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \

18. _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \

19. _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \

20. _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31)

21. ‌#define VRIDDEF(_)

23. ‌#define RIDENUM(name)        RID_##name,

25. enum {

26. GPRDEF(RIDENUM)                /* General-purpose registers (GPRs). */

27. FPRDEF(RIDENUM)                /* Floating-point registers (FPRs). */

28. RID_MAX,

29. RID_TMP = RID_R0,

31. /* Calling conventions. */

32. RID_RET = RID_R3,

33. RID_RETHI = RID_R3,

34. RID_RETLO = RID_R4,

35. RID_FPRET = RID_F1,

37. /* These definitions must match with the *.dasc file(s): */

38. RID_BASE = RID_R14,                /* Interpreter BASE. */

39. RID_LPC = RID_R16,                /* Interpreter PC. */

40. RID_DISPATCH = RID_R17,        /* Interpreter DISPATCH table. */

41. RID_LREG = RID_R18,                /* Interpreter L. */

42. RID_JGL = RID_R31,                /* On-trace: global_State + 32768. */

44. /* Register ranges [min, max) and number of registers. */

45. RID_MIN_GPR = RID_R0,

46. RID_MAX_GPR = RID_R31+1,

47. RID_MIN_FPR = RID_F0,

48. RID_MAX_FPR = RID_F31+1,

49. RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR,

50. RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR

51. };

53. ‌#define RID_NUM_KREF                RID_NUM_GPR

54. ‌#define RID_MIN_KREF                RID_R0

56. /* -- Register sets ------------------------------------------------------- */

58. /* Make use of all registers, except TMP, SP, SYS1, SYS2 and JGL. */

59. ‌#define RSET_FIXED \

60. (RID2RSET(RID_TMP)|RID2RSET(RID_SP)|RID2RSET(RID_SYS1)|\

61. RID2RSET(RID_SYS2)|RID2RSET(RID_JGL))

62. ‌#define RSET_GPR        (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED)

63. ‌#define RSET_FPR        RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)

64. ‌#define RSET_ALL        (RSET_GPR|RSET_FPR)

65. ‌#define RSET_INIT        RSET_ALL

67. ‌#define RSET_SCRATCH_GPR        (RSET_RANGE(RID_R3, RID_R12+1))

68. ‌#define RSET_SCRATCH_FPR        (RSET_RANGE(RID_F0, RID_F13+1))

69. ‌#define RSET_SCRATCH                (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR)

70. ‌#define REGARG_FIRSTGPR                RID_R3

71. ‌#define REGARG_LASTGPR                RID_R10

72. ‌#define REGARG_NUMGPR                8

73. ‌#define REGARG_FIRSTFPR                RID_F1

74. ‌#define REGARG_LASTFPR                RID_F8

75. ‌#define REGARG_NUMFPR                8

77. /* -- Spill slots --------------------------------------------------------- */

79. /* Spill slots are 32 bit wide. An even/odd pair is used for FPRs.

80. **

81. ** SPS_FIXED: Available fixed spill slots in interpreter frame.

82. ** This definition must match with the *.dasc file(s).

83. **

84. ** SPS_FIRST: First spill slot for general use.

85. ** [sp+12] tmplo word \

86. ** [sp+ 8] tmphi word / tmp dword, parameter area for callee

87. ** [sp+ 4] tmpw, LR of callee

88. ** [sp+ 0] stack chain

89. */

90. ‌#define SPS_FIXED        7

91. ‌#define SPS_FIRST        4

93. /* Stack offsets for temporary slots. Used for FP<->int conversions etc. */

94. ‌#define SPOFS_TMPW        4

95. ‌#define SPOFS_TMP        8

96. ‌#define SPOFS_TMPHI        8

97. ‌#define SPOFS_TMPLO        12

99. ‌#define sps_scale(slot)                (4 * (int32_t)(slot))

100. ‌#define sps_align(slot)                (((slot) - SPS_FIXED + 3) & ~3)

102. /* -- Exit state ---------------------------------------------------------- */

104. /* This definition must match with the *.dasc file(s). */

105. typedef struct {

106. lua_Number fpr[RID_NUM_FPR];        /* Floating-point registers. */

107. intptr_t gpr[RID_NUM_GPR];        /* General-purpose registers. */

108. int32_t spill[256];                /* Spill slots. */

109. ‌} ExitState;

111. /* Highest exit + 1 indicates stack check. */

112. ‌#define EXITSTATE_CHECKEXIT        1

114. /* Return the address of a per-trace exit stub. */

115. ‌static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p, uint32_t exitno)

116. {

117. while (*p == 0x60000000) p++;  /* Skip PPCI_NOP. */

118. return p + 3 + exitno;

119. }

120. /* Avoid dependence on lj_jit.h if only including lj_target.h. */

121. ‌#define exitstub_trace_addr(T, exitno) \

122. exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode), (exitno))

124. /* -- Instructions -------------------------------------------------------- */

126. /* Instruction fields. */

127. ‌#define PPCF_CC(cc)        ((((cc) & 3) << 16) | (((cc) & 4) << 22))

128. ‌#define PPCF_T(r)        ((r) << 21)

129. ‌#define PPCF_A(r)        ((r) << 16)

130. ‌#define PPCF_B(r)        ((r) << 11)

131. ‌#define PPCF_C(r)        ((r) << 6)

132. ‌#define PPCF_MB(n)        ((n) << 6)

133. ‌#define PPCF_ME(n)        ((n) << 1)

134. ‌#define PPCF_Y                0x00200000

135. ‌#define PPCF_DOT        0x00000001

137. ‌typedef enum PPCIns {

138. /* Integer instructions. */

139. PPCI_MR = 0x7c000378,

140. PPCI_NOP = 0x60000000,

142. PPCI_LI = 0x38000000,

143. PPCI_LIS = 0x3c000000,

145. PPCI_ADD = 0x7c000214,

146. PPCI_ADDC = 0x7c000014,

147. PPCI_ADDO = 0x7c000614,

148. PPCI_ADDE = 0x7c000114,

149. PPCI_ADDZE = 0x7c000194,

150. PPCI_ADDME = 0x7c0001d4,

151. PPCI_ADDI = 0x38000000,

152. PPCI_ADDIS = 0x3c000000,

153. PPCI_ADDIC = 0x30000000,

154. PPCI_ADDICDOT = 0x34000000,

156. PPCI_SUBF = 0x7c000050,

157. PPCI_SUBFC = 0x7c000010,

158. PPCI_SUBFO = 0x7c000450,

159. PPCI_SUBFE = 0x7c000110,

160. PPCI_SUBFZE = 0x7c000190,

161. PPCI_SUBFME = 0x7c0001d0,

162. PPCI_SUBFIC = 0x20000000,

164. PPCI_NEG = 0x7c0000d0,

166. PPCI_AND = 0x7c000038,

167. PPCI_ANDC = 0x7c000078,

168. PPCI_NAND = 0x7c0003b8,

169. PPCI_ANDIDOT = 0x70000000,

170. PPCI_ANDISDOT = 0x74000000,

172. PPCI_OR = 0x7c000378,

173. PPCI_NOR = 0x7c0000f8,

174. PPCI_ORI = 0x60000000,

175. PPCI_ORIS = 0x64000000,

177. PPCI_XOR = 0x7c000278,

178. PPCI_EQV = 0x7c000238,

179. PPCI_XORI = 0x68000000,

180. PPCI_XORIS = 0x6c000000,

182. PPCI_CMPW = 0x7c000000,

183. PPCI_CMPLW = 0x7c000040,

184. PPCI_CMPWI = 0x2c000000,

185. PPCI_CMPLWI = 0x28000000,

187. PPCI_MULLW = 0x7c0001d6,

188. PPCI_MULLI = 0x1c000000,

189. PPCI_MULLWO = 0x7c0005d6,

191. PPCI_EXTSB = 0x7c000774,

192. PPCI_EXTSH = 0x7c000734,

194. PPCI_SLW = 0x7c000030,

195. PPCI_SRW = 0x7c000430,

196. PPCI_SRAW = 0x7c000630,

197. PPCI_SRAWI = 0x7c000670,

199. PPCI_RLWNM = 0x5c000000,

200. PPCI_RLWINM = 0x54000000,

201. PPCI_RLWIMI = 0x50000000,

203. PPCI_B = 0x48000000,

204. PPCI_BL = 0x48000001,

205. PPCI_BC = 0x40800000,

206. PPCI_BCL = 0x40800001,

207. PPCI_BCTR = 0x4e800420,

208. PPCI_BCTRL = 0x4e800421,

210. PPCI_CRANDC = 0x4c000102,

211. PPCI_CRXOR = 0x4c000182,

212. PPCI_CRAND = 0x4c000202,

213. PPCI_CREQV = 0x4c000242,

214. PPCI_CRORC = 0x4c000342,

215. PPCI_CROR = 0x4c000382,

217. PPCI_MFLR = 0x7c0802a6,

218. PPCI_MTCTR = 0x7c0903a6,

220. PPCI_MCRXR = 0x7c000400,

222. /* Load/store instructions. */

223. PPCI_LWZ = 0x80000000,

224. PPCI_LBZ = 0x88000000,

225. PPCI_STW = 0x90000000,

226. PPCI_STB = 0x98000000,

227. PPCI_LHZ = 0xa0000000,

228. PPCI_LHA = 0xa8000000,

229. PPCI_STH = 0xb0000000,

231. PPCI_STWU = 0x94000000,

233. PPCI_LFS = 0xc0000000,

234. PPCI_LFD = 0xc8000000,

235. PPCI_STFS = 0xd0000000,

236. PPCI_STFD = 0xd8000000,

238. PPCI_LWZX = 0x7c00002e,

239. PPCI_LBZX = 0x7c0000ae,

240. PPCI_STWX = 0x7c00012e,

241. PPCI_STBX = 0x7c0001ae,

242. PPCI_LHZX = 0x7c00022e,

243. PPCI_LHAX = 0x7c0002ae,

244. PPCI_STHX = 0x7c00032e,

246. PPCI_LWBRX = 0x7c00042c,

247. PPCI_STWBRX = 0x7c00052c,

249. PPCI_LFSX = 0x7c00042e,

250. PPCI_LFDX = 0x7c0004ae,

251. PPCI_STFSX = 0x7c00052e,

252. PPCI_STFDX = 0x7c0005ae,

254. /* FP instructions. */

255. PPCI_FMR = 0xfc000090,

256. PPCI_FNEG = 0xfc000050,

257. PPCI_FABS = 0xfc000210,

259. PPCI_FRSP = 0xfc000018,

260. PPCI_FCTIWZ = 0xfc00001e,

262. PPCI_FADD = 0xfc00002a,

263. PPCI_FSUB = 0xfc000028,

264. PPCI_FMUL = 0xfc000032,

265. PPCI_FDIV = 0xfc000024,

266. PPCI_FSQRT = 0xfc00002c,

268. PPCI_FMADD = 0xfc00003a,

269. PPCI_FMSUB = 0xfc000038,

270. PPCI_FNMSUB = 0xfc00003c,

272. PPCI_FCMPU = 0xfc000000,

273. PPCI_FSEL = 0xfc00002e,

274. ‌} PPCIns;

276. ‌typedef enum PPCCC {

277. CC_GE, CC_LE, CC_NE, CC_NS, CC_LT, CC_GT, CC_EQ, CC_SO

278. ‌} PPCCC;

280. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_alloc.c - luajit-2.0-src

Global variables defined

- ntavm

Data types defined

- PNTAVM

- bindex_t

- binmap_t

- flag_t

- malloc_chunk

- malloc_segment

- malloc_state

- malloc_tree_chunk

- mchunk

- mchunkptr

- msegment

- msegmentptr

- mstate

- sbinptr

- tbinptr

- tchunk

- tchunkptr

Functions defined

- static LJ_AINLINE void *CALL_MMAP(size_t size)

- static LJ_AINLINE void *CALL_MMAP(size_t size)

- static LJ_AINLINE void *CALL_MMAP(size_t size)

- static LJ_AINLINE void *CALL_MMAP(size_t size)

- static LJ_AINLINE void *CALL_MMAP(size_t size)

- static LJ_AINLINE void *CALL_MREMAP_(void *ptr, size_t osz, size_t nsz,

- static LJ_AINLINE int CALL_MUNMAP(void *ptr, size_t size)

- static LJ_AINLINE int CALL_MUNMAP(void *ptr, size_t size)

- static LJ_AINLINE void *DIRECT_MMAP(size_t size)

- static LJ_AINLINE void *DIRECT_MMAP(size_t size)

- static void INIT_MMAP(void)

- add_segment

- alloc_sys

- alloc_trim

- direct_alloc

- direct_resize

- has_segment_link

- init_bins

- init_top

- lj_alloc_create

- lj_alloc_destroy

- lj_alloc_f

- lj_alloc_free

- lj_alloc_malloc

- lj_alloc_realloc

- prepend_alloc

- release_unused_segments

- segment_holding

- tmalloc_large

- tmalloc_small

Macros defined

- CALL_MREMAP

- CALL_MREMAP

- CALL_MREMAP_MAYMOVE

- CALL_MREMAP_MV

- CALL_MREMAP_MV

- CALL_MREMAP_NOMOVE

- CHUNK_ALIGN_MASK

- CHUNK_OVERHEAD

- CINUSE_BIT

- CMFAIL

- DEFAULT_GRANULARITY

- DEFAULT_MMAP_THRESHOLD

- DEFAULT_TRIM_THRESHOLD

- DIRECT_CHUNK_OVERHEAD

- DIRECT_FOOT_PAD

- DIRECT_MMAP

- FENCEPOST_HEAD

- FOUR_SIZE_T_SIZES

- INIT_MMAP

- INIT_MMAP

- INUSE_BITS

- IS_DIRECT_BIT

- LUA_CORE

- MALLOC_ALIGNMENT

- MAP_ANONYMOUS

- MAX_RELEASE_CHECK_RATE

- MAX_REQUEST

- MAX_SIZE_T

- MAX_SMALL_REQUEST

- MAX_SMALL_SIZE

- MCHUNK_SIZE

- MFAIL

- MIN_CHUNK_SIZE

- MIN_LARGE_SIZE

- MIN_REQUEST

- MIN_SMALL_INDEX

- MMAP_FLAGS

- MMAP_PROT

- MMAP_REGION_END

- MMAP_REGION_START

- MMAP_REGION_START

- MMAP_REGION_START

- MMAP_REGION_START

- MMAP_REGION_START

- NSMALLBINS

- NTAVM_ZEROBITS

- NTREEBINS

- PINUSE_BIT

- SIX_SIZE_T_SIZES

- SIZE_T_BITSIZE

- SIZE_T_ONE

- SIZE_T_SIZE

- SIZE_T_TWO

- SIZE_T_ZERO

- SMALLBIN_SHIFT

- SMALLBIN_WIDTH

- TOP_FOOT_SIZE

- TREEBIN_SHIFT

- TWO_SIZE_T_SIZES

- WIN32_LEAN_AND_MEAN

- _GNU_SOURCE

- align_as_chunk

- align_offset

- bit_for_tree_index

- chunk2mem

- chunk_minus_offset

- chunk_plus_offset

- chunksize

- cinuse

- clear_cinuse

- clear_pinuse

- clear_smallmap

- clear_treemap

- compute_tree_index

- get_foot

- granularity_align

- idx2bit

- insert_chunk

- insert_large_chunk

- insert_small_chunk

- is_direct

- is_initialized

- is_small

- left_bits

- leftmost_child

- leftshift_for_tree_index

- lj_alloc_c

- mark_smallmap

- mark_treemap

- mem2chunk

- minsize_for_tree_index

- mmap_align

- mmap_align

- next_chunk

- next_pinuse

- overhead_for

- pad_request

- page_align

- pinuse

- prev_chunk

- replace_dv

- request2size

- segment_holds

- set_foot

- set_free_with_pinuse

- set_inuse

- set_inuse_and_pinuse

- set_size_and_pinuse_of_free_chunk

- set_size_and_pinuse_of_inuse_chunk

- small_index

- small_index2size

- smallbin_at

- smallmap_is_marked

- treebin_at

- treemap_is_marked

- unlink_chunk

- unlink_first_small_chunk

- unlink_large_chunk

- unlink_small_chunk

Source code

1. /*

2. ** Bundled memory allocator.

3. **

4. ** Beware: this is a HEAVILY CUSTOMIZED version of dlmalloc.

5. ** The original bears the following remark:

6. **

7. **   This is a version (aka dlmalloc) of malloc/free/realloc written by

8. **   Doug Lea and released to the public domain, as explained at

9. **   http://creativecommons.org/licenses/publicdomain.

10. **

11. **   * Version pre-2.8.4 Wed Mar 29 19:46:29 2006    (dl at gee)

12. **

13. ** No additional copyright is claimed over the customizations.

14. ** Please do NOT bother the original author about this version here!

15. **

16. ** If you want to use dlmalloc in another project, you should get

17. ** the original from: ftp://gee.cs.oswego.edu/pub/misc/

18. ** For thread-safe derivatives, take a look at:

19. ** - ptmalloc: http://www.malloc.de/

20. ** - nedmalloc: http://www.nedprod.com/programs/portable/nedmalloc/

21. */

23. ‌#define lj_alloc_c

24. ‌#define LUA_CORE

26. /* To get the mremap prototype. Must be defined before any system includes. */

27. #if defined(__linux__) && !defined(_GNU_SOURCE)

28. ‌#define _GNU_SOURCE

29. #endif

31. #include "lj_def.h"

32. #include "lj_arch.h"

33. #include "lj_alloc.h"

35. #ifndef LUAJIT_USE_SYSMALLOC

37. ‌#define MAX_SIZE_T                (~(size_t)0)

38. ‌#define MALLOC_ALIGNMENT        ((size_t)8U)

40. ‌#define DEFAULT_GRANULARITY        ((size_t)128U * (size_t)1024U)

41. ‌#define DEFAULT_TRIM_THRESHOLD        ((size_t)2U * (size_t)1024U * (size_t)1024U)

42. ‌#define DEFAULT_MMAP_THRESHOLD        ((size_t)128U * (size_t)1024U)

43. ‌#define MAX_RELEASE_CHECK_RATE        255

45. /* ------------------- size_t and alignment properties -------------------- */

47. /* The byte and bit size of a size_t */

48. ‌#define SIZE_T_SIZE                (sizeof(size_t))

49. ‌#define SIZE_T_BITSIZE                (sizeof(size_t) << 3)

51. /* Some constants coerced to size_t */

52. /* Annoying but necessary to avoid errors on some platforms */

53. ‌#define SIZE_T_ZERO                ((size_t)0)

54. ‌#define SIZE_T_ONE                ((size_t)1)

55. ‌#define SIZE_T_TWO                ((size_t)2)

56. ‌#define TWO_SIZE_T_SIZES        (SIZE_T_SIZE<<1)

57. ‌#define FOUR_SIZE_T_SIZES        (SIZE_T_SIZE<<2)

58. ‌#define SIX_SIZE_T_SIZES        (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES)

60. /* The bit mask value corresponding to MALLOC_ALIGNMENT */

61. ‌#define CHUNK_ALIGN_MASK        (MALLOC_ALIGNMENT - SIZE_T_ONE)

63. /* the number of bytes to offset an address to align it */

64. ‌#define align_offset(A)\

65. ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\

66. ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK))

68. /* -------------------------- MMAP support ------------------------------- */

70. ‌#define MFAIL                        ((void *)(MAX_SIZE_T))

71. ‌#define CMFAIL                        ((char *)(MFAIL)) /* defined for convenience */

73. ‌#define IS_DIRECT_BIT                (SIZE_T_ONE)

75. #if LJ_TARGET_WINDOWS

77. ‌#define WIN32_LEAN_AND_MEAN

78. #include <windows.h>

80. #if LJ_64 && !LJ_GC64

82. /* Undocumented, but hey, that's what we all love so much about Windows. */

83. ‌typedef long (*PNTAVM)(HANDLE handle, void **addr, ULONG zbits,

84. size_t *size, ULONG alloctype, ULONG prot);

85. ‌static PNTAVM ntavm;

87. /* Number of top bits of the lower 32 bits of an address that must be zero.

88. ** Apparently 0 gives us full 64 bit addresses and 1 gives us the lower 2GB.

89. */

90. ‌#define NTAVM_ZEROBITS                1

92. ‌static void INIT_MMAP(void)

93. {

94. ntavm = (PNTAVM)GetProcAddress(GetModuleHandleA("ntdll.dll"),

95. "NtAllocateVirtualMemory");

96. }

98. /* Win64 32 bit MMAP via NtAllocateVirtualMemory. */

99. ‌static LJ_AINLINE void *CALL_MMAP(size_t size)

100. {

101. DWORD olderr = GetLastError();

102. void *ptr = NULL;

103. long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size,

104. MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);

105. SetLastError(olderr);

106. return st == 0 ? ptr : MFAIL;

107. }

109. /* For direct MMAP, use MEM_TOP_DOWN to minimize interference */

110. ‌static LJ_AINLINE void *DIRECT_MMAP(size_t size)

111. {

112. DWORD olderr = GetLastError();

113. void *ptr = NULL;

114. long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size,

115. MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, PAGE_READWRITE);

116. SetLastError(olderr);

117. return st == 0 ? ptr : MFAIL;

118. }

120. #else

122. ‌#define INIT_MMAP()                ((void)0)

124. /* Win32 MMAP via VirtualAlloc */

125. ‌static LJ_AINLINE void *CALL_MMAP(size_t size)

126. {

127. DWORD olderr = GetLastError();

128. void *ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);

129. SetLastError(olderr);

130. return ptr ? ptr : MFAIL;

131. }

133. /* For direct MMAP, use MEM_TOP_DOWN to minimize interference */

134. ‌static LJ_AINLINE void *DIRECT_MMAP(size_t size)

135. {

136. DWORD olderr = GetLastError();

137. void *ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN,

138. PAGE_READWRITE);

139. SetLastError(olderr);

140. return ptr ? ptr : MFAIL;

141. }

143. #endif

145. /* This function supports releasing coalesed segments */

146. ‌static LJ_AINLINE int CALL_MUNMAP(void *ptr, size_t size)

147. {

148. DWORD olderr = GetLastError();

149. MEMORY_BASIC_INFORMATION minfo;

150. char *cptr = (char *)ptr;

151. while (size) {

152. if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0)

153. return -1;

154. if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr ||

155. minfo.State != MEM_COMMIT || minfo.RegionSize > size)

156. return -1;

157. if (VirtualFree(cptr, 0, MEM_RELEASE) == 0)

158. return -1;

159. cptr += minfo.RegionSize;

160. size -= minfo.RegionSize;

161. }

162. SetLastError(olderr);

163. return 0;

164. }

166. #else

168. #include <errno.h>

169. #include <sys/mman.h>

171. ‌#define MMAP_PROT                (PROT_READ|PROT_WRITE)

172. #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)

173. ‌#define MAP_ANONYMOUS                MAP_ANON

174. #endif

175. ‌#define MMAP_FLAGS                (MAP_PRIVATE|MAP_ANONYMOUS)

177. #if LJ_64 && !LJ_GC64

178. /* 64 bit mode with 32 bit pointers needs special support for allocating

179. ** memory in the lower 2GB.

180. */

182. #if defined(MAP_32BIT)

184. #if defined(__sun__)

185. ‌#define MMAP_REGION_START        ((uintptr_t)0x1000)

186. #else

187. /* Actually this only gives us max. 1GB in current Linux kernels. */

188. ‌#define MMAP_REGION_START        ((uintptr_t)0)

189. #endif

191. ‌static LJ_AINLINE void *CALL_MMAP(size_t size)

192. {

193. int olderr = errno;

194. void *ptr = mmap((void *)MMAP_REGION_START, size, MMAP_PROT, MAP_32BIT|MMAP_FLAGS, -1, 0);

195. errno = olderr;

196. return ptr;

197. }

199. #elif LJ_TARGET_OSX || LJ_TARGET_PS4 || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__sun__)

201. /* OSX and FreeBSD mmap() use a naive first-fit linear search.

202. ** That's perfect for us. Except that -pagezero_size must be set for OSX,

203. ** otherwise the lower 4GB are blocked. And the 32GB RLIMIT_DATA needs

204. ** to be reduced to 250MB on FreeBSD.

205. */

206. #if LJ_TARGET_OSX || defined(__DragonFly__)

207. ‌#define MMAP_REGION_START        ((uintptr_t)0x10000)

208. #elif LJ_TARGET_PS4

209. ‌#define MMAP_REGION_START        ((uintptr_t)0x4000)

210. #else

211. ‌#define MMAP_REGION_START        ((uintptr_t)0x10000000)

212. #endif

213. ‌#define MMAP_REGION_END                ((uintptr_t)0x80000000)

215. #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__)) && !LJ_TARGET_PS4

216. #include <sys/resource.h>

217. #endif

219. ‌static LJ_AINLINE void *CALL_MMAP(size_t size)

220. {

221. int olderr = errno;

222. /* Hint for next allocation. Doesn't need to be thread-safe. */

223. static uintptr_t alloc_hint = MMAP_REGION_START;

224. int retry = 0;

225. #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__)) && !LJ_TARGET_PS4

226. static int rlimit_modified = 0;

227. if (LJ_UNLIKELY(rlimit_modified == 0)) {

228. struct rlimit rlim;

229. rlim.rlim_cur = rlim.rlim_max = MMAP_REGION_START;

230. setrlimit(RLIMIT_DATA, &rlim);  /* Ignore result. May fail below. */

231. rlimit_modified = 1;

232. }

233. #endif

234. for (;;) {

235. void *p = mmap((void *)alloc_hint, size, MMAP_PROT, MMAP_FLAGS, -1, 0);

236. if ((uintptr_t)p >= MMAP_REGION_START &&

237. (uintptr_t)p + size < MMAP_REGION_END) {

238. alloc_hint = (uintptr_t)p + size;

239. errno = olderr;

240. return p;

241. }

242. if (p != CMFAIL) munmap(p, size);

243. #if defined(__sun__) || defined(__DragonFly__)

244. alloc_hint += 0x1000000;  /* Need near-exhaustive linear scan. */

245. if (alloc_hint + size < MMAP_REGION_END) continue;

246. #endif

247. if (retry) break;

248. retry = 1;

249. alloc_hint = MMAP_REGION_START;

250. }

251. errno = olderr;

252. return CMFAIL;

253. }

255. #else

257. #error "NYI: need an equivalent of MAP_32BIT for this 64 bit OS"

259. #endif

261. #else

263. /* 32 bit mode and GC64 mode is easy. */

264. ‌static LJ_AINLINE void *CALL_MMAP(size_t size)

265. {

266. int olderr = errno;

267. void *ptr = mmap(NULL, size, MMAP_PROT, MMAP_FLAGS, -1, 0);

268. errno = olderr;

269. return ptr;

270. }

272. #endif

274. ‌#define INIT_MMAP()                ((void)0)

275. ‌#define DIRECT_MMAP(s)                CALL_MMAP(s)

277. ‌static LJ_AINLINE int CALL_MUNMAP(void *ptr, size_t size)

278. {

279. int olderr = errno;

280. int ret = munmap(ptr, size);

281. errno = olderr;

282. return ret;

283. }

285. #if LJ_TARGET_LINUX

286. /* Need to define _GNU_SOURCE to get the mremap prototype. */

287. ‌static LJ_AINLINE void *CALL_MREMAP_(void *ptr, size_t osz, size_t nsz,

288. int flags)

289. {

290. int olderr = errno;

291. ptr = mremap(ptr, osz, nsz, flags);

292. errno = olderr;

293. return ptr;

294. }

296. ‌#define CALL_MREMAP(addr, osz, nsz, mv) CALL_MREMAP_((addr), (osz), (nsz), (mv))

297. ‌#define CALL_MREMAP_NOMOVE        0

298. ‌#define CALL_MREMAP_MAYMOVE        1

299. #if LJ_64 && !LJ_GC64

300. ‌#define CALL_MREMAP_MV                CALL_MREMAP_NOMOVE

301. #else

302. ‌#define CALL_MREMAP_MV                CALL_MREMAP_MAYMOVE

303. #endif

304. #endif

306. #endif

308. #ifndef CALL_MREMAP

309. ‌#define CALL_MREMAP(addr, osz, nsz, mv) ((void)osz, MFAIL)

310. #endif

312. /* -----------------------  Chunk representations ------------------------ */

314. ‌struct malloc_chunk {

315. size_t               prev_foot;  /* Size of previous chunk (if free).  */

316. size_t               head;       /* Size and inuse bits. */

317. struct malloc_chunk *fd;         /* double links -- used only if free. */

318. struct malloc_chunk *bk;

319. };

321. ‌typedef struct malloc_chunk  mchunk;

322. ‌typedef struct malloc_chunk *mchunkptr;

323. ‌typedef struct malloc_chunk *sbinptr;  /* The type of bins of chunks */

324. ‌typedef size_t bindex_t;               /* Described below */

325. ‌typedef unsigned int binmap_t;         /* Described below */

326. ‌typedef unsigned int flag_t;           /* The type of various bit flag sets */

328. /* ------------------- Chunks sizes and alignments ----------------------- */

330. ‌#define MCHUNK_SIZE                (sizeof(mchunk))

332. ‌#define CHUNK_OVERHEAD                (SIZE_T_SIZE)

334. /* Direct chunks need a second word of overhead ... */

335. ‌#define DIRECT_CHUNK_OVERHEAD        (TWO_SIZE_T_SIZES)

336. /* ... and additional padding for fake next-chunk at foot */

337. ‌#define DIRECT_FOOT_PAD                (FOUR_SIZE_T_SIZES)

339. /* The smallest size we can malloc is an aligned minimal chunk */

340. ‌#define MIN_CHUNK_SIZE\

341. ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)

343. /* conversion from malloc headers to user pointers, and back */

344. ‌#define chunk2mem(p)                ((void *)((char *)(p) + TWO_SIZE_T_SIZES))

345. ‌#define mem2chunk(mem)                ((mchunkptr)((char *)(mem) - TWO_SIZE_T_SIZES))

346. /* chunk associated with aligned address A */

347. ‌#define align_as_chunk(A)        (mchunkptr)((A) + align_offset(chunk2mem(A)))

349. /* Bounds on request (not chunk) sizes. */

350. ‌#define MAX_REQUEST                ((~MIN_CHUNK_SIZE+1) << 2)

351. ‌#define MIN_REQUEST                (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE)

353. /* pad request bytes into a usable size */

354. ‌#define pad_request(req) \

355. (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)

357. /* pad request, checking for minimum (but not maximum) */

358. ‌#define request2size(req) \

359. (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req))

361. /* ------------------ Operations on head and foot fields ----------------- */

363. ‌#define PINUSE_BIT                (SIZE_T_ONE)

364. ‌#define CINUSE_BIT                (SIZE_T_TWO)

365. ‌#define INUSE_BITS                (PINUSE_BIT|CINUSE_BIT)

367. /* Head value for fenceposts */

368. ‌#define FENCEPOST_HEAD                (INUSE_BITS|SIZE_T_SIZE)

370. /* extraction of fields from head words */

371. ‌#define cinuse(p)                ((p)->head & CINUSE_BIT)

372. ‌#define pinuse(p)                ((p)->head & PINUSE_BIT)

373. ‌#define chunksize(p)                ((p)->head & ~(INUSE_BITS))

375. ‌#define clear_pinuse(p)                ((p)->head &= ~PINUSE_BIT)

376. ‌#define clear_cinuse(p)                ((p)->head &= ~CINUSE_BIT)

378. /* Treat space at ptr +/- offset as a chunk */

379. ‌#define chunk_plus_offset(p, s)                ((mchunkptr)(((char *)(p)) + (s)))

380. ‌#define chunk_minus_offset(p, s)        ((mchunkptr)(((char *)(p)) - (s)))

382. /* Ptr to next or previous physical malloc_chunk. */

383. ‌#define next_chunk(p)        ((mchunkptr)(((char *)(p)) + ((p)->head & ~INUSE_BITS)))

384. ‌#define prev_chunk(p)        ((mchunkptr)(((char *)(p)) - ((p)->prev_foot) ))

386. /* extract next chunk's pinuse bit */

387. ‌#define next_pinuse(p)        ((next_chunk(p)->head) & PINUSE_BIT)

389. /* Get/set size at footer */

390. ‌#define get_foot(p, s)        (((mchunkptr)((char *)(p) + (s)))->prev_foot)

391. ‌#define set_foot(p, s)        (((mchunkptr)((char *)(p) + (s)))->prev_foot = (s))

393. /* Set size, pinuse bit, and foot */

394. ‌#define set_size_and_pinuse_of_free_chunk(p, s)\

395. ((p)->head = (s|PINUSE_BIT), set_foot(p, s))

397. /* Set size, pinuse bit, foot, and clear next pinuse */

398. ‌#define set_free_with_pinuse(p, s, n)\

399. (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s))

401. ‌#define is_direct(p)\

402. (!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_DIRECT_BIT))

404. /* Get the internal overhead associated with chunk p */

405. ‌#define overhead_for(p)\

406. (is_direct(p)? DIRECT_CHUNK_OVERHEAD : CHUNK_OVERHEAD)

408. /* ---------------------- Overlaid data structures ----------------------- */

410. ‌struct malloc_tree_chunk {

411. /* The first four fields must be compatible with malloc_chunk */

412. size_t                    prev_foot;

413. size_t                    head;

414. struct malloc_tree_chunk *fd;

415. struct malloc_tree_chunk *bk;

417. struct malloc_tree_chunk *child[2];

418. struct malloc_tree_chunk *parent;

419. bindex_t                  index;

420. };

422. ‌typedef struct malloc_tree_chunk  tchunk;

423. ‌typedef struct malloc_tree_chunk *tchunkptr;

424. ‌typedef struct malloc_tree_chunk *tbinptr; /* The type of bins of trees */

426. /* A little helper macro for trees */

427. ‌#define leftmost_child(t) ((t)->child[0] != 0? (t)->child[0] : (t)->child[1])

429. /* ----------------------------- Segments -------------------------------- */

431. ‌struct malloc_segment {

432. char        *base;             /* base address */

433. size_t       size;             /* allocated size */

434. struct malloc_segment *next;   /* ptr to next segment */

435. };

437. ‌typedef struct malloc_segment  msegment;

438. ‌typedef struct malloc_segment *msegmentptr;

440. /* ---------------------------- malloc_state ----------------------------- */

442. /* Bin types, widths and sizes */

443. ‌#define NSMALLBINS                (32U)

444. ‌#define NTREEBINS                (32U)

445. ‌#define SMALLBIN_SHIFT                (3U)

446. ‌#define SMALLBIN_WIDTH                (SIZE_T_ONE << SMALLBIN_SHIFT)

447. ‌#define TREEBIN_SHIFT                (8U)

448. ‌#define MIN_LARGE_SIZE                (SIZE_T_ONE << TREEBIN_SHIFT)

449. ‌#define MAX_SMALL_SIZE                (MIN_LARGE_SIZE - SIZE_T_ONE)

450. ‌#define MAX_SMALL_REQUEST  (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD)

452. ‌struct malloc_state {

453. binmap_t   smallmap;

454. binmap_t   treemap;

455. size_t     dvsize;

456. size_t     topsize;

457. mchunkptr  dv;

458. mchunkptr  top;

459. size_t     trim_check;

460. size_t     release_checks;

461. mchunkptr  smallbins[(NSMALLBINS+1)*2];

462. tbinptr    treebins[NTREEBINS];

463. msegment   seg;

464. };

466. ‌typedef struct malloc_state *mstate;

468. ‌#define is_initialized(M)        ((M)->top != 0)

470. /* -------------------------- system alloc setup ------------------------- */

472. /* page-align a size */

473. ‌#define page_align(S)\

474. (((S) + (LJ_PAGESIZE - SIZE_T_ONE)) & ~(LJ_PAGESIZE - SIZE_T_ONE))

476. /* granularity-align a size */

477. ‌#define granularity_align(S)\

478. (((S) + (DEFAULT_GRANULARITY - SIZE_T_ONE))\

479. & ~(DEFAULT_GRANULARITY - SIZE_T_ONE))

481. #if LJ_TARGET_WINDOWS

482. ‌#define mmap_align(S)        granularity_align(S)

483. #else

484. ‌#define mmap_align(S)        page_align(S)

485. #endif

487. /*  True if segment S holds address A */

488. ‌#define segment_holds(S, A)\

489. ((char *)(A) >= S->base && (char *)(A) < S->base + S->size)

491. /* Return segment holding given address */

492. ‌static msegmentptr segment_holding(mstate m, char *addr)

493. {

494. msegmentptr sp = &m->seg;

495. for (;;) {

496. if (addr >= sp->base && addr < sp->base + sp->size)

497. return sp;

498. if ((sp = sp->next) == 0)

499. return 0;

500. }

501. }

503. /* Return true if segment contains a segment link */

504. ‌static int has_segment_link(mstate m, msegmentptr ss)

505. {

506. msegmentptr sp = &m->seg;

507. for (;;) {

508. if ((char *)sp >= ss->base && (char *)sp < ss->base + ss->size)

509. return 1;

510. if ((sp = sp->next) == 0)

511. return 0;

512. }

513. }

515. /*

516. TOP_FOOT_SIZE is padding at the end of a segment, including space

517. that may be needed to place segment records and fenceposts when new

518. noncontiguous segments are added.

519. */

520. ‌#define TOP_FOOT_SIZE\

521. (align_offset(chunk2mem(0))+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE)

523. /* ---------------------------- Indexing Bins ---------------------------- */

525. ‌#define is_small(s)                (((s) >> SMALLBIN_SHIFT) < NSMALLBINS)

526. ‌#define small_index(s)                ((s)  >> SMALLBIN_SHIFT)

527. ‌#define small_index2size(i)        ((i)  << SMALLBIN_SHIFT)

528. ‌#define MIN_SMALL_INDEX                (small_index(MIN_CHUNK_SIZE))

530. /* addressing by index. See above about smallbin repositioning */

531. ‌#define smallbin_at(M, i)        ((sbinptr)((char *)&((M)->smallbins[(i)<<1])))

532. ‌#define treebin_at(M,i)                (&((M)->treebins[i]))

534. /* assign tree index for size S to variable I */

535. ‌#define compute_tree_index(S, I)\

536. {\

537. unsigned int X = (unsigned int)(S >> TREEBIN_SHIFT);\

538. if (X == 0) {\

539. I = 0;\

540. } else if (X > 0xFFFF) {\

541. I = NTREEBINS-1;\

542. } else {\

543. unsigned int K = lj_fls(X);\

544. I =  (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\

545. }\

546. }

548. /* Bit representing maximum resolved size in a treebin at i */

549. ‌#define bit_for_tree_index(i) \

550. (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2)

552. /* Shift placing maximum resolved bit in a treebin at i as sign bit */

553. ‌#define leftshift_for_tree_index(i) \

554. ((i == NTREEBINS-1)? 0 : \

555. ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2)))

557. /* The size of the smallest chunk held in bin with index i */

558. ‌#define minsize_for_tree_index(i) \

559. ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) |  \

560. (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1)))

562. /* ------------------------ Operations on bin maps ----------------------- */

564. /* bit corresponding to given index */

565. ‌#define idx2bit(i)                ((binmap_t)(1) << (i))

567. /* Mark/Clear bits with given index */

568. ‌#define mark_smallmap(M,i)        ((M)->smallmap |=  idx2bit(i))

569. ‌#define clear_smallmap(M,i)        ((M)->smallmap &= ~idx2bit(i))

570. ‌#define smallmap_is_marked(M,i)        ((M)->smallmap &   idx2bit(i))

572. ‌#define mark_treemap(M,i)        ((M)->treemap  |=  idx2bit(i))

573. ‌#define clear_treemap(M,i)        ((M)->treemap  &= ~idx2bit(i))

574. ‌#define treemap_is_marked(M,i)        ((M)->treemap  &   idx2bit(i))

576. /* mask with all bits to left of least bit of x on */

577. ‌#define left_bits(x)                ((x<<1) | (~(x<<1)+1))

579. /* Set cinuse bit and pinuse bit of next chunk */

580. ‌#define set_inuse(M,p,s)\

581. ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\

582. ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT)

584. /* Set cinuse and pinuse of this chunk and pinuse of next chunk */

585. ‌#define set_inuse_and_pinuse(M,p,s)\

586. ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\

587. ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT)

589. /* Set size, cinuse and pinuse bit of this chunk */

590. ‌#define set_size_and_pinuse_of_inuse_chunk(M, p, s)\

591. ((p)->head = (s|PINUSE_BIT|CINUSE_BIT))

593. /* ----------------------- Operations on smallbins ----------------------- */

595. /* Link a free chunk into a smallbin  */

596. ‌#define insert_small_chunk(M, P, S) {\

597. bindex_t I = small_index(S);\

598. mchunkptr B = smallbin_at(M, I);\

599. mchunkptr F = B;\

600. if (!smallmap_is_marked(M, I))\

601. mark_smallmap(M, I);\

602. else\

603. F = B->fd;\

604. B->fd = P;\

605. F->bk = P;\

606. P->fd = F;\

607. P->bk = B;\

608. }

610. /* Unlink a chunk from a smallbin  */

611. ‌#define unlink_small_chunk(M, P, S) {\

612. mchunkptr F = P->fd;\

613. mchunkptr B = P->bk;\

614. bindex_t I = small_index(S);\

615. if (F == B) {\

616. clear_smallmap(M, I);\

617. } else {\

618. F->bk = B;\

619. B->fd = F;\

620. }\

621. }

623. /* Unlink the first chunk from a smallbin */

624. ‌#define unlink_first_small_chunk(M, B, P, I) {\

625. mchunkptr F = P->fd;\

626. if (B == F) {\

627. clear_smallmap(M, I);\

628. } else {\

629. B->fd = F;\

630. F->bk = B;\

631. }\

632. }

634. /* Replace dv node, binning the old one */

635. /* Used only when dvsize known to be small */

636. ‌#define replace_dv(M, P, S) {\

637. size_t DVS = M->dvsize;\

638. if (DVS != 0) {\

639. mchunkptr DV = M->dv;\

640. insert_small_chunk(M, DV, DVS);\

641. }\

642. M->dvsize = S;\

643. M->dv = P;\

644. }

646. /* ------------------------- Operations on trees ------------------------- */

648. /* Insert chunk into tree */

649. ‌#define insert_large_chunk(M, X, S) {\

650. tbinptr *H;\

651. bindex_t I;\

652. compute_tree_index(S, I);\

653. H = treebin_at(M, I);\

654. X->index = I;\

655. X->child[0] = X->child[1] = 0;\

656. if (!treemap_is_marked(M, I)) {\

657. mark_treemap(M, I);\

658. *H = X;\

659. X->parent = (tchunkptr)H;\

660. X->fd = X->bk = X;\

661. } else {\

662. tchunkptr T = *H;\

663. size_t K = S << leftshift_for_tree_index(I);\

664. for (;;) {\

665. if (chunksize(T) != S) {\

666. tchunkptr *C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\

667. K <<= 1;\

668. if (*C != 0) {\

669. T = *C;\

670. } else {\

671. *C = X;\

672. X->parent = T;\

673. X->fd = X->bk = X;\

674. break;\

675. }\

676. } else {\

677. tchunkptr F = T->fd;\

678. T->fd = F->bk = X;\

679. X->fd = F;\

680. X->bk = T;\

681. X->parent = 0;\

682. break;\

683. }\

684. }\

685. }\

686. }

688. ‌#define unlink_large_chunk(M, X) {\

689. tchunkptr XP = X->parent;\

690. tchunkptr R;\

691. if (X->bk != X) {\

692. tchunkptr F = X->fd;\

693. R = X->bk;\

694. F->bk = R;\

695. R->fd = F;\

696. } else {\

697. tchunkptr *RP;\

698. if (((R = *(RP = &(X->child[1]))) != 0) ||\

699. ((R = *(RP = &(X->child[0]))) != 0)) {\

700. tchunkptr *CP;\

701. while ((*(CP = &(R->child[1])) != 0) ||\

702. (*(CP = &(R->child[0])) != 0)) {\

703. R = *(RP = CP);\

704. }\

705. *RP = 0;\

706. }\

707. }\

708. if (XP != 0) {\

709. tbinptr *H = treebin_at(M, X->index);\

710. if (X == *H) {\

711. if ((*H = R) == 0) \

712. clear_treemap(M, X->index);\

713. } else {\

714. if (XP->child[0] == X) \

715. XP->child[0] = R;\

716. else \

717. XP->child[1] = R;\

718. }\

719. if (R != 0) {\

720. tchunkptr C0, C1;\

721. R->parent = XP;\

722. if ((C0 = X->child[0]) != 0) {\

723. R->child[0] = C0;\

724. C0->parent = R;\

725. }\

726. if ((C1 = X->child[1]) != 0) {\

727. R->child[1] = C1;\

728. C1->parent = R;\

729. }\

730. }\

731. }\

732. }

734. /* Relays to large vs small bin operations */

736. ‌#define insert_chunk(M, P, S)\

737. if (is_small(S)) { insert_small_chunk(M, P, S)\

738. } else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); }

740. ‌#define unlink_chunk(M, P, S)\

741. if (is_small(S)) { unlink_small_chunk(M, P, S)\

742. } else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); }

744. /* -----------------------  Direct-mmapping chunks ----------------------- */

746. ‌static void *direct_alloc(size_t nb)

747. {

748. size_t mmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK);

749. if (LJ_LIKELY(mmsize > nb)) {     /* Check for wrap around 0 */

750. char *mm = (char *)(DIRECT_MMAP(mmsize));

751. if (mm != CMFAIL) {

752. size_t offset = align_offset(chunk2mem(mm));

753. size_t psize = mmsize - offset - DIRECT_FOOT_PAD;

754. mchunkptr p = (mchunkptr)(mm + offset);

755. p->prev_foot = offset | IS_DIRECT_BIT;

756. p->head = psize|CINUSE_BIT;

757. chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD;

758. chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0;

759. return chunk2mem(p);

760. }

761. }

762. return NULL;

763. }

765. ‌static mchunkptr direct_resize(mchunkptr oldp, size_t nb)

766. {

767. size_t oldsize = chunksize(oldp);

768. if (is_small(nb)) /* Can't shrink direct regions below small size */

769. return NULL;

770. /* Keep old chunk if big enough but not too big */

771. if (oldsize >= nb + SIZE_T_SIZE &&

772. (oldsize - nb) <= (DEFAULT_GRANULARITY >> 1)) {

773. return oldp;

774. } else {

775. size_t offset = oldp->prev_foot & ~IS_DIRECT_BIT;

776. size_t oldmmsize = oldsize + offset + DIRECT_FOOT_PAD;

777. size_t newmmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK);

778. char *cp = (char *)CALL_MREMAP((char *)oldp - offset,

779. oldmmsize, newmmsize, CALL_MREMAP_MV);

780. if (cp != CMFAIL) {

781. mchunkptr newp = (mchunkptr)(cp + offset);

782. size_t psize = newmmsize - offset - DIRECT_FOOT_PAD;

783. newp->head = psize|CINUSE_BIT;

784. chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD;

785. chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0;

786. return newp;

787. }

788. }

789. return NULL;

790. }

792. /* -------------------------- mspace management -------------------------- */

794. /* Initialize top chunk and its size */

795. ‌static void init_top(mstate m, mchunkptr p, size_t psize)

796. {

797. /* Ensure alignment */

798. size_t offset = align_offset(chunk2mem(p));

799. p = (mchunkptr)((char *)p + offset);

800. psize -= offset;

802. m->top = p;

803. m->topsize = psize;

804. p->head = psize | PINUSE_BIT;

805. /* set size of fake trailing chunk holding overhead space only once */

806. chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE;

807. m->trim_check = DEFAULT_TRIM_THRESHOLD; /* reset on each update */

808. }

810. /* Initialize bins for a new mstate that is otherwise zeroed out */

811. ‌static void init_bins(mstate m)

812. {

813. /* Establish circular links for smallbins */

814. bindex_t i;

815. for (i = 0; i < NSMALLBINS; i++) {

816. sbinptr bin = smallbin_at(m,i);

817. bin->fd = bin->bk = bin;

818. }

819. }

821. /* Allocate chunk and prepend remainder with chunk in successor base. */

822. ‌static void *prepend_alloc(mstate m, char *newbase, char *oldbase, size_t nb)

823. {

824. mchunkptr p = align_as_chunk(newbase);

825. mchunkptr oldfirst = align_as_chunk(oldbase);

826. size_t psize = (size_t)((char *)oldfirst - (char *)p);

827. mchunkptr q = chunk_plus_offset(p, nb);

828. size_t qsize = psize - nb;

829. set_size_and_pinuse_of_inuse_chunk(m, p, nb);

831. /* consolidate remainder with first chunk of old base */

832. if (oldfirst == m->top) {

833. size_t tsize = m->topsize += qsize;

834. m->top = q;

835. q->head = tsize | PINUSE_BIT;

836. } else if (oldfirst == m->dv) {

837. size_t dsize = m->dvsize += qsize;

838. m->dv = q;

839. set_size_and_pinuse_of_free_chunk(q, dsize);

840. } else {

841. if (!cinuse(oldfirst)) {

842. size_t nsize = chunksize(oldfirst);

843. unlink_chunk(m, oldfirst, nsize);

844. oldfirst = chunk_plus_offset(oldfirst, nsize);

845. qsize += nsize;

846. }

847. set_free_with_pinuse(q, qsize, oldfirst);

848. insert_chunk(m, q, qsize);

849. }

851. return chunk2mem(p);

852. }

854. /* Add a segment to hold a new noncontiguous region */

855. ‌static void add_segment(mstate m, char *tbase, size_t tsize)

856. {

857. /* Determine locations and sizes of segment, fenceposts, old top */

858. char *old_top = (char *)m->top;

859. msegmentptr oldsp = segment_holding(m, old_top);

860. char *old_end = oldsp->base + oldsp->size;

861. size_t ssize = pad_request(sizeof(struct malloc_segment));

862. char *rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK);

863. size_t offset = align_offset(chunk2mem(rawsp));

864. char *asp = rawsp + offset;

865. char *csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp;

866. mchunkptr sp = (mchunkptr)csp;

867. msegmentptr ss = (msegmentptr)(chunk2mem(sp));

868. mchunkptr tnext = chunk_plus_offset(sp, ssize);

869. mchunkptr p = tnext;

871. /* reset top to new space */

872. init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);

874. /* Set up segment record */

875. set_size_and_pinuse_of_inuse_chunk(m, sp, ssize);

876. *ss = m->seg; /* Push current record */

877. m->seg.base = tbase;

878. m->seg.size = tsize;

879. m->seg.next = ss;

881. /* Insert trailing fenceposts */

882. for (;;) {

883. mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE);

884. p->head = FENCEPOST_HEAD;

885. if ((char *)(&(nextp->head)) < old_end)

886. p = nextp;

887. else

888. break;

889. }

891. /* Insert the rest of old top into a bin as an ordinary free chunk */

892. if (csp != old_top) {

893. mchunkptr q = (mchunkptr)old_top;

894. size_t psize = (size_t)(csp - old_top);

895. mchunkptr tn = chunk_plus_offset(q, psize);

896. set_free_with_pinuse(q, psize, tn);

897. insert_chunk(m, q, psize);

898. }

899. }

901. /* -------------------------- System allocation -------------------------- */

903. ‌static void *alloc_sys(mstate m, size_t nb)

904. {

905. char *tbase = CMFAIL;

906. size_t tsize = 0;

908. /* Directly map large chunks */

909. if (LJ_UNLIKELY(nb >= DEFAULT_MMAP_THRESHOLD)) {

910. void *mem = direct_alloc(nb);

911. if (mem != 0)

912. return mem;

913. }

915. {

916. size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE;

917. size_t rsize = granularity_align(req);

918. if (LJ_LIKELY(rsize > nb)) { /* Fail if wraps around zero */

919. char *mp = (char *)(CALL_MMAP(rsize));

920. if (mp != CMFAIL) {

921. tbase = mp;

922. tsize = rsize;

923. }

924. }

925. }

927. if (tbase != CMFAIL) {

928. msegmentptr sp = &m->seg;

929. /* Try to merge with an existing segment */

930. while (sp != 0 && tbase != sp->base + sp->size)

931. sp = sp->next;

932. if (sp != 0 && segment_holds(sp, m->top)) { /* append */

933. sp->size += tsize;

934. init_top(m, m->top, m->topsize + tsize);

935. } else {

936. sp = &m->seg;

937. while (sp != 0 && sp->base != tbase + tsize)

938. sp = sp->next;

939. if (sp != 0) {

940. char *oldbase = sp->base;

941. sp->base = tbase;

942. sp->size += tsize;

943. return prepend_alloc(m, tbase, oldbase, nb);

944. } else {

945. add_segment(m, tbase, tsize);

946. }

947. }

949. if (nb < m->topsize) { /* Allocate from new or extended top space */

950. size_t rsize = m->topsize -= nb;

951. mchunkptr p = m->top;

952. mchunkptr r = m->top = chunk_plus_offset(p, nb);

953. r->head = rsize | PINUSE_BIT;

954. set_size_and_pinuse_of_inuse_chunk(m, p, nb);

955. return chunk2mem(p);

956. }

957. }

959. return NULL;

960. }

962. /* -----------------------  system deallocation -------------------------- */

964. /* Unmap and unlink any mmapped segments that don't contain used chunks */

965. ‌static size_t release_unused_segments(mstate m)

966. {

967. size_t released = 0;

968. size_t nsegs = 0;

969. msegmentptr pred = &m->seg;

970. msegmentptr sp = pred->next;

971. while (sp != 0) {

972. char *base = sp->base;

973. size_t size = sp->size;

974. msegmentptr next = sp->next;

975. nsegs++;

976. {

977. mchunkptr p = align_as_chunk(base);

978. size_t psize = chunksize(p);

979. /* Can unmap if first chunk holds entire segment and not pinned */

980. if (!cinuse(p) && (char *)p + psize >= base + size - TOP_FOOT_SIZE) {

981. tchunkptr tp = (tchunkptr)p;

982. if (p == m->dv) {

983. m->dv = 0;

984. m->dvsize = 0;

985. } else {

986. unlink_large_chunk(m, tp);

987. }

988. if (CALL_MUNMAP(base, size) == 0) {

989. released += size;

990. /* unlink obsoleted record */

991. sp = pred;

992. sp->next = next;

993. } else { /* back out if cannot unmap */

994. insert_large_chunk(m, tp, psize);

995. }

996. }

997. }

998. pred = sp;

999. sp = next;

1000. }

1001. /* Reset check counter */

1002. m->release_checks = nsegs > MAX_RELEASE_CHECK_RATE ?

1003. nsegs : MAX_RELEASE_CHECK_RATE;

1004. return released;

1005. }

1007. ‌static int alloc_trim(mstate m, size_t pad)

1008. {

1009. size_t released = 0;

1010. if (pad < MAX_REQUEST && is_initialized(m)) {

1011. pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */

1013. if (m->topsize > pad) {

1014. /* Shrink top space in granularity-size units, keeping at least one */

1015. size_t unit = DEFAULT_GRANULARITY;

1016. size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit -

1017. SIZE_T_ONE) * unit;

1018. msegmentptr sp = segment_holding(m, (char *)m->top);

1020. if (sp->size >= extra &&

1021. !has_segment_link(m, sp)) { /* can't shrink if pinned */

1022. size_t newsize = sp->size - extra;

1023. /* Prefer mremap, fall back to munmap */

1024. if ((CALL_MREMAP(sp->base, sp->size, newsize, CALL_MREMAP_NOMOVE) != MFAIL) ||

1025. (CALL_MUNMAP(sp->base + newsize, extra) == 0)) {

1026. released = extra;

1027. }

1028. }

1030. if (released != 0) {

1031. sp->size -= released;

1032. init_top(m, m->top, m->topsize - released);

1033. }

1034. }

1036. /* Unmap any unused mmapped segments */

1037. released += release_unused_segments(m);

1039. /* On failure, disable autotrim to avoid repeated failed future calls */

1040. if (released == 0 && m->topsize > m->trim_check)

1041. m->trim_check = MAX_SIZE_T;

1042. }

1044. return (released != 0)? 1 : 0;

1045. }

1047. /* ---------------------------- malloc support --------------------------- */

1049. /* allocate a large request from the best fitting chunk in a treebin */

1050. ‌static void *tmalloc_large(mstate m, size_t nb)

1051. {

1052. tchunkptr v = 0;

1053. size_t rsize = ~nb+1; /* Unsigned negation */

1054. tchunkptr t;

1055. bindex_t idx;

1056. compute_tree_index(nb, idx);

1058. if ((t = *treebin_at(m, idx)) != 0) {

1059. /* Traverse tree for this bin looking for node with size == nb */

1060. size_t sizebits = nb << leftshift_for_tree_index(idx);

1061. tchunkptr rst = 0;  /* The deepest untaken right subtree */

1062. for (;;) {

1063. tchunkptr rt;

1064. size_t trem = chunksize(t) - nb;

1065. if (trem < rsize) {

1066. v = t;

1067. if ((rsize = trem) == 0)

1068. break;

1069. }

1070. rt = t->child[1];

1071. t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1];

1072. if (rt != 0 && rt != t)

1073. rst = rt;

1074. if (t == 0) {

1075. t = rst; /* set t to least subtree holding sizes > nb */

1076. break;

1077. }

1078. sizebits <<= 1;

1079. }

1080. }

1082. if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */

1083. binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap;

1084. if (leftbits != 0)

1085. t = *treebin_at(m, lj_ffs(leftbits));

1086. }

1088. while (t != 0) { /* find smallest of tree or subtree */

1089. size_t trem = chunksize(t) - nb;

1090. if (trem < rsize) {

1091. rsize = trem;

1092. v = t;

1093. }

1094. t = leftmost_child(t);

1095. }

1097. /*  If dv is a better fit, return NULL so malloc will use it */

1098. if (v != 0 && rsize < (size_t)(m->dvsize - nb)) {

1099. mchunkptr r = chunk_plus_offset(v, nb);

1100. unlink_large_chunk(m, v);

1101. if (rsize < MIN_CHUNK_SIZE) {

1102. set_inuse_and_pinuse(m, v, (rsize + nb));

1103. } else {

1104. set_size_and_pinuse_of_inuse_chunk(m, v, nb);

1105. set_size_and_pinuse_of_free_chunk(r, rsize);

1106. insert_chunk(m, r, rsize);

1107. }

1108. return chunk2mem(v);

1109. }

1110. return NULL;

1111. }

1113. /* allocate a small request from the best fitting chunk in a treebin */

1114. ‌static void *tmalloc_small(mstate m, size_t nb)

1115. {

1116. tchunkptr t, v;

1117. mchunkptr r;

1118. size_t rsize;

1119. bindex_t i = lj_ffs(m->treemap);

1121. v = t = *treebin_at(m, i);

1122. rsize = chunksize(t) - nb;

1124. while ((t = leftmost_child(t)) != 0) {

1125. size_t trem = chunksize(t) - nb;

1126. if (trem < rsize) {

1127. rsize = trem;

1128. v = t;

1129. }

1130. }

1132. r = chunk_plus_offset(v, nb);

1133. unlink_large_chunk(m, v);

1134. if (rsize < MIN_CHUNK_SIZE) {

1135. set_inuse_and_pinuse(m, v, (rsize + nb));

1136. } else {

1137. set_size_and_pinuse_of_inuse_chunk(m, v, nb);

1138. set_size_and_pinuse_of_free_chunk(r, rsize);

1139. replace_dv(m, r, rsize);

1140. }

1141. return chunk2mem(v);

1142. }

1144. /* ----------------------------------------------------------------------- */

1146. ‌void *lj_alloc_create(void)

1147. {

1148. size_t tsize = DEFAULT_GRANULARITY;

1149. char *tbase;

1150. INIT_MMAP();

1151. tbase = (char *)(CALL_MMAP(tsize));

1152. if (tbase != CMFAIL) {

1153. size_t msize = pad_request(sizeof(struct malloc_state));

1154. mchunkptr mn;

1155. mchunkptr msp = align_as_chunk(tbase);

1156. mstate m = (mstate)(chunk2mem(msp));

1157. memset(m, 0, msize);

1158. msp->head = (msize|PINUSE_BIT|CINUSE_BIT);

1159. m->seg.base = tbase;

1160. m->seg.size = tsize;

1161. m->release_checks = MAX_RELEASE_CHECK_RATE;

1162. init_bins(m);

1163. mn = next_chunk(mem2chunk(m));

1164. init_top(m, mn, (size_t)((tbase + tsize) - (char *)mn) - TOP_FOOT_SIZE);

1165. return m;

1166. }

1167. return NULL;

1168. }

1170. ‌void lj_alloc_destroy(void *msp)

1171. {

1172. mstate ms = (mstate)msp;

1173. msegmentptr sp = &ms->seg;

1174. while (sp != 0) {

1175. char *base = sp->base;

1176. size_t size = sp->size;

1177. sp = sp->next;

1178. CALL_MUNMAP(base, size);

1179. }

1180. }

1182. ‌static LJ_NOINLINE void *lj_alloc_malloc(void *msp, size_t nsize)

1183. {

1184. mstate ms = (mstate)msp;

1185. void *mem;

1186. size_t nb;

1187. if (nsize <= MAX_SMALL_REQUEST) {

1188. bindex_t idx;

1189. binmap_t smallbits;

1190. nb = (nsize < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(nsize);

1191. idx = small_index(nb);

1192. smallbits = ms->smallmap >> idx;

1194. if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */

1195. mchunkptr b, p;

1196. idx += ~smallbits & 1;       /* Uses next bin if idx empty */

1197. b = smallbin_at(ms, idx);

1198. p = b->fd;

1199. unlink_first_small_chunk(ms, b, p, idx);

1200. set_inuse_and_pinuse(ms, p, small_index2size(idx));

1201. mem = chunk2mem(p);

1202. return mem;

1203. } else if (nb > ms->dvsize) {

1204. if (smallbits != 0) { /* Use chunk in next nonempty smallbin */

1205. mchunkptr b, p, r;

1206. size_t rsize;

1207. binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx));

1208. bindex_t i = lj_ffs(leftbits);

1209. b = smallbin_at(ms, i);

1210. p = b->fd;

1211. unlink_first_small_chunk(ms, b, p, i);

1212. rsize = small_index2size(i) - nb;

1213. /* Fit here cannot be remainderless if 4byte sizes */

1214. if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) {

1215. set_inuse_and_pinuse(ms, p, small_index2size(i));

1216. } else {

1217. set_size_and_pinuse_of_inuse_chunk(ms, p, nb);

1218. r = chunk_plus_offset(p, nb);

1219. set_size_and_pinuse_of_free_chunk(r, rsize);

1220. replace_dv(ms, r, rsize);

1221. }

1222. mem = chunk2mem(p);

1223. return mem;

1224. } else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) {

1225. return mem;

1226. }

1227. }

1228. } else if (nsize >= MAX_REQUEST) {

1229. nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */

1230. } else {

1231. nb = pad_request(nsize);

1232. if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) {

1233. return mem;

1234. }

1235. }

1237. if (nb <= ms->dvsize) {

1238. size_t rsize = ms->dvsize - nb;

1239. mchunkptr p = ms->dv;

1240. if (rsize >= MIN_CHUNK_SIZE) { /* split dv */

1241. mchunkptr r = ms->dv = chunk_plus_offset(p, nb);

1242. ms->dvsize = rsize;

1243. set_size_and_pinuse_of_free_chunk(r, rsize);

1244. set_size_and_pinuse_of_inuse_chunk(ms, p, nb);

1245. } else { /* exhaust dv */

1246. size_t dvs = ms->dvsize;

1247. ms->dvsize = 0;

1248. ms->dv = 0;

1249. set_inuse_and_pinuse(ms, p, dvs);

1250. }

1251. mem = chunk2mem(p);

1252. return mem;

1253. } else if (nb < ms->topsize) { /* Split top */

1254. size_t rsize = ms->topsize -= nb;

1255. mchunkptr p = ms->top;

1256. mchunkptr r = ms->top = chunk_plus_offset(p, nb);

1257. r->head = rsize | PINUSE_BIT;

1258. set_size_and_pinuse_of_inuse_chunk(ms, p, nb);

1259. mem = chunk2mem(p);

1260. return mem;

1261. }

1262. return alloc_sys(ms, nb);

1263. }

1265. ‌static LJ_NOINLINE void *lj_alloc_free(void *msp, void *ptr)

1266. {

1267. if (ptr != 0) {

1268. mchunkptr p = mem2chunk(ptr);

1269. mstate fm = (mstate)msp;

1270. size_t psize = chunksize(p);

1271. mchunkptr next = chunk_plus_offset(p, psize);

1272. if (!pinuse(p)) {

1273. size_t prevsize = p->prev_foot;

1274. if ((prevsize & IS_DIRECT_BIT) != 0) {

1275. prevsize &= ~IS_DIRECT_BIT;

1276. psize += prevsize + DIRECT_FOOT_PAD;

1277. CALL_MUNMAP((char *)p - prevsize, psize);

1278. return NULL;

1279. } else {

1280. mchunkptr prev = chunk_minus_offset(p, prevsize);

1281. psize += prevsize;

1282. p = prev;

1283. /* consolidate backward */

1284. if (p != fm->dv) {

1285. unlink_chunk(fm, p, prevsize);

1286. } else if ((next->head & INUSE_BITS) == INUSE_BITS) {

1287. fm->dvsize = psize;

1288. set_free_with_pinuse(p, psize, next);

1289. return NULL;

1290. }

1291. }

1292. }

1293. if (!cinuse(next)) {  /* consolidate forward */

1294. if (next == fm->top) {

1295. size_t tsize = fm->topsize += psize;

1296. fm->top = p;

1297. p->head = tsize | PINUSE_BIT;

1298. if (p == fm->dv) {

1299. fm->dv = 0;

1300. fm->dvsize = 0;

1301. }

1302. if (tsize > fm->trim_check)

1303. alloc_trim(fm, 0);

1304. return NULL;

1305. } else if (next == fm->dv) {

1306. size_t dsize = fm->dvsize += psize;

1307. fm->dv = p;

1308. set_size_and_pinuse_of_free_chunk(p, dsize);

1309. return NULL;

1310. } else {

1311. size_t nsize = chunksize(next);

1312. psize += nsize;

1313. unlink_chunk(fm, next, nsize);

1314. set_size_and_pinuse_of_free_chunk(p, psize);

1315. if (p == fm->dv) {

1316. fm->dvsize = psize;

1317. return NULL;

1318. }

1319. }

1320. } else {

1321. set_free_with_pinuse(p, psize, next);

1322. }

1324. if (is_small(psize)) {

1325. insert_small_chunk(fm, p, psize);

1326. } else {

1327. tchunkptr tp = (tchunkptr)p;

1328. insert_large_chunk(fm, tp, psize);

1329. if (--fm->release_checks == 0)

1330. release_unused_segments(fm);

1331. }

1332. }

1333. return NULL;

1334. }

1336. ‌static LJ_NOINLINE void *lj_alloc_realloc(void *msp, void *ptr, size_t nsize)

1337. {

1338. if (nsize >= MAX_REQUEST) {

1339. return NULL;

1340. } else {

1341. mstate m = (mstate)msp;

1342. mchunkptr oldp = mem2chunk(ptr);

1343. size_t oldsize = chunksize(oldp);

1344. mchunkptr next = chunk_plus_offset(oldp, oldsize);

1345. mchunkptr newp = 0;

1346. size_t nb = request2size(nsize);

1348. /* Try to either shrink or extend into top. Else malloc-copy-free */

1349. if (is_direct(oldp)) {

1350. newp = direct_resize(oldp, nb);  /* this may return NULL. */

1351. } else if (oldsize >= nb) { /* already big enough */

1352. size_t rsize = oldsize - nb;

1353. newp = oldp;

1354. if (rsize >= MIN_CHUNK_SIZE) {

1355. mchunkptr rem = chunk_plus_offset(newp, nb);

1356. set_inuse(m, newp, nb);

1357. set_inuse(m, rem, rsize);

1358. lj_alloc_free(m, chunk2mem(rem));

1359. }

1360. } else if (next == m->top && oldsize + m->topsize > nb) {

1361. /* Expand into top */

1362. size_t newsize = oldsize + m->topsize;

1363. size_t newtopsize = newsize - nb;

1364. mchunkptr newtop = chunk_plus_offset(oldp, nb);

1365. set_inuse(m, oldp, nb);

1366. newtop->head = newtopsize |PINUSE_BIT;

1367. m->top = newtop;

1368. m->topsize = newtopsize;

1369. newp = oldp;

1370. }

1372. if (newp != 0) {

1373. return chunk2mem(newp);

1374. } else {

1375. void *newmem = lj_alloc_malloc(m, nsize);

1376. if (newmem != 0) {

1377. size_t oc = oldsize - overhead_for(oldp);

1378. memcpy(newmem, ptr, oc < nsize ? oc : nsize);

1379. lj_alloc_free(m, ptr);

1380. }

1381. return newmem;

1382. }

1383. }

1384. }

1386. ‌void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize)

1387. {

1388. (void)osize;

1389. if (nsize == 0) {

1390. return lj_alloc_free(msp, ptr);

1391. } else if (ptr == NULL) {

1392. return lj_alloc_malloc(msp, nsize);

1393. } else {

1394. return lj_alloc_realloc(msp, ptr, nsize);

1395. }

1396. }

1398. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/ljamalg.c - luajit-2.0-src

Macros defined

- LUA_CORE

- WINVER

- _GNU_SOURCE

- ljamalg_c

Source code

1. /*

2. ** LuaJIT core and libraries amalgamation.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /*

7. +--------------------------------------------------------------------------+

8. | WARNING: Compiling the amalgamation needs a lot of virtual memory        |

9. | (around 300 MB with GCC 4.x)! If you don't have enough physical memory   |

10. | your machine will start swapping to disk and the compile will not finish |

11. | within a reasonable amount of time.                                      |

12. | So either compile on a bigger machine or use the non-amalgamated build.  |

13. +--------------------------------------------------------------------------+

14. */

16. ‌#define ljamalg_c

17. ‌#define LUA_CORE

19. /* To get the mremap prototype. Must be defined before any system includes. */

20. #if defined(__linux__) && !defined(_GNU_SOURCE)

21. ‌#define _GNU_SOURCE

22. #endif

24. #ifndef WINVER

25. ‌#define WINVER 0x0501

26. #endif

28. #include "lua.h"

29. #include "lauxlib.h"

31. #include "lj_gc.c"

32. #include "lj_err.c"

33. #include "lj_char.c"

34. #include "lj_bc.c"

35. #include "lj_obj.c"

36. #include "lj_buf.c"

37. #include "lj_str.c"

38. #include "lj_tab.c"

39. #include "lj_func.c"

40. #include "lj_udata.c"

41. #include "lj_meta.c"

42. #include "lj_debug.c"

43. #include "lj_state.c"

44. #include "lj_dispatch.c"

45. #include "lj_vmevent.c"

46. #include "lj_vmmath.c"

47. #include "lj_strscan.c"

48. #include "lj_strfmt.c"

49. #include "lj_api.c"

50. #include "lj_profile.c"

51. #include "lj_lex.c"

52. #include "lj_parse.c"

53. #include "lj_bcread.c"

54. #include "lj_bcwrite.c"

55. #include "lj_load.c"

56. #include "lj_ctype.c"

57. #include "lj_cdata.c"

58. #include "lj_cconv.c"

59. #include "lj_ccall.c"

60. #include "lj_ccallback.c"

61. #include "lj_carith.c"

62. #include "lj_clib.c"

63. #include "lj_cparse.c"

64. #include "lj_lib.c"

65. #include "lj_ir.c"

66. #include "lj_opt_mem.c"

67. #include "lj_opt_fold.c"

68. #include "lj_opt_narrow.c"

69. #include "lj_opt_dce.c"

70. #include "lj_opt_loop.c"

71. #include "lj_opt_split.c"

72. #include "lj_opt_sink.c"

73. #include "lj_mcode.c"

74. #include "lj_snap.c"

75. #include "lj_record.c"

76. #include "lj_crecord.c"

77. #include "lj_ffrecord.c"

78. #include "lj_asm.c"

79. #include "lj_trace.c"

80. #include "lj_gdbjit.c"

81. #include "lj_alloc.c"

83. #include "lib_aux.c"

84. #include "lib_base.c"

85. #include "lib_math.c"

86. #include "lib_string.c"

87. #include "lib_table.c"

88. #include "lib_io.c"

89. #include "lib_os.c"

90. #include "lib_package.c"

91. #include "lib_debug.c"

92. #include "lib_bit.c"

93. #include "lib_jit.c"

94. #include "lib_ffi.c"

95. #include "lib_init.c"

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_bcread.c - luajit-2.0-src

Functions defined

- bcread_block

- bcread_byte

- bcread_bytecode

- bcread_dbg

- bcread_error

- bcread_fill

- bcread_header

- bcread_kgc

- bcread_knum

- bcread_ktab

- bcread_ktabk

- bcread_mem

- bcread_need

- bcread_uleb128

- bcread_uleb128_33

- bcread_uv

- bcread_varinfo

- bcread_want

- lj_bcread

- lj_bcread_proto

Macros defined

- LUA_CORE

- bcread_flags

- bcread_oldtop

- bcread_savetop

- bcread_swap

- lj_bcread_c

Source code

1. /*

2. ** Bytecode reader.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_bcread_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #include "lj_gc.h"

11. #include "lj_err.h"

12. #include "lj_buf.h"

13. #include "lj_str.h"

14. #include "lj_tab.h"

15. #include "lj_bc.h"

16. #if LJ_HASFFI

17. #include "lj_ctype.h"

18. #include "lj_cdata.h"

19. #include "lualib.h"

20. #endif

21. #include "lj_lex.h"

22. #include "lj_bcdump.h"

23. #include "lj_state.h"

24. #include "lj_strfmt.h"

26. /* Reuse some lexer fields for our own purposes. */

27. ‌#define bcread_flags(ls)        ls->level

28. ‌#define bcread_swap(ls) \

29. ((bcread_flags(ls) & BCDUMP_F_BE) != LJ_BE*BCDUMP_F_BE)

30. ‌#define bcread_oldtop(L, ls)        restorestack(L, ls->lastline)

31. ‌#define bcread_savetop(L, ls, top) \

32. ls->lastline = (BCLine)savestack(L, (top))

34. /* -- Input buffer handling ----------------------------------------------- */

36. /* Throw reader error. */

37. ‌static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em)

38. {

39. lua_State *L = ls->L;

40. const char *name = ls->chunkarg;

41. if (*name == BCDUMP_HEAD1) name = "(binary)";

42. else if (*name == '@' || *name == '=') name++;

43. lj_strfmt_pushf(L, "%s: %s", name, err2msg(em));

44. lj_err_throw(L, LUA_ERRSYNTAX);

45. }

47. /* Refill buffer. */

48. ‌static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need)

49. {

50. lua_assert(len != 0);

51. if (len > LJ_MAX_BUF || ls->c < 0)

52. bcread_error(ls, LJ_ERR_BCBAD);

53. do {

54. const char *buf;

55. size_t sz;

56. char *p = sbufB(&ls->sb);

57. MSize n = (MSize)(ls->pe - ls->p);

58. if (n) {  /* Copy remainder to buffer. */

59. if (sbuflen(&ls->sb)) {  /* Move down in buffer. */

60. lua_assert(ls->pe == sbufP(&ls->sb));

61. if (ls->p != p) memmove(p, ls->p, n);

62. } else {  /* Copy from buffer provided by reader. */

63. p = lj_buf_need(&ls->sb, len);

64. memcpy(p, ls->p, n);

65. }

66. ls->p = p;

67. ls->pe = p + n;

68. }

69. setsbufP(&ls->sb, p + n);

70. buf = ls->rfunc(ls->L, ls->rdata, &sz);  /* Get more data from reader. */

71. if (buf == NULL || sz == 0) {  /* EOF? */

72. if (need) bcread_error(ls, LJ_ERR_BCBAD);

73. ls->c = -1;  /* Only bad if we get called again. */

74. break;

75. }

76. if (n) {  /* Append to buffer. */

77. n += (MSize)sz;

78. p = lj_buf_need(&ls->sb, n < len ? len : n);

79. memcpy(sbufP(&ls->sb), buf, sz);

80. setsbufP(&ls->sb, p + n);

81. ls->p = p;

82. ls->pe = p + n;

83. } else {  /* Return buffer provided by reader. */

84. ls->p = buf;

85. ls->pe = buf + sz;

86. }

87. } while (ls->p + len > ls->pe);

88. }

90. /* Need a certain number of bytes. */

91. ‌static LJ_AINLINE void bcread_need(LexState *ls, MSize len)

92. {

93. if (LJ_UNLIKELY(ls->p + len > ls->pe))

94. bcread_fill(ls, len, 1);

95. }

97. /* Want to read up to a certain number of bytes, but may need less. */

98. ‌static LJ_AINLINE void bcread_want(LexState *ls, MSize len)

99. {

100. if (LJ_UNLIKELY(ls->p + len > ls->pe))

101. bcread_fill(ls, len, 0);

102. }

104. /* Return memory block from buffer. */

105. ‌static LJ_AINLINE uint8_t *bcread_mem(LexState *ls, MSize len)

106. {

107. uint8_t *p = (uint8_t *)ls->p;

108. ls->p += len;

109. lua_assert(ls->p <= ls->pe);

110. return p;

111. }

113. /* Copy memory block from buffer. */

114. ‌static void bcread_block(LexState *ls, void *q, MSize len)

115. {

116. memcpy(q, bcread_mem(ls, len), len);

117. }

119. /* Read byte from buffer. */

120. ‌static LJ_AINLINE uint32_t bcread_byte(LexState *ls)

121. {

122. lua_assert(ls->p < ls->pe);

123. return (uint32_t)(uint8_t)*ls->p++;

124. }

126. /* Read ULEB128 value from buffer. */

127. ‌static LJ_AINLINE uint32_t bcread_uleb128(LexState *ls)

128. {

129. uint32_t v = lj_buf_ruleb128(&ls->p);

130. lua_assert(ls->p <= ls->pe);

131. return v;

132. }

134. /* Read top 32 bits of 33 bit ULEB128 value from buffer. */

135. ‌static uint32_t bcread_uleb128_33(LexState *ls)

136. {

137. const uint8_t *p = (const uint8_t *)ls->p;

138. uint32_t v = (*p++ >> 1);

139. if (LJ_UNLIKELY(v >= 0x40)) {

140. int sh = -1;

141. v &= 0x3f;

142. do {

143. v |= ((*p & 0x7f) << (sh += 7));

144. } while (*p++ >= 0x80);

145. }

146. ls->p = (char *)p;

147. lua_assert(ls->p <= ls->pe);

148. return v;

149. }

151. /* -- Bytecode reader ----------------------------------------------------- */

153. /* Read debug info of a prototype. */

154. ‌static void bcread_dbg(LexState *ls, GCproto *pt, MSize sizedbg)

155. {

156. void *lineinfo = (void *)proto_lineinfo(pt);

157. bcread_block(ls, lineinfo, sizedbg);

158. /* Swap lineinfo if the endianess differs. */

159. if (bcread_swap(ls) && pt->numline >= 256) {

160. MSize i, n = pt->sizebc-1;

161. if (pt->numline < 65536) {

162. uint16_t *p = (uint16_t *)lineinfo;

163. for (i = 0; i < n; i++) p[i] = (uint16_t)((p[i] >> 8)|(p[i] << 8));

164. } else {

165. uint32_t *p = (uint32_t *)lineinfo;

166. for (i = 0; i < n; i++) p[i] = lj_bswap(p[i]);

167. }

168. }

169. }

171. /* Find pointer to varinfo. */

172. ‌static const void *bcread_varinfo(GCproto *pt)

173. {

174. const uint8_t *p = proto_uvinfo(pt);

175. MSize n = pt->sizeuv;

176. if (n) while (*p++ || --n) ;

177. return p;

178. }

180. /* Read a single constant key/value of a template table. */

181. ‌static void bcread_ktabk(LexState *ls, TValue *o)

182. {

183. MSize tp = bcread_uleb128(ls);

184. if (tp >= BCDUMP_KTAB_STR) {

185. MSize len = tp - BCDUMP_KTAB_STR;

186. const char *p = (const char *)bcread_mem(ls, len);

187. setstrV(ls->L, o, lj_str_new(ls->L, p, len));

188. } else if (tp == BCDUMP_KTAB_INT) {

189. setintV(o, (int32_t)bcread_uleb128(ls));

190. } else if (tp == BCDUMP_KTAB_NUM) {

191. o->u32.lo = bcread_uleb128(ls);

192. o->u32.hi = bcread_uleb128(ls);

193. } else {

194. lua_assert(tp <= BCDUMP_KTAB_TRUE);

195. setpriV(o, ~tp);

196. }

197. }

199. /* Read a template table. */

200. ‌static GCtab *bcread_ktab(LexState *ls)

201. {

202. MSize narray = bcread_uleb128(ls);

203. MSize nhash = bcread_uleb128(ls);

204. GCtab *t = lj_tab_new(ls->L, narray, hsize2hbits(nhash));

205. if (narray) {  /* Read array entries. */

206. MSize i;

207. TValue *o = tvref(t->array);

208. for (i = 0; i < narray; i++, o++)

209. bcread_ktabk(ls, o);

210. }

211. if (nhash) {  /* Read hash entries. */

212. MSize i;

213. for (i = 0; i < nhash; i++) {

214. TValue key;

215. bcread_ktabk(ls, &key);

216. lua_assert(!tvisnil(&key));

217. bcread_ktabk(ls, lj_tab_set(ls->L, t, &key));

218. }

219. }

220. return t;

221. }

223. /* Read GC constants of a prototype. */

224. ‌static void bcread_kgc(LexState *ls, GCproto *pt, MSize sizekgc)

225. {

226. MSize i;

227. GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc;

228. for (i = 0; i < sizekgc; i++, kr++) {

229. MSize tp = bcread_uleb128(ls);

230. if (tp >= BCDUMP_KGC_STR) {

231. MSize len = tp - BCDUMP_KGC_STR;

232. const char *p = (const char *)bcread_mem(ls, len);

233. setgcref(*kr, obj2gco(lj_str_new(ls->L, p, len)));

234. } else if (tp == BCDUMP_KGC_TAB) {

235. setgcref(*kr, obj2gco(bcread_ktab(ls)));

236. #if LJ_HASFFI

237. } else if (tp != BCDUMP_KGC_CHILD) {

238. CTypeID id = tp == BCDUMP_KGC_COMPLEX ? CTID_COMPLEX_DOUBLE :

239. tp == BCDUMP_KGC_I64 ? CTID_INT64 : CTID_UINT64;

240. CTSize sz = tp == BCDUMP_KGC_COMPLEX ? 16 : 8;

241. GCcdata *cd = lj_cdata_new_(ls->L, id, sz);

242. TValue *p = (TValue *)cdataptr(cd);

243. setgcref(*kr, obj2gco(cd));

244. p[0].u32.lo = bcread_uleb128(ls);

245. p[0].u32.hi = bcread_uleb128(ls);

246. if (tp == BCDUMP_KGC_COMPLEX) {

247. p[1].u32.lo = bcread_uleb128(ls);

248. p[1].u32.hi = bcread_uleb128(ls);

249. }

250. #endif

251. } else {

252. lua_State *L = ls->L;

253. lua_assert(tp == BCDUMP_KGC_CHILD);

254. if (L->top <= bcread_oldtop(L, ls))  /* Stack underflow? */

255. bcread_error(ls, LJ_ERR_BCBAD);

256. L->top--;

257. setgcref(*kr, obj2gco(protoV(L->top)));

258. }

259. }

260. }

262. /* Read number constants of a prototype. */

263. ‌static void bcread_knum(LexState *ls, GCproto *pt, MSize sizekn)

264. {

265. MSize i;

266. TValue *o = mref(pt->k, TValue);

267. for (i = 0; i < sizekn; i++, o++) {

268. int isnum = (ls->p[0] & 1);

269. uint32_t lo = bcread_uleb128_33(ls);

270. if (isnum) {

271. o->u32.lo = lo;

272. o->u32.hi = bcread_uleb128(ls);

273. } else {

274. setintV(o, lo);

275. }

276. }

277. }

279. /* Read bytecode instructions. */

280. ‌static void bcread_bytecode(LexState *ls, GCproto *pt, MSize sizebc)

281. {

282. BCIns *bc = proto_bc(pt);

283. bc[0] = BCINS_AD((pt->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF,

284. pt->framesize, 0);

285. bcread_block(ls, bc+1, (sizebc-1)*(MSize)sizeof(BCIns));

286. /* Swap bytecode instructions if the endianess differs. */

287. if (bcread_swap(ls)) {

288. MSize i;

289. for (i = 1; i < sizebc; i++) bc[i] = lj_bswap(bc[i]);

290. }

291. }

293. /* Read upvalue refs. */

294. ‌static void bcread_uv(LexState *ls, GCproto *pt, MSize sizeuv)

295. {

296. if (sizeuv) {

297. uint16_t *uv = proto_uv(pt);

298. bcread_block(ls, uv, sizeuv*2);

299. /* Swap upvalue refs if the endianess differs. */

300. if (bcread_swap(ls)) {

301. MSize i;

302. for (i = 0; i < sizeuv; i++)

303. uv[i] = (uint16_t)((uv[i] >> 8)|(uv[i] << 8));

304. }

305. }

306. }

308. /* Read a prototype. */

309. ‌GCproto *lj_bcread_proto(LexState *ls)

310. {

311. GCproto *pt;

312. MSize framesize, numparams, flags, sizeuv, sizekgc, sizekn, sizebc, sizept;

313. MSize ofsk, ofsuv, ofsdbg;

314. MSize sizedbg = 0;

315. BCLine firstline = 0, numline = 0;

317. /* Read prototype header. */

318. flags = bcread_byte(ls);

319. numparams = bcread_byte(ls);

320. framesize = bcread_byte(ls);

321. sizeuv = bcread_byte(ls);

322. sizekgc = bcread_uleb128(ls);

323. sizekn = bcread_uleb128(ls);

324. sizebc = bcread_uleb128(ls) + 1;

325. if (!(bcread_flags(ls) & BCDUMP_F_STRIP)) {

326. sizedbg = bcread_uleb128(ls);

327. if (sizedbg) {

328. firstline = bcread_uleb128(ls);

329. numline = bcread_uleb128(ls);

330. }

331. }

333. /* Calculate total size of prototype including all colocated arrays. */

334. sizept = (MSize)sizeof(GCproto) +

335. sizebc*(MSize)sizeof(BCIns) +

336. sizekgc*(MSize)sizeof(GCRef);

337. sizept = (sizept + (MSize)sizeof(TValue)-1) & ~((MSize)sizeof(TValue)-1);

338. ofsk = sizept; sizept += sizekn*(MSize)sizeof(TValue);

339. ofsuv = sizept; sizept += ((sizeuv+1)&~1)*2;

340. ofsdbg = sizept; sizept += sizedbg;

342. /* Allocate prototype object and initialize its fields. */

343. pt = (GCproto *)lj_mem_newgco(ls->L, (MSize)sizept);

344. pt->gct = ~LJ_TPROTO;

345. pt->numparams = (uint8_t)numparams;

346. pt->framesize = (uint8_t)framesize;

347. pt->sizebc = sizebc;

348. setmref(pt->k, (char *)pt + ofsk);

349. setmref(pt->uv, (char *)pt + ofsuv);

350. pt->sizekgc = 0;  /* Set to zero until fully initialized. */

351. pt->sizekn = sizekn;

352. pt->sizept = sizept;

353. pt->sizeuv = (uint8_t)sizeuv;

354. pt->flags = (uint8_t)flags;

355. pt->trace = 0;

356. setgcref(pt->chunkname, obj2gco(ls->chunkname));

358. /* Close potentially uninitialized gap between bc and kgc. */

359. *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(sizekgc+1)) = 0;

361. /* Read bytecode instructions and upvalue refs. */

362. bcread_bytecode(ls, pt, sizebc);

363. bcread_uv(ls, pt, sizeuv);

365. /* Read constants. */

366. bcread_kgc(ls, pt, sizekgc);

367. pt->sizekgc = sizekgc;

368. bcread_knum(ls, pt, sizekn);

370. /* Read and initialize debug info. */

371. pt->firstline = firstline;

372. pt->numline = numline;

373. if (sizedbg) {

374. MSize sizeli = (sizebc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2);

375. setmref(pt->lineinfo, (char *)pt + ofsdbg);

376. setmref(pt->uvinfo, (char *)pt + ofsdbg + sizeli);

377. bcread_dbg(ls, pt, sizedbg);

378. setmref(pt->varinfo, bcread_varinfo(pt));

379. } else {

380. setmref(pt->lineinfo, NULL);

381. setmref(pt->uvinfo, NULL);

382. setmref(pt->varinfo, NULL);

383. }

384. return pt;

385. }

387. /* Read and check header of bytecode dump. */

388. ‌static int bcread_header(LexState *ls)

389. {

390. uint32_t flags;

391. bcread_want(ls, 3+5+5);

392. if (bcread_byte(ls) != BCDUMP_HEAD2 ||

393. bcread_byte(ls) != BCDUMP_HEAD3 ||

394. bcread_byte(ls) != BCDUMP_VERSION) return 0;

395. bcread_flags(ls) = flags = bcread_uleb128(ls);

396. if ((flags & ~(BCDUMP_F_KNOWN)) != 0) return 0;

397. if ((flags & BCDUMP_F_FR2) != LJ_FR2*BCDUMP_F_FR2) return 0;

398. if ((flags & BCDUMP_F_FFI)) {

399. #if LJ_HASFFI

400. lua_State *L = ls->L;

401. if (!ctype_ctsG(G(L))) {

402. ptrdiff_t oldtop = savestack(L, L->top);

403. luaopen_ffi(L);  /* Load FFI library on-demand. */

404. L->top = restorestack(L, oldtop);

405. }

406. #else

407. return 0;

408. #endif

409. }

410. if ((flags & BCDUMP_F_STRIP)) {

411. ls->chunkname = lj_str_newz(ls->L, ls->chunkarg);

412. } else {

413. MSize len = bcread_uleb128(ls);

414. bcread_need(ls, len);

415. ls->chunkname = lj_str_new(ls->L, (const char *)bcread_mem(ls, len), len);

416. }

417. return 1;  /* Ok. */

418. }

420. /* Read a bytecode dump. */

421. ‌GCproto *lj_bcread(LexState *ls)

422. {

423. lua_State *L = ls->L;

424. lua_assert(ls->c == BCDUMP_HEAD1);

425. bcread_savetop(L, ls, L->top);

426. lj_buf_reset(&ls->sb);

427. /* Check for a valid bytecode dump header. */

428. if (!bcread_header(ls))

429. bcread_error(ls, LJ_ERR_BCFMT);

430. for (;;) {  /* Process all prototypes in the bytecode dump. */

431. GCproto *pt;

432. MSize len;

433. const char *startp;

434. /* Read length. */

435. if (ls->p < ls->pe && ls->p[0] == 0) {  /* Shortcut EOF. */

436. ls->p++;

437. break;

438. }

439. bcread_want(ls, 5);

440. len = bcread_uleb128(ls);

441. if (!len) break;  /* EOF */

442. bcread_need(ls, len);

443. startp = ls->p;

444. pt = lj_bcread_proto(ls);

445. if (ls->p != startp + len)

446. bcread_error(ls, LJ_ERR_BCBAD);

447. setprotoV(L, L->top, pt);

448. incr_top(L);

449. }

450. if ((int32_t)(2*(uint32_t)(ls->pe - ls->p)) > 0 ||

451. L->top-1 != bcread_oldtop(L, ls))

452. bcread_error(ls, LJ_ERR_BCBAD);

453. /* Pop off last prototype. */

454. L->top--;

455. return protoV(L->top);

456. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_bcdump.h - luajit-2.0-src

Macros defined

- BCDUMP_F_BE

- BCDUMP_F_FFI

- BCDUMP_F_FR2

- BCDUMP_F_KNOWN

- BCDUMP_F_STRIP

- BCDUMP_HEAD1

- BCDUMP_HEAD2

- BCDUMP_HEAD3

- BCDUMP_VERSION

- _LJ_BCDUMP_H

Source code

1. /*

2. ** Bytecode dump definitions.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_BCDUMP_H

7. ‌#define _LJ_BCDUMP_H

9. #include "lj_obj.h"

10. #include "lj_lex.h"

12. /* -- Bytecode dump format ------------------------------------------------ */

14. /*

15. ** dump   = header proto+ 0U

16. ** header = ESC 'L' 'J' versionB flagsU [namelenU nameB*]

17. ** proto  = lengthU pdata

18. ** pdata  = phead bcinsW* uvdataH* kgc* knum* [debugB*]

19. ** phead  = flagsB numparamsB framesizeB numuvB numkgcU numknU numbcU

20. **          [debuglenU [firstlineU numlineU]]

21. ** kgc    = kgctypeU { ktab | (loU hiU) | (rloU rhiU iloU ihiU) | strB* }

22. ** knum   = intU0 | (loU1 hiU)

23. ** ktab   = narrayU nhashU karray* khash*

24. ** karray = ktabk

25. ** khash  = ktabk ktabk

26. ** ktabk  = ktabtypeU { intU | (loU hiU) | strB* }

27. **

28. ** B = 8 bit, H = 16 bit, W = 32 bit, U = ULEB128 of W, U0/U1 = ULEB128 of W+1

29. */

31. /* Bytecode dump header. */

32. ‌#define BCDUMP_HEAD1                0x1b

33. ‌#define BCDUMP_HEAD2                0x4c

34. ‌#define BCDUMP_HEAD3                0x4a

36. /* If you perform *any* kind of private modifications to the bytecode itself

37. ** or to the dump format, you *must* set BCDUMP_VERSION to 0x80 or higher.

38. */

39. ‌#define BCDUMP_VERSION                2

41. /* Compatibility flags. */

42. ‌#define BCDUMP_F_BE                0x01

43. ‌#define BCDUMP_F_STRIP                0x02

44. ‌#define BCDUMP_F_FFI                0x04

45. ‌#define BCDUMP_F_FR2                0x08

47. ‌#define BCDUMP_F_KNOWN                (BCDUMP_F_FR2*2-1)

49. /* Type codes for the GC constants of a prototype. Plus length for strings. */

50. enum {

51. BCDUMP_KGC_CHILD, BCDUMP_KGC_TAB, BCDUMP_KGC_I64, BCDUMP_KGC_U64,

52. BCDUMP_KGC_COMPLEX, BCDUMP_KGC_STR

53. };

55. /* Type codes for the keys/values of a constant table. */

56. enum {

57. BCDUMP_KTAB_NIL, BCDUMP_KTAB_FALSE, BCDUMP_KTAB_TRUE,

58. BCDUMP_KTAB_INT, BCDUMP_KTAB_NUM, BCDUMP_KTAB_STR

59. };

61. /* -- Bytecode reader/writer ---------------------------------------------- */

63. LJ_FUNC int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer,

64. void *data, int strip);

65. LJ_FUNC GCproto *lj_bcread_proto(LexState *ls);

66. LJ_FUNC GCproto *lj_bcread(LexState *ls);

68. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_bcwrite.c - luajit-2.0-src

Data types defined

- BCWriteCtx

- BCWriteCtx

Functions defined

- bcwrite_bytecode

- bcwrite_footer

- bcwrite_header

- bcwrite_kgc

- bcwrite_knum

- bcwrite_ktab

- bcwrite_ktabk

- bcwrite_proto

- cpwriter

- lj_bcwrite

Macros defined

- LUA_CORE

- lj_bcwrite_c

Source code

1. /*

2. ** Bytecode writer.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_bcwrite_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #include "lj_gc.h"

11. #include "lj_buf.h"

12. #include "lj_bc.h"

13. #if LJ_HASFFI

14. #include "lj_ctype.h"

15. #endif

16. #if LJ_HASJIT

17. #include "lj_dispatch.h"

18. #include "lj_jit.h"

19. #endif

20. #include "lj_strfmt.h"

21. #include "lj_bcdump.h"

22. #include "lj_vm.h"

24. /* Context for bytecode writer. */

25. ‌typedef struct BCWriteCtx {

26. SBuf sb;                        /* Output buffer. */

27. GCproto *pt;                        /* Root prototype. */

28. lua_Writer wfunc;                /* Writer callback. */

29. void *wdata;                        /* Writer callback data. */

30. int strip;                        /* Strip debug info. */

31. int status;                        /* Status from writer callback. */

32. ‌} BCWriteCtx;

34. /* -- Bytecode writer ----------------------------------------------------- */

36. /* Write a single constant key/value of a template table. */

37. ‌static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)

38. {

39. char *p = lj_buf_more(&ctx->sb, 1+10);

40. if (tvisstr(o)) {

41. const GCstr *str = strV(o);

42. MSize len = str->len;

43. p = lj_buf_more(&ctx->sb, 5+len);

44. p = lj_strfmt_wuleb128(p, BCDUMP_KTAB_STR+len);

45. p = lj_buf_wmem(p, strdata(str), len);

46. } else if (tvisint(o)) {

47. *p++ = BCDUMP_KTAB_INT;

48. p = lj_strfmt_wuleb128(p, intV(o));

49. } else if (tvisnum(o)) {

50. if (!LJ_DUALNUM && narrow) {  /* Narrow number constants to integers. */

51. lua_Number num = numV(o);

52. int32_t k = lj_num2int(num);

53. if (num == (lua_Number)k) {  /* -0 is never a constant. */

54. *p++ = BCDUMP_KTAB_INT;

55. p = lj_strfmt_wuleb128(p, k);

56. setsbufP(&ctx->sb, p);

57. return;

58. }

59. }

60. *p++ = BCDUMP_KTAB_NUM;

61. p = lj_strfmt_wuleb128(p, o->u32.lo);

62. p = lj_strfmt_wuleb128(p, o->u32.hi);

63. } else {

64. lua_assert(tvispri(o));

65. *p++ = BCDUMP_KTAB_NIL+~itype(o);

66. }

67. setsbufP(&ctx->sb, p);

68. }

70. /* Write a template table. */

71. ‌static void bcwrite_ktab(BCWriteCtx *ctx, char *p, const GCtab *t)

72. {

73. MSize narray = 0, nhash = 0;

74. if (t->asize > 0) {  /* Determine max. length of array part. */

75. ptrdiff_t i;

76. TValue *array = tvref(t->array);

77. for (i = (ptrdiff_t)t->asize-1; i >= 0; i--)

78. if (!tvisnil(&array[i]))

79. break;

80. narray = (MSize)(i+1);

81. }

82. if (t->hmask > 0) {  /* Count number of used hash slots. */

83. MSize i, hmask = t->hmask;

84. Node *node = noderef(t->node);

85. for (i = 0; i <= hmask; i++)

86. nhash += !tvisnil(&node[i].val);

87. }

88. /* Write number of array slots and hash slots. */

89. p = lj_strfmt_wuleb128(p, narray);

90. p = lj_strfmt_wuleb128(p, nhash);

91. setsbufP(&ctx->sb, p);

92. if (narray) {  /* Write array entries (may contain nil). */

93. MSize i;

94. TValue *o = tvref(t->array);

95. for (i = 0; i < narray; i++, o++)

96. bcwrite_ktabk(ctx, o, 1);

97. }

98. if (nhash) {  /* Write hash entries. */

99. MSize i = nhash;

100. Node *node = noderef(t->node) + t->hmask;

101. for (;; node--)

102. if (!tvisnil(&node->val)) {

103. bcwrite_ktabk(ctx, &node->key, 0);

104. bcwrite_ktabk(ctx, &node->val, 1);

105. if (--i == 0) break;

106. }

107. }

108. }

110. /* Write GC constants of a prototype. */

111. ‌static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)

112. {

113. MSize i, sizekgc = pt->sizekgc;

114. GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc;

115. for (i = 0; i < sizekgc; i++, kr++) {

116. GCobj *o = gcref(*kr);

117. MSize tp, need = 1;

118. char *p;

119. /* Determine constant type and needed size. */

120. if (o->gch.gct == ~LJ_TSTR) {

121. tp = BCDUMP_KGC_STR + gco2str(o)->len;

122. need = 5+gco2str(o)->len;

123. } else if (o->gch.gct == ~LJ_TPROTO) {

124. lua_assert((pt->flags & PROTO_CHILD));

125. tp = BCDUMP_KGC_CHILD;

126. #if LJ_HASFFI

127. } else if (o->gch.gct == ~LJ_TCDATA) {

128. CTypeID id = gco2cd(o)->ctypeid;

129. need = 1+4*5;

130. if (id == CTID_INT64) {

131. tp = BCDUMP_KGC_I64;

132. } else if (id == CTID_UINT64) {

133. tp = BCDUMP_KGC_U64;

134. } else {

135. lua_assert(id == CTID_COMPLEX_DOUBLE);

136. tp = BCDUMP_KGC_COMPLEX;

137. }

138. #endif

139. } else {

140. lua_assert(o->gch.gct == ~LJ_TTAB);

141. tp = BCDUMP_KGC_TAB;

142. need = 1+2*5;

143. }

144. /* Write constant type. */

145. p = lj_buf_more(&ctx->sb, need);

146. p = lj_strfmt_wuleb128(p, tp);

147. /* Write constant data (if any). */

148. if (tp >= BCDUMP_KGC_STR) {

149. p = lj_buf_wmem(p, strdata(gco2str(o)), gco2str(o)->len);

150. } else if (tp == BCDUMP_KGC_TAB) {

151. bcwrite_ktab(ctx, p, gco2tab(o));

152. continue;

153. #if LJ_HASFFI

154. } else if (tp != BCDUMP_KGC_CHILD) {

155. cTValue *q = (TValue *)cdataptr(gco2cd(o));

156. p = lj_strfmt_wuleb128(p, q[0].u32.lo);

157. p = lj_strfmt_wuleb128(p, q[0].u32.hi);

158. if (tp == BCDUMP_KGC_COMPLEX) {

159. p = lj_strfmt_wuleb128(p, q[1].u32.lo);

160. p = lj_strfmt_wuleb128(p, q[1].u32.hi);

161. }

162. #endif

163. }

164. setsbufP(&ctx->sb, p);

165. }

166. }

168. /* Write number constants of a prototype. */

169. ‌static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)

170. {

171. MSize i, sizekn = pt->sizekn;

172. cTValue *o = mref(pt->k, TValue);

173. char *p = lj_buf_more(&ctx->sb, 10*sizekn);

174. for (i = 0; i < sizekn; i++, o++) {

175. int32_t k;

176. if (tvisint(o)) {

177. k = intV(o);

178. goto save_int;

179. } else {

180. /* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */

181. if (!LJ_DUALNUM) {  /* Narrow number constants to integers. */

182. lua_Number num = numV(o);

183. k = lj_num2int(num);

184. if (num == (lua_Number)k) {  /* -0 is never a constant. */

185. save_int:

186. p = lj_strfmt_wuleb128(p, 2*(uint32_t)k | ((uint32_t)k&0x80000000u));

187. if (k < 0)

188. p[-1] = (p[-1] & 7) | ((k>>27) & 0x18);

189. continue;

190. }

191. }

192. p = lj_strfmt_wuleb128(p, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));

193. if (o->u32.lo >= 0x80000000u)

194. p[-1] = (p[-1] & 7) | ((o->u32.lo>>27) & 0x18);

195. p = lj_strfmt_wuleb128(p, o->u32.hi);

196. }

197. }

198. setsbufP(&ctx->sb, p);

199. }

201. /* Write bytecode instructions. */

202. ‌static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt)

203. {

204. MSize nbc = pt->sizebc-1;  /* Omit the [JI]FUNC* header. */

205. #if LJ_HASJIT

206. uint8_t *q = (uint8_t *)p;

207. #endif

208. p = lj_buf_wmem(p, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));

209. UNUSED(ctx);

210. #if LJ_HASJIT

211. /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */

212. if ((pt->flags & PROTO_ILOOP) || pt->trace) {

213. jit_State *J = L2J(sbufL(&ctx->sb));

214. MSize i;

215. for (i = 0; i < nbc; i++, q += sizeof(BCIns)) {

216. BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)];

217. if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||

218. op == BC_JFORI) {

219. q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);

220. } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {

221. BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8);

222. BCIns ins = traceref(J, rd)->startins;

223. q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL);

224. q[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins);

225. q[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins);

226. }

227. }

228. }

229. #endif

230. return p;

231. }

233. /* Write prototype. */

234. ‌static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)

235. {

236. MSize sizedbg = 0;

237. char *p;

239. /* Recursively write children of prototype. */

240. if ((pt->flags & PROTO_CHILD)) {

241. ptrdiff_t i, n = pt->sizekgc;

242. GCRef *kr = mref(pt->k, GCRef) - 1;

243. for (i = 0; i < n; i++, kr--) {

244. GCobj *o = gcref(*kr);

245. if (o->gch.gct == ~LJ_TPROTO)

246. bcwrite_proto(ctx, gco2pt(o));

247. }

248. }

250. /* Start writing the prototype info to a buffer. */

251. p = lj_buf_need(&ctx->sb,

252. 5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);

253. p += 5;  /* Leave room for final size. */

255. /* Write prototype header. */

256. *p++ = (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI));

257. *p++ = pt->numparams;

258. *p++ = pt->framesize;

259. *p++ = pt->sizeuv;

260. p = lj_strfmt_wuleb128(p, pt->sizekgc);

261. p = lj_strfmt_wuleb128(p, pt->sizekn);

262. p = lj_strfmt_wuleb128(p, pt->sizebc-1);

263. if (!ctx->strip) {

264. if (proto_lineinfo(pt))

265. sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);

266. p = lj_strfmt_wuleb128(p, sizedbg);

267. if (sizedbg) {

268. p = lj_strfmt_wuleb128(p, pt->firstline);

269. p = lj_strfmt_wuleb128(p, pt->numline);

270. }

271. }

273. /* Write bytecode instructions and upvalue refs. */

274. p = bcwrite_bytecode(ctx, p, pt);

275. p = lj_buf_wmem(p, proto_uv(pt), pt->sizeuv*2);

276. setsbufP(&ctx->sb, p);

278. /* Write constants. */

279. bcwrite_kgc(ctx, pt);

280. bcwrite_knum(ctx, pt);

282. /* Write debug info, if not stripped. */

283. if (sizedbg) {

284. p = lj_buf_more(&ctx->sb, sizedbg);

285. p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg);

286. setsbufP(&ctx->sb, p);

287. }

289. /* Pass buffer to writer function. */

290. if (ctx->status == 0) {

291. MSize n = sbuflen(&ctx->sb) - 5;

292. MSize nn = (lj_fls(n)+8)*9 >> 6;

293. char *q = sbufB(&ctx->sb) + (5 - nn);

294. p = lj_strfmt_wuleb128(q, n);  /* Fill in final size. */

295. lua_assert(p == sbufB(&ctx->sb) + 5);

296. ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata);

297. }

298. }

300. /* Write header of bytecode dump. */

301. ‌static void bcwrite_header(BCWriteCtx *ctx)

302. {

303. GCstr *chunkname = proto_chunkname(ctx->pt);

304. const char *name = strdata(chunkname);

305. MSize len = chunkname->len;

306. char *p = lj_buf_need(&ctx->sb, 5+5+len);

307. *p++ = BCDUMP_HEAD1;

308. *p++ = BCDUMP_HEAD2;

309. *p++ = BCDUMP_HEAD3;

310. *p++ = BCDUMP_VERSION;

311. *p++ = (ctx->strip ? BCDUMP_F_STRIP : 0) +

312. LJ_BE*BCDUMP_F_BE +

313. ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0) +

314. LJ_FR2*BCDUMP_F_FR2;

315. if (!ctx->strip) {

316. p = lj_strfmt_wuleb128(p, len);

317. p = lj_buf_wmem(p, name, len);

318. }

319. ctx->status = ctx->wfunc(sbufL(&ctx->sb), sbufB(&ctx->sb),

320. (MSize)(p - sbufB(&ctx->sb)), ctx->wdata);

321. }

323. /* Write footer of bytecode dump. */

324. ‌static void bcwrite_footer(BCWriteCtx *ctx)

325. {

326. if (ctx->status == 0) {

327. uint8_t zero = 0;

328. ctx->status = ctx->wfunc(sbufL(&ctx->sb), &zero, 1, ctx->wdata);

329. }

330. }

332. /* Protected callback for bytecode writer. */

333. ‌static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)

334. {

335. BCWriteCtx *ctx = (BCWriteCtx *)ud;

336. UNUSED(L); UNUSED(dummy);

337. lj_buf_need(&ctx->sb, 1024);  /* Avoids resize for most prototypes. */

338. bcwrite_header(ctx);

339. bcwrite_proto(ctx, ctx->pt);

340. bcwrite_footer(ctx);

341. return NULL;

342. }

344. /* Write bytecode for a prototype. */

345. ‌int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data,

346. int strip)

347. {

348. BCWriteCtx ctx;

349. int status;

350. ctx.pt = pt;

351. ctx.wfunc = writer;

352. ctx.wdata = data;

353. ctx.strip = strip;

354. ctx.status = 0;

355. lj_buf_init(L, &ctx.sb);

356. status = lj_vm_cpcall(L, NULL, &ctx, cpwriter);

357. if (status == 0) status = ctx.status;

358. lj_buf_free(G(sbufL(&ctx.sb)), &ctx.sb);

359. return status;

360. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ff.h - luajit-2.0-src

Data types defined

- FastFunc

Macros defined

- FFDEF

- _LJ_FF_H

Source code

1. /*

2. ** Fast function IDs.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_FF_H

7. ‌#define _LJ_FF_H

9. /* Fast function ID. */

10. typedef enum {

11. FF_LUA_ = FF_LUA,        /* Lua function (must be 0). */

12. FF_C_ = FF_C,                /* Regular C function (must be 1). */

13. ‌#define FFDEF(name)        FF_##name,

14. #include "lj_ffdef.h"

15. FF__MAX

16. ‌} FastFunc;

18. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/host/buildvm_libbc.h - luajit-2.0-src

Global variables defined

- libbc_code

- libbc_endian

- libbc_map

Source code

1. /* This is a generated file. DO NOT EDIT! */

3. ‌static const int libbc_endian = 0;

5. ‌static const uint8_t libbc_code[] = {

6. #if LJ_FR2

7. 0,1,2,0,0,1,2,24,1,0,0,76,1,2,0,241,135,158,166,3,220,203,178,130,4,0,1,2,0,

8. 0,1,2,24,1,0,0,76,1,2,0,243,244,148,165,20,198,190,199,252,3,0,1,2,0,0,0,3,

9. 16,0,5,0,21,1,0,0,76,1,2,0,0,2,10,0,0,0,15,16,0,12,0,16,1,9,0,41,2,1,0,21,3,

10. 0,0,41,4,1,0,77,2,8,128,18,6,1,0,18,8,5,0,59,9,5,0,66,6,3,2,10,6,0,0,88,7,1,

11. 128,76,6,2,0,79,2,248,127,75,0,1,0,0,2,11,0,0,0,16,16,0,12,0,16,1,9,0,43,2,

12. 0,0,18,3,0,0,41,4,0,0,88,5,7,128,18,7,1,0,18,9,5,0,18,10,6,0,66,7,3,2,10,7,

13. 0,0,88,8,1,128,76,7,2,0,70,5,3,3,82,5,247,127,75,0,1,0,0,1,2,0,0,0,3,16,0,12,

14. 0,21,1,0,0,76,1,2,0,0,2,10,0,0,2,30,16,0,12,0,21,2,0,0,11,1,0,0,88,3,7,128,

15. 8,2,0,0,88,3,23,128,59,3,2,0,43,4,0,0,64,4,2,0,76,3,2,0,88,3,18,128,16,1,14,

16. 0,41,3,1,0,3,3,1,0,88,3,14,128,3,1,2,0,88,3,12,128,59,3,1,0,22,4,1,1,18,5,2,

17. 0,41,6,1,0,77,4,4,128,23,8,1,7,59,9,7,0,64,9,8,0,79,4,252,127,43,4,0,0,64,4,

18. 2,0,76,3,2,0,75,0,1,0,0,2,0

19. #else

20. 0,1,2,0,0,1,2,24,1,0,0,76,1,2,0,241,135,158,166,3,220,203,178,130,4,0,1,2,0,

21. 0,1,2,24,1,0,0,76,1,2,0,243,244,148,165,20,198,190,199,252,3,0,1,2,0,0,0,3,

22. 16,0,5,0,21,1,0,0,76,1,2,0,0,2,9,0,0,0,15,16,0,12,0,16,1,9,0,41,2,1,0,21,3,

23. 0,0,41,4,1,0,77,2,8,128,18,6,1,0,18,7,5,0,59,8,5,0,66,6,3,2,10,6,0,0,88,7,1,

24. 128,76,6,2,0,79,2,248,127,75,0,1,0,0,2,10,0,0,0,16,16,0,12,0,16,1,9,0,43,2,

25. 0,0,18,3,0,0,41,4,0,0,88,5,7,128,18,7,1,0,18,8,5,0,18,9,6,0,66,7,3,2,10,7,0,

26. 0,88,8,1,128,76,7,2,0,70,5,3,3,82,5,247,127,75,0,1,0,0,1,2,0,0,0,3,16,0,12,

27. 0,21,1,0,0,76,1,2,0,0,2,10,0,0,2,30,16,0,12,0,21,2,0,0,11,1,0,0,88,3,7,128,

28. 8,2,0,0,88,3,23,128,59,3,2,0,43,4,0,0,64,4,2,0,76,3,2,0,88,3,18,128,16,1,14,

29. 0,41,3,1,0,3,3,1,0,88,3,14,128,3,1,2,0,88,3,12,128,59,3,1,0,22,4,1,1,18,5,2,

30. 0,41,6,1,0,77,4,4,128,23,8,1,7,59,9,7,0,64,9,8,0,79,4,252,127,43,4,0,0,64,4,

31. 2,0,76,3,2,0,75,0,1,0,0,2,0

32. #endif

33. };

35. ‌static const struct { const char *name; int ofs; } libbc_map[] = {

36. {"math_deg",0},

37. {"math_rad",25},

38. {"string_len",50},

39. {"table_foreachi",69},

40. {"table_foreach",136},

41. {"table_getn",207},

42. {"table_remove",226},

43. {NULL,355}

44. };

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_recdef.h - luajit-2.0-src

Global variables defined

- recff_func

- recff_idmap

Source code

1. /* This is a generated file. DO NOT EDIT! */

3. ‌static const uint16_t recff_idmap[] = {

4. 0,

5. 0x0100,

6. 0x0200,

7. 0x0300,

8. 0,

9. 0x0400+(0),

10. 0x0500,

11. 0x0400+(1),

12. 0x0600,

13. 0x0700,

14. 0x0800,

15. 0,

16. 0x0900,

17. 0x0a00,

18. 0x0b00,

19. 0,

20. 0x0c00,

21. 0x0d00,

22. 0x0e00,

23. 0,

24. 0x0f00,

25. 0x1000,

26. 0,

27. 0,

28. 0,

29. 0,

30. 0,

31. 0,

32. 0,

33. 0,

34. 0,

35. 0,

36. 0,

37. 0,

38. 0,

39. 0,

40. 0,

41. 0x1100,

42. 0x1200+(IRFPM_FLOOR),

43. 0x1200+(IRFPM_CEIL),

44. 0x1300+(IRFPM_SQRT),

45. 0x1300+(IRFPM_LOG10),

46. 0x1300+(IRFPM_EXP),

47. 0x1300+(IRFPM_SIN),

48. 0x1300+(IRFPM_COS),

49. 0x1300+(IRFPM_TAN),

50. 0x1400+(FF_math_asin),

51. 0x1400+(FF_math_acos),

52. 0x1400+(FF_math_atan),

53. 0x1500+(IRCALL_sinh),

54. 0x1500+(IRCALL_cosh),

55. 0x1500+(IRCALL_tanh),

56. 0,

57. 0x1600,

58. 0x1700,

59. 0x1800,

60. 0x1900,

61. 0,

62. 0x1a00,

63. 0x1b00+(IR_MIN),

64. 0x1b00+(IR_MAX),

65. 0x1c00,

66. 0,

67. 0x1d00,

68. 0x1e00+(IR_BNOT),

69. 0x1e00+(IR_BSWAP),

70. 0x1f00+(IR_BSHL),

71. 0x1f00+(IR_BSHR),

72. 0x1f00+(IR_BSAR),

73. 0x1f00+(IR_BROL),

74. 0x1f00+(IR_BROR),

75. 0x2000+(IR_BAND),

76. 0x2000+(IR_BOR),

77. 0x2000+(IR_BXOR),

78. 0x2100,

79. 0x2200+(0),

80. 0x2300,

81. 0x2200+(1),

82. 0x2400,

83. 0x2500+(IRCALL_lj_buf_putstr_reverse),

84. 0x2500+(IRCALL_lj_buf_putstr_lower),

85. 0x2500+(IRCALL_lj_buf_putstr_upper),

86. 0,

87. 0x2600,

88. 0,

89. 0,

90. 0,

91. 0,

92. 0x2700,

93. 0,

94. 0x2800,

95. 0x2900,

96. 0,

97. 0x2a00,

98. 0x2b00,

99. 0,

100. 0,

101. 0x2c00+(0),

102. 0x2d00+(0),

103. 0,

104. 0,

105. 0,

106. 0,

107. 0,

108. 0,

109. 0,

110. 0,

111. 0,

112. 0,

113. 0x2c00+(GCROOT_IO_OUTPUT),

114. 0x2d00+(GCROOT_IO_OUTPUT),

115. 0,

116. 0,

117. 0,

118. 0,

119. 0,

120. 0,

121. 0,

122. 0,

123. 0,

124. 0,

125. 0,

126. 0,

127. 0,

128. 0,

129. 0,

130. 0,

131. 0x2e00,

132. 0,

133. 0,

134. 0,

135. 0,

136. 0,

137. 0,

138. 0,

139. 0,

140. 0,

141. 0,

142. 0,

143. 0,

144. 0,

145. 0,

146. 0,

147. 0,

148. 0,

149. 0,

150. 0,

151. 0,

152. 0,

153. 0,

154. 0,

155. 0,

156. 0,

157. 0,

158. 0,

159. 0,

160. 0,

161. 0,

162. 0,

163. 0,

164. 0,

165. 0,

166. 0x2f00+(0),

167. 0x2f00+(1),

168. 0x3000+(MM_eq),

169. 0x3000+(MM_len),

170. 0x3000+(MM_lt),

171. 0x3000+(MM_le),

172. 0x3000+(MM_concat),

173. 0x3100,

174. 0x3000+(MM_add),

175. 0x3000+(MM_sub),

176. 0x3000+(MM_mul),

177. 0x3000+(MM_div),

178. 0x3000+(MM_mod),

179. 0x3000+(MM_pow),

180. 0x3000+(MM_unm),

181. 0,

182. 0,

183. 0,

184. 0x3200+(1),

185. 0x3200+(0),

186. 0,

187. 0,

188. 0,

189. 0,

190. 0x3300,

191. 0x3300,

192. 0x3400,

193. 0,

194. 0x3500,

195. 0x3600+(FF_ffi_sizeof),

196. 0x3600+(FF_ffi_alignof),

197. 0x3600+(FF_ffi_offsetof),

198. 0x3700,

199. 0x3800,

200. 0x3900,

201. 0x3a00,

202. 0x3b00,

203. 0,

204. 0x3c00

205. };

207. ‌static const RecordFunc recff_func[] = {

208. recff_nyi,

209. recff_c,

210. recff_assert,

211. recff_type,

212. recff_xpairs,

213. recff_ipairs_aux,

214. recff_getmetatable,

215. recff_setmetatable,

216. recff_getfenv,

217. recff_rawget,

218. recff_rawset,

219. recff_rawequal,

220. recff_select,

221. recff_tonumber,

222. recff_tostring,

223. recff_pcall,

224. recff_xpcall,

225. recff_math_abs,

226. recff_math_round,

227. recff_math_unary,

228. recff_math_atrig,

229. recff_math_htrig,

230. recff_math_modf,

231. recff_math_log,

232. recff_math_atan2,

233. recff_math_pow,

234. recff_math_ldexp,

235. recff_math_minmax,

236. recff_math_random,

237. recff_bit_tobit,

238. recff_bit_unary,

239. recff_bit_shift,

240. recff_bit_nary,

241. recff_bit_tohex,

242. recff_string_range,

243. recff_string_char,

244. recff_string_rep,

245. recff_string_op,

246. recff_string_find,

247. recff_string_format,

248. recff_table_insert,

249. recff_table_concat,

250. recff_table_new,

251. recff_table_clear,

252. recff_io_write,

253. recff_io_flush,

254. recff_debug_getmetatable,

255. recff_cdata_index,

256. recff_cdata_arith,

257. recff_cdata_call,

258. recff_clib_index,

259. recff_ffi_new,

260. recff_ffi_typeof,

261. recff_ffi_istype,

262. recff_ffi_xof,

263. recff_ffi_errno,

264. recff_ffi_string,

265. recff_ffi_copy,

266. recff_ffi_fill,

267. recff_ffi_abi,

268. recff_ffi_gc

269. };

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ffrecord.c - luajit-2.0-src

Data types defined

- RecordFunc

Functions defined

- argv2int

- argv2str

- lj_ffrecord_func

- lj_ffrecord_select_mode

- recdef_lookup

- recff_assert

- recff_bit_nary

- recff_bit_shift

- recff_bit_tobit

- recff_bit_tohex

- recff_bit_unary

- recff_bufhdr

- recff_debug_getmetatable

- recff_getfenv

- recff_getmetatable

- recff_io_flush

- recff_io_fp

- recff_io_write

- recff_ipairs_aux

- recff_math_abs

- recff_math_atan2

- recff_math_atrig

- recff_math_htrig

- recff_math_ldexp

- recff_math_log

- recff_math_minmax

- recff_math_modf

- recff_math_pow

- recff_math_random

- recff_math_round

- recff_math_unary

- recff_metacall

- recff_metacall_cp

- recff_nyi

- recff_pcall

- recff_rawequal

- recff_rawget

- recff_rawlen

- recff_rawset

- recff_select

- recff_setmetatable

- recff_stitch

- recff_string_char

- recff_string_find

- recff_string_format

- recff_string_op

- recff_string_range

- recff_string_rep

- recff_string_start

- recff_table_clear

- recff_table_concat

- recff_table_insert

- recff_table_new

- recff_tonumber

- recff_tostring

- recff_type

- recff_xpairs

- recff_xpcall

- recff_xpcall_cp

- results_wanted

Macros defined

- IR

- IR

- LUA_CORE

- emitir

- emitir

- lj_ffrecord_c

- recff_c

- recff_nyiu

Source code

1. /*

2. ** Fast function call recorder.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_ffrecord_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT

13. #include "lj_err.h"

14. #include "lj_str.h"

15. #include "lj_tab.h"

16. #include "lj_frame.h"

17. #include "lj_bc.h"

18. #include "lj_ff.h"

19. #include "lj_ir.h"

20. #include "lj_jit.h"

21. #include "lj_ircall.h"

22. #include "lj_iropt.h"

23. #include "lj_trace.h"

24. #include "lj_record.h"

25. #include "lj_ffrecord.h"

26. #include "lj_crecord.h"

27. #include "lj_dispatch.h"

28. #include "lj_vm.h"

29. #include "lj_strscan.h"

30. #include "lj_strfmt.h"

32. /* Some local macros to save typing. Undef'd at the end. */

33. ‌#define IR(ref)                        (&J->cur.ir[(ref)])

35. /* Pass IR on to next optimization in chain (FOLD). */

36. ‌#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))

38. /* -- Fast function recording handlers ------------------------------------ */

40. /* Conventions for fast function call handlers:

41. **

42. ** The argument slots start at J->base[0]. All of them are guaranteed to be

43. ** valid and type-specialized references. J->base[J->maxslot] is set to 0

44. ** as a sentinel. The runtime argument values start at rd->argv[0].

45. **

46. ** In general fast functions should check for presence of all of their

47. ** arguments and for the correct argument types. Some simplifications

48. ** are allowed if the interpreter throws instead. But even if recording

49. ** is aborted, the generated IR must be consistent (no zero-refs).

50. **

51. ** The number of results in rd->nres is set to 1. Handlers that return

52. ** a different number of results need to override it. A negative value

53. ** prevents return processing (e.g. for pending calls).

54. **

55. ** Results need to be stored starting at J->base[0]. Return processing

56. ** moves them to the right slots later.

57. **

58. ** The per-ffid auxiliary data is the value of the 2nd part of the

59. ** LJLIB_REC() annotation. This allows handling similar functionality

60. ** in a common handler.

61. */

63. /* Type of handler to record a fast function. */

64. ‌typedef void (LJ_FASTCALL *RecordFunc)(jit_State *J, RecordFFData *rd);

66. /* Get runtime value of int argument. */

67. ‌static int32_t argv2int(jit_State *J, TValue *o)

68. {

69. if (!lj_strscan_numberobj(o))

70. lj_trace_err(J, LJ_TRERR_BADTYPE);

71. return tvisint(o) ? intV(o) : lj_num2int(numV(o));

72. }

74. /* Get runtime value of string argument. */

75. ‌static GCstr *argv2str(jit_State *J, TValue *o)

76. {

77. if (LJ_LIKELY(tvisstr(o))) {

78. return strV(o);

79. } else {

80. GCstr *s;

81. if (!tvisnumber(o))

82. lj_trace_err(J, LJ_TRERR_BADTYPE);

83. s = lj_strfmt_number(J->L, o);

84. setstrV(J->L, o, s);

85. return s;

86. }

87. }

89. /* Return number of results wanted by caller. */

90. ‌static ptrdiff_t results_wanted(jit_State *J)

91. {

92. TValue *frame = J->L->base-1;

93. if (frame_islua(frame))

94. return (ptrdiff_t)bc_b(frame_pc(frame)[-1]) - 1;

95. else

96. return -1;

97. }

99. /* Trace stitching: add continuation below frame to start a new trace. */

100. ‌static void recff_stitch(jit_State *J)

101. {

102. ASMFunction cont = lj_cont_stitch;

103. TraceNo traceno = J->cur.traceno;

104. lua_State *L = J->L;

105. TValue *base = L->base;

106. const BCIns *pc = frame_pc(base-1);

107. TValue *pframe = frame_prevl(base-1);

108. TRef trcont;

110. lua_assert(!LJ_FR2);  /* TODO_FR2: handle frame shift. */

111. /* Move func + args up in Lua stack and insert continuation. */

112. memmove(&base[1], &base[-1], sizeof(TValue)*(J->maxslot+1));

113. setframe_ftsz(base+1, ((char *)(base+1) - (char *)pframe) + FRAME_CONT);

114. setcont(base, cont);

115. setframe_pc(base, pc);

116. if (LJ_DUALNUM) setintV(base-1, traceno); else base[-1].u64 = traceno;

117. L->base += 2;

118. L->top += 2;

120. /* Ditto for the IR. */

121. memmove(&J->base[1], &J->base[-1], sizeof(TRef)*(J->maxslot+1));

122. #if LJ_64

123. trcont = lj_ir_kptr(J, (void *)((int64_t)cont-(int64_t)lj_vm_asm_begin));

124. #else

125. trcont = lj_ir_kptr(J, (void *)cont);

126. #endif

127. J->base[0] = trcont | TREF_CONT;

128. J->base[-1] = LJ_DUALNUM ? lj_ir_kint(J,traceno) : lj_ir_knum_u64(J,traceno);

129. J->base += 2;

130. J->baseslot += 2;

131. J->framedepth++;

133. lj_record_stop(J, LJ_TRLINK_STITCH, 0);

135. /* Undo Lua stack changes. */

136. memmove(&base[-1], &base[1], sizeof(TValue)*(J->maxslot+1));

137. setframe_pc(base-1, pc);

138. L->base -= 2;

139. L->top -= 2;

140. }

142. /* Fallback handler for fast functions that are not recorded (yet). */

143. ‌static void LJ_FASTCALL recff_nyi(jit_State *J, RecordFFData *rd)

144. {

145. if (J->cur.nins < (IRRef)J->param[JIT_P_minstitch] + REF_BASE) {

146. lj_trace_err_info(J, LJ_TRERR_TRACEUV);

147. } else {

148. /* Can only stitch from Lua call. */

149. if (J->framedepth && frame_islua(J->L->base-1)) {

150. BCOp op = bc_op(*frame_pc(J->L->base-1));

151. /* Stitched trace cannot start with *M op with variable # of args. */

152. if (!(op == BC_CALLM || op == BC_CALLMT ||

153. op == BC_RETM || op == BC_TSETM)) {

154. switch (J->fn->c.ffid) {

155. case FF_error:

156. case FF_debug_sethook:

157. case FF_jit_flush:

158. break;  /* Don't stitch across special builtins. */

159. default:

160. recff_stitch(J);  /* Use trace stitching. */

161. rd->nres = -1;

162. return;

163. }

164. }

165. }

166. /* Otherwise stop trace and return to interpreter. */

167. lj_record_stop(J, LJ_TRLINK_RETURN, 0);

168. rd->nres = -1;

169. }

170. }

172. /* Fallback handler for unsupported variants of fast functions. */

173. ‌#define recff_nyiu        recff_nyi

175. /* Must stop the trace for classic C functions with arbitrary side-effects. */

176. ‌#define recff_c                recff_nyi

178. /* Emit BUFHDR for the global temporary buffer. */

179. ‌static TRef recff_bufhdr(jit_State *J)

180. {

181. return emitir(IRT(IR_BUFHDR, IRT_P32),

182. lj_ir_kptr(J, &J2G(J)->tmpbuf), IRBUFHDR_RESET);

183. }

185. /* -- Base library fast functions ----------------------------------------- */

187. ‌static void LJ_FASTCALL recff_assert(jit_State *J, RecordFFData *rd)

188. {

189. /* Arguments already specialized. The interpreter throws for nil/false. */

190. rd->nres = J->maxslot;  /* Pass through all arguments. */

191. }

193. ‌static void LJ_FASTCALL recff_type(jit_State *J, RecordFFData *rd)

194. {

195. /* Arguments already specialized. Result is a constant string. Neat, huh? */

196. uint32_t t;

197. if (tvisnumber(&rd->argv[0]))

198. t = ~LJ_TNUMX;

199. else if (LJ_64 && !LJ_GC64 && tvislightud(&rd->argv[0]))

200. t = ~LJ_TLIGHTUD;

201. else

202. t = ~itype(&rd->argv[0]);

203. J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[t]));

204. UNUSED(rd);

205. }

207. ‌static void LJ_FASTCALL recff_getmetatable(jit_State *J, RecordFFData *rd)

208. {

209. TRef tr = J->base[0];

210. if (tr) {

211. RecordIndex ix;

212. ix.tab = tr;

213. copyTV(J->L, &ix.tabv, &rd->argv[0]);

214. if (lj_record_mm_lookup(J, &ix, MM_metatable))

215. J->base[0] = ix.mobj;

216. else

217. J->base[0] = ix.mt;

218. }  /* else: Interpreter will throw. */

219. }

221. ‌static void LJ_FASTCALL recff_setmetatable(jit_State *J, RecordFFData *rd)

222. {

223. TRef tr = J->base[0];

224. TRef mt = J->base[1];

225. if (tref_istab(tr) && (tref_istab(mt) || (mt && tref_isnil(mt)))) {

226. TRef fref, mtref;

227. RecordIndex ix;

228. ix.tab = tr;

229. copyTV(J->L, &ix.tabv, &rd->argv[0]);

230. lj_record_mm_lookup(J, &ix, MM_metatable); /* Guard for no __metatable. */

231. fref = emitir(IRT(IR_FREF, IRT_P32), tr, IRFL_TAB_META);

232. mtref = tref_isnil(mt) ? lj_ir_knull(J, IRT_TAB) : mt;

233. emitir(IRT(IR_FSTORE, IRT_TAB), fref, mtref);

234. if (!tref_isnil(mt))

235. emitir(IRT(IR_TBAR, IRT_TAB), tr, 0);

236. J->base[0] = tr;

237. J->needsnap = 1;

238. }  /* else: Interpreter will throw. */

239. }

241. ‌static void LJ_FASTCALL recff_rawget(jit_State *J, RecordFFData *rd)

242. {

243. RecordIndex ix;

244. ix.tab = J->base[0]; ix.key = J->base[1];

245. if (tref_istab(ix.tab) && ix.key) {

246. ix.val = 0; ix.idxchain = 0;

247. settabV(J->L, &ix.tabv, tabV(&rd->argv[0]));

248. copyTV(J->L, &ix.keyv, &rd->argv[1]);

249. J->base[0] = lj_record_idx(J, &ix);

250. }  /* else: Interpreter will throw. */

251. }

253. ‌static void LJ_FASTCALL recff_rawset(jit_State *J, RecordFFData *rd)

254. {

255. RecordIndex ix;

256. ix.tab = J->base[0]; ix.key = J->base[1]; ix.val = J->base[2];

257. if (tref_istab(ix.tab) && ix.key && ix.val) {

258. ix.idxchain = 0;

259. settabV(J->L, &ix.tabv, tabV(&rd->argv[0]));

260. copyTV(J->L, &ix.keyv, &rd->argv[1]);

261. copyTV(J->L, &ix.valv, &rd->argv[2]);

262. lj_record_idx(J, &ix);

263. /* Pass through table at J->base[0] as result. */

264. }  /* else: Interpreter will throw. */

265. }

267. ‌static void LJ_FASTCALL recff_rawequal(jit_State *J, RecordFFData *rd)

268. {

269. TRef tra = J->base[0];

270. TRef trb = J->base[1];

271. if (tra && trb) {

272. int diff = lj_record_objcmp(J, tra, trb, &rd->argv[0], &rd->argv[1]);

273. J->base[0] = diff ? TREF_FALSE : TREF_TRUE;

274. }  /* else: Interpreter will throw. */

275. }

277. #if LJ_52

278. ‌static void LJ_FASTCALL recff_rawlen(jit_State *J, RecordFFData *rd)

279. {

280. TRef tr = J->base[0];

281. if (tref_isstr(tr))

282. J->base[0] = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN);

283. else if (tref_istab(tr))

284. J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, tr);

285. /* else: Interpreter will throw. */

286. UNUSED(rd);

287. }

288. #endif

290. /* Determine mode of select() call. */

291. ‌int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv)

292. {

293. if (tref_isstr(tr) && *strVdata(tv) == '#') {  /* select('#', ...) */

294. if (strV(tv)->len == 1) {

295. emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, strV(tv)));

296. } else {

297. TRef trptr = emitir(IRT(IR_STRREF, IRT_P32), tr, lj_ir_kint(J, 0));

298. TRef trchar = emitir(IRT(IR_XLOAD, IRT_U8), trptr, IRXLOAD_READONLY);

299. emitir(IRTG(IR_EQ, IRT_INT), trchar, lj_ir_kint(J, '#'));

300. }

301. return 0;

302. } else {  /* select(n, ...) */

303. int32_t start = argv2int(J, tv);

304. if (start == 0) lj_trace_err(J, LJ_TRERR_BADTYPE);  /* A bit misleading. */

305. return start;

306. }

307. }

309. ‌static void LJ_FASTCALL recff_select(jit_State *J, RecordFFData *rd)

310. {

311. TRef tr = J->base[0];

312. if (tr) {

313. ptrdiff_t start = lj_ffrecord_select_mode(J, tr, &rd->argv[0]);

314. if (start == 0) {  /* select('#', ...) */

315. J->base[0] = lj_ir_kint(J, J->maxslot - 1);

316. } else if (tref_isk(tr)) {  /* select(k, ...) */

317. ptrdiff_t n = (ptrdiff_t)J->maxslot;

318. if (start < 0) start += n;

319. else if (start > n) start = n;

320. rd->nres = n - start;

321. if (start >= 1) {

322. ptrdiff_t i;

323. for (i = 0; i < n - start; i++)

324. J->base[i] = J->base[start+i];

325. }  /* else: Interpreter will throw. */

326. } else {

327. recff_nyiu(J, rd);

328. return;

329. }

330. }  /* else: Interpreter will throw. */

331. }

333. ‌static void LJ_FASTCALL recff_tonumber(jit_State *J, RecordFFData *rd)

334. {

335. TRef tr = J->base[0];

336. TRef base = J->base[1];

337. if (tr && !tref_isnil(base)) {

338. base = lj_opt_narrow_toint(J, base);

339. if (!tref_isk(base) || IR(tref_ref(base))->i != 10) {

340. recff_nyiu(J, rd);

341. return;

342. }

343. }

344. if (tref_isnumber_str(tr)) {

345. if (tref_isstr(tr)) {

346. TValue tmp;

347. if (!lj_strscan_num(strV(&rd->argv[0]), &tmp)) {

348. recff_nyiu(J, rd);  /* Would need an inverted STRTO for this case. */

349. return;

350. }

351. tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);

352. }

353. #if LJ_HASFFI

354. } else if (tref_iscdata(tr)) {

355. lj_crecord_tonumber(J, rd);

356. return;

357. #endif

358. } else {

359. tr = TREF_NIL;

360. }

361. J->base[0] = tr;

362. UNUSED(rd);

363. }

365. ‌static TValue *recff_metacall_cp(lua_State *L, lua_CFunction dummy, void *ud)

366. {

367. jit_State *J = (jit_State *)ud;

368. lj_record_tailcall(J, 0, 1);

369. UNUSED(L); UNUSED(dummy);

370. return NULL;

371. }

373. ‌static int recff_metacall(jit_State *J, RecordFFData *rd, MMS mm)

374. {

375. RecordIndex ix;

376. ix.tab = J->base[0];

377. copyTV(J->L, &ix.tabv, &rd->argv[0]);

378. if (lj_record_mm_lookup(J, &ix, mm)) {  /* Has metamethod? */

379. int errcode;

380. TValue argv0;

381. /* Temporarily insert metamethod below object. */

382. J->base[1] = J->base[0];

383. J->base[0] = ix.mobj;

384. copyTV(J->L, &argv0, &rd->argv[0]);

385. copyTV(J->L, &rd->argv[1], &rd->argv[0]);

386. copyTV(J->L, &rd->argv[0], &ix.mobjv);

387. /* Need to protect lj_record_tailcall because it may throw. */

388. errcode = lj_vm_cpcall(J->L, NULL, J, recff_metacall_cp);

389. /* Always undo Lua stack changes to avoid confusing the interpreter. */

390. copyTV(J->L, &rd->argv[0], &argv0);

391. if (errcode)

392. lj_err_throw(J->L, errcode);  /* Propagate errors. */

393. rd->nres = -1;  /* Pending call. */

394. return 1;  /* Tailcalled to metamethod. */

395. }

396. return 0;

397. }

399. ‌static void LJ_FASTCALL recff_tostring(jit_State *J, RecordFFData *rd)

400. {

401. TRef tr = J->base[0];

402. if (tref_isstr(tr)) {

403. /* Ignore __tostring in the string base metatable. */

404. /* Pass on result in J->base[0]. */

405. } else if (tr && !recff_metacall(J, rd, MM_tostring)) {

406. if (tref_isnumber(tr)) {

407. J->base[0] = emitir(IRT(IR_TOSTR, IRT_STR), tr,

408. tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT);

409. } else if (tref_ispri(tr)) {

410. J->base[0] = lj_ir_kstr(J, lj_strfmt_obj(J->L, &rd->argv[0]));

411. } else {

412. recff_nyiu(J, rd);

413. return;

414. }

415. }

416. }

418. ‌static void LJ_FASTCALL recff_ipairs_aux(jit_State *J, RecordFFData *rd)

419. {

420. RecordIndex ix;

421. ix.tab = J->base[0];

422. if (tref_istab(ix.tab)) {

423. if (!tvisnumber(&rd->argv[1]))  /* No support for string coercion. */

424. lj_trace_err(J, LJ_TRERR_BADTYPE);

425. setintV(&ix.keyv, numberVint(&rd->argv[1])+1);

426. settabV(J->L, &ix.tabv, tabV(&rd->argv[0]));

427. ix.val = 0; ix.idxchain = 0;

428. ix.key = lj_opt_narrow_toint(J, J->base[1]);

429. J->base[0] = ix.key = emitir(IRTI(IR_ADD), ix.key, lj_ir_kint(J, 1));

430. J->base[1] = lj_record_idx(J, &ix);

431. rd->nres = tref_isnil(J->base[1]) ? 0 : 2;

432. }  /* else: Interpreter will throw. */

433. }

435. ‌static void LJ_FASTCALL recff_xpairs(jit_State *J, RecordFFData *rd)

436. {

437. if (!(LJ_52 && recff_metacall(J, rd, MM_ipairs))) {

438. TRef tab = J->base[0];

439. if (tref_istab(tab)) {

440. J->base[0] = lj_ir_kfunc(J, funcV(&J->fn->c.upvalue[0]));

441. J->base[1] = tab;

442. J->base[2] = rd->data ? lj_ir_kint(J, 0) : TREF_NIL;

443. rd->nres = 3;

444. }  /* else: Interpreter will throw. */

445. }

446. }

448. ‌static void LJ_FASTCALL recff_pcall(jit_State *J, RecordFFData *rd)

449. {

450. if (J->maxslot >= 1) {

451. lj_record_call(J, 0, J->maxslot - 1);

452. rd->nres = -1;  /* Pending call. */

453. }  /* else: Interpreter will throw. */

454. }

456. ‌static TValue *recff_xpcall_cp(lua_State *L, lua_CFunction dummy, void *ud)

457. {

458. jit_State *J = (jit_State *)ud;

459. lj_record_call(J, 1, J->maxslot - 2);

460. UNUSED(L); UNUSED(dummy);

461. return NULL;

462. }

464. ‌static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd)

465. {

466. if (J->maxslot >= 2) {

467. TValue argv0, argv1;

468. TRef tmp;

469. int errcode;

470. lua_assert(!LJ_FR2);  /* TODO_FR2: handle different frame setup. */

471. /* Swap function and traceback. */

472. tmp = J->base[0]; J->base[0] = J->base[1]; J->base[1] = tmp;

473. copyTV(J->L, &argv0, &rd->argv[0]);

474. copyTV(J->L, &argv1, &rd->argv[1]);

475. copyTV(J->L, &rd->argv[0], &argv1);

476. copyTV(J->L, &rd->argv[1], &argv0);

477. /* Need to protect lj_record_call because it may throw. */

478. errcode = lj_vm_cpcall(J->L, NULL, J, recff_xpcall_cp);

479. /* Always undo Lua stack swap to avoid confusing the interpreter. */

480. copyTV(J->L, &rd->argv[0], &argv0);

481. copyTV(J->L, &rd->argv[1], &argv1);

482. if (errcode)

483. lj_err_throw(J->L, errcode);  /* Propagate errors. */

484. rd->nres = -1;  /* Pending call. */

485. }  /* else: Interpreter will throw. */

486. }

488. ‌static void LJ_FASTCALL recff_getfenv(jit_State *J, RecordFFData *rd)

489. {

490. TRef tr = J->base[0];

491. /* Only support getfenv(0) for now. */

492. if (tref_isint(tr) && tref_isk(tr) && IR(tref_ref(tr))->i == 0) {

493. TRef trl = emitir(IRT(IR_LREF, IRT_THREAD), 0, 0);

494. J->base[0] = emitir(IRT(IR_FLOAD, IRT_TAB), trl, IRFL_THREAD_ENV);

495. return;

496. }

497. recff_nyiu(J, rd);

498. }

500. /* -- Math library fast functions ----------------------------------------- */

502. ‌static void LJ_FASTCALL recff_math_abs(jit_State *J, RecordFFData *rd)

503. {

504. TRef tr = lj_ir_tonum(J, J->base[0]);

505. J->base[0] = emitir(IRTN(IR_ABS), tr, lj_ir_knum_abs(J));

506. UNUSED(rd);

507. }

509. /* Record rounding functions math.floor and math.ceil. */

510. ‌static void LJ_FASTCALL recff_math_round(jit_State *J, RecordFFData *rd)

511. {

512. TRef tr = J->base[0];

513. if (!tref_isinteger(tr)) {  /* Pass through integers unmodified. */

514. tr = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, tr), rd->data);

515. /* Result is integral (or NaN/Inf), but may not fit an int32_t. */

516. if (LJ_DUALNUM) {  /* Try to narrow using a guarded conversion to int. */

517. lua_Number n = lj_vm_foldfpm(numberVnum(&rd->argv[0]), rd->data);

518. if (n == (lua_Number)lj_num2int(n))

519. tr = emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_CHECK);

520. }

521. J->base[0] = tr;

522. }

523. }

525. /* Record unary math.* functions, mapped to IR_FPMATH opcode. */

526. ‌static void LJ_FASTCALL recff_math_unary(jit_State *J, RecordFFData *rd)

527. {

528. J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data);

529. }

531. /* Record math.log. */

532. ‌static void LJ_FASTCALL recff_math_log(jit_State *J, RecordFFData *rd)

533. {

534. TRef tr = lj_ir_tonum(J, J->base[0]);

535. if (J->base[1]) {

536. #ifdef LUAJIT_NO_LOG2

537. uint32_t fpm = IRFPM_LOG;

538. #else

539. uint32_t fpm = IRFPM_LOG2;

540. #endif

541. TRef trb = lj_ir_tonum(J, J->base[1]);

542. tr = emitir(IRTN(IR_FPMATH), tr, fpm);

543. trb = emitir(IRTN(IR_FPMATH), trb, fpm);

544. trb = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), trb);

545. tr = emitir(IRTN(IR_MUL), tr, trb);

546. } else {

547. tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_LOG);

548. }

549. J->base[0] = tr;

550. UNUSED(rd);

551. }

553. /* Record math.atan2. */

554. ‌static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd)

555. {

556. TRef tr = lj_ir_tonum(J, J->base[0]);

557. TRef tr2 = lj_ir_tonum(J, J->base[1]);

558. J->base[0] = emitir(IRTN(IR_ATAN2), tr, tr2);

559. UNUSED(rd);

560. }

562. /* Record math.ldexp. */

563. ‌static void LJ_FASTCALL recff_math_ldexp(jit_State *J, RecordFFData *rd)

564. {

565. TRef tr = lj_ir_tonum(J, J->base[0]);

566. #if LJ_TARGET_X86ORX64

567. TRef tr2 = lj_ir_tonum(J, J->base[1]);

568. #else

569. TRef tr2 = lj_opt_narrow_toint(J, J->base[1]);

570. #endif

571. J->base[0] = emitir(IRTN(IR_LDEXP), tr, tr2);

572. UNUSED(rd);

573. }

575. /* Record math.asin, math.acos, math.atan. */

576. ‌static void LJ_FASTCALL recff_math_atrig(jit_State *J, RecordFFData *rd)

577. {

578. TRef y = lj_ir_tonum(J, J->base[0]);

579. TRef x = lj_ir_knum_one(J);

580. uint32_t ffid = rd->data;

581. if (ffid != FF_math_atan) {

582. TRef tmp = emitir(IRTN(IR_MUL), y, y);

583. tmp = emitir(IRTN(IR_SUB), x, tmp);

584. tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_SQRT);

585. if (ffid == FF_math_asin) { x = tmp; } else { x = y; y = tmp; }

586. }

587. J->base[0] = emitir(IRTN(IR_ATAN2), y, x);

588. }

590. ‌static void LJ_FASTCALL recff_math_htrig(jit_State *J, RecordFFData *rd)

591. {

592. TRef tr = lj_ir_tonum(J, J->base[0]);

593. J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data);

594. }

596. ‌static void LJ_FASTCALL recff_math_modf(jit_State *J, RecordFFData *rd)

597. {

598. TRef tr = J->base[0];

599. if (tref_isinteger(tr)) {

600. J->base[0] = tr;

601. J->base[1] = lj_ir_kint(J, 0);

602. } else {

603. TRef trt;

604. tr = lj_ir_tonum(J, tr);

605. trt = emitir(IRTN(IR_FPMATH), tr, IRFPM_TRUNC);

606. J->base[0] = trt;

607. J->base[1] = emitir(IRTN(IR_SUB), tr, trt);

608. }

609. rd->nres = 2;

610. }

612. ‌static void LJ_FASTCALL recff_math_pow(jit_State *J, RecordFFData *rd)

613. {

614. TRef tr = lj_ir_tonum(J, J->base[0]);

615. if (!tref_isnumber_str(J->base[1]))

616. lj_trace_err(J, LJ_TRERR_BADTYPE);

617. J->base[0] = lj_opt_narrow_pow(J, tr, J->base[1], &rd->argv[1]);

618. UNUSED(rd);

619. }

621. ‌static void LJ_FASTCALL recff_math_minmax(jit_State *J, RecordFFData *rd)

622. {

623. TRef tr = lj_ir_tonumber(J, J->base[0]);

624. uint32_t op = rd->data;

625. BCReg i;

626. for (i = 1; J->base[i] != 0; i++) {

627. TRef tr2 = lj_ir_tonumber(J, J->base[i]);

628. IRType t = IRT_INT;

629. if (!(tref_isinteger(tr) && tref_isinteger(tr2))) {

630. if (tref_isinteger(tr)) tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT);

631. if (tref_isinteger(tr2)) tr2 = emitir(IRTN(IR_CONV), tr2, IRCONV_NUM_INT);

632. t = IRT_NUM;

633. }

634. tr = emitir(IRT(op, t), tr, tr2);

635. }

636. J->base[0] = tr;

637. }

639. ‌static void LJ_FASTCALL recff_math_random(jit_State *J, RecordFFData *rd)

640. {

641. GCudata *ud = udataV(&J->fn->c.upvalue[0]);

642. TRef tr, one;

643. lj_ir_kgc(J, obj2gco(ud), IRT_UDATA);  /* Prevent collection. */

644. tr = lj_ir_call(J, IRCALL_lj_math_random_step, lj_ir_kptr(J, uddata(ud)));

645. one = lj_ir_knum_one(J);

646. tr = emitir(IRTN(IR_SUB), tr, one);

647. if (J->base[0]) {

648. TRef tr1 = lj_ir_tonum(J, J->base[0]);

649. if (J->base[1]) {  /* d = floor(d*(r2-r1+1.0)) + r1 */

650. TRef tr2 = lj_ir_tonum(J, J->base[1]);

651. tr2 = emitir(IRTN(IR_SUB), tr2, tr1);

652. tr2 = emitir(IRTN(IR_ADD), tr2, one);

653. tr = emitir(IRTN(IR_MUL), tr, tr2);

654. tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR);

655. tr = emitir(IRTN(IR_ADD), tr, tr1);

656. } else {  /* d = floor(d*r1) + 1.0 */

657. tr = emitir(IRTN(IR_MUL), tr, tr1);

658. tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR);

659. tr = emitir(IRTN(IR_ADD), tr, one);

660. }

661. }

662. J->base[0] = tr;

663. UNUSED(rd);

664. }

666. /* -- Bit library fast functions ------------------------------------------ */

668. /* Record bit.tobit. */

669. ‌static void LJ_FASTCALL recff_bit_tobit(jit_State *J, RecordFFData *rd)

670. {

671. TRef tr = J->base[0];

672. #if LJ_HASFFI

673. if (tref_iscdata(tr)) { recff_bit64_tobit(J, rd); return; }

674. #endif

675. J->base[0] = lj_opt_narrow_tobit(J, tr);

676. UNUSED(rd);

677. }

679. /* Record unary bit.bnot, bit.bswap. */

680. ‌static void LJ_FASTCALL recff_bit_unary(jit_State *J, RecordFFData *rd)

681. {

682. #if LJ_HASFFI

683. if (recff_bit64_unary(J, rd))

684. return;

685. #endif

686. J->base[0] = emitir(IRTI(rd->data), lj_opt_narrow_tobit(J, J->base[0]), 0);

687. }

689. /* Record N-ary bit.band, bit.bor, bit.bxor. */

690. ‌static void LJ_FASTCALL recff_bit_nary(jit_State *J, RecordFFData *rd)

691. {

692. #if LJ_HASFFI

693. if (recff_bit64_nary(J, rd))

694. return;

695. #endif

696. {

697. TRef tr = lj_opt_narrow_tobit(J, J->base[0]);

698. uint32_t ot = IRTI(rd->data);

699. BCReg i;

700. for (i = 1; J->base[i] != 0; i++)

701. tr = emitir(ot, tr, lj_opt_narrow_tobit(J, J->base[i]));

702. J->base[0] = tr;

703. }

704. }

706. /* Record bit shifts. */

707. ‌static void LJ_FASTCALL recff_bit_shift(jit_State *J, RecordFFData *rd)

708. {

709. #if LJ_HASFFI

710. if (recff_bit64_shift(J, rd))

711. return;

712. #endif

713. {

714. TRef tr = lj_opt_narrow_tobit(J, J->base[0]);

715. TRef tsh = lj_opt_narrow_tobit(J, J->base[1]);

716. IROp op = (IROp)rd->data;

717. if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) &&

718. !tref_isk(tsh))

719. tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 31));

720. #ifdef LJ_TARGET_UNIFYROT

721. if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) {

722. op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR;

723. tsh = emitir(IRTI(IR_NEG), tsh, tsh);

724. }

725. #endif

726. J->base[0] = emitir(IRTI(op), tr, tsh);

727. }

728. }

730. ‌static void LJ_FASTCALL recff_bit_tohex(jit_State *J, RecordFFData *rd)

731. {

732. #if LJ_HASFFI

733. TRef hdr = recff_bufhdr(J);

734. TRef tr = recff_bit64_tohex(J, rd, hdr);

735. J->base[0] = emitir(IRT(IR_BUFSTR, IRT_STR), tr, hdr);

736. #else

737. recff_nyiu(J, rd);  /* Don't bother working around this NYI. */

738. #endif

739. }

741. /* -- String library fast functions --------------------------------------- */

743. /* Specialize to relative starting position for string. */

744. ‌static TRef recff_string_start(jit_State *J, GCstr *s, int32_t *st, TRef tr,

745. TRef trlen, TRef tr0)

746. {

747. int32_t start = *st;

748. if (start < 0) {

749. emitir(IRTGI(IR_LT), tr, tr0);

750. tr = emitir(IRTI(IR_ADD), trlen, tr);

751. start = start + (int32_t)s->len;

752. emitir(start < 0 ? IRTGI(IR_LT) : IRTGI(IR_GE), tr, tr0);

753. if (start < 0) {

754. tr = tr0;

755. start = 0;

756. }

757. } else if (start == 0) {

758. emitir(IRTGI(IR_EQ), tr, tr0);

759. tr = tr0;

760. } else {

761. tr = emitir(IRTI(IR_ADD), tr, lj_ir_kint(J, -1));

762. emitir(IRTGI(IR_GE), tr, tr0);

763. start--;

764. }

765. *st = start;

766. return tr;

767. }

769. /* Handle string.byte (rd->data = 0) and string.sub (rd->data = 1). */

770. ‌static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd)

771. {

772. TRef trstr = lj_ir_tostr(J, J->base[0]);

773. TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN);

774. TRef tr0 = lj_ir_kint(J, 0);

775. TRef trstart, trend;

776. GCstr *str = argv2str(J, &rd->argv[0]);

777. int32_t start, end;

778. if (rd->data) {  /* string.sub(str, start [,end]) */

779. start = argv2int(J, &rd->argv[1]);

780. trstart = lj_opt_narrow_toint(J, J->base[1]);

781. trend = J->base[2];

782. if (tref_isnil(trend)) {

783. trend = lj_ir_kint(J, -1);

784. end = -1;

785. } else {

786. trend = lj_opt_narrow_toint(J, trend);

787. end = argv2int(J, &rd->argv[2]);

788. }

789. } else {  /* string.byte(str, [,start [,end]]) */

790. if (tref_isnil(J->base[1])) {

791. start = 1;

792. trstart = lj_ir_kint(J, 1);

793. } else {

794. start = argv2int(J, &rd->argv[1]);

795. trstart = lj_opt_narrow_toint(J, J->base[1]);

796. }

797. if (J->base[1] && !tref_isnil(J->base[2])) {

798. trend = lj_opt_narrow_toint(J, J->base[2]);

799. end = argv2int(J, &rd->argv[2]);

800. } else {

801. trend = trstart;

802. end = start;

803. }

804. }

805. if (end < 0) {

806. emitir(IRTGI(IR_LT), trend, tr0);

807. trend = emitir(IRTI(IR_ADD), emitir(IRTI(IR_ADD), trlen, trend),

808. lj_ir_kint(J, 1));

809. end = end+(int32_t)str->len+1;

810. } else if ((MSize)end <= str->len) {

811. emitir(IRTGI(IR_ULE), trend, trlen);

812. } else {

813. emitir(IRTGI(IR_UGT), trend, trlen);

814. end = (int32_t)str->len;

815. trend = trlen;

816. }

817. trstart = recff_string_start(J, str, &start, trstart, trlen, tr0);

818. if (rd->data) {  /* Return string.sub result. */

819. if (end - start >= 0) {

820. /* Also handle empty range here, to avoid extra traces. */

821. TRef trptr, trslen = emitir(IRTI(IR_SUB), trend, trstart);

822. emitir(IRTGI(IR_GE), trslen, tr0);

823. trptr = emitir(IRT(IR_STRREF, IRT_P32), trstr, trstart);

824. J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), trptr, trslen);

825. } else {  /* Range underflow: return empty string. */

826. emitir(IRTGI(IR_LT), trend, trstart);

827. J->base[0] = lj_ir_kstr(J, &J2G(J)->strempty);

828. }

829. } else {  /* Return string.byte result(s). */

830. ptrdiff_t i, len = end - start;

831. if (len > 0) {

832. TRef trslen = emitir(IRTI(IR_SUB), trend, trstart);

833. emitir(IRTGI(IR_EQ), trslen, lj_ir_kint(J, (int32_t)len));

834. if (J->baseslot + len > LJ_MAX_JSLOTS)

835. lj_trace_err_info(J, LJ_TRERR_STACKOV);

836. rd->nres = len;

837. for (i = 0; i < len; i++) {

838. TRef tmp = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, (int32_t)i));

839. tmp = emitir(IRT(IR_STRREF, IRT_P32), trstr, tmp);

840. J->base[i] = emitir(IRT(IR_XLOAD, IRT_U8), tmp, IRXLOAD_READONLY);

841. }

842. } else {  /* Empty range or range underflow: return no results. */

843. emitir(IRTGI(IR_LE), trend, trstart);

844. rd->nres = 0;

845. }

846. }

847. }

849. ‌static void LJ_FASTCALL recff_string_char(jit_State *J, RecordFFData *rd)

850. {

851. TRef k255 = lj_ir_kint(J, 255);

852. BCReg i;

853. for (i = 0; J->base[i] != 0; i++) {  /* Convert char values to strings. */

854. TRef tr = lj_opt_narrow_toint(J, J->base[i]);

855. emitir(IRTGI(IR_ULE), tr, k255);

856. J->base[i] = emitir(IRT(IR_TOSTR, IRT_STR), tr, IRTOSTR_CHAR);

857. }

858. if (i > 1) {  /* Concatenate the strings, if there's more than one. */

859. TRef hdr = recff_bufhdr(J), tr = hdr;

860. for (i = 0; J->base[i] != 0; i++)

861. tr = emitir(IRT(IR_BUFPUT, IRT_P32), tr, J->base[i]);

862. J->base[0] = emitir(IRT(IR_BUFSTR, IRT_STR), tr, hdr);

863. }

864. UNUSED(rd);

865. }

867. ‌static void LJ_FASTCALL recff_string_rep(jit_State *J, RecordFFData *rd)

868. {

869. TRef str = lj_ir_tostr(J, J->base[0]);

870. TRef rep = lj_opt_narrow_toint(J, J->base[1]);

871. TRef hdr, tr, str2 = 0;

872. if (!tref_isnil(J->base[2])) {

873. TRef sep = lj_ir_tostr(J, J->base[2]);

874. int32_t vrep = argv2int(J, &rd->argv[1]);

875. emitir(IRTGI(vrep > 1 ? IR_GT : IR_LE), rep, lj_ir_kint(J, 1));

876. if (vrep > 1) {

877. TRef hdr2 = recff_bufhdr(J);

878. TRef tr2 = emitir(IRT(IR_BUFPUT, IRT_P32), hdr2, sep);

879. tr2 = emitir(IRT(IR_BUFPUT, IRT_P32), tr2, str);

880. str2 = emitir(IRT(IR_BUFSTR, IRT_STR), tr2, hdr2);

881. }

882. }

883. tr = hdr = recff_bufhdr(J);

884. if (str2) {

885. tr = emitir(IRT(IR_BUFPUT, IRT_P32), tr, str);

886. str = str2;

887. rep = emitir(IRTI(IR_ADD), rep, lj_ir_kint(J, -1));

888. }

889. tr = lj_ir_call(J, IRCALL_lj_buf_putstr_rep, tr, str, rep);

890. J->base[0] = emitir(IRT(IR_BUFSTR, IRT_STR), tr, hdr);

891. }

893. ‌static void LJ_FASTCALL recff_string_op(jit_State *J, RecordFFData *rd)

894. {

895. TRef str = lj_ir_tostr(J, J->base[0]);

896. TRef hdr = recff_bufhdr(J);

897. TRef tr = lj_ir_call(J, rd->data, hdr, str);

898. J->base[0] = emitir(IRT(IR_BUFSTR, IRT_STR), tr, hdr);

899. }

901. ‌static void LJ_FASTCALL recff_string_find(jit_State *J, RecordFFData *rd)

902. {

903. TRef trstr = lj_ir_tostr(J, J->base[0]);

904. TRef trpat = lj_ir_tostr(J, J->base[1]);

905. TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN);

906. TRef tr0 = lj_ir_kint(J, 0);

907. TRef trstart;

908. GCstr *str = argv2str(J, &rd->argv[0]);

909. GCstr *pat = argv2str(J, &rd->argv[1]);

910. int32_t start;

911. J->needsnap = 1;

912. if (tref_isnil(J->base[2])) {

913. trstart = lj_ir_kint(J, 1);

914. start = 1;

915. } else {

916. trstart = lj_opt_narrow_toint(J, J->base[2]);

917. start = argv2int(J, &rd->argv[2]);

918. }

919. trstart = recff_string_start(J, str, &start, trstart, trlen, tr0);

920. if ((MSize)start <= str->len) {

921. emitir(IRTGI(IR_ULE), trstart, trlen);

922. } else {

923. emitir(IRTGI(IR_UGT), trstart, trlen);

924. #if LJ_52

925. J->base[0] = TREF_NIL;

926. return;

927. #else

928. trstart = trlen;

929. start = str->len;

930. #endif

931. }

932. /* Fixed arg or no pattern matching chars? (Specialized to pattern string.) */

933. if ((J->base[2] && tref_istruecond(J->base[3])) ||

934. (emitir(IRTG(IR_EQ, IRT_STR), trpat, lj_ir_kstr(J, pat)),

935. !lj_str_haspattern(pat))) {  /* Search for fixed string. */

936. TRef trsptr = emitir(IRT(IR_STRREF, IRT_P32), trstr, trstart);

937. TRef trpptr = emitir(IRT(IR_STRREF, IRT_P32), trpat, tr0);

938. TRef trslen = emitir(IRTI(IR_SUB), trlen, trstart);

939. TRef trplen = emitir(IRTI(IR_FLOAD), trpat, IRFL_STR_LEN);

940. TRef tr = lj_ir_call(J, IRCALL_lj_str_find, trsptr, trpptr, trslen, trplen);

941. TRef trp0 = lj_ir_kkptr(J, NULL);

942. if (lj_str_find(strdata(str)+(MSize)start, strdata(pat),

943. str->len-(MSize)start, pat->len)) {

944. TRef pos;

945. emitir(IRTG(IR_NE, IRT_P32), tr, trp0);

946. pos = emitir(IRTI(IR_SUB), tr, emitir(IRT(IR_STRREF, IRT_P32), trstr, tr0));

947. J->base[0] = emitir(IRTI(IR_ADD), pos, lj_ir_kint(J, 1));

948. J->base[1] = emitir(IRTI(IR_ADD), pos, trplen);

949. rd->nres = 2;

950. } else {

951. emitir(IRTG(IR_EQ, IRT_P32), tr, trp0);

952. J->base[0] = TREF_NIL;

953. }

954. } else {  /* Search for pattern. */

955. recff_nyiu(J, rd);

956. return;

957. }

958. }

960. ‌static void LJ_FASTCALL recff_string_format(jit_State *J, RecordFFData *rd)

961. {

962. TRef trfmt = lj_ir_tostr(J, J->base[0]);

963. GCstr *fmt = argv2str(J, &rd->argv[0]);

964. int arg = 1;

965. TRef hdr, tr;

966. FormatState fs;

967. SFormat sf;

968. /* Specialize to the format string. */

969. emitir(IRTG(IR_EQ, IRT_STR), trfmt, lj_ir_kstr(J, fmt));

970. tr = hdr = recff_bufhdr(J);

971. lj_strfmt_init(&fs, strdata(fmt), fmt->len);

972. while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) {  /* Parse format. */

973. TRef tra = sf == STRFMT_LIT ? 0 : J->base[arg++];

974. TRef trsf = lj_ir_kint(J, (int32_t)sf);

975. IRCallID id;

976. switch (STRFMT_TYPE(sf)) {

977. case STRFMT_LIT:

978. tr = emitir(IRT(IR_BUFPUT, IRT_P32), tr,

979. lj_ir_kstr(J, lj_str_new(J->L, fs.str, fs.len)));

980. break;

981. case STRFMT_INT:

982. id = IRCALL_lj_strfmt_putfnum_int;

983. handle_int:

984. if (!tref_isinteger(tra))

985. goto handle_num;

986. if (sf == STRFMT_INT) { /* Shortcut for plain %d. */

987. tr = emitir(IRT(IR_BUFPUT, IRT_P32), tr,

988. emitir(IRT(IR_TOSTR, IRT_STR), tra, IRTOSTR_INT));

989. } else {

990. #if LJ_HASFFI

991. tra = emitir(IRT(IR_CONV, IRT_U64), tra,

992. (IRT_INT|(IRT_U64<<5)|IRCONV_SEXT));

993. tr = lj_ir_call(J, IRCALL_lj_strfmt_putfxint, tr, trsf, tra);

994. lj_needsplit(J);

995. #else

996. recff_nyiu(J, rd);  /* Don't bother working around this NYI. */

997. return;

998. #endif

999. }

1000. break;

1001. case STRFMT_UINT:

1002. id = IRCALL_lj_strfmt_putfnum_uint;

1003. goto handle_int;

1004. case STRFMT_NUM:

1005. id = IRCALL_lj_strfmt_putfnum;

1006. handle_num:

1007. tra = lj_ir_tonum(J, tra);

1008. tr = lj_ir_call(J, id, tr, trsf, tra);

1009. if (LJ_SOFTFP) lj_needsplit(J);

1010. break;

1011. case STRFMT_STR:

1012. if (!tref_isstr(tra)) {

1013. recff_nyiu(J, rd);  /* NYI: __tostring and non-string types for %s. */

1014. return;

1015. }

1016. if (sf == STRFMT_STR)  /* Shortcut for plain %s. */

1017. tr = emitir(IRT(IR_BUFPUT, IRT_P32), tr, tra);

1018. else if ((sf & STRFMT_T_QUOTED))

1019. tr = lj_ir_call(J, IRCALL_lj_strfmt_putquoted, tr, tra);

1020. else

1021. tr = lj_ir_call(J, IRCALL_lj_strfmt_putfstr, tr, trsf, tra);

1022. break;

1023. case STRFMT_CHAR:

1024. tra = lj_opt_narrow_toint(J, tra);

1025. if (sf == STRFMT_CHAR)  /* Shortcut for plain %c. */

1026. tr = emitir(IRT(IR_BUFPUT, IRT_P32), tr,

1027. emitir(IRT(IR_TOSTR, IRT_STR), tra, IRTOSTR_CHAR));

1028. else

1029. tr = lj_ir_call(J, IRCALL_lj_strfmt_putfchar, tr, trsf, tra);

1030. break;

1031. case STRFMT_PTR:  /* NYI */

1032. case STRFMT_ERR:

1033. default:

1034. recff_nyiu(J, rd);

1035. return;

1036. }

1037. }

1038. J->base[0] = emitir(IRT(IR_BUFSTR, IRT_STR), tr, hdr);

1039. }

1041. /* -- Table library fast functions ---------------------------------------- */

1043. ‌static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd)

1044. {

1045. RecordIndex ix;

1046. ix.tab = J->base[0];

1047. ix.val = J->base[1];

1048. rd->nres = 0;

1049. if (tref_istab(ix.tab) && ix.val) {

1050. if (!J->base[2]) {  /* Simple push: t[#t+1] = v */

1051. TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, ix.tab);

1052. GCtab *t = tabV(&rd->argv[0]);

1053. ix.key = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1));

1054. settabV(J->L, &ix.tabv, t);

1055. setintV(&ix.keyv, lj_tab_len(t) + 1);

1056. ix.idxchain = 0;

1057. lj_record_idx(J, &ix);  /* Set new value. */

1058. } else {  /* Complex case: insert in the middle. */

1059. recff_nyiu(J, rd);

1060. return;

1061. }

1062. }  /* else: Interpreter will throw. */

1063. }

1065. ‌static void LJ_FASTCALL recff_table_concat(jit_State *J, RecordFFData *rd)

1066. {

1067. TRef tab = J->base[0];

1068. if (tref_istab(tab)) {

1069. TRef sep = !tref_isnil(J->base[1]) ?

1070. lj_ir_tostr(J, J->base[1]) : lj_ir_knull(J, IRT_STR);

1071. TRef tri = (J->base[1] && !tref_isnil(J->base[2])) ?

1072. lj_opt_narrow_toint(J, J->base[2]) : lj_ir_kint(J, 1);

1073. TRef tre = (J->base[1] && J->base[2] && !tref_isnil(J->base[3])) ?

1074. lj_opt_narrow_toint(J, J->base[3]) :

1075. lj_ir_call(J, IRCALL_lj_tab_len, tab);

1076. TRef hdr = recff_bufhdr(J);

1077. TRef tr = lj_ir_call(J, IRCALL_lj_buf_puttab, hdr, tab, sep, tri, tre);

1078. emitir(IRTG(IR_NE, IRT_PTR), tr, lj_ir_kptr(J, NULL));

1079. J->base[0] = emitir(IRT(IR_BUFSTR, IRT_STR), tr, hdr);

1080. }  /* else: Interpreter will throw. */

1081. UNUSED(rd);

1082. }

1084. ‌static void LJ_FASTCALL recff_table_new(jit_State *J, RecordFFData *rd)

1085. {

1086. TRef tra = lj_opt_narrow_toint(J, J->base[0]);

1087. TRef trh = lj_opt_narrow_toint(J, J->base[1]);

1088. J->base[0] = lj_ir_call(J, IRCALL_lj_tab_new_ah, tra, trh);

1089. UNUSED(rd);

1090. }

1092. ‌static void LJ_FASTCALL recff_table_clear(jit_State *J, RecordFFData *rd)

1093. {

1094. TRef tr = J->base[0];

1095. if (tref_istab(tr)) {

1096. rd->nres = 0;

1097. lj_ir_call(J, IRCALL_lj_tab_clear, tr);

1098. J->needsnap = 1;

1099. }  /* else: Interpreter will throw. */

1100. }

1102. /* -- I/O library fast functions ------------------------------------------ */

1104. /* Get FILE* for I/O function. Any I/O error aborts recording, so there's

1105. ** no need to encode the alternate cases for any of the guards.

1106. */

1107. ‌static TRef recff_io_fp(jit_State *J, TRef *udp, int32_t id)

1108. {

1109. TRef tr, ud, fp;

1110. if (id) {  /* io.func() */

1111. tr = lj_ir_kptr(J, &J2G(J)->gcroot[id]);

1112. ud = emitir(IRT(IR_XLOAD, IRT_UDATA), tr, 0);

1113. } else {  /* fp:method() */

1114. ud = J->base[0];

1115. if (!tref_isudata(ud))

1116. lj_trace_err(J, LJ_TRERR_BADTYPE);

1117. tr = emitir(IRT(IR_FLOAD, IRT_U8), ud, IRFL_UDATA_UDTYPE);

1118. emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE));

1119. }

1120. *udp = ud;

1121. fp = emitir(IRT(IR_FLOAD, IRT_PTR), ud, IRFL_UDATA_FILE);

1122. emitir(IRTG(IR_NE, IRT_PTR), fp, lj_ir_knull(J, IRT_PTR));

1123. return fp;

1124. }

1126. ‌static void LJ_FASTCALL recff_io_write(jit_State *J, RecordFFData *rd)

1127. {

1128. TRef ud, fp = recff_io_fp(J, &ud, rd->data);

1129. TRef zero = lj_ir_kint(J, 0);

1130. TRef one = lj_ir_kint(J, 1);

1131. ptrdiff_t i = rd->data == 0 ? 1 : 0;

1132. for (; J->base[i]; i++) {

1133. TRef str = lj_ir_tostr(J, J->base[i]);

1134. TRef buf = emitir(IRT(IR_STRREF, IRT_P32), str, zero);

1135. TRef len = emitir(IRTI(IR_FLOAD), str, IRFL_STR_LEN);

1136. if (tref_isk(len) && IR(tref_ref(len))->i == 1) {

1137. IRIns *irs = IR(tref_ref(str));

1138. TRef tr = (irs->o == IR_TOSTR && irs->op2 == IRTOSTR_CHAR) ?

1139. irs->op1 :

1140. emitir(IRT(IR_XLOAD, IRT_U8), buf, IRXLOAD_READONLY);

1141. tr = lj_ir_call(J, IRCALL_fputc, tr, fp);

1142. if (results_wanted(J) != 0)  /* Check result only if not ignored. */

1143. emitir(IRTGI(IR_NE), tr, lj_ir_kint(J, -1));

1144. } else {

1145. TRef tr = lj_ir_call(J, IRCALL_fwrite, buf, one, len, fp);

1146. if (results_wanted(J) != 0)  /* Check result only if not ignored. */

1147. emitir(IRTGI(IR_EQ), tr, len);

1148. }

1149. }

1150. J->base[0] = LJ_52 ? ud : TREF_TRUE;

1151. }

1153. ‌static void LJ_FASTCALL recff_io_flush(jit_State *J, RecordFFData *rd)

1154. {

1155. TRef ud, fp = recff_io_fp(J, &ud, rd->data);

1156. TRef tr = lj_ir_call(J, IRCALL_fflush, fp);

1157. if (results_wanted(J) != 0)  /* Check result only if not ignored. */

1158. emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0));

1159. J->base[0] = TREF_TRUE;

1160. }

1162. /* -- Debug library fast functions ---------------------------------------- */

1164. ‌static void LJ_FASTCALL recff_debug_getmetatable(jit_State *J, RecordFFData *rd)

1165. {

1166. GCtab *mt;

1167. TRef mtref;

1168. TRef tr = J->base[0];

1169. if (tref_istab(tr)) {

1170. mt = tabref(tabV(&rd->argv[0])->metatable);

1171. mtref = emitir(IRT(IR_FLOAD, IRT_TAB), tr, IRFL_TAB_META);

1172. } else if (tref_isudata(tr)) {

1173. mt = tabref(udataV(&rd->argv[0])->metatable);

1174. mtref = emitir(IRT(IR_FLOAD, IRT_TAB), tr, IRFL_UDATA_META);

1175. } else {

1176. mt = tabref(basemt_obj(J2G(J), &rd->argv[0]));

1177. J->base[0] = mt ? lj_ir_ktab(J, mt) : TREF_NIL;

1178. return;

1179. }

1180. emitir(IRTG(mt ? IR_NE : IR_EQ, IRT_TAB), mtref, lj_ir_knull(J, IRT_TAB));

1181. J->base[0] = mt ? mtref : TREF_NIL;

1182. }

1184. /* -- Record calls to fast functions -------------------------------------- */

1186. #include "lj_recdef.h"

1188. ‌static uint32_t recdef_lookup(GCfunc *fn)

1189. {

1190. if (fn->c.ffid < sizeof(recff_idmap)/sizeof(recff_idmap[0]))

1191. return recff_idmap[fn->c.ffid];

1192. else

1193. return 0;

1194. }

1196. /* Record entry to a fast function or C function. */

1197. ‌void lj_ffrecord_func(jit_State *J)

1198. {

1199. RecordFFData rd;

1200. uint32_t m = recdef_lookup(J->fn);

1201. rd.data = m & 0xff;

1202. rd.nres = 1;  /* Default is one result. */

1203. rd.argv = J->L->base;

1204. J->base[J->maxslot] = 0;  /* Mark end of arguments. */

1205. (recff_func[m >> 8])(J, &rd);  /* Call recff_* handler. */

1206. if (rd.nres >= 0) {

1207. if (J->postproc == LJ_POST_NONE) J->postproc = LJ_POST_FFRETRY;

1208. lj_record_ret(J, 0, rd.nres);

1209. }

1210. }

1212. ‌#undef IR

1213. ‌#undef emitir

1215. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ffrecord.h - luajit-2.0-src

Data types defined

- RecordFFData

- RecordFFData

Macros defined

- _LJ_FFRECORD_H

Source code

1. /*

2. ** Fast function call recorder.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_FFRECORD_H

7. ‌#define _LJ_FFRECORD_H

9. #include "lj_obj.h"

10. #include "lj_jit.h"

12. #if LJ_HASJIT

13. /* Data used by handlers to record a fast function. */

14. ‌typedef struct RecordFFData {

15. TValue *argv;                /* Runtime argument values. */

16. ptrdiff_t nres;        /* Number of returned results (defaults to 1). */

17. uint32_t data;        /* Per-ffid auxiliary data (opcode, literal etc.). */

18. ‌} RecordFFData;

20. LJ_FUNC int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv);

21. LJ_FUNC void lj_ffrecord_func(jit_State *J);

22. #endif

24. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_record.c - luajit-2.0-src

Data types defined

- LoopEvent

Functions defined

- canonicalize_slots

- check_call_unroll

- check_downrec_unroll

- find_kinit

- fori_arg

- fori_load

- getcurrf

- innerloopleft

- lj_record_call

- lj_record_constify

- lj_record_idx

- lj_record_ins

- lj_record_mm_lookup

- lj_record_objcmp

- lj_record_ret

- lj_record_setup

- lj_record_stop

- lj_record_tailcall

- nommstr

- rec_call_setup

- rec_call_specialize

- rec_cat

- rec_check_ir

- rec_check_slots

- rec_comp_fixup

- rec_comp_prep

- rec_for

- rec_for_check

- rec_for_direction

- rec_for_iter

- rec_for_loop

- rec_func_jit

- rec_func_lua

- rec_func_setup

- rec_func_vararg

- rec_idx_abc

- rec_idx_key

- rec_iterl

- rec_loop

- rec_loop_interp

- rec_loop_jit

- rec_mm_arith

- rec_mm_callcomp

- rec_mm_comp

- rec_mm_comp_cdata

- rec_mm_equal

- rec_mm_len

- rec_mm_prep

- rec_profile_ins

- rec_profile_need

- rec_profile_ret

- rec_setup_root

- rec_tnew

- rec_tsetm

- rec_upvalue

- rec_upvalue_constify

- rec_varg

- select_detect

- sload

- sloadt

Macros defined

- IR

- IR

- LUA_CORE

- emitir

- emitir

- emitir_raw

- emitir_raw

- getslot

- lj_record_c

- rav

- rav

- rbv

- rbv

- rcv

- rcv

Source code

1. /*

2. ** Trace recorder (bytecode -> SSA IR).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_record_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT

13. #include "lj_err.h"

14. #include "lj_str.h"

15. #include "lj_tab.h"

16. #include "lj_meta.h"

17. #include "lj_frame.h"

18. #if LJ_HASFFI

19. #include "lj_ctype.h"

20. #endif

21. #include "lj_bc.h"

22. #include "lj_ff.h"

23. #if LJ_HASPROFILE

24. #include "lj_debug.h"

25. #endif

26. #include "lj_ir.h"

27. #include "lj_jit.h"

28. #include "lj_ircall.h"

29. #include "lj_iropt.h"

30. #include "lj_trace.h"

31. #include "lj_record.h"

32. #include "lj_ffrecord.h"

33. #include "lj_snap.h"

34. #include "lj_dispatch.h"

35. #include "lj_vm.h"

37. /* Some local macros to save typing. Undef'd at the end. */

38. ‌#define IR(ref)                        (&J->cur.ir[(ref)])

40. /* Pass IR on to next optimization in chain (FOLD). */

41. ‌#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))

43. /* Emit raw IR without passing through optimizations. */

44. ‌#define emitir_raw(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J))

46. /* -- Sanity checks ------------------------------------------------------- */

48. #ifdef LUA_USE_ASSERT

49. /* Sanity check the whole IR -- sloooow. */

50. ‌static void rec_check_ir(jit_State *J)

51. {

52. IRRef i, nins = J->cur.nins, nk = J->cur.nk;

53. lua_assert(nk <= REF_BIAS && nins >= REF_BIAS && nins < 65536);

54. for (i = nins-1; i >= nk; i--) {

55. IRIns *ir = IR(i);

56. uint32_t mode = lj_ir_mode[ir->o];

57. IRRef op1 = ir->op1;

58. IRRef op2 = ir->op2;

59. switch (irm_op1(mode)) {

60. case IRMnone: lua_assert(op1 == 0); break;

61. case IRMref: lua_assert(op1 >= nk);

62. lua_assert(i >= REF_BIAS ? op1 < i : op1 > i); break;

63. case IRMlit: break;

64. case IRMcst: lua_assert(i < REF_BIAS); continue;

65. }

66. switch (irm_op2(mode)) {

67. case IRMnone: lua_assert(op2 == 0); break;

68. case IRMref: lua_assert(op2 >= nk);

69. lua_assert(i >= REF_BIAS ? op2 < i : op2 > i); break;

70. case IRMlit: break;

71. case IRMcst: lua_assert(0); break;

72. }

73. if (ir->prev) {

74. lua_assert(ir->prev >= nk);

75. lua_assert(i >= REF_BIAS ? ir->prev < i : ir->prev > i);

76. lua_assert(ir->o == IR_NOP || IR(ir->prev)->o == ir->o);

77. }

78. }

79. }

81. /* Compare stack slots and frames of the recorder and the VM. */

82. ‌static void rec_check_slots(jit_State *J)

83. {

84. BCReg s, nslots = J->baseslot + J->maxslot;

85. int32_t depth = 0;

86. cTValue *base = J->L->base - J->baseslot;

87. lua_assert(J->baseslot >= 1 && J->baseslot < LJ_MAX_JSLOTS);

88. lua_assert(J->baseslot == 1 || (J->slot[J->baseslot-1] & TREF_FRAME));

89. lua_assert(nslots < LJ_MAX_JSLOTS);

90. for (s = 0; s < nslots; s++) {

91. TRef tr = J->slot[s];

92. if (tr) {

93. cTValue *tv = &base[s];

94. IRRef ref = tref_ref(tr);

95. IRIns *ir;

96. lua_assert(ref >= J->cur.nk && ref < J->cur.nins);

97. ir = IR(ref);

98. lua_assert(irt_t(ir->t) == tref_t(tr));

99. if (s == 0) {

100. lua_assert(tref_isfunc(tr));

101. } else if ((tr & TREF_FRAME)) {

102. GCfunc *fn = gco2func(frame_gc(tv));

103. BCReg delta = (BCReg)(tv - frame_prev(tv));

104. lua_assert(tref_isfunc(tr));

105. if (tref_isk(tr)) lua_assert(fn == ir_kfunc(ir));

106. lua_assert(s > delta ? (J->slot[s-delta] & TREF_FRAME) : (s == delta));

107. depth++;

108. } else if ((tr & TREF_CONT)) {

109. lua_assert(ir_kptr(ir) == gcrefp(tv->gcr, void));

110. lua_assert((J->slot[s+1] & TREF_FRAME));

111. depth++;

112. } else {

113. if (tvisnumber(tv))

114. lua_assert(tref_isnumber(tr));  /* Could be IRT_INT etc., too. */

115. else

116. lua_assert(itype2irt(tv) == tref_type(tr));

117. if (tref_isk(tr)) {  /* Compare constants. */

118. TValue tvk;

119. lj_ir_kvalue(J->L, &tvk, ir);

120. if (!(tvisnum(&tvk) && tvisnan(&tvk)))

121. lua_assert(lj_obj_equal(tv, &tvk));

122. else

123. lua_assert(tvisnum(tv) && tvisnan(tv));

124. }

125. }

126. }

127. }

128. lua_assert(J->framedepth == depth);

129. }

130. #endif

132. /* -- Type handling and specialization ------------------------------------ */

134. /* Note: these functions return tagged references (TRef). */

136. /* Specialize a slot to a specific type. Note: slot can be negative! */

137. ‌static TRef sloadt(jit_State *J, int32_t slot, IRType t, int mode)

138. {

139. /* Caller may set IRT_GUARD in t. */

140. TRef ref = emitir_raw(IRT(IR_SLOAD, t), (int32_t)J->baseslot+slot, mode);

141. J->base[slot] = ref;

142. return ref;

143. }

145. /* Specialize a slot to the runtime type. Note: slot can be negative! */

146. ‌static TRef sload(jit_State *J, int32_t slot)

147. {

148. IRType t = itype2irt(&J->L->base[slot]);

149. TRef ref = emitir_raw(IRTG(IR_SLOAD, t), (int32_t)J->baseslot+slot,

150. IRSLOAD_TYPECHECK);

151. if (irtype_ispri(t)) ref = TREF_PRI(t);  /* Canonicalize primitive refs. */

152. J->base[slot] = ref;

153. return ref;

154. }

156. /* Get TRef from slot. Load slot and specialize if not done already. */

157. ‌#define getslot(J, s)        (J->base[(s)] ? J->base[(s)] : sload(J, (int32_t)(s)))

159. /* Get TRef for current function. */

160. ‌static TRef getcurrf(jit_State *J)

161. {

162. if (J->base[-1])

163. return J->base[-1];

164. lua_assert(J->baseslot == 1);

165. return sloadt(J, -1, IRT_FUNC, IRSLOAD_READONLY);

166. }

168. /* Compare for raw object equality.

169. ** Returns 0 if the objects are the same.

170. ** Returns 1 if they are different, but the same type.

171. ** Returns 2 for two different types.

172. ** Comparisons between primitives always return 1 -- no caller cares about it.

173. */

174. ‌int lj_record_objcmp(jit_State *J, TRef a, TRef b, cTValue *av, cTValue *bv)

175. {

176. int diff = !lj_obj_equal(av, bv);

177. if (!tref_isk2(a, b)) {  /* Shortcut, also handles primitives. */

178. IRType ta = tref_isinteger(a) ? IRT_INT : tref_type(a);

179. IRType tb = tref_isinteger(b) ? IRT_INT : tref_type(b);

180. if (ta != tb) {

181. /* Widen mixed number/int comparisons to number/number comparison. */

182. if (ta == IRT_INT && tb == IRT_NUM) {

183. a = emitir(IRTN(IR_CONV), a, IRCONV_NUM_INT);

184. ta = IRT_NUM;

185. } else if (ta == IRT_NUM && tb == IRT_INT) {

186. b = emitir(IRTN(IR_CONV), b, IRCONV_NUM_INT);

187. } else {

188. return 2;  /* Two different types are never equal. */

189. }

190. }

191. emitir(IRTG(diff ? IR_NE : IR_EQ, ta), a, b);

192. }

193. return diff;

194. }

196. /* Constify a value. Returns 0 for non-representable object types. */

197. ‌TRef lj_record_constify(jit_State *J, cTValue *o)

198. {

199. if (tvisgcv(o))

200. return lj_ir_kgc(J, gcV(o), itype2irt(o));

201. else if (tvisint(o))

202. return lj_ir_kint(J, intV(o));

203. else if (tvisnum(o))

204. return lj_ir_knumint(J, numV(o));

205. else if (tvisbool(o))

206. return TREF_PRI(itype2irt(o));

207. else

208. return 0;  /* Can't represent lightuserdata (pointless). */

209. }

211. /* -- Record loop ops ----------------------------------------------------- */

213. /* Loop event. */

214. typedef enum {

215. LOOPEV_LEAVE,                /* Loop is left or not entered. */

216. LOOPEV_ENTERLO,        /* Loop is entered with a low iteration count left. */

217. LOOPEV_ENTER                /* Loop is entered. */

218. ‌} LoopEvent;

220. /* Canonicalize slots: convert integers to numbers. */

221. ‌static void canonicalize_slots(jit_State *J)

222. {

223. BCReg s;

224. if (LJ_DUALNUM) return;

225. for (s = J->baseslot+J->maxslot-1; s >= 1; s--) {

226. TRef tr = J->slot[s];

227. if (tref_isinteger(tr)) {

228. IRIns *ir = IR(tref_ref(tr));

229. if (!(ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_READONLY)))

230. J->slot[s] = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT);

231. }

232. }

233. }

235. /* Stop recording. */

236. ‌void lj_record_stop(jit_State *J, TraceLink linktype, TraceNo lnk)

237. {

238. lj_trace_end(J);

239. J->cur.linktype = (uint8_t)linktype;

240. J->cur.link = (uint16_t)lnk;

241. /* Looping back at the same stack level? */

242. if (lnk == J->cur.traceno && J->framedepth + J->retdepth == 0) {

243. if ((J->flags & JIT_F_OPT_LOOP))  /* Shall we try to create a loop? */

244. goto nocanon;  /* Do not canonicalize or we lose the narrowing. */

245. if (J->cur.root)  /* Otherwise ensure we always link to the root trace. */

246. J->cur.link = J->cur.root;

247. }

248. canonicalize_slots(J);

249. nocanon:

250. /* Note: all loop ops must set J->pc to the following instruction! */

251. lj_snap_add(J);  /* Add loop snapshot. */

252. J->needsnap = 0;

253. J->mergesnap = 1;  /* In case recording continues. */

254. }

256. /* Search bytecode backwards for a int/num constant slot initializer. */

257. ‌static TRef find_kinit(jit_State *J, const BCIns *endpc, BCReg slot, IRType t)

258. {

259. /* This algorithm is rather simplistic and assumes quite a bit about

260. ** how the bytecode is generated. It works fine for FORI initializers,

261. ** but it won't necessarily work in other cases (e.g. iterator arguments).

262. ** It doesn't do anything fancy, either (like backpropagating MOVs).

263. */

264. const BCIns *pc, *startpc = proto_bc(J->pt);

265. for (pc = endpc-1; pc > startpc; pc--) {

266. BCIns ins = *pc;

267. BCOp op = bc_op(ins);

268. /* First try to find the last instruction that stores to this slot. */

269. if (bcmode_a(op) == BCMbase && bc_a(ins) <= slot) {

270. return 0;  /* Multiple results, e.g. from a CALL or KNIL. */

271. } else if (bcmode_a(op) == BCMdst && bc_a(ins) == slot) {

272. if (op == BC_KSHORT || op == BC_KNUM) {  /* Found const. initializer. */

273. /* Now try to verify there's no forward jump across it. */

274. const BCIns *kpc = pc;

275. for (; pc > startpc; pc--)

276. if (bc_op(*pc) == BC_JMP) {

277. const BCIns *target = pc+bc_j(*pc)+1;

278. if (target > kpc && target <= endpc)

279. return 0;  /* Conditional assignment. */

280. }

281. if (op == BC_KSHORT) {

282. int32_t k = (int32_t)(int16_t)bc_d(ins);

283. return t == IRT_INT ? lj_ir_kint(J, k) : lj_ir_knum(J, (lua_Number)k);

284. } else {

285. cTValue *tv = proto_knumtv(J->pt, bc_d(ins));

286. if (t == IRT_INT) {

287. int32_t k = numberVint(tv);

288. if (tvisint(tv) || numV(tv) == (lua_Number)k)  /* -0 is ok here. */

289. return lj_ir_kint(J, k);

290. return 0;  /* Type mismatch. */

291. } else {

292. return lj_ir_knum(J, numberVnum(tv));

293. }

294. }

295. }

296. return 0;  /* Non-constant initializer. */

297. }

298. }

299. return 0;  /* No assignment to this slot found? */

300. }

302. /* Load and optionally convert a FORI argument from a slot. */

303. ‌static TRef fori_load(jit_State *J, BCReg slot, IRType t, int mode)

304. {

305. int conv = (tvisint(&J->L->base[slot]) != (t==IRT_INT)) ? IRSLOAD_CONVERT : 0;

306. return sloadt(J, (int32_t)slot,

307. t + (((mode & IRSLOAD_TYPECHECK) ||

308. (conv && t == IRT_INT && !(mode >> 16))) ?

309. IRT_GUARD : 0),

310. mode + conv);

311. }

313. /* Peek before FORI to find a const initializer. Otherwise load from slot. */

314. ‌static TRef fori_arg(jit_State *J, const BCIns *fori, BCReg slot,

315. IRType t, int mode)

316. {

317. TRef tr = J->base[slot];

318. if (!tr) {

319. tr = find_kinit(J, fori, slot, t);

320. if (!tr)

321. tr = fori_load(J, slot, t, mode);

322. }

323. return tr;

324. }

326. /* Return the direction of the FOR loop iterator.

327. ** It's important to exactly reproduce the semantics of the interpreter.

328. */

329. ‌static int rec_for_direction(cTValue *o)

330. {

331. return (tvisint(o) ? intV(o) : (int32_t)o->u32.hi) >= 0;

332. }

334. /* Simulate the runtime behavior of the FOR loop iterator. */

335. ‌static LoopEvent rec_for_iter(IROp *op, cTValue *o, int isforl)

336. {

337. lua_Number stopv = numberVnum(&o[FORL_STOP]);

338. lua_Number idxv = numberVnum(&o[FORL_IDX]);

339. lua_Number stepv = numberVnum(&o[FORL_STEP]);

340. if (isforl)

341. idxv += stepv;

342. if (rec_for_direction(&o[FORL_STEP])) {

343. if (idxv <= stopv) {

344. *op = IR_LE;

345. return idxv + 2*stepv > stopv ? LOOPEV_ENTERLO : LOOPEV_ENTER;

346. }

347. *op = IR_GT; return LOOPEV_LEAVE;

348. } else {

349. if (stopv <= idxv) {

350. *op = IR_GE;

351. return idxv + 2*stepv < stopv ? LOOPEV_ENTERLO : LOOPEV_ENTER;

352. }

353. *op = IR_LT; return LOOPEV_LEAVE;

354. }

355. }

357. /* Record checks for FOR loop overflow and step direction. */

358. ‌static void rec_for_check(jit_State *J, IRType t, int dir,

359. TRef stop, TRef step, int init)

360. {

361. if (!tref_isk(step)) {

362. /* Non-constant step: need a guard for the direction. */

363. TRef zero = (t == IRT_INT) ? lj_ir_kint(J, 0) : lj_ir_knum_zero(J);

364. emitir(IRTG(dir ? IR_GE : IR_LT, t), step, zero);

365. /* Add hoistable overflow checks for a narrowed FORL index. */

366. if (init && t == IRT_INT) {

367. if (tref_isk(stop)) {

368. /* Constant stop: optimize check away or to a range check for step. */

369. int32_t k = IR(tref_ref(stop))->i;

370. if (dir) {

371. if (k > 0)

372. emitir(IRTGI(IR_LE), step, lj_ir_kint(J, (int32_t)0x7fffffff-k));

373. } else {

374. if (k < 0)

375. emitir(IRTGI(IR_GE), step, lj_ir_kint(J, (int32_t)0x80000000-k));

376. }

377. } else {

378. /* Stop+step variable: need full overflow check. */

379. TRef tr = emitir(IRTGI(IR_ADDOV), step, stop);

380. emitir(IRTI(IR_USE), tr, 0);  /* ADDOV is weak. Avoid dead result. */

381. }

382. }

383. } else if (init && t == IRT_INT && !tref_isk(stop)) {

384. /* Constant step: optimize overflow check to a range check for stop. */

385. int32_t k = IR(tref_ref(step))->i;

386. k = (int32_t)(dir ? 0x7fffffff : 0x80000000) - k;

387. emitir(IRTGI(dir ? IR_LE : IR_GE), stop, lj_ir_kint(J, k));

388. }

389. }

391. /* Record a FORL instruction. */

392. ‌static void rec_for_loop(jit_State *J, const BCIns *fori, ScEvEntry *scev,

393. int init)

394. {

395. BCReg ra = bc_a(*fori);

396. cTValue *tv = &J->L->base[ra];

397. TRef idx = J->base[ra+FORL_IDX];

398. IRType t = idx ? tref_type(idx) :

399. (init || LJ_DUALNUM) ? lj_opt_narrow_forl(J, tv) : IRT_NUM;

400. int mode = IRSLOAD_INHERIT +

401. ((!LJ_DUALNUM || tvisint(tv) == (t == IRT_INT)) ? IRSLOAD_READONLY : 0);

402. TRef stop = fori_arg(J, fori, ra+FORL_STOP, t, mode);

403. TRef step = fori_arg(J, fori, ra+FORL_STEP, t, mode);

404. int tc, dir = rec_for_direction(&tv[FORL_STEP]);

405. lua_assert(bc_op(*fori) == BC_FORI || bc_op(*fori) == BC_JFORI);

406. scev->t.irt = t;

407. scev->dir = dir;

408. scev->stop = tref_ref(stop);

409. scev->step = tref_ref(step);

410. rec_for_check(J, t, dir, stop, step, init);

411. scev->start = tref_ref(find_kinit(J, fori, ra+FORL_IDX, IRT_INT));

412. tc = (LJ_DUALNUM &&

413. !(scev->start && irref_isk(scev->stop) && irref_isk(scev->step) &&

414. tvisint(&tv[FORL_IDX]) == (t == IRT_INT))) ?

415. IRSLOAD_TYPECHECK : 0;

416. if (tc) {

417. J->base[ra+FORL_STOP] = stop;

418. J->base[ra+FORL_STEP] = step;

419. }

420. if (!idx)

421. idx = fori_load(J, ra+FORL_IDX, t,

422. IRSLOAD_INHERIT + tc + (J->scev.start << 16));

423. if (!init)

424. J->base[ra+FORL_IDX] = idx = emitir(IRT(IR_ADD, t), idx, step);

425. J->base[ra+FORL_EXT] = idx;

426. scev->idx = tref_ref(idx);

427. setmref(scev->pc, fori);

428. J->maxslot = ra+FORL_EXT+1;

429. }

431. /* Record FORL/JFORL or FORI/JFORI. */

432. ‌static LoopEvent rec_for(jit_State *J, const BCIns *fori, int isforl)

433. {

434. BCReg ra = bc_a(*fori);

435. TValue *tv = &J->L->base[ra];

436. TRef *tr = &J->base[ra];

437. IROp op;

438. LoopEvent ev;

439. TRef stop;

440. IRType t;

441. if (isforl) {  /* Handle FORL/JFORL opcodes. */

442. TRef idx = tr[FORL_IDX];

443. if (mref(J->scev.pc, const BCIns) == fori && tref_ref(idx) == J->scev.idx) {

444. t = J->scev.t.irt;

445. stop = J->scev.stop;

446. idx = emitir(IRT(IR_ADD, t), idx, J->scev.step);

447. tr[FORL_EXT] = tr[FORL_IDX] = idx;

448. } else {

449. ScEvEntry scev;

450. rec_for_loop(J, fori, &scev, 0);

451. t = scev.t.irt;

452. stop = scev.stop;

453. }

454. } else {  /* Handle FORI/JFORI opcodes. */

455. BCReg i;

456. lj_meta_for(J->L, tv);

457. t = (LJ_DUALNUM || tref_isint(tr[FORL_IDX])) ? lj_opt_narrow_forl(J, tv) :

458. IRT_NUM;

459. for (i = FORL_IDX; i <= FORL_STEP; i++) {

460. if (!tr[i]) sload(J, ra+i);

461. lua_assert(tref_isnumber_str(tr[i]));

462. if (tref_isstr(tr[i]))

463. tr[i] = emitir(IRTG(IR_STRTO, IRT_NUM), tr[i], 0);

464. if (t == IRT_INT) {

465. if (!tref_isinteger(tr[i]))

466. tr[i] = emitir(IRTGI(IR_CONV), tr[i], IRCONV_INT_NUM|IRCONV_CHECK);

467. } else {

468. if (!tref_isnum(tr[i]))

469. tr[i] = emitir(IRTN(IR_CONV), tr[i], IRCONV_NUM_INT);

470. }

471. }

472. tr[FORL_EXT] = tr[FORL_IDX];

473. stop = tr[FORL_STOP];

474. rec_for_check(J, t, rec_for_direction(&tv[FORL_STEP]),

475. stop, tr[FORL_STEP], 1);

476. }

478. ev = rec_for_iter(&op, tv, isforl);

479. if (ev == LOOPEV_LEAVE) {

480. J->maxslot = ra+FORL_EXT+1;

481. J->pc = fori+1;

482. } else {

483. J->maxslot = ra;

484. J->pc = fori+bc_j(*fori)+1;

485. }

486. lj_snap_add(J);

488. emitir(IRTG(op, t), tr[FORL_IDX], stop);

490. if (ev == LOOPEV_LEAVE) {

491. J->maxslot = ra;

492. J->pc = fori+bc_j(*fori)+1;

493. } else {

494. J->maxslot = ra+FORL_EXT+1;

495. J->pc = fori+1;

496. }

497. J->needsnap = 1;

498. return ev;

499. }

501. /* Record ITERL/JITERL. */

502. ‌static LoopEvent rec_iterl(jit_State *J, const BCIns iterins)

503. {

504. BCReg ra = bc_a(iterins);

505. lua_assert(!LJ_FR2);  /* TODO_FR2: handle different frame setup. */

506. if (!tref_isnil(getslot(J, ra))) {  /* Looping back? */

507. J->base[ra-1] = J->base[ra];  /* Copy result of ITERC to control var. */

508. J->maxslot = ra-1+bc_b(J->pc[-1]);

509. J->pc += bc_j(iterins)+1;

510. return LOOPEV_ENTER;

511. } else {

512. J->maxslot = ra-3;

513. J->pc++;

514. return LOOPEV_LEAVE;

515. }

516. }

518. /* Record LOOP/JLOOP. Now, that was easy. */

519. ‌static LoopEvent rec_loop(jit_State *J, BCReg ra)

520. {

521. if (ra < J->maxslot) J->maxslot = ra;

522. J->pc++;

523. return LOOPEV_ENTER;

524. }

526. /* Check if a loop repeatedly failed to trace because it didn't loop back. */

527. ‌static int innerloopleft(jit_State *J, const BCIns *pc)

528. {

529. ptrdiff_t i;

530. for (i = 0; i < PENALTY_SLOTS; i++)

531. if (mref(J->penalty[i].pc, const BCIns) == pc) {

532. if ((J->penalty[i].reason == LJ_TRERR_LLEAVE ||

533. J->penalty[i].reason == LJ_TRERR_LINNER) &&

534. J->penalty[i].val >= 2*PENALTY_MIN)

535. return 1;

536. break;

537. }

538. return 0;

539. }

541. /* Handle the case when an interpreted loop op is hit. */

542. ‌static void rec_loop_interp(jit_State *J, const BCIns *pc, LoopEvent ev)

543. {

544. if (J->parent == 0 && J->exitno == 0) {

545. if (pc == J->startpc && J->framedepth + J->retdepth == 0) {

546. /* Same loop? */

547. if (ev == LOOPEV_LEAVE)  /* Must loop back to form a root trace. */

548. lj_trace_err(J, LJ_TRERR_LLEAVE);

549. lj_record_stop(J, LJ_TRLINK_LOOP, J->cur.traceno);  /* Looping trace. */

550. } else if (ev != LOOPEV_LEAVE) {  /* Entering inner loop? */

551. /* It's usually better to abort here and wait until the inner loop

552. ** is traced. But if the inner loop repeatedly didn't loop back,

553. ** this indicates a low trip count. In this case try unrolling

554. ** an inner loop even in a root trace. But it's better to be a bit

555. ** more conservative here and only do it for very short loops.

556. */

557. if (bc_j(*pc) != -1 && !innerloopleft(J, pc))

558. lj_trace_err(J, LJ_TRERR_LINNER);  /* Root trace hit an inner loop. */

559. if ((ev != LOOPEV_ENTERLO &&

560. J->loopref && J->cur.nins - J->loopref > 24) || --J->loopunroll < 0)

561. lj_trace_err(J, LJ_TRERR_LUNROLL);  /* Limit loop unrolling. */

562. J->loopref = J->cur.nins;

563. }

564. } else if (ev != LOOPEV_LEAVE) {  /* Side trace enters an inner loop. */

565. J->loopref = J->cur.nins;

566. if (--J->loopunroll < 0)

567. lj_trace_err(J, LJ_TRERR_LUNROLL);  /* Limit loop unrolling. */

568. }  /* Side trace continues across a loop that's left or not entered. */

569. }

571. /* Handle the case when an already compiled loop op is hit. */

572. ‌static void rec_loop_jit(jit_State *J, TraceNo lnk, LoopEvent ev)

573. {

574. if (J->parent == 0 && J->exitno == 0) {  /* Root trace hit an inner loop. */

575. /* Better let the inner loop spawn a side trace back here. */

576. lj_trace_err(J, LJ_TRERR_LINNER);

577. } else if (ev != LOOPEV_LEAVE) {  /* Side trace enters a compiled loop. */

578. J->instunroll = 0;  /* Cannot continue across a compiled loop op. */

579. if (J->pc == J->startpc && J->framedepth + J->retdepth == 0)

580. lj_record_stop(J, LJ_TRLINK_LOOP, J->cur.traceno);  /* Form extra loop. */

581. else

582. lj_record_stop(J, LJ_TRLINK_ROOT, lnk);  /* Link to the loop. */

583. }  /* Side trace continues across a loop that's left or not entered. */

584. }

586. /* -- Record profiler hook checks ----------------------------------------- */

588. #if LJ_HASPROFILE

590. /* Need to insert profiler hook check? */

591. ‌static int rec_profile_need(jit_State *J, GCproto *pt, const BCIns *pc)

592. {

593. GCproto *ppt;

594. lua_assert(J->prof_mode == 'f' || J->prof_mode == 'l');

595. if (!pt)

596. return 0;

597. ppt = J->prev_pt;

598. J->prev_pt = pt;

599. if (pt != ppt && ppt) {

600. J->prev_line = -1;

601. return 1;

602. }

603. if (J->prof_mode == 'l') {

604. BCLine line = lj_debug_line(pt, proto_bcpos(pt, pc));

605. BCLine pline = J->prev_line;

606. J->prev_line = line;

607. if (pline != line)

608. return 1;

609. }

610. return 0;

611. }

613. ‌static void rec_profile_ins(jit_State *J, const BCIns *pc)

614. {

615. if (J->prof_mode && rec_profile_need(J, J->pt, pc)) {

616. emitir(IRTG(IR_PROF, IRT_NIL), 0, 0);

617. lj_snap_add(J);

618. }

619. }

621. ‌static void rec_profile_ret(jit_State *J)

622. {

623. if (J->prof_mode == 'f') {

624. emitir(IRTG(IR_PROF, IRT_NIL), 0, 0);

625. J->prev_pt = NULL;

626. lj_snap_add(J);

627. }

628. }

630. #endif

632. /* -- Record calls and returns -------------------------------------------- */

634. /* Specialize to the runtime value of the called function or its prototype. */

635. ‌static TRef rec_call_specialize(jit_State *J, GCfunc *fn, TRef tr)

636. {

637. TRef kfunc;

638. if (isluafunc(fn)) {

639. GCproto *pt = funcproto(fn);

640. /* Too many closures created? Probably not a monomorphic function. */

641. if (pt->flags >= PROTO_CLC_POLY) {  /* Specialize to prototype instead. */

642. TRef trpt = emitir(IRT(IR_FLOAD, IRT_P32), tr, IRFL_FUNC_PC);

643. emitir(IRTG(IR_EQ, IRT_P32), trpt, lj_ir_kptr(J, proto_bc(pt)));

644. (void)lj_ir_kgc(J, obj2gco(pt), IRT_PROTO);  /* Prevent GC of proto. */

645. return tr;

646. }

647. } else {

648. /* Don't specialize to non-monomorphic builtins. */

649. switch (fn->c.ffid) {

650. case FF_coroutine_wrap_aux:

651. case FF_string_gmatch_aux:

652. /* NYI: io_file_iter doesn't have an ffid, yet. */

653. {  /* Specialize to the ffid. */

654. TRef trid = emitir(IRT(IR_FLOAD, IRT_U8), tr, IRFL_FUNC_FFID);

655. emitir(IRTG(IR_EQ, IRT_INT), trid, lj_ir_kint(J, fn->c.ffid));

656. }

657. return tr;

658. default:

659. /* NYI: don't specialize to non-monomorphic C functions. */

660. break;

661. }

662. }

663. /* Otherwise specialize to the function (closure) value itself. */

664. kfunc = lj_ir_kfunc(J, fn);

665. emitir(IRTG(IR_EQ, IRT_FUNC), tr, kfunc);

666. return kfunc;

667. }

669. /* Record call setup. */

670. ‌static void rec_call_setup(jit_State *J, BCReg func, ptrdiff_t nargs)

671. {

672. RecordIndex ix;

673. TValue *functv = &J->L->base[func];

674. TRef *fbase = &J->base[func];

675. ptrdiff_t i;

676. lua_assert(!LJ_FR2);  /* TODO_FR2: handle different frame setup. */

677. for (i = 0; i <= nargs; i++)

678. (void)getslot(J, func+i);  /* Ensure func and all args have a reference. */

679. if (!tref_isfunc(fbase[0])) {  /* Resolve __call metamethod. */

680. ix.tab = fbase[0];

681. copyTV(J->L, &ix.tabv, functv);

682. if (!lj_record_mm_lookup(J, &ix, MM_call) || !tref_isfunc(ix.mobj))

683. lj_trace_err(J, LJ_TRERR_NOMM);

684. for (i = ++nargs; i > 0; i--)  /* Shift arguments up. */

685. fbase[i] = fbase[i-1];

686. fbase[0] = ix.mobj;  /* Replace function. */

687. functv = &ix.mobjv;

688. }

689. fbase[0] = TREF_FRAME | rec_call_specialize(J, funcV(functv), fbase[0]);

690. J->maxslot = (BCReg)nargs;

691. }

693. /* Record call. */

694. ‌void lj_record_call(jit_State *J, BCReg func, ptrdiff_t nargs)

695. {

696. rec_call_setup(J, func, nargs);

697. /* Bump frame. */

698. J->framedepth++;

699. J->base += func+1;

700. J->baseslot += func+1;

701. }

703. /* Record tail call. */

704. ‌void lj_record_tailcall(jit_State *J, BCReg func, ptrdiff_t nargs)

705. {

706. rec_call_setup(J, func, nargs);

707. if (frame_isvarg(J->L->base - 1)) {

708. BCReg cbase = (BCReg)frame_delta(J->L->base - 1);

709. if (--J->framedepth < 0)

710. lj_trace_err(J, LJ_TRERR_NYIRETL);

711. J->baseslot -= (BCReg)cbase;

712. J->base -= cbase;

713. func += cbase;

714. }

715. /* Move func + args down. */

716. memmove(&J->base[-1], &J->base[func], sizeof(TRef)*(J->maxslot+1));

717. /* Note: the new TREF_FRAME is now at J->base[-1] (even for slot #0). */

718. /* Tailcalls can form a loop, so count towards the loop unroll limit. */

719. if (++J->tailcalled > J->loopunroll)

720. lj_trace_err(J, LJ_TRERR_LUNROLL);

721. }

723. /* Check unroll limits for down-recursion. */

724. ‌static int check_downrec_unroll(jit_State *J, GCproto *pt)

725. {

726. IRRef ptref;

727. for (ptref = J->chain[IR_KGC]; ptref; ptref = IR(ptref)->prev)

728. if (ir_kgc(IR(ptref)) == obj2gco(pt)) {

729. int count = 0;

730. IRRef ref;

731. for (ref = J->chain[IR_RETF]; ref; ref = IR(ref)->prev)

732. if (IR(ref)->op1 == ptref)

733. count++;

734. if (count) {

735. if (J->pc == J->startpc) {

736. if (count + J->tailcalled > J->param[JIT_P_recunroll])

737. return 1;

738. } else {

739. lj_trace_err(J, LJ_TRERR_DOWNREC);

740. }

741. }

742. }

743. return 0;

744. }

746. static TRef rec_cat(jit_State *J, BCReg baseslot, BCReg topslot);

748. /* Record return. */

749. ‌void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults)

750. {

751. TValue *frame = J->L->base - 1;

752. ptrdiff_t i;

753. for (i = 0; i < gotresults; i++)

754. (void)getslot(J, rbase+i);  /* Ensure all results have a reference. */

755. while (frame_ispcall(frame)) {  /* Immediately resolve pcall() returns. */

756. BCReg cbase = (BCReg)frame_delta(frame);

757. if (--J->framedepth < 0)

758. lj_trace_err(J, LJ_TRERR_NYIRETL);

759. lua_assert(J->baseslot > 1);

760. gotresults++;

761. rbase += cbase;

762. J->baseslot -= (BCReg)cbase;

763. J->base -= cbase;

764. J->base[--rbase] = TREF_TRUE;  /* Prepend true to results. */

765. frame = frame_prevd(frame);

766. }

767. /* Return to lower frame via interpreter for unhandled cases. */

768. if (J->framedepth == 0 && J->pt && bc_isret(bc_op(*J->pc)) &&

769. (!frame_islua(frame) ||

770. (J->parent == 0 && J->exitno == 0 &&

771. !bc_isret(bc_op(J->cur.startins))))) {

772. /* NYI: specialize to frame type and return directly, not via RET*. */

773. for (i = 0; i < (ptrdiff_t)rbase; i++)

774. J->base[i] = 0;  /* Purge dead slots. */

775. J->maxslot = rbase + (BCReg)gotresults;

776. lj_record_stop(J, LJ_TRLINK_RETURN, 0);  /* Return to interpreter. */

777. return;

778. }

779. if (frame_isvarg(frame)) {

780. BCReg cbase = (BCReg)frame_delta(frame);

781. if (--J->framedepth < 0)  /* NYI: return of vararg func to lower frame. */

782. lj_trace_err(J, LJ_TRERR_NYIRETL);

783. lua_assert(J->baseslot > 1);

784. rbase += cbase;

785. J->baseslot -= (BCReg)cbase;

786. J->base -= cbase;

787. frame = frame_prevd(frame);

788. }

789. if (frame_islua(frame)) {  /* Return to Lua frame. */

790. BCIns callins = *(frame_pc(frame)-1);

791. ptrdiff_t nresults = bc_b(callins) ? (ptrdiff_t)bc_b(callins)-1 :gotresults;

792. BCReg cbase = bc_a(callins);

793. GCproto *pt = funcproto(frame_func(frame - (cbase+1-LJ_FR2)));

794. lua_assert(!LJ_FR2);  /* TODO_FR2: handle different frame teardown. */

795. if ((pt->flags & PROTO_NOJIT))

796. lj_trace_err(J, LJ_TRERR_CJITOFF);

797. if (J->framedepth == 0 && J->pt && frame == J->L->base - 1) {

798. if (check_downrec_unroll(J, pt)) {

799. J->maxslot = (BCReg)(rbase + gotresults);

800. lj_snap_purge(J);

801. lj_record_stop(J, LJ_TRLINK_DOWNREC, J->cur.traceno);  /* Down-rec. */

802. return;

803. }

804. lj_snap_add(J);

805. }

806. for (i = 0; i < nresults; i++)  /* Adjust results. */

807. J->base[i-1] = i < gotresults ? J->base[rbase+i] : TREF_NIL;

808. J->maxslot = cbase+(BCReg)nresults;

809. if (J->framedepth > 0) {  /* Return to a frame that is part of the trace. */

810. J->framedepth--;

811. lua_assert(J->baseslot > cbase+1);

812. J->baseslot -= cbase+1;

813. J->base -= cbase+1;

814. } else if (J->parent == 0 && J->exitno == 0 &&

815. !bc_isret(bc_op(J->cur.startins))) {

816. /* Return to lower frame would leave the loop in a root trace. */

817. lj_trace_err(J, LJ_TRERR_LLEAVE);

818. } else if (J->needsnap) {  /* Tailcalled to ff with side-effects. */

819. lj_trace_err(J, LJ_TRERR_NYIRETL);  /* No way to insert snapshot here. */

820. } else {  /* Return to lower frame. Guard for the target we return to. */

821. TRef trpt = lj_ir_kgc(J, obj2gco(pt), IRT_PROTO);

822. TRef trpc = lj_ir_kptr(J, (void *)frame_pc(frame));

823. emitir(IRTG(IR_RETF, IRT_P32), trpt, trpc);

824. J->retdepth++;

825. J->needsnap = 1;

826. lua_assert(J->baseslot == 1);

827. /* Shift result slots up and clear the slots of the new frame below. */

828. memmove(J->base + cbase, J->base-1, sizeof(TRef)*nresults);

829. memset(J->base-1, 0, sizeof(TRef)*(cbase+1));

830. }

831. } else if (frame_iscont(frame)) {  /* Return to continuation frame. */

832. ASMFunction cont = frame_contf(frame);

833. BCReg cbase = (BCReg)frame_delta(frame);

834. if ((J->framedepth -= 2) < 0)

835. lj_trace_err(J, LJ_TRERR_NYIRETL);

836. J->baseslot -= (BCReg)cbase;

837. J->base -= cbase;

838. J->maxslot = cbase-2;

839. if (cont == lj_cont_ra) {

840. /* Copy result to destination slot. */

841. BCReg dst = bc_a(*(frame_contpc(frame)-1));

842. J->base[dst] = gotresults ? J->base[cbase+rbase] : TREF_NIL;

843. if (dst >= J->maxslot) J->maxslot = dst+1;

844. } else if (cont == lj_cont_nop) {

845. /* Nothing to do here. */

846. } else if (cont == lj_cont_cat) {

847. BCReg bslot = bc_b(*(frame_contpc(frame)-1));

848. TRef tr = gotresults ? J->base[cbase+rbase] : TREF_NIL;

849. if (bslot != cbase-2) {  /* Concatenate the remainder. */

850. TValue *b = J->L->base, save;  /* Simulate lower frame and result. */

851. J->base[cbase-2] = tr;

852. copyTV(J->L, &save, b-2);

853. if (gotresults) copyTV(J->L, b-2, b+rbase); else setnilV(b-2);

854. J->L->base = b - cbase;

855. tr = rec_cat(J, bslot, cbase-2);

856. b = J->L->base + cbase;  /* Undo. */

857. J->L->base = b;

858. copyTV(J->L, b-2, &save);

859. }

860. if (tr) {  /* Store final result. */

861. BCReg dst = bc_a(*(frame_contpc(frame)-1));

862. J->base[dst] = tr;

863. if (dst >= J->maxslot) J->maxslot = dst+1;

864. }  /* Otherwise continue with another __concat call. */

865. } else {

866. /* Result type already specialized. */

867. lua_assert(cont == lj_cont_condf || cont == lj_cont_condt);

868. }

869. } else {

870. lj_trace_err(J, LJ_TRERR_NYIRETL);  /* NYI: handle return to C frame. */

871. }

872. lua_assert(J->baseslot >= 1);

873. }

875. /* -- Metamethod handling ------------------------------------------------- */

877. /* Prepare to record call to metamethod. */

878. ‌static BCReg rec_mm_prep(jit_State *J, ASMFunction cont)

879. {

880. BCReg s, top = cont == lj_cont_cat ? J->maxslot : curr_proto(J->L)->framesize;

881. #if LJ_64

882. TRef trcont = lj_ir_kptr(J, (void *)((int64_t)cont-(int64_t)lj_vm_asm_begin));

883. #else

884. TRef trcont = lj_ir_kptr(J, (void *)cont);

885. #endif

886. J->base[top] = trcont | TREF_CONT;

887. J->framedepth++;

888. for (s = J->maxslot; s < top; s++)

889. J->base[s] = 0;  /* Clear frame gap to avoid resurrecting previous refs. */

890. return top+1;

891. }

893. /* Record metamethod lookup. */

894. ‌int lj_record_mm_lookup(jit_State *J, RecordIndex *ix, MMS mm)

895. {

896. RecordIndex mix;

897. GCtab *mt;

898. if (tref_istab(ix->tab)) {

899. mt = tabref(tabV(&ix->tabv)->metatable);

900. mix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_TAB_META);

901. } else if (tref_isudata(ix->tab)) {

902. int udtype = udataV(&ix->tabv)->udtype;

903. mt = tabref(udataV(&ix->tabv)->metatable);

904. /* The metatables of special userdata objects are treated as immutable. */

905. if (udtype != UDTYPE_USERDATA) {

906. cTValue *mo;

907. if (LJ_HASFFI && udtype == UDTYPE_FFI_CLIB) {

908. /* Specialize to the C library namespace object. */

909. emitir(IRTG(IR_EQ, IRT_P32), ix->tab, lj_ir_kptr(J, udataV(&ix->tabv)));

910. } else {

911. /* Specialize to the type of userdata. */

912. TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), ix->tab, IRFL_UDATA_UDTYPE);

913. emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, udtype));

914. }

915. immutable_mt:

916. mo = lj_tab_getstr(mt, mmname_str(J2G(J), mm));

917. if (!mo || tvisnil(mo))

918. return 0;  /* No metamethod. */

919. /* Treat metamethod or index table as immutable, too. */

920. if (!(tvisfunc(mo) || tvistab(mo)))

921. lj_trace_err(J, LJ_TRERR_BADTYPE);

922. copyTV(J->L, &ix->mobjv, mo);

923. ix->mobj = lj_ir_kgc(J, gcV(mo), tvisfunc(mo) ? IRT_FUNC : IRT_TAB);

924. ix->mtv = mt;

925. ix->mt = TREF_NIL;  /* Dummy value for comparison semantics. */

926. return 1;  /* Got metamethod or index table. */

927. }

928. mix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_UDATA_META);

929. } else {

930. /* Specialize to base metatable. Must flush mcode in lua_setmetatable(). */

931. mt = tabref(basemt_obj(J2G(J), &ix->tabv));

932. if (mt == NULL) {

933. ix->mt = TREF_NIL;

934. return 0;  /* No metamethod. */

935. }

936. /* The cdata metatable is treated as immutable. */

937. if (LJ_HASFFI && tref_iscdata(ix->tab)) goto immutable_mt;

938. ix->mt = mix.tab = lj_ir_ktab(J, mt);

939. goto nocheck;

940. }

941. ix->mt = mt ? mix.tab : TREF_NIL;

942. emitir(IRTG(mt ? IR_NE : IR_EQ, IRT_TAB), mix.tab, lj_ir_knull(J, IRT_TAB));

943. nocheck:

944. if (mt) {

945. GCstr *mmstr = mmname_str(J2G(J), mm);

946. cTValue *mo = lj_tab_getstr(mt, mmstr);

947. if (mo && !tvisnil(mo))

948. copyTV(J->L, &ix->mobjv, mo);

949. ix->mtv = mt;

950. settabV(J->L, &mix.tabv, mt);

951. setstrV(J->L, &mix.keyv, mmstr);

952. mix.key = lj_ir_kstr(J, mmstr);

953. mix.val = 0;

954. mix.idxchain = 0;

955. ix->mobj = lj_record_idx(J, &mix);

956. return !tref_isnil(ix->mobj);  /* 1 if metamethod found, 0 if not. */

957. }

958. return 0;  /* No metamethod. */

959. }

961. /* Record call to arithmetic metamethod. */

962. ‌static TRef rec_mm_arith(jit_State *J, RecordIndex *ix, MMS mm)

963. {

964. /* Set up metamethod call first to save ix->tab and ix->tabv. */

965. BCReg func = rec_mm_prep(J, mm == MM_concat ? lj_cont_cat : lj_cont_ra);

966. TRef *base = J->base + func;

967. TValue *basev = J->L->base + func;

968. base[1] = ix->tab; base[2] = ix->key;

969. copyTV(J->L, basev+1, &ix->tabv);

970. copyTV(J->L, basev+2, &ix->keyv);

971. if (!lj_record_mm_lookup(J, ix, mm)) {  /* Lookup mm on 1st operand. */

972. if (mm != MM_unm) {

973. ix->tab = ix->key;

974. copyTV(J->L, &ix->tabv, &ix->keyv);

975. if (lj_record_mm_lookup(J, ix, mm))  /* Lookup mm on 2nd operand. */

976. goto ok;

977. }

978. lj_trace_err(J, LJ_TRERR_NOMM);

979. }

980. ok:

981. lua_assert(!LJ_FR2);  /* TODO_FR2: handle different frame setup. */

982. base[0] = ix->mobj;

983. copyTV(J->L, basev+0, &ix->mobjv);

984. lj_record_call(J, func, 2);

985. return 0;  /* No result yet. */

986. }

988. /* Record call to __len metamethod. */

989. ‌static TRef rec_mm_len(jit_State *J, TRef tr, TValue *tv)

990. {

991. RecordIndex ix;

992. ix.tab = tr;

993. copyTV(J->L, &ix.tabv, tv);

994. if (lj_record_mm_lookup(J, &ix, MM_len)) {

995. BCReg func = rec_mm_prep(J, lj_cont_ra);

996. TRef *base = J->base + func;

997. TValue *basev = J->L->base + func;

998. lua_assert(!LJ_FR2);  /* TODO_FR2: handle different frame setup. */

999. base[0] = ix.mobj; copyTV(J->L, basev+0, &ix.mobjv);

1000. base[1] = tr; copyTV(J->L, basev+1, tv);

1001. #if LJ_52

1002. base[2] = tr; copyTV(J->L, basev+2, tv);

1003. #else

1004. base[2] = TREF_NIL; setnilV(basev+2);

1005. #endif

1006. lj_record_call(J, func, 2);

1007. } else {

1008. if (LJ_52 && tref_istab(tr))

1009. return lj_ir_call(J, IRCALL_lj_tab_len, tr);

1010. lj_trace_err(J, LJ_TRERR_NOMM);

1011. }

1012. return 0;  /* No result yet. */

1013. }

1015. /* Call a comparison metamethod. */

1016. ‌static void rec_mm_callcomp(jit_State *J, RecordIndex *ix, int op)

1017. {

1018. BCReg func = rec_mm_prep(J, (op&1) ? lj_cont_condf : lj_cont_condt);

1019. TRef *base = J->base + func;

1020. TValue *tv = J->L->base + func;

1021. lua_assert(!LJ_FR2);  /* TODO_FR2: handle different frame setup. */

1022. base[0] = ix->mobj; base[1] = ix->val; base[2] = ix->key;

1023. copyTV(J->L, tv+0, &ix->mobjv);

1024. copyTV(J->L, tv+1, &ix->valv);

1025. copyTV(J->L, tv+2, &ix->keyv);

1026. lj_record_call(J, func, 2);

1027. }

1029. /* Record call to equality comparison metamethod (for tab and udata only). */

1030. ‌static void rec_mm_equal(jit_State *J, RecordIndex *ix, int op)

1031. {

1032. ix->tab = ix->val;

1033. copyTV(J->L, &ix->tabv, &ix->valv);

1034. if (lj_record_mm_lookup(J, ix, MM_eq)) {  /* Lookup mm on 1st operand. */

1035. cTValue *bv;

1036. TRef mo1 = ix->mobj;

1037. TValue mo1v;

1038. copyTV(J->L, &mo1v, &ix->mobjv);

1039. /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */

1040. bv = &ix->keyv;

1041. if (tvistab(bv) && tabref(tabV(bv)->metatable) == ix->mtv) {

1042. TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_TAB_META);

1043. emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt);

1044. } else if (tvisudata(bv) && tabref(udataV(bv)->metatable) == ix->mtv) {

1045. TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_UDATA_META);

1046. emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt);

1047. } else {  /* Lookup metamethod on 2nd operand and compare both. */

1048. ix->tab = ix->key;

1049. copyTV(J->L, &ix->tabv, bv);

1050. if (!lj_record_mm_lookup(J, ix, MM_eq) ||

1051. lj_record_objcmp(J, mo1, ix->mobj, &mo1v, &ix->mobjv))

1052. return;

1053. }

1054. rec_mm_callcomp(J, ix, op);

1055. }

1056. }

1058. /* Record call to ordered comparison metamethods (for arbitrary objects). */

1059. ‌static void rec_mm_comp(jit_State *J, RecordIndex *ix, int op)

1060. {

1061. ix->tab = ix->val;

1062. copyTV(J->L, &ix->tabv, &ix->valv);

1063. while (1) {

1064. MMS mm = (op & 2) ? MM_le : MM_lt;  /* Try __le + __lt or only __lt. */

1065. #if LJ_52

1066. if (!lj_record_mm_lookup(J, ix, mm)) {  /* Lookup mm on 1st operand. */

1067. ix->tab = ix->key;

1068. copyTV(J->L, &ix->tabv, &ix->keyv);

1069. if (!lj_record_mm_lookup(J, ix, mm))  /* Lookup mm on 2nd operand. */

1070. goto nomatch;

1071. }

1072. rec_mm_callcomp(J, ix, op);

1073. return;

1074. #else

1075. if (lj_record_mm_lookup(J, ix, mm)) {  /* Lookup mm on 1st operand. */

1076. cTValue *bv;

1077. TRef mo1 = ix->mobj;

1078. TValue mo1v;

1079. copyTV(J->L, &mo1v, &ix->mobjv);

1080. /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */

1081. bv = &ix->keyv;

1082. if (tvistab(bv) && tabref(tabV(bv)->metatable) == ix->mtv) {

1083. TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_TAB_META);

1084. emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt);

1085. } else if (tvisudata(bv) && tabref(udataV(bv)->metatable) == ix->mtv) {

1086. TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_UDATA_META);

1087. emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt);

1088. } else {  /* Lookup metamethod on 2nd operand and compare both. */

1089. ix->tab = ix->key;

1090. copyTV(J->L, &ix->tabv, bv);

1091. if (!lj_record_mm_lookup(J, ix, mm) ||

1092. lj_record_objcmp(J, mo1, ix->mobj, &mo1v, &ix->mobjv))

1093. goto nomatch;

1094. }

1095. rec_mm_callcomp(J, ix, op);

1096. return;

1097. }

1098. #endif

1099. nomatch:

1100. /* Lookup failed. Retry with  __lt and swapped operands. */

1101. if (!(op & 2)) break;  /* Already at __lt. Interpreter will throw. */

1102. ix->tab = ix->key; ix->key = ix->val; ix->val = ix->tab;

1103. copyTV(J->L, &ix->tabv, &ix->keyv);

1104. copyTV(J->L, &ix->keyv, &ix->valv);

1105. copyTV(J->L, &ix->valv, &ix->tabv);

1106. op ^= 3;

1107. }

1108. }

1110. #if LJ_HASFFI

1111. /* Setup call to cdata comparison metamethod. */

1112. ‌static void rec_mm_comp_cdata(jit_State *J, RecordIndex *ix, int op, MMS mm)

1113. {

1114. lj_snap_add(J);

1115. if (tref_iscdata(ix->val)) {

1116. ix->tab = ix->val;

1117. copyTV(J->L, &ix->tabv, &ix->valv);

1118. } else {

1119. lua_assert(tref_iscdata(ix->key));

1120. ix->tab = ix->key;

1121. copyTV(J->L, &ix->tabv, &ix->keyv);

1122. }

1123. lj_record_mm_lookup(J, ix, mm);

1124. rec_mm_callcomp(J, ix, op);

1125. }

1126. #endif

1128. /* -- Indexed access ------------------------------------------------------ */

1130. /* Record bounds-check. */

1131. ‌static void rec_idx_abc(jit_State *J, TRef asizeref, TRef ikey, uint32_t asize)

1132. {

1133. /* Try to emit invariant bounds checks. */

1134. if ((J->flags & (JIT_F_OPT_LOOP|JIT_F_OPT_ABC)) ==

1135. (JIT_F_OPT_LOOP|JIT_F_OPT_ABC)) {

1136. IRRef ref = tref_ref(ikey);

1137. IRIns *ir = IR(ref);

1138. int32_t ofs = 0;

1139. IRRef ofsref = 0;

1140. /* Handle constant offsets. */

1141. if (ir->o == IR_ADD && irref_isk(ir->op2)) {

1142. ofsref = ir->op2;

1143. ofs = IR(ofsref)->i;

1144. ref = ir->op1;

1145. ir = IR(ref);

1146. }

1147. /* Got scalar evolution analysis results for this reference? */

1148. if (ref == J->scev.idx) {

1149. int32_t stop;

1150. lua_assert(irt_isint(J->scev.t) && ir->o == IR_SLOAD);

1151. stop = numberVint(&(J->L->base - J->baseslot)[ir->op1 + FORL_STOP]);

1152. /* Runtime value for stop of loop is within bounds? */

1153. if ((uint64_t)stop + ofs < (uint64_t)asize) {

1154. /* Emit invariant bounds check for stop. */

1155. emitir(IRTG(IR_ABC, IRT_P32), asizeref, ofs == 0 ? J->scev.stop :

1156. emitir(IRTI(IR_ADD), J->scev.stop, ofsref));

1157. /* Emit invariant bounds check for start, if not const or negative. */

1158. if (!(J->scev.dir && J->scev.start &&

1159. (int64_t)IR(J->scev.start)->i + ofs >= 0))

1160. emitir(IRTG(IR_ABC, IRT_P32), asizeref, ikey);

1161. return;

1162. }

1163. }

1164. }

1165. emitir(IRTGI(IR_ABC), asizeref, ikey);  /* Emit regular bounds check. */

1166. }

1168. /* Record indexed key lookup. */

1169. ‌static TRef rec_idx_key(jit_State *J, RecordIndex *ix, IRRef *rbref)

1170. {

1171. TRef key;

1172. GCtab *t = tabV(&ix->tabv);

1173. ix->oldv = lj_tab_get(J->L, t, &ix->keyv);  /* Lookup previous value. */

1174. *rbref = 0;

1176. /* Integer keys are looked up in the array part first. */

1177. key = ix->key;

1178. if (tref_isnumber(key)) {

1179. int32_t k = numberVint(&ix->keyv);

1180. if (!tvisint(&ix->keyv) && numV(&ix->keyv) != (lua_Number)k)

1181. k = LJ_MAX_ASIZE;

1182. if ((MSize)k < LJ_MAX_ASIZE) {  /* Potential array key? */

1183. TRef ikey = lj_opt_narrow_index(J, key);

1184. TRef asizeref = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_ASIZE);

1185. if ((MSize)k < t->asize) {  /* Currently an array key? */

1186. TRef arrayref;

1187. rec_idx_abc(J, asizeref, ikey, t->asize);

1188. arrayref = emitir(IRT(IR_FLOAD, IRT_P32), ix->tab, IRFL_TAB_ARRAY);

1189. return emitir(IRT(IR_AREF, IRT_P32), arrayref, ikey);

1190. } else {  /* Currently not in array (may be an array extension)? */

1191. emitir(IRTGI(IR_ULE), asizeref, ikey);  /* Inv. bounds check. */

1192. if (k == 0 && tref_isk(key))

1193. key = lj_ir_knum_zero(J);  /* Canonicalize 0 or +-0.0 to +0.0. */

1194. /* And continue with the hash lookup. */

1195. }

1196. } else if (!tref_isk(key)) {

1197. /* We can rule out const numbers which failed the integerness test

1198. ** above. But all other numbers are potential array keys.

1199. */

1200. if (t->asize == 0) {  /* True sparse tables have an empty array part. */

1201. /* Guard that the array part stays empty. */

1202. TRef tmp = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_ASIZE);

1203. emitir(IRTGI(IR_EQ), tmp, lj_ir_kint(J, 0));

1204. } else {

1205. lj_trace_err(J, LJ_TRERR_NYITMIX);

1206. }

1207. }

1208. }

1210. /* Otherwise the key is located in the hash part. */

1211. if (t->hmask == 0) {  /* Shortcut for empty hash part. */

1212. /* Guard that the hash part stays empty. */

1213. TRef tmp = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_HMASK);

1214. emitir(IRTGI(IR_EQ), tmp, lj_ir_kint(J, 0));

1215. return lj_ir_kkptr(J, niltvg(J2G(J)));

1216. }

1217. if (tref_isinteger(key))  /* Hash keys are based on numbers, not ints. */

1218. key = emitir(IRTN(IR_CONV), key, IRCONV_NUM_INT);

1219. if (tref_isk(key)) {

1220. /* Optimize lookup of constant hash keys. */

1221. MSize hslot = (MSize)((char *)ix->oldv - (char *)&noderef(t->node)[0].val);

1222. if (t->hmask > 0 && hslot <= t->hmask*(MSize)sizeof(Node) &&

1223. hslot <= 65535*(MSize)sizeof(Node)) {

1224. TRef node, kslot, hm;

1225. *rbref = J->cur.nins;  /* Mark possible rollback point. */

1226. hm = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_HMASK);

1227. emitir(IRTGI(IR_EQ), hm, lj_ir_kint(J, (int32_t)t->hmask));

1228. node = emitir(IRT(IR_FLOAD, IRT_P32), ix->tab, IRFL_TAB_NODE);

1229. kslot = lj_ir_kslot(J, key, hslot / sizeof(Node));

1230. return emitir(IRTG(IR_HREFK, IRT_P32), node, kslot);

1231. }

1232. }

1233. /* Fall back to a regular hash lookup. */

1234. return emitir(IRT(IR_HREF, IRT_P32), ix->tab, key);

1235. }

1237. /* Determine whether a key is NOT one of the fast metamethod names. */

1238. ‌static int nommstr(jit_State *J, TRef key)

1239. {

1240. if (tref_isstr(key)) {

1241. if (tref_isk(key)) {

1242. GCstr *str = ir_kstr(IR(tref_ref(key)));

1243. uint32_t mm;

1244. for (mm = 0; mm <= MM_FAST; mm++)

1245. if (mmname_str(J2G(J), mm) == str)

1246. return 0;  /* MUST be one the fast metamethod names. */

1247. } else {

1248. return 0;  /* Variable string key MAY be a metamethod name. */

1249. }

1250. }

1251. return 1;  /* CANNOT be a metamethod name. */

1252. }

1254. /* Record indexed load/store. */

1255. ‌TRef lj_record_idx(jit_State *J, RecordIndex *ix)

1256. {

1257. TRef xref;

1258. IROp xrefop, loadop;

1259. IRRef rbref;

1260. cTValue *oldv;

1262. while (!tref_istab(ix->tab)) { /* Handle non-table lookup. */

1263. /* Never call raw lj_record_idx() on non-table. */

1264. lua_assert(ix->idxchain != 0);

1265. if (!lj_record_mm_lookup(J, ix, ix->val ? MM_newindex : MM_index))

1266. lj_trace_err(J, LJ_TRERR_NOMM);

1267. handlemm:

1268. if (tref_isfunc(ix->mobj)) {  /* Handle metamethod call. */

1269. BCReg func = rec_mm_prep(J, ix->val ? lj_cont_nop : lj_cont_ra);

1270. TRef *base = J->base + func;

1271. TValue *tv = J->L->base + func;

1272. lua_assert(!LJ_FR2);  /* TODO_FR2: handle different frame setup. */

1273. base[0] = ix->mobj; base[1] = ix->tab; base[2] = ix->key;

1274. setfuncV(J->L, tv+0, funcV(&ix->mobjv));

1275. copyTV(J->L, tv+1, &ix->tabv);

1276. copyTV(J->L, tv+2, &ix->keyv);

1277. if (ix->val) {

1278. base[3] = ix->val;

1279. copyTV(J->L, tv+3, &ix->valv);

1280. lj_record_call(J, func, 3);  /* mobj(tab, key, val) */

1281. return 0;

1282. } else {

1283. lj_record_call(J, func, 2);  /* res = mobj(tab, key) */

1284. return 0;  /* No result yet. */

1285. }

1286. }

1287. /* Otherwise retry lookup with metaobject. */

1288. ix->tab = ix->mobj;

1289. copyTV(J->L, &ix->tabv, &ix->mobjv);

1290. if (--ix->idxchain == 0)

1291. lj_trace_err(J, LJ_TRERR_IDXLOOP);

1292. }

1294. /* First catch nil and NaN keys for tables. */

1295. if (tvisnil(&ix->keyv) || (tvisnum(&ix->keyv) && tvisnan(&ix->keyv))) {

1296. if (ix->val)  /* Better fail early. */

1297. lj_trace_err(J, LJ_TRERR_STORENN);

1298. if (tref_isk(ix->key)) {

1299. if (ix->idxchain && lj_record_mm_lookup(J, ix, MM_index))

1300. goto handlemm;

1301. return TREF_NIL;

1302. }

1303. }

1305. /* Record the key lookup. */

1306. xref = rec_idx_key(J, ix, &rbref);

1307. xrefop = IR(tref_ref(xref))->o;

1308. loadop = xrefop == IR_AREF ? IR_ALOAD : IR_HLOAD;

1309. /* The lj_meta_tset() inconsistency is gone, but better play safe. */

1310. oldv = xrefop == IR_KKPTR ? (cTValue *)ir_kptr(IR(tref_ref(xref))) : ix->oldv;

1312. if (ix->val == 0) {  /* Indexed load */

1313. IRType t = itype2irt(oldv);

1314. TRef res;

1315. if (oldv == niltvg(J2G(J))) {

1316. emitir(IRTG(IR_EQ, IRT_P32), xref, lj_ir_kkptr(J, niltvg(J2G(J))));

1317. res = TREF_NIL;

1318. } else {

1319. res = emitir(IRTG(loadop, t), xref, 0);

1320. }

1321. if (tref_ref(res) < rbref)  /* HREFK + load forwarded? */

1322. lj_ir_rollback(J, rbref);  /* Rollback to eliminate hmask guard. */

1323. if (t == IRT_NIL && ix->idxchain && lj_record_mm_lookup(J, ix, MM_index))

1324. goto handlemm;

1325. if (irtype_ispri(t)) res = TREF_PRI(t);  /* Canonicalize primitives. */

1326. return res;

1327. } else {  /* Indexed store. */

1328. GCtab *mt = tabref(tabV(&ix->tabv)->metatable);

1329. int keybarrier = tref_isgcv(ix->key) && !tref_isnil(ix->val);

1330. if (tref_ref(xref) < rbref)  /* HREFK forwarded? */

1331. lj_ir_rollback(J, rbref);  /* Rollback to eliminate hmask guard. */

1332. if (tvisnil(oldv)) {  /* Previous value was nil? */

1333. /* Need to duplicate the hasmm check for the early guards. */

1334. int hasmm = 0;

1335. if (ix->idxchain && mt) {

1336. cTValue *mo = lj_tab_getstr(mt, mmname_str(J2G(J), MM_newindex));

1337. hasmm = mo && !tvisnil(mo);

1338. }

1339. if (hasmm)

1340. emitir(IRTG(loadop, IRT_NIL), xref, 0);  /* Guard for nil value. */

1341. else if (xrefop == IR_HREF)

1342. emitir(IRTG(oldv == niltvg(J2G(J)) ? IR_EQ : IR_NE, IRT_P32),

1343. xref, lj_ir_kkptr(J, niltvg(J2G(J))));

1344. if (ix->idxchain && lj_record_mm_lookup(J, ix, MM_newindex)) {

1345. lua_assert(hasmm);

1346. goto handlemm;

1347. }

1348. lua_assert(!hasmm);

1349. if (oldv == niltvg(J2G(J))) {  /* Need to insert a new key. */

1350. TRef key = ix->key;

1351. if (tref_isinteger(key))  /* NEWREF needs a TValue as a key. */

1352. key = emitir(IRTN(IR_CONV), key, IRCONV_NUM_INT);

1353. xref = emitir(IRT(IR_NEWREF, IRT_P32), ix->tab, key);

1354. keybarrier = 0;  /* NEWREF already takes care of the key barrier. */

1355. }

1356. } else if (!lj_opt_fwd_wasnonnil(J, loadop, tref_ref(xref))) {

1357. /* Cannot derive that the previous value was non-nil, must do checks. */

1358. if (xrefop == IR_HREF)  /* Guard against store to niltv. */

1359. emitir(IRTG(IR_NE, IRT_P32), xref, lj_ir_kkptr(J, niltvg(J2G(J))));

1360. if (ix->idxchain) {  /* Metamethod lookup required? */

1361. /* A check for NULL metatable is cheaper (hoistable) than a load. */

1362. if (!mt) {

1363. TRef mtref = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_TAB_META);

1364. emitir(IRTG(IR_EQ, IRT_TAB), mtref, lj_ir_knull(J, IRT_TAB));

1365. } else {

1366. IRType t = itype2irt(oldv);

1367. emitir(IRTG(loadop, t), xref, 0);  /* Guard for non-nil value. */

1368. }

1369. }

1370. } else {

1371. keybarrier = 0;  /* Previous non-nil value kept the key alive. */

1372. }

1373. /* Convert int to number before storing. */

1374. if (!LJ_DUALNUM && tref_isinteger(ix->val))

1375. ix->val = emitir(IRTN(IR_CONV), ix->val, IRCONV_NUM_INT);

1376. emitir(IRT(loadop+IRDELTA_L2S, tref_type(ix->val)), xref, ix->val);

1377. if (keybarrier || tref_isgcv(ix->val))

1378. emitir(IRT(IR_TBAR, IRT_NIL), ix->tab, 0);

1379. /* Invalidate neg. metamethod cache for stores with certain string keys. */

1380. if (!nommstr(J, ix->key)) {

1381. TRef fref = emitir(IRT(IR_FREF, IRT_P32), ix->tab, IRFL_TAB_NOMM);

1382. emitir(IRT(IR_FSTORE, IRT_U8), fref, lj_ir_kint(J, 0));

1383. }

1384. J->needsnap = 1;

1385. return 0;

1386. }

1387. }

1389. ‌static void rec_tsetm(jit_State *J, BCReg ra, BCReg rn, int32_t i)

1390. {

1391. RecordIndex ix;

1392. cTValue *basev = J->L->base;

1393. copyTV(J->L, &ix.tabv, &basev[ra-1]);

1394. ix.tab = getslot(J, ra-1);

1395. ix.idxchain = 0;

1396. for (; ra < rn; i++, ra++) {

1397. setintV(&ix.keyv, i);

1398. ix.key = lj_ir_kint(J, i);

1399. copyTV(J->L, &ix.valv, &basev[ra]);

1400. ix.val = getslot(J, ra);

1401. lj_record_idx(J, &ix);

1402. }

1403. }

1405. /* -- Upvalue access ------------------------------------------------------ */

1407. /* Check whether upvalue is immutable and ok to constify. */

1408. ‌static int rec_upvalue_constify(jit_State *J, GCupval *uvp)

1409. {

1410. if (uvp->immutable) {

1411. cTValue *o = uvval(uvp);

1412. /* Don't constify objects that may retain large amounts of memory. */

1413. #if LJ_HASFFI

1414. if (tviscdata(o)) {

1415. GCcdata *cd = cdataV(o);

1416. if (!cdataisv(cd) && !(cd->marked & LJ_GC_CDATA_FIN)) {

1417. CType *ct = ctype_raw(ctype_ctsG(J2G(J)), cd->ctypeid);

1418. if (!ctype_hassize(ct->info) || ct->size <= 16)

1419. return 1;

1420. }

1421. return 0;

1422. }

1423. #else

1424. UNUSED(J);

1425. #endif

1426. if (!(tvistab(o) || tvisudata(o) || tvisthread(o)))

1427. return 1;

1428. }

1429. return 0;

1430. }

1432. /* Record upvalue load/store. */

1433. ‌static TRef rec_upvalue(jit_State *J, uint32_t uv, TRef val)

1434. {

1435. GCupval *uvp = &gcref(J->fn->l.uvptr[uv])->uv;

1436. TRef fn = getcurrf(J);

1437. IRRef uref;

1438. int needbarrier = 0;

1439. if (rec_upvalue_constify(J, uvp)) {  /* Try to constify immutable upvalue. */

1440. TRef tr, kfunc;

1441. lua_assert(val == 0);

1442. if (!tref_isk(fn)) {  /* Late specialization of current function. */

1443. if (J->pt->flags >= PROTO_CLC_POLY)

1444. goto noconstify;

1445. kfunc = lj_ir_kfunc(J, J->fn);

1446. emitir(IRTG(IR_EQ, IRT_FUNC), fn, kfunc);

1447. J->base[-1] = TREF_FRAME | kfunc;

1448. fn = kfunc;

1449. }

1450. tr = lj_record_constify(J, uvval(uvp));

1451. if (tr)

1452. return tr;

1453. }

1454. noconstify:

1455. /* Note: this effectively limits LJ_MAX_UPVAL to 127. */

1456. uv = (uv << 8) | (hashrot(uvp->dhash, uvp->dhash + HASH_BIAS) & 0xff);

1457. if (!uvp->closed) {

1458. /* In current stack? */

1459. if (uvval(uvp) >= tvref(J->L->stack) &&

1460. uvval(uvp) < tvref(J->L->maxstack)) {

1461. int32_t slot = (int32_t)(uvval(uvp) - (J->L->base - J->baseslot));

1462. if (slot >= 0) {  /* Aliases an SSA slot? */

1463. slot -= (int32_t)J->baseslot;  /* Note: slot number may be negative! */

1464. /* NYI: add IR to guard that it's still aliasing the same slot. */

1465. if (val == 0) {

1466. return getslot(J, slot);

1467. } else {

1468. J->base[slot] = val;

1469. if (slot >= (int32_t)J->maxslot) J->maxslot = (BCReg)(slot+1);

1470. return 0;

1471. }

1472. }

1473. }

1474. uref = tref_ref(emitir(IRTG(IR_UREFO, IRT_P32), fn, uv));

1475. } else {

1476. needbarrier = 1;

1477. uref = tref_ref(emitir(IRTG(IR_UREFC, IRT_P32), fn, uv));

1478. }

1479. if (val == 0) {  /* Upvalue load */

1480. IRType t = itype2irt(uvval(uvp));

1481. TRef res = emitir(IRTG(IR_ULOAD, t), uref, 0);

1482. if (irtype_ispri(t)) res = TREF_PRI(t);  /* Canonicalize primitive refs. */

1483. return res;

1484. } else {  /* Upvalue store. */

1485. /* Convert int to number before storing. */

1486. if (!LJ_DUALNUM && tref_isinteger(val))

1487. val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT);

1488. emitir(IRT(IR_USTORE, tref_type(val)), uref, val);

1489. if (needbarrier && tref_isgcv(val))

1490. emitir(IRT(IR_OBAR, IRT_NIL), uref, val);

1491. J->needsnap = 1;

1492. return 0;

1493. }

1494. }

1496. /* -- Record calls to Lua functions --------------------------------------- */

1498. /* Check unroll limits for calls. */

1499. ‌static void check_call_unroll(jit_State *J, TraceNo lnk)

1500. {

1501. cTValue *frame = J->L->base - 1;

1502. void *pc = mref(frame_func(frame)->l.pc, void);

1503. int32_t depth = J->framedepth;

1504. int32_t count = 0;

1505. if ((J->pt->flags & PROTO_VARARG)) depth--;  /* Vararg frame still missing. */

1506. for (; depth > 0; depth--) {  /* Count frames with same prototype. */

1507. if (frame_iscont(frame)) depth--;

1508. frame = frame_prev(frame);

1509. if (mref(frame_func(frame)->l.pc, void) == pc)

1510. count++;

1511. }

1512. if (J->pc == J->startpc) {

1513. if (count + J->tailcalled > J->param[JIT_P_recunroll]) {

1514. J->pc++;

1515. if (J->framedepth + J->retdepth == 0)

1516. lj_record_stop(J, LJ_TRLINK_TAILREC, J->cur.traceno);  /* Tail-rec. */

1517. else

1518. lj_record_stop(J, LJ_TRLINK_UPREC, J->cur.traceno);  /* Up-recursion. */

1519. }

1520. } else {

1521. if (count > J->param[JIT_P_callunroll]) {

1522. if (lnk) {  /* Possible tail- or up-recursion. */

1523. lj_trace_flush(J, lnk);  /* Flush trace that only returns. */

1524. /* Set a small, pseudo-random hotcount for a quick retry of JFUNC*. */

1525. hotcount_set(J2GG(J), J->pc+1, LJ_PRNG_BITS(J, 4));

1526. }

1527. lj_trace_err(J, LJ_TRERR_CUNROLL);

1528. }

1529. }

1530. }

1532. /* Record Lua function setup. */

1533. ‌static void rec_func_setup(jit_State *J)

1534. {

1535. GCproto *pt = J->pt;

1536. BCReg s, numparams = pt->numparams;

1537. if ((pt->flags & PROTO_NOJIT))

1538. lj_trace_err(J, LJ_TRERR_CJITOFF);

1539. if (J->baseslot + pt->framesize >= LJ_MAX_JSLOTS)

1540. lj_trace_err(J, LJ_TRERR_STACKOV);

1541. /* Fill up missing parameters with nil. */

1542. for (s = J->maxslot; s < numparams; s++)

1543. J->base[s] = TREF_NIL;

1544. /* The remaining slots should never be read before they are written. */

1545. J->maxslot = numparams;

1546. }

1548. /* Record Lua vararg function setup. */

1549. ‌static void rec_func_vararg(jit_State *J)

1550. {

1551. GCproto *pt = J->pt;

1552. BCReg s, fixargs, vframe = J->maxslot+1;

1553. lua_assert((pt->flags & PROTO_VARARG));

1554. if (J->baseslot + vframe + pt->framesize >= LJ_MAX_JSLOTS)

1555. lj_trace_err(J, LJ_TRERR_STACKOV);

1556. J->base[vframe-1] = J->base[-1];  /* Copy function up. */

1557. /* Copy fixarg slots up and set their original slots to nil. */

1558. fixargs = pt->numparams < J->maxslot ? pt->numparams : J->maxslot;

1559. for (s = 0; s < fixargs; s++) {

1560. J->base[vframe+s] = J->base[s];

1561. J->base[s] = TREF_NIL;

1562. }

1563. J->maxslot = fixargs;

1564. J->framedepth++;

1565. J->base += vframe;

1566. J->baseslot += vframe;

1567. }

1569. /* Record entry to a Lua function. */

1570. ‌static void rec_func_lua(jit_State *J)

1571. {

1572. rec_func_setup(J);

1573. check_call_unroll(J, 0);

1574. }

1576. /* Record entry to an already compiled function. */

1577. ‌static void rec_func_jit(jit_State *J, TraceNo lnk)

1578. {

1579. GCtrace *T;

1580. rec_func_setup(J);

1581. T = traceref(J, lnk);

1582. if (T->linktype == LJ_TRLINK_RETURN) {  /* Trace returns to interpreter? */

1583. check_call_unroll(J, lnk);

1584. /* Temporarily unpatch JFUNC* to continue recording across function. */

1585. J->patchins = *J->pc;

1586. J->patchpc = (BCIns *)J->pc;

1587. *J->patchpc = T->startins;

1588. return;

1589. }

1590. J->instunroll = 0;  /* Cannot continue across a compiled function. */

1591. if (J->pc == J->startpc && J->framedepth + J->retdepth == 0)

1592. lj_record_stop(J, LJ_TRLINK_TAILREC, J->cur.traceno);  /* Extra tail-rec. */

1593. else

1594. lj_record_stop(J, LJ_TRLINK_ROOT, lnk);  /* Link to the function. */

1595. }

1597. /* -- Vararg handling ----------------------------------------------------- */

1599. /* Detect y = select(x, ...) idiom. */

1600. ‌static int select_detect(jit_State *J)

1601. {

1602. BCIns ins = J->pc[1];

1603. if (bc_op(ins) == BC_CALLM && bc_b(ins) == 2 && bc_c(ins) == 1) {

1604. cTValue *func = &J->L->base[bc_a(ins)];

1605. if (tvisfunc(func) && funcV(func)->c.ffid == FF_select)

1606. return 1;

1607. }

1608. return 0;

1609. }

1611. /* Record vararg instruction. */

1612. ‌static void rec_varg(jit_State *J, BCReg dst, ptrdiff_t nresults)

1613. {

1614. int32_t numparams = J->pt->numparams;

1615. ptrdiff_t nvararg = frame_delta(J->L->base-1) - numparams - 1;

1616. lua_assert(frame_isvarg(J->L->base-1));

1617. if (J->framedepth > 0) {  /* Simple case: varargs defined on-trace. */

1618. ptrdiff_t i;

1619. if (nvararg < 0) nvararg = 0;

1620. if (nresults == -1) {

1621. nresults = nvararg;

1622. J->maxslot = dst + (BCReg)nvararg;

1623. } else if (dst + nresults > J->maxslot) {

1624. J->maxslot = dst + (BCReg)nresults;

1625. }

1626. for (i = 0; i < nresults; i++)

1627. J->base[dst+i] = i < nvararg ? getslot(J, i - nvararg - 1) : TREF_NIL;

1628. } else {  /* Unknown number of varargs passed to trace. */

1629. TRef fr = emitir(IRTI(IR_SLOAD), 0, IRSLOAD_READONLY|IRSLOAD_FRAME);

1630. int32_t frofs = 8*(1+numparams)+FRAME_VARG;

1631. if (nresults >= 0) {  /* Known fixed number of results. */

1632. ptrdiff_t i;

1633. if (nvararg > 0) {

1634. ptrdiff_t nload = nvararg >= nresults ? nresults : nvararg;

1635. TRef vbase;

1636. if (nvararg >= nresults)

1637. emitir(IRTGI(IR_GE), fr, lj_ir_kint(J, frofs+8*(int32_t)nresults));

1638. else

1639. emitir(IRTGI(IR_EQ), fr,

1640. lj_ir_kint(J, (int32_t)frame_ftsz(J->L->base-1)));

1641. vbase = emitir(IRTI(IR_SUB), REF_BASE, fr);

1642. vbase = emitir(IRT(IR_ADD, IRT_P32), vbase, lj_ir_kint(J, frofs-8));

1643. for (i = 0; i < nload; i++) {

1644. IRType t = itype2irt(&J->L->base[i-1-nvararg]);

1645. TRef aref = emitir(IRT(IR_AREF, IRT_P32),

1646. vbase, lj_ir_kint(J, (int32_t)i));

1647. TRef tr = emitir(IRTG(IR_VLOAD, t), aref, 0);

1648. if (irtype_ispri(t)) tr = TREF_PRI(t);  /* Canonicalize primitives. */

1649. J->base[dst+i] = tr;

1650. }

1651. } else {

1652. emitir(IRTGI(IR_LE), fr, lj_ir_kint(J, frofs));

1653. nvararg = 0;

1654. }

1655. for (i = nvararg; i < nresults; i++)

1656. J->base[dst+i] = TREF_NIL;

1657. if (dst + (BCReg)nresults > J->maxslot)

1658. J->maxslot = dst + (BCReg)nresults;

1659. } else if (select_detect(J)) {  /* y = select(x, ...) */

1660. TRef tridx = J->base[dst-1];

1661. TRef tr = TREF_NIL;

1662. ptrdiff_t idx = lj_ffrecord_select_mode(J, tridx, &J->L->base[dst-1]);

1663. if (idx < 0) goto nyivarg;

1664. if (idx != 0 && !tref_isinteger(tridx))

1665. tridx = emitir(IRTGI(IR_CONV), tridx, IRCONV_INT_NUM|IRCONV_INDEX);

1666. if (idx != 0 && tref_isk(tridx)) {

1667. emitir(IRTGI(idx <= nvararg ? IR_GE : IR_LT),

1668. fr, lj_ir_kint(J, frofs+8*(int32_t)idx));

1669. frofs -= 8;  /* Bias for 1-based index. */

1670. } else if (idx <= nvararg) {  /* Compute size. */

1671. TRef tmp = emitir(IRTI(IR_ADD), fr, lj_ir_kint(J, -frofs));

1672. if (numparams)

1673. emitir(IRTGI(IR_GE), tmp, lj_ir_kint(J, 0));

1674. tr = emitir(IRTI(IR_BSHR), tmp, lj_ir_kint(J, 3));

1675. if (idx != 0) {

1676. tridx = emitir(IRTI(IR_ADD), tridx, lj_ir_kint(J, -1));

1677. rec_idx_abc(J, tr, tridx, (uint32_t)nvararg);

1678. }

1679. } else {

1680. TRef tmp = lj_ir_kint(J, frofs);

1681. if (idx != 0) {

1682. TRef tmp2 = emitir(IRTI(IR_BSHL), tridx, lj_ir_kint(J, 3));

1683. tmp = emitir(IRTI(IR_ADD), tmp2, tmp);

1684. } else {

1685. tr = lj_ir_kint(J, 0);

1686. }

1687. emitir(IRTGI(IR_LT), fr, tmp);

1688. }

1689. if (idx != 0 && idx <= nvararg) {

1690. IRType t;

1691. TRef aref, vbase = emitir(IRTI(IR_SUB), REF_BASE, fr);

1692. vbase = emitir(IRT(IR_ADD, IRT_P32), vbase, lj_ir_kint(J, frofs-8));

1693. t = itype2irt(&J->L->base[idx-2-nvararg]);

1694. aref = emitir(IRT(IR_AREF, IRT_P32), vbase, tridx);

1695. tr = emitir(IRTG(IR_VLOAD, t), aref, 0);

1696. if (irtype_ispri(t)) tr = TREF_PRI(t);  /* Canonicalize primitives. */

1697. }

1698. J->base[dst-2] = tr;

1699. J->maxslot = dst-1;

1700. J->bcskip = 2;  /* Skip CALLM + select. */

1701. } else {

1702. nyivarg:

1703. setintV(&J->errinfo, BC_VARG);

1704. lj_trace_err_info(J, LJ_TRERR_NYIBC);

1705. }

1706. }

1707. }

1709. /* -- Record allocations -------------------------------------------------- */

1711. ‌static TRef rec_tnew(jit_State *J, uint32_t ah)

1712. {

1713. uint32_t asize = ah & 0x7ff;

1714. uint32_t hbits = ah >> 11;

1715. if (asize == 0x7ff) asize = 0x801;

1716. return emitir(IRTG(IR_TNEW, IRT_TAB), asize, hbits);

1717. }

1719. /* -- Concatenation ------------------------------------------------------- */

1721. ‌static TRef rec_cat(jit_State *J, BCReg baseslot, BCReg topslot)

1722. {

1723. TRef *top = &J->base[topslot];

1724. TValue savetv[5];

1725. BCReg s;

1726. RecordIndex ix;

1727. lua_assert(baseslot < topslot);

1728. for (s = baseslot; s <= topslot; s++)

1729. (void)getslot(J, s);  /* Ensure all arguments have a reference. */

1730. if (tref_isnumber_str(top[0]) && tref_isnumber_str(top[-1])) {

1731. TRef tr, hdr, *trp, *xbase, *base = &J->base[baseslot];

1732. /* First convert numbers to strings. */

1733. for (trp = top; trp >= base; trp--) {

1734. if (tref_isnumber(*trp))

1735. *trp = emitir(IRT(IR_TOSTR, IRT_STR), *trp,

1736. tref_isnum(*trp) ? IRTOSTR_NUM : IRTOSTR_INT);

1737. else if (!tref_isstr(*trp))

1738. break;

1739. }

1740. xbase = ++trp;

1741. tr = hdr = emitir(IRT(IR_BUFHDR, IRT_P32),

1742. lj_ir_kptr(J, &J2G(J)->tmpbuf), IRBUFHDR_RESET);

1743. do {

1744. tr = emitir(IRT(IR_BUFPUT, IRT_P32), tr, *trp++);

1745. } while (trp <= top);

1746. tr = emitir(IRT(IR_BUFSTR, IRT_STR), tr, hdr);

1747. J->maxslot = (BCReg)(xbase - J->base);

1748. if (xbase == base) return tr;  /* Return simple concatenation result. */

1749. /* Pass partial result. */

1750. topslot = J->maxslot--;

1751. *xbase = tr;

1752. top = xbase;

1753. setstrV(J->L, &ix.keyv, &J2G(J)->strempty);  /* Simulate string result. */

1754. } else {

1755. J->maxslot = topslot-1;

1756. copyTV(J->L, &ix.keyv, &J->L->base[topslot]);

1757. }

1758. copyTV(J->L, &ix.tabv, &J->L->base[topslot-1]);

1759. ix.tab = top[-1];

1760. ix.key = top[0];

1761. memcpy(savetv, &J->L->base[topslot-1], sizeof(savetv));  /* Save slots. */

1762. rec_mm_arith(J, &ix, MM_concat);  /* Call __concat metamethod. */

1763. memcpy(&J->L->base[topslot-1], savetv, sizeof(savetv));  /* Restore slots. */

1764. return 0;  /* No result yet. */

1765. }

1767. /* -- Record bytecode ops ------------------------------------------------- */

1769. /* Prepare for comparison. */

1770. ‌static void rec_comp_prep(jit_State *J)

1771. {

1772. /* Prevent merging with snapshot #0 (GC exit) since we fixup the PC. */

1773. if (J->cur.nsnap == 1 && J->cur.snap[0].ref == J->cur.nins)

1774. emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0);

1775. lj_snap_add(J);

1776. }

1778. /* Fixup comparison. */

1779. ‌static void rec_comp_fixup(jit_State *J, const BCIns *pc, int cond)

1780. {

1781. BCIns jmpins = pc[1];

1782. const BCIns *npc = pc + 2 + (cond ? bc_j(jmpins) : 0);

1783. SnapShot *snap = &J->cur.snap[J->cur.nsnap-1];

1784. /* Set PC to opposite target to avoid re-recording the comp. in side trace. */

1785. J->cur.snapmap[snap->mapofs + snap->nent] = SNAP_MKPC(npc);

1786. J->needsnap = 1;

1787. if (bc_a(jmpins) < J->maxslot) J->maxslot = bc_a(jmpins);

1788. lj_snap_shrink(J);  /* Shrink last snapshot if possible. */

1789. }

1791. /* Record the next bytecode instruction (_before_ it's executed). */

1792. ‌void lj_record_ins(jit_State *J)

1793. {

1794. cTValue *lbase;

1795. RecordIndex ix;

1796. const BCIns *pc;

1797. BCIns ins;

1798. BCOp op;

1799. TRef ra, rb, rc;

1801. /* Perform post-processing action before recording the next instruction. */

1802. if (LJ_UNLIKELY(J->postproc != LJ_POST_NONE)) {

1803. switch (J->postproc) {

1804. case LJ_POST_FIXCOMP:  /* Fixup comparison. */

1805. pc = (const BCIns *)(uintptr_t)J2G(J)->tmptv.u64;

1806. rec_comp_fixup(J, pc, (!tvistruecond(&J2G(J)->tmptv2) ^ (bc_op(*pc)&1)));

1807. /* fallthrough */

1808. case LJ_POST_FIXGUARD:  /* Fixup and emit pending guard. */

1809. case LJ_POST_FIXGUARDSNAP:  /* Fixup and emit pending guard and snapshot. */

1810. if (!tvistruecond(&J2G(J)->tmptv2)) {

1811. J->fold.ins.o ^= 1;  /* Flip guard to opposite. */

1812. if (J->postproc == LJ_POST_FIXGUARDSNAP) {

1813. SnapShot *snap = &J->cur.snap[J->cur.nsnap-1];

1814. J->cur.snapmap[snap->mapofs+snap->nent-1]--;  /* False -> true. */

1815. }

1816. }

1817. lj_opt_fold(J);  /* Emit pending guard. */

1818. /* fallthrough */

1819. case LJ_POST_FIXBOOL:

1820. if (!tvistruecond(&J2G(J)->tmptv2)) {

1821. BCReg s;

1822. TValue *tv = J->L->base;

1823. for (s = 0; s < J->maxslot; s++)  /* Fixup stack slot (if any). */

1824. if (J->base[s] == TREF_TRUE && tvisfalse(&tv[s])) {

1825. J->base[s] = TREF_FALSE;

1826. break;

1827. }

1828. }

1829. break;

1830. case LJ_POST_FIXCONST:

1831. {

1832. BCReg s;

1833. TValue *tv = J->L->base;

1834. for (s = 0; s < J->maxslot; s++)  /* Constify stack slots (if any). */

1835. if (J->base[s] == TREF_NIL && !tvisnil(&tv[s]))

1836. J->base[s] = lj_record_constify(J, &tv[s]);

1837. }

1838. break;

1839. case LJ_POST_FFRETRY:  /* Suppress recording of retried fast function. */

1840. if (bc_op(*J->pc) >= BC__MAX)

1841. return;

1842. break;

1843. default: lua_assert(0); break;

1844. }

1845. J->postproc = LJ_POST_NONE;

1846. }

1848. /* Need snapshot before recording next bytecode (e.g. after a store). */

1849. if (J->needsnap) {

1850. J->needsnap = 0;

1851. lj_snap_purge(J);

1852. lj_snap_add(J);

1853. J->mergesnap = 1;

1854. }

1856. /* Skip some bytecodes. */

1857. if (LJ_UNLIKELY(J->bcskip > 0)) {

1858. J->bcskip--;

1859. return;

1860. }

1862. /* Record only closed loops for root traces. */

1863. pc = J->pc;

1864. if (J->framedepth == 0 &&

1865. (MSize)((char *)pc - (char *)J->bc_min) >= J->bc_extent)

1866. lj_trace_err(J, LJ_TRERR_LLEAVE);

1868. #ifdef LUA_USE_ASSERT

1869. rec_check_slots(J);

1870. rec_check_ir(J);

1871. #endif

1873. #if LJ_HASPROFILE

1874. rec_profile_ins(J, pc);

1875. #endif

1877. /* Keep a copy of the runtime values of var/num/str operands. */

1878. ‌#define rav        (&ix.valv)

1879. ‌#define rbv        (&ix.tabv)

1880. ‌#define rcv        (&ix.keyv)

1882. lbase = J->L->base;

1883. ins = *pc;

1884. op = bc_op(ins);

1885. ra = bc_a(ins);

1886. ix.val = 0;

1887. switch (bcmode_a(op)) {

1888. case BCMvar:

1889. copyTV(J->L, rav, &lbase[ra]); ix.val = ra = getslot(J, ra); break;

1890. default: break;  /* Handled later. */

1891. }

1892. rb = bc_b(ins);

1893. rc = bc_c(ins);

1894. switch (bcmode_b(op)) {

1895. case BCMnone: rb = 0; rc = bc_d(ins); break;  /* Upgrade rc to 'rd'. */

1896. case BCMvar:

1897. copyTV(J->L, rbv, &lbase[rb]); ix.tab = rb = getslot(J, rb); break;

1898. default: break;  /* Handled later. */

1899. }

1900. switch (bcmode_c(op)) {

1901. case BCMvar:

1902. copyTV(J->L, rcv, &lbase[rc]); ix.key = rc = getslot(J, rc); break;

1903. case BCMpri: setpriV(rcv, ~rc); ix.key = rc = TREF_PRI(IRT_NIL+rc); break;

1904. case BCMnum: { cTValue *tv = proto_knumtv(J->pt, rc);

1905. copyTV(J->L, rcv, tv); ix.key = rc = tvisint(tv) ? lj_ir_kint(J, intV(tv)) :

1906. lj_ir_knumint(J, numV(tv)); } break;

1907. case BCMstr: { GCstr *s = gco2str(proto_kgc(J->pt, ~(ptrdiff_t)rc));

1908. setstrV(J->L, rcv, s); ix.key = rc = lj_ir_kstr(J, s); } break;

1909. default: break;  /* Handled later. */

1910. }

1912. switch (op) {

1914. /* -- Comparison ops ---------------------------------------------------- */

1916. case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:

1917. #if LJ_HASFFI

1918. if (tref_iscdata(ra) || tref_iscdata(rc)) {

1919. rec_mm_comp_cdata(J, &ix, op, ((int)op & 2) ? MM_le : MM_lt);

1920. break;

1921. }

1922. #endif

1923. /* Emit nothing for two numeric or string consts. */

1924. if (!(tref_isk2(ra,rc) && tref_isnumber_str(ra) && tref_isnumber_str(rc))) {

1925. IRType ta = tref_isinteger(ra) ? IRT_INT : tref_type(ra);

1926. IRType tc = tref_isinteger(rc) ? IRT_INT : tref_type(rc);

1927. int irop;

1928. if (ta != tc) {

1929. /* Widen mixed number/int comparisons to number/number comparison. */

1930. if (ta == IRT_INT && tc == IRT_NUM) {

1931. ra = emitir(IRTN(IR_CONV), ra, IRCONV_NUM_INT);

1932. ta = IRT_NUM;

1933. } else if (ta == IRT_NUM && tc == IRT_INT) {

1934. rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT);

1935. } else if (LJ_52) {

1936. ta = IRT_NIL;  /* Force metamethod for different types. */

1937. } else if (!((ta == IRT_FALSE || ta == IRT_TRUE) &&

1938. (tc == IRT_FALSE || tc == IRT_TRUE))) {

1939. break;  /* Interpreter will throw for two different types. */

1940. }

1941. }

1942. rec_comp_prep(J);

1943. irop = (int)op - (int)BC_ISLT + (int)IR_LT;

1944. if (ta == IRT_NUM) {

1945. if ((irop & 1)) irop ^= 4;  /* ISGE/ISGT are unordered. */

1946. if (!lj_ir_numcmp(numberVnum(rav), numberVnum(rcv), (IROp)irop))

1947. irop ^= 5;

1948. } else if (ta == IRT_INT) {

1949. if (!lj_ir_numcmp(numberVnum(rav), numberVnum(rcv), (IROp)irop))

1950. irop ^= 1;

1951. } else if (ta == IRT_STR) {

1952. if (!lj_ir_strcmp(strV(rav), strV(rcv), (IROp)irop)) irop ^= 1;

1953. ra = lj_ir_call(J, IRCALL_lj_str_cmp, ra, rc);

1954. rc = lj_ir_kint(J, 0);

1955. ta = IRT_INT;

1956. } else {

1957. rec_mm_comp(J, &ix, (int)op);

1958. break;

1959. }

1960. emitir(IRTG(irop, ta), ra, rc);

1961. rec_comp_fixup(J, J->pc, ((int)op ^ irop) & 1);

1962. }

1963. break;

1965. case BC_ISEQV: case BC_ISNEV:

1966. case BC_ISEQS: case BC_ISNES:

1967. case BC_ISEQN: case BC_ISNEN:

1968. case BC_ISEQP: case BC_ISNEP:

1969. #if LJ_HASFFI

1970. if (tref_iscdata(ra) || tref_iscdata(rc)) {

1971. rec_mm_comp_cdata(J, &ix, op, MM_eq);

1972. break;

1973. }

1974. #endif

1975. /* Emit nothing for two non-table, non-udata consts. */

1976. if (!(tref_isk2(ra, rc) && !(tref_istab(ra) || tref_isudata(ra)))) {

1977. int diff;

1978. rec_comp_prep(J);

1979. diff = lj_record_objcmp(J, ra, rc, rav, rcv);

1980. if (diff == 2 || !(tref_istab(ra) || tref_isudata(ra)))

1981. rec_comp_fixup(J, J->pc, ((int)op & 1) == !diff);

1982. else if (diff == 1)  /* Only check __eq if different, but same type. */

1983. rec_mm_equal(J, &ix, (int)op);

1984. }

1985. break;

1987. /* -- Unary test and copy ops ------------------------------------------- */

1989. case BC_ISTC: case BC_ISFC:

1990. if ((op & 1) == tref_istruecond(rc))

1991. rc = 0;  /* Don't store if condition is not true. */

1992. /* fallthrough */

1993. case BC_IST: case BC_ISF:  /* Type specialization suffices. */

1994. if (bc_a(pc[1]) < J->maxslot)

1995. J->maxslot = bc_a(pc[1]);  /* Shrink used slots. */

1996. break;

1998. case BC_ISTYPE: case BC_ISNUM:

1999. /* These coercions need to correspond with lj_meta_istype(). */

2000. if (LJ_DUALNUM && rc == ~LJ_TNUMX+1)

2001. ra = lj_opt_narrow_toint(J, ra);

2002. else if (rc == ~LJ_TNUMX+2)

2003. ra = lj_ir_tonum(J, ra);

2004. else if (rc == ~LJ_TSTR+1)

2005. ra = lj_ir_tostr(J, ra);

2006. /* else: type specialization suffices. */

2007. J->base[bc_a(ins)] = ra;

2008. break;

2010. /* -- Unary ops --------------------------------------------------------- */

2012. case BC_NOT:

2013. /* Type specialization already forces const result. */

2014. rc = tref_istruecond(rc) ? TREF_FALSE : TREF_TRUE;

2015. break;

2017. case BC_LEN:

2018. if (tref_isstr(rc))

2019. rc = emitir(IRTI(IR_FLOAD), rc, IRFL_STR_LEN);

2020. else if (!LJ_52 && tref_istab(rc))

2021. rc = lj_ir_call(J, IRCALL_lj_tab_len, rc);

2022. else

2023. rc = rec_mm_len(J, rc, rcv);

2024. break;

2026. /* -- Arithmetic ops ---------------------------------------------------- */

2028. case BC_UNM:

2029. if (tref_isnumber_str(rc)) {

2030. rc = lj_opt_narrow_unm(J, rc, rcv);

2031. } else {

2032. ix.tab = rc;

2033. copyTV(J->L, &ix.tabv, rcv);

2034. rc = rec_mm_arith(J, &ix, MM_unm);

2035. }

2036. break;

2038. case BC_ADDNV: case BC_SUBNV: case BC_MULNV: case BC_DIVNV: case BC_MODNV:

2039. /* Swap rb/rc and rbv/rcv. rav is temp. */

2040. ix.tab = rc; ix.key = rc = rb; rb = ix.tab;

2041. copyTV(J->L, rav, rbv);

2042. copyTV(J->L, rbv, rcv);

2043. copyTV(J->L, rcv, rav);

2044. if (op == BC_MODNV)

2045. goto recmod;

2046. /* fallthrough */

2047. case BC_ADDVN: case BC_SUBVN: case BC_MULVN: case BC_DIVVN:

2048. case BC_ADDVV: case BC_SUBVV: case BC_MULVV: case BC_DIVVV: {

2049. MMS mm = bcmode_mm(op);

2050. if (tref_isnumber_str(rb) && tref_isnumber_str(rc))

2051. rc = lj_opt_narrow_arith(J, rb, rc, rbv, rcv,

2052. (int)mm - (int)MM_add + (int)IR_ADD);

2053. else

2054. rc = rec_mm_arith(J, &ix, mm);

2055. break;

2056. }

2058. case BC_MODVN: case BC_MODVV:

2059. recmod:

2060. if (tref_isnumber_str(rb) && tref_isnumber_str(rc))

2061. rc = lj_opt_narrow_mod(J, rb, rc, rcv);

2062. else

2063. rc = rec_mm_arith(J, &ix, MM_mod);

2064. break;

2066. case BC_POW:

2067. if (tref_isnumber_str(rb) && tref_isnumber_str(rc))

2068. rc = lj_opt_narrow_pow(J, lj_ir_tonum(J, rb), rc, rcv);

2069. else

2070. rc = rec_mm_arith(J, &ix, MM_pow);

2071. break;

2073. /* -- Miscellaneous ops ------------------------------------------------- */

2075. case BC_CAT:

2076. rc = rec_cat(J, rb, rc);

2077. break;

2079. /* -- Constant and move ops --------------------------------------------- */

2081. case BC_MOV:

2082. /* Clear gap of method call to avoid resurrecting previous refs. */

2083. if (ra > J->maxslot) J->base[ra-1] = 0;

2084. break;

2085. case BC_KSTR: case BC_KNUM: case BC_KPRI:

2086. break;

2087. case BC_KSHORT:

2088. rc = lj_ir_kint(J, (int32_t)(int16_t)rc);

2089. break;

2090. case BC_KNIL:

2091. while (ra <= rc)

2092. J->base[ra++] = TREF_NIL;

2093. if (rc >= J->maxslot) J->maxslot = rc+1;

2094. break;

2095. #if LJ_HASFFI

2096. case BC_KCDATA:

2097. rc = lj_ir_kgc(J, proto_kgc(J->pt, ~(ptrdiff_t)rc), IRT_CDATA);

2098. break;

2099. #endif

2101. /* -- Upvalue and function ops ------------------------------------------ */

2103. case BC_UGET:

2104. rc = rec_upvalue(J, rc, 0);

2105. break;

2106. case BC_USETV: case BC_USETS: case BC_USETN: case BC_USETP:

2107. rec_upvalue(J, ra, rc);

2108. break;

2110. /* -- Table ops --------------------------------------------------------- */

2112. case BC_GGET: case BC_GSET:

2113. settabV(J->L, &ix.tabv, tabref(J->fn->l.env));

2114. ix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), getcurrf(J), IRFL_FUNC_ENV);

2115. ix.idxchain = LJ_MAX_IDXCHAIN;

2116. rc = lj_record_idx(J, &ix);

2117. break;

2119. case BC_TGETB: case BC_TSETB:

2120. setintV(&ix.keyv, (int32_t)rc);

2121. ix.key = lj_ir_kint(J, (int32_t)rc);

2122. /* fallthrough */

2123. case BC_TGETV: case BC_TGETS: case BC_TSETV: case BC_TSETS:

2124. ix.idxchain = LJ_MAX_IDXCHAIN;

2125. rc = lj_record_idx(J, &ix);

2126. break;

2127. case BC_TGETR: case BC_TSETR:

2128. ix.idxchain = 0;

2129. rc = lj_record_idx(J, &ix);

2130. break;

2132. case BC_TSETM:

2133. rec_tsetm(J, ra, (BCReg)(J->L->top - J->L->base), (int32_t)rcv->u32.lo);

2134. break;

2136. case BC_TNEW:

2137. rc = rec_tnew(J, rc);

2138. break;

2139. case BC_TDUP:

2140. rc = emitir(IRTG(IR_TDUP, IRT_TAB),

2141. lj_ir_ktab(J, gco2tab(proto_kgc(J->pt, ~(ptrdiff_t)rc))), 0);

2142. break;

2144. /* -- Calls and vararg handling ----------------------------------------- */

2146. case BC_ITERC:

2147. J->base[ra] = getslot(J, ra-3-LJ_FR2);

2148. J->base[ra+1] = getslot(J, ra-2-LJ_FR2);

2149. J->base[ra+2] = getslot(J, ra-1-LJ_FR2);

2150. { /* Do the actual copy now because lj_record_call needs the values. */

2151. TValue *b = &J->L->base[ra];

2152. copyTV(J->L, b, b-3-LJ_FR2);

2153. copyTV(J->L, b+1, b-2-LJ_FR2);

2154. copyTV(J->L, b+2, b-1-LJ_FR2);

2155. }

2156. lj_record_call(J, ra, (ptrdiff_t)rc-1);

2157. break;

2159. /* L->top is set to L->base+ra+rc+NARGS-1+1. See lj_dispatch_ins(). */

2160. case BC_CALLM:

2161. rc = (BCReg)(J->L->top - J->L->base) - ra - LJ_FR2;

2162. /* fallthrough */

2163. case BC_CALL:

2164. lj_record_call(J, ra, (ptrdiff_t)rc-1);

2165. break;

2167. case BC_CALLMT:

2168. rc = (BCReg)(J->L->top - J->L->base) - ra - LJ_FR2;

2169. /* fallthrough */

2170. case BC_CALLT:

2171. lj_record_tailcall(J, ra, (ptrdiff_t)rc-1);

2172. break;

2174. case BC_VARG:

2175. rec_varg(J, ra, (ptrdiff_t)rb-1);

2176. break;

2178. /* -- Returns ----------------------------------------------------------- */

2180. case BC_RETM:

2181. /* L->top is set to L->base+ra+rc+NRESULTS-1, see lj_dispatch_ins(). */

2182. rc = (BCReg)(J->L->top - J->L->base) - ra + 1;

2183. /* fallthrough */

2184. case BC_RET: case BC_RET0: case BC_RET1:

2185. #if LJ_HASPROFILE

2186. rec_profile_ret(J);

2187. #endif

2188. lj_record_ret(J, ra, (ptrdiff_t)rc-1);

2189. break;

2191. /* -- Loops and branches ------------------------------------------------ */

2193. case BC_FORI:

2194. if (rec_for(J, pc, 0) != LOOPEV_LEAVE)

2195. J->loopref = J->cur.nins;

2196. break;

2197. case BC_JFORI:

2198. lua_assert(bc_op(pc[(ptrdiff_t)rc-BCBIAS_J]) == BC_JFORL);

2199. if (rec_for(J, pc, 0) != LOOPEV_LEAVE)  /* Link to existing loop. */

2200. lj_record_stop(J, LJ_TRLINK_ROOT, bc_d(pc[(ptrdiff_t)rc-BCBIAS_J]));

2201. /* Continue tracing if the loop is not entered. */

2202. break;

2204. case BC_FORL:

2205. rec_loop_interp(J, pc, rec_for(J, pc+((ptrdiff_t)rc-BCBIAS_J), 1));

2206. break;

2207. case BC_ITERL:

2208. rec_loop_interp(J, pc, rec_iterl(J, *pc));

2209. break;

2210. case BC_LOOP:

2211. rec_loop_interp(J, pc, rec_loop(J, ra));

2212. break;

2214. case BC_JFORL:

2215. rec_loop_jit(J, rc, rec_for(J, pc+bc_j(traceref(J, rc)->startins), 1));

2216. break;

2217. case BC_JITERL:

2218. rec_loop_jit(J, rc, rec_iterl(J, traceref(J, rc)->startins));

2219. break;

2220. case BC_JLOOP:

2221. rec_loop_jit(J, rc, rec_loop(J, ra));

2222. break;

2224. case BC_IFORL:

2225. case BC_IITERL:

2226. case BC_ILOOP:

2227. case BC_IFUNCF:

2228. case BC_IFUNCV:

2229. lj_trace_err(J, LJ_TRERR_BLACKL);

2230. break;

2232. case BC_JMP:

2233. if (ra < J->maxslot)

2234. J->maxslot = ra;  /* Shrink used slots. */

2235. break;

2237. /* -- Function headers -------------------------------------------------- */

2239. case BC_FUNCF:

2240. rec_func_lua(J);

2241. break;

2242. case BC_JFUNCF:

2243. rec_func_jit(J, rc);

2244. break;

2246. case BC_FUNCV:

2247. rec_func_vararg(J);

2248. rec_func_lua(J);

2249. break;

2250. case BC_JFUNCV:

2251. lua_assert(0);  /* Cannot happen. No hotcall counting for varag funcs. */

2252. break;

2254. case BC_FUNCC:

2255. case BC_FUNCCW:

2256. lj_ffrecord_func(J);

2257. break;

2259. default:

2260. if (op >= BC__MAX) {

2261. lj_ffrecord_func(J);

2262. break;

2263. }

2264. /* fallthrough */

2265. case BC_ITERN:

2266. case BC_ISNEXT:

2267. case BC_UCLO:

2268. case BC_FNEW:

2269. setintV(&J->errinfo, (int32_t)op);

2270. lj_trace_err_info(J, LJ_TRERR_NYIBC);

2271. break;

2272. }

2274. /* rc == 0 if we have no result yet, e.g. pending __index metamethod call. */

2275. if (bcmode_a(op) == BCMdst && rc) {

2276. J->base[ra] = rc;

2277. if (ra >= J->maxslot) J->maxslot = ra+1;

2278. }

2280. ‌#undef rav

2281. ‌#undef rbv

2282. ‌#undef rcv

2284. /* Limit the number of recorded IR instructions. */

2285. if (J->cur.nins > REF_FIRST+(IRRef)J->param[JIT_P_maxrecord])

2286. lj_trace_err(J, LJ_TRERR_TRACEOV);

2287. }

2289. /* -- Recording setup ----------------------------------------------------- */

2291. /* Setup recording for a root trace started by a hot loop. */

2292. ‌static const BCIns *rec_setup_root(jit_State *J)

2293. {

2294. /* Determine the next PC and the bytecode range for the loop. */

2295. const BCIns *pcj, *pc = J->pc;

2296. BCIns ins = *pc;

2297. BCReg ra = bc_a(ins);

2298. switch (bc_op(ins)) {

2299. case BC_FORL:

2300. J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns);

2301. pc += 1+bc_j(ins);

2302. J->bc_min = pc;

2303. break;

2304. case BC_ITERL:

2305. lua_assert(bc_op(pc[-1]) == BC_ITERC);

2306. J->maxslot = ra + bc_b(pc[-1]) - 1;

2307. J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns);

2308. pc += 1+bc_j(ins);

2309. lua_assert(bc_op(pc[-1]) == BC_JMP);

2310. J->bc_min = pc;

2311. break;

2312. case BC_LOOP:

2313. /* Only check BC range for real loops, but not for "repeat until true". */

2314. pcj = pc + bc_j(ins);

2315. ins = *pcj;

2316. if (bc_op(ins) == BC_JMP && bc_j(ins) < 0) {

2317. J->bc_min = pcj+1 + bc_j(ins);

2318. J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns);

2319. }

2320. J->maxslot = ra;

2321. pc++;

2322. break;

2323. case BC_RET:

2324. case BC_RET0:

2325. case BC_RET1:

2326. /* No bytecode range check for down-recursive root traces. */

2327. J->maxslot = ra + bc_d(ins) - 1;

2328. break;

2329. case BC_FUNCF:

2330. /* No bytecode range check for root traces started by a hot call. */

2331. J->maxslot = J->pt->numparams;

2332. pc++;

2333. break;

2334. case BC_CALLM:

2335. case BC_CALL:

2336. case BC_ITERC:

2337. /* No bytecode range check for stitched traces. */

2338. pc++;

2339. break;

2340. default:

2341. lua_assert(0);

2342. break;

2343. }

2344. return pc;

2345. }

2347. /* Setup for recording a new trace. */

2348. ‌void lj_record_setup(jit_State *J)

2349. {

2350. uint32_t i;

2352. /* Initialize state related to current trace. */

2353. memset(J->slot, 0, sizeof(J->slot));

2354. memset(J->chain, 0, sizeof(J->chain));

2355. memset(J->bpropcache, 0, sizeof(J->bpropcache));

2356. J->scev.idx = REF_NIL;

2357. setmref(J->scev.pc, NULL);

2359. J->baseslot = 1;  /* Invoking function is at base[-1]. */

2360. J->base = J->slot + J->baseslot;

2361. J->maxslot = 0;

2362. J->framedepth = 0;

2363. J->retdepth = 0;

2365. J->instunroll = J->param[JIT_P_instunroll];

2366. J->loopunroll = J->param[JIT_P_loopunroll];

2367. J->tailcalled = 0;

2368. J->loopref = 0;

2370. J->bc_min = NULL;  /* Means no limit. */

2371. J->bc_extent = ~(MSize)0;

2373. /* Emit instructions for fixed references. Also triggers initial IR alloc. */

2374. emitir_raw(IRT(IR_BASE, IRT_P32), J->parent, J->exitno);

2375. for (i = 0; i <= 2; i++) {

2376. IRIns *ir = IR(REF_NIL-i);

2377. ir->i = 0;

2378. ir->t.irt = (uint8_t)(IRT_NIL+i);

2379. ir->o = IR_KPRI;

2380. ir->prev = 0;

2381. }

2382. J->cur.nk = REF_TRUE;

2384. J->startpc = J->pc;

2385. setmref(J->cur.startpc, J->pc);

2386. if (J->parent) {  /* Side trace. */

2387. GCtrace *T = traceref(J, J->parent);

2388. TraceNo root = T->root ? T->root : J->parent;

2389. J->cur.root = (uint16_t)root;

2390. J->cur.startins = BCINS_AD(BC_JMP, 0, 0);

2391. /* Check whether we could at least potentially form an extra loop. */

2392. if (J->exitno == 0 && T->snap[0].nent == 0) {

2393. /* We can narrow a FORL for some side traces, too. */

2394. if (J->pc > proto_bc(J->pt) && bc_op(J->pc[-1]) == BC_JFORI &&

2395. bc_d(J->pc[bc_j(J->pc[-1])-1]) == root) {

2396. lj_snap_add(J);

2397. rec_for_loop(J, J->pc-1, &J->scev, 1);

2398. goto sidecheck;

2399. }

2400. } else {

2401. J->startpc = NULL;  /* Prevent forming an extra loop. */

2402. }

2403. lj_snap_replay(J, T);

2404. sidecheck:

2405. if (traceref(J, J->cur.root)->nchild >= J->param[JIT_P_maxside] ||

2406. T->snap[J->exitno].count >= J->param[JIT_P_hotexit] +

2407. J->param[JIT_P_tryside]) {

2408. lj_record_stop(J, LJ_TRLINK_INTERP, 0);

2409. }

2410. } else {  /* Root trace. */

2411. J->cur.root = 0;

2412. J->cur.startins = *J->pc;

2413. J->pc = rec_setup_root(J);

2414. /* Note: the loop instruction itself is recorded at the end and not

2415. ** at the start! So snapshot #0 needs to point to the *next* instruction.

2416. */

2417. lj_snap_add(J);

2418. if (bc_op(J->cur.startins) == BC_FORL)

2419. rec_for_loop(J, J->pc-1, &J->scev, 1);

2420. else if (bc_op(J->cur.startins) == BC_ITERC)

2421. J->startpc = NULL;

2422. if (1 + J->pt->framesize >= LJ_MAX_JSLOTS)

2423. lj_trace_err(J, LJ_TRERR_STACKOV);

2424. }

2425. #if LJ_HASPROFILE

2426. J->prev_pt = NULL;

2427. J->prev_line = -1;

2428. #endif

2429. #ifdef LUAJIT_ENABLE_CHECKHOOK

2430. /* Regularly check for instruction/line hooks from compiled code and

2431. ** exit to the interpreter if the hooks are set.

2432. **

2433. ** This is a compile-time option and disabled by default, since the

2434. ** hook checks may be quite expensive in tight loops.

2435. **

2436. ** Note this is only useful if hooks are *not* set most of the time.

2437. ** Use this only if you want to *asynchronously* interrupt the execution.

2438. **

2439. ** You can set the instruction hook via lua_sethook() with a count of 1

2440. ** from a signal handler or another native thread. Please have a look

2441. ** at the first few functions in luajit.c for an example (Ctrl-C handler).

2442. */

2443. {

2444. TRef tr = emitir(IRT(IR_XLOAD, IRT_U8),

2445. lj_ir_kptr(J, &J2G(J)->hookmask), IRXLOAD_VOLATILE);

2446. tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (LUA_MASKLINE|LUA_MASKCOUNT)));

2447. emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0));

2448. }

2449. #endif

2450. }

2452. ‌#undef IR

2453. ‌#undef emitir_raw

2454. ‌#undef emitir

2456. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_record.h - luajit-2.0-src

Data types defined

- RecordIndex

- RecordIndex

Macros defined

- _LJ_RECORD_H

Source code

1. /*

2. ** Trace recorder (bytecode -> SSA IR).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_RECORD_H

7. ‌#define _LJ_RECORD_H

9. #include "lj_obj.h"

10. #include "lj_jit.h"

12. #if LJ_HASJIT

13. /* Context for recording an indexed load/store. */

14. ‌typedef struct RecordIndex {

15. TValue tabv;                /* Runtime value of table (or indexed object). */

16. TValue keyv;                /* Runtime value of key. */

17. TValue valv;                /* Runtime value of stored value. */

18. TValue mobjv;                /* Runtime value of metamethod object. */

19. GCtab *mtv;                /* Runtime value of metatable object. */

20. cTValue *oldv;        /* Runtime value of previously stored value. */

21. TRef tab;                /* Table (or indexed object) reference. */

22. TRef key;                /* Key reference. */

23. TRef val;                /* Value reference for a store or 0 for a load. */

24. TRef mt;                /* Metatable reference. */

25. TRef mobj;                /* Metamethod object reference. */

26. int idxchain;                /* Index indirections left or 0 for raw lookup. */

27. ‌} RecordIndex;

29. LJ_FUNC int lj_record_objcmp(jit_State *J, TRef a, TRef b,

30. cTValue *av, cTValue *bv);

31. LJ_FUNC void lj_record_stop(jit_State *J, TraceLink linktype, TraceNo lnk);

32. LJ_FUNC TRef lj_record_constify(jit_State *J, cTValue *o);

34. LJ_FUNC void lj_record_call(jit_State *J, BCReg func, ptrdiff_t nargs);

35. LJ_FUNC void lj_record_tailcall(jit_State *J, BCReg func, ptrdiff_t nargs);

36. LJ_FUNC void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults);

38. LJ_FUNC int lj_record_mm_lookup(jit_State *J, RecordIndex *ix, MMS mm);

39. LJ_FUNC TRef lj_record_idx(jit_State *J, RecordIndex *ix);

41. LJ_FUNC void lj_record_ins(jit_State *J);

42. LJ_FUNC void lj_record_setup(jit_State *J);

43. #endif

45. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_crecord.c - luajit-2.0-src

Data types defined

- CRecMemList

- CRecMemList

Functions defined

- argv2cdata

- argv2ctype

- crec_alloc

- crec_arith_int64

- crec_arith_meta

- crec_arith_ptr

- crec_bit64_type

- crec_call

- crec_call_args

- crec_constructor

- crec_copy

- crec_copy_emit

- crec_copy_struct

- crec_copy_unroll

- crec_ct2irt

- crec_ct_ct

- crec_ct_tv

- crec_fill

- crec_fill_emit

- crec_fill_unroll

- crec_finalizer

- crec_index_meta

- crec_isnonzero

- crec_reassoc_ofs

- crec_snap_caller

- crec_toint

- crec_tv_ct

- lj_crecord_tonumber

- recff_bit64_nary

- recff_bit64_shift

- recff_bit64_tobit

- recff_bit64_tohex

- recff_bit64_unary

- recff_cdata_arith

- recff_cdata_call

- recff_cdata_index

- recff_clib_index

- recff_ffi_abi

- recff_ffi_copy

- recff_ffi_errno

- recff_ffi_fill

- recff_ffi_gc

- recff_ffi_istype

- recff_ffi_new

- recff_ffi_string

- recff_ffi_typeof

- recff_ffi_xof

Macros defined

- CREC_COPY_MAXLEN

- CREC_COPY_MAXUNROLL

- CREC_COPY_REGWIN

- CREC_COPY_REGWIN

- CREC_COPY_REGWIN

- CREC_FILL_MAXUNROLL

- IR

- IR

- LUA_CORE

- emitconv

- emitconv

- emitir

- emitir

- lj_ffrecord_c

Source code

1. /*

2. ** Trace recorder for C data operations.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_ffrecord_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT && LJ_HASFFI

13. #include "lj_err.h"

14. #include "lj_tab.h"

15. #include "lj_frame.h"

16. #include "lj_ctype.h"

17. #include "lj_cdata.h"

18. #include "lj_cparse.h"

19. #include "lj_cconv.h"

20. #include "lj_carith.h"

21. #include "lj_clib.h"

22. #include "lj_ccall.h"

23. #include "lj_ff.h"

24. #include "lj_ir.h"

25. #include "lj_jit.h"

26. #include "lj_ircall.h"

27. #include "lj_iropt.h"

28. #include "lj_trace.h"

29. #include "lj_record.h"

30. #include "lj_ffrecord.h"

31. #include "lj_snap.h"

32. #include "lj_crecord.h"

33. #include "lj_dispatch.h"

34. #include "lj_strfmt.h"

36. /* Some local macros to save typing. Undef'd at the end. */

37. ‌#define IR(ref)                        (&J->cur.ir[(ref)])

39. /* Pass IR on to next optimization in chain (FOLD). */

40. ‌#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))

42. ‌#define emitconv(a, dt, st, flags) \

43. emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags))

45. /* -- C type checks ------------------------------------------------------- */

47. ‌static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o)

48. {

49. GCcdata *cd;

50. TRef trtypeid;

51. if (!tref_iscdata(tr))

52. lj_trace_err(J, LJ_TRERR_BADTYPE);

53. cd = cdataV(o);

54. /* Specialize to the CTypeID. */

55. trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_CTYPEID);

56. emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->ctypeid));

57. return cd;

58. }

60. /* Specialize to the CTypeID held by a cdata constructor. */

61. ‌static CTypeID crec_constructor(jit_State *J, GCcdata *cd, TRef tr)

62. {

63. CTypeID id;

64. lua_assert(tref_iscdata(tr) && cd->ctypeid == CTID_CTYPEID);

65. id = *(CTypeID *)cdataptr(cd);

66. tr = emitir(IRT(IR_FLOAD, IRT_INT), tr, IRFL_CDATA_INT);

67. emitir(IRTG(IR_EQ, IRT_INT), tr, lj_ir_kint(J, (int32_t)id));

68. return id;

69. }

71. ‌static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o)

72. {

73. if (tref_isstr(tr)) {

74. GCstr *s = strV(o);

75. CPState cp;

76. CTypeID oldtop;

77. /* Specialize to the string containing the C type declaration. */

78. emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s));

79. cp.L = J->L;

80. cp.cts = ctype_ctsG(J2G(J));

81. oldtop = cp.cts->top;

82. cp.srcname = strdata(s);

83. cp.p = strdata(s);

84. cp.param = NULL;

85. cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;

86. if (lj_cparse(&cp) || cp.cts->top > oldtop)  /* Avoid new struct defs. */

87. lj_trace_err(J, LJ_TRERR_BADTYPE);

88. return cp.val.id;

89. } else {

90. GCcdata *cd = argv2cdata(J, tr, o);

91. return cd->ctypeid == CTID_CTYPEID ? crec_constructor(J, cd, tr) :

92. cd->ctypeid;

93. }

94. }

96. /* Convert CType to IRType (if possible). */

97. ‌static IRType crec_ct2irt(CTState *cts, CType *ct)

98. {

99. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);

100. if (LJ_LIKELY(ctype_isnum(ct->info))) {

101. if ((ct->info & CTF_FP)) {

102. if (ct->size == sizeof(double))

103. return IRT_NUM;

104. else if (ct->size == sizeof(float))

105. return IRT_FLOAT;

106. } else {

107. uint32_t b = lj_fls(ct->size);

108. if (b <= 3)

109. return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0);

110. }

111. } else if (ctype_isptr(ct->info)) {

112. return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;

113. } else if (ctype_iscomplex(ct->info)) {

114. if (ct->size == 2*sizeof(double))

115. return IRT_NUM;

116. else if (ct->size == 2*sizeof(float))

117. return IRT_FLOAT;

118. }

119. return IRT_CDATA;

120. }

122. /* -- Optimized memory fill and copy -------------------------------------- */

124. /* Maximum length and unroll of inlined copy/fill. */

125. ‌#define CREC_COPY_MAXUNROLL                16

126. ‌#define CREC_COPY_MAXLEN                128

128. ‌#define CREC_FILL_MAXUNROLL                16

130. /* Number of windowed registers used for optimized memory copy. */

131. #if LJ_TARGET_X86

132. ‌#define CREC_COPY_REGWIN                2

133. #elif LJ_TARGET_PPC || LJ_TARGET_MIPS

134. ‌#define CREC_COPY_REGWIN                8

135. #else

136. ‌#define CREC_COPY_REGWIN                4

137. #endif

139. /* List of memory offsets for copy/fill. */

140. ‌typedef struct CRecMemList {

141. CTSize ofs;                /* Offset in bytes. */

142. IRType tp;                /* Type of load/store. */

143. TRef trofs;                /* TRef of interned offset. */

144. TRef trval;                /* TRef of load value. */

145. ‌} CRecMemList;

147. /* Generate copy list for element-wise struct copy. */

148. ‌static MSize crec_copy_struct(CRecMemList *ml, CTState *cts, CType *ct)

149. {

150. CTypeID fid = ct->sib;

151. MSize mlp = 0;

152. while (fid) {

153. CType *df = ctype_get(cts, fid);

154. fid = df->sib;

155. if (ctype_isfield(df->info)) {

156. CType *cct;

157. IRType tp;

158. if (!gcref(df->name)) continue;  /* Ignore unnamed fields. */

159. cct = ctype_rawchild(cts, df);  /* Field type. */

160. tp = crec_ct2irt(cts, cct);

161. if (tp == IRT_CDATA) return 0;  /* NYI: aggregates. */

162. if (mlp >= CREC_COPY_MAXUNROLL) return 0;

163. ml[mlp].ofs = df->size;

164. ml[mlp].tp = tp;

165. mlp++;

166. if (ctype_iscomplex(cct->info)) {

167. if (mlp >= CREC_COPY_MAXUNROLL) return 0;

168. ml[mlp].ofs = df->size + (cct->size >> 1);

169. ml[mlp].tp = tp;

170. mlp++;

171. }

172. } else if (!ctype_isconstval(df->info)) {

173. /* NYI: bitfields and sub-structures. */

174. return 0;

175. }

176. }

177. return mlp;

178. }

180. /* Generate unrolled copy list, from highest to lowest step size/alignment. */

181. ‌static MSize crec_copy_unroll(CRecMemList *ml, CTSize len, CTSize step,

182. IRType tp)

183. {

184. CTSize ofs = 0;

185. MSize mlp = 0;

186. if (tp == IRT_CDATA) tp = IRT_U8 + 2*lj_fls(step);

187. do {

188. while (ofs + step <= len) {

189. if (mlp >= CREC_COPY_MAXUNROLL) return 0;

190. ml[mlp].ofs = ofs;

191. ml[mlp].tp = tp;

192. mlp++;

193. ofs += step;

194. }

195. step >>= 1;

196. tp -= 2;

197. } while (ofs < len);

198. return mlp;

199. }

201. /*

202. ** Emit copy list with windowed loads/stores.

203. ** LJ_TARGET_UNALIGNED: may emit unaligned loads/stores (not marked as such).

204. */

205. ‌static void crec_copy_emit(jit_State *J, CRecMemList *ml, MSize mlp,

206. TRef trdst, TRef trsrc)

207. {

208. MSize i, j, rwin = 0;

209. for (i = 0, j = 0; i < mlp; ) {

210. TRef trofs = lj_ir_kintp(J, ml[i].ofs);

211. TRef trsptr = emitir(IRT(IR_ADD, IRT_PTR), trsrc, trofs);

212. ml[i].trval = emitir(IRT(IR_XLOAD, ml[i].tp), trsptr, 0);

213. ml[i].trofs = trofs;

214. i++;

215. rwin += (LJ_SOFTFP && ml[i].tp == IRT_NUM) ? 2 : 1;

216. if (rwin >= CREC_COPY_REGWIN || i >= mlp) {  /* Flush buffered stores. */

217. rwin = 0;

218. for ( ; j < i; j++) {

219. TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, ml[j].trofs);

220. emitir(IRT(IR_XSTORE, ml[j].tp), trdptr, ml[j].trval);

221. }

222. }

223. }

224. }

226. /* Optimized memory copy. */

227. ‌static void crec_copy(jit_State *J, TRef trdst, TRef trsrc, TRef trlen,

228. CType *ct)

229. {

230. if (tref_isk(trlen)) {  /* Length must be constant. */

231. CRecMemList ml[CREC_COPY_MAXUNROLL];

232. MSize mlp = 0;

233. CTSize step = 1, len = (CTSize)IR(tref_ref(trlen))->i;

234. IRType tp = IRT_CDATA;

235. int needxbar = 0;

236. if (len == 0) return;  /* Shortcut. */

237. if (len > CREC_COPY_MAXLEN) goto fallback;

238. if (ct) {

239. CTState *cts = ctype_ctsG(J2G(J));

240. lua_assert(ctype_isarray(ct->info) || ctype_isstruct(ct->info));

241. if (ctype_isarray(ct->info)) {

242. CType *cct = ctype_rawchild(cts, ct);

243. tp = crec_ct2irt(cts, cct);

244. if (tp == IRT_CDATA) goto rawcopy;

245. step = lj_ir_type_size[tp];

246. lua_assert((len & (step-1)) == 0);

247. } else if ((ct->info & CTF_UNION)) {

248. step = (1u << ctype_align(ct->info));

249. goto rawcopy;

250. } else {

251. mlp = crec_copy_struct(ml, cts, ct);

252. goto emitcopy;

253. }

254. } else {

255. rawcopy:

256. needxbar = 1;

257. if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)

258. step = CTSIZE_PTR;

259. }

260. mlp = crec_copy_unroll(ml, len, step, tp);

261. emitcopy:

262. if (mlp) {

263. crec_copy_emit(J, ml, mlp, trdst, trsrc);

264. if (needxbar)

265. emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);

266. return;

267. }

268. }

269. fallback:

270. /* Call memcpy. Always needs a barrier to disable alias analysis. */

271. lj_ir_call(J, IRCALL_memcpy, trdst, trsrc, trlen);

272. emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);

273. }

275. /* Generate unrolled fill list, from highest to lowest step size/alignment. */

276. ‌static MSize crec_fill_unroll(CRecMemList *ml, CTSize len, CTSize step)

277. {

278. CTSize ofs = 0;

279. MSize mlp = 0;

280. IRType tp = IRT_U8 + 2*lj_fls(step);

281. do {

282. while (ofs + step <= len) {

283. if (mlp >= CREC_COPY_MAXUNROLL) return 0;

284. ml[mlp].ofs = ofs;

285. ml[mlp].tp = tp;

286. mlp++;

287. ofs += step;

288. }

289. step >>= 1;

290. tp -= 2;

291. } while (ofs < len);

292. return mlp;

293. }

295. /*

296. ** Emit stores for fill list.

297. ** LJ_TARGET_UNALIGNED: may emit unaligned stores (not marked as such).

298. */

299. ‌static void crec_fill_emit(jit_State *J, CRecMemList *ml, MSize mlp,

300. TRef trdst, TRef trfill)

301. {

302. MSize i;

303. for (i = 0; i < mlp; i++) {

304. TRef trofs = lj_ir_kintp(J, ml[i].ofs);

305. TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, trofs);

306. emitir(IRT(IR_XSTORE, ml[i].tp), trdptr, trfill);

307. }

308. }

310. /* Optimized memory fill. */

311. ‌static void crec_fill(jit_State *J, TRef trdst, TRef trlen, TRef trfill,

312. CTSize step)

313. {

314. if (tref_isk(trlen)) {  /* Length must be constant. */

315. CRecMemList ml[CREC_FILL_MAXUNROLL];

316. MSize mlp;

317. CTSize len = (CTSize)IR(tref_ref(trlen))->i;

318. if (len == 0) return;  /* Shortcut. */

319. if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)

320. step = CTSIZE_PTR;

321. if (step * CREC_FILL_MAXUNROLL < len) goto fallback;

322. mlp = crec_fill_unroll(ml, len, step);

323. if (!mlp) goto fallback;

324. if (tref_isk(trfill) || ml[0].tp != IRT_U8)

325. trfill = emitconv(trfill, IRT_INT, IRT_U8, 0);

326. if (ml[0].tp != IRT_U8) {  /* Scatter U8 to U16/U32/U64. */

327. if (CTSIZE_PTR == 8 && ml[0].tp == IRT_U64) {

328. if (tref_isk(trfill))  /* Pointless on x64 with zero-extended regs. */

329. trfill = emitconv(trfill, IRT_U64, IRT_U32, 0);

330. trfill = emitir(IRT(IR_MUL, IRT_U64), trfill,

331. lj_ir_kint64(J, U64x(01010101,01010101)));

332. } else {

333. trfill = emitir(IRTI(IR_MUL), trfill,

334. lj_ir_kint(J, ml[0].tp == IRT_U16 ? 0x0101 : 0x01010101));

335. }

336. }

337. crec_fill_emit(J, ml, mlp, trdst, trfill);

338. } else {

339. fallback:

340. /* Call memset. Always needs a barrier to disable alias analysis. */

341. lj_ir_call(J, IRCALL_memset, trdst, trfill, trlen);  /* Note: arg order! */

342. }

343. emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);

344. }

346. /* -- Convert C type to C type -------------------------------------------- */

348. /*

349. ** This code mirrors the code in lj_cconv.c. It performs the same steps

350. ** for the trace recorder that lj_cconv.c does for the interpreter.

351. **

352. ** One major difference is that we can get away with much fewer checks

353. ** here. E.g. checks for casts, constness or correct types can often be

354. ** omitted, even if they might fail. The interpreter subsequently throws

355. ** an error, which aborts the trace.

356. **

357. ** All operations are specialized to their C types, so the on-trace

358. ** outcome must be the same as the outcome in the interpreter. If the

359. ** interpreter doesn't throw an error, then the trace is correct, too.

360. ** Care must be taken not to generate invalid (temporary) IR or to

361. ** trigger asserts.

362. */

364. /* Determine whether a passed number or cdata number is non-zero. */

365. ‌static int crec_isnonzero(CType *s, void *p)

366. {

367. if (p == (void *)0)

368. return 0;

369. if (p == (void *)1)

370. return 1;

371. if ((s->info & CTF_FP)) {

372. if (s->size == sizeof(float))

373. return (*(float *)p != 0);

374. else

375. return (*(double *)p != 0);

376. } else {

377. if (s->size == 1)

378. return (*(uint8_t *)p != 0);

379. else if (s->size == 2)

380. return (*(uint16_t *)p != 0);

381. else if (s->size == 4)

382. return (*(uint32_t *)p != 0);

383. else

384. return (*(uint64_t *)p != 0);

385. }

386. }

388. ‌static TRef crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp,

389. void *svisnz)

390. {

391. IRType dt = crec_ct2irt(ctype_ctsG(J2G(J)), d);

392. IRType st = crec_ct2irt(ctype_ctsG(J2G(J)), s);

393. CTSize dsize = d->size, ssize = s->size;

394. CTInfo dinfo = d->info, sinfo = s->info;

396. if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)

397. goto err_conv;

399. /*

400. ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and

401. ** numbers up to 8 bytes. Otherwise sp holds a pointer.

402. */

404. switch (cconv_idx2(dinfo, sinfo)) {

405. /* Destination is a bool. */

406. case CCX(B, B):

407. goto xstore;  /* Source operand is already normalized. */

408. case CCX(B, I):

409. case CCX(B, F):

410. if (st != IRT_CDATA) {

411. /* Specialize to the result of a comparison against 0. */

412. TRef zero = (st == IRT_NUM  || st == IRT_FLOAT) ? lj_ir_knum(J, 0) :

413. (st == IRT_I64 || st == IRT_U64) ? lj_ir_kint64(J, 0) :

414. lj_ir_kint(J, 0);

415. int isnz = crec_isnonzero(s, svisnz);

416. emitir(IRTG(isnz ? IR_NE : IR_EQ, st), sp, zero);

417. sp = lj_ir_kint(J, isnz);

418. goto xstore;

419. }

420. goto err_nyi;

422. /* Destination is an integer. */

423. case CCX(I, B):

424. case CCX(I, I):

425. conv_I_I:

426. if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;

427. /* Extend 32 to 64 bit integer. */

428. if (dsize == 8 && ssize < 8 && !(LJ_64 && (sinfo & CTF_UNSIGNED)))

429. sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st,

430. (sinfo & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);

431. else if (dsize < 8 && ssize == 8)  /* Truncate from 64 bit integer. */

432. sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, 0);

433. else if (st == IRT_INT)

434. sp = lj_opt_narrow_toint(J, sp);

435. xstore:

436. if (dt == IRT_I64 || dt == IRT_U64) lj_needsplit(J);

437. if (dp == 0) return sp;

438. emitir(IRT(IR_XSTORE, dt), dp, sp);

439. break;

440. case CCX(I, C):

441. sp = emitir(IRT(IR_XLOAD, st), sp, 0);  /* Load re. */

442. /* fallthrough */

443. case CCX(I, F):

444. if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;

445. sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, IRCONV_ANY);

446. goto xstore;

447. case CCX(I, P):

448. case CCX(I, A):

449. sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);

450. ssize = CTSIZE_PTR;

451. st = IRT_UINTP;

452. if (((dsize ^ ssize) & 8) == 0) {  /* Must insert no-op type conversion. */

453. sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, IRT_PTR, 0);

454. goto xstore;

455. }

456. goto conv_I_I;

458. /* Destination is a floating-point number. */

459. case CCX(F, B):

460. case CCX(F, I):

461. conv_F_I:

462. if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;

463. sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, 0);

464. goto xstore;

465. case CCX(F, C):

466. sp = emitir(IRT(IR_XLOAD, st), sp, 0);  /* Load re. */

467. /* fallthrough */

468. case CCX(F, F):

469. conv_F_F:

470. if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;

471. if (dt != st) sp = emitconv(sp, dt, st, 0);

472. goto xstore;

474. /* Destination is a complex number. */

475. case CCX(C, I):

476. case CCX(C, F):

477. {  /* Clear im. */

478. TRef ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));

479. emitir(IRT(IR_XSTORE, dt), ptr, lj_ir_knum(J, 0));

480. }

481. /* Convert to re. */

482. if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I;

484. case CCX(C, C):

485. if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;

486. {

487. TRef re, im, ptr;

488. re = emitir(IRT(IR_XLOAD, st), sp, 0);

489. ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, (ssize >> 1)));

490. im = emitir(IRT(IR_XLOAD, st), ptr, 0);

491. if (dt != st) {

492. re = emitconv(re, dt, st, 0);

493. im = emitconv(im, dt, st, 0);

494. }

495. emitir(IRT(IR_XSTORE, dt), dp, re);

496. ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));

497. emitir(IRT(IR_XSTORE, dt), ptr, im);

498. }

499. break;

501. /* Destination is a vector. */

502. case CCX(V, I):

503. case CCX(V, F):

504. case CCX(V, C):

505. case CCX(V, V):

506. goto err_nyi;

508. /* Destination is a pointer. */

509. case CCX(P, P):

510. case CCX(P, A):

511. case CCX(P, S):

512. /* There are only 32 bit pointers/addresses on 32 bit machines.

513. ** Also ok on x64, since all 32 bit ops clear the upper part of the reg.

514. */

515. goto xstore;

516. case CCX(P, I):

517. if (st == IRT_CDATA) goto err_nyi;

518. if (!LJ_64 && ssize == 8)  /* Truncate from 64 bit integer. */

519. sp = emitconv(sp, IRT_U32, st, 0);

520. goto xstore;

521. case CCX(P, F):

522. if (st == IRT_CDATA) goto err_nyi;

523. /* The signed conversion is cheaper. x64 really has 47 bit pointers. */

524. sp = emitconv(sp, (LJ_64 && dsize == 8) ? IRT_I64 : IRT_U32,

525. st, IRCONV_ANY);

526. goto xstore;

528. /* Destination is an array. */

529. case CCX(A, A):

530. /* Destination is a struct/union. */

531. case CCX(S, S):

532. if (dp == 0) goto err_conv;

533. crec_copy(J, dp, sp, lj_ir_kint(J, dsize), d);

534. break;

536. default:

537. err_conv:

538. err_nyi:

539. lj_trace_err(J, LJ_TRERR_NYICONV);

540. break;

541. }

542. return 0;

543. }

545. /* -- Convert C type to TValue (load) ------------------------------------- */

547. ‌static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp)

548. {

549. CTState *cts = ctype_ctsG(J2G(J));

550. IRType t = crec_ct2irt(cts, s);

551. CTInfo sinfo = s->info;

552. if (ctype_isnum(sinfo)) {

553. TRef tr;

554. if (t == IRT_CDATA)

555. goto err_nyi;  /* NYI: copyval of >64 bit integers. */

556. tr = emitir(IRT(IR_XLOAD, t), sp, 0);

557. if (t == IRT_FLOAT || t == IRT_U32) {  /* Keep uint32_t/float as numbers. */

558. return emitconv(tr, IRT_NUM, t, 0);

559. } else if (t == IRT_I64 || t == IRT_U64) {  /* Box 64 bit integer. */

560. sp = tr;

561. lj_needsplit(J);

562. } else if ((sinfo & CTF_BOOL)) {

563. /* Assume not equal to zero. Fixup and emit pending guard later. */

564. lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));

565. J->postproc = LJ_POST_FIXGUARD;

566. return TREF_TRUE;

567. } else {

568. return tr;

569. }

570. } else if (ctype_isptr(sinfo) || ctype_isenum(sinfo)) {

571. sp = emitir(IRT(IR_XLOAD, t), sp, 0);  /* Box pointers and enums. */

572. } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {

573. cts->L = J->L;

574. sid = lj_ctype_intern(cts, CTINFO_REF(sid), CTSIZE_PTR);  /* Create ref. */

575. } else if (ctype_iscomplex(sinfo)) {  /* Unbox/box complex. */

576. ptrdiff_t esz = (ptrdiff_t)(s->size >> 1);

577. TRef ptr, tr1, tr2, dp;

578. dp = emitir(IRTG(IR_CNEW, IRT_CDATA), lj_ir_kint(J, sid), TREF_NIL);

579. tr1 = emitir(IRT(IR_XLOAD, t), sp, 0);

580. ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, esz));

581. tr2 = emitir(IRT(IR_XLOAD, t), ptr, 0);

582. ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)));

583. emitir(IRT(IR_XSTORE, t), ptr, tr1);

584. ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)+esz));

585. emitir(IRT(IR_XSTORE, t), ptr, tr2);

586. return dp;

587. } else {

588. /* NYI: copyval of vectors. */

589. err_nyi:

590. lj_trace_err(J, LJ_TRERR_NYICONV);

591. }

592. /* Box pointer, ref, enum or 64 bit integer. */

593. return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, sid), sp);

594. }

596. /* -- Convert TValue to C type (store) ------------------------------------ */

598. ‌static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, cTValue *sval)

599. {

600. CTState *cts = ctype_ctsG(J2G(J));

601. CTypeID sid = CTID_P_VOID;

602. void *svisnz = 0;

603. CType *s;

604. if (LJ_LIKELY(tref_isinteger(sp))) {

605. sid = CTID_INT32;

606. svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));

607. } else if (tref_isnum(sp)) {

608. sid = CTID_DOUBLE;

609. svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));

610. } else if (tref_isbool(sp)) {

611. sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0);

612. sid = CTID_BOOL;

613. } else if (tref_isnil(sp)) {

614. sp = lj_ir_kptr(J, NULL);

615. } else if (tref_isudata(sp)) {

616. GCudata *ud = udataV(sval);

617. if (ud->udtype == UDTYPE_IO_FILE) {

618. TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), sp, IRFL_UDATA_UDTYPE);

619. emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE));

620. sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp, IRFL_UDATA_FILE);

621. } else {

622. sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCudata)));

623. }

624. } else if (tref_isstr(sp)) {

625. if (ctype_isenum(d->info)) {  /* Match string against enum constant. */

626. GCstr *str = strV(sval);

627. CTSize ofs;

628. CType *cct = lj_ctype_getfield(cts, d, str, &ofs);

629. /* Specialize to the name of the enum constant. */

630. emitir(IRTG(IR_EQ, IRT_STR), sp, lj_ir_kstr(J, str));

631. if (cct && ctype_isconstval(cct->info)) {

632. lua_assert(ctype_child(cts, cct)->size == 4);

633. svisnz = (void *)(intptr_t)(ofs != 0);

634. sp = lj_ir_kint(J, (int32_t)ofs);

635. sid = ctype_cid(cct->info);

636. }  /* else: interpreter will throw. */

637. } else if (ctype_isrefarray(d->info)) {  /* Copy string to array. */

638. lj_trace_err(J, LJ_TRERR_BADTYPE);  /* NYI */

639. } else {  /* Otherwise pass the string data as a const char[]. */

640. /* Don't use STRREF. It folds with SNEW, which loses the trailing NUL. */

641. sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCstr)));

642. sid = CTID_A_CCHAR;

643. }

644. } else if (tref_islightud(sp)) {

645. #if LJ_64

646. sp = emitir(IRT(IR_BAND, IRT_P64), sp,

647. lj_ir_kint64(J, U64x(00007fff,ffffffff)));

648. #endif

649. } else {  /* NYI: tref_istab(sp). */

650. IRType t;

651. sid = argv2cdata(J, sp, sval)->ctypeid;

652. s = ctype_raw(cts, sid);

653. svisnz = cdataptr(cdataV(sval));

654. if (ctype_isfunc(s->info)) {

655. sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR);

656. s = ctype_get(cts, sid);

657. t = IRT_PTR;

658. } else {

659. t = crec_ct2irt(cts, s);

660. }

661. if (ctype_isptr(s->info)) {

662. sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_PTR);

663. if (ctype_isref(s->info)) {

664. svisnz = *(void **)svisnz;

665. s = ctype_rawchild(cts, s);

666. if (ctype_isenum(s->info)) s = ctype_child(cts, s);

667. t = crec_ct2irt(cts, s);

668. } else {

669. goto doconv;

670. }

671. } else if (t == IRT_I64 || t == IRT_U64) {

672. sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64);

673. lj_needsplit(J);

674. goto doconv;

675. } else if (t == IRT_INT || t == IRT_U32) {

676. if (ctype_isenum(s->info)) s = ctype_child(cts, s);

677. sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT);

678. goto doconv;

679. } else {

680. sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata)));

681. }

682. if (ctype_isnum(s->info) && t != IRT_CDATA)

683. sp = emitir(IRT(IR_XLOAD, t), sp, 0);  /* Load number value. */

684. goto doconv;

685. }

686. s = ctype_get(cts, sid);

687. doconv:

688. if (ctype_isenum(d->info)) d = ctype_child(cts, d);

689. return crec_ct_ct(J, d, s, dp, sp, svisnz);

690. }

692. /* -- C data metamethods -------------------------------------------------- */

694. /* This would be rather difficult in FOLD, so do it here:

695. ** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k)

696. ** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)

697. */

698. ‌static TRef crec_reassoc_ofs(jit_State *J, TRef tr, ptrdiff_t *ofsp, MSize sz)

699. {

700. IRIns *ir = IR(tref_ref(tr));

701. if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && irref_isk(ir->op2) &&

702. (ir->o == IR_ADD || ir->o == IR_ADDOV || ir->o == IR_SUBOV)) {

703. IRIns *irk = IR(ir->op2);

704. ptrdiff_t k;

705. if (LJ_64 && irk->o == IR_KINT64)

706. k = (ptrdiff_t)ir_kint64(irk)->u64 * sz;

707. else

708. k = (ptrdiff_t)irk->i * sz;

709. if (ir->o == IR_SUBOV) *ofsp -= k; else *ofsp += k;

710. tr = ir->op1;  /* Not a TRef, but the caller doesn't care. */

711. }

712. return tr;

713. }

715. /* Record ctype __index/__newindex metamethods. */

716. ‌static void crec_index_meta(jit_State *J, CTState *cts, CType *ct,

717. RecordFFData *rd)

718. {

719. CTypeID id = ctype_typeid(cts, ct);

720. cTValue *tv = lj_ctype_meta(cts, id, rd->data ? MM_newindex : MM_index);

721. if (!tv)

722. lj_trace_err(J, LJ_TRERR_BADTYPE);

723. if (tvisfunc(tv)) {

724. J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME;

725. rd->nres = -1;  /* Pending tailcall. */

726. } else if (rd->data == 0 && tvistab(tv) && tref_isstr(J->base[1])) {

727. /* Specialize to result of __index lookup. */

728. cTValue *o = lj_tab_get(J->L, tabV(tv), &rd->argv[1]);

729. J->base[0] = lj_record_constify(J, o);

730. if (!J->base[0])

731. lj_trace_err(J, LJ_TRERR_BADTYPE);

732. /* Always specialize to the key. */

733. emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1])));

734. } else {

735. /* NYI: resolving of non-function metamethods. */

736. /* NYI: non-string keys for __index table. */

737. /* NYI: stores to __newindex table. */

738. lj_trace_err(J, LJ_TRERR_BADTYPE);

739. }

740. }

742. ‌void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)

743. {

744. TRef idx, ptr = J->base[0];

745. ptrdiff_t ofs = sizeof(GCcdata);

746. GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]);

747. CTState *cts = ctype_ctsG(J2G(J));

748. CType *ct = ctype_raw(cts, cd->ctypeid);

749. CTypeID sid = 0;

751. /* Resolve pointer or reference for cdata object. */

752. if (ctype_isptr(ct->info)) {

753. IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;

754. if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);

755. ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_PTR);

756. ofs = 0;

757. ptr = crec_reassoc_ofs(J, ptr, &ofs, 1);

758. }

760. again:

761. idx = J->base[1];

762. if (tref_isnumber(idx)) {

763. idx = lj_opt_narrow_cindex(J, idx);

764. if (ctype_ispointer(ct->info)) {

765. CTSize sz;

766. integer_key:

767. if ((ct->info & CTF_COMPLEX))

768. idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1));

769. sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info)));

770. idx = crec_reassoc_ofs(J, idx, &ofs, sz);

771. #if LJ_TARGET_ARM || LJ_TARGET_PPC

772. /* Hoist base add to allow fusion of index/shift into operands. */

773. if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs

774. #if LJ_TARGET_ARM

775. && (sz == 1 || sz == 4)

776. #endif

777. ) {

778. ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));

779. ofs = 0;

780. }

781. #endif

782. idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz));

783. ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);

784. }

785. } else if (tref_iscdata(idx)) {

786. GCcdata *cdk = cdataV(&rd->argv[1]);

787. CType *ctk = ctype_raw(cts, cdk->ctypeid);

788. IRType t = crec_ct2irt(cts, ctk);

789. if (ctype_ispointer(ct->info) && t >= IRT_I8 && t <= IRT_U64) {

790. if (ctk->size == 8) {

791. idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT64);

792. } else if (ctk->size == 4) {

793. idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT);

794. } else {

795. idx = emitir(IRT(IR_ADD, IRT_PTR), idx,

796. lj_ir_kintp(J, sizeof(GCcdata)));

797. idx = emitir(IRT(IR_XLOAD, t), idx, 0);

798. }

799. if (LJ_64 && ctk->size < sizeof(intptr_t) && !(ctk->info & CTF_UNSIGNED))

800. idx = emitconv(idx, IRT_INTP, IRT_INT, IRCONV_SEXT);

801. if (!LJ_64 && ctk->size > sizeof(intptr_t)) {

802. idx = emitconv(idx, IRT_INTP, t, 0);

803. lj_needsplit(J);

804. }

805. goto integer_key;

806. }

807. } else if (tref_isstr(idx)) {

808. GCstr *name = strV(&rd->argv[1]);

809. if (cd && cd->ctypeid == CTID_CTYPEID)

810. ct = ctype_raw(cts, crec_constructor(J, cd, ptr));

811. if (ctype_isstruct(ct->info)) {

812. CTSize fofs;

813. CType *fct;

814. fct = lj_ctype_getfield(cts, ct, name, &fofs);

815. if (fct) {

816. /* Always specialize to the field name. */

817. emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));

818. if (ctype_isconstval(fct->info)) {

819. if (fct->size >= 0x80000000u &&

820. (ctype_child(cts, fct)->info & CTF_UNSIGNED)) {

821. J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size);

822. return;

823. }

824. J->base[0] = lj_ir_kint(J, (int32_t)fct->size);

825. return;  /* Interpreter will throw for newindex. */

826. } else if (ctype_isbitfield(fct->info)) {

827. lj_trace_err(J, LJ_TRERR_NYICONV);

828. } else {

829. lua_assert(ctype_isfield(fct->info));

830. sid = ctype_cid(fct->info);

831. }

832. ofs += (ptrdiff_t)fofs;

833. }

834. } else if (ctype_iscomplex(ct->info)) {

835. if (name->len == 2 &&

836. ((strdata(name)[0] == 'r' && strdata(name)[1] == 'e') ||

837. (strdata(name)[0] == 'i' && strdata(name)[1] == 'm'))) {

838. /* Always specialize to the field name. */

839. emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));

840. if (strdata(name)[0] == 'i') ofs += (ct->size >> 1);

841. sid = ctype_cid(ct->info);

842. }

843. }

844. }

845. if (!sid) {

846. if (ctype_isptr(ct->info)) {  /* Automatically perform '->'. */

847. CType *cct = ctype_rawchild(cts, ct);

848. if (ctype_isstruct(cct->info)) {

849. ct = cct;

850. cd = NULL;

851. if (tref_isstr(idx)) goto again;

852. }

853. }

854. crec_index_meta(J, cts, ct, rd);

855. return;

856. }

858. if (ofs)

859. ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));

861. /* Resolve reference for field. */

862. ct = ctype_get(cts, sid);

863. if (ctype_isref(ct->info)) {

864. ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0);

865. sid = ctype_cid(ct->info);

866. ct = ctype_get(cts, sid);

867. }

869. while (ctype_isattrib(ct->info))

870. ct = ctype_child(cts, ct);  /* Skip attributes. */

872. if (rd->data == 0) {  /* __index metamethod. */

873. J->base[0] = crec_tv_ct(J, ct, sid, ptr);

874. } else {  /* __newindex metamethod. */

875. rd->nres = 0;

876. J->needsnap = 1;

877. crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);

878. }

879. }

881. /* Record setting a finalizer. */

882. ‌static void crec_finalizer(jit_State *J, TRef trcd, TRef trfin, cTValue *fin)

883. {

884. if (tvisgcv(fin)) {

885. if (!trfin) trfin = lj_ir_kptr(J, gcval(fin));

886. } else if (tvisnil(fin)) {

887. trfin = lj_ir_kptr(J, NULL);

888. } else {

889. lj_trace_err(J, LJ_TRERR_BADTYPE);

890. }

891. lj_ir_call(J, IRCALL_lj_cdata_setfin, trcd,

892. trfin, lj_ir_kint(J, (int32_t)itype(fin)));

893. J->needsnap = 1;

894. }

896. /* Record cdata allocation. */

897. ‌static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id)

898. {

899. CTState *cts = ctype_ctsG(J2G(J));

900. CTSize sz;

901. CTInfo info = lj_ctype_info(cts, id, &sz);

902. CType *d = ctype_raw(cts, id);

903. TRef trcd, trid = lj_ir_kint(J, id);

904. cTValue *fin;

905. /* Use special instruction to box pointer or 32/64 bit integer. */

906. if (ctype_isptr(info) || (ctype_isinteger(info) && (sz == 4 || sz == 8))) {

907. TRef sp = J->base[1] ? crec_ct_tv(J, d, 0, J->base[1], &rd->argv[1]) :

908. ctype_isptr(info) ? lj_ir_kptr(J, NULL) :

909. sz == 4 ? lj_ir_kint(J, 0) :

910. (lj_needsplit(J), lj_ir_kint64(J, 0));

911. J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, sp);

912. return;

913. } else {

914. TRef trsz = TREF_NIL;

915. if ((info & CTF_VLA)) {  /* Calculate VLA/VLS size at runtime. */

916. CTSize sz0, sz1;

917. if (!J->base[1] || J->base[2])

918. lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: init VLA/VLS. */

919. trsz = crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0,

920. J->base[1], &rd->argv[1]);

921. sz0 = lj_ctype_vlsize(cts, d, 0);

922. sz1 = lj_ctype_vlsize(cts, d, 1);

923. trsz = emitir(IRTGI(IR_MULOV), trsz, lj_ir_kint(J, (int32_t)(sz1-sz0)));

924. trsz = emitir(IRTGI(IR_ADDOV), trsz, lj_ir_kint(J, (int32_t)sz0));

925. J->base[1] = 0;  /* Simplify logic below. */

926. } else if (ctype_align(info) > CT_MEMALIGN) {

927. trsz = lj_ir_kint(J, sz);

928. }

929. trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, trsz);

930. if (sz > 128 || (info & CTF_VLA)) {

931. TRef dp;

932. CTSize align;

933. special:  /* Only handle bulk zero-fill for large/VLA/VLS types. */

934. if (J->base[1])

935. lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: init large/VLA/VLS types. */

936. dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));

937. if (trsz == TREF_NIL) trsz = lj_ir_kint(J, sz);

938. align = ctype_align(info);

939. if (align < CT_MEMALIGN) align = CT_MEMALIGN;

940. crec_fill(J, dp, trsz, lj_ir_kint(J, 0), (1u << align));

941. } else if (J->base[1] && !J->base[2] &&

942. !lj_cconv_multi_init(cts, d, &rd->argv[1])) {

943. goto single_init;

944. } else if (ctype_isarray(d->info)) {

945. CType *dc = ctype_rawchild(cts, d);  /* Array element type. */

946. CTSize ofs, esize = dc->size;

947. TRef sp = 0;

948. TValue tv;

949. TValue *sval = &tv;

950. MSize i;

951. tv.u64 = 0;

952. if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)) ||

953. esize * CREC_FILL_MAXUNROLL < sz)

954. goto special;

955. for (i = 1, ofs = 0; ofs < sz; ofs += esize) {

956. TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,

957. lj_ir_kintp(J, ofs + sizeof(GCcdata)));

958. if (J->base[i]) {

959. sp = J->base[i];

960. sval = &rd->argv[i];

961. i++;

962. } else if (i != 2) {

963. sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL;

964. }

965. crec_ct_tv(J, dc, dp, sp, sval);

966. }

967. } else if (ctype_isstruct(d->info)) {

968. CTypeID fid = d->sib;

969. MSize i = 1;

970. while (fid) {

971. CType *df = ctype_get(cts, fid);

972. fid = df->sib;

973. if (ctype_isfield(df->info)) {

974. CType *dc;

975. TRef sp, dp;

976. TValue tv;

977. TValue *sval = &tv;

978. setintV(&tv, 0);

979. if (!gcref(df->name)) continue;  /* Ignore unnamed fields. */

980. dc = ctype_rawchild(cts, df);  /* Field type. */

981. if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) ||

982. ctype_isenum(dc->info)))

983. lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: init aggregates. */

984. if (J->base[i]) {

985. sp = J->base[i];

986. sval = &rd->argv[i];

987. i++;

988. } else {

989. sp = ctype_isptr(dc->info) ? TREF_NIL : lj_ir_kint(J, 0);

990. }

991. dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,

992. lj_ir_kintp(J, df->size + sizeof(GCcdata)));

993. crec_ct_tv(J, dc, dp, sp, sval);

994. } else if (!ctype_isconstval(df->info)) {

995. /* NYI: init bitfields and sub-structures. */

996. lj_trace_err(J, LJ_TRERR_NYICONV);

997. }

998. }

999. } else {

1000. TRef dp;

1001. single_init:

1002. dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));

1003. if (J->base[1]) {

1004. crec_ct_tv(J, d, dp, J->base[1], &rd->argv[1]);

1005. } else {

1006. TValue tv;

1007. tv.u64 = 0;

1008. crec_ct_tv(J, d, dp, lj_ir_kint(J, 0), &tv);

1009. }

1010. }

1011. }

1012. J->base[0] = trcd;

1013. /* Handle __gc metamethod. */

1014. fin = lj_ctype_meta(cts, id, MM_gc);

1015. if (fin)

1016. crec_finalizer(J, trcd, 0, fin);

1017. }

1019. /* Record argument conversions. */

1020. ‌static TRef crec_call_args(jit_State *J, RecordFFData *rd,

1021. CTState *cts, CType *ct)

1022. {

1023. TRef args[CCI_NARGS_MAX];

1024. CTypeID fid;

1025. MSize i, n;

1026. TRef tr, *base;

1027. cTValue *o;

1028. #if LJ_TARGET_X86

1029. #if LJ_ABI_WIN

1030. TRef *arg0 = NULL, *arg1 = NULL;

1031. #endif

1032. int ngpr = 0;

1033. if (ctype_cconv(ct->info) == CTCC_THISCALL)

1034. ngpr = 1;

1035. else if (ctype_cconv(ct->info) == CTCC_FASTCALL)

1036. ngpr = 2;

1037. #endif

1039. /* Skip initial attributes. */

1040. fid = ct->sib;

1041. while (fid) {

1042. CType *ctf = ctype_get(cts, fid);

1043. if (!ctype_isattrib(ctf->info)) break;

1044. fid = ctf->sib;

1045. }

1046. args[0] = TREF_NIL;

1047. for (n = 0, base = J->base+1, o = rd->argv+1; *base; n++, base++, o++) {

1048. CTypeID did;

1049. CType *d;

1051. if (n >= CCI_NARGS_MAX)

1052. lj_trace_err(J, LJ_TRERR_NYICALL);

1054. if (fid) {  /* Get argument type from field. */

1055. CType *ctf = ctype_get(cts, fid);

1056. fid = ctf->sib;

1057. lua_assert(ctype_isfield(ctf->info));

1058. did = ctype_cid(ctf->info);

1059. } else {

1060. if (!(ct->info & CTF_VARARG))

1061. lj_trace_err(J, LJ_TRERR_NYICALL);  /* Too many arguments. */

1062. did = lj_ccall_ctid_vararg(cts, o);  /* Infer vararg type. */

1063. }

1064. d = ctype_raw(cts, did);

1065. if (!(ctype_isnum(d->info) || ctype_isptr(d->info) ||

1066. ctype_isenum(d->info)))

1067. lj_trace_err(J, LJ_TRERR_NYICALL);

1068. tr = crec_ct_tv(J, d, 0, *base, o);

1069. if (ctype_isinteger_or_bool(d->info)) {

1070. if (d->size < 4) {

1071. if ((d->info & CTF_UNSIGNED))

1072. tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_U8 : IRT_U16, 0);

1073. else

1074. tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_I8 : IRT_I16,IRCONV_SEXT);

1075. }

1076. } else if (LJ_SOFTFP && ctype_isfp(d->info) && d->size > 4) {

1077. lj_needsplit(J);

1078. }

1079. #if LJ_TARGET_X86

1080. /* 64 bit args must not end up in registers for fastcall/thiscall. */

1081. #if LJ_ABI_WIN

1082. if (!ctype_isfp(d->info)) {

1083. /* Sigh, the Windows/x86 ABI allows reordering across 64 bit args. */

1084. if (tref_typerange(tr, IRT_I64, IRT_U64)) {

1085. if (ngpr) {

1086. arg0 = &args[n]; args[n++] = TREF_NIL; ngpr--;

1087. if (ngpr) {

1088. arg1 = &args[n]; args[n++] = TREF_NIL; ngpr--;

1089. }

1090. }

1091. } else {

1092. if (arg0) { *arg0 = tr; arg0 = NULL; n--; continue; }

1093. if (arg1) { *arg1 = tr; arg1 = NULL; n--; continue; }

1094. if (ngpr) ngpr--;

1095. }

1096. }

1097. #else

1098. if (!ctype_isfp(d->info) && ngpr) {

1099. if (tref_typerange(tr, IRT_I64, IRT_U64)) {

1100. /* No reordering for other x86 ABIs. Simply add alignment args. */

1101. do { args[n++] = TREF_NIL; } while (--ngpr);

1102. } else {

1103. ngpr--;

1104. }

1105. }

1106. #endif

1107. #endif

1108. args[n] = tr;

1109. }

1110. tr = args[0];

1111. for (i = 1; i < n; i++)

1112. tr = emitir(IRT(IR_CARG, IRT_NIL), tr, args[i]);

1113. return tr;

1114. }

1116. /* Create a snapshot for the caller, simulating a 'false' return value. */

1117. ‌static void crec_snap_caller(jit_State *J)

1118. {

1119. lua_State *L = J->L;

1120. TValue *base = L->base, *top = L->top;

1121. const BCIns *pc = J->pc;

1122. TRef ftr = J->base[-1];

1123. ptrdiff_t delta;

1124. if (!frame_islua(base-1) || J->framedepth <= 0)

1125. lj_trace_err(J, LJ_TRERR_NYICALL);

1126. J->pc = frame_pc(base-1); delta = 1+LJ_FR2+bc_a(J->pc[-1]);

1127. L->top = base; L->base = base - delta;

1128. J->base[-1] = TREF_FALSE;

1129. J->base -= delta; J->baseslot -= (BCReg)delta;

1130. J->maxslot = (BCReg)delta; J->framedepth--;

1131. lj_snap_add(J);

1132. L->base = base; L->top = top;

1133. J->framedepth++; J->maxslot = 1;

1134. J->base += delta; J->baseslot += (BCReg)delta;

1135. J->base[-1] = ftr; J->pc = pc;

1136. }

1138. /* Record function call. */

1139. ‌static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd)

1140. {

1141. CTState *cts = ctype_ctsG(J2G(J));

1142. CType *ct = ctype_raw(cts, cd->ctypeid);

1143. IRType tp = IRT_PTR;

1144. if (ctype_isptr(ct->info)) {

1145. tp = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;

1146. ct = ctype_rawchild(cts, ct);

1147. }

1148. if (ctype_isfunc(ct->info)) {

1149. TRef func = emitir(IRT(IR_FLOAD, tp), J->base[0], IRFL_CDATA_PTR);

1150. CType *ctr = ctype_rawchild(cts, ct);

1151. IRType t = crec_ct2irt(cts, ctr);

1152. TRef tr;

1153. TValue tv;

1154. /* Check for blacklisted C functions that might call a callback. */

1155. setlightudV(&tv,

1156. cdata_getptr(cdataptr(cd), (LJ_64 && tp == IRT_P64) ? 8 : 4));

1157. if (tvistrue(lj_tab_get(J->L, cts->miscmap, &tv)))

1158. lj_trace_err(J, LJ_TRERR_BLACKL);

1159. if (ctype_isvoid(ctr->info)) {

1160. t = IRT_NIL;

1161. rd->nres = 0;

1162. } else if (!(ctype_isnum(ctr->info) || ctype_isptr(ctr->info) ||

1163. ctype_isenum(ctr->info)) || t == IRT_CDATA) {

1164. lj_trace_err(J, LJ_TRERR_NYICALL);

1165. }

1166. if ((ct->info & CTF_VARARG)

1167. #if LJ_TARGET_X86

1168. || ctype_cconv(ct->info) != CTCC_CDECL

1169. #endif

1170. )

1171. func = emitir(IRT(IR_CARG, IRT_NIL), func,

1172. lj_ir_kint(J, ctype_typeid(cts, ct)));

1173. tr = emitir(IRT(IR_CALLXS, t), crec_call_args(J, rd, cts, ct), func);

1174. if (ctype_isbool(ctr->info)) {

1175. if (frame_islua(J->L->base-1) && bc_b(frame_pc(J->L->base-1)[-1]) == 1) {

1176. /* Don't check result if ignored. */

1177. tr = TREF_NIL;

1178. } else {

1179. crec_snap_caller(J);

1180. #if LJ_TARGET_X86ORX64

1181. /* Note: only the x86/x64 backend supports U8 and only for EQ(tr, 0). */

1182. lj_ir_set(J, IRTG(IR_NE, IRT_U8), tr, lj_ir_kint(J, 0));

1183. #else

1184. lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));

1185. #endif

1186. J->postproc = LJ_POST_FIXGUARDSNAP;

1187. tr = TREF_TRUE;

1188. }

1189. } else if (t == IRT_PTR || (LJ_64 && t == IRT_P32) ||

1190. t == IRT_I64 || t == IRT_U64 || ctype_isenum(ctr->info)) {

1191. TRef trid = lj_ir_kint(J, ctype_cid(ct->info));

1192. tr = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, tr);

1193. if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);

1194. } else if (t == IRT_FLOAT || t == IRT_U32) {

1195. tr = emitconv(tr, IRT_NUM, t, 0);

1196. } else if (t == IRT_I8 || t == IRT_I16) {

1197. tr = emitconv(tr, IRT_INT, t, IRCONV_SEXT);

1198. } else if (t == IRT_U8 || t == IRT_U16) {

1199. tr = emitconv(tr, IRT_INT, t, 0);

1200. }

1201. J->base[0] = tr;

1202. J->needsnap = 1;

1203. return 1;

1204. }

1205. return 0;

1206. }

1208. ‌void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd)

1209. {

1210. CTState *cts = ctype_ctsG(J2G(J));

1211. GCcdata *cd = argv2cdata(J, J->base[0], &rd->argv[0]);

1212. CTypeID id = cd->ctypeid;

1213. CType *ct;

1214. cTValue *tv;

1215. MMS mm = MM_call;

1216. if (id == CTID_CTYPEID) {

1217. id = crec_constructor(J, cd, J->base[0]);

1218. mm = MM_new;

1219. } else if (crec_call(J, rd, cd)) {

1220. return;

1221. }

1222. /* Record ctype __call/__new metamethod. */

1223. ct = ctype_raw(cts, id);

1224. tv = lj_ctype_meta(cts, ctype_isptr(ct->info) ? ctype_cid(ct->info) : id, mm);

1225. if (tv) {

1226. if (tvisfunc(tv)) {

1227. J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME;

1228. rd->nres = -1;  /* Pending tailcall. */

1229. return;

1230. }

1231. } else if (mm == MM_new) {

1232. crec_alloc(J, rd, id);

1233. return;

1234. }

1235. /* No metamethod or NYI: non-function metamethods. */

1236. lj_trace_err(J, LJ_TRERR_BADTYPE);

1237. }

1239. ‌static TRef crec_arith_int64(jit_State *J, TRef *sp, CType **s, MMS mm)

1240. {

1241. if (ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) {

1242. IRType dt;

1243. CTypeID id;

1244. TRef tr;

1245. MSize i;

1246. IROp op;

1247. lj_needsplit(J);

1248. if (((s[0]->info & CTF_UNSIGNED) && s[0]->size == 8) ||

1249. ((s[1]->info & CTF_UNSIGNED) && s[1]->size == 8)) {

1250. dt = IRT_U64; id = CTID_UINT64;

1251. } else {

1252. dt = IRT_I64; id = CTID_INT64;

1253. if (mm < MM_add &&

1254. !((s[0]->info | s[1]->info) & CTF_FP) &&

1255. s[0]->size == 4 && s[1]->size == 4) {  /* Try to narrow comparison. */

1256. if (!((s[0]->info ^ s[1]->info) & CTF_UNSIGNED) ||

1257. (tref_isk(sp[1]) && IR(tref_ref(sp[1]))->i >= 0)) {

1258. dt = (s[0]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT;

1259. goto comp;

1260. } else if (tref_isk(sp[0]) && IR(tref_ref(sp[0]))->i >= 0) {

1261. dt = (s[1]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT;

1262. goto comp;

1263. }

1264. }

1265. }

1266. for (i = 0; i < 2; i++) {

1267. IRType st = tref_type(sp[i]);

1268. if (st == IRT_NUM || st == IRT_FLOAT)

1269. sp[i] = emitconv(sp[i], dt, st, IRCONV_ANY);

1270. else if (!(st == IRT_I64 || st == IRT_U64))

1271. sp[i] = emitconv(sp[i], dt, IRT_INT,

1272. (s[i]->info & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);

1273. }

1274. if (mm < MM_add) {

1275. comp:

1276. /* Assume true comparison. Fixup and emit pending guard later. */

1277. if (mm == MM_eq) {

1278. op = IR_EQ;

1279. } else {

1280. op = mm == MM_lt ? IR_LT : IR_LE;

1281. if (dt == IRT_U32 || dt == IRT_U64)

1282. op += (IR_ULT-IR_LT);

1283. }

1284. lj_ir_set(J, IRTG(op, dt), sp[0], sp[1]);

1285. J->postproc = LJ_POST_FIXGUARD;

1286. return TREF_TRUE;

1287. } else {

1288. tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, dt), sp[0], sp[1]);

1289. }

1290. return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);

1291. }

1292. return 0;

1293. }

1295. ‌static TRef crec_arith_ptr(jit_State *J, TRef *sp, CType **s, MMS mm)

1296. {

1297. CTState *cts = ctype_ctsG(J2G(J));

1298. CType *ctp = s[0];

1299. if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {

1300. if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&

1301. (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {

1302. if (mm == MM_sub) {  /* Pointer difference. */

1303. TRef tr;

1304. CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));

1305. if (sz == 0 || (sz & (sz-1)) != 0)

1306. return 0;  /* NYI: integer division. */

1307. tr = emitir(IRT(IR_SUB, IRT_INTP), sp[0], sp[1]);

1308. tr = emitir(IRT(IR_BSAR, IRT_INTP), tr, lj_ir_kint(J, lj_fls(sz)));

1309. #if LJ_64

1310. tr = emitconv(tr, IRT_NUM, IRT_INTP, 0);

1311. #endif

1312. return tr;

1313. } else {  /* Pointer comparison (unsigned). */

1314. /* Assume true comparison. Fixup and emit pending guard later. */

1315. IROp op = mm == MM_eq ? IR_EQ : mm == MM_lt ? IR_ULT : IR_ULE;

1316. lj_ir_set(J, IRTG(op, IRT_PTR), sp[0], sp[1]);

1317. J->postproc = LJ_POST_FIXGUARD;

1318. return TREF_TRUE;

1319. }

1320. }

1321. if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(s[1]->info)))

1322. return 0;

1323. } else if (mm == MM_add && ctype_isnum(ctp->info) &&

1324. (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {

1325. TRef tr = sp[0]; sp[0] = sp[1]; sp[1] = tr;  /* Swap pointer and index. */

1326. ctp = s[1];

1327. } else {

1328. return 0;

1329. }

1330. {

1331. TRef tr = sp[1];

1332. IRType t = tref_type(tr);

1333. CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));

1334. CTypeID id;

1335. #if LJ_64

1336. if (t == IRT_NUM || t == IRT_FLOAT)

1337. tr = emitconv(tr, IRT_INTP, t, IRCONV_ANY);

1338. else if (!(t == IRT_I64 || t == IRT_U64))

1339. tr = emitconv(tr, IRT_INTP, IRT_INT,

1340. ((t - IRT_I8) & 1) ? 0 : IRCONV_SEXT);

1341. #else

1342. if (!tref_typerange(sp[1], IRT_I8, IRT_U32)) {

1343. tr = emitconv(tr, IRT_INTP, t,

1344. (t == IRT_NUM || t == IRT_FLOAT) ? IRCONV_ANY : 0);

1345. }

1346. #endif

1347. tr = emitir(IRT(IR_MUL, IRT_INTP), tr, lj_ir_kintp(J, sz));

1348. tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, IRT_PTR), sp[0], tr);

1349. id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),

1350. CTSIZE_PTR);

1351. return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);

1352. }

1353. }

1355. /* Record ctype arithmetic metamethods. */

1356. ‌static TRef crec_arith_meta(jit_State *J, TRef *sp, CType **s, CTState *cts,

1357. RecordFFData *rd)

1358. {

1359. cTValue *tv = NULL;

1360. if (J->base[0]) {

1361. if (tviscdata(&rd->argv[0])) {

1362. CTypeID id = argv2cdata(J, J->base[0], &rd->argv[0])->ctypeid;

1363. CType *ct = ctype_raw(cts, id);

1364. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);

1365. tv = lj_ctype_meta(cts, id, (MMS)rd->data);

1366. }

1367. if (!tv && J->base[1] && tviscdata(&rd->argv[1])) {

1368. CTypeID id = argv2cdata(J, J->base[1], &rd->argv[1])->ctypeid;

1369. CType *ct = ctype_raw(cts, id);

1370. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);

1371. tv = lj_ctype_meta(cts, id, (MMS)rd->data);

1372. }

1373. }

1374. if (tv) {

1375. if (tvisfunc(tv)) {

1376. J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME;

1377. rd->nres = -1;  /* Pending tailcall. */

1378. return 0;

1379. }  /* NYI: non-function metamethods. */

1380. } else if ((MMS)rd->data == MM_eq) {  /* Fallback cdata pointer comparison. */

1381. if (sp[0] && sp[1] && ctype_isnum(s[0]->info) == ctype_isnum(s[1]->info)) {

1382. /* Assume true comparison. Fixup and emit pending guard later. */

1383. lj_ir_set(J, IRTG(IR_EQ, IRT_PTR), sp[0], sp[1]);

1384. J->postproc = LJ_POST_FIXGUARD;

1385. return TREF_TRUE;

1386. } else {

1387. return TREF_FALSE;

1388. }

1389. }

1390. lj_trace_err(J, LJ_TRERR_BADTYPE);

1391. return 0;

1392. }

1394. ‌void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd)

1395. {

1396. CTState *cts = ctype_ctsG(J2G(J));

1397. TRef sp[2];

1398. CType *s[2];

1399. MSize i;

1400. for (i = 0; i < 2; i++) {

1401. TRef tr = J->base[i];

1402. CType *ct = ctype_get(cts, CTID_DOUBLE);

1403. if (!tr) {

1404. lj_trace_err(J, LJ_TRERR_BADTYPE);

1405. } else if (tref_iscdata(tr)) {

1406. CTypeID id = argv2cdata(J, tr, &rd->argv[i])->ctypeid;

1407. IRType t;

1408. ct = ctype_raw(cts, id);

1409. t = crec_ct2irt(cts, ct);

1410. if (ctype_isptr(ct->info)) {  /* Resolve pointer or reference. */

1411. tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_PTR);

1412. if (ctype_isref(ct->info)) {

1413. ct = ctype_rawchild(cts, ct);

1414. t = crec_ct2irt(cts, ct);

1415. }

1416. } else if (t == IRT_I64 || t == IRT_U64) {

1417. tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT64);

1418. lj_needsplit(J);

1419. goto ok;

1420. } else if (t == IRT_INT || t == IRT_U32) {

1421. tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT);

1422. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);

1423. goto ok;

1424. } else if (ctype_isfunc(ct->info)) {

1425. tr = emitir(IRT(IR_FLOAD, IRT_PTR), tr, IRFL_CDATA_PTR);

1426. ct = ctype_get(cts,

1427. lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR));

1428. goto ok;

1429. } else {

1430. tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata)));

1431. }

1432. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);

1433. if (ctype_isnum(ct->info)) {

1434. if (t == IRT_CDATA) {

1435. tr = 0;

1436. } else {

1437. if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);

1438. tr = emitir(IRT(IR_XLOAD, t), tr, 0);

1439. }

1440. }

1441. } else if (tref_isnil(tr)) {

1442. tr = lj_ir_kptr(J, NULL);

1443. ct = ctype_get(cts, CTID_P_VOID);

1444. } else if (tref_isinteger(tr)) {

1445. ct = ctype_get(cts, CTID_INT32);

1446. } else if (tref_isstr(tr)) {

1447. TRef tr2 = J->base[1-i];

1448. CTypeID id = argv2cdata(J, tr2, &rd->argv[1-i])->ctypeid;

1449. ct = ctype_raw(cts, id);

1450. if (ctype_isenum(ct->info)) {  /* Match string against enum constant. */

1451. GCstr *str = strV(&rd->argv[i]);

1452. CTSize ofs;

1453. CType *cct = lj_ctype_getfield(cts, ct, str, &ofs);

1454. if (cct && ctype_isconstval(cct->info)) {

1455. /* Specialize to the name of the enum constant. */

1456. emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, str));

1457. ct = ctype_child(cts, cct);

1458. tr = lj_ir_kint(J, (int32_t)ofs);

1459. } else {  /* Interpreter will throw or return false. */

1460. ct = ctype_get(cts, CTID_P_VOID);

1461. }

1462. } else if (ctype_isptr(ct->info)) {

1463. tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCstr)));

1464. } else {

1465. ct = ctype_get(cts, CTID_P_VOID);

1466. }

1467. } else if (!tref_isnum(tr)) {

1468. tr = 0;

1469. ct = ctype_get(cts, CTID_P_VOID);

1470. }

1471. ok:

1472. s[i] = ct;

1473. sp[i] = tr;

1474. }

1475. {

1476. TRef tr;

1477. if (!(tr = crec_arith_int64(J, sp, s, (MMS)rd->data)) &&

1478. !(tr = crec_arith_ptr(J, sp, s, (MMS)rd->data)) &&

1479. !(tr = crec_arith_meta(J, sp, s, cts, rd)))

1480. return;

1481. J->base[0] = tr;

1482. /* Fixup cdata comparisons, too. Avoids some cdata escapes. */

1483. if (J->postproc == LJ_POST_FIXGUARD && frame_iscont(J->L->base-1) &&

1484. !irt_isguard(J->guardemit)) {

1485. const BCIns *pc = frame_contpc(J->L->base-1) - 1;

1486. if (bc_op(*pc) <= BC_ISNEP) {

1487. J2G(J)->tmptv.u64 = (uint64_t)(uintptr_t)pc;

1488. J->postproc = LJ_POST_FIXCOMP;

1489. }

1490. }

1491. }

1492. }

1494. /* -- C library namespace metamethods ------------------------------------- */

1496. ‌void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd)

1497. {

1498. CTState *cts = ctype_ctsG(J2G(J));

1499. if (tref_isudata(J->base[0]) && tref_isstr(J->base[1]) &&

1500. udataV(&rd->argv[0])->udtype == UDTYPE_FFI_CLIB) {

1501. CLibrary *cl = (CLibrary *)uddata(udataV(&rd->argv[0]));

1502. GCstr *name = strV(&rd->argv[1]);

1503. CType *ct;

1504. CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX);

1505. cTValue *tv = lj_tab_getstr(cl->cache, name);

1506. rd->nres = rd->data;

1507. if (id && tv && !tvisnil(tv)) {

1508. /* Specialize to the symbol name and make the result a constant. */

1509. emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, name));

1510. if (ctype_isconstval(ct->info)) {

1511. if (ct->size >= 0x80000000u &&

1512. (ctype_child(cts, ct)->info & CTF_UNSIGNED))

1513. J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)ct->size);

1514. else

1515. J->base[0] = lj_ir_kint(J, (int32_t)ct->size);

1516. } else if (ctype_isextern(ct->info)) {

1517. CTypeID sid = ctype_cid(ct->info);

1518. void *sp = *(void **)cdataptr(cdataV(tv));

1519. TRef ptr;

1520. ct = ctype_raw(cts, sid);

1521. if (LJ_64 && !checkptr32(sp))

1522. ptr = lj_ir_kintp(J, (uintptr_t)sp);

1523. else

1524. ptr = lj_ir_kptr(J, sp);

1525. if (rd->data) {

1526. J->base[0] = crec_tv_ct(J, ct, sid, ptr);

1527. } else {

1528. J->needsnap = 1;

1529. crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);

1530. }

1531. } else {

1532. J->base[0] = lj_ir_kgc(J, obj2gco(cdataV(tv)), IRT_CDATA);

1533. }

1534. } else {

1535. lj_trace_err(J, LJ_TRERR_NOCACHE);

1536. }

1537. }  /* else: interpreter will throw. */

1538. }

1540. /* -- FFI library functions ----------------------------------------------- */

1542. ‌static TRef crec_toint(jit_State *J, CTState *cts, TRef sp, TValue *sval)

1543. {

1544. return crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, sp, sval);

1545. }

1547. ‌void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd)

1548. {

1549. crec_alloc(J, rd, argv2ctype(J, J->base[0], &rd->argv[0]));

1550. }

1552. ‌void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd)

1553. {

1554. UNUSED(rd);

1555. if (J->base[0])

1556. lj_trace_err(J, LJ_TRERR_NYICALL);

1557. J->base[0] = lj_ir_call(J, IRCALL_lj_vm_errno);

1558. }

1560. ‌void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd)

1561. {

1562. CTState *cts = ctype_ctsG(J2G(J));

1563. TRef tr = J->base[0];

1564. if (tr) {

1565. TRef trlen = J->base[1];

1566. if (!tref_isnil(trlen)) {

1567. trlen = crec_toint(J, cts, trlen, &rd->argv[1]);

1568. tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, &rd->argv[0]);

1569. } else {

1570. tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CCHAR), 0, tr, &rd->argv[0]);

1571. trlen = lj_ir_call(J, IRCALL_strlen, tr);

1572. }

1573. J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), tr, trlen);

1574. }  /* else: interpreter will throw. */

1575. }

1577. ‌void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd)

1578. {

1579. CTState *cts = ctype_ctsG(J2G(J));

1580. TRef trdst = J->base[0], trsrc = J->base[1], trlen = J->base[2];

1581. if (trdst && trsrc && (trlen || tref_isstr(trsrc))) {

1582. trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]);

1583. trsrc = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, trsrc, &rd->argv[1]);

1584. if (trlen) {

1585. trlen = crec_toint(J, cts, trlen, &rd->argv[2]);

1586. } else {

1587. trlen = emitir(IRTI(IR_FLOAD), J->base[1], IRFL_STR_LEN);

1588. trlen = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1));

1589. }

1590. rd->nres = 0;

1591. crec_copy(J, trdst, trsrc, trlen, NULL);

1592. }  /* else: interpreter will throw. */

1593. }

1595. ‌void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd)

1596. {

1597. CTState *cts = ctype_ctsG(J2G(J));

1598. TRef trdst = J->base[0], trlen = J->base[1], trfill = J->base[2];

1599. if (trdst && trlen) {

1600. CTSize step = 1;

1601. if (tviscdata(&rd->argv[0])) {  /* Get alignment of original destination. */

1602. CTSize sz;

1603. CType *ct = ctype_raw(cts, cdataV(&rd->argv[0])->ctypeid);

1604. if (ctype_isptr(ct->info))

1605. ct = ctype_rawchild(cts, ct);

1606. step = (1u<<ctype_align(lj_ctype_info(cts, ctype_typeid(cts, ct), &sz)));

1607. }

1608. trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]);

1609. trlen = crec_toint(J, cts, trlen, &rd->argv[1]);

1610. if (trfill)

1611. trfill = crec_toint(J, cts, trfill, &rd->argv[2]);

1612. else

1613. trfill = lj_ir_kint(J, 0);

1614. rd->nres = 0;

1615. crec_fill(J, trdst, trlen, trfill, step);

1616. }  /* else: interpreter will throw. */

1617. }

1619. ‌void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd)

1620. {

1621. if (tref_iscdata(J->base[0])) {

1622. TRef trid = lj_ir_kint(J, argv2ctype(J, J->base[0], &rd->argv[0]));

1623. J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA),

1624. lj_ir_kint(J, CTID_CTYPEID), trid);

1625. } else {

1626. setfuncV(J->L, &J->errinfo, J->fn);

1627. lj_trace_err_info(J, LJ_TRERR_NYIFFU);

1628. }

1629. }

1631. ‌void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd)

1632. {

1633. argv2ctype(J, J->base[0], &rd->argv[0]);

1634. if (tref_iscdata(J->base[1])) {

1635. argv2ctype(J, J->base[1], &rd->argv[1]);

1636. J->postproc = LJ_POST_FIXBOOL;

1637. J->base[0] = TREF_TRUE;

1638. } else {

1639. J->base[0] = TREF_FALSE;

1640. }

1641. }

1643. ‌void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd)

1644. {

1645. if (tref_isstr(J->base[0])) {

1646. /* Specialize to the ABI string to make the boolean result a constant. */

1647. emitir(IRTG(IR_EQ, IRT_STR), J->base[0], lj_ir_kstr(J, strV(&rd->argv[0])));

1648. J->postproc = LJ_POST_FIXBOOL;

1649. J->base[0] = TREF_TRUE;

1650. } else {

1651. lj_trace_err(J, LJ_TRERR_BADTYPE);

1652. }

1653. }

1655. /* Record ffi.sizeof(), ffi.alignof(), ffi.offsetof(). */

1656. ‌void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd)

1657. {

1658. CTypeID id = argv2ctype(J, J->base[0], &rd->argv[0]);

1659. if (rd->data == FF_ffi_sizeof) {

1660. CType *ct = lj_ctype_rawref(ctype_ctsG(J2G(J)), id);

1661. if (ctype_isvltype(ct->info))

1662. lj_trace_err(J, LJ_TRERR_BADTYPE);

1663. } else if (rd->data == FF_ffi_offsetof) {  /* Specialize to the field name. */

1664. if (!tref_isstr(J->base[1]))

1665. lj_trace_err(J, LJ_TRERR_BADTYPE);

1666. emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1])));

1667. rd->nres = 3;  /* Just in case. */

1668. }

1669. J->postproc = LJ_POST_FIXCONST;

1670. J->base[0] = J->base[1] = J->base[2] = TREF_NIL;

1671. }

1673. ‌void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd)

1674. {

1675. argv2cdata(J, J->base[0], &rd->argv[0]);

1676. if (!J->base[1])

1677. lj_trace_err(J, LJ_TRERR_BADTYPE);

1678. crec_finalizer(J, J->base[0], J->base[1], &rd->argv[1]);

1679. }

1681. /* -- 64 bit bit.* library functions -------------------------------------- */

1683. /* Determine bit operation type from argument type. */

1684. ‌static CTypeID crec_bit64_type(CTState *cts, cTValue *tv)

1685. {

1686. if (tviscdata(tv)) {

1687. CType *ct = lj_ctype_rawref(cts, cdataV(tv)->ctypeid);

1688. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);

1689. if ((ct->info & (CTMASK_NUM|CTF_BOOL|CTF_FP|CTF_UNSIGNED)) ==

1690. CTINFO(CT_NUM, CTF_UNSIGNED) && ct->size == 8)

1691. return CTID_UINT64;  /* Use uint64_t, since it has the highest rank. */

1692. return CTID_INT64;  /* Otherwise use int64_t. */

1693. }

1694. return 0;  /* Use regular 32 bit ops. */

1695. }

1697. ‌void LJ_FASTCALL recff_bit64_tobit(jit_State *J, RecordFFData *rd)

1698. {

1699. CTState *cts = ctype_ctsG(J2G(J));

1700. TRef tr = crec_ct_tv(J, ctype_get(cts, CTID_INT64), 0,

1701. J->base[0], &rd->argv[0]);

1702. if (!tref_isinteger(tr))

1703. tr = emitconv(tr, IRT_INT, tref_type(tr), 0);

1704. J->base[0] = tr;

1705. }

1707. ‌int LJ_FASTCALL recff_bit64_unary(jit_State *J, RecordFFData *rd)

1708. {

1709. CTState *cts = ctype_ctsG(J2G(J));

1710. CTypeID id = crec_bit64_type(cts, &rd->argv[0]);

1711. if (id) {

1712. TRef tr = crec_ct_tv(J, ctype_get(cts, id), 0, J->base[0], &rd->argv[0]);

1713. tr = emitir(IRT(rd->data, id-CTID_INT64+IRT_I64), tr, 0);

1714. J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);

1715. return 1;

1716. }

1717. return 0;

1718. }

1720. ‌int LJ_FASTCALL recff_bit64_nary(jit_State *J, RecordFFData *rd)

1721. {

1722. CTState *cts = ctype_ctsG(J2G(J));

1723. CTypeID id = 0;

1724. MSize i;

1725. for (i = 0; J->base[i] != 0; i++) {

1726. CTypeID aid = crec_bit64_type(cts, &rd->argv[i]);

1727. if (id < aid) id = aid;  /* Determine highest type rank of all arguments. */

1728. }

1729. if (id) {

1730. CType *ct = ctype_get(cts, id);

1731. uint32_t ot = IRT(rd->data, id-CTID_INT64+IRT_I64);

1732. TRef tr = crec_ct_tv(J, ct, 0, J->base[0], &rd->argv[0]);

1733. for (i = 1; J->base[i] != 0; i++) {

1734. TRef tr2 = crec_ct_tv(J, ct, 0, J->base[i], &rd->argv[i]);

1735. tr = emitir(ot, tr, tr2);

1736. }

1737. J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);

1738. return 1;

1739. }

1740. return 0;

1741. }

1743. ‌int LJ_FASTCALL recff_bit64_shift(jit_State *J, RecordFFData *rd)

1744. {

1745. CTState *cts = ctype_ctsG(J2G(J));

1746. CTypeID id;

1747. TRef tsh = 0;

1748. if (J->base[0] && tref_iscdata(J->base[1])) {

1749. tsh = crec_ct_tv(J, ctype_get(cts, CTID_INT64), 0,

1750. J->base[1], &rd->argv[1]);

1751. if (!tref_isinteger(tsh))

1752. tsh = emitconv(tsh, IRT_INT, tref_type(tsh), 0);

1753. J->base[1] = tsh;

1754. }

1755. id = crec_bit64_type(cts, &rd->argv[0]);

1756. if (id) {

1757. TRef tr = crec_ct_tv(J, ctype_get(cts, id), 0, J->base[0], &rd->argv[0]);

1758. uint32_t op = rd->data;

1759. if (!tsh) tsh = lj_opt_narrow_tobit(J, J->base[1]);

1760. if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) &&

1761. !tref_isk(tsh))

1762. tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 63));

1763. #ifdef LJ_TARGET_UNIFYROT

1764. if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) {

1765. op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR;

1766. tsh = emitir(IRTI(IR_NEG), tsh, tsh);

1767. }

1768. #endif

1769. tr = emitir(IRT(op, id-CTID_INT64+IRT_I64), tr, tsh);

1770. J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);

1771. return 1;

1772. }

1773. return 0;

1774. }

1776. ‌TRef recff_bit64_tohex(jit_State *J, RecordFFData *rd, TRef hdr)

1777. {

1778. CTState *cts = ctype_ctsG(J2G(J));

1779. CTypeID id = crec_bit64_type(cts, &rd->argv[0]);

1780. TRef tr, trsf = J->base[1];

1781. SFormat sf = (STRFMT_UINT|STRFMT_T_HEX);

1782. int32_t n;

1783. if (trsf) {

1784. CTypeID id2 = 0;

1785. n = (int32_t)lj_carith_check64(J->L, 2, &id2);

1786. if (id2)

1787. trsf = crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, trsf, &rd->argv[1]);

1788. else

1789. trsf = lj_opt_narrow_tobit(J, trsf);

1790. emitir(IRTGI(IR_EQ), trsf, lj_ir_kint(J, n));  /* Specialize to n. */

1791. } else {

1792. n = id ? 16 : 8;

1793. }

1794. if (n < 0) { n = -n; sf |= STRFMT_F_UPPER; }

1795. sf |= ((SFormat)((n+1)&255) << STRFMT_SH_PREC);

1796. if (id) {

1797. tr = crec_ct_tv(J, ctype_get(cts, id), 0, J->base[0], &rd->argv[0]);

1798. if (n < 16)

1799. tr = emitir(IRT(IR_BAND, IRT_U64), tr,

1800. lj_ir_kint64(J, ((uint64_t)1 << 4*n)-1));

1801. } else {

1802. tr = lj_opt_narrow_tobit(J, J->base[0]);

1803. if (n < 8)

1804. tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << 4*n)-1)));

1805. tr = emitconv(tr, IRT_U64, IRT_INT, 0);  /* No sign-extension. */

1806. lj_needsplit(J);

1807. }

1808. return lj_ir_call(J, IRCALL_lj_strfmt_putfxint, hdr, lj_ir_kint(J, sf), tr);

1809. }

1811. /* -- Miscellaneous library functions ------------------------------------- */

1813. ‌void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd)

1814. {

1815. CTState *cts = ctype_ctsG(J2G(J));

1816. CType *d, *ct = lj_ctype_rawref(cts, cdataV(&rd->argv[0])->ctypeid);

1817. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);

1818. if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {

1819. if (ctype_isinteger_or_bool(ct->info) && ct->size <= 4 &&

1820. !(ct->size == 4 && (ct->info & CTF_UNSIGNED)))

1821. d = ctype_get(cts, CTID_INT32);

1822. else

1823. d = ctype_get(cts, CTID_DOUBLE);

1824. J->base[0] = crec_ct_tv(J, d, 0, J->base[0], &rd->argv[0]);

1825. } else {

1826. J->base[0] = TREF_NIL;

1827. }

1828. }

1830. ‌#undef IR

1831. ‌#undef emitir

1832. ‌#undef emitconv

1834. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ffdef.h - luajit-2.0-src

Macros defined

- FFDEF

- FF_NUM_ASMFUNC

Source code

1. /* This is a generated file. DO NOT EDIT! */

3. FFDEF(assert)

4. FFDEF(type)

5. FFDEF(next)

6. FFDEF(pairs)

7. FFDEF(ipairs_aux)

8. FFDEF(ipairs)

9. FFDEF(getmetatable)

10. FFDEF(setmetatable)

11. FFDEF(getfenv)

12. FFDEF(setfenv)

13. FFDEF(rawget)

14. FFDEF(rawset)

15. FFDEF(rawequal)

16. FFDEF(unpack)

17. FFDEF(select)

18. FFDEF(tonumber)

19. FFDEF(tostring)

20. FFDEF(error)

21. FFDEF(pcall)

22. FFDEF(xpcall)

23. FFDEF(loadfile)

24. FFDEF(load)

25. FFDEF(loadstring)

26. FFDEF(dofile)

27. FFDEF(gcinfo)

28. FFDEF(collectgarbage)

29. FFDEF(newproxy)

30. FFDEF(print)

31. FFDEF(coroutine_status)

32. FFDEF(coroutine_running)

33. FFDEF(coroutine_create)

34. FFDEF(coroutine_yield)

35. FFDEF(coroutine_resume)

36. FFDEF(coroutine_wrap_aux)

37. FFDEF(coroutine_wrap)

38. FFDEF(math_abs)

39. FFDEF(math_floor)

40. FFDEF(math_ceil)

41. FFDEF(math_sqrt)

42. FFDEF(math_log10)

43. FFDEF(math_exp)

44. FFDEF(math_sin)

45. FFDEF(math_cos)

46. FFDEF(math_tan)

47. FFDEF(math_asin)

48. FFDEF(math_acos)

49. FFDEF(math_atan)

50. FFDEF(math_sinh)

51. FFDEF(math_cosh)

52. FFDEF(math_tanh)

53. FFDEF(math_frexp)

54. FFDEF(math_modf)

55. FFDEF(math_log)

56. FFDEF(math_atan2)

57. FFDEF(math_pow)

58. FFDEF(math_fmod)

59. FFDEF(math_ldexp)

60. FFDEF(math_min)

61. FFDEF(math_max)

62. FFDEF(math_random)

63. FFDEF(math_randomseed)

64. FFDEF(bit_tobit)

65. FFDEF(bit_bnot)

66. FFDEF(bit_bswap)

67. FFDEF(bit_lshift)

68. FFDEF(bit_rshift)

69. FFDEF(bit_arshift)

70. FFDEF(bit_rol)

71. FFDEF(bit_ror)

72. FFDEF(bit_band)

73. FFDEF(bit_bor)

74. FFDEF(bit_bxor)

75. FFDEF(bit_tohex)

76. FFDEF(string_byte)

77. FFDEF(string_char)

78. FFDEF(string_sub)

79. FFDEF(string_rep)

80. FFDEF(string_reverse)

81. FFDEF(string_lower)

82. FFDEF(string_upper)

83. FFDEF(string_dump)

84. FFDEF(string_find)

85. FFDEF(string_match)

86. FFDEF(string_gmatch_aux)

87. FFDEF(string_gmatch)

88. FFDEF(string_gsub)

89. FFDEF(string_format)

90. FFDEF(table_maxn)

91. FFDEF(table_insert)

92. FFDEF(table_concat)

93. FFDEF(table_sort)

94. FFDEF(table_new)

95. FFDEF(table_clear)

96. FFDEF(io_method_close)

97. FFDEF(io_method_read)

98. FFDEF(io_method_write)

99. FFDEF(io_method_flush)

100. FFDEF(io_method_seek)

101. FFDEF(io_method_setvbuf)

102. FFDEF(io_method_lines)

103. FFDEF(io_method___gc)

104. FFDEF(io_method___tostring)

105. FFDEF(io_open)

106. FFDEF(io_popen)

107. FFDEF(io_tmpfile)

108. FFDEF(io_close)

109. FFDEF(io_read)

110. FFDEF(io_write)

111. FFDEF(io_flush)

112. FFDEF(io_input)

113. FFDEF(io_output)

114. FFDEF(io_lines)

115. FFDEF(io_type)

116. FFDEF(os_execute)

117. FFDEF(os_remove)

118. FFDEF(os_rename)

119. FFDEF(os_tmpname)

120. FFDEF(os_getenv)

121. FFDEF(os_exit)

122. FFDEF(os_clock)

123. FFDEF(os_date)

124. FFDEF(os_time)

125. FFDEF(os_difftime)

126. FFDEF(os_setlocale)

127. FFDEF(debug_getregistry)

128. FFDEF(debug_getmetatable)

129. FFDEF(debug_setmetatable)

130. FFDEF(debug_getfenv)

131. FFDEF(debug_setfenv)

132. FFDEF(debug_getinfo)

133. FFDEF(debug_getlocal)

134. FFDEF(debug_setlocal)

135. FFDEF(debug_getupvalue)

136. FFDEF(debug_setupvalue)

137. FFDEF(debug_upvalueid)

138. FFDEF(debug_upvaluejoin)

139. FFDEF(debug_sethook)

140. FFDEF(debug_gethook)

141. FFDEF(debug_debug)

142. FFDEF(debug_traceback)

143. FFDEF(jit_on)

144. FFDEF(jit_off)

145. FFDEF(jit_flush)

146. FFDEF(jit_status)

147. FFDEF(jit_attach)

148. FFDEF(jit_util_funcinfo)

149. FFDEF(jit_util_funcbc)

150. FFDEF(jit_util_funck)

151. FFDEF(jit_util_funcuvname)

152. FFDEF(jit_util_traceinfo)

153. FFDEF(jit_util_traceir)

154. FFDEF(jit_util_tracek)

155. FFDEF(jit_util_tracesnap)

156. FFDEF(jit_util_tracemc)

157. FFDEF(jit_util_traceexitstub)

158. FFDEF(jit_util_ircalladdr)

159. FFDEF(jit_opt_start)

160. FFDEF(jit_profile_start)

161. FFDEF(jit_profile_stop)

162. FFDEF(jit_profile_dumpstack)

163. FFDEF(ffi_meta___index)

164. FFDEF(ffi_meta___newindex)

165. FFDEF(ffi_meta___eq)

166. FFDEF(ffi_meta___len)

167. FFDEF(ffi_meta___lt)

168. FFDEF(ffi_meta___le)

169. FFDEF(ffi_meta___concat)

170. FFDEF(ffi_meta___call)

171. FFDEF(ffi_meta___add)

172. FFDEF(ffi_meta___sub)

173. FFDEF(ffi_meta___mul)

174. FFDEF(ffi_meta___div)

175. FFDEF(ffi_meta___mod)

176. FFDEF(ffi_meta___pow)

177. FFDEF(ffi_meta___unm)

178. FFDEF(ffi_meta___tostring)

179. FFDEF(ffi_meta___pairs)

180. FFDEF(ffi_meta___ipairs)

181. FFDEF(ffi_clib___index)

182. FFDEF(ffi_clib___newindex)

183. FFDEF(ffi_clib___gc)

184. FFDEF(ffi_callback_free)

185. FFDEF(ffi_callback_set)

186. FFDEF(ffi_cdef)

187. FFDEF(ffi_new)

188. FFDEF(ffi_cast)

189. FFDEF(ffi_typeof)

190. FFDEF(ffi_typeinfo)

191. FFDEF(ffi_istype)

192. FFDEF(ffi_sizeof)

193. FFDEF(ffi_alignof)

194. FFDEF(ffi_offsetof)

195. FFDEF(ffi_errno)

196. FFDEF(ffi_string)

197. FFDEF(ffi_copy)

198. FFDEF(ffi_fill)

199. FFDEF(ffi_abi)

200. FFDEF(ffi_metatype)

201. FFDEF(ffi_gc)

202. FFDEF(ffi_load)

204. ‌#undef FFDEF

206. #ifndef FF_NUM_ASMFUNC

207. ‌#define FF_NUM_ASMFUNC 57

208. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_libdef.h - luajit-2.0-src

Global variables defined

- lj_lib_cf_base

- lj_lib_cf_bit

- lj_lib_cf_coroutine

- lj_lib_cf_debug

- lj_lib_cf_ffi

- lj_lib_cf_ffi_callback

- lj_lib_cf_ffi_clib

- lj_lib_cf_ffi_meta

- lj_lib_cf_io

- lj_lib_cf_io_method

- lj_lib_cf_jit

- lj_lib_cf_jit_opt

- lj_lib_cf_jit_profile

- lj_lib_cf_jit_util

- lj_lib_cf_math

- lj_lib_cf_os

- lj_lib_cf_string

- lj_lib_cf_table

- lj_lib_init_base

- lj_lib_init_bit

- lj_lib_init_coroutine

- lj_lib_init_debug

- lj_lib_init_ffi

- lj_lib_init_ffi_callback

- lj_lib_init_ffi_clib

- lj_lib_init_ffi_meta

- lj_lib_init_io

- lj_lib_init_io_method

- lj_lib_init_jit

- lj_lib_init_jit_opt

- lj_lib_init_jit_profile

- lj_lib_init_jit_util

- lj_lib_init_math

- lj_lib_init_os

- lj_lib_init_string

- lj_lib_init_table

Macros defined

- LJLIB_MODULE_base

- LJLIB_MODULE_bit

- LJLIB_MODULE_coroutine

- LJLIB_MODULE_debug

- LJLIB_MODULE_ffi

- LJLIB_MODULE_ffi_callback

- LJLIB_MODULE_ffi_clib

- LJLIB_MODULE_ffi_meta

- LJLIB_MODULE_io

- LJLIB_MODULE_io_method

- LJLIB_MODULE_jit

- LJLIB_MODULE_jit_opt

- LJLIB_MODULE_jit_profile

- LJLIB_MODULE_jit_util

- LJLIB_MODULE_math

- LJLIB_MODULE_os

- LJLIB_MODULE_string

- LJLIB_MODULE_table

Source code

1. /* This is a generated file. DO NOT EDIT! */

3. #ifdef LJLIB_MODULE_base

4. ‌#undef LJLIB_MODULE_base

5. ‌static const lua_CFunction lj_lib_cf_base[] = {

6. lj_ffh_assert,

7. lj_ffh_next,

8. lj_ffh_pairs,

9. lj_ffh_ipairs_aux,

10. lj_ffh_ipairs,

11. lj_ffh_setmetatable,

12. lj_cf_getfenv,

13. lj_cf_setfenv,

14. lj_ffh_rawget,

15. lj_cf_rawset,

16. lj_cf_rawequal,

17. lj_cf_unpack,

18. lj_cf_select,

19. lj_ffh_tonumber,

20. lj_ffh_tostring,

21. lj_cf_error,

22. lj_ffh_pcall,

23. lj_cf_loadfile,

24. lj_cf_load,

25. lj_cf_loadstring,

26. lj_cf_dofile,

27. lj_cf_gcinfo,

28. lj_cf_collectgarbage,

29. lj_cf_newproxy,

30. lj_cf_print

31. };

32. ‌static const uint8_t lj_lib_init_base[] = {

33. 2,0,28,70,97,115,115,101,114,116,195,110,105,108,199,98,111,111,108,101,97,

34. 110,252,1,200,117,115,101,114,100,97,116,97,198,115,116,114,105,110,103,197,

35. 117,112,118,97,108,198,116,104,114,101,97,100,197,112,114,111,116,111,200,102,

36. 117,110,99,116,105,111,110,197,116,114,97,99,101,197,99,100,97,116,97,197,116,

37. 97,98,108,101,252,9,198,110,117,109,98,101,114,132,116,121,112,101,68,110,101,

38. 120,116,253,69,112,97,105,114,115,64,253,70,105,112,97,105,114,115,140,103,

39. 101,116,109,101,116,97,116,97,98,108,101,76,115,101,116,109,101,116,97,116,

40. 97,98,108,101,7,103,101,116,102,101,110,118,7,115,101,116,102,101,110,118,70,

41. 114,97,119,103,101,116,6,114,97,119,115,101,116,8,114,97,119,101,113,117,97,

42. 108,6,117,110,112,97,99,107,6,115,101,108,101,99,116,72,116,111,110,117,109,

43. 98,101,114,72,116,111,115,116,114,105,110,103,5,101,114,114,111,114,69,112,

44. 99,97,108,108,134,120,112,99,97,108,108,8,108,111,97,100,102,105,108,101,4,

45. 108,111,97,100,10,108,111,97,100,115,116,114,105,110,103,6,100,111,102,105,

46. 108,101,6,103,99,105,110,102,111,14,99,111,108,108,101,99,116,103,97,114,98,

47. 97,103,101,252,2,8,110,101,119,112,114,111,120,121,200,116,111,115,116,114,

48. 105,110,103,5,112,114,105,110,116,252,3,200,95,86,69,82,83,73,79,78,250,255

49. };

50. #endif

52. #ifdef LJLIB_MODULE_coroutine

53. ‌#undef LJLIB_MODULE_coroutine

54. ‌static const lua_CFunction lj_lib_cf_coroutine[] = {

55. lj_cf_coroutine_status,

56. lj_cf_coroutine_running,

57. lj_cf_coroutine_create,

58. lj_ffh_coroutine_yield,

59. lj_ffh_coroutine_resume,

60. lj_cf_coroutine_wrap

61. };

62. ‌static const uint8_t lj_lib_init_coroutine[] = {

63. 30,13,6,6,115,116,97,116,117,115,7,114,117,110,110,105,110,103,6,99,114,101,

64. 97,116,101,69,121,105,101,108,100,70,114,101,115,117,109,101,254,4,119,114,

65. 97,112,255

66. };

67. #endif

69. #ifdef LJLIB_MODULE_math

70. ‌#undef LJLIB_MODULE_math

71. ‌static const lua_CFunction lj_lib_cf_math[] = {

72. lj_ffh_math_abs,

73. lj_ffh_math_sqrt,

74. lj_ffh_math_log,

75. lj_ffh_math_atan2,

76. lj_ffh_math_ldexp,

77. lj_ffh_math_min,

78. lj_cf_math_random,

79. lj_cf_math_randomseed

80. };

81. ‌static const uint8_t lj_lib_init_math[] = {

82. 37,16,30,67,97,98,115,133,102,108,111,111,114,132,99,101,105,108,68,115,113,

83. 114,116,133,108,111,103,49,48,131,101,120,112,131,115,105,110,131,99,111,115,

84. 131,116,97,110,132,97,115,105,110,132,97,99,111,115,132,97,116,97,110,132,115,

85. 105,110,104,132,99,111,115,104,132,116,97,110,104,133,102,114,101,120,112,132,

86. 109,111,100,102,67,108,111,103,249,3,100,101,103,0,1,2,0,0,1,2,24,1,0,0,76,

87. 1,2,0,241,135,158,166,3,220,203,178,130,4,249,3,114,97,100,0,1,2,0,0,1,2,24,

88. 1,0,0,76,1,2,0,243,244,148,165,20,198,190,199,252,3,69,97,116,97,110,50,131,

89. 112,111,119,132,102,109,111,100,69,108,100,101,120,112,67,109,105,110,131,109,

90. 97,120,251,24,45,68,84,251,33,9,64,194,112,105,250,251,0,0,0,0,0,0,240,127,

91. 196,104,117,103,101,250,252,2,6,114,97,110,100,111,109,252,2,10,114,97,110,

92. 100,111,109,115,101,101,100,255

93. };

94. #endif

96. #ifdef LJLIB_MODULE_bit

97. ‌#undef LJLIB_MODULE_bit

98. ‌static const lua_CFunction lj_lib_cf_bit[] = {

99. lj_ffh_bit_tobit,

100. lj_ffh_bit_bnot,

101. lj_ffh_bit_bswap,

102. lj_ffh_bit_lshift,

103. lj_ffh_bit_band,

104. lj_cf_bit_tohex

105. };

106. ‌static const uint8_t lj_lib_init_bit[] = {

107. 63,40,12,69,116,111,98,105,116,68,98,110,111,116,69,98,115,119,97,112,70,108,

108. 115,104,105,102,116,134,114,115,104,105,102,116,135,97,114,115,104,105,102,

109. 116,131,114,111,108,131,114,111,114,68,98,97,110,100,131,98,111,114,132,98,

110. 120,111,114,5,116,111,104,101,120,255

111. };

112. #endif

114. #ifdef LJLIB_MODULE_string

115. ‌#undef LJLIB_MODULE_string

116. ‌static const lua_CFunction lj_lib_cf_string[] = {

117. lj_ffh_string_byte,

118. lj_ffh_string_char,

119. lj_ffh_string_sub,

120. lj_cf_string_rep,

121. lj_ffh_string_reverse,

122. lj_cf_string_dump,

123. lj_cf_string_find,

124. lj_cf_string_match,

125. lj_cf_string_gmatch,

126. lj_cf_string_gsub,

127. lj_cf_string_format

128. };

129. ‌static const uint8_t lj_lib_init_string[] = {

130. 75,51,14,249,3,108,101,110,0,1,2,0,0,0,3,16,0,5,0,21,1,0,0,76,1,2,0,68,98,121,

131. 116,101,68,99,104,97,114,67,115,117,98,3,114,101,112,71,114,101,118,101,114,

132. 115,101,133,108,111,119,101,114,133,117,112,112,101,114,4,100,117,109,112,4,

133. 102,105,110,100,5,109,97,116,99,104,254,6,103,109,97,116,99,104,4,103,115,117,

134. 98,6,102,111,114,109,97,116,255

135. };

136. #endif

138. #ifdef LJLIB_MODULE_table

139. ‌#undef LJLIB_MODULE_table

140. ‌static const lua_CFunction lj_lib_cf_table[] = {

141. lj_cf_table_maxn,

142. lj_cf_table_insert,

143. lj_cf_table_concat,

144. lj_cf_table_sort

145. };

146. ‌static const uint8_t lj_lib_init_table[] = {

147. 89,57,8,249,8,102,111,114,101,97,99,104,105,0,2,9,0,0,0,15,16,0,12,0,16,1,9,

148. 0,41,2,1,0,21,3,0,0,41,4,1,0,77,2,8,128,18,6,1,0,18,7,5,0,59,8,5,0,66,6,3,2,

149. 10,6,0,0,88,7,1,128,76,6,2,0,79,2,248,127,75,0,1,0,249,7,102,111,114,101,97,

150. 99,104,0,2,10,0,0,0,16,16,0,12,0,16,1,9,0,43,2,0,0,18,3,0,0,41,4,0,0,88,5,7,

151. 128,18,7,1,0,18,8,5,0,18,9,6,0,66,7,3,2,10,7,0,0,88,8,1,128,76,7,2,0,70,5,3,

152. 3,82,5,247,127,75,0,1,0,249,4,103,101,116,110,0,1,2,0,0,0,3,16,0,12,0,21,1,

153. 0,0,76,1,2,0,4,109,97,120,110,6,105,110,115,101,114,116,249,6,114,101,109,111,

154. 118,101,0,2,10,0,0,2,30,16,0,12,0,21,2,0,0,11,1,0,0,88,3,7,128,8,2,0,0,88,3,

155. 23,128,59,3,2,0,43,4,0,0,64,4,2,0,76,3,2,0,88,3,18,128,16,1,14,0,41,3,1,0,3,

156. 3,1,0,88,3,14,128,3,1,2,0,88,3,12,128,59,3,1,0,22,4,1,1,18,5,2,0,41,6,1,0,77,

157. 4,4,128,23,8,1,7,59,9,7,0,64,9,8,0,79,4,252,127,43,4,0,0,64,4,2,0,76,3,2,0,

158. 75,0,1,0,0,2,6,99,111,110,99,97,116,4,115,111,114,116,254,254,255

159. };

160. #endif

162. #ifdef LJLIB_MODULE_io_method

163. ‌#undef LJLIB_MODULE_io_method

164. ‌static const lua_CFunction lj_lib_cf_io_method[] = {

165. lj_cf_io_method_close,

166. lj_cf_io_method_read,

167. lj_cf_io_method_write,

168. lj_cf_io_method_flush,

169. lj_cf_io_method_seek,

170. lj_cf_io_method_setvbuf,

171. lj_cf_io_method_lines,

172. lj_cf_io_method___gc,

173. lj_cf_io_method___tostring

174. };

175. ‌static const uint8_t lj_lib_init_io_method[] = {

176. 95,57,10,5,99,108,111,115,101,4,114,101,97,100,5,119,114,105,116,101,5,102,

177. 108,117,115,104,4,115,101,101,107,7,115,101,116,118,98,117,102,5,108,105,110,

178. 101,115,4,95,95,103,99,10,95,95,116,111,115,116,114,105,110,103,252,1,199,95,

179. 95,105,110,100,101,120,250,255

180. };

181. #endif

183. #ifdef LJLIB_MODULE_io

184. ‌#undef LJLIB_MODULE_io

185. ‌static const lua_CFunction lj_lib_cf_io[] = {

186. lj_cf_io_open,

187. lj_cf_io_popen,

188. lj_cf_io_tmpfile,

189. lj_cf_io_close,

190. lj_cf_io_read,

191. lj_cf_io_write,

192. lj_cf_io_flush,

193. lj_cf_io_input,

194. lj_cf_io_output,

195. lj_cf_io_lines,

196. lj_cf_io_type

197. };

198. ‌static const uint8_t lj_lib_init_io[] = {

199. 104,57,12,252,2,192,250,4,111,112,101,110,5,112,111,112,101,110,7,116,109,112,

200. 102,105,108,101,5,99,108,111,115,101,4,114,101,97,100,5,119,114,105,116,101,

201. 5,102,108,117,115,104,5,105,110,112,117,116,6,111,117,116,112,117,116,5,108,

202. 105,110,101,115,4,116,121,112,101,255

203. };

204. #endif

206. #ifdef LJLIB_MODULE_os

207. ‌#undef LJLIB_MODULE_os

208. ‌static const lua_CFunction lj_lib_cf_os[] = {

209. lj_cf_os_execute,

210. lj_cf_os_remove,

211. lj_cf_os_rename,

212. lj_cf_os_tmpname,

213. lj_cf_os_getenv,

214. lj_cf_os_exit,

215. lj_cf_os_clock,

216. lj_cf_os_date,

217. lj_cf_os_time,

218. lj_cf_os_difftime,

219. lj_cf_os_setlocale

220. };

221. ‌static const uint8_t lj_lib_init_os[] = {

222. 115,57,11,7,101,120,101,99,117,116,101,6,114,101,109,111,118,101,6,114,101,

223. 110,97,109,101,7,116,109,112,110,97,109,101,6,103,101,116,101,110,118,4,101,

224. 120,105,116,5,99,108,111,99,107,4,100,97,116,101,4,116,105,109,101,8,100,105,

225. 102,102,116,105,109,101,9,115,101,116,108,111,99,97,108,101,255

226. };

227. #endif

229. #ifdef LJLIB_MODULE_debug

230. ‌#undef LJLIB_MODULE_debug

231. ‌static const lua_CFunction lj_lib_cf_debug[] = {

232. lj_cf_debug_getregistry,

233. lj_cf_debug_getmetatable,

234. lj_cf_debug_setmetatable,

235. lj_cf_debug_getfenv,

236. lj_cf_debug_setfenv,

237. lj_cf_debug_getinfo,

238. lj_cf_debug_getlocal,

239. lj_cf_debug_setlocal,

240. lj_cf_debug_getupvalue,

241. lj_cf_debug_setupvalue,

242. lj_cf_debug_upvalueid,

243. lj_cf_debug_upvaluejoin,

244. lj_cf_debug_sethook,

245. lj_cf_debug_gethook,

246. lj_cf_debug_debug,

247. lj_cf_debug_traceback

248. };

249. ‌static const uint8_t lj_lib_init_debug[] = {

250. 126,57,16,11,103,101,116,114,101,103,105,115,116,114,121,12,103,101,116,109,

251. 101,116,97,116,97,98,108,101,12,115,101,116,109,101,116,97,116,97,98,108,101,

252. 7,103,101,116,102,101,110,118,7,115,101,116,102,101,110,118,7,103,101,116,105,

253. 110,102,111,8,103,101,116,108,111,99,97,108,8,115,101,116,108,111,99,97,108,

254. 10,103,101,116,117,112,118,97,108,117,101,10,115,101,116,117,112,118,97,108,

255. 117,101,9,117,112,118,97,108,117,101,105,100,11,117,112,118,97,108,117,101,

256. 106,111,105,110,7,115,101,116,104,111,111,107,7,103,101,116,104,111,111,107,

257. 5,100,101,98,117,103,9,116,114,97,99,101,98,97,99,107,255

258. };

259. #endif

261. #ifdef LJLIB_MODULE_jit

262. ‌#undef LJLIB_MODULE_jit

263. ‌static const lua_CFunction lj_lib_cf_jit[] = {

264. lj_cf_jit_on,

265. lj_cf_jit_off,

266. lj_cf_jit_flush,

267. lj_cf_jit_status,

268. lj_cf_jit_attach

269. };

270. ‌static const uint8_t lj_lib_init_jit[] = {

271. 142,57,9,2,111,110,3,111,102,102,5,102,108,117,115,104,6,115,116,97,116,117,

272. 115,6,97,116,116,97,99,104,252,5,194,111,115,250,252,4,196,97,114,99,104,250,

273. 252,3,203,118,101,114,115,105,111,110,95,110,117,109,250,252,2,199,118,101,

274. 114,115,105,111,110,250,255

275. };

276. #endif

278. #ifdef LJLIB_MODULE_jit_util

279. ‌#undef LJLIB_MODULE_jit_util

280. ‌static const lua_CFunction lj_lib_cf_jit_util[] = {

281. lj_cf_jit_util_funcinfo,

282. lj_cf_jit_util_funcbc,

283. lj_cf_jit_util_funck,

284. lj_cf_jit_util_funcuvname,

285. lj_cf_jit_util_traceinfo,

286. lj_cf_jit_util_traceir,

287. lj_cf_jit_util_tracek,

288. lj_cf_jit_util_tracesnap,

289. lj_cf_jit_util_tracemc,

290. lj_cf_jit_util_traceexitstub,

291. lj_cf_jit_util_ircalladdr

292. };

293. ‌static const uint8_t lj_lib_init_jit_util[] = {

294. 147,57,11,8,102,117,110,99,105,110,102,111,6,102,117,110,99,98,99,5,102,117,

295. 110,99,107,10,102,117,110,99,117,118,110,97,109,101,9,116,114,97,99,101,105,

296. 110,102,111,7,116,114,97,99,101,105,114,6,116,114,97,99,101,107,9,116,114,97,

297. 99,101,115,110,97,112,7,116,114,97,99,101,109,99,13,116,114,97,99,101,101,120,

298. 105,116,115,116,117,98,10,105,114,99,97,108,108,97,100,100,114,255

299. };

300. #endif

302. #ifdef LJLIB_MODULE_jit_opt

303. ‌#undef LJLIB_MODULE_jit_opt

304. ‌static const lua_CFunction lj_lib_cf_jit_opt[] = {

305. lj_cf_jit_opt_start

306. };

307. ‌static const uint8_t lj_lib_init_jit_opt[] = {

308. 158,57,1,5,115,116,97,114,116,255

309. };

310. #endif

312. #ifdef LJLIB_MODULE_jit_profile

313. ‌#undef LJLIB_MODULE_jit_profile

314. ‌static const lua_CFunction lj_lib_cf_jit_profile[] = {

315. lj_cf_jit_profile_start,

316. lj_cf_jit_profile_stop,

317. lj_cf_jit_profile_dumpstack

318. };

319. ‌static const uint8_t lj_lib_init_jit_profile[] = {

320. 159,57,3,5,115,116,97,114,116,4,115,116,111,112,9,100,117,109,112,115,116,97,

321. 99,107,255

322. };

323. #endif

325. #ifdef LJLIB_MODULE_ffi_meta

326. ‌#undef LJLIB_MODULE_ffi_meta

327. ‌static const lua_CFunction lj_lib_cf_ffi_meta[] = {

328. lj_cf_ffi_meta___index,

329. lj_cf_ffi_meta___newindex,

330. lj_cf_ffi_meta___eq,

331. lj_cf_ffi_meta___len,

332. lj_cf_ffi_meta___lt,

333. lj_cf_ffi_meta___le,

334. lj_cf_ffi_meta___concat,

335. lj_cf_ffi_meta___call,

336. lj_cf_ffi_meta___add,

337. lj_cf_ffi_meta___sub,

338. lj_cf_ffi_meta___mul,

339. lj_cf_ffi_meta___div,

340. lj_cf_ffi_meta___mod,

341. lj_cf_ffi_meta___pow,

342. lj_cf_ffi_meta___unm,

343. lj_cf_ffi_meta___tostring,

344. lj_cf_ffi_meta___pairs,

345. lj_cf_ffi_meta___ipairs

346. };

347. ‌static const uint8_t lj_lib_init_ffi_meta[] = {

348. 162,57,19,7,95,95,105,110,100,101,120,10,95,95,110,101,119,105,110,100,101,

349. 120,4,95,95,101,113,5,95,95,108,101,110,4,95,95,108,116,4,95,95,108,101,8,95,

350. 95,99,111,110,99,97,116,6,95,95,99,97,108,108,5,95,95,97,100,100,5,95,95,115,

351. 117,98,5,95,95,109,117,108,5,95,95,100,105,118,5,95,95,109,111,100,5,95,95,

352. 112,111,119,5,95,95,117,110,109,10,95,95,116,111,115,116,114,105,110,103,7,

353. 95,95,112,97,105,114,115,8,95,95,105,112,97,105,114,115,195,102,102,105,203,

354. 95,95,109,101,116,97,116,97,98,108,101,250,255

355. };

356. #endif

358. #ifdef LJLIB_MODULE_ffi_clib

359. ‌#undef LJLIB_MODULE_ffi_clib

360. ‌static const lua_CFunction lj_lib_cf_ffi_clib[] = {

361. lj_cf_ffi_clib___index,

362. lj_cf_ffi_clib___newindex,

363. lj_cf_ffi_clib___gc

364. };

365. ‌static const uint8_t lj_lib_init_ffi_clib[] = {

366. 180,57,3,7,95,95,105,110,100,101,120,10,95,95,110,101,119,105,110,100,101,120,

367. 4,95,95,103,99,255

368. };

369. #endif

371. #ifdef LJLIB_MODULE_ffi_callback

372. ‌#undef LJLIB_MODULE_ffi_callback

373. ‌static const lua_CFunction lj_lib_cf_ffi_callback[] = {

374. lj_cf_ffi_callback_free,

375. lj_cf_ffi_callback_set

376. };

377. ‌static const uint8_t lj_lib_init_ffi_callback[] = {

378. 183,57,3,4,102,114,101,101,3,115,101,116,252,1,199,95,95,105,110,100,101,120,

379. 250,255

380. };

381. #endif

383. #ifdef LJLIB_MODULE_ffi

384. ‌#undef LJLIB_MODULE_ffi

385. ‌static const lua_CFunction lj_lib_cf_ffi[] = {

386. lj_cf_ffi_cdef,

387. lj_cf_ffi_new,

388. lj_cf_ffi_cast,

389. lj_cf_ffi_typeof,

390. lj_cf_ffi_typeinfo,

391. lj_cf_ffi_istype,

392. lj_cf_ffi_sizeof,

393. lj_cf_ffi_alignof,

394. lj_cf_ffi_offsetof,

395. lj_cf_ffi_errno,

396. lj_cf_ffi_string,

397. lj_cf_ffi_copy,

398. lj_cf_ffi_fill,

399. lj_cf_ffi_abi,

400. lj_cf_ffi_metatype,

401. lj_cf_ffi_gc,

402. lj_cf_ffi_load

403. };

404. ‌static const uint8_t lj_lib_init_ffi[] = {

405. 185,57,23,4,99,100,101,102,3,110,101,119,4,99,97,115,116,6,116,121,112,101,

406. 111,102,8,116,121,112,101,105,110,102,111,6,105,115,116,121,112,101,6,115,105,

407. 122,101,111,102,7,97,108,105,103,110,111,102,8,111,102,102,115,101,116,111,

408. 102,5,101,114,114,110,111,6,115,116,114,105,110,103,4,99,111,112,121,4,102,

409. 105,108,108,3,97,98,105,252,8,192,250,8,109,101,116,97,116,121,112,101,252,

410. 7,192,250,2,103,99,252,5,192,250,4,108,111,97,100,252,4,193,67,250,252,3,194,

411. 111,115,250,252,2,196,97,114,99,104,250,255

412. };

413. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/host/buildvm_fold.c - luajit-2.0-src

Global variables defined

- foldkeys

- funcidx

- lineno

- nkeys

Functions defined

- emit_fold

- foldrule

- makehash

- nexttoken

- printhash

- tryhash

Source code

1. /*

2. ** LuaJIT VM builder: IR folding hash table generator.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "buildvm.h"

7. #include "lj_obj.h"

8. #include "lj_ir.h"

10. /* Context for the folding hash table generator. */

11. ‌static int lineno;

12. ‌static int funcidx;

13. ‌static uint32_t foldkeys[BUILD_MAX_FOLD];

14. ‌static uint32_t nkeys;

16. /* Try to fill the hash table with keys using the hash parameters. */

17. ‌static int tryhash(uint32_t *htab, uint32_t sz, uint32_t r, int dorol)

18. {

19. uint32_t i;

20. if (dorol && ((r & 31) == 0 || (r>>5) == 0))

21. return 0;  /* Avoid zero rotates. */

22. memset(htab, 0xff, (sz+1)*sizeof(uint32_t));

23. for (i = 0; i < nkeys; i++) {

24. uint32_t key = foldkeys[i];

25. uint32_t k = key & 0xffffff;

26. uint32_t h = (dorol ? lj_rol(lj_rol(k, r>>5) - k, r&31) :

27. (((k << (r>>5)) - k) << (r&31))) % sz;

28. if (htab[h] != 0xffffffff) {  /* Collision on primary slot. */

29. if (htab[h+1] != 0xffffffff) {  /* Collision on secondary slot. */

30. /* Try to move the colliding key, if possible. */

31. if (h < sz-1 && htab[h+2] == 0xffffffff) {

32. uint32_t k2 = htab[h+1] & 0xffffff;

33. uint32_t h2 = (dorol ? lj_rol(lj_rol(k2, r>>5) - k2, r&31) :

34. (((k2 << (r>>5)) - k2) << (r&31))) % sz;

35. if (h2 != h+1) return 0;  /* Cannot resolve collision. */

36. htab[h+2] = htab[h+1];  /* Move colliding key to secondary slot. */

37. } else {

38. return 0;  /* Collision. */

39. }

40. }

41. htab[h+1] = key;

42. } else {

43. htab[h] = key;

44. }

45. }

46. return 1;  /* Success, all keys could be stored. */

47. }

49. /* Print the generated hash table. */

50. ‌static void printhash(BuildCtx *ctx, uint32_t *htab, uint32_t sz)

51. {

52. uint32_t i;

53. fprintf(ctx->fp, "static const uint32_t fold_hash[%d] = {\n0x%08x",

54. sz+1, htab[0]);

55. for (i = 1; i < sz+1; i++)

56. fprintf(ctx->fp, ",\n0x%08x", htab[i]);

57. fprintf(ctx->fp, "\n};\n\n");

58. }

60. /* Exhaustive search for the shortest semi-perfect hash table. */

61. ‌static void makehash(BuildCtx *ctx)

62. {

63. uint32_t htab[BUILD_MAX_FOLD*2+1];

64. uint32_t sz, r;

65. /* Search for the smallest hash table with an odd size. */

66. for (sz = (nkeys|1); sz < BUILD_MAX_FOLD*2; sz += 2) {

67. /* First try all shift hash combinations. */

68. for (r = 0; r < 32*32; r++) {

69. if (tryhash(htab, sz, r, 0)) {

70. printhash(ctx, htab, sz);

71. fprintf(ctx->fp,

72. "#define fold_hashkey(k)\t(((((k)<<%u)-(k))<<%u)%%%u)\n\n",

73. r>>5, r&31, sz);

74. return;

75. }

76. }

77. /* Then try all rotate hash combinations. */

78. for (r = 0; r < 32*32; r++) {

79. if (tryhash(htab, sz, r, 1)) {

80. printhash(ctx, htab, sz);

81. fprintf(ctx->fp,

82. "#define fold_hashkey(k)\t(lj_rol(lj_rol((k),%u)-(k),%u)%%%u)\n\n",

83. r>>5, r&31, sz);

84. return;

85. }

86. }

87. }

88. fprintf(stderr, "Error: search for perfect hash failed\n");

89. exit(1);

90. }

92. /* Parse one token of a fold rule. */

93. ‌static uint32_t nexttoken(char **pp, int allowlit, int allowany)

94. {

95. char *p = *pp;

96. if (p) {

97. uint32_t i;

98. char *q = strchr(p, ' ');

99. if (q) *q++ = '\0';

100. *pp = q;

101. if (allowlit && !strncmp(p, "IRFPM_", 6)) {

102. for (i = 0; irfpm_names[i]; i++)

103. if (!strcmp(irfpm_names[i], p+6))

104. return i;

105. } else if (allowlit && !strncmp(p, "IRFL_", 5)) {

106. for (i = 0; irfield_names[i]; i++)

107. if (!strcmp(irfield_names[i], p+5))

108. return i;

109. } else if (allowlit && !strncmp(p, "IRCALL_", 7)) {

110. for (i = 0; ircall_names[i]; i++)

111. if (!strcmp(ircall_names[i], p+7))

112. return i;

113. } else if (allowlit && !strncmp(p, "IRCONV_", 7)) {

114. for (i = 0; irt_names[i]; i++) {

115. const char *r = strchr(p+7, '_');

116. if (r && !strncmp(irt_names[i], p+7, r-(p+7))) {

117. uint32_t j;

118. for (j = 0; irt_names[j]; j++)

119. if (!strcmp(irt_names[j], r+1))

120. return (i << 5) + j;

121. }

122. }

123. } else if (allowlit && *p >= '0' && *p <= '9') {

124. for (i = 0; *p >= '0' && *p <= '9'; p++)

125. i = i*10 + (*p - '0');

126. if (*p == '\0')

127. return i;

128. } else if (allowany && !strcmp("any", p)) {

129. return allowany;

130. } else {

131. for (i = 0; ir_names[i]; i++)

132. if (!strcmp(ir_names[i], p))

133. return i;

134. }

135. fprintf(stderr, "Error: bad fold definition token \"%s\" at line %d\n", p, lineno);

136. exit(1);

137. }

138. return 0;

139. }

141. /* Parse a fold rule. */

142. ‌static void foldrule(char *p)

143. {

144. uint32_t op = nexttoken(&p, 0, 0);

145. uint32_t left = nexttoken(&p, 0, 0x7f);

146. uint32_t right = nexttoken(&p, 1, 0x3ff);

147. uint32_t key = (funcidx << 24) | (op << 17) | (left << 10) | right;

148. uint32_t i;

149. if (nkeys >= BUILD_MAX_FOLD) {

150. fprintf(stderr, "Error: too many fold rules, increase BUILD_MAX_FOLD.\n");

151. exit(1);

152. }

153. /* Simple insertion sort to detect duplicates. */

154. for (i = nkeys; i > 0; i--) {

155. if ((foldkeys[i-1]&0xffffff) < (key & 0xffffff))

156. break;

157. if ((foldkeys[i-1]&0xffffff) == (key & 0xffffff)) {

158. fprintf(stderr, "Error: duplicate fold definition at line %d\n", lineno);

159. exit(1);

160. }

161. foldkeys[i] = foldkeys[i-1];

162. }

163. foldkeys[i] = key;

164. nkeys++;

165. }

167. /* Emit C source code for IR folding hash table. */

168. ‌void emit_fold(BuildCtx *ctx)

169. {

170. char buf[256];  /* We don't care about analyzing lines longer than that. */

171. const char *fname = ctx->args[0];

172. FILE *fp;

174. if (fname == NULL) {

175. fprintf(stderr, "Error: missing input filename\n");

176. exit(1);

177. }

179. if (fname[0] == '-' && fname[1] == '\0') {

180. fp = stdin;

181. } else {

182. fp = fopen(fname, "r");

183. if (!fp) {

184. fprintf(stderr, "Error: cannot open input file '%s': %s\n",

185. fname, strerror(errno));

186. exit(1);

187. }

188. }

190. fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n");

191. fprintf(ctx->fp, "static const FoldFunc fold_func[] = {\n");

193. lineno = 0;

194. funcidx = 0;

195. nkeys = 0;

196. while (fgets(buf, sizeof(buf), fp) != NULL) {

197. lineno++;

198. /* The prefix must be at the start of a line, otherwise it's ignored. */

199. if (!strncmp(buf, FOLDDEF_PREFIX, sizeof(FOLDDEF_PREFIX)-1)) {

200. char *p = buf+sizeof(FOLDDEF_PREFIX)-1;

201. char *q = strchr(p, ')');

202. if (p[0] == '(' && q) {

203. p++;

204. *q = '\0';

205. foldrule(p);

206. } else if ((p[0] == 'F' || p[0] == 'X') && p[1] == '(' && q) {

207. p += 2;

208. *q = '\0';

209. if (funcidx)

210. fprintf(ctx->fp, ",\n");

211. if (p[-2] == 'X')

212. fprintf(ctx->fp, "  %s", p);

213. else

214. fprintf(ctx->fp, "  fold_%s", p);

215. funcidx++;

216. } else {

217. buf[strlen(buf)-1] = '\0';

218. fprintf(stderr, "Error: unknown fold definition tag %s%s at line %d\n",

219. FOLDDEF_PREFIX, p, lineno);

220. exit(1);

221. }

222. }

223. }

224. fclose(fp);

225. fprintf(ctx->fp, "\n};\n\n");

227. makehash(ctx);

228. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_folddef.h - luajit-2.0-src

Global variables defined

- fold_func

- fold_hash

Macros defined

- fold_hashkey

Source code

1. /* This is a generated file. DO NOT EDIT! */

3. ‌static const FoldFunc fold_func[] = {

4. fold_kfold_numarith,

5. fold_kfold_ldexp,

6. fold_kfold_fpmath,

7. fold_kfold_numpow,

8. fold_kfold_numcomp,

9. fold_kfold_intarith,

10. fold_kfold_intovarith,

11. fold_kfold_bnot,

12. fold_kfold_bswap,

13. fold_kfold_intcomp,

14. fold_kfold_intcomp0,

15. fold_kfold_int64arith,

16. fold_kfold_int64arith2,

17. fold_kfold_int64shift,

18. fold_kfold_bnot64,

19. fold_kfold_bswap64,

20. fold_kfold_int64comp,

21. fold_kfold_int64comp0,

22. fold_kfold_snew_kptr,

23. fold_kfold_snew_empty,

24. fold_kfold_strref,

25. fold_kfold_strref_snew,

26. fold_kfold_strcmp,

27. fold_bufput_append,

28. fold_bufput_kgc,

29. fold_bufstr_kfold_cse,

30. fold_bufput_kfold_op,

31. fold_bufput_kfold_rep,

32. fold_bufput_kfold_fmt,

33. fold_kfold_add_kgc,

34. fold_kfold_add_kptr,

35. fold_kfold_add_kright,

36. fold_kfold_tobit,

37. fold_kfold_conv_kint_num,

38. fold_kfold_conv_kintu32_num,

39. fold_kfold_conv_kint_ext,

40. fold_kfold_conv_kint_i64,

41. fold_kfold_conv_kint64_num_i64,

42. fold_kfold_conv_kint64_num_u64,

43. fold_kfold_conv_kint64_int_i64,

44. fold_kfold_conv_knum_int_num,

45. fold_kfold_conv_knum_u32_num,

46. fold_kfold_conv_knum_i64_num,

47. fold_kfold_conv_knum_u64_num,

48. fold_kfold_tostr_knum,

49. fold_kfold_tostr_kint,

50. fold_kfold_strto,

51. lj_opt_cse,

52. fold_kfold_kref,

53. fold_shortcut_round,

54. fold_shortcut_left,

55. fold_shortcut_dropleft,

56. fold_shortcut_leftleft,

57. fold_simplify_numadd_negx,

58. fold_simplify_numadd_xneg,

59. fold_simplify_numsub_k,

60. fold_simplify_numsub_negk,

61. fold_simplify_numsub_xneg,

62. fold_simplify_nummuldiv_k,

63. fold_simplify_nummuldiv_negk,

64. fold_simplify_nummuldiv_negneg,

65. fold_simplify_numpow_xk,

66. fold_simplify_numpow_kx,

67. fold_shortcut_conv_num_int,

68. fold_simplify_conv_int_num,

69. fold_simplify_conv_i64_num,

70. fold_simplify_conv_int_i64,

71. fold_simplify_conv_flt_num,

72. fold_simplify_tobit_conv,

73. fold_simplify_floor_conv,

74. fold_simplify_conv_sext,

75. fold_simplify_conv_narrow,

76. fold_cse_conv,

77. fold_narrow_convert,

78. fold_simplify_intadd_k,

79. fold_simplify_intmul_k,

80. fold_simplify_intsub_k,

81. fold_simplify_intsub_kleft,

82. fold_simplify_intadd_k64,

83. fold_simplify_intsub_k64,

84. fold_simplify_intmul_k32,

85. fold_simplify_intmul_k64,

86. fold_simplify_intmod_k,

87. fold_simplify_intmod_kleft,

88. fold_simplify_intsub,

89. fold_simplify_intsubadd_leftcancel,

90. fold_simplify_intsubsub_leftcancel,

91. fold_simplify_intsubsub_rightcancel,

92. fold_simplify_intsubadd_rightcancel,

93. fold_simplify_intsubaddadd_cancel,

94. fold_simplify_band_k,

95. fold_simplify_bor_k,

96. fold_simplify_bxor_k,

97. fold_simplify_shift_ik,

98. fold_simplify_shift_andk,

99. fold_simplify_shift1_ki,

100. fold_simplify_shift2_ki,

101. fold_simplify_shiftk_andk,

102. fold_simplify_andk_shiftk,

103. fold_reassoc_intarith_k,

104. fold_reassoc_intarith_k64,

105. fold_reassoc_dup,

106. fold_reassoc_bxor,

107. fold_reassoc_shift,

108. fold_reassoc_minmax_k,

109. fold_reassoc_minmax_left,

110. fold_reassoc_minmax_right,

111. fold_abc_fwd,

112. fold_abc_k,

113. fold_abc_invar,

114. fold_comm_swap,

115. fold_comm_equal,

116. fold_comm_comp,

117. fold_comm_dup,

118. fold_comm_bxor,

119. fold_merge_eqne_snew_kgc,

120. lj_opt_fwd_aload,

121. fold_kfold_hload_kkptr,

122. lj_opt_fwd_hload,

123. lj_opt_fwd_uload,

124. lj_opt_fwd_tab_len,

125. fold_cse_uref,

126. lj_opt_fwd_hrefk,

127. fold_fwd_href_tnew,

128. fold_fwd_href_tdup,

129. fold_fload_tab_tnew_asize,

130. fold_fload_tab_tnew_hmask,

131. fold_fload_tab_tdup_asize,

132. fold_fload_tab_tdup_hmask,

133. fold_fload_tab_ah,

134. fold_fload_str_len_kgc,

135. fold_fload_str_len_snew,

136. fold_fload_str_len_tostr,

137. fold_fload_cdata_typeid_kgc,

138. fold_fload_cdata_int64_kgc,

139. fold_fload_cdata_typeid_cnew,

140. fold_fload_cdata_ptr_int64_cnew,

141. lj_opt_cse,

142. lj_opt_fwd_fload,

143. fold_fwd_sload,

144. fold_xload_kptr,

145. lj_opt_fwd_xload,

146. fold_barrier_tab,

147. fold_barrier_tnew_tdup,

148. fold_prof,

149. lj_opt_dse_ahstore,

150. lj_opt_dse_ustore,

151. lj_opt_dse_fstore,

152. lj_opt_dse_xstore,

153. lj_ir_emit

154. };

156. ‌static const uint32_t fold_hash[978] = {

157. 0xffffffff,

158. 0xffffffff,

159. 0xffffffff,

160. 0xffffffff,

161. 0xffffffff,

162. 0xffffffff,

163. 0xffffffff,

164. 0xffffffff,

165. 0x0b54741d,

166. 0x47b4aa76,

167. 0xffffffff,

168. 0x0c58741d,

169. 0x898bfc11,

170. 0xffffffff,

171. 0x0c5c741d,

172. 0xffffffff,

173. 0xffffffff,

174. 0xffffffff,

175. 0xffffffff,

176. 0xffffffff,

177. 0xffffffff,

178. 0x47b4aa96,

179. 0xffffffff,

180. 0x035c7017,

181. 0xffffffff,

182. 0x1cc18c0c,

183. 0xffffffff,

184. 0xffffffff,

185. 0xffffffff,

186. 0xffffffff,

187. 0xffffffff,

188. 0xffffffff,

189. 0xffffffff,

190. 0xffffffff,

191. 0xffffffff,

192. 0xffffffff,

193. 0xffffffff,

194. 0xffffffff,

195. 0xffffffff,

196. 0x316ad401,

197. 0xffffffff,

198. 0xffffffff,

199. 0xffffffff,

200. 0xffffffff,

201. 0x5f485fff,

202. 0xffffffff,

203. 0xffffffff,

204. 0x604c5fff,

205. 0xffffffff,

206. 0xffffffff,

207. 0x60505fff,

208. 0x6352a417,

209. 0x64468c1d,

210. 0x4d545fff,

211. 0xffffffff,

212. 0xffffffff,

213. 0x2cb873ff,

214. 0xffffffff,

215. 0xffffffff,

216. 0x0402701c,

217. 0x868a6010,

218. 0xffffffff,

219. 0x0406701c,

220. 0xffffffff,

221. 0xffffffff,

222. 0x040a701c,

223. 0xffffffff,

224. 0xffffffff,

225. 0x040e701c,

226. 0x818bfc07,

227. 0xffffffff,

228. 0x0412701c,

229. 0xffffffff,

230. 0xffffffff,

231. 0xffffffff,

232. 0xffffffff,

233. 0xffffffff,

234. 0x1000741d,

235. 0x3b56bc1c,

236. 0xffffffff,

237. 0x1004741d,

238. 0xffffffff,

239. 0xffffffff,

240. 0x1008741d,

241. 0x129c6817,

242. 0xffffffff,

243. 0x100c741d,

244. 0x888b4c0f,

245. 0xffffffff,

246. 0x3010741d,

247. 0x139dfc17,

248. 0xffffffff,

249. 0xffffffff,

250. 0xffffffff,

251. 0xffffffff,

252. 0x3fb569d3,

253. 0xffffffff,

254. 0xffffffff,

255. 0xffffffff,

256. 0x5f4a77ff,

257. 0xffffffff,

258. 0x5e49fc21,

259. 0x604e77ff,

260. 0xffffffff,

261. 0x5e4dfc21,

262. 0xffffffff,

263. 0xffffffff,

264. 0x5e51fc21,

265. 0xffffffff,

266. 0xffffffff,

267. 0xffffffff,

268. 0xffffffff,

269. 0xffffffff,

270. 0xffffffff,

271. 0xffffffff,

272. 0xffffffff,

273. 0xffffffff,

274. 0x1cc18c0d,

275. 0xffffffff,

276. 0x4a6dfc17,

277. 0xffffffff,

278. 0x6750a017,

279. 0x6444881d,

280. 0x4b71fc17,

281. 0xffffffff,

282. 0xffffffff,

283. 0xffffffff,

284. 0xffffffff,

285. 0xffffffff,

286. 0x3013fc1b,

287. 0xffffffff,

288. 0x316ad402,

289. 0xffffffff,

290. 0xffffffff,

291. 0x61488417,

292. 0x7c774418,

293. 0xffffffff,

294. 0xffffffff,

295. 0xffffffff,

296. 0xffffffff,

297. 0x61508417,

298. 0x3a57fc1c,

299. 0x5654abff,

300. 0xffffffff,

301. 0xffffffff,

302. 0xffffffff,

303. 0x95c1ffff,

304. 0xffffffff,

305. 0xffffffff,

306. 0x7d8b4008,

307. 0x95c5ffff,

308. 0xffffffff,

309. 0x868a6011,

310. 0xffffffff,

311. 0x17ab5056,

312. 0x5b45fc17,

313. 0xffffffff,

314. 0xffffffff,

315. 0x5d49fc17,

316. 0xffffffff,

317. 0xffffffff,

318. 0x5d4dfc17,

319. 0x818bfc08,

320. 0xffffffff,

321. 0x5d51fc17,

322. 0xffffffff,

323. 0xffffffff,

324. 0x4c55fc17,

325. 0xffffffff,

326. 0xffffffff,

327. 0xffffffff,

328. 0xffffffff,

329. 0xffffffff,

330. 0x3d5dfc17,

331. 0xffffffff,

332. 0xffffffff,

333. 0xffffffff,

334. 0xffffffff,

335. 0xffffffff,

336. 0x888b4c10,

337. 0x6868d017,

338. 0xffffffff,

339. 0xffffffff,

340. 0xffffffff,

341. 0xffffffff,

342. 0x3e5c73ff,

343. 0xffffffff,

344. 0xffffffff,

345. 0x95a1ffff,

346. 0xffffffff,

347. 0xffffffff,

348. 0x95a5ffff,

349. 0x674e9c17,

350. 0x6442841d,

351. 0x95a9ffff,

352. 0xffffffff,

353. 0xffffffff,

354. 0x19adffff,

355. 0xffffffff,

356. 0xffffffff,

357. 0x8eb1ffff,

358. 0xffffffff,

359. 0xffffffff,

360. 0x48b5ffff,

361. 0xffffffff,

362. 0xffffffff,

363. 0xffffffff,

364. 0xffffffff,

365. 0x14806017,

366. 0x1cc18c0e,

367. 0xffffffff,

368. 0xffffffff,

369. 0xffffffff,

370. 0xffffffff,

371. 0xffffffff,

372. 0xffffffff,

373. 0xffffffff,

374. 0x5a43fc1d,

375. 0xffffffff,

376. 0xffffffff,

377. 0x5c47fc1d,

378. 0xffffffff,

379. 0xffffffff,

380. 0xffffffff,

381. 0x1e52681d,

382. 0xffffffff,

383. 0xffffffff,

384. 0xffffffff,

385. 0xffffffff,

386. 0x4e53fc1d,

387. 0xffffffff,

388. 0xffffffff,

389. 0x5157fc1d,

390. 0x1e526417,

391. 0x8c8c6bff,

392. 0xffffffff,

393. 0xffffffff,

394. 0xffffffff,

395. 0xffffffff,

396. 0x8d8dffff,

397. 0xffffffff,

398. 0x7e8b4009,

399. 0x5954a429,

400. 0x8991ffff,

401. 0xffffffff,

402. 0x9195ffff,

403. 0xffffffff,

404. 0xffffffff,

405. 0x9399ffff,

406. 0x49b4aa6e,

407. 0xffffffff,

408. 0x6c15fc17,

409. 0x6866cc17,

410. 0x818bfc09,

411. 0x23b45e6f,

412. 0x44b7681c,

413. 0x0f407400,

414. 0xffffffff,

415. 0xffffffff,

416. 0xffffffff,

417. 0xffffffff,

418. 0xffffffff,

419. 0xffffffff,

420. 0x674c9817,

421. 0xffffffff,

422. 0xffffffff,

423. 0xffffffff,

424. 0x073e5c00,

425. 0xffffffff,

426. 0xffffffff,

427. 0xffffffff,

428. 0x888b4c11,

429. 0xffffffff,

430. 0xffffffff,

431. 0xffffffff,

432. 0x49b4aaae,

433. 0xffffffff,

434. 0xffffffff,

435. 0x7169ffff,

436. 0xffffffff,

437. 0xffffffff,

438. 0x6e6dffff,

439. 0xffffffff,

440. 0xffffffff,

441. 0x6e71ffff,

442. 0xffffffff,

443. 0xffffffff,

444. 0x7a75ffff,

445. 0x28b4726e,

446. 0xffffffff,

447. 0x9579ffff,

448. 0xffffffff,

449. 0x95b20000,

450. 0xffffffff,

451. 0xffffffff,

452. 0xffffffff,

453. 0xffffffff,

454. 0xffffffff,

455. 0xffffffff,

456. 0xffffffff,

457. 0x29b4728e,

458. 0x1cc18c0f,

459. 0xffffffff,

460. 0xffffffff,

461. 0xffffffff,

462. 0x25b475d5,

463. 0xffffffff,

464. 0x110bfc1d,

465. 0xffffffff,

466. 0xffffffff,

467. 0xffffffff,

468. 0xffffffff,

469. 0xffffffff,

470. 0x2ab472ae,

471. 0xffffffff,

472. 0xffffffff,

473. 0xffffffff,

474. 0x7145ffff,

475. 0xffffffff,

476. 0xffffffff,

477. 0xffffffff,

478. 0xffffffff,

479. 0xffffffff,

480. 0xffffffff,

481. 0xffffffff,

482. 0xffffffff,

483. 0x2bb472ce,

484. 0x05665c17,

485. 0x62429417,

486. 0x5455ffff,

487. 0xffffffff,

488. 0xffffffff,

489. 0x47b4ae75,

490. 0x066e5c17,

491. 0x30126fff,

492. 0x674a9417,

493. 0xffffffff,

494. 0xffffffff,

495. 0xffffffff,

496. 0xffffffff,

497. 0x6a67fc34,

498. 0xffffffff,

499. 0x6568d3ff,

500. 0xffffffff,

501. 0x42b56a75,

502. 0x23b45e70,

503. 0x47b4ae95,

504. 0xffffffff,

505. 0xffffffff,

506. 0xffffffff,

507. 0xffffffff,

508. 0x8faf4000,

509. 0xffffffff,

510. 0xffffffff,

511. 0x95a3fc00,

512. 0xffffffff,

513. 0xffffffff,

514. 0x42b56a95,

515. 0xffffffff,

516. 0xffffffff,

517. 0xffffffff,

518. 0xffffffff,

519. 0x3c58bc2f,

520. 0x15813bff,

521. 0x8eaffc00,

522. 0xffffffff,

523. 0x05425c17,

524. 0xffffffff,

525. 0xffffffff,

526. 0x05465c17,

527. 0x1d52601d,

528. 0x27b47675,

529. 0x054a5c17,

530. 0xffffffff,

531. 0xffffffff,

532. 0x054e5c17,

533. 0xffffffff,

534. 0xffffffff,

535. 0x05525c17,

536. 0xffffffff,

537. 0xffffffff,

538. 0x05565c17,

539. 0xffffffff,

540. 0x27b47695,

541. 0x055a5c17,

542. 0xffffffff,

543. 0xffffffff,

544. 0x055e5c17,

545. 0x49b6a41c,

546. 0xffffffff,

547. 0xffffffff,

548. 0xffffffff,

549. 0xffffffff,

550. 0xffffffff,

551. 0x75866800,

552. 0xffffffff,

553. 0xffffffff,

554. 0x26b475d6,

555. 0x7001ffff,

556. 0x7687fc00,

557. 0xffffffff,

558. 0x7005ffff,

559. 0x898bfc00,

560. 0xffffffff,

561. 0x7009ffff,

562. 0x67489017,

563. 0xffffffff,

564. 0x700dffff,

565. 0xffffffff,

566. 0xffffffff,

567. 0x6f11ffff,

568. 0xffffffff,

569. 0xffffffff,

570. 0x6d15ffff,

571. 0x6566cfff,

572. 0xffffffff,

573. 0xffffffff,

574. 0xffffffff,

575. 0xffffffff,

576. 0xffffffff,

577. 0x0060701c,

578. 0x3955fc2f,

579. 0xffffffff,

580. 0x0064701c,

581. 0x0d487417,

582. 0x47b4ae76,

583. 0x2f13141b,

584. 0x0068701c,

585. 0x0d4c7417,

586. 0xffffffff,

587. 0x0d507417,

588. 0xffffffff,

589. 0xffffffff,

590. 0x797c63ff,

591. 0xffffffff,

592. 0xffffffff,

593. 0x42b56a76,

594. 0x23b45e71,

595. 0x47b4ae96,

596. 0xffffffff,

597. 0xffffffff,

598. 0xffffffff,

599. 0xffffffff,

600. 0xffffffff,

601. 0xffffffff,

602. 0xffffffff,

603. 0xffffffff,

604. 0xffffffff,

605. 0xffffffff,

606. 0x42b56a96,

607. 0x09025c17,

608. 0xffffffff,

609. 0xffffffff,

610. 0x09065c17,

611. 0xffffffff,

612. 0xffffffff,

613. 0x090a5c17,

614. 0xffffffff,

615. 0xffffffff,

616. 0x090e5c17,

617. 0x6456ac1d,

618. 0xffffffff,

619. 0x30125c17,

620. 0xffffffff,

621. 0xffffffff,

622. 0xffffffff,

623. 0xffffffff,

624. 0xffffffff,

625. 0xffffffff,

626. 0xffffffff,

627. 0xffffffff,

628. 0xffffffff,

629. 0xffffffff,

630. 0x73113818,

631. 0xffffffff,

632. 0xffffffff,

633. 0x63468c17,

634. 0x0054701c,

635. 0x47b4a675,

636. 0xffffffff,

637. 0x30106018,

638. 0x0058701c,

639. 0xffffffff,

640. 0x0b42741d,

641. 0xffffffff,

642. 0xffffffff,

643. 0x0b46741d,

644. 0xffffffff,

645. 0xffffffff,

646. 0xffffffff,

647. 0x47b4a695,

648. 0xffffffff,

649. 0xffffffff,

650. 0xffffffff,

651. 0x898bfc01,

652. 0x0b52741d,

653. 0xffffffff,

654. 0x848b7000,

655. 0x0b56741d,

656. 0xffffffff,

657. 0xffffffff,

658. 0x0c5a741d,

659. 0x7c77441c,

660. 0xffffffff,

661. 0xffffffff,

662. 0xffffffff,

663. 0xffffffff,

664. 0xffffffff,

665. 0x3360bc1c,

666. 0xffffffff,

667. 0xffffffff,

668. 0xffffffff,

669. 0x2eba6000,

670. 0xffffffff,

671. 0xffffffff,

672. 0xffffffff,

673. 0xffffffff,

674. 0xffffffff,

675. 0xffffffff,

676. 0xffffffff,

677. 0xffffffff,

678. 0xffffffff,

679. 0xffffffff,

680. 0xffffffff,

681. 0xffffffff,

682. 0xffffffff,

683. 0xffffffff,

684. 0x34408000,

685. 0xffffffff,

686. 0x23b45e72,

687. 0xffffffff,

688. 0xffffffff,

689. 0xffffffff,

690. 0xffffffff,

691. 0x5f4a5fff,

692. 0x838b3800,

693. 0xffffffff,

694. 0x604e5fff,

695. 0xffffffff,

696. 0xffffffff,

697. 0xffffffff,

698. 0xffffffff,

699. 0x828a6000,

700. 0xffffffff,

701. 0xffffffff,

702. 0xffffffff,

703. 0x0400701c,

704. 0x535a5fff,

705. 0x63448817,

706. 0x0404701c,

707. 0xffffffff,

708. 0xffffffff,

709. 0x0408701c,

710. 0xffffffff,

711. 0xffffffff,

712. 0x040c701c,

713. 0x878b480e,

714. 0x21b45dd3,

715. 0x0410701c,

716. 0xffffffff,

717. 0xffffffff,

718. 0xffffffff,

719. 0xffffffff,

720. 0xffffffff,

721. 0xffffffff,

722. 0x3854bc1c,

723. 0x7f8b4408,

724. 0x1002741d,

725. 0x3b58bc1c,

726. 0x47b4a676,

727. 0x1006741d,

728. 0xffffffff,

729. 0xffffffff,

730. 0x100a741d,

731. 0xffffffff,

732. 0xffffffff,

733. 0x100e741d,

734. 0xffffffff,

735. 0xffffffff,

736. 0x3012741d,

737. 0xffffffff,

738. 0xffffffff,

739. 0x47b4a696,

740. 0xffffffff,

741. 0xffffffff,

742. 0xffffffff,

743. 0x5f4877ff,

744. 0xffffffff,

745. 0xffffffff,

746. 0x604c77ff,

747. 0xffffffff,

748. 0x5e4bfc21,

749. 0x605077ff,

750. 0xffffffff,

751. 0x5e4ffc21,

752. 0x4d5477ff,

753. 0xffffffff,

754. 0xffffffff,

755. 0xffffffff,

756. 0x43b569ae,

757. 0xffffffff,

758. 0xffffffff,

759. 0x6452a41d,

760. 0xffffffff,

761. 0xffffffff,

762. 0xffffffff,

763. 0xffffffff,

764. 0xffffffff,

765. 0xffffffff,

766. 0x4a6ffc17,

767. 0xffffffff,

768. 0xffffffff,

769. 0xffffffff,

770. 0xffffffff,

771. 0xffffffff,

772. 0xffffffff,

773. 0x3011fc1b,

774. 0xffffffff,

775. 0x63428417,

776. 0x026a73ff,

777. 0xffffffff,

778. 0xffffffff,

779. 0xffffffff,

780. 0xffffffff,

781. 0x614a8417,

782. 0xffffffff,

783. 0xffffffff,

784. 0x614e8417,

785. 0x3755fc1c,

786. 0x78c1fc1e,

787. 0x3a59fc1c,

788. 0xffffffff,

789. 0xffffffff,

790. 0xffffffff,

791. 0xffffffff,

792. 0xffffffff,

793. 0x95c3ffff,

794. 0xffffffff,

795. 0x66468fff,

796. 0xffffffff,

797. 0x6868d01c,

798. 0xffffffff,

799. 0x5a43fc17,

800. 0xffffffff,

801. 0xffffffff,

802. 0x5c47fc17,

803. 0xffffffff,

804. 0x24b45eb3,

805. 0x5d4bfc17,

806. 0x22b45dd4,

807. 0x1e526817,

808. 0x5d4ffc17,

809. 0xffffffff,

810. 0xffffffff,

811. 0x4a53fc17,

812. 0xffffffff,

813. 0xffffffff,

814. 0x5057fc17,

815. 0x808b4409,

816. 0xffffffff,

817. 0x525bfc17,

818. 0x24b45ed3,

819. 0xffffffff,

820. 0xffffffff,

821. 0x343e7c00,

822. 0xffffffff,

823. 0xffffffff,

824. 0xffffffff,

825. 0xffffffff,

826. 0x1ac18c13,

827. 0xffffffff,

828. 0xffffffff,

829. 0xffffffff,

830. 0xffffffff,

831. 0xffffffff,

832. 0xffffffff,

833. 0xffffffff,

834. 0x40b56a6e,

835. 0xffffffff,

836. 0xffffffff,

837. 0xffffffff,

838. 0xffffffff,

839. 0xffffffff,

840. 0xffffffff,

841. 0x456b6800,

842. 0x18abffff,

843. 0xffffffff,

844. 0xffffffff,

845. 0xffffffff,

846. 0xffffffff,

847. 0x40b56a8e,

848. 0x08405c00,

849. 0xffffffff,

850. 0xffffffff,

851. 0xffffffff,

852. 0xffffffff,

853. 0xffffffff,

854. 0xffffffff,

855. 0xffffffff,

856. 0x95bfffff,

857. 0xffffffff,

858. 0xffffffff,

859. 0x5554a7ff,

860. 0x41b56aae,

861. 0xffffffff,

862. 0xffffffff,

863. 0xffffffff,

864. 0x5b45fc1d,

865. 0xffffffff,

866. 0x65448bff,

867. 0xffffffff,

868. 0x6866cc1c,

869. 0xffffffff,

870. 0x898bfc0e,

871. 0xffffffff,

872. 0xffffffff,

873. 0x41b56ace,

874. 0xffffffff,

875. 0xffffffff,

876. 0x4f55fc1d,

877. 0xffffffff,

878. 0xffffffff,

879. 0xffffffff,

880. 0xffffffff,

881. 0xffffffff,

882. 0xffffffff,

883. 0x8a8bffff,

884. 0x1ac18c09,

885. 0xffffffff,

886. 0x8b8fffff,

887. 0xffffffff,

888. 0xffffffff,

889. 0x0a0bfc17,

890. 0x9193ffff,

891. 0xffffffff,

892. 0x9297ffff,

893. 0xffffffff,

894. 0xffffffff,

895. 0x6a69fc33,

896. 0x949bffff,

897. 0x24b45eb4,

898. 0x959fffff,

899. 0xffffffff,

900. 0xffffffff,

901. 0xffffffff,

902. 0xffffffff,

903. 0x1f53fc18,

904. 0xffffffff,

905. 0xffffffff,

906. 0xffffffff,

907. 0xffffffff,

908. 0xffffffff,

909. 0x24b45ed4,

910. 0xffffffff,

911. 0x3552bfff,

912. 0xffffffff,

913. 0xffffffff,

914. 0xffffffff,

915. 0xffffffff,

916. 0xffffffff,

917. 0xffffffff,

918. 0x1ac18c14,

919. 0xffffffff,

920. 0x345ebfff,

921. 0xffffffff,

922. 0x7167ffff,

923. 0x7b7743ff,

924. 0x8faf4400,

925. 0xffffffff,

926. 0xffffffff,

927. 0x898bfc04,

928. 0x546fffff,

929. 0xffffffff,

930. 0xffffffff,

931. 0xffffffff,

932. 0xffffffff,

933. 0x456b6801,

934. 0x8177ffff,

935. 0xffffffff,

936. 0xffffffff,

937. 0x654287ff,

938. 0x8e7bffff,

939. 0xffffffff,

940. 0xffffffff,

941. 0xffffffff,

942. 0x1e52641d,

943. 0xffffffff,

944. 0xffffffff,

945. 0xffffffff,

946. 0xffffffff,

947. 0xffffffff,

948. 0xffffffff,

949. 0xffffffff,

950. 0xffffffff,

951. 0x1d526017,

952. 0xffffffff,

953. 0xffffffff,

954. 0xffffffff,

955. 0xffffffff,

956. 0xffffffff,

957. 0xffffffff,

958. 0xffffffff,

959. 0xffffffff,

960. 0x49b6a81c,

961. 0x7143ffff,

962. 0x898bfc0f,

963. 0xffffffff,

964. 0x7247ffff,

965. 0xffffffff,

966. 0x0e3e7400,

967. 0x49b4a66e,

968. 0xffffffff,

969. 0xffffffff,

970. 0xffffffff,

971. 0xffffffff,

972. 0xffffffff,

973. 0x6e53ffff,

974. 0x05685c17,

975. 0x1cc18c0a,

976. 0x6e57ffff,

977. 0x066c5c17,

978. 0x30106fff,

979. 0xffffffff,

980. 0x06705c17,

981. 0xffffffff,

982. 0xffffffff,

983. 0xffffffff,

984. 0xffffffff,

985. 0xffffffff,

986. 0x6966d3ff,

987. 0xffffffff,

988. 0xffffffff,

989. 0xffffffff,

990. 0xffffffff,

991. 0xffffffff,

992. 0xffffffff,

993. 0x49b4a6ae,

994. 0xffffffff,

995. 0x1f53fc19,

996. 0xffffffff,

997. 0xffffffff,

998. 0x20b6701c,

999. 0xffffffff,

1000. 0xffffffff,

1001. 0xffffffff,

1002. 0xffffffff,

1003. 0x5855fc29,

1004. 0xffffffff,

1005. 0xffffffff,

1006. 0x3c56bc2f,

1007. 0xffffffff,

1008. 0xffffffff,

1009. 0x902bffff,

1010. 0x858a600e,

1011. 0x1ac18c15,

1012. 0xffffffff,

1013. 0x05445c17,

1014. 0xffffffff,

1015. 0xffffffff,

1016. 0x05485c17,

1017. 0xffffffff,

1018. 0xffffffff,

1019. 0x054c5c17,

1020. 0xffffffff,

1021. 0xffffffff,

1022. 0x05505c17,

1023. 0xffffffff,

1024. 0xffffffff,

1025. 0x05545c17,

1026. 0x456b6802,

1027. 0xffffffff,

1028. 0xffffffff,

1029. 0xffffffff,

1030. 0xffffffff,

1031. 0xffffffff,

1032. 0xffffffff,

1033. 0xffffffff,

1034. 0xffffffff,

1035. 0xffffffff,

1036. 0xffffffff,

1037. 0xffffffff,

1038. 0xffffffff,

1039. 0xffffffff,

1040. 0xffffffff,

1041. 0xffffffff,

1042. 0x6356ac17,

1043. 0x7485fc00,

1044. 0xffffffff,

1045. 0x7003ffff,

1046. 0x62429017,

1047. 0x7789fc00,

1048. 0x7007ffff,

1049. 0xffffffff,

1050. 0x47b4aa75,

1051. 0x700bffff,

1052. 0xffffffff,

1053. 0xffffffff,

1054. 0x700fffff,

1055. 0x898bfc10,

1056. 0xffffffff,

1057. 0x6f13ffff,

1058. 0xffffffff,

1059. 0xffffffff,

1060. 0x9517ffff,

1061. 0x6968cfff,

1062. 0xffffffff,

1063. 0x47b4aa95,

1064. 0x2db85fff,

1065. 0x3653fc2f,

1066. 0xffffffff,

1067. 0x0062701c,

1068. 0x1cc18c0b,

1069. 0xffffffff,

1070. 0x2f11141b,

1071. 0x0066701c,

1072. 0x0d4a7417,

1073. 0xffffffff,

1074. 0x0d4e7417,

1075. 0xffffffff,

1076. 0xffffffff,

1077. 0x797a63ff,

1078. 0xffffffff,

1079. 0xffffffff,

1080. 0x46b5feb3,

1081. 0x3260c01c,

1082. 0x316ad400,

1083. 0xffffffff,

1084. 0x7c774416,

1085. 0xffffffff,

1086. 0xffffffff,

1087. 0x6b15fc29,

1088. 0x1f53fc1a,

1089. 0xffffffff,

1090. 0xffffffff,

1091. 0xffffffff,

1092. 0xffffffff,

1093. 0x46b5fed3,

1094. 0x09005c17,

1095. 0x5755fc2a,

1096. 0x16bd8c00,

1097. 0x09045c17,

1098. 0x01647017,

1099. 0xffffffff,

1100. 0x09085c17,

1101. 0xffffffff,

1102. 0x868a600f,

1103. 0x1bc18c16,

1104. 0x090c5c17,

1105. 0xffffffff,

1106. 0x30105c17,

1107. 0xffffffff,

1108. 0xffffffff,

1109. 0x09145c17,

1110. 0xffffffff,

1111. 0x818bfc06,

1112. 0xffffffff,

1113. 0xffffffff,

1114. 0xffffffff,

1115. 0xffffffff,

1116. 0xffffffff,

1117. 0xffffffff,

1118. 0xffffffff,

1119. 0xffffffff,

1120. 0x73133818,

1121. 0x0052701c,

1122. 0xffffffff,

1123. 0xffffffff,

1124. 0x0056701c,

1125. 0xffffffff,

1126. 0xffffffff,

1127. 0x30126018,

1128. 0xffffffff,

1129. 0x878b4c0e,

1130. 0x0b44741d,

1131. 0x005e701c,

1132. 0xffffffff,

1133. 0xffffffff,

1134. 0xffffffff

1135. };

1137. ‌#define fold_hashkey(k)        (lj_rol(lj_rol((k),10)-(k),1)%977)

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_gdbjit.h - luajit-2.0-src

Macros defined

- _LJ_GDBJIT_H

- lj_gdbjit_addtrace

- lj_gdbjit_deltrace

Source code

1. /*

2. ** Client for the GDB JIT API.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_GDBJIT_H

7. ‌#define _LJ_GDBJIT_H

9. #include "lj_obj.h"

10. #include "lj_jit.h"

12. #if LJ_HASJIT && defined(LUAJIT_USE_GDBJIT)

14. LJ_FUNC void lj_gdbjit_addtrace(jit_State *J, GCtrace *T);

15. LJ_FUNC void lj_gdbjit_deltrace(jit_State *J, GCtrace *T);

17. #else

18. ‌#define lj_gdbjit_addtrace(J, T)        UNUSED(T)

19. ‌#define lj_gdbjit_deltrace(J, T)        UNUSED(T)

20. #endif

22. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/host/buildvm_asm.c - luajit-2.0-src

Global variables defined

- jccnames

Functions defined

- emit_asm

- emit_asm_align

- emit_asm_bytes

- emit_asm_label

- emit_asm_reloc

- emit_asm_reloc_text

- emit_asm_wordreloc

- emit_asm_words

Macros defined

- ELFASM_PX

- ELFASM_PX

Source code

1. /*

2. ** LuaJIT VM builder: Assembler source code emitter.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #include "buildvm.h"

7. #include "lj_bc.h"

9. /* ------------------------------------------------------------------------ */

11. #if LJ_TARGET_X86ORX64

12. /* Emit bytes piecewise as assembler text. */

13. ‌static void emit_asm_bytes(BuildCtx *ctx, uint8_t *p, int n)

14. {

15. int i;

16. for (i = 0; i < n; i++) {

17. if ((i & 15) == 0)

18. fprintf(ctx->fp, "\t.byte %d", p[i]);

19. else

20. fprintf(ctx->fp, ",%d", p[i]);

21. if ((i & 15) == 15) putc('\n', ctx->fp);

22. }

23. if ((n & 15) != 0) putc('\n', ctx->fp);

24. }

26. /* Emit relocation */

27. ‌static void emit_asm_reloc(BuildCtx *ctx, int type, const char *sym)

28. {

29. switch (ctx->mode) {

30. case BUILD_elfasm:

31. if (type)

32. fprintf(ctx->fp, "\t.long %s-.-4\n", sym);

33. else

34. fprintf(ctx->fp, "\t.long %s\n", sym);

35. break;

36. case BUILD_coffasm:

37. fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", sym);

38. if (type)

39. fprintf(ctx->fp, "\t.long %s-.-4\n", sym);

40. else

41. fprintf(ctx->fp, "\t.long %s\n", sym);

42. break;

43. default:  /* BUILD_machasm for relative relocations handled below. */

44. fprintf(ctx->fp, "\t.long %s\n", sym);

45. break;

46. }

47. }

49. ‌static const char *const jccnames[] = {

50. "jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "ja",

51. "js", "jns", "jpe", "jpo", "jl", "jge", "jle", "jg"

52. };

54. /* Emit x86/x64 text relocations. */

55. ‌static void emit_asm_reloc_text(BuildCtx *ctx, uint8_t *cp, int n,

56. const char *sym)

57. {

58. const char *opname = NULL;

59. if (--n < 0) goto err;

60. if (cp[n] == 0xe8) {

61. opname = "call";

62. } else if (cp[n] == 0xe9) {

63. opname = "jmp";

64. } else if (cp[n] >= 0x80 && cp[n] <= 0x8f && n > 0 && cp[n-1] == 0x0f) {

65. opname = jccnames[cp[n]-0x80];

66. n--;

67. } else {

68. err:

69. fprintf(stderr, "Error: unsupported opcode for %s symbol relocation.\n",

70. sym);

71. exit(1);

72. }

73. emit_asm_bytes(ctx, cp, n);

74. if (strncmp(sym+(*sym == '_'), LABEL_PREFIX, sizeof(LABEL_PREFIX)-1)) {

75. /* Various fixups for external symbols outside of our binary. */

76. if (ctx->mode == BUILD_elfasm) {

77. if (LJ_32)

78. fprintf(ctx->fp, "#if __PIC__\n\t%s lj_wrap_%s\n#else\n", opname, sym);

79. fprintf(ctx->fp, "\t%s %s@PLT\n", opname, sym);

80. if (LJ_32)

81. fprintf(ctx->fp, "#endif\n");

82. return;

83. } else if (LJ_32 && ctx->mode == BUILD_machasm) {

84. fprintf(ctx->fp, "\t%s L%s$stub\n", opname, sym);

85. return;

86. }

87. }

88. fprintf(ctx->fp, "\t%s %s\n", opname, sym);

89. }

90. #else

91. /* Emit words piecewise as assembler text. */

92. ‌static void emit_asm_words(BuildCtx *ctx, uint8_t *p, int n)

93. {

94. int i;

95. for (i = 0; i < n; i += 4) {

96. if ((i & 15) == 0)

97. fprintf(ctx->fp, "\t.long 0x%08x", *(uint32_t *)(p+i));

98. else

99. fprintf(ctx->fp, ",0x%08x", *(uint32_t *)(p+i));

100. if ((i & 15) == 12) putc('\n', ctx->fp);

101. }

102. if ((n & 15) != 0) putc('\n', ctx->fp);

103. }

105. /* Emit relocation as part of an instruction. */

106. ‌static void emit_asm_wordreloc(BuildCtx *ctx, uint8_t *p, int n,

107. const char *sym)

108. {

109. uint32_t ins;

110. emit_asm_words(ctx, p, n-4);

111. ins = *(uint32_t *)(p+n-4);

112. #if LJ_TARGET_ARM

113. if ((ins & 0xff000000u) == 0xfa000000u) {

114. fprintf(ctx->fp, "\tblx %s\n", sym);

115. } else if ((ins & 0x0e000000u) == 0x0a000000u) {

116. fprintf(ctx->fp, "\t%s%.2s %s\n", (ins & 0x01000000u) ? "bl" : "b",

117. &"eqnecsccmiplvsvchilsgeltgtle"[2*(ins >> 28)], sym);

118. } else {

119. fprintf(stderr,

120. "Error: unsupported opcode %08x for %s symbol relocation.\n",

121. ins, sym);

122. exit(1);

123. }

124. #elif LJ_TARGET_ARM64

125. if ((ins >> 26) == 0x25u) {

126. fprintf(ctx->fp, "\tbl %s\n", sym);

127. } else {

128. fprintf(stderr,

129. "Error: unsupported opcode %08x for %s symbol relocation.\n",

130. ins, sym);

131. exit(1);

132. }

133. #elif LJ_TARGET_PPC

134. #if LJ_TARGET_PS3

135. #define TOCPREFIX "."

136. #else

137. #define TOCPREFIX ""

138. #endif

139. if ((ins >> 26) == 16) {

140. fprintf(ctx->fp, "\t%s %d, %d, " TOCPREFIX "%s\n",

141. (ins & 1) ? "bcl" : "bc", (ins >> 21) & 31, (ins >> 16) & 31, sym);

142. } else if ((ins >> 26) == 18) {

143. #if LJ_ARCH_PPC64

144. const char *suffix = strchr(sym, '@');

145. if (suffix && suffix[1] == 'h') {

146. fprintf(ctx->fp, "\taddis 11, 2, %s\n", sym);

147. } else if (suffix && suffix[1] == 'l') {

148. fprintf(ctx->fp, "\tld 12, %s\n", sym);

149. } else

150. #endif

151. fprintf(ctx->fp, "\t%s " TOCPREFIX "%s\n", (ins & 1) ? "bl" : "b", sym);

152. } else {

153. fprintf(stderr,

154. "Error: unsupported opcode %08x for %s symbol relocation.\n",

155. ins, sym);

156. exit(1);

157. }

158. #elif LJ_TARGET_MIPS

159. fprintf(stderr,

160. "Error: unsupported opcode %08x for %s symbol relocation.\n",

161. ins, sym);

162. exit(1);

163. #else

164. #error "missing relocation support for this architecture"

165. #endif

166. }

167. #endif

169. #if LJ_TARGET_ARM

170. ‌#define ELFASM_PX        "%%"

171. #else

172. ‌#define ELFASM_PX        "@"

173. #endif

175. /* Emit an assembler label. */

176. ‌static void emit_asm_label(BuildCtx *ctx, const char *name, int size, int isfunc)

177. {

178. switch (ctx->mode) {

179. case BUILD_elfasm:

180. #if LJ_TARGET_PS3

181. if (!strncmp(name, "lj_vm_", 6) &&

182. strcmp(name, ctx->beginsym) &&

183. !strstr(name, "hook")) {

184. fprintf(ctx->fp,

185. "\n\t.globl %s\n"

186. "\t.section \".opd\",\"aw\"\n"

187. "%s:\n"

188. "\t.long .%s,.TOC.@tocbase32\n"

189. "\t.size %s,8\n"

190. "\t.previous\n"

191. "\t.globl .%s\n"

192. "\t.hidden .%s\n"

193. "\t.type .%s, " ELFASM_PX "function\n"

194. "\t.size .%s, %d\n"

195. ".%s:\n",

196. name, name, name, name, name, name, name, name, size, name);

197. break;

198. }

199. #endif

200. fprintf(ctx->fp,

201. "\n\t.globl %s\n"

202. "\t.hidden %s\n"

203. "\t.type %s, " ELFASM_PX "%s\n"

204. "\t.size %s, %d\n"

205. "%s:\n",

206. name, name, name, isfunc ? "function" : "object", name, size, name);

207. break;

208. case BUILD_coffasm:

209. fprintf(ctx->fp, "\n\t.globl %s\n", name);

210. if (isfunc)

211. fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", name);

212. fprintf(ctx->fp, "%s:\n", name);

213. break;

214. case BUILD_machasm:

215. fprintf(ctx->fp,

216. "\n\t.private_extern %s\n"

217. "%s:\n", name, name);

218. break;

219. default:

220. break;

221. }

222. }

224. /* Emit alignment. */

225. ‌static void emit_asm_align(BuildCtx *ctx, int bits)

226. {

227. switch (ctx->mode) {

228. case BUILD_elfasm:

229. case BUILD_coffasm:

230. fprintf(ctx->fp, "\t.p2align %d\n", bits);

231. break;

232. case BUILD_machasm:

233. fprintf(ctx->fp, "\t.align %d\n", bits);

234. break;

235. default:

236. break;

237. }

238. }

240. /* ------------------------------------------------------------------------ */

242. /* Emit assembler source code. */

243. ‌void emit_asm(BuildCtx *ctx)

244. {

245. int i, rel;

247. fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch);

248. #if LJ_ARCH_PPC64

249. fprintf(ctx->fp, "\t.abiversion 2\n");

250. #endif

251. fprintf(ctx->fp, "\t.text\n");

252. emit_asm_align(ctx, 4);

254. #if LJ_TARGET_PS3

255. emit_asm_label(ctx, ctx->beginsym, ctx->codesz, 0);

256. #else

257. emit_asm_label(ctx, ctx->beginsym, 0, 0);

258. #endif

259. if (ctx->mode != BUILD_machasm)

260. fprintf(ctx->fp, ".Lbegin:\n");

262. #if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND

263. /* This should really be moved into buildvm_arm.dasc. */

264. fprintf(ctx->fp,

265. ".fnstart\n"

266. ".save {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"

267. ".pad #28\n");

268. #endif

269. #if LJ_TARGET_MIPS

270. fprintf(ctx->fp, ".set nomips16\n.abicalls\n.set noreorder\n.set nomacro\n");

271. #endif

273. for (i = rel = 0; i < ctx->nsym; i++) {

274. int32_t ofs = ctx->sym[i].ofs;

275. int32_t next = ctx->sym[i+1].ofs;

276. #if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND && LJ_HASFFI

277. if (!strcmp(ctx->sym[i].name, "lj_vm_ffi_call"))

278. fprintf(ctx->fp,

279. ".globl lj_err_unwind_arm\n"

280. ".personality lj_err_unwind_arm\n"

281. ".fnend\n"

282. ".fnstart\n"

283. ".save {r4, r5, r11, lr}\n"

284. ".setfp r11, sp\n");

285. #endif

286. emit_asm_label(ctx, ctx->sym[i].name, next - ofs, 1);

287. while (rel < ctx->nreloc && ctx->reloc[rel].ofs <= next) {

288. BuildReloc *r = &ctx->reloc[rel];

289. int n = r->ofs - ofs;

290. #if LJ_TARGET_X86ORX64

291. if (r->type != 0 &&

292. (ctx->mode == BUILD_elfasm || ctx->mode == BUILD_machasm)) {

293. emit_asm_reloc_text(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);

294. } else {

295. emit_asm_bytes(ctx, ctx->code+ofs, n);

296. emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]);

297. }

298. ofs += n+4;

299. #else

300. emit_asm_wordreloc(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);

301. ofs += n;

302. #endif

303. rel++;

304. }

305. #if LJ_TARGET_X86ORX64

306. emit_asm_bytes(ctx, ctx->code+ofs, next-ofs);

307. #else

308. emit_asm_words(ctx, ctx->code+ofs, next-ofs);

309. #endif

310. }

312. #if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND

313. fprintf(ctx->fp,

314. #if !LJ_HASFFI

315. ".globl lj_err_unwind_arm\n"

316. ".personality lj_err_unwind_arm\n"

317. #endif

318. ".fnend\n");

319. #endif

321. fprintf(ctx->fp, "\n");

322. switch (ctx->mode) {

323. case BUILD_elfasm:

324. #if !(LJ_TARGET_PS3 || LJ_TARGET_PSVITA)

325. fprintf(ctx->fp, "\t.section .note.GNU-stack,\"\"," ELFASM_PX "progbits\n");

326. #endif

327. #if LJ_TARGET_PPC && !LJ_TARGET_PS3

328. /* Hard-float ABI. */

329. fprintf(ctx->fp, "\t.gnu_attribute 4, 1\n");

330. #endif

331. /* fallthrough */

332. case BUILD_coffasm:

333. fprintf(ctx->fp, "\t.ident \"%s\"\n", ctx->dasm_ident);

334. break;

335. case BUILD_machasm:

336. fprintf(ctx->fp,

337. "\t.cstring\n"

338. "\t.ascii \"%s\\0\"\n", ctx->dasm_ident);

339. break;

340. default:

341. break;

342. }

343. fprintf(ctx->fp, "\n");

344. }

One Level Up                               Top Level

One Level Up                               Top Level

src/host/buildvm_peobj.c - luajit-2.0-src

Global variables defined

- strtab

- strtabofs

Data types defined

- PEheader

- PEheader

- PEreloc

- PEreloc

- PEsection

- PEsection

- PEsym

- PEsym

- PEsymaux

- PEsymaux

Functions defined

- emit_peobj

- emit_peobj

- emit_peobj_sym

- emit_peobj_sym_sect

Macros defined

- PEOBJ_ARCH_TARGET

- PEOBJ_ARCH_TARGET

- PEOBJ_ARCH_TARGET

- PEOBJ_RELOC_ADDR32NB

- PEOBJ_RELOC_DIR32

- PEOBJ_RELOC_DIR32

- PEOBJ_RELOC_DIR32

- PEOBJ_RELOC_OFS

- PEOBJ_RELOC_OFS

- PEOBJ_RELOC_OFS

- PEOBJ_RELOC_REL32

- PEOBJ_RELOC_REL32

- PEOBJ_RELOC_REL32

- PEOBJ_RELOC_SIZE

- PEOBJ_SCL_EXTERN

- PEOBJ_SCL_STATIC

- PEOBJ_SYM_SIZE

- PEOBJ_TEXT_FLAGS

- PEOBJ_TEXT_FLAGS

- PEOBJ_TEXT_FLAGS

- PEOBJ_TYPE_FUNC

- PEOBJ_TYPE_NULL

Source code

1. /*

2. ** LuaJIT VM builder: PE object emitter.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Only used for building on Windows, since we cannot assume the presence

6. ** of a suitable assembler. The host and target byte order must match.

7. */

9. #include "buildvm.h"

10. #include "lj_bc.h"

12. #if LJ_TARGET_X86ORX64 || LJ_TARGET_PPC

14. /* Context for PE object emitter. */

15. ‌static char *strtab;

16. ‌static size_t strtabofs;

18. /* -- PE object definitions ----------------------------------------------- */

20. /* PE header. */

21. ‌typedef struct PEheader {

22. uint16_t arch;

23. uint16_t nsects;

24. uint32_t time;

25. uint32_t symtabofs;

26. uint32_t nsyms;

27. uint16_t opthdrsz;

28. uint16_t flags;

29. ‌} PEheader;

31. /* PE section. */

32. ‌typedef struct PEsection {

33. char name[8];

34. uint32_t vsize;

35. uint32_t vaddr;

36. uint32_t size;

37. uint32_t ofs;

38. uint32_t relocofs;

39. uint32_t lineofs;

40. uint16_t nreloc;

41. uint16_t nline;

42. uint32_t flags;

43. ‌} PEsection;

45. /* PE relocation. */

46. ‌typedef struct PEreloc {

47. uint32_t vaddr;

48. uint32_t symidx;

49. uint16_t type;

50. ‌} PEreloc;

52. /* Cannot use sizeof, because it pads up to the max. alignment. */

53. ‌#define PEOBJ_RELOC_SIZE        (4+4+2)

55. /* PE symbol table entry. */

56. ‌typedef struct PEsym {

57. union {

58. char name[8];

59. uint32_t nameref[2];

60. } n;

61. uint32_t value;

62. int16_t sect;

63. uint16_t type;

64. uint8_t scl;

65. uint8_t naux;

66. ‌} PEsym;

68. /* PE symbol table auxiliary entry for a section. */

69. ‌typedef struct PEsymaux {

70. uint32_t size;

71. uint16_t nreloc;

72. uint16_t nline;

73. uint32_t cksum;

74. uint16_t assoc;

75. uint8_t comdatsel;

76. uint8_t unused[3];

77. ‌} PEsymaux;

79. /* Cannot use sizeof, because it pads up to the max. alignment. */

80. ‌#define PEOBJ_SYM_SIZE        (8+4+2+2+1+1)

82. /* PE object CPU specific defines. */

83. #if LJ_TARGET_X86

84. ‌#define PEOBJ_ARCH_TARGET        0x014c

85. ‌#define PEOBJ_RELOC_REL32        0x14  /* MS: REL32, GNU: DISP32. */

86. ‌#define PEOBJ_RELOC_DIR32        0x06

87. ‌#define PEOBJ_RELOC_OFS                0

88. ‌#define PEOBJ_TEXT_FLAGS        0x60500020  /* 60=r+x, 50=align16, 20=code. */

89. #elif LJ_TARGET_X64

90. ‌#define PEOBJ_ARCH_TARGET        0x8664

91. ‌#define PEOBJ_RELOC_REL32        0x04  /* MS: REL32, GNU: DISP32. */

92. ‌#define PEOBJ_RELOC_DIR32        0x02

93. ‌#define PEOBJ_RELOC_ADDR32NB        0x03

94. ‌#define PEOBJ_RELOC_OFS                0

95. ‌#define PEOBJ_TEXT_FLAGS        0x60500020  /* 60=r+x, 50=align16, 20=code. */

96. #elif LJ_TARGET_PPC

97. ‌#define PEOBJ_ARCH_TARGET        0x01f2

98. ‌#define PEOBJ_RELOC_REL32        0x06

99. ‌#define PEOBJ_RELOC_DIR32        0x02

100. ‌#define PEOBJ_RELOC_OFS                (-4)

101. ‌#define PEOBJ_TEXT_FLAGS        0x60400020  /* 60=r+x, 40=align8, 20=code. */

102. #endif

104. /* Section numbers (0-based). */

105. enum {

106. PEOBJ_SECT_ABS = -2,

107. PEOBJ_SECT_UNDEF = -1,

108. PEOBJ_SECT_TEXT,

109. #if LJ_TARGET_X64

110. PEOBJ_SECT_PDATA,

111. PEOBJ_SECT_XDATA,

112. #endif

113. PEOBJ_SECT_RDATA_Z,

114. PEOBJ_NSECTIONS

115. };

117. /* Symbol types. */

118. ‌#define PEOBJ_TYPE_NULL                0

119. ‌#define PEOBJ_TYPE_FUNC                0x20

121. /* Symbol storage class. */

122. ‌#define PEOBJ_SCL_EXTERN        2

123. ‌#define PEOBJ_SCL_STATIC        3

125. /* -- PE object emitter --------------------------------------------------- */

127. /* Emit PE object symbol. */

128. ‌static void emit_peobj_sym(BuildCtx *ctx, const char *name, uint32_t value,

129. int sect, int type, int scl)

130. {

131. PEsym sym;

132. size_t len = strlen(name);

133. if (!strtab) {  /* Pass 1: only calculate string table length. */

134. if (len > 8) strtabofs += len+1;

135. return;

136. }

137. if (len <= 8) {

138. memcpy(sym.n.name, name, len);

139. memset(sym.n.name+len, 0, 8-len);

140. } else {

141. sym.n.nameref[0] = 0;

142. sym.n.nameref[1] = (uint32_t)strtabofs;

143. memcpy(strtab + strtabofs, name, len);

144. strtab[strtabofs+len] = 0;

145. strtabofs += len+1;

146. }

147. sym.value = value;

148. sym.sect = (int16_t)(sect+1);  /* 1-based section number. */

149. sym.type = (uint16_t)type;

150. sym.scl = (uint8_t)scl;

151. sym.naux = 0;

152. owrite(ctx, &sym, PEOBJ_SYM_SIZE);

153. }

155. /* Emit PE object section symbol. */

156. ‌static void emit_peobj_sym_sect(BuildCtx *ctx, PEsection *pesect, int sect)

157. {

158. PEsym sym;

159. PEsymaux aux;

160. if (!strtab) return;  /* Pass 1: no output. */

161. memcpy(sym.n.name, pesect[sect].name, 8);

162. sym.value = 0;

163. sym.sect = (int16_t)(sect+1);  /* 1-based section number. */

164. sym.type = PEOBJ_TYPE_NULL;

165. sym.scl = PEOBJ_SCL_STATIC;

166. sym.naux = 1;

167. owrite(ctx, &sym, PEOBJ_SYM_SIZE);

168. memset(&aux, 0, sizeof(PEsymaux));

169. aux.size = pesect[sect].size;

170. aux.nreloc = pesect[sect].nreloc;

171. owrite(ctx, &aux, PEOBJ_SYM_SIZE);

172. }

174. /* Emit Windows PE object file. */

175. ‌void emit_peobj(BuildCtx *ctx)

176. {

177. PEheader pehdr;

178. PEsection pesect[PEOBJ_NSECTIONS];

179. uint32_t sofs;

180. int i, nrsym;

181. union { uint8_t b; uint32_t u; } host_endian;

183. sofs = sizeof(PEheader) + PEOBJ_NSECTIONS*sizeof(PEsection);

185. /* Fill in PE sections. */

186. memset(&pesect, 0, PEOBJ_NSECTIONS*sizeof(PEsection));

187. memcpy(pesect[PEOBJ_SECT_TEXT].name, ".text", sizeof(".text")-1);

188. pesect[PEOBJ_SECT_TEXT].ofs = sofs;

189. sofs += (pesect[PEOBJ_SECT_TEXT].size = (uint32_t)ctx->codesz);

190. pesect[PEOBJ_SECT_TEXT].relocofs = sofs;

191. sofs += (pesect[PEOBJ_SECT_TEXT].nreloc = (uint16_t)ctx->nreloc) * PEOBJ_RELOC_SIZE;

192. /* Flags: 60 = read+execute, 50 = align16, 20 = code. */

193. pesect[PEOBJ_SECT_TEXT].flags = PEOBJ_TEXT_FLAGS;

195. #if LJ_TARGET_X64

196. memcpy(pesect[PEOBJ_SECT_PDATA].name, ".pdata", sizeof(".pdata")-1);

197. pesect[PEOBJ_SECT_PDATA].ofs = sofs;

198. sofs += (pesect[PEOBJ_SECT_PDATA].size = 6*4);

199. pesect[PEOBJ_SECT_PDATA].relocofs = sofs;

200. sofs += (pesect[PEOBJ_SECT_PDATA].nreloc = 6) * PEOBJ_RELOC_SIZE;

201. /* Flags: 40 = read, 30 = align4, 40 = initialized data. */

202. pesect[PEOBJ_SECT_PDATA].flags = 0x40300040;

204. memcpy(pesect[PEOBJ_SECT_XDATA].name, ".xdata", sizeof(".xdata")-1);

205. pesect[PEOBJ_SECT_XDATA].ofs = sofs;

206. sofs += (pesect[PEOBJ_SECT_XDATA].size = 8*2+4+6*2);  /* See below. */

207. pesect[PEOBJ_SECT_XDATA].relocofs = sofs;

208. sofs += (pesect[PEOBJ_SECT_XDATA].nreloc = 1) * PEOBJ_RELOC_SIZE;

209. /* Flags: 40 = read, 30 = align4, 40 = initialized data. */

210. pesect[PEOBJ_SECT_XDATA].flags = 0x40300040;

211. #endif

213. memcpy(pesect[PEOBJ_SECT_RDATA_Z].name, ".rdata$Z", sizeof(".rdata$Z")-1);

214. pesect[PEOBJ_SECT_RDATA_Z].ofs = sofs;

215. sofs += (pesect[PEOBJ_SECT_RDATA_Z].size = (uint32_t)strlen(ctx->dasm_ident)+1);

216. /* Flags: 40 = read, 30 = align4, 40 = initialized data. */

217. pesect[PEOBJ_SECT_RDATA_Z].flags = 0x40300040;

219. /* Fill in PE header. */

220. pehdr.arch = PEOBJ_ARCH_TARGET;

221. pehdr.nsects = PEOBJ_NSECTIONS;

222. pehdr.time = 0;  /* Timestamp is optional. */

223. pehdr.symtabofs = sofs;

224. pehdr.opthdrsz = 0;

225. pehdr.flags = 0;

227. /* Compute the size of the symbol table:

228. ** @feat.00 + nsections*2

229. ** + asm_start + nsym

230. ** + nrsym

231. */

232. nrsym = ctx->nrelocsym;

233. pehdr.nsyms = 1+PEOBJ_NSECTIONS*2 + 1+ctx->nsym + nrsym;

234. #if LJ_TARGET_X64

235. pehdr.nsyms += 1;  /* Symbol for lj_err_unwind_win64. */

236. #endif

238. /* Write PE object header and all sections. */

239. owrite(ctx, &pehdr, sizeof(PEheader));

240. owrite(ctx, &pesect, sizeof(PEsection)*PEOBJ_NSECTIONS);

242. /* Write .text section. */

243. host_endian.u = 1;

244. if (host_endian.b != LJ_ENDIAN_SELECT(1, 0)) {

245. #if LJ_TARGET_PPC

246. uint32_t *p = (uint32_t *)ctx->code;

247. int n = (int)(ctx->codesz >> 2);

248. for (i = 0; i < n; i++, p++)

249. *p = lj_bswap(*p);  /* Byteswap .text section. */

250. #else

251. fprintf(stderr, "Error: different byte order for host and target\n");

252. exit(1);

253. #endif

254. }

255. owrite(ctx, ctx->code, ctx->codesz);

256. for (i = 0; i < ctx->nreloc; i++) {

257. PEreloc reloc;

258. reloc.vaddr = (uint32_t)ctx->reloc[i].ofs + PEOBJ_RELOC_OFS;

259. reloc.symidx = 1+2+ctx->reloc[i].sym;  /* Reloc syms are after .text sym. */

260. reloc.type = ctx->reloc[i].type ? PEOBJ_RELOC_REL32 : PEOBJ_RELOC_DIR32;

261. owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);

262. }

264. #if LJ_TARGET_X64

265. { /* Write .pdata section. */

266. uint32_t fcofs = (uint32_t)ctx->sym[ctx->nsym-1].ofs;

267. uint32_t pdata[3];  /* Start of .text, end of .text and .xdata. */

268. PEreloc reloc;

269. pdata[0] = 0; pdata[1] = fcofs; pdata[2] = 0;

270. owrite(ctx, &pdata, sizeof(pdata));

271. pdata[0] = fcofs; pdata[1] = (uint32_t)ctx->codesz; pdata[2] = 20;

272. owrite(ctx, &pdata, sizeof(pdata));

273. reloc.vaddr = 0; reloc.symidx = 1+2+nrsym+2+2+1;

274. reloc.type = PEOBJ_RELOC_ADDR32NB;

275. owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);

276. reloc.vaddr = 4; reloc.symidx = 1+2+nrsym+2+2+1;

277. reloc.type = PEOBJ_RELOC_ADDR32NB;

278. owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);

279. reloc.vaddr = 8; reloc.symidx = 1+2+nrsym+2;

280. reloc.type = PEOBJ_RELOC_ADDR32NB;

281. owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);

282. reloc.vaddr = 12; reloc.symidx = 1+2+nrsym+2+2+1;

283. reloc.type = PEOBJ_RELOC_ADDR32NB;

284. owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);

285. reloc.vaddr = 16; reloc.symidx = 1+2+nrsym+2+2+1;

286. reloc.type = PEOBJ_RELOC_ADDR32NB;

287. owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);

288. reloc.vaddr = 20; reloc.symidx = 1+2+nrsym+2;

289. reloc.type = PEOBJ_RELOC_ADDR32NB;

290. owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);

291. }

292. { /* Write .xdata section. */

293. uint16_t xdata[8+2+6];

294. PEreloc reloc;

295. xdata[0] = 0x01|0x08|0x10;  /* Ver. 1, uhandler/ehandler, prolog size 0. */

296. xdata[1] = 0x0005;  /* Number of unwind codes, no frame pointer. */

297. xdata[2] = 0x4200;  /* Stack offset 4*8+8 = aword*5. */

298. xdata[3] = 0x3000;  /* Push rbx. */

299. xdata[4] = 0x6000;  /* Push rsi. */

300. xdata[5] = 0x7000;  /* Push rdi. */

301. xdata[6] = 0x5000;  /* Push rbp. */

302. xdata[7] = 0;  /* Alignment. */

303. xdata[8] = xdata[9] = 0;  /* Relocated address of exception handler. */

304. xdata[10] = 0x01;  /* Ver. 1, no handler, prolog size 0. */

305. xdata[11] = 0x1504;  /* Number of unwind codes, fp = rbp, fpofs = 16. */

306. xdata[12] = 0x0300;  /* set_fpreg. */

307. xdata[13] = 0x0200;  /* stack offset 0*8+8 = aword*1. */

308. xdata[14] = 0x3000;  /* Push rbx. */

309. xdata[15] = 0x5000;  /* Push rbp. */

310. owrite(ctx, &xdata, sizeof(xdata));

311. reloc.vaddr = 2*8; reloc.symidx = 1+2+nrsym+2+2;

312. reloc.type = PEOBJ_RELOC_ADDR32NB;

313. owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);

314. }

315. #endif

317. /* Write .rdata$Z section. */

318. owrite(ctx, ctx->dasm_ident, strlen(ctx->dasm_ident)+1);

320. /* Write symbol table. */

321. strtab = NULL;  /* 1st pass: collect string sizes. */

322. for (;;) {

323. strtabofs = 4;

324. /* Mark as SafeSEH compliant. */

325. emit_peobj_sym(ctx, "@feat.00", 1,

326. PEOBJ_SECT_ABS, PEOBJ_TYPE_NULL, PEOBJ_SCL_STATIC);

328. emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_TEXT);

329. for (i = 0; i < nrsym; i++)

330. emit_peobj_sym(ctx, ctx->relocsym[i], 0,

331. PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);

333. #if LJ_TARGET_X64

334. emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_PDATA);

335. emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_XDATA);

336. emit_peobj_sym(ctx, "lj_err_unwind_win64", 0,

337. PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);

338. #endif

340. emit_peobj_sym(ctx, ctx->beginsym, 0,

341. PEOBJ_SECT_TEXT, PEOBJ_TYPE_NULL, PEOBJ_SCL_EXTERN);

342. for (i = 0; i < ctx->nsym; i++)

343. emit_peobj_sym(ctx, ctx->sym[i].name, (uint32_t)ctx->sym[i].ofs,

344. PEOBJ_SECT_TEXT, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);

346. emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_RDATA_Z);

348. if (strtab)

349. break;

350. /* 2nd pass: alloc strtab, write syms and copy strings. */

351. strtab = (char *)malloc(strtabofs);

352. *(uint32_t *)strtab = (uint32_t)strtabofs;

353. }

355. /* Write string table. */

356. owrite(ctx, strtab, strtabofs);

357. }

359. #else

361. ‌void emit_peobj(BuildCtx *ctx)

362. {

363. UNUSED(ctx);

364. fprintf(stderr, "Error: no PE object support for this target\n");

365. exit(1);

366. }

368. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/host/genlibbc.lua - luajit-2.0-src

Functions defined

- code

- code

- find_defs

- fixup_dump

- gen_header

- local function w(x) t[#t+1]

- local isbe

- parse_arg

- read_files

- read_uleb128

- transform_lua

- usage

- write_file

Source code

1. ----------------------------------------------------------------------------

2. -- Lua script to dump the bytecode of the library functions written in Lua.

3. -- The resulting 'buildvm_libbc.h' is used for the build process of LuaJIT.

4. ----------------------------------------------------------------------------

5. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

6. -- Released under the MIT license. See Copyright Notice in luajit.h

7. ----------------------------------------------------------------------------

9. local ffi = require("ffi")

10. local bit = require("bit")

11. local vmdef = require("jit.vmdef")

12. local bcnames = vmdef.bcnames

14. local format = string.format

16. ‌local isbe = (string.byte(string.dump(function() end), 5) % 2 == 1)

18. ‌local function usage(arg)

19. io.stderr:write("Usage: ", arg and arg[0] or "genlibbc",

20. " [-o buildvm_libbc.h] lib_*.c\n")

21. os.exit(1)

22. end

24. ‌local function parse_arg(arg)

25. local outfile = "-"

26. if not (arg and arg[1]) then

27. usage(arg)

28. end

29. if arg[1] == "-o" then

30. outfile = arg[2]

31. if not outfile then usage(arg) end

32. table.remove(arg, 1)

33. table.remove(arg, 1)

34. end

35. return outfile

36. end

38. ‌local function read_files(names)

39. local src = ""

40. for _,name in ipairs(names) do

41. local fp = assert(io.open(name))

42. src = src .. fp:read("*a")

43. fp:close()

44. end

45. return src

46. end

48. ‌local function transform_lua(code)

49. local fixup = {}

50. local n = -30000

51. ‌  code = string.gsub(code, "CHECK_(%w*)%((.-)%)", function(tp, var)

52. n = n + 1

53. fixup[n] = { "CHECK", tp }

54. return format("%s=%d", var, n)

55. end)

56. ‌  code = string.gsub(code, "PAIRS%((.-)%)", function(var)

57. fixup.PAIRS = true

58. return format("nil, %s, 0", var)

59. end)

60. return "return "..code, fixup

61. end

63. ‌local function read_uleb128(p)

64. local v = p[0]; p = p + 1

65. if v >= 128 then

66. local sh = 7; v = v - 128

67. repeat

68. local r = p[0]

69. v = v + bit.lshift(bit.band(r, 127), sh)

70. sh = sh + 7

71. p = p + 1

72. until r < 128

73. end

74. return p, v

75. end

77. -- ORDER LJ_T

78. local name2itype = {

79. str = 5, func = 9, tab = 12, int = 14, num = 15

80. }

82. local BC = {}

83. for i=0,#bcnames/6-1 do

84. BC[string.gsub(string.sub(bcnames, i*6+1, i*6+6), " ", "")] = i

85. end

86. local xop, xra = isbe and 3 or 0, isbe and 2 or 1

87. local xrc, xrb = isbe and 1 or 2, isbe and 0 or 3

89. ‌local function fixup_dump(dump, fixup)

90. local buf = ffi.new("uint8_t[?]", #dump+1, dump)

91. local p = buf+5

92. local n, sizebc

93. p, n = read_uleb128(p)

94. local start = p

95. p = p + 4

96. p = read_uleb128(p)

97. p = read_uleb128(p)

98. p, sizebc = read_uleb128(p)

99. local rawtab = {}

100. for i=0,sizebc-1 do

101. local op = p[xop]

102. if op == BC.KSHORT then

103. local rd = p[xrc] + 256*p[xrb]

104. rd = bit.arshift(bit.lshift(rd, 16), 16)

105. local f = fixup[rd]

106. if f then

107. if f[1] == "CHECK" then

108. local tp = f[2]

109. if tp == "tab" then rawtab[p[xra]] = true end

110. p[xop] = tp == "num" and BC.ISNUM or BC.ISTYPE

111. p[xrb] = 0

112. p[xrc] = name2itype[tp]

113. else

114. error("unhandled fixup type: "..f[1])

115. end

116. end

117. elseif op == BC.TGETV then

118. if rawtab[p[xrb]] then

119. p[xop] = BC.TGETR

120. end

121. elseif op == BC.TSETV then

122. if rawtab[p[xrb]] then

123. p[xop] = BC.TSETR

124. end

125. elseif op == BC.ITERC then

126. if fixup.PAIRS then

127. p[xop] = BC.ITERN

128. end

129. end

130. p = p + 4

131. end

132. return ffi.string(start, n)

133. end

135. ‌local function find_defs(src)

136. local defs = {}

137. for name, code in string.gmatch(src, "LJLIB_LUA%(([^)]*)%)%s*/%*(.-)%*/") do

138. local env = {}

139. local tcode, fixup = transform_lua(code)

140. local func = assert(load(tcode, "", nil, env))()

141. defs[name] = fixup_dump(string.dump(func, true), fixup)

142. defs[#defs+1] = name

143. end

144. return defs

145. end

147. ‌local function gen_header(defs)

148. local t = {}

149. ‌  local function w(x) t[#t+1] = x end

150. w("/* This is a generated file. DO NOT EDIT! */\n\n")

151. w("static const int libbc_endian = ") w(isbe and 1 or 0) w(";\n\n")

152. local s = ""

153. for _,name in ipairs(defs) do

154. s = s .. defs[name]

155. end

156. w("static const uint8_t libbc_code[] = {\n")

157. local n = 0

158. for i=1,#s do

159. local x = string.byte(s, i)

160. w(x); w(",")

161. n = n + (x < 10 and 2 or (x < 100 and 3 or 4))

162. if n >= 75 then n = 0; w("\n") end

163. end

164. w("0\n};\n\n")

165. w("static const struct { const char *name; int ofs; } libbc_map[] = {\n")

166. local m = 0

167. for _,name in ipairs(defs) do

168. w('{"'); w(name); w('",'); w(m) w('},\n')

169. m = m + #defs[name]

170. end

171. w("{NULL,"); w(m); w("}\n};\n\n")

172. return table.concat(t)

173. end

175. ‌local function write_file(name, data)

176. if name == "-" then

177. assert(io.write(data))

178. assert(io.flush())

179. else

180. local fp = io.open(name)

181. if fp then

182. local old = fp:read("*a")

183. fp:close()

184. if data == old then return end

185. end

186. fp = assert(io.open(name, "w"))

187. assert(fp:write(data))

188. assert(fp:close())

189. end

190. end

192. local outfile = parse_arg(arg)

193. local src = read_files(arg)

194. local defs = find_defs(src)

195. local hdr = gen_header(defs)

196. write_file(outfile, hdr)

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/bcsave.lua - luajit-2.0-src

Functions defined

- aligned

- bclist

- bcsave

- bcsave_c

- bcsave_elfobj

- bcsave_machobj

- bcsave_obj

- bcsave_peobj

- bcsave_raw

- bcsave_tail

- check

- checkarg

- checkmodname

- detectmodname

- detecttype

- docmd

- f16

- f16

- f32

- f32

- fofs

- if type(input)

- readfile

- savefile

- usage

Source code

1. ----------------------------------------------------------------------------

2. -- LuaJIT module to save/list bytecode.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. --

8. -- This module saves or lists the bytecode for an input file.

9. -- It's run by the -b command line option.

10. --

11. ------------------------------------------------------------------------------

13. local jit = require("jit")

14. assert(jit.version_num == 20100, "LuaJIT core/library version mismatch")

15. local bit = require("bit")

17. -- Symbol name prefix for LuaJIT bytecode.

18. local LJBC_PREFIX = "luaJIT_BC_"

20. ------------------------------------------------------------------------------

22. ‌local function usage()

23. io.stderr:write[[

24. Save LuaJIT bytecode: luajit -b[options] input output

25. -l        Only list bytecode.

26. -s        Strip debug info (default).

27. -g        Keep debug info.

28. -n name   Set module name (default: auto-detect from input name).

29. -t type   Set output file type (default: auto-detect from output name).

30. -a arch   Override architecture for object files (default: native).

31. -o os     Override OS for object files (default: native).

32. -e chunk  Use chunk string as input.

33. --        Stop handling options.

34. -         Use stdin as input and/or stdout as output.

36. File types: c h obj o raw (default)

37. ]]

38. os.exit(1)

39. end

41. ‌local function check(ok, ...)

42. if ok then return ok, ... end

43. io.stderr:write("luajit: ", ...)

44. io.stderr:write("\n")

45. os.exit(1)

46. end

48. ‌local function readfile(input)

49. ‌  if type(input) == "function" then return input end

50. if input == "-" then input = nil end

51. return check(loadfile(input))

52. end

54. ‌local function savefile(name, mode)

55. if name == "-" then return io.stdout end

56. return check(io.open(name, mode))

57. end

59. ------------------------------------------------------------------------------

61. local map_type = {

62. raw = "raw", c = "c", h = "h", o = "obj", obj = "obj",

63. }

65. local map_arch = {

66. x86 = true, x64 = true, arm = true, ppc = true,

67. mips = true, mipsel = true,

68. }

70. local map_os = {

71. linux = true, windows = true, osx = true, freebsd = true, netbsd = true,

72. openbsd = true, dragonfly = true, solaris = true,

73. }

75. ‌local function checkarg(str, map, err)

76. str = string.lower(str)

77. local s = check(map[str], "unknown ", err)

78. return s == true and str or s

79. end

81. ‌local function detecttype(str)

82. local ext = string.match(string.lower(str), "%.(%a+)$")

83. return map_type[ext] or "raw"

84. end

86. ‌local function checkmodname(str)

87. check(string.match(str, "^[%w_.%-]+$"), "bad module name")

88. return string.gsub(str, "[%.%-]", "_")

89. end

91. ‌local function detectmodname(str)

92. if type(str) == "string" then

93. local tail = string.match(str, "[^/\\]+$")

94. if tail then str = tail end

95. local head = string.match(str, "^(.*)%.[^.]*$")

96. if head then str = head end

97. str = string.match(str, "^[%w_.%-]+")

98. else

99. str = nil

100. end

101. check(str, "cannot derive module name, use -n name")

102. return string.gsub(str, "[%.%-]", "_")

103. end

105. ------------------------------------------------------------------------------

107. ‌local function bcsave_tail(fp, output, s)

108. local ok, err = fp:write(s)

109. if ok and output ~= "-" then ok, err = fp:close() end

110. check(ok, "cannot write ", output, ": ", err)

111. end

113. ‌local function bcsave_raw(output, s)

114. local fp = savefile(output, "wb")

115. bcsave_tail(fp, output, s)

116. end

118. ‌local function bcsave_c(ctx, output, s)

119. local fp = savefile(output, "w")

120. if ctx.type == "c" then

121. fp:write(string.format([[

122. #ifdef _cplusplus

123. extern "C"

124. #endif

125. #ifdef _WIN32

126. __declspec(dllexport)

127. #endif

128. const char %s%s[] = {

129. ]], LJBC_PREFIX, ctx.modname))

130. else

131. fp:write(string.format([[

132. #define %s%s_SIZE %d

133. static const char %s%s[] = {

134. ]], LJBC_PREFIX, ctx.modname, #s, LJBC_PREFIX, ctx.modname))

135. end

136. local t, n, m = {}, 0, 0

137. for i=1,#s do

138. local b = tostring(string.byte(s, i))

139. m = m + #b + 1

140. if m > 78 then

141. fp:write(table.concat(t, ",", 1, n), ",\n")

142. n, m = 0, #b + 1

143. end

144. n = n + 1

145. t[n] = b

146. end

147. bcsave_tail(fp, output, table.concat(t, ",", 1, n).."\n};\n")

148. end

150. ‌local function bcsave_elfobj(ctx, output, s, ffi)

151. ffi.cdef[[

152. typedef struct {

153. uint8_t emagic[4], eclass, eendian, eversion, eosabi, eabiversion, epad[7];

154. uint16_t type, machine;

155. uint32_t version;

156. uint32_t entry, phofs, shofs;

157. uint32_t flags;

158. uint16_t ehsize, phentsize, phnum, shentsize, shnum, shstridx;

159. } ELF32header;

160. typedef struct {

161. uint8_t emagic[4], eclass, eendian, eversion, eosabi, eabiversion, epad[7];

162. uint16_t type, machine;

163. uint32_t version;

164. uint64_t entry, phofs, shofs;

165. uint32_t flags;

166. uint16_t ehsize, phentsize, phnum, shentsize, shnum, shstridx;

167. } ELF64header;

168. typedef struct {

169. uint32_t name, type, flags, addr, ofs, size, link, info, align, entsize;

170. } ELF32sectheader;

171. typedef struct {

172. uint32_t name, type;

173. uint64_t flags, addr, ofs, size;

174. uint32_t link, info;

175. uint64_t align, entsize;

176. } ELF64sectheader;

177. typedef struct {

178. uint32_t name, value, size;

179. uint8_t info, other;

180. uint16_t sectidx;

181. } ELF32symbol;

182. typedef struct {

183. uint32_t name;

184. uint8_t info, other;

185. uint16_t sectidx;

186. uint64_t value, size;

187. } ELF64symbol;

188. typedef struct {

189. ELF32header hdr;

190. ELF32sectheader sect[6];

191. ELF32symbol sym[2];

192. uint8_t space[4096];

193. } ELF32obj;

194. typedef struct {

195. ELF64header hdr;

196. ELF64sectheader sect[6];

197. ELF64symbol sym[2];

198. uint8_t space[4096];

199. } ELF64obj;

200. ]]

201. local symname = LJBC_PREFIX..ctx.modname

202. local is64, isbe = false, false

203. if ctx.arch == "x64" then

204. is64 = true

205. elseif ctx.arch == "ppc" or ctx.arch == "mips" then

206. isbe = true

207. end

209. -- Handle different host/target endianess.

210. ‌  local function f32(x) return x end

211. local f16, fofs = f32, f32

212. if ffi.abi("be") ~= isbe then

213. f32 = bit.bswap

214. ‌    function f16(x) return bit.rshift(bit.bswap(x), 16) end

215. if is64 then

216. local two32 = ffi.cast("int64_t", 2^32)

217. ‌      function fofs(x) return bit.bswap(x)*two32 end

218. else

219. fofs = f32

220. end

221. end

223. -- Create ELF object and fill in header.

224. local o = ffi.new(is64 and "ELF64obj" or "ELF32obj")

225. local hdr = o.hdr

226. if ctx.os == "bsd" or ctx.os == "other" then -- Determine native hdr.eosabi.

227. local bf = assert(io.open("/bin/ls", "rb"))

228. local bs = bf:read(9)

229. bf:close()

230. ffi.copy(o, bs, 9)

231. check(hdr.emagic[0] == 127, "no support for writing native object files")

232. else

233. hdr.emagic = "\127ELF"

234. hdr.eosabi = ({ freebsd=9, netbsd=2, openbsd=12, solaris=6 })[ctx.os] or 0

235. end

236. hdr.eclass = is64 and 2 or 1

237. hdr.eendian = isbe and 2 or 1

238. hdr.eversion = 1

239. hdr.type = f16(1)

240. hdr.machine = f16(({ x86=3, x64=62, arm=40, ppc=20, mips=8, mipsel=8 })[ctx.arch])

241. if ctx.arch == "mips" or ctx.arch == "mipsel" then

242. hdr.flags = 0x50001006

243. end

244. hdr.version = f32(1)

245. hdr.shofs = fofs(ffi.offsetof(o, "sect"))

246. hdr.ehsize = f16(ffi.sizeof(hdr))

247. hdr.shentsize = f16(ffi.sizeof(o.sect[0]))

248. hdr.shnum = f16(6)

249. hdr.shstridx = f16(2)

251. -- Fill in sections and symbols.

252. local sofs, ofs = ffi.offsetof(o, "space"), 1

253. for i,name in ipairs{

254. ".symtab", ".shstrtab", ".strtab", ".rodata", ".note.GNU-stack",

255. } do

256. local sect = o.sect[i]

257. sect.align = fofs(1)

258. sect.name = f32(ofs)

259. ffi.copy(o.space+ofs, name)

260. ofs = ofs + #name+1

261. end

262. o.sect[1].type = f32(2) -- .symtab

263. o.sect[1].link = f32(3)

264. o.sect[1].info = f32(1)

265. o.sect[1].align = fofs(8)

266. o.sect[1].ofs = fofs(ffi.offsetof(o, "sym"))

267. o.sect[1].entsize = fofs(ffi.sizeof(o.sym[0]))

268. o.sect[1].size = fofs(ffi.sizeof(o.sym))

269. o.sym[1].name = f32(1)

270. o.sym[1].sectidx = f16(4)

271. o.sym[1].size = fofs(#s)

272. o.sym[1].info = 17

273. o.sect[2].type = f32(3) -- .shstrtab

274. o.sect[2].ofs = fofs(sofs)

275. o.sect[2].size = fofs(ofs)

276. o.sect[3].type = f32(3) -- .strtab

277. o.sect[3].ofs = fofs(sofs + ofs)

278. o.sect[3].size = fofs(#symname+1)

279. ffi.copy(o.space+ofs+1, symname)

280. ofs = ofs + #symname + 2

281. o.sect[4].type = f32(1) -- .rodata

282. o.sect[4].flags = fofs(2)

283. o.sect[4].ofs = fofs(sofs + ofs)

284. o.sect[4].size = fofs(#s)

285. o.sect[5].type = f32(1) -- .note.GNU-stack

286. o.sect[5].ofs = fofs(sofs + ofs + #s)

288. -- Write ELF object file.

289. local fp = savefile(output, "wb")

290. fp:write(ffi.string(o, ffi.sizeof(o)-4096+ofs))

291. bcsave_tail(fp, output, s)

292. end

294. ‌local function bcsave_peobj(ctx, output, s, ffi)

295. ffi.cdef[[

296. typedef struct {

297. uint16_t arch, nsects;

298. uint32_t time, symtabofs, nsyms;

299. uint16_t opthdrsz, flags;

300. } PEheader;

301. typedef struct {

302. char name[8];

303. uint32_t vsize, vaddr, size, ofs, relocofs, lineofs;

304. uint16_t nreloc, nline;

305. uint32_t flags;

306. } PEsection;

307. typedef struct __attribute((packed)) {

308. union {

309. char name[8];

310. uint32_t nameref[2];

311. };

312. uint32_t value;

313. int16_t sect;

314. uint16_t type;

315. uint8_t scl, naux;

316. } PEsym;

317. typedef struct __attribute((packed)) {

318. uint32_t size;

319. uint16_t nreloc, nline;

320. uint32_t cksum;

321. uint16_t assoc;

322. uint8_t comdatsel, unused[3];

323. } PEsymaux;

324. typedef struct {

325. PEheader hdr;

326. PEsection sect[2];

327. // Must be an even number of symbol structs.

328. PEsym sym0;

329. PEsymaux sym0aux;

330. PEsym sym1;

331. PEsymaux sym1aux;

332. PEsym sym2;

333. PEsym sym3;

334. uint32_t strtabsize;

335. uint8_t space[4096];

336. } PEobj;

337. ]]

338. local symname = LJBC_PREFIX..ctx.modname

339. local is64 = false

340. if ctx.arch == "x86" then

341. symname = "_"..symname

342. elseif ctx.arch == "x64" then

343. is64 = true

344. end

345. local symexport = "   /EXPORT:"..symname..",DATA "

347. -- The file format is always little-endian. Swap if the host is big-endian.

348. ‌  local function f32(x) return x end

349. local f16 = f32

350. if ffi.abi("be") then

351. f32 = bit.bswap

352. ‌    function f16(x) return bit.rshift(bit.bswap(x), 16) end

353. end

355. -- Create PE object and fill in header.

356. local o = ffi.new("PEobj")

357. local hdr = o.hdr

358. hdr.arch = f16(({ x86=0x14c, x64=0x8664, arm=0x1c0, ppc=0x1f2, mips=0x366, mipsel=0x366 })[ctx.arch])

359. hdr.nsects = f16(2)

360. hdr.symtabofs = f32(ffi.offsetof(o, "sym0"))

361. hdr.nsyms = f32(6)

363. -- Fill in sections and symbols.

364. o.sect[0].name = ".drectve"

365. o.sect[0].size = f32(#symexport)

366. o.sect[0].flags = f32(0x00100a00)

367. o.sym0.sect = f16(1)

368. o.sym0.scl = 3

369. o.sym0.name = ".drectve"

370. o.sym0.naux = 1

371. o.sym0aux.size = f32(#symexport)

372. o.sect[1].name = ".rdata"

373. o.sect[1].size = f32(#s)

374. o.sect[1].flags = f32(0x40300040)

375. o.sym1.sect = f16(2)

376. o.sym1.scl = 3

377. o.sym1.name = ".rdata"

378. o.sym1.naux = 1

379. o.sym1aux.size = f32(#s)

380. o.sym2.sect = f16(2)

381. o.sym2.scl = 2

382. o.sym2.nameref[1] = f32(4)

383. o.sym3.sect = f16(-1)

384. o.sym3.scl = 2

385. o.sym3.value = f32(1)

386. o.sym3.name = "@feat.00" -- Mark as SafeSEH compliant.

387. ffi.copy(o.space, symname)

388. local ofs = #symname + 1

389. o.strtabsize = f32(ofs + 4)

390. o.sect[0].ofs = f32(ffi.offsetof(o, "space") + ofs)

391. ffi.copy(o.space + ofs, symexport)

392. ofs = ofs + #symexport

393. o.sect[1].ofs = f32(ffi.offsetof(o, "space") + ofs)

395. -- Write PE object file.

396. local fp = savefile(output, "wb")

397. fp:write(ffi.string(o, ffi.sizeof(o)-4096+ofs))

398. bcsave_tail(fp, output, s)

399. end

401. ‌local function bcsave_machobj(ctx, output, s, ffi)

402. ffi.cdef[[

403. typedef struct

404. {

405. uint32_t magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags;

406. } mach_header;

407. typedef struct

408. {

409. mach_header; uint32_t reserved;

410. } mach_header_64;

411. typedef struct {

412. uint32_t cmd, cmdsize;

413. char segname[16];

414. uint32_t vmaddr, vmsize, fileoff, filesize;

415. uint32_t maxprot, initprot, nsects, flags;

416. } mach_segment_command;

417. typedef struct {

418. uint32_t cmd, cmdsize;

419. char segname[16];

420. uint64_t vmaddr, vmsize, fileoff, filesize;

421. uint32_t maxprot, initprot, nsects, flags;

422. } mach_segment_command_64;

423. typedef struct {

424. char sectname[16], segname[16];

425. uint32_t addr, size;

426. uint32_t offset, align, reloff, nreloc, flags;

427. uint32_t reserved1, reserved2;

428. } mach_section;

429. typedef struct {

430. char sectname[16], segname[16];

431. uint64_t addr, size;

432. uint32_t offset, align, reloff, nreloc, flags;

433. uint32_t reserved1, reserved2, reserved3;

434. } mach_section_64;

435. typedef struct {

436. uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize;

437. } mach_symtab_command;

438. typedef struct {

439. int32_t strx;

440. uint8_t type, sect;

441. int16_t desc;

442. uint32_t value;

443. } mach_nlist;

444. typedef struct {

445. uint32_t strx;

446. uint8_t type, sect;

447. uint16_t desc;

448. uint64_t value;

449. } mach_nlist_64;

450. typedef struct

451. {

452. uint32_t magic, nfat_arch;

453. } mach_fat_header;

454. typedef struct

455. {

456. uint32_t cputype, cpusubtype, offset, size, align;

457. } mach_fat_arch;

458. typedef struct {

459. struct {

460. mach_header hdr;

461. mach_segment_command seg;

462. mach_section sec;

463. mach_symtab_command sym;

464. } arch[1];

465. mach_nlist sym_entry;

466. uint8_t space[4096];

467. } mach_obj;

468. typedef struct {

469. struct {

470. mach_header_64 hdr;

471. mach_segment_command_64 seg;

472. mach_section_64 sec;

473. mach_symtab_command sym;

474. } arch[1];

475. mach_nlist_64 sym_entry;

476. uint8_t space[4096];

477. } mach_obj_64;

478. typedef struct {

479. mach_fat_header fat;

480. mach_fat_arch fat_arch[4];

481. struct {

482. mach_header hdr;

483. mach_segment_command seg;

484. mach_section sec;

485. mach_symtab_command sym;

486. } arch[4];

487. mach_nlist sym_entry;

488. uint8_t space[4096];

489. } mach_fat_obj;

490. ]]

491. local symname = '_'..LJBC_PREFIX..ctx.modname

492. local isfat, is64, align, mobj = false, false, 4, "mach_obj"

493. if ctx.arch == "x64" then

494. is64, align, mobj = true, 8, "mach_obj_64"

495. elseif ctx.arch == "arm" then

496. isfat, mobj = true, "mach_fat_obj"

497. else

498. check(ctx.arch == "x86", "unsupported architecture for OSX")

499. end

500. ‌  local function aligned(v, a) return bit.band(v+a-1, -a) end

501. local be32 = bit.bswap -- Mach-O FAT is BE, supported archs are LE.

503. -- Create Mach-O object and fill in header.

504. local o = ffi.new(mobj)

505. local mach_size = aligned(ffi.offsetof(o, "space")+#symname+2, align)

506. local cputype = ({ x86={7}, x64={0x01000007}, arm={7,12,12,12} })[ctx.arch]

507. local cpusubtype = ({ x86={3}, x64={3}, arm={3,6,9,11} })[ctx.arch]

508. if isfat then

509. o.fat.magic = be32(0xcafebabe)

510. o.fat.nfat_arch = be32(#cpusubtype)

511. end

513. -- Fill in sections and symbols.

514. for i=0,#cpusubtype-1 do

515. local ofs = 0

516. if isfat then

517. local a = o.fat_arch[i]

518. a.cputype = be32(cputype[i+1])

519. a.cpusubtype = be32(cpusubtype[i+1])

520. -- Subsequent slices overlap each other to share data.

521. ofs = ffi.offsetof(o, "arch") + i*ffi.sizeof(o.arch[0])

522. a.offset = be32(ofs)

523. a.size = be32(mach_size-ofs+#s)

524. end

525. local a = o.arch[i]

526. a.hdr.magic = is64 and 0xfeedfacf or 0xfeedface

527. a.hdr.cputype = cputype[i+1]

528. a.hdr.cpusubtype = cpusubtype[i+1]

529. a.hdr.filetype = 1

530. a.hdr.ncmds = 2

531. a.hdr.sizeofcmds = ffi.sizeof(a.seg)+ffi.sizeof(a.sec)+ffi.sizeof(a.sym)

532. a.seg.cmd = is64 and 0x19 or 0x1

533. a.seg.cmdsize = ffi.sizeof(a.seg)+ffi.sizeof(a.sec)

534. a.seg.vmsize = #s

535. a.seg.fileoff = mach_size-ofs

536. a.seg.filesize = #s

537. a.seg.maxprot = 1

538. a.seg.initprot = 1

539. a.seg.nsects = 1

540. ffi.copy(a.sec.sectname, "__data")

541. ffi.copy(a.sec.segname, "__DATA")

542. a.sec.size = #s

543. a.sec.offset = mach_size-ofs

544. a.sym.cmd = 2

545. a.sym.cmdsize = ffi.sizeof(a.sym)

546. a.sym.symoff = ffi.offsetof(o, "sym_entry")-ofs

547. a.sym.nsyms = 1

548. a.sym.stroff = ffi.offsetof(o, "sym_entry")+ffi.sizeof(o.sym_entry)-ofs

549. a.sym.strsize = aligned(#symname+2, align)

550. end

551. o.sym_entry.type = 0xf

552. o.sym_entry.sect = 1

553. o.sym_entry.strx = 1

554. ffi.copy(o.space+1, symname)

556. -- Write Macho-O object file.

557. local fp = savefile(output, "wb")

558. fp:write(ffi.string(o, mach_size))

559. bcsave_tail(fp, output, s)

560. end

562. ‌local function bcsave_obj(ctx, output, s)

563. local ok, ffi = pcall(require, "ffi")

564. check(ok, "FFI library required to write this file type")

565. if ctx.os == "windows" then

566. return bcsave_peobj(ctx, output, s, ffi)

567. elseif ctx.os == "osx" then

568. return bcsave_machobj(ctx, output, s, ffi)

569. else

570. return bcsave_elfobj(ctx, output, s, ffi)

571. end

572. end

574. ------------------------------------------------------------------------------

576. ‌local function bclist(input, output)

577. local f = readfile(input)

578. require("jit.bc").dump(f, savefile(output, "w"), true)

579. end

581. ‌local function bcsave(ctx, input, output)

582. local f = readfile(input)

583. local s = string.dump(f, ctx.strip)

584. local t = ctx.type

585. if not t then

586. t = detecttype(output)

587. ctx.type = t

588. end

589. if t == "raw" then

590. bcsave_raw(output, s)

591. else

592. if not ctx.modname then ctx.modname = detectmodname(input) end

593. if t == "obj" then

594. bcsave_obj(ctx, output, s)

595. else

596. bcsave_c(ctx, output, s)

597. end

598. end

599. end

601. ‌local function docmd(...)

602. local arg = {...}

603. local n = 1

604. local list = false

605. local ctx = {

606. strip = true, arch = jit.arch, os = string.lower(jit.os),

607. type = false, modname = false,

608. }

609. while n <= #arg do

610. local a = arg[n]

611. if type(a) == "string" and string.sub(a, 1, 1) == "-" and a ~= "-" then

612. table.remove(arg, n)

613. if a == "--" then break end

614. for m=2,#a do

615. local opt = string.sub(a, m, m)

616. if opt == "l" then

617. list = true

618. elseif opt == "s" then

619. ctx.strip = true

620. elseif opt == "g" then

621. ctx.strip = false

622. else

623. if arg[n] == nil or m ~= #a then usage() end

624. if opt == "e" then

625. if n ~= 1 then usage() end

626. arg[1] = check(loadstring(arg[1]))

627. elseif opt == "n" then

628. ctx.modname = checkmodname(table.remove(arg, n))

629. elseif opt == "t" then

630. ctx.type = checkarg(table.remove(arg, n), map_type, "file type")

631. elseif opt == "a" then

632. ctx.arch = checkarg(table.remove(arg, n), map_arch, "architecture")

633. elseif opt == "o" then

634. ctx.os = checkarg(table.remove(arg, n), map_os, "OS name")

635. else

636. usage()

637. end

638. end

639. end

640. else

641. n = n + 1

642. end

643. end

644. if list then

645. if #arg == 0 or #arg > 2 then usage() end

646. bclist(arg[1], arg[2] or "-")

647. else

648. if #arg ~= 2 then usage() end

649. bcsave(ctx, arg[1], arg[2])

650. end

651. end

653. ------------------------------------------------------------------------------

655. -- Public module functions.

656. return {

657. start = docmd -- Process -b command line option.

658. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/ - luajit-2.0-src

- bc.lua

- bcsave.lua

- dis_arm.lua

- dis_mips.lua

- dis_mipsel.lua

- dis_ppc.lua

- dis_x64.lua

- dis_x86.lua

- dump.lua

- p.lua

- v.lua

- vmdef.lua

- zone.lua

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/bc.lua - luajit-2.0-src

Functions defined

- bcdump

- bcline

- bclistoff

- bcliston

- bctargets

- ctlsub

- h_list

Source code

1. ----------------------------------------------------------------------------

2. -- LuaJIT bytecode listing module.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. --

8. -- This module lists the bytecode of a Lua function. If it's loaded by -jbc

9. -- it hooks into the parser and lists all functions of a chunk as they

10. -- are parsed.

11. --

12. -- Example usage:

13. --

14. --   luajit -jbc -e 'local x=0; for i=1,1e6 do x=x+i end; print(x)'

15. --   luajit -jbc=- foo.lua

16. --   luajit -jbc=foo.list foo.lua

17. --

18. -- Default output is to stderr. To redirect the output to a file, pass a

19. -- filename as an argument (use '-' for stdout) or set the environment

20. -- variable LUAJIT_LISTFILE. The file is overwritten every time the module

21. -- is started.

22. --

23. -- This module can also be used programmatically:

24. --

25. --   local bc = require("jit.bc")

26. --

27. --   local function foo() print("hello") end

28. --

29. --   bc.dump(foo)           --> -- BYTECODE -- [...]

30. --   print(bc.line(foo, 2)) --> 0002    KSTR     1   1      ; "hello"

31. --

32. --   local out = {

33. --     -- Do something with each line:

34. --     write = function(t, ...) io.write(...) end,

35. --     close = function(t) end,

36. --     flush = function(t) end,

37. --   }

38. --   bc.dump(foo, out)

39. --

40. ------------------------------------------------------------------------------

42. -- Cache some library functions and objects.

43. local jit = require("jit")

44. assert(jit.version_num == 20100, "LuaJIT core/library version mismatch")

45. local jutil = require("jit.util")

46. local vmdef = require("jit.vmdef")

47. local bit = require("bit")

48. local sub, gsub, format = string.sub, string.gsub, string.format

49. local byte, band, shr = string.byte, bit.band, bit.rshift

50. local funcinfo, funcbc, funck = jutil.funcinfo, jutil.funcbc, jutil.funck

51. local funcuvname = jutil.funcuvname

52. local bcnames = vmdef.bcnames

53. local stdout, stderr = io.stdout, io.stderr

55. ------------------------------------------------------------------------------

57. ‌local function ctlsub(c)

58. if c == "\n" then return "\\n"

59. elseif c == "\r" then return "\\r"

60. elseif c == "\t" then return "\\t"

61. else return format("\\%03d", byte(c))

62. end

63. end

65. -- Return one bytecode line.

66. ‌local function bcline(func, pc, prefix)

67. local ins, m = funcbc(func, pc)

68. if not ins then return end

69. local ma, mb, mc = band(m, 7), band(m, 15*8), band(m, 15*128)

70. local a = band(shr(ins, 8), 0xff)

71. local oidx = 6*band(ins, 0xff)

72. local op = sub(bcnames, oidx+1, oidx+6)

73. local s = format("%04d %s %-6s %3s ",

74. pc, prefix or "  ", op, ma == 0 and "" or a)

75. local d = shr(ins, 16)

76. if mc == 13*128 then -- BCMjump

77. return format("%s=> %04d\n", s, pc+d-0x7fff)

78. end

79. if mb ~= 0 then

80. d = band(d, 0xff)

81. elseif mc == 0 then

82. return s.."\n"

83. end

84. local kc

85. if mc == 10*128 then -- BCMstr

86. kc = funck(func, -d-1)

87. kc = format(#kc > 40 and '"%.40s"~' or '"%s"', gsub(kc, "%c", ctlsub))

88. elseif mc == 9*128 then -- BCMnum

89. kc = funck(func, d)

90. if op == "TSETM " then kc = kc - 2^52 end

91. elseif mc == 12*128 then -- BCMfunc

92. local fi = funcinfo(funck(func, -d-1))

93. if fi.ffid then

94. kc = vmdef.ffnames[fi.ffid]

95. else

96. kc = fi.loc

97. end

98. elseif mc == 5*128 then -- BCMuv

99. kc = funcuvname(func, d)

100. end

101. if ma == 5 then -- BCMuv

102. local ka = funcuvname(func, a)

103. if kc then kc = ka.." ; "..kc else kc = ka end

104. end

105. if mb ~= 0 then

106. local b = shr(ins, 24)

107. if kc then return format("%s%3d %3d  ; %s\n", s, b, d, kc) end

108. return format("%s%3d %3d\n", s, b, d)

109. end

110. if kc then return format("%s%3d      ; %s\n", s, d, kc) end

111. if mc == 7*128 and d > 32767 then d = d - 65536 end -- BCMlits

112. return format("%s%3d\n", s, d)

113. end

115. -- Collect branch targets of a function.

116. ‌local function bctargets(func)

117. local target = {}

118. for pc=1,1000000000 do

119. local ins, m = funcbc(func, pc)

120. if not ins then break end

121. if band(m, 15*128) == 13*128 then target[pc+shr(ins, 16)-0x7fff] = true end

122. end

123. return target

124. end

126. -- Dump bytecode instructions of a function.

127. ‌local function bcdump(func, out, all)

128. if not out then out = stdout end

129. local fi = funcinfo(func)

130. if all and fi.children then

131. for n=-1,-1000000000,-1 do

132. local k = funck(func, n)

133. if not k then break end

134. if type(k) == "proto" then bcdump(k, out, true) end

135. end

136. end

137. out:write(format("-- BYTECODE -- %s-%d\n", fi.loc, fi.lastlinedefined))

138. local target = bctargets(func)

139. for pc=1,1000000000 do

140. local s = bcline(func, pc, target[pc] and "=>")

141. if not s then break end

142. out:write(s)

143. end

144. out:write("\n")

145. out:flush()

146. end

148. ------------------------------------------------------------------------------

150. -- Active flag and output file handle.

151. local active, out

153. -- List handler.

154. ‌local function h_list(func)

155. return bcdump(func, out)

156. end

158. -- Detach list handler.

159. ‌local function bclistoff()

160. if active then

161. active = false

162. jit.attach(h_list)

163. if out and out ~= stdout and out ~= stderr then out:close() end

164. out = nil

165. end

166. end

168. -- Open the output file and attach list handler.

169. ‌local function bcliston(outfile)

170. if active then bclistoff() end

171. if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end

172. if outfile then

173. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))

174. else

175. out = stderr

176. end

177. jit.attach(h_list, "bc")

178. active = true

179. end

181. -- Public module functions.

182. return {

183. line = bcline,

184. dump = bcdump,

185. targets = bctargets,

186. on = bcliston,

187. off = bclistoff,

188. start = bcliston -- For -j command line option.

189. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/zone.lua - luajit-2.0-src

Functions defined

- __call

- flush

- get

Source code

1. ----------------------------------------------------------------------------

2. -- LuaJIT profiler zones.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. --

8. -- This module implements a simple hierarchical zone model.

9. --

10. -- Example usage:

11. --

12. --   local zone = require("jit.zone")

13. --   zone("AI")

14. --   ...

15. --     zone("A*")

16. --     ...

17. --     print(zone:get()) --> "A*"

18. --     ...

19. --     zone()

20. --   ...

21. --   print(zone:get()) --> "AI"

22. --   ...

23. --   zone()

24. --

25. ----------------------------------------------------------------------------

27. local remove = table.remove

29. return setmetatable({

30. ‌  flush = function(t)

31. for i=#t,1,-1 do t[i] = nil end

32. end,

33. ‌  get = function(t)

34. return t[#t]

35. end

36. }, {

37. ‌  __call = function(t, zone)

38. if zone then

39. t[#t+1] = zone

40. else

41. return (assert(remove(t), "empty zone stack"))

42. end

43. end

44. })

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/p.lua - luajit-2.0-src

Functions defined

- key_stack

- mode

- mode

- mode

- prof_annotate

- prof_cb

- prof_finish

- prof_start

- prof_top

- start

Source code

1. ----------------------------------------------------------------------------

2. -- LuaJIT profiler.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. --

8. -- This module is a simple command line interface to the built-in

9. -- low-overhead profiler of LuaJIT.

10. --

11. -- The lower-level API of the profiler is accessible via the "jit.profile"

12. -- module or the luaJIT_profile_* C API.

13. --

14. -- Example usage:

15. --

16. --   luajit -jp myapp.lua

17. --   luajit -jp=s myapp.lua

18. --   luajit -jp=-s myapp.lua

19. --   luajit -jp=vl myapp.lua

20. --   luajit -jp=G,profile.txt myapp.lua

21. --

22. -- The following dump features are available:

23. --

24. --   f  Stack dump: function name, otherwise module:line. Default mode.

25. --   F  Stack dump: ditto, but always prepend module.

26. --   l  Stack dump: module:line.

27. --   <number> stack dump depth (callee < caller). Default: 1.

28. --   -<number> Inverse stack dump depth (caller > callee).

29. --   s  Split stack dump after first stack level. Implies abs(depth) >= 2.

30. --   p  Show full path for module names.

31. --   v  Show VM states. Can be combined with stack dumps, e.g. vf or fv.

32. --   z  Show zones. Can be combined with stack dumps, e.g. zf or fz.

33. --   r  Show raw sample counts. Default: show percentages.

34. --   a  Annotate excerpts from source code files.

35. --   A  Annotate complete source code files.

36. --   G  Produce raw output suitable for graphical tools (e.g. flame graphs).

37. --   m<number> Minimum sample percentage to be shown. Default: 3.

38. --   i<number> Sampling interval in milliseconds. Default: 10.

39. --

40. ----------------------------------------------------------------------------

42. -- Cache some library functions and objects.

43. local jit = require("jit")

44. assert(jit.version_num == 20100, "LuaJIT core/library version mismatch")

45. local profile = require("jit.profile")

46. local vmdef = require("jit.vmdef")

47. local math = math

48. local pairs, ipairs, tonumber, floor = pairs, ipairs, tonumber, math.floor

49. local sort, format = table.sort, string.format

50. local stdout = io.stdout

51. local zone -- Load jit.zone module on demand.

53. -- Output file handle.

54. local out

56. ------------------------------------------------------------------------------

58. local prof_ud

59. local prof_states, prof_split, prof_min, prof_raw, prof_fmt, prof_depth

60. local prof_ann, prof_count1, prof_count2, prof_samples

62. local map_vmmode = {

63. N = "Compiled",

64. I = "Interpreted",

65. C = "C code",

66. G = "Garbage Collector",

67. J = "JIT Compiler",

68. }

70. -- Profiler callback.

71. ‌local function prof_cb(th, samples, vmmode)

72. prof_samples = prof_samples + samples

73. local key_stack, key_stack2, key_state

74. -- Collect keys for sample.

75. if prof_states then

76. if prof_states == "v" then

77. key_state = map_vmmode[vmmode] or vmmode

78. else

79. key_state = zone:get() or "(none)"

80. end

81. end

82. if prof_fmt then

83. key_stack = profile.dumpstack(th, prof_fmt, prof_depth)

84. ‌    key_stack = key_stack:gsub("%[builtin#(%d+)%]", function(x)

85. return vmdef.ffnames[tonumber(x)]

86. end)

87. if prof_split == 2 then

88. local k1, k2 = key_stack:match("(.-) [<>] (.*)")

89. if k2 then key_stack, key_stack2 = k1, k2 end

90. elseif prof_split == 3 then

91. key_stack2 = profile.dumpstack(th, "l", 1)

92. end

93. end

94. -- Order keys.

95. local k1, k2

96. if prof_split == 1 then

97. if key_state then

98. k1 = key_state

99. if key_stack then k2 = key_stack end

100. end

101. elseif key_stack then

102. k1 = key_stack

103. if key_stack2 then k2 = key_stack2 elseif key_state then k2 = key_state end

104. end

105. -- Coalesce samples in one or two levels.

106. if k1 then

107. local t1 = prof_count1

108. t1[k1] = (t1[k1] or 0) + samples

109. if k2 then

110. local t2 = prof_count2

111. local t3 = t2[k1]

112. if not t3 then t3 = {}; t2[k1] = t3 end

113. t3[k2] = (t3[k2] or 0) + samples

114. end

115. end

116. end

118. ------------------------------------------------------------------------------

120. -- Show top N list.

121. ‌local function prof_top(count1, count2, samples, indent)

122. local t, n = {}, 0

123. for k, v in pairs(count1) do

124. n = n + 1

125. t[n] = k

126. end

127. sort(t, function(a, b) return count1[a] > count1[b] end)

128. for i=1,n do

129. local k = t[i]

130. local v = count1[k]

131. local pct = floor(v*100/samples + 0.5)

132. if pct < prof_min then break end

133. if not prof_raw then

134. out:write(format("%s%2d%%  %s\n", indent, pct, k))

135. elseif prof_raw == "r" then

136. out:write(format("%s%5d  %s\n", indent, v, k))

137. else

138. out:write(format("%s %d\n", k, v))

139. end

140. if count2 then

141. local r = count2[k]

142. if r then

143. prof_top(r, nil, v, (prof_split == 3 or prof_split == 1) and "  -- " or

144. (prof_depth < 0 and "  -> " or "  <- "))

145. end

146. end

147. end

148. end

150. -- Annotate source code

151. ‌local function prof_annotate(count1, samples)

152. local files = {}

153. local ms = 0

154. for k, v in pairs(count1) do

155. local pct = floor(v*100/samples + 0.5)

156. ms = math.max(ms, v)

157. if pct >= prof_min then

158. local file, line = k:match("^(.*):(%d+)$")

159. local fl = files[file]

160. if not fl then fl = {}; files[file] = fl; files[#files+1] = file end

161. line = tonumber(line)

162. fl[line] = prof_raw and v or pct

163. end

164. end

165. sort(files)

166. local fmtv, fmtn = " %3d%% | %s\n", "      | %s\n"

167. if prof_raw then

168. local n = math.max(5, math.ceil(math.log10(ms)))

169. fmtv = "%"..n.."d | %s\n"

170. fmtn = (" "):rep(n).." | %s\n"

171. end

172. local ann = prof_ann

173. for _, file in ipairs(files) do

174. local f0 = file:byte()

175. if f0 == 40 or f0 == 91 then

176. out:write(format("\n====== %s ======\n[Cannot annotate non-file]\n", file))

177. break

178. end

179. local fp, err = io.open(file)

180. if not fp then

181. out:write(format("====== ERROR: %s: %s\n", file, err))

182. break

183. end

184. out:write(format("\n====== %s ======\n", file))

185. local fl = files[file]

186. local n, show = 1, false

187. if ann ~= 0 then

188. for i=1,ann do

189. if fl[i] then show = true; out:write("@@ 1 @@\n"); break end

190. end

191. end

192. for line in fp:lines() do

193. if line:byte() == 27 then

194. out:write("[Cannot annotate bytecode file]\n")

195. break

196. end

197. local v = fl[n]

198. if ann ~= 0 then

199. local v2 = fl[n+ann]

200. if show then

201. if v2 then show = n+ann elseif v then show = n

202. elseif show+ann < n then show = false end

203. elseif v2 then

204. show = n+ann

205. out:write(format("@@ %d @@\n", n))

206. end

207. if not show then goto next end

208. end

209. if v then

210. out:write(format(fmtv, v, line))

211. else

212. out:write(format(fmtn, line))

213. end

214. ::next::

215. n = n + 1

216. end

217. fp:close()

218. end

219. end

221. ------------------------------------------------------------------------------

223. -- Finish profiling and dump result.

224. ‌local function prof_finish()

225. if prof_ud then

226. profile.stop()

227. local samples = prof_samples

228. if samples == 0 then

229. if prof_raw ~= true then out:write("[No samples collected]\n") end

230. return

231. end

232. if prof_ann then

233. prof_annotate(prof_count1, samples)

234. else

235. prof_top(prof_count1, prof_count2, samples, "")

236. end

237. prof_count1 = nil

238. prof_count2 = nil

239. prof_ud = nil

240. end

241. end

243. -- Start profiling.

244. ‌local function prof_start(mode)

245. local interval = ""

246. ‌  mode = mode:gsub("i%d*", function(s) interval = s; return "" end)

247. prof_min = 3

248. ‌  mode = mode:gsub("m(%d+)", function(s) prof_min = tonumber(s); return "" end)

249. prof_depth = 1

250. ‌  mode = mode:gsub("%-?%d+", function(s) prof_depth = tonumber(s); return "" end)

251. local m = {}

252. for c in mode:gmatch(".") do m[c] = c end

253. prof_states = m.z or m.v

254. if prof_states == "z" then zone = require("jit.zone") end

255. local scope = m.l or m.f or m.F or (prof_states and "" or "f")

256. local flags = (m.p or "")

257. prof_raw = m.r

258. if m.s then

259. prof_split = 2

260. if prof_depth == -1 or m["-"] then prof_depth = -2

261. elseif prof_depth == 1 then prof_depth = 2 end

262. elseif mode:find("[fF].*l") then

263. scope = "l"

264. prof_split = 3

265. else

266. prof_split = (scope == "" or mode:find("[zv].*[lfF]")) and 1 or 0

267. end

268. prof_ann = m.A and 0 or (m.a and 3)

269. if prof_ann then

270. scope = "l"

271. prof_fmt = "pl"

272. prof_split = 0

273. prof_depth = 1

274. elseif m.G and scope ~= "" then

275. prof_fmt = flags..scope.."Z;"

276. prof_depth = -100

277. prof_raw = true

278. prof_min = 0

279. elseif scope == "" then

280. prof_fmt = false

281. else

282. local sc = prof_split == 3 and m.f or m.F or scope

283. prof_fmt = flags..sc..(prof_depth >= 0 and "Z < " or "Z > ")

284. end

285. prof_count1 = {}

286. prof_count2 = {}

287. prof_samples = 0

288. profile.start(scope:lower()..interval, prof_cb)

289. prof_ud = newproxy(true)

290. getmetatable(prof_ud).__gc = prof_finish

291. end

293. ------------------------------------------------------------------------------

295. ‌local function start(mode, outfile)

296. if not outfile then outfile = os.getenv("LUAJIT_PROFILEFILE") end

297. if outfile then

298. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))

299. else

300. out = stdout

301. end

302. prof_start(mode or "f")

303. end

305. -- Public module functions.

306. return {

307. start = start, -- For -j command line option.

308. stop = prof_finish

309. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/dis_x86.lua - luajit-2.0-src

Functions defined

- ["!"]

- [""]

- ["*"]

- [":"]

- clearprefixes

- create

- create64

- disass

- disass64

- disass_block

- dispatch

- dispatchmap

- fp

- getimm

- getmrm

- incomplete

- nop

- opc2

- opc3

- putop

- putpat

- regname

- regname64

- rex

- sz

- unknown

- vm

Source code

1. ----------------------------------------------------------------------------

2. -- LuaJIT x86/x64 disassembler module.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. -- This is a helper module used by the LuaJIT machine code dumper module.

8. --

9. -- Sending small code snippets to an external disassembler and mixing the

10. -- output with our own stuff was too fragile. So I had to bite the bullet

11. -- and write yet another x86 disassembler. Oh well ...

12. --

13. -- The output format is very similar to what ndisasm generates. But it has

14. -- been developed independently by looking at the opcode tables from the

15. -- Intel and AMD manuals. The supported instruction set is quite extensive

16. -- and reflects what a current generation Intel or AMD CPU implements in

17. -- 32 bit and 64 bit mode. Yes, this includes MMX, SSE, SSE2, SSE3, SSSE3,

18. -- SSE4.1, SSE4.2, SSE4a and even privileged and hypervisor (VMX/SVM)

19. -- instructions.

20. --

21. -- Notes:

22. -- * The (useless) a16 prefix, 3DNow and pre-586 opcodes are unsupported.

23. -- * No attempt at optimization has been made -- it's fast enough for my needs.

24. -- * The public API may change when more architectures are added.

25. ------------------------------------------------------------------------------

27. local type = type

28. local sub, byte, format = string.sub, string.byte, string.format

29. local match, gmatch, gsub = string.match, string.gmatch, string.gsub

30. local lower, rep = string.lower, string.rep

31. local bit = require("bit")

32. local tohex = bit.tohex

34. -- Map for 1st opcode byte in 32 bit mode. Ugly? Well ... read on.

35. local map_opc1_32 = {

36. --0x

37. [0]="addBmr","addVmr","addBrm","addVrm","addBai","addVai","push es","pop es",

38. "orBmr","orVmr","orBrm","orVrm","orBai","orVai","push cs","opc2*",

39. --1x

40. "adcBmr","adcVmr","adcBrm","adcVrm","adcBai","adcVai","push ss","pop ss",

41. "sbbBmr","sbbVmr","sbbBrm","sbbVrm","sbbBai","sbbVai","push ds","pop ds",

42. --2x

43. "andBmr","andVmr","andBrm","andVrm","andBai","andVai","es:seg","daa",

44. "subBmr","subVmr","subBrm","subVrm","subBai","subVai","cs:seg","das",

45. --3x

46. "xorBmr","xorVmr","xorBrm","xorVrm","xorBai","xorVai","ss:seg","aaa",

47. "cmpBmr","cmpVmr","cmpBrm","cmpVrm","cmpBai","cmpVai","ds:seg","aas",

48. --4x

49. "incVR","incVR","incVR","incVR","incVR","incVR","incVR","incVR",

50. "decVR","decVR","decVR","decVR","decVR","decVR","decVR","decVR",

51. --5x

52. "pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR",

53. "popUR","popUR","popUR","popUR","popUR","popUR","popUR","popUR",

54. --6x

55. "sz*pushaw,pusha","sz*popaw,popa","boundVrm","arplWmr",

56. "fs:seg","gs:seg","o16:","a16",

57. "pushUi","imulVrmi","pushBs","imulVrms",

58. "insb","insVS","outsb","outsVS",

59. --7x

60. "joBj","jnoBj","jbBj","jnbBj","jzBj","jnzBj","jbeBj","jaBj",

61. "jsBj","jnsBj","jpeBj","jpoBj","jlBj","jgeBj","jleBj","jgBj",

62. --8x

63. "arith!Bmi","arith!Vmi","arith!Bmi","arith!Vms",

64. "testBmr","testVmr","xchgBrm","xchgVrm",

65. "movBmr","movVmr","movBrm","movVrm",

66. "movVmg","leaVrm","movWgm","popUm",

67. --9x

68. "nop*xchgVaR|pause|xchgWaR|repne nop","xchgVaR","xchgVaR","xchgVaR",

69. "xchgVaR","xchgVaR","xchgVaR","xchgVaR",

70. "sz*cbw,cwde,cdqe","sz*cwd,cdq,cqo","call farViw","wait",

71. "sz*pushfw,pushf","sz*popfw,popf","sahf","lahf",

72. --Ax

73. "movBao","movVao","movBoa","movVoa",

74. "movsb","movsVS","cmpsb","cmpsVS",

75. "testBai","testVai","stosb","stosVS",

76. "lodsb","lodsVS","scasb","scasVS",

77. --Bx

78. "movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi",

79. "movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI",

80. --Cx

81. "shift!Bmu","shift!Vmu","retBw","ret","$lesVrm","$ldsVrm","movBmi","movVmi",

82. "enterBwu","leave","retfBw","retf","int3","intBu","into","iretVS",

83. --Dx

84. "shift!Bm1","shift!Vm1","shift!Bmc","shift!Vmc","aamBu","aadBu","salc","xlatb",

85. "fp*0","fp*1","fp*2","fp*3","fp*4","fp*5","fp*6","fp*7",

86. --Ex

87. "loopneBj","loopeBj","loopBj","sz*jcxzBj,jecxzBj,jrcxzBj",

88. "inBau","inVau","outBua","outVua",

89. "callVj","jmpVj","jmp farViw","jmpBj","inBad","inVad","outBda","outVda",

90. --Fx

91. "lock:","int1","repne:rep","rep:","hlt","cmc","testb!Bm","testv!Vm",

92. "clc","stc","cli","sti","cld","std","incb!Bm","incd!Vm",

93. }

94. assert(#map_opc1_32 == 255)

96. -- Map for 1st opcode byte in 64 bit mode (overrides only).

97. local map_opc1_64 = setmetatable({

98. [0x06]=false, [0x07]=false, [0x0e]=false,

99. [0x16]=false, [0x17]=false, [0x1e]=false, [0x1f]=false,

100. [0x27]=false, [0x2f]=false, [0x37]=false, [0x3f]=false,

101. [0x60]=false, [0x61]=false, [0x62]=false, [0x63]="movsxdVrDmt", [0x67]="a32:",

102. [0x40]="rex*",   [0x41]="rex*b",   [0x42]="rex*x",   [0x43]="rex*xb",

103. [0x44]="rex*r",  [0x45]="rex*rb",  [0x46]="rex*rx",  [0x47]="rex*rxb",

104. [0x48]="rex*w",  [0x49]="rex*wb",  [0x4a]="rex*wx",  [0x4b]="rex*wxb",

105. [0x4c]="rex*wr", [0x4d]="rex*wrb", [0x4e]="rex*wrx", [0x4f]="rex*wrxb",

106. [0x82]=false, [0x9a]=false, [0xc4]=false, [0xc5]=false, [0xce]=false,

107. [0xd4]=false, [0xd5]=false, [0xd6]=false, [0xea]=false,

108. }, { __index = map_opc1_32 })

110. -- Map for 2nd opcode byte (0F xx). True CISC hell. Hey, I told you.

111. -- Prefix dependent MMX/SSE opcodes: (none)|rep|o16|repne, -|F3|66|F2

112. local map_opc2 = {

113. --0x

114. [0]="sldt!Dmp","sgdt!Ump","larVrm","lslVrm",nil,"syscall","clts","sysret",

115. "invd","wbinvd",nil,"ud1",nil,"$prefetch!Bm","femms","3dnowMrmu",

116. --1x

117. "movupsXrm|movssXrm|movupdXrm|movsdXrm",

118. "movupsXmr|movssXmr|movupdXmr|movsdXmr",

119. "movhlpsXrm$movlpsXrm|movsldupXrm|movlpdXrm|movddupXrm",

120. "movlpsXmr||movlpdXmr",

121. "unpcklpsXrm||unpcklpdXrm",

122. "unpckhpsXrm||unpckhpdXrm",

123. "movlhpsXrm$movhpsXrm|movshdupXrm|movhpdXrm",

124. "movhpsXmr||movhpdXmr",

125. "$prefetcht!Bm","hintnopVm","hintnopVm","hintnopVm",

126. "hintnopVm","hintnopVm","hintnopVm","hintnopVm",

127. --2x

128. "movUmx$","movUmy$","movUxm$","movUym$","movUmz$",nil,"movUzm$",nil,

129. "movapsXrm||movapdXrm",

130. "movapsXmr||movapdXmr",

131. "cvtpi2psXrMm|cvtsi2ssXrVmt|cvtpi2pdXrMm|cvtsi2sdXrVmt",

132. "movntpsXmr|movntssXmr|movntpdXmr|movntsdXmr",

133. "cvttps2piMrXm|cvttss2siVrXm|cvttpd2piMrXm|cvttsd2siVrXm",

134. "cvtps2piMrXm|cvtss2siVrXm|cvtpd2piMrXm|cvtsd2siVrXm",

135. "ucomissXrm||ucomisdXrm",

136. "comissXrm||comisdXrm",

137. --3x

138. "wrmsr","rdtsc","rdmsr","rdpmc","sysenter","sysexit",nil,"getsec",

139. "opc3*38",nil,"opc3*3a",nil,nil,nil,nil,nil,

140. --4x

141. "cmovoVrm","cmovnoVrm","cmovbVrm","cmovnbVrm",

142. "cmovzVrm","cmovnzVrm","cmovbeVrm","cmovaVrm",

143. "cmovsVrm","cmovnsVrm","cmovpeVrm","cmovpoVrm",

144. "cmovlVrm","cmovgeVrm","cmovleVrm","cmovgVrm",

145. --5x

146. "movmskpsVrXm$||movmskpdVrXm$","sqrtpsXrm|sqrtssXrm|sqrtpdXrm|sqrtsdXrm",

147. "rsqrtpsXrm|rsqrtssXrm","rcppsXrm|rcpssXrm",

148. "andpsXrm||andpdXrm","andnpsXrm||andnpdXrm",

149. "orpsXrm||orpdXrm","xorpsXrm||xorpdXrm",

150. "addpsXrm|addssXrm|addpdXrm|addsdXrm","mulpsXrm|mulssXrm|mulpdXrm|mulsdXrm",

151. "cvtps2pdXrm|cvtss2sdXrm|cvtpd2psXrm|cvtsd2ssXrm",

152. "cvtdq2psXrm|cvttps2dqXrm|cvtps2dqXrm",

153. "subpsXrm|subssXrm|subpdXrm|subsdXrm","minpsXrm|minssXrm|minpdXrm|minsdXrm",

154. "divpsXrm|divssXrm|divpdXrm|divsdXrm","maxpsXrm|maxssXrm|maxpdXrm|maxsdXrm",

155. --6x

156. "punpcklbwPrm","punpcklwdPrm","punpckldqPrm","packsswbPrm",

157. "pcmpgtbPrm","pcmpgtwPrm","pcmpgtdPrm","packuswbPrm",

158. "punpckhbwPrm","punpckhwdPrm","punpckhdqPrm","packssdwPrm",

159. "||punpcklqdqXrm","||punpckhqdqXrm",

160. "movPrVSm","movqMrm|movdquXrm|movdqaXrm",

161. --7x

162. "pshufwMrmu|pshufhwXrmu|pshufdXrmu|pshuflwXrmu","pshiftw!Pmu",

163. "pshiftd!Pmu","pshiftq!Mmu||pshiftdq!Xmu",

164. "pcmpeqbPrm","pcmpeqwPrm","pcmpeqdPrm","emms|",

165. "vmreadUmr||extrqXmuu$|insertqXrmuu$","vmwriteUrm||extrqXrm$|insertqXrm$",

166. nil,nil,

167. "||haddpdXrm|haddpsXrm","||hsubpdXrm|hsubpsXrm",

168. "movVSmMr|movqXrm|movVSmXr","movqMmr|movdquXmr|movdqaXmr",

169. --8x

170. "joVj","jnoVj","jbVj","jnbVj","jzVj","jnzVj","jbeVj","jaVj",

171. "jsVj","jnsVj","jpeVj","jpoVj","jlVj","jgeVj","jleVj","jgVj",

172. --9x

173. "setoBm","setnoBm","setbBm","setnbBm","setzBm","setnzBm","setbeBm","setaBm",

174. "setsBm","setnsBm","setpeBm","setpoBm","setlBm","setgeBm","setleBm","setgBm",

175. --Ax

176. "push fs","pop fs","cpuid","btVmr","shldVmru","shldVmrc",nil,nil,

177. "push gs","pop gs","rsm","btsVmr","shrdVmru","shrdVmrc","fxsave!Dmp","imulVrm",

178. --Bx

179. "cmpxchgBmr","cmpxchgVmr","$lssVrm","btrVmr",

180. "$lfsVrm","$lgsVrm","movzxVrBmt","movzxVrWmt",

181. "|popcntVrm","ud2Dp","bt!Vmu","btcVmr",

182. "bsfVrm","bsrVrm|lzcntVrm|bsrWrm","movsxVrBmt","movsxVrWmt",

183. --Cx

184. "xaddBmr","xaddVmr",

185. "cmppsXrmu|cmpssXrmu|cmppdXrmu|cmpsdXrmu","$movntiVmr|",

186. "pinsrwPrWmu","pextrwDrPmu",

187. "shufpsXrmu||shufpdXrmu","$cmpxchg!Qmp",

188. "bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR",

189. --Dx

190. "||addsubpdXrm|addsubpsXrm","psrlwPrm","psrldPrm","psrlqPrm",

191. "paddqPrm","pmullwPrm",

192. "|movq2dqXrMm|movqXmr|movdq2qMrXm$","pmovmskbVrMm||pmovmskbVrXm",

193. "psubusbPrm","psubuswPrm","pminubPrm","pandPrm",

194. "paddusbPrm","padduswPrm","pmaxubPrm","pandnPrm",

195. --Ex

196. "pavgbPrm","psrawPrm","psradPrm","pavgwPrm",

197. "pmulhuwPrm","pmulhwPrm",

198. "|cvtdq2pdXrm|cvttpd2dqXrm|cvtpd2dqXrm","$movntqMmr||$movntdqXmr",

199. "psubsbPrm","psubswPrm","pminswPrm","porPrm",

200. "paddsbPrm","paddswPrm","pmaxswPrm","pxorPrm",

201. --Fx

202. "|||lddquXrm","psllwPrm","pslldPrm","psllqPrm",

203. "pmuludqPrm","pmaddwdPrm","psadbwPrm","maskmovqMrm||maskmovdquXrm$",

204. "psubbPrm","psubwPrm","psubdPrm","psubqPrm",

205. "paddbPrm","paddwPrm","padddPrm","ud",

206. }

207. assert(map_opc2[255] == "ud")

209. -- Map for three-byte opcodes. Can't wait for their next invention.

210. local map_opc3 = {

211. ["38"] = { -- [66] 0f 38 xx

212. --0x

213. [0]="pshufbPrm","phaddwPrm","phadddPrm","phaddswPrm",

214. "pmaddubswPrm","phsubwPrm","phsubdPrm","phsubswPrm",

215. "psignbPrm","psignwPrm","psigndPrm","pmulhrswPrm",

216. nil,nil,nil,nil,

217. --1x

218. "||pblendvbXrma",nil,nil,nil,

219. "||blendvpsXrma","||blendvpdXrma",nil,"||ptestXrm",

220. nil,nil,nil,nil,

221. "pabsbPrm","pabswPrm","pabsdPrm",nil,

222. --2x

223. "||pmovsxbwXrm","||pmovsxbdXrm","||pmovsxbqXrm","||pmovsxwdXrm",

224. "||pmovsxwqXrm","||pmovsxdqXrm",nil,nil,

225. "||pmuldqXrm","||pcmpeqqXrm","||$movntdqaXrm","||packusdwXrm",

226. nil,nil,nil,nil,

227. --3x

228. "||pmovzxbwXrm","||pmovzxbdXrm","||pmovzxbqXrm","||pmovzxwdXrm",

229. "||pmovzxwqXrm","||pmovzxdqXrm",nil,"||pcmpgtqXrm",

230. "||pminsbXrm","||pminsdXrm","||pminuwXrm","||pminudXrm",

231. "||pmaxsbXrm","||pmaxsdXrm","||pmaxuwXrm","||pmaxudXrm",

232. --4x

233. "||pmulddXrm","||phminposuwXrm",

234. --Fx

235. [0xf0] = "|||crc32TrBmt",[0xf1] = "|||crc32TrVmt",

236. },

238. ["3a"] = { -- [66] 0f 3a xx

239. --0x

240. [0x00]=nil,nil,nil,nil,nil,nil,nil,nil,

241. "||roundpsXrmu","||roundpdXrmu","||roundssXrmu","||roundsdXrmu",

242. "||blendpsXrmu","||blendpdXrmu","||pblendwXrmu","palignrPrmu",

243. --1x

244. nil,nil,nil,nil,

245. "||pextrbVmXru","||pextrwVmXru","||pextrVmSXru","||extractpsVmXru",

246. nil,nil,nil,nil,nil,nil,nil,nil,

247. --2x

248. "||pinsrbXrVmu","||insertpsXrmu","||pinsrXrVmuS",nil,

249. --4x

250. [0x40] = "||dppsXrmu",

251. [0x41] = "||dppdXrmu",

252. [0x42] = "||mpsadbwXrmu",

253. --6x

254. [0x60] = "||pcmpestrmXrmu",[0x61] = "||pcmpestriXrmu",

255. [0x62] = "||pcmpistrmXrmu",[0x63] = "||pcmpistriXrmu",

256. },

257. }

259. -- Map for VMX/SVM opcodes 0F 01 C0-FF (sgdt group with register operands).

260. local map_opcvm = {

261. [0xc1]="vmcall",[0xc2]="vmlaunch",[0xc3]="vmresume",[0xc4]="vmxoff",

262. [0xc8]="monitor",[0xc9]="mwait",

263. [0xd8]="vmrun",[0xd9]="vmmcall",[0xda]="vmload",[0xdb]="vmsave",

264. [0xdc]="stgi",[0xdd]="clgi",[0xde]="skinit",[0xdf]="invlpga",

265. [0xf8]="swapgs",[0xf9]="rdtscp",

266. }

268. -- Map for FP opcodes. And you thought stack machines are simple?

269. local map_opcfp = {

270. -- D8-DF 00-BF: opcodes with a memory operand.

271. -- D8

272. [0]="faddFm","fmulFm","fcomFm","fcompFm","fsubFm","fsubrFm","fdivFm","fdivrFm",

273. "fldFm",nil,"fstFm","fstpFm","fldenvVm","fldcwWm","fnstenvVm","fnstcwWm",

274. -- DA

275. "fiaddDm","fimulDm","ficomDm","ficompDm",

276. "fisubDm","fisubrDm","fidivDm","fidivrDm",

277. -- DB

278. "fildDm","fisttpDm","fistDm","fistpDm",nil,"fld twordFmp",nil,"fstp twordFmp",

279. -- DC

280. "faddGm","fmulGm","fcomGm","fcompGm","fsubGm","fsubrGm","fdivGm","fdivrGm",

281. -- DD

282. "fldGm","fisttpQm","fstGm","fstpGm","frstorDmp",nil,"fnsaveDmp","fnstswWm",

283. -- DE

284. "fiaddWm","fimulWm","ficomWm","ficompWm",

285. "fisubWm","fisubrWm","fidivWm","fidivrWm",

286. -- DF

287. "fildWm","fisttpWm","fistWm","fistpWm",

288. "fbld twordFmp","fildQm","fbstp twordFmp","fistpQm",

289. -- xx C0-FF: opcodes with a pseudo-register operand.

290. -- D8

291. "faddFf","fmulFf","fcomFf","fcompFf","fsubFf","fsubrFf","fdivFf","fdivrFf",

292. -- D9

293. "fldFf","fxchFf",{"fnop"},nil,

294. {"fchs","fabs",nil,nil,"ftst","fxam"},

295. {"fld1","fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz"},

296. {"f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp","fincstp"},

297. {"fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos"},

298. -- DA

299. "fcmovbFf","fcmoveFf","fcmovbeFf","fcmovuFf",nil,{nil,"fucompp"},nil,nil,

300. -- DB

301. "fcmovnbFf","fcmovneFf","fcmovnbeFf","fcmovnuFf",

302. {nil,nil,"fnclex","fninit"},"fucomiFf","fcomiFf",nil,

303. -- DC

304. "fadd toFf","fmul toFf",nil,nil,

305. "fsub toFf","fsubr toFf","fdivr toFf","fdiv toFf",

306. -- DD

307. "ffreeFf",nil,"fstFf","fstpFf","fucomFf","fucompFf",nil,nil,

308. -- DE

309. "faddpFf","fmulpFf",nil,{nil,"fcompp"},

310. "fsubrpFf","fsubpFf","fdivrpFf","fdivpFf",

311. -- DF

312. nil,nil,nil,nil,{"fnstsw ax"},"fucomipFf","fcomipFf",nil,

313. }

314. assert(map_opcfp[126] == "fcomipFf")

316. -- Map for opcode groups. The subkey is sp from the ModRM byte.

317. local map_opcgroup = {

318. arith = { "add", "or", "adc", "sbb", "and", "sub", "xor", "cmp" },

319. shift = { "rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar" },

320. testb = { "testBmi", "testBmi", "not", "neg", "mul", "imul", "div", "idiv" },

321. testv = { "testVmi", "testVmi", "not", "neg", "mul", "imul", "div", "idiv" },

322. incb = { "inc", "dec" },

323. incd = { "inc", "dec", "callUmp", "$call farDmp",

324. "jmpUmp", "$jmp farDmp", "pushUm" },

325. sldt = { "sldt", "str", "lldt", "ltr", "verr", "verw" },

326. sgdt = { "vm*$sgdt", "vm*$sidt", "$lgdt", "vm*$lidt",

327. "smsw", nil, "lmsw", "vm*$invlpg" },

328. bt = { nil, nil, nil, nil, "bt", "bts", "btr", "btc" },

329. cmpxchg = { nil, "sz*,cmpxchg8bQmp,cmpxchg16bXmp", nil, nil,

330. nil, nil, "vmptrld|vmxon|vmclear", "vmptrst" },

331. pshiftw = { nil, nil, "psrlw", nil, "psraw", nil, "psllw" },

332. pshiftd = { nil, nil, "psrld", nil, "psrad", nil, "pslld" },

333. pshiftq = { nil, nil, "psrlq", nil, nil, nil, "psllq" },

334. pshiftdq = { nil, nil, "psrlq", "psrldq", nil, nil, "psllq", "pslldq" },

335. fxsave = { "$fxsave", "$fxrstor", "$ldmxcsr", "$stmxcsr",

336. nil, "lfenceDp$", "mfenceDp$", "sfenceDp$clflush" },

337. prefetch = { "prefetch", "prefetchw" },

338. prefetcht = { "prefetchnta", "prefetcht0", "prefetcht1", "prefetcht2" },

339. }

341. ------------------------------------------------------------------------------

343. -- Maps for register names.

344. local map_regs = {

345. B = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",

346. "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" },

347. B64 = { "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",

348. "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" },

349. W = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",

350. "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w" },

351. D = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",

352. "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" },

353. Q = { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",

354. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" },

355. M = { "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",

356. "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7" }, -- No x64 ext!

357. X = { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",

358. "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15" },

359. }

360. local map_segregs = { "es", "cs", "ss", "ds", "fs", "gs", "segr6", "segr7" }

362. -- Maps for size names.

363. local map_sz2n = {

364. B = 1, W = 2, D = 4, Q = 8, M = 8, X = 16,

365. }

366. local map_sz2prefix = {

367. B = "byte", W = "word", D = "dword",

368. Q = "qword",

369. M = "qword", X = "xword",

370. F = "dword", G = "qword", -- No need for sizes/register names for these two.

371. }

373. ------------------------------------------------------------------------------

375. -- Output a nicely formatted line with an opcode and operands.

376. ‌local function putop(ctx, text, operands)

377. local code, pos, hex = ctx.code, ctx.pos, ""

378. local hmax = ctx.hexdump

379. if hmax > 0 then

380. for i=ctx.start,pos-1 do

381. hex = hex..format("%02X", byte(code, i, i))

382. end

383. if #hex > hmax then hex = sub(hex, 1, hmax)..". "

384. else hex = hex..rep(" ", hmax-#hex+2) end

385. end

386. if operands then text = text.." "..operands end

387. if ctx.o16 then text = "o16 "..text; ctx.o16 = false end

388. if ctx.a32 then text = "a32 "..text; ctx.a32 = false end

389. if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end

390. if ctx.rex then

391. local t = (ctx.rexw and "w" or "")..(ctx.rexr and "r" or "")..

392. (ctx.rexx and "x" or "")..(ctx.rexb and "b" or "")

393. if t ~= "" then text = "rex."..t.." "..text end

394. ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false

395. ctx.rex = false

396. end

397. if ctx.seg then

398. local text2, n = gsub(text, "%[", "["..ctx.seg..":")

399. if n == 0 then text = ctx.seg.." "..text else text = text2 end

400. ctx.seg = false

401. end

402. if ctx.lock then text = "lock "..text; ctx.lock = false end

403. local imm = ctx.imm

404. if imm then

405. local sym = ctx.symtab[imm]

406. if sym then text = text.."\t->"..sym end

407. end

408. ctx.out(format("%08x  %s%s\n", ctx.addr+ctx.start, hex, text))

409. ctx.mrm = false

410. ctx.start = pos

411. ctx.imm = nil

412. end

414. -- Clear all prefix flags.

415. ‌local function clearprefixes(ctx)

416. ctx.o16 = false; ctx.seg = false; ctx.lock = false; ctx.rep = false

417. ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false

418. ctx.rex = false; ctx.a32 = false

419. end

421. -- Fallback for incomplete opcodes at the end.

422. ‌local function incomplete(ctx)

423. ctx.pos = ctx.stop+1

424. clearprefixes(ctx)

425. return putop(ctx, "(incomplete)")

426. end

428. -- Fallback for unknown opcodes.

429. ‌local function unknown(ctx)

430. clearprefixes(ctx)

431. return putop(ctx, "(unknown)")

432. end

434. -- Return an immediate of the specified size.

435. ‌local function getimm(ctx, pos, n)

436. if pos+n-1 > ctx.stop then return incomplete(ctx) end

437. local code = ctx.code

438. if n == 1 then

439. local b1 = byte(code, pos, pos)

440. return b1

441. elseif n == 2 then

442. local b1, b2 = byte(code, pos, pos+1)

443. return b1+b2*256

444. else

445. local b1, b2, b3, b4 = byte(code, pos, pos+3)

446. local imm = b1+b2*256+b3*65536+b4*16777216

447. ctx.imm = imm

448. return imm

449. end

450. end

452. -- Process pattern string and generate the operands.

453. ‌local function putpat(ctx, name, pat)

454. local operands, regs, sz, mode, sp, rm, sc, rx, sdisp

455. local code, pos, stop = ctx.code, ctx.pos, ctx.stop

457. -- Chars used: 1DFGIMPQRSTUVWXacdfgijmoprstuwxyz

458. for p in gmatch(pat, ".") do

459. local x = nil

460. if p == "V" or p == "U" then

461. if ctx.rexw then sz = "Q"; ctx.rexw = false

462. elseif ctx.o16 then sz = "W"; ctx.o16 = false

463. elseif p == "U" and ctx.x64 then sz = "Q"

464. else sz = "D" end

465. regs = map_regs[sz]

466. elseif p == "T" then

467. if ctx.rexw then sz = "Q"; ctx.rexw = false else sz = "D" end

468. regs = map_regs[sz]

469. elseif p == "B" then

470. sz = "B"

471. regs = ctx.rex and map_regs.B64 or map_regs.B

472. elseif match(p, "[WDQMXFG]") then

473. sz = p

474. regs = map_regs[sz]

475. elseif p == "P" then

476. sz = ctx.o16 and "X" or "M"; ctx.o16 = false

477. regs = map_regs[sz]

478. elseif p == "S" then

479. name = name..lower(sz)

480. elseif p == "s" then

481. local imm = getimm(ctx, pos, 1); if not imm then return end

482. x = imm <= 127 and format("+0x%02x", imm)

483. or format("-0x%02x", 256-imm)

484. pos = pos+1

485. elseif p == "u" then

486. local imm = getimm(ctx, pos, 1); if not imm then return end

487. x = format("0x%02x", imm)

488. pos = pos+1

489. elseif p == "w" then

490. local imm = getimm(ctx, pos, 2); if not imm then return end

491. x = format("0x%x", imm)

492. pos = pos+2

493. elseif p == "o" then -- [offset]

494. if ctx.x64 then

495. local imm1 = getimm(ctx, pos, 4); if not imm1 then return end

496. local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end

497. x = format("[0x%08x%08x]", imm2, imm1)

498. pos = pos+8

499. else

500. local imm = getimm(ctx, pos, 4); if not imm then return end

501. x = format("[0x%08x]", imm)

502. pos = pos+4

503. end

504. elseif p == "i" or p == "I" then

505. local n = map_sz2n[sz]

506. if n == 8 and ctx.x64 and p == "I" then

507. local imm1 = getimm(ctx, pos, 4); if not imm1 then return end

508. local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end

509. x = format("0x%08x%08x", imm2, imm1)

510. else

511. if n == 8 then n = 4 end

512. local imm = getimm(ctx, pos, n); if not imm then return end

513. if sz == "Q" and (imm < 0 or imm > 0x7fffffff) then

514. imm = (0xffffffff+1)-imm

515. x = format(imm > 65535 and "-0x%08x" or "-0x%x", imm)

516. else

517. x = format(imm > 65535 and "0x%08x" or "0x%x", imm)

518. end

519. end

520. pos = pos+n

521. elseif p == "j" then

522. local n = map_sz2n[sz]

523. if n == 8 then n = 4 end

524. local imm = getimm(ctx, pos, n); if not imm then return end

525. if sz == "B" and imm > 127 then imm = imm-256

526. elseif imm > 2147483647 then imm = imm-4294967296 end

527. pos = pos+n

528. imm = imm + pos + ctx.addr

529. if imm > 4294967295 and not ctx.x64 then imm = imm-4294967296 end

530. ctx.imm = imm

531. if sz == "W" then

532. x = format("word 0x%04x", imm%65536)

533. elseif ctx.x64 then

534. local lo = imm % 0x1000000

535. x = format("0x%02x%06x", (imm-lo) / 0x1000000, lo)

536. else

537. x = "0x"..tohex(imm)

538. end

539. elseif p == "R" then

540. local r = byte(code, pos-1, pos-1)%8

541. if ctx.rexb then r = r + 8; ctx.rexb = false end

542. x = regs[r+1]

543. elseif p == "a" then x = regs[1]

544. elseif p == "c" then x = "cl"

545. elseif p == "d" then x = "dx"

546. elseif p == "1" then x = "1"

547. else

548. if not mode then

549. mode = ctx.mrm

550. if not mode then

551. if pos > stop then return incomplete(ctx) end

552. mode = byte(code, pos, pos)

553. pos = pos+1

554. end

555. rm = mode%8; mode = (mode-rm)/8

556. sp = mode%8; mode = (mode-sp)/8

557. sdisp = ""

558. if mode < 3 then

559. if rm == 4 then

560. if pos > stop then return incomplete(ctx) end

561. sc = byte(code, pos, pos)

562. pos = pos+1

563. rm = sc%8; sc = (sc-rm)/8

564. rx = sc%8; sc = (sc-rx)/8

565. if ctx.rexx then rx = rx + 8; ctx.rexx = false end

566. if rx == 4 then rx = nil end

567. end

568. if mode > 0 or rm == 5 then

569. local dsz = mode

570. if dsz ~= 1 then dsz = 4 end

571. local disp = getimm(ctx, pos, dsz); if not disp then return end

572. if mode == 0 then rm = nil end

573. if rm or rx or (not sc and ctx.x64 and not ctx.a32) then

574. if dsz == 1 and disp > 127 then

575. sdisp = format("-0x%x", 256-disp)

576. elseif disp >= 0 and disp <= 0x7fffffff then

577. sdisp = format("+0x%x", disp)

578. else

579. sdisp = format("-0x%x", (0xffffffff+1)-disp)

580. end

581. else

582. sdisp = format(ctx.x64 and not ctx.a32 and

583. not (disp >= 0 and disp <= 0x7fffffff)

584. and "0xffffffff%08x" or "0x%08x", disp)

585. end

586. pos = pos+dsz

587. end

588. end

589. if rm and ctx.rexb then rm = rm + 8; ctx.rexb = false end

590. if ctx.rexr then sp = sp + 8; ctx.rexr = false end

591. end

592. if p == "m" then

593. if mode == 3 then x = regs[rm+1]

594. else

595. local aregs = ctx.a32 and map_regs.D or ctx.aregs

596. local srm, srx = "", ""

597. if rm then srm = aregs[rm+1]

598. elseif not sc and ctx.x64 and not ctx.a32 then srm = "rip" end

599. ctx.a32 = false

600. if rx then

601. if rm then srm = srm.."+" end

602. srx = aregs[rx+1]

603. if sc > 0 then srx = srx.."*"..(2^sc) end

604. end

605. x = format("[%s%s%s]", srm, srx, sdisp)

606. end

607. if mode < 3 and

608. (not match(pat, "[aRrgp]") or match(pat, "t")) then -- Yuck.

609. x = map_sz2prefix[sz].." "..x

610. end

611. elseif p == "r" then x = regs[sp+1]

612. elseif p == "g" then x = map_segregs[sp+1]

613. elseif p == "p" then -- Suppress prefix.

614. elseif p == "f" then x = "st"..rm

615. elseif p == "x" then

616. if sp == 0 and ctx.lock and not ctx.x64 then

617. x = "CR8"; ctx.lock = false

618. else

619. x = "CR"..sp

620. end

621. elseif p == "y" then x = "DR"..sp

622. elseif p == "z" then x = "TR"..sp

623. elseif p == "t" then

624. else

625. error("bad pattern `"..pat.."'")

626. end

627. end

628. if x then operands = operands and operands..", "..x or x end

629. end

630. ctx.pos = pos

631. return putop(ctx, name, operands)

632. end

634. -- Forward declaration.

635. local map_act

637. -- Fetch and cache MRM byte.

638. ‌local function getmrm(ctx)

639. local mrm = ctx.mrm

640. if not mrm then

641. local pos = ctx.pos

642. if pos > ctx.stop then return nil end

643. mrm = byte(ctx.code, pos, pos)

644. ctx.pos = pos+1

645. ctx.mrm = mrm

646. end

647. return mrm

648. end

650. -- Dispatch to handler depending on pattern.

651. ‌local function dispatch(ctx, opat, patgrp)

652. if not opat then return unknown(ctx) end

653. if match(opat, "%|") then -- MMX/SSE variants depending on prefix.

654. local p

655. if ctx.rep then

656. p = ctx.rep=="rep" and "%|([^%|]*)" or "%|[^%|]*%|[^%|]*%|([^%|]*)"

657. ctx.rep = false

658. elseif ctx.o16 then p = "%|[^%|]*%|([^%|]*)"; ctx.o16 = false

659. else p = "^[^%|]*" end

660. opat = match(opat, p)

661. if not opat then return unknown(ctx) end

662. --    ctx.rep = false; ctx.o16 = false

663. --XXX fails for 66 f2 0f 38 f1 06  crc32 eax,WORD PTR [esi]

664. --XXX remove in branches?

665. end

666. if match(opat, "%$") then -- reg$mem variants.

667. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end

668. opat = match(opat, mrm >= 192 and "^[^%$]*" or "%$(.*)")

669. if opat == "" then return unknown(ctx) end

670. end

671. if opat == "" then return unknown(ctx) end

672. local name, pat = match(opat, "^([a-z0-9 ]*)(.*)")

673. if pat == "" and patgrp then pat = patgrp end

674. return map_act[sub(pat, 1, 1)](ctx, name, pat)

675. end

677. -- Get a pattern from an opcode map and dispatch to handler.

678. ‌local function dispatchmap(ctx, opcmap)

679. local pos = ctx.pos

680. local opat = opcmap[byte(ctx.code, pos, pos)]

681. pos = pos + 1

682. ctx.pos = pos

683. return dispatch(ctx, opat)

684. end

686. -- Map for action codes. The key is the first char after the name.

687. map_act = {

688. -- Simple opcodes without operands.

689. ‌  [""] = function(ctx, name, pat)

690. return putop(ctx, name)

691. end,

693. -- Operand size chars fall right through.

694. B = putpat, W = putpat, D = putpat, Q = putpat,

695. V = putpat, U = putpat, T = putpat,

696. M = putpat, X = putpat, P = putpat,

697. F = putpat, G = putpat,

699. -- Collect prefixes.

700. ‌  [":"] = function(ctx, name, pat)

701. ctx[pat == ":" and name or sub(pat, 2)] = name

702. if ctx.pos - ctx.start > 5 then return unknown(ctx) end -- Limit #prefixes.

703. end,

705. -- Chain to special handler specified by name.

706. ‌  ["*"] = function(ctx, name, pat)

707. return map_act[name](ctx, name, sub(pat, 2))

708. end,

710. -- Use named subtable for opcode group.

711. ‌  ["!"] = function(ctx, name, pat)

712. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end

713. return dispatch(ctx, map_opcgroup[name][((mrm-(mrm%8))/8)%8+1], sub(pat, 2))

714. end,

716. -- o16,o32[,o64] variants.

717. ‌  sz = function(ctx, name, pat)

718. if ctx.o16 then ctx.o16 = false

719. else

720. pat = match(pat, ",(.*)")

721. if ctx.rexw then

722. local p = match(pat, ",(.*)")

723. if p then pat = p; ctx.rexw = false end

724. end

725. end

726. pat = match(pat, "^[^,]*")

727. return dispatch(ctx, pat)

728. end,

730. -- Two-byte opcode dispatch.

731. ‌  opc2 = function(ctx, name, pat)

732. return dispatchmap(ctx, map_opc2)

733. end,

735. -- Three-byte opcode dispatch.

736. ‌  opc3 = function(ctx, name, pat)

737. return dispatchmap(ctx, map_opc3[pat])

738. end,

740. -- VMX/SVM dispatch.

741. ‌  vm = function(ctx, name, pat)

742. return dispatch(ctx, map_opcvm[ctx.mrm])

743. end,

745. -- Floating point opcode dispatch.

746. ‌  fp = function(ctx, name, pat)

747. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end

748. local rm = mrm%8

749. local idx = pat*8 + ((mrm-rm)/8)%8

750. if mrm >= 192 then idx = idx + 64 end

751. local opat = map_opcfp[idx]

752. if type(opat) == "table" then opat = opat[rm+1] end

753. return dispatch(ctx, opat)

754. end,

756. -- REX prefix.

757. ‌  rex = function(ctx, name, pat)

758. if ctx.rex then return unknown(ctx) end -- Only 1 REX prefix allowed.

759. for p in gmatch(pat, ".") do ctx["rex"..p] = true end

760. ctx.rex = true

761. end,

763. -- Special case for nop with REX prefix.

764. ‌  nop = function(ctx, name, pat)

765. return dispatch(ctx, ctx.rex and pat or "nop")

766. end,

767. }

769. ------------------------------------------------------------------------------

771. -- Disassemble a block of code.

772. ‌local function disass_block(ctx, ofs, len)

773. if not ofs then ofs = 0 end

774. local stop = len and ofs+len or #ctx.code

775. ofs = ofs + 1

776. ctx.start = ofs

777. ctx.pos = ofs

778. ctx.stop = stop

779. ctx.imm = nil

780. ctx.mrm = false

781. clearprefixes(ctx)

782. while ctx.pos <= stop do dispatchmap(ctx, ctx.map1) end

783. if ctx.pos ~= ctx.start then incomplete(ctx) end

784. end

786. -- Extended API: create a disassembler context. Then call ctx:disass(ofs, len).

787. ‌local function create(code, addr, out)

788. local ctx = {}

789. ctx.code = code

790. ctx.addr = (addr or 0) - 1

791. ctx.out = out or io.write

792. ctx.symtab = {}

793. ctx.disass = disass_block

794. ctx.hexdump = 16

795. ctx.x64 = false

796. ctx.map1 = map_opc1_32

797. ctx.aregs = map_regs.D

798. return ctx

799. end

801. ‌local function create64(code, addr, out)

802. local ctx = create(code, addr, out)

803. ctx.x64 = true

804. ctx.map1 = map_opc1_64

805. ctx.aregs = map_regs.Q

806. return ctx

807. end

809. -- Simple API: disassemble code (a string) at address and output via out.

810. ‌local function disass(code, addr, out)

811. create(code, addr, out):disass()

812. end

814. ‌local function disass64(code, addr, out)

815. create64(code, addr, out):disass()

816. end

818. -- Return register name for RID.

819. ‌local function regname(r)

820. if r < 8 then return map_regs.D[r+1] end

821. return map_regs.X[r-7]

822. end

824. ‌local function regname64(r)

825. if r < 16 then return map_regs.Q[r+1] end

826. return map_regs.X[r-15]

827. end

829. -- Public module functions.

830. return {

831. create = create,

832. create64 = create64,

833. disass = disass,

834. disass64 = disass64,

835. regname = regname,

836. regname64 = regname64

837. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/dis_mips.lua - luajit-2.0-src

Functions defined

- create

- create_el

- disass

- disass_block

- disass_el

- disass_ins

- get_be

- get_le

- putop

- regname

- unknown

Source code

1. ----------------------------------------------------------------------------

2. -- LuaJIT MIPS disassembler module.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT/X license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. -- This is a helper module used by the LuaJIT machine code dumper module.

8. --

9. -- It disassembles all standard MIPS32R1/R2 instructions.

10. -- Default mode is big-endian, but see: dis_mipsel.lua

11. ------------------------------------------------------------------------------

13. local type = type

14. local sub, byte, format = string.sub, string.byte, string.format

15. local match, gmatch, gsub = string.match, string.gmatch, string.gsub

16. local concat = table.concat

17. local bit = require("bit")

18. local band, bor, tohex = bit.band, bit.bor, bit.tohex

19. local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift

21. ------------------------------------------------------------------------------

22. -- Primary and extended opcode maps

23. ------------------------------------------------------------------------------

25. local map_movci = { shift = 16, mask = 1, [0] = "movfDSC", "movtDSC", }

26. local map_srl = { shift = 21, mask = 1, [0] = "srlDTA", "rotrDTA", }

27. local map_srlv = { shift = 6, mask = 1, [0] = "srlvDTS", "rotrvDTS", }

29. local map_special = {

30. shift = 0, mask = 63,

31. [0] = { shift = 0, mask = -1, [0] = "nop", _ = "sllDTA" },

32. map_movci,        map_srl,        "sraDTA",

33. "sllvDTS",        false,                map_srlv,        "sravDTS",

34. "jrS",        "jalrD1S",        "movzDST",        "movnDST",

35. "syscallY",        "breakY",        false,                "sync",

36. "mfhiD",        "mthiS",        "mfloD",        "mtloS",

37. false,        false,                false,                false,

38. "multST",        "multuST",        "divST",        "divuST",

39. false,        false,                false,                false,

40. "addDST",        "addu|moveDST0", "subDST",        "subu|neguDS0T",

41. "andDST",        "orDST",        "xorDST",        "nor|notDST0",

42. false,        false,                "sltDST",        "sltuDST",

43. false,        false,                false,                false,

44. "tgeSTZ",        "tgeuSTZ",        "tltSTZ",        "tltuSTZ",

45. "teqSTZ",        false,                "tneSTZ",

46. }

48. local map_special2 = {

49. shift = 0, mask = 63,

50. [0] = "maddST", "madduST",        "mulDST",        false,

51. "msubST",        "msubuST",

52. [32] = "clzDS", [33] = "cloDS",

53. [63] = "sdbbpY",

54. }

56. local map_bshfl = {

57. shift = 6, mask = 31,

58. [2] = "wsbhDT",

59. [16] = "sebDT",

60. [24] = "sehDT",

61. }

63. local map_special3 = {

64. shift = 0, mask = 63,

65. [0] = "extTSAK", [4] = "insTSAL",

66. [32] = map_bshfl,

67. [59] = "rdhwrTD",

68. }

70. local map_regimm = {

71. shift = 16, mask = 31,

72. [0] = "bltzSB",        "bgezSB",        "bltzlSB",        "bgezlSB",

73. false,        false,                false,                false,

74. "tgeiSI",        "tgeiuSI",        "tltiSI",        "tltiuSI",

75. "teqiSI",        false,                "tneiSI",        false,

76. "bltzalSB",        "bgezalSB",        "bltzallSB",        "bgezallSB",

77. false,        false,                false,                false,

78. false,        false,                false,                false,

79. false,        false,                false,                "synciSO",

80. }

82. local map_cop0 = {

83. shift = 25, mask = 1,

84. [0] = {

85. shift = 21, mask = 15,

86. [0] = "mfc0TDW", [4] = "mtc0TDW",

87. [10] = "rdpgprDT",

88. [11] = { shift = 5, mask = 1, [0] = "diT0", "eiT0", },

89. [14] = "wrpgprDT",

90. }, {

91. shift = 0, mask = 63,

92. [1] = "tlbr", [2] = "tlbwi", [6] = "tlbwr", [8] = "tlbp",

93. [24] = "eret", [31] = "deret",

94. [32] = "wait",

95. },

96. }

98. local map_cop1s = {

99. shift = 0, mask = 63,

100. [0] = "add.sFGH",        "sub.sFGH",        "mul.sFGH",        "div.sFGH",

101. "sqrt.sFG",                "abs.sFG",        "mov.sFG",        "neg.sFG",

102. "round.l.sFG",        "trunc.l.sFG",        "ceil.l.sFG",        "floor.l.sFG",

103. "round.w.sFG",        "trunc.w.sFG",        "ceil.w.sFG",        "floor.w.sFG",

104. false,

105. { shift = 16, mask = 1, [0] = "movf.sFGC", "movt.sFGC" },

106. "movz.sFGT",        "movn.sFGT",

107. false,        "recip.sFG",        "rsqrt.sFG",        false,

108. false,        false,                false,                false,

109. false,        false,                false,                false,

110. false,        "cvt.d.sFG",        false,                false,

111. "cvt.w.sFG",        "cvt.l.sFG",        "cvt.ps.sFGH",        false,

112. false,        false,                false,                false,

113. false,        false,                false,                false,

114. "c.f.sVGH",        "c.un.sVGH",        "c.eq.sVGH",        "c.ueq.sVGH",

115. "c.olt.sVGH",        "c.ult.sVGH",        "c.ole.sVGH",        "c.ule.sVGH",

116. "c.sf.sVGH",        "c.ngle.sVGH",        "c.seq.sVGH",        "c.ngl.sVGH",

117. "c.lt.sVGH",        "c.nge.sVGH",        "c.le.sVGH",        "c.ngt.sVGH",

118. }

120. local map_cop1d = {

121. shift = 0, mask = 63,

122. [0] = "add.dFGH",        "sub.dFGH",        "mul.dFGH",        "div.dFGH",

123. "sqrt.dFG",                "abs.dFG",        "mov.dFG",        "neg.dFG",

124. "round.l.dFG",        "trunc.l.dFG",        "ceil.l.dFG",        "floor.l.dFG",

125. "round.w.dFG",        "trunc.w.dFG",        "ceil.w.dFG",        "floor.w.dFG",

126. false,

127. { shift = 16, mask = 1, [0] = "movf.dFGC", "movt.dFGC" },

128. "movz.dFGT",        "movn.dFGT",

129. false,        "recip.dFG",        "rsqrt.dFG",        false,

130. false,        false,                false,                false,

131. false,        false,                false,                false,

132. "cvt.s.dFG",        false,                false,                false,

133. "cvt.w.dFG",        "cvt.l.dFG",        false,                false,

134. false,        false,                false,                false,

135. false,        false,                false,                false,

136. "c.f.dVGH",        "c.un.dVGH",        "c.eq.dVGH",        "c.ueq.dVGH",

137. "c.olt.dVGH",        "c.ult.dVGH",        "c.ole.dVGH",        "c.ule.dVGH",

138. "c.df.dVGH",        "c.ngle.dVGH",        "c.deq.dVGH",        "c.ngl.dVGH",

139. "c.lt.dVGH",        "c.nge.dVGH",        "c.le.dVGH",        "c.ngt.dVGH",

140. }

142. local map_cop1ps = {

143. shift = 0, mask = 63,

144. [0] = "add.psFGH",        "sub.psFGH",        "mul.psFGH",        false,

145. false,                "abs.psFG",        "mov.psFG",        "neg.psFG",

146. false,                false,                false,                false,

147. false,                false,                false,                false,

148. false,

149. { shift = 16, mask = 1, [0] = "movf.psFGC", "movt.psFGC" },

150. "movz.psFGT",        "movn.psFGT",

151. false,        false,                false,                false,

152. false,        false,                false,                false,

153. false,        false,                false,                false,

154. "cvt.s.puFG",        false,                false,                false,

155. false,        false,                false,                false,

156. "cvt.s.plFG",        false,                false,                false,

157. "pll.psFGH",        "plu.psFGH",        "pul.psFGH",        "puu.psFGH",

158. "c.f.psVGH",        "c.un.psVGH",        "c.eq.psVGH",        "c.ueq.psVGH",

159. "c.olt.psVGH", "c.ult.psVGH",        "c.ole.psVGH",        "c.ule.psVGH",

160. "c.psf.psVGH", "c.ngle.psVGH", "c.pseq.psVGH", "c.ngl.psVGH",

161. "c.lt.psVGH",        "c.nge.psVGH",        "c.le.psVGH",        "c.ngt.psVGH",

162. }

164. local map_cop1w = {

165. shift = 0, mask = 63,

166. [32] = "cvt.s.wFG", [33] = "cvt.d.wFG",

167. }

169. local map_cop1l = {

170. shift = 0, mask = 63,

171. [32] = "cvt.s.lFG", [33] = "cvt.d.lFG",

172. }

174. local map_cop1bc = {

175. shift = 16, mask = 3,

176. [0] = "bc1fCB", "bc1tCB",        "bc1flCB",        "bc1tlCB",

177. }

179. local map_cop1 = {

180. shift = 21, mask = 31,

181. [0] = "mfc1TG", false,        "cfc1TG",        "mfhc1TG",

182. "mtc1TG",        false,                "ctc1TG",        "mthc1TG",

183. map_cop1bc,        false,                false,                false,

184. false,        false,                false,                false,

185. map_cop1s,        map_cop1d,        false,                false,

186. map_cop1w,        map_cop1l,        map_cop1ps,

187. }

189. local map_cop1x = {

190. shift = 0, mask = 63,

191. [0] = "lwxc1FSX",        "ldxc1FSX",        false,                false,

192. false,        "luxc1FSX",        false,                false,

193. "swxc1FSX",        "sdxc1FSX",        false,                false,

194. false,        "suxc1FSX",        false,                "prefxMSX",

195. false,        false,                false,                false,

196. false,        false,                false,                false,

197. false,        false,                false,                false,

198. false,        false,                "alnv.psFGHS",        false,

199. "madd.sFRGH",        "madd.dFRGH",        false,                false,

200. false,        false,                "madd.psFRGH",        false,

201. "msub.sFRGH",        "msub.dFRGH",        false,                false,

202. false,        false,                "msub.psFRGH",        false,

203. "nmadd.sFRGH", "nmadd.dFRGH",        false,                false,

204. false,        false,                "nmadd.psFRGH",        false,

205. "nmsub.sFRGH", "nmsub.dFRGH",        false,                false,

206. false,        false,                "nmsub.psFRGH",        false,

207. }

209. local map_pri = {

210. [0] = map_special,        map_regimm,        "jJ",        "jalJ",

211. "beq|beqz|bST00B",        "bne|bnezST0B",                "blezSB",        "bgtzSB",

212. "addiTSI",        "addiu|liTS0I",        "sltiTSI",        "sltiuTSI",

213. "andiTSU",        "ori|liTS0U",        "xoriTSU",        "luiTU",

214. map_cop0,        map_cop1,        false,                map_cop1x,

215. "beql|beqzlST0B",        "bnel|bnezlST0B",        "blezlSB",        "bgtzlSB",

216. false,        false,                false,                false,

217. map_special2,        false,                false,                map_special3,

218. "lbTSO",        "lhTSO",        "lwlTSO",        "lwTSO",

219. "lbuTSO",        "lhuTSO",        "lwrTSO",        false,

220. "sbTSO",        "shTSO",        "swlTSO",        "swTSO",

221. false,        false,                "swrTSO",        "cacheNSO",

222. "llTSO",        "lwc1HSO",        "lwc2TSO",        "prefNSO",

223. false,        "ldc1HSO",        "ldc2TSO",        false,

224. "scTSO",        "swc1HSO",        "swc2TSO",        false,

225. false,        "sdc1HSO",        "sdc2TSO",        false,

226. }

228. ------------------------------------------------------------------------------

230. local map_gpr = {

231. [0] = "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",

232. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",

233. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",

234. "r24", "r25", "r26", "r27", "r28", "sp", "r30", "ra",

235. }

237. ------------------------------------------------------------------------------

239. -- Output a nicely formatted line with an opcode and operands.

240. ‌local function putop(ctx, text, operands)

241. local pos = ctx.pos

242. local extra = ""

243. if ctx.rel then

244. local sym = ctx.symtab[ctx.rel]

245. if sym then extra = "\t->"..sym end

246. end

247. if ctx.hexdump > 0 then

248. ctx.out(format("%08x  %s  %-7s %s%s\n",

249. ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra))

250. else

251. ctx.out(format("%08x  %-7s %s%s\n",

252. ctx.addr+pos, text, concat(operands, ", "), extra))

253. end

254. ctx.pos = pos + 4

255. end

257. -- Fallback for unknown opcodes.

258. ‌local function unknown(ctx)

259. return putop(ctx, ".long", { "0x"..tohex(ctx.op) })

260. end

262. ‌local function get_be(ctx)

263. local pos = ctx.pos

264. local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)

265. return bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3)

266. end

268. ‌local function get_le(ctx)

269. local pos = ctx.pos

270. local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)

271. return bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0)

272. end

274. -- Disassemble a single instruction.

275. ‌local function disass_ins(ctx)

276. local op = ctx:get()

277. local operands = {}

278. local last = nil

279. ctx.op = op

280. ctx.rel = nil

282. local opat = map_pri[rshift(op, 26)]

283. while type(opat) ~= "string" do

284. if not opat then return unknown(ctx) end

285. opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._

286. end

287. local name, pat = match(opat, "^([a-z0-9_.]*)(.*)")

288. local altname, pat2 = match(pat, "|([a-z0-9_.|]*)(.*)")

289. if altname then pat = pat2 end

291. for p in gmatch(pat, ".") do

292. local x = nil

293. if p == "S" then

294. x = map_gpr[band(rshift(op, 21), 31)]

295. elseif p == "T" then

296. x = map_gpr[band(rshift(op, 16), 31)]

297. elseif p == "D" then

298. x = map_gpr[band(rshift(op, 11), 31)]

299. elseif p == "F" then

300. x = "f"..band(rshift(op, 6), 31)

301. elseif p == "G" then

302. x = "f"..band(rshift(op, 11), 31)

303. elseif p == "H" then

304. x = "f"..band(rshift(op, 16), 31)

305. elseif p == "R" then

306. x = "f"..band(rshift(op, 21), 31)

307. elseif p == "A" then

308. x = band(rshift(op, 6), 31)

309. elseif p == "M" then

310. x = band(rshift(op, 11), 31)

311. elseif p == "N" then

312. x = band(rshift(op, 16), 31)

313. elseif p == "C" then

314. x = band(rshift(op, 18), 7)

315. if x == 0 then x = nil end

316. elseif p == "K" then

317. x = band(rshift(op, 11), 31) + 1

318. elseif p == "L" then

319. x = band(rshift(op, 11), 31) - last + 1

320. elseif p == "I" then

321. x = arshift(lshift(op, 16), 16)

322. elseif p == "U" then

323. x = band(op, 0xffff)

324. elseif p == "O" then

325. local disp = arshift(lshift(op, 16), 16)

326. operands[#operands] = format("%d(%s)", disp, last)

327. elseif p == "X" then

328. local index = map_gpr[band(rshift(op, 16), 31)]

329. operands[#operands] = format("%s(%s)", index, last)

330. elseif p == "B" then

331. x = ctx.addr + ctx.pos + arshift(lshift(op, 16), 16)*4 + 4

332. ctx.rel = x

333. x = "0x"..tohex(x)

334. elseif p == "J" then

335. x = band(ctx.addr + ctx.pos, 0xf0000000) + band(op, 0x03ffffff)*4

336. ctx.rel = x

337. x = "0x"..tohex(x)

338. elseif p == "V" then

339. x = band(rshift(op, 8), 7)

340. if x == 0 then x = nil end

341. elseif p == "W" then

342. x = band(op, 7)

343. if x == 0 then x = nil end

344. elseif p == "Y" then

345. x = band(rshift(op, 6), 0x000fffff)

346. if x == 0 then x = nil end

347. elseif p == "Z" then

348. x = band(rshift(op, 6), 1023)

349. if x == 0 then x = nil end

350. elseif p == "0" then

351. if last == "r0" or last == 0 then

352. local n = #operands

353. operands[n] = nil

354. last = operands[n-1]

355. if altname then

356. local a1, a2 = match(altname, "([^|]*)|(.*)")

357. if a1 then name, altname = a1, a2

358. else name = altname end

359. end

360. end

361. elseif p == "1" then

362. if last == "ra" then

363. operands[#operands] = nil

364. end

365. else

366. assert(false)

367. end

368. if x then operands[#operands+1] = x; last = x end

369. end

371. return putop(ctx, name, operands)

372. end

374. ------------------------------------------------------------------------------

376. -- Disassemble a block of code.

377. ‌local function disass_block(ctx, ofs, len)

378. if not ofs then ofs = 0 end

379. local stop = len and ofs+len or #ctx.code

380. stop = stop - stop % 4

381. ctx.pos = ofs - ofs % 4

382. ctx.rel = nil

383. while ctx.pos < stop do disass_ins(ctx) end

384. end

386. -- Extended API: create a disassembler context. Then call ctx:disass(ofs, len).

387. ‌local function create(code, addr, out)

388. local ctx = {}

389. ctx.code = code

390. ctx.addr = addr or 0

391. ctx.out = out or io.write

392. ctx.symtab = {}

393. ctx.disass = disass_block

394. ctx.hexdump = 8

395. ctx.get = get_be

396. return ctx

397. end

399. ‌local function create_el(code, addr, out)

400. local ctx = create(code, addr, out)

401. ctx.get = get_le

402. return ctx

403. end

405. -- Simple API: disassemble code (a string) at address and output via out.

406. ‌local function disass(code, addr, out)

407. create(code, addr, out):disass()

408. end

410. ‌local function disass_el(code, addr, out)

411. create_el(code, addr, out):disass()

412. end

414. -- Return register name for RID.

415. ‌local function regname(r)

416. if r < 32 then return map_gpr[r] end

417. return "f"..(r-32)

418. end

420. -- Public module functions.

421. return {

422. create = create,

423. create_el = create_el,

424. disass = disass,

425. disass_el = disass_el,

426. regname = regname

427. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/dis_arm.lua - luajit-2.0-src

Functions defined

- create

- disass

- disass_block

- disass_ins

- fmtload

- fmtvload

- fmtvr

- putop

- regname

- unknown

Source code

1. ----------------------------------------------------------------------------

2. -- LuaJIT ARM disassembler module.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. -- This is a helper module used by the LuaJIT machine code dumper module.

8. --

9. -- It disassembles most user-mode ARMv7 instructions

10. -- NYI: Advanced SIMD and VFP instructions.

11. ------------------------------------------------------------------------------

13. local type = type

14. local sub, byte, format = string.sub, string.byte, string.format

15. local match, gmatch, gsub = string.match, string.gmatch, string.gsub

16. local concat = table.concat

17. local bit = require("bit")

18. local band, bor, ror, tohex = bit.band, bit.bor, bit.ror, bit.tohex

19. local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift

21. ------------------------------------------------------------------------------

22. -- Opcode maps

23. ------------------------------------------------------------------------------

25. local map_loadc = {

26. shift = 8, mask = 15,

27. [10] = {

28. shift = 20, mask = 1,

29. [0] = {

30. shift = 23, mask = 3,

31. [0] = "vmovFmDN", "vstmFNdr",

32. _ = {

33. shift = 21, mask = 1,

34. [0] = "vstrFdl",

35. { shift = 16, mask = 15, [13] = "vpushFdr", _ = "vstmdbFNdr", }

36. },

37. },

38. {

39. shift = 23, mask = 3,

40. [0] = "vmovFDNm",

41. { shift = 16, mask = 15, [13] = "vpopFdr", _ = "vldmFNdr", },

42. _ = {

43. shift = 21, mask = 1,

44. [0] = "vldrFdl", "vldmdbFNdr",

45. },

46. },

47. },

48. [11] = {

49. shift = 20, mask = 1,

50. [0] = {

51. shift = 23, mask = 3,

52. [0] = "vmovGmDN", "vstmGNdr",

53. _ = {

54. shift = 21, mask = 1,

55. [0] = "vstrGdl",

56. { shift = 16, mask = 15, [13] = "vpushGdr", _ = "vstmdbGNdr", }

57. },

58. },

59. {

60. shift = 23, mask = 3,

61. [0] = "vmovGDNm",

62. { shift = 16, mask = 15, [13] = "vpopGdr", _ = "vldmGNdr", },

63. _ = {

64. shift = 21, mask = 1,

65. [0] = "vldrGdl", "vldmdbGNdr",

66. },

67. },

68. },

69. _ = {

70. shift = 0, mask = 0 -- NYI ldc, mcrr, mrrc.

71. },

72. }

74. local map_vfps = {

75. shift = 6, mask = 0x2c001,

76. [0] = "vmlaF.dnm", "vmlsF.dnm",

77. [0x04000] = "vnmlsF.dnm", [0x04001] = "vnmlaF.dnm",

78. [0x08000] = "vmulF.dnm", [0x08001] = "vnmulF.dnm",

79. [0x0c000] = "vaddF.dnm", [0x0c001] = "vsubF.dnm",

80. [0x20000] = "vdivF.dnm",

81. [0x24000] = "vfnmsF.dnm", [0x24001] = "vfnmaF.dnm",

82. [0x28000] = "vfmaF.dnm", [0x28001] = "vfmsF.dnm",

83. [0x2c000] = "vmovF.dY",

84. [0x2c001] = {

85. shift = 7, mask = 0x1e01,

86. [0] = "vmovF.dm", "vabsF.dm",

87. [0x0200] = "vnegF.dm", [0x0201] = "vsqrtF.dm",

88. [0x0800] = "vcmpF.dm", [0x0801] = "vcmpeF.dm",

89. [0x0a00] = "vcmpzF.d", [0x0a01] = "vcmpzeF.d",

90. [0x0e01] = "vcvtG.dF.m",

91. [0x1000] = "vcvt.f32.u32Fdm", [0x1001] = "vcvt.f32.s32Fdm",

92. [0x1800] = "vcvtr.u32F.dm", [0x1801] = "vcvt.u32F.dm",

93. [0x1a00] = "vcvtr.s32F.dm", [0x1a01] = "vcvt.s32F.dm",

94. },

95. }

97. local map_vfpd = {

98. shift = 6, mask = 0x2c001,

99. [0] = "vmlaG.dnm", "vmlsG.dnm",

100. [0x04000] = "vnmlsG.dnm", [0x04001] = "vnmlaG.dnm",

101. [0x08000] = "vmulG.dnm", [0x08001] = "vnmulG.dnm",

102. [0x0c000] = "vaddG.dnm", [0x0c001] = "vsubG.dnm",

103. [0x20000] = "vdivG.dnm",

104. [0x24000] = "vfnmsG.dnm", [0x24001] = "vfnmaG.dnm",

105. [0x28000] = "vfmaG.dnm", [0x28001] = "vfmsG.dnm",

106. [0x2c000] = "vmovG.dY",

107. [0x2c001] = {

108. shift = 7, mask = 0x1e01,

109. [0] = "vmovG.dm", "vabsG.dm",

110. [0x0200] = "vnegG.dm", [0x0201] = "vsqrtG.dm",

111. [0x0800] = "vcmpG.dm", [0x0801] = "vcmpeG.dm",

112. [0x0a00] = "vcmpzG.d", [0x0a01] = "vcmpzeG.d",

113. [0x0e01] = "vcvtF.dG.m",

114. [0x1000] = "vcvt.f64.u32GdFm", [0x1001] = "vcvt.f64.s32GdFm",

115. [0x1800] = "vcvtr.u32FdG.m", [0x1801] = "vcvt.u32FdG.m",

116. [0x1a00] = "vcvtr.s32FdG.m", [0x1a01] = "vcvt.s32FdG.m",

117. },

118. }

120. local map_datac = {

121. shift = 24, mask = 1,

122. [0] = {

123. shift = 4, mask = 1,

124. [0] = {

125. shift = 8, mask = 15,

126. [10] = map_vfps,

127. [11] = map_vfpd,

128. -- NYI cdp, mcr, mrc.

129. },

130. {

131. shift = 8, mask = 15,

132. [10] = {

133. shift = 20, mask = 15,

134. [0] = "vmovFnD", "vmovFDn",

135. [14] = "vmsrD",

136. [15] = { shift = 12, mask = 15, [15] = "vmrs", _ = "vmrsD", },

137. },

138. },

139. },

140. "svcT",

141. }

143. local map_loadcu = {

144. shift = 0, mask = 0, -- NYI unconditional CP load/store.

145. }

147. local map_datacu = {

148. shift = 0, mask = 0, -- NYI unconditional CP data.

149. }

151. local map_simddata = {

152. shift = 0, mask = 0, -- NYI SIMD data.

153. }

155. local map_simdload = {

156. shift = 0, mask = 0, -- NYI SIMD load/store, preload.

157. }

159. local map_preload = {

160. shift = 0, mask = 0, -- NYI preload.

161. }

163. local map_media = {

164. shift = 20, mask = 31,

165. [0] = false,

166. { --01

167. shift = 5, mask = 7,

168. [0] = "sadd16DNM", "sasxDNM", "ssaxDNM", "ssub16DNM",

169. "sadd8DNM", false, false, "ssub8DNM",

170. },

171. { --02

172. shift = 5, mask = 7,

173. [0] = "qadd16DNM", "qasxDNM", "qsaxDNM", "qsub16DNM",

174. "qadd8DNM", false, false, "qsub8DNM",

175. },

176. { --03

177. shift = 5, mask = 7,

178. [0] = "shadd16DNM", "shasxDNM", "shsaxDNM", "shsub16DNM",

179. "shadd8DNM", false, false, "shsub8DNM",

180. },

181. false,

182. { --05

183. shift = 5, mask = 7,

184. [0] = "uadd16DNM", "uasxDNM", "usaxDNM", "usub16DNM",

185. "uadd8DNM", false, false, "usub8DNM",

186. },

187. { --06

188. shift = 5, mask = 7,

189. [0] = "uqadd16DNM", "uqasxDNM", "uqsaxDNM", "uqsub16DNM",

190. "uqadd8DNM", false, false, "uqsub8DNM",

191. },

192. { --07

193. shift = 5, mask = 7,

194. [0] = "uhadd16DNM", "uhasxDNM", "uhsaxDNM", "uhsub16DNM",

195. "uhadd8DNM", false, false, "uhsub8DNM",

196. },

197. { --08

198. shift = 5, mask = 7,

199. [0] = "pkhbtDNMU", false, "pkhtbDNMU",

200. { shift = 16, mask = 15, [15] = "sxtb16DMU", _ = "sxtab16DNMU", },

201. "pkhbtDNMU", "selDNM", "pkhtbDNMU",

202. },

203. false,

204. { --0a

205. shift = 5, mask = 7,

206. [0] = "ssatDxMu", "ssat16DxM", "ssatDxMu",

207. { shift = 16, mask = 15, [15] = "sxtbDMU", _ = "sxtabDNMU", },

208. "ssatDxMu", false, "ssatDxMu",

209. },

210. { --0b

211. shift = 5, mask = 7,

212. [0] = "ssatDxMu", "revDM", "ssatDxMu",

213. { shift = 16, mask = 15, [15] = "sxthDMU", _ = "sxtahDNMU", },

214. "ssatDxMu", "rev16DM", "ssatDxMu",

215. },

216. { --0c

217. shift = 5, mask = 7,

218. [3] = { shift = 16, mask = 15, [15] = "uxtb16DMU", _ = "uxtab16DNMU", },

219. },

220. false,

221. { --0e

222. shift = 5, mask = 7,

223. [0] = "usatDwMu", "usat16DwM", "usatDwMu",

224. { shift = 16, mask = 15, [15] = "uxtbDMU", _ = "uxtabDNMU", },

225. "usatDwMu", false, "usatDwMu",

226. },

227. { --0f

228. shift = 5, mask = 7,

229. [0] = "usatDwMu", "rbitDM", "usatDwMu",

230. { shift = 16, mask = 15, [15] = "uxthDMU", _ = "uxtahDNMU", },

231. "usatDwMu", "revshDM", "usatDwMu",

232. },

233. { --10

234. shift = 12, mask = 15,

235. [15] = {

236. shift = 5, mask = 7,

237. "smuadNMS", "smuadxNMS", "smusdNMS", "smusdxNMS",

238. },

239. _ = {

240. shift = 5, mask = 7,

241. [0] = "smladNMSD", "smladxNMSD", "smlsdNMSD", "smlsdxNMSD",

242. },

243. },

244. false, false, false,

245. { --14

246. shift = 5, mask = 7,

247. [0] = "smlaldDNMS", "smlaldxDNMS", "smlsldDNMS", "smlsldxDNMS",

248. },

249. { --15

250. shift = 5, mask = 7,

251. [0] = { shift = 12, mask = 15, [15] = "smmulNMS", _ = "smmlaNMSD", },

252. { shift = 12, mask = 15, [15] = "smmulrNMS", _ = "smmlarNMSD", },

253. false, false, false, false,

254. "smmlsNMSD", "smmlsrNMSD",

255. },

256. false, false,

257. { --18

258. shift = 5, mask = 7,

259. [0] = { shift = 12, mask = 15, [15] = "usad8NMS", _ = "usada8NMSD", },

260. },

261. false,

262. { --1a

263. shift = 5, mask = 3, [2] = "sbfxDMvw",

264. },

265. { --1b

266. shift = 5, mask = 3, [2] = "sbfxDMvw",

267. },

268. { --1c

269. shift = 5, mask = 3,

270. [0] = { shift = 0, mask = 15, [15] = "bfcDvX", _ = "bfiDMvX", },

271. },

272. { --1d

273. shift = 5, mask = 3,

274. [0] = { shift = 0, mask = 15, [15] = "bfcDvX", _ = "bfiDMvX", },

275. },

276. { --1e

277. shift = 5, mask = 3, [2] = "ubfxDMvw",

278. },

279. { --1f

280. shift = 5, mask = 3, [2] = "ubfxDMvw",

281. },

282. }

284. local map_load = {

285. shift = 21, mask = 9,

286. {

287. shift = 20, mask = 5,

288. [0] = "strtDL", "ldrtDL", [4] = "strbtDL", [5] = "ldrbtDL",

289. },

290. _ = {

291. shift = 20, mask = 5,

292. [0] = "strDL", "ldrDL", [4] = "strbDL", [5] = "ldrbDL",

293. }

294. }

296. local map_load1 = {

297. shift = 4, mask = 1,

298. [0] = map_load, map_media,

299. }

301. local map_loadm = {

302. shift = 20, mask = 1,

303. [0] = {

304. shift = 23, mask = 3,

305. [0] = "stmdaNR", "stmNR",

306. { shift = 16, mask = 63, [45] = "pushR", _ = "stmdbNR", }, "stmibNR",

307. },

308. {

309. shift = 23, mask = 3,

310. [0] = "ldmdaNR", { shift = 16, mask = 63, [61] = "popR", _ = "ldmNR", },

311. "ldmdbNR", "ldmibNR",

312. },

313. }

315. local map_data = {

316. shift = 21, mask = 15,

317. [0] = "andDNPs", "eorDNPs", "subDNPs", "rsbDNPs",

318. "addDNPs", "adcDNPs", "sbcDNPs", "rscDNPs",

319. "tstNP", "teqNP", "cmpNP", "cmnNP",

320. "orrDNPs", "movDPs", "bicDNPs", "mvnDPs",

321. }

323. local map_mul = {

324. shift = 21, mask = 7,

325. [0] = "mulNMSs", "mlaNMSDs", "umaalDNMS", "mlsDNMS",

326. "umullDNMSs", "umlalDNMSs", "smullDNMSs", "smlalDNMSs",

327. }

329. local map_sync = {

330. shift = 20, mask = 15, -- NYI: brackets around N. R(D+1) for ldrexd/strexd.

331. [0] = "swpDMN", false, false, false,

332. "swpbDMN", false, false, false,

333. "strexDMN", "ldrexDN", "strexdDN", "ldrexdDN",

334. "strexbDMN", "ldrexbDN", "strexhDN", "ldrexhDN",

335. }

337. local map_mulh = {

338. shift = 21, mask = 3,

339. [0] = { shift = 5, mask = 3,

340. [0] = "smlabbNMSD", "smlatbNMSD", "smlabtNMSD", "smlattNMSD", },

341. { shift = 5, mask = 3,

342. [0] = "smlawbNMSD", "smulwbNMS", "smlawtNMSD", "smulwtNMS", },

343. { shift = 5, mask = 3,

344. [0] = "smlalbbDNMS", "smlaltbDNMS", "smlalbtDNMS", "smlalttDNMS", },

345. { shift = 5, mask = 3,

346. [0] = "smulbbNMS", "smultbNMS", "smulbtNMS", "smulttNMS", },

347. }

349. local map_misc = {

350. shift = 4, mask = 7,

351. -- NYI: decode PSR bits of msr.

352. [0] = { shift = 21, mask = 1, [0] = "mrsD", "msrM", },

353. { shift = 21, mask = 3, "bxM", false, "clzDM", },

354. { shift = 21, mask = 3, "bxjM", },

355. { shift = 21, mask = 3, "blxM", },

356. false,

357. { shift = 21, mask = 3, [0] = "qaddDMN", "qsubDMN", "qdaddDMN", "qdsubDMN", },

358. false,

359. { shift = 21, mask = 3, "bkptK", },

360. }

362. local map_datar = {

363. shift = 4, mask = 9,

364. [9] = {

365. shift = 5, mask = 3,

366. [0] = { shift = 24, mask = 1, [0] = map_mul, map_sync, },

367. { shift = 20, mask = 1, [0] = "strhDL", "ldrhDL", },

368. { shift = 20, mask = 1, [0] = "ldrdDL", "ldrsbDL", },

369. { shift = 20, mask = 1, [0] = "strdDL", "ldrshDL", },

370. },

371. _ = {

372. shift = 20, mask = 25,

373. [16] = { shift = 7, mask = 1, [0] = map_misc, map_mulh, },

374. _ = {

375. shift = 0, mask = 0xffffffff,

376. [bor(0xe1a00000)] = "nop",

377. _ = map_data,

378. }

379. },

380. }

382. local map_datai = {

383. shift = 20, mask = 31, -- NYI: decode PSR bits of msr. Decode imm12.

384. [16] = "movwDW", [20] = "movtDW",

385. [18] = { shift = 0, mask = 0xf00ff, [0] = "nopv6", _ = "msrNW", },

386. [22] = "msrNW",

387. _ = map_data,

388. }

390. local map_branch = {

391. shift = 24, mask = 1,

392. [0] = "bB", "blB"

393. }

395. local map_condins = {

396. [0] = map_datar, map_datai, map_load, map_load1,

397. map_loadm, map_branch, map_loadc, map_datac

398. }

400. -- NYI: setend.

401. local map_uncondins = {

402. [0] = false, map_simddata, map_simdload, map_preload,

403. false, "blxB", map_loadcu, map_datacu,

404. }

406. ------------------------------------------------------------------------------

408. local map_gpr = {

409. [0] = "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",

410. "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",

411. }

413. local map_cond = {

414. [0] = "eq", "ne", "hs", "lo", "mi", "pl", "vs", "vc",

415. "hi", "ls", "ge", "lt", "gt", "le", "al",

416. }

418. local map_shift = { [0] = "lsl", "lsr", "asr", "ror", }

420. ------------------------------------------------------------------------------

422. -- Output a nicely formatted line with an opcode and operands.

423. ‌local function putop(ctx, text, operands)

424. local pos = ctx.pos

425. local extra = ""

426. if ctx.rel then

427. local sym = ctx.symtab[ctx.rel]

428. if sym then

429. extra = "\t->"..sym

430. elseif band(ctx.op, 0x0e000000) ~= 0x0a000000 then

431. extra = "\t; 0x"..tohex(ctx.rel)

432. end

433. end

434. if ctx.hexdump > 0 then

435. ctx.out(format("%08x  %s  %-5s %s%s\n",

436. ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra))

437. else

438. ctx.out(format("%08x  %-5s %s%s\n",

439. ctx.addr+pos, text, concat(operands, ", "), extra))

440. end

441. ctx.pos = pos + 4

442. end

444. -- Fallback for unknown opcodes.

445. ‌local function unknown(ctx)

446. return putop(ctx, ".long", { "0x"..tohex(ctx.op) })

447. end

449. -- Format operand 2 of load/store opcodes.

450. ‌local function fmtload(ctx, op, pos)

451. local base = map_gpr[band(rshift(op, 16), 15)]

452. local x, ofs

453. local ext = (band(op, 0x04000000) == 0)

454. if not ext and band(op, 0x02000000) == 0 then

455. ofs = band(op, 4095)

456. if band(op, 0x00800000) == 0 then ofs = -ofs end

457. if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end

458. ofs = "#"..ofs

459. elseif ext and band(op, 0x00400000) ~= 0 then

460. ofs = band(op, 15) + band(rshift(op, 4), 0xf0)

461. if band(op, 0x00800000) == 0 then ofs = -ofs end

462. if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end

463. ofs = "#"..ofs

464. else

465. ofs = map_gpr[band(op, 15)]

466. if ext or band(op, 0xfe0) == 0 then

467. elseif band(op, 0xfe0) == 0x60 then

468. ofs = format("%s, rrx", ofs)

469. else

470. local sh = band(rshift(op, 7), 31)

471. if sh == 0 then sh = 32 end

472. ofs = format("%s, %s #%d", ofs, map_shift[band(rshift(op, 5), 3)], sh)

473. end

474. if band(op, 0x00800000) == 0 then ofs = "-"..ofs end

475. end

476. if ofs == "#0" then

477. x = format("[%s]", base)

478. elseif band(op, 0x01000000) == 0 then

479. x = format("[%s], %s", base, ofs)

480. else

481. x = format("[%s, %s]", base, ofs)

482. end

483. if band(op, 0x01200000) == 0x01200000 then x = x.."!" end

484. return x

485. end

487. -- Format operand 2 of vector load/store opcodes.

488. ‌local function fmtvload(ctx, op, pos)

489. local base = map_gpr[band(rshift(op, 16), 15)]

490. local ofs = band(op, 255)*4

491. if band(op, 0x00800000) == 0 then ofs = -ofs end

492. if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end

493. if ofs == 0 then

494. return format("[%s]", base)

495. else

496. return format("[%s, #%d]", base, ofs)

497. end

498. end

500. ‌local function fmtvr(op, vr, sh0, sh1)

501. if vr == "s" then

502. return format("s%d", 2*band(rshift(op, sh0), 15)+band(rshift(op, sh1), 1))

503. else

504. return format("d%d", band(rshift(op, sh0), 15)+band(rshift(op, sh1-4), 16))

505. end

506. end

508. -- Disassemble a single instruction.

509. ‌local function disass_ins(ctx)

510. local pos = ctx.pos

511. local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)

512. local op = bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0)

513. local operands = {}

514. local suffix = ""

515. local last, name, pat

516. local vr

517. ctx.op = op

518. ctx.rel = nil

520. local cond = rshift(op, 28)

521. local opat

522. if cond == 15 then

523. opat = map_uncondins[band(rshift(op, 25), 7)]

524. else

525. if cond ~= 14 then suffix = map_cond[cond] end

526. opat = map_condins[band(rshift(op, 25), 7)]

527. end

528. while type(opat) ~= "string" do

529. if not opat then return unknown(ctx) end

530. opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._

531. end

532. name, pat = match(opat, "^([a-z0-9]*)(.*)")

533. if sub(pat, 1, 1) == "." then

534. local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)")

535. suffix = suffix..s2

536. pat = p2

537. end

539. for p in gmatch(pat, ".") do

540. local x = nil

541. if p == "D" then

542. x = map_gpr[band(rshift(op, 12), 15)]

543. elseif p == "N" then

544. x = map_gpr[band(rshift(op, 16), 15)]

545. elseif p == "S" then

546. x = map_gpr[band(rshift(op, 8), 15)]

547. elseif p == "M" then

548. x = map_gpr[band(op, 15)]

549. elseif p == "d" then

550. x = fmtvr(op, vr, 12, 22)

551. elseif p == "n" then

552. x = fmtvr(op, vr, 16, 7)

553. elseif p == "m" then

554. x = fmtvr(op, vr, 0, 5)

555. elseif p == "P" then

556. if band(op, 0x02000000) ~= 0 then

557. x = ror(band(op, 255), 2*band(rshift(op, 8), 15))

558. else

559. x = map_gpr[band(op, 15)]

560. if band(op, 0xff0) ~= 0 then

561. operands[#operands+1] = x

562. local s = map_shift[band(rshift(op, 5), 3)]

563. local r = nil

564. if band(op, 0xf90) == 0 then

565. if s == "ror" then s = "rrx" else r = "#32" end

566. elseif band(op, 0x10) == 0 then

567. r = "#"..band(rshift(op, 7), 31)

568. else

569. r = map_gpr[band(rshift(op, 8), 15)]

570. end

571. if name == "mov" then name = s; x = r

572. elseif r then x = format("%s %s", s, r)

573. else x = s end

574. end

575. end

576. elseif p == "L" then

577. x = fmtload(ctx, op, pos)

578. elseif p == "l" then

579. x = fmtvload(ctx, op, pos)

580. elseif p == "B" then

581. local addr = ctx.addr + pos + 8 + arshift(lshift(op, 8), 6)

582. if cond == 15 then addr = addr + band(rshift(op, 23), 2) end

583. ctx.rel = addr

584. x = "0x"..tohex(addr)

585. elseif p == "F" then

586. vr = "s"

587. elseif p == "G" then

588. vr = "d"

589. elseif p == "." then

590. suffix = suffix..(vr == "s" and ".f32" or ".f64")

591. elseif p == "R" then

592. if band(op, 0x00200000) ~= 0 and #operands == 1 then

593. operands[1] = operands[1].."!"

594. end

595. local t = {}

596. for i=0,15 do

597. if band(rshift(op, i), 1) == 1 then t[#t+1] = map_gpr[i] end

598. end

599. x = "{"..concat(t, ", ").."}"

600. elseif p == "r" then

601. if band(op, 0x00200000) ~= 0 and #operands == 2 then

602. operands[1] = operands[1].."!"

603. end

604. local s = tonumber(sub(last, 2))

605. local n = band(op, 255)

606. if vr == "d" then n = rshift(n, 1) end

607. operands[#operands] = format("{%s-%s%d}", last, vr, s+n-1)

608. elseif p == "W" then

609. x = band(op, 0x0fff) + band(rshift(op, 4), 0xf000)

610. elseif p == "T" then

611. x = "#0x"..tohex(band(op, 0x00ffffff), 6)

612. elseif p == "U" then

613. x = band(rshift(op, 7), 31)

614. if x == 0 then x = nil end

615. elseif p == "u" then

616. x = band(rshift(op, 7), 31)

617. if band(op, 0x40) == 0 then

618. if x == 0 then x = nil else x = "lsl #"..x end

619. else

620. if x == 0 then x = "asr #32" else x = "asr #"..x end

621. end

622. elseif p == "v" then

623. x = band(rshift(op, 7), 31)

624. elseif p == "w" then

625. x = band(rshift(op, 16), 31)

626. elseif p == "x" then

627. x = band(rshift(op, 16), 31) + 1

628. elseif p == "X" then

629. x = band(rshift(op, 16), 31) - last + 1

630. elseif p == "Y" then

631. x = band(rshift(op, 12), 0xf0) + band(op, 0x0f)

632. elseif p == "K" then

633. x = "#0x"..tohex(band(rshift(op, 4), 0x0000fff0) + band(op, 15), 4)

634. elseif p == "s" then

635. if band(op, 0x00100000) ~= 0 then suffix = "s"..suffix end

636. else

637. assert(false)

638. end

639. if x then

640. last = x

641. if type(x) == "number" then x = "#"..x end

642. operands[#operands+1] = x

643. end

644. end

646. return putop(ctx, name..suffix, operands)

647. end

649. ------------------------------------------------------------------------------

651. -- Disassemble a block of code.

652. ‌local function disass_block(ctx, ofs, len)

653. if not ofs then ofs = 0 end

654. local stop = len and ofs+len or #ctx.code

655. ctx.pos = ofs

656. ctx.rel = nil

657. while ctx.pos < stop do disass_ins(ctx) end

658. end

660. -- Extended API: create a disassembler context. Then call ctx:disass(ofs, len).

661. ‌local function create(code, addr, out)

662. local ctx = {}

663. ctx.code = code

664. ctx.addr = addr or 0

665. ctx.out = out or io.write

666. ctx.symtab = {}

667. ctx.disass = disass_block

668. ctx.hexdump = 8

669. return ctx

670. end

672. -- Simple API: disassemble code (a string) at address and output via out.

673. ‌local function disass(code, addr, out)

674. create(code, addr, out):disass()

675. end

677. -- Return register name for RID.

678. ‌local function regname(r)

679. if r < 16 then return map_gpr[r] end

680. return "d"..(r-16)

681. end

683. -- Public module functions.

684. return {

685. create = create,

686. disass = disass,

687. regname = regname

688. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/dis_mipsel.lua - luajit-2.0-src

1. ----------------------------------------------------------------------------

2. -- LuaJIT MIPSEL disassembler wrapper module.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. -- This module just exports the little-endian functions from the

8. -- MIPS disassembler module. All the interesting stuff is there.

9. ------------------------------------------------------------------------------

11. local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips")

12. return {

13. create = dis_mips.create_el,

14. disass = dis_mips.disass_el,

15. regname = dis_mips.regname

16. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/dis_ppc.lua - luajit-2.0-src

Functions defined

- condfmt

- create

- disass

- disass_block

- disass_ins

- putop

- regname

- unknown

- { __index

- { __index

Source code

1. ----------------------------------------------------------------------------

2. -- LuaJIT PPC disassembler module.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT/X license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. -- This is a helper module used by the LuaJIT machine code dumper module.

8. --

9. -- It disassembles all common, non-privileged 32/64 bit PowerPC instructions

10. -- plus the e500 SPE instructions and some Cell/Xenon extensions.

11. --

12. -- NYI: VMX, VMX128

13. ------------------------------------------------------------------------------

15. local type = type

16. local sub, byte, format = string.sub, string.byte, string.format

17. local match, gmatch, gsub = string.match, string.gmatch, string.gsub

18. local concat = table.concat

19. local bit = require("bit")

20. local band, bor, tohex = bit.band, bit.bor, bit.tohex

21. local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift

23. ------------------------------------------------------------------------------

24. -- Primary and extended opcode maps

25. ------------------------------------------------------------------------------

27. local map_crops = {

28. shift = 1, mask = 1023,

29. [0] = "mcrfXX",

30. [33] = "crnor|crnotCCC=", [129] = "crandcCCC",

31. [193] = "crxor|crclrCCC%", [225] = "crnandCCC",

32. [257] = "crandCCC", [289] = "creqv|crsetCCC%",

33. [417] = "crorcCCC", [449] = "cror|crmoveCCC=",

34. [16] = "b_lrKB", [528] = "b_ctrKB",

35. [150] = "isync",

36. }

38. local map_rlwinm = setmetatable({

39. shift = 0, mask = -1,

40. },

41. ‌{ __index = function(t, x)

42. local rot = band(rshift(x, 11), 31)

43. local mb = band(rshift(x, 6), 31)

44. local me = band(rshift(x, 1), 31)

45. if mb == 0 and me == 31-rot then

46. return "slwiRR~A."

47. elseif me == 31 and mb == 32-rot then

48. return "srwiRR~-A."

49. else

50. return "rlwinmRR~AAA."

51. end

52. end

53. })

55. local map_rld = {

56. shift = 2, mask = 7,

57. [0] = "rldiclRR~HM.", "rldicrRR~HM.", "rldicRR~HM.", "rldimiRR~HM.",

58. {

59. shift = 1, mask = 1,

60. [0] = "rldclRR~RM.", "rldcrRR~RM.",

61. },

62. }

64. local map_ext = setmetatable({

65. shift = 1, mask = 1023,

67. [0] = "cmp_YLRR", [32] = "cmpl_YLRR",

68. [4] = "twARR", [68] = "tdARR",

70. [8] = "subfcRRR.", [40] = "subfRRR.",

71. [104] = "negRR.", [136] = "subfeRRR.",

72. [200] = "subfzeRR.", [232] = "subfmeRR.",

73. [520] = "subfcoRRR.", [552] = "subfoRRR.",

74. [616] = "negoRR.", [648] = "subfeoRRR.",

75. [712] = "subfzeoRR.", [744] = "subfmeoRR.",

77. [9] = "mulhduRRR.", [73] = "mulhdRRR.", [233] = "mulldRRR.",

78. [457] = "divduRRR.", [489] = "divdRRR.",

79. [745] = "mulldoRRR.",

80. [969] = "divduoRRR.", [1001] = "divdoRRR.",

82. [10] = "addcRRR.", [138] = "addeRRR.",

83. [202] = "addzeRR.", [234] = "addmeRR.", [266] = "addRRR.",

84. [522] = "addcoRRR.", [650] = "addeoRRR.",

85. [714] = "addzeoRR.", [746] = "addmeoRR.", [778] = "addoRRR.",

87. [11] = "mulhwuRRR.", [75] = "mulhwRRR.", [235] = "mullwRRR.",

88. [459] = "divwuRRR.", [491] = "divwRRR.",

89. [747] = "mullwoRRR.",

90. [971] = "divwouRRR.", [1003] = "divwoRRR.",

92. [15] = "iselltRRR", [47] = "iselgtRRR", [79] = "iseleqRRR",

94. [144] = { shift = 20, mask = 1, [0] = "mtcrfRZ~", "mtocrfRZ~", },

95. [19] = { shift = 20, mask = 1, [0] = "mfcrR", "mfocrfRZ", },

96. [371] = { shift = 11, mask = 1023, [392] = "mftbR", [424] = "mftbuR", },

97. [339] = {

98. shift = 11, mask = 1023,

99. [32] = "mferR", [256] = "mflrR", [288] = "mfctrR", [16] = "mfspefscrR",

100. },

101. [467] = {

102. shift = 11, mask = 1023,

103. [32] = "mtxerR", [256] = "mtlrR", [288] = "mtctrR", [16] = "mtspefscrR",

104. },

106. [20] = "lwarxRR0R", [84] = "ldarxRR0R",

108. [21] = "ldxRR0R", [53] = "lduxRRR",

109. [149] = "stdxRR0R", [181] = "stduxRRR",

110. [341] = "lwaxRR0R", [373] = "lwauxRRR",

112. [23] = "lwzxRR0R", [55] = "lwzuxRRR",

113. [87] = "lbzxRR0R", [119] = "lbzuxRRR",

114. [151] = "stwxRR0R", [183] = "stwuxRRR",

115. [215] = "stbxRR0R", [247] = "stbuxRRR",

116. [279] = "lhzxRR0R", [311] = "lhzuxRRR",

117. [343] = "lhaxRR0R", [375] = "lhauxRRR",

118. [407] = "sthxRR0R", [439] = "sthuxRRR",

120. [54] = "dcbst-R0R", [86] = "dcbf-R0R",

121. [150] = "stwcxRR0R.", [214] = "stdcxRR0R.",

122. [246] = "dcbtst-R0R", [278] = "dcbt-R0R",

123. [310] = "eciwxRR0R", [438] = "ecowxRR0R",

124. [470] = "dcbi-RR",

126. [598] = {

127. shift = 21, mask = 3,

128. [0] = "sync", "lwsync", "ptesync",

129. },

130. [758] = "dcba-RR",

131. [854] = "eieio", [982] = "icbi-R0R", [1014] = "dcbz-R0R",

133. [26] = "cntlzwRR~", [58] = "cntlzdRR~",

134. [122] = "popcntbRR~",

135. [154] = "prtywRR~", [186] = "prtydRR~",

137. [28] = "andRR~R.", [60] = "andcRR~R.", [124] = "nor|notRR~R=.",

138. [284] = "eqvRR~R.", [316] = "xorRR~R.",

139. [412] = "orcRR~R.", [444] = "or|mrRR~R=.", [476] = "nandRR~R.",

140. [508] = "cmpbRR~R",

142. [512] = "mcrxrX",

144. [532] = "ldbrxRR0R", [660] = "stdbrxRR0R",

146. [533] = "lswxRR0R", [597] = "lswiRR0A",

147. [661] = "stswxRR0R", [725] = "stswiRR0A",

149. [534] = "lwbrxRR0R", [662] = "stwbrxRR0R",

150. [790] = "lhbrxRR0R", [918] = "sthbrxRR0R",

152. [535] = "lfsxFR0R", [567] = "lfsuxFRR",

153. [599] = "lfdxFR0R", [631] = "lfduxFRR",

154. [663] = "stfsxFR0R", [695] = "stfsuxFRR",

155. [727] = "stfdxFR0R", [759] = "stfduxFR0R",

156. [855] = "lfiwaxFR0R",

157. [983] = "stfiwxFR0R",

159. [24] = "slwRR~R.",

161. [27] = "sldRR~R.", [536] = "srwRR~R.",

162. [792] = "srawRR~R.", [824] = "srawiRR~A.",

164. [794] = "sradRR~R.", [826] = "sradiRR~H.", [827] = "sradiRR~H.",

165. [922] = "extshRR~.", [954] = "extsbRR~.", [986] = "extswRR~.",

167. [539] = "srdRR~R.",

168. },

169. ‌{ __index = function(t, x)

170. if band(x, 31) == 15 then return "iselRRRC" end

171. end

172. })

174. local map_ld = {

175. shift = 0, mask = 3,

176. [0] = "ldRRE", "lduRRE", "lwaRRE",

177. }

179. local map_std = {

180. shift = 0, mask = 3,

181. [0] = "stdRRE", "stduRRE",

182. }

184. local map_fps = {

185. shift = 5, mask = 1,

186. {

187. shift = 1, mask = 15,

188. [0] = false, false, "fdivsFFF.", false,

189. "fsubsFFF.", "faddsFFF.", "fsqrtsF-F.", false,

190. "fresF-F.", "fmulsFF-F.", "frsqrtesF-F.", false,

191. "fmsubsFFFF~.", "fmaddsFFFF~.", "fnmsubsFFFF~.", "fnmaddsFFFF~.",

192. }

193. }

195. local map_fpd = {

196. shift = 5, mask = 1,

197. [0] = {

198. shift = 1, mask = 1023,

199. [0] = "fcmpuXFF", [32] = "fcmpoXFF", [64] = "mcrfsXX",

200. [38] = "mtfsb1A.", [70] = "mtfsb0A.", [134] = "mtfsfiA>>-A>",

201. [8] = "fcpsgnFFF.", [40] = "fnegF-F.", [72] = "fmrF-F.",

202. [136] = "fnabsF-F.", [264] = "fabsF-F.",

203. [12] = "frspF-F.",

204. [14] = "fctiwF-F.", [15] = "fctiwzF-F.",

205. [583] = "mffsF.", [711] = "mtfsfZF.",

206. [392] = "frinF-F.", [424] = "frizF-F.",

207. [456] = "fripF-F.", [488] = "frimF-F.",

208. [814] = "fctidF-F.", [815] = "fctidzF-F.", [846] = "fcfidF-F.",

209. },

210. {

211. shift = 1, mask = 15,

212. [0] = false, false, "fdivFFF.", false,

213. "fsubFFF.", "faddFFF.", "fsqrtF-F.", "fselFFFF~.",

214. "freF-F.", "fmulFF-F.", "frsqrteF-F.", false,

215. "fmsubFFFF~.", "fmaddFFFF~.", "fnmsubFFFF~.", "fnmaddFFFF~.",

216. }

217. }

219. local map_spe = {

220. shift = 0, mask = 2047,

222. [512] = "evaddwRRR", [514] = "evaddiwRAR~",

223. [516] = "evsubwRRR~", [518] = "evsubiwRAR~",

224. [520] = "evabsRR", [521] = "evnegRR",

225. [522] = "evextsbRR", [523] = "evextshRR", [524] = "evrndwRR",

226. [525] = "evcntlzwRR", [526] = "evcntlswRR",

228. [527] = "brincRRR",

230. [529] = "evandRRR", [530] = "evandcRRR", [534] = "evxorRRR",

231. [535] = "evor|evmrRRR=", [536] = "evnor|evnotRRR=",

232. [537] = "eveqvRRR", [539] = "evorcRRR", [542] = "evnandRRR",

234. [544] = "evsrwuRRR", [545] = "evsrwsRRR",

235. [546] = "evsrwiuRRA", [547] = "evsrwisRRA",

236. [548] = "evslwRRR", [550] = "evslwiRRA",

237. [552] = "evrlwRRR", [553] = "evsplatiRS",

238. [554] = "evrlwiRRA", [555] = "evsplatfiRS",

239. [556] = "evmergehiRRR", [557] = "evmergeloRRR",

240. [558] = "evmergehiloRRR", [559] = "evmergelohiRRR",

242. [560] = "evcmpgtuYRR", [561] = "evcmpgtsYRR",

243. [562] = "evcmpltuYRR", [563] = "evcmpltsYRR",

244. [564] = "evcmpeqYRR",

246. [632] = "evselRRR", [633] = "evselRRRW",

247. [634] = "evselRRRW", [635] = "evselRRRW",

248. [636] = "evselRRRW", [637] = "evselRRRW",

249. [638] = "evselRRRW", [639] = "evselRRRW",

251. [640] = "evfsaddRRR", [641] = "evfssubRRR",

252. [644] = "evfsabsRR", [645] = "evfsnabsRR", [646] = "evfsnegRR",

253. [648] = "evfsmulRRR", [649] = "evfsdivRRR",

254. [652] = "evfscmpgtYRR", [653] = "evfscmpltYRR", [654] = "evfscmpeqYRR",

255. [656] = "evfscfuiR-R", [657] = "evfscfsiR-R",

256. [658] = "evfscfufR-R", [659] = "evfscfsfR-R",

257. [660] = "evfsctuiR-R", [661] = "evfsctsiR-R",

258. [662] = "evfsctufR-R", [663] = "evfsctsfR-R",

259. [664] = "evfsctuizR-R", [666] = "evfsctsizR-R",

260. [668] = "evfststgtYRR", [669] = "evfststltYRR", [670] = "evfststeqYRR",

262. [704] = "efsaddRRR", [705] = "efssubRRR",

263. [708] = "efsabsRR", [709] = "efsnabsRR", [710] = "efsnegRR",

264. [712] = "efsmulRRR", [713] = "efsdivRRR",

265. [716] = "efscmpgtYRR", [717] = "efscmpltYRR", [718] = "efscmpeqYRR",

266. [719] = "efscfdR-R",

267. [720] = "efscfuiR-R", [721] = "efscfsiR-R",

268. [722] = "efscfufR-R", [723] = "efscfsfR-R",

269. [724] = "efsctuiR-R", [725] = "efsctsiR-R",

270. [726] = "efsctufR-R", [727] = "efsctsfR-R",

271. [728] = "efsctuizR-R", [730] = "efsctsizR-R",

272. [732] = "efststgtYRR", [733] = "efststltYRR", [734] = "efststeqYRR",

274. [736] = "efdaddRRR", [737] = "efdsubRRR",

275. [738] = "efdcfuidR-R", [739] = "efdcfsidR-R",

276. [740] = "efdabsRR", [741] = "efdnabsRR", [742] = "efdnegRR",

277. [744] = "efdmulRRR", [745] = "efddivRRR",

278. [746] = "efdctuidzR-R", [747] = "efdctsidzR-R",

279. [748] = "efdcmpgtYRR", [749] = "efdcmpltYRR", [750] = "efdcmpeqYRR",

280. [751] = "efdcfsR-R",

281. [752] = "efdcfuiR-R", [753] = "efdcfsiR-R",

282. [754] = "efdcfufR-R", [755] = "efdcfsfR-R",

283. [756] = "efdctuiR-R", [757] = "efdctsiR-R",

284. [758] = "efdctufR-R", [759] = "efdctsfR-R",

285. [760] = "efdctuizR-R", [762] = "efdctsizR-R",

286. [764] = "efdtstgtYRR", [765] = "efdtstltYRR", [766] = "efdtsteqYRR",

288. [768] = "evlddxRR0R", [769] = "evlddRR8",

289. [770] = "evldwxRR0R", [771] = "evldwRR8",

290. [772] = "evldhxRR0R", [773] = "evldhRR8",

291. [776] = "evlhhesplatxRR0R", [777] = "evlhhesplatRR2",

292. [780] = "evlhhousplatxRR0R", [781] = "evlhhousplatRR2",

293. [782] = "evlhhossplatxRR0R", [783] = "evlhhossplatRR2",

294. [784] = "evlwhexRR0R", [785] = "evlwheRR4",

295. [788] = "evlwhouxRR0R", [789] = "evlwhouRR4",

296. [790] = "evlwhosxRR0R", [791] = "evlwhosRR4",

297. [792] = "evlwwsplatxRR0R", [793] = "evlwwsplatRR4",

298. [796] = "evlwhsplatxRR0R", [797] = "evlwhsplatRR4",

300. [800] = "evstddxRR0R", [801] = "evstddRR8",

301. [802] = "evstdwxRR0R", [803] = "evstdwRR8",

302. [804] = "evstdhxRR0R", [805] = "evstdhRR8",

303. [816] = "evstwhexRR0R", [817] = "evstwheRR4",

304. [820] = "evstwhoxRR0R", [821] = "evstwhoRR4",

305. [824] = "evstwwexRR0R", [825] = "evstwweRR4",

306. [828] = "evstwwoxRR0R", [829] = "evstwwoRR4",

308. [1027] = "evmhessfRRR", [1031] = "evmhossfRRR", [1032] = "evmheumiRRR",

309. [1033] = "evmhesmiRRR", [1035] = "evmhesmfRRR", [1036] = "evmhoumiRRR",

310. [1037] = "evmhosmiRRR", [1039] = "evmhosmfRRR", [1059] = "evmhessfaRRR",

311. [1063] = "evmhossfaRRR", [1064] = "evmheumiaRRR", [1065] = "evmhesmiaRRR",

312. [1067] = "evmhesmfaRRR", [1068] = "evmhoumiaRRR", [1069] = "evmhosmiaRRR",

313. [1071] = "evmhosmfaRRR", [1095] = "evmwhssfRRR", [1096] = "evmwlumiRRR",

314. [1100] = "evmwhumiRRR", [1101] = "evmwhsmiRRR", [1103] = "evmwhsmfRRR",

315. [1107] = "evmwssfRRR", [1112] = "evmwumiRRR", [1113] = "evmwsmiRRR",

316. [1115] = "evmwsmfRRR", [1127] = "evmwhssfaRRR", [1128] = "evmwlumiaRRR",

317. [1132] = "evmwhumiaRRR", [1133] = "evmwhsmiaRRR", [1135] = "evmwhsmfaRRR",

318. [1139] = "evmwssfaRRR", [1144] = "evmwumiaRRR", [1145] = "evmwsmiaRRR",

319. [1147] = "evmwsmfaRRR",

321. [1216] = "evaddusiaawRR", [1217] = "evaddssiaawRR",

322. [1218] = "evsubfusiaawRR", [1219] = "evsubfssiaawRR",

323. [1220] = "evmraRR",

324. [1222] = "evdivwsRRR", [1223] = "evdivwuRRR",

325. [1224] = "evaddumiaawRR", [1225] = "evaddsmiaawRR",

326. [1226] = "evsubfumiaawRR", [1227] = "evsubfsmiaawRR",

328. [1280] = "evmheusiaawRRR", [1281] = "evmhessiaawRRR",

329. [1283] = "evmhessfaawRRR", [1284] = "evmhousiaawRRR",

330. [1285] = "evmhossiaawRRR", [1287] = "evmhossfaawRRR",

331. [1288] = "evmheumiaawRRR", [1289] = "evmhesmiaawRRR",

332. [1291] = "evmhesmfaawRRR", [1292] = "evmhoumiaawRRR",

333. [1293] = "evmhosmiaawRRR", [1295] = "evmhosmfaawRRR",

334. [1320] = "evmhegumiaaRRR", [1321] = "evmhegsmiaaRRR",

335. [1323] = "evmhegsmfaaRRR", [1324] = "evmhogumiaaRRR",

336. [1325] = "evmhogsmiaaRRR", [1327] = "evmhogsmfaaRRR",

337. [1344] = "evmwlusiaawRRR", [1345] = "evmwlssiaawRRR",

338. [1352] = "evmwlumiaawRRR", [1353] = "evmwlsmiaawRRR",

339. [1363] = "evmwssfaaRRR", [1368] = "evmwumiaaRRR",

340. [1369] = "evmwsmiaaRRR", [1371] = "evmwsmfaaRRR",

341. [1408] = "evmheusianwRRR", [1409] = "evmhessianwRRR",

342. [1411] = "evmhessfanwRRR", [1412] = "evmhousianwRRR",

343. [1413] = "evmhossianwRRR", [1415] = "evmhossfanwRRR",

344. [1416] = "evmheumianwRRR", [1417] = "evmhesmianwRRR",

345. [1419] = "evmhesmfanwRRR", [1420] = "evmhoumianwRRR",

346. [1421] = "evmhosmianwRRR", [1423] = "evmhosmfanwRRR",

347. [1448] = "evmhegumianRRR", [1449] = "evmhegsmianRRR",

348. [1451] = "evmhegsmfanRRR", [1452] = "evmhogumianRRR",

349. [1453] = "evmhogsmianRRR", [1455] = "evmhogsmfanRRR",

350. [1472] = "evmwlusianwRRR", [1473] = "evmwlssianwRRR",

351. [1480] = "evmwlumianwRRR", [1481] = "evmwlsmianwRRR",

352. [1491] = "evmwssfanRRR", [1496] = "evmwumianRRR",

353. [1497] = "evmwsmianRRR", [1499] = "evmwsmfanRRR",

354. }

356. local map_pri = {

357. [0] = false,        false,                "tdiARI",        "twiARI",

358. map_spe,        false,                false,                "mulliRRI",

359. "subficRRI",        false,                "cmpl_iYLRU",        "cmp_iYLRI",

360. "addicRRI",        "addic.RRI",        "addi|liRR0I",        "addis|lisRR0I",

361. "b_KBJ",        "sc",                 "bKJ",                map_crops,

362. "rlwimiRR~AAA.", map_rlwinm,        false,                "rlwnmRR~RAA.",

363. "oriNRR~U",        "orisRR~U",        "xoriRR~U",        "xorisRR~U",

364. "andi.RR~U",        "andis.RR~U",        map_rld,        map_ext,

365. "lwzRRD",        "lwzuRRD",        "lbzRRD",        "lbzuRRD",

366. "stwRRD",        "stwuRRD",        "stbRRD",        "stbuRRD",

367. "lhzRRD",        "lhzuRRD",        "lhaRRD",        "lhauRRD",

368. "sthRRD",        "sthuRRD",        "lmwRRD",        "stmwRRD",

369. "lfsFRD",        "lfsuFRD",        "lfdFRD",        "lfduFRD",

370. "stfsFRD",        "stfsuFRD",        "stfdFRD",        "stfduFRD",

371. false,        false,                map_ld,                map_fps,

372. false,        false,                map_std,        map_fpd,

373. }

375. ------------------------------------------------------------------------------

377. local map_gpr = {

378. [0] = "r0", "sp", "r2", "r3", "r4", "r5", "r6", "r7",

379. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",

380. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",

381. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",

382. }

384. local map_cond = { [0] = "lt", "gt", "eq", "so", "ge", "le", "ne", "ns", }

386. -- Format a condition bit.

387. ‌local function condfmt(cond)

388. if cond <= 3 then

389. return map_cond[band(cond, 3)]

390. else

391. return format("4*cr%d+%s", rshift(cond, 2), map_cond[band(cond, 3)])

392. end

393. end

395. ------------------------------------------------------------------------------

397. -- Output a nicely formatted line with an opcode and operands.

398. ‌local function putop(ctx, text, operands)

399. local pos = ctx.pos

400. local extra = ""

401. if ctx.rel then

402. local sym = ctx.symtab[ctx.rel]

403. if sym then extra = "\t->"..sym end

404. end

405. if ctx.hexdump > 0 then

406. ctx.out(format("%08x  %s  %-7s %s%s\n",

407. ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra))

408. else

409. ctx.out(format("%08x  %-7s %s%s\n",

410. ctx.addr+pos, text, concat(operands, ", "), extra))

411. end

412. ctx.pos = pos + 4

413. end

415. -- Fallback for unknown opcodes.

416. ‌local function unknown(ctx)

417. return putop(ctx, ".long", { "0x"..tohex(ctx.op) })

418. end

420. -- Disassemble a single instruction.

421. ‌local function disass_ins(ctx)

422. local pos = ctx.pos

423. local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)

424. local op = bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3)

425. local operands = {}

426. local last = nil

427. local rs = 21

428. ctx.op = op

429. ctx.rel = nil

431. local opat = map_pri[rshift(b0, 2)]

432. while type(opat) ~= "string" do

433. if not opat then return unknown(ctx) end

434. opat = opat[band(rshift(op, opat.shift), opat.mask)]

435. end

436. local name, pat = match(opat, "^([a-z0-9_.]*)(.*)")

437. local altname, pat2 = match(pat, "|([a-z0-9_.]*)(.*)")

438. if altname then pat = pat2 end

440. for p in gmatch(pat, ".") do

441. local x = nil

442. if p == "R" then

443. x = map_gpr[band(rshift(op, rs), 31)]

444. rs = rs - 5

445. elseif p == "F" then

446. x = "f"..band(rshift(op, rs), 31)

447. rs = rs - 5

448. elseif p == "A" then

449. x = band(rshift(op, rs), 31)

450. rs = rs - 5

451. elseif p == "S" then

452. x = arshift(lshift(op, 27-rs), 27)

453. rs = rs - 5

454. elseif p == "I" then

455. x = arshift(lshift(op, 16), 16)

456. elseif p == "U" then

457. x = band(op, 0xffff)

458. elseif p == "D" or p == "E" then

459. local disp = arshift(lshift(op, 16), 16)

460. if p == "E" then disp = band(disp, -4) end

461. if last == "r0" then last = "0" end

462. operands[#operands] = format("%d(%s)", disp, last)

463. elseif p >= "2" and p <= "8" then

464. local disp = band(rshift(op, rs), 31) * p

465. if last == "r0" then last = "0" end

466. operands[#operands] = format("%d(%s)", disp, last)

467. elseif p == "H" then

468. x = band(rshift(op, rs), 31) + lshift(band(op, 2), 4)

469. rs = rs - 5

470. elseif p == "M" then

471. x = band(rshift(op, rs), 31) + band(op, 0x20)

472. elseif p == "C" then

473. x = condfmt(band(rshift(op, rs), 31))

474. rs = rs - 5

475. elseif p == "B" then

476. local bo = rshift(op, 21)

477. local cond = band(rshift(op, 16), 31)

478. local cn = ""

479. rs = rs - 10

480. if band(bo, 4) == 0 then

481. cn = band(bo, 2) == 0 and "dnz" or "dz"

482. if band(bo, 0x10) == 0 then

483. cn = cn..(band(bo, 8) == 0 and "f" or "t")

484. end

485. if band(bo, 0x10) == 0 then x = condfmt(cond) end

486. name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+")

487. elseif band(bo, 0x10) == 0 then

488. cn = map_cond[band(cond, 3) + (band(bo, 8) == 0 and 4 or 0)]

489. if cond > 3 then x = "cr"..rshift(cond, 2) end

490. name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+")

491. end

492. name = gsub(name, "_", cn)

493. elseif p == "J" then

494. x = arshift(lshift(op, 27-rs), 29-rs)*4

495. if band(op, 2) == 0 then x = ctx.addr + pos + x end

496. ctx.rel = x

497. x = "0x"..tohex(x)

498. elseif p == "K" then

499. if band(op, 1) ~= 0 then name = name.."l" end

500. if band(op, 2) ~= 0 then name = name.."a" end

501. elseif p == "X" or p == "Y" then

502. x = band(rshift(op, rs+2), 7)

503. if x == 0 and p == "Y" then x = nil else x = "cr"..x end

504. rs = rs - 5

505. elseif p == "W" then

506. x = "cr"..band(op, 7)

507. elseif p == "Z" then

508. x = band(rshift(op, rs-4), 255)

509. rs = rs - 10

510. elseif p == ">" then

511. operands[#operands] = rshift(operands[#operands], 1)

512. elseif p == "0" then

513. if last == "r0" then

514. operands[#operands] = nil

515. if altname then name = altname end

516. end

517. elseif p == "L" then

518. name = gsub(name, "_", band(op, 0x00200000) ~= 0 and "d" or "w")

519. elseif p == "." then

520. if band(op, 1) == 1 then name = name.."." end

521. elseif p == "N" then

522. if op == 0x60000000 then name = "nop"; break end

523. elseif p == "~" then

524. local n = #operands

525. operands[n-1],  operands[n] = operands[n], operands[n-1]

526. elseif p == "=" then

527. local n = #operands

528. if last == operands[n-1] then

529. operands[n] = nil

530. name = altname

531. end

532. elseif p == "%" then

533. local n = #operands

534. if last == operands[n-1] and last == operands[n-2] then

535. operands[n] = nil

536. operands[n-1] = nil

537. name = altname

538. end

539. elseif p == "-" then

540. rs = rs - 5

541. else

542. assert(false)

543. end

544. if x then operands[#operands+1] = x; last = x end

545. end

547. return putop(ctx, name, operands)

548. end

550. ------------------------------------------------------------------------------

552. -- Disassemble a block of code.

553. ‌local function disass_block(ctx, ofs, len)

554. if not ofs then ofs = 0 end

555. local stop = len and ofs+len or #ctx.code

556. stop = stop - stop % 4

557. ctx.pos = ofs - ofs % 4

558. ctx.rel = nil

559. while ctx.pos < stop do disass_ins(ctx) end

560. end

562. -- Extended API: create a disassembler context. Then call ctx:disass(ofs, len).

563. ‌local function create(code, addr, out)

564. local ctx = {}

565. ctx.code = code

566. ctx.addr = addr or 0

567. ctx.out = out or io.write

568. ctx.symtab = {}

569. ctx.disass = disass_block

570. ctx.hexdump = 8

571. return ctx

572. end

574. -- Simple API: disassemble code (a string) at address and output via out.

575. ‌local function disass(code, addr, out)

576. create(code, addr, out):disass()

577. end

579. -- Return register name for RID.

580. ‌local function regname(r)

581. if r < 32 then return map_gpr[r] end

582. return "f"..(r-32)

583. end

585. -- Public module functions.

586. return {

587. create = create,

588. disass = disass,

589. regname = regname

590. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/dis_x64.lua - luajit-2.0-src

1. ----------------------------------------------------------------------------

2. -- LuaJIT x64 disassembler wrapper module.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. -- This module just exports the 64 bit functions from the combined

8. -- x86/x64 disassembler module. All the interesting stuff is there.

9. ------------------------------------------------------------------------------

11. local dis_x86 = require((string.match(..., ".*%.") or "").."dis_x86")

12. return {

13. create = dis_x86.create64,

14. disass = dis_x86.disass64,

15. regname = dis_x86.regname64

16. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/dump.lua - luajit-2.0-src

Functions defined

- ["CONV "]

- ["SLOAD "]

- colorize_ansi

- colorize_html

- colorize_text

- ctlsub

- dump_ir

- dump_mcode

- dump_record

- dump_snap

- dump_texit

- dump_trace

- dumpcallargs

- dumpcallfunc

- dumpoff

- dumpon

- dumpwrite

- elseif tn

- fillsymtab

- fillsymtab_tr

- fmterr

- fmtfunc

- formatk

- if type(info)

- opt

- printsnap

- ridsp_name

- { __index

- { __index

Source code

1. ----------------------------------------------------------------------------

2. -- LuaJIT compiler dump module.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. --

8. -- This module can be used to debug the JIT compiler itself. It dumps the

9. -- code representations and structures used in various compiler stages.

10. --

11. -- Example usage:

12. --

13. --   luajit -jdump -e "local x=0; for i=1,1e6 do x=x+i end; print(x)"

14. --   luajit -jdump=im -e "for i=1,1000 do for j=1,1000 do end end" | less -R

15. --   luajit -jdump=is myapp.lua | less -R

16. --   luajit -jdump=-b myapp.lua

17. --   luajit -jdump=+aH,myapp.html myapp.lua

18. --   luajit -jdump=ixT,myapp.dump myapp.lua

19. --

20. -- The first argument specifies the dump mode. The second argument gives

21. -- the output file name. Default output is to stdout, unless the environment

22. -- variable LUAJIT_DUMPFILE is set. The file is overwritten every time the

23. -- module is started.

24. --

25. -- Different features can be turned on or off with the dump mode. If the

26. -- mode starts with a '+', the following features are added to the default

27. -- set of features; a '-' removes them. Otherwise the features are replaced.

28. --

29. -- The following dump features are available (* marks the default):

30. --

31. --  * t  Print a line for each started, ended or aborted trace (see also -jv).

32. --  * b  Dump the traced bytecode.

33. --  * i  Dump the IR (intermediate representation).

34. --    r  Augment the IR with register/stack slots.

35. --    s  Dump the snapshot map.

36. --  * m  Dump the generated machine code.

37. --    x  Print each taken trace exit.

38. --    X  Print each taken trace exit and the contents of all registers.

39. --    a  Print the IR of aborted traces, too.

40. --

41. -- The output format can be set with the following characters:

42. --

43. --    T  Plain text output.

44. --    A  ANSI-colored text output

45. --    H  Colorized HTML + CSS output.

46. --

47. -- The default output format is plain text. It's set to ANSI-colored text

48. -- if the COLORTERM variable is set. Note: this is independent of any output

49. -- redirection, which is actually considered a feature.

50. --

51. -- You probably want to use less -R to enjoy viewing ANSI-colored text from

52. -- a pipe or a file. Add this to your ~/.bashrc: export LESS="-R"

53. --

54. ------------------------------------------------------------------------------

56. -- Cache some library functions and objects.

57. local jit = require("jit")

58. assert(jit.version_num == 20100, "LuaJIT core/library version mismatch")

59. local jutil = require("jit.util")

60. local vmdef = require("jit.vmdef")

61. local funcinfo, funcbc = jutil.funcinfo, jutil.funcbc

62. local traceinfo, traceir, tracek = jutil.traceinfo, jutil.traceir, jutil.tracek

63. local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap

64. local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr

65. local bit = require("bit")

66. local band, shl, shr, tohex = bit.band, bit.lshift, bit.rshift, bit.tohex

67. local sub, gsub, format = string.sub, string.gsub, string.format

68. local byte, char, rep = string.byte, string.char, string.rep

69. local type, tostring = type, tostring

70. local stdout, stderr = io.stdout, io.stderr

72. -- Load other modules on-demand.

73. local bcline, disass

75. -- Active flag, output file handle and dump mode.

76. local active, out, dumpmode

78. ------------------------------------------------------------------------------

80. local symtabmt = { __index = false }

81. local symtab = {}

82. local nexitsym = 0

84. -- Fill nested symbol table with per-trace exit stub addresses.

85. ‌local function fillsymtab_tr(tr, nexit)

86. local t = {}

87. symtabmt.__index = t

88. if jit.arch == "mips" or jit.arch == "mipsel" then

89. t[traceexitstub(tr, 0)] = "exit"

90. return

91. end

92. for i=0,nexit-1 do

93. local addr = traceexitstub(tr, i)

94. if addr < 0 then addr = addr + 2^32 end

95. t[addr] = tostring(i)

96. end

97. local addr = traceexitstub(tr, nexit)

98. if addr then t[addr] = "stack_check" end

99. end

101. -- Fill symbol table with trace exit stub addresses.

102. ‌local function fillsymtab(tr, nexit)

103. local t = symtab

104. if nexitsym == 0 then

105. local ircall = vmdef.ircall

106. for i=0,#ircall do

107. local addr = ircalladdr(i)

108. if addr ~= 0 then

109. if addr < 0 then addr = addr + 2^32 end

110. t[addr] = ircall[i]

111. end

112. end

113. end

114. if nexitsym == 1000000 then -- Per-trace exit stubs.

115. fillsymtab_tr(tr, nexit)

116. elseif nexit > nexitsym then -- Shared exit stubs.

117. for i=nexitsym,nexit-1 do

118. local addr = traceexitstub(i)

119. if addr == nil then -- Fall back to per-trace exit stubs.

120. fillsymtab_tr(tr, nexit)

121. setmetatable(symtab, symtabmt)

122. nexit = 1000000

123. break

124. end

125. if addr < 0 then addr = addr + 2^32 end

126. t[addr] = tostring(i)

127. end

128. nexitsym = nexit

129. end

130. return t

131. end

133. ‌local function dumpwrite(s)

134. out:write(s)

135. end

137. -- Disassemble machine code.

138. ‌local function dump_mcode(tr)

139. local info = traceinfo(tr)

140. if not info then return end

141. local mcode, addr, loop = tracemc(tr)

142. if not mcode then return end

143. if not disass then disass = require("jit.dis_"..jit.arch) end

144. if addr < 0 then addr = addr + 2^32 end

145. out:write("---- TRACE ", tr, " mcode ", #mcode, "\n")

146. local ctx = disass.create(mcode, addr, dumpwrite)

147. ctx.hexdump = 0

148. ctx.symtab = fillsymtab(tr, info.nexit)

149. if loop ~= 0 then

150. symtab[addr+loop] = "LOOP"

151. ctx:disass(0, loop)

152. out:write("->LOOP:\n")

153. ctx:disass(loop, #mcode-loop)

154. symtab[addr+loop] = nil

155. else

156. ctx:disass(0, #mcode)

157. end

158. end

160. ------------------------------------------------------------------------------

162. local irtype_text = {

163. [0] = "nil",

164. "fal",

165. "tru",

166. "lud",

167. "str",

168. "p32",

169. "thr",

170. "pro",

171. "fun",

172. "p64",

173. "cdt",

174. "tab",

175. "udt",

176. "flt",

177. "num",

178. "i8 ",

179. "u8 ",

180. "i16",

181. "u16",

182. "int",

183. "u32",

184. "i64",

185. "u64",

186. "sfp",

187. }

189. local colortype_ansi = {

190. [0] = "%s",

191. "%s",

192. "%s",

193. "\027[36m%s\027[m",

194. "\027[32m%s\027[m",

195. "%s",

196. "\027[1m%s\027[m",

197. "%s",

198. "\027[1m%s\027[m",

199. "%s",

200. "\027[33m%s\027[m",

201. "\027[31m%s\027[m",

202. "\027[36m%s\027[m",

203. "\027[34m%s\027[m",

204. "\027[34m%s\027[m",

205. "\027[35m%s\027[m",

206. "\027[35m%s\027[m",

207. "\027[35m%s\027[m",

208. "\027[35m%s\027[m",

209. "\027[35m%s\027[m",

210. "\027[35m%s\027[m",

211. "\027[35m%s\027[m",

212. "\027[35m%s\027[m",

213. "\027[35m%s\027[m",

214. }

216. ‌local function colorize_text(s, t)

217. return s

218. end

220. ‌local function colorize_ansi(s, t)

221. return format(colortype_ansi[t], s)

222. end

224. local irtype_ansi = setmetatable({},

225. ‌  { __index = function(tab, t)

226. local s = colorize_ansi(irtype_text[t], t); tab[t] = s; return s; end })

228. local html_escape = { ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;", }

230. ‌local function colorize_html(s, t)

231. s = gsub(s, "[<>&]", html_escape)

232. return format('<span class="irt_%s">%s</span>', irtype_text[t], s)

233. end

235. local irtype_html = setmetatable({},

236. ‌  { __index = function(tab, t)

237. local s = colorize_html(irtype_text[t], t); tab[t] = s; return s; end })

239. local header_html = [[

240. <style type="text/css">

241. background { background: #ffffff; color: #000000; }

242. pre.ljdump {

243. font-size: 10pt;

244. background: #f0f4ff;

245. color: #000000;

246. border: 1px solid #bfcfff;

247. padding: 0.5em;

248. margin-left: 2em;

249. margin-right: 2em;

250. }

251. span.irt_str { color: #00a000; }

252. span.irt_thr, span.irt_fun { color: #404040; font-weight: bold; }

253. span.irt_tab { color: #c00000; }

254. span.irt_udt, span.irt_lud { color: #00c0c0; }

255. span.irt_num { color: #4040c0; }

256. span.irt_int, span.irt_i8, span.irt_u8, span.irt_i16, span.irt_u16 { color: #b040b0; }

257. </style>

258. ]]

260. local colorize, irtype

262. -- Lookup tables to convert some literals into names.

263. local litname = {

264. ‌  ["SLOAD "] = setmetatable({}, { __index = function(t, mode)

265. local s = ""

266. if band(mode, 1) ~= 0 then s = s.."P" end

267. if band(mode, 2) ~= 0 then s = s.."F" end

268. if band(mode, 4) ~= 0 then s = s.."T" end

269. if band(mode, 8) ~= 0 then s = s.."C" end

270. if band(mode, 16) ~= 0 then s = s.."R" end

271. if band(mode, 32) ~= 0 then s = s.."I" end

272. t[mode] = s

273. return s

274. end}),

275. ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", },

276. ‌  ["CONV  "] = setmetatable({}, { __index = function(t, mode)

277. local s = irtype[band(mode, 31)]

278. s = irtype[band(shr(mode, 5), 31)].."."..s

279. if band(mode, 0x800) ~= 0 then s = s.." sext" end

280. local c = shr(mode, 14)

281. if c == 2 then s = s.." index" elseif c == 3 then s = s.." check" end

282. t[mode] = s

283. return s

284. end}),

285. ["FLOAD "] = vmdef.irfield,

286. ["FREF  "] = vmdef.irfield,

287. ["FPMATH"] = vmdef.irfpm,

288. ["BUFHDR"] = { [0] = "RESET", "APPEND" },

289. ["TOSTR "] = { [0] = "INT", "NUM", "CHAR" },

290. }

292. ‌local function ctlsub(c)

293. if c == "\n" then return "\\n"

294. elseif c == "\r" then return "\\r"

295. elseif c == "\t" then return "\\t"

296. else return format("\\%03d", byte(c))

297. end

298. end

300. ‌local function fmtfunc(func, pc)

301. local fi = funcinfo(func, pc)

302. if fi.loc then

303. return fi.loc

304. elseif fi.ffid then

305. return vmdef.ffnames[fi.ffid]

306. elseif fi.addr then

307. return format("C:%x", fi.addr)

308. else

309. return "(?)"

310. end

311. end

313. ‌local function formatk(tr, idx)

314. local k, t, slot = tracek(tr, idx)

315. local tn = type(k)

316. local s

317. if tn == "number" then

318. if k == 2^52+2^51 then

319. s = "bias"

320. else

321. s = format("%+.14g", k)

322. end

323. elseif tn == "string" then

324. s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub))

325. ‌  elseif tn == "function" then

326. s = fmtfunc(k)

327. elseif tn == "table" then

328. s = format("{%p}", k)

329. elseif tn == "userdata" then

330. if t == 12 then

331. s = format("userdata:%p", k)

332. else

333. s = format("[%p]", k)

334. if s == "[0x00000000]" then s = "NULL" end

335. end

336. elseif t == 21 then -- int64_t

337. s = sub(tostring(k), 1, -3)

338. if sub(s, 1, 1) ~= "-" then s = "+"..s end

339. else

340. s = tostring(k) -- For primitives.

341. end

342. s = colorize(format("%-4s", s), t)

343. if slot then

344. s = format("%s @%d", s, slot)

345. end

346. return s

347. end

349. ‌local function printsnap(tr, snap)

350. local n = 2

351. for s=0,snap[1]-1 do

352. local sn = snap[n]

353. if shr(sn, 24) == s then

354. n = n + 1

355. local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS

356. if ref < 0 then

357. out:write(formatk(tr, ref))

358. elseif band(sn, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM

359. out:write(colorize(format("%04d/%04d", ref, ref+1), 14))

360. else

361. local m, ot, op1, op2 = traceir(tr, ref)

362. out:write(colorize(format("%04d", ref), band(ot, 31)))

363. end

364. out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME

365. else

366. out:write("---- ")

367. end

368. end

369. out:write("]\n")

370. end

372. -- Dump snapshots (not interleaved with IR).

373. ‌local function dump_snap(tr)

374. out:write("---- TRACE ", tr, " snapshots\n")

375. for i=0,1000000000 do

376. local snap = tracesnap(tr, i)

377. if not snap then break end

378. out:write(format("#%-3d %04d [ ", i, snap[0]))

379. printsnap(tr, snap)

380. end

381. end

383. -- Return a register name or stack slot for a rid/sp location.

384. ‌local function ridsp_name(ridsp, ins)

385. if not disass then disass = require("jit.dis_"..jit.arch) end

386. local rid, slot = band(ridsp, 0xff), shr(ridsp, 8)

387. if rid == 253 or rid == 254 then

388. return (slot == 0 or slot == 255) and " {sink" or format(" {%04d", ins-slot)

389. end

390. if ridsp > 255 then return format("[%x]", slot*4) end

391. if rid < 128 then return disass.regname(rid) end

392. return ""

393. end

395. -- Dump CALL* function ref and return optional ctype.

396. ‌local function dumpcallfunc(tr, ins)

397. local ctype

398. if ins > 0 then

399. local m, ot, op1, op2 = traceir(tr, ins)

400. if band(ot, 31) == 0 then -- nil type means CARG(func, ctype).

401. ins = op1

402. ctype = formatk(tr, op2)

403. end

404. end

405. if ins < 0 then

406. out:write(format("[0x%x](", tonumber((tracek(tr, ins)))))

407. else

408. out:write(format("%04d (", ins))

409. end

410. return ctype

411. end

413. -- Recursively gather CALL* args and dump them.

414. ‌local function dumpcallargs(tr, ins)

415. if ins < 0 then

416. out:write(formatk(tr, ins))

417. else

418. local m, ot, op1, op2 = traceir(tr, ins)

419. local oidx = 6*shr(ot, 8)

420. local op = sub(vmdef.irnames, oidx+1, oidx+6)

421. if op == "CARG  " then

422. dumpcallargs(tr, op1)

423. if op2 < 0 then

424. out:write(" ", formatk(tr, op2))

425. else

426. out:write(" ", format("%04d", op2))

427. end

428. else

429. out:write(format("%04d", ins))

430. end

431. end

432. end

434. -- Dump IR and interleaved snapshots.

435. ‌local function dump_ir(tr, dumpsnap, dumpreg)

436. local info = traceinfo(tr)

437. if not info then return end

438. local nins = info.nins

439. out:write("---- TRACE ", tr, " IR\n")

440. local irnames = vmdef.irnames

441. local snapref = 65536

442. local snap, snapno

443. if dumpsnap then

444. snap = tracesnap(tr, 0)

445. snapref = snap[0]

446. snapno = 0

447. end

448. for ins=1,nins do

449. if ins >= snapref then

450. if dumpreg then

451. out:write(format("....              SNAP   #%-3d [ ", snapno))

452. else

453. out:write(format("....        SNAP   #%-3d [ ", snapno))

454. end

455. printsnap(tr, snap)

456. snapno = snapno + 1

457. snap = tracesnap(tr, snapno)

458. snapref = snap and snap[0] or 65536

459. end

460. local m, ot, op1, op2, ridsp = traceir(tr, ins)

461. local oidx, t = 6*shr(ot, 8), band(ot, 31)

462. local op = sub(irnames, oidx+1, oidx+6)

463. if op == "LOOP  " then

464. if dumpreg then

465. out:write(format("%04d ------------ LOOP ------------\n", ins))

466. else

467. out:write(format("%04d ------ LOOP ------------\n", ins))

468. end

469. elseif op ~= "NOP   " and op ~= "CARG  " and

470. (dumpreg or op ~= "RENAME") then

471. local rid = band(ridsp, 255)

472. if dumpreg then

473. out:write(format("%04d %-6s", ins, ridsp_name(ridsp, ins)))

474. else

475. out:write(format("%04d ", ins))

476. end

477. out:write(format("%s%s %s %s ",

478. (rid == 254 or rid == 253) and "}" or

479. (band(ot, 128) == 0 and " " or ">"),

480. band(ot, 64) == 0 and " " or "+",

481. irtype[t], op))

482. local m1, m2 = band(m, 3), band(m, 3*4)

483. if sub(op, 1, 4) == "CALL" then

484. local ctype

485. if m2 == 1*4 then -- op2 == IRMlit

486. out:write(format("%-10s  (", vmdef.ircall[op2]))

487. else

488. ctype = dumpcallfunc(tr, op2)

489. end

490. if op1 ~= -1 then dumpcallargs(tr, op1) end

491. out:write(")")

492. if ctype then out:write(" ctype ", ctype) end

493. elseif op == "CNEW  " and op2 == -1 then

494. out:write(formatk(tr, op1))

495. elseif m1 ~= 3 then -- op1 != IRMnone

496. if op1 < 0 then

497. out:write(formatk(tr, op1))

498. else

499. out:write(format(m1 == 0 and "%04d" or "#%-3d", op1))

500. end

501. if m2 ~= 3*4 then -- op2 != IRMnone

502. if m2 == 1*4 then -- op2 == IRMlit

503. local litn = litname[op]

504. if litn and litn[op2] then

505. out:write("  ", litn[op2])

506. elseif op == "UREFO " or op == "UREFC " then

507. out:write(format("  #%-3d", shr(op2, 8)))

508. else

509. out:write(format("  #%-3d", op2))

510. end

511. elseif op2 < 0 then

512. out:write("  ", formatk(tr, op2))

513. else

514. out:write(format("  %04d", op2))

515. end

516. end

517. end

518. out:write("\n")

519. end

520. end

521. if snap then

522. if dumpreg then

523. out:write(format("....              SNAP   #%-3d [ ", snapno))

524. else

525. out:write(format("....        SNAP   #%-3d [ ", snapno))

526. end

527. printsnap(tr, snap)

528. end

529. end

531. ------------------------------------------------------------------------------

533. local recprefix = ""

534. local recdepth = 0

536. -- Format trace error message.

537. ‌local function fmterr(err, info)

538. if type(err) == "number" then

539. ‌    if type(info) == "function" then info = fmtfunc(info) end

540. err = format(vmdef.traceerr[err], info)

541. end

542. return err

543. end

545. -- Dump trace states.

546. ‌local function dump_trace(what, tr, func, pc, otr, oex)

547. if what == "stop" or (what == "abort" and dumpmode.a) then

548. if dumpmode.i then dump_ir(tr, dumpmode.s, dumpmode.r and what == "stop")

549. elseif dumpmode.s then dump_snap(tr) end

550. if dumpmode.m then dump_mcode(tr) end

551. end

552. if what == "start" then

553. if dumpmode.H then out:write('<pre class="ljdump">\n') end

554. out:write("---- TRACE ", tr, " ", what)

555. if otr then out:write(" ", otr, "/", oex) end

556. out:write(" ", fmtfunc(func, pc), "\n")

557. elseif what == "stop" or what == "abort" then

558. out:write("---- TRACE ", tr, " ", what)

559. if what == "abort" then

560. out:write(" ", fmtfunc(func, pc), " -- ", fmterr(otr, oex), "\n")

561. else

562. local info = traceinfo(tr)

563. local link, ltype = info.link, info.linktype

564. if link == tr or link == 0 then

565. out:write(" -> ", ltype, "\n")

566. elseif ltype == "root" then

567. out:write(" -> ", link, "\n")

568. else

569. out:write(" -> ", link, " ", ltype, "\n")

570. end

571. end

572. if dumpmode.H then out:write("</pre>\n\n") else out:write("\n") end

573. else

574. out:write("---- TRACE ", what, "\n\n")

575. end

576. out:flush()

577. end

579. -- Dump recorded bytecode.

580. ‌local function dump_record(tr, func, pc, depth, callee)

581. if depth ~= recdepth then

582. recdepth = depth

583. recprefix = rep(" .", depth)

584. end

585. local line

586. if pc >= 0 then

587. line = bcline(func, pc, recprefix)

588. if dumpmode.H then line = gsub(line, "[<>&]", html_escape) end

589. else

590. line = "0000 "..recprefix.." FUNCC      \n"

591. callee = func

592. end

593. if pc <= 0 then

594. out:write(sub(line, 1, -2), "         ; ", fmtfunc(func), "\n")

595. else

596. out:write(line)

597. end

598. if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC

599. out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond.

600. end

601. end

603. ------------------------------------------------------------------------------

605. -- Dump taken trace exits.

606. ‌local function dump_texit(tr, ex, ngpr, nfpr, ...)

607. out:write("---- TRACE ", tr, " exit ", ex, "\n")

608. if dumpmode.X then

609. local regs = {...}

610. if jit.arch == "x64" then

611. for i=1,ngpr do

612. out:write(format(" %016x", regs[i]))

613. if i % 4 == 0 then out:write("\n") end

614. end

615. else

616. for i=1,ngpr do

617. out:write(" ", tohex(regs[i]))

618. if i % 8 == 0 then out:write("\n") end

619. end

620. end

621. if jit.arch == "mips" or jit.arch == "mipsel" then

622. for i=1,nfpr,2 do

623. out:write(format(" %+17.14g", regs[ngpr+i]))

624. if i % 8 == 7 then out:write("\n") end

625. end

626. else

627. for i=1,nfpr do

628. out:write(format(" %+17.14g", regs[ngpr+i]))

629. if i % 4 == 0 then out:write("\n") end

630. end

631. end

632. end

633. end

635. ------------------------------------------------------------------------------

637. -- Detach dump handlers.

638. ‌local function dumpoff()

639. if active then

640. active = false

641. jit.attach(dump_texit)

642. jit.attach(dump_record)

643. jit.attach(dump_trace)

644. if out and out ~= stdout and out ~= stderr then out:close() end

645. out = nil

646. end

647. end

649. -- Open the output file and attach dump handlers.

650. ‌local function dumpon(opt, outfile)

651. if active then dumpoff() end

653. local colormode = os.getenv("COLORTERM") and "A" or "T"

654. if opt then

655. ‌    opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end)

656. end

658. local m = { t=true, b=true, i=true, m=true, }

659. if opt and opt ~= "" then

660. local o = sub(opt, 1, 1)

661. if o ~= "+" and o ~= "-" then m = {} end

662. for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end

663. end

664. dumpmode = m

666. if m.t or m.b or m.i or m.s or m.m then

667. jit.attach(dump_trace, "trace")

668. end

669. if m.b then

670. jit.attach(dump_record, "record")

671. if not bcline then bcline = require("jit.bc").line end

672. end

673. if m.x or m.X then

674. jit.attach(dump_texit, "texit")

675. end

677. if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end

678. if outfile then

679. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))

680. else

681. out = stdout

682. end

684. m[colormode] = true

685. if colormode == "A" then

686. colorize = colorize_ansi

687. irtype = irtype_ansi

688. elseif colormode == "H" then

689. colorize = colorize_html

690. irtype = irtype_html

691. out:write(header_html)

692. else

693. colorize = colorize_text

694. irtype = irtype_text

695. end

697. active = true

698. end

700. -- Public module functions.

701. return {

702. on = dumpon,

703. off = dumpoff,

704. start = dumpon -- For -j command line option.

705. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/v.lua - luajit-2.0-src

Functions defined

- dump_trace

- dumpoff

- dumpon

- fmterr

- fmtfunc

- if type(info)

Source code

1. ----------------------------------------------------------------------------

2. -- Verbose mode of the LuaJIT compiler.

3. --

4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.

5. -- Released under the MIT license. See Copyright Notice in luajit.h

6. ----------------------------------------------------------------------------

7. --

8. -- This module shows verbose information about the progress of the

9. -- JIT compiler. It prints one line for each generated trace. This module

10. -- is useful to see which code has been compiled or where the compiler

11. -- punts and falls back to the interpreter.

12. --

13. -- Example usage:

14. --

15. --   luajit -jv -e "for i=1,1000 do for j=1,1000 do end end"

16. --   luajit -jv=myapp.out myapp.lua

17. --

18. -- Default output is to stderr. To redirect the output to a file, pass a

19. -- filename as an argument (use '-' for stdout) or set the environment

20. -- variable LUAJIT_VERBOSEFILE. The file is overwritten every time the

21. -- module is started.

22. --

23. -- The output from the first example should look like this:

24. --

25. -- [TRACE   1 (command line):1 loop]

26. -- [TRACE   2 (1/3) (command line):1 -> 1]

27. --

28. -- The first number in each line is the internal trace number. Next are

29. -- the file name ('(command line)') and the line number (':1') where the

30. -- trace has started. Side traces also show the parent trace number and

31. -- the exit number where they are attached to in parentheses ('(1/3)').

32. -- An arrow at the end shows where the trace links to ('-> 1'), unless

33. -- it loops to itself.

34. --

35. -- In this case the inner loop gets hot and is traced first, generating

36. -- a root trace. Then the last exit from the 1st trace gets hot, too,

37. -- and triggers generation of the 2nd trace. The side trace follows the

38. -- path along the outer loop and *around* the inner loop, back to its

39. -- start, and then links to the 1st trace. Yes, this may seem unusual,

40. -- if you know how traditional compilers work. Trace compilers are full

41. -- of surprises like this -- have fun! :-)

42. --

43. -- Aborted traces are shown like this:

44. --

45. -- [TRACE --- foo.lua:44 -- leaving loop in root trace at foo:lua:50]

46. --

47. -- Don't worry -- trace aborts are quite common, even in programs which

48. -- can be fully compiled. The compiler may retry several times until it

49. -- finds a suitable trace.

50. --

51. -- Of course this doesn't work with features that are not-yet-implemented

52. -- (NYI error messages). The VM simply falls back to the interpreter. This

53. -- may not matter at all if the particular trace is not very high up in

54. -- the CPU usage profile. Oh, and the interpreter is quite fast, too.

55. --

56. -- Also check out the -jdump module, which prints all the gory details.

57. --

58. ------------------------------------------------------------------------------

60. -- Cache some library functions and objects.

61. local jit = require("jit")

62. assert(jit.version_num == 20100, "LuaJIT core/library version mismatch")

63. local jutil = require("jit.util")

64. local vmdef = require("jit.vmdef")

65. local funcinfo, traceinfo = jutil.funcinfo, jutil.traceinfo

66. local type, format = type, string.format

67. local stdout, stderr = io.stdout, io.stderr

69. -- Active flag and output file handle.

70. local active, out

72. ------------------------------------------------------------------------------

74. local startloc, startex

76. ‌local function fmtfunc(func, pc)

77. local fi = funcinfo(func, pc)

78. if fi.loc then

79. return fi.loc

80. elseif fi.ffid then

81. return vmdef.ffnames[fi.ffid]

82. elseif fi.addr then

83. return format("C:%x", fi.addr)

84. else

85. return "(?)"

86. end

87. end

89. -- Format trace error message.

90. ‌local function fmterr(err, info)

91. if type(err) == "number" then

92. ‌    if type(info) == "function" then info = fmtfunc(info) end

93. err = format(vmdef.traceerr[err], info)

94. end

95. return err

96. end

98. -- Dump trace states.

99. ‌local function dump_trace(what, tr, func, pc, otr, oex)

100. if what == "start" then

101. startloc = fmtfunc(func, pc)

102. startex = otr and "("..otr.."/"..oex..") " or ""

103. else

104. if what == "abort" then

105. local loc = fmtfunc(func, pc)

106. if loc ~= startloc then

107. out:write(format("[TRACE --- %s%s -- %s at %s]\n",

108. startex, startloc, fmterr(otr, oex), loc))

109. else

110. out:write(format("[TRACE --- %s%s -- %s]\n",

111. startex, startloc, fmterr(otr, oex)))

112. end

113. elseif what == "stop" then

114. local info = traceinfo(tr)

115. local link, ltype = info.link, info.linktype

116. if ltype == "interpreter" then

117. out:write(format("[TRACE %3s %s%s -- fallback to interpreter]\n",

118. tr, startex, startloc))

119. elseif ltype == "stitch" then

120. out:write(format("[TRACE %3s %s%s %s %s]\n",

121. tr, startex, startloc, ltype, fmtfunc(func, pc)))

122. elseif link == tr or link == 0 then

123. out:write(format("[TRACE %3s %s%s %s]\n",

124. tr, startex, startloc, ltype))

125. elseif ltype == "root" then

126. out:write(format("[TRACE %3s %s%s -> %d]\n",

127. tr, startex, startloc, link))

128. else

129. out:write(format("[TRACE %3s %s%s -> %d %s]\n",

130. tr, startex, startloc, link, ltype))

131. end

132. else

133. out:write(format("[TRACE %s]\n", what))

134. end

135. out:flush()

136. end

137. end

139. ------------------------------------------------------------------------------

141. -- Detach dump handlers.

142. ‌local function dumpoff()

143. if active then

144. active = false

145. jit.attach(dump_trace)

146. if out and out ~= stdout and out ~= stderr then out:close() end

147. out = nil

148. end

149. end

151. -- Open the output file and attach dump handlers.

152. ‌local function dumpon(outfile)

153. if active then dumpoff() end

154. if not outfile then outfile = os.getenv("LUAJIT_VERBOSEFILE") end

155. if outfile then

156. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))

157. else

158. out = stderr

159. end

160. jit.attach(dump_trace, "trace")

161. active = true

162. end

164. -- Public module functions.

165. return {

166. on = dumpon,

167. off = dumpoff,

168. start = dumpon -- For -j command line option.

169. }

One Level Up                               Top Level

One Level Up                               Top Level

src/jit/vmdef.lua - luajit-2.0-src

1. -- This is a generated file. DO NOT EDIT!

3. return {

5. bcnames = "ISLT  ISGE  ISLE  ISGT  ISEQV ISNEV ISEQS ISNES ISEQN ISNEN ISEQP ISNEP ISTC  ISFC  IST   ISF   ISTYPEISNUM MOV   NOT   UNM   LEN   ADDVN SUBVN MULVN DIVVN MODVN ADDNV SUBNV MULNV DIVNV MODNV ADDVV SUBVV MULVV DIVVV MODVV POW   CAT   KSTR  KCDATAKSHORTKNUM  KPRI  KNIL  UGET  USETV USETS USETN USETP UCLO  FNEW  TNEW  TDUP  GGET  GSET  TGETV TGETS TGETB TGETR TSETV TSETS TSETB TSETM TSETR CALLM CALL  CALLMTCALLT ITERC ITERN VARG  ISNEXTRETM  RET   RET0  RET1  FORI  JFORI FORL  IFORL JFORL ITERL IITERLJITERLLOOP  ILOOP JLOOP JMP   FUNCF IFUNCFJFUNCFFUNCV IFUNCVJFUNCVFUNCC FUNCCW",

7. irnames = "LT    GE    LE    GT    ULT   UGE   ULE   UGT   EQ    NE    ABC   RETF  NOP   BASE  PVAL  GCSTEPHIOP  LOOP  USE   PHI   RENAMEPROF  KPRI  KINT  KGC   KPTR  KKPTR KNULL KNUM  KINT64KSLOT BNOT  BSWAP BAND  BOR   BXOR  BSHL  BSHR  BSAR  BROL  BROR  ADD   SUB   MUL   DIV   MOD   POW   NEG   ABS   ATAN2 LDEXP MIN   MAX   FPMATHADDOV SUBOV MULOV AREF  HREFK HREF  NEWREFUREFO UREFC FREF  STRREFLREF  ALOAD HLOAD ULOAD FLOAD XLOAD SLOAD VLOAD ASTOREHSTOREUSTOREFSTOREXSTORESNEW  XSNEW TNEW  TDUP  CNEW  CNEWI BUFHDRBUFPUTBUFSTRTBAR  OBAR  XBAR  CONV  TOBIT TOSTR STRTO CALLN CALLA CALLL CALLS CALLXSCARG  ",

9. irfpm = { [0]="floor", "ceil", "trunc", "sqrt", "exp", "exp2", "log", "log2", "log10", "sin", "cos", "tan", "other", },

11. irfield = { [0]="str.len", "func.env", "func.pc", "func.ffid", "thread.env", "tab.meta", "tab.array", "tab.node", "tab.asize", "tab.hmask", "tab.nomm", "udata.meta", "udata.udtype", "udata.file", "cdata.ctypeid", "cdata.ptr", "cdata.int", "cdata.int64", "cdata.int64_4", },

13. ircall = {

14. [0]="lj_str_cmp",

15. "lj_str_find",

16. "lj_str_new",

17. "lj_strscan_num",

18. "lj_strfmt_int",

19. "lj_strfmt_num",

20. "lj_strfmt_char",

21. "lj_strfmt_putint",

22. "lj_strfmt_putnum",

23. "lj_strfmt_putquoted",

24. "lj_strfmt_putfxint",

25. "lj_strfmt_putfnum_int",

26. "lj_strfmt_putfnum_uint",

27. "lj_strfmt_putfnum",

28. "lj_strfmt_putfstr",

29. "lj_strfmt_putfchar",

30. "lj_buf_putmem",

31. "lj_buf_putstr",

32. "lj_buf_putchar",

33. "lj_buf_putstr_reverse",

34. "lj_buf_putstr_lower",

35. "lj_buf_putstr_upper",

36. "lj_buf_putstr_rep",

37. "lj_buf_puttab",

38. "lj_buf_tostr",

39. "lj_tab_new_ah",

40. "lj_tab_new1",

41. "lj_tab_dup",

42. "lj_tab_clear",

43. "lj_tab_newkey",

44. "lj_tab_len",

45. "lj_gc_step_jit",

46. "lj_gc_barrieruv",

47. "lj_mem_newgco",

48. "lj_math_random_step",

49. "lj_vm_modi",

50. "sinh",

51. "cosh",

52. "tanh",

53. "fputc",

54. "fwrite",

55. "fflush",

56. "lj_vm_floor",

57. "lj_vm_ceil",

58. "lj_vm_trunc",

59. "sqrt",

60. "exp",

61. "lj_vm_exp2",

62. "log",

63. "lj_vm_log2",

64. "log10",

65. "sin",

66. "cos",

67. "tan",

68. "lj_vm_powi",

69. "pow",

70. "atan2",

71. "ldexp",

72. "lj_vm_tobit",

73. "softfp_add",

74. "softfp_sub",

75. "softfp_mul",

76. "softfp_div",

77. "softfp_cmp",

78. "softfp_i2d",

79. "softfp_d2i",

80. "softfp_ui2d",

81. "softfp_f2d",

82. "softfp_d2ui",

83. "softfp_d2f",

84. "softfp_i2f",

85. "softfp_ui2f",

86. "softfp_f2i",

87. "softfp_f2ui",

88. "fp64_l2d",

89. "fp64_ul2d",

90. "fp64_l2f",

91. "fp64_ul2f",

92. "fp64_d2l",

93. "fp64_d2ul",

94. "fp64_f2l",

95. "fp64_f2ul",

96. "lj_carith_divi64",

97. "lj_carith_divu64",

98. "lj_carith_modi64",

99. "lj_carith_modu64",

100. "lj_carith_powi64",

101. "lj_carith_powu64",

102. "lj_cdata_newv",

103. "lj_cdata_setfin",

104. "strlen",

105. "memcpy",

106. "memset",

107. "lj_vm_errno",

108. "lj_carith_mul64",

109. "lj_carith_shl64",

110. "lj_carith_shr64",

111. "lj_carith_sar64",

112. "lj_carith_rol64",

113. "lj_carith_ror64",

114. },

116. traceerr = {

117. [0]="error thrown or hook called during recording",

118. "trace too short",

119. "trace too long",

120. "trace too deep",

121. "too many snapshots",

122. "blacklisted",

123. "NYI: bytecode %d",

124. "leaving loop in root trace",

125. "inner loop in root trace",

126. "loop unroll limit reached",

127. "bad argument type",

128. "JIT compilation disabled for function",

129. "call unroll limit reached",

130. "down-recursion, restarting",

131. "NYI: unsupported variant of FastFunc %s",

132. "NYI: return to lower frame",

133. "store with nil or NaN key",

134. "missing metamethod",

135. "looping index lookup",

136. "NYI: mixed sparse/dense table",

137. "symbol not in cache",

138. "NYI: unsupported C type conversion",

139. "NYI: unsupported C function type",

140. "guard would always fail",

141. "too many PHIs",

142. "persistent type instability",

143. "failed to allocate mcode memory",

144. "machine code too long",

145. "hit mcode limit (retrying)",

146. "too many spill slots",

147. "inconsistent register allocation",

148. "NYI: cannot assemble IR instruction %d",

149. "NYI: PHI shuffling too complex",

150. "NYI: register coalescing too complex",

151. },

153. ffnames = {

154. [0]="Lua",

155. "C",

156. "assert",

157. "type",

158. "next",

159. "pairs",

160. "ipairs_aux",

161. "ipairs",

162. "getmetatable",

163. "setmetatable",

164. "getfenv",

165. "setfenv",

166. "rawget",

167. "rawset",

168. "rawequal",

169. "unpack",

170. "select",

171. "tonumber",

172. "tostring",

173. "error",

174. "pcall",

175. "xpcall",

176. "loadfile",

177. "load",

178. "loadstring",

179. "dofile",

180. "gcinfo",

181. "collectgarbage",

182. "newproxy",

183. "print",

184. "coroutine.status",

185. "coroutine.running",

186. "coroutine.create",

187. "coroutine.yield",

188. "coroutine.resume",

189. "coroutine.wrap_aux",

190. "coroutine.wrap",

191. "math.abs",

192. "math.floor",

193. "math.ceil",

194. "math.sqrt",

195. "math.log10",

196. "math.exp",

197. "math.sin",

198. "math.cos",

199. "math.tan",

200. "math.asin",

201. "math.acos",

202. "math.atan",

203. "math.sinh",

204. "math.cosh",

205. "math.tanh",

206. "math.frexp",

207. "math.modf",

208. "math.log",

209. "math.atan2",

210. "math.pow",

211. "math.fmod",

212. "math.ldexp",

213. "math.min",

214. "math.max",

215. "math.random",

216. "math.randomseed",

217. "bit.tobit",

218. "bit.bnot",

219. "bit.bswap",

220. "bit.lshift",

221. "bit.rshift",

222. "bit.arshift",

223. "bit.rol",

224. "bit.ror",

225. "bit.band",

226. "bit.bor",

227. "bit.bxor",

228. "bit.tohex",

229. "string.byte",

230. "string.char",

231. "string.sub",

232. "string.rep",

233. "string.reverse",

234. "string.lower",

235. "string.upper",

236. "string.dump",

237. "string.find",

238. "string.match",

239. "string.gmatch_aux",

240. "string.gmatch",

241. "string.gsub",

242. "string.format",

243. "table.maxn",

244. "table.insert",

245. "table.concat",

246. "table.sort",

247. "table.new",

248. "table.clear",

249. "io.method.close",

250. "io.method.read",

251. "io.method.write",

252. "io.method.flush",

253. "io.method.seek",

254. "io.method.setvbuf",

255. "io.method.lines",

256. "io.method.__gc",

257. "io.method.__tostring",

258. "io.open",

259. "io.popen",

260. "io.tmpfile",

261. "io.close",

262. "io.read",

263. "io.write",

264. "io.flush",

265. "io.input",

266. "io.output",

267. "io.lines",

268. "io.type",

269. "os.execute",

270. "os.remove",

271. "os.rename",

272. "os.tmpname",

273. "os.getenv",

274. "os.exit",

275. "os.clock",

276. "os.date",

277. "os.time",

278. "os.difftime",

279. "os.setlocale",

280. "debug.getregistry",

281. "debug.getmetatable",

282. "debug.setmetatable",

283. "debug.getfenv",

284. "debug.setfenv",

285. "debug.getinfo",

286. "debug.getlocal",

287. "debug.setlocal",

288. "debug.getupvalue",

289. "debug.setupvalue",

290. "debug.upvalueid",

291. "debug.upvaluejoin",

292. "debug.sethook",

293. "debug.gethook",

294. "debug.debug",

295. "debug.traceback",

296. "jit.on",

297. "jit.off",

298. "jit.flush",

299. "jit.status",

300. "jit.attach",

301. "jit.util.funcinfo",

302. "jit.util.funcbc",

303. "jit.util.funck",

304. "jit.util.funcuvname",

305. "jit.util.traceinfo",

306. "jit.util.traceir",

307. "jit.util.tracek",

308. "jit.util.tracesnap",

309. "jit.util.tracemc",

310. "jit.util.traceexitstub",

311. "jit.util.ircalladdr",

312. "jit.opt.start",

313. "jit.profile.start",

314. "jit.profile.stop",

315. "jit.profile.dumpstack",

316. "ffi.meta.__index",

317. "ffi.meta.__newindex",

318. "ffi.meta.__eq",

319. "ffi.meta.__len",

320. "ffi.meta.__lt",

321. "ffi.meta.__le",

322. "ffi.meta.__concat",

323. "ffi.meta.__call",

324. "ffi.meta.__add",

325. "ffi.meta.__sub",

326. "ffi.meta.__mul",

327. "ffi.meta.__div",

328. "ffi.meta.__mod",

329. "ffi.meta.__pow",

330. "ffi.meta.__unm",

331. "ffi.meta.__tostring",

332. "ffi.meta.__pairs",

333. "ffi.meta.__ipairs",

334. "ffi.clib.__index",

335. "ffi.clib.__newindex",

336. "ffi.clib.__gc",

337. "ffi.callback.free",

338. "ffi.callback.set",

339. "ffi.cdef",

340. "ffi.new",

341. "ffi.cast",

342. "ffi.typeof",

343. "ffi.typeinfo",

344. "ffi.istype",

345. "ffi.sizeof",

346. "ffi.alignof",

347. "ffi.offsetof",

348. "ffi.errno",

349. "ffi.string",

350. "ffi.copy",

351. "ffi.fill",

352. "ffi.abi",

353. "ffi.metatype",

354. "ffi.gc",

355. "ffi.load",

356. },

358. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_os.c - luajit-2.0-src

Functions defined

- LJLIB_CF(os_execute)

- LJLIB_CF(os_remove)

- LJLIB_CF(os_rename)

- LJLIB_CF(os_tmpname)

- LJLIB_CF(os_getenv)

- LJLIB_CF(os_exit)

- LJLIB_CF(os_clock)

- LJLIB_CF(os_date)

- LJLIB_CF(os_time)

- LJLIB_CF(os_difftime)

- LJLIB_CF(os_setlocale)

- getboolfield

- getfield

- luaopen_os

- setboolfield

- setfield

Macros defined

- LJLIB_MODULE_os

- LUA_LIB

- lib_os_c

Source code

1. /*

2. ** OS library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. #include <errno.h>

10. #include <time.h>

12. ‌#define lib_os_c

13. ‌#define LUA_LIB

15. #include "lua.h"

16. #include "lauxlib.h"

17. #include "lualib.h"

19. #include "lj_obj.h"

20. #include "lj_gc.h"

21. #include "lj_err.h"

22. #include "lj_buf.h"

23. #include "lj_str.h"

24. #include "lj_lib.h"

26. #if LJ_TARGET_POSIX

27. #include <unistd.h>

28. #else

29. #include <stdio.h>

30. #endif

32. #if !LJ_TARGET_PSVITA

33. #include <locale.h>

34. #endif

36. /* ------------------------------------------------------------------------ */

38. ‌#define LJLIB_MODULE_os

40. ‌LJLIB_CF(os_execute)

41. {

42. #if LJ_TARGET_CONSOLE

43. #if LJ_52

44. errno = ENOSYS;

45. return luaL_fileresult(L, 0, NULL);

46. #else

47. lua_pushinteger(L, -1);

48. return 1;

49. #endif

50. #else

51. const char *cmd = luaL_optstring(L, 1, NULL);

52. int stat = system(cmd);

53. #if LJ_52

54. if (cmd)

55. return luaL_execresult(L, stat);

56. setboolV(L->top++, 1);

57. #else

58. setintV(L->top++, stat);

59. #endif

60. return 1;

61. #endif

62. }

64. ‌LJLIB_CF(os_remove)

65. {

66. const char *filename = luaL_checkstring(L, 1);

67. return luaL_fileresult(L, remove(filename) == 0, filename);

68. }

70. ‌LJLIB_CF(os_rename)

71. {

72. const char *fromname = luaL_checkstring(L, 1);

73. const char *toname = luaL_checkstring(L, 2);

74. return luaL_fileresult(L, rename(fromname, toname) == 0, fromname);

75. }

77. ‌LJLIB_CF(os_tmpname)

78. {

79. #if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PSVITA

80. lj_err_caller(L, LJ_ERR_OSUNIQF);

81. return 0;

82. #else

83. #if LJ_TARGET_POSIX

84. char buf[15+1];

85. int fp;

86. strcpy(buf, "/tmp/lua_XXXXXX");

87. fp = mkstemp(buf);

88. if (fp != -1)

89. close(fp);

90. else

91. lj_err_caller(L, LJ_ERR_OSUNIQF);

92. #else

93. char buf[L_tmpnam];

94. if (tmpnam(buf) == NULL)

95. lj_err_caller(L, LJ_ERR_OSUNIQF);

96. #endif

97. lua_pushstring(L, buf);

98. return 1;

99. #endif

100. }

102. ‌LJLIB_CF(os_getenv)

103. {

104. #if LJ_TARGET_CONSOLE

105. lua_pushnil(L);

106. #else

107. lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */

108. #endif

109. return 1;

110. }

112. ‌LJLIB_CF(os_exit)

113. {

114. int status;

115. if (L->base < L->top && tvisbool(L->base))

116. status = boolV(L->base) ? EXIT_SUCCESS : EXIT_FAILURE;

117. else

118. status = lj_lib_optint(L, 1, EXIT_SUCCESS);

119. if (L->base+1 < L->top && tvistruecond(L->base+1))

120. lua_close(L);

121. exit(status);

122. return 0;  /* Unreachable. */

123. }

125. ‌LJLIB_CF(os_clock)

126. {

127. setnumV(L->top++, ((lua_Number)clock())*(1.0/(lua_Number)CLOCKS_PER_SEC));

128. return 1;

129. }

131. /* ------------------------------------------------------------------------ */

133. ‌static void setfield(lua_State *L, const char *key, int value)

134. {

135. lua_pushinteger(L, value);

136. lua_setfield(L, -2, key);

137. }

139. ‌static void setboolfield(lua_State *L, const char *key, int value)

140. {

141. if (value < 0)  /* undefined? */

142. return;  /* does not set field */

143. lua_pushboolean(L, value);

144. lua_setfield(L, -2, key);

145. }

147. ‌static int getboolfield(lua_State *L, const char *key)

148. {

149. int res;

150. lua_getfield(L, -1, key);

151. res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);

152. lua_pop(L, 1);

153. return res;

154. }

156. ‌static int getfield(lua_State *L, const char *key, int d)

157. {

158. int res;

159. lua_getfield(L, -1, key);

160. if (lua_isnumber(L, -1)) {

161. res = (int)lua_tointeger(L, -1);

162. } else {

163. if (d < 0)

164. lj_err_callerv(L, LJ_ERR_OSDATEF, key);

165. res = d;

166. }

167. lua_pop(L, 1);

168. return res;

169. }

171. ‌LJLIB_CF(os_date)

172. {

173. const char *s = luaL_optstring(L, 1, "%c");

174. time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));

175. struct tm *stm;

176. #if LJ_TARGET_POSIX

177. struct tm rtm;

178. #endif

179. if (*s == '!') {  /* UTC? */

180. s++;  /* Skip '!' */

181. #if LJ_TARGET_POSIX

182. stm = gmtime_r(&t, &rtm);

183. #else

184. stm = gmtime(&t);

185. #endif

186. } else {

187. #if LJ_TARGET_POSIX

188. stm = localtime_r(&t, &rtm);

189. #else

190. stm = localtime(&t);

191. #endif

192. }

193. if (stm == NULL) {  /* Invalid date? */

194. setnilV(L->top++);

195. } else if (strcmp(s, "*t") == 0) {

196. lua_createtable(L, 0, 9);  /* 9 = number of fields */

197. setfield(L, "sec", stm->tm_sec);

198. setfield(L, "min", stm->tm_min);

199. setfield(L, "hour", stm->tm_hour);

200. setfield(L, "day", stm->tm_mday);

201. setfield(L, "month", stm->tm_mon+1);

202. setfield(L, "year", stm->tm_year+1900);

203. setfield(L, "wday", stm->tm_wday+1);

204. setfield(L, "yday", stm->tm_yday+1);

205. setboolfield(L, "isdst", stm->tm_isdst);

206. } else if (*s) {

207. SBuf *sb = &G(L)->tmpbuf;

208. MSize sz = 0;

209. const char *q;

210. for (q = s; *q; q++)

211. sz += (*q == '%') ? 30 : 1;  /* Overflow doesn't matter. */

212. setsbufL(sb, L);

213. for (;;) {

214. char *buf = lj_buf_need(sb, sz);

215. size_t len = strftime(buf, sbufsz(sb), s, stm);

216. if (len) {

217. setstrV(L, L->top++, lj_str_new(L, buf, len));

218. lj_gc_check(L);

219. break;

220. }

221. sz += (sz|1);

222. }

223. } else {

224. setstrV(L, L->top++, &G(L)->strempty);

225. }

226. return 1;

227. }

229. ‌LJLIB_CF(os_time)

230. {

231. time_t t;

232. if (lua_isnoneornil(L, 1)) {  /* called without args? */

233. t = time(NULL);  /* get current time */

234. } else {

235. struct tm ts;

236. luaL_checktype(L, 1, LUA_TTABLE);

237. lua_settop(L, 1);  /* make sure table is at the top */

238. ts.tm_sec = getfield(L, "sec", 0);

239. ts.tm_min = getfield(L, "min", 0);

240. ts.tm_hour = getfield(L, "hour", 12);

241. ts.tm_mday = getfield(L, "day", -1);

242. ts.tm_mon = getfield(L, "month", -1) - 1;

243. ts.tm_year = getfield(L, "year", -1) - 1900;

244. ts.tm_isdst = getboolfield(L, "isdst");

245. t = mktime(&ts);

246. }

247. if (t == (time_t)(-1))

248. lua_pushnil(L);

249. else

250. lua_pushnumber(L, (lua_Number)t);

251. return 1;

252. }

254. ‌LJLIB_CF(os_difftime)

255. {

256. lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),

257. (time_t)(luaL_optnumber(L, 2, (lua_Number)0))));

258. return 1;

259. }

261. /* ------------------------------------------------------------------------ */

263. ‌LJLIB_CF(os_setlocale)

264. {

265. #if LJ_TARGET_PSVITA

266. lua_pushliteral(L, "C");

267. #else

268. GCstr *s = lj_lib_optstr(L, 1);

269. const char *str = s ? strdata(s) : NULL;

270. int opt = lj_lib_checkopt(L, 2, 6,

271. "\5ctype\7numeric\4time\7collate\10monetary\1\377\3all");

272. if (opt == 0) opt = LC_CTYPE;

273. else if (opt == 1) opt = LC_NUMERIC;

274. else if (opt == 2) opt = LC_TIME;

275. else if (opt == 3) opt = LC_COLLATE;

276. else if (opt == 4) opt = LC_MONETARY;

277. else if (opt == 6) opt = LC_ALL;

278. lua_pushstring(L, setlocale(opt, str));

279. #endif

280. return 1;

281. }

283. /* ------------------------------------------------------------------------ */

285. #include "lj_libdef.h"

287. ‌LUALIB_API int luaopen_os(lua_State *L)

288. {

289. LJ_LIB_REG(L, LUA_OSLIBNAME, os);

290. return 1;

291. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lib_table.c - luajit-2.0-src

Functions defined

- LJLIB_CF(table_insert) LJLIB_REC(.)

- LJLIB_CF(table_sort)

- LJLIB_CF(table_pack)

- LJLIB_NOREG LJLIB_CF(table_new) LJLIB_REC(.)

- LJLIB_NOREG LJLIB_CF(table_clear) LJLIB_REC(.)

- LJLIB_LUA(table_foreachi) /*

- LJLIB_LUA(table_remove) /*

- auxsort

- luaopen_table

- luaopen_table_clear

- luaopen_table_new

- set2

- sort_comp

Macros defined

- LJLIB_MODULE_table

- LUA_LIB

- lib_table_c

Source code

1. /*

2. ** Table library.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. **

5. ** Major portions taken verbatim or adapted from the Lua interpreter.

6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h

7. */

9. ‌#define lib_table_c

10. ‌#define LUA_LIB

12. #include "lua.h"

13. #include "lauxlib.h"

14. #include "lualib.h"

16. #include "lj_obj.h"

17. #include "lj_gc.h"

18. #include "lj_err.h"

19. #include "lj_buf.h"

20. #include "lj_tab.h"

21. #include "lj_ff.h"

22. #include "lj_lib.h"

24. /* ------------------------------------------------------------------------ */

26. ‌#define LJLIB_MODULE_table

28. ‌LJLIB_LUA(table_foreachi) /*

29. function(t, f)

30. CHECK_tab(t)

31. CHECK_func(f)

32. for i=1,#t do

33. local r = f(i, t[i])

34. if r ~= nil then return r end

35. end

36. end

37. */

39. LJLIB_LUA(table_foreach) /*

40. function(t, f)

41. CHECK_tab(t)

42. CHECK_func(f)

43. for k, v in PAIRS(t) do

44. local r = f(k, v)

45. if r ~= nil then return r end

46. end

47. end

48. */

50. LJLIB_LUA(table_getn) /*

51. function(t)

52. CHECK_tab(t)

53. return #t

54. end

55. */

57. LJLIB_CF(table_maxn)

58. {

59. GCtab *t = lj_lib_checktab(L, 1);

60. TValue *array = tvref(t->array);

61. Node *node;

62. lua_Number m = 0;

63. ptrdiff_t i;

64. for (i = (ptrdiff_t)t->asize - 1; i >= 0; i--)

65. if (!tvisnil(&array[i])) {

66. m = (lua_Number)(int32_t)i;

67. break;

68. }

69. node = noderef(t->node);

70. for (i = (ptrdiff_t)t->hmask; i >= 0; i--)

71. if (!tvisnil(&node[i].val) && tvisnumber(&node[i].key)) {

72. lua_Number n = numberVnum(&node[i].key);

73. if (n > m) m = n;

74. }

75. setnumV(L->top-1, m);

76. return 1;

77. }

79. ‌LJLIB_CF(table_insert)                LJLIB_REC(.)

80. {

81. GCtab *t = lj_lib_checktab(L, 1);

82. int32_t n, i = (int32_t)lj_tab_len(t) + 1;

83. int nargs = (int)((char *)L->top - (char *)L->base);

84. if (nargs != 2*sizeof(TValue)) {

85. if (nargs != 3*sizeof(TValue))

86. lj_err_caller(L, LJ_ERR_TABINS);

87. /* NOBARRIER: This just moves existing elements around. */

88. for (n = lj_lib_checkint(L, 2); i > n; i--) {

89. /* The set may invalidate the get pointer, so need to do it first! */

90. TValue *dst = lj_tab_setint(L, t, i);

91. cTValue *src = lj_tab_getint(t, i-1);

92. if (src) {

93. copyTV(L, dst, src);

94. } else {

95. setnilV(dst);

96. }

97. }

98. i = n;

99. }

100. {

101. TValue *dst = lj_tab_setint(L, t, i);

102. copyTV(L, dst, L->top-1);  /* Set new value. */

103. lj_gc_barriert(L, t, dst);

104. }

105. return 0;

106. }

108. ‌LJLIB_LUA(table_remove) /*

109. function(t, pos)

110. CHECK_tab(t)

111. local len = #t

112. if pos == nil then

113. if len ~= 0 then

114. local old = t[len]

115. t[len] = nil

116. return old

117. end

118. else

119. CHECK_int(pos)

120. if pos >= 1 and pos <= len then

121. local old = t[pos]

122. for i=pos+1,len do

123. t[i-1] = t[i]

124. end

125. t[len] = nil

126. return old

127. end

128. end

129. end

130. */

132. LJLIB_CF(table_concat)                LJLIB_REC(.)

133. {

134. GCtab *t = lj_lib_checktab(L, 1);

135. GCstr *sep = lj_lib_optstr(L, 2);

136. int32_t i = lj_lib_optint(L, 3, 1);

137. int32_t e = (L->base+3 < L->top && !tvisnil(L->base+3)) ?

138. lj_lib_checkint(L, 4) : (int32_t)lj_tab_len(t);

139. SBuf *sb = lj_buf_tmp_(L);

140. SBuf *sbx = lj_buf_puttab(sb, t, sep, i, e);

141. if (LJ_UNLIKELY(!sbx)) {  /* Error: bad element type. */

142. int32_t idx = (int32_t)(intptr_t)sbufP(sb);

143. cTValue *o = lj_tab_getint(t, idx);

144. lj_err_callerv(L, LJ_ERR_TABCAT,

145. lj_obj_itypename[o ? itypemap(o) : ~LJ_TNIL], idx);

146. }

147. setstrV(L, L->top-1, lj_buf_str(L, sbx));

148. lj_gc_check(L);

149. return 1;

150. }

152. /* ------------------------------------------------------------------------ */

154. ‌static void set2(lua_State *L, int i, int j)

155. {

156. lua_rawseti(L, 1, i);

157. lua_rawseti(L, 1, j);

158. }

160. ‌static int sort_comp(lua_State *L, int a, int b)

161. {

162. if (!lua_isnil(L, 2)) {  /* function? */

163. int res;

164. lua_pushvalue(L, 2);

165. lua_pushvalue(L, a-1);  /* -1 to compensate function */

166. lua_pushvalue(L, b-2);  /* -2 to compensate function and `a' */

167. lua_call(L, 2, 1);

168. res = lua_toboolean(L, -1);

169. lua_pop(L, 1);

170. return res;

171. } else {  /* a < b? */

172. return lua_lessthan(L, a, b);

173. }

174. }

176. ‌static void auxsort(lua_State *L, int l, int u)

177. {

178. while (l < u) {  /* for tail recursion */

179. int i, j;

180. /* sort elements a[l], a[(l+u)/2] and a[u] */

181. lua_rawgeti(L, 1, l);

182. lua_rawgeti(L, 1, u);

183. if (sort_comp(L, -1, -2))  /* a[u] < a[l]? */

184. set2(L, l, u);  /* swap a[l] - a[u] */

185. else

186. lua_pop(L, 2);

187. if (u-l == 1) break;  /* only 2 elements */

188. i = (l+u)/2;

189. lua_rawgeti(L, 1, i);

190. lua_rawgeti(L, 1, l);

191. if (sort_comp(L, -2, -1)) {  /* a[i]<a[l]? */

192. set2(L, i, l);

193. } else {

194. lua_pop(L, 1);  /* remove a[l] */

195. lua_rawgeti(L, 1, u);

196. if (sort_comp(L, -1, -2))  /* a[u]<a[i]? */

197. set2(L, i, u);

198. else

199. lua_pop(L, 2);

200. }

201. if (u-l == 2) break;  /* only 3 elements */

202. lua_rawgeti(L, 1, i);  /* Pivot */

203. lua_pushvalue(L, -1);

204. lua_rawgeti(L, 1, u-1);

205. set2(L, i, u-1);

206. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */

207. i = l; j = u-1;

208. for (;;) {  /* invariant: a[l..i] <= P <= a[j..u] */

209. /* repeat ++i until a[i] >= P */

210. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {

211. if (i>=u) lj_err_caller(L, LJ_ERR_TABSORT);

212. lua_pop(L, 1);  /* remove a[i] */

213. }

214. /* repeat --j until a[j] <= P */

215. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {

216. if (j<=l) lj_err_caller(L, LJ_ERR_TABSORT);

217. lua_pop(L, 1);  /* remove a[j] */

218. }

219. if (j<i) {

220. lua_pop(L, 3);  /* pop pivot, a[i], a[j] */

221. break;

222. }

223. set2(L, i, j);

224. }

225. lua_rawgeti(L, 1, u-1);

226. lua_rawgeti(L, 1, i);

227. set2(L, u-1, i);  /* swap pivot (a[u-1]) with a[i] */

228. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */

229. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */

230. if (i-l < u-i) {

231. j=l; i=i-1; l=i+2;

232. } else {

233. j=i+1; i=u; u=j-2;

234. }

235. auxsort(L, j, i);  /* call recursively the smaller one */

236. }  /* repeat the routine for the larger one */

237. }

239. ‌LJLIB_CF(table_sort)

240. {

241. GCtab *t = lj_lib_checktab(L, 1);

242. int32_t n = (int32_t)lj_tab_len(t);

243. lua_settop(L, 2);

244. if (!tvisnil(L->base+1))

245. lj_lib_checkfunc(L, 2);

246. auxsort(L, 1, n);

247. return 0;

248. }

250. #if LJ_52

251. LJLIB_PUSH("n")

252. ‌LJLIB_CF(table_pack)

253. {

254. TValue *array, *base = L->base;

255. MSize i, n = (uint32_t)(L->top - base);

256. GCtab *t = lj_tab_new(L, n ? n+1 : 0, 1);

257. /* NOBARRIER: The table is new (marked white). */

258. setintV(lj_tab_setstr(L, t, strV(lj_lib_upvalue(L, 1))), (int32_t)n);

259. for (array = tvref(t->array) + 1, i = 0; i < n; i++)

260. copyTV(L, &array[i], &base[i]);

261. settabV(L, base, t);

262. L->top = base+1;

263. lj_gc_check(L);

264. return 1;

265. }

266. #endif

268. ‌LJLIB_NOREG LJLIB_CF(table_new)                LJLIB_REC(.)

269. {

270. int32_t a = lj_lib_checkint(L, 1);

271. int32_t h = lj_lib_checkint(L, 2);

272. lua_createtable(L, a, h);

273. return 1;

274. }

276. ‌LJLIB_NOREG LJLIB_CF(table_clear)        LJLIB_REC(.)

277. {

278. lj_tab_clear(lj_lib_checktab(L, 1));

279. return 0;

280. }

282. ‌static int luaopen_table_new(lua_State *L)

283. {

284. return lj_lib_postreg(L, lj_cf_table_new, FF_table_new, "new");

285. }

287. ‌static int luaopen_table_clear(lua_State *L)

288. {

289. return lj_lib_postreg(L, lj_cf_table_clear, FF_table_clear, "clear");

290. }

292. /* ------------------------------------------------------------------------ */

294. #include "lj_libdef.h"

296. ‌LUALIB_API int luaopen_table(lua_State *L)

297. {

298. LJ_LIB_REG(L, LUA_TABLIBNAME, table);

299. #if LJ_52

300. lua_getglobal(L, "unpack");

301. lua_setfield(L, -2, "unpack");

302. #endif

303. lj_lib_prereg(L, LUA_TABLIBNAME ".new", luaopen_table_new, tabV(L->top-1));

304. lj_lib_prereg(L, LUA_TABLIBNAME ".clear", luaopen_table_clear, tabV(L->top-1));

305. return 1;

306. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_alloc.h - luajit-2.0-src

Macros defined

- _LJ_ALLOC_H

Source code

1. /*

2. ** Bundled memory allocator.

3. ** Donated to the public domain.

4. */

6. #ifndef _LJ_ALLOC_H

7. ‌#define _LJ_ALLOC_H

9. #include "lj_def.h"

11. #ifndef LUAJIT_USE_SYSMALLOC

12. LJ_FUNC void *lj_alloc_create(void);

13. LJ_FUNC void lj_alloc_destroy(void *msp);

14. LJ_FUNC void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize);

15. #endif

17. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_asm.h - luajit-2.0-src

Macros defined

- _LJ_ASM_H

Source code

1. /*

2. ** IR assembler (SSA IR -> machine code).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_ASM_H

7. ‌#define _LJ_ASM_H

9. #include "lj_jit.h"

11. #if LJ_HASJIT

12. LJ_FUNC void lj_asm_trace(jit_State *J, GCtrace *T);

13. LJ_FUNC void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno,

14. MCode *target);

15. #endif

17. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_asm_mips.h - luajit-2.0-src

Functions defined

- asm_add

- asm_add64

- asm_ahustore

- asm_ahuvload

- asm_aref

- asm_arithov

- asm_bitop

- asm_bitshift

- asm_bnot

- asm_bror

- asm_bswap

- asm_callround

- asm_callx

- asm_cnew

- asm_comp

- asm_comp64

- asm_comp64eq

- asm_conv

- asm_equal

- asm_exitstub_setup

- asm_fload

- asm_fparith

- asm_fpmath

- asm_fpunary

- asm_fref

- asm_fstore

- asm_fuseabase

- asm_fuseahuref

- asm_fusexref

- asm_fxloadins

- asm_fxstoreins

- asm_gc_check

- asm_gencall

- asm_guard

- asm_head_root_base

- asm_head_side_base

- asm_hiop

- asm_href

- asm_hrefk

- asm_loop_fixup

- asm_min_max

- asm_mul

- asm_mulov

- asm_neg

- asm_neg64

- asm_obar

- asm_prof

- asm_retf

- asm_setup_call_slots

- asm_setup_target

- asm_setupresult

- asm_sload

- asm_sparejump_setup

- asm_stack_check

- asm_stack_restore

- asm_strref

- asm_strto

- asm_sub

- asm_sub64

- asm_tail_fixup

- asm_tail_prep

- asm_tbar

- asm_tobit

- asm_tointg

- asm_tvptr

- asm_uref

- asm_xload

- asm_xstore_

- lj_asm_patchexit

- noconflict

- ra_alloc1z

- ra_alloc2

- ra_hintalloc

Macros defined

- CONFLICT_SEARCH_LIM

- MIPS_SPAREJUMP

- asm_abs

- asm_addov

- asm_atan2

- asm_band

- asm_bor

- asm_brol

- asm_bsar

- asm_bshl

- asm_bshr

- asm_bxor

- asm_cnew

- asm_div

- asm_exitstub_addr

- asm_ldexp

- asm_max

- asm_min

- asm_mod

- asm_pow

- asm_subov

- asm_xstore

Source code

1. /*

2. ** MIPS IR assembler (SSA IR -> machine code).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* -- Register allocator extensions --------------------------------------- */

8. /* Allocate a register with a hint. */

9. ‌static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)

10. {

11. Reg r = IR(ref)->r;

12. if (ra_noreg(r)) {

13. if (!ra_hashint(r) && !iscrossref(as, ref))

14. ra_sethint(IR(ref)->r, hint);  /* Propagate register hint. */

15. r = ra_allocref(as, ref, allow);

16. }

17. ra_noweak(as, r);

18. return r;

19. }

21. /* Allocate a register or RID_ZERO. */

22. ‌static Reg ra_alloc1z(ASMState *as, IRRef ref, RegSet allow)

23. {

24. Reg r = IR(ref)->r;

25. if (ra_noreg(r)) {

26. if (!(allow & RSET_FPR) && irref_isk(ref) && IR(ref)->i == 0)

27. return RID_ZERO;

28. r = ra_allocref(as, ref, allow);

29. } else {

30. ra_noweak(as, r);

31. }

32. return r;

33. }

35. /* Allocate two source registers for three-operand instructions. */

36. ‌static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)

37. {

38. IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);

39. Reg left = irl->r, right = irr->r;

40. if (ra_hasreg(left)) {

41. ra_noweak(as, left);

42. if (ra_noreg(right))

43. right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left));

44. else

45. ra_noweak(as, right);

46. } else if (ra_hasreg(right)) {

47. ra_noweak(as, right);

48. left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right));

49. } else if (ra_hashint(right)) {

50. right = ra_alloc1z(as, ir->op2, allow);

51. left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right));

52. } else {

53. left = ra_alloc1z(as, ir->op1, allow);

54. right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left));

55. }

56. return left | (right << 8);

57. }

59. /* -- Guard handling ------------------------------------------------------ */

61. /* Need some spare long-range jump slots, for out-of-range branches. */

62. ‌#define MIPS_SPAREJUMP                4

64. /* Setup spare long-range jump slots per mcarea. */

65. ‌static void asm_sparejump_setup(ASMState *as)

66. {

67. MCode *mxp = as->mcbot;

68. /* Assumes sizeof(MCLink) == 8. */

69. if (((uintptr_t)mxp & (LJ_PAGESIZE-1)) == 8) {

70. lua_assert(MIPSI_NOP == 0);

71. memset(mxp+2, 0, MIPS_SPAREJUMP*8);

72. mxp += MIPS_SPAREJUMP*2;

73. lua_assert(mxp < as->mctop);

74. lj_mcode_sync(as->mcbot, mxp);

75. lj_mcode_commitbot(as->J, mxp);

76. as->mcbot = mxp;

77. as->mclim = as->mcbot + MCLIM_REDZONE;

78. }

79. }

81. /* Setup exit stub after the end of each trace. */

82. ‌static void asm_exitstub_setup(ASMState *as)

83. {

84. MCode *mxp = as->mctop;

85. /* sw TMP, 0(sp); j ->vm_exit_handler; li TMP, traceno */

86. *--mxp = MIPSI_LI|MIPSF_T(RID_TMP)|as->T->traceno;

87. *--mxp = MIPSI_J|((((uintptr_t)(void *)lj_vm_exit_handler)>>2)&0x03ffffffu);

88. lua_assert(((uintptr_t)mxp ^ (uintptr_t)(void *)lj_vm_exit_handler)>>28 == 0);

89. *--mxp = MIPSI_SW|MIPSF_T(RID_TMP)|MIPSF_S(RID_SP)|0;

90. as->mctop = mxp;

91. }

93. /* Keep this in-sync with exitstub_trace_addr(). */

94. ‌#define asm_exitstub_addr(as)        ((as)->mctop)

96. /* Emit conditional branch to exit for guard. */

97. ‌static void asm_guard(ASMState *as, MIPSIns mi, Reg rs, Reg rt)

98. {

99. MCode *target = asm_exitstub_addr(as);

100. MCode *p = as->mcp;

101. if (LJ_UNLIKELY(p == as->invmcp)) {

102. as->invmcp = NULL;

103. as->loopinv = 1;

104. as->mcp = p+1;

105. mi = mi ^ ((mi>>28) == 1 ? 0x04000000u : 0x00010000u);  /* Invert cond. */

106. target = p;  /* Patch target later in asm_loop_fixup. */

107. }

108. emit_ti(as, MIPSI_LI, RID_TMP, as->snapno);

109. emit_branch(as, mi, rs, rt, target);

110. }

112. /* -- Operand fusion ------------------------------------------------------ */

114. /* Limit linear search to this distance. Avoids O(n^2) behavior. */

115. ‌#define CONFLICT_SEARCH_LIM        31

117. /* Check if there's no conflicting instruction between curins and ref. */

118. ‌static int noconflict(ASMState *as, IRRef ref, IROp conflict)

119. {

120. IRIns *ir = as->ir;

121. IRRef i = as->curins;

122. if (i > ref + CONFLICT_SEARCH_LIM)

123. return 0;  /* Give up, ref is too far away. */

124. while (--i > ref)

125. if (ir[i].o == conflict)

126. return 0;  /* Conflict found. */

127. return 1;  /* Ok, no conflict. */

128. }

130. /* Fuse the array base of colocated arrays. */

131. ‌static int32_t asm_fuseabase(ASMState *as, IRRef ref)

132. {

133. IRIns *ir = IR(ref);

134. if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&

135. !neverfuse(as) && noconflict(as, ref, IR_NEWREF))

136. return (int32_t)sizeof(GCtab);

137. return 0;

138. }

140. /* Fuse array/hash/upvalue reference into register+offset operand. */

141. ‌static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow)

142. {

143. IRIns *ir = IR(ref);

144. if (ra_noreg(ir->r)) {

145. if (ir->o == IR_AREF) {

146. if (mayfuse(as, ref)) {

147. if (irref_isk(ir->op2)) {

148. IRRef tab = IR(ir->op1)->op1;

149. int32_t ofs = asm_fuseabase(as, tab);

150. IRRef refa = ofs ? tab : ir->op1;

151. ofs += 8*IR(ir->op2)->i;

152. if (checki16(ofs)) {

153. *ofsp = ofs;

154. return ra_alloc1(as, refa, allow);

155. }

156. }

157. }

158. } else if (ir->o == IR_HREFK) {

159. if (mayfuse(as, ref)) {

160. int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));

161. if (checki16(ofs)) {

162. *ofsp = ofs;

163. return ra_alloc1(as, ir->op1, allow);

164. }

165. }

166. } else if (ir->o == IR_UREFC) {

167. if (irref_isk(ir->op1)) {

168. GCfunc *fn = ir_kfunc(IR(ir->op1));

169. int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv);

170. int32_t jgl = (intptr_t)J2G(as->J);

171. if ((uint32_t)(ofs-jgl) < 65536) {

172. *ofsp = ofs-jgl-32768;

173. return RID_JGL;

174. } else {

175. *ofsp = (int16_t)ofs;

176. return ra_allock(as, ofs-(int16_t)ofs, allow);

177. }

178. }

179. }

180. }

181. *ofsp = 0;

182. return ra_alloc1(as, ref, allow);

183. }

185. /* Fuse XLOAD/XSTORE reference into load/store operand. */

186. ‌static void asm_fusexref(ASMState *as, MIPSIns mi, Reg rt, IRRef ref,

187. RegSet allow, int32_t ofs)

188. {

189. IRIns *ir = IR(ref);

190. Reg base;

191. if (ra_noreg(ir->r) && canfuse(as, ir)) {

192. if (ir->o == IR_ADD) {

193. int32_t ofs2;

194. if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) {

195. ref = ir->op1;

196. ofs = ofs2;

197. }

198. } else if (ir->o == IR_STRREF) {

199. int32_t ofs2 = 65536;

200. lua_assert(ofs == 0);

201. ofs = (int32_t)sizeof(GCstr);

202. if (irref_isk(ir->op2)) {

203. ofs2 = ofs + IR(ir->op2)->i;

204. ref = ir->op1;

205. } else if (irref_isk(ir->op1)) {

206. ofs2 = ofs + IR(ir->op1)->i;

207. ref = ir->op2;

208. }

209. if (!checki16(ofs2)) {

210. /* NYI: Fuse ADD with constant. */

211. Reg right, left = ra_alloc2(as, ir, allow);

212. right = (left >> 8); left &= 255;

213. emit_hsi(as, mi, rt, RID_TMP, ofs);

214. emit_dst(as, MIPSI_ADDU, RID_TMP, left, right);

215. return;

216. }

217. ofs = ofs2;

218. }

219. }

220. base = ra_alloc1(as, ref, allow);

221. emit_hsi(as, mi, rt, base, ofs);

222. }

224. /* -- Calls --------------------------------------------------------------- */

226. /* Generate a call to a C function. */

227. ‌static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)

228. {

229. uint32_t n, nargs = CCI_XNARGS(ci);

230. int32_t ofs = 16;

231. Reg gpr, fpr = REGARG_FIRSTFPR;

232. if ((void *)ci->func)

233. emit_call(as, (void *)ci->func);

234. for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++)

235. as->cost[gpr] = REGCOST(~0u, ASMREF_L);

236. gpr = REGARG_FIRSTGPR;

237. for (n = 0; n < nargs; n++) {  /* Setup args. */

238. IRRef ref = args[n];

239. if (ref) {

240. IRIns *ir = IR(ref);

241. if (irt_isfp(ir->t) && fpr <= REGARG_LASTFPR &&

242. !(ci->flags & CCI_VARARG)) {

243. lua_assert(rset_test(as->freeset, fpr));  /* Already evicted. */

244. ra_leftov(as, fpr, ref);

245. fpr += 2;

246. gpr += irt_isnum(ir->t) ? 2 : 1;

247. } else {

248. fpr = REGARG_LASTFPR+1;

249. if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1;

250. if (gpr <= REGARG_LASTGPR) {

251. lua_assert(rset_test(as->freeset, gpr));  /* Already evicted. */

252. if (irt_isfp(ir->t)) {

253. RegSet of = as->freeset;

254. Reg r;

255. /* Workaround to protect argument GPRs from being used for remat. */

256. as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1);

257. r = ra_alloc1(as, ref, RSET_FPR);

258. as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1));

259. if (irt_isnum(ir->t)) {

260. emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?0:1), r+1);

261. emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?1:0), r);

262. lua_assert(rset_test(as->freeset, gpr+1));  /* Already evicted. */

263. gpr += 2;

264. } else if (irt_isfloat(ir->t)) {

265. emit_tg(as, MIPSI_MFC1, gpr, r);

266. gpr++;

267. }

268. } else {

269. ra_leftov(as, gpr, ref);

270. gpr++;

271. }

272. } else {

273. Reg r = ra_alloc1z(as, ref, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);

274. if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;

275. emit_spstore(as, ir, r, ofs);

276. ofs += irt_isnum(ir->t) ? 8 : 4;

277. }

278. }

279. } else {

280. fpr = REGARG_LASTFPR+1;

281. if (gpr <= REGARG_LASTGPR)

282. gpr++;

283. else

284. ofs += 4;

285. }

286. checkmclim(as);

287. }

288. }

290. /* Setup result reg/sp for call. Evict scratch regs. */

291. ‌static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)

292. {

293. RegSet drop = RSET_SCRATCH;

294. int hiop = ((ir+1)->o == IR_HIOP);

295. if ((ci->flags & CCI_NOFPRCLOBBER))

296. drop &= ~RSET_FPR;

297. if (ra_hasreg(ir->r))

298. rset_clear(drop, ir->r);  /* Dest reg handled below. */

299. if (hiop && ra_hasreg((ir+1)->r))

300. rset_clear(drop, (ir+1)->r);  /* Dest reg handled below. */

301. ra_evictset(as, drop);  /* Evictions must be performed first. */

302. if (ra_used(ir)) {

303. lua_assert(!irt_ispri(ir->t));

304. if (irt_isfp(ir->t)) {

305. if ((ci->flags & CCI_CASTU64)) {

306. int32_t ofs = sps_scale(ir->s);

307. Reg dest = ir->r;

308. if (ra_hasreg(dest)) {

309. ra_free(as, dest);

310. ra_modified(as, dest);

311. emit_tg(as, MIPSI_MTC1, RID_RETHI, dest+1);

312. emit_tg(as, MIPSI_MTC1, RID_RETLO, dest);

313. }

314. if (ofs) {

315. emit_tsi(as, MIPSI_SW, RID_RETLO, RID_SP, ofs+(LJ_BE?4:0));

316. emit_tsi(as, MIPSI_SW, RID_RETHI, RID_SP, ofs+(LJ_BE?0:4));

317. }

318. } else {

319. ra_destreg(as, ir, RID_FPRET);

320. }

321. } else if (hiop) {

322. ra_destpair(as, ir);

323. } else {

324. ra_destreg(as, ir, RID_RET);

325. }

326. }

327. }

329. ‌static void asm_callx(ASMState *as, IRIns *ir)

330. {

331. IRRef args[CCI_NARGS_MAX*2];

332. CCallInfo ci;

333. IRRef func;

334. IRIns *irf;

335. ci.flags = asm_callx_flags(as, ir);

336. asm_collectargs(as, ir, &ci, args);

337. asm_setupresult(as, ir, &ci);

338. func = ir->op2; irf = IR(func);

339. if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }

340. if (irref_isk(func)) {  /* Call to constant address. */

341. ci.func = (ASMFunction)(void *)(irf->i);

342. } else {  /* Need specific register for indirect calls. */

343. Reg r = ra_alloc1(as, func, RID2RSET(RID_CFUNCADDR));

344. MCode *p = as->mcp;

345. if (r == RID_CFUNCADDR)

346. *--p = MIPSI_NOP;

347. else

348. *--p = MIPSI_MOVE | MIPSF_D(RID_CFUNCADDR) | MIPSF_S(r);

349. *--p = MIPSI_JALR | MIPSF_S(r);

350. as->mcp = p;

351. ci.func = (ASMFunction)(void *)0;

352. }

353. asm_gencall(as, &ci, args);

354. }

356. ‌static void asm_callround(ASMState *as, IRIns *ir, IRCallID id)

357. {

358. /* The modified regs must match with the *.dasc implementation. */

359. RegSet drop = RID2RSET(RID_R1)|RID2RSET(RID_R12)|RID2RSET(RID_FPRET)|

360. RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(REGARG_FIRSTFPR);

361. if (ra_hasreg(ir->r)) rset_clear(drop, ir->r);

362. ra_evictset(as, drop);

363. ra_destreg(as, ir, RID_FPRET);

364. emit_call(as, (void *)lj_ir_callinfo[id].func);

365. ra_leftov(as, REGARG_FIRSTFPR, ir->op1);

366. }

368. /* -- Returns ------------------------------------------------------------- */

370. /* Return to lower frame. Guard that it goes to the right spot. */

371. ‌static void asm_retf(ASMState *as, IRIns *ir)

372. {

373. Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);

374. void *pc = ir_kptr(IR(ir->op2));

375. int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));

376. as->topslot -= (BCReg)delta;

377. if ((int32_t)as->topslot < 0) as->topslot = 0;

378. irt_setmark(IR(REF_BASE)->t);  /* Children must not coalesce with BASE reg. */

379. emit_setgl(as, base, jit_base);

380. emit_addptr(as, base, -8*delta);

381. asm_guard(as, MIPSI_BNE, RID_TMP,

382. ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base)));

383. emit_tsi(as, MIPSI_LW, RID_TMP, base, -8);

384. }

386. /* -- Type conversions ---------------------------------------------------- */

388. ‌static void asm_tointg(ASMState *as, IRIns *ir, Reg left)

389. {

390. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));

391. Reg dest = ra_dest(as, ir, RSET_GPR);

392. asm_guard(as, MIPSI_BC1F, 0, 0);

393. emit_fgh(as, MIPSI_C_EQ_D, 0, tmp, left);

394. emit_fg(as, MIPSI_CVT_D_W, tmp, tmp);

395. emit_tg(as, MIPSI_MFC1, dest, tmp);

396. emit_fg(as, MIPSI_CVT_W_D, tmp, left);

397. }

399. ‌static void asm_tobit(ASMState *as, IRIns *ir)

400. {

401. RegSet allow = RSET_FPR;

402. Reg dest = ra_dest(as, ir, RSET_GPR);

403. Reg left = ra_alloc1(as, ir->op1, allow);

404. Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));

405. Reg tmp = ra_scratch(as, rset_clear(allow, right));

406. emit_tg(as, MIPSI_MFC1, dest, tmp);

407. emit_fgh(as, MIPSI_ADD_D, tmp, left, right);

408. }

410. ‌static void asm_conv(ASMState *as, IRIns *ir)

411. {

412. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);

413. int stfp = (st == IRT_NUM || st == IRT_FLOAT);

414. IRRef lref = ir->op1;

415. lua_assert(irt_type(ir->t) != st);

416. lua_assert(!(irt_isint64(ir->t) ||

417. (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */

418. if (irt_isfp(ir->t)) {

419. Reg dest = ra_dest(as, ir, RSET_FPR);

420. if (stfp) {  /* FP to FP conversion. */

421. emit_fg(as, st == IRT_NUM ? MIPSI_CVT_S_D : MIPSI_CVT_D_S,

422. dest, ra_alloc1(as, lref, RSET_FPR));

423. } else if (st == IRT_U32) {  /* U32 to FP conversion. */

424. /* y = (x ^ 0x8000000) + 2147483648.0 */

425. Reg left = ra_alloc1(as, lref, RSET_GPR);

426. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, dest));

427. emit_fgh(as, irt_isfloat(ir->t) ? MIPSI_ADD_S : MIPSI_ADD_D,

428. dest, dest, tmp);

429. emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W,

430. dest, dest);

431. if (irt_isfloat(ir->t))

432. emit_lsptr(as, MIPSI_LWC1, (tmp & 31),

433. (void *)lj_ir_k64_find(as->J, U64x(4f000000,4f000000)),

434. RSET_GPR);

435. else

436. emit_lsptr(as, MIPSI_LDC1, (tmp & 31),

437. (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)),

438. RSET_GPR);

439. emit_tg(as, MIPSI_MTC1, RID_TMP, dest);

440. emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, left);

441. emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000);

442. } else {  /* Integer to FP conversion. */

443. Reg left = ra_alloc1(as, lref, RSET_GPR);

444. emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W,

445. dest, dest);

446. emit_tg(as, MIPSI_MTC1, left, dest);

447. }

448. } else if (stfp) {  /* FP to integer conversion. */

449. if (irt_isguard(ir->t)) {

450. /* Checked conversions are only supported from number to int. */

451. lua_assert(irt_isint(ir->t) && st == IRT_NUM);

452. asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));

453. } else {

454. Reg dest = ra_dest(as, ir, RSET_GPR);

455. Reg left = ra_alloc1(as, lref, RSET_FPR);

456. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));

457. if (irt_isu32(ir->t)) {

458. /* y = (int)floor(x - 2147483648.0) ^ 0x80000000 */

459. emit_dst(as, MIPSI_XOR, dest, dest, RID_TMP);

460. emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000);

461. emit_tg(as, MIPSI_MFC1, dest, tmp);

462. emit_fg(as, st == IRT_FLOAT ? MIPSI_FLOOR_W_S : MIPSI_FLOOR_W_D,

463. tmp, tmp);

464. emit_fgh(as, st == IRT_FLOAT ? MIPSI_SUB_S : MIPSI_SUB_D,

465. tmp, left, tmp);

466. if (st == IRT_FLOAT)

467. emit_lsptr(as, MIPSI_LWC1, (tmp & 31),

468. (void *)lj_ir_k64_find(as->J, U64x(4f000000,4f000000)),

469. RSET_GPR);

470. else

471. emit_lsptr(as, MIPSI_LDC1, (tmp & 31),

472. (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)),

473. RSET_GPR);

474. } else {

475. emit_tg(as, MIPSI_MFC1, dest, tmp);

476. emit_fg(as, st == IRT_FLOAT ? MIPSI_TRUNC_W_S : MIPSI_TRUNC_W_D,

477. tmp, left);

478. }

479. }

480. } else {

481. Reg dest = ra_dest(as, ir, RSET_GPR);

482. if (st >= IRT_I8 && st <= IRT_U16) {  /* Extend to 32 bit integer. */

483. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);

484. lua_assert(irt_isint(ir->t) || irt_isu32(ir->t));

485. if ((ir->op2 & IRCONV_SEXT)) {

486. if ((as->flags & JIT_F_MIPS32R2)) {

487. emit_dst(as, st == IRT_I8 ? MIPSI_SEB : MIPSI_SEH, dest, 0, left);

488. } else {

489. uint32_t shift = st == IRT_I8 ? 24 : 16;

490. emit_dta(as, MIPSI_SRA, dest, dest, shift);

491. emit_dta(as, MIPSI_SLL, dest, left, shift);

492. }

493. } else {

494. emit_tsi(as, MIPSI_ANDI, dest, left,

495. (int32_t)(st == IRT_U8 ? 0xff : 0xffff));

496. }

497. } else {  /* 32/64 bit integer conversions. */

498. /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */

499. ra_leftov(as, dest, lref);  /* Do nothing, but may need to move regs. */

500. }

501. }

502. }

504. ‌static void asm_strto(ASMState *as, IRIns *ir)

505. {

506. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];

507. IRRef args[2];

508. RegSet drop = RSET_SCRATCH;

509. if (ra_hasreg(ir->r)) rset_set(drop, ir->r);  /* Spill dest reg (if any). */

510. ra_evictset(as, drop);

511. asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO);  /* Test return status. */

512. args[0] = ir->op1;      /* GCstr *str */

513. args[1] = ASMREF_TMP1;  /* TValue *n  */

514. asm_gencall(as, ci, args);

515. /* Store the result to the spill slot or temp slots. */

516. emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1),

517. RID_SP, sps_scale(ir->s));

518. }

520. /* -- Memory references --------------------------------------------------- */

522. /* Get pointer to TValue. */

523. ‌static void asm_tvptr(ASMState *as, Reg dest, IRRef ref)

524. {

525. IRIns *ir = IR(ref);

526. if (irt_isnum(ir->t)) {

527. if (irref_isk(ref))  /* Use the number constant itself as a TValue. */

528. ra_allockreg(as, i32ptr(ir_knum(ir)), dest);

529. else  /* Otherwise force a spill and use the spill slot. */

530. emit_tsi(as, MIPSI_ADDIU, dest, RID_SP, ra_spill(as, ir));

531. } else {

532. /* Otherwise use g->tmptv to hold the TValue. */

533. RegSet allow = rset_exclude(RSET_GPR, dest);

534. Reg type;

535. emit_tsi(as, MIPSI_ADDIU, dest, RID_JGL, offsetof(global_State, tmptv)-32768);

536. if (!irt_ispri(ir->t)) {

537. Reg src = ra_alloc1(as, ref, allow);

538. emit_setgl(as, src, tmptv.gcr);

539. }

540. type = ra_allock(as, irt_toitype(ir->t), allow);

541. emit_setgl(as, type, tmptv.it);

542. }

543. }

545. ‌static void asm_aref(ASMState *as, IRIns *ir)

546. {

547. Reg dest = ra_dest(as, ir, RSET_GPR);

548. Reg idx, base;

549. if (irref_isk(ir->op2)) {

550. IRRef tab = IR(ir->op1)->op1;

551. int32_t ofs = asm_fuseabase(as, tab);

552. IRRef refa = ofs ? tab : ir->op1;

553. ofs += 8*IR(ir->op2)->i;

554. if (checki16(ofs)) {

555. base = ra_alloc1(as, refa, RSET_GPR);

556. emit_tsi(as, MIPSI_ADDIU, dest, base, ofs);

557. return;

558. }

559. }

560. base = ra_alloc1(as, ir->op1, RSET_GPR);

561. idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));

562. emit_dst(as, MIPSI_ADDU, dest, RID_TMP, base);

563. emit_dta(as, MIPSI_SLL, RID_TMP, idx, 3);

564. }

566. /* Inlined hash lookup. Specialized for key type and for const keys.

567. ** The equivalent C code is:

568. **   Node *n = hashkey(t, key);

569. **   do {

570. **     if (lj_obj_equal(&n->key, key)) return &n->val;

571. **   } while ((n = nextnode(n)));

572. **   return niltv(L);

573. */

574. ‌static void asm_href(ASMState *as, IRIns *ir, IROp merge)

575. {

576. RegSet allow = RSET_GPR;

577. int destused = ra_used(ir);

578. Reg dest = ra_dest(as, ir, allow);

579. Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));

580. Reg key = RID_NONE, type = RID_NONE, tmpnum = RID_NONE, tmp1 = RID_TMP, tmp2;

581. IRRef refkey = ir->op2;

582. IRIns *irkey = IR(refkey);

583. IRType1 kt = irkey->t;

584. uint32_t khash;

585. MCLabel l_end, l_loop, l_next;

587. rset_clear(allow, tab);

588. if (irt_isnum(kt)) {

589. key = ra_alloc1(as, refkey, RSET_FPR);

590. tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key));

591. } else if (!irt_ispri(kt)) {

592. key = ra_alloc1(as, refkey, allow);

593. rset_clear(allow, key);

594. type = ra_allock(as, irt_toitype(irkey->t), allow);

595. rset_clear(allow, type);

596. }

597. tmp2 = ra_scratch(as, allow);

598. rset_clear(allow, tmp2);

600. /* Key not found in chain: jump to exit (if merged) or load niltv. */

601. l_end = emit_label(as);

602. as->invmcp = NULL;

603. if (merge == IR_NE)

604. asm_guard(as, MIPSI_B, RID_ZERO, RID_ZERO);

605. else if (destused)

606. emit_loada(as, dest, niltvg(J2G(as->J)));

607. /* Follow hash chain until the end. */

608. emit_move(as, dest, tmp2);

609. l_loop = --as->mcp;

610. emit_tsi(as, MIPSI_LW, tmp2, dest, (int32_t)offsetof(Node, next));

611. l_next = emit_label(as);

613. /* Type and value comparison. */

614. if (merge == IR_EQ) {  /* Must match asm_guard(). */

615. emit_ti(as, MIPSI_LI, RID_TMP, as->snapno);

616. l_end = asm_exitstub_addr(as);

617. }

618. if (irt_isnum(kt)) {

619. emit_branch(as, MIPSI_BC1T, 0, 0, l_end);

620. emit_fgh(as, MIPSI_C_EQ_D, 0, tmpnum, key);

621. *--as->mcp = MIPSI_NOP;  /* Avoid NaN comparison overhead. */

622. emit_branch(as, MIPSI_BEQ, tmp2, RID_ZERO, l_next);

623. emit_tsi(as, MIPSI_SLTIU, tmp2, tmp2, (int32_t)LJ_TISNUM);

624. emit_hsi(as, MIPSI_LDC1, tmpnum, dest, (int32_t)offsetof(Node, key.n));

625. } else {

626. if (irt_ispri(kt)) {

627. emit_branch(as, MIPSI_BEQ, tmp2, type, l_end);

628. } else {

629. emit_branch(as, MIPSI_BEQ, tmp1, key, l_end);

630. emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, key.gcr));

631. emit_branch(as, MIPSI_BNE, tmp2, type, l_next);

632. }

633. }

634. emit_tsi(as, MIPSI_LW, tmp2, dest, (int32_t)offsetof(Node, key.it));

635. *l_loop = MIPSI_BNE | MIPSF_S(tmp2) | ((as->mcp-l_loop-1) & 0xffffu);

637. /* Load main position relative to tab->node into dest. */

638. khash = irref_isk(refkey) ? ir_khash(irkey) : 1;

639. if (khash == 0) {

640. emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node));

641. } else {

642. Reg tmphash = tmp1;

643. if (irref_isk(refkey))

644. tmphash = ra_allock(as, khash, allow);

645. emit_dst(as, MIPSI_ADDU, dest, dest, tmp1);

646. lua_assert(sizeof(Node) == 24);

647. emit_dst(as, MIPSI_SUBU, tmp1, tmp2, tmp1);

648. emit_dta(as, MIPSI_SLL, tmp1, tmp1, 3);

649. emit_dta(as, MIPSI_SLL, tmp2, tmp1, 5);

650. emit_dst(as, MIPSI_AND, tmp1, tmp2, tmphash);

651. emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node));

652. emit_tsi(as, MIPSI_LW, tmp2, tab, (int32_t)offsetof(GCtab, hmask));

653. if (irref_isk(refkey)) {

654. /* Nothing to do. */

655. } else if (irt_isstr(kt)) {

656. emit_tsi(as, MIPSI_LW, tmp1, key, (int32_t)offsetof(GCstr, hash));

657. } else {  /* Must match with hash*() in lj_tab.c. */

658. emit_dst(as, MIPSI_SUBU, tmp1, tmp1, tmp2);

659. emit_rotr(as, tmp2, tmp2, dest, (-HASH_ROT3)&31);

660. emit_dst(as, MIPSI_XOR, tmp1, tmp1, tmp2);

661. emit_rotr(as, tmp1, tmp1, dest, (-HASH_ROT2-HASH_ROT1)&31);

662. emit_dst(as, MIPSI_SUBU, tmp2, tmp2, dest);

663. if (irt_isnum(kt)) {

664. emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1);

665. if ((as->flags & JIT_F_MIPS32R2)) {

666. emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31);

667. } else {

668. emit_dst(as, MIPSI_OR, dest, dest, tmp1);

669. emit_dta(as, MIPSI_SLL, tmp1, tmp1, HASH_ROT1);

670. emit_dta(as, MIPSI_SRL, dest, tmp1, (-HASH_ROT1)&31);

671. }

672. emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1);

673. emit_tg(as, MIPSI_MFC1, tmp2, key);

674. emit_tg(as, MIPSI_MFC1, tmp1, key+1);

675. } else {

676. emit_dst(as, MIPSI_XOR, tmp2, key, tmp1);

677. emit_rotr(as, dest, tmp1, tmp2, (-HASH_ROT1)&31);

678. emit_dst(as, MIPSI_ADDU, tmp1, key, ra_allock(as, HASH_BIAS, allow));

679. }

680. }

681. }

682. }

684. ‌static void asm_hrefk(ASMState *as, IRIns *ir)

685. {

686. IRIns *kslot = IR(ir->op2);

687. IRIns *irkey = IR(kslot->op1);

688. int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));

689. int32_t kofs = ofs + (int32_t)offsetof(Node, key);

690. Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;

691. Reg node = ra_alloc1(as, ir->op1, RSET_GPR);

692. Reg key = RID_NONE, type = RID_TMP, idx = node;

693. RegSet allow = rset_exclude(RSET_GPR, node);

694. int32_t lo, hi;

695. lua_assert(ofs % sizeof(Node) == 0);

696. if (ofs > 32736) {

697. idx = dest;

698. rset_clear(allow, dest);

699. kofs = (int32_t)offsetof(Node, key);

700. } else if (ra_hasreg(dest)) {

701. emit_tsi(as, MIPSI_ADDIU, dest, node, ofs);

702. }

703. if (!irt_ispri(irkey->t)) {

704. key = ra_scratch(as, allow);

705. rset_clear(allow, key);

706. }

707. if (irt_isnum(irkey->t)) {

708. lo = (int32_t)ir_knum(irkey)->u32.lo;

709. hi = (int32_t)ir_knum(irkey)->u32.hi;

710. } else {

711. lo = irkey->i;

712. hi = irt_toitype(irkey->t);

713. if (!ra_hasreg(key))

714. goto nolo;

715. }

716. asm_guard(as, MIPSI_BNE, key, lo ? ra_allock(as, lo, allow) : RID_ZERO);

717. nolo:

718. asm_guard(as, MIPSI_BNE, type, hi ? ra_allock(as, hi, allow) : RID_ZERO);

719. if (ra_hasreg(key)) emit_tsi(as, MIPSI_LW, key, idx, kofs+(LJ_BE?4:0));

720. emit_tsi(as, MIPSI_LW, type, idx, kofs+(LJ_BE?0:4));

721. if (ofs > 32736)

722. emit_tsi(as, MIPSI_ADDU, dest, node, ra_allock(as, ofs, allow));

723. }

725. ‌static void asm_uref(ASMState *as, IRIns *ir)

726. {

727. /* NYI: Check that UREFO is still open and not aliasing a slot. */

728. Reg dest = ra_dest(as, ir, RSET_GPR);

729. if (irref_isk(ir->op1)) {

730. GCfunc *fn = ir_kfunc(IR(ir->op1));

731. MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;

732. emit_lsptr(as, MIPSI_LW, dest, v, RSET_GPR);

733. } else {

734. Reg uv = ra_scratch(as, RSET_GPR);

735. Reg func = ra_alloc1(as, ir->op1, RSET_GPR);

736. if (ir->o == IR_UREFC) {

737. asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);

738. emit_tsi(as, MIPSI_ADDIU, dest, uv, (int32_t)offsetof(GCupval, tv));

739. emit_tsi(as, MIPSI_LBU, RID_TMP, uv, (int32_t)offsetof(GCupval, closed));

740. } else {

741. emit_tsi(as, MIPSI_LW, dest, uv, (int32_t)offsetof(GCupval, v));

742. }

743. emit_tsi(as, MIPSI_LW, uv, func,

744. (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8));

745. }

746. }

748. ‌static void asm_fref(ASMState *as, IRIns *ir)

749. {

750. UNUSED(as); UNUSED(ir);

751. lua_assert(!ra_used(ir));

752. }

754. ‌static void asm_strref(ASMState *as, IRIns *ir)

755. {

756. Reg dest = ra_dest(as, ir, RSET_GPR);

757. IRRef ref = ir->op2, refk = ir->op1;

758. int32_t ofs = (int32_t)sizeof(GCstr);

759. Reg r;

760. if (irref_isk(ref)) {

761. IRRef tmp = refk; refk = ref; ref = tmp;

762. } else if (!irref_isk(refk)) {

763. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);

764. IRIns *irr = IR(ir->op2);

765. if (ra_hasreg(irr->r)) {

766. ra_noweak(as, irr->r);

767. right = irr->r;

768. } else if (mayfuse(as, irr->op2) &&

769. irr->o == IR_ADD && irref_isk(irr->op2) &&

770. checki16(ofs + IR(irr->op2)->i)) {

771. ofs += IR(irr->op2)->i;

772. right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));

773. } else {

774. right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left));

775. }

776. emit_tsi(as, MIPSI_ADDIU, dest, dest, ofs);

777. emit_dst(as, MIPSI_ADDU, dest, left, right);

778. return;

779. }

780. r = ra_alloc1(as, ref, RSET_GPR);

781. ofs += IR(refk)->i;

782. if (checki16(ofs))

783. emit_tsi(as, MIPSI_ADDIU, dest, r, ofs);

784. else

785. emit_dst(as, MIPSI_ADDU, dest, r,

786. ra_allock(as, ofs, rset_exclude(RSET_GPR, r)));

787. }

789. /* -- Loads and stores ---------------------------------------------------- */

791. ‌static MIPSIns asm_fxloadins(IRIns *ir)

792. {

793. switch (irt_type(ir->t)) {

794. case IRT_I8: return MIPSI_LB;

795. case IRT_U8: return MIPSI_LBU;

796. case IRT_I16: return MIPSI_LH;

797. case IRT_U16: return MIPSI_LHU;

798. case IRT_NUM: return MIPSI_LDC1;

799. case IRT_FLOAT: return MIPSI_LWC1;

800. default: return MIPSI_LW;

801. }

802. }

804. ‌static MIPSIns asm_fxstoreins(IRIns *ir)

805. {

806. switch (irt_type(ir->t)) {

807. case IRT_I8: case IRT_U8: return MIPSI_SB;

808. case IRT_I16: case IRT_U16: return MIPSI_SH;

809. case IRT_NUM: return MIPSI_SDC1;

810. case IRT_FLOAT: return MIPSI_SWC1;

811. default: return MIPSI_SW;

812. }

813. }

815. ‌static void asm_fload(ASMState *as, IRIns *ir)

816. {

817. Reg dest = ra_dest(as, ir, RSET_GPR);

818. Reg idx = ra_alloc1(as, ir->op1, RSET_GPR);

819. MIPSIns mi = asm_fxloadins(ir);

820. int32_t ofs;

821. if (ir->op2 == IRFL_TAB_ARRAY) {

822. ofs = asm_fuseabase(as, ir->op1);

823. if (ofs) {  /* Turn the t->array load into an add for colocated arrays. */

824. emit_tsi(as, MIPSI_ADDIU, dest, idx, ofs);

825. return;

826. }

827. }

828. ofs = field_ofs[ir->op2];

829. lua_assert(!irt_isfp(ir->t));

830. emit_tsi(as, mi, dest, idx, ofs);

831. }

833. ‌static void asm_fstore(ASMState *as, IRIns *ir)

834. {

835. if (ir->r != RID_SINK) {

836. Reg src = ra_alloc1z(as, ir->op2, RSET_GPR);

837. IRIns *irf = IR(ir->op1);

838. Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));

839. int32_t ofs = field_ofs[irf->op2];

840. MIPSIns mi = asm_fxstoreins(ir);

841. lua_assert(!irt_isfp(ir->t));

842. emit_tsi(as, mi, src, idx, ofs);

843. }

844. }

846. ‌static void asm_xload(ASMState *as, IRIns *ir)

847. {

848. Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);

849. lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED));

850. asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0);

851. }

853. ‌static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs)

854. {

855. if (ir->r != RID_SINK) {

856. Reg src = ra_alloc1z(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);

857. asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,

858. rset_exclude(RSET_GPR, src), ofs);

859. }

860. }

862. ‌#define asm_xstore(as, ir)        asm_xstore_(as, ir, 0)

864. ‌static void asm_ahuvload(ASMState *as, IRIns *ir)

865. {

866. IRType1 t = ir->t;

867. Reg dest = RID_NONE, type = RID_TMP, idx;

868. RegSet allow = RSET_GPR;

869. int32_t ofs = 0;

870. if (ra_used(ir)) {

871. lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));

872. dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);

873. rset_clear(allow, dest);

874. }

875. idx = asm_fuseahuref(as, ir->op1, &ofs, allow);

876. rset_clear(allow, idx);

877. if (irt_isnum(t)) {

878. asm_guard(as, MIPSI_BEQ, type, RID_ZERO);

879. emit_tsi(as, MIPSI_SLTIU, type, type, (int32_t)LJ_TISNUM);

880. if (ra_hasreg(dest))

881. emit_hsi(as, MIPSI_LDC1, dest, idx, ofs);

882. } else {

883. asm_guard(as, MIPSI_BNE, type, ra_allock(as, irt_toitype(t), allow));

884. if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, idx, ofs+(LJ_BE?4:0));

885. }

886. emit_tsi(as, MIPSI_LW, type, idx, ofs+(LJ_BE?0:4));

887. }

889. ‌static void asm_ahustore(ASMState *as, IRIns *ir)

890. {

891. RegSet allow = RSET_GPR;

892. Reg idx, src = RID_NONE, type = RID_NONE;

893. int32_t ofs = 0;

894. if (ir->r == RID_SINK)

895. return;

896. if (irt_isnum(ir->t)) {

897. src = ra_alloc1(as, ir->op2, RSET_FPR);

898. } else {

899. if (!irt_ispri(ir->t)) {

900. src = ra_alloc1(as, ir->op2, allow);

901. rset_clear(allow, src);

902. }

903. type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);

904. rset_clear(allow, type);

905. }

906. idx = asm_fuseahuref(as, ir->op1, &ofs, allow);

907. if (irt_isnum(ir->t)) {

908. emit_hsi(as, MIPSI_SDC1, src, idx, ofs);

909. } else {

910. if (ra_hasreg(src))

911. emit_tsi(as, MIPSI_SW, src, idx, ofs+(LJ_BE?4:0));

912. emit_tsi(as, MIPSI_SW, type, idx, ofs+(LJ_BE?0:4));

913. }

914. }

916. ‌static void asm_sload(ASMState *as, IRIns *ir)

917. {

918. int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0);

919. IRType1 t = ir->t;

920. Reg dest = RID_NONE, type = RID_NONE, base;

921. RegSet allow = RSET_GPR;

922. lua_assert(!(ir->op2 & IRSLOAD_PARENT));  /* Handled by asm_head_side(). */

923. lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK));

924. lua_assert(!irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME)));

925. if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {

926. dest = ra_scratch(as, RSET_FPR);

927. asm_tointg(as, ir, dest);

928. t.irt = IRT_NUM;  /* Continue with a regular number type check. */

929. } else if (ra_used(ir)) {

930. lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));

931. dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);

932. rset_clear(allow, dest);

933. base = ra_alloc1(as, REF_BASE, allow);

934. rset_clear(allow, base);

935. if ((ir->op2 & IRSLOAD_CONVERT)) {

936. if (irt_isint(t)) {

937. Reg tmp = ra_scratch(as, RSET_FPR);

938. emit_tg(as, MIPSI_MFC1, dest, tmp);

939. emit_fg(as, MIPSI_TRUNC_W_D, tmp, tmp);

940. dest = tmp;

941. t.irt = IRT_NUM;  /* Check for original type. */

942. } else {

943. Reg tmp = ra_scratch(as, RSET_GPR);

944. emit_fg(as, MIPSI_CVT_D_W, dest, dest);

945. emit_tg(as, MIPSI_MTC1, tmp, dest);

946. dest = tmp;

947. t.irt = IRT_INT;  /* Check for original type. */

948. }

949. }

950. goto dotypecheck;

951. }

952. base = ra_alloc1(as, REF_BASE, allow);

953. rset_clear(allow, base);

954. dotypecheck:

955. if (irt_isnum(t)) {

956. if ((ir->op2 & IRSLOAD_TYPECHECK)) {

957. asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);

958. emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)LJ_TISNUM);

959. type = RID_TMP;

960. }

961. if (ra_hasreg(dest)) emit_hsi(as, MIPSI_LDC1, dest, base, ofs);

962. } else {

963. if ((ir->op2 & IRSLOAD_TYPECHECK)) {

964. Reg ktype = ra_allock(as, irt_toitype(t), allow);

965. asm_guard(as, MIPSI_BNE, RID_TMP, ktype);

966. type = RID_TMP;

967. }

968. if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, base, ofs ^ (LJ_BE?4:0));

969. }

970. if (ra_hasreg(type)) emit_tsi(as, MIPSI_LW, type, base, ofs ^ (LJ_BE?0:4));

971. }

973. /* -- Allocations --------------------------------------------------------- */

975. #if LJ_HASFFI

976. ‌static void asm_cnew(ASMState *as, IRIns *ir)

977. {

978. CTState *cts = ctype_ctsG(J2G(as->J));

979. CTypeID id = (CTypeID)IR(ir->op1)->i;

980. CTSize sz;

981. CTInfo info = lj_ctype_info(cts, id, &sz);

982. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];

983. IRRef args[4];

984. RegSet drop = RSET_SCRATCH;

985. lua_assert(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL));

987. as->gcsteps++;

988. if (ra_hasreg(ir->r))

989. rset_clear(drop, ir->r);  /* Dest reg handled below. */

990. ra_evictset(as, drop);

991. if (ra_used(ir))

992. ra_destreg(as, ir, RID_RET);  /* GCcdata * */

994. /* Initialize immutable cdata object. */

995. if (ir->o == IR_CNEWI) {

996. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);

997. int32_t ofs = sizeof(GCcdata);

998. lua_assert(sz == 4 || sz == 8);

999. if (sz == 8) {

1000. ofs += 4;

1001. lua_assert((ir+1)->o == IR_HIOP);

1002. if (LJ_LE) ir++;

1003. }

1004. for (;;) {

1005. Reg r = ra_alloc1z(as, ir->op2, allow);

1006. emit_tsi(as, MIPSI_SW, r, RID_RET, ofs);

1007. rset_clear(allow, r);

1008. if (ofs == sizeof(GCcdata)) break;

1009. ofs -= 4; if (LJ_BE) ir++; else ir--;

1010. }

1011. } else if (ir->op2 != REF_NIL) {  /* Create VLA/VLS/aligned cdata. */

1012. ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];

1013. args[0] = ASMREF_L;     /* lua_State *L */

1014. args[1] = ir->op1;      /* CTypeID id   */

1015. args[2] = ir->op2;      /* CTSize sz    */

1016. args[3] = ASMREF_TMP1;  /* CTSize align */

1017. asm_gencall(as, ci, args);

1018. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));

1019. return;

1020. }

1022. /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */

1023. emit_tsi(as, MIPSI_SB, RID_RET+1, RID_RET, offsetof(GCcdata, gct));

1024. emit_tsi(as, MIPSI_SH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid));

1025. emit_ti(as, MIPSI_LI, RID_RET+1, ~LJ_TCDATA);

1026. emit_ti(as, MIPSI_LI, RID_TMP, id); /* Lower 16 bit used. Sign-ext ok. */

1027. args[0] = ASMREF_L;     /* lua_State *L */

1028. args[1] = ASMREF_TMP1;  /* MSize size   */

1029. asm_gencall(as, ci, args);

1030. ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),

1031. ra_releasetmp(as, ASMREF_TMP1));

1032. }

1033. #else

1034. ‌#define asm_cnew(as, ir)        ((void)0)

1035. #endif

1037. /* -- Write barriers ------------------------------------------------------ */

1039. ‌static void asm_tbar(ASMState *as, IRIns *ir)

1040. {

1041. Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);

1042. Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab));

1043. Reg link = RID_TMP;

1044. MCLabel l_end = emit_label(as);

1045. emit_tsi(as, MIPSI_SW, link, tab, (int32_t)offsetof(GCtab, gclist));

1046. emit_tsi(as, MIPSI_SB, mark, tab, (int32_t)offsetof(GCtab, marked));

1047. emit_setgl(as, tab, gc.grayagain);

1048. emit_getgl(as, link, gc.grayagain);

1049. emit_dst(as, MIPSI_XOR, mark, mark, RID_TMP);  /* Clear black bit. */

1050. emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);

1051. emit_tsi(as, MIPSI_ANDI, RID_TMP, mark, LJ_GC_BLACK);

1052. emit_tsi(as, MIPSI_LBU, mark, tab, (int32_t)offsetof(GCtab, marked));

1053. }

1055. ‌static void asm_obar(ASMState *as, IRIns *ir)

1056. {

1057. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];

1058. IRRef args[2];

1059. MCLabel l_end;

1060. Reg obj, val, tmp;

1061. /* No need for other object barriers (yet). */

1062. lua_assert(IR(ir->op1)->o == IR_UREFC);

1063. ra_evictset(as, RSET_SCRATCH);

1064. l_end = emit_label(as);

1065. args[0] = ASMREF_TMP1;  /* global_State *g */

1066. args[1] = ir->op1;      /* TValue *tv      */

1067. asm_gencall(as, ci, args);

1068. emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);

1069. obj = IR(ir->op1)->r;

1070. tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));

1071. emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);

1072. emit_tsi(as, MIPSI_ANDI, tmp, tmp, LJ_GC_BLACK);

1073. emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);

1074. emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, LJ_GC_WHITES);

1075. val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));

1076. emit_tsi(as, MIPSI_LBU, tmp, obj,

1077. (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));

1078. emit_tsi(as, MIPSI_LBU, RID_TMP, val, (int32_t)offsetof(GChead, marked));

1079. }

1081. /* -- Arithmetic and logic operations ------------------------------------- */

1083. ‌static void asm_fparith(ASMState *as, IRIns *ir, MIPSIns mi)

1084. {

1085. Reg dest = ra_dest(as, ir, RSET_FPR);

1086. Reg right, left = ra_alloc2(as, ir, RSET_FPR);

1087. right = (left >> 8); left &= 255;

1088. emit_fgh(as, mi, dest, left, right);

1089. }

1091. ‌static void asm_fpunary(ASMState *as, IRIns *ir, MIPSIns mi)

1092. {

1093. Reg dest = ra_dest(as, ir, RSET_FPR);

1094. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);

1095. emit_fg(as, mi, dest, left);

1096. }

1098. ‌static void asm_fpmath(ASMState *as, IRIns *ir)

1099. {

1100. if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir))

1101. return;

1102. if (ir->op2 <= IRFPM_TRUNC)

1103. asm_callround(as, ir, IRCALL_lj_vm_floor + ir->op2);

1104. else if (ir->op2 == IRFPM_SQRT)

1105. asm_fpunary(as, ir, MIPSI_SQRT_D);

1106. else

1107. asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);

1108. }

1110. ‌static void asm_add(ASMState *as, IRIns *ir)

1111. {

1112. if (irt_isnum(ir->t)) {

1113. asm_fparith(as, ir, MIPSI_ADD_D);

1114. } else {

1115. Reg dest = ra_dest(as, ir, RSET_GPR);

1116. Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1117. if (irref_isk(ir->op2)) {

1118. int32_t k = IR(ir->op2)->i;

1119. if (checki16(k)) {

1120. emit_tsi(as, MIPSI_ADDIU, dest, left, k);

1121. return;

1122. }

1123. }

1124. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1125. emit_dst(as, MIPSI_ADDU, dest, left, right);

1126. }

1127. }

1129. ‌static void asm_sub(ASMState *as, IRIns *ir)

1130. {

1131. if (irt_isnum(ir->t)) {

1132. asm_fparith(as, ir, MIPSI_SUB_D);

1133. } else {

1134. Reg dest = ra_dest(as, ir, RSET_GPR);

1135. Reg right, left = ra_alloc2(as, ir, RSET_GPR);

1136. right = (left >> 8); left &= 255;

1137. emit_dst(as, MIPSI_SUBU, dest, left, right);

1138. }

1139. }

1141. ‌static void asm_mul(ASMState *as, IRIns *ir)

1142. {

1143. if (irt_isnum(ir->t)) {

1144. asm_fparith(as, ir, MIPSI_MUL_D);

1145. } else {

1146. Reg dest = ra_dest(as, ir, RSET_GPR);

1147. Reg right, left = ra_alloc2(as, ir, RSET_GPR);

1148. right = (left >> 8); left &= 255;

1149. emit_dst(as, MIPSI_MUL, dest, left, right);

1150. }

1151. }

1153. ‌#define asm_div(as, ir)                asm_fparith(as, ir, MIPSI_DIV_D)

1154. ‌#define asm_mod(as, ir)                asm_callid(as, ir, IRCALL_lj_vm_modi)

1155. ‌#define asm_pow(as, ir)                asm_callid(as, ir, IRCALL_lj_vm_powi)

1157. ‌static void asm_neg(ASMState *as, IRIns *ir)

1158. {

1159. if (irt_isnum(ir->t)) {

1160. asm_fpunary(as, ir, MIPSI_NEG_D);

1161. } else {

1162. Reg dest = ra_dest(as, ir, RSET_GPR);

1163. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1164. emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left);

1165. }

1166. }

1168. ‌#define asm_abs(as, ir)                asm_fpunary(as, ir, MIPSI_ABS_D)

1169. ‌#define asm_atan2(as, ir)        asm_callid(as, ir, IRCALL_atan2)

1170. ‌#define asm_ldexp(as, ir)        asm_callid(as, ir, IRCALL_ldexp)

1172. ‌static void asm_arithov(ASMState *as, IRIns *ir)

1173. {

1174. Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR);

1175. if (irref_isk(ir->op2)) {

1176. int k = IR(ir->op2)->i;

1177. if (ir->o == IR_SUBOV) k = -k;

1178. if (checki16(k)) {  /* (dest < left) == (k >= 0 ? 1 : 0) */

1179. left = ra_alloc1(as, ir->op1, RSET_GPR);

1180. asm_guard(as, k >= 0 ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);

1181. emit_dst(as, MIPSI_SLT, RID_TMP, dest, dest == left ? RID_TMP : left);

1182. emit_tsi(as, MIPSI_ADDIU, dest, left, k);

1183. if (dest == left) emit_move(as, RID_TMP, left);

1184. return;

1185. }

1186. }

1187. left = ra_alloc2(as, ir, RSET_GPR);

1188. right = (left >> 8); left &= 255;

1189. tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left),

1190. right), dest));

1191. asm_guard(as, MIPSI_BLTZ, RID_TMP, 0);

1192. emit_dst(as, MIPSI_AND, RID_TMP, RID_TMP, tmp);

1193. if (ir->o == IR_ADDOV) {  /* ((dest^left) & (dest^right)) < 0 */

1194. emit_dst(as, MIPSI_XOR, RID_TMP, dest, dest == right ? RID_TMP : right);

1195. } else {  /* ((dest^left) & (dest^~right)) < 0 */

1196. emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, dest);

1197. emit_dst(as, MIPSI_NOR, RID_TMP, dest == right ? RID_TMP : right, RID_ZERO);

1198. }

1199. emit_dst(as, MIPSI_XOR, tmp, dest, dest == left ? RID_TMP : left);

1200. emit_dst(as, ir->o == IR_ADDOV ? MIPSI_ADDU : MIPSI_SUBU, dest, left, right);

1201. if (dest == left || dest == right)

1202. emit_move(as, RID_TMP, dest == left ? left : right);

1203. }

1205. ‌#define asm_addov(as, ir)        asm_arithov(as, ir)

1206. ‌#define asm_subov(as, ir)        asm_arithov(as, ir)

1208. ‌static void asm_mulov(ASMState *as, IRIns *ir)

1209. {

1210. Reg dest = ra_dest(as, ir, RSET_GPR);

1211. Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR);

1212. right = (left >> 8); left &= 255;

1213. tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left),

1214. right), dest));

1215. asm_guard(as, MIPSI_BNE, RID_TMP, tmp);

1216. emit_dta(as, MIPSI_SRA, RID_TMP, dest, 31);

1217. emit_dst(as, MIPSI_MFHI, tmp, 0, 0);

1218. emit_dst(as, MIPSI_MFLO, dest, 0, 0);

1219. emit_dst(as, MIPSI_MULT, 0, left, right);

1220. }

1222. #if LJ_HASFFI

1223. ‌static void asm_add64(ASMState *as, IRIns *ir)

1224. {

1225. Reg dest = ra_dest(as, ir, RSET_GPR);

1226. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);

1227. if (irref_isk(ir->op2)) {

1228. int32_t k = IR(ir->op2)->i;

1229. if (k == 0) {

1230. emit_dst(as, MIPSI_ADDU, dest, left, RID_TMP);

1231. goto loarith;

1232. } else if (checki16(k)) {

1233. emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP);

1234. emit_tsi(as, MIPSI_ADDIU, dest, left, k);

1235. goto loarith;

1236. }

1237. }

1238. emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP);

1239. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1240. emit_dst(as, MIPSI_ADDU, dest, left, right);

1241. loarith:

1242. ir--;

1243. dest = ra_dest(as, ir, RSET_GPR);

1244. left = ra_alloc1(as, ir->op1, RSET_GPR);

1245. if (irref_isk(ir->op2)) {

1246. int32_t k = IR(ir->op2)->i;

1247. if (k == 0) {

1248. if (dest != left)

1249. emit_move(as, dest, left);

1250. return;

1251. } else if (checki16(k)) {

1252. if (dest == left) {

1253. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, left));

1254. emit_move(as, dest, tmp);

1255. dest = tmp;

1256. }

1257. emit_dst(as, MIPSI_SLTU, RID_TMP, dest, left);

1258. emit_tsi(as, MIPSI_ADDIU, dest, left, k);

1259. return;

1260. }

1261. }

1262. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1263. if (dest == left && dest == right) {

1264. Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right));

1265. emit_move(as, dest, tmp);

1266. dest = tmp;

1267. }

1268. emit_dst(as, MIPSI_SLTU, RID_TMP, dest, dest == left ? right : left);

1269. emit_dst(as, MIPSI_ADDU, dest, left, right);

1270. }

1272. ‌static void asm_sub64(ASMState *as, IRIns *ir)

1273. {

1274. Reg dest = ra_dest(as, ir, RSET_GPR);

1275. Reg right, left = ra_alloc2(as, ir, RSET_GPR);

1276. right = (left >> 8); left &= 255;

1277. emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP);

1278. emit_dst(as, MIPSI_SUBU, dest, left, right);

1279. ir--;

1280. dest = ra_dest(as, ir, RSET_GPR);

1281. left = ra_alloc2(as, ir, RSET_GPR);

1282. right = (left >> 8); left &= 255;

1283. if (dest == left) {

1284. Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right));

1285. emit_move(as, dest, tmp);

1286. dest = tmp;

1287. }

1288. emit_dst(as, MIPSI_SLTU, RID_TMP, left, dest);

1289. emit_dst(as, MIPSI_SUBU, dest, left, right);

1290. }

1292. ‌static void asm_neg64(ASMState *as, IRIns *ir)

1293. {

1294. Reg dest = ra_dest(as, ir, RSET_GPR);

1295. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);

1296. emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP);

1297. emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left);

1298. ir--;

1299. dest = ra_dest(as, ir, RSET_GPR);

1300. left = ra_alloc1(as, ir->op1, RSET_GPR);

1301. emit_dst(as, MIPSI_SLTU, RID_TMP, RID_ZERO, dest);

1302. emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left);

1303. }

1304. #endif

1306. ‌static void asm_bnot(ASMState *as, IRIns *ir)

1307. {

1308. Reg left, right, dest = ra_dest(as, ir, RSET_GPR);

1309. IRIns *irl = IR(ir->op1);

1310. if (mayfuse(as, ir->op1) && irl->o == IR_BOR) {

1311. left = ra_alloc2(as, irl, RSET_GPR);

1312. right = (left >> 8); left &= 255;

1313. } else {

1314. left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1315. right = RID_ZERO;

1316. }

1317. emit_dst(as, MIPSI_NOR, dest, left, right);

1318. }

1320. ‌static void asm_bswap(ASMState *as, IRIns *ir)

1321. {

1322. Reg dest = ra_dest(as, ir, RSET_GPR);

1323. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);

1324. if ((as->flags & JIT_F_MIPS32R2)) {

1325. emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16);

1326. emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left);

1327. } else {

1328. Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), dest));

1329. emit_dst(as, MIPSI_OR, dest, dest, tmp);

1330. emit_dst(as, MIPSI_OR, dest, dest, RID_TMP);

1331. emit_tsi(as, MIPSI_ANDI, dest, dest, 0xff00);

1332. emit_dta(as, MIPSI_SLL, RID_TMP, RID_TMP, 8);

1333. emit_dta(as, MIPSI_SRL, dest, left, 8);

1334. emit_tsi(as, MIPSI_ANDI, RID_TMP, left, 0xff00);

1335. emit_dst(as, MIPSI_OR, tmp, tmp, RID_TMP);

1336. emit_dta(as, MIPSI_SRL, tmp, left, 24);

1337. emit_dta(as, MIPSI_SLL, RID_TMP, left, 24);

1338. }

1339. }

1341. ‌static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)

1342. {

1343. Reg dest = ra_dest(as, ir, RSET_GPR);

1344. Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1345. if (irref_isk(ir->op2)) {

1346. int32_t k = IR(ir->op2)->i;

1347. if (checku16(k)) {

1348. emit_tsi(as, mik, dest, left, k);

1349. return;

1350. }

1351. }

1352. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1353. emit_dst(as, mi, dest, left, right);

1354. }

1356. ‌#define asm_band(as, ir)        asm_bitop(as, ir, MIPSI_AND, MIPSI_ANDI)

1357. ‌#define asm_bor(as, ir)                asm_bitop(as, ir, MIPSI_OR, MIPSI_ORI)

1358. ‌#define asm_bxor(as, ir)        asm_bitop(as, ir, MIPSI_XOR, MIPSI_XORI)

1360. ‌static void asm_bitshift(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)

1361. {

1362. Reg dest = ra_dest(as, ir, RSET_GPR);

1363. if (irref_isk(ir->op2)) {  /* Constant shifts. */

1364. uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31);

1365. emit_dta(as, mik, dest, ra_hintalloc(as, ir->op1, dest, RSET_GPR), shift);

1366. } else {

1367. Reg right, left = ra_alloc2(as, ir, RSET_GPR);

1368. right = (left >> 8); left &= 255;

1369. emit_dst(as, mi, dest, right, left);  /* Shift amount is in rs. */

1370. }

1371. }

1373. ‌#define asm_bshl(as, ir)        asm_bitshift(as, ir, MIPSI_SLLV, MIPSI_SLL)

1374. ‌#define asm_bshr(as, ir)        asm_bitshift(as, ir, MIPSI_SRLV, MIPSI_SRL)

1375. ‌#define asm_bsar(as, ir)        asm_bitshift(as, ir, MIPSI_SRAV, MIPSI_SRA)

1376. ‌#define asm_brol(as, ir)        lua_assert(0)

1378. ‌static void asm_bror(ASMState *as, IRIns *ir)

1379. {

1380. if ((as->flags & JIT_F_MIPS32R2)) {

1381. asm_bitshift(as, ir, MIPSI_ROTRV, MIPSI_ROTR);

1382. } else {

1383. Reg dest = ra_dest(as, ir, RSET_GPR);

1384. if (irref_isk(ir->op2)) {  /* Constant shifts. */

1385. uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31);

1386. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1387. emit_rotr(as, dest, left, RID_TMP, shift);

1388. } else {

1389. Reg right, left = ra_alloc2(as, ir, RSET_GPR);

1390. right = (left >> 8); left &= 255;

1391. emit_dst(as, MIPSI_OR, dest, dest, RID_TMP);

1392. emit_dst(as, MIPSI_SRLV, dest, right, left);

1393. emit_dst(as, MIPSI_SLLV, RID_TMP, RID_TMP, left);

1394. emit_dst(as, MIPSI_SUBU, RID_TMP, ra_allock(as, 32, RSET_GPR), right);

1395. }

1396. }

1397. }

1399. ‌static void asm_min_max(ASMState *as, IRIns *ir, int ismax)

1400. {

1401. if (irt_isnum(ir->t)) {

1402. Reg dest = ra_dest(as, ir, RSET_FPR);

1403. Reg right, left = ra_alloc2(as, ir, RSET_FPR);

1404. right = (left >> 8); left &= 255;

1405. if (dest == left) {

1406. emit_fg(as, MIPSI_MOVT_D, dest, right);

1407. } else {

1408. emit_fg(as, MIPSI_MOVF_D, dest, left);

1409. if (dest != right) emit_fg(as, MIPSI_MOV_D, dest, right);

1410. }

1411. emit_fgh(as, MIPSI_C_OLT_D, 0, ismax ? left : right, ismax ? right : left);

1412. } else {

1413. Reg dest = ra_dest(as, ir, RSET_GPR);

1414. Reg right, left = ra_alloc2(as, ir, RSET_GPR);

1415. right = (left >> 8); left &= 255;

1416. if (dest == left) {

1417. emit_dst(as, MIPSI_MOVN, dest, right, RID_TMP);

1418. } else {

1419. emit_dst(as, MIPSI_MOVZ, dest, left, RID_TMP);

1420. if (dest != right) emit_move(as, dest, right);

1421. }

1422. emit_dst(as, MIPSI_SLT, RID_TMP,

1423. ismax ? left : right, ismax ? right : left);

1424. }

1425. }

1427. ‌#define asm_min(as, ir)                asm_min_max(as, ir, 0)

1428. ‌#define asm_max(as, ir)                asm_min_max(as, ir, 1)

1430. /* -- Comparisons --------------------------------------------------------- */

1432. ‌static void asm_comp(ASMState *as, IRIns *ir)

1433. {

1434. /* ORDER IR: LT GE LE GT  ULT UGE ULE UGT. */

1435. IROp op = ir->o;

1436. if (irt_isnum(ir->t)) {

1437. Reg right, left = ra_alloc2(as, ir, RSET_FPR);

1438. right = (left >> 8); left &= 255;

1439. asm_guard(as, (op&1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0);

1440. emit_fgh(as, MIPSI_C_OLT_D + ((op&3) ^ ((op>>2)&1)), 0, left, right);

1441. } else {

1442. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);

1443. if (op == IR_ABC) op = IR_UGT;

1444. if ((op&4) == 0 && irref_isk(ir->op2) && IR(ir->op2)->i == 0) {

1445. MIPSIns mi = (op&2) ? ((op&1) ? MIPSI_BLEZ : MIPSI_BGTZ) :

1446. ((op&1) ? MIPSI_BLTZ : MIPSI_BGEZ);

1447. asm_guard(as, mi, left, 0);

1448. } else {

1449. if (irref_isk(ir->op2)) {

1450. int32_t k = IR(ir->op2)->i;

1451. if ((op&2)) k++;

1452. if (checki16(k)) {

1453. asm_guard(as, (op&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);

1454. emit_tsi(as, (op&4) ? MIPSI_SLTIU : MIPSI_SLTI,

1455. RID_TMP, left, k);

1456. return;

1457. }

1458. }

1459. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1460. asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);

1461. emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT,

1462. RID_TMP, (op&2) ? right : left, (op&2) ? left : right);

1463. }

1464. }

1465. }

1467. ‌static void asm_equal(ASMState *as, IRIns *ir)

1468. {

1469. Reg right, left = ra_alloc2(as, ir, irt_isnum(ir->t) ? RSET_FPR : RSET_GPR);

1470. right = (left >> 8); left &= 255;

1471. if (irt_isnum(ir->t)) {

1472. asm_guard(as, (ir->o & 1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0);

1473. emit_fgh(as, MIPSI_C_EQ_D, 0, left, right);

1474. } else {

1475. asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, left, right);

1476. }

1477. }

1479. #if LJ_HASFFI

1480. /* 64 bit integer comparisons. */

1481. ‌static void asm_comp64(ASMState *as, IRIns *ir)

1482. {

1483. /* ORDER IR: LT GE LE GT  ULT UGE ULE UGT. */

1484. IROp op = (ir-1)->o;

1485. MCLabel l_end;

1486. Reg rightlo, leftlo, righthi, lefthi = ra_alloc2(as, ir, RSET_GPR);

1487. righthi = (lefthi >> 8); lefthi &= 255;

1488. leftlo = ra_alloc2(as, ir-1,

1489. rset_exclude(rset_exclude(RSET_GPR, lefthi), righthi));

1490. rightlo = (leftlo >> 8); leftlo &= 255;

1491. asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);

1492. l_end = emit_label(as);

1493. if (lefthi != righthi)

1494. emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT, RID_TMP,

1495. (op&2) ? righthi : lefthi, (op&2) ? lefthi : righthi);

1496. emit_dst(as, MIPSI_SLTU, RID_TMP,

1497. (op&2) ? rightlo : leftlo, (op&2) ? leftlo : rightlo);

1498. if (lefthi != righthi)

1499. emit_branch(as, MIPSI_BEQ, lefthi, righthi, l_end);

1500. }

1502. ‌static void asm_comp64eq(ASMState *as, IRIns *ir)

1503. {

1504. Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR);

1505. right = (left >> 8); left &= 255;

1506. asm_guard(as, ((ir-1)->o & 1) ? MIPSI_BEQ : MIPSI_BNE, RID_TMP, RID_ZERO);

1507. tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right));

1508. emit_dst(as, MIPSI_OR, RID_TMP, RID_TMP, tmp);

1509. emit_dst(as, MIPSI_XOR, tmp, left, right);

1510. left = ra_alloc2(as, ir-1, RSET_GPR);

1511. right = (left >> 8); left &= 255;

1512. emit_dst(as, MIPSI_XOR, RID_TMP, left, right);

1513. }

1514. #endif

1516. /* -- Support for 64 bit ops in 32 bit mode ------------------------------- */

1518. /* Hiword op of a split 64 bit op. Previous op must be the loword op. */

1519. ‌static void asm_hiop(ASMState *as, IRIns *ir)

1520. {

1521. #if LJ_HASFFI

1522. /* HIOP is marked as a store because it needs its own DCE logic. */

1523. int uselo = ra_used(ir-1), usehi = ra_used(ir);  /* Loword/hiword used? */

1524. if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;

1525. if ((ir-1)->o == IR_CONV) {  /* Conversions to/from 64 bit. */

1526. as->curins--;  /* Always skip the CONV. */

1527. if (usehi || uselo)

1528. asm_conv64(as, ir);

1529. return;

1530. } else if ((ir-1)->o < IR_EQ) {  /* 64 bit integer comparisons. ORDER IR. */

1531. as->curins--;  /* Always skip the loword comparison. */

1532. asm_comp64(as, ir);

1533. return;

1534. } else if ((ir-1)->o <= IR_NE) {  /* 64 bit integer comparisons. ORDER IR. */

1535. as->curins--;  /* Always skip the loword comparison. */

1536. asm_comp64eq(as, ir);

1537. return;

1538. } else if ((ir-1)->o == IR_XSTORE) {

1539. as->curins--;  /* Handle both stores here. */

1540. if ((ir-1)->r != RID_SINK) {

1541. asm_xstore_(as, ir, LJ_LE ? 4 : 0);

1542. asm_xstore_(as, ir-1, LJ_LE ? 0 : 4);

1543. }

1544. return;

1545. }

1546. if (!usehi) return;  /* Skip unused hiword op for all remaining ops. */

1547. switch ((ir-1)->o) {

1548. case IR_ADD: as->curins--; asm_add64(as, ir); break;

1549. case IR_SUB: as->curins--; asm_sub64(as, ir); break;

1550. case IR_NEG: as->curins--; asm_neg64(as, ir); break;

1551. case IR_CALLN:

1552. case IR_CALLXS:

1553. if (!uselo)

1554. ra_allocref(as, ir->op1, RID2RSET(RID_RETLO));  /* Mark lo op as used. */

1555. break;

1556. case IR_CNEWI:

1557. /* Nothing to do here. Handled by lo op itself. */

1558. break;

1559. default: lua_assert(0); break;

1560. }

1561. #else

1562. UNUSED(as); UNUSED(ir); lua_assert(0);  /* Unused without FFI. */

1563. #endif

1564. }

1566. /* -- Profiling ----------------------------------------------------------- */

1568. ‌static void asm_prof(ASMState *as, IRIns *ir)

1569. {

1570. UNUSED(ir);

1571. asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO);

1572. emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, HOOK_PROFILE);

1573. emit_lsglptr(as, MIPSI_LBU, RID_TMP,

1574. (int32_t)offsetof(global_State, hookmask));

1575. }

1577. /* -- Stack handling ------------------------------------------------------ */

1579. /* Check Lua stack size for overflow. Use exit handler as fallback. */

1580. ‌static void asm_stack_check(ASMState *as, BCReg topslot,

1581. IRIns *irp, RegSet allow, ExitNo exitno)

1582. {

1583. /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */

1584. Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE;

1585. ExitNo oldsnap = as->snapno;

1586. rset_clear(allow, pbase);

1587. tmp = allow ? rset_pickbot(allow) :

1588. (pbase == RID_RETHI ? RID_RETLO : RID_RETHI);

1589. as->snapno = exitno;

1590. asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO);

1591. as->snapno = oldsnap;

1592. if (allow == RSET_EMPTY)  /* Restore temp. register. */

1593. emit_tsi(as, MIPSI_LW, tmp, RID_SP, 0);

1594. else

1595. ra_modified(as, tmp);

1596. emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)(8*topslot));

1597. emit_dst(as, MIPSI_SUBU, RID_TMP, tmp, pbase);

1598. emit_tsi(as, MIPSI_LW, tmp, tmp, offsetof(lua_State, maxstack));

1599. if (pbase == RID_TMP)

1600. emit_getgl(as, RID_TMP, jit_base);

1601. emit_getgl(as, tmp, cur_L);

1602. if (allow == RSET_EMPTY)  /* Spill temp. register. */

1603. emit_tsi(as, MIPSI_SW, tmp, RID_SP, 0);

1604. }

1606. /* Restore Lua stack from on-trace state. */

1607. ‌static void asm_stack_restore(ASMState *as, SnapShot *snap)

1608. {

1609. SnapEntry *map = &as->T->snapmap[snap->mapofs];

1610. SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1];

1611. MSize n, nent = snap->nent;

1612. /* Store the value of all modified slots to the Lua stack. */

1613. for (n = 0; n < nent; n++) {

1614. SnapEntry sn = map[n];

1615. BCReg s = snap_slot(sn);

1616. int32_t ofs = 8*((int32_t)s-1);

1617. IRRef ref = snap_ref(sn);

1618. IRIns *ir = IR(ref);

1619. if ((sn & SNAP_NORESTORE))

1620. continue;

1621. if (irt_isnum(ir->t)) {

1622. Reg src = ra_alloc1(as, ref, RSET_FPR);

1623. emit_hsi(as, MIPSI_SDC1, src, RID_BASE, ofs);

1624. } else {

1625. Reg type;

1626. RegSet allow = rset_exclude(RSET_GPR, RID_BASE);

1627. lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t));

1628. if (!irt_ispri(ir->t)) {

1629. Reg src = ra_alloc1(as, ref, allow);

1630. rset_clear(allow, src);

1631. emit_tsi(as, MIPSI_SW, src, RID_BASE, ofs+(LJ_BE?4:0));

1632. }

1633. if ((sn & (SNAP_CONT|SNAP_FRAME))) {

1634. if (s == 0) continue;  /* Do not overwrite link to previous frame. */

1635. type = ra_allock(as, (int32_t)(*flinks--), allow);

1636. } else {

1637. type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);

1638. }

1639. emit_tsi(as, MIPSI_SW, type, RID_BASE, ofs+(LJ_BE?0:4));

1640. }

1641. checkmclim(as);

1642. }

1643. lua_assert(map + nent == flinks);

1644. }

1646. /* -- GC handling --------------------------------------------------------- */

1648. /* Check GC threshold and do one or more GC steps. */

1649. ‌static void asm_gc_check(ASMState *as)

1650. {

1651. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];

1652. IRRef args[2];

1653. MCLabel l_end;

1654. Reg tmp;

1655. ra_evictset(as, RSET_SCRATCH);

1656. l_end = emit_label(as);

1657. /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */

1658. /* Assumes asm_snap_prep() already done. */

1659. asm_guard(as, MIPSI_BNE, RID_RET, RID_ZERO);

1660. args[0] = ASMREF_TMP1;  /* global_State *g */

1661. args[1] = ASMREF_TMP2;  /* MSize steps     */

1662. asm_gencall(as, ci, args);

1663. emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);

1664. tmp = ra_releasetmp(as, ASMREF_TMP2);

1665. emit_loadi(as, tmp, as->gcsteps);

1666. /* Jump around GC step if GC total < GC threshold. */

1667. emit_branch(as, MIPSI_BNE, RID_TMP, RID_ZERO, l_end);

1668. emit_dst(as, MIPSI_SLTU, RID_TMP, RID_TMP, tmp);

1669. emit_getgl(as, tmp, gc.threshold);

1670. emit_getgl(as, RID_TMP, gc.total);

1671. as->gcsteps = 0;

1672. checkmclim(as);

1673. }

1675. /* -- Loop handling ------------------------------------------------------- */

1677. /* Fixup the loop branch. */

1678. ‌static void asm_loop_fixup(ASMState *as)

1679. {

1680. MCode *p = as->mctop;

1681. MCode *target = as->mcp;

1682. p[-1] = MIPSI_NOP;

1683. if (as->loopinv) {  /* Inverted loop branch? */

1684. /* asm_guard already inverted the cond branch. Only patch the target. */

1685. p[-3] |= ((target-p+2) & 0x0000ffffu);

1686. } else {

1687. p[-2] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);

1688. }

1689. }

1691. /* -- Head of trace ------------------------------------------------------- */

1693. /* Coalesce BASE register for a root trace. */

1694. ‌static void asm_head_root_base(ASMState *as)

1695. {

1696. IRIns *ir = IR(REF_BASE);

1697. Reg r = ir->r;

1698. if (as->loopinv) as->mctop--;

1699. if (ra_hasreg(r)) {

1700. ra_free(as, r);

1701. if (rset_test(as->modset, r) || irt_ismarked(ir->t))

1702. ir->r = RID_INIT;  /* No inheritance for modified BASE register. */

1703. if (r != RID_BASE)

1704. emit_move(as, r, RID_BASE);

1705. }

1706. }

1708. /* Coalesce BASE register for a side trace. */

1709. ‌static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow)

1710. {

1711. IRIns *ir = IR(REF_BASE);

1712. Reg r = ir->r;

1713. if (as->loopinv) as->mctop--;

1714. if (ra_hasreg(r)) {

1715. ra_free(as, r);

1716. if (rset_test(as->modset, r) || irt_ismarked(ir->t))

1717. ir->r = RID_INIT;  /* No inheritance for modified BASE register. */

1718. if (irp->r == r) {

1719. rset_clear(allow, r);  /* Mark same BASE register as coalesced. */

1720. } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {

1721. rset_clear(allow, irp->r);

1722. emit_move(as, r, irp->r);  /* Move from coalesced parent reg. */

1723. } else {

1724. emit_getgl(as, r, jit_base);  /* Otherwise reload BASE. */

1725. }

1726. }

1727. return allow;

1728. }

1730. /* -- Tail of trace ------------------------------------------------------- */

1732. /* Fixup the tail code. */

1733. ‌static void asm_tail_fixup(ASMState *as, TraceNo lnk)

1734. {

1735. MCode *target = lnk ? traceref(as->J,lnk)->mcode : (MCode *)lj_vm_exit_interp;

1736. int32_t spadj = as->T->spadjust;

1737. MCode *p = as->mctop-1;

1738. *p = spadj ? (MIPSI_ADDIU|MIPSF_T(RID_SP)|MIPSF_S(RID_SP)|spadj) : MIPSI_NOP;

1739. p[-1] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);

1740. }

1742. /* Prepare tail of code. */

1743. ‌static void asm_tail_prep(ASMState *as)

1744. {

1745. as->mcp = as->mctop-2;  /* Leave room for branch plus nop or stack adj. */

1746. as->invmcp = as->loopref ? as->mcp : NULL;

1747. }

1749. /* -- Trace setup --------------------------------------------------------- */

1751. /* Ensure there are enough stack slots for call arguments. */

1752. ‌static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)

1753. {

1754. IRRef args[CCI_NARGS_MAX*2];

1755. uint32_t i, nargs = CCI_XNARGS(ci);

1756. int nslots = 4, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;

1757. asm_collectargs(as, ir, ci, args);

1758. for (i = 0; i < nargs; i++) {

1759. if (args[i] && irt_isfp(IR(args[i])->t) &&

1760. nfpr > 0 && !(ci->flags & CCI_VARARG)) {

1761. nfpr--;

1762. ngpr -= irt_isnum(IR(args[i])->t) ? 2 : 1;

1763. } else if (args[i] && irt_isnum(IR(args[i])->t)) {

1764. nfpr = 0;

1765. ngpr = ngpr & ~1;

1766. if (ngpr > 0) ngpr -= 2; else nslots = (nslots+3) & ~1;

1767. } else {

1768. nfpr = 0;

1769. if (ngpr > 0) ngpr--; else nslots++;

1770. }

1771. }

1772. if (nslots > as->evenspill)  /* Leave room for args in stack slots. */

1773. as->evenspill = nslots;

1774. return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET);

1775. }

1777. ‌static void asm_setup_target(ASMState *as)

1778. {

1779. asm_sparejump_setup(as);

1780. asm_exitstub_setup(as);

1781. }

1783. /* -- Trace patching ------------------------------------------------------ */

1785. /* Patch exit jumps of existing machine code to a new target. */

1786. ‌void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)

1787. {

1788. MCode *p = T->mcode;

1789. MCode *pe = (MCode *)((char *)p + T->szmcode);

1790. MCode *px = exitstub_trace_addr(T, exitno);

1791. MCode *cstart = NULL, *cstop = NULL;

1792. MCode *mcarea = lj_mcode_patch(J, p, 0);

1793. MCode exitload = MIPSI_LI | MIPSF_T(RID_TMP) | exitno;

1794. MCode tjump = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);

1795. for (p++; p < pe; p++) {

1796. if (*p == exitload) {  /* Look for load of exit number. */

1797. if (((p[-1] ^ (px-p)) & 0xffffu) == 0) {  /* Look for exitstub branch. */

1798. ptrdiff_t delta = target - p;

1799. if (((delta + 0x8000) >> 16) == 0) {  /* Patch in-range branch. */

1800. patchbranch:

1801. p[-1] = (p[-1] & 0xffff0000u) | (delta & 0xffffu);

1802. *p = MIPSI_NOP;  /* Replace the load of the exit number. */

1803. cstop = p;

1804. if (!cstart) cstart = p-1;

1805. } else {  /* Branch out of range. Use spare jump slot in mcarea. */

1806. int i;

1807. for (i = 2; i < 2+MIPS_SPAREJUMP*2; i += 2) {

1808. if (mcarea[i] == tjump) {

1809. delta = mcarea+i - p;

1810. goto patchbranch;

1811. } else if (mcarea[i] == MIPSI_NOP) {

1812. mcarea[i] = tjump;

1813. cstart = mcarea+i;

1814. delta = mcarea+i - p;

1815. goto patchbranch;

1816. }

1817. }

1818. /* Ignore jump slot overflow. Child trace is simply not attached. */

1819. }

1820. } else if (p+1 == pe) {

1821. /* Patch NOP after code for inverted loop branch. Use of J is ok. */

1822. lua_assert(p[1] == MIPSI_NOP);

1823. p[1] = tjump;

1824. *p = MIPSI_NOP;  /* Replace the load of the exit number. */

1825. cstop = p+2;

1826. if (!cstart) cstart = p+1;

1827. }

1828. }

1829. }

1830. if (cstart) lj_mcode_sync(cstart, cstop);

1831. lj_mcode_patch(J, mcarea, 1);

1832. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_asm_ppc.h - luajit-2.0-src

Global variables defined

- asm_compmap

Functions defined

- asm_add

- asm_add64

- asm_ahustore

- asm_ahuvload

- asm_aref

- asm_arithov

- asm_band

- asm_bitop

- asm_bitshift

- asm_bnot

- asm_bswap

- asm_callx

- asm_cnew

- asm_comp

- asm_comp64

- asm_conv

- asm_exitstub_addr

- asm_exitstub_setup

- asm_fload

- asm_fparith

- asm_fpmath

- asm_fpunary

- asm_fref

- asm_fstore

- asm_fuseabase

- asm_fuseahuref

- asm_fuseandsh

- asm_fusemadd

- asm_fusexref

- asm_fusexrefx

- asm_fxloadins

- asm_fxstoreins

- asm_gc_check

- asm_gencall

- asm_guardcc

- asm_head_root_base

- asm_head_side_base

- asm_hiop

- asm_href

- asm_hrefk

- asm_intcomp_

- asm_loop_fixup

- asm_min_max

- asm_mul

- asm_neg

- asm_neg64

- asm_obar

- asm_prof

- asm_retf

- asm_setup_call_slots

- asm_setup_target

- asm_setupresult

- asm_sload

- asm_stack_check

- asm_stack_restore

- asm_strref

- asm_strto

- asm_sub

- asm_sub64

- asm_tail_fixup

- asm_tail_prep

- asm_tbar

- asm_tobit

- asm_tointg

- asm_tvptr

- asm_uref

- asm_xload

- asm_xstore_

- lj_asm_patchexit

- noconflict

- ra_alloc2

- ra_hintalloc

Macros defined

- AHUREF_LSX

- CC_TWO

- CC_UNSIGNED

- CONFLICT_SEARCH_LIM

- asm_abs

- asm_addov

- asm_atan2

- asm_bor

- asm_brol

- asm_bror

- asm_bsar

- asm_bshl

- asm_bshr

- asm_bxor

- asm_cnew

- asm_div

- asm_equal

- asm_ldexp

- asm_max

- asm_min

- asm_mod

- asm_mulov

- asm_pow

- asm_subov

- asm_xstore

Source code

1. /*

2. ** PPC IR assembler (SSA IR -> machine code).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* -- Register allocator extensions --------------------------------------- */

8. /* Allocate a register with a hint. */

9. ‌static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)

10. {

11. Reg r = IR(ref)->r;

12. if (ra_noreg(r)) {

13. if (!ra_hashint(r) && !iscrossref(as, ref))

14. ra_sethint(IR(ref)->r, hint);  /* Propagate register hint. */

15. r = ra_allocref(as, ref, allow);

16. }

17. ra_noweak(as, r);

18. return r;

19. }

21. /* Allocate two source registers for three-operand instructions. */

22. ‌static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)

23. {

24. IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);

25. Reg left = irl->r, right = irr->r;

26. if (ra_hasreg(left)) {

27. ra_noweak(as, left);

28. if (ra_noreg(right))

29. right = ra_allocref(as, ir->op2, rset_exclude(allow, left));

30. else

31. ra_noweak(as, right);

32. } else if (ra_hasreg(right)) {

33. ra_noweak(as, right);

34. left = ra_allocref(as, ir->op1, rset_exclude(allow, right));

35. } else if (ra_hashint(right)) {

36. right = ra_allocref(as, ir->op2, allow);

37. left = ra_alloc1(as, ir->op1, rset_exclude(allow, right));

38. } else {

39. left = ra_allocref(as, ir->op1, allow);

40. right = ra_alloc1(as, ir->op2, rset_exclude(allow, left));

41. }

42. return left | (right << 8);

43. }

45. /* -- Guard handling ------------------------------------------------------ */

47. /* Setup exit stubs after the end of each trace. */

48. ‌static void asm_exitstub_setup(ASMState *as, ExitNo nexits)

49. {

50. ExitNo i;

51. MCode *mxp = as->mctop;

52. if (mxp - (nexits + 3 + MCLIM_REDZONE) < as->mclim)

53. asm_mclimit(as);

54. /* 1: mflr r0; bl ->vm_exit_handler; li r0, traceno; bl <1; bl <1; ... */

55. for (i = nexits-1; (int32_t)i >= 0; i--)

56. *--mxp = PPCI_BL|(((-3-i)&0x00ffffffu)<<2);

57. *--mxp = PPCI_LI|PPCF_T(RID_TMP)|as->T->traceno;  /* Read by exit handler. */

58. mxp--;

59. *mxp = PPCI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)&0x00ffffffu)<<2);

60. *--mxp = PPCI_MFLR|PPCF_T(RID_TMP);

61. as->mctop = mxp;

62. }

64. ‌static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno)

65. {

66. /* Keep this in-sync with exitstub_trace_addr(). */

67. return as->mctop + exitno + 3;

68. }

70. /* Emit conditional branch to exit for guard. */

71. ‌static void asm_guardcc(ASMState *as, PPCCC cc)

72. {

73. MCode *target = asm_exitstub_addr(as, as->snapno);

74. MCode *p = as->mcp;

75. if (LJ_UNLIKELY(p == as->invmcp)) {

76. as->loopinv = 1;

77. *p = PPCI_B | (((target-p) & 0x00ffffffu) << 2);

78. emit_condbranch(as, PPCI_BC, cc^4, p);

79. return;

80. }

81. emit_condbranch(as, PPCI_BC, cc, target);

82. }

84. /* -- Operand fusion ------------------------------------------------------ */

86. /* Limit linear search to this distance. Avoids O(n^2) behavior. */

87. ‌#define CONFLICT_SEARCH_LIM        31

89. /* Check if there's no conflicting instruction between curins and ref. */

90. ‌static int noconflict(ASMState *as, IRRef ref, IROp conflict)

91. {

92. IRIns *ir = as->ir;

93. IRRef i = as->curins;

94. if (i > ref + CONFLICT_SEARCH_LIM)

95. return 0;  /* Give up, ref is too far away. */

96. while (--i > ref)

97. if (ir[i].o == conflict)

98. return 0;  /* Conflict found. */

99. return 1;  /* Ok, no conflict. */

100. }

102. /* Fuse the array base of colocated arrays. */

103. ‌static int32_t asm_fuseabase(ASMState *as, IRRef ref)

104. {

105. IRIns *ir = IR(ref);

106. if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&

107. !neverfuse(as) && noconflict(as, ref, IR_NEWREF))

108. return (int32_t)sizeof(GCtab);

109. return 0;

110. }

112. /* Indicates load/store indexed is ok. */

113. ‌#define AHUREF_LSX        ((int32_t)0x80000000)

115. /* Fuse array/hash/upvalue reference into register+offset operand. */

116. ‌static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow)

117. {

118. IRIns *ir = IR(ref);

119. if (ra_noreg(ir->r)) {

120. if (ir->o == IR_AREF) {

121. if (mayfuse(as, ref)) {

122. if (irref_isk(ir->op2)) {

123. IRRef tab = IR(ir->op1)->op1;

124. int32_t ofs = asm_fuseabase(as, tab);

125. IRRef refa = ofs ? tab : ir->op1;

126. ofs += 8*IR(ir->op2)->i;

127. if (checki16(ofs)) {

128. *ofsp = ofs;

129. return ra_alloc1(as, refa, allow);

130. }

131. }

132. if (*ofsp == AHUREF_LSX) {

133. Reg base = ra_alloc1(as, ir->op1, allow);

134. Reg idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));

135. return base | (idx << 8);

136. }

137. }

138. } else if (ir->o == IR_HREFK) {

139. if (mayfuse(as, ref)) {

140. int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));

141. if (checki16(ofs)) {

142. *ofsp = ofs;

143. return ra_alloc1(as, ir->op1, allow);

144. }

145. }

146. } else if (ir->o == IR_UREFC) {

147. if (irref_isk(ir->op1)) {

148. GCfunc *fn = ir_kfunc(IR(ir->op1));

149. int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv);

150. int32_t jgl = (intptr_t)J2G(as->J);

151. if ((uint32_t)(ofs-jgl) < 65536) {

152. *ofsp = ofs-jgl-32768;

153. return RID_JGL;

154. } else {

155. *ofsp = (int16_t)ofs;

156. return ra_allock(as, ofs-(int16_t)ofs, allow);

157. }

158. }

159. }

160. }

161. *ofsp = 0;

162. return ra_alloc1(as, ref, allow);

163. }

165. /* Fuse XLOAD/XSTORE reference into load/store operand. */

166. ‌static void asm_fusexref(ASMState *as, PPCIns pi, Reg rt, IRRef ref,

167. RegSet allow, int32_t ofs)

168. {

169. IRIns *ir = IR(ref);

170. Reg base;

171. if (ra_noreg(ir->r) && canfuse(as, ir)) {

172. if (ir->o == IR_ADD) {

173. int32_t ofs2;

174. if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) {

175. ofs = ofs2;

176. ref = ir->op1;

177. } else if (ofs == 0) {

178. Reg right, left = ra_alloc2(as, ir, allow);

179. right = (left >> 8); left &= 255;

180. emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right);

181. return;

182. }

183. } else if (ir->o == IR_STRREF) {

184. lua_assert(ofs == 0);

185. ofs = (int32_t)sizeof(GCstr);

186. if (irref_isk(ir->op2)) {

187. ofs += IR(ir->op2)->i;

188. ref = ir->op1;

189. } else if (irref_isk(ir->op1)) {

190. ofs += IR(ir->op1)->i;

191. ref = ir->op2;

192. } else {

193. /* NYI: Fuse ADD with constant. */

194. Reg tmp, right, left = ra_alloc2(as, ir, allow);

195. right = (left >> 8); left &= 255;

196. tmp = ra_scratch(as, rset_exclude(rset_exclude(allow, left), right));

197. emit_fai(as, pi, rt, tmp, ofs);

198. emit_tab(as, PPCI_ADD, tmp, left, right);

199. return;

200. }

201. if (!checki16(ofs)) {

202. Reg left = ra_alloc1(as, ref, allow);

203. Reg right = ra_allock(as, ofs, rset_exclude(allow, left));

204. emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right);

205. return;

206. }

207. }

208. }

209. base = ra_alloc1(as, ref, allow);

210. emit_fai(as, pi, rt, base, ofs);

211. }

213. /* Fuse XLOAD/XSTORE reference into indexed-only load/store operand. */

214. ‌static void asm_fusexrefx(ASMState *as, PPCIns pi, Reg rt, IRRef ref,

215. RegSet allow)

216. {

217. IRIns *ira = IR(ref);

218. Reg right, left;

219. if (canfuse(as, ira) && ira->o == IR_ADD && ra_noreg(ira->r)) {

220. left = ra_alloc2(as, ira, allow);

221. right = (left >> 8); left &= 255;

222. } else {

223. right = ra_alloc1(as, ref, allow);

224. left = RID_R0;

225. }

226. emit_tab(as, pi, rt, left, right);

227. }

229. /* Fuse to multiply-add/sub instruction. */

230. ‌static int asm_fusemadd(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pir)

231. {

232. IRRef lref = ir->op1, rref = ir->op2;

233. IRIns *irm;

234. if (lref != rref &&

235. ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) &&

236. ra_noreg(irm->r)) ||

237. (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) &&

238. (rref = lref, pi = pir, ra_noreg(irm->r))))) {

239. Reg dest = ra_dest(as, ir, RSET_FPR);

240. Reg add = ra_alloc1(as, rref, RSET_FPR);

241. Reg right, left = ra_alloc2(as, irm, rset_exclude(RSET_FPR, add));

242. right = (left >> 8); left &= 255;

243. emit_facb(as, pi, dest, left, right, add);

244. return 1;

245. }

246. return 0;

247. }

249. /* -- Calls --------------------------------------------------------------- */

251. /* Generate a call to a C function. */

252. ‌static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)

253. {

254. uint32_t n, nargs = CCI_XNARGS(ci);

255. int32_t ofs = 8;

256. Reg gpr = REGARG_FIRSTGPR, fpr = REGARG_FIRSTFPR;

257. if ((void *)ci->func)

258. emit_call(as, (void *)ci->func);

259. for (n = 0; n < nargs; n++) {  /* Setup args. */

260. IRRef ref = args[n];

261. if (ref) {

262. IRIns *ir = IR(ref);

263. if (irt_isfp(ir->t)) {

264. if (fpr <= REGARG_LASTFPR) {

265. lua_assert(rset_test(as->freeset, fpr));  /* Already evicted. */

266. ra_leftov(as, fpr, ref);

267. fpr++;

268. } else {

269. Reg r = ra_alloc1(as, ref, RSET_FPR);

270. if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;

271. emit_spstore(as, ir, r, ofs);

272. ofs += irt_isnum(ir->t) ? 8 : 4;

273. }

274. } else {

275. if (gpr <= REGARG_LASTGPR) {

276. lua_assert(rset_test(as->freeset, gpr));  /* Already evicted. */

277. ra_leftov(as, gpr, ref);

278. gpr++;

279. } else {

280. Reg r = ra_alloc1(as, ref, RSET_GPR);

281. emit_spstore(as, ir, r, ofs);

282. ofs += 4;

283. }

284. }

285. } else {

286. if (gpr <= REGARG_LASTGPR)

287. gpr++;

288. else

289. ofs += 4;

290. }

291. checkmclim(as);

292. }

293. if ((ci->flags & CCI_VARARG))  /* Vararg calls need to know about FPR use. */

294. emit_tab(as, fpr == REGARG_FIRSTFPR ? PPCI_CRXOR : PPCI_CREQV, 6, 6, 6);

295. }

297. /* Setup result reg/sp for call. Evict scratch regs. */

298. ‌static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)

299. {

300. RegSet drop = RSET_SCRATCH;

301. int hiop = ((ir+1)->o == IR_HIOP);

302. if ((ci->flags & CCI_NOFPRCLOBBER))

303. drop &= ~RSET_FPR;

304. if (ra_hasreg(ir->r))

305. rset_clear(drop, ir->r);  /* Dest reg handled below. */

306. if (hiop && ra_hasreg((ir+1)->r))

307. rset_clear(drop, (ir+1)->r);  /* Dest reg handled below. */

308. ra_evictset(as, drop);  /* Evictions must be performed first. */

309. if (ra_used(ir)) {

310. lua_assert(!irt_ispri(ir->t));

311. if (irt_isfp(ir->t)) {

312. if ((ci->flags & CCI_CASTU64)) {

313. /* Use spill slot or temp slots. */

314. int32_t ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP;

315. Reg dest = ir->r;

316. if (ra_hasreg(dest)) {

317. ra_free(as, dest);

318. ra_modified(as, dest);

319. emit_fai(as, PPCI_LFD, dest, RID_SP, ofs);

320. }

321. emit_tai(as, PPCI_STW, RID_RETHI, RID_SP, ofs);

322. emit_tai(as, PPCI_STW, RID_RETLO, RID_SP, ofs+4);

323. } else {

324. ra_destreg(as, ir, RID_FPRET);

325. }

326. #if LJ_32

327. } else if (hiop) {

328. ra_destpair(as, ir);

329. #endif

330. } else {

331. ra_destreg(as, ir, RID_RET);

332. }

333. }

334. }

336. ‌static void asm_callx(ASMState *as, IRIns *ir)

337. {

338. IRRef args[CCI_NARGS_MAX*2];

339. CCallInfo ci;

340. IRRef func;

341. IRIns *irf;

342. ci.flags = asm_callx_flags(as, ir);

343. asm_collectargs(as, ir, &ci, args);

344. asm_setupresult(as, ir, &ci);

345. func = ir->op2; irf = IR(func);

346. if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }

347. if (irref_isk(func)) {  /* Call to constant address. */

348. ci.func = (ASMFunction)(void *)(intptr_t)(irf->i);

349. } else {  /* Need a non-argument register for indirect calls. */

350. RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1);

351. Reg freg = ra_alloc1(as, func, allow);

352. *--as->mcp = PPCI_BCTRL;

353. *--as->mcp = PPCI_MTCTR | PPCF_T(freg);

354. ci.func = (ASMFunction)(void *)0;

355. }

356. asm_gencall(as, &ci, args);

357. }

359. /* -- Returns ------------------------------------------------------------- */

361. /* Return to lower frame. Guard that it goes to the right spot. */

362. ‌static void asm_retf(ASMState *as, IRIns *ir)

363. {

364. Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);

365. void *pc = ir_kptr(IR(ir->op2));

366. int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));

367. as->topslot -= (BCReg)delta;

368. if ((int32_t)as->topslot < 0) as->topslot = 0;

369. irt_setmark(IR(REF_BASE)->t);  /* Children must not coalesce with BASE reg. */

370. emit_setgl(as, base, jit_base);

371. emit_addptr(as, base, -8*delta);

372. asm_guardcc(as, CC_NE);

373. emit_ab(as, PPCI_CMPW, RID_TMP,

374. ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base)));

375. emit_tai(as, PPCI_LWZ, RID_TMP, base, -8);

376. }

378. /* -- Type conversions ---------------------------------------------------- */

380. ‌static void asm_tointg(ASMState *as, IRIns *ir, Reg left)

381. {

382. RegSet allow = RSET_FPR;

383. Reg tmp = ra_scratch(as, rset_clear(allow, left));

384. Reg fbias = ra_scratch(as, rset_clear(allow, tmp));

385. Reg dest = ra_dest(as, ir, RSET_GPR);

386. Reg hibias = ra_allock(as, 0x43300000, rset_exclude(RSET_GPR, dest));

387. asm_guardcc(as, CC_NE);

388. emit_fab(as, PPCI_FCMPU, 0, tmp, left);

389. emit_fab(as, PPCI_FSUB, tmp, tmp, fbias);

390. emit_fai(as, PPCI_LFD, tmp, RID_SP, SPOFS_TMP);

391. emit_tai(as, PPCI_STW, RID_TMP, RID_SP, SPOFS_TMPLO);

392. emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);

393. emit_asi(as, PPCI_XORIS, RID_TMP, dest, 0x8000);

394. emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);

395. emit_lsptr(as, PPCI_LFS, (fbias & 31),

396. (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)),

397. RSET_GPR);

398. emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);

399. emit_fb(as, PPCI_FCTIWZ, tmp, left);

400. }

402. ‌static void asm_tobit(ASMState *as, IRIns *ir)

403. {

404. RegSet allow = RSET_FPR;

405. Reg dest = ra_dest(as, ir, RSET_GPR);

406. Reg left = ra_alloc1(as, ir->op1, allow);

407. Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));

408. Reg tmp = ra_scratch(as, rset_clear(allow, right));

409. emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);

410. emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);

411. emit_fab(as, PPCI_FADD, tmp, left, right);

412. }

414. ‌static void asm_conv(ASMState *as, IRIns *ir)

415. {

416. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);

417. int stfp = (st == IRT_NUM || st == IRT_FLOAT);

418. IRRef lref = ir->op1;

419. lua_assert(irt_type(ir->t) != st);

420. lua_assert(!(irt_isint64(ir->t) ||

421. (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */

422. if (irt_isfp(ir->t)) {

423. Reg dest = ra_dest(as, ir, RSET_FPR);

424. if (stfp) {  /* FP to FP conversion. */

425. if (st == IRT_NUM)  /* double -> float conversion. */

426. emit_fb(as, PPCI_FRSP, dest, ra_alloc1(as, lref, RSET_FPR));

427. else  /* float -> double conversion is a no-op on PPC. */

428. ra_leftov(as, dest, lref);  /* Do nothing, but may need to move regs. */

429. } else {  /* Integer to FP conversion. */

430. /* IRT_INT: Flip hibit, bias with 2^52, subtract 2^52+2^31. */

431. /* IRT_U32: Bias with 2^52, subtract 2^52. */

432. RegSet allow = RSET_GPR;

433. Reg left = ra_alloc1(as, lref, allow);

434. Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, left));

435. Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest));

436. const float *kbias;

437. if (irt_isfloat(ir->t)) emit_fb(as, PPCI_FRSP, dest, dest);

438. emit_fab(as, PPCI_FSUB, dest, dest, fbias);

439. emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP);

440. kbias = (const float *)lj_ir_k64_find(as->J, U64x(59800004,59800000));

441. if (st == IRT_U32) kbias++;

442. emit_lsptr(as, PPCI_LFS, (fbias & 31), (void *)kbias,

443. rset_clear(allow, hibias));

444. emit_tai(as, PPCI_STW, st == IRT_U32 ? left : RID_TMP,

445. RID_SP, SPOFS_TMPLO);

446. emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);

447. if (st != IRT_U32) emit_asi(as, PPCI_XORIS, RID_TMP, left, 0x8000);

448. }

449. } else if (stfp) {  /* FP to integer conversion. */

450. if (irt_isguard(ir->t)) {

451. /* Checked conversions are only supported from number to int. */

452. lua_assert(irt_isint(ir->t) && st == IRT_NUM);

453. asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));

454. } else {

455. Reg dest = ra_dest(as, ir, RSET_GPR);

456. Reg left = ra_alloc1(as, lref, RSET_FPR);

457. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));

458. if (irt_isu32(ir->t)) {

459. /* Convert both x and x-2^31 to int and merge results. */

460. Reg tmpi = ra_scratch(as, rset_exclude(RSET_GPR, dest));

461. emit_asb(as, PPCI_OR, dest, dest, tmpi);  /* Select with mask idiom. */

462. emit_asb(as, PPCI_AND, tmpi, tmpi, RID_TMP);

463. emit_asb(as, PPCI_ANDC, dest, dest, RID_TMP);

464. emit_tai(as, PPCI_LWZ, tmpi, RID_SP, SPOFS_TMPLO);  /* tmp = (int)(x) */

465. emit_tai(as, PPCI_ADDIS, dest, dest, 0x8000);  /* dest += 2^31 */

466. emit_asb(as, PPCI_SRAWI, RID_TMP, dest, 31);  /* mask = -(dest < 0) */

467. emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);

468. emit_tai(as, PPCI_LWZ, dest,

469. RID_SP, SPOFS_TMPLO);  /* dest = (int)(x-2^31) */

470. emit_fb(as, PPCI_FCTIWZ, tmp, left);

471. emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);

472. emit_fb(as, PPCI_FCTIWZ, tmp, tmp);

473. emit_fab(as, PPCI_FSUB, tmp, left, tmp);

474. emit_lsptr(as, PPCI_LFS, (tmp & 31),

475. (void *)lj_ir_k64_find(as->J, U64x(4f000000,00000000)),

476. RSET_GPR);

477. } else {

478. emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);

479. emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);

480. emit_fb(as, PPCI_FCTIWZ, tmp, left);

481. }

482. }

483. } else {

484. Reg dest = ra_dest(as, ir, RSET_GPR);

485. if (st >= IRT_I8 && st <= IRT_U16) {  /* Extend to 32 bit integer. */

486. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);

487. lua_assert(irt_isint(ir->t) || irt_isu32(ir->t));

488. if ((ir->op2 & IRCONV_SEXT))

489. emit_as(as, st == IRT_I8 ? PPCI_EXTSB : PPCI_EXTSH, dest, left);

490. else

491. emit_rot(as, PPCI_RLWINM, dest, left, 0, st == IRT_U8 ? 24 : 16, 31);

492. } else {  /* 32/64 bit integer conversions. */

493. /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */

494. ra_leftov(as, dest, lref);  /* Do nothing, but may need to move regs. */

495. }

496. }

497. }

499. ‌static void asm_strto(ASMState *as, IRIns *ir)

500. {

501. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];

502. IRRef args[2];

503. int32_t ofs;

504. RegSet drop = RSET_SCRATCH;

505. if (ra_hasreg(ir->r)) rset_set(drop, ir->r);  /* Spill dest reg (if any). */

506. ra_evictset(as, drop);

507. asm_guardcc(as, CC_EQ);

508. emit_ai(as, PPCI_CMPWI, RID_RET, 0);  /* Test return status. */

509. args[0] = ir->op1;      /* GCstr *str */

510. args[1] = ASMREF_TMP1;  /* TValue *n  */

511. asm_gencall(as, ci, args);

512. /* Store the result to the spill slot or temp slots. */

513. ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP;

514. emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_SP, ofs);

515. }

517. /* -- Memory references --------------------------------------------------- */

519. /* Get pointer to TValue. */

520. ‌static void asm_tvptr(ASMState *as, Reg dest, IRRef ref)

521. {

522. IRIns *ir = IR(ref);

523. if (irt_isnum(ir->t)) {

524. if (irref_isk(ref))  /* Use the number constant itself as a TValue. */

525. ra_allockreg(as, i32ptr(ir_knum(ir)), dest);

526. else  /* Otherwise force a spill and use the spill slot. */

527. emit_tai(as, PPCI_ADDI, dest, RID_SP, ra_spill(as, ir));

528. } else {

529. /* Otherwise use g->tmptv to hold the TValue. */

530. RegSet allow = rset_exclude(RSET_GPR, dest);

531. Reg type;

532. emit_tai(as, PPCI_ADDI, dest, RID_JGL, (int32_t)offsetof(global_State, tmptv)-32768);

533. if (!irt_ispri(ir->t)) {

534. Reg src = ra_alloc1(as, ref, allow);

535. emit_setgl(as, src, tmptv.gcr);

536. }

537. type = ra_allock(as, irt_toitype(ir->t), allow);

538. emit_setgl(as, type, tmptv.it);

539. }

540. }

542. ‌static void asm_aref(ASMState *as, IRIns *ir)

543. {

544. Reg dest = ra_dest(as, ir, RSET_GPR);

545. Reg idx, base;

546. if (irref_isk(ir->op2)) {

547. IRRef tab = IR(ir->op1)->op1;

548. int32_t ofs = asm_fuseabase(as, tab);

549. IRRef refa = ofs ? tab : ir->op1;

550. ofs += 8*IR(ir->op2)->i;

551. if (checki16(ofs)) {

552. base = ra_alloc1(as, refa, RSET_GPR);

553. emit_tai(as, PPCI_ADDI, dest, base, ofs);

554. return;

555. }

556. }

557. base = ra_alloc1(as, ir->op1, RSET_GPR);

558. idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));

559. emit_tab(as, PPCI_ADD, dest, RID_TMP, base);

560. emit_slwi(as, RID_TMP, idx, 3);

561. }

563. /* Inlined hash lookup. Specialized for key type and for const keys.

564. ** The equivalent C code is:

565. **   Node *n = hashkey(t, key);

566. **   do {

567. **     if (lj_obj_equal(&n->key, key)) return &n->val;

568. **   } while ((n = nextnode(n)));

569. **   return niltv(L);

570. */

571. ‌static void asm_href(ASMState *as, IRIns *ir, IROp merge)

572. {

573. RegSet allow = RSET_GPR;

574. int destused = ra_used(ir);

575. Reg dest = ra_dest(as, ir, allow);

576. Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));

577. Reg key = RID_NONE, tmp1 = RID_TMP, tmp2;

578. Reg tisnum = RID_NONE, tmpnum = RID_NONE;

579. IRRef refkey = ir->op2;

580. IRIns *irkey = IR(refkey);

581. IRType1 kt = irkey->t;

582. uint32_t khash;

583. MCLabel l_end, l_loop, l_next;

585. rset_clear(allow, tab);

586. if (irt_isnum(kt)) {

587. key = ra_alloc1(as, refkey, RSET_FPR);

588. tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key));

589. tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow);

590. rset_clear(allow, tisnum);

591. } else if (!irt_ispri(kt)) {

592. key = ra_alloc1(as, refkey, allow);

593. rset_clear(allow, key);

594. }

595. tmp2 = ra_scratch(as, allow);

596. rset_clear(allow, tmp2);

598. /* Key not found in chain: jump to exit (if merged) or load niltv. */

599. l_end = emit_label(as);

600. as->invmcp = NULL;

601. if (merge == IR_NE)

602. asm_guardcc(as, CC_EQ);

603. else if (destused)

604. emit_loada(as, dest, niltvg(J2G(as->J)));

606. /* Follow hash chain until the end. */

607. l_loop = --as->mcp;

608. emit_ai(as, PPCI_CMPWI, dest, 0);

609. emit_tai(as, PPCI_LWZ, dest, dest, (int32_t)offsetof(Node, next));

610. l_next = emit_label(as);

612. /* Type and value comparison. */

613. if (merge == IR_EQ)

614. asm_guardcc(as, CC_EQ);

615. else

616. emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);

617. if (irt_isnum(kt)) {

618. emit_fab(as, PPCI_FCMPU, 0, tmpnum, key);

619. emit_condbranch(as, PPCI_BC, CC_GE, l_next);

620. emit_ab(as, PPCI_CMPLW, tmp1, tisnum);

621. emit_fai(as, PPCI_LFD, tmpnum, dest, (int32_t)offsetof(Node, key.n));

622. } else {

623. if (!irt_ispri(kt)) {

624. emit_ab(as, PPCI_CMPW, tmp2, key);

625. emit_condbranch(as, PPCI_BC, CC_NE, l_next);

626. }

627. emit_ai(as, PPCI_CMPWI, tmp1, irt_toitype(irkey->t));

628. if (!irt_ispri(kt))

629. emit_tai(as, PPCI_LWZ, tmp2, dest, (int32_t)offsetof(Node, key.gcr));

630. }

631. emit_tai(as, PPCI_LWZ, tmp1, dest, (int32_t)offsetof(Node, key.it));

632. *l_loop = PPCI_BC | PPCF_Y | PPCF_CC(CC_NE) |

633. (((char *)as->mcp-(char *)l_loop) & 0xffffu);

635. /* Load main position relative to tab->node into dest. */

636. khash = irref_isk(refkey) ? ir_khash(irkey) : 1;

637. if (khash == 0) {

638. emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node));

639. } else {

640. Reg tmphash = tmp1;

641. if (irref_isk(refkey))

642. tmphash = ra_allock(as, khash, allow);

643. emit_tab(as, PPCI_ADD, dest, dest, tmp1);

644. emit_tai(as, PPCI_MULLI, tmp1, tmp1, sizeof(Node));

645. emit_asb(as, PPCI_AND, tmp1, tmp2, tmphash);

646. emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node));

647. emit_tai(as, PPCI_LWZ, tmp2, tab, (int32_t)offsetof(GCtab, hmask));

648. if (irref_isk(refkey)) {

649. /* Nothing to do. */

650. } else if (irt_isstr(kt)) {

651. emit_tai(as, PPCI_LWZ, tmp1, key, (int32_t)offsetof(GCstr, hash));

652. } else {  /* Must match with hash*() in lj_tab.c. */

653. emit_tab(as, PPCI_SUBF, tmp1, tmp2, tmp1);

654. emit_rotlwi(as, tmp2, tmp2, HASH_ROT3);

655. emit_asb(as, PPCI_XOR, tmp1, tmp1, tmp2);

656. emit_rotlwi(as, tmp1, tmp1, (HASH_ROT2+HASH_ROT1)&31);

657. emit_tab(as, PPCI_SUBF, tmp2, dest, tmp2);

658. if (irt_isnum(kt)) {

659. int32_t ofs = ra_spill(as, irkey);

660. emit_asb(as, PPCI_XOR, tmp2, tmp2, tmp1);

661. emit_rotlwi(as, dest, tmp1, HASH_ROT1);

662. emit_tab(as, PPCI_ADD, tmp1, tmp1, tmp1);

663. emit_tai(as, PPCI_LWZ, tmp2, RID_SP, ofs+4);

664. emit_tai(as, PPCI_LWZ, tmp1, RID_SP, ofs);

665. } else {

666. emit_asb(as, PPCI_XOR, tmp2, key, tmp1);

667. emit_rotlwi(as, dest, tmp1, HASH_ROT1);

668. emit_tai(as, PPCI_ADDI, tmp1, tmp2, HASH_BIAS);

669. emit_tai(as, PPCI_ADDIS, tmp2, key, (HASH_BIAS + 32768)>>16);

670. }

671. }

672. }

673. }

675. ‌static void asm_hrefk(ASMState *as, IRIns *ir)

676. {

677. IRIns *kslot = IR(ir->op2);

678. IRIns *irkey = IR(kslot->op1);

679. int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));

680. int32_t kofs = ofs + (int32_t)offsetof(Node, key);

681. Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;

682. Reg node = ra_alloc1(as, ir->op1, RSET_GPR);

683. Reg key = RID_NONE, type = RID_TMP, idx = node;

684. RegSet allow = rset_exclude(RSET_GPR, node);

685. lua_assert(ofs % sizeof(Node) == 0);

686. if (ofs > 32736) {

687. idx = dest;

688. rset_clear(allow, dest);

689. kofs = (int32_t)offsetof(Node, key);

690. } else if (ra_hasreg(dest)) {

691. emit_tai(as, PPCI_ADDI, dest, node, ofs);

692. }

693. asm_guardcc(as, CC_NE);

694. if (!irt_ispri(irkey->t)) {

695. key = ra_scratch(as, allow);

696. rset_clear(allow, key);

697. }

698. rset_clear(allow, type);

699. if (irt_isnum(irkey->t)) {

700. emit_cmpi(as, key, (int32_t)ir_knum(irkey)->u32.lo);

701. asm_guardcc(as, CC_NE);

702. emit_cmpi(as, type, (int32_t)ir_knum(irkey)->u32.hi);

703. } else {

704. if (ra_hasreg(key)) {

705. emit_cmpi(as, key, irkey->i);  /* May use RID_TMP, i.e. type. */

706. asm_guardcc(as, CC_NE);

707. }

708. emit_ai(as, PPCI_CMPWI, type, irt_toitype(irkey->t));

709. }

710. if (ra_hasreg(key)) emit_tai(as, PPCI_LWZ, key, idx, kofs+4);

711. emit_tai(as, PPCI_LWZ, type, idx, kofs);

712. if (ofs > 32736) {

713. emit_tai(as, PPCI_ADDIS, dest, dest, (ofs + 32768) >> 16);

714. emit_tai(as, PPCI_ADDI, dest, node, ofs);

715. }

716. }

718. ‌static void asm_uref(ASMState *as, IRIns *ir)

719. {

720. /* NYI: Check that UREFO is still open and not aliasing a slot. */

721. Reg dest = ra_dest(as, ir, RSET_GPR);

722. if (irref_isk(ir->op1)) {

723. GCfunc *fn = ir_kfunc(IR(ir->op1));

724. MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;

725. emit_lsptr(as, PPCI_LWZ, dest, v, RSET_GPR);

726. } else {

727. Reg uv = ra_scratch(as, RSET_GPR);

728. Reg func = ra_alloc1(as, ir->op1, RSET_GPR);

729. if (ir->o == IR_UREFC) {

730. asm_guardcc(as, CC_NE);

731. emit_ai(as, PPCI_CMPWI, RID_TMP, 1);

732. emit_tai(as, PPCI_ADDI, dest, uv, (int32_t)offsetof(GCupval, tv));

733. emit_tai(as, PPCI_LBZ, RID_TMP, uv, (int32_t)offsetof(GCupval, closed));

734. } else {

735. emit_tai(as, PPCI_LWZ, dest, uv, (int32_t)offsetof(GCupval, v));

736. }

737. emit_tai(as, PPCI_LWZ, uv, func,

738. (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8));

739. }

740. }

742. ‌static void asm_fref(ASMState *as, IRIns *ir)

743. {

744. UNUSED(as); UNUSED(ir);

745. lua_assert(!ra_used(ir));

746. }

748. ‌static void asm_strref(ASMState *as, IRIns *ir)

749. {

750. Reg dest = ra_dest(as, ir, RSET_GPR);

751. IRRef ref = ir->op2, refk = ir->op1;

752. int32_t ofs = (int32_t)sizeof(GCstr);

753. Reg r;

754. if (irref_isk(ref)) {

755. IRRef tmp = refk; refk = ref; ref = tmp;

756. } else if (!irref_isk(refk)) {

757. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);

758. IRIns *irr = IR(ir->op2);

759. if (ra_hasreg(irr->r)) {

760. ra_noweak(as, irr->r);

761. right = irr->r;

762. } else if (mayfuse(as, irr->op2) &&

763. irr->o == IR_ADD && irref_isk(irr->op2) &&

764. checki16(ofs + IR(irr->op2)->i)) {

765. ofs += IR(irr->op2)->i;

766. right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));

767. } else {

768. right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left));

769. }

770. emit_tai(as, PPCI_ADDI, dest, dest, ofs);

771. emit_tab(as, PPCI_ADD, dest, left, right);

772. return;

773. }

774. r = ra_alloc1(as, ref, RSET_GPR);

775. ofs += IR(refk)->i;

776. if (checki16(ofs))

777. emit_tai(as, PPCI_ADDI, dest, r, ofs);

778. else

779. emit_tab(as, PPCI_ADD, dest, r,

780. ra_allock(as, ofs, rset_exclude(RSET_GPR, r)));

781. }

783. /* -- Loads and stores ---------------------------------------------------- */

785. ‌static PPCIns asm_fxloadins(IRIns *ir)

786. {

787. switch (irt_type(ir->t)) {

788. case IRT_I8: return PPCI_LBZ;  /* Needs sign-extension. */

789. case IRT_U8: return PPCI_LBZ;

790. case IRT_I16: return PPCI_LHA;

791. case IRT_U16: return PPCI_LHZ;

792. case IRT_NUM: return PPCI_LFD;

793. case IRT_FLOAT: return PPCI_LFS;

794. default: return PPCI_LWZ;

795. }

796. }

798. ‌static PPCIns asm_fxstoreins(IRIns *ir)

799. {

800. switch (irt_type(ir->t)) {

801. case IRT_I8: case IRT_U8: return PPCI_STB;

802. case IRT_I16: case IRT_U16: return PPCI_STH;

803. case IRT_NUM: return PPCI_STFD;

804. case IRT_FLOAT: return PPCI_STFS;

805. default: return PPCI_STW;

806. }

807. }

809. ‌static void asm_fload(ASMState *as, IRIns *ir)

810. {

811. Reg dest = ra_dest(as, ir, RSET_GPR);

812. Reg idx = ra_alloc1(as, ir->op1, RSET_GPR);

813. PPCIns pi = asm_fxloadins(ir);

814. int32_t ofs;

815. if (ir->op2 == IRFL_TAB_ARRAY) {

816. ofs = asm_fuseabase(as, ir->op1);

817. if (ofs) {  /* Turn the t->array load into an add for colocated arrays. */

818. emit_tai(as, PPCI_ADDI, dest, idx, ofs);

819. return;

820. }

821. }

822. ofs = field_ofs[ir->op2];

823. lua_assert(!irt_isi8(ir->t));

824. emit_tai(as, pi, dest, idx, ofs);

825. }

827. ‌static void asm_fstore(ASMState *as, IRIns *ir)

828. {

829. if (ir->r != RID_SINK) {

830. Reg src = ra_alloc1(as, ir->op2, RSET_GPR);

831. IRIns *irf = IR(ir->op1);

832. Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));

833. int32_t ofs = field_ofs[irf->op2];

834. PPCIns pi = asm_fxstoreins(ir);

835. emit_tai(as, pi, src, idx, ofs);

836. }

837. }

839. ‌static void asm_xload(ASMState *as, IRIns *ir)

840. {

841. Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);

842. lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED));

843. if (irt_isi8(ir->t))

844. emit_as(as, PPCI_EXTSB, dest, dest);

845. asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0);

846. }

848. ‌static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs)

849. {

850. IRIns *irb;

851. if (ir->r == RID_SINK)

852. return;

853. if (ofs == 0 && mayfuse(as, ir->op2) && (irb = IR(ir->op2))->o == IR_BSWAP &&

854. ra_noreg(irb->r) && (irt_isint(ir->t) || irt_isu32(ir->t))) {

855. /* Fuse BSWAP with XSTORE to stwbrx. */

856. Reg src = ra_alloc1(as, irb->op1, RSET_GPR);

857. asm_fusexrefx(as, PPCI_STWBRX, src, ir->op1, rset_exclude(RSET_GPR, src));

858. } else {

859. Reg src = ra_alloc1(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);

860. asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,

861. rset_exclude(RSET_GPR, src), ofs);

862. }

863. }

865. ‌#define asm_xstore(as, ir)        asm_xstore_(as, ir, 0)

867. ‌static void asm_ahuvload(ASMState *as, IRIns *ir)

868. {

869. IRType1 t = ir->t;

870. Reg dest = RID_NONE, type = RID_TMP, tmp = RID_TMP, idx;

871. RegSet allow = RSET_GPR;

872. int32_t ofs = AHUREF_LSX;

873. if (ra_used(ir)) {

874. lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));

875. if (!irt_isnum(t)) ofs = 0;

876. dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);

877. rset_clear(allow, dest);

878. }

879. idx = asm_fuseahuref(as, ir->op1, &ofs, allow);

880. if (irt_isnum(t)) {

881. Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, rset_exclude(allow, idx));

882. asm_guardcc(as, CC_GE);

883. emit_ab(as, PPCI_CMPLW, type, tisnum);

884. if (ra_hasreg(dest)) {

885. if (ofs == AHUREF_LSX) {

886. tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR,

887. (idx&255)), (idx>>8)));

888. emit_fab(as, PPCI_LFDX, dest, (idx&255), tmp);

889. } else {

890. emit_fai(as, PPCI_LFD, dest, idx, ofs);

891. }

892. }

893. } else {

894. asm_guardcc(as, CC_NE);

895. emit_ai(as, PPCI_CMPWI, type, irt_toitype(t));

896. if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, idx, ofs+4);

897. }

898. if (ofs == AHUREF_LSX) {

899. emit_tab(as, PPCI_LWZX, type, (idx&255), tmp);

900. emit_slwi(as, tmp, (idx>>8), 3);

901. } else {

902. emit_tai(as, PPCI_LWZ, type, idx, ofs);

903. }

904. }

906. ‌static void asm_ahustore(ASMState *as, IRIns *ir)

907. {

908. RegSet allow = RSET_GPR;

909. Reg idx, src = RID_NONE, type = RID_NONE;

910. int32_t ofs = AHUREF_LSX;

911. if (ir->r == RID_SINK)

912. return;

913. if (irt_isnum(ir->t)) {

914. src = ra_alloc1(as, ir->op2, RSET_FPR);

915. } else {

916. if (!irt_ispri(ir->t)) {

917. src = ra_alloc1(as, ir->op2, allow);

918. rset_clear(allow, src);

919. ofs = 0;

920. }

921. type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);

922. rset_clear(allow, type);

923. }

924. idx = asm_fuseahuref(as, ir->op1, &ofs, allow);

925. if (irt_isnum(ir->t)) {

926. if (ofs == AHUREF_LSX) {

927. emit_fab(as, PPCI_STFDX, src, (idx&255), RID_TMP);

928. emit_slwi(as, RID_TMP, (idx>>8), 3);

929. } else {

930. emit_fai(as, PPCI_STFD, src, idx, ofs);

931. }

932. } else {

933. if (ra_hasreg(src))

934. emit_tai(as, PPCI_STW, src, idx, ofs+4);

935. if (ofs == AHUREF_LSX) {

936. emit_tab(as, PPCI_STWX, type, (idx&255), RID_TMP);

937. emit_slwi(as, RID_TMP, (idx>>8), 3);

938. } else {

939. emit_tai(as, PPCI_STW, type, idx, ofs);

940. }

941. }

942. }

944. ‌static void asm_sload(ASMState *as, IRIns *ir)

945. {

946. int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 0 : 4);

947. IRType1 t = ir->t;

948. Reg dest = RID_NONE, type = RID_NONE, base;

949. RegSet allow = RSET_GPR;

950. lua_assert(!(ir->op2 & IRSLOAD_PARENT));  /* Handled by asm_head_side(). */

951. lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK));

952. lua_assert(LJ_DUALNUM ||

953. !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME)));

954. if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {

955. dest = ra_scratch(as, RSET_FPR);

956. asm_tointg(as, ir, dest);

957. t.irt = IRT_NUM;  /* Continue with a regular number type check. */

958. } else if (ra_used(ir)) {

959. lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));

960. dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);

961. rset_clear(allow, dest);

962. base = ra_alloc1(as, REF_BASE, allow);

963. rset_clear(allow, base);

964. if ((ir->op2 & IRSLOAD_CONVERT)) {

965. if (irt_isint(t)) {

966. emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);

967. dest = ra_scratch(as, RSET_FPR);

968. emit_fai(as, PPCI_STFD, dest, RID_SP, SPOFS_TMP);

969. emit_fb(as, PPCI_FCTIWZ, dest, dest);

970. t.irt = IRT_NUM;  /* Check for original type. */

971. } else {

972. Reg tmp = ra_scratch(as, allow);

973. Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, tmp));

974. Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest));

975. emit_fab(as, PPCI_FSUB, dest, dest, fbias);

976. emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP);

977. emit_lsptr(as, PPCI_LFS, (fbias & 31),

978. (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)),

979. rset_clear(allow, hibias));

980. emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPLO);

981. emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);

982. emit_asi(as, PPCI_XORIS, tmp, tmp, 0x8000);

983. dest = tmp;

984. t.irt = IRT_INT;  /* Check for original type. */

985. }

986. }

987. goto dotypecheck;

988. }

989. base = ra_alloc1(as, REF_BASE, allow);

990. rset_clear(allow, base);

991. dotypecheck:

992. if (irt_isnum(t)) {

993. if ((ir->op2 & IRSLOAD_TYPECHECK)) {

994. Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow);

995. asm_guardcc(as, CC_GE);

996. emit_ab(as, PPCI_CMPLW, RID_TMP, tisnum);

997. type = RID_TMP;

998. }

999. if (ra_hasreg(dest)) emit_fai(as, PPCI_LFD, dest, base, ofs-4);

1000. } else {

1001. if ((ir->op2 & IRSLOAD_TYPECHECK)) {

1002. asm_guardcc(as, CC_NE);

1003. emit_ai(as, PPCI_CMPWI, RID_TMP, irt_toitype(t));

1004. type = RID_TMP;

1005. }

1006. if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, base, ofs);

1007. }

1008. if (ra_hasreg(type)) emit_tai(as, PPCI_LWZ, type, base, ofs-4);

1009. }

1011. /* -- Allocations --------------------------------------------------------- */

1013. #if LJ_HASFFI

1014. ‌static void asm_cnew(ASMState *as, IRIns *ir)

1015. {

1016. CTState *cts = ctype_ctsG(J2G(as->J));

1017. CTypeID id = (CTypeID)IR(ir->op1)->i;

1018. CTSize sz;

1019. CTInfo info = lj_ctype_info(cts, id, &sz);

1020. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];

1021. IRRef args[4];

1022. RegSet drop = RSET_SCRATCH;

1023. lua_assert(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL));

1025. as->gcsteps++;

1026. if (ra_hasreg(ir->r))

1027. rset_clear(drop, ir->r);  /* Dest reg handled below. */

1028. ra_evictset(as, drop);

1029. if (ra_used(ir))

1030. ra_destreg(as, ir, RID_RET);  /* GCcdata * */

1032. /* Initialize immutable cdata object. */

1033. if (ir->o == IR_CNEWI) {

1034. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);

1035. int32_t ofs = sizeof(GCcdata);

1036. lua_assert(sz == 4 || sz == 8);

1037. if (sz == 8) {

1038. ofs += 4;

1039. lua_assert((ir+1)->o == IR_HIOP);

1040. }

1041. for (;;) {

1042. Reg r = ra_alloc1(as, ir->op2, allow);

1043. emit_tai(as, PPCI_STW, r, RID_RET, ofs);

1044. rset_clear(allow, r);

1045. if (ofs == sizeof(GCcdata)) break;

1046. ofs -= 4; ir++;

1047. }

1048. } else if (ir->op2 != REF_NIL) {  /* Create VLA/VLS/aligned cdata. */

1049. ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];

1050. args[0] = ASMREF_L;     /* lua_State *L */

1051. args[1] = ir->op1;      /* CTypeID id   */

1052. args[2] = ir->op2;      /* CTSize sz    */

1053. args[3] = ASMREF_TMP1;  /* CTSize align */

1054. asm_gencall(as, ci, args);

1055. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));

1056. return;

1057. }

1059. /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */

1060. emit_tai(as, PPCI_STB, RID_RET+1, RID_RET, offsetof(GCcdata, gct));

1061. emit_tai(as, PPCI_STH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid));

1062. emit_ti(as, PPCI_LI, RID_RET+1, ~LJ_TCDATA);

1063. emit_ti(as, PPCI_LI, RID_TMP, id);  /* Lower 16 bit used. Sign-ext ok. */

1064. args[0] = ASMREF_L;     /* lua_State *L */

1065. args[1] = ASMREF_TMP1;  /* MSize size   */

1066. asm_gencall(as, ci, args);

1067. ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),

1068. ra_releasetmp(as, ASMREF_TMP1));

1069. }

1070. #else

1071. ‌#define asm_cnew(as, ir)        ((void)0)

1072. #endif

1074. /* -- Write barriers ------------------------------------------------------ */

1076. ‌static void asm_tbar(ASMState *as, IRIns *ir)

1077. {

1078. Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);

1079. Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab));

1080. Reg link = RID_TMP;

1081. MCLabel l_end = emit_label(as);

1082. emit_tai(as, PPCI_STW, link, tab, (int32_t)offsetof(GCtab, gclist));

1083. emit_tai(as, PPCI_STB, mark, tab, (int32_t)offsetof(GCtab, marked));

1084. emit_setgl(as, tab, gc.grayagain);

1085. lua_assert(LJ_GC_BLACK == 0x04);

1086. emit_rot(as, PPCI_RLWINM, mark, mark, 0, 30, 28);  /* Clear black bit. */

1087. emit_getgl(as, link, gc.grayagain);

1088. emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);

1089. emit_asi(as, PPCI_ANDIDOT, RID_TMP, mark, LJ_GC_BLACK);

1090. emit_tai(as, PPCI_LBZ, mark, tab, (int32_t)offsetof(GCtab, marked));

1091. }

1093. ‌static void asm_obar(ASMState *as, IRIns *ir)

1094. {

1095. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];

1096. IRRef args[2];

1097. MCLabel l_end;

1098. Reg obj, val, tmp;

1099. /* No need for other object barriers (yet). */

1100. lua_assert(IR(ir->op1)->o == IR_UREFC);

1101. ra_evictset(as, RSET_SCRATCH);

1102. l_end = emit_label(as);

1103. args[0] = ASMREF_TMP1;  /* global_State *g */

1104. args[1] = ir->op1;      /* TValue *tv      */

1105. asm_gencall(as, ci, args);

1106. emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);

1107. obj = IR(ir->op1)->r;

1108. tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));

1109. emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);

1110. emit_asi(as, PPCI_ANDIDOT, tmp, tmp, LJ_GC_BLACK);

1111. emit_condbranch(as, PPCI_BC, CC_EQ, l_end);

1112. emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, LJ_GC_WHITES);

1113. val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));

1114. emit_tai(as, PPCI_LBZ, tmp, obj,

1115. (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));

1116. emit_tai(as, PPCI_LBZ, RID_TMP, val, (int32_t)offsetof(GChead, marked));

1117. }

1119. /* -- Arithmetic and logic operations ------------------------------------- */

1121. ‌static void asm_fparith(ASMState *as, IRIns *ir, PPCIns pi)

1122. {

1123. Reg dest = ra_dest(as, ir, RSET_FPR);

1124. Reg right, left = ra_alloc2(as, ir, RSET_FPR);

1125. right = (left >> 8); left &= 255;

1126. if (pi == PPCI_FMUL)

1127. emit_fac(as, pi, dest, left, right);

1128. else

1129. emit_fab(as, pi, dest, left, right);

1130. }

1132. ‌static void asm_fpunary(ASMState *as, IRIns *ir, PPCIns pi)

1133. {

1134. Reg dest = ra_dest(as, ir, RSET_FPR);

1135. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);

1136. emit_fb(as, pi, dest, left);

1137. }

1139. ‌static void asm_fpmath(ASMState *as, IRIns *ir)

1140. {

1141. if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir))

1142. return;

1143. if (ir->op2 == IRFPM_SQRT && (as->flags & JIT_F_SQRT))

1144. asm_fpunary(as, ir, PPCI_FSQRT);

1145. else

1146. asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);

1147. }

1149. ‌static void asm_add(ASMState *as, IRIns *ir)

1150. {

1151. if (irt_isnum(ir->t)) {

1152. if (!asm_fusemadd(as, ir, PPCI_FMADD, PPCI_FMADD))

1153. asm_fparith(as, ir, PPCI_FADD);

1154. } else {

1155. Reg dest = ra_dest(as, ir, RSET_GPR);

1156. Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1157. PPCIns pi;

1158. if (irref_isk(ir->op2)) {

1159. int32_t k = IR(ir->op2)->i;

1160. if (checki16(k)) {

1161. pi = PPCI_ADDI;

1162. /* May fail due to spills/restores above, but simplifies the logic. */

1163. if (as->flagmcp == as->mcp) {

1164. as->flagmcp = NULL;

1165. as->mcp++;

1166. pi = PPCI_ADDICDOT;

1167. }

1168. emit_tai(as, pi, dest, left, k);

1169. return;

1170. } else if ((k & 0xffff) == 0) {

1171. emit_tai(as, PPCI_ADDIS, dest, left, (k >> 16));

1172. return;

1173. } else if (!as->sectref) {

1174. emit_tai(as, PPCI_ADDIS, dest, dest, (k + 32768) >> 16);

1175. emit_tai(as, PPCI_ADDI, dest, left, k);

1176. return;

1177. }

1178. }

1179. pi = PPCI_ADD;

1180. /* May fail due to spills/restores above, but simplifies the logic. */

1181. if (as->flagmcp == as->mcp) {

1182. as->flagmcp = NULL;

1183. as->mcp++;

1184. pi |= PPCF_DOT;

1185. }

1186. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1187. emit_tab(as, pi, dest, left, right);

1188. }

1189. }

1191. ‌static void asm_sub(ASMState *as, IRIns *ir)

1192. {

1193. if (irt_isnum(ir->t)) {

1194. if (!asm_fusemadd(as, ir, PPCI_FMSUB, PPCI_FNMSUB))

1195. asm_fparith(as, ir, PPCI_FSUB);

1196. } else {

1197. PPCIns pi = PPCI_SUBF;

1198. Reg dest = ra_dest(as, ir, RSET_GPR);

1199. Reg left, right;

1200. if (irref_isk(ir->op1)) {

1201. int32_t k = IR(ir->op1)->i;

1202. if (checki16(k)) {

1203. right = ra_alloc1(as, ir->op2, RSET_GPR);

1204. emit_tai(as, PPCI_SUBFIC, dest, right, k);

1205. return;

1206. }

1207. }

1208. /* May fail due to spills/restores above, but simplifies the logic. */

1209. if (as->flagmcp == as->mcp) {

1210. as->flagmcp = NULL;

1211. as->mcp++;

1212. pi |= PPCF_DOT;

1213. }

1214. left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1215. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1216. emit_tab(as, pi, dest, right, left);  /* Subtract right _from_ left. */

1217. }

1218. }

1220. ‌static void asm_mul(ASMState *as, IRIns *ir)

1221. {

1222. if (irt_isnum(ir->t)) {

1223. asm_fparith(as, ir, PPCI_FMUL);

1224. } else {

1225. PPCIns pi = PPCI_MULLW;

1226. Reg dest = ra_dest(as, ir, RSET_GPR);

1227. Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1228. if (irref_isk(ir->op2)) {

1229. int32_t k = IR(ir->op2)->i;

1230. if (checki16(k)) {

1231. emit_tai(as, PPCI_MULLI, dest, left, k);

1232. return;

1233. }

1234. }

1235. /* May fail due to spills/restores above, but simplifies the logic. */

1236. if (as->flagmcp == as->mcp) {

1237. as->flagmcp = NULL;

1238. as->mcp++;

1239. pi |= PPCF_DOT;

1240. }

1241. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1242. emit_tab(as, pi, dest, left, right);

1243. }

1244. }

1246. ‌#define asm_div(as, ir)                asm_fparith(as, ir, PPCI_FDIV)

1247. ‌#define asm_mod(as, ir)                asm_callid(as, ir, IRCALL_lj_vm_modi)

1248. ‌#define asm_pow(as, ir)                asm_callid(as, ir, IRCALL_lj_vm_powi)

1250. ‌static void asm_neg(ASMState *as, IRIns *ir)

1251. {

1252. if (irt_isnum(ir->t)) {

1253. asm_fpunary(as, ir, PPCI_FNEG);

1254. } else {

1255. Reg dest, left;

1256. PPCIns pi = PPCI_NEG;

1257. if (as->flagmcp == as->mcp) {

1258. as->flagmcp = NULL;

1259. as->mcp++;

1260. pi |= PPCF_DOT;

1261. }

1262. dest = ra_dest(as, ir, RSET_GPR);

1263. left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1264. emit_tab(as, pi, dest, left, 0);

1265. }

1266. }

1268. ‌#define asm_abs(as, ir)                asm_fpunary(as, ir, PPCI_FABS)

1269. ‌#define asm_atan2(as, ir)        asm_callid(as, ir, IRCALL_atan2)

1270. ‌#define asm_ldexp(as, ir)        asm_callid(as, ir, IRCALL_ldexp)

1272. ‌static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi)

1273. {

1274. Reg dest, left, right;

1275. if (as->flagmcp == as->mcp) {

1276. as->flagmcp = NULL;

1277. as->mcp++;

1278. }

1279. asm_guardcc(as, CC_SO);

1280. dest = ra_dest(as, ir, RSET_GPR);

1281. left = ra_alloc2(as, ir, RSET_GPR);

1282. right = (left >> 8); left &= 255;

1283. if (pi == PPCI_SUBFO) { Reg tmp = left; left = right; right = tmp; }

1284. emit_tab(as, pi|PPCF_DOT, dest, left, right);

1285. }

1287. ‌#define asm_addov(as, ir)        asm_arithov(as, ir, PPCI_ADDO)

1288. ‌#define asm_subov(as, ir)        asm_arithov(as, ir, PPCI_SUBFO)

1289. ‌#define asm_mulov(as, ir)        asm_arithov(as, ir, PPCI_MULLWO)

1291. #if LJ_HASFFI

1292. ‌static void asm_add64(ASMState *as, IRIns *ir)

1293. {

1294. Reg dest = ra_dest(as, ir, RSET_GPR);

1295. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);

1296. PPCIns pi = PPCI_ADDE;

1297. if (irref_isk(ir->op2)) {

1298. int32_t k = IR(ir->op2)->i;

1299. if (k == 0)

1300. pi = PPCI_ADDZE;

1301. else if (k == -1)

1302. pi = PPCI_ADDME;

1303. else

1304. goto needright;

1305. right = 0;

1306. } else {

1307. needright:

1308. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1309. }

1310. emit_tab(as, pi, dest, left, right);

1311. ir--;

1312. dest = ra_dest(as, ir, RSET_GPR);

1313. left = ra_alloc1(as, ir->op1, RSET_GPR);

1314. if (irref_isk(ir->op2)) {

1315. int32_t k = IR(ir->op2)->i;

1316. if (checki16(k)) {

1317. emit_tai(as, PPCI_ADDIC, dest, left, k);

1318. return;

1319. }

1320. }

1321. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1322. emit_tab(as, PPCI_ADDC, dest, left, right);

1323. }

1325. ‌static void asm_sub64(ASMState *as, IRIns *ir)

1326. {

1327. Reg dest = ra_dest(as, ir, RSET_GPR);

1328. Reg left, right = ra_alloc1(as, ir->op2, RSET_GPR);

1329. PPCIns pi = PPCI_SUBFE;

1330. if (irref_isk(ir->op1)) {

1331. int32_t k = IR(ir->op1)->i;

1332. if (k == 0)

1333. pi = PPCI_SUBFZE;

1334. else if (k == -1)

1335. pi = PPCI_SUBFME;

1336. else

1337. goto needleft;

1338. left = 0;

1339. } else {

1340. needleft:

1341. left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right));

1342. }

1343. emit_tab(as, pi, dest, right, left);  /* Subtract right _from_ left. */

1344. ir--;

1345. dest = ra_dest(as, ir, RSET_GPR);

1346. right = ra_alloc1(as, ir->op2, RSET_GPR);

1347. if (irref_isk(ir->op1)) {

1348. int32_t k = IR(ir->op1)->i;

1349. if (checki16(k)) {

1350. emit_tai(as, PPCI_SUBFIC, dest, right, k);

1351. return;

1352. }

1353. }

1354. left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right));

1355. emit_tab(as, PPCI_SUBFC, dest, right, left);

1356. }

1358. ‌static void asm_neg64(ASMState *as, IRIns *ir)

1359. {

1360. Reg dest = ra_dest(as, ir, RSET_GPR);

1361. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);

1362. emit_tab(as, PPCI_SUBFZE, dest, left, 0);

1363. ir--;

1364. dest = ra_dest(as, ir, RSET_GPR);

1365. left = ra_alloc1(as, ir->op1, RSET_GPR);

1366. emit_tai(as, PPCI_SUBFIC, dest, left, 0);

1367. }

1368. #endif

1370. ‌static void asm_bnot(ASMState *as, IRIns *ir)

1371. {

1372. Reg dest, left, right;

1373. PPCIns pi = PPCI_NOR;

1374. if (as->flagmcp == as->mcp) {

1375. as->flagmcp = NULL;

1376. as->mcp++;

1377. pi |= PPCF_DOT;

1378. }

1379. dest = ra_dest(as, ir, RSET_GPR);

1380. if (mayfuse(as, ir->op1)) {

1381. IRIns *irl = IR(ir->op1);

1382. if (irl->o == IR_BAND)

1383. pi ^= (PPCI_NOR ^ PPCI_NAND);

1384. else if (irl->o == IR_BXOR)

1385. pi ^= (PPCI_NOR ^ PPCI_EQV);

1386. else if (irl->o != IR_BOR)

1387. goto nofuse;

1388. left = ra_hintalloc(as, irl->op1, dest, RSET_GPR);

1389. right = ra_alloc1(as, irl->op2, rset_exclude(RSET_GPR, left));

1390. } else {

1391. nofuse:

1392. left = right = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1393. }

1394. emit_asb(as, pi, dest, left, right);

1395. }

1397. ‌static void asm_bswap(ASMState *as, IRIns *ir)

1398. {

1399. Reg dest = ra_dest(as, ir, RSET_GPR);

1400. IRIns *irx;

1401. if (mayfuse(as, ir->op1) && (irx = IR(ir->op1))->o == IR_XLOAD &&

1402. ra_noreg(irx->r) && (irt_isint(irx->t) || irt_isu32(irx->t))) {

1403. /* Fuse BSWAP with XLOAD to lwbrx. */

1404. asm_fusexrefx(as, PPCI_LWBRX, dest, irx->op1, RSET_GPR);

1405. } else {

1406. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);

1407. Reg tmp = dest;

1408. if (tmp == left) {

1409. tmp = RID_TMP;

1410. emit_mr(as, dest, RID_TMP);

1411. }

1412. emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 16, 23);

1413. emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 0, 7);

1414. emit_rotlwi(as, tmp, left, 8);

1415. }

1416. }

1418. /* Fuse BAND with contiguous bitmask and a shift to rlwinm. */

1419. ‌static void asm_fuseandsh(ASMState *as, PPCIns pi, int32_t mask, IRRef ref)

1420. {

1421. IRIns *ir;

1422. Reg left;

1423. if (mayfuse(as, ref) && (ir = IR(ref), ra_noreg(ir->r)) &&

1424. irref_isk(ir->op2) && ir->o >= IR_BSHL && ir->o <= IR_BROR) {

1425. int32_t sh = (IR(ir->op2)->i & 31);

1426. switch (ir->o) {

1427. case IR_BSHL:

1428. if ((mask & ((1u<<sh)-1))) goto nofuse;

1429. break;

1430. case IR_BSHR:

1431. if ((mask & ~((~0u)>>sh))) goto nofuse;

1432. sh = ((32-sh)&31);

1433. break;

1434. case IR_BROL:

1435. break;

1436. default:

1437. goto nofuse;

1438. }

1439. left = ra_alloc1(as, ir->op1, RSET_GPR);

1440. *--as->mcp = pi | PPCF_T(left) | PPCF_B(sh);

1441. return;

1442. }

1443. nofuse:

1444. left = ra_alloc1(as, ref, RSET_GPR);

1445. *--as->mcp = pi | PPCF_T(left);

1446. }

1448. ‌static void asm_band(ASMState *as, IRIns *ir)

1449. {

1450. Reg dest, left, right;

1451. IRRef lref = ir->op1;

1452. PPCIns dot = 0;

1453. IRRef op2;

1454. if (as->flagmcp == as->mcp) {

1455. as->flagmcp = NULL;

1456. as->mcp++;

1457. dot = PPCF_DOT;

1458. }

1459. dest = ra_dest(as, ir, RSET_GPR);

1460. if (irref_isk(ir->op2)) {

1461. int32_t k = IR(ir->op2)->i;

1462. if (k) {

1463. /* First check for a contiguous bitmask as used by rlwinm. */

1464. uint32_t s1 = lj_ffs((uint32_t)k);

1465. uint32_t k1 = ((uint32_t)k >> s1);

1466. if ((k1 & (k1+1)) == 0) {

1467. asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) |

1468. PPCF_MB(31-lj_fls((uint32_t)k)) | PPCF_ME(31-s1),

1469. k, lref);

1470. return;

1471. }

1472. if (~(uint32_t)k) {

1473. uint32_t s2 = lj_ffs(~(uint32_t)k);

1474. uint32_t k2 = (~(uint32_t)k >> s2);

1475. if ((k2 & (k2+1)) == 0) {

1476. asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) |

1477. PPCF_MB(32-s2) | PPCF_ME(30-lj_fls(~(uint32_t)k)),

1478. k, lref);

1479. return;

1480. }

1481. }

1482. }

1483. if (checku16(k)) {

1484. left = ra_alloc1(as, lref, RSET_GPR);

1485. emit_asi(as, PPCI_ANDIDOT, dest, left, k);

1486. return;

1487. } else if ((k & 0xffff) == 0) {

1488. left = ra_alloc1(as, lref, RSET_GPR);

1489. emit_asi(as, PPCI_ANDISDOT, dest, left, (k >> 16));

1490. return;

1491. }

1492. }

1493. op2 = ir->op2;

1494. if (mayfuse(as, op2) && IR(op2)->o == IR_BNOT && ra_noreg(IR(op2)->r)) {

1495. dot ^= (PPCI_AND ^ PPCI_ANDC);

1496. op2 = IR(op2)->op1;

1497. }

1498. left = ra_hintalloc(as, lref, dest, RSET_GPR);

1499. right = ra_alloc1(as, op2, rset_exclude(RSET_GPR, left));

1500. emit_asb(as, PPCI_AND ^ dot, dest, left, right);

1501. }

1503. ‌static void asm_bitop(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik)

1504. {

1505. Reg dest = ra_dest(as, ir, RSET_GPR);

1506. Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);

1507. if (irref_isk(ir->op2)) {

1508. int32_t k = IR(ir->op2)->i;

1509. Reg tmp = left;

1510. if ((checku16(k) || (k & 0xffff) == 0) || (tmp = dest, !as->sectref)) {

1511. if (!checku16(k)) {

1512. emit_asi(as, pik ^ (PPCI_ORI ^ PPCI_ORIS), dest, tmp, (k >> 16));

1513. if ((k & 0xffff) == 0) return;

1514. }

1515. emit_asi(as, pik, dest, left, k);

1516. return;

1517. }

1518. }

1519. /* May fail due to spills/restores above, but simplifies the logic. */

1520. if (as->flagmcp == as->mcp) {

1521. as->flagmcp = NULL;

1522. as->mcp++;

1523. pi |= PPCF_DOT;

1524. }

1525. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1526. emit_asb(as, pi, dest, left, right);

1527. }

1529. ‌#define asm_bor(as, ir)                asm_bitop(as, ir, PPCI_OR, PPCI_ORI)

1530. ‌#define asm_bxor(as, ir)        asm_bitop(as, ir, PPCI_XOR, PPCI_XORI)

1532. ‌static void asm_bitshift(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik)

1533. {

1534. Reg dest, left;

1535. Reg dot = 0;

1536. if (as->flagmcp == as->mcp) {

1537. as->flagmcp = NULL;

1538. as->mcp++;

1539. dot = PPCF_DOT;

1540. }

1541. dest = ra_dest(as, ir, RSET_GPR);

1542. left = ra_alloc1(as, ir->op1, RSET_GPR);

1543. if (irref_isk(ir->op2)) {  /* Constant shifts. */

1544. int32_t shift = (IR(ir->op2)->i & 31);

1545. if (pik == 0)  /* SLWI */

1546. emit_rot(as, PPCI_RLWINM|dot, dest, left, shift, 0, 31-shift);

1547. else if (pik == 1)  /* SRWI */

1548. emit_rot(as, PPCI_RLWINM|dot, dest, left, (32-shift)&31, shift, 31);

1549. else

1550. emit_asb(as, pik|dot, dest, left, shift);

1551. } else {

1552. Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));

1553. emit_asb(as, pi|dot, dest, left, right);

1554. }

1555. }

1557. ‌#define asm_bshl(as, ir)        asm_bitshift(as, ir, PPCI_SLW, 0)

1558. ‌#define asm_bshr(as, ir)        asm_bitshift(as, ir, PPCI_SRW, 1)

1559. ‌#define asm_bsar(as, ir)        asm_bitshift(as, ir, PPCI_SRAW, PPCI_SRAWI)

1560. ‌#define asm_brol(as, ir) \

1561. asm_bitshift(as, ir, PPCI_RLWNM|PPCF_MB(0)|PPCF_ME(31), \

1562. PPCI_RLWINM|PPCF_MB(0)|PPCF_ME(31))

1563. ‌#define asm_bror(as, ir)        lua_assert(0)

1565. ‌static void asm_min_max(ASMState *as, IRIns *ir, int ismax)

1566. {

1567. if (irt_isnum(ir->t)) {

1568. Reg dest = ra_dest(as, ir, RSET_FPR);

1569. Reg tmp = dest;

1570. Reg right, left = ra_alloc2(as, ir, RSET_FPR);

1571. right = (left >> 8); left &= 255;

1572. if (tmp == left || tmp == right)

1573. tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_FPR,

1574. dest), left), right));

1575. emit_facb(as, PPCI_FSEL, dest, tmp,

1576. ismax ? left : right, ismax ? right : left);

1577. emit_fab(as, PPCI_FSUB, tmp, left, right);

1578. } else {

1579. Reg dest = ra_dest(as, ir, RSET_GPR);

1580. Reg tmp1 = RID_TMP, tmp2 = dest;

1581. Reg right, left = ra_alloc2(as, ir, RSET_GPR);

1582. right = (left >> 8); left &= 255;

1583. if (tmp2 == left || tmp2 == right)

1584. tmp2 = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR,

1585. dest), left), right));

1586. emit_tab(as, PPCI_ADD, dest, tmp2, right);

1587. emit_asb(as, ismax ? PPCI_ANDC : PPCI_AND, tmp2, tmp2, tmp1);

1588. emit_tab(as, PPCI_SUBFE, tmp1, tmp1, tmp1);

1589. emit_tab(as, PPCI_SUBFC, tmp2, tmp2, tmp1);

1590. emit_asi(as, PPCI_XORIS, tmp2, right, 0x8000);

1591. emit_asi(as, PPCI_XORIS, tmp1, left, 0x8000);

1592. }

1593. }

1595. ‌#define asm_min(as, ir)                asm_min_max(as, ir, 0)

1596. ‌#define asm_max(as, ir)                asm_min_max(as, ir, 1)

1598. /* -- Comparisons --------------------------------------------------------- */

1600. ‌#define CC_UNSIGNED        0x08        /* Unsigned integer comparison. */

1601. ‌#define CC_TWO                0x80        /* Check two flags for FP comparison. */

1603. /* Map of comparisons to flags. ORDER IR. */

1604. ‌static const uint8_t asm_compmap[IR_ABC+1] = {

1605. /* op     int cc                 FP cc */

1606. /* LT  */ CC_GE               + (CC_GE<<4),

1607. /* GE  */ CC_LT               + (CC_LE<<4) + CC_TWO,

1608. /* LE  */ CC_GT               + (CC_GE<<4) + CC_TWO,

1609. /* GT  */ CC_LE               + (CC_LE<<4),

1610. /* ULT */ CC_GE + CC_UNSIGNED + (CC_GT<<4) + CC_TWO,

1611. /* UGE */ CC_LT + CC_UNSIGNED + (CC_LT<<4),

1612. /* ULE */ CC_GT + CC_UNSIGNED + (CC_GT<<4),

1613. /* UGT */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO,

1614. /* EQ  */ CC_NE               + (CC_NE<<4),

1615. /* NE  */ CC_EQ               + (CC_EQ<<4),

1616. /* ABC */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO  /* Same as UGT. */

1617. };

1619. ‌static void asm_intcomp_(ASMState *as, IRRef lref, IRRef rref, Reg cr, PPCCC cc)

1620. {

1621. Reg right, left = ra_alloc1(as, lref, RSET_GPR);

1622. if (irref_isk(rref)) {

1623. int32_t k = IR(rref)->i;

1624. if ((cc & CC_UNSIGNED) == 0) {  /* Signed comparison with constant. */

1625. if (checki16(k)) {

1626. emit_tai(as, PPCI_CMPWI, cr, left, k);

1627. /* Signed comparison with zero and referencing previous ins? */

1628. if (k == 0 && lref == as->curins-1)

1629. as->flagmcp = as->mcp;  /* Allow elimination of the compare. */

1630. return;

1631. } else if ((cc & 3) == (CC_EQ & 3)) {  /* Use CMPLWI for EQ or NE. */

1632. if (checku16(k)) {

1633. emit_tai(as, PPCI_CMPLWI, cr, left, k);

1634. return;

1635. } else if (!as->sectref && ra_noreg(IR(rref)->r)) {

1636. emit_tai(as, PPCI_CMPLWI, cr, RID_TMP, k);

1637. emit_asi(as, PPCI_XORIS, RID_TMP, left, (k >> 16));

1638. return;

1639. }

1640. }

1641. } else {  /* Unsigned comparison with constant. */

1642. if (checku16(k)) {

1643. emit_tai(as, PPCI_CMPLWI, cr, left, k);

1644. return;

1645. }

1646. }

1647. }

1648. right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left));

1649. emit_tab(as, (cc & CC_UNSIGNED) ? PPCI_CMPLW : PPCI_CMPW, cr, left, right);

1650. }

1652. ‌static void asm_comp(ASMState *as, IRIns *ir)

1653. {

1654. PPCCC cc = asm_compmap[ir->o];

1655. if (irt_isnum(ir->t)) {

1656. Reg right, left = ra_alloc2(as, ir, RSET_FPR);

1657. right = (left >> 8); left &= 255;

1658. asm_guardcc(as, (cc >> 4));

1659. if ((cc & CC_TWO))

1660. emit_tab(as, PPCI_CROR, ((cc>>4)&3), ((cc>>4)&3), (CC_EQ&3));

1661. emit_fab(as, PPCI_FCMPU, 0, left, right);

1662. } else {

1663. IRRef lref = ir->op1, rref = ir->op2;

1664. if (irref_isk(lref) && !irref_isk(rref)) {

1665. /* Swap constants to the right (only for ABC). */

1666. IRRef tmp = lref; lref = rref; rref = tmp;

1667. if ((cc & 2) == 0) cc ^= 1;  /* LT <-> GT, LE <-> GE */

1668. }

1669. asm_guardcc(as, cc);

1670. asm_intcomp_(as, lref, rref, 0, cc);

1671. }

1672. }

1674. ‌#define asm_equal(as, ir)        asm_comp(as, ir)

1676. #if LJ_HASFFI

1677. /* 64 bit integer comparisons. */

1678. ‌static void asm_comp64(ASMState *as, IRIns *ir)

1679. {

1680. PPCCC cc = asm_compmap[(ir-1)->o];

1681. if ((cc&3) == (CC_EQ&3)) {

1682. asm_guardcc(as, cc);

1683. emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CROR,

1684. (CC_EQ&3), (CC_EQ&3), 4+(CC_EQ&3));

1685. } else {

1686. asm_guardcc(as, CC_EQ);

1687. emit_tab(as, PPCI_CROR, (CC_EQ&3), (CC_EQ&3), ((cc^~(cc>>2))&1));

1688. emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CRANDC,

1689. (CC_EQ&3), (CC_EQ&3), 4+(cc&3));

1690. }

1691. /* Loword comparison sets cr1 and is unsigned, except for equality. */

1692. asm_intcomp_(as, (ir-1)->op1, (ir-1)->op2, 4,

1693. cc | ((cc&3) == (CC_EQ&3) ? 0 : CC_UNSIGNED));

1694. /* Hiword comparison sets cr0. */

1695. asm_intcomp_(as, ir->op1, ir->op2, 0, cc);

1696. as->flagmcp = NULL;  /* Doesn't work here. */

1697. }

1698. #endif

1700. /* -- Support for 64 bit ops in 32 bit mode ------------------------------- */

1702. /* Hiword op of a split 64 bit op. Previous op must be the loword op. */

1703. ‌static void asm_hiop(ASMState *as, IRIns *ir)

1704. {

1705. #if LJ_HASFFI

1706. /* HIOP is marked as a store because it needs its own DCE logic. */

1707. int uselo = ra_used(ir-1), usehi = ra_used(ir);  /* Loword/hiword used? */

1708. if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;

1709. if ((ir-1)->o == IR_CONV) {  /* Conversions to/from 64 bit. */

1710. as->curins--;  /* Always skip the CONV. */

1711. if (usehi || uselo)

1712. asm_conv64(as, ir);

1713. return;

1714. } else if ((ir-1)->o <= IR_NE) {  /* 64 bit integer comparisons. ORDER IR. */

1715. as->curins--;  /* Always skip the loword comparison. */

1716. asm_comp64(as, ir);

1717. return;

1718. } else if ((ir-1)->o == IR_XSTORE) {

1719. as->curins--;  /* Handle both stores here. */

1720. if ((ir-1)->r != RID_SINK) {

1721. asm_xstore_(as, ir, 0);

1722. asm_xstore_(as, ir-1, 4);

1723. }

1724. return;

1725. }

1726. if (!usehi) return;  /* Skip unused hiword op for all remaining ops. */

1727. switch ((ir-1)->o) {

1728. case IR_ADD: as->curins--; asm_add64(as, ir); break;

1729. case IR_SUB: as->curins--; asm_sub64(as, ir); break;

1730. case IR_NEG: as->curins--; asm_neg64(as, ir); break;

1731. case IR_CALLN:

1732. case IR_CALLXS:

1733. if (!uselo)

1734. ra_allocref(as, ir->op1, RID2RSET(RID_RETLO));  /* Mark lo op as used. */

1735. break;

1736. case IR_CNEWI:

1737. /* Nothing to do here. Handled by lo op itself. */

1738. break;

1739. default: lua_assert(0); break;

1740. }

1741. #else

1742. UNUSED(as); UNUSED(ir); lua_assert(0);  /* Unused without FFI. */

1743. #endif

1744. }

1746. /* -- Profiling ----------------------------------------------------------- */

1748. ‌static void asm_prof(ASMState *as, IRIns *ir)

1749. {

1750. UNUSED(ir);

1751. asm_guardcc(as, CC_NE);

1752. emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, HOOK_PROFILE);

1753. emit_lsglptr(as, PPCI_LBZ, RID_TMP,

1754. (int32_t)offsetof(global_State, hookmask));

1755. }

1757. /* -- Stack handling ------------------------------------------------------ */

1759. /* Check Lua stack size for overflow. Use exit handler as fallback. */

1760. ‌static void asm_stack_check(ASMState *as, BCReg topslot,

1761. IRIns *irp, RegSet allow, ExitNo exitno)

1762. {

1763. /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */

1764. Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE;

1765. rset_clear(allow, pbase);

1766. tmp = allow ? rset_pickbot(allow) :

1767. (pbase == RID_RETHI ? RID_RETLO : RID_RETHI);

1768. emit_condbranch(as, PPCI_BC, CC_LT, asm_exitstub_addr(as, exitno));

1769. if (allow == RSET_EMPTY)  /* Restore temp. register. */

1770. emit_tai(as, PPCI_LWZ, tmp, RID_SP, SPOFS_TMPW);

1771. else

1772. ra_modified(as, tmp);

1773. emit_ai(as, PPCI_CMPLWI, RID_TMP, (int32_t)(8*topslot));

1774. emit_tab(as, PPCI_SUBF, RID_TMP, pbase, tmp);

1775. emit_tai(as, PPCI_LWZ, tmp, tmp, offsetof(lua_State, maxstack));

1776. if (pbase == RID_TMP)

1777. emit_getgl(as, RID_TMP, jit_base);

1778. emit_getgl(as, tmp, cur_L);

1779. if (allow == RSET_EMPTY)  /* Spill temp. register. */

1780. emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPW);

1781. }

1783. /* Restore Lua stack from on-trace state. */

1784. ‌static void asm_stack_restore(ASMState *as, SnapShot *snap)

1785. {

1786. SnapEntry *map = &as->T->snapmap[snap->mapofs];

1787. SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1];

1788. MSize n, nent = snap->nent;

1789. /* Store the value of all modified slots to the Lua stack. */

1790. for (n = 0; n < nent; n++) {

1791. SnapEntry sn = map[n];

1792. BCReg s = snap_slot(sn);

1793. int32_t ofs = 8*((int32_t)s-1);

1794. IRRef ref = snap_ref(sn);

1795. IRIns *ir = IR(ref);

1796. if ((sn & SNAP_NORESTORE))

1797. continue;

1798. if (irt_isnum(ir->t)) {

1799. Reg src = ra_alloc1(as, ref, RSET_FPR);

1800. emit_fai(as, PPCI_STFD, src, RID_BASE, ofs);

1801. } else {

1802. Reg type;

1803. RegSet allow = rset_exclude(RSET_GPR, RID_BASE);

1804. lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t));

1805. if (!irt_ispri(ir->t)) {

1806. Reg src = ra_alloc1(as, ref, allow);

1807. rset_clear(allow, src);

1808. emit_tai(as, PPCI_STW, src, RID_BASE, ofs+4);

1809. }

1810. if ((sn & (SNAP_CONT|SNAP_FRAME))) {

1811. if (s == 0) continue;  /* Do not overwrite link to previous frame. */

1812. type = ra_allock(as, (int32_t)(*flinks--), allow);

1813. } else {

1814. type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);

1815. }

1816. emit_tai(as, PPCI_STW, type, RID_BASE, ofs);

1817. }

1818. checkmclim(as);

1819. }

1820. lua_assert(map + nent == flinks);

1821. }

1823. /* -- GC handling --------------------------------------------------------- */

1825. /* Check GC threshold and do one or more GC steps. */

1826. ‌static void asm_gc_check(ASMState *as)

1827. {

1828. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];

1829. IRRef args[2];

1830. MCLabel l_end;

1831. Reg tmp;

1832. ra_evictset(as, RSET_SCRATCH);

1833. l_end = emit_label(as);

1834. /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */

1835. asm_guardcc(as, CC_NE);  /* Assumes asm_snap_prep() already done. */

1836. emit_ai(as, PPCI_CMPWI, RID_RET, 0);

1837. args[0] = ASMREF_TMP1;  /* global_State *g */

1838. args[1] = ASMREF_TMP2;  /* MSize steps     */

1839. asm_gencall(as, ci, args);

1840. emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);

1841. tmp = ra_releasetmp(as, ASMREF_TMP2);

1842. emit_loadi(as, tmp, as->gcsteps);

1843. /* Jump around GC step if GC total < GC threshold. */

1844. emit_condbranch(as, PPCI_BC|PPCF_Y, CC_LT, l_end);

1845. emit_ab(as, PPCI_CMPLW, RID_TMP, tmp);

1846. emit_getgl(as, tmp, gc.threshold);

1847. emit_getgl(as, RID_TMP, gc.total);

1848. as->gcsteps = 0;

1849. checkmclim(as);

1850. }

1852. /* -- Loop handling ------------------------------------------------------- */

1854. /* Fixup the loop branch. */

1855. ‌static void asm_loop_fixup(ASMState *as)

1856. {

1857. MCode *p = as->mctop;

1858. MCode *target = as->mcp;

1859. if (as->loopinv) {  /* Inverted loop branch? */

1860. /* asm_guardcc already inverted the cond branch and patched the final b. */

1861. p[-2] = (p[-2] & (0xffff0000u & ~PPCF_Y)) | (((target-p+2) & 0x3fffu) << 2);

1862. } else {

1863. p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2);

1864. }

1865. }

1867. /* -- Head of trace ------------------------------------------------------- */

1869. /* Coalesce BASE register for a root trace. */

1870. ‌static void asm_head_root_base(ASMState *as)

1871. {

1872. IRIns *ir = IR(REF_BASE);

1873. Reg r = ir->r;

1874. if (ra_hasreg(r)) {

1875. ra_free(as, r);

1876. if (rset_test(as->modset, r) || irt_ismarked(ir->t))

1877. ir->r = RID_INIT;  /* No inheritance for modified BASE register. */

1878. if (r != RID_BASE)

1879. emit_mr(as, r, RID_BASE);

1880. }

1881. }

1883. /* Coalesce BASE register for a side trace. */

1884. ‌static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow)

1885. {

1886. IRIns *ir = IR(REF_BASE);

1887. Reg r = ir->r;

1888. if (ra_hasreg(r)) {

1889. ra_free(as, r);

1890. if (rset_test(as->modset, r) || irt_ismarked(ir->t))

1891. ir->r = RID_INIT;  /* No inheritance for modified BASE register. */

1892. if (irp->r == r) {

1893. rset_clear(allow, r);  /* Mark same BASE register as coalesced. */

1894. } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {

1895. rset_clear(allow, irp->r);

1896. emit_mr(as, r, irp->r);  /* Move from coalesced parent reg. */

1897. } else {

1898. emit_getgl(as, r, jit_base);  /* Otherwise reload BASE. */

1899. }

1900. }

1901. return allow;

1902. }

1904. /* -- Tail of trace ------------------------------------------------------- */

1906. /* Fixup the tail code. */

1907. ‌static void asm_tail_fixup(ASMState *as, TraceNo lnk)

1908. {

1909. MCode *p = as->mctop;

1910. MCode *target;

1911. int32_t spadj = as->T->spadjust;

1912. if (spadj == 0) {

1913. *--p = PPCI_NOP;

1914. *--p = PPCI_NOP;

1915. as->mctop = p;

1916. } else {

1917. /* Patch stack adjustment. */

1918. lua_assert(checki16(CFRAME_SIZE+spadj));

1919. p[-3] = PPCI_ADDI | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | (CFRAME_SIZE+spadj);

1920. p[-2] = PPCI_STWU | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | spadj;

1921. }

1922. /* Patch exit branch. */

1923. target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp;

1924. p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2);

1925. }

1927. /* Prepare tail of code. */

1928. ‌static void asm_tail_prep(ASMState *as)

1929. {

1930. MCode *p = as->mctop - 1;  /* Leave room for exit branch. */

1931. if (as->loopref) {

1932. as->invmcp = as->mcp = p;

1933. } else {

1934. as->mcp = p-2;  /* Leave room for stack pointer adjustment. */

1935. as->invmcp = NULL;

1936. }

1937. }

1939. /* -- Trace setup --------------------------------------------------------- */

1941. /* Ensure there are enough stack slots for call arguments. */

1942. ‌static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)

1943. {

1944. IRRef args[CCI_NARGS_MAX*2];

1945. uint32_t i, nargs = CCI_XNARGS(ci);

1946. int nslots = 2, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;

1947. asm_collectargs(as, ir, ci, args);

1948. for (i = 0; i < nargs; i++)

1949. if (args[i] && irt_isfp(IR(args[i])->t)) {

1950. if (nfpr > 0) nfpr--; else nslots = (nslots+3) & ~1;

1951. } else {

1952. if (ngpr > 0) ngpr--; else nslots++;

1953. }

1954. if (nslots > as->evenspill)  /* Leave room for args in stack slots. */

1955. as->evenspill = nslots;

1956. return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET);

1957. }

1959. ‌static void asm_setup_target(ASMState *as)

1960. {

1961. asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0));

1962. }

1964. /* -- Trace patching ------------------------------------------------------ */

1966. /* Patch exit jumps of existing machine code to a new target. */

1967. ‌void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)

1968. {

1969. MCode *p = T->mcode;

1970. MCode *pe = (MCode *)((char *)p + T->szmcode);

1971. MCode *px = exitstub_trace_addr(T, exitno);

1972. MCode *cstart = NULL;

1973. MCode *mcarea = lj_mcode_patch(J, p, 0);

1974. int clearso = 0;

1975. for (; p < pe; p++) {

1976. /* Look for exitstub branch, try to replace with branch to target. */

1977. uint32_t ins = *p;

1978. if ((ins & 0xfc000000u) == 0x40000000u &&

1979. ((ins ^ ((char *)px-(char *)p)) & 0xffffu) == 0) {

1980. ptrdiff_t delta = (char *)target - (char *)p;

1981. if (((ins >> 16) & 3) == (CC_SO&3)) {

1982. clearso = sizeof(MCode);

1983. delta -= sizeof(MCode);

1984. }

1985. /* Many, but not all short-range branches can be patched directly. */

1986. if (((delta + 0x8000) >> 16) == 0) {

1987. *p = (ins & 0xffdf0000u) | ((uint32_t)delta & 0xffffu) |

1988. ((delta & 0x8000) * (PPCF_Y/0x8000));

1989. if (!cstart) cstart = p;

1990. }

1991. } else if ((ins & 0xfc000000u) == PPCI_B &&

1992. ((ins ^ ((char *)px-(char *)p)) & 0x03ffffffu) == 0) {

1993. ptrdiff_t delta = (char *)target - (char *)p;

1994. lua_assert(((delta + 0x02000000) >> 26) == 0);

1995. *p = PPCI_B | ((uint32_t)delta & 0x03ffffffu);

1996. if (!cstart) cstart = p;

1997. }

1998. }

1999. {  /* Always patch long-range branch in exit stub itself. */

2000. ptrdiff_t delta = (char *)target - (char *)px - clearso;

2001. lua_assert(((delta + 0x02000000) >> 26) == 0);

2002. *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu);

2003. }

2004. if (!cstart) cstart = px;

2005. lj_mcode_sync(cstart, px+1);

2006. if (clearso) {  /* Extend the current trace. Ugly workaround. */

2007. MCode *pp = J->cur.mcode;

2008. J->cur.szmcode += sizeof(MCode);

2009. *--pp = PPCI_MCRXR;  /* Clear SO flag. */

2010. J->cur.mcode = pp;

2011. lj_mcode_sync(pp, pp+1);

2012. }

2013. lj_mcode_patch(J, mcarea, 1);

2014. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_emit_ppc.h - luajit-2.0-src

Data types defined

- MCLabel

Functions defined

- emit_addptr

- emit_call

- emit_cmpi

- emit_condbranch

- emit_jmp

- emit_kdelta1

- emit_loadi

- emit_loadofs

- emit_lsglptr

- emit_lsptr

- emit_movrr

- emit_rot

- emit_rotlwi

- emit_slwi

- emit_spsub

- emit_storeofs

- emit_tab

- emit_tai

Macros defined

- emit_ab

- emit_ai

- emit_as

- emit_asb

- emit_asi

- emit_canremat

- emit_fab

- emit_fac

- emit_facb

- emit_fai

- emit_fb

- emit_getgl

- emit_label

- emit_loada

- emit_loadn

- emit_mr

- emit_setgl

- emit_setvmstate

- emit_ti

Source code

1. /*

2. ** PPC instruction emitter.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* -- Emit basic instructions --------------------------------------------- */

8. ‌static void emit_tab(ASMState *as, PPCIns pi, Reg rt, Reg ra, Reg rb)

9. {

10. *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | PPCF_B(rb);

11. }

13. ‌#define emit_asb(as, pi, ra, rs, rb)        emit_tab(as, (pi), (rs), (ra), (rb))

14. ‌#define emit_as(as, pi, ra, rs)                emit_tab(as, (pi), (rs), (ra), 0)

15. ‌#define emit_ab(as, pi, ra, rb)                emit_tab(as, (pi), 0, (ra), (rb))

17. ‌static void emit_tai(ASMState *as, PPCIns pi, Reg rt, Reg ra, int32_t i)

18. {

19. *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | (i & 0xffff);

20. }

22. ‌#define emit_ti(as, pi, rt, i)                emit_tai(as, (pi), (rt), 0, (i))

23. ‌#define emit_ai(as, pi, ra, i)                emit_tai(as, (pi), 0, (ra), (i))

24. ‌#define emit_asi(as, pi, ra, rs, i)        emit_tai(as, (pi), (rs), (ra), (i))

26. ‌#define emit_fab(as, pi, rf, ra, rb) \

27. emit_tab(as, (pi), (rf)&31, (ra)&31, (rb)&31)

28. ‌#define emit_fb(as, pi, rf, rb)                emit_tab(as, (pi), (rf)&31, 0, (rb)&31)

29. ‌#define emit_fac(as, pi, rf, ra, rc) \

30. emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, 0)

31. ‌#define emit_facb(as, pi, rf, ra, rc, rb) \

32. emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, (rb)&31)

33. ‌#define emit_fai(as, pi, rf, ra, i)        emit_tai(as, (pi), (rf)&31, (ra), (i))

35. ‌static void emit_rot(ASMState *as, PPCIns pi, Reg ra, Reg rs,

36. int32_t n, int32_t b, int32_t e)

37. {

38. *--as->mcp = pi | PPCF_T(rs) | PPCF_A(ra) | PPCF_B(n) |

39. PPCF_MB(b) | PPCF_ME(e);

40. }

42. ‌static void emit_slwi(ASMState *as, Reg ra, Reg rs, int32_t n)

43. {

44. lua_assert(n >= 0 && n < 32);

45. emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31-n);

46. }

48. ‌static void emit_rotlwi(ASMState *as, Reg ra, Reg rs, int32_t n)

49. {

50. lua_assert(n >= 0 && n < 32);

51. emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31);

52. }

54. /* -- Emit loads/stores --------------------------------------------------- */

56. /* Prefer rematerialization of BASE/L from global_State over spills. */

57. ‌#define emit_canremat(ref)        ((ref) <= REF_BASE)

59. /* Try to find a one step delta relative to another constant. */

60. ‌static int emit_kdelta1(ASMState *as, Reg t, int32_t i)

61. {

62. RegSet work = ~as->freeset & RSET_GPR;

63. while (work) {

64. Reg r = rset_picktop(work);

65. IRRef ref = regcost_ref(as->cost[r]);

66. lua_assert(r != t);

67. if (ref < ASMREF_L) {

68. int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i);

69. if (checki16(delta)) {

70. emit_tai(as, PPCI_ADDI, t, r, delta);

71. return 1;

72. }

73. }

74. rset_clear(work, r);

75. }

76. return 0;  /* Failed. */

77. }

79. /* Load a 32 bit constant into a GPR. */

80. ‌static void emit_loadi(ASMState *as, Reg r, int32_t i)

81. {

82. if (checki16(i)) {

83. emit_ti(as, PPCI_LI, r, i);

84. } else {

85. if ((i & 0xffff)) {

86. int32_t jgl = i32ptr(J2G(as->J));

87. if ((uint32_t)(i-jgl) < 65536) {

88. emit_tai(as, PPCI_ADDI, r, RID_JGL, i-jgl-32768);

89. return;

90. } else if (emit_kdelta1(as, r, i)) {

91. return;

92. }

93. emit_asi(as, PPCI_ORI, r, r, i);

94. }

95. emit_ti(as, PPCI_LIS, r, (i >> 16));

96. }

97. }

99. ‌#define emit_loada(as, r, addr)                emit_loadi(as, (r), i32ptr((addr)))

101. static Reg ra_allock(ASMState *as, int32_t k, RegSet allow);

103. /* Get/set from constant pointer. */

104. ‌static void emit_lsptr(ASMState *as, PPCIns pi, Reg r, void *p, RegSet allow)

105. {

106. int32_t jgl = i32ptr(J2G(as->J));

107. int32_t i = i32ptr(p);

108. Reg base;

109. if ((uint32_t)(i-jgl) < 65536) {

110. i = i-jgl-32768;

111. base = RID_JGL;

112. } else {

113. base = ra_allock(as, i-(int16_t)i, allow);

114. }

115. emit_tai(as, pi, r, base, i);

116. }

118. ‌#define emit_loadn(as, r, tv) \

119. emit_lsptr(as, PPCI_LFD, ((r) & 31), (void *)(tv), RSET_GPR)

121. /* Get/set global_State fields. */

122. ‌static void emit_lsglptr(ASMState *as, PPCIns pi, Reg r, int32_t ofs)

123. {

124. emit_tai(as, pi, r, RID_JGL, ofs-32768);

125. }

127. ‌#define emit_getgl(as, r, field) \

128. emit_lsglptr(as, PPCI_LWZ, (r), (int32_t)offsetof(global_State, field))

129. ‌#define emit_setgl(as, r, field) \

130. emit_lsglptr(as, PPCI_STW, (r), (int32_t)offsetof(global_State, field))

132. /* Trace number is determined from per-trace exit stubs. */

133. ‌#define emit_setvmstate(as, i)                UNUSED(i)

135. /* -- Emit control-flow instructions -------------------------------------- */

137. /* Label for internal jumps. */

138. ‌typedef MCode *MCLabel;

140. /* Return label pointing to current PC. */

141. ‌#define emit_label(as)                ((as)->mcp)

143. ‌static void emit_condbranch(ASMState *as, PPCIns pi, PPCCC cc, MCode *target)

144. {

145. MCode *p = --as->mcp;

146. ptrdiff_t delta = (char *)target - (char *)p;

147. lua_assert(((delta + 0x8000) >> 16) == 0);

148. pi ^= (delta & 0x8000) * (PPCF_Y/0x8000);

149. *p = pi | PPCF_CC(cc) | ((uint32_t)delta & 0xffffu);

150. }

152. ‌static void emit_jmp(ASMState *as, MCode *target)

153. {

154. MCode *p = --as->mcp;

155. ptrdiff_t delta = (char *)target - (char *)p;

156. *p = PPCI_B | (delta & 0x03fffffcu);

157. }

159. ‌static void emit_call(ASMState *as, void *target)

160. {

161. MCode *p = --as->mcp;

162. ptrdiff_t delta = (char *)target - (char *)p;

163. if ((((delta>>2) + 0x00800000) >> 24) == 0) {

164. *p = PPCI_BL | (delta & 0x03fffffcu);

165. } else {  /* Target out of range: need indirect call. Don't use arg reg. */

166. RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1);

167. Reg r = ra_allock(as, i32ptr(target), allow);

168. *p = PPCI_BCTRL;

169. p[-1] = PPCI_MTCTR | PPCF_T(r);

170. as->mcp = p-1;

171. }

172. }

174. /* -- Emit generic operations --------------------------------------------- */

176. ‌#define emit_mr(as, dst, src) \

177. emit_asb(as, PPCI_MR, (dst), (src), (src))

179. /* Generic move between two regs. */

180. ‌static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src)

181. {

182. UNUSED(ir);

183. if (dst < RID_MAX_GPR)

184. emit_mr(as, dst, src);

185. else

186. emit_fb(as, PPCI_FMR, dst, src);

187. }

189. /* Generic load of register with base and (small) offset address. */

190. ‌static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)

191. {

192. if (r < RID_MAX_GPR)

193. emit_tai(as, PPCI_LWZ, r, base, ofs);

194. else

195. emit_fai(as, irt_isnum(ir->t) ? PPCI_LFD : PPCI_LFS, r, base, ofs);

196. }

198. /* Generic store of register with base and (small) offset address. */

199. ‌static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs)

200. {

201. if (r < RID_MAX_GPR)

202. emit_tai(as, PPCI_STW, r, base, ofs);

203. else

204. emit_fai(as, irt_isnum(ir->t) ? PPCI_STFD : PPCI_STFS, r, base, ofs);

205. }

207. /* Emit a compare (for equality) with a constant operand. */

208. ‌static void emit_cmpi(ASMState *as, Reg r, int32_t k)

209. {

210. if (checki16(k)) {

211. emit_ai(as, PPCI_CMPWI, r, k);

212. } else if (checku16(k)) {

213. emit_ai(as, PPCI_CMPLWI, r, k);

214. } else {

215. emit_ai(as, PPCI_CMPLWI, RID_TMP, k);

216. emit_asi(as, PPCI_XORIS, RID_TMP, r, (k >> 16));

217. }

218. }

220. /* Add offset to pointer. */

221. ‌static void emit_addptr(ASMState *as, Reg r, int32_t ofs)

222. {

223. if (ofs) {

224. emit_tai(as, PPCI_ADDI, r, r, ofs);

225. if (!checki16(ofs))

226. emit_tai(as, PPCI_ADDIS, r, r, (ofs + 32768) >> 16);

227. }

228. }

230. ‌static void emit_spsub(ASMState *as, int32_t ofs)

231. {

232. if (ofs) {

233. emit_tai(as, PPCI_STWU, RID_TMP, RID_SP, -ofs);

234. emit_tai(as, PPCI_ADDI, RID_TMP, RID_SP,

235. CFRAME_SIZE + (as->parent ? as->parent->spadjust : 0));

236. }

237. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_asm_x86.h - luajit-2.0-src

Global variables defined

- asm_compmap

Functions defined

- asm_add

- asm_ahustore

- asm_ahuvload

- asm_aref

- asm_bitshift

- asm_bswap

- asm_callx

- asm_callx_func

- asm_cnew

- asm_comp

- asm_comp_int64

- asm_conv

- asm_conv64

- asm_conv_fp_int64

- asm_conv_int64_fp

- asm_count_call_slots

- asm_div

- asm_exitstub_gen

- asm_exitstub_setup

- asm_fparith

- asm_fpmath

- asm_fppowi

- asm_fref

- asm_fuseabase

- asm_fuseahuref

- asm_fusearef

- asm_fusefref

- asm_fuseload

- asm_fuseloadm

- asm_fusestrref

- asm_fusexref

- asm_fxload

- asm_fxstore

- asm_gc_check

- asm_gencall

- asm_guardcc

- asm_head_root_base

- asm_head_side_base

- asm_hiop

- asm_href

- asm_hrefk

- asm_intarith

- asm_intmin_max

- asm_isk32

- asm_ldexp

- asm_lea

- asm_load_lightud64

- asm_loop_fixup

- asm_max

- asm_min

- asm_mod

- asm_mul

- asm_neg

- asm_neg_not

- asm_obar

- asm_pow

- asm_prof

- asm_retf

- asm_setup_call_slots

- asm_setup_target

- asm_setupresult

- asm_sload

- asm_stack_check

- asm_stack_restore

- asm_strref

- asm_strto

- asm_sub

- asm_swapops

- asm_tail_fixup

- asm_tail_prep

- asm_tbar

- asm_tobit

- asm_tointg

- asm_tvptr

- asm_uref

- asm_x87load

- lj_asm_patchexit

- noconflict

Macros defined

- COMPFLAGS

- CONFLICT_SEARCH_LIM

- VCC_P

- VCC_PS

- VCC_S

- VCC_U

- asm_abs

- asm_addov

- asm_atan2

- asm_band

- asm_bnot

- asm_bor

- asm_brol

- asm_bror

- asm_bsar

- asm_bshl

- asm_bshr

- asm_bxor

- asm_cnew

- asm_equal

- asm_fload

- asm_fstore

- asm_fuseloadm

- asm_mulov

- asm_subov

- asm_xload

- asm_xstore

Source code

1. /*

2. ** x86/x64 IR assembler (SSA IR -> machine code).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* -- Guard handling ------------------------------------------------------ */

8. /* Generate an exit stub group at the bottom of the reserved MCode memory. */

9. ‌static MCode *asm_exitstub_gen(ASMState *as, ExitNo group)

10. {

11. ExitNo i, groupofs = (group*EXITSTUBS_PER_GROUP) & 0xff;

12. MCode *mxp = as->mcbot;

13. MCode *mxpstart = mxp;

14. if (mxp + (2+2)*EXITSTUBS_PER_GROUP+8+5 >= as->mctop)

15. asm_mclimit(as);

16. /* Push low byte of exitno for each exit stub. */

17. *mxp++ = XI_PUSHi8; *mxp++ = (MCode)groupofs;

18. for (i = 1; i < EXITSTUBS_PER_GROUP; i++) {

19. *mxp++ = XI_JMPs; *mxp++ = (MCode)((2+2)*(EXITSTUBS_PER_GROUP - i) - 2);

20. *mxp++ = XI_PUSHi8; *mxp++ = (MCode)(groupofs + i);

21. }

22. /* Push the high byte of the exitno for each exit stub group. */

23. *mxp++ = XI_PUSHi8; *mxp++ = (MCode)((group*EXITSTUBS_PER_GROUP)>>8);

24. /* Store DISPATCH at original stack slot 0. Account for the two push ops. */

25. *mxp++ = XI_MOVmi;

26. *mxp++ = MODRM(XM_OFS8, 0, RID_ESP);

27. *mxp++ = MODRM(XM_SCALE1, RID_ESP, RID_ESP);

28. *mxp++ = 2*sizeof(void *);

29. *(int32_t *)mxp = ptr2addr(J2GG(as->J)->dispatch); mxp += 4;

30. /* Jump to exit handler which fills in the ExitState. */

31. *mxp++ = XI_JMP; mxp += 4;

32. *((int32_t *)(mxp-4)) = jmprel(mxp, (MCode *)(void *)lj_vm_exit_handler);

33. /* Commit the code for this group (even if assembly fails later on). */

34. lj_mcode_commitbot(as->J, mxp);

35. as->mcbot = mxp;

36. as->mclim = as->mcbot + MCLIM_REDZONE;

37. return mxpstart;

38. }

40. /* Setup all needed exit stubs. */

41. ‌static void asm_exitstub_setup(ASMState *as, ExitNo nexits)

42. {

43. ExitNo i;

44. if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR)

45. lj_trace_err(as->J, LJ_TRERR_SNAPOV);

46. for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++)

47. if (as->J->exitstubgroup[i] == NULL)

48. as->J->exitstubgroup[i] = asm_exitstub_gen(as, i);

49. }

51. /* Emit conditional branch to exit for guard.

52. ** It's important to emit this *after* all registers have been allocated,

53. ** because rematerializations may invalidate the flags.

54. */

55. ‌static void asm_guardcc(ASMState *as, int cc)

56. {

57. MCode *target = exitstub_addr(as->J, as->snapno);

58. MCode *p = as->mcp;

59. if (LJ_UNLIKELY(p == as->invmcp)) {

60. as->loopinv = 1;

61. *(int32_t *)(p+1) = jmprel(p+5, target);

62. target = p;

63. cc ^= 1;

64. if (as->realign) {

65. emit_sjcc(as, cc, target);

66. return;

67. }

68. }

69. emit_jcc(as, cc, target);

70. }

72. /* -- Memory operand fusion ----------------------------------------------- */

74. /* Limit linear search to this distance. Avoids O(n^2) behavior. */

75. ‌#define CONFLICT_SEARCH_LIM        31

77. /* Check if a reference is a signed 32 bit constant. */

78. ‌static int asm_isk32(ASMState *as, IRRef ref, int32_t *k)

79. {

80. if (irref_isk(ref)) {

81. IRIns *ir = IR(ref);

82. if (ir->o != IR_KINT64) {

83. *k = ir->i;

84. return 1;

85. } else if (checki32((int64_t)ir_kint64(ir)->u64)) {

86. *k = (int32_t)ir_kint64(ir)->u64;

87. return 1;

88. }

89. }

90. return 0;

91. }

93. /* Check if there's no conflicting instruction between curins and ref.

94. ** Also avoid fusing loads if there are multiple references.

95. */

96. ‌static int noconflict(ASMState *as, IRRef ref, IROp conflict, int noload)

97. {

98. IRIns *ir = as->ir;

99. IRRef i = as->curins;

100. if (i > ref + CONFLICT_SEARCH_LIM)

101. return 0;  /* Give up, ref is too far away. */

102. while (--i > ref) {

103. if (ir[i].o == conflict)

104. return 0;  /* Conflict found. */

105. else if (!noload && (ir[i].op1 == ref || ir[i].op2 == ref))

106. return 0;

107. }

108. return 1;  /* Ok, no conflict. */

109. }

111. /* Fuse array base into memory operand. */

112. ‌static IRRef asm_fuseabase(ASMState *as, IRRef ref)

113. {

114. IRIns *irb = IR(ref);

115. as->mrm.ofs = 0;

116. if (irb->o == IR_FLOAD) {

117. IRIns *ira = IR(irb->op1);

118. lua_assert(irb->op2 == IRFL_TAB_ARRAY);

119. /* We can avoid the FLOAD of t->array for colocated arrays. */

120. if (ira->o == IR_TNEW && ira->op1 <= LJ_MAX_COLOSIZE &&

121. !neverfuse(as) && noconflict(as, irb->op1, IR_NEWREF, 1)) {

122. as->mrm.ofs = (int32_t)sizeof(GCtab);  /* Ofs to colocated array. */

123. return irb->op1;  /* Table obj. */

124. }

125. } else if (irb->o == IR_ADD && irref_isk(irb->op2)) {

126. /* Fuse base offset (vararg load). */

127. as->mrm.ofs = IR(irb->op2)->i;

128. return irb->op1;

129. }

130. return ref;  /* Otherwise use the given array base. */

131. }

133. /* Fuse array reference into memory operand. */

134. ‌static void asm_fusearef(ASMState *as, IRIns *ir, RegSet allow)

135. {

136. IRIns *irx;

137. lua_assert(ir->o == IR_AREF);

138. as->mrm.base = (uint8_t)ra_alloc1(as, asm_fuseabase(as, ir->op1), allow);

139. irx = IR(ir->op2);

140. if (irref_isk(ir->op2)) {

141. as->mrm.ofs += 8*irx->i;

142. as->mrm.idx = RID_NONE;

143. } else {

144. rset_clear(allow, as->mrm.base);

145. as->mrm.scale = XM_SCALE8;

146. /* Fuse a constant ADD (e.g. t[i+1]) into the offset.

147. ** Doesn't help much without ABCelim, but reduces register pressure.

148. */

149. if (!LJ_64 &&  /* Has bad effects with negative index on x64. */

150. mayfuse(as, ir->op2) && ra_noreg(irx->r) &&

151. irx->o == IR_ADD && irref_isk(irx->op2)) {

152. as->mrm.ofs += 8*IR(irx->op2)->i;

153. as->mrm.idx = (uint8_t)ra_alloc1(as, irx->op1, allow);

154. } else {

155. as->mrm.idx = (uint8_t)ra_alloc1(as, ir->op2, allow);

156. }

157. }

158. }

160. /* Fuse array/hash/upvalue reference into memory operand.

161. ** Caveat: this may allocate GPRs for the base/idx registers. Be sure to

162. ** pass the final allow mask, excluding any GPRs used for other inputs.

163. ** In particular: 2-operand GPR instructions need to call ra_dest() first!

164. */

165. ‌static void asm_fuseahuref(ASMState *as, IRRef ref, RegSet allow)

166. {

167. IRIns *ir = IR(ref);

168. if (ra_noreg(ir->r)) {

169. switch ((IROp)ir->o) {

170. case IR_AREF:

171. if (mayfuse(as, ref)) {

172. asm_fusearef(as, ir, allow);

173. return;

174. }

175. break;

176. case IR_HREFK:

177. if (mayfuse(as, ref)) {

178. as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow);

179. as->mrm.ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));

180. as->mrm.idx = RID_NONE;

181. return;

182. }

183. break;

184. case IR_UREFC:

185. if (irref_isk(ir->op1)) {

186. GCfunc *fn = ir_kfunc(IR(ir->op1));

187. GCupval *uv = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv;

188. as->mrm.ofs = ptr2addr(&uv->tv);

189. as->mrm.base = as->mrm.idx = RID_NONE;

190. return;

191. }

192. break;

193. default:

194. lua_assert(ir->o == IR_HREF || ir->o == IR_NEWREF || ir->o == IR_UREFO ||

195. ir->o == IR_KKPTR);

196. break;

197. }

198. }

199. as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow);

200. as->mrm.ofs = 0;

201. as->mrm.idx = RID_NONE;

202. }

204. /* Fuse FLOAD/FREF reference into memory operand. */

205. ‌static void asm_fusefref(ASMState *as, IRIns *ir, RegSet allow)

206. {

207. lua_assert(ir->o == IR_FLOAD || ir->o == IR_FREF);

208. as->mrm.ofs = field_ofs[ir->op2];

209. as->mrm.idx = RID_NONE;

210. if (irref_isk(ir->op1)) {

211. as->mrm.ofs += IR(ir->op1)->i;

212. as->mrm.base = RID_NONE;

213. } else {

214. as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow);

215. }

216. }

218. /* Fuse string reference into memory operand. */

219. ‌static void asm_fusestrref(ASMState *as, IRIns *ir, RegSet allow)

220. {

221. IRIns *irr;

222. lua_assert(ir->o == IR_STRREF);

223. as->mrm.base = as->mrm.idx = RID_NONE;

224. as->mrm.scale = XM_SCALE1;

225. as->mrm.ofs = sizeof(GCstr);

226. if (irref_isk(ir->op1)) {

227. as->mrm.ofs += IR(ir->op1)->i;

228. } else {

229. Reg r = ra_alloc1(as, ir->op1, allow);

230. rset_clear(allow, r);

231. as->mrm.base = (uint8_t)r;

232. }

233. irr = IR(ir->op2);

234. if (irref_isk(ir->op2)) {

235. as->mrm.ofs += irr->i;

236. } else {

237. Reg r;

238. /* Fuse a constant add into the offset, e.g. string.sub(s, i+10). */

239. if (!LJ_64 &&  /* Has bad effects with negative index on x64. */

240. mayfuse(as, ir->op2) && irr->o == IR_ADD && irref_isk(irr->op2)) {

241. as->mrm.ofs += IR(irr->op2)->i;

242. r = ra_alloc1(as, irr->op1, allow);

243. } else {

244. r = ra_alloc1(as, ir->op2, allow);

245. }

246. if (as->mrm.base == RID_NONE)

247. as->mrm.base = (uint8_t)r;

248. else

249. as->mrm.idx = (uint8_t)r;

250. }

251. }

253. ‌static void asm_fusexref(ASMState *as, IRRef ref, RegSet allow)

254. {

255. IRIns *ir = IR(ref);

256. as->mrm.idx = RID_NONE;

257. if (ir->o == IR_KPTR || ir->o == IR_KKPTR) {

258. as->mrm.ofs = ir->i;

259. as->mrm.base = RID_NONE;

260. } else if (ir->o == IR_STRREF) {

261. asm_fusestrref(as, ir, allow);

262. } else {

263. as->mrm.ofs = 0;

264. if (canfuse(as, ir) && ir->o == IR_ADD && ra_noreg(ir->r)) {

265. /* Gather (base+idx*sz)+ofs as emitted by cdata ptr/array indexing. */

266. IRIns *irx;

267. IRRef idx;

268. Reg r;

269. if (asm_isk32(as, ir->op2, &as->mrm.ofs)) {  /* Recognize x+ofs. */

270. ref = ir->op1;

271. ir = IR(ref);

272. if (!(ir->o == IR_ADD && canfuse(as, ir) && ra_noreg(ir->r)))

273. goto noadd;

274. }

275. as->mrm.scale = XM_SCALE1;

276. idx = ir->op1;

277. ref = ir->op2;

278. irx = IR(idx);

279. if (!(irx->o == IR_BSHL || irx->o == IR_ADD)) {  /* Try other operand. */

280. idx = ir->op2;

281. ref = ir->op1;

282. irx = IR(idx);

283. }

284. if (canfuse(as, irx) && ra_noreg(irx->r)) {

285. if (irx->o == IR_BSHL && irref_isk(irx->op2) && IR(irx->op2)->i <= 3) {

286. /* Recognize idx<<b with b = 0-3, corresponding to sz = (1),2,4,8. */

287. idx = irx->op1;

288. as->mrm.scale = (uint8_t)(IR(irx->op2)->i << 6);

289. } else if (irx->o == IR_ADD && irx->op1 == irx->op2) {

290. /* FOLD does idx*2 ==> idx<<1 ==> idx+idx. */

291. idx = irx->op1;

292. as->mrm.scale = XM_SCALE2;

293. }

294. }

295. r = ra_alloc1(as, idx, allow);

296. rset_clear(allow, r);

297. as->mrm.idx = (uint8_t)r;

298. }

299. noadd:

300. as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow);

301. }

302. }

304. /* Fuse load into memory operand. */

305. ‌static Reg asm_fuseload(ASMState *as, IRRef ref, RegSet allow)

306. {

307. IRIns *ir = IR(ref);

308. if (ra_hasreg(ir->r)) {

309. if (allow != RSET_EMPTY) {  /* Fast path. */

310. ra_noweak(as, ir->r);

311. return ir->r;

312. }

313. fusespill:

314. /* Force a spill if only memory operands are allowed (asm_x87load). */

315. as->mrm.base = RID_ESP;

316. as->mrm.ofs = ra_spill(as, ir);

317. as->mrm.idx = RID_NONE;

318. return RID_MRM;

319. }

320. if (ir->o == IR_KNUM) {

321. RegSet avail = as->freeset & ~as->modset & RSET_FPR;

322. lua_assert(allow != RSET_EMPTY);

323. if (!(avail & (avail-1))) {  /* Fuse if less than two regs available. */

324. as->mrm.ofs = ptr2addr(ir_knum(ir));

325. as->mrm.base = as->mrm.idx = RID_NONE;

326. return RID_MRM;

327. }

328. } else if (ir->o == IR_KINT64) {

329. RegSet avail = as->freeset & ~as->modset & RSET_GPR;

330. lua_assert(allow != RSET_EMPTY);

331. if (!(avail & (avail-1))) {  /* Fuse if less than two regs available. */

332. as->mrm.ofs = ptr2addr(ir_kint64(ir));

333. as->mrm.base = as->mrm.idx = RID_NONE;

334. return RID_MRM;

335. }

336. } else if (mayfuse(as, ref)) {

337. RegSet xallow = (allow & RSET_GPR) ? allow : RSET_GPR;

338. if (ir->o == IR_SLOAD) {

339. if (!(ir->op2 & (IRSLOAD_PARENT|IRSLOAD_CONVERT)) &&

340. noconflict(as, ref, IR_RETF, 0)) {

341. as->mrm.base = (uint8_t)ra_alloc1(as, REF_BASE, xallow);

342. as->mrm.ofs = 8*((int32_t)ir->op1-1) + ((ir->op2&IRSLOAD_FRAME)?4:0);

343. as->mrm.idx = RID_NONE;

344. return RID_MRM;

345. }

346. } else if (ir->o == IR_FLOAD) {

347. /* Generic fusion is only ok for 32 bit operand (but see asm_comp). */

348. if ((irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)) &&

349. noconflict(as, ref, IR_FSTORE, 0)) {

350. asm_fusefref(as, ir, xallow);

351. return RID_MRM;

352. }

353. } else if (ir->o == IR_ALOAD || ir->o == IR_HLOAD || ir->o == IR_ULOAD) {

354. if (noconflict(as, ref, ir->o + IRDELTA_L2S, 0)) {

355. asm_fuseahuref(as, ir->op1, xallow);

356. return RID_MRM;

357. }

358. } else if (ir->o == IR_XLOAD) {

359. /* Generic fusion is not ok for 8/16 bit operands (but see asm_comp).

360. ** Fusing unaligned memory operands is ok on x86 (except for SIMD types).

361. */

362. if ((!irt_typerange(ir->t, IRT_I8, IRT_U16)) &&

363. noconflict(as, ref, IR_XSTORE, 0)) {

364. asm_fusexref(as, ir->op1, xallow);

365. return RID_MRM;

366. }

367. } else if (ir->o == IR_VLOAD) {

368. asm_fuseahuref(as, ir->op1, xallow);

369. return RID_MRM;

370. }

371. }

372. if (!(as->freeset & allow) && !irref_isk(ref) &&

373. (allow == RSET_EMPTY || ra_hasspill(ir->s) || iscrossref(as, ref)))

374. goto fusespill;

375. return ra_allocref(as, ref, allow);

376. }

378. #if LJ_64

379. /* Don't fuse a 32 bit load into a 64 bit operation. */

380. ‌static Reg asm_fuseloadm(ASMState *as, IRRef ref, RegSet allow, int is64)

381. {

382. if (is64 && !irt_is64(IR(ref)->t))

383. return ra_alloc1(as, ref, allow);

384. return asm_fuseload(as, ref, allow);

385. }

386. #else

387. ‌#define asm_fuseloadm(as, ref, allow, is64)  asm_fuseload(as, (ref), (allow))

388. #endif

390. /* -- Calls --------------------------------------------------------------- */

392. /* Count the required number of stack slots for a call. */

393. ‌static int asm_count_call_slots(ASMState *as, const CCallInfo *ci, IRRef *args)

394. {

395. uint32_t i, nargs = CCI_XNARGS(ci);

396. int nslots = 0;

397. #if LJ_64

398. if (LJ_ABI_WIN) {

399. nslots = (int)(nargs*2);  /* Only matters for more than four args. */

400. } else {

401. int ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;

402. for (i = 0; i < nargs; i++)

403. if (args[i] && irt_isfp(IR(args[i])->t)) {

404. if (nfpr > 0) nfpr--; else nslots += 2;

405. } else {

406. if (ngpr > 0) ngpr--; else nslots += 2;

407. }

408. }

409. #else

410. int ngpr = 0;

411. if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL)

412. ngpr = 2;

413. else if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL)

414. ngpr = 1;

415. for (i = 0; i < nargs; i++)

416. if (args[i] && irt_isfp(IR(args[i])->t)) {

417. nslots += irt_isnum(IR(args[i])->t) ? 2 : 1;

418. } else {

419. if (ngpr > 0) ngpr--; else nslots++;

420. }

421. #endif

422. return nslots;

423. }

425. /* Generate a call to a C function. */

426. ‌static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)

427. {

428. uint32_t n, nargs = CCI_XNARGS(ci);

429. int32_t ofs = STACKARG_OFS;

430. #if LJ_64

431. uint32_t gprs = REGARG_GPRS;

432. Reg fpr = REGARG_FIRSTFPR;

433. #if !LJ_ABI_WIN

434. MCode *patchnfpr = NULL;

435. #endif

436. #else

437. uint32_t gprs = 0;

438. if ((ci->flags & CCI_CC_MASK) != CCI_CC_CDECL) {

439. if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL)

440. gprs = (REGARG_GPRS & 31);

441. else if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL)

442. gprs = REGARG_GPRS;

443. }

444. #endif

445. if ((void *)ci->func)

446. emit_call(as, ci->func);

447. #if LJ_64

448. if ((ci->flags & CCI_VARARG)) {  /* Special handling for vararg calls. */

449. #if LJ_ABI_WIN

450. for (n = 0; n < 4 && n < nargs; n++) {

451. IRIns *ir = IR(args[n]);

452. if (irt_isfp(ir->t))  /* Duplicate FPRs in GPRs. */

453. emit_rr(as, XO_MOVDto, (irt_isnum(ir->t) ? REX_64 : 0) | (fpr+n),

454. ((gprs >> (n*5)) & 31));  /* Either MOVD or MOVQ. */

455. }

456. #else

457. patchnfpr = --as->mcp;  /* Indicate number of used FPRs in register al. */

458. *--as->mcp = XI_MOVrib | RID_EAX;

459. #endif

460. }

461. #endif

462. for (n = 0; n < nargs; n++) {  /* Setup args. */

463. IRRef ref = args[n];

464. IRIns *ir = IR(ref);

465. Reg r;

466. #if LJ_64 && LJ_ABI_WIN

467. /* Windows/x64 argument registers are strictly positional. */

468. r = irt_isfp(ir->t) ? (fpr <= REGARG_LASTFPR ? fpr : 0) : (gprs & 31);

469. fpr++; gprs >>= 5;

470. #elif LJ_64

471. /* POSIX/x64 argument registers are used in order of appearance. */

472. if (irt_isfp(ir->t)) {

473. r = fpr <= REGARG_LASTFPR ? fpr++ : 0;

474. } else {

475. r = gprs & 31; gprs >>= 5;

476. }

477. #else

478. if (ref && irt_isfp(ir->t)) {

479. r = 0;

480. } else {

481. r = gprs & 31; gprs >>= 5;

482. if (!ref) continue;

483. }

484. #endif

485. if (r) {  /* Argument is in a register. */

486. if (r < RID_MAX_GPR && ref < ASMREF_TMP1) {

487. #if LJ_64

488. if (ir->o == IR_KINT64)

489. emit_loadu64(as, r, ir_kint64(ir)->u64);

490. else

491. #endif

492. emit_loadi(as, r, ir->i);

493. } else {

494. lua_assert(rset_test(as->freeset, r));  /* Must have been evicted. */

495. if (ra_hasreg(ir->r)) {

496. ra_noweak(as, ir->r);

497. emit_movrr(as, ir, r, ir->r);

498. } else {

499. ra_allocref(as, ref, RID2RSET(r));

500. }

501. }

502. } else if (irt_isfp(ir->t)) {  /* FP argument is on stack. */

503. lua_assert(!(irt_isfloat(ir->t) && irref_isk(ref)));  /* No float k. */

504. if (LJ_32 && (ofs & 4) && irref_isk(ref)) {

505. /* Split stores for unaligned FP consts. */

506. emit_movmroi(as, RID_ESP, ofs, (int32_t)ir_knum(ir)->u32.lo);

507. emit_movmroi(as, RID_ESP, ofs+4, (int32_t)ir_knum(ir)->u32.hi);

508. } else {

509. r = ra_alloc1(as, ref, RSET_FPR);

510. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto,

511. r, RID_ESP, ofs);

512. }

513. ofs += (LJ_32 && irt_isfloat(ir->t)) ? 4 : 8;

514. } else {  /* Non-FP argument is on stack. */

515. if (LJ_32 && ref < ASMREF_TMP1) {

516. emit_movmroi(as, RID_ESP, ofs, ir->i);

517. } else {

518. r = ra_alloc1(as, ref, RSET_GPR);

519. emit_movtomro(as, REX_64 + r, RID_ESP, ofs);

520. }

521. ofs += sizeof(intptr_t);

522. }

523. checkmclim(as);

524. }

525. #if LJ_64 && !LJ_ABI_WIN

526. if (patchnfpr) *patchnfpr = fpr - REGARG_FIRSTFPR;

527. #endif

528. }

530. /* Setup result reg/sp for call. Evict scratch regs. */

531. ‌static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)

532. {

533. RegSet drop = RSET_SCRATCH;

534. int hiop = (LJ_32 && (ir+1)->o == IR_HIOP);

535. if ((ci->flags & CCI_NOFPRCLOBBER))

536. drop &= ~RSET_FPR;

537. if (ra_hasreg(ir->r))

538. rset_clear(drop, ir->r);  /* Dest reg handled below. */

539. if (hiop && ra_hasreg((ir+1)->r))

540. rset_clear(drop, (ir+1)->r);  /* Dest reg handled below. */

541. ra_evictset(as, drop);  /* Evictions must be performed first. */

542. if (ra_used(ir)) {

543. if (irt_isfp(ir->t)) {

544. int32_t ofs = sps_scale(ir->s);  /* Use spill slot or temp slots. */

545. #if LJ_64

546. if ((ci->flags & CCI_CASTU64)) {

547. Reg dest = ir->r;

548. if (ra_hasreg(dest)) {

549. ra_free(as, dest);

550. ra_modified(as, dest);

551. emit_rr(as, XO_MOVD, dest|REX_64, RID_RET);  /* Really MOVQ. */

552. }

553. if (ofs) emit_movtomro(as, RID_RET|REX_64, RID_ESP, ofs);

554. } else {

555. ra_destreg(as, ir, RID_FPRET);

556. }

557. #else

558. /* Number result is in x87 st0 for x86 calling convention. */

559. Reg dest = ir->r;

560. if (ra_hasreg(dest)) {

561. ra_free(as, dest);

562. ra_modified(as, dest);

563. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSD : XO_MOVSS,

564. dest, RID_ESP, ofs);

565. }

566. if ((ci->flags & CCI_CASTU64)) {

567. emit_movtomro(as, RID_RETLO, RID_ESP, ofs);

568. emit_movtomro(as, RID_RETHI, RID_ESP, ofs+4);

569. } else {

570. emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd,

571. irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs);

572. }

573. #endif

574. #if LJ_32

575. } else if (hiop) {

576. ra_destpair(as, ir);

577. #endif

578. } else {

579. lua_assert(!irt_ispri(ir->t));

580. ra_destreg(as, ir, RID_RET);

581. }

582. } else if (LJ_32 && irt_isfp(ir->t) && !(ci->flags & CCI_CASTU64)) {

583. emit_x87op(as, XI_FPOP);  /* Pop unused result from x87 st0. */

584. }

585. }

587. /* Return a constant function pointer or NULL for indirect calls. */

588. ‌static void *asm_callx_func(ASMState *as, IRIns *irf, IRRef func)

589. {

590. #if LJ_32

591. UNUSED(as);

592. if (irref_isk(func))

593. return (void *)irf->i;

594. #else

595. if (irref_isk(func)) {

596. MCode *p;

597. if (irf->o == IR_KINT64)

598. p = (MCode *)(void *)ir_k64(irf)->u64;

599. else

600. p = (MCode *)(void *)(uintptr_t)(uint32_t)irf->i;

601. if (p - as->mcp == (int32_t)(p - as->mcp))

602. return p;  /* Call target is still in +-2GB range. */

603. /* Avoid the indirect case of emit_call(). Try to hoist func addr. */

604. }

605. #endif

606. return NULL;

607. }

609. ‌static void asm_callx(ASMState *as, IRIns *ir)

610. {

611. IRRef args[CCI_NARGS_MAX*2];

612. CCallInfo ci;

613. IRRef func;

614. IRIns *irf;

615. int32_t spadj = 0;

616. ci.flags = asm_callx_flags(as, ir);

617. asm_collectargs(as, ir, &ci, args);

618. asm_setupresult(as, ir, &ci);

619. #if LJ_32

620. /* Have to readjust stack after non-cdecl calls due to callee cleanup. */

621. if ((ci.flags & CCI_CC_MASK) != CCI_CC_CDECL)

622. spadj = 4 * asm_count_call_slots(as, &ci, args);

623. #endif

624. func = ir->op2; irf = IR(func);

625. if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }

626. ci.func = (ASMFunction)asm_callx_func(as, irf, func);

627. if (!(void *)ci.func) {

628. /* Use a (hoistable) non-scratch register for indirect calls. */

629. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);

630. Reg r = ra_alloc1(as, func, allow);

631. if (LJ_32) emit_spsub(as, spadj);  /* Above code may cause restores! */

632. emit_rr(as, XO_GROUP5, XOg_CALL, r);

633. } else if (LJ_32) {

634. emit_spsub(as, spadj);

635. }

636. asm_gencall(as, &ci, args);

637. }

639. /* -- Returns ------------------------------------------------------------- */

641. /* Return to lower frame. Guard that it goes to the right spot. */

642. ‌static void asm_retf(ASMState *as, IRIns *ir)

643. {

644. Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);

645. void *pc = ir_kptr(IR(ir->op2));

646. int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));

647. as->topslot -= (BCReg)delta;

648. if ((int32_t)as->topslot < 0) as->topslot = 0;

649. irt_setmark(IR(REF_BASE)->t);  /* Children must not coalesce with BASE reg. */

650. emit_setgl(as, base, jit_base);

651. emit_addptr(as, base, -8*delta);

652. asm_guardcc(as, CC_NE);

653. emit_gmroi(as, XG_ARITHi(XOg_CMP), base, -4, ptr2addr(pc));

654. }

656. /* -- Type conversions ---------------------------------------------------- */

658. ‌static void asm_tointg(ASMState *as, IRIns *ir, Reg left)

659. {

660. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));

661. Reg dest = ra_dest(as, ir, RSET_GPR);

662. asm_guardcc(as, CC_P);

663. asm_guardcc(as, CC_NE);

664. emit_rr(as, XO_UCOMISD, left, tmp);

665. emit_rr(as, XO_CVTSI2SD, tmp, dest);

666. emit_rr(as, XO_XORPS, tmp, tmp);  /* Avoid partial register stall. */

667. emit_rr(as, XO_CVTTSD2SI, dest, left);

668. /* Can't fuse since left is needed twice. */

669. }

671. ‌static void asm_tobit(ASMState *as, IRIns *ir)

672. {

673. Reg dest = ra_dest(as, ir, RSET_GPR);

674. Reg tmp = ra_noreg(IR(ir->op1)->r) ?

675. ra_alloc1(as, ir->op1, RSET_FPR) :

676. ra_scratch(as, RSET_FPR);

677. Reg right = asm_fuseload(as, ir->op2, rset_exclude(RSET_FPR, tmp));

678. emit_rr(as, XO_MOVDto, tmp, dest);

679. emit_mrm(as, XO_ADDSD, tmp, right);

680. ra_left(as, tmp, ir->op1);

681. }

683. ‌static void asm_conv(ASMState *as, IRIns *ir)

684. {

685. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);

686. int st64 = (st == IRT_I64 || st == IRT_U64 || (LJ_64 && st == IRT_P64));

687. int stfp = (st == IRT_NUM || st == IRT_FLOAT);

688. IRRef lref = ir->op1;

689. lua_assert(irt_type(ir->t) != st);

690. lua_assert(!(LJ_32 && (irt_isint64(ir->t) || st64)));  /* Handled by SPLIT. */

691. if (irt_isfp(ir->t)) {

692. Reg dest = ra_dest(as, ir, RSET_FPR);

693. if (stfp) {  /* FP to FP conversion. */

694. Reg left = asm_fuseload(as, lref, RSET_FPR);

695. emit_mrm(as, st == IRT_NUM ? XO_CVTSD2SS : XO_CVTSS2SD, dest, left);

696. if (left == dest) return;  /* Avoid the XO_XORPS. */

697. } else if (LJ_32 && st == IRT_U32) {  /* U32 to FP conversion on x86. */

698. /* number = (2^52+2^51 .. u32) - (2^52+2^51) */

699. cTValue *k = lj_ir_k64_find(as->J, U64x(43380000,00000000));

700. Reg bias = ra_scratch(as, rset_exclude(RSET_FPR, dest));

701. if (irt_isfloat(ir->t))

702. emit_rr(as, XO_CVTSD2SS, dest, dest);

703. emit_rr(as, XO_SUBSD, dest, bias);  /* Subtract 2^52+2^51 bias. */

704. emit_rr(as, XO_XORPS, dest, bias);  /* Merge bias and integer. */

705. emit_loadn(as, bias, k);

706. emit_mrm(as, XO_MOVD, dest, asm_fuseload(as, lref, RSET_GPR));

707. return;

708. } else {  /* Integer to FP conversion. */

709. Reg left = (LJ_64 && (st == IRT_U32 || st == IRT_U64)) ?

710. ra_alloc1(as, lref, RSET_GPR) :

711. asm_fuseloadm(as, lref, RSET_GPR, st64);

712. if (LJ_64 && st == IRT_U64) {

713. MCLabel l_end = emit_label(as);

714. const void *k = lj_ir_k64_find(as->J, U64x(43f00000,00000000));

715. emit_rma(as, XO_ADDSD, dest, k);  /* Add 2^64 to compensate. */

716. emit_sjcc(as, CC_NS, l_end);

717. emit_rr(as, XO_TEST, left|REX_64, left);  /* Check if u64 >= 2^63. */

718. }

719. emit_mrm(as, irt_isnum(ir->t) ? XO_CVTSI2SD : XO_CVTSI2SS,

720. dest|((LJ_64 && (st64 || st == IRT_U32)) ? REX_64 : 0), left);

721. }

722. emit_rr(as, XO_XORPS, dest, dest);  /* Avoid partial register stall. */

723. } else if (stfp) {  /* FP to integer conversion. */

724. if (irt_isguard(ir->t)) {

725. /* Checked conversions are only supported from number to int. */

726. lua_assert(irt_isint(ir->t) && st == IRT_NUM);

727. asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));

728. } else {

729. Reg dest = ra_dest(as, ir, RSET_GPR);

730. x86Op op = st == IRT_NUM ? XO_CVTTSD2SI : XO_CVTTSS2SI;

731. if (LJ_64 ? irt_isu64(ir->t) : irt_isu32(ir->t)) {

732. /* LJ_64: For inputs >= 2^63 add -2^64, convert again. */

733. /* LJ_32: For inputs >= 2^31 add -2^31, convert again and add 2^31. */

734. Reg tmp = ra_noreg(IR(lref)->r) ? ra_alloc1(as, lref, RSET_FPR) :

735. ra_scratch(as, RSET_FPR);

736. MCLabel l_end = emit_label(as);

737. if (LJ_32)

738. emit_gri(as, XG_ARITHi(XOg_ADD), dest, (int32_t)0x80000000);

739. emit_rr(as, op, dest|REX_64, tmp);

740. if (st == IRT_NUM)

741. emit_rma(as, XO_ADDSD, tmp, lj_ir_k64_find(as->J,

742. LJ_64 ? U64x(c3f00000,00000000) : U64x(c1e00000,00000000)));

743. else

744. emit_rma(as, XO_ADDSS, tmp, lj_ir_k64_find(as->J,

745. LJ_64 ? U64x(00000000,df800000) : U64x(00000000,cf000000)));

746. emit_sjcc(as, CC_NS, l_end);

747. emit_rr(as, XO_TEST, dest|REX_64, dest);  /* Check if dest negative. */

748. emit_rr(as, op, dest|REX_64, tmp);

749. ra_left(as, tmp, lref);

750. } else {

751. Reg left = asm_fuseload(as, lref, RSET_FPR);

752. if (LJ_64 && irt_isu32(ir->t))

753. emit_rr(as, XO_MOV, dest, dest);  /* Zero hiword. */

754. emit_mrm(as, op,

755. dest|((LJ_64 &&

756. (irt_is64(ir->t) || irt_isu32(ir->t))) ? REX_64 : 0),

757. left);

758. }

759. }

760. } else if (st >= IRT_I8 && st <= IRT_U16) {  /* Extend to 32 bit integer. */

761. Reg left, dest = ra_dest(as, ir, RSET_GPR);

762. RegSet allow = RSET_GPR;

763. x86Op op;

764. lua_assert(irt_isint(ir->t) || irt_isu32(ir->t));

765. if (st == IRT_I8) {

766. op = XO_MOVSXb; allow = RSET_GPR8; dest |= FORCE_REX;

767. } else if (st == IRT_U8) {

768. op = XO_MOVZXb; allow = RSET_GPR8; dest |= FORCE_REX;

769. } else if (st == IRT_I16) {

770. op = XO_MOVSXw;

771. } else {

772. op = XO_MOVZXw;

773. }

774. left = asm_fuseload(as, lref, allow);

775. /* Add extra MOV if source is already in wrong register. */

776. if (!LJ_64 && left != RID_MRM && !rset_test(allow, left)) {

777. Reg tmp = ra_scratch(as, allow);

778. emit_rr(as, op, dest, tmp);

779. emit_rr(as, XO_MOV, tmp, left);

780. } else {

781. emit_mrm(as, op, dest, left);

782. }

783. } else {  /* 32/64 bit integer conversions. */

784. if (LJ_32) {  /* Only need to handle 32/32 bit no-op (cast) on x86. */

785. Reg dest = ra_dest(as, ir, RSET_GPR);

786. ra_left(as, dest, lref);  /* Do nothing, but may need to move regs. */

787. } else if (irt_is64(ir->t)) {

788. Reg dest = ra_dest(as, ir, RSET_GPR);

789. if (st64 || !(ir->op2 & IRCONV_SEXT)) {

790. /* 64/64 bit no-op (cast) or 32 to 64 bit zero extension. */

791. ra_left(as, dest, lref);  /* Do nothing, but may need to move regs. */

792. } else {  /* 32 to 64 bit sign extension. */

793. Reg left = asm_fuseload(as, lref, RSET_GPR);

794. emit_mrm(as, XO_MOVSXd, dest|REX_64, left);

795. }

796. } else {

797. Reg dest = ra_dest(as, ir, RSET_GPR);

798. if (st64) {

799. Reg left = asm_fuseload(as, lref, RSET_GPR);

800. /* This is either a 32 bit reg/reg mov which zeroes the hiword

801. ** or a load of the loword from a 64 bit address.

802. */

803. emit_mrm(as, XO_MOV, dest, left);

804. } else {  /* 32/32 bit no-op (cast). */

805. ra_left(as, dest, lref);  /* Do nothing, but may need to move regs. */

806. }

807. }

808. }

809. }

811. #if LJ_32 && LJ_HASFFI

812. /* No SSE conversions to/from 64 bit on x86, so resort to ugly x87 code. */

814. /* 64 bit integer to FP conversion in 32 bit mode. */

815. ‌static void asm_conv_fp_int64(ASMState *as, IRIns *ir)

816. {

817. Reg hi = ra_alloc1(as, ir->op1, RSET_GPR);

818. Reg lo = ra_alloc1(as, (ir-1)->op1, rset_exclude(RSET_GPR, hi));

819. int32_t ofs = sps_scale(ir->s);  /* Use spill slot or temp slots. */

820. Reg dest = ir->r;

821. if (ra_hasreg(dest)) {

822. ra_free(as, dest);

823. ra_modified(as, dest);

824. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSD : XO_MOVSS, dest, RID_ESP, ofs);

825. }

826. emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd,

827. irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs);

828. if (((ir-1)->op2 & IRCONV_SRCMASK) == IRT_U64) {

829. /* For inputs in [2^63,2^64-1] add 2^64 to compensate. */

830. MCLabel l_end = emit_label(as);

831. emit_rma(as, XO_FADDq, XOg_FADDq,

832. lj_ir_k64_find(as->J, U64x(43f00000,00000000)));

833. emit_sjcc(as, CC_NS, l_end);

834. emit_rr(as, XO_TEST, hi, hi);  /* Check if u64 >= 2^63. */

835. } else {

836. lua_assert(((ir-1)->op2 & IRCONV_SRCMASK) == IRT_I64);

837. }

838. emit_rmro(as, XO_FILDq, XOg_FILDq, RID_ESP, 0);

839. /* NYI: Avoid narrow-to-wide store-to-load forwarding stall. */

840. emit_rmro(as, XO_MOVto, hi, RID_ESP, 4);

841. emit_rmro(as, XO_MOVto, lo, RID_ESP, 0);

842. }

844. /* FP to 64 bit integer conversion in 32 bit mode. */

845. ‌static void asm_conv_int64_fp(ASMState *as, IRIns *ir)

846. {

847. IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK);

848. IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH);

849. Reg lo, hi;

850. lua_assert(st == IRT_NUM || st == IRT_FLOAT);

851. lua_assert(dt == IRT_I64 || dt == IRT_U64);

852. hi = ra_dest(as, ir, RSET_GPR);

853. lo = ra_dest(as, ir-1, rset_exclude(RSET_GPR, hi));

854. if (ra_used(ir-1)) emit_rmro(as, XO_MOV, lo, RID_ESP, 0);

855. /* NYI: Avoid wide-to-narrow store-to-load forwarding stall. */

856. if (!(as->flags & JIT_F_SSE3)) {  /* Set FPU rounding mode to default. */

857. emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 4);

858. emit_rmro(as, XO_MOVto, lo, RID_ESP, 4);

859. emit_gri(as, XG_ARITHi(XOg_AND), lo, 0xf3ff);

860. }

861. if (dt == IRT_U64) {

862. /* For inputs in [2^63,2^64-1] add -2^64 and convert again. */

863. MCLabel l_pop, l_end = emit_label(as);

864. emit_x87op(as, XI_FPOP);

865. l_pop = emit_label(as);

866. emit_sjmp(as, l_end);

867. emit_rmro(as, XO_MOV, hi, RID_ESP, 4);

868. if ((as->flags & JIT_F_SSE3))

869. emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0);

870. else

871. emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0);

872. emit_rma(as, XO_FADDq, XOg_FADDq,

873. lj_ir_k64_find(as->J, U64x(c3f00000,00000000)));

874. emit_sjcc(as, CC_NS, l_pop);

875. emit_rr(as, XO_TEST, hi, hi);  /* Check if out-of-range (2^63). */

876. }

877. emit_rmro(as, XO_MOV, hi, RID_ESP, 4);

878. if ((as->flags & JIT_F_SSE3)) {  /* Truncation is easy with SSE3. */

879. emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0);

880. } else {  /* Otherwise set FPU rounding mode to truncate before the store. */

881. emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0);

882. emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 0);

883. emit_rmro(as, XO_MOVtow, lo, RID_ESP, 0);

884. emit_rmro(as, XO_ARITHw(XOg_OR), lo, RID_ESP, 0);

885. emit_loadi(as, lo, 0xc00);

886. emit_rmro(as, XO_FNSTCW, XOg_FNSTCW, RID_ESP, 0);

887. }

888. if (dt == IRT_U64)

889. emit_x87op(as, XI_FDUP);

890. emit_mrm(as, st == IRT_NUM ? XO_FLDq : XO_FLDd,

891. st == IRT_NUM ? XOg_FLDq: XOg_FLDd,

892. asm_fuseload(as, ir->op1, RSET_EMPTY));

893. }

895. ‌static void asm_conv64(ASMState *as, IRIns *ir)

896. {

897. if (irt_isfp(ir->t))

898. asm_conv_fp_int64(as, ir);

899. else

900. asm_conv_int64_fp(as, ir);

901. }

902. #endif

904. ‌static void asm_strto(ASMState *as, IRIns *ir)

905. {

906. /* Force a spill slot for the destination register (if any). */

907. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];

908. IRRef args[2];

909. RegSet drop = RSET_SCRATCH;

910. if ((drop & RSET_FPR) != RSET_FPR && ra_hasreg(ir->r))

911. rset_set(drop, ir->r);  /* WIN64 doesn't spill all FPRs. */

912. ra_evictset(as, drop);

913. asm_guardcc(as, CC_E);

914. emit_rr(as, XO_TEST, RID_RET, RID_RET);  /* Test return status. */

915. args[0] = ir->op1;      /* GCstr *str */

916. args[1] = ASMREF_TMP1;  /* TValue *n  */

917. asm_gencall(as, ci, args);

918. /* Store the result to the spill slot or temp slots. */

919. emit_rmro(as, XO_LEA, ra_releasetmp(as, ASMREF_TMP1)|REX_64,

920. RID_ESP, sps_scale(ir->s));

921. }

923. /* -- Memory references --------------------------------------------------- */

925. /* Get pointer to TValue. */

926. ‌static void asm_tvptr(ASMState *as, Reg dest, IRRef ref)

927. {

928. IRIns *ir = IR(ref);

929. if (irt_isnum(ir->t)) {

930. /* For numbers use the constant itself or a spill slot as a TValue. */

931. if (irref_isk(ref))

932. emit_loada(as, dest, ir_knum(ir));

933. else

934. emit_rmro(as, XO_LEA, dest|REX_64, RID_ESP, ra_spill(as, ir));

935. } else {

936. /* Otherwise use g->tmptv to hold the TValue. */

937. if (!irref_isk(ref)) {

938. Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, dest));

939. emit_movtomro(as, REX_64IR(ir, src), dest, 0);

940. } else if (!irt_ispri(ir->t)) {

941. emit_movmroi(as, dest, 0, ir->i);

942. }

943. if (!(LJ_64 && irt_islightud(ir->t)))

944. emit_movmroi(as, dest, 4, irt_toitype(ir->t));

945. emit_loada(as, dest, &J2G(as->J)->tmptv);

946. }

947. }

949. ‌static void asm_aref(ASMState *as, IRIns *ir)

950. {

951. Reg dest = ra_dest(as, ir, RSET_GPR);

952. asm_fusearef(as, ir, RSET_GPR);

953. if (!(as->mrm.idx == RID_NONE && as->mrm.ofs == 0))

954. emit_mrm(as, XO_LEA, dest, RID_MRM);

955. else if (as->mrm.base != dest)

956. emit_rr(as, XO_MOV, dest, as->mrm.base);

957. }

959. /* Inlined hash lookup. Specialized for key type and for const keys.

960. ** The equivalent C code is:

961. **   Node *n = hashkey(t, key);

962. **   do {

963. **     if (lj_obj_equal(&n->key, key)) return &n->val;

964. **   } while ((n = nextnode(n)));

965. **   return niltv(L);

966. */

967. ‌static void asm_href(ASMState *as, IRIns *ir, IROp merge)

968. {

969. RegSet allow = RSET_GPR;

970. int destused = ra_used(ir);

971. Reg dest = ra_dest(as, ir, allow);

972. Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));

973. Reg key = RID_NONE, tmp = RID_NONE;

974. IRIns *irkey = IR(ir->op2);

975. int isk = irref_isk(ir->op2);

976. IRType1 kt = irkey->t;

977. uint32_t khash;

978. MCLabel l_end, l_loop, l_next;

980. if (!isk) {

981. rset_clear(allow, tab);

982. key = ra_alloc1(as, ir->op2, irt_isnum(kt) ? RSET_FPR : allow);

983. if (!irt_isstr(kt))

984. tmp = ra_scratch(as, rset_exclude(allow, key));

985. }

987. /* Key not found in chain: jump to exit (if merged) or load niltv. */

988. l_end = emit_label(as);

989. if (merge == IR_NE)

990. asm_guardcc(as, CC_E);  /* XI_JMP is not found by lj_asm_patchexit. */

991. else if (destused)

992. emit_loada(as, dest, niltvg(J2G(as->J)));

994. /* Follow hash chain until the end. */

995. l_loop = emit_sjcc_label(as, CC_NZ);

996. emit_rr(as, XO_TEST, dest, dest);

997. emit_rmro(as, XO_MOV, dest, dest, offsetof(Node, next));

998. l_next = emit_label(as);

1000. /* Type and value comparison. */

1001. if (merge == IR_EQ)

1002. asm_guardcc(as, CC_E);

1003. else

1004. emit_sjcc(as, CC_E, l_end);

1005. if (irt_isnum(kt)) {

1006. if (isk) {

1007. /* Assumes -0.0 is already canonicalized to +0.0. */

1008. emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.lo),

1009. (int32_t)ir_knum(irkey)->u32.lo);

1010. emit_sjcc(as, CC_NE, l_next);

1011. emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.hi),

1012. (int32_t)ir_knum(irkey)->u32.hi);

1013. } else {

1014. emit_sjcc(as, CC_P, l_next);

1015. emit_rmro(as, XO_UCOMISD, key, dest, offsetof(Node, key.n));

1016. emit_sjcc(as, CC_AE, l_next);

1017. /* The type check avoids NaN penalties and complaints from Valgrind. */

1018. #if LJ_64

1019. emit_u32(as, LJ_TISNUM);

1020. emit_rmro(as, XO_ARITHi, XOg_CMP, dest, offsetof(Node, key.it));

1021. #else

1022. emit_i8(as, LJ_TISNUM);

1023. emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it));

1024. #endif

1025. }

1026. #if LJ_64

1027. } else if (irt_islightud(kt)) {

1028. emit_rmro(as, XO_CMP, key|REX_64, dest, offsetof(Node, key.u64));

1029. #endif

1030. } else {

1031. if (!irt_ispri(kt)) {

1032. lua_assert(irt_isaddr(kt));

1033. if (isk)

1034. emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.gcr),

1035. ptr2addr(ir_kgc(irkey)));

1036. else

1037. emit_rmro(as, XO_CMP, key, dest, offsetof(Node, key.gcr));

1038. emit_sjcc(as, CC_NE, l_next);

1039. }

1040. lua_assert(!irt_isnil(kt));

1041. emit_i8(as, irt_toitype(kt));

1042. emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it));

1043. }

1044. emit_sfixup(as, l_loop);

1045. checkmclim(as);

1047. /* Load main position relative to tab->node into dest. */

1048. khash = isk ? ir_khash(irkey) : 1;

1049. if (khash == 0) {

1050. emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, node));

1051. } else {

1052. emit_rmro(as, XO_ARITH(XOg_ADD), dest, tab, offsetof(GCtab, node));

1053. if ((as->flags & JIT_F_PREFER_IMUL)) {

1054. emit_i8(as, sizeof(Node));

1055. emit_rr(as, XO_IMULi8, dest, dest);

1056. } else {

1057. emit_shifti(as, XOg_SHL, dest, 3);

1058. emit_rmrxo(as, XO_LEA, dest, dest, dest, XM_SCALE2, 0);

1059. }

1060. if (isk) {

1061. emit_gri(as, XG_ARITHi(XOg_AND), dest, (int32_t)khash);

1062. emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask));

1063. } else if (irt_isstr(kt)) {

1064. emit_rmro(as, XO_ARITH(XOg_AND), dest, key, offsetof(GCstr, hash));

1065. emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask));

1066. } else {  /* Must match with hashrot() in lj_tab.c. */

1067. emit_rmro(as, XO_ARITH(XOg_AND), dest, tab, offsetof(GCtab, hmask));

1068. emit_rr(as, XO_ARITH(XOg_SUB), dest, tmp);

1069. emit_shifti(as, XOg_ROL, tmp, HASH_ROT3);

1070. emit_rr(as, XO_ARITH(XOg_XOR), dest, tmp);

1071. emit_shifti(as, XOg_ROL, dest, HASH_ROT2);

1072. emit_rr(as, XO_ARITH(XOg_SUB), tmp, dest);

1073. emit_shifti(as, XOg_ROL, dest, HASH_ROT1);

1074. emit_rr(as, XO_ARITH(XOg_XOR), tmp, dest);

1075. if (irt_isnum(kt)) {

1076. emit_rr(as, XO_ARITH(XOg_ADD), dest, dest);

1077. #if LJ_64

1078. emit_shifti(as, XOg_SHR|REX_64, dest, 32);

1079. emit_rr(as, XO_MOV, tmp, dest);

1080. emit_rr(as, XO_MOVDto, key|REX_64, dest);

1081. #else

1082. emit_rmro(as, XO_MOV, dest, RID_ESP, ra_spill(as, irkey)+4);

1083. emit_rr(as, XO_MOVDto, key, tmp);

1084. #endif

1085. } else {

1086. emit_rr(as, XO_MOV, tmp, key);

1087. emit_rmro(as, XO_LEA, dest, key, HASH_BIAS);

1088. }

1089. }

1090. }

1091. }

1093. ‌static void asm_hrefk(ASMState *as, IRIns *ir)

1094. {

1095. IRIns *kslot = IR(ir->op2);

1096. IRIns *irkey = IR(kslot->op1);

1097. int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));

1098. Reg dest = ra_used(ir) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;

1099. Reg node = ra_alloc1(as, ir->op1, RSET_GPR);

1100. #if !LJ_64

1101. MCLabel l_exit;

1102. #endif

1103. lua_assert(ofs % sizeof(Node) == 0);

1104. if (ra_hasreg(dest)) {

1105. if (ofs != 0) {

1106. if (dest == node && !(as->flags & JIT_F_LEA_AGU))

1107. emit_gri(as, XG_ARITHi(XOg_ADD), dest, ofs);

1108. else

1109. emit_rmro(as, XO_LEA, dest, node, ofs);

1110. } else if (dest != node) {

1111. emit_rr(as, XO_MOV, dest, node);

1112. }

1113. }

1114. asm_guardcc(as, CC_NE);

1115. #if LJ_64

1116. if (!irt_ispri(irkey->t)) {

1117. Reg key = ra_scratch(as, rset_exclude(RSET_GPR, node));

1118. emit_rmro(as, XO_CMP, key|REX_64, node,

1119. ofs + (int32_t)offsetof(Node, key.u64));

1120. lua_assert(irt_isnum(irkey->t) || irt_isgcv(irkey->t));

1121. /* Assumes -0.0 is already canonicalized to +0.0. */

1122. emit_loadu64(as, key, irt_isnum(irkey->t) ? ir_knum(irkey)->u64 :

1123. ((uint64_t)irt_toitype(irkey->t) << 32) |

1124. (uint64_t)(uint32_t)ptr2addr(ir_kgc(irkey)));

1125. } else {

1126. lua_assert(!irt_isnil(irkey->t));

1127. emit_i8(as, irt_toitype(irkey->t));

1128. emit_rmro(as, XO_ARITHi8, XOg_CMP, node,

1129. ofs + (int32_t)offsetof(Node, key.it));

1130. }

1131. #else

1132. l_exit = emit_label(as);

1133. if (irt_isnum(irkey->t)) {

1134. /* Assumes -0.0 is already canonicalized to +0.0. */

1135. emit_gmroi(as, XG_ARITHi(XOg_CMP), node,

1136. ofs + (int32_t)offsetof(Node, key.u32.lo),

1137. (int32_t)ir_knum(irkey)->u32.lo);

1138. emit_sjcc(as, CC_NE, l_exit);

1139. emit_gmroi(as, XG_ARITHi(XOg_CMP), node,

1140. ofs + (int32_t)offsetof(Node, key.u32.hi),

1141. (int32_t)ir_knum(irkey)->u32.hi);

1142. } else {

1143. if (!irt_ispri(irkey->t)) {

1144. lua_assert(irt_isgcv(irkey->t));

1145. emit_gmroi(as, XG_ARITHi(XOg_CMP), node,

1146. ofs + (int32_t)offsetof(Node, key.gcr),

1147. ptr2addr(ir_kgc(irkey)));

1148. emit_sjcc(as, CC_NE, l_exit);

1149. }

1150. lua_assert(!irt_isnil(irkey->t));

1151. emit_i8(as, irt_toitype(irkey->t));

1152. emit_rmro(as, XO_ARITHi8, XOg_CMP, node,

1153. ofs + (int32_t)offsetof(Node, key.it));

1154. }

1155. #endif

1156. }

1158. ‌static void asm_uref(ASMState *as, IRIns *ir)

1159. {

1160. /* NYI: Check that UREFO is still open and not aliasing a slot. */

1161. Reg dest = ra_dest(as, ir, RSET_GPR);

1162. if (irref_isk(ir->op1)) {

1163. GCfunc *fn = ir_kfunc(IR(ir->op1));

1164. MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;

1165. emit_rma(as, XO_MOV, dest, v);

1166. } else {

1167. Reg uv = ra_scratch(as, RSET_GPR);

1168. Reg func = ra_alloc1(as, ir->op1, RSET_GPR);

1169. if (ir->o == IR_UREFC) {

1170. emit_rmro(as, XO_LEA, dest, uv, offsetof(GCupval, tv));

1171. asm_guardcc(as, CC_NE);

1172. emit_i8(as, 1);

1173. emit_rmro(as, XO_ARITHib, XOg_CMP, uv, offsetof(GCupval, closed));

1174. } else {

1175. emit_rmro(as, XO_MOV, dest, uv, offsetof(GCupval, v));

1176. }

1177. emit_rmro(as, XO_MOV, uv, func,

1178. (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8));

1179. }

1180. }

1182. ‌static void asm_fref(ASMState *as, IRIns *ir)

1183. {

1184. Reg dest = ra_dest(as, ir, RSET_GPR);

1185. asm_fusefref(as, ir, RSET_GPR);

1186. emit_mrm(as, XO_LEA, dest, RID_MRM);

1187. }

1189. ‌static void asm_strref(ASMState *as, IRIns *ir)

1190. {

1191. Reg dest = ra_dest(as, ir, RSET_GPR);

1192. asm_fusestrref(as, ir, RSET_GPR);

1193. if (as->mrm.base == RID_NONE)

1194. emit_loadi(as, dest, as->mrm.ofs);

1195. else if (as->mrm.base == dest && as->mrm.idx == RID_NONE)

1196. emit_gri(as, XG_ARITHi(XOg_ADD), dest, as->mrm.ofs);

1197. else

1198. emit_mrm(as, XO_LEA, dest, RID_MRM);

1199. }

1201. /* -- Loads and stores ---------------------------------------------------- */

1203. ‌static void asm_fxload(ASMState *as, IRIns *ir)

1204. {

1205. Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);

1206. x86Op xo;

1207. if (ir->o == IR_FLOAD)

1208. asm_fusefref(as, ir, RSET_GPR);

1209. else

1210. asm_fusexref(as, ir->op1, RSET_GPR);

1211. /* ir->op2 is ignored -- unaligned loads are ok on x86. */

1212. switch (irt_type(ir->t)) {

1213. case IRT_I8: xo = XO_MOVSXb; break;

1214. case IRT_U8: xo = XO_MOVZXb; break;

1215. case IRT_I16: xo = XO_MOVSXw; break;

1216. case IRT_U16: xo = XO_MOVZXw; break;

1217. case IRT_NUM: xo = XO_MOVSD; break;

1218. case IRT_FLOAT: xo = XO_MOVSS; break;

1219. default:

1220. if (LJ_64 && irt_is64(ir->t))

1221. dest |= REX_64;

1222. else

1223. lua_assert(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t));

1224. xo = XO_MOV;

1225. break;

1226. }

1227. emit_mrm(as, xo, dest, RID_MRM);

1228. }

1230. ‌#define asm_fload(as, ir)        asm_fxload(as, ir)

1231. ‌#define asm_xload(as, ir)        asm_fxload(as, ir)

1233. ‌static void asm_fxstore(ASMState *as, IRIns *ir)

1234. {

1235. RegSet allow = RSET_GPR;

1236. Reg src = RID_NONE, osrc = RID_NONE;

1237. int32_t k = 0;

1238. if (ir->r == RID_SINK)

1239. return;

1240. /* The IRT_I16/IRT_U16 stores should never be simplified for constant

1241. ** values since mov word [mem], imm16 has a length-changing prefix.

1242. */

1243. if (irt_isi16(ir->t) || irt_isu16(ir->t) || irt_isfp(ir->t) ||

1244. !asm_isk32(as, ir->op2, &k)) {

1245. RegSet allow8 = irt_isfp(ir->t) ? RSET_FPR :

1246. (irt_isi8(ir->t) || irt_isu8(ir->t)) ? RSET_GPR8 : RSET_GPR;

1247. src = osrc = ra_alloc1(as, ir->op2, allow8);

1248. if (!LJ_64 && !rset_test(allow8, src)) {  /* Already in wrong register. */

1249. rset_clear(allow, osrc);

1250. src = ra_scratch(as, allow8);

1251. }

1252. rset_clear(allow, src);

1253. }

1254. if (ir->o == IR_FSTORE) {

1255. asm_fusefref(as, IR(ir->op1), allow);

1256. } else {

1257. asm_fusexref(as, ir->op1, allow);

1258. if (LJ_32 && ir->o == IR_HIOP) as->mrm.ofs += 4;

1259. }

1260. if (ra_hasreg(src)) {

1261. x86Op xo;

1262. switch (irt_type(ir->t)) {

1263. case IRT_I8: case IRT_U8: xo = XO_MOVtob; src |= FORCE_REX; break;

1264. case IRT_I16: case IRT_U16: xo = XO_MOVtow; break;

1265. case IRT_NUM: xo = XO_MOVSDto; break;

1266. case IRT_FLOAT: xo = XO_MOVSSto; break;

1267. #if LJ_64

1268. case IRT_LIGHTUD: lua_assert(0);  /* NYI: mask 64 bit lightuserdata. */

1269. #endif

1270. default:

1271. if (LJ_64 && irt_is64(ir->t))

1272. src |= REX_64;

1273. else

1274. lua_assert(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t));

1275. xo = XO_MOVto;

1276. break;

1277. }

1278. emit_mrm(as, xo, src, RID_MRM);

1279. if (!LJ_64 && src != osrc) {

1280. ra_noweak(as, osrc);

1281. emit_rr(as, XO_MOV, src, osrc);

1282. }

1283. } else {

1284. if (irt_isi8(ir->t) || irt_isu8(ir->t)) {

1285. emit_i8(as, k);

1286. emit_mrm(as, XO_MOVmib, 0, RID_MRM);

1287. } else {

1288. lua_assert(irt_is64(ir->t) || irt_isint(ir->t) || irt_isu32(ir->t) ||

1289. irt_isaddr(ir->t));

1290. emit_i32(as, k);

1291. emit_mrm(as, XO_MOVmi, REX_64IR(ir, 0), RID_MRM);

1292. }

1293. }

1294. }

1296. ‌#define asm_fstore(as, ir)        asm_fxstore(as, ir)

1297. ‌#define asm_xstore(as, ir)        asm_fxstore(as, ir)

1299. #if LJ_64

1300. ‌static Reg asm_load_lightud64(ASMState *as, IRIns *ir, int typecheck)

1301. {

1302. if (ra_used(ir) || typecheck) {

1303. Reg dest = ra_dest(as, ir, RSET_GPR);

1304. if (typecheck) {

1305. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, dest));

1306. asm_guardcc(as, CC_NE);

1307. emit_i8(as, -2);

1308. emit_rr(as, XO_ARITHi8, XOg_CMP, tmp);

1309. emit_shifti(as, XOg_SAR|REX_64, tmp, 47);

1310. emit_rr(as, XO_MOV, tmp|REX_64, dest);

1311. }

1312. return dest;

1313. } else {

1314. return RID_NONE;

1315. }

1316. }

1317. #endif

1319. ‌static void asm_ahuvload(ASMState *as, IRIns *ir)

1320. {

1321. lua_assert(irt_isnum(ir->t) || irt_ispri(ir->t) || irt_isaddr(ir->t) ||

1322. (LJ_DUALNUM && irt_isint(ir->t)));

1323. #if LJ_64

1324. if (irt_islightud(ir->t)) {

1325. Reg dest = asm_load_lightud64(as, ir, 1);

1326. if (ra_hasreg(dest)) {

1327. asm_fuseahuref(as, ir->op1, RSET_GPR);

1328. emit_mrm(as, XO_MOV, dest|REX_64, RID_MRM);

1329. }

1330. return;

1331. } else

1332. #endif

1333. if (ra_used(ir)) {

1334. RegSet allow = irt_isnum(ir->t) ? RSET_FPR : RSET_GPR;

1335. Reg dest = ra_dest(as, ir, allow);

1336. asm_fuseahuref(as, ir->op1, RSET_GPR);

1337. emit_mrm(as, dest < RID_MAX_GPR ? XO_MOV : XO_MOVSD, dest, RID_MRM);

1338. } else {

1339. asm_fuseahuref(as, ir->op1, RSET_GPR);

1340. }

1341. /* Always do the type check, even if the load result is unused. */

1342. as->mrm.ofs += 4;

1343. asm_guardcc(as, irt_isnum(ir->t) ? CC_AE : CC_NE);

1344. if (LJ_64 && irt_type(ir->t) >= IRT_NUM) {

1345. lua_assert(irt_isinteger(ir->t) || irt_isnum(ir->t));

1346. emit_u32(as, LJ_TISNUM);

1347. emit_mrm(as, XO_ARITHi, XOg_CMP, RID_MRM);

1348. } else {

1349. emit_i8(as, irt_toitype(ir->t));

1350. emit_mrm(as, XO_ARITHi8, XOg_CMP, RID_MRM);

1351. }

1352. }

1354. ‌static void asm_ahustore(ASMState *as, IRIns *ir)

1355. {

1356. if (ir->r == RID_SINK)

1357. return;

1358. if (irt_isnum(ir->t)) {

1359. Reg src = ra_alloc1(as, ir->op2, RSET_FPR);

1360. asm_fuseahuref(as, ir->op1, RSET_GPR);

1361. emit_mrm(as, XO_MOVSDto, src, RID_MRM);

1362. #if LJ_64

1363. } else if (irt_islightud(ir->t)) {

1364. Reg src = ra_alloc1(as, ir->op2, RSET_GPR);

1365. asm_fuseahuref(as, ir->op1, rset_exclude(RSET_GPR, src));

1366. emit_mrm(as, XO_MOVto, src|REX_64, RID_MRM);

1367. #endif

1368. } else {

1369. IRIns *irr = IR(ir->op2);

1370. RegSet allow = RSET_GPR;

1371. Reg src = RID_NONE;

1372. if (!irref_isk(ir->op2)) {

1373. src = ra_alloc1(as, ir->op2, allow);

1374. rset_clear(allow, src);

1375. }

1376. asm_fuseahuref(as, ir->op1, allow);

1377. if (ra_hasreg(src)) {

1378. emit_mrm(as, XO_MOVto, src, RID_MRM);

1379. } else if (!irt_ispri(irr->t)) {

1380. lua_assert(irt_isaddr(ir->t) || (LJ_DUALNUM && irt_isinteger(ir->t)));

1381. emit_i32(as, irr->i);

1382. emit_mrm(as, XO_MOVmi, 0, RID_MRM);

1383. }

1384. as->mrm.ofs += 4;

1385. emit_i32(as, (int32_t)irt_toitype(ir->t));

1386. emit_mrm(as, XO_MOVmi, 0, RID_MRM);

1387. }

1388. }

1390. ‌static void asm_sload(ASMState *as, IRIns *ir)

1391. {

1392. int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0);

1393. IRType1 t = ir->t;

1394. Reg base;

1395. lua_assert(!(ir->op2 & IRSLOAD_PARENT));  /* Handled by asm_head_side(). */

1396. lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK));

1397. lua_assert(LJ_DUALNUM ||

1398. !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME)));

1399. if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {

1400. Reg left = ra_scratch(as, RSET_FPR);

1401. asm_tointg(as, ir, left);  /* Frees dest reg. Do this before base alloc. */

1402. base = ra_alloc1(as, REF_BASE, RSET_GPR);

1403. emit_rmro(as, XO_MOVSD, left, base, ofs);

1404. t.irt = IRT_NUM;  /* Continue with a regular number type check. */

1405. #if LJ_64

1406. } else if (irt_islightud(t)) {

1407. Reg dest = asm_load_lightud64(as, ir, (ir->op2 & IRSLOAD_TYPECHECK));

1408. if (ra_hasreg(dest)) {

1409. base = ra_alloc1(as, REF_BASE, RSET_GPR);

1410. emit_rmro(as, XO_MOV, dest|REX_64, base, ofs);

1411. }

1412. return;

1413. #endif

1414. } else if (ra_used(ir)) {

1415. RegSet allow = irt_isnum(t) ? RSET_FPR : RSET_GPR;

1416. Reg dest = ra_dest(as, ir, allow);

1417. base = ra_alloc1(as, REF_BASE, RSET_GPR);

1418. lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));

1419. if ((ir->op2 & IRSLOAD_CONVERT)) {

1420. t.irt = irt_isint(t) ? IRT_NUM : IRT_INT;  /* Check for original type. */

1421. emit_rmro(as, irt_isint(t) ? XO_CVTSI2SD : XO_CVTTSD2SI, dest, base, ofs);

1422. } else {

1423. emit_rmro(as, irt_isnum(t) ? XO_MOVSD : XO_MOV, dest, base, ofs);

1424. }

1425. } else {

1426. if (!(ir->op2 & IRSLOAD_TYPECHECK))

1427. return;  /* No type check: avoid base alloc. */

1428. base = ra_alloc1(as, REF_BASE, RSET_GPR);

1429. }

1430. if ((ir->op2 & IRSLOAD_TYPECHECK)) {

1431. /* Need type check, even if the load result is unused. */

1432. asm_guardcc(as, irt_isnum(t) ? CC_AE : CC_NE);

1433. if (LJ_64 && irt_type(t) >= IRT_NUM) {

1434. lua_assert(irt_isinteger(t) || irt_isnum(t));

1435. emit_u32(as, LJ_TISNUM);

1436. emit_rmro(as, XO_ARITHi, XOg_CMP, base, ofs+4);

1437. } else {

1438. emit_i8(as, irt_toitype(t));

1439. emit_rmro(as, XO_ARITHi8, XOg_CMP, base, ofs+4);

1440. }

1441. }

1442. }

1444. /* -- Allocations --------------------------------------------------------- */

1446. #if LJ_HASFFI

1447. ‌static void asm_cnew(ASMState *as, IRIns *ir)

1448. {

1449. CTState *cts = ctype_ctsG(J2G(as->J));

1450. CTypeID id = (CTypeID)IR(ir->op1)->i;

1451. CTSize sz;

1452. CTInfo info = lj_ctype_info(cts, id, &sz);

1453. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];

1454. IRRef args[4];

1455. lua_assert(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL));

1457. as->gcsteps++;

1458. asm_setupresult(as, ir, ci);  /* GCcdata * */

1460. /* Initialize immutable cdata object. */

1461. if (ir->o == IR_CNEWI) {

1462. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);

1463. #if LJ_64

1464. Reg r64 = sz == 8 ? REX_64 : 0;

1465. if (irref_isk(ir->op2)) {

1466. IRIns *irk = IR(ir->op2);

1467. uint64_t k = irk->o == IR_KINT64 ? ir_k64(irk)->u64 :

1468. (uint64_t)(uint32_t)irk->i;

1469. if (sz == 4 || checki32((int64_t)k)) {

1470. emit_i32(as, (int32_t)k);

1471. emit_rmro(as, XO_MOVmi, r64, RID_RET, sizeof(GCcdata));

1472. } else {

1473. emit_movtomro(as, RID_ECX + r64, RID_RET, sizeof(GCcdata));

1474. emit_loadu64(as, RID_ECX, k);

1475. }

1476. } else {

1477. Reg r = ra_alloc1(as, ir->op2, allow);

1478. emit_movtomro(as, r + r64, RID_RET, sizeof(GCcdata));

1479. }

1480. #else

1481. int32_t ofs = sizeof(GCcdata);

1482. if (sz == 8) {

1483. ofs += 4; ir++;

1484. lua_assert(ir->o == IR_HIOP);

1485. }

1486. do {

1487. if (irref_isk(ir->op2)) {

1488. emit_movmroi(as, RID_RET, ofs, IR(ir->op2)->i);

1489. } else {

1490. Reg r = ra_alloc1(as, ir->op2, allow);

1491. emit_movtomro(as, r, RID_RET, ofs);

1492. rset_clear(allow, r);

1493. }

1494. if (ofs == sizeof(GCcdata)) break;

1495. ofs -= 4; ir--;

1496. } while (1);

1497. #endif

1498. lua_assert(sz == 4 || sz == 8);

1499. } else if (ir->op2 != REF_NIL) {  /* Create VLA/VLS/aligned cdata. */

1500. ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];

1501. args[0] = ASMREF_L;     /* lua_State *L */

1502. args[1] = ir->op1;      /* CTypeID id   */

1503. args[2] = ir->op2;      /* CTSize sz    */

1504. args[3] = ASMREF_TMP1;  /* CTSize align */

1505. asm_gencall(as, ci, args);

1506. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));

1507. return;

1508. }

1510. /* Combine initialization of marked, gct and ctypeid. */

1511. emit_movtomro(as, RID_ECX, RID_RET, offsetof(GCcdata, marked));

1512. emit_gri(as, XG_ARITHi(XOg_OR), RID_ECX,

1513. (int32_t)((~LJ_TCDATA<<8)+(id<<16)));

1514. emit_gri(as, XG_ARITHi(XOg_AND), RID_ECX, LJ_GC_WHITES);

1515. emit_opgl(as, XO_MOVZXb, RID_ECX, gc.currentwhite);

1517. args[0] = ASMREF_L;     /* lua_State *L */

1518. args[1] = ASMREF_TMP1;  /* MSize size   */

1519. asm_gencall(as, ci, args);

1520. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)(sz+sizeof(GCcdata)));

1521. }

1522. #else

1523. ‌#define asm_cnew(as, ir)        ((void)0)

1524. #endif

1526. /* -- Write barriers ------------------------------------------------------ */

1528. ‌static void asm_tbar(ASMState *as, IRIns *ir)

1529. {

1530. Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);

1531. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, tab));

1532. MCLabel l_end = emit_label(as);

1533. emit_movtomro(as, tmp, tab, offsetof(GCtab, gclist));

1534. emit_setgl(as, tab, gc.grayagain);

1535. emit_getgl(as, tmp, gc.grayagain);

1536. emit_i8(as, ~LJ_GC_BLACK);

1537. emit_rmro(as, XO_ARITHib, XOg_AND, tab, offsetof(GCtab, marked));

1538. emit_sjcc(as, CC_Z, l_end);

1539. emit_i8(as, LJ_GC_BLACK);

1540. emit_rmro(as, XO_GROUP3b, XOg_TEST, tab, offsetof(GCtab, marked));

1541. }

1543. ‌static void asm_obar(ASMState *as, IRIns *ir)

1544. {

1545. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];

1546. IRRef args[2];

1547. MCLabel l_end;

1548. Reg obj;

1549. /* No need for other object barriers (yet). */

1550. lua_assert(IR(ir->op1)->o == IR_UREFC);

1551. ra_evictset(as, RSET_SCRATCH);

1552. l_end = emit_label(as);

1553. args[0] = ASMREF_TMP1;  /* global_State *g */

1554. args[1] = ir->op1;      /* TValue *tv      */

1555. asm_gencall(as, ci, args);

1556. emit_loada(as, ra_releasetmp(as, ASMREF_TMP1), J2G(as->J));

1557. obj = IR(ir->op1)->r;

1558. emit_sjcc(as, CC_Z, l_end);

1559. emit_i8(as, LJ_GC_WHITES);

1560. if (irref_isk(ir->op2)) {

1561. GCobj *vp = ir_kgc(IR(ir->op2));

1562. emit_rma(as, XO_GROUP3b, XOg_TEST, &vp->gch.marked);

1563. } else {

1564. Reg val = ra_alloc1(as, ir->op2, rset_exclude(RSET_SCRATCH&RSET_GPR, obj));

1565. emit_rmro(as, XO_GROUP3b, XOg_TEST, val, (int32_t)offsetof(GChead, marked));

1566. }

1567. emit_sjcc(as, CC_Z, l_end);

1568. emit_i8(as, LJ_GC_BLACK);

1569. emit_rmro(as, XO_GROUP3b, XOg_TEST, obj,

1570. (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));

1571. }

1573. /* -- FP/int arithmetic and logic operations ------------------------------ */

1575. /* Load reference onto x87 stack. Force a spill to memory if needed. */

1576. ‌static void asm_x87load(ASMState *as, IRRef ref)

1577. {

1578. IRIns *ir = IR(ref);

1579. if (ir->o == IR_KNUM) {

1580. cTValue *tv = ir_knum(ir);

1581. if (tvispzero(tv))  /* Use fldz only for +0. */

1582. emit_x87op(as, XI_FLDZ);

1583. else if (tvispone(tv))

1584. emit_x87op(as, XI_FLD1);

1585. else

1586. emit_rma(as, XO_FLDq, XOg_FLDq, tv);

1587. } else if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT && !ra_used(ir) &&

1588. !irref_isk(ir->op1) && mayfuse(as, ir->op1)) {

1589. IRIns *iri = IR(ir->op1);

1590. emit_rmro(as, XO_FILDd, XOg_FILDd, RID_ESP, ra_spill(as, iri));

1591. } else {

1592. emit_mrm(as, XO_FLDq, XOg_FLDq, asm_fuseload(as, ref, RSET_EMPTY));

1593. }

1594. }

1596. ‌static void asm_fpmath(ASMState *as, IRIns *ir)

1597. {

1598. IRFPMathOp fpm = (IRFPMathOp)ir->op2;

1599. if (fpm == IRFPM_SQRT) {

1600. Reg dest = ra_dest(as, ir, RSET_FPR);

1601. Reg left = asm_fuseload(as, ir->op1, RSET_FPR);

1602. emit_mrm(as, XO_SQRTSD, dest, left);

1603. } else if (fpm <= IRFPM_TRUNC) {

1604. if (as->flags & JIT_F_SSE4_1) {  /* SSE4.1 has a rounding instruction. */

1605. Reg dest = ra_dest(as, ir, RSET_FPR);

1606. Reg left = asm_fuseload(as, ir->op1, RSET_FPR);

1607. /* ROUNDSD has a 4-byte opcode which doesn't fit in x86Op.

1608. ** Let's pretend it's a 3-byte opcode, and compensate afterwards.

1609. ** This is atrocious, but the alternatives are much worse.

1610. */

1611. /* Round down/up/trunc == 1001/1010/1011. */

1612. emit_i8(as, 0x09 + fpm);

1613. emit_mrm(as, XO_ROUNDSD, dest, left);

1614. if (LJ_64 && as->mcp[1] != (MCode)(XO_ROUNDSD >> 16)) {

1615. as->mcp[0] = as->mcp[1]; as->mcp[1] = 0x0f;  /* Swap 0F and REX. */

1616. }

1617. *--as->mcp = 0x66;  /* 1st byte of ROUNDSD opcode. */

1618. } else {  /* Call helper functions for SSE2 variant. */

1619. /* The modified regs must match with the *.dasc implementation. */

1620. RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX);

1621. if (ra_hasreg(ir->r))

1622. rset_clear(drop, ir->r);  /* Dest reg handled below. */

1623. ra_evictset(as, drop);

1624. ra_destreg(as, ir, RID_XMM0);

1625. emit_call(as, fpm == IRFPM_FLOOR ? lj_vm_floor_sse :

1626. fpm == IRFPM_CEIL ? lj_vm_ceil_sse : lj_vm_trunc_sse);

1627. ra_left(as, RID_XMM0, ir->op1);

1628. }

1629. } else if (fpm == IRFPM_EXP2 && asm_fpjoin_pow(as, ir)) {

1630. /* Rejoined to pow(). */

1631. } else {

1632. asm_callid(as, ir, IRCALL_lj_vm_floor + fpm);

1633. }

1634. }

1636. ‌#define asm_atan2(as, ir)        asm_callid(as, ir, IRCALL_atan2)

1638. ‌static void asm_ldexp(ASMState *as, IRIns *ir)

1639. {

1640. int32_t ofs = sps_scale(ir->s);  /* Use spill slot or temp slots. */

1641. Reg dest = ir->r;

1642. if (ra_hasreg(dest)) {

1643. ra_free(as, dest);

1644. ra_modified(as, dest);

1645. emit_rmro(as, XO_MOVSD, dest, RID_ESP, ofs);

1646. }

1647. emit_rmro(as, XO_FSTPq, XOg_FSTPq, RID_ESP, ofs);

1648. emit_x87op(as, XI_FPOP1);

1649. emit_x87op(as, XI_FSCALE);

1650. asm_x87load(as, ir->op1);

1651. asm_x87load(as, ir->op2);

1652. }

1654. ‌static void asm_fppowi(ASMState *as, IRIns *ir)

1655. {

1656. /* The modified regs must match with the *.dasc implementation. */

1657. RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM1+1)|RID2RSET(RID_EAX);

1658. if (ra_hasreg(ir->r))

1659. rset_clear(drop, ir->r);  /* Dest reg handled below. */

1660. ra_evictset(as, drop);

1661. ra_destreg(as, ir, RID_XMM0);

1662. emit_call(as, lj_vm_powi_sse);

1663. ra_left(as, RID_XMM0, ir->op1);

1664. ra_left(as, RID_EAX, ir->op2);

1665. }

1667. ‌static void asm_pow(ASMState *as, IRIns *ir)

1668. {

1669. #if LJ_64 && LJ_HASFFI

1670. if (!irt_isnum(ir->t))

1671. asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 :

1672. IRCALL_lj_carith_powu64);

1673. else

1674. #endif

1675. asm_fppowi(as, ir);

1676. }

1678. ‌static int asm_swapops(ASMState *as, IRIns *ir)

1679. {

1680. IRIns *irl = IR(ir->op1);

1681. IRIns *irr = IR(ir->op2);

1682. lua_assert(ra_noreg(irr->r));

1683. if (!irm_iscomm(lj_ir_mode[ir->o]))

1684. return 0;  /* Can't swap non-commutative operations. */

1685. if (irref_isk(ir->op2))

1686. return 0;  /* Don't swap constants to the left. */

1687. if (ra_hasreg(irl->r))

1688. return 1;  /* Swap if left already has a register. */

1689. if (ra_samehint(ir->r, irr->r))

1690. return 1;  /* Swap if dest and right have matching hints. */

1691. if (as->curins > as->loopref) {  /* In variant part? */

1692. if (ir->op2 < as->loopref && !irt_isphi(irr->t))

1693. return 0;  /* Keep invariants on the right. */

1694. if (ir->op1 < as->loopref && !irt_isphi(irl->t))

1695. return 1;  /* Swap invariants to the right. */

1696. }

1697. if (opisfusableload(irl->o))

1698. return 1;  /* Swap fusable loads to the right. */

1699. return 0;  /* Otherwise don't swap. */

1700. }

1702. ‌static void asm_fparith(ASMState *as, IRIns *ir, x86Op xo)

1703. {

1704. IRRef lref = ir->op1;

1705. IRRef rref = ir->op2;

1706. RegSet allow = RSET_FPR;

1707. Reg dest;

1708. Reg right = IR(rref)->r;

1709. if (ra_hasreg(right)) {

1710. rset_clear(allow, right);

1711. ra_noweak(as, right);

1712. }

1713. dest = ra_dest(as, ir, allow);

1714. if (lref == rref) {

1715. right = dest;

1716. } else if (ra_noreg(right)) {

1717. if (asm_swapops(as, ir)) {

1718. IRRef tmp = lref; lref = rref; rref = tmp;

1719. }

1720. right = asm_fuseload(as, rref, rset_clear(allow, dest));

1721. }

1722. emit_mrm(as, xo, dest, right);

1723. ra_left(as, dest, lref);

1724. }

1726. ‌static void asm_intarith(ASMState *as, IRIns *ir, x86Arith xa)

1727. {

1728. IRRef lref = ir->op1;

1729. IRRef rref = ir->op2;

1730. RegSet allow = RSET_GPR;

1731. Reg dest, right;

1732. int32_t k = 0;

1733. if (as->flagmcp == as->mcp) {  /* Drop test r,r instruction. */

1734. MCode *p = as->mcp + ((LJ_64 && *as->mcp < XI_TESTb) ? 3 : 2);

1735. if ((p[1] & 15) < 14) {

1736. if ((p[1] & 15) >= 12) p[1] -= 4;  /* L <->S, NL <-> NS */

1737. as->flagmcp = NULL;

1738. as->mcp = p;

1739. }  /* else: cannot transform LE/NLE to cc without use of OF. */

1740. }

1741. right = IR(rref)->r;

1742. if (ra_hasreg(right)) {

1743. rset_clear(allow, right);

1744. ra_noweak(as, right);

1745. }

1746. dest = ra_dest(as, ir, allow);

1747. if (lref == rref) {

1748. right = dest;

1749. } else if (ra_noreg(right) && !asm_isk32(as, rref, &k)) {

1750. if (asm_swapops(as, ir)) {

1751. IRRef tmp = lref; lref = rref; rref = tmp;

1752. }

1753. right = asm_fuseloadm(as, rref, rset_clear(allow, dest), irt_is64(ir->t));

1754. }

1755. if (irt_isguard(ir->t))  /* For IR_ADDOV etc. */

1756. asm_guardcc(as, CC_O);

1757. if (xa != XOg_X_IMUL) {

1758. if (ra_hasreg(right))

1759. emit_mrm(as, XO_ARITH(xa), REX_64IR(ir, dest), right);

1760. else

1761. emit_gri(as, XG_ARITHi(xa), REX_64IR(ir, dest), k);

1762. } else if (ra_hasreg(right)) {  /* IMUL r, mrm. */

1763. emit_mrm(as, XO_IMUL, REX_64IR(ir, dest), right);

1764. } else {  /* IMUL r, r, k. */

1765. /* NYI: use lea/shl/add/sub (FOLD only does 2^k) depending on CPU. */

1766. Reg left = asm_fuseloadm(as, lref, RSET_GPR, irt_is64(ir->t));

1767. x86Op xo;

1768. if (checki8(k)) { emit_i8(as, k); xo = XO_IMULi8;

1769. } else { emit_i32(as, k); xo = XO_IMULi; }

1770. emit_mrm(as, xo, REX_64IR(ir, dest), left);

1771. return;

1772. }

1773. ra_left(as, dest, lref);

1774. }

1776. /* LEA is really a 4-operand ADD with an independent destination register,

1777. ** up to two source registers and an immediate. One register can be scaled

1778. ** by 1, 2, 4 or 8. This can be used to avoid moves or to fuse several

1779. ** instructions.

1780. **

1781. ** Currently only a few common cases are supported:

1782. ** - 3-operand ADD:    y = a+b; y = a+k   with a and b already allocated

1783. ** - Left ADD fusion:  y = (a+b)+k; y = (a+k)+b

1784. ** - Right ADD fusion: y = a+(b+k)

1785. ** The ommited variants have already been reduced by FOLD.

1786. **

1787. ** There are more fusion opportunities, like gathering shifts or joining

1788. ** common references. But these are probably not worth the trouble, since

1789. ** array indexing is not decomposed and already makes use of all fields

1790. ** of the ModRM operand.

1791. */

1792. ‌static int asm_lea(ASMState *as, IRIns *ir)

1793. {

1794. IRIns *irl = IR(ir->op1);

1795. IRIns *irr = IR(ir->op2);

1796. RegSet allow = RSET_GPR;

1797. Reg dest;

1798. as->mrm.base = as->mrm.idx = RID_NONE;

1799. as->mrm.scale = XM_SCALE1;

1800. as->mrm.ofs = 0;

1801. if (ra_hasreg(irl->r)) {

1802. rset_clear(allow, irl->r);

1803. ra_noweak(as, irl->r);

1804. as->mrm.base = irl->r;

1805. if (irref_isk(ir->op2) || ra_hasreg(irr->r)) {

1806. /* The PHI renaming logic does a better job in some cases. */

1807. if (ra_hasreg(ir->r) &&

1808. ((irt_isphi(irl->t) && as->phireg[ir->r] == ir->op1) ||

1809. (irt_isphi(irr->t) && as->phireg[ir->r] == ir->op2)))

1810. return 0;

1811. if (irref_isk(ir->op2)) {

1812. as->mrm.ofs = irr->i;

1813. } else {

1814. rset_clear(allow, irr->r);

1815. ra_noweak(as, irr->r);

1816. as->mrm.idx = irr->r;

1817. }

1818. } else if (irr->o == IR_ADD && mayfuse(as, ir->op2) &&

1819. irref_isk(irr->op2)) {

1820. Reg idx = ra_alloc1(as, irr->op1, allow);

1821. rset_clear(allow, idx);

1822. as->mrm.idx = (uint8_t)idx;

1823. as->mrm.ofs = IR(irr->op2)->i;

1824. } else {

1825. return 0;

1826. }

1827. } else if (ir->op1 != ir->op2 && irl->o == IR_ADD && mayfuse(as, ir->op1) &&

1828. (irref_isk(ir->op2) || irref_isk(irl->op2))) {

1829. Reg idx, base = ra_alloc1(as, irl->op1, allow);

1830. rset_clear(allow, base);

1831. as->mrm.base = (uint8_t)base;

1832. if (irref_isk(ir->op2)) {

1833. as->mrm.ofs = irr->i;

1834. idx = ra_alloc1(as, irl->op2, allow);

1835. } else {

1836. as->mrm.ofs = IR(irl->op2)->i;

1837. idx = ra_alloc1(as, ir->op2, allow);

1838. }

1839. rset_clear(allow, idx);

1840. as->mrm.idx = (uint8_t)idx;

1841. } else {

1842. return 0;

1843. }

1844. dest = ra_dest(as, ir, allow);

1845. emit_mrm(as, XO_LEA, dest, RID_MRM);

1846. return 1;  /* Success. */

1847. }

1849. ‌static void asm_add(ASMState *as, IRIns *ir)

1850. {

1851. if (irt_isnum(ir->t))

1852. asm_fparith(as, ir, XO_ADDSD);

1853. else if ((as->flags & JIT_F_LEA_AGU) || as->flagmcp == as->mcp ||

1854. irt_is64(ir->t) || !asm_lea(as, ir))

1855. asm_intarith(as, ir, XOg_ADD);

1856. }

1858. ‌static void asm_sub(ASMState *as, IRIns *ir)

1859. {

1860. if (irt_isnum(ir->t))

1861. asm_fparith(as, ir, XO_SUBSD);

1862. else  /* Note: no need for LEA trick here. i-k is encoded as i+(-k). */

1863. asm_intarith(as, ir, XOg_SUB);

1864. }

1866. ‌static void asm_mul(ASMState *as, IRIns *ir)

1867. {

1868. if (irt_isnum(ir->t))

1869. asm_fparith(as, ir, XO_MULSD);

1870. else

1871. asm_intarith(as, ir, XOg_X_IMUL);

1872. }

1874. ‌static void asm_div(ASMState *as, IRIns *ir)

1875. {

1876. #if LJ_64 && LJ_HASFFI

1877. if (!irt_isnum(ir->t))

1878. asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 :

1879. IRCALL_lj_carith_divu64);

1880. else

1881. #endif

1882. asm_fparith(as, ir, XO_DIVSD);

1883. }

1885. ‌static void asm_mod(ASMState *as, IRIns *ir)

1886. {

1887. #if LJ_64 && LJ_HASFFI

1888. if (!irt_isint(ir->t))

1889. asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 :

1890. IRCALL_lj_carith_modu64);

1891. else

1892. #endif

1893. asm_callid(as, ir, IRCALL_lj_vm_modi);

1894. }

1896. ‌static void asm_neg_not(ASMState *as, IRIns *ir, x86Group3 xg)

1897. {

1898. Reg dest = ra_dest(as, ir, RSET_GPR);

1899. emit_rr(as, XO_GROUP3, REX_64IR(ir, xg), dest);

1900. ra_left(as, dest, ir->op1);

1901. }

1903. ‌static void asm_neg(ASMState *as, IRIns *ir)

1904. {

1905. if (irt_isnum(ir->t))

1906. asm_fparith(as, ir, XO_XORPS);

1907. else

1908. asm_neg_not(as, ir, XOg_NEG);

1909. }

1911. ‌#define asm_abs(as, ir)                asm_fparith(as, ir, XO_ANDPS)

1913. ‌static void asm_intmin_max(ASMState *as, IRIns *ir, int cc)

1914. {

1915. Reg right, dest = ra_dest(as, ir, RSET_GPR);

1916. IRRef lref = ir->op1, rref = ir->op2;

1917. if (irref_isk(rref)) { lref = rref; rref = ir->op1; }

1918. right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, dest));

1919. emit_rr(as, XO_CMOV + (cc<<24), REX_64IR(ir, dest), right);

1920. emit_rr(as, XO_CMP, REX_64IR(ir, dest), right);

1921. ra_left(as, dest, lref);

1922. }

1924. ‌static void asm_min(ASMState *as, IRIns *ir)

1925. {

1926. if (irt_isnum(ir->t))

1927. asm_fparith(as, ir, XO_MINSD);

1928. else

1929. asm_intmin_max(as, ir, CC_G);

1930. }

1932. ‌static void asm_max(ASMState *as, IRIns *ir)

1933. {

1934. if (irt_isnum(ir->t))

1935. asm_fparith(as, ir, XO_MAXSD);

1936. else

1937. asm_intmin_max(as, ir, CC_L);

1938. }

1940. /* Note: don't use LEA for overflow-checking arithmetic! */

1941. ‌#define asm_addov(as, ir)        asm_intarith(as, ir, XOg_ADD)

1942. ‌#define asm_subov(as, ir)        asm_intarith(as, ir, XOg_SUB)

1943. ‌#define asm_mulov(as, ir)        asm_intarith(as, ir, XOg_X_IMUL)

1945. ‌#define asm_bnot(as, ir)        asm_neg_not(as, ir, XOg_NOT)

1947. ‌static void asm_bswap(ASMState *as, IRIns *ir)

1948. {

1949. Reg dest = ra_dest(as, ir, RSET_GPR);

1950. as->mcp = emit_op(XO_BSWAP + ((dest&7) << 24),

1951. REX_64IR(ir, 0), dest, 0, as->mcp, 1);

1952. ra_left(as, dest, ir->op1);

1953. }

1955. ‌#define asm_band(as, ir)        asm_intarith(as, ir, XOg_AND)

1956. ‌#define asm_bor(as, ir)                asm_intarith(as, ir, XOg_OR)

1957. ‌#define asm_bxor(as, ir)        asm_intarith(as, ir, XOg_XOR)

1959. ‌static void asm_bitshift(ASMState *as, IRIns *ir, x86Shift xs)

1960. {

1961. IRRef rref = ir->op2;

1962. IRIns *irr = IR(rref);

1963. Reg dest;

1964. if (irref_isk(rref)) {  /* Constant shifts. */

1965. int shift;

1966. dest = ra_dest(as, ir, RSET_GPR);

1967. shift = irr->i & (irt_is64(ir->t) ? 63 : 31);

1968. switch (shift) {

1969. case 0: break;

1970. case 1: emit_rr(as, XO_SHIFT1, REX_64IR(ir, xs), dest); break;

1971. default: emit_shifti(as, REX_64IR(ir, xs), dest, shift); break;

1972. }

1973. } else {  /* Variable shifts implicitly use register cl (i.e. ecx). */

1974. Reg right;

1975. dest = ra_dest(as, ir, rset_exclude(RSET_GPR, RID_ECX));

1976. if (dest == RID_ECX) {

1977. dest = ra_scratch(as, rset_exclude(RSET_GPR, RID_ECX));

1978. emit_rr(as, XO_MOV, RID_ECX, dest);

1979. }

1980. right = irr->r;

1981. if (ra_noreg(right))

1982. right = ra_allocref(as, rref, RID2RSET(RID_ECX));

1983. else if (right != RID_ECX)

1984. ra_scratch(as, RID2RSET(RID_ECX));

1985. emit_rr(as, XO_SHIFTcl, REX_64IR(ir, xs), dest);

1986. ra_noweak(as, right);

1987. if (right != RID_ECX)

1988. emit_rr(as, XO_MOV, RID_ECX, right);

1989. }

1990. ra_left(as, dest, ir->op1);

1991. /*

1992. ** Note: avoid using the flags resulting from a shift or rotate!

1993. ** All of them cause a partial flag stall, except for r,1 shifts

1994. ** (but not rotates). And a shift count of 0 leaves the flags unmodified.

1995. */

1996. }

1998. ‌#define asm_bshl(as, ir)        asm_bitshift(as, ir, XOg_SHL)

1999. ‌#define asm_bshr(as, ir)        asm_bitshift(as, ir, XOg_SHR)

2000. ‌#define asm_bsar(as, ir)        asm_bitshift(as, ir, XOg_SAR)

2001. ‌#define asm_brol(as, ir)        asm_bitshift(as, ir, XOg_ROL)

2002. ‌#define asm_bror(as, ir)        asm_bitshift(as, ir, XOg_ROR)

2004. /* -- Comparisons --------------------------------------------------------- */

2006. /* Virtual flags for unordered FP comparisons. */

2007. ‌#define VCC_U        0x1000                /* Unordered. */

2008. ‌#define VCC_P        0x2000                /* Needs extra CC_P branch. */

2009. ‌#define VCC_S        0x4000                /* Swap avoids CC_P branch. */

2010. ‌#define VCC_PS        (VCC_P|VCC_S)

2012. /* Map of comparisons to flags. ORDER IR. */

2013. ‌#define COMPFLAGS(ci, cin, cu, cf)        ((ci)+((cu)<<4)+((cin)<<8)+(cf))

2014. ‌static const uint16_t asm_compmap[IR_ABC+1] = {

2015. /*                 signed non-eq unsigned flags */

2016. /* LT  */ COMPFLAGS(CC_GE, CC_G,  CC_AE, VCC_PS),

2017. /* GE  */ COMPFLAGS(CC_L,  CC_L,  CC_B,  0),

2018. /* LE  */ COMPFLAGS(CC_G,  CC_G,  CC_A,  VCC_PS),

2019. /* GT  */ COMPFLAGS(CC_LE, CC_L,  CC_BE, 0),

2020. /* ULT */ COMPFLAGS(CC_AE, CC_A,  CC_AE, VCC_U),

2021. /* UGE */ COMPFLAGS(CC_B,  CC_B,  CC_B,  VCC_U|VCC_PS),

2022. /* ULE */ COMPFLAGS(CC_A,  CC_A,  CC_A,  VCC_U),

2023. /* UGT */ COMPFLAGS(CC_BE, CC_B,  CC_BE, VCC_U|VCC_PS),

2024. /* EQ  */ COMPFLAGS(CC_NE, CC_NE, CC_NE, VCC_P),

2025. /* NE  */ COMPFLAGS(CC_E,  CC_E,  CC_E,  VCC_U|VCC_P),

2026. /* ABC */ COMPFLAGS(CC_BE, CC_B,  CC_BE, VCC_U|VCC_PS)  /* Same as UGT. */

2027. };

2029. /* FP and integer comparisons. */

2030. ‌static void asm_comp(ASMState *as, IRIns *ir)

2031. {

2032. uint32_t cc = asm_compmap[ir->o];

2033. if (irt_isnum(ir->t)) {

2034. IRRef lref = ir->op1;

2035. IRRef rref = ir->op2;

2036. Reg left, right;

2037. MCLabel l_around;

2038. /*

2039. ** An extra CC_P branch is required to preserve ordered/unordered

2040. ** semantics for FP comparisons. This can be avoided by swapping

2041. ** the operands and inverting the condition (except for EQ and UNE).

2042. ** So always try to swap if possible.

2043. **

2044. ** Another option would be to swap operands to achieve better memory

2045. ** operand fusion. But it's unlikely that this outweighs the cost

2046. ** of the extra branches.

2047. */

2048. if (cc & VCC_S) {  /* Swap? */

2049. IRRef tmp = lref; lref = rref; rref = tmp;

2050. cc ^= (VCC_PS|(5<<4));  /* A <-> B, AE <-> BE, PS <-> none */

2051. }

2052. left = ra_alloc1(as, lref, RSET_FPR);

2053. right = asm_fuseload(as, rref, rset_exclude(RSET_FPR, left));

2054. l_around = emit_label(as);

2055. asm_guardcc(as, cc >> 4);

2056. if (cc & VCC_P) {  /* Extra CC_P branch required? */

2057. if (!(cc & VCC_U)) {

2058. asm_guardcc(as, CC_P);  /* Branch to exit for ordered comparisons. */

2059. } else if (l_around != as->invmcp) {

2060. emit_sjcc(as, CC_P, l_around);  /* Branch around for unordered. */

2061. } else {

2062. /* Patched to mcloop by asm_loop_fixup. */

2063. as->loopinv = 2;

2064. if (as->realign)

2065. emit_sjcc(as, CC_P, as->mcp);

2066. else

2067. emit_jcc(as, CC_P, as->mcp);

2068. }

2069. }

2070. emit_mrm(as, XO_UCOMISD, left, right);

2071. } else {

2072. IRRef lref = ir->op1, rref = ir->op2;

2073. IROp leftop = (IROp)(IR(lref)->o);

2074. Reg r64 = REX_64IR(ir, 0);

2075. int32_t imm = 0;

2076. lua_assert(irt_is64(ir->t) || irt_isint(ir->t) ||

2077. irt_isu32(ir->t) || irt_isaddr(ir->t) || irt_isu8(ir->t));

2078. /* Swap constants (only for ABC) and fusable loads to the right. */

2079. if (irref_isk(lref) || (!irref_isk(rref) && opisfusableload(leftop))) {

2080. if ((cc & 0xc) == 0xc) cc ^= 0x53;  /* L <-> G, LE <-> GE */

2081. else if ((cc & 0xa) == 0x2) cc ^= 0x55;  /* A <-> B, AE <-> BE */

2082. lref = ir->op2; rref = ir->op1;

2083. }

2084. if (asm_isk32(as, rref, &imm)) {

2085. IRIns *irl = IR(lref);

2086. /* Check wether we can use test ins. Not for unsigned, since CF=0. */

2087. int usetest = (imm == 0 && (cc & 0xa) != 0x2);

2088. if (usetest && irl->o == IR_BAND && irl+1 == ir && !ra_used(irl)) {

2089. /* Combine comp(BAND(ref, r/imm), 0) into test mrm, r/imm. */

2090. Reg right, left = RID_NONE;

2091. RegSet allow = RSET_GPR;

2092. if (!asm_isk32(as, irl->op2, &imm)) {

2093. left = ra_alloc1(as, irl->op2, allow);

2094. rset_clear(allow, left);

2095. } else {  /* Try to Fuse IRT_I8/IRT_U8 loads, too. See below. */

2096. IRIns *irll = IR(irl->op1);

2097. if (opisfusableload((IROp)irll->o) &&

2098. (irt_isi8(irll->t) || irt_isu8(irll->t))) {

2099. IRType1 origt = irll->t;  /* Temporarily flip types. */

2100. irll->t.irt = (irll->t.irt & ~IRT_TYPE) | IRT_INT;

2101. as->curins--;  /* Skip to BAND to avoid failing in noconflict(). */

2102. right = asm_fuseload(as, irl->op1, RSET_GPR);

2103. as->curins++;

2104. irll->t = origt;

2105. if (right != RID_MRM) goto test_nofuse;

2106. /* Fusion succeeded, emit test byte mrm, imm8. */

2107. asm_guardcc(as, cc);

2108. emit_i8(as, (imm & 0xff));

2109. emit_mrm(as, XO_GROUP3b, XOg_TEST, RID_MRM);

2110. return;

2111. }

2112. }

2113. as->curins--;  /* Skip to BAND to avoid failing in noconflict(). */

2114. right = asm_fuseloadm(as, irl->op1, allow, r64);

2115. as->curins++;  /* Undo the above. */

2116. test_nofuse:

2117. asm_guardcc(as, cc);

2118. if (ra_noreg(left)) {

2119. emit_i32(as, imm);

2120. emit_mrm(as, XO_GROUP3, r64 + XOg_TEST, right);

2121. } else {

2122. emit_mrm(as, XO_TEST, r64 + left, right);

2123. }

2124. } else {

2125. Reg left;

2126. if (opisfusableload((IROp)irl->o) &&

2127. ((irt_isu8(irl->t) && checku8(imm)) ||

2128. ((irt_isi8(irl->t) || irt_isi16(irl->t)) && checki8(imm)) ||

2129. (irt_isu16(irl->t) && checku16(imm) && checki8((int16_t)imm)))) {

2130. /* Only the IRT_INT case is fused by asm_fuseload.

2131. ** The IRT_I8/IRT_U8 loads and some IRT_I16/IRT_U16 loads

2132. ** are handled here.

2133. ** Note that cmp word [mem], imm16 should not be generated,

2134. ** since it has a length-changing prefix. Compares of a word

2135. ** against a sign-extended imm8 are ok, however.

2136. */

2137. IRType1 origt = irl->t;  /* Temporarily flip types. */

2138. irl->t.irt = (irl->t.irt & ~IRT_TYPE) | IRT_INT;

2139. left = asm_fuseload(as, lref, RSET_GPR);

2140. irl->t = origt;

2141. if (left == RID_MRM) {  /* Fusion succeeded? */

2142. if (irt_isu8(irl->t) || irt_isu16(irl->t))

2143. cc >>= 4;  /* Need unsigned compare. */

2144. asm_guardcc(as, cc);

2145. emit_i8(as, imm);

2146. emit_mrm(as, (irt_isi8(origt) || irt_isu8(origt)) ?

2147. XO_ARITHib : XO_ARITHiw8, r64 + XOg_CMP, RID_MRM);

2148. return;

2149. }  /* Otherwise handle register case as usual. */

2150. } else {

2151. left = asm_fuseloadm(as, lref,

2152. irt_isu8(ir->t) ? RSET_GPR8 : RSET_GPR, r64);

2153. }

2154. asm_guardcc(as, cc);

2155. if (usetest && left != RID_MRM) {

2156. /* Use test r,r instead of cmp r,0. */

2157. x86Op xo = XO_TEST;

2158. if (irt_isu8(ir->t)) {

2159. lua_assert(ir->o == IR_EQ || ir->o == IR_NE);

2160. xo = XO_TESTb;

2161. if (!rset_test(RSET_RANGE(RID_EAX, RID_EBX+1), left)) {

2162. if (LJ_64) {

2163. left |= FORCE_REX;

2164. } else {

2165. emit_i32(as, 0xff);

2166. emit_mrm(as, XO_GROUP3, XOg_TEST, left);

2167. return;

2168. }

2169. }

2170. }

2171. emit_rr(as, xo, r64 + left, left);

2172. if (irl+1 == ir)  /* Referencing previous ins? */

2173. as->flagmcp = as->mcp;  /* Set flag to drop test r,r if possible. */

2174. } else {

2175. emit_gmrmi(as, XG_ARITHi(XOg_CMP), r64 + left, imm);

2176. }

2177. }

2178. } else {

2179. Reg left = ra_alloc1(as, lref, RSET_GPR);

2180. Reg right = asm_fuseloadm(as, rref, rset_exclude(RSET_GPR, left), r64);

2181. asm_guardcc(as, cc);

2182. emit_mrm(as, XO_CMP, r64 + left, right);

2183. }

2184. }

2185. }

2187. ‌#define asm_equal(as, ir)        asm_comp(as, ir)

2189. #if LJ_32 && LJ_HASFFI

2190. /* 64 bit integer comparisons in 32 bit mode. */

2191. ‌static void asm_comp_int64(ASMState *as, IRIns *ir)

2192. {

2193. uint32_t cc = asm_compmap[(ir-1)->o];

2194. RegSet allow = RSET_GPR;

2195. Reg lefthi = RID_NONE, leftlo = RID_NONE;

2196. Reg righthi = RID_NONE, rightlo = RID_NONE;

2197. MCLabel l_around;

2198. x86ModRM mrm;

2200. as->curins--;  /* Skip loword ins. Avoids failing in noconflict(), too. */

2202. /* Allocate/fuse hiword operands. */

2203. if (irref_isk(ir->op2)) {

2204. lefthi = asm_fuseload(as, ir->op1, allow);

2205. } else {

2206. lefthi = ra_alloc1(as, ir->op1, allow);

2207. rset_clear(allow, lefthi);

2208. righthi = asm_fuseload(as, ir->op2, allow);

2209. if (righthi == RID_MRM) {

2210. if (as->mrm.base != RID_NONE) rset_clear(allow, as->mrm.base);

2211. if (as->mrm.idx != RID_NONE) rset_clear(allow, as->mrm.idx);

2212. } else {

2213. rset_clear(allow, righthi);

2214. }

2215. }

2216. mrm = as->mrm;  /* Save state for hiword instruction. */

2218. /* Allocate/fuse loword operands. */

2219. if (irref_isk((ir-1)->op2)) {

2220. leftlo = asm_fuseload(as, (ir-1)->op1, allow);

2221. } else {

2222. leftlo = ra_alloc1(as, (ir-1)->op1, allow);

2223. rset_clear(allow, leftlo);

2224. rightlo = asm_fuseload(as, (ir-1)->op2, allow);

2225. }

2227. /* All register allocations must be performed _before_ this point. */

2228. l_around = emit_label(as);

2229. as->invmcp = as->flagmcp = NULL;  /* Cannot use these optimizations. */

2231. /* Loword comparison and branch. */

2232. asm_guardcc(as, cc >> 4);  /* Always use unsigned compare for loword. */

2233. if (ra_noreg(rightlo)) {

2234. int32_t imm = IR((ir-1)->op2)->i;

2235. if (imm == 0 && ((cc >> 4) & 0xa) != 0x2 && leftlo != RID_MRM)

2236. emit_rr(as, XO_TEST, leftlo, leftlo);

2237. else

2238. emit_gmrmi(as, XG_ARITHi(XOg_CMP), leftlo, imm);

2239. } else {

2240. emit_mrm(as, XO_CMP, leftlo, rightlo);

2241. }

2243. /* Hiword comparison and branches. */

2244. if ((cc & 15) != CC_NE)

2245. emit_sjcc(as, CC_NE, l_around);  /* Hiword unequal: skip loword compare. */

2246. if ((cc & 15) != CC_E)

2247. asm_guardcc(as, cc >> 8);  /* Hiword compare without equality check. */

2248. as->mrm = mrm;  /* Restore state. */

2249. if (ra_noreg(righthi)) {

2250. int32_t imm = IR(ir->op2)->i;

2251. if (imm == 0 && (cc & 0xa) != 0x2 && lefthi != RID_MRM)

2252. emit_rr(as, XO_TEST, lefthi, lefthi);

2253. else

2254. emit_gmrmi(as, XG_ARITHi(XOg_CMP), lefthi, imm);

2255. } else {

2256. emit_mrm(as, XO_CMP, lefthi, righthi);

2257. }

2258. }

2259. #endif

2261. /* -- Support for 64 bit ops in 32 bit mode ------------------------------- */

2263. /* Hiword op of a split 64 bit op. Previous op must be the loword op. */

2264. ‌static void asm_hiop(ASMState *as, IRIns *ir)

2265. {

2266. #if LJ_32 && LJ_HASFFI

2267. /* HIOP is marked as a store because it needs its own DCE logic. */

2268. int uselo = ra_used(ir-1), usehi = ra_used(ir);  /* Loword/hiword used? */

2269. if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;

2270. if ((ir-1)->o == IR_CONV) {  /* Conversions to/from 64 bit. */

2271. as->curins--;  /* Always skip the CONV. */

2272. if (usehi || uselo)

2273. asm_conv64(as, ir);

2274. return;

2275. } else if ((ir-1)->o <= IR_NE) {  /* 64 bit integer comparisons. ORDER IR. */

2276. asm_comp_int64(as, ir);

2277. return;

2278. } else if ((ir-1)->o == IR_XSTORE) {

2279. if ((ir-1)->r != RID_SINK)

2280. asm_fxstore(as, ir);

2281. return;

2282. }

2283. if (!usehi) return;  /* Skip unused hiword op for all remaining ops. */

2284. switch ((ir-1)->o) {

2285. case IR_ADD:

2286. as->flagmcp = NULL;

2287. as->curins--;

2288. asm_intarith(as, ir, XOg_ADC);

2289. asm_intarith(as, ir-1, XOg_ADD);

2290. break;

2291. case IR_SUB:

2292. as->flagmcp = NULL;

2293. as->curins--;

2294. asm_intarith(as, ir, XOg_SBB);

2295. asm_intarith(as, ir-1, XOg_SUB);

2296. break;

2297. case IR_NEG: {

2298. Reg dest = ra_dest(as, ir, RSET_GPR);

2299. emit_rr(as, XO_GROUP3, XOg_NEG, dest);

2300. emit_i8(as, 0);

2301. emit_rr(as, XO_ARITHi8, XOg_ADC, dest);

2302. ra_left(as, dest, ir->op1);

2303. as->curins--;

2304. asm_neg_not(as, ir-1, XOg_NEG);

2305. break;

2306. }

2307. case IR_CALLN:

2308. case IR_CALLXS:

2309. if (!uselo)

2310. ra_allocref(as, ir->op1, RID2RSET(RID_RETLO));  /* Mark lo op as used. */

2311. break;

2312. case IR_CNEWI:

2313. /* Nothing to do here. Handled by CNEWI itself. */

2314. break;

2315. default: lua_assert(0); break;

2316. }

2317. #else

2318. UNUSED(as); UNUSED(ir); lua_assert(0);  /* Unused on x64 or without FFI. */

2319. #endif

2320. }

2322. /* -- Profiling ----------------------------------------------------------- */

2324. ‌static void asm_prof(ASMState *as, IRIns *ir)

2325. {

2326. UNUSED(ir);

2327. asm_guardcc(as, CC_NE);

2328. emit_i8(as, HOOK_PROFILE);

2329. emit_rma(as, XO_GROUP3b, XOg_TEST, &J2G(as->J)->hookmask);

2330. }

2332. /* -- Stack handling ------------------------------------------------------ */

2334. /* Check Lua stack size for overflow. Use exit handler as fallback. */

2335. ‌static void asm_stack_check(ASMState *as, BCReg topslot,

2336. IRIns *irp, RegSet allow, ExitNo exitno)

2337. {

2338. /* Try to get an unused temp. register, otherwise spill/restore eax. */

2339. Reg pbase = irp ? irp->r : RID_BASE;

2340. Reg r = allow ? rset_pickbot(allow) : RID_EAX;

2341. emit_jcc(as, CC_B, exitstub_addr(as->J, exitno));

2342. if (allow == RSET_EMPTY)  /* Restore temp. register. */

2343. emit_rmro(as, XO_MOV, r|REX_64, RID_ESP, 0);

2344. else

2345. ra_modified(as, r);

2346. emit_gri(as, XG_ARITHi(XOg_CMP), r, (int32_t)(8*topslot));

2347. if (ra_hasreg(pbase) && pbase != r)

2348. emit_rr(as, XO_ARITH(XOg_SUB), r, pbase);

2349. else

2350. emit_rmro(as, XO_ARITH(XOg_SUB), r, RID_NONE,

2351. ptr2addr(&J2G(as->J)->jit_base));

2352. emit_rmro(as, XO_MOV, r, r, offsetof(lua_State, maxstack));

2353. emit_getgl(as, r, cur_L);

2354. if (allow == RSET_EMPTY)  /* Spill temp. register. */

2355. emit_rmro(as, XO_MOVto, r|REX_64, RID_ESP, 0);

2356. }

2358. /* Restore Lua stack from on-trace state. */

2359. ‌static void asm_stack_restore(ASMState *as, SnapShot *snap)

2360. {

2361. SnapEntry *map = &as->T->snapmap[snap->mapofs];

2362. SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1];

2363. MSize n, nent = snap->nent;

2364. /* Store the value of all modified slots to the Lua stack. */

2365. for (n = 0; n < nent; n++) {

2366. SnapEntry sn = map[n];

2367. BCReg s = snap_slot(sn);

2368. int32_t ofs = 8*((int32_t)s-1);

2369. IRRef ref = snap_ref(sn);

2370. IRIns *ir = IR(ref);

2371. if ((sn & SNAP_NORESTORE))

2372. continue;

2373. if (irt_isnum(ir->t)) {

2374. Reg src = ra_alloc1(as, ref, RSET_FPR);

2375. emit_rmro(as, XO_MOVSDto, src, RID_BASE, ofs);

2376. } else {

2377. lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) ||

2378. (LJ_DUALNUM && irt_isinteger(ir->t)));

2379. if (!irref_isk(ref)) {

2380. Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE));

2381. emit_movtomro(as, REX_64IR(ir, src), RID_BASE, ofs);

2382. } else if (!irt_ispri(ir->t)) {

2383. emit_movmroi(as, RID_BASE, ofs, ir->i);

2384. }

2385. if ((sn & (SNAP_CONT|SNAP_FRAME))) {

2386. if (s != 0)  /* Do not overwrite link to previous frame. */

2387. emit_movmroi(as, RID_BASE, ofs+4, (int32_t)(*flinks--));

2388. } else {

2389. if (!(LJ_64 && irt_islightud(ir->t)))

2390. emit_movmroi(as, RID_BASE, ofs+4, irt_toitype(ir->t));

2391. }

2392. }

2393. checkmclim(as);

2394. }

2395. lua_assert(map + nent == flinks);

2396. }

2398. /* -- GC handling --------------------------------------------------------- */

2400. /* Check GC threshold and do one or more GC steps. */

2401. ‌static void asm_gc_check(ASMState *as)

2402. {

2403. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];

2404. IRRef args[2];

2405. MCLabel l_end;

2406. Reg tmp;

2407. ra_evictset(as, RSET_SCRATCH);

2408. l_end = emit_label(as);

2409. /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */

2410. asm_guardcc(as, CC_NE);  /* Assumes asm_snap_prep() already done. */

2411. emit_rr(as, XO_TEST, RID_RET, RID_RET);

2412. args[0] = ASMREF_TMP1;  /* global_State *g */

2413. args[1] = ASMREF_TMP2;  /* MSize steps     */

2414. asm_gencall(as, ci, args);

2415. tmp = ra_releasetmp(as, ASMREF_TMP1);

2416. emit_loada(as, tmp, J2G(as->J));

2417. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP2), as->gcsteps);

2418. /* Jump around GC step if GC total < GC threshold. */

2419. emit_sjcc(as, CC_B, l_end);

2420. emit_opgl(as, XO_ARITH(XOg_CMP), tmp, gc.threshold);

2421. emit_getgl(as, tmp, gc.total);

2422. as->gcsteps = 0;

2423. checkmclim(as);

2424. }

2426. /* -- Loop handling ------------------------------------------------------- */

2428. /* Fixup the loop branch. */

2429. ‌static void asm_loop_fixup(ASMState *as)

2430. {

2431. MCode *p = as->mctop;

2432. MCode *target = as->mcp;

2433. if (as->realign) {  /* Realigned loops use short jumps. */

2434. as->realign = NULL;  /* Stop another retry. */

2435. lua_assert(((intptr_t)target & 15) == 0);

2436. if (as->loopinv) {  /* Inverted loop branch? */

2437. p -= 5;

2438. p[0] = XI_JMP;

2439. lua_assert(target - p >= -128);

2440. p[-1] = (MCode)(target - p);  /* Patch sjcc. */

2441. if (as->loopinv == 2)

2442. p[-3] = (MCode)(target - p + 2);  /* Patch opt. short jp. */

2443. } else {

2444. lua_assert(target - p >= -128);

2445. p[-1] = (MCode)(int8_t)(target - p);  /* Patch short jmp. */

2446. p[-2] = XI_JMPs;

2447. }

2448. } else {

2449. MCode *newloop;

2450. p[-5] = XI_JMP;

2451. if (as->loopinv) {  /* Inverted loop branch? */

2452. /* asm_guardcc already inverted the jcc and patched the jmp. */

2453. p -= 5;

2454. newloop = target+4;

2455. *(int32_t *)(p-4) = (int32_t)(target - p);  /* Patch jcc. */

2456. if (as->loopinv == 2) {

2457. *(int32_t *)(p-10) = (int32_t)(target - p + 6);  /* Patch opt. jp. */

2458. newloop = target+8;

2459. }

2460. } else {  /* Otherwise just patch jmp. */

2461. *(int32_t *)(p-4) = (int32_t)(target - p);

2462. newloop = target+3;

2463. }

2464. /* Realign small loops and shorten the loop branch. */

2465. if (newloop >= p - 128) {

2466. as->realign = newloop;  /* Force a retry and remember alignment. */

2467. as->curins = as->stopins;  /* Abort asm_trace now. */

2468. as->T->nins = as->orignins;  /* Remove any added renames. */

2469. }

2470. }

2471. }

2473. /* -- Head of trace ------------------------------------------------------- */

2475. /* Coalesce BASE register for a root trace. */

2476. ‌static void asm_head_root_base(ASMState *as)

2477. {

2478. IRIns *ir = IR(REF_BASE);

2479. Reg r = ir->r;

2480. if (ra_hasreg(r)) {

2481. ra_free(as, r);

2482. if (rset_test(as->modset, r) || irt_ismarked(ir->t))

2483. ir->r = RID_INIT;  /* No inheritance for modified BASE register. */

2484. if (r != RID_BASE)

2485. emit_rr(as, XO_MOV, r, RID_BASE);

2486. }

2487. }

2489. /* Coalesce or reload BASE register for a side trace. */

2490. ‌static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow)

2491. {

2492. IRIns *ir = IR(REF_BASE);

2493. Reg r = ir->r;

2494. if (ra_hasreg(r)) {

2495. ra_free(as, r);

2496. if (rset_test(as->modset, r) || irt_ismarked(ir->t))

2497. ir->r = RID_INIT;  /* No inheritance for modified BASE register. */

2498. if (irp->r == r) {

2499. rset_clear(allow, r);  /* Mark same BASE register as coalesced. */

2500. } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {

2501. rset_clear(allow, irp->r);

2502. emit_rr(as, XO_MOV, r, irp->r);  /* Move from coalesced parent reg. */

2503. } else {

2504. emit_getgl(as, r, jit_base);  /* Otherwise reload BASE. */

2505. }

2506. }

2507. return allow;

2508. }

2510. /* -- Tail of trace ------------------------------------------------------- */

2512. /* Fixup the tail code. */

2513. ‌static void asm_tail_fixup(ASMState *as, TraceNo lnk)

2514. {

2515. /* Note: don't use as->mcp swap + emit_*: emit_op overwrites more bytes. */

2516. MCode *p = as->mctop;

2517. MCode *target, *q;

2518. int32_t spadj = as->T->spadjust;

2519. if (spadj == 0) {

2520. p -= ((as->flags & JIT_F_LEA_AGU) ? 7 : 6) + (LJ_64 ? 1 : 0);

2521. } else {

2522. MCode *p1;

2523. /* Patch stack adjustment. */

2524. if (checki8(spadj)) {

2525. p -= 3;

2526. p1 = p-6;

2527. *p1 = (MCode)spadj;

2528. } else {

2529. p1 = p-9;

2530. *(int32_t *)p1 = spadj;

2531. }

2532. if ((as->flags & JIT_F_LEA_AGU)) {

2533. #if LJ_64

2534. p1[-4] = 0x48;

2535. #endif

2536. p1[-3] = (MCode)XI_LEA;

2537. p1[-2] = MODRM(checki8(spadj) ? XM_OFS8 : XM_OFS32, RID_ESP, RID_ESP);

2538. p1[-1] = MODRM(XM_SCALE1, RID_ESP, RID_ESP);

2539. } else {

2540. #if LJ_64

2541. p1[-3] = 0x48;

2542. #endif

2543. p1[-2] = (MCode)(checki8(spadj) ? XI_ARITHi8 : XI_ARITHi);

2544. p1[-1] = MODRM(XM_REG, XOg_ADD, RID_ESP);

2545. }

2546. }

2547. /* Patch exit branch. */

2548. target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp;

2549. *(int32_t *)(p-4) = jmprel(p, target);

2550. p[-5] = XI_JMP;

2551. /* Drop unused mcode tail. Fill with NOPs to make the prefetcher happy. */

2552. for (q = as->mctop-1; q >= p; q--)

2553. *q = XI_NOP;

2554. as->mctop = p;

2555. }

2557. /* Prepare tail of code. */

2558. ‌static void asm_tail_prep(ASMState *as)

2559. {

2560. MCode *p = as->mctop;

2561. /* Realign and leave room for backwards loop branch or exit branch. */

2562. if (as->realign) {

2563. int i = ((int)(intptr_t)as->realign) & 15;

2564. /* Fill unused mcode tail with NOPs to make the prefetcher happy. */

2565. while (i-- > 0)

2566. *--p = XI_NOP;

2567. as->mctop = p;

2568. p -= (as->loopinv ? 5 : 2);  /* Space for short/near jmp. */

2569. } else {

2570. p -= 5;  /* Space for exit branch (near jmp). */

2571. }

2572. if (as->loopref) {

2573. as->invmcp = as->mcp = p;

2574. } else {

2575. /* Leave room for ESP adjustment: add esp, imm or lea esp, [esp+imm] */

2576. as->mcp = p - (((as->flags & JIT_F_LEA_AGU) ? 7 : 6)  + (LJ_64 ? 1 : 0));

2577. as->invmcp = NULL;

2578. }

2579. }

2581. /* -- Trace setup --------------------------------------------------------- */

2583. /* Ensure there are enough stack slots for call arguments. */

2584. ‌static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)

2585. {

2586. IRRef args[CCI_NARGS_MAX*2];

2587. int nslots;

2588. asm_collectargs(as, ir, ci, args);

2589. nslots = asm_count_call_slots(as, ci, args);

2590. if (nslots > as->evenspill)  /* Leave room for args in stack slots. */

2591. as->evenspill = nslots;

2592. #if LJ_64

2593. return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET);

2594. #else

2595. return irt_isfp(ir->t) ? REGSP_INIT : REGSP_HINT(RID_RET);

2596. #endif

2597. }

2599. /* Target-specific setup. */

2600. ‌static void asm_setup_target(ASMState *as)

2601. {

2602. asm_exitstub_setup(as, as->T->nsnap);

2603. }

2605. /* -- Trace patching ------------------------------------------------------ */

2607. /* Patch exit jumps of existing machine code to a new target. */

2608. ‌void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)

2609. {

2610. MCode *p = T->mcode;

2611. MCode *mcarea = lj_mcode_patch(J, p, 0);

2612. MSize len = T->szmcode;

2613. MCode *px = exitstub_addr(J, exitno) - 6;

2614. MCode *pe = p+len-6;

2615. uint32_t stateaddr = u32ptr(&J2G(J)->vmstate);

2616. if (len > 5 && p[len-5] == XI_JMP && p+len-6 + *(int32_t *)(p+len-4) == px)

2617. *(int32_t *)(p+len-4) = jmprel(p+len, target);

2618. /* Do not patch parent exit for a stack check. Skip beyond vmstate update. */

2619. for (; p < pe; p++)

2620. if (*(uint32_t *)(p+(LJ_64 ? 3 : 2)) == stateaddr && p[0] == XI_MOVmi) {

2621. p += LJ_64 ? 11 : 10;

2622. break;

2623. }

2624. lua_assert(p < pe);

2625. for (; p < pe; p++) {

2626. if ((*(uint16_t *)p & 0xf0ff) == 0x800f && p + *(int32_t *)(p+2) == px) {

2627. *(int32_t *)(p+2) = jmprel(p+6, target);

2628. p += 5;

2629. }

2630. }

2631. lj_mcode_sync(T->mcode, T->mcode + T->szmcode);

2632. lj_mcode_patch(J, mcarea, 1);

2633. }

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_bc.c - luajit-2.0-src

Macros defined

- LUA_CORE

- lj_bc_c

Source code

1. /*

2. ** Bytecode instruction modes.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_bc_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

10. #include "lj_bc.h"

12. /* Bytecode offsets and bytecode instruction modes. */

13. #include "lj_bcdef.h"

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_bcdef.h - luajit-2.0-src

Global variables defined

- lj_bc_mode

- lj_bc_ofs

Source code

1. /* This is a generated file. DO NOT EDIT! */

3. ‌LJ_DATADEF const uint16_t lj_bc_ofs[] = {

4. 0,

5. 133,

6. 266,

7. 399,

8. 532,

9. 727,

10. 925,

11. 992,

12. 1055,

13. 1191,

14. 1327,

15. 1380,

16. 1432,

17. 1483,

18. 1534,

19. 1575,

20. 1616,

21. 1644,

22. 1676,

23. 1702,

24. 1734,

25. 1833,

26. 1904,

27. 1981,

28. 2058,

29. 2136,

30. 2205,

31. 2279,

32. 2356,

33. 2433,

34. 2511,

35. 2580,

36. 2631,

37. 2706,

38. 2781,

39. 2857,

40. 2924,

41. 2973,

42. 3053,

43. 3129,

44. 3165,

45. 3201,

46. 3233,

47. 3262,

48. 3287,

49. 3330,

50. 3366,

51. 3453,

52. 3535,

53. 3573,

54. 3607,

55. 3658,

56. 3722,

57. 3831,

58. 3924,

59. 3942,

60. 3960,

61. 4088,

62. 4212,

63. 4311,

64. 4363,

65. 4516,

66. 4745,

67. 4869,

68. 5011,

69. 5096,

70. 5142,

71. 5184,

72. 5188,

73. 5336,

74. 5404,

75. 5571,

76. 5762,

77. 5850,

78. 5854,

79. 5990,

80. 6082,

81. 6187,

82. 6354,

83. 6559,

84. 6579,

85. 6763,

86. 6952,

87. 6972,

88. 7016,

89. 7055,

90. 7075,

91. 7093,

92. 7140,

93. 7165,

94. 7185,

95. 7248,

96. 7302,

97. 7302,

98. 7427,

99. 7428,

100. 7517,

101. 9333,

102. 9400,

103. 9920,

104. 10023,

105. 10080,

106. 10194,

107. 9466,

108. 9628,

109. 9720,

110. 9772,

111. 9812,

112. 10258,

113. 10299,

114. 10921,

115. 10354,

116. 10664,

117. 10973,

118. 11147,

119. 11219,

120. 11291,

121. 11362,

122. 11402,

123. 11442,

124. 11482,

125. 11522,

126. 11562,

127. 11602,

128. 11642,

129. 11682,

130. 11722,

131. 11762,

132. 12025,

133. 12173,

134. 11322,

135. 11860,

136. 11802,

137. 11918,

138. 11976,

139. 12272,

140. 12389,

141. 13089,

142. 13550,

143. 13488,

144. 13621,

145. 13699,

146. 13777,

147. 13855,

148. 13929,

149. 13152,

150. 13264,

151. 13376,

152. 12506,

153. 12549,

154. 12669,

155. 12834,

156. 12919,

157. 13004

158. };

160. ‌LJ_DATADEF const uint16_t lj_bc_mode[] = {

161. BCDEF(BCMODE)

162. BCMODE_FF,

163. BCMODE_FF,

164. BCMODE_FF,

165. BCMODE_FF,

166. BCMODE_FF,

167. BCMODE_FF,

168. BCMODE_FF,

169. BCMODE_FF,

170. BCMODE_FF,

171. BCMODE_FF,

172. BCMODE_FF,

173. BCMODE_FF,

174. BCMODE_FF,

175. BCMODE_FF,

176. BCMODE_FF,

177. BCMODE_FF,

178. BCMODE_FF,

179. BCMODE_FF,

180. BCMODE_FF,

181. BCMODE_FF,

182. BCMODE_FF,

183. BCMODE_FF,

184. BCMODE_FF,

185. BCMODE_FF,

186. BCMODE_FF,

187. BCMODE_FF,

188. BCMODE_FF,

189. BCMODE_FF,

190. BCMODE_FF,

191. BCMODE_FF,

192. BCMODE_FF,

193. BCMODE_FF,

194. BCMODE_FF,

195. BCMODE_FF,

196. BCMODE_FF,

197. BCMODE_FF,

198. BCMODE_FF,

199. BCMODE_FF,

200. BCMODE_FF,

201. BCMODE_FF,

202. BCMODE_FF,

203. BCMODE_FF,

204. BCMODE_FF,

205. BCMODE_FF,

206. BCMODE_FF,

207. BCMODE_FF,

208. BCMODE_FF,

209. BCMODE_FF,

210. BCMODE_FF,

211. BCMODE_FF,

212. BCMODE_FF,

213. BCMODE_FF,

214. BCMODE_FF,

215. BCMODE_FF,

216. BCMODE_FF,

217. BCMODE_FF,

218. BCMODE_FF

219. };

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_carith.h - luajit-2.0-src

Macros defined

- _LJ_CARITH_H

Source code

1. /*

2. ** C data arithmetic.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_CARITH_H

7. ‌#define _LJ_CARITH_H

9. #include "lj_obj.h"

11. #if LJ_HASFFI

13. LJ_FUNC int lj_carith_op(lua_State *L, MMS mm);

15. #if LJ_32

16. LJ_FUNC uint64_t lj_carith_shl64(uint64_t x, int32_t sh);

17. LJ_FUNC uint64_t lj_carith_shr64(uint64_t x, int32_t sh);

18. LJ_FUNC uint64_t lj_carith_sar64(uint64_t x, int32_t sh);

19. LJ_FUNC uint64_t lj_carith_rol64(uint64_t x, int32_t sh);

20. LJ_FUNC uint64_t lj_carith_ror64(uint64_t x, int32_t sh);

21. #endif

22. LJ_FUNC uint64_t lj_carith_shift64(uint64_t x, int32_t sh, int op);

23. LJ_FUNC uint64_t lj_carith_check64(lua_State *L, int narg, CTypeID *id);

25. #if LJ_32 && LJ_HASJIT

26. LJ_FUNC int64_t lj_carith_mul64(int64_t x, int64_t k);

27. #endif

28. LJ_FUNC uint64_t lj_carith_divu64(uint64_t a, uint64_t b);

29. LJ_FUNC int64_t lj_carith_divi64(int64_t a, int64_t b);

30. LJ_FUNC uint64_t lj_carith_modu64(uint64_t a, uint64_t b);

31. LJ_FUNC int64_t lj_carith_modi64(int64_t a, int64_t b);

32. LJ_FUNC uint64_t lj_carith_powu64(uint64_t x, uint64_t k);

33. LJ_FUNC int64_t lj_carith_powi64(int64_t x, int64_t k);

35. #endif

37. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_ccallback.h - luajit-2.0-src

Macros defined

- _LJ_CCALLBACK_H

Source code

1. /*

2. ** FFI C callback handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_CCALLBACK_H

7. ‌#define _LJ_CCALLBACK_H

9. #include "lj_obj.h"

10. #include "lj_ctype.h"

12. #if LJ_HASFFI

14. /* Really belongs to lj_vm.h. */

15. LJ_ASMF void lj_vm_ffi_callback(void);

17. LJ_FUNC MSize lj_ccallback_ptr2slot(CTState *cts, void *p);

18. LJ_FUNCA lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf);

19. LJ_FUNCA void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o);

20. LJ_FUNC void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn);

21. LJ_FUNC void lj_ccallback_mcode_free(CTState *cts);

23. #endif

25. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_char.c - luajit-2.0-src

Global variables defined

- lj_char_bits

Macros defined

- LUA_CORE

- lj_char_c

Source code

1. /*

2. ** Character types.

3. ** Donated to the public domain.

4. **

5. ** This is intended to replace the problematic libc single-byte NLS functions.

6. ** These just don't make sense anymore with UTF-8 locales becoming the norm

7. ** on POSIX systems. It never worked too well on Windows systems since hardly

8. ** anyone bothered to call setlocale().

9. **

10. ** This table is hardcoded for ASCII. Identifiers include the characters

11. ** 128-255, too. This allows for the use of all non-ASCII chars as identifiers

12. ** in the lexer. This is a broad definition, but works well in practice

13. ** for both UTF-8 locales and most single-byte locales (such as ISO-8859-*).

14. **

15. ** If you really need proper character types for UTF-8 strings, please use

16. ** an add-on library such as slnunicode: http://luaforge.net/projects/sln/

17. */

19. ‌#define lj_char_c

20. ‌#define LUA_CORE

22. #include "lj_char.h"

24. ‌LJ_DATADEF const uint8_t lj_char_bits[257] = {

25. 0,

26. 1,  1,  1,  1,  1,  1,  1,  1,  1,  3,  3,  3,  3,  3,  1,  1,

27. 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,

28. 2,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,

29. 152,152,152,152,152,152,152,152,152,152,  4,  4,  4,  4,  4,  4,

30. 4,176,176,176,176,176,176,160,160,160,160,160,160,160,160,160,

31. 160,160,160,160,160,160,160,160,160,160,160,  4,  4,  4,  4,132,

32. 4,208,208,208,208,208,208,192,192,192,192,192,192,192,192,192,

33. 192,192,192,192,192,192,192,192,192,192,192,  4,  4,  4,  4,  1,

34. 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,

35. 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,

36. 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,

37. 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,

38. 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,

39. 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,

40. 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,

41. 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128

42. };

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_crecord.h - luajit-2.0-src

Macros defined

- _LJ_CRECORD_H

Source code

1. /*

2. ** Trace recorder for C data operations.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_CRECORD_H

7. ‌#define _LJ_CRECORD_H

9. #include "lj_obj.h"

10. #include "lj_jit.h"

11. #include "lj_ffrecord.h"

13. #if LJ_HASJIT && LJ_HASFFI

14. LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd);

15. LJ_FUNC void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd);

16. LJ_FUNC void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd);

17. LJ_FUNC void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd);

18. LJ_FUNC void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd);

19. LJ_FUNC void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd);

20. LJ_FUNC void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd);

21. LJ_FUNC void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd);

22. LJ_FUNC void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd);

23. LJ_FUNC void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd);

24. LJ_FUNC void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd);

25. LJ_FUNC void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd);

26. LJ_FUNC void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd);

27. LJ_FUNC void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd);

29. LJ_FUNC void LJ_FASTCALL recff_bit64_tobit(jit_State *J, RecordFFData *rd);

30. LJ_FUNC int LJ_FASTCALL recff_bit64_unary(jit_State *J, RecordFFData *rd);

31. LJ_FUNC int LJ_FASTCALL recff_bit64_nary(jit_State *J, RecordFFData *rd);

32. LJ_FUNC int LJ_FASTCALL recff_bit64_shift(jit_State *J, RecordFFData *rd);

33. LJ_FUNC TRef recff_bit64_tohex(jit_State *J, RecordFFData *rd, TRef hdr);

35. LJ_FUNC void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd);

36. #endif

38. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_errmsg.h - luajit-2.0-src

Macros defined

- ERRDEF

Source code

1. /*

2. ** VM error messages.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* This file may be included multiple times with different ERRDEF macros. */

8. /* Basic error handling. */

9. ERRDEF(ERRMEM,        "not enough memory")

10. ERRDEF(ERRERR,        "error in error handling")

11. ERRDEF(ERRCPP,        "C++ exception")

13. /* Allocations. */

14. ERRDEF(STROV,        "string length overflow")

15. ERRDEF(UDATAOV,        "userdata length overflow")

16. ERRDEF(STKOV,        "stack overflow")

17. ERRDEF(STKOVM,        "stack overflow (%s)")

18. ERRDEF(TABOV,        "table overflow")

20. /* Table indexing. */

21. ERRDEF(NANIDX,        "table index is NaN")

22. ERRDEF(NILIDX,        "table index is nil")

23. ERRDEF(NEXTIDX,        "invalid key to " LUA_QL("next"))

25. /* Metamethod resolving. */

26. ERRDEF(BADCALL,        "attempt to call a %s value")

27. ERRDEF(BADOPRT,        "attempt to %s %s " LUA_QS " (a %s value)")

28. ERRDEF(BADOPRV,        "attempt to %s a %s value")

29. ERRDEF(BADCMPT,        "attempt to compare %s with %s")

30. ERRDEF(BADCMPV,        "attempt to compare two %s values")

31. ERRDEF(GETLOOP,        "loop in gettable")

32. ERRDEF(SETLOOP,        "loop in settable")

33. ERRDEF(OPCALL,        "call")

34. ERRDEF(OPINDEX,        "index")

35. ERRDEF(OPARITH,        "perform arithmetic on")

36. ERRDEF(OPCAT,        "concatenate")

37. ERRDEF(OPLEN,        "get length of")

39. /* Type checks. */

40. ERRDEF(BADSELF,        "calling " LUA_QS " on bad self (%s)")

41. ERRDEF(BADARG,        "bad argument #%d to " LUA_QS " (%s)")

42. ERRDEF(BADTYPE,        "%s expected, got %s")

43. ERRDEF(BADVAL,        "invalid value")

44. ERRDEF(NOVAL,        "value expected")

45. ERRDEF(NOCORO,        "coroutine expected")

46. ERRDEF(NOTABN,        "nil or table expected")

47. ERRDEF(NOLFUNC,        "Lua function expected")

48. ERRDEF(NOFUNCL,        "function or level expected")

49. ERRDEF(NOSFT,        "string/function/table expected")

50. ERRDEF(NOPROXY,        "boolean or proxy expected")

51. ERRDEF(FORINIT,        LUA_QL("for") " initial value must be a number")

52. ERRDEF(FORLIM,        LUA_QL("for") " limit must be a number")

53. ERRDEF(FORSTEP,        LUA_QL("for") " step must be a number")

55. /* C API checks. */

56. ERRDEF(NOENV,        "no calling environment")

57. ERRDEF(CYIELD,        "attempt to yield across C-call boundary")

58. ERRDEF(BADLU,        "bad light userdata pointer")

59. ERRDEF(NOGCMM,        "bad action while in __gc metamethod")

60. #if LJ_TARGET_WINDOWS

61. ERRDEF(BADFPU,        "bad FPU precision (use D3DCREATE_FPU_PRESERVE with DirectX)")

62. #endif

64. /* Standard library function errors. */

65. ERRDEF(ASSERT,        "assertion failed!")

66. ERRDEF(PROTMT,        "cannot change a protected metatable")

67. ERRDEF(UNPACK,        "too many results to unpack")

68. ERRDEF(RDRSTR,        "reader function must return a string")

69. ERRDEF(PRTOSTR,        LUA_QL("tostring") " must return a string to " LUA_QL("print"))

70. ERRDEF(IDXRNG,        "index out of range")

71. ERRDEF(BASERNG,        "base out of range")

72. ERRDEF(LVLRNG,        "level out of range")

73. ERRDEF(INVLVL,        "invalid level")

74. ERRDEF(INVOPT,        "invalid option")

75. ERRDEF(INVOPTM,        "invalid option " LUA_QS)

76. ERRDEF(INVFMT,        "invalid format")

77. ERRDEF(SETFENV,        LUA_QL("setfenv") " cannot change environment of given object")

78. ERRDEF(CORUN,        "cannot resume running coroutine")

79. ERRDEF(CODEAD,        "cannot resume dead coroutine")

80. ERRDEF(COSUSP,        "cannot resume non-suspended coroutine")

81. ERRDEF(TABINS,        "wrong number of arguments to " LUA_QL("insert"))

82. ERRDEF(TABCAT,        "invalid value (%s) at index %d in table for " LUA_QL("concat"))

83. ERRDEF(TABSORT,        "invalid order function for sorting")

84. ERRDEF(IOCLFL,        "attempt to use a closed file")

85. ERRDEF(IOSTDCL,        "standard file is closed")

86. ERRDEF(OSUNIQF,        "unable to generate a unique filename")

87. ERRDEF(OSDATEF,        "field " LUA_QS " missing in date table")

88. ERRDEF(STRDUMP,        "unable to dump given function")

89. ERRDEF(STRSLC,        "string slice too long")

90. ERRDEF(STRPATB,        "missing " LUA_QL("[") " after " LUA_QL("%f") " in pattern")

91. ERRDEF(STRPATC,        "invalid pattern capture")

92. ERRDEF(STRPATE,        "malformed pattern (ends with " LUA_QL("%") ")")

93. ERRDEF(STRPATM,        "malformed pattern (missing " LUA_QL("]") ")")

94. ERRDEF(STRPATU,        "unbalanced pattern")

95. ERRDEF(STRPATX,        "pattern too complex")

96. ERRDEF(STRCAPI,        "invalid capture index")

97. ERRDEF(STRCAPN,        "too many captures")

98. ERRDEF(STRCAPU,        "unfinished capture")

99. ERRDEF(STRFMT,        "invalid option " LUA_QS " to " LUA_QL("format"))

100. ERRDEF(STRGSRV,        "invalid replacement value (a %s)")

101. ERRDEF(BADMODN,        "name conflict for module " LUA_QS)

102. #if LJ_HASJIT

103. ERRDEF(JITPROT,        "runtime code generation failed, restricted kernel?")

104. #if LJ_TARGET_X86ORX64

105. ERRDEF(NOJIT,        "JIT compiler disabled, CPU does not support SSE2")

106. #else

107. ERRDEF(NOJIT,        "JIT compiler disabled")

108. #endif

109. #elif defined(LJ_ARCH_NOJIT)

110. ERRDEF(NOJIT,        "no JIT compiler for this architecture (yet)")

111. #else

112. ERRDEF(NOJIT,        "JIT compiler permanently disabled by build option")

113. #endif

114. ERRDEF(JITOPT,        "unknown or malformed optimization flag " LUA_QS)

116. /* Lexer/parser errors. */

117. ERRDEF(XMODE,        "attempt to load chunk with wrong mode")

118. ERRDEF(XNEAR,        "%s near " LUA_QS)

119. ERRDEF(XLINES,        "chunk has too many lines")

120. ERRDEF(XLEVELS,        "chunk has too many syntax levels")

121. ERRDEF(XNUMBER,        "malformed number")

122. ERRDEF(XLSTR,        "unfinished long string")

123. ERRDEF(XLCOM,        "unfinished long comment")

124. ERRDEF(XSTR,        "unfinished string")

125. ERRDEF(XESC,        "invalid escape sequence")

126. ERRDEF(XLDELIM,        "invalid long string delimiter")

127. ERRDEF(XTOKEN,        LUA_QS " expected")

128. ERRDEF(XJUMP,        "control structure too long")

129. ERRDEF(XSLOTS,        "function or expression too complex")

130. ERRDEF(XLIMC,        "chunk has more than %d local variables")

131. ERRDEF(XLIMM,        "main function has more than %d %s")

132. ERRDEF(XLIMF,        "function at line %d has more than %d %s")

133. ERRDEF(XMATCH,        LUA_QS " expected (to close " LUA_QS " at line %d)")

134. ERRDEF(XFIXUP,        "function too long for return fixup")

135. ERRDEF(XPARAM,        "<name> or " LUA_QL("...") " expected")

136. #if !LJ_52

137. ERRDEF(XAMBIG,        "ambiguous syntax (function call x new statement)")

138. #endif

139. ERRDEF(XFUNARG,        "function arguments expected")

140. ERRDEF(XSYMBOL,        "unexpected symbol")

141. ERRDEF(XDOTS,        "cannot use " LUA_QL("...") " outside a vararg function")

142. ERRDEF(XSYNTAX,        "syntax error")

143. ERRDEF(XFOR,        LUA_QL("=") " or " LUA_QL("in") " expected")

144. ERRDEF(XBREAK,        "no loop to break")

145. ERRDEF(XLUNDEF,        "undefined label " LUA_QS)

146. ERRDEF(XLDUP,        "duplicate label " LUA_QS)

147. ERRDEF(XGSCOPE,        "<goto %s> jumps into the scope of local " LUA_QS)

149. /* Bytecode reader errors. */

150. ERRDEF(BCFMT,        "cannot load incompatible bytecode")

151. ERRDEF(BCBAD,        "cannot load malformed bytecode")

153. #if LJ_HASFFI

154. /* FFI errors. */

155. ERRDEF(FFI_INVTYPE,        "invalid C type")

156. ERRDEF(FFI_INVSIZE,        "size of C type is unknown or too large")

157. ERRDEF(FFI_BADSCL,        "bad storage class")

158. ERRDEF(FFI_DECLSPEC,        "declaration specifier expected")

159. ERRDEF(FFI_BADTAG,        "undeclared or implicit tag " LUA_QS)

160. ERRDEF(FFI_REDEF,        "attempt to redefine " LUA_QS)

161. ERRDEF(FFI_NUMPARAM,        "wrong number of type parameters")

162. ERRDEF(FFI_INITOV,        "too many initializers for " LUA_QS)

163. ERRDEF(FFI_BADCONV,        "cannot convert " LUA_QS " to " LUA_QS)

164. ERRDEF(FFI_BADLEN,        "attempt to get length of " LUA_QS)

165. ERRDEF(FFI_BADCONCAT,        "attempt to concatenate " LUA_QS " and " LUA_QS)

166. ERRDEF(FFI_BADARITH,        "attempt to perform arithmetic on " LUA_QS " and " LUA_QS)

167. ERRDEF(FFI_BADCOMP,        "attempt to compare " LUA_QS " with " LUA_QS)

168. ERRDEF(FFI_BADCALL,        LUA_QS " is not callable")

169. ERRDEF(FFI_NUMARG,        "wrong number of arguments for function call")

170. ERRDEF(FFI_BADMEMBER,        LUA_QS " has no member named " LUA_QS)

171. ERRDEF(FFI_BADIDX,        LUA_QS " cannot be indexed")

172. ERRDEF(FFI_BADIDXW,        LUA_QS " cannot be indexed with " LUA_QS)

173. ERRDEF(FFI_BADMM,        LUA_QS " has no " LUA_QS " metamethod")

174. ERRDEF(FFI_WRCONST,        "attempt to write to constant location")

175. ERRDEF(FFI_NODECL,        "missing declaration for symbol " LUA_QS)

176. ERRDEF(FFI_BADCBACK,        "bad callback")

177. #if LJ_OS_NOJIT

178. ERRDEF(FFI_CBACKOV,        "no support for callbacks on this OS")

179. #else

180. ERRDEF(FFI_CBACKOV,        "too many callbacks")

181. #endif

182. ERRDEF(FFI_NYIPACKBIT,        "NYI: packed bit fields")

183. ERRDEF(FFI_NYICALL,        "NYI: cannot call this C function (yet)")

184. #endif

186. ‌#undef ERRDEF

188. /* Detecting unused error messages:

189. awk -F, '/^ERRDEF/ { gsub(/ERRDEF./, ""); printf "grep -q LJ_ERR_%s *.[ch] || echo %s\n", $1, $1}' lj_errmsg.h | sh

190. */

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_func.h - luajit-2.0-src

Macros defined

- _LJ_FUNC_H

Source code

1. /*

2. ** Function handling (prototypes, functions and upvalues).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_FUNC_H

7. ‌#define _LJ_FUNC_H

9. #include "lj_obj.h"

11. /* Prototypes. */

12. LJ_FUNC void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt);

14. /* Upvalues. */

15. LJ_FUNCA void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level);

16. LJ_FUNC void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv);

18. /* Functions (closures). */

19. LJ_FUNC GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env);

20. LJ_FUNC GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env);

21. LJ_FUNCA GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent);

22. LJ_FUNC void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *c);

24. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_gdbjit.c - luajit-2.0-src

Global variables defined

- __jit_debug_descriptor

- elfhdr_template

Data types defined

- ELFheader

- ELFheader

- ELFsectheader

- ELFsectheader

- ELFsymbol

- ELFsymbol

- GDBJITctx

- GDBJITctx

- GDBJITdesc

- GDBJITdesc

- GDBJITentry

- GDBJITentry

- GDBJITentryobj

- GDBJITentryobj

- GDBJITinitf

- GDBJITobj

- GDBJITobj

Functions defined

- __jit_debug_register_code

- gdbjit_buildobj

- gdbjit_catnum

- gdbjit_debugabbrev

- gdbjit_debuginfo

- gdbjit_debugline

- gdbjit_ehframe

- gdbjit_initsect

- gdbjit_newentry

- gdbjit_secthdr

- gdbjit_sleb128

- gdbjit_strz

- gdbjit_symtab

- lj_gdbjit_addtrace

- lj_gdbjit_deltrace

Macros defined

- DADDR

- DADDR

- DALIGNNOP

- DALIGNNOP

- DB

- DB

- DI8

- DI8

- DLNE

- DLNE

- DSECT

- DSECT

- DSTR

- DSTR

- DSV

- DSV

- DU16

- DU16

- DU32

- DU32

- DUV

- DUV

- DW_CIE_VERSION

- ELFSECT_FLAGS_ALLOC

- ELFSECT_FLAGS_EXEC

- ELFSECT_FLAGS_WRITE

- ELFSECT_IDX_ABS

- LUA_CORE

- SECTALIGN

- SECTALIGN

- SECTDEF

- SECTDEF

- lj_gdbjit_c

Source code

1. /*

2. ** Client for the GDB JIT API.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_gdbjit_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT

13. #include "lj_gc.h"

14. #include "lj_err.h"

15. #include "lj_debug.h"

16. #include "lj_frame.h"

17. #include "lj_buf.h"

18. #include "lj_strfmt.h"

19. #include "lj_jit.h"

20. #include "lj_dispatch.h"

22. /* This is not compiled in by default.

23. ** Enable with -DLUAJIT_USE_GDBJIT in the Makefile and recompile everything.

24. */

25. #ifdef LUAJIT_USE_GDBJIT

27. /* The GDB JIT API allows JIT compilers to pass debug information about

28. ** JIT-compiled code back to GDB. You need at least GDB 7.0 or higher

29. ** to see it in action.

30. **

31. ** This is a passive API, so it works even when not running under GDB

32. ** or when attaching to an already running process. Alas, this implies

33. ** enabling it always has a non-negligible overhead -- do not use in

34. ** release mode!

35. **

36. ** The LuaJIT GDB JIT client is rather minimal at the moment. It gives

37. ** each trace a symbol name and adds a source location and frame unwind

38. ** information. Obviously LuaJIT itself and any embedding C application

39. ** should be compiled with debug symbols, too (see the Makefile).

40. **

41. ** Traces are named TRACE_1, TRACE_2, ... these correspond to the trace

42. ** numbers from -jv or -jdump. Use "break TRACE_1" or "tbreak TRACE_1" etc.

43. ** to set breakpoints on specific traces (even ahead of their creation).

44. **

45. ** The source location for each trace allows listing the corresponding

46. ** source lines with the GDB command "list" (but only if the Lua source

47. ** has been loaded from a file). Currently this is always set to the

48. ** location where the trace has been started.

49. **

50. ** Frame unwind information can be inspected with the GDB command

51. ** "info frame". This also allows proper backtraces across JIT-compiled

52. ** code with the GDB command "bt".

53. **

54. ** You probably want to add the following settings to a .gdbinit file

55. ** (or add them to ~/.gdbinit):

56. **   set disassembly-flavor intel

57. **   set breakpoint pending on

58. **

59. ** Here's a sample GDB session:

60. ** ------------------------------------------------------------------------

62. $ cat >x.lua

63. for outer=1,100 do

64. for inner=1,100 do end

65. end

66. ^D

68. $ luajit -jv x.lua

69. [TRACE   1 x.lua:2]

70. [TRACE   2 (1/3) x.lua:1 -> 1]

72. $ gdb --quiet --args luajit x.lua

73. (gdb) tbreak TRACE_1

74. Function "TRACE_1" not defined.

75. Temporary breakpoint 1 (TRACE_1) pending.

76. (gdb) run

77. Starting program: luajit x.lua

79. Temporary breakpoint 1, TRACE_1 () at x.lua:2

80. 2          for inner=1,100 do end

81. (gdb) list

82. 1        for outer=1,100 do

83. 2          for inner=1,100 do end

84. 3        end

85. (gdb) bt

86. #0  TRACE_1 () at x.lua:2

87. #1  0x08053690 in lua_pcall [...]

88. [...]

89. #7  0x0806ff90 in main [...]

90. (gdb) disass TRACE_1

91. Dump of assembler code for function TRACE_1:

92. 0xf7fd9fba <TRACE_1+0>:        mov    DWORD PTR ds:0xf7e0e2a0,0x1

93. 0xf7fd9fc4 <TRACE_1+10>:        movsd  xmm7,QWORD PTR [edx+0x20]

94. [...]

95. 0xf7fd9ff8 <TRACE_1+62>:        jmp    0xf7fd2014

96. End of assembler dump.

97. (gdb) tbreak TRACE_2

98. Function "TRACE_2" not defined.

99. Temporary breakpoint 2 (TRACE_2) pending.

100. (gdb) cont

101. Continuing.

103. Temporary breakpoint 2, TRACE_2 () at x.lua:1

104. 1        for outer=1,100 do

105. (gdb) info frame

106. Stack level 0, frame at 0xffffd7c0:

107. eip = 0xf7fd9f60 in TRACE_2 (x.lua:1); saved eip 0x8053690

108. called by frame at 0xffffd7e0

109. source language unknown.

110. Arglist at 0xffffd78c, args:

111. Locals at 0xffffd78c, Previous frame's sp is 0xffffd7c0

112. Saved registers:

113. ebx at 0xffffd7ac, ebp at 0xffffd7b8, esi at 0xffffd7b0, edi at 0xffffd7b4,

114. eip at 0xffffd7bc

115. (gdb)

117. ** ------------------------------------------------------------------------

118. */

120. /* -- GDB JIT API --------------------------------------------------------- */

122. /* GDB JIT actions. */

123. enum {

124. GDBJIT_NOACTION = 0,

125. GDBJIT_REGISTER,

126. GDBJIT_UNREGISTER

127. };

129. /* GDB JIT entry. */

130. ‌typedef struct GDBJITentry {

131. struct GDBJITentry *next_entry;

132. struct GDBJITentry *prev_entry;

133. const char *symfile_addr;

134. uint64_t symfile_size;

135. ‌} GDBJITentry;

137. /* GDB JIT descriptor. */

138. ‌typedef struct GDBJITdesc {

139. uint32_t version;

140. uint32_t action_flag;

141. GDBJITentry *relevant_entry;

142. GDBJITentry *first_entry;

143. ‌} GDBJITdesc;

145. ‌GDBJITdesc __jit_debug_descriptor = {

146. 1, GDBJIT_NOACTION, NULL, NULL

147. };

149. /* GDB sets a breakpoint at this function. */

150. ‌void LJ_NOINLINE __jit_debug_register_code()

151. {

152. __asm__ __volatile__("");

153. };

155. /* -- In-memory ELF object definitions ------------------------------------ */

157. /* ELF definitions. */

158. ‌typedef struct ELFheader {

159. uint8_t emagic[4];

160. uint8_t eclass;

161. uint8_t eendian;

162. uint8_t eversion;

163. uint8_t eosabi;

164. uint8_t eabiversion;

165. uint8_t epad[7];

166. uint16_t type;

167. uint16_t machine;

168. uint32_t version;

169. uintptr_t entry;

170. uintptr_t phofs;

171. uintptr_t shofs;

172. uint32_t flags;

173. uint16_t ehsize;

174. uint16_t phentsize;

175. uint16_t phnum;

176. uint16_t shentsize;

177. uint16_t shnum;

178. uint16_t shstridx;

179. ‌} ELFheader;

181. ‌typedef struct ELFsectheader {

182. uint32_t name;

183. uint32_t type;

184. uintptr_t flags;

185. uintptr_t addr;

186. uintptr_t ofs;

187. uintptr_t size;

188. uint32_t link;

189. uint32_t info;

190. uintptr_t align;

191. uintptr_t entsize;

192. ‌} ELFsectheader;

194. ‌#define ELFSECT_IDX_ABS                0xfff1

196. enum {

197. ELFSECT_TYPE_PROGBITS = 1,

198. ELFSECT_TYPE_SYMTAB = 2,

199. ELFSECT_TYPE_STRTAB = 3,

200. ELFSECT_TYPE_NOBITS = 8

201. };

203. ‌#define ELFSECT_FLAGS_WRITE        1

204. ‌#define ELFSECT_FLAGS_ALLOC        2

205. ‌#define ELFSECT_FLAGS_EXEC        4

207. ‌typedef struct ELFsymbol {

208. #if LJ_64

209. uint32_t name;

210. uint8_t info;

211. uint8_t other;

212. uint16_t sectidx;

213. uintptr_t value;

214. uint64_t size;

215. #else

216. uint32_t name;

217. uintptr_t value;

218. uint32_t size;

219. uint8_t info;

220. uint8_t other;

221. uint16_t sectidx;

222. #endif

223. ‌} ELFsymbol;

225. enum {

226. ELFSYM_TYPE_FUNC = 2,

227. ELFSYM_TYPE_FILE = 4,

228. ELFSYM_BIND_LOCAL = 0 << 4,

229. ELFSYM_BIND_GLOBAL = 1 << 4,

230. };

232. /* DWARF definitions. */

233. ‌#define DW_CIE_VERSION        1

235. enum {

236. DW_CFA_nop = 0x0,

237. DW_CFA_offset_extended = 0x5,

238. DW_CFA_def_cfa = 0xc,

239. DW_CFA_def_cfa_offset = 0xe,

240. DW_CFA_offset_extended_sf = 0x11,

241. DW_CFA_advance_loc = 0x40,

242. DW_CFA_offset = 0x80

243. };

245. enum {

246. DW_EH_PE_udata4 = 3,

247. DW_EH_PE_textrel = 0x20

248. };

250. enum {

251. DW_TAG_compile_unit = 0x11

252. };

254. enum {

255. DW_children_no = 0,

256. DW_children_yes = 1

257. };

259. enum {

260. DW_AT_name = 0x03,

261. DW_AT_stmt_list = 0x10,

262. DW_AT_low_pc = 0x11,

263. DW_AT_high_pc = 0x12

264. };

266. enum {

267. DW_FORM_addr = 0x01,

268. DW_FORM_data4 = 0x06,

269. DW_FORM_string = 0x08

270. };

272. enum {

273. DW_LNS_extended_op = 0,

274. DW_LNS_copy = 1,

275. DW_LNS_advance_pc = 2,

276. DW_LNS_advance_line = 3

277. };

279. enum {

280. DW_LNE_end_sequence = 1,

281. DW_LNE_set_address = 2

282. };

284. enum {

285. #if LJ_TARGET_X86

286. DW_REG_AX, DW_REG_CX, DW_REG_DX, DW_REG_BX,

287. DW_REG_SP, DW_REG_BP, DW_REG_SI, DW_REG_DI,

288. DW_REG_RA,

289. #elif LJ_TARGET_X64

290. /* Yes, the order is strange, but correct. */

291. DW_REG_AX, DW_REG_DX, DW_REG_CX, DW_REG_BX,

292. DW_REG_SI, DW_REG_DI, DW_REG_BP, DW_REG_SP,

293. DW_REG_8, DW_REG_9, DW_REG_10, DW_REG_11,

294. DW_REG_12, DW_REG_13, DW_REG_14, DW_REG_15,

295. DW_REG_RA,

296. #elif LJ_TARGET_ARM

297. DW_REG_SP = 13,

298. DW_REG_RA = 14,

299. #elif LJ_TARGET_PPC

300. DW_REG_SP = 1,

301. DW_REG_RA = 65,

302. DW_REG_CR = 70,

303. #elif LJ_TARGET_MIPS

304. DW_REG_SP = 29,

305. DW_REG_RA = 31,

306. #else

307. #error "Unsupported target architecture"

308. #endif

309. };

311. /* Minimal list of sections for the in-memory ELF object. */

312. enum {

313. GDBJIT_SECT_NULL,

314. GDBJIT_SECT_text,

315. GDBJIT_SECT_eh_frame,

316. GDBJIT_SECT_shstrtab,

317. GDBJIT_SECT_strtab,

318. GDBJIT_SECT_symtab,

319. GDBJIT_SECT_debug_info,

320. GDBJIT_SECT_debug_abbrev,

321. GDBJIT_SECT_debug_line,

322. GDBJIT_SECT__MAX

323. };

325. enum {

326. GDBJIT_SYM_UNDEF,

327. GDBJIT_SYM_FILE,

328. GDBJIT_SYM_FUNC,

329. GDBJIT_SYM__MAX

330. };

332. /* In-memory ELF object. */

333. ‌typedef struct GDBJITobj {

334. ELFheader hdr;                        /* ELF header. */

335. ELFsectheader sect[GDBJIT_SECT__MAX];        /* ELF sections. */

336. ELFsymbol sym[GDBJIT_SYM__MAX];        /* ELF symbol table. */

337. uint8_t space[4096];                        /* Space for various section data. */

338. ‌} GDBJITobj;

340. /* Combined structure for GDB JIT entry and ELF object. */

341. ‌typedef struct GDBJITentryobj {

342. GDBJITentry entry;

343. size_t sz;

344. GDBJITobj obj;

345. ‌} GDBJITentryobj;

347. /* Template for in-memory ELF header. */

348. ‌static const ELFheader elfhdr_template = {

349. .emagic = { 0x7f, 'E', 'L', 'F' },

350. .eclass = LJ_64 ? 2 : 1,

351. .eendian = LJ_ENDIAN_SELECT(1, 2),

352. .eversion = 1,

353. #if LJ_TARGET_LINUX

354. .eosabi = 0,  /* Nope, it's not 3. */

355. #elif defined(__FreeBSD__)

356. .eosabi = 9,

357. #elif defined(__NetBSD__)

358. .eosabi = 2,

359. #elif defined(__OpenBSD__)

360. .eosabi = 12,

361. #elif defined(__DragonFly__)

362. .eosabi = 0,

363. #elif (defined(__sun__) && defined(__svr4__))

364. .eosabi = 6,

365. #else

366. .eosabi = 0,

367. #endif

368. .eabiversion = 0,

369. .epad = { 0, 0, 0, 0, 0, 0, 0 },

370. .type = 1,

371. #if LJ_TARGET_X86

372. .machine = 3,

373. #elif LJ_TARGET_X64

374. .machine = 62,

375. #elif LJ_TARGET_ARM

376. .machine = 40,

377. #elif LJ_TARGET_PPC

378. .machine = 20,

379. #elif LJ_TARGET_MIPS

380. .machine = 8,

381. #else

382. #error "Unsupported target architecture"

383. #endif

384. .version = 1,

385. .entry = 0,

386. .phofs = 0,

387. .shofs = offsetof(GDBJITobj, sect),

388. .flags = 0,

389. .ehsize = sizeof(ELFheader),

390. .phentsize = 0,

391. .phnum = 0,

392. .shentsize = sizeof(ELFsectheader),

393. .shnum = GDBJIT_SECT__MAX,

394. .shstridx = GDBJIT_SECT_shstrtab

395. };

397. /* -- In-memory ELF object generation ------------------------------------- */

399. /* Context for generating the ELF object for the GDB JIT API. */

400. ‌typedef struct GDBJITctx {

401. uint8_t *p;                /* Pointer to next address in obj.space. */

402. uint8_t *startp;        /* Pointer to start address in obj.space. */

403. GCtrace *T;                /* Generate symbols for this trace. */

404. uintptr_t mcaddr;        /* Machine code address. */

405. MSize szmcode;        /* Size of machine code. */

406. MSize spadjp;                /* Stack adjustment for parent trace or interpreter. */

407. MSize spadj;                /* Stack adjustment for trace itself. */

408. BCLine lineno;        /* Starting line number. */

409. const char *filename;        /* Starting file name. */

410. size_t objsize;        /* Final size of ELF object. */

411. GDBJITobj obj;        /* In-memory ELF object. */

412. ‌} GDBJITctx;

414. /* Add a zero-terminated string. */

415. ‌static uint32_t gdbjit_strz(GDBJITctx *ctx, const char *str)

416. {

417. uint8_t *p = ctx->p;

418. uint32_t ofs = (uint32_t)(p - ctx->startp);

419. do {

420. *p++ = (uint8_t)*str;

421. } while (*str++);

422. ctx->p = p;

423. return ofs;

424. }

426. /* Append a decimal number. */

427. ‌static void gdbjit_catnum(GDBJITctx *ctx, uint32_t n)

428. {

429. if (n >= 10) { uint32_t m = n / 10; n = n % 10; gdbjit_catnum(ctx, m); }

430. *ctx->p++ = '0' + n;

431. }

433. /* Add a SLEB128 value. */

434. ‌static void gdbjit_sleb128(GDBJITctx *ctx, int32_t v)

435. {

436. uint8_t *p = ctx->p;

437. for (; (uint32_t)(v+0x40) >= 0x80; v >>= 7)

438. *p++ = (uint8_t)((v & 0x7f) | 0x80);

439. *p++ = (uint8_t)(v & 0x7f);

440. ctx->p = p;

441. }

443. /* Shortcuts to generate DWARF structures. */

444. ‌#define DB(x)                (*p++ = (x))

445. ‌#define DI8(x)                (*(int8_t *)p = (x), p++)

446. ‌#define DU16(x)                (*(uint16_t *)p = (x), p += 2)

447. ‌#define DU32(x)                (*(uint32_t *)p = (x), p += 4)

448. ‌#define DADDR(x)        (*(uintptr_t *)p = (x), p += sizeof(uintptr_t))

449. ‌#define DUV(x)                (p = (uint8_t *)lj_strfmt_wuleb128((char *)p, (x)))

450. ‌#define DSV(x)                (ctx->p = p, gdbjit_sleb128(ctx, (x)), p = ctx->p)

451. ‌#define DSTR(str)        (ctx->p = p, gdbjit_strz(ctx, (str)), p = ctx->p)

452. ‌#define DALIGNNOP(s)        while ((uintptr_t)p & ((s)-1)) *p++ = DW_CFA_nop

453. ‌#define DSECT(name, stmt) \

454. { uint32_t *szp_##name = (uint32_t *)p; p += 4; stmt \

455. *szp_##name = (uint32_t)((p-(uint8_t *)szp_##name)-4); } \

457. /* Initialize ELF section headers. */

458. ‌static void LJ_FASTCALL gdbjit_secthdr(GDBJITctx *ctx)

459. {

460. ELFsectheader *sect;

462. *ctx->p++ = '\0';  /* Empty string at start of string table. */

464. ‌#define SECTDEF(id, tp, al) \

465. sect = &ctx->obj.sect[GDBJIT_SECT_##id]; \

466. sect->name = gdbjit_strz(ctx, "." #id); \

467. sect->type = ELFSECT_TYPE_##tp; \

468. sect->align = (al)

470. SECTDEF(text, NOBITS, 16);

471. sect->flags = ELFSECT_FLAGS_ALLOC|ELFSECT_FLAGS_EXEC;

472. sect->addr = ctx->mcaddr;

473. sect->ofs = 0;

474. sect->size = ctx->szmcode;

476. SECTDEF(eh_frame, PROGBITS, sizeof(uintptr_t));

477. sect->flags = ELFSECT_FLAGS_ALLOC;

479. SECTDEF(shstrtab, STRTAB, 1);

480. SECTDEF(strtab, STRTAB, 1);

482. SECTDEF(symtab, SYMTAB, sizeof(uintptr_t));

483. sect->ofs = offsetof(GDBJITobj, sym);

484. sect->size = sizeof(ctx->obj.sym);

485. sect->link = GDBJIT_SECT_strtab;

486. sect->entsize = sizeof(ELFsymbol);

487. sect->info = GDBJIT_SYM_FUNC;

489. SECTDEF(debug_info, PROGBITS, 1);

490. SECTDEF(debug_abbrev, PROGBITS, 1);

491. SECTDEF(debug_line, PROGBITS, 1);

493. ‌#undef SECTDEF

494. }

496. /* Initialize symbol table. */

497. ‌static void LJ_FASTCALL gdbjit_symtab(GDBJITctx *ctx)

498. {

499. ELFsymbol *sym;

501. *ctx->p++ = '\0';  /* Empty string at start of string table. */

503. sym = &ctx->obj.sym[GDBJIT_SYM_FILE];

504. sym->name = gdbjit_strz(ctx, "JIT mcode");

505. sym->sectidx = ELFSECT_IDX_ABS;

506. sym->info = ELFSYM_TYPE_FILE|ELFSYM_BIND_LOCAL;

508. sym = &ctx->obj.sym[GDBJIT_SYM_FUNC];

509. sym->name = gdbjit_strz(ctx, "TRACE_"); ctx->p--;

510. gdbjit_catnum(ctx, ctx->T->traceno); *ctx->p++ = '\0';

511. sym->sectidx = GDBJIT_SECT_text;

512. sym->value = 0;

513. sym->size = ctx->szmcode;

514. sym->info = ELFSYM_TYPE_FUNC|ELFSYM_BIND_GLOBAL;

515. }

517. /* Initialize .eh_frame section. */

518. ‌static void LJ_FASTCALL gdbjit_ehframe(GDBJITctx *ctx)

519. {

520. uint8_t *p = ctx->p;

521. uint8_t *framep = p;

523. /* Emit DWARF EH CIE. */

524. DSECT(CIE,

525. DU32(0);                        /* Offset to CIE itself. */

526. DB(DW_CIE_VERSION);

527. DSTR("zR");                        /* Augmentation. */

528. DUV(1);                        /* Code alignment factor. */

529. DSV(-(int32_t)sizeof(uintptr_t));  /* Data alignment factor. */

530. DB(DW_REG_RA);                /* Return address register. */

531. DB(1); DB(DW_EH_PE_textrel|DW_EH_PE_udata4);  /* Augmentation data. */

532. DB(DW_CFA_def_cfa); DUV(DW_REG_SP); DUV(sizeof(uintptr_t));

533. #if LJ_TARGET_PPC

534. DB(DW_CFA_offset_extended_sf); DB(DW_REG_RA); DSV(-1);

535. #else

536. DB(DW_CFA_offset|DW_REG_RA); DUV(1);

537. #endif

538. DALIGNNOP(sizeof(uintptr_t));

539. )

541. /* Emit DWARF EH FDE. */

542. DSECT(FDE,

543. DU32((uint32_t)(p-framep));        /* Offset to CIE. */

544. DU32(0);                        /* Machine code offset relative to .text. */

545. DU32(ctx->szmcode);                /* Machine code length. */

546. DB(0);                        /* Augmentation data. */

547. /* Registers saved in CFRAME. */

548. #if LJ_TARGET_X86

549. DB(DW_CFA_offset|DW_REG_BP); DUV(2);

550. DB(DW_CFA_offset|DW_REG_DI); DUV(3);

551. DB(DW_CFA_offset|DW_REG_SI); DUV(4);

552. DB(DW_CFA_offset|DW_REG_BX); DUV(5);

553. #elif LJ_TARGET_X64

554. DB(DW_CFA_offset|DW_REG_BP); DUV(2);

555. DB(DW_CFA_offset|DW_REG_BX); DUV(3);

556. DB(DW_CFA_offset|DW_REG_15); DUV(4);

557. DB(DW_CFA_offset|DW_REG_14); DUV(5);

558. /* Extra registers saved for JIT-compiled code. */

559. DB(DW_CFA_offset|DW_REG_13); DUV(9);

560. DB(DW_CFA_offset|DW_REG_12); DUV(10);

561. #elif LJ_TARGET_ARM

562. {

563. int i;

564. for (i = 11; i >= 4; i--) { DB(DW_CFA_offset|i); DUV(2+(11-i)); }

565. }

566. #elif LJ_TARGET_PPC

567. {

568. int i;

569. DB(DW_CFA_offset_extended); DB(DW_REG_CR); DUV(55);

570. for (i = 14; i <= 31; i++) {

571. DB(DW_CFA_offset|i); DUV(37+(31-i));

572. DB(DW_CFA_offset|32|i); DUV(2+2*(31-i));

573. }

574. }

575. #elif LJ_TARGET_MIPS

576. {

577. int i;

578. DB(DW_CFA_offset|30); DUV(2);

579. for (i = 23; i >= 16; i--) { DB(DW_CFA_offset|i); DUV(26-i); }

580. for (i = 30; i >= 20; i -= 2) { DB(DW_CFA_offset|32|i); DUV(42-i); }

581. }

582. #else

583. #error "Unsupported target architecture"

584. #endif

585. if (ctx->spadjp != ctx->spadj) {  /* Parent/interpreter stack frame size. */

586. DB(DW_CFA_def_cfa_offset); DUV(ctx->spadjp);

587. DB(DW_CFA_advance_loc|1);  /* Only an approximation. */

588. }

589. DB(DW_CFA_def_cfa_offset); DUV(ctx->spadj);  /* Trace stack frame size. */

590. DALIGNNOP(sizeof(uintptr_t));

591. )

593. ctx->p = p;

594. }

596. /* Initialize .debug_info section. */

597. ‌static void LJ_FASTCALL gdbjit_debuginfo(GDBJITctx *ctx)

598. {

599. uint8_t *p = ctx->p;

601. DSECT(info,

602. DU16(2);                        /* DWARF version. */

603. DU32(0);                        /* Abbrev offset. */

604. DB(sizeof(uintptr_t));        /* Pointer size. */

606. DUV(1);                        /* Abbrev #1: DW_TAG_compile_unit. */

607. DSTR(ctx->filename);        /* DW_AT_name. */

608. DADDR(ctx->mcaddr);                /* DW_AT_low_pc. */

609. DADDR(ctx->mcaddr + ctx->szmcode);  /* DW_AT_high_pc. */

610. DU32(0);                        /* DW_AT_stmt_list. */

611. )

613. ctx->p = p;

614. }

616. /* Initialize .debug_abbrev section. */

617. ‌static void LJ_FASTCALL gdbjit_debugabbrev(GDBJITctx *ctx)

618. {

619. uint8_t *p = ctx->p;

621. /* Abbrev #1: DW_TAG_compile_unit. */

622. DUV(1); DUV(DW_TAG_compile_unit);

623. DB(DW_children_no);

624. DUV(DW_AT_name);        DUV(DW_FORM_string);

625. DUV(DW_AT_low_pc);        DUV(DW_FORM_addr);

626. DUV(DW_AT_high_pc);        DUV(DW_FORM_addr);

627. DUV(DW_AT_stmt_list);        DUV(DW_FORM_data4);

628. DB(0); DB(0);

630. ctx->p = p;

631. }

633. ‌#define DLNE(op, s)        (DB(DW_LNS_extended_op), DUV(1+(s)), DB((op)))

635. /* Initialize .debug_line section. */

636. ‌static void LJ_FASTCALL gdbjit_debugline(GDBJITctx *ctx)

637. {

638. uint8_t *p = ctx->p;

640. DSECT(line,

641. DU16(2);                        /* DWARF version. */

642. DSECT(header,

643. DB(1);                        /* Minimum instruction length. */

644. DB(1);                        /* is_stmt. */

645. DI8(0);                        /* Line base for special opcodes. */

646. DB(2);                        /* Line range for special opcodes. */

647. DB(3+1);                        /* Opcode base at DW_LNS_advance_line+1. */

648. DB(0); DB(1); DB(1);        /* Standard opcode lengths. */

649. /* Directory table. */

650. DB(0);

651. /* File name table. */

652. DSTR(ctx->filename); DUV(0); DUV(0); DUV(0);

653. DB(0);

654. )

656. DLNE(DW_LNE_set_address, sizeof(uintptr_t)); DADDR(ctx->mcaddr);

657. if (ctx->lineno) {

658. DB(DW_LNS_advance_line); DSV(ctx->lineno-1);

659. }

660. DB(DW_LNS_copy);

661. DB(DW_LNS_advance_pc); DUV(ctx->szmcode);

662. DLNE(DW_LNE_end_sequence, 0);

663. )

665. ctx->p = p;

666. }

668. ‌#undef DLNE

670. /* Undef shortcuts. */

671. ‌#undef DB

672. ‌#undef DI8

673. ‌#undef DU16

674. ‌#undef DU32

675. ‌#undef DADDR

676. ‌#undef DUV

677. ‌#undef DSV

678. ‌#undef DSTR

679. ‌#undef DALIGNNOP

680. ‌#undef DSECT

682. /* Type of a section initializer callback. */

683. ‌typedef void (LJ_FASTCALL *GDBJITinitf)(GDBJITctx *ctx);

685. /* Call section initializer and set the section offset and size. */

686. ‌static void gdbjit_initsect(GDBJITctx *ctx, int sect, GDBJITinitf initf)

687. {

688. ctx->startp = ctx->p;

689. ctx->obj.sect[sect].ofs = (uintptr_t)((char *)ctx->p - (char *)&ctx->obj);

690. initf(ctx);

691. ctx->obj.sect[sect].size = (uintptr_t)(ctx->p - ctx->startp);

692. }

694. ‌#define SECTALIGN(p, a) \

695. ((p) = (uint8_t *)(((uintptr_t)(p) + ((a)-1)) & ~(uintptr_t)((a)-1)))

697. /* Build in-memory ELF object. */

698. ‌static void gdbjit_buildobj(GDBJITctx *ctx)

699. {

700. GDBJITobj *obj = &ctx->obj;

701. /* Fill in ELF header and clear structures. */

702. memcpy(&obj->hdr, &elfhdr_template, sizeof(ELFheader));

703. memset(&obj->sect, 0, sizeof(ELFsectheader)*GDBJIT_SECT__MAX);

704. memset(&obj->sym, 0, sizeof(ELFsymbol)*GDBJIT_SYM__MAX);

705. /* Initialize sections. */

706. ctx->p = obj->space;

707. gdbjit_initsect(ctx, GDBJIT_SECT_shstrtab, gdbjit_secthdr);

708. gdbjit_initsect(ctx, GDBJIT_SECT_strtab, gdbjit_symtab);

709. gdbjit_initsect(ctx, GDBJIT_SECT_debug_info, gdbjit_debuginfo);

710. gdbjit_initsect(ctx, GDBJIT_SECT_debug_abbrev, gdbjit_debugabbrev);

711. gdbjit_initsect(ctx, GDBJIT_SECT_debug_line, gdbjit_debugline);

712. SECTALIGN(ctx->p, sizeof(uintptr_t));

713. gdbjit_initsect(ctx, GDBJIT_SECT_eh_frame, gdbjit_ehframe);

714. ctx->objsize = (size_t)((char *)ctx->p - (char *)obj);

715. lua_assert(ctx->objsize < sizeof(GDBJITobj));

716. }

718. ‌#undef SECTALIGN

720. /* -- Interface to GDB JIT API -------------------------------------------- */

722. /* Add new entry to GDB JIT symbol chain. */

723. ‌static void gdbjit_newentry(lua_State *L, GDBJITctx *ctx)

724. {

725. /* Allocate memory for GDB JIT entry and ELF object. */

726. MSize sz = (MSize)(sizeof(GDBJITentryobj) - sizeof(GDBJITobj) + ctx->objsize);

727. GDBJITentryobj *eo = lj_mem_newt(L, sz, GDBJITentryobj);

728. memcpy(&eo->obj, &ctx->obj, ctx->objsize);  /* Copy ELF object. */

729. eo->sz = sz;

730. ctx->T->gdbjit_entry = (void *)eo;

731. /* Link new entry to chain and register it. */

732. eo->entry.prev_entry = NULL;

733. eo->entry.next_entry = __jit_debug_descriptor.first_entry;

734. if (eo->entry.next_entry)

735. eo->entry.next_entry->prev_entry = &eo->entry;

736. eo->entry.symfile_addr = (const char *)&eo->obj;

737. eo->entry.symfile_size = ctx->objsize;

738. __jit_debug_descriptor.first_entry = &eo->entry;

739. __jit_debug_descriptor.relevant_entry = &eo->entry;

740. __jit_debug_descriptor.action_flag = GDBJIT_REGISTER;

741. __jit_debug_register_code();

742. }

744. /* Add debug info for newly compiled trace and notify GDB. */

745. ‌void lj_gdbjit_addtrace(jit_State *J, GCtrace *T)

746. {

747. GDBJITctx ctx;

748. GCproto *pt = &gcref(T->startpt)->pt;

749. TraceNo parent = T->ir[REF_BASE].op1;

750. const BCIns *startpc = mref(T->startpc, const BCIns);

751. ctx.T = T;

752. ctx.mcaddr = (uintptr_t)T->mcode;

753. ctx.szmcode = T->szmcode;

754. ctx.spadjp = CFRAME_SIZE_JIT +

755. (MSize)(parent ? traceref(J, parent)->spadjust : 0);

756. ctx.spadj = CFRAME_SIZE_JIT + T->spadjust;

757. lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc);

758. ctx.lineno = lj_debug_line(pt, proto_bcpos(pt, startpc));

759. ctx.filename = proto_chunknamestr(pt);

760. if (*ctx.filename == '@' || *ctx.filename == '=')

761. ctx.filename++;

762. else

763. ctx.filename = "(string)";

764. gdbjit_buildobj(&ctx);

765. gdbjit_newentry(J->L, &ctx);

766. }

768. /* Delete debug info for trace and notify GDB. */

769. ‌void lj_gdbjit_deltrace(jit_State *J, GCtrace *T)

770. {

771. GDBJITentryobj *eo = (GDBJITentryobj *)T->gdbjit_entry;

772. if (eo) {

773. if (eo->entry.prev_entry)

774. eo->entry.prev_entry->next_entry = eo->entry.next_entry;

775. else

776. __jit_debug_descriptor.first_entry = eo->entry.next_entry;

777. if (eo->entry.next_entry)

778. eo->entry.next_entry->prev_entry = eo->entry.prev_entry;

779. __jit_debug_descriptor.relevant_entry = &eo->entry;

780. __jit_debug_descriptor.action_flag = GDBJIT_UNREGISTER;

781. __jit_debug_register_code();

782. lj_mem_free(J2G(J), eo, eo->sz);

783. }

784. }

786. #endif

787. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_opt_split.c - luajit-2.0-src

Functions defined

- cpsplit

- lj_opt_split

- split_bitop

- split_bitshift

- split_call_l

- split_call_li

- split_call_ll

- split_emit

- split_ir

- split_needsplit

- split_num2int

- split_ptr

- split_subst_snap

Macros defined

- IR

- IR

- LUA_CORE

- lj_opt_split_c

Source code

1. /*

2. ** SPLIT: Split 64 bit IR instructions into 32 bit IR instructions.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. ‌#define lj_opt_split_c

7. ‌#define LUA_CORE

9. #include "lj_obj.h"

11. #if LJ_HASJIT && (LJ_SOFTFP || (LJ_32 && LJ_HASFFI))

13. #include "lj_err.h"

14. #include "lj_buf.h"

15. #include "lj_ir.h"

16. #include "lj_jit.h"

17. #include "lj_ircall.h"

18. #include "lj_iropt.h"

19. #include "lj_vm.h"

21. /* SPLIT pass:

22. **

23. ** This pass splits up 64 bit IR instructions into multiple 32 bit IR

24. ** instructions. It's only active for soft-float targets or for 32 bit CPUs

25. ** which lack native 64 bit integer operations (the FFI is currently the

26. ** only emitter for 64 bit integer instructions).

27. **

28. ** Splitting the IR in a separate pass keeps each 32 bit IR assembler

29. ** backend simple. Only a small amount of extra functionality needs to be

30. ** implemented. This is much easier than adding support for allocating

31. ** register pairs to each backend (believe me, I tried). A few simple, but

32. ** important optimizations can be performed by the SPLIT pass, which would

33. ** be tedious to do in the backend.

34. **

35. ** The basic idea is to replace each 64 bit IR instruction with its 32 bit

36. ** equivalent plus an extra HIOP instruction. The splitted IR is not passed

37. ** through FOLD or any other optimizations, so each HIOP is guaranteed to

38. ** immediately follow it's counterpart. The actual functionality of HIOP is

39. ** inferred from the previous instruction.

40. **

41. ** The operands of HIOP hold the hiword input references. The output of HIOP

42. ** is the hiword output reference, which is also used to hold the hiword

43. ** register or spill slot information. The register allocator treats this

44. ** instruction independently of any other instruction, which improves code

45. ** quality compared to using fixed register pairs.

46. **

47. ** It's easier to split up some instructions into two regular 32 bit

48. ** instructions. E.g. XLOAD is split up into two XLOADs with two different

49. ** addresses. Obviously 64 bit constants need to be split up into two 32 bit

50. ** constants, too. Some hiword instructions can be entirely omitted, e.g.

51. ** when zero-extending a 32 bit value to 64 bits. 64 bit arguments for calls

52. ** are split up into two 32 bit arguments each.

53. **

54. ** On soft-float targets, floating-point instructions are directly converted

55. ** to soft-float calls by the SPLIT pass (except for comparisons and MIN/MAX).

56. ** HIOP for number results has the type IRT_SOFTFP ("sfp" in -jdump).

57. **

58. ** Here's the IR and x64 machine code for 'x.b = x.a + 1' for a struct with

59. ** two int64_t fields:

60. **

61. ** 0100    p32 ADD    base  +8

62. ** 0101    i64 XLOAD  0100

63. ** 0102    i64 ADD    0101  +1

64. ** 0103    p32 ADD    base  +16

65. ** 0104    i64 XSTORE 0103  0102

66. **

67. **         mov rax, [esi+0x8]

68. **         add rax, +0x01

69. **         mov [esi+0x10], rax

70. **

71. ** Here's the transformed IR and the x86 machine code after the SPLIT pass:

72. **

73. ** 0100    p32 ADD    base  +8

74. ** 0101    int XLOAD  0100

75. ** 0102    p32 ADD    base  +12

76. ** 0103    int XLOAD  0102

77. ** 0104    int ADD    0101  +1

78. ** 0105    int HIOP   0103  +0

79. ** 0106    p32 ADD    base  +16

80. ** 0107    int XSTORE 0106  0104

81. ** 0108    int HIOP   0106  0105

82. **

83. **         mov eax, [esi+0x8]

84. **         mov ecx, [esi+0xc]

85. **         add eax, +0x01

86. **         adc ecx, +0x00

87. **         mov [esi+0x10], eax

88. **         mov [esi+0x14], ecx

89. **

90. ** You may notice the reassociated hiword address computation, which is

91. ** later fused into the mov operands by the assembler.

92. */

94. /* Some local macros to save typing. Undef'd at the end. */

95. ‌#define IR(ref)                (&J->cur.ir[(ref)])

97. /* Directly emit the transformed IR without updating chains etc. */

98. ‌static IRRef split_emit(jit_State *J, uint16_t ot, IRRef1 op1, IRRef1 op2)

99. {

100. IRRef nref = lj_ir_nextins(J);

101. IRIns *ir = IR(nref);

102. ir->ot = ot;

103. ir->op1 = op1;

104. ir->op2 = op2;

105. return nref;

106. }

108. #if LJ_SOFTFP

109. /* Emit a (checked) number to integer conversion. */

110. ‌static IRRef split_num2int(jit_State *J, IRRef lo, IRRef hi, int check)

111. {

112. IRRef tmp, res;

113. #if LJ_LE

114. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), lo, hi);

115. #else

116. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hi, lo);

117. #endif

118. res = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_softfp_d2i);

119. if (check) {

120. tmp = split_emit(J, IRTI(IR_CALLN), res, IRCALL_softfp_i2d);

121. split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp);

122. split_emit(J, IRTGI(IR_EQ), tmp, lo);

123. split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP), tmp+1, hi);

124. }

125. return res;

126. }

128. /* Emit a CALLN with one split 64 bit argument. */

129. ‌static IRRef split_call_l(jit_State *J, IRRef1 *hisubst, IRIns *oir,

130. IRIns *ir, IRCallID id)

131. {

132. IRRef tmp, op1 = ir->op1;

133. J->cur.nins--;

134. #if LJ_LE

135. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]);

136. #else

137. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev);

138. #endif

139. ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id);

140. return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp);

141. }

142. #endif

144. /* Emit a CALLN with one split 64 bit argument and a 32 bit argument. */

145. ‌static IRRef split_call_li(jit_State *J, IRRef1 *hisubst, IRIns *oir,

146. IRIns *ir, IRCallID id)

147. {

148. IRRef tmp, op1 = ir->op1, op2 = ir->op2;

149. J->cur.nins--;

150. #if LJ_LE

151. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]);

152. #else

153. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev);

154. #endif

155. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev);

156. ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id);

157. return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp);

158. }

160. /* Emit a CALLN with two split 64 bit arguments. */

161. ‌static IRRef split_call_ll(jit_State *J, IRRef1 *hisubst, IRIns *oir,

162. IRIns *ir, IRCallID id)

163. {

164. IRRef tmp, op1 = ir->op1, op2 = ir->op2;

165. J->cur.nins--;

166. #if LJ_LE

167. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]);

168. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev);

169. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]);

170. #else

171. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev);

172. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]);

173. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev);

174. #endif

175. ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id);

176. return split_emit(J,

177. IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT),

178. tmp, tmp);

179. }

181. /* Get a pointer to the other 32 bit word (LE: hiword, BE: loword). */

182. ‌static IRRef split_ptr(jit_State *J, IRIns *oir, IRRef ref)

183. {

184. IRRef nref = oir[ref].prev;

185. IRIns *ir = IR(nref);

186. int32_t ofs = 4;

187. if (ir->o == IR_KPTR)

188. return lj_ir_kptr(J, (char *)ir_kptr(ir) + ofs);

189. if (ir->o == IR_ADD && irref_isk(ir->op2) && !irt_isphi(oir[ref].t)) {

190. /* Reassociate address. */

191. ofs += IR(ir->op2)->i;

192. nref = ir->op1;

193. if (ofs == 0) return nref;

194. }

195. return split_emit(J, IRTI(IR_ADD), nref, lj_ir_kint(J, ofs));

196. }

198. #if LJ_HASFFI

199. ‌static IRRef split_bitshift(jit_State *J, IRRef1 *hisubst,

200. IRIns *oir, IRIns *nir, IRIns *ir)

201. {

202. IROp op = ir->o;

203. IRRef kref = nir->op2;

204. if (irref_isk(kref)) {  /* Optimize constant shifts. */

205. int32_t k = (IR(kref)->i & 63);

206. IRRef lo = nir->op1, hi = hisubst[ir->op1];

207. if (op == IR_BROL || op == IR_BROR) {

208. if (op == IR_BROR) k = (-k & 63);

209. if (k >= 32) { IRRef t = lo; lo = hi; hi = t; k -= 32; }

210. if (k == 0) {

211. passthrough:

212. J->cur.nins--;

213. ir->prev = lo;

214. return hi;

215. } else {

216. TRef k1, k2;

217. IRRef t1, t2, t3, t4;

218. J->cur.nins--;

219. k1 = lj_ir_kint(J, k);

220. k2 = lj_ir_kint(J, (-k & 31));

221. t1 = split_emit(J, IRTI(IR_BSHL), lo, k1);

222. t2 = split_emit(J, IRTI(IR_BSHL), hi, k1);

223. t3 = split_emit(J, IRTI(IR_BSHR), lo, k2);

224. t4 = split_emit(J, IRTI(IR_BSHR), hi, k2);

225. ir->prev = split_emit(J, IRTI(IR_BOR), t1, t4);

226. return split_emit(J, IRTI(IR_BOR), t2, t3);

227. }

228. } else if (k == 0) {

229. goto passthrough;

230. } else if (k < 32) {

231. if (op == IR_BSHL) {

232. IRRef t1 = split_emit(J, IRTI(IR_BSHL), hi, kref);

233. IRRef t2 = split_emit(J, IRTI(IR_BSHR), lo, lj_ir_kint(J, (-k&31)));

234. return split_emit(J, IRTI(IR_BOR), t1, t2);

235. } else {

236. IRRef t1 = ir->prev, t2;

237. lua_assert(op == IR_BSHR || op == IR_BSAR);

238. nir->o = IR_BSHR;

239. t2 = split_emit(J, IRTI(IR_BSHL), hi, lj_ir_kint(J, (-k&31)));

240. ir->prev = split_emit(J, IRTI(IR_BOR), t1, t2);

241. return split_emit(J, IRTI(op), hi, kref);

242. }

243. } else {

244. if (op == IR_BSHL) {

245. if (k == 32)

246. J->cur.nins--;

247. else

248. lo = ir->prev;

249. ir->prev = lj_ir_kint(J, 0);

250. return lo;

251. } else {

252. lua_assert(op == IR_BSHR || op == IR_BSAR);

253. if (k == 32) {

254. J->cur.nins--;

255. ir->prev = hi;

256. } else {

257. nir->op1 = hi;

258. }

259. if (op == IR_BSHR)

260. return lj_ir_kint(J, 0);

261. else

262. return split_emit(J, IRTI(IR_BSAR), hi, lj_ir_kint(J, 31));

263. }

264. }

265. }

266. return split_call_li(J, hisubst, oir, ir,

267. op - IR_BSHL + IRCALL_lj_carith_shl64);

268. }

270. ‌static IRRef split_bitop(jit_State *J, IRRef1 *hisubst,

271. IRIns *nir, IRIns *ir)

272. {

273. IROp op = ir->o;

274. IRRef hi, kref = nir->op2;

275. if (irref_isk(kref)) {  /* Optimize bit operations with lo constant. */

276. int32_t k = IR(kref)->i;

277. if (k == 0 || k == -1) {

278. if (op == IR_BAND) k = ~k;

279. if (k == 0) {

280. J->cur.nins--;

281. ir->prev = nir->op1;

282. } else if (op == IR_BXOR) {

283. nir->o = IR_BNOT;

284. nir->op2 = 0;

285. } else {

286. J->cur.nins--;

287. ir->prev = kref;

288. }

289. }

290. }

291. hi = hisubst[ir->op1];

292. kref = hisubst[ir->op2];

293. if (irref_isk(kref)) {  /* Optimize bit operations with hi constant. */

294. int32_t k = IR(kref)->i;

295. if (k == 0 || k == -1) {

296. if (op == IR_BAND) k = ~k;

297. if (k == 0) {

298. return hi;

299. } else if (op == IR_BXOR) {

300. return split_emit(J, IRTI(IR_BNOT), hi, 0);

301. } else {

302. return kref;

303. }

304. }

305. }

306. return split_emit(J, IRTI(op), hi, kref);

307. }

308. #endif

310. /* Substitute references of a snapshot. */

311. ‌static void split_subst_snap(jit_State *J, SnapShot *snap, IRIns *oir)

312. {

313. SnapEntry *map = &J->cur.snapmap[snap->mapofs];

314. MSize n, nent = snap->nent;

315. for (n = 0; n < nent; n++) {

316. SnapEntry sn = map[n];

317. IRIns *ir = &oir[snap_ref(sn)];

318. if (!(LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && irref_isk(snap_ref(sn))))

319. map[n] = ((sn & 0xffff0000) | ir->prev);

320. }

321. }

323. /* Transform the old IR to the new IR. */

324. ‌static void split_ir(jit_State *J)

325. {

326. IRRef nins = J->cur.nins, nk = J->cur.nk;

327. MSize irlen = nins - nk;

328. MSize need = (irlen+1)*(sizeof(IRIns) + sizeof(IRRef1));

329. IRIns *oir = (IRIns *)lj_buf_tmp(J->L, need);

330. IRRef1 *hisubst;

331. IRRef ref, snref;

332. SnapShot *snap;

334. /* Copy old IR to buffer. */

335. memcpy(oir, IR(nk), irlen*sizeof(IRIns));

336. /* Bias hiword substitution table and old IR. Loword kept in field prev. */

337. hisubst = (IRRef1 *)&oir[irlen] - nk;

338. oir -= nk;

340. /* Remove all IR instructions, but retain IR constants. */

341. J->cur.nins = REF_FIRST;

342. J->loopref = 0;

344. /* Process constants and fixed references. */

345. for (ref = nk; ref <= REF_BASE; ref++) {

346. IRIns *ir = &oir[ref];

347. if ((LJ_SOFTFP && ir->o == IR_KNUM) || ir->o == IR_KINT64) {

348. /* Split up 64 bit constant. */

349. TValue tv = *ir_k64(ir);

350. ir->prev = lj_ir_kint(J, (int32_t)tv.u32.lo);

351. hisubst[ref] = lj_ir_kint(J, (int32_t)tv.u32.hi);

352. } else {

353. ir->prev = ref;  /* Identity substitution for loword. */

354. hisubst[ref] = 0;

355. }

356. }

358. /* Process old IR instructions. */

359. snap = J->cur.snap;

360. snref = snap->ref;

361. for (ref = REF_FIRST; ref < nins; ref++) {

362. IRIns *ir = &oir[ref];

363. IRRef nref = lj_ir_nextins(J);

364. IRIns *nir = IR(nref);

365. IRRef hi = 0;

367. if (ref >= snref) {

368. snap->ref = nref;

369. split_subst_snap(J, snap++, oir);

370. snref = snap < &J->cur.snap[J->cur.nsnap] ? snap->ref : ~(IRRef)0;

371. }

373. /* Copy-substitute old instruction to new instruction. */

374. nir->op1 = ir->op1 < nk ? ir->op1 : oir[ir->op1].prev;

375. nir->op2 = ir->op2 < nk ? ir->op2 : oir[ir->op2].prev;

376. ir->prev = nref;  /* Loword substitution. */

377. nir->o = ir->o;

378. nir->t.irt = ir->t.irt & ~(IRT_MARK|IRT_ISPHI);

379. hisubst[ref] = 0;

381. /* Split 64 bit instructions. */

382. #if LJ_SOFTFP

383. if (irt_isnum(ir->t)) {

384. nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD);  /* Turn into INT op. */

385. /* Note: hi ref = lo ref + 1! Required for SNAP_SOFTFPNUM logic. */

386. switch (ir->o) {

387. case IR_ADD:

388. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_add);

389. break;

390. case IR_SUB:

391. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_sub);

392. break;

393. case IR_MUL:

394. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_mul);

395. break;

396. case IR_DIV:

397. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_div);

398. break;

399. case IR_POW:

400. hi = split_call_li(J, hisubst, oir, ir, IRCALL_lj_vm_powi);

401. break;

402. case IR_FPMATH:

403. /* Try to rejoin pow from EXP2, MUL and LOG2. */

404. if (nir->op2 == IRFPM_EXP2 && nir->op1 > J->loopref) {

405. IRIns *irp = IR(nir->op1);

406. if (irp->o == IR_CALLN && irp->op2 == IRCALL_softfp_mul) {

407. IRIns *irm4 = IR(irp->op1);

408. IRIns *irm3 = IR(irm4->op1);

409. IRIns *irm12 = IR(irm3->op1);

410. IRIns *irl1 = IR(irm12->op1);

411. if (irm12->op1 > J->loopref && irl1->o == IR_CALLN &&

412. irl1->op2 == IRCALL_lj_vm_log2) {

413. IRRef tmp = irl1->op1;  /* Recycle first two args from LOG2. */

414. IRRef arg3 = irm3->op2, arg4 = irm4->op2;

415. J->cur.nins--;

416. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, arg3);

417. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, arg4);

418. ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_pow);

419. hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp);

420. break;

421. }

422. }

423. }

424. hi = split_call_l(J, hisubst, oir, ir, IRCALL_lj_vm_floor + ir->op2);

425. break;

426. case IR_ATAN2:

427. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_atan2);

428. break;

429. case IR_LDEXP:

430. hi = split_call_li(J, hisubst, oir, ir, IRCALL_ldexp);

431. break;

432. case IR_NEG: case IR_ABS:

433. nir->o = IR_CONV;  /* Pass through loword. */

434. nir->op2 = (IRT_INT << 5) | IRT_INT;

435. hi = split_emit(J, IRT(ir->o == IR_NEG ? IR_BXOR : IR_BAND, IRT_SOFTFP),

436. hisubst[ir->op1], hisubst[ir->op2]);

437. break;

438. case IR_SLOAD:

439. if ((nir->op2 & IRSLOAD_CONVERT)) {  /* Convert from int to number. */

440. nir->op2 &= ~IRSLOAD_CONVERT;

441. ir->prev = nref = split_emit(J, IRTI(IR_CALLN), nref,

442. IRCALL_softfp_i2d);

443. hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref);

444. break;

445. }

446. /* fallthrough */

447. case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:

448. case IR_STRTO:

449. hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref);

450. break;

451. case IR_XLOAD: {

452. IRIns inslo = *nir;  /* Save/undo the emit of the lo XLOAD. */

453. J->cur.nins--;

454. hi = split_ptr(J, oir, ir->op1);  /* Insert the hiref ADD. */

455. nref = lj_ir_nextins(J);

456. nir = IR(nref);

457. *nir = inslo;  /* Re-emit lo XLOAD immediately before hi XLOAD. */

458. hi = split_emit(J, IRT(IR_XLOAD, IRT_SOFTFP), hi, ir->op2);

459. #if LJ_LE

460. ir->prev = nref;

461. #else

462. ir->prev = hi; hi = nref;

463. #endif

464. break;

465. }

466. case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_XSTORE:

467. split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nir->op1, hisubst[ir->op2]);

468. break;

469. case IR_CONV: {  /* Conversion to number. Others handled below. */

470. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);

471. UNUSED(st);

472. #if LJ_32 && LJ_HASFFI

473. if (st == IRT_I64 || st == IRT_U64) {

474. hi = split_call_l(J, hisubst, oir, ir,

475. st == IRT_I64 ? IRCALL_fp64_l2d : IRCALL_fp64_ul2d);

476. break;

477. }

478. #endif

479. lua_assert(st == IRT_INT ||

480. (LJ_32 && LJ_HASFFI && (st == IRT_U32 || st == IRT_FLOAT)));

481. nir->o = IR_CALLN;

482. #if LJ_32 && LJ_HASFFI

483. nir->op2 = st == IRT_INT ? IRCALL_softfp_i2d :

484. st == IRT_FLOAT ? IRCALL_softfp_f2d :

485. IRCALL_softfp_ui2d;

486. #else

487. nir->op2 = IRCALL_softfp_i2d;

488. #endif

489. hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref);

490. break;

491. }

492. case IR_CALLN:

493. case IR_CALLL:

494. case IR_CALLS:

495. case IR_CALLXS:

496. goto split_call;

497. case IR_PHI:

498. if (nir->op1 == nir->op2)

499. J->cur.nins--;  /* Drop useless PHIs. */

500. if (hisubst[ir->op1] != hisubst[ir->op2])

501. split_emit(J, IRT(IR_PHI, IRT_SOFTFP),

502. hisubst[ir->op1], hisubst[ir->op2]);

503. break;

504. case IR_HIOP:

505. J->cur.nins--;  /* Drop joining HIOP. */

506. ir->prev = nir->op1;

507. hi = nir->op2;

508. break;

509. default:

510. lua_assert(ir->o <= IR_NE || ir->o == IR_MIN || ir->o == IR_MAX);

511. hi = split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP),

512. hisubst[ir->op1], hisubst[ir->op2]);

513. break;

514. }

515. } else

516. #endif

517. #if LJ_32 && LJ_HASFFI

518. if (irt_isint64(ir->t)) {

519. IRRef hiref = hisubst[ir->op1];

520. nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD);  /* Turn into INT op. */

521. switch (ir->o) {

522. case IR_ADD:

523. case IR_SUB:

524. /* Use plain op for hiword if loword cannot produce a carry/borrow. */

525. if (irref_isk(nir->op2) && IR(nir->op2)->i == 0) {

526. ir->prev = nir->op1;  /* Pass through loword. */

527. nir->op1 = hiref; nir->op2 = hisubst[ir->op2];

528. hi = nref;

529. break;

530. }

531. /* fallthrough */

532. case IR_NEG:

533. hi = split_emit(J, IRTI(IR_HIOP), hiref, hisubst[ir->op2]);

534. break;

535. case IR_MUL:

536. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_lj_carith_mul64);

537. break;

538. case IR_DIV:

539. hi = split_call_ll(J, hisubst, oir, ir,

540. irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 :

541. IRCALL_lj_carith_divu64);

542. break;

543. case IR_MOD:

544. hi = split_call_ll(J, hisubst, oir, ir,

545. irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 :

546. IRCALL_lj_carith_modu64);

547. break;

548. case IR_POW:

549. hi = split_call_ll(J, hisubst, oir, ir,

550. irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 :

551. IRCALL_lj_carith_powu64);

552. break;

553. case IR_BNOT:

554. hi = split_emit(J, IRTI(IR_BNOT), hiref, 0);

555. break;

556. case IR_BSWAP:

557. ir->prev = split_emit(J, IRTI(IR_BSWAP), hiref, 0);

558. hi = nref;

559. break;

560. case IR_BAND: case IR_BOR: case IR_BXOR:

561. hi = split_bitop(J, hisubst, nir, ir);

562. break;

563. case IR_BSHL: case IR_BSHR: case IR_BSAR: case IR_BROL: case IR_BROR:

564. hi = split_bitshift(J, hisubst, oir, nir, ir);

565. break;

566. case IR_FLOAD:

567. lua_assert(ir->op2 == IRFL_CDATA_INT64);

568. hi = split_emit(J, IRTI(IR_FLOAD), nir->op1, IRFL_CDATA_INT64_4);

569. #if LJ_BE

570. ir->prev = hi; hi = nref;

571. #endif

572. break;

573. case IR_XLOAD:

574. hi = split_emit(J, IRTI(IR_XLOAD), split_ptr(J, oir, ir->op1), ir->op2);

575. #if LJ_BE

576. ir->prev = hi; hi = nref;

577. #endif

578. break;

579. case IR_XSTORE:

580. split_emit(J, IRTI(IR_HIOP), nir->op1, hisubst[ir->op2]);

581. break;

582. case IR_CONV: {  /* Conversion to 64 bit integer. Others handled below. */

583. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);

584. #if LJ_SOFTFP

585. if (st == IRT_NUM) {  /* NUM to 64 bit int conv. */

586. hi = split_call_l(J, hisubst, oir, ir,

587. irt_isi64(ir->t) ? IRCALL_fp64_d2l : IRCALL_fp64_d2ul);

588. } else if (st == IRT_FLOAT) {  /* FLOAT to 64 bit int conv. */

589. nir->o = IR_CALLN;

590. nir->op2 = irt_isi64(ir->t) ? IRCALL_fp64_f2l : IRCALL_fp64_f2ul;

591. hi = split_emit(J, IRTI(IR_HIOP), nref, nref);

592. }

593. #else

594. if (st == IRT_NUM || st == IRT_FLOAT) {  /* FP to 64 bit int conv. */

595. hi = split_emit(J, IRTI(IR_HIOP), nir->op1, nref);

596. }

597. #endif

598. else if (st == IRT_I64 || st == IRT_U64) {  /* 64/64 bit cast. */

599. /* Drop cast, since assembler doesn't care. */

600. goto fwdlo;

601. } else if ((ir->op2 & IRCONV_SEXT)) {  /* Sign-extend to 64 bit. */

602. IRRef k31 = lj_ir_kint(J, 31);

603. nir = IR(nref);  /* May have been reallocated. */

604. ir->prev = nir->op1;  /* Pass through loword. */

605. nir->o = IR_BSAR;  /* hi = bsar(lo, 31). */

606. nir->op2 = k31;

607. hi = nref;

608. } else {  /* Zero-extend to 64 bit. */

609. hi = lj_ir_kint(J, 0);

610. goto fwdlo;

611. }

612. break;

613. }

614. case IR_CALLXS:

615. goto split_call;

616. case IR_PHI: {

617. IRRef hiref2;

618. if ((irref_isk(nir->op1) && irref_isk(nir->op2)) ||

619. nir->op1 == nir->op2)

620. J->cur.nins--;  /* Drop useless PHIs. */

621. hiref2 = hisubst[ir->op2];

622. if (!((irref_isk(hiref) && irref_isk(hiref2)) || hiref == hiref2))

623. split_emit(J, IRTI(IR_PHI), hiref, hiref2);

624. break;

625. }

626. case IR_HIOP:

627. J->cur.nins--;  /* Drop joining HIOP. */

628. ir->prev = nir->op1;

629. hi = nir->op2;

630. break;

631. default:

632. lua_assert(ir->o <= IR_NE);  /* Comparisons. */

633. split_emit(J, IRTGI(IR_HIOP), hiref, hisubst[ir->op2]);

634. break;

635. }

636. } else

637. #endif

638. #if LJ_SOFTFP

639. if (ir->o == IR_SLOAD) {

640. if ((nir->op2 & IRSLOAD_CONVERT)) {  /* Convert from number to int. */

641. nir->op2 &= ~IRSLOAD_CONVERT;

642. if (!(nir->op2 & IRSLOAD_TYPECHECK))

643. nir->t.irt = IRT_INT;  /* Drop guard. */

644. split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref);

645. ir->prev = split_num2int(J, nref, nref+1, irt_isguard(ir->t));

646. }

647. } else if (ir->o == IR_TOBIT) {

648. IRRef tmp, op1 = ir->op1;

649. J->cur.nins--;

650. #if LJ_LE

651. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]);

652. #else

653. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev);

654. #endif

655. ir->prev = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_lj_vm_tobit);

656. } else if (ir->o == IR_TOSTR) {

657. if (hisubst[ir->op1]) {

658. if (irref_isk(ir->op1))

659. nir->op1 = ir->op1;

660. else

661. split_emit(J, IRT(IR_HIOP, IRT_NIL), hisubst[ir->op1], nref);

662. }

663. } else if (ir->o == IR_HREF || ir->o == IR_NEWREF) {

664. if (irref_isk(ir->op2) && hisubst[ir->op2])

665. nir->op2 = ir->op2;

666. } else

667. #endif

668. if (ir->o == IR_CONV) {  /* See above, too. */

669. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);

670. #if LJ_32 && LJ_HASFFI

671. if (st == IRT_I64 || st == IRT_U64) {  /* Conversion from 64 bit int. */

672. #if LJ_SOFTFP

673. if (irt_isfloat(ir->t)) {

674. split_call_l(J, hisubst, oir, ir,

675. st == IRT_I64 ? IRCALL_fp64_l2f : IRCALL_fp64_ul2f);

676. J->cur.nins--;  /* Drop unused HIOP. */

677. }

678. #else

679. if (irt_isfp(ir->t)) {  /* 64 bit integer to FP conversion. */

680. ir->prev = split_emit(J, IRT(IR_HIOP, irt_type(ir->t)),

681. hisubst[ir->op1], nref);

682. }

683. #endif

684. else {  /* Truncate to lower 32 bits. */

685. fwdlo:

686. ir->prev = nir->op1;  /* Forward loword. */

687. /* Replace with NOP to avoid messing up the snapshot logic. */

688. nir->ot = IRT(IR_NOP, IRT_NIL);

689. nir->op1 = nir->op2 = 0;

690. }

691. }

692. #endif

693. #if LJ_SOFTFP && LJ_32 && LJ_HASFFI

694. else if (irt_isfloat(ir->t)) {

695. if (st == IRT_NUM) {

696. split_call_l(J, hisubst, oir, ir, IRCALL_softfp_d2f);

697. J->cur.nins--;  /* Drop unused HIOP. */

698. } else {

699. nir->o = IR_CALLN;

700. nir->op2 = st == IRT_INT ? IRCALL_softfp_i2f : IRCALL_softfp_ui2f;

701. }

702. } else if (st == IRT_FLOAT) {

703. nir->o = IR_CALLN;

704. nir->op2 = irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui;

705. } else

706. #endif

707. #if LJ_SOFTFP

708. if (st == IRT_NUM || (LJ_32 && LJ_HASFFI && st == IRT_FLOAT)) {

709. if (irt_isguard(ir->t)) {

710. lua_assert(st == IRT_NUM && irt_isint(ir->t));

711. J->cur.nins--;

712. ir->prev = split_num2int(J, nir->op1, hisubst[ir->op1], 1);

713. } else {

714. split_call_l(J, hisubst, oir, ir,

715. #if LJ_32 && LJ_HASFFI

716. st == IRT_NUM ?

717. (irt_isint(ir->t) ? IRCALL_softfp_d2i : IRCALL_softfp_d2ui) :

718. (irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui)

719. #else

720. IRCALL_softfp_d2i

721. #endif

722. );

723. J->cur.nins--;  /* Drop unused HIOP. */

724. }

725. }

726. #endif

727. } else if (ir->o == IR_CALLXS) {

728. IRRef hiref;

729. split_call:

730. hiref = hisubst[ir->op1];

731. if (hiref) {

732. IROpT ot = nir->ot;

733. IRRef op2 = nir->op2;

734. nir->ot = IRT(IR_CARG, IRT_NIL);

735. #if LJ_LE

736. nir->op2 = hiref;

737. #else

738. nir->op2 = nir->op1; nir->op1 = hiref;

739. #endif

740. ir->prev = nref = split_emit(J, ot, nref, op2);

741. }

742. if (LJ_SOFTFP ? irt_is64(ir->t) : irt_isint64(ir->t))

743. hi = split_emit(J,

744. IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT),

745. nref, nref);

746. } else if (ir->o == IR_CARG) {

747. IRRef hiref = hisubst[ir->op1];

748. if (hiref) {

749. IRRef op2 = nir->op2;

750. #if LJ_LE

751. nir->op2 = hiref;

752. #else

753. nir->op2 = nir->op1; nir->op1 = hiref;

754. #endif

755. ir->prev = nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2);

756. nir = IR(nref);

757. }

758. hiref = hisubst[ir->op2];

759. if (hiref) {

760. #if !LJ_TARGET_X86

761. int carg = 0;

762. IRIns *cir;

763. for (cir = IR(nir->op1); cir->o == IR_CARG; cir = IR(cir->op1))

764. carg++;

765. if ((carg & 1) == 0) {  /* Align 64 bit arguments. */

766. IRRef op2 = nir->op2;

767. nir->op2 = REF_NIL;

768. nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2);

769. nir = IR(nref);

770. }

771. #endif

772. #if LJ_BE

773. { IRRef tmp = nir->op2; nir->op2 = hiref; hiref = tmp; }

774. #endif

775. ir->prev = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, hiref);

776. }

777. } else if (ir->o == IR_CNEWI) {

778. if (hisubst[ir->op2])

779. split_emit(J, IRT(IR_HIOP, IRT_NIL), nref, hisubst[ir->op2]);

780. } else if (ir->o == IR_LOOP) {

781. J->loopref = nref;  /* Needed by assembler. */

782. }

783. hisubst[ref] = hi;  /* Store hiword substitution. */

784. }

785. if (snref == nins) {  /* Substitution for last snapshot. */

786. snap->ref = J->cur.nins;

787. split_subst_snap(J, snap, oir);

788. }

790. /* Add PHI marks. */

791. for (ref = J->cur.nins-1; ref >= REF_FIRST; ref--) {

792. IRIns *ir = IR(ref);

793. if (ir->o != IR_PHI) break;

794. if (!irref_isk(ir->op1)) irt_setphi(IR(ir->op1)->t);

795. if (ir->op2 > J->loopref) irt_setphi(IR(ir->op2)->t);

796. }

797. }

799. /* Protected callback for split pass. */

800. ‌static TValue *cpsplit(lua_State *L, lua_CFunction dummy, void *ud)

801. {

802. jit_State *J = (jit_State *)ud;

803. split_ir(J);

804. UNUSED(L); UNUSED(dummy);

805. return NULL;

806. }

808. #if defined(LUA_USE_ASSERT) || LJ_SOFTFP

809. /* Slow, but sure way to check whether a SPLIT pass is needed. */

810. ‌static int split_needsplit(jit_State *J)

811. {

812. IRIns *ir, *irend;

813. IRRef ref;

814. for (ir = IR(REF_FIRST), irend = IR(J->cur.nins); ir < irend; ir++)

815. if (LJ_SOFTFP ? irt_is64orfp(ir->t) : irt_isint64(ir->t))

816. return 1;

817. if (LJ_SOFTFP) {

818. for (ref = J->chain[IR_SLOAD]; ref; ref = IR(ref)->prev)

819. if ((IR(ref)->op2 & IRSLOAD_CONVERT))

820. return 1;

821. if (J->chain[IR_TOBIT])

822. return 1;

823. }

824. for (ref = J->chain[IR_CONV]; ref; ref = IR(ref)->prev) {

825. IRType st = (IR(ref)->op2 & IRCONV_SRCMASK);

826. if ((LJ_SOFTFP && (st == IRT_NUM || st == IRT_FLOAT)) ||

827. st == IRT_I64 || st == IRT_U64)

828. return 1;

829. }

830. return 0;  /* Nope. */

831. }

832. #endif

834. /* SPLIT pass. */

835. ‌void lj_opt_split(jit_State *J)

836. {

837. #if LJ_SOFTFP

838. if (!J->needsplit)

839. J->needsplit = split_needsplit(J);

840. #else

841. lua_assert(J->needsplit >= split_needsplit(J));  /* Verify flag. */

842. #endif

843. if (J->needsplit) {

844. int errcode = lj_vm_cpcall(J->L, NULL, J, cpsplit);

845. if (errcode) {

846. /* Completely reset the trace to avoid inconsistent dump on abort. */

847. J->cur.nins = J->cur.nk = REF_BASE;

848. J->cur.nsnap = 0;

849. lj_err_throw(J->L, errcode);  /* Propagate errors. */

850. }

851. }

852. }

854. ‌#undef IR

856. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_parse.h - luajit-2.0-src

Macros defined

- _LJ_PARSE_H

Source code

1. /*

2. ** Lua parser (source code -> bytecode).

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_PARSE_H

7. ‌#define _LJ_PARSE_H

9. #include "lj_obj.h"

10. #include "lj_lex.h"

12. LJ_FUNC GCproto *lj_parse(LexState *ls);

13. LJ_FUNC GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t l);

14. #if LJ_HASFFI

15. LJ_FUNC void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd);

16. #endif

18. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_profile.h - luajit-2.0-src

Macros defined

- _LJ_PROFILE_H

Source code

1. /*

2. ** Low-overhead profiling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_PROFILE_H

7. ‌#define _LJ_PROFILE_H

9. #include "lj_obj.h"

11. #if LJ_HASPROFILE

13. LJ_FUNC void LJ_FASTCALL lj_profile_interpreter(lua_State *L);

14. #if !LJ_PROFILE_SIGPROF

15. LJ_FUNC void LJ_FASTCALL lj_profile_hook_enter(global_State *g);

16. LJ_FUNC void LJ_FASTCALL lj_profile_hook_leave(global_State *g);

17. #endif

19. #endif

21. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_traceerr.h - luajit-2.0-src

Macros defined

- TREDEF

Source code

1. /*

2. ** Trace compiler error messages.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. /* This file may be included multiple times with different TREDEF macros. */

8. /* Recording. */

9. TREDEF(RECERR,        "error thrown or hook called during recording")

10. TREDEF(TRACEUV,        "trace too short")

11. TREDEF(TRACEOV,        "trace too long")

12. TREDEF(STACKOV,        "trace too deep")

13. TREDEF(SNAPOV,        "too many snapshots")

14. TREDEF(BLACKL,        "blacklisted")

15. TREDEF(NYIBC,        "NYI: bytecode %d")

17. /* Recording loop ops. */

18. TREDEF(LLEAVE,        "leaving loop in root trace")

19. TREDEF(LINNER,        "inner loop in root trace")

20. TREDEF(LUNROLL,        "loop unroll limit reached")

22. /* Recording calls/returns. */

23. TREDEF(BADTYPE,        "bad argument type")

24. TREDEF(CJITOFF,        "JIT compilation disabled for function")

25. TREDEF(CUNROLL,        "call unroll limit reached")

26. TREDEF(DOWNREC,        "down-recursion, restarting")

27. TREDEF(NYIFFU,        "NYI: unsupported variant of FastFunc %s")

28. TREDEF(NYIRETL,        "NYI: return to lower frame")

30. /* Recording indexed load/store. */

31. TREDEF(STORENN,        "store with nil or NaN key")

32. TREDEF(NOMM,        "missing metamethod")

33. TREDEF(IDXLOOP,        "looping index lookup")

34. TREDEF(NYITMIX,        "NYI: mixed sparse/dense table")

36. /* Recording C data operations. */

37. TREDEF(NOCACHE,        "symbol not in cache")

38. TREDEF(NYICONV,        "NYI: unsupported C type conversion")

39. TREDEF(NYICALL,        "NYI: unsupported C function type")

41. /* Optimizations. */

42. TREDEF(GFAIL,        "guard would always fail")

43. TREDEF(PHIOV,        "too many PHIs")

44. TREDEF(TYPEINS,        "persistent type instability")

46. /* Assembler. */

47. TREDEF(MCODEAL,        "failed to allocate mcode memory")

48. TREDEF(MCODEOV,        "machine code too long")

49. TREDEF(MCODELM,        "hit mcode limit (retrying)")

50. TREDEF(SPILLOV,        "too many spill slots")

51. TREDEF(BADRA,        "inconsistent register allocation")

52. TREDEF(NYIIR,        "NYI: cannot assemble IR instruction %d")

53. TREDEF(NYIPHI,        "NYI: PHI shuffling too complex")

54. TREDEF(NYICOAL,        "NYI: register coalescing too complex")

56. ‌#undef TREDEF

58. /* Detecting unused error messages:

59. awk -F, '/^TREDEF/ { gsub(/TREDEF./, ""); printf "grep -q LJ_TRERR_%s *.[ch] || echo %s\n", $1, $1}' lj_traceerr.h | sh

60. */

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_udata.h - luajit-2.0-src

Macros defined

- _LJ_UDATA_H

Source code

1. /*

2. ** Userdata handling.

3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

4. */

6. #ifndef _LJ_UDATA_H

7. ‌#define _LJ_UDATA_H

9. #include "lj_obj.h"

11. LJ_FUNC GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env);

12. LJ_FUNC void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud);

14. #endif

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_vm.S - luajit-2.0-src

1. .file "buildvm_x86.dasc"

2. .text

3. .p2align 4

5. .globl lj_vm_asm_begin

6. .hidden lj_vm_asm_begin

7. .type lj_vm_asm_begin, @object

8. .size lj_vm_asm_begin, 0

9. lj_vm_asm_begin:

10. .Lbegin:

12. .globl lj_BC_ISLT

13. .hidden lj_BC_ISLT

14. .type lj_BC_ISLT, @function

15. .size lj_BC_ISLT, 133

16. lj_BC_ISLT:

17. .byte 129,124,202,4,255,255,254,255,117,50,129,124,194,4,255,255

18. .byte 254,255,117,69,139,44,202,131,195,4,59,44,194,125,11,15

19. .byte 183,67,254,141,156,131,0,0,254,255,139,3,15,182,204,15

20. .byte 182,232,131,195,4,193,232,16,65,255,36,238,15,135,130,34

21. .byte 0,0,129,124,194,4,255,255,254,255,114,40,15,133,114,34

22. .byte 0,0,242,15,42,4,194,235,32,15,135,101,34,0,0,242

23. .byte 15,42,12,202,242,15,16,4,194,131,195,4,102,15,46,193

24. .byte 118,184,235,171,242,15,16,4,194,131,195,4,102,15,46,4

25. .byte 202,118,167,235,154

27. .globl lj_BC_ISGE

28. .hidden lj_BC_ISGE

29. .type lj_BC_ISGE, @function

30. .size lj_BC_ISGE, 133

31. lj_BC_ISGE:

32. .byte 129,124,202,4,255,255,254,255,117,50,129,124,194,4,255,255

33. .byte 254,255,117,69,139,44,202,131,195,4,59,44,194,124,11,15

34. .byte 183,67,254,141,156,131,0,0,254,255,139,3,15,182,204,15

35. .byte 182,232,131,195,4,193,232,16,65,255,36,238,15,135,253,33

36. .byte 0,0,129,124,194,4,255,255,254,255,114,40,15,133,237,33

37. .byte 0,0,242,15,42,4,194,235,32,15,135,224,33,0,0,242

38. .byte 15,42,12,202,242,15,16,4,194,131,195,4,102,15,46,193

39. .byte 119,184,235,171,242,15,16,4,194,131,195,4,102,15,46,4

40. .byte 202,119,167,235,154

42. .globl lj_BC_ISLE

43. .hidden lj_BC_ISLE

44. .type lj_BC_ISLE, @function

45. .size lj_BC_ISLE, 133

46. lj_BC_ISLE:

47. .byte 129,124,202,4,255,255,254,255,117,50,129,124,194,4,255,255

48. .byte 254,255,117,69,139,44,202,131,195,4,59,44,194,127,11,15

49. .byte 183,67,254,141,156,131,0,0,254,255,139,3,15,182,204,15

50. .byte 182,232,131,195,4,193,232,16,65,255,36,238,15,135,120,33

51. .byte 0,0,129,124,194,4,255,255,254,255,114,40,15,133,104,33

52. .byte 0,0,242,15,42,4,194,235,32,15,135,91,33,0,0,242

53. .byte 15,42,12,202,242,15,16,4,194,131,195,4,102,15,46,193

54. .byte 114,184,235,171,242,15,16,4,194,131,195,4,102,15,46,4

55. .byte 202,114,167,235,154

57. .globl lj_BC_ISGT

58. .hidden lj_BC_ISGT

59. .type lj_BC_ISGT, @function

60. .size lj_BC_ISGT, 133

61. lj_BC_ISGT:

62. .byte 129,124,202,4,255,255,254,255,117,50,129,124,194,4,255,255

63. .byte 254,255,117,69,139,44,202,131,195,4,59,44,194,126,11,15

64. .byte 183,67,254,141,156,131,0,0,254,255,139,3,15,182,204,15

65. .byte 182,232,131,195,4,193,232,16,65,255,36,238,15,135,243,32

66. .byte 0,0,129,124,194,4,255,255,254,255,114,40,15,133,227,32

67. .byte 0,0,242,15,42,4,194,235,32,15,135,214,32,0,0,242

68. .byte 15,42,12,202,242,15,16,4,194,131,195,4,102,15,46,193

69. .byte 115,184,235,171,242,15,16,4,194,131,195,4,102,15,46,4

70. .byte 202,115,167,235,154

72. .globl lj_BC_ISEQV

73. .hidden lj_BC_ISEQV

74. .type lj_BC_ISEQV, @function

75. .size lj_BC_ISEQV, 195

76. lj_BC_ISEQV:

77. .byte 139,108,194,4,131,195,4,129,253,255,255,254,255,117,47,129

78. .byte 124,202,4,255,255,254,255,117,58,139,44,194,59,44,202,117

79. .byte 11,15,183,67,254,141,156,131,0,0,254,255,139,3,15,182

80. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238,119,60

81. .byte 129,124,202,4,255,255,254,255,114,23,117,48,242,15,42,4

82. .byte 202,235,19,119,39,242,15,42,4,194,102,15,46,4,202,235

83. .byte 10,242,15,16,4,202,102,15,46,4,194,122,13,117,11,15

84. .byte 183,67,254,141,156,131,0,0,254,255,235,176,131,253,245,15

85. .byte 132,163,32,0,0,131,124,202,4,245,15,132,152,32,0,0

86. .byte 57,108,202,4,117,228,131,253,253,115,212,139,12,202,139,4

87. .byte 194,57,193,116,202,131,253,244,119,208,131,253,243,114,203,139

88. .byte 105,16,133,237,116,196,246,69,6,16,117,190,49,237,233,72

89. .byte 32,0,0

91. .globl lj_BC_ISNEV

92. .hidden lj_BC_ISNEV

93. .type lj_BC_ISNEV, @function

94. .size lj_BC_ISNEV, 198

95. lj_BC_ISNEV:

96. .byte 139,108,194,4,131,195,4,129,253,255,255,254,255,117,47,129

97. .byte 124,202,4,255,255,254,255,117,58,139,44,194,59,44,202,116

98. .byte 11,15,183,67,254,141,156,131,0,0,254,255,139,3,15,182

99. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238,119,60

100. .byte 129,124,202,4,255,255,254,255,114,23,117,48,242,15,42,4

101. .byte 202,235,19,119,39,242,15,42,4,194,102,15,46,4,202,235

102. .byte 10,242,15,16,4,202,102,15,46,4,194,122,2,116,11,15

103. .byte 183,67,254,141,156,131,0,0,254,255,235,176,131,253,245,15

104. .byte 132,224,31,0,0,131,124,202,4,245,15,132,213,31,0,0

105. .byte 57,108,202,4,117,217,131,253,253,115,223,139,12,202,139,4

106. .byte 194,57,193,116,213,131,253,244,119,197,131,253,243,114,192,139

107. .byte 105,16,133,237,116,185,246,69,6,16,117,179,189,1,0,0

108. .byte 0,233,130,31,0,0

110. .globl lj_BC_ISEQS

111. .hidden lj_BC_ISEQS

112. .type lj_BC_ISEQS, @function

113. .size lj_BC_ISEQS, 67

114. lj_BC_ISEQS:

115. .byte 72,247,208,139,108,202,4,131,195,4,131,253,251,117,38,139

116. .byte 12,202,65,59,12,135,117,11,15,183,67,254,141,156,131,0

117. .byte 0,254,255,139,3,15,182,204,15,182,232,131,195,4,193,232

118. .byte 16,65,255,36,238,131,253,245,15,133,40,255,255,255,233,92

119. .byte 31,0,0

121. .globl lj_BC_ISNES

122. .hidden lj_BC_ISNES

123. .type lj_BC_ISNES, @function

124. .size lj_BC_ISNES, 63

125. lj_BC_ISNES:

126. .byte 72,247,208,139,108,202,4,131,195,4,131,253,251,117,38,139

127. .byte 12,202,65,59,12,135,116,11,15,183,67,254,141,156,131,0

128. .byte 0,254,255,139,3,15,182,204,15,182,232,131,195,4,193,232

129. .byte 16,65,255,36,238,131,253,245,117,222,233,29,31,0,0

131. .globl lj_BC_ISEQN

132. .hidden lj_BC_ISEQN

133. .type lj_BC_ISEQN, @function

134. .size lj_BC_ISEQN, 136

135. lj_BC_ISEQN:

136. .byte 139,108,202,4,131,195,4,129,253,255,255,254,255,117,49,65

137. .byte 129,124,199,4,255,255,254,255,117,59,65,139,44,199,59,44

138. .byte 202,117,11,15,183,67,254,141,156,131,0,0,254,255,139,3

139. .byte 15,182,204,15,182,232,131,195,4,193,232,16,65,255,36,238

140. .byte 119,60,65,129,124,199,4,255,255,254,255,114,21,242,65,15

141. .byte 42,4,199,235,19,242,15,42,4,202,102,65,15,46,4,199

142. .byte 235,11,242,65,15,16,4,199,102,15,46,4,202,122,13,117

143. .byte 11,15,183,67,254,141,156,131,0,0,254,255,235,176,131,253

144. .byte 245,117,171,233,149,30,0,0

146. .globl lj_BC_ISNEN

147. .hidden lj_BC_ISNEN

148. .type lj_BC_ISNEN, @function

149. .size lj_BC_ISNEN, 136

150. lj_BC_ISNEN:

151. .byte 139,108,202,4,131,195,4,129,253,255,255,254,255,117,49,65

152. .byte 129,124,199,4,255,255,254,255,117,59,65,139,44,199,59,44

153. .byte 202,116,11,15,183,67,254,141,156,131,0,0,254,255,139,3

154. .byte 15,182,204,15,182,232,131,195,4,193,232,16,65,255,36,238

155. .byte 119,60,65,129,124,199,4,255,255,254,255,114,21,242,65,15

156. .byte 42,4,199,235,19,242,15,42,4,202,102,65,15,46,4,199

157. .byte 235,11,242,65,15,16,4,199,102,15,46,4,202,122,2,116

158. .byte 11,15,183,67,254,141,156,131,0,0,254,255,235,176,131,253

159. .byte 245,117,238,233,13,30,0,0

161. .globl lj_BC_ISEQP

162. .hidden lj_BC_ISEQP

163. .type lj_BC_ISEQP, @function

164. .size lj_BC_ISEQP, 53

165. lj_BC_ISEQP:

166. .byte 72,247,208,139,108,202,4,131,195,4,57,197,117,29,15,183

167. .byte 67,254,141,156,131,0,0,254,255,139,3,15,182,204,15,182

168. .byte 232,131,195,4,193,232,16,65,255,36,238,131,253,245,117,233

169. .byte 233,216,29,0,0

171. .globl lj_BC_ISNEP

172. .hidden lj_BC_ISNEP

173. .type lj_BC_ISNEP, @function

174. .size lj_BC_ISNEP, 52

175. lj_BC_ISNEP:

176. .byte 72,247,208,139,108,202,4,131,195,4,57,197,116,20,131,253

177. .byte 245,15,132,193,29,0,0,15,183,67,254,141,156,131,0,0

178. .byte 254,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

179. .byte 65,255,36,238

181. .globl lj_BC_ISTC

182. .hidden lj_BC_ISTC

183. .type lj_BC_ISTC, @function

184. .size lj_BC_ISTC, 51

185. lj_BC_ISTC:

186. .byte 139,108,194,4,131,195,4,131,253,254,115,21,137,108,202,4

187. .byte 139,44,194,137,44,202,15,183,67,254,141,156,131,0,0,254

188. .byte 255,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

189. .byte 255,36,238

191. .globl lj_BC_ISFC

192. .hidden lj_BC_ISFC

193. .type lj_BC_ISFC, @function

194. .size lj_BC_ISFC, 51

195. lj_BC_ISFC:

196. .byte 139,108,194,4,131,195,4,131,253,254,114,21,137,108,202,4

197. .byte 139,44,194,137,44,202,15,183,67,254,141,156,131,0,0,254

198. .byte 255,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

199. .byte 255,36,238

201. .globl lj_BC_IST

202. .hidden lj_BC_IST

203. .type lj_BC_IST, @function

204. .size lj_BC_IST, 41

205. lj_BC_IST:

206. .byte 139,108,194,4,131,195,4,131,253,254,115,11,15,183,67,254

207. .byte 141,156,131,0,0,254,255,139,3,15,182,204,15,182,232,131

208. .byte 195,4,193,232,16,65,255,36,238

210. .globl lj_BC_ISF

211. .hidden lj_BC_ISF

212. .type lj_BC_ISF, @function

213. .size lj_BC_ISF, 41

214. lj_BC_ISF:

215. .byte 139,108,194,4,131,195,4,131,253,254,114,11,15,183,67,254

216. .byte 141,156,131,0,0,254,255,139,3,15,182,204,15,182,232,131

217. .byte 195,4,193,232,16,65,255,36,238

219. .globl lj_BC_ISTYPE

220. .hidden lj_BC_ISTYPE

221. .type lj_BC_ISTYPE, @function

222. .size lj_BC_ISTYPE, 28

223. lj_BC_ISTYPE:

224. .byte 3,68,202,4,15,133,252,28,0,0,139,3,15,182,204,15

225. .byte 182,232,131,195,4,193,232,16,65,255,36,238

227. .globl lj_BC_ISNUM

228. .hidden lj_BC_ISNUM

229. .type lj_BC_ISNUM, @function

230. .size lj_BC_ISNUM, 32

231. lj_BC_ISNUM:

232. .byte 129,124,202,4,255,255,254,255,15,131,220,28,0,0,139,3

233. .byte 15,182,204,15,182,232,131,195,4,193,232,16,65,255,36,238

235. .globl lj_BC_MOV

236. .hidden lj_BC_MOV

237. .type lj_BC_MOV, @function

238. .size lj_BC_MOV, 26

239. lj_BC_MOV:

240. .byte 72,139,44,194,72,137,44,202,139,3,15,182,204,15,182,232

241. .byte 131,195,4,193,232,16,65,255,36,238

243. .globl lj_BC_NOT

244. .hidden lj_BC_NOT

245. .type lj_BC_NOT, @function

246. .size lj_BC_NOT, 32

247. lj_BC_NOT:

248. .byte 49,237,131,124,194,4,254,131,213,253,137,108,202,4,139,3

249. .byte 15,182,204,15,182,232,131,195,4,193,232,16,65,255,36,238

251. .globl lj_BC_UNM

252. .hidden lj_BC_UNM

253. .type lj_BC_UNM, @function

254. .size lj_BC_UNM, 99

255. lj_BC_UNM:

256. .byte 129,124,194,4,255,255,254,255,117,53,139,44,194,247,221,112

257. .byte 29,199,68,202,4,255,255,254,255,137,44,202,139,3,15,182

258. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238,199,68

259. .byte 202,4,0,0,224,65,199,4,202,0,0,0,0,235,221,15

260. .byte 135,128,28,0,0,242,15,16,4,194,72,184,0,0,0,0

261. .byte 0,0,0,128,102,72,15,110,200,15,87,193,242,15,17,4

262. .byte 202,235,185

264. .globl lj_BC_LEN

265. .hidden lj_BC_LEN

266. .type lj_BC_LEN, @function

267. .size lj_BC_LEN, 71

268. lj_BC_LEN:

269. .byte 131,124,194,4,251,117,35,139,4,194,139,64,12,199,68,202

270. .byte 4,255,255,254,255,137,4,202,139,3,15,182,204,15,182,232

271. .byte 131,195,4,193,232,16,65,255,36,238,131,124,194,4,244,15

272. .byte 133,125,28,0,0,139,60,194,137,213

273. call lj_tab_len

274. .byte 137,234,15,182,75,253,235,198

276. .globl lj_BC_ADDVN

277. .hidden lj_BC_ADDVN

278. .type lj_BC_ADDVN, @function

279. .size lj_BC_ADDVN, 77

280. lj_BC_ADDVN:

281. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,133

282. .byte 243,27,0,0,65,129,124,199,4,255,255,254,255,15,133,228

283. .byte 27,0,0,139,44,234,65,3,44,199,15,128,211,27,0,0

284. .byte 199,68,202,4,255,255,254,255,137,44,202,139,3,15,182,204

285. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238

287. .globl lj_BC_SUBVN

288. .hidden lj_BC_SUBVN

289. .type lj_BC_SUBVN, @function

290. .size lj_BC_SUBVN, 77

291. lj_BC_SUBVN:

292. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,133

293. .byte 166,27,0,0,65,129,124,199,4,255,255,254,255,15,133,151

294. .byte 27,0,0,139,44,234,65,43,44,199,15,128,134,27,0,0

295. .byte 199,68,202,4,255,255,254,255,137,44,202,139,3,15,182,204

296. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238

298. .globl lj_BC_MULVN

299. .hidden lj_BC_MULVN

300. .type lj_BC_MULVN, @function

301. .size lj_BC_MULVN, 78

302. lj_BC_MULVN:

303. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,133

304. .byte 89,27,0,0,65,129,124,199,4,255,255,254,255,15,133,74

305. .byte 27,0,0,139,44,234,65,15,175,44,199,15,128,56,27,0

306. .byte 0,199,68,202,4,255,255,254,255,137,44,202,139,3,15,182

307. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238

309. .globl lj_BC_DIVVN

310. .hidden lj_BC_DIVVN

311. .type lj_BC_DIVVN, @function

312. .size lj_BC_DIVVN, 69

313. lj_BC_DIVVN:

314. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

315. .byte 11,27,0,0,65,129,124,199,4,255,255,254,255,15,131,252

316. .byte 26,0,0,242,15,16,4,234,242,65,15,94,4,199,242,15

317. .byte 17,4,202,139,3,15,182,204,15,182,232,131,195,4,193,232

318. .byte 16,65,255,36,238

320. .globl lj_BC_MODVN

321. .hidden lj_BC_MODVN

322. .type lj_BC_MODVN, @function

323. .size lj_BC_MODVN, 74

324. lj_BC_MODVN:

325. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

326. .byte 198,26,0,0,65,129,124,199,4,255,255,254,255,15,131,183

327. .byte 26,0,0,242,15,16,4,234,242,65,15,16,12,199,232,245

328. .byte 50,0,0,242,15,17,4,202,139,3,15,182,204,15,182,232

329. .byte 131,195,4,193,232,16,65,255,36,238

331. .globl lj_BC_ADDNV

332. .hidden lj_BC_ADDNV

333. .type lj_BC_ADDNV, @function

334. .size lj_BC_ADDNV, 77

335. lj_BC_ADDNV:

336. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,133

337. .byte 134,26,0,0,65,129,124,199,4,255,255,254,255,15,133,119

338. .byte 26,0,0,65,139,4,199,3,4,234,15,128,102,26,0,0

339. .byte 199,68,202,4,255,255,254,255,137,4,202,139,3,15,182,204

340. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238

342. .globl lj_BC_SUBNV

343. .hidden lj_BC_SUBNV

344. .type lj_BC_SUBNV, @function

345. .size lj_BC_SUBNV, 77

346. lj_BC_SUBNV:

347. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,133

348. .byte 57,26,0,0,65,129,124,199,4,255,255,254,255,15,133,42

349. .byte 26,0,0,65,139,4,199,43,4,234,15,128,25,26,0,0

350. .byte 199,68,202,4,255,255,254,255,137,4,202,139,3,15,182,204

351. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238

353. .globl lj_BC_MULNV

354. .hidden lj_BC_MULNV

355. .type lj_BC_MULNV, @function

356. .size lj_BC_MULNV, 78

357. lj_BC_MULNV:

358. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,133

359. .byte 236,25,0,0,65,129,124,199,4,255,255,254,255,15,133,221

360. .byte 25,0,0,65,139,4,199,15,175,4,234,15,128,203,25,0

361. .byte 0,199,68,202,4,255,255,254,255,137,4,202,139,3,15,182

362. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238

364. .globl lj_BC_DIVNV

365. .hidden lj_BC_DIVNV

366. .type lj_BC_DIVNV, @function

367. .size lj_BC_DIVNV, 69

368. lj_BC_DIVNV:

369. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

370. .byte 158,25,0,0,65,129,124,199,4,255,255,254,255,15,131,143

371. .byte 25,0,0,242,65,15,16,4,199,242,15,94,4,234,242,15

372. .byte 17,4,202,139,3,15,182,204,15,182,232,131,195,4,193,232

373. .byte 16,65,255,36,238

375. .globl lj_BC_MODNV

376. .hidden lj_BC_MODNV

377. .type lj_BC_MODNV, @function

378. .size lj_BC_MODNV, 51

379. lj_BC_MODNV:

380. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

381. .byte 89,25,0,0,65,129,124,199,4,255,255,254,255,15,131,74

382. .byte 25,0,0,242,65,15,16,4,199,242,15,16,12,234,233,132

383. .byte 254,255,255

385. .globl lj_BC_ADDVV

386. .hidden lj_BC_ADDVV

387. .type lj_BC_ADDVV, @function

388. .size lj_BC_ADDVV, 75

389. lj_BC_ADDVV:

390. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,133

391. .byte 59,25,0,0,129,124,194,4,255,255,254,255,15,133,45,25

392. .byte 0,0,139,44,234,3,44,194,15,128,29,25,0,0,199,68

393. .byte 202,4,255,255,254,255,137,44,202,139,3,15,182,204,15,182

394. .byte 232,131,195,4,193,232,16,65,255,36,238

396. .globl lj_BC_SUBVV

397. .hidden lj_BC_SUBVV

398. .type lj_BC_SUBVV, @function

399. .size lj_BC_SUBVV, 75

400. lj_BC_SUBVV:

401. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,133

402. .byte 240,24,0,0,129,124,194,4,255,255,254,255,15,133,226,24

403. .byte 0,0,139,44,234,43,44,194,15,128,210,24,0,0,199,68

404. .byte 202,4,255,255,254,255,137,44,202,139,3,15,182,204,15,182

405. .byte 232,131,195,4,193,232,16,65,255,36,238

407. .globl lj_BC_MULVV

408. .hidden lj_BC_MULVV

409. .type lj_BC_MULVV, @function

410. .size lj_BC_MULVV, 76

411. lj_BC_MULVV:

412. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,133

413. .byte 165,24,0,0,129,124,194,4,255,255,254,255,15,133,151,24

414. .byte 0,0,139,44,234,15,175,44,194,15,128,134,24,0,0,199

415. .byte 68,202,4,255,255,254,255,137,44,202,139,3,15,182,204,15

416. .byte 182,232,131,195,4,193,232,16,65,255,36,238

418. .globl lj_BC_DIVVV

419. .hidden lj_BC_DIVVV

420. .type lj_BC_DIVVV, @function

421. .size lj_BC_DIVVV, 67

422. lj_BC_DIVVV:

423. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

424. .byte 89,24,0,0,129,124,194,4,255,255,254,255,15,131,75,24

425. .byte 0,0,242,15,16,4,234,242,15,94,4,194,242,15,17,4

426. .byte 202,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

427. .byte 255,36,238

429. .globl lj_BC_MODVV

430. .hidden lj_BC_MODVV

431. .type lj_BC_MODVV, @function

432. .size lj_BC_MODVV, 49

433. lj_BC_MODVV:

434. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

435. .byte 22,24,0,0,129,124,194,4,255,255,254,255,15,131,8,24

436. .byte 0,0,242,15,16,4,234,242,15,16,12,194,233,46,253,255

437. .byte 255

439. .globl lj_BC_POW

440. .hidden lj_BC_POW

441. .type lj_BC_POW, @function

442. .size lj_BC_POW, 80

443. lj_BC_POW:

444. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

445. .byte 229,23,0,0,129,124,194,4,255,255,254,255,15,131,215,23

446. .byte 0,0,242,15,16,4,234,242,15,16,12,194,137,213

447. call pow@PLT

448. .byte 15,182,75,253,137,234,242,15,17,4,202,139,3,15,182,204

449. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238

451. .globl lj_BC_CAT

452. .hidden lj_BC_CAT

453. .type lj_BC_CAT, @function

454. .size lj_BC_CAT, 76

455. lj_BC_CAT:

456. .byte 15,182,236,15,182,192,139,124,36,24,137,87,16,141,52,194

457. .byte 137,194,41,234,137,253,137,92,36,28

458. call lj_meta_cat

459. .byte 139,85,16,133,192,15,133,176,23,0,0,15,182,107,255,15

460. .byte 182,75,253,72,139,4,234,72,137,4,202,139,3,15,182,204

461. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238

463. .globl lj_BC_KSTR

464. .hidden lj_BC_KSTR

465. .type lj_BC_KSTR, @function

466. .size lj_BC_KSTR, 36

467. lj_BC_KSTR:

468. .byte 72,247,208,65,139,4,135,199,68,202,4,251,255,255,255,137

469. .byte 4,202,139,3,15,182,204,15,182,232,131,195,4,193,232,16

470. .byte 65,255,36,238

472. .globl lj_BC_KCDATA

473. .hidden lj_BC_KCDATA

474. .type lj_BC_KCDATA, @function

475. .size lj_BC_KCDATA, 36

476. lj_BC_KCDATA:

477. .byte 72,247,208,65,139,4,135,199,68,202,4,245,255,255,255,137

478. .byte 4,202,139,3,15,182,204,15,182,232,131,195,4,193,232,16

479. .byte 65,255,36,238

481. .globl lj_BC_KSHORT

482. .hidden lj_BC_KSHORT

483. .type lj_BC_KSHORT, @function

484. .size lj_BC_KSHORT, 32

485. lj_BC_KSHORT:

486. .byte 15,191,192,199,68,202,4,255,255,254,255,137,4,202,139,3

487. .byte 15,182,204,15,182,232,131,195,4,193,232,16,65,255,36,238

489. .globl lj_BC_KNUM

490. .hidden lj_BC_KNUM

491. .type lj_BC_KNUM, @function

492. .size lj_BC_KNUM, 29

493. lj_BC_KNUM:

494. .byte 242,65,15,16,4,199,242,15,17,4,202,139,3,15,182,204

495. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238

497. .globl lj_BC_KPRI

498. .hidden lj_BC_KPRI

499. .type lj_BC_KPRI, @function

500. .size lj_BC_KPRI, 25

501. lj_BC_KPRI:

502. .byte 72,247,208,137,68,202,4,139,3,15,182,204,15,182,232,131

503. .byte 195,4,193,232,16,65,255,36,238

505. .globl lj_BC_KNIL

506. .hidden lj_BC_KNIL

507. .type lj_BC_KNIL, @function

508. .size lj_BC_KNIL, 43

509. lj_BC_KNIL:

510. .byte 141,76,202,12,141,68,194,4,189,255,255,255,255,137,105,248

511. .byte 137,41,131,193,8,57,193,118,247,139,3,15,182,204,15,182

512. .byte 232,131,195,4,193,232,16,65,255,36,238

514. .globl lj_BC_UGET

515. .hidden lj_BC_UGET

516. .type lj_BC_UGET, @function

517. .size lj_BC_UGET, 36

518. lj_BC_UGET:

519. .byte 139,106,248,139,108,133,20,139,109,16,72,139,69,0,72,137

520. .byte 4,202,139,3,15,182,204,15,182,232,131,195,4,193,232,16

521. .byte 65,255,36,238

523. .globl lj_BC_USETV

524. .hidden lj_BC_USETV

525. .type lj_BC_USETV, @function

526. .size lj_BC_USETV, 87

527. lj_BC_USETV:

528. .byte 139,106,248,139,108,141,20,128,125,6,0,139,109,16,139,12

529. .byte 194,139,68,194,4,137,77,0,137,69,4,116,6,246,69,252

530. .byte 4,117,18,139,3,15,182,204,15,182,232,131,195,4,193,232

531. .byte 16,65,255,36,238,131,232,252,131,248,246,118,230,246,65,4

532. .byte 3,116,224,137,238,137,213,65,141,190,32,244,255,255

533. call lj_gc_barrieruv

534. .byte 137,234,235,204

536. .globl lj_BC_USETS

537. .hidden lj_BC_USETS

538. .type lj_BC_USETS, @function

539. .size lj_BC_USETS, 82

540. lj_BC_USETS:

541. .byte 72,247,208,139,106,248,139,108,141,20,65,139,12,135,139,69

542. .byte 16,137,8,199,64,4,251,255,255,255,246,69,4,4,117,18

543. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

544. .byte 36,238,246,65,4,3,116,232,128,125,6,0,116,226,137,213

545. .byte 137,198,65,141,190,32,244,255,255

546. call lj_gc_barrieruv

547. .byte 137,234,235,206

549. .globl lj_BC_USETN

550. .hidden lj_BC_USETN

551. .type lj_BC_USETN, @function

552. .size lj_BC_USETN, 38

553. lj_BC_USETN:

554. .byte 139,106,248,242,65,15,16,4,199,139,108,141,20,139,77,16

555. .byte 242,15,17,1,139,3,15,182,204,15,182,232,131,195,4,193

556. .byte 232,16,65,255,36,238

558. .globl lj_BC_USETP

559. .hidden lj_BC_USETP

560. .type lj_BC_USETP, @function

561. .size lj_BC_USETP, 34

562. lj_BC_USETP:

563. .byte 72,247,208,139,106,248,139,108,141,20,139,77,16,137,65,4

564. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

565. .byte 36,238

567. .globl lj_BC_UCLO

568. .hidden lj_BC_UCLO

569. .type lj_BC_UCLO, @function

570. .size lj_BC_UCLO, 51

571. lj_BC_UCLO:

572. .byte 141,156,131,0,0,254,255,139,108,36,24,131,125,40,0,116

573. .byte 16,137,85,16,141,52,202,137,239

574. call lj_func_closeuv

575. .byte 139,85,16,139,3,15,182,204,15,182,232,131,195,4,193,232

576. .byte 16,65,255,36,238

578. .globl lj_BC_FNEW

579. .hidden lj_BC_FNEW

580. .type lj_BC_FNEW, @function

581. .size lj_BC_FNEW, 64

582. lj_BC_FNEW:

583. .byte 72,247,208,139,108,36,24,137,85,16,139,82,248,65,139,52

584. .byte 135,137,239,137,92,36,28

585. call lj_func_newL_gc

586. .byte 139,85,16,15,182,75,253,137,4,202,199,68,202,4,247,255

587. .byte 255,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

588. .byte 65,255,36,238

590. .globl lj_BC_TNEW

591. .hidden lj_BC_TNEW

592. .type lj_BC_TNEW, @function

593. .size lj_BC_TNEW, 109

594. lj_BC_TNEW:

595. .byte 139,108,36,24,137,85,16,65,139,142,64,244,255,255,65,59

596. .byte 142,68,244,255,255,137,92,36,28,115,69,137,194,37,255,7

597. .byte 0,0,193,234,11,61,255,7,0,0,116,45,137,239,137,198

598. call lj_tab_new

599. .byte 139,85,16,15,182,75,253,137,4,202,199,68,202,4,244,255

600. .byte 255,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

601. .byte 65,255,36,238,184,1,8,0,0,235,204,137,239

602. call lj_gc_step_fixtop

603. .byte 15,183,67,254,235,174

605. .globl lj_BC_TDUP

606. .hidden lj_BC_TDUP

607. .type lj_BC_TDUP, @function

608. .size lj_BC_TDUP, 93

609. lj_BC_TDUP:

610. .byte 72,247,208,139,108,36,24,65,139,142,64,244,255,255,137,92

611. .byte 36,28,65,59,142,68,244,255,255,137,85,16,115,47,65,139

612. .byte 52,135,137,239

613. call lj_tab_dup

614. .byte 139,85,16,15,182,75,253,137,4,202,199,68,202,4,244,255

615. .byte 255,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

616. .byte 65,255,36,238,137,239

617. call lj_gc_step_fixtop

618. .byte 15,183,67,254,72,247,208,235,193

620. .globl lj_BC_GGET

621. .hidden lj_BC_GGET

622. .type lj_BC_GGET, @function

623. .size lj_BC_GGET, 18

624. lj_BC_GGET:

625. .byte 72,247,208,139,106,248,139,109,8,65,139,4,135,233,173,0

626. .byte 0,0

628. .globl lj_BC_GSET

629. .hidden lj_BC_GSET

630. .type lj_BC_GSET, @function

631. .size lj_BC_GSET, 18

632. lj_BC_GSET:

633. .byte 72,247,208,139,106,248,139,109,8,65,139,4,135,233,71,2

634. .byte 0,0

636. .globl lj_BC_TGETV

637. .hidden lj_BC_TGETV

638. .type lj_BC_TGETV, @function

639. .size lj_BC_TGETV, 128

640. lj_BC_TGETV:

641. .byte 15,182,236,15,182,192,131,124,234,4,244,15,133,232,17,0

642. .byte 0,139,44,234,129,124,194,4,255,255,254,255,117,82,139,4

643. .byte 194,59,69,24,15,131,207,17,0,0,193,224,3,3,69,8

644. .byte 131,120,4,255,116,25,72,139,40,72,137,44,202,139,3,15

645. .byte 182,204,15,182,232,131,195,4,193,232,16,65,255,36,238,131

646. .byte 125,16,0,116,17,139,77,16,246,65,6,1,15,132,151,17

647. .byte 0,0,15,182,75,253,199,68,202,4,255,255,255,255,235,205

648. .byte 131,124,194,4,251,15,133,126,17,0,0,139,4,194,235,27

650. .globl lj_BC_TGETS

651. .hidden lj_BC_TGETS

652. .type lj_BC_TGETS, @function

653. .size lj_BC_TGETS, 124

654. lj_BC_TGETS:

655. .byte 15,182,236,15,182,192,72,247,208,65,139,4,135,131,124,234

656. .byte 4,244,15,133,35,17,0,0,139,44,234,139,77,28,35,72

657. .byte 8,107,201,24,3,77,20,131,121,12,251,117,54,57,65,8

658. .byte 117,49,131,121,4,255,116,50,15,182,67,253,72,139,41,72

659. .byte 137,44,194,139,3,15,182,204,15,182,232,131,195,4,193,232

660. .byte 16,65,255,36,238,15,182,67,253,199,68,194,4,255,255,255

661. .byte 255,235,224,139,73,16,133,201,117,189,139,77,16,133,201,116

662. .byte 228,246,65,6,1,117,222,233,191,16,0,0

664. .globl lj_BC_TGETB

665. .hidden lj_BC_TGETB

666. .type lj_BC_TGETB, @function

667. .size lj_BC_TGETB, 99

668. lj_BC_TGETB:

669. .byte 15,182,236,15,182,192,131,124,234,4,244,15,133,215,16,0

670. .byte 0,139,44,234,59,69,24,15,131,203,16,0,0,193,224,3

671. .byte 3,69,8,131,120,4,255,116,25,72,139,40,72,137,44,202

672. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

673. .byte 36,238,131,125,16,0,116,17,139,77,16,246,65,6,1,15

674. .byte 132,147,16,0,0,15,182,75,253,199,68,202,4,255,255,255

675. .byte 255,235,205

677. .globl lj_BC_TGETR

678. .hidden lj_BC_TGETR

679. .type lj_BC_TGETR, @function

680. .size lj_BC_TGETR, 52

681. lj_BC_TGETR:

682. .byte 15,182,236,15,182,192,139,44,234,139,4,194,59,69,24,15

683. .byte 131,230,16,0,0,193,224,3,3,69,8,72,139,40,72,137

684. .byte 44,202,139,3,15,182,204,15,182,232,131,195,4,193,232,16

685. .byte 65,255,36,238

687. .globl lj_BC_TSETV

688. .hidden lj_BC_TSETV

689. .type lj_BC_TSETV, @function

690. .size lj_BC_TSETV, 153

691. lj_BC_TSETV:

692. .byte 15,182,236,15,182,192,131,124,234,4,244,15,133,26,17,0

693. .byte 0,139,44,234,129,124,194,4,255,255,254,255,117,80,139,4

694. .byte 194,59,69,24,15,131,1,17,0,0,193,224,3,3,69,8

695. .byte 131,120,4,255,116,31,246,69,4,4,117,66,72,139,44,202

696. .byte 72,137,40,139,3,15,182,204,15,182,232,131,195,4,193,232

697. .byte 16,65,255,36,238,131,125,16,0,116,219,139,77,16,246,65

698. .byte 6,2,15,132,195,16,0,0,15,182,75,253,235,200,131,124

699. .byte 194,4,251,15,133,178,16,0,0,139,4,194,235,54,128,101

700. .byte 4,251,65,139,142,92,244,255,255,65,137,174,92,244,255,255

701. .byte 137,77,12,15,182,75,253,235,163

703. .globl lj_BC_TSETS

704. .hidden lj_BC_TSETS

705. .type lj_BC_TSETS, @function

706. .size lj_BC_TSETS, 229

707. lj_BC_TSETS:

708. .byte 15,182,236,15,182,192,72,247,208,65,139,4,135,131,124,234

709. .byte 4,244,15,133,60,16,0,0,139,44,234,139,77,28,35,72

710. .byte 8,107,201,24,198,69,6,0,3,77,20,131,121,12,251,117

711. .byte 77,57,65,8,117,72,131,121,4,255,116,39,246,69,4,4

712. .byte 15,133,133,0,0,0,15,182,67,253,72,139,44,194,72,137

713. .byte 41,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

714. .byte 255,36,238,131,125,16,0,116,211,137,12,36,139,77,16,246

715. .byte 65,6,2,15,132,219,15,0,0,139,12,36,235,190,139,73

716. .byte 16,133,201,117,166,139,77,16,133,201,116,10,246,65,6,2

717. .byte 15,132,190,15,0,0,137,4,36,199,68,36,4,251,255,255

718. .byte 255,137,108,36,8,139,124,36,24,137,87,16,72,141,20,36

719. .byte 137,238,137,253,137,92,36,28

720. call lj_tab_newkey

721. .byte 139,85,16,139,108,36,8,137,193,233,113,255,255,255,128,101

722. .byte 4,251,65,139,134,92,244,255,255,65,137,174,92,244,255,255

723. .byte 137,69,12,233,97,255,255,255

725. .globl lj_BC_TSETB

726. .hidden lj_BC_TSETB

727. .type lj_BC_TSETB, @function

728. .size lj_BC_TSETB, 124

729. lj_BC_TSETB:

730. .byte 15,182,236,15,182,192,131,124,234,4,244,15,133,135,15,0

731. .byte 0,139,44,234,59,69,24,15,131,123,15,0,0,193,224,3

732. .byte 3,69,8,131,120,4,255,116,31,246,69,4,4,117,50,72

733. .byte 139,12,202,72,137,8,139,3,15,182,204,15,182,232,131,195

734. .byte 4,193,232,16,65,255,36,238,131,125,16,0,116,219,139,77

735. .byte 16,246,65,6,2,15,132,61,15,0,0,15,182,75,253,235

736. .byte 200,128,101,4,251,65,139,142,92,244,255,255,65,137,174,92

737. .byte 244,255,255,137,77,12,15,182,75,253,235,179

739. .globl lj_BC_TSETM

740. .hidden lj_BC_TSETM

741. .type lj_BC_TSETM, @function

742. .size lj_BC_TSETM, 142

743. lj_BC_TSETM:

744. .byte 68,137,60,36,69,139,60,199,141,12,202,139,105,248,246,69

745. .byte 4,4,117,99,139,68,36,4,131,232,1,116,37,68,1,248

746. .byte 59,69,24,119,51,68,41,248,65,193,231,3,68,3,125,8

747. .byte 72,139,41,131,193,8,73,137,47,65,131,199,8,131,232,1

748. .byte 117,238,68,139,60,36,139,3,15,182,204,15,182,232,131,195

749. .byte 4,193,232,16,65,255,36,238,139,124,36,24,137,87,16,137

750. .byte 238,137,194,137,253,137,92,36,28

751. call lj_tab_reasize

752. .byte 139,85,16,15,182,75,253,235,145,128,101,4,251,65,139,134

753. .byte 92,244,255,255,65,137,174,92,244,255,255,137,69,12,235,134

755. .globl lj_BC_TSETR

756. .hidden lj_BC_TSETR

757. .type lj_BC_TSETR, @function

758. .size lj_BC_TSETR, 85

759. lj_BC_TSETR:

760. .byte 15,182,236,15,182,192,139,44,234,139,4,194,246,69,4,4

761. .byte 117,40,59,69,24,15,131,245,14,0,0,193,224,3,3,69

762. .byte 8,72,139,44,202,72,137,40,139,3,15,182,204,15,182,232

763. .byte 131,195,4,193,232,16,65,255,36,238,128,101,4,251,65,139

764. .byte 142,92,244,255,255,65,137,174,92,244,255,255,137,77,12,15

765. .byte 182,75,253,235,189

767. .globl lj_BC_CALLM

768. .hidden lj_BC_CALLM

769. .type lj_BC_CALLM, @function

770. .size lj_BC_CALLM, 46

771. lj_BC_CALLM:

772. .byte 15,182,192,3,68,36,4,131,124,202,4,247,139,44,202,15

773. .byte 133,248,15,0,0,141,84,202,8,137,90,252,139,93,16,139

774. .byte 11,15,182,233,15,182,205,131,195,4,65,255,36,238

776. .globl lj_BC_CALL

777. .hidden lj_BC_CALL

778. .type lj_BC_CALL, @function

779. .size lj_BC_CALL, 42

780. lj_BC_CALL:

781. .byte 15,182,192,131,124,202,4,247,139,44,202,15,133,206,15,0

782. .byte 0,141,84,202,8,137,90,252,139,93,16,139,11,15,182,233

783. .byte 15,182,205,131,195,4,65,255,36,238

785. .globl lj_BC_CALLMT

786. .hidden lj_BC_CALLMT

787. .type lj_BC_CALLMT, @function

788. .size lj_BC_CALLMT, 4

789. lj_BC_CALLMT:

790. .byte 3,68,36,4

792. .globl lj_BC_CALLT

793. .hidden lj_BC_CALLT

794. .type lj_BC_CALLT, @function

795. .size lj_BC_CALLT, 148

796. lj_BC_CALLT:

797. .byte 141,76,202,8,65,137,215,139,105,248,131,121,252,247,15,133

798. .byte 161,15,0,0,139,90,252,247,195,3,0,0,0,117,91,137

799. .byte 106,248,137,68,36,4,131,232,1,116,21,72,139,41,131,193

800. .byte 8,73,137,47,65,131,199,8,131,232,1,117,238,139,106,248

801. .byte 139,68,36,4,128,125,6,1,119,18,139,93,16,139,11,15

802. .byte 182,233,15,182,205,131,195,4,65,255,36,238,247,195,3,0

803. .byte 0,0,117,230,15,182,75,253,72,247,209,68,139,124,202,248

804. .byte 69,139,127,16,69,139,127,208,235,208,131,235,3,247,195,7

805. .byte 0,0,0,117,10,41,218,65,137,215,139,90,252,235,144,131

806. .byte 195,3,235,139

808. .globl lj_BC_ITERC

809. .hidden lj_BC_ITERC

810. .type lj_BC_ITERC, @function

811. .size lj_BC_ITERC, 68

812. lj_BC_ITERC:

813. .byte 141,76,202,8,72,139,105,232,72,139,65,240,72,137,41,72

814. .byte 137,65,8,139,105,224,139,65,228,137,105,248,137,65,252,131

815. .byte 248,247,184,3,0,0,0,15,133,244,14,0,0,137,202,137

816. .byte 90,252,139,93,16,139,11,15,182,233,15,182,205,131,195,4

817. .byte 65,255,36,238

819. .globl lj_BC_ITERN

820. .hidden lj_BC_ITERN

821. .type lj_BC_ITERN, @function

822. .size lj_BC_ITERN, 167

823. lj_BC_ITERN:

824. .byte 68,137,60,36,68,137,116,36,4,139,108,202,240,139,68,202

825. .byte 248,68,139,117,24,131,195,4,68,139,125,8,68,57,240,115

826. .byte 78,65,131,124,199,4,255,116,65,199,68,202,4,255,255,254

827. .byte 255,137,4,202,73,139,44,199,72,137,108,202,8,131,192,1

828. .byte 137,68,202,248,15,183,67,254,141,156,131,0,0,254,255,68

829. .byte 139,116,36,4,68,139,60,36,139,3,15,182,204,15,182,232

830. .byte 131,195,4,193,232,16,65,255,36,238,131,192,1,235,173,68

831. .byte 41,240,59,69,28,119,216,68,107,248,24,68,3,125,20,65

832. .byte 131,127,4,255,116,28,70,141,116,48,1,73,139,111,8,73

833. .byte 139,7,72,137,44,202,72,137,68,202,8,68,137,116,202,248

834. .byte 235,162,131,192,1,235,203

836. .globl lj_BC_VARG

837. .hidden lj_BC_VARG

838. .type lj_BC_VARG, @function

839. .size lj_BC_VARG, 191

840. lj_BC_VARG:

841. .byte 15,182,236,15,182,192,68,137,60,36,68,141,124,194,11,141

842. .byte 12,202,68,43,122,252,133,237,116,68,141,108,233,248,65,57

843. .byte 215,115,23,73,139,71,248,65,131,199,8,72,137,1,131,193

844. .byte 8,57,233,115,19,65,57,215,114,233,199,65,4,255,255,255

845. .byte 255,131,193,8,57,233,114,242,68,139,60,36,139,3,15,182

846. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238,199,68

847. .byte 36,4,1,0,0,0,137,208,68,41,248,118,219,137,197,193

848. .byte 237,3,131,197,1,137,108,36,4,139,108,36,24,1,200,59

849. .byte 69,32,119,21,73,139,71,248,65,131,199,8,72,137,1,131

850. .byte 193,8,65,57,215,114,237,235,175,137,85,16,137,77,24,137

851. .byte 92,36,28,65,41,215,139,116,36,4,131,238,1,137,239

852. call lj_state_growstack

853. .byte 139,85,16,139,77,24,65,1,215,235,197

855. .globl lj_BC_ISNEXT

856. .hidden lj_BC_ISNEXT

857. .type lj_BC_ISNEXT, @function

858. .size lj_BC_ISNEXT, 88

859. lj_BC_ISNEXT:

860. .byte 131,124,202,236,247,117,65,139,108,202,232,131,124,202,244,244

861. .byte 117,54,131,124,202,252,255,117,47,128,125,6,4,117,41,141

862. .byte 156,131,0,0,254,255,199,68,202,248,0,0,0,0,199,68

863. .byte 202,252,255,127,254,255,139,3,15,182,204,15,182,232,131,195

864. .byte 4,193,232,16,65,255,36,238,198,67,252,88,141,156,131,0

865. .byte 0,254,255,198,3,69,235,222

867. .globl lj_BC_RETM

868. .hidden lj_BC_RETM

869. .type lj_BC_RETM, @function

870. .size lj_BC_RETM, 4

871. lj_BC_RETM:

872. .byte 3,68,36,4

874. .globl lj_BC_RET

875. .hidden lj_BC_RET

876. .type lj_BC_RET, @function

877. .size lj_BC_RET, 136

878. lj_BC_RET:

879. .byte 193,225,3,139,90,252,137,68,36,4,247,195,3,0,0,0

880. .byte 117,94,65,137,215,131,232,1,116,17,73,139,44,15,73,137

881. .byte 111,248,65,131,199,8,131,232,1,117,239,139,68,36,4,15

882. .byte 182,107,255,57,197,119,40,15,182,75,253,72,247,209,141,20

883. .byte 202,68,139,122,248,69,139,127,16,69,139,127,208,139,3,15

884. .byte 182,204,15,182,232,131,195,4,193,232,16,65,255,36,238,65

885. .byte 199,71,252,255,255,255,255,65,131,199,8,131,192,1,235,195

886. .byte 141,107,253,247,197,7,0,0,0,15,133,151,6,0,0,41

887. .byte 234,1,233,233,123,255,255,255

889. .globl lj_BC_RET0

890. .hidden lj_BC_RET0

891. .type lj_BC_RET0, @function

892. .size lj_BC_RET0, 92

893. lj_BC_RET0:

894. .byte 139,90,252,137,68,36,4,247,195,3,0,0,0,117,58,56

895. .byte 67,255,119,40,15,182,75,253,72,247,209,141,20,202,68,139

896. .byte 122,248,69,139,127,16,69,139,127,208,139,3,15,182,204,15

897. .byte 182,232,131,195,4,193,232,16,65,255,36,238,199,68,194,244

898. .byte 255,255,255,255,131,192,1,235,198,141,107,253,247,197,7,0

899. .byte 0,0,15,133,54,6,0,0,41,234,235,164

901. .globl lj_BC_RET1

902. .hidden lj_BC_RET1

903. .type lj_BC_RET1, @function

904. .size lj_BC_RET1, 105

905. lj_BC_RET1:

906. .byte 193,225,3,139,90,252,137,68,36,4,247,195,3,0,0,0

907. .byte 117,66,72,139,44,10,72,137,106,248,56,67,255,119,40,15

908. .byte 182,75,253,72,247,209,141,20,202,68,139,122,248,69,139,127

909. .byte 16,69,139,127,208,139,3,15,182,204,15,182,232,131,195,4

910. .byte 193,232,16,65,255,36,238,199,68,194,244,255,255,255,255,131

911. .byte 192,1,235,198,141,107,253,247,197,7,0,0,0,15,133,207

912. .byte 5,0,0,41,234,1,233,235,154

914. .globl lj_BC_FORI

915. .hidden lj_BC_FORI

916. .type lj_BC_FORI, @function

917. .size lj_BC_FORI, 167

918. lj_BC_FORI:

919. .byte 141,12,202,129,121,4,255,255,254,255,117,91,129,121,12,255

920. .byte 255,254,255,15,133,6,12,0,0,129,121,20,255,255,254,255

921. .byte 15,133,249,11,0,0,139,41,131,121,16,0,124,40,59,105

922. .byte 8,199,65,28,255,255,254,255,137,105,24,126,7,141,156,131

923. .byte 0,0,254,255,139,3,15,182,204,15,182,232,131,195,4,193

924. .byte 232,16,65,255,36,238,59,105,8,199,65,28,255,255,254,255

925. .byte 137,105,24,125,223,235,214,15,131,178,11,0,0,129,121,12

926. .byte 255,255,254,255,15,131,165,11,0,0,139,105,20,129,253,255

927. .byte 255,254,255,15,131,150,11,0,0,242,15,16,1,242,15,16

928. .byte 73,8,124,13,102,15,46,200,242,15,17,65,24,115,165,235

929. .byte 156,102,15,46,193,235,241

931. .globl lj_BC_JFORI

932. .hidden lj_BC_JFORI

933. .type lj_BC_JFORI, @function

934. .size lj_BC_JFORI, 205

935. lj_BC_JFORI:

936. .byte 141,12,202,129,121,4,255,255,254,255,117,114,129,121,12,255

937. .byte 255,254,255,15,133,95,11,0,0,129,121,20,255,255,254,255

938. .byte 15,133,82,11,0,0,139,41,131,121,16,0,124,48,59,105

939. .byte 8,199,65,28,255,255,254,255,137,105,24,141,156,131,0,0

940. .byte 254,255,15,183,67,254,15,142,151,2,0,0,139,3,15,182

941. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238,59,105

942. .byte 8,199,65,28,255,255,254,255,137,105,24,141,156,131,0,0

943. .byte 254,255,15,183,67,254,15,141,103,2,0,0,235,206,15,131

944. .byte 244,10,0,0,129,121,12,255,255,254,255,15,131,231,10,0

945. .byte 0,139,105,20,129,253,255,255,254,255,15,131,216,10,0,0

946. .byte 242,15,16,1,242,15,16,73,8,124,28,102,15,46,200,242

947. .byte 15,17,65,24,141,156,131,0,0,254,255,15,183,67,254,15

948. .byte 131,30,2,0,0,235,133,102,15,46,193,235,226

950. .globl lj_BC_FORL

951. .hidden lj_BC_FORL

952. .type lj_BC_FORL, @function

953. .size lj_BC_FORL, 20

954. lj_BC_FORL:

955. .byte 137,221,209,237,131,229,126,102,65,131,108,46,128,2,15,130

956. .byte 66,30,0,0

958. .globl lj_BC_IFORL

959. .hidden lj_BC_IFORL

960. .type lj_BC_IFORL, @function

961. .size lj_BC_IFORL, 184

962. lj_BC_IFORL:

963. .byte 141,12,202,129,121,4,255,255,254,255,117,102,129,121,12,255

964. .byte 255,254,255,15,133,227,34,0,0,129,121,20,255,255,254,255

965. .byte 15,133,214,34,0,0,139,105,16,133,237,120,46,3,41,112

966. .byte 24,137,41,59,105,8,199,65,28,255,255,254,255,137,105,24

967. .byte 127,7,141,156,131,0,0,254,255,139,3,15,182,204,15,182

968. .byte 232,131,195,4,193,232,16,65,255,36,238,3,41,112,234,137

969. .byte 41,59,105,8,199,65,28,255,255,254,255,137,105,24,124,217

970. .byte 235,208,129,121,12,255,255,254,255,15,131,125,34,0,0,129

971. .byte 121,20,255,255,254,255,15,131,112,34,0,0,139,105,20,242

972. .byte 15,16,1,242,15,16,73,8,242,15,88,65,16,242,15,17

973. .byte 1,133,237,120,13,102,15,46,200,242,15,17,65,24,114,153

974. .byte 235,144,102,15,46,193,235,241

976. .globl lj_BC_JFORL

977. .hidden lj_BC_JFORL

978. .type lj_BC_JFORL, @function

979. .size lj_BC_JFORL, 189

980. lj_BC_JFORL:

981. .byte 141,12,202,129,121,4,255,255,254,255,117,103,129,121,12,255

982. .byte 255,254,255,15,133,43,34,0,0,129,121,20,255,255,254,255

983. .byte 15,133,30,34,0,0,139,105,16,133,237,120,43,3,41,112

984. .byte 21,137,41,59,105,8,199,65,28,255,255,254,255,137,105,24

985. .byte 15,142,4,1,0,0,139,3,15,182,204,15,182,232,131,195

986. .byte 4,193,232,16,65,255,36,238,3,41,112,234,137,41,59,105

987. .byte 8,199,65,28,255,255,254,255,137,105,24,15,141,217,0,0

988. .byte 0,235,211,129,121,12,255,255,254,255,15,131,196,33,0,0

989. .byte 129,121,20,255,255,254,255,15,131,183,33,0,0,139,105,20

990. .byte 242,15,16,1,242,15,16,73,8,242,15,88,65,16,242,15

991. .byte 17,1,133,237,120,17,102,15,46,200,242,15,17,65,24,15

992. .byte 131,149,0,0,0,235,143,102,15,46,193,235,237

994. .globl lj_BC_ITERL

995. .hidden lj_BC_ITERL

996. .type lj_BC_ITERL, @function

997. .size lj_BC_ITERL, 20

998. lj_BC_ITERL:

999. .byte 137,221,209,237,131,229,126,102,65,131,108,46,128,2,15,130

1000. .byte 185,28,0,0

1002. .globl lj_BC_IITERL

1003. .hidden lj_BC_IITERL

1004. .type lj_BC_IITERL, @function

1005. .size lj_BC_IITERL, 44

1006. lj_BC_IITERL:

1007. .byte 141,12,202,139,105,4,131,253,255,116,15,141,156,131,0,0

1008. .byte 254,255,139,1,137,105,252,137,65,248,139,3,15,182,204,15

1009. .byte 182,232,131,195,4,193,232,16,65,255,36,238

1011. .globl lj_BC_JITERL

1012. .hidden lj_BC_JITERL

1013. .type lj_BC_JITERL, @function

1014. .size lj_BC_JITERL, 39

1015. lj_BC_JITERL:

1016. .byte 141,12,202,139,105,4,131,253,255,116,10,137,105,252,139,41

1017. .byte 137,105,248,235,56,139,3,15,182,204,15,182,232,131,195,4

1018. .byte 193,232,16,65,255,36,238

1020. .globl lj_BC_LOOP

1021. .hidden lj_BC_LOOP

1022. .type lj_BC_LOOP, @function

1023. .size lj_BC_LOOP, 20

1024. lj_BC_LOOP:

1025. .byte 137,221,209,237,131,229,126,102,65,131,108,46,128,2,15,130

1026. .byte 82,28,0,0

1028. .globl lj_BC_ILOOP

1029. .hidden lj_BC_ILOOP

1030. .type lj_BC_ILOOP, @function

1031. .size lj_BC_ILOOP, 18

1032. lj_BC_ILOOP:

1033. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

1034. .byte 36,238

1036. .globl lj_BC_JLOOP

1037. .hidden lj_BC_JLOOP

1038. .type lj_BC_JLOOP, @function

1039. .size lj_BC_JLOOP, 47

1040. lj_BC_JLOOP:

1041. .byte 65,139,142,240,246,255,255,139,4,129,72,139,64,64,139,108

1042. .byte 36,24,65,137,150,28,245,255,255,65,137,174,136,244,255,255

1043. .byte 76,137,36,36,76,137,108,36,8,72,131,236,16,255,224

1045. .globl lj_BC_JMP

1046. .hidden lj_BC_JMP

1047. .type lj_BC_JMP, @function

1048. .size lj_BC_JMP, 25

1049. lj_BC_JMP:

1050. .byte 141,156,131,0,0,254,255,139,3,15,182,204,15,182,232,131

1051. .byte 195,4,193,232,16,65,255,36,238

1053. .globl lj_BC_FUNCF

1054. .hidden lj_BC_FUNCF

1055. .type lj_BC_FUNCF, @function

1056. .size lj_BC_FUNCF, 20

1057. lj_BC_FUNCF:

1058. .byte 137,221,209,237,131,229,126,102,65,131,108,46,128,1,15,130

1059. .byte 28,28,0,0

1061. .globl lj_BC_IFUNCF

1062. .hidden lj_BC_IFUNCF

1063. .type lj_BC_IFUNCF, @function

1064. .size lj_BC_IFUNCF, 63

1065. lj_BC_IFUNCF:

1066. .byte 68,139,123,204,139,108,36,24,141,12,202,59,77,32,15,135

1067. .byte 223,2,0,0,15,182,75,194,57,200,118,18,139,3,15,182

1068. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238,199,68

1069. .byte 194,252,255,255,255,255,131,192,1,57,200,118,241,235,221

1071. .globl lj_BC_JFUNCF

1072. .hidden lj_BC_JFUNCF

1073. .type lj_BC_JFUNCF, @function

1074. .size lj_BC_JFUNCF, 54

1075. lj_BC_JFUNCF:

1076. .byte 68,139,123,204,139,108,36,24,141,12,202,59,77,32,15,135

1077. .byte 160,2,0,0,15,182,75,194,57,200,118,9,15,183,67,254

1078. .byte 233,64,255,255,255,199,68,194,252,255,255,255,255,131,192,1

1079. .byte 57,200,118,241,235,230

1081. .globl lj_BC_FUNCV

1082. .hidden lj_BC_FUNCV

1083. .type lj_BC_FUNCV, @function

1084. .size lj_BC_FUNCV, 0

1085. lj_BC_FUNCV:

1087. .globl lj_BC_IFUNCV

1088. .hidden lj_BC_IFUNCV

1089. .type lj_BC_IFUNCV, @function

1090. .size lj_BC_IFUNCV, 125

1091. lj_BC_IFUNCV:

1092. .byte 141,44,197,3,0,0,0,141,4,194,68,139,122,248,137,104

1093. .byte 252,68,137,120,248,139,108,36,24,141,12,200,59,77,32,15

1094. .byte 135,84,2,0,0,137,209,137,194,15,182,107,194,133,237,116

1095. .byte 37,131,193,8,57,209,115,52,68,139,121,248,68,137,56,68

1096. .byte 139,121,252,68,137,120,4,131,192,8,199,65,252,255,255,255

1097. .byte 255,131,237,1,117,219,68,139,123,204,139,3,15,182,204,15

1098. .byte 182,232,131,195,4,193,232,16,65,255,36,238,199,64,4,255

1099. .byte 255,255,255,131,192,8,131,237,1,117,241,235,217

1101. .globl lj_BC_JFUNCV

1102. .hidden lj_BC_JFUNCV

1103. .type lj_BC_JFUNCV, @function

1104. .size lj_BC_JFUNCV, 1

1105. lj_BC_JFUNCV:

1106. .byte 204

1108. .globl lj_BC_FUNCC

1109. .hidden lj_BC_FUNCC

1110. .type lj_BC_FUNCC, @function

1111. .size lj_BC_FUNCC, 89

1112. lj_BC_FUNCC:

1113. .byte 139,106,248,76,139,125,24,139,108,36,24,141,68,194,248,137

1114. .byte 85,16,141,136,160,0,0,0,59,77,32,137,69,24,137,239

1115. .byte 15,135,206,1,0,0,65,199,134,120,244,255,255,254,255,255

1116. .byte 255,65,255,215,139,85,16,65,137,174,24,245,255,255,65,199

1117. .byte 134,120,244,255,255,255,255,255,255,141,12,194,247,217,3,77

1118. .byte 24,139,90,252,233,126,0,0,0

1120. .globl lj_BC_FUNCCW

1121. .hidden lj_BC_FUNCCW

1122. .type lj_BC_FUNCCW, @function

1123. .size lj_BC_FUNCCW, 93

1124. lj_BC_FUNCCW:

1125. .byte 139,106,248,76,139,125,24,139,108,36,24,141,68,194,248,137

1126. .byte 85,16,141,136,160,0,0,0,59,77,32,137,69,24,76,137

1127. .byte 254,137,239,15,135,114,1,0,0,65,199,134,120,244,255,255

1128. .byte 254,255,255,255,65,255,150,0,245,255,255,139,85,16,65,137

1129. .byte 174,24,245,255,255,65,199,134,120,244,255,255,255,255,255,255

1130. .byte 141,12,194,247,217,3,77,24,139,90,252,235,33

1132. .globl lj_vm_returnp

1133. .hidden lj_vm_returnp

1134. .type lj_vm_returnp, @function

1135. .size lj_vm_returnp, 33

1136. lj_vm_returnp:

1137. .byte 247,195,4,0,0,0,15,132,245,2,0,0,131,227,248,41

1138. .byte 218,72,141,76,25,248,139,90,252,199,68,10,4,253,255,255

1139. .byte 255

1141. .globl lj_vm_returnc

1142. .hidden lj_vm_returnc

1143. .type lj_vm_returnc, @function

1144. .size lj_vm_returnc, 25

1145. lj_vm_returnc:

1146. .byte 131,192,1,15,132,167,0,0,0,137,68,36,4,247,195,3

1147. .byte 0,0,0,15,132,252,248,255,255

1149. .globl lj_vm_return

1150. .hidden lj_vm_return

1151. .type lj_vm_return, @function

1152. .size lj_vm_return, 75

1153. lj_vm_return:

1154. .byte 131,243,1,247,195,3,0,0,0,117,187,65,199,134,120,244

1155. .byte 255,255,254,255,255,255,131,227,248,41,211,247,219,131,232,1

1156. .byte 116,16,72,139,44,10,72,137,106,248,131,194,8,131,232,1

1157. .byte 117,240,139,108,36,24,137,93,16,139,68,36,4,139,76,36

1158. .byte 16,57,193,117,28,131,234,8,137,85,24

1160. .globl lj_vm_leave_cp

1161. .hidden lj_vm_leave_cp

1162. .type lj_vm_leave_cp, @function

1163. .size lj_vm_leave_cp, 11

1164. lj_vm_leave_cp:

1165. .byte 72,139,76,36,32,72,137,77,48,49,192

1167. .globl lj_vm_leave_unw

1168. .hidden lj_vm_leave_unw

1169. .type lj_vm_leave_unw, @function

1170. .size lj_vm_leave_unw, 65

1171. lj_vm_leave_unw:

1172. .byte 72,131,196,40,65,94,65,95,91,93,195,114,20,59,85,32

1173. .byte 119,26,199,66,252,255,255,255,255,131,194,8,131,192,1,235

1174. .byte 202,133,201,116,202,41,193,141,20,202,235,195,137,85,24,137

1175. .byte 68,36,4,137,206,137,239

1176. call lj_state_growstack

1177. .byte 139,85,24,235,162

1179. .globl lj_vm_unwind_yield

1180. .hidden lj_vm_unwind_yield

1181. .type lj_vm_unwind_yield, @function

1182. .size lj_vm_unwind_yield, 4

1183. lj_vm_unwind_yield:

1184. .byte 176,1,235,5

1186. .globl lj_vm_unwind_c

1187. .hidden lj_vm_unwind_c

1188. .type lj_vm_unwind_c, @function

1189. .size lj_vm_unwind_c, 5

1190. lj_vm_unwind_c:

1191. .byte 137,240,72,137,252

1193. .globl lj_vm_unwind_c_eh

1194. .hidden lj_vm_unwind_c_eh

1195. .type lj_vm_unwind_c_eh, @function

1196. .size lj_vm_unwind_c_eh, 16

1197. lj_vm_unwind_c_eh:

1198. .byte 139,108,36,24,139,109,8,199,69,88,254,255,255,255,235,166

1200. .globl lj_vm_unwind_rethrow

1201. .hidden lj_vm_unwind_rethrow

1202. .type lj_vm_unwind_rethrow, @function

1203. .size lj_vm_unwind_rethrow, 21

1204. lj_vm_unwind_rethrow:

1205. .byte 139,124,36,24,137,198,72,131,196,40,65,94,65,95,91,93

1206. jmp lj_err_throw

1208. .globl lj_vm_unwind_ff

1209. .hidden lj_vm_unwind_ff

1210. .type lj_vm_unwind_ff, @function

1211. .size lj_vm_unwind_ff, 7

1212. lj_vm_unwind_ff:

1213. .byte 72,131,231,252,72,137,252

1215. .globl lj_vm_unwind_ff_eh

1216. .hidden lj_vm_unwind_ff_eh

1217. .type lj_vm_unwind_ff_eh, @function

1218. .size lj_vm_unwind_ff_eh, 56

1219. lj_vm_unwind_ff_eh:

1220. .byte 139,108,36,24,72,199,193,248,255,255,255,184,2,0,0,0

1221. .byte 139,85,16,68,139,117,8,65,129,198,224,11,0,0,139,90

1222. .byte 252,199,66,252,254,255,255,255,65,199,134,120,244,255,255,255

1223. .byte 255,255,255,233,227,254,255,255

1225. .globl lj_vm_growstack_c

1226. .hidden lj_vm_growstack_c

1227. .type lj_vm_growstack_c, @function

1228. .size lj_vm_growstack_c, 7

1229. lj_vm_growstack_c:

1230. .byte 190,20,0,0,0,235,28

1232. .globl lj_vm_growstack_v

1233. .hidden lj_vm_growstack_v

1234. .type lj_vm_growstack_v, @function

1235. .size lj_vm_growstack_v, 5

1236. lj_vm_growstack_v:

1237. .byte 131,232,8,235,4

1239. .globl lj_vm_growstack_f

1240. .hidden lj_vm_growstack_f

1241. .type lj_vm_growstack_f, @function

1242. .size lj_vm_growstack_f, 65

1243. lj_vm_growstack_f:

1244. .byte 141,68,194,248,15,182,75,195,131,195,4,137,85,16,137,69

1245. .byte 24,137,92,36,28,137,206,137,239

1246. call lj_state_growstack

1247. .byte 139,85,16,139,69,24,139,106,248,41,208,193,232,3,131,192

1248. .byte 1,139,93,16,139,11,15,182,233,15,182,205,131,195,4,65

1249. .byte 255,36,238

1251. .globl lj_vm_resume

1252. .hidden lj_vm_resume

1253. .type lj_vm_resume, @function

1254. .size lj_vm_resume, 132

1255. lj_vm_resume:

1256. .byte 85,83,65,87,65,86,72,131,236,40,137,253,137,124,36,24

1257. .byte 137,241,187,5,0,0,0,49,192,76,141,124,36,1,68,139

1258. .byte 117,8,65,129,198,224,11,0,0,137,68,36,28,72,137,68

1259. .byte 36,32,137,68,36,16,137,68,36,20,76,137,125,48,56,69

1260. .byte 7,15,132,137,0,0,0,65,137,174,24,245,255,255,65,199

1261. .byte 134,120,244,255,255,255,255,255,255,136,69,7,139,85,16,139

1262. .byte 69,24,41,200,193,232,3,131,192,1,41,209,139,90,252,137

1263. .byte 68,36,4,247,195,3,0,0,0,15,132,44,247,255,255,233

1264. .byte 43,254,255,255

1266. .globl lj_vm_pcall

1267. .hidden lj_vm_pcall

1268. .type lj_vm_pcall, @function

1269. .size lj_vm_pcall, 21

1270. lj_vm_pcall:

1271. .byte 85,83,65,87,65,86,72,131,236,40,187,5,0,0,0,137

1272. .byte 76,36,20,235,15

1274. .globl lj_vm_call

1275. .hidden lj_vm_call

1276. .type lj_vm_call, @function

1277. .size lj_vm_call, 91

1278. lj_vm_call:

1279. .byte 85,83,65,87,65,86,72,131,236,40,187,1,0,0,0,137

1280. .byte 84,36,16,137,253,137,124,36,24,137,241,68,139,117,8,76

1281. .byte 139,125,48,76,137,124,36,32,137,108,36,28,65,129,198,224

1282. .byte 11,0,0,72,137,101,48,65,137,174,24,245,255,255,65,199

1283. .byte 134,120,244,255,255,255,255,255,255,139,85,16,1,203,41,211

1284. .byte 139,69,24,41,200,193,232,3,131,192,1

1286. .globl lj_vm_call_dispatch

1287. .hidden lj_vm_call_dispatch

1288. .type lj_vm_call_dispatch, @function

1289. .size lj_vm_call_dispatch, 13

1290. lj_vm_call_dispatch:

1291. .byte 139,105,248,131,121,252,247,15,133,179,3,0,0

1293. .globl lj_vm_call_dispatch_f

1294. .hidden lj_vm_call_dispatch_f

1295. .type lj_vm_call_dispatch_f, @function

1296. .size lj_vm_call_dispatch_f, 23

1297. lj_vm_call_dispatch_f:

1298. .byte 137,202,137,90,252,139,93,16,139,11,15,182,233,15,182,205

1299. .byte 131,195,4,65,255,36,238

1301. .globl lj_vm_cpcall

1302. .hidden lj_vm_cpcall

1303. .type lj_vm_cpcall, @function

1304. .size lj_vm_cpcall, 94

1305. lj_vm_cpcall:

1306. .byte 85,83,65,87,65,86,72,131,236,40,137,253,137,124,36,24

1307. .byte 137,108,36,28,68,139,125,36,68,43,125,24,68,139,117,8

1308. .byte 199,68,36,20,0,0,0,0,68,137,124,36,16,65,129,198

1309. .byte 224,11,0,0,76,139,125,48,76,137,124,36,32,72,137,101

1310. .byte 48,65,137,174,24,245,255,255,255,209,133,192,15,132,144,253

1311. .byte 255,255,137,193,187,5,0,0,0,233,90,255,255,255

1313. .globl lj_cont_dispatch

1314. .hidden lj_cont_dispatch

1315. .type lj_cont_dispatch, @function

1316. .size lj_cont_dispatch, 74

1317. lj_cont_dispatch:

1318. .byte 1,209,131,227,248,137,213,41,218,199,68,193,252,255,255,255

1319. .byte 255,137,200,139,93,244,72,99,77,240,131,249,1,118,24,76

1320. .byte 141,61,31,223,255,255,76,1,249,68,139,122,248,69,139,127

1321. .byte 16,69,139,127,208,255,225,15,132,108,28,0,0,41,213,193

1322. .byte 237,3,141,69,255,233,9,22,0,0

1324. .globl lj_cont_cat

1325. .hidden lj_cont_cat

1326. .type lj_cont_cat, @function

1327. .size lj_cont_cat, 46

1328. lj_cont_cat:

1329. .byte 15,182,75,255,131,237,16,141,12,202,41,233,15,132,134,0

1330. .byte 0,0,247,217,193,233,3,139,124,36,24,137,87,16,137,202

1331. .byte 72,139,8,72,137,77,0,137,238,233,206,234,255,255

1333. .globl lj_vmeta_tgets

1334. .hidden lj_vmeta_tgets

1335. .type lj_vmeta_tgets, @function

1336. .size lj_vmeta_tgets, 41

1337. lj_vmeta_tgets:

1338. .byte 137,4,36,199,68,36,4,251,255,255,255,72,141,4,36,128

1339. .byte 123,252,54,117,48,65,141,142,176,244,255,255,137,41,199,65

1340. .byte 4,244,255,255,255,137,205,235,35

1342. .globl lj_vmeta_tgetb

1343. .hidden lj_vmeta_tgetb

1344. .type lj_vmeta_tgetb, @function

1345. .size lj_vmeta_tgetb, 21

1346. lj_vmeta_tgetb:

1347. .byte 15,182,67,254,199,68,36,4,255,255,254,255,137,4,36,72

1348. .byte 141,4,36,235,7

1350. .globl lj_vmeta_tgetv

1351. .hidden lj_vmeta_tgetv

1352. .type lj_vmeta_tgetv, @function

1353. .size lj_vmeta_tgetv, 44

1354. lj_vmeta_tgetv:

1355. .byte 15,182,67,254,141,4,194,15,182,107,255,141,44,234,139,124

1356. .byte 36,24,137,87,16,137,238,72,137,194,137,253,137,92,36,28

1357. call lj_meta_tget

1358. .byte 139,85,16,133,192,116,29

1360. .globl lj_cont_ra

1361. .hidden lj_cont_ra

1362. .type lj_cont_ra, @function

1363. .size lj_cont_ra, 53

1364. lj_cont_ra:

1365. .byte 15,182,75,253,72,139,40,72,137,44,202,139,3,15,182,204

1366. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238,139,77,24

1367. .byte 137,89,244,141,89,2,41,211,139,105,248,184,3,0,0,0

1368. .byte 233,116,254,255,255

1370. .globl lj_vmeta_tgetr

1371. .hidden lj_vmeta_tgetr

1372. .type lj_vmeta_tgetr, @function

1373. .size lj_vmeta_tgetr, 38

1374. lj_vmeta_tgetr:

1375. .byte 137,239,137,213,137,198

1376. call lj_tab_getinth

1377. .byte 15,182,75,253,137,234,133,192,15,133,7,239,255,255,199,68

1378. .byte 202,4,255,255,255,255,233,1,239,255,255

1380. .globl lj_vmeta_tsets

1381. .hidden lj_vmeta_tsets

1382. .type lj_vmeta_tsets, @function

1383. .size lj_vmeta_tsets, 41

1384. lj_vmeta_tsets:

1385. .byte 137,4,36,199,68,36,4,251,255,255,255,72,141,4,36,128

1386. .byte 123,252,55,117,48,65,141,142,176,244,255,255,137,41,199,65

1387. .byte 4,244,255,255,255,137,205,235,35

1389. .globl lj_vmeta_tsetb

1390. .hidden lj_vmeta_tsetb

1391. .type lj_vmeta_tsetb, @function

1392. .size lj_vmeta_tsetb, 21

1393. lj_vmeta_tsetb:

1394. .byte 15,182,67,254,199,68,36,4,255,255,254,255,137,4,36,72

1395. .byte 141,4,36,235,7

1397. .globl lj_vmeta_tsetv

1398. .hidden lj_vmeta_tsetv

1399. .type lj_vmeta_tsetv, @function

1400. .size lj_vmeta_tsetv, 55

1401. lj_vmeta_tsetv:

1402. .byte 15,182,67,254,141,4,194,15,182,107,255,141,44,234,139,124

1403. .byte 36,24,137,87,16,137,238,72,137,194,137,253,137,92,36,28

1404. call lj_meta_tset

1405. .byte 139,85,16,133,192,116,29,15,182,75,253,72,139,44,202,72

1406. .byte 137,40

1408. .globl lj_cont_nop

1409. .hidden lj_cont_nop

1410. .type lj_cont_nop, @function

1411. .size lj_cont_nop, 54

1412. lj_cont_nop:

1413. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

1414. .byte 36,238,139,77,24,137,89,244,15,182,67,253,72,139,44,194

1415. .byte 72,137,105,16,141,89,2,41,211,139,105,248,184,4,0,0

1416. .byte 0,233,163,253,255,255

1418. .globl lj_vmeta_tsetr

1419. .hidden lj_vmeta_tsetr

1420. .type lj_vmeta_tsetr, @function

1421. .size lj_vmeta_tsetr, 33

1422. lj_vmeta_tsetr:

1423. .byte 139,124,36,24,137,238,137,87,16,137,213,137,194,137,92,36

1424. .byte 28

1425. call lj_tab_setinth

1426. .byte 15,182,75,253,137,234,233,240,240,255,255

1428. .globl lj_vmeta_comp

1429. .hidden lj_vmeta_comp

1430. .type lj_vmeta_comp, @function

1431. .size lj_vmeta_comp, 74

1432. lj_vmeta_comp:

1433. .byte 139,108,36,24,137,85,16,141,52,202,141,20,194,137,239,15

1434. .byte 182,75,252,137,92,36,28

1435. call lj_meta_comp

1436. .byte 139,85,16,131,248,1,15,135,219,0,0,0,141,91,4,114

1437. .byte 11,15,183,67,254,141,156,131,0,0,254,255,139,3,15,182

1438. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238

1440. .globl lj_cont_condt

1441. .hidden lj_cont_condt

1442. .type lj_cont_condt, @function

1443. .size lj_cont_condt, 11

1444. lj_cont_condt:

1445. .byte 131,195,4,131,120,4,254,114,218,235,227

1447. .globl lj_cont_condf

1448. .hidden lj_cont_condf

1449. .type lj_cont_condf, @function

1450. .size lj_cont_condf, 6

1451. lj_cont_condf:

1452. .byte 131,120,4,254,235,205

1454. .globl lj_vmeta_equal

1455. .hidden lj_vmeta_equal

1456. .type lj_vmeta_equal, @function

1457. .size lj_vmeta_equal, 29

1458. lj_vmeta_equal:

1459. .byte 131,235,4,137,206,137,233,139,108,36,24,137,85,16,137,194

1460. .byte 137,239,137,92,36,28

1461. call lj_meta_equal

1462. .byte 235,164

1464. .globl lj_vmeta_equal_cd

1465. .hidden lj_vmeta_equal_cd

1466. .type lj_vmeta_equal_cd, @function

1467. .size lj_vmeta_equal_cd, 26

1468. lj_vmeta_equal_cd:

1469. .byte 131,235,4,139,108,36,24,137,85,16,137,239,139,115,252,137

1470. .byte 92,36,28

1471. call lj_meta_equal_cd

1472. .byte 235,138

1474. .globl lj_vmeta_istype

1475. .hidden lj_vmeta_istype

1476. .type lj_vmeta_istype, @function

1477. .size lj_vmeta_istype, 29

1478. lj_vmeta_istype:

1479. .byte 139,108,36,24,137,85,16,137,206,15,183,83,254,137,239,137

1480. .byte 92,36,28

1481. call lj_meta_istype

1482. .byte 139,85,16,235,137

1484. .globl lj_vmeta_arith_vno

1485. .hidden lj_vmeta_arith_vno

1486. .type lj_vmeta_arith_vno, @function

1487. .size lj_vmeta_arith_vno, 4

1488. lj_vmeta_arith_vno:

1489. .byte 15,182,107,255

1491. .globl lj_vmeta_arith_vn

1492. .hidden lj_vmeta_arith_vn

1493. .type lj_vmeta_arith_vn, @function

1494. .size lj_vmeta_arith_vn, 6

1495. lj_vmeta_arith_vn:

1496. .byte 65,141,4,199,235,28

1498. .globl lj_vmeta_arith_nvo

1499. .hidden lj_vmeta_arith_nvo

1500. .type lj_vmeta_arith_nvo, @function

1501. .size lj_vmeta_arith_nvo, 4

1502. lj_vmeta_arith_nvo:

1503. .byte 15,182,67,254

1505. .globl lj_vmeta_arith_nv

1506. .hidden lj_vmeta_arith_nv

1507. .type lj_vmeta_arith_nv, @function

1508. .size lj_vmeta_arith_nv, 10

1509. lj_vmeta_arith_nv:

1510. .byte 65,141,4,199,141,44,234,149,235,17

1512. .globl lj_vmeta_unm

1513. .hidden lj_vmeta_unm

1514. .type lj_vmeta_unm, @function

1515. .size lj_vmeta_unm, 7

1516. lj_vmeta_unm:

1517. .byte 141,4,194,137,197,235,10

1519. .globl lj_vmeta_arith_vvo

1520. .hidden lj_vmeta_arith_vvo

1521. .type lj_vmeta_arith_vvo, @function

1522. .size lj_vmeta_arith_vvo, 4

1523. lj_vmeta_arith_vvo:

1524. .byte 15,182,107,255

1526. .globl lj_vmeta_arith_vv

1527. .hidden lj_vmeta_arith_vv

1528. .type lj_vmeta_arith_vv, @function

1529. .size lj_vmeta_arith_vv, 49

1530. lj_vmeta_arith_vv:

1531. .byte 141,4,194,141,44,234,141,12,202,68,15,182,67,252,137,206

1532. .byte 137,193,139,124,36,24,137,87,16,137,234,137,253,137,92,36

1533. .byte 28

1534. call lj_meta_arith

1535. .byte 139,85,16,133,192,15,132,166,254,255,255

1537. .globl lj_vmeta_binop

1538. .hidden lj_vmeta_binop

1539. .type lj_vmeta_binop, @function

1540. .size lj_vmeta_binop, 20

1541. lj_vmeta_binop:

1542. .byte 137,193,41,208,137,89,244,141,88,2,184,3,0,0,0,233

1543. .byte 94,252,255,255

1545. .globl lj_vmeta_len

1546. .hidden lj_vmeta_len

1547. .type lj_vmeta_len, @function

1548. .size lj_vmeta_len, 26

1549. lj_vmeta_len:

1550. .byte 139,108,36,24,137,85,16,141,52,194,137,239,137,92,36,28

1551. call lj_meta_len

1552. .byte 139,85,16,235,210

1554. .globl lj_vmeta_call_ra

1555. .hidden lj_vmeta_call_ra

1556. .type lj_vmeta_call_ra, @function

1557. .size lj_vmeta_call_ra, 4

1558. lj_vmeta_call_ra:

1559. .byte 141,76,202,8

1561. .globl lj_vmeta_call

1562. .hidden lj_vmeta_call

1563. .type lj_vmeta_call, @function

1564. .size lj_vmeta_call, 81

1565. lj_vmeta_call:

1566. .byte 137,76,36,4,137,4,36,131,233,8,139,108,36,24,137,85

1567. .byte 16,137,206,141,20,193,137,239,137,92,36,28

1568. call lj_meta_call

1569. .byte 139,85,16,139,76,36,4,139,4,36,139,105,248,131,192,1

1570. .byte 65,57,215,15,132,37,240,255,255,137,202,137,90,252,139,93

1571. .byte 16,139,11,15,182,233,15,182,205,131,195,4,65,255,36,238

1573. .globl lj_vmeta_for

1574. .hidden lj_vmeta_for

1575. .type lj_vmeta_for, @function

1576. .size lj_vmeta_for, 43

1577. lj_vmeta_for:

1578. .byte 139,108,36,24,137,85,16,137,206,137,239,137,92,36,28

1579. call lj_meta_for

1580. .byte 139,85,16,139,67,252,15,182,204,15,182,232,193,232,16,65

1581. .byte 255,164,238,208,4,0,0

1583. .globl lj_ff_assert

1584. .hidden lj_ff_assert

1585. .type lj_ff_assert, @function

1586. .size lj_ff_assert, 67

1587. lj_ff_assert:

1588. .byte 131,248,2,15,130,57,18,0,0,139,106,4,131,253,254,15

1589. .byte 131,45,18,0,0,139,90,252,137,68,36,4,137,106,252,139

1590. .byte 42,137,106,248,131,232,2,116,17,137,209,131,193,8,72,139

1591. .byte 41,72,137,105,248,131,232,1,117,241,139,68,36,4,233,145

1592. .byte 6,0,0

1594. .globl lj_ff_type

1595. .hidden lj_ff_type

1596. .type lj_ff_type, @function

1597. .size lj_ff_type, 66

1598. lj_ff_type:

1599. .byte 131,248,2,15,130,246,17,0,0,139,106,4,137,233,193,249

1600. .byte 15,131,249,254,116,37,184,13,0,0,0,247,213,57,232,15

1601. .byte 71,197,139,106,248,139,68,197,32,139,90,252,199,66,252,251

1602. .byte 255,255,255,137,66,248,233,77,6,0,0,184,3,0,0,0

1603. .byte 235,224

1605. .globl lj_ff_getmetatable

1606. .hidden lj_ff_getmetatable

1607. .type lj_ff_getmetatable, @function

1608. .size lj_ff_getmetatable, 162

1609. lj_ff_getmetatable:

1610. .byte 131,248,2,15,130,180,17,0,0,139,106,4,139,90,252,131

1611. .byte 253,244,117,97,139,42,139,109,16,133,237,199,66,252,255,255

1612. .byte 255,255,15,132,30,6,0,0,65,139,134,108,245,255,255,199

1613. .byte 66,252,244,255,255,255,137,106,248,139,77,28,35,72,8,107

1614. .byte 201,24,3,77,20,131,121,12,251,117,5,57,65,8,116,12

1615. .byte 139,73,16,133,201,117,238,233,234,5,0,0,139,105,4,131

1616. .byte 253,255,15,132,222,5,0,0,139,1,137,106,252,137,66,248

1617. .byte 233,209,5,0,0,131,253,243,116,154,131,253,242,119,20,129

1618. .byte 253,255,255,254,255,118,7,189,252,255,255,255,235,5,189,242

1619. .byte 255,255,255,247,213,65,139,172,174,128,245,255,255,233,119,255

1620. .byte 255,255

1622. .globl lj_ff_setmetatable

1623. .hidden lj_ff_setmetatable

1624. .type lj_ff_setmetatable, @function

1625. .size lj_ff_setmetatable, 92

1626. lj_ff_setmetatable:

1627. .byte 131,248,3,15,130,18,17,0,0,131,122,4,244,15,133,8

1628. .byte 17,0,0,139,42,131,125,16,0,15,133,252,16,0,0,131

1629. .byte 122,12,244,15,133,242,16,0,0,139,66,8,137,69,16,139

1630. .byte 90,252,199,66,252,244,255,255,255,137,106,248,246,69,4,4

1631. .byte 116,21,128,101,4,251,65,139,134,92,244,255,255,65,137,174

1632. .byte 92,244,255,255,137,69,12,233,72,5,0,0

1634. .globl lj_ff_rawget

1635. .hidden lj_ff_rawget

1636. .type lj_ff_rawget, @function

1637. .size lj_ff_rawget, 52

1638. lj_ff_rawget:

1639. .byte 131,248,3,15,130,182,16,0,0,131,122,4,244,15,133,172

1640. .byte 16,0,0,137,213,139,50,141,82,8,139,124,36,24

1641. call lj_tab_get

1642. .byte 137,234,72,139,40,139,90,252,72,137,106,248,233,20,5,0

1643. .byte 0

1645. .globl lj_ff_tonumber

1646. .hidden lj_ff_tonumber

1647. .type lj_ff_tonumber, @function

1648. .size lj_ff_tonumber, 40

1649. lj_ff_tonumber:

1650. .byte 131,248,2,15,133,130,16,0,0,129,122,4,255,255,254,255

1651. .byte 117,7,139,42,233,181,4,0,0,15,135,108,16,0,0,242

1652. .byte 15,16,2,233,228,4,0,0

1654. .globl lj_ff_tostring

1655. .hidden lj_ff_tostring

1656. .type lj_ff_tostring, @function

1657. .size lj_ff_tostring, 108

1658. lj_ff_tostring:

1659. .byte 131,248,2,15,130,90,16,0,0,139,90,252,131,122,4,251

1660. .byte 117,17,139,2,199,66,252,251,255,255,255,137,66,248,233,201

1661. .byte 4,0,0,129,122,4,255,255,254,255,15,135,51,16,0,0

1662. .byte 65,131,190,180,245,255,255,0,15,133,37,16,0,0,65,139

1663. .byte 174,64,244,255,255,65,59,174,68,244,255,255,114,5,232,159

1664. .byte 16,0,0,139,108,36,24,137,85,16,137,92,36,28,137,214

1665. .byte 137,239

1666. call lj_strfmt_number

1667. .byte 139,85,16,235,168

1669. .globl lj_ff_next

1670. .hidden lj_ff_next

1671. .type lj_ff_next, @function

1672. .size lj_ff_next, 72

1673. lj_ff_next:

1674. .byte 131,248,2,15,130,238,15,0,0,116,71,131,122,4,244,15

1675. .byte 133,226,15,0,0,139,108,36,24,137,85,16,137,85,24,139

1676. .byte 90,252,139,50,141,82,8,137,239,137,92,36,28

1677. call lj_tab_next

1678. .byte 139,85,16,133,192,116,34,72,139,106,8,72,139,66,16,72

1679. .byte 137,106,248,72,137,2

1681. .globl lj_fff_res2

1682. .hidden lj_fff_res2

1683. .type lj_fff_res2, @function

1684. .size lj_fff_res2, 31

1685. lj_fff_res2:

1686. .byte 184,3,0,0,0,233,51,4,0,0,199,66,12,255,255,255

1687. .byte 255,235,176,199,66,252,255,255,255,255,233,25,4,0,0

1689. .globl lj_ff_pairs

1690. .hidden lj_ff_pairs

1691. .type lj_ff_pairs, @function

1692. .size lj_ff_pairs, 57

1693. lj_ff_pairs:

1694. .byte 131,248,2,15,130,135,15,0,0,139,42,131,122,4,244,15

1695. .byte 133,123,15,0,0,139,106,248,139,69,32,139,90,252,199,66

1696. .byte 252,247,255,255,255,137,66,248,199,66,12,255,255,255,255,184

1697. .byte 4,0,0,0,233,229,3,0,0

1699. .globl lj_ff_ipairs_aux

1700. .hidden lj_ff_ipairs_aux

1701. .type lj_ff_ipairs_aux, @function

1702. .size lj_ff_ipairs_aux, 104

1703. lj_ff_ipairs_aux:

1704. .byte 131,248,3,15,130,78,15,0,0,131,122,4,244,15,133,68

1705. .byte 15,0,0,129,122,12,255,255,254,255,15,133,55,15,0,0

1706. .byte 139,90,252,139,66,8,131,192,1,199,66,252,255,255,254,255

1707. .byte 137,66,248,139,42,59,69,24,115,23,193,224,3,3,69,8

1708. .byte 131,120,4,255,116,34,72,139,40,72,137,42,233,87,255,255

1709. .byte 255,131,125,28,0,116,17,137,239,137,213,137,198

1710. call lj_tab_getinth

1711. .byte 137,234,133,192,117,216

1713. .globl lj_fff_res0

1714. .hidden lj_fff_res0

1715. .type lj_fff_res0, @function

1716. .size lj_fff_res0, 10

1717. lj_fff_res0:

1718. .byte 184,1,0,0,0,233,115,3,0,0

1720. .globl lj_ff_ipairs

1721. .hidden lj_ff_ipairs

1722. .type lj_ff_ipairs, @function

1723. .size lj_ff_ipairs, 64

1724. lj_ff_ipairs:

1725. .byte 131,248,2,15,130,220,14,0,0,139,42,131,122,4,244,15

1726. .byte 133,208,14,0,0,139,106,248,139,69,32,139,90,252,199,66

1727. .byte 252,247,255,255,255,137,66,248,199,66,12,255,255,254,255,199

1728. .byte 66,8,0,0,0,0,184,4,0,0,0,233,51,3,0,0

1730. .globl lj_ff_pcall

1731. .hidden lj_ff_pcall

1732. .type lj_ff_pcall, @function

1733. .size lj_ff_pcall, 41

1734. lj_ff_pcall:

1735. .byte 131,248,2,15,130,156,14,0,0,141,74,8,131,232,1,187

1736. .byte 14,0,0,0,65,15,182,174,157,244,255,255,193,237,4,131

1737. .byte 229,1,1,235,233,254,247,255,255

1739. .globl lj_ff_xpcall

1740. .hidden lj_ff_xpcall

1741. .type lj_ff_xpcall, @function

1742. .size lj_ff_xpcall, 55

1743. lj_ff_xpcall:

1744. .byte 131,248,3,15,130,115,14,0,0,131,122,12,247,15,133,105

1745. .byte 14,0,0,139,106,4,137,106,12,199,66,4,247,255,255,255

1746. .byte 139,42,139,90,8,137,106,8,137,26,141,74,16,131,232,2

1747. .byte 187,22,0,0,0,235,180

1749. .globl lj_ff_coroutine_resume

1750. .hidden lj_ff_coroutine_resume

1751. .type lj_ff_coroutine_resume, @function

1752. .size lj_ff_coroutine_resume, 310

1753. lj_ff_coroutine_resume:

1754. .byte 131,248,2,15,130,60,14,0,0,139,42,139,90,252,137,92

1755. .byte 36,28,137,44,36,131,122,4,249,15,133,38,14,0,0,72

1756. .byte 131,125,48,0,15,133,27,14,0,0,128,125,7,1,15,135

1757. .byte 17,14,0,0,139,77,24,116,9,59,77,16,15,132,3,14

1758. .byte 0,0,141,92,193,240,59,93,32,15,135,246,13,0,0,137

1759. .byte 93,24,139,108,36,24,137,85,16,131,194,8,137,85,24,141

1760. .byte 108,194,232,72,41,221,57,203,116,15,72,139,4,43,72,137

1761. .byte 67,248,131,235,8,57,203,117,241,137,206,139,60,36,232,80

1762. .byte 246,255,255,139,108,36,24,139,28,36,139,85,16,65,137,174

1763. .byte 24,245,255,255,65,199,134,120,244,255,255,255,255,255,255,131

1764. .byte 248,1,119,90,139,75,16,68,139,123,24,137,75,24,68,137

1765. .byte 251,41,203,116,31,141,4,26,193,235,3,59,69,32,119,91

1766. .byte 137,213,72,41,205,72,139,1,72,137,4,41,131,193,8,68

1767. .byte 57,249,117,241,141,67,2,199,66,252,253,255,255,255,139,92

1768. .byte 36,28,137,68,36,4,72,199,193,248,255,255,255,247,195,3

1769. .byte 0,0,0,15,132,133,237,255,255,233,132,244,255,255,199,66

1770. .byte 252,254,255,255,255,139,75,24,131,233,8,137,75,24,72,139

1771. .byte 1,72,137,2,184,3,0,0,0,235,195,139,12,36,68,137

1772. .byte 121,24,137,222,137,239

1773. call lj_state_growstack

1774. .byte 139,28,36,139,85,16,233,110,255,255,255

1776. .globl lj_ff_coroutine_wrap_aux

1777. .hidden lj_ff_coroutine_wrap_aux

1778. .type lj_ff_coroutine_wrap_aux, @function

1779. .size lj_ff_coroutine_wrap_aux, 257

1780. lj_ff_coroutine_wrap_aux:

1781. .byte 139,106,248,139,109,32,139,90,252,137,92,36,28,137,44,36

1782. .byte 72,131,125,48,0,15,133,244,12,0,0,128,125,7,1,15

1783. .byte 135,234,12,0,0,139,77,24,116,9,59,77,16,15,132,220

1784. .byte 12,0,0,141,92,193,248,59,93,32,15,135,207,12,0,0

1785. .byte 137,93,24,139,108,36,24,137,85,16,137,85,24,141,108,194

1786. .byte 240,72,41,221,57,203,116,15,72,139,4,43,72,137,67,248

1787. .byte 131,235,8,57,203,117,241,137,206,139,60,36,232,44,245,255

1788. .byte 255,139,108,36,24,139,28,36,139,85,16,65,137,174,24,245

1789. .byte 255,255,65,199,134,120,244,255,255,255,255,255,255,131,248,1

1790. .byte 119,78,139,75,16,68,139,123,24,137,75,24,68,137,251,41

1791. .byte 203,116,31,141,4,26,193,235,3,59,69,32,119,59,137,213

1792. .byte 72,41,205,72,139,1,72,137,4,41,131,193,8,68,57,249

1793. .byte 117,241,141,67,1,139,92,36,28,137,68,36,4,49,201,247

1794. .byte 195,3,0,0,0,15,132,109,236,255,255,233,108,243,255,255

1795. .byte 137,222,137,239

1796. call lj_ffh_coroutine_wrap_err

1797. .byte 139,12,36,68,137,121,24,137,222,137,239

1798. call lj_state_growstack

1799. .byte 139,28,36,139,85,16,235,145

1801. .globl lj_ff_coroutine_yield

1802. .hidden lj_ff_coroutine_yield

1803. .type lj_ff_coroutine_yield, @function

1804. .size lj_ff_coroutine_yield, 44

1805. lj_ff_coroutine_yield:

1806. .byte 139,108,36,24,72,247,69,48,1,0,0,0,15,132,252,11

1807. .byte 0,0,137,85,16,141,68,194,248,137,69,24,49,192,72,137

1808. .byte 69,48,176,1,136,69,7,233,117,243,255,255

1810. .globl lj_fff_resn

1811. .hidden lj_fff_resn

1812. .type lj_fff_resn, @function

1813. .size lj_fff_resn, 8

1814. lj_fff_resn:

1815. .byte 139,90,252,221,90,248,235,99

1817. .globl lj_ff_math_abs

1818. .hidden lj_ff_math_abs

1819. .type lj_ff_math_abs, @function

1820. .size lj_ff_math_abs, 29

1821. lj_ff_math_abs:

1822. .byte 131,248,2,15,130,209,11,0,0,129,122,4,255,255,254,255

1823. .byte 117,45,139,42,131,253,0,121,4,247,221,120,15

1825. .globl lj_fff_resi

1826. .hidden lj_fff_resi

1827. .type lj_fff_resi, @function

1828. .size lj_fff_resi, 0

1829. lj_fff_resi:

1831. .globl lj_fff_resbit

1832. .hidden lj_fff_resbit

1833. .type lj_fff_resbit, @function

1834. .size lj_fff_resbit, 62

1835. lj_fff_resbit:

1836. .byte 139,90,252,199,66,252,255,255,254,255,137,106,248,235,55,139

1837. .byte 90,252,199,66,252,0,0,224,65,199,66,248,0,0,0,0

1838. .byte 235,36,15,135,149,11,0,0,242,15,16,2,72,184,255,255

1839. .byte 255,255,255,255,255,127,102,72,15,110,200,15,84,193

1841. .globl lj_fff_resxmm0

1842. .hidden lj_fff_resxmm0

1843. .type lj_fff_resxmm0, @function

1844. .size lj_fff_resxmm0, 8

1845. lj_fff_resxmm0:

1846. .byte 139,90,252,242,15,17,66,248

1848. .globl lj_fff_res1

1849. .hidden lj_fff_res1

1850. .type lj_fff_res1, @function

1851. .size lj_fff_res1, 5

1852. lj_fff_res1:

1853. .byte 184,2,0,0,0

1855. .globl lj_fff_res

1856. .hidden lj_fff_res

1857. .type lj_fff_res, @function

1858. .size lj_fff_res, 4

1859. lj_fff_res:

1860. .byte 137,68,36,4

1862. .globl lj_fff_res_

1863. .hidden lj_fff_res_

1864. .type lj_fff_res_, @function

1865. .size lj_fff_res_, 66

1866. lj_fff_res_:

1867. .byte 247,195,3,0,0,0,117,46,56,67,255,119,28,15,182,75

1868. .byte 253,72,247,209,141,20,202,139,3,15,182,204,15,182,232,131

1869. .byte 195,4,193,232,16,65,255,36,238,199,68,194,244,255,255,255

1870. .byte 255,131,192,1,235,210,72,199,193,248,255,255,255,233,105,242

1871. .byte 255,255

1873. .globl lj_ff_math_floor

1874. .hidden lj_ff_math_floor

1875. .type lj_ff_math_floor, @function

1876. .size lj_ff_math_floor, 72

1877. lj_ff_math_floor:

1878. .byte 129,122,4,255,255,254,255,117,7,139,42,233,95,255,255,255

1879. .byte 15,135,22,11,0,0,242,15,16,2,232,7,15,0,0,242

1880. .byte 15,44,232,129,253,0,0,0,128,15,133,64,255,255,255,242

1881. .byte 15,42,205,102,15,46,193,15,138,112,255,255,255,15,132,44

1882. .byte 255,255,255,233,101,255,255,255

1884. .globl lj_ff_math_ceil

1885. .hidden lj_ff_math_ceil

1886. .type lj_ff_math_ceil, @function

1887. .size lj_ff_math_ceil, 72

1888. lj_ff_math_ceil:

1889. .byte 129,122,4,255,255,254,255,117,7,139,42,233,23,255,255,255

1890. .byte 15,135,206,10,0,0,242,15,16,2,232,26,15,0,0,242

1891. .byte 15,44,232,129,253,0,0,0,128,15,133,248,254,255,255,242

1892. .byte 15,42,205,102,15,46,193,15,138,40,255,255,255,15,132,228

1893. .byte 254,255,255,233,29,255,255,255

1895. .globl lj_ff_math_sqrt

1896. .hidden lj_ff_math_sqrt

1897. .type lj_ff_math_sqrt, @function

1898. .size lj_ff_math_sqrt, 31

1899. lj_ff_math_sqrt:

1900. .byte 131,248,2,15,130,147,10,0,0,129,122,4,255,255,254,255

1901. .byte 15,131,134,10,0,0,242,15,81,2,233,254,254,255,255

1903. .globl lj_ff_math_log

1904. .hidden lj_ff_math_log

1905. .type lj_ff_math_log, @function

1906. .size lj_ff_math_log, 40

1907. lj_ff_math_log:

1908. .byte 131,248,2,15,133,116,10,0,0,129,122,4,255,255,254,255

1909. .byte 15,131,103,10,0,0,242,15,16,2,137,213

1910. call log@PLT

1911. .byte 137,234,233,214,254,255,255

1913. .globl lj_ff_math_log10

1914. .hidden lj_ff_math_log10

1915. .type lj_ff_math_log10, @function

1916. .size lj_ff_math_log10, 40

1917. lj_ff_math_log10:

1918. .byte 131,248,2,15,130,76,10,0,0,129,122,4,255,255,254,255

1919. .byte 15,131,63,10,0,0,242,15,16,2,137,213

1920. call log10@PLT

1921. .byte 137,234,233,174,254,255,255

1923. .globl lj_ff_math_exp

1924. .hidden lj_ff_math_exp

1925. .type lj_ff_math_exp, @function

1926. .size lj_ff_math_exp, 40

1927. lj_ff_math_exp:

1928. .byte 131,248,2,15,130,36,10,0,0,129,122,4,255,255,254,255

1929. .byte 15,131,23,10,0,0,242,15,16,2,137,213

1930. call exp@PLT

1931. .byte 137,234,233,134,254,255,255

1933. .globl lj_ff_math_sin

1934. .hidden lj_ff_math_sin

1935. .type lj_ff_math_sin, @function

1936. .size lj_ff_math_sin, 40

1937. lj_ff_math_sin:

1938. .byte 131,248,2,15,130,252,9,0,0,129,122,4,255,255,254,255

1939. .byte 15,131,239,9,0,0,242,15,16,2,137,213

1940. call sin@PLT

1941. .byte 137,234,233,94,254,255,255

1943. .globl lj_ff_math_cos

1944. .hidden lj_ff_math_cos

1945. .type lj_ff_math_cos, @function

1946. .size lj_ff_math_cos, 40

1947. lj_ff_math_cos:

1948. .byte 131,248,2,15,130,212,9,0,0,129,122,4,255,255,254,255

1949. .byte 15,131,199,9,0,0,242,15,16,2,137,213

1950. call cos@PLT

1951. .byte 137,234,233,54,254,255,255

1953. .globl lj_ff_math_tan

1954. .hidden lj_ff_math_tan

1955. .type lj_ff_math_tan, @function

1956. .size lj_ff_math_tan, 40

1957. lj_ff_math_tan:

1958. .byte 131,248,2,15,130,172,9,0,0,129,122,4,255,255,254,255

1959. .byte 15,131,159,9,0,0,242,15,16,2,137,213

1960. call tan@PLT

1961. .byte 137,234,233,14,254,255,255

1963. .globl lj_ff_math_asin

1964. .hidden lj_ff_math_asin

1965. .type lj_ff_math_asin, @function

1966. .size lj_ff_math_asin, 40

1967. lj_ff_math_asin:

1968. .byte 131,248,2,15,130,132,9,0,0,129,122,4,255,255,254,255

1969. .byte 15,131,119,9,0,0,242,15,16,2,137,213

1970. call asin@PLT

1971. .byte 137,234,233,230,253,255,255

1973. .globl lj_ff_math_acos

1974. .hidden lj_ff_math_acos

1975. .type lj_ff_math_acos, @function

1976. .size lj_ff_math_acos, 40

1977. lj_ff_math_acos:

1978. .byte 131,248,2,15,130,92,9,0,0,129,122,4,255,255,254,255

1979. .byte 15,131,79,9,0,0,242,15,16,2,137,213

1980. call acos@PLT

1981. .byte 137,234,233,190,253,255,255

1983. .globl lj_ff_math_atan

1984. .hidden lj_ff_math_atan

1985. .type lj_ff_math_atan, @function

1986. .size lj_ff_math_atan, 40

1987. lj_ff_math_atan:

1988. .byte 131,248,2,15,130,52,9,0,0,129,122,4,255,255,254,255

1989. .byte 15,131,39,9,0,0,242,15,16,2,137,213

1990. call atan@PLT

1991. .byte 137,234,233,150,253,255,255

1993. .globl lj_ff_math_sinh

1994. .hidden lj_ff_math_sinh

1995. .type lj_ff_math_sinh, @function

1996. .size lj_ff_math_sinh, 40

1997. lj_ff_math_sinh:

1998. .byte 131,248,2,15,130,12,9,0,0,129,122,4,255,255,254,255

1999. .byte 15,131,255,8,0,0,242,15,16,2,137,213

2000. call sinh@PLT

2001. .byte 137,234,233,110,253,255,255

2003. .globl lj_ff_math_cosh

2004. .hidden lj_ff_math_cosh

2005. .type lj_ff_math_cosh, @function

2006. .size lj_ff_math_cosh, 40

2007. lj_ff_math_cosh:

2008. .byte 131,248,2,15,130,228,8,0,0,129,122,4,255,255,254,255

2009. .byte 15,131,215,8,0,0,242,15,16,2,137,213

2010. call cosh@PLT

2011. .byte 137,234,233,70,253,255,255

2013. .globl lj_ff_math_tanh

2014. .hidden lj_ff_math_tanh

2015. .type lj_ff_math_tanh, @function

2016. .size lj_ff_math_tanh, 40

2017. lj_ff_math_tanh:

2018. .byte 131,248,2,15,130,188,8,0,0,129,122,4,255,255,254,255

2019. .byte 15,131,175,8,0,0,242,15,16,2,137,213

2020. call tanh@PLT

2021. .byte 137,234,233,30,253,255,255

2023. .globl lj_ff_math_pow

2024. .hidden lj_ff_math_pow

2025. .type lj_ff_math_pow, @function

2026. .size lj_ff_math_pow, 58

2027. lj_ff_math_pow:

2028. .byte 131,248,3,15,130,148,8,0,0,129,122,4,255,255,254,255

2029. .byte 15,131,135,8,0,0,129,122,12,255,255,254,255,15,131,122

2030. .byte 8,0,0,242,15,16,2,242,15,16,74,8,137,213

2031. call pow@PLT

2032. .byte 137,234,233,228,252,255,255

2034. .globl lj_ff_math_atan2

2035. .hidden lj_ff_math_atan2

2036. .type lj_ff_math_atan2, @function

2037. .size lj_ff_math_atan2, 58

2038. lj_ff_math_atan2:

2039. .byte 131,248,3,15,130,90,8,0,0,129,122,4,255,255,254,255

2040. .byte 15,131,77,8,0,0,129,122,12,255,255,254,255,15,131,64

2041. .byte 8,0,0,242,15,16,2,242,15,16,74,8,137,213

2042. call atan2@PLT

2043. .byte 137,234,233,170,252,255,255

2045. .globl lj_ff_math_fmod

2046. .hidden lj_ff_math_fmod

2047. .type lj_ff_math_fmod, @function

2048. .size lj_ff_math_fmod, 58

2049. lj_ff_math_fmod:

2050. .byte 131,248,3,15,130,32,8,0,0,129,122,4,255,255,254,255

2051. .byte 15,131,19,8,0,0,129,122,12,255,255,254,255,15,131,6

2052. .byte 8,0,0,242,15,16,2,242,15,16,74,8,137,213

2053. call fmod@PLT

2054. .byte 137,234,233,112,252,255,255

2056. .globl lj_ff_math_ldexp

2057. .hidden lj_ff_math_ldexp

2058. .type lj_ff_math_ldexp, @function

2059. .size lj_ff_math_ldexp, 49

2060. lj_ff_math_ldexp:

2061. .byte 131,248,3,15,130,230,7,0,0,129,122,4,255,255,254,255

2062. .byte 15,131,217,7,0,0,129,122,12,255,255,254,255,15,131,204

2063. .byte 7,0,0,221,66,8,221,2,217,253,221,217,233,220,251,255

2064. .byte 255

2066. .globl lj_ff_math_frexp

2067. .hidden lj_ff_math_frexp

2068. .type lj_ff_math_frexp, @function

2069. .size lj_ff_math_frexp, 148

2070. lj_ff_math_frexp:

2071. .byte 131,248,2,15,130,181,7,0,0,139,106,4,129,253,255,255

2072. .byte 254,255,15,131,166,7,0,0,139,90,252,139,2,137,106,252

2073. .byte 137,66,248,209,229,129,253,0,0,224,255,115,58,9,232,116

2074. .byte 54,184,254,3,0,0,129,253,0,0,32,0,114,46,193,237

2075. .byte 21,41,197,242,15,42,197,139,106,252,129,229,255,255,15,128

2076. .byte 129,205,0,0,224,63,137,106,252,242,15,17,2,184,3,0

2077. .byte 0,0,233,229,251,255,255,15,87,192,235,237,242,15,16,2

2078. .byte 72,189,0,0,0,0,0,0,80,67,102,72,15,110,205,242

2079. .byte 15,89,193,242,15,17,66,248,139,106,252,184,52,4,0,0

2080. .byte 209,229,235,170

2082. .globl lj_ff_math_modf

2083. .hidden lj_ff_math_modf

2084. .type lj_ff_math_modf, @function

2085. .size lj_ff_math_modf, 99

2086. lj_ff_math_modf:

2087. .byte 131,248,2,15,130,33,7,0,0,129,122,4,255,255,254,255

2088. .byte 15,131,20,7,0,0,242,15,16,2,139,106,4,139,90,252

2089. .byte 209,229,129,253,0,0,224,255,116,52,15,40,224,232,168,11

2090. .byte 0,0,242,15,92,224,242,15,17,66,248,242,15,17,34,139

2091. .byte 66,252,139,106,4,49,232,120,10,184,3,0,0,0,233,101

2092. .byte 251,255,255,129,245,0,0,0,128,137,106,4,235,235,15,87

2093. .byte 228,235,211

2095. .globl lj_ff_math_min

2096. .hidden lj_ff_math_min

2097. .type lj_ff_math_min, @function

2098. .size lj_ff_math_min, 117

2099. lj_ff_math_min:

2100. .byte 185,2,0,0,0,129,122,4,255,255,254,255,117,46,139,42

2101. .byte 57,193,15,131,242,250,255,255,129,124,202,252,255,255,254,255

2102. .byte 117,14,59,108,202,248,15,79,108,202,248,131,193,1,235,224

2103. .byte 15,135,145,6,0,0,242,15,42,197,235,42,15,135,133,6

2104. .byte 0,0,242,15,16,2,57,193,15,131,250,250,255,255,129,124

2105. .byte 202,252,255,255,254,255,114,14,15,135,105,6,0,0,242,15

2106. .byte 42,76,202,248,235,6,242,15,16,76,202,248,242,15,93,193

2107. .byte 131,193,1,235,209

2109. .globl lj_ff_math_max

2110. .hidden lj_ff_math_max

2111. .type lj_ff_math_max, @function

2112. .size lj_ff_math_max, 117

2113. lj_ff_math_max:

2114. .byte 185,2,0,0,0,129,122,4,255,255,254,255,117,46,139,42

2115. .byte 57,193,15,131,125,250,255,255,129,124,202,252,255,255,254,255

2116. .byte 117,14,59,108,202,248,15,76,108,202,248,131,193,1,235,224

2117. .byte 15,135,28,6,0,0,242,15,42,197,235,42,15,135,16,6

2118. .byte 0,0,242,15,16,2,57,193,15,131,133,250,255,255,129,124

2119. .byte 202,252,255,255,254,255,114,14,15,135,244,5,0,0,242,15

2120. .byte 42,76,202,248,235,6,242,15,16,76,202,248,242,15,95,193

2121. .byte 131,193,1,235,209

2123. .globl lj_ff_string_byte

2124. .hidden lj_ff_string_byte

2125. .type lj_ff_string_byte, @function

2126. .size lj_ff_string_byte, 43

2127. lj_ff_string_byte:

2128. .byte 131,248,2,15,133,212,5,0,0,131,122,4,251,15,133,202

2129. .byte 5,0,0,139,42,139,90,252,131,125,12,1,15,130,204,246

2130. .byte 255,255,15,182,109,16,233,245,249,255,255

2132. .globl lj_ff_string_char

2133. .hidden lj_ff_string_char

2134. .type lj_ff_string_char, @function

2135. .size lj_ff_string_char, 74

2136. lj_ff_string_char:

2137. .byte 65,139,174,64,244,255,255,65,59,174,68,244,255,255,114,5

2138. .byte 232,44,6,0,0,131,248,2,15,133,148,5,0,0,129,122

2139. .byte 4,255,255,254,255,15,133,135,5,0,0,139,42,129,253,255

2140. .byte 0,0,0,15,135,121,5,0,0,137,108,36,4,199,68,36

2141. .byte 8,1,0,0,0,72,141,68,36,4

2143. .globl lj_fff_newstr

2144. .hidden lj_fff_newstr

2145. .type lj_fff_newstr, @function

2146. .size lj_fff_newstr, 25

2147. lj_fff_newstr:

2148. .byte 139,108,36,24,137,85,16,139,84,36,8,72,137,198,137,239

2149. .byte 137,92,36,28

2150. call lj_str_new

2152. .globl lj_fff_resstr

2153. .hidden lj_fff_resstr

2154. .type lj_fff_resstr, @function

2155. .size lj_fff_resstr, 21

2156. lj_fff_resstr:

2157. .byte 139,85,16,139,90,252,199,66,252,251,255,255,255,137,66,248

2158. .byte 233,195,249,255,255

2160. .globl lj_ff_string_sub

2161. .hidden lj_ff_string_sub

2162. .type lj_ff_string_sub, @function

2163. .size lj_ff_string_sub, 161

2164. lj_ff_string_sub:

2165. .byte 65,139,174,64,244,255,255,65,59,174,68,244,255,255,114,5

2166. .byte 232,180,5,0,0,199,68,36,4,255,255,255,255,131,248,3

2167. .byte 15,130,20,5,0,0,118,20,129,122,20,255,255,254,255,15

2168. .byte 133,5,5,0,0,139,106,16,137,108,36,4,131,122,4,251

2169. .byte 15,133,244,4,0,0,129,122,12,255,255,254,255,15,133,231

2170. .byte 4,0,0,139,42,137,108,36,8,139,109,12,139,74,8,139

2171. .byte 68,36,4,57,197,114,30,133,201,126,38,139,108,36,8,41

2172. .byte 200,124,46,141,108,13,15,131,192,1,137,68,36,8,137,232

2173. .byte 233,77,255,255,255,124,6,141,68,40,1,235,218,137,232,235

2174. .byte 214,116,7,1,233,131,193,1,127,209,185,1,0,0,0,235

2175. .byte 202

2177. .globl lj_fff_emptystr

2178. .hidden lj_fff_emptystr

2179. .type lj_fff_emptystr, @function

2180. .size lj_fff_emptystr, 4

2181. lj_fff_emptystr:

2182. .byte 49,192,235,213

2184. .globl lj_ff_string_reverse

2185. .hidden lj_ff_string_reverse

2186. .type lj_ff_string_reverse, @function

2187. .size lj_ff_string_reverse, 85

2188. lj_ff_string_reverse:

2189. .byte 131,248,2,15,130,140,4,0,0,65,139,174,64,244,255,255

2190. .byte 65,59,174,68,244,255,255,114,5,232,6,5,0,0,131,122

2191. .byte 4,251,15,133,109,4,0,0,139,108,36,24,65,141,190,124

2192. .byte 244,255,255,137,85,16,139,50,139,71,8,137,111,12,137,7

2193. .byte 137,92,36,28

2194. call lj_buf_putstr_reverse

2195. .byte 137,199

2196. call lj_buf_tostr

2197. .byte 233,241,254,255,255

2199. .globl lj_ff_string_lower

2200. .hidden lj_ff_string_lower

2201. .type lj_ff_string_lower, @function

2202. .size lj_ff_string_lower, 85

2203. lj_ff_string_lower:

2204. .byte 131,248,2,15,130,55,4,0,0,65,139,174,64,244,255,255

2205. .byte 65,59,174,68,244,255,255,114,5,232,177,4,0,0,131,122

2206. .byte 4,251,15,133,24,4,0,0,139,108,36,24,65,141,190,124

2207. .byte 244,255,255,137,85,16,139,50,139,71,8,137,111,12,137,7

2208. .byte 137,92,36,28

2209. call lj_buf_putstr_lower

2210. .byte 137,199

2211. call lj_buf_tostr

2212. .byte 233,156,254,255,255

2214. .globl lj_ff_string_upper

2215. .hidden lj_ff_string_upper

2216. .type lj_ff_string_upper, @function

2217. .size lj_ff_string_upper, 85

2218. lj_ff_string_upper:

2219. .byte 131,248,2,15,130,226,3,0,0,65,139,174,64,244,255,255

2220. .byte 65,59,174,68,244,255,255,114,5,232,92,4,0,0,131,122

2221. .byte 4,251,15,133,195,3,0,0,139,108,36,24,65,141,190,124

2222. .byte 244,255,255,137,85,16,139,50,139,71,8,137,111,12,137,7

2223. .byte 137,92,36,28

2224. call lj_buf_putstr_upper

2225. .byte 137,199

2226. call lj_buf_tostr

2227. .byte 233,71,254,255,255

2229. .globl lj_ff_bit_tobit

2230. .hidden lj_ff_bit_tobit

2231. .type lj_ff_bit_tobit, @function

2232. .size lj_ff_bit_tobit, 63

2233. lj_ff_bit_tobit:

2234. .byte 131,248,2,15,130,141,3,0,0,129,122,4,255,255,254,255

2235. .byte 117,7,139,42,233,192,247,255,255,15,135,119,3,0,0,242

2236. .byte 15,16,2,72,189,0,0,0,0,0,0,56,67,102,72,15

2237. .byte 110,205,242,15,88,193,102,15,126,197,233,154,247,255,255

2239. .globl lj_ff_bit_band

2240. .hidden lj_ff_bit_band

2241. .type lj_ff_bit_band, @function

2242. .size lj_ff_bit_band, 112

2243. lj_ff_bit_band:

2244. .byte 131,248,2,15,130,78,3,0,0,72,189,0,0,0,0,0

2245. .byte 0,56,67,102,72,15,110,205,129,122,4,255,255,254,255,117

2246. .byte 4,139,42,235,18,15,135,44,3,0,0,242,15,16,2,242

2247. .byte 15,88,193,102,15,126,197,137,68,36,4,141,68,194,240,57

2248. .byte 208,15,134,83,247,255,255,129,120,4,255,255,254,255,117,7

2249. .byte 35,40,131,232,8,235,232,15,135,111,1,0,0,242,15,16

2250. .byte 0,242,15,88,193,102,15,126,193,33,205,131,232,8,235,207

2252. .globl lj_ff_bit_bor

2253. .hidden lj_ff_bit_bor

2254. .type lj_ff_bit_bor, @function

2255. .size lj_ff_bit_bor, 112

2256. lj_ff_bit_bor:

2257. .byte 131,248,2,15,130,222,2,0,0,72,189,0,0,0,0,0

2258. .byte 0,56,67,102,72,15,110,205,129,122,4,255,255,254,255,117

2259. .byte 4,139,42,235,18,15,135,188,2,0,0,242,15,16,2,242

2260. .byte 15,88,193,102,15,126,197,137,68,36,4,141,68,194,240,57

2261. .byte 208,15,134,227,246,255,255,129,120,4,255,255,254,255,117,7

2262. .byte 11,40,131,232,8,235,232,15,135,255,0,0,0,242,15,16

2263. .byte 0,242,15,88,193,102,15,126,193,9,205,131,232,8,235,207

2265. .globl lj_ff_bit_bxor

2266. .hidden lj_ff_bit_bxor

2267. .type lj_ff_bit_bxor, @function

2268. .size lj_ff_bit_bxor, 112

2269. lj_ff_bit_bxor:

2270. .byte 131,248,2,15,130,110,2,0,0,72,189,0,0,0,0,0

2271. .byte 0,56,67,102,72,15,110,205,129,122,4,255,255,254,255,117

2272. .byte 4,139,42,235,18,15,135,76,2,0,0,242,15,16,2,242

2273. .byte 15,88,193,102,15,126,197,137,68,36,4,141,68,194,240,57

2274. .byte 208,15,134,115,246,255,255,129,120,4,255,255,254,255,117,7

2275. .byte 51,40,131,232,8,235,232,15,135,143,0,0,0,242,15,16

2276. .byte 0,242,15,88,193,102,15,126,193,49,205,131,232,8,235,207

2278. .globl lj_ff_bit_bswap

2279. .hidden lj_ff_bit_bswap

2280. .type lj_ff_bit_bswap, @function

2281. .size lj_ff_bit_bswap, 62

2282. lj_ff_bit_bswap:

2283. .byte 131,248,2,15,130,254,1,0,0,129,122,4,255,255,254,255

2284. .byte 117,4,139,42,235,33,15,135,235,1,0,0,242,15,16,2

2285. .byte 72,189,0,0,0,0,0,0,56,67,102,72,15,110,205,242

2286. .byte 15,88,193,102,15,126,197,15,205,233,12,246,255,255

2288. .globl lj_ff_bit_bnot

2289. .hidden lj_ff_bit_bnot

2290. .type lj_ff_bit_bnot, @function

2291. .size lj_ff_bit_bnot, 62

2292. lj_ff_bit_bnot:

2293. .byte 131,248,2,15,130,192,1,0,0,129,122,4,255,255,254,255

2294. .byte 117,4,139,42,235,33,15,135,173,1,0,0,242,15,16,2

2295. .byte 72,189,0,0,0,0,0,0,56,67,102,72,15,110,205,242

2296. .byte 15,88,193,102,15,126,197,247,213,233,206,245,255,255

2298. .globl lj_fff_fallback_bit_op

2299. .hidden lj_fff_fallback_bit_op

2300. .type lj_fff_fallback_bit_op, @function

2301. .size lj_fff_fallback_bit_op, 9

2302. lj_fff_fallback_bit_op:

2303. .byte 139,68,36,4,233,130,1,0,0

2305. .globl lj_ff_bit_lshift

2306. .hidden lj_ff_bit_lshift

2307. .type lj_ff_bit_lshift, @function

2308. .size lj_ff_bit_lshift, 78

2309. lj_ff_bit_lshift:

2310. .byte 131,248,2,15,130,121,1,0,0,129,122,4,255,255,254,255

2311. .byte 117,4,139,42,235,33,15,135,102,1,0,0,242,15,16,2

2312. .byte 72,189,0,0,0,0,0,0,56,67,102,72,15,110,205,242

2313. .byte 15,88,193,102,15,126,197,129,122,12,255,255,254,255,15,133

2314. .byte 62,1,0,0,139,74,8,211,229,233,119,245,255,255

2316. .globl lj_ff_bit_rshift

2317. .hidden lj_ff_bit_rshift

2318. .type lj_ff_bit_rshift, @function

2319. .size lj_ff_bit_rshift, 78

2320. lj_ff_bit_rshift:

2321. .byte 131,248,2,15,130,43,1,0,0,129,122,4,255,255,254,255

2322. .byte 117,4,139,42,235,33,15,135,24,1,0,0,242,15,16,2

2323. .byte 72,189,0,0,0,0,0,0,56,67,102,72,15,110,205,242

2324. .byte 15,88,193,102,15,126,197,129,122,12,255,255,254,255,15,133

2325. .byte 240,0,0,0,139,74,8,211,237,233,41,245,255,255

2327. .globl lj_ff_bit_arshift

2328. .hidden lj_ff_bit_arshift

2329. .type lj_ff_bit_arshift, @function

2330. .size lj_ff_bit_arshift, 78

2331. lj_ff_bit_arshift:

2332. .byte 131,248,2,15,130,221,0,0,0,129,122,4,255,255,254,255

2333. .byte 117,4,139,42,235,33,15,135,202,0,0,0,242,15,16,2

2334. .byte 72,189,0,0,0,0,0,0,56,67,102,72,15,110,205,242

2335. .byte 15,88,193,102,15,126,197,129,122,12,255,255,254,255,15,133

2336. .byte 162,0,0,0,139,74,8,211,253,233,219,244,255,255

2338. .globl lj_ff_bit_rol

2339. .hidden lj_ff_bit_rol

2340. .type lj_ff_bit_rol, @function

2341. .size lj_ff_bit_rol, 74

2342. lj_ff_bit_rol:

2343. .byte 131,248,2,15,130,143,0,0,0,129,122,4,255,255,254,255

2344. .byte 117,4,139,42,235,33,15,135,124,0,0,0,242,15,16,2

2345. .byte 72,189,0,0,0,0,0,0,56,67,102,72,15,110,205,242

2346. .byte 15,88,193,102,15,126,197,129,122,12,255,255,254,255,117,88

2347. .byte 139,74,8,211,197,233,145,244,255,255

2349. .globl lj_ff_bit_ror

2350. .hidden lj_ff_bit_ror

2351. .type lj_ff_bit_ror, @function

2352. .size lj_ff_bit_ror, 66

2353. lj_ff_bit_ror:

2354. .byte 131,248,2,114,73,129,122,4,255,255,254,255,117,4,139,42

2355. .byte 235,29,119,58,242,15,16,2,72,189,0,0,0,0,0,0

2356. .byte 56,67,102,72,15,110,205,242,15,88,193,102,15,126,197,129

2357. .byte 122,12,255,255,254,255,117,22,139,74,8,211,205,233,79,244

2358. .byte 255,255

2360. .globl lj_fff_fallback_2

2361. .hidden lj_fff_fallback_2

2362. .type lj_fff_fallback_2, @function

2363. .size lj_fff_fallback_2, 7

2364. lj_fff_fallback_2:

2365. .byte 184,3,0,0,0,235,5

2367. .globl lj_fff_fallback_1

2368. .hidden lj_fff_fallback_1

2369. .type lj_fff_fallback_1, @function

2370. .size lj_fff_fallback_1, 5

2371. lj_fff_fallback_1:

2372. .byte 184,2,0,0,0

2374. .globl lj_fff_fallback

2375. .hidden lj_fff_fallback

2376. .type lj_fff_fallback, @function

2377. .size lj_fff_fallback, 87

2378. lj_fff_fallback:

2379. .byte 139,108,36,24,139,90,252,137,92,36,28,137,85,16,141,68

2380. .byte 194,248,141,136,160,0,0,0,137,69,24,139,66,248,59,77

2381. .byte 32,119,89,137,239,255,80,24,139,85,16,133,192,15,143,91

2382. .byte 244,255,255,139,77,24,41,209,193,233,3,133,192,141,65,1

2383. .byte 139,106,248,117,18,139,93,16,139,11,15,182,233,15,182,205

2384. .byte 131,195,4,65,255,36,238

2386. .globl lj_vm_call_tail

2387. .hidden lj_vm_call_tail

2388. .type lj_vm_call_tail, @function

2389. .size lj_vm_call_tail, 56

2390. lj_vm_call_tail:

2391. .byte 137,209,247,195,3,0,0,0,117,15,15,182,107,253,72,247

2392. .byte 213,141,20,234,233,18,233,255,255,137,221,131,229,248,41,234

2393. .byte 233,6,233,255,255,190,20,0,0,0,137,239

2394. call lj_state_growstack

2395. .byte 139,85,16,49,192,235,164

2397. .globl lj_fff_gcstep

2398. .hidden lj_fff_gcstep

2399. .type lj_fff_gcstep, @function

2400. .size lj_fff_gcstep, 52

2401. lj_fff_gcstep:

2402. .byte 93,72,137,108,36,8,139,108,36,24,137,92,36,28,137,85

2403. .byte 16,141,68,194,248,137,239,137,69,24

2404. call lj_gc_step

2405. .byte 139,85,16,139,69,24,41,208,193,232,3,131,192,1,72,139

2406. .byte 108,36,8,85,195

2408. .globl lj_vm_record

2409. .hidden lj_vm_record

2410. .type lj_vm_record, @function

2411. .size lj_vm_record, 29

2412. lj_vm_record:

2413. .byte 65,15,182,134,157,244,255,255,168,32,117,83,168,16,117,56

2414. .byte 168,12,116,52,65,255,142,240,244,255,255,235,43

2416. .globl lj_vm_rethook

2417. .hidden lj_vm_rethook

2418. .type lj_vm_rethook, @function

2419. .size lj_vm_rethook, 14

2420. lj_vm_rethook:

2421. .byte 65,15,182,134,157,244,255,255,168,16,117,54,235,29

2423. .globl lj_vm_inshook

2424. .hidden lj_vm_inshook

2425. .type lj_vm_inshook, @function

2426. .size lj_vm_inshook, 68

2427. lj_vm_inshook:

2428. .byte 65,15,182,134,157,244,255,255,168,16,117,40,168,12,116,36

2429. .byte 65,255,142,240,244,255,255,116,4,168,4,116,23,139,108,36

2430. .byte 24,137,85,16,137,222,137,239

2431. call lj_dispatch_ins

2432. .byte 139,85,16,15,182,75,253,15,182,107,252,15,183,67,254,65

2433. .byte 255,164,238,208,4,0,0

2435. .globl lj_cont_hook

2436. .hidden lj_cont_hook

2437. .type lj_cont_hook, @function

2438. .size lj_cont_hook, 12

2439. lj_cont_hook:

2440. .byte 131,195,4,139,77,232,137,76,36,4,235,224

2442. .globl lj_vm_hotloop

2443. .hidden lj_vm_hotloop

2444. .type lj_vm_hotloop, @function

2445. .size lj_vm_hotloop, 50

2446. lj_vm_hotloop:

2447. .byte 139,106,248,139,109,16,15,182,69,199,141,4,194,139,108,36

2448. .byte 24,137,85,16,137,69,24,137,222,65,141,190,192,245,255,255

2449. .byte 73,137,174,32,246,255,255,137,92,36,28

2450. call lj_trace_hot

2451. .byte 235,171

2453. .globl lj_vm_callhook

2454. .hidden lj_vm_callhook

2455. .type lj_vm_callhook, @function

2456. .size lj_vm_callhook, 6

2457. lj_vm_callhook:

2458. .byte 137,92,36,28,235,7

2460. .globl lj_vm_hotcall

2461. .hidden lj_vm_hotcall

2462. .type lj_vm_hotcall, @function

2463. .size lj_vm_hotcall, 67

2464. lj_vm_hotcall:

2465. .byte 137,92,36,28,131,203,1,141,68,194,248,139,108,36,24,137

2466. .byte 85,16,137,69,24,137,222,137,239

2467. call lj_dispatch_call

2468. .byte 199,68,36,28,0,0,0,0,131,227,254,139,85,16,72,137

2469. .byte 193,139,69,24,41,208,72,137,205,15,182,75,253,193,232,3

2470. .byte 131,192,1,255,229

2472. .globl lj_cont_stitch

2473. .hidden lj_cont_stitch

2474. .type lj_cont_stitch, @function

2475. .size lj_cont_stitch, 166

2476. lj_cont_stitch:

2477. .byte 139,77,232,137,12,36,68,137,116,36,8,68,139,116,36,4

2478. .byte 15,182,75,253,141,12,202,65,131,238,1,116,18,72,139,40

2479. .byte 72,137,41,131,192,8,131,193,8,65,131,238,1,117,238,15

2480. .byte 182,67,253,15,182,107,255,1,232,141,68,194,248,57,200,119

2481. .byte 89,68,139,116,36,8,139,44,36,65,139,142,240,246,255,255

2482. .byte 139,4,169,133,192,15,132,162,233,255,255,15,183,64,82,57

2483. .byte 232,15,132,150,233,255,255,133,192,15,133,214,226,255,255,65

2484. .byte 137,174,44,255,255,255,139,108,36,24,137,85,16,137,222,65

2485. .byte 141,190,192,245,255,255,73,137,174,32,246,255,255

2486. call lj_dispatch_stitch

2487. .byte 139,85,16,233,99,233,255,255,199,65,4,255,255,255,255,131

2488. .byte 193,8,235,151

2490. .globl lj_vm_profhook

2491. .hidden lj_vm_profhook

2492. .type lj_vm_profhook, @function

2493. .size lj_vm_profhook, 27

2494. lj_vm_profhook:

2495. .byte 139,108,36,24,137,85,16,137,222,137,239

2496. call lj_dispatch_profile

2497. .byte 139,85,16,131,235,4,233,60,233,255,255

2499. .globl lj_vm_exit_handler

2500. .hidden lj_vm_exit_handler

2501. .type lj_vm_exit_handler, @function

2502. .size lj_vm_exit_handler, 247

2503. lj_vm_exit_handler:

2504. .byte 65,85,65,84,65,83,65,82,65,81,65,80,87,86,85,72

2505. .byte 141,108,36,88,85,83,82,81,80,15,182,69,248,138,101,240

2506. .byte 76,137,125,248,76,137,117,240,68,139,117,0,65,139,142,120

2507. .byte 244,255,255,65,199,134,120,244,255,255,252,255,255,255,65,137

2508. .byte 134,44,255,255,255,65,137,142,40,255,255,255,72,129,236,128

2509. .byte 0,0,0,72,131,197,128,242,68,15,17,125,248,242,68,15

2510. .byte 17,117,240,242,68,15,17,109,232,242,68,15,17,101,224,242

2511. .byte 68,15,17,93,216,242,68,15,17,85,208,242,68,15,17,77

2512. .byte 200,242,68,15,17,69,192,242,15,17,125,184,242,15,17,117

2513. .byte 176,242,15,17,109,168,242,15,17,101,160,242,15,17,93,152

2514. .byte 242,15,17,85,144,242,15,17,77,136,242,15,17,69,128,65

2515. .byte 139,174,24,245,255,255,65,139,150,28,245,255,255,73,137,174

2516. .byte 32,246,255,255,137,85,16,72,137,230,65,141,190,192,245,255

2517. .byte 255,65,199,134,28,245,255,255,0,0,0,0

2518. call lj_trace_exit

2519. .byte 72,139,77,48,72,131,225,252,72,137,204,137,105,24,139,85

2520. .byte 16,139,89,28,235,4

2522. .globl lj_vm_exit_interp

2523. .hidden lj_vm_exit_interp

2524. .type lj_vm_exit_interp, @function

2525. .size lj_vm_exit_interp, 137

2526. lj_vm_exit_interp:

2527. .byte 72,131,196,16,76,139,108,36,8,76,139,36,36,133,192,120

2528. .byte 109,139,108,36,24,137,68,36,4,68,139,122,248,69,139,127

2529. .byte 16,69,139,127,208,137,85,16,65,199,134,28,245,255,255,0

2530. .byte 0,0,0,65,199,134,120,244,255,255,255,255,255,255,139,3

2531. .byte 15,182,204,15,182,232,131,195,4,193,232,16,131,253,89,114

2532. .byte 9,131,253,97,115,8,139,68,36,4,65,255,36,238,139,66

2533. .byte 252,169,3,0,0,0,117,238,15,182,64,253,72,247,208,68

2534. .byte 139,124,194,248,69,139,127,16,69,139,127,208,235,216,247,216

2535. .byte 137,239,137,198

2536. call lj_err_throw

2538. .globl lj_vm_floor_sse

2539. .hidden lj_vm_floor_sse

2540. .type lj_vm_floor_sse, @function

2541. .size lj_vm_floor_sse, 0

2542. lj_vm_floor_sse:

2544. .globl lj_vm_floor

2545. .hidden lj_vm_floor

2546. .type lj_vm_floor, @function

2547. .size lj_vm_floor, 91

2548. lj_vm_floor:

2549. .byte 72,184,255,255,255,255,255,255,255,127,102,72,15,110,208,72

2550. .byte 184,0,0,0,0,0,0,48,67,102,72,15,110,216,15,40

2551. .byte 200,102,15,84,202,102,15,46,217,118,47,102,15,85,208,242

2552. .byte 15,88,203,242,15,92,203,102,15,86,202,72,184,0,0,0

2553. .byte 0,0,0,240,63,102,72,15,110,208,242,15,194,193,1,102

2554. .byte 15,84,194,242,15,92,200,15,40,193,195

2556. .globl lj_vm_ceil_sse

2557. .hidden lj_vm_ceil_sse

2558. .type lj_vm_ceil_sse, @function

2559. .size lj_vm_ceil_sse, 0

2560. lj_vm_ceil_sse:

2562. .globl lj_vm_ceil

2563. .hidden lj_vm_ceil

2564. .type lj_vm_ceil, @function

2565. .size lj_vm_ceil, 91

2566. lj_vm_ceil:

2567. .byte 72,184,255,255,255,255,255,255,255,127,102,72,15,110,208,72

2568. .byte 184,0,0,0,0,0,0,48,67,102,72,15,110,216,15,40

2569. .byte 200,102,15,84,202,102,15,46,217,118,47,102,15,85,208,242

2570. .byte 15,88,203,242,15,92,203,102,15,86,202,72,184,0,0,0

2571. .byte 0,0,0,240,191,102,72,15,110,208,242,15,194,193,6,102

2572. .byte 15,84,194,242,15,92,200,15,40,193,195

2574. .globl lj_vm_trunc_sse

2575. .hidden lj_vm_trunc_sse

2576. .type lj_vm_trunc_sse, @function

2577. .size lj_vm_trunc_sse, 0

2578. lj_vm_trunc_sse:

2580. .globl lj_vm_trunc

2581. .hidden lj_vm_trunc

2582. .type lj_vm_trunc, @function

2583. .size lj_vm_trunc, 94

2584. lj_vm_trunc:

2585. .byte 72,184,255,255,255,255,255,255,255,127,102,72,15,110,208,72

2586. .byte 184,0,0,0,0,0,0,48,67,102,72,15,110,216,15,40

2587. .byte 200,102,15,84,202,102,15,46,217,118,50,102,15,85,208,15

2588. .byte 40,193,242,15,88,203,242,15,92,203,72,184,0,0,0,0

2589. .byte 0,0,240,63,102,72,15,110,216,242,15,194,193,1,102,15

2590. .byte 84,195,242,15,92,200,102,15,86,202,15,40,193,195

2592. .globl lj_vm_mod

2593. .hidden lj_vm_mod

2594. .type lj_vm_mod, @function

2595. .size lj_vm_mod, 118

2596. lj_vm_mod:

2597. .byte 15,40,232,242,15,94,193,72,184,255,255,255,255,255,255,255

2598. .byte 127,102,72,15,110,208,72,184,0,0,0,0,0,0,48,67

2599. .byte 102,72,15,110,216,15,40,224,102,15,84,226,102,15,46,220

2600. .byte 118,56,102,15,85,208,242,15,88,227,242,15,92,227,102,15

2601. .byte 86,226,72,184,0,0,0,0,0,0,240,63,102,72,15,110

2602. .byte 208,242,15,194,196,1,102,15,84,194,242,15,92,224,15,40

2603. .byte 197,242,15,89,204,242,15,92,193,195,242,15,89,200,15,40

2604. .byte 197,242,15,92,193,195

2606. .globl lj_vm_powi_sse

2607. .hidden lj_vm_powi_sse

2608. .type lj_vm_powi_sse, @function

2609. .size lj_vm_powi_sse, 98

2610. lj_vm_powi_sse:

2611. .byte 131,248,1,126,43,169,1,0,0,0,117,8,242,15,89,192

2612. .byte 209,232,235,241,209,232,116,23,15,40,200,242,15,89,192,209

2613. .byte 232,116,8,115,246,242,15,89,200,235,240,242,15,89,193,195

2614. .byte 116,253,114,30,247,216,232,202,255,255,255,72,184,0,0,0

2615. .byte 0,0,0,240,63,102,72,15,110,200,242,15,94,200,15,40

2616. .byte 193,195,72,184,0,0,0,0,0,0,240,63,102,72,15,110

2617. .byte 192,195

2619. .globl lj_vm_cpuid

2620. .hidden lj_vm_cpuid

2621. .type lj_vm_cpuid, @function

2622. .size lj_vm_cpuid, 18

2623. lj_vm_cpuid:

2624. .byte 137,248,83,15,162,137,6,137,94,4,137,78,8,137,86,12

2625. .byte 91,195

2627. .globl lj_assert_bad_for_arg_type

2628. .hidden lj_assert_bad_for_arg_type

2629. .type lj_assert_bad_for_arg_type, @function

2630. .size lj_assert_bad_for_arg_type, 2

2631. lj_assert_bad_for_arg_type:

2632. .byte 204,204

2634. .globl lj_vm_ffi_callback

2635. .hidden lj_vm_ffi_callback

2636. .type lj_vm_ffi_callback, @function

2637. .size lj_vm_ffi_callback, 179

2638. lj_vm_ffi_callback:

2639. .byte 83,65,87,65,86,72,131,236,40,68,141,181,224,11,0,0

2640. .byte 139,157,4,1,0,0,15,183,192,137,131,208,0,0,0,72

2641. .byte 137,123,112,72,137,115,120,72,137,147,128,0,0,0,72,137

2642. .byte 139,136,0,0,0,242,15,17,67,48,242,15,17,75,56,242

2643. .byte 15,17,83,64,242,15,17,91,72,72,141,68,36,80,76,137

2644. .byte 131,144,0,0,0,76,137,139,152,0,0,0,242,15,17,99

2645. .byte 80,242,15,17,107,88,242,15,17,115,96,242,15,17,123,104

2646. .byte 72,137,131,176,0,0,0,72,137,230,137,92,36,28,137,223

2647. call lj_ccallback_enter

2648. .byte 65,199,134,120,244,255,255,255,255,255,255,139,80,16,139,64

2649. .byte 24,41,208,139,106,248,193,232,3,131,192,1,139,93,16,139

2650. .byte 11,15,182,233,15,182,205,131,195,4,65,255,36,238

2652. .globl lj_cont_ffi_callback

2653. .hidden lj_cont_ffi_callback

2654. .type lj_cont_ffi_callback, @function

2655. .size lj_cont_ffi_callback, 44

2656. lj_cont_ffi_callback:

2657. .byte 139,76,36,24,65,139,158,36,245,255,255,72,137,75,16,137

2658. .byte 81,16,137,105,24,137,223,137,198

2659. call lj_ccallback_leave

2660. .byte 72,139,67,112,242,15,16,67,48,233,186,224,255,255

2662. .globl lj_vm_ffi_call

2663. .hidden lj_vm_ffi_call

2664. .type lj_vm_ffi_call, @function

2665. .size lj_vm_ffi_call, 160

2666. lj_vm_ffi_call:

2667. .byte 85,72,137,229,83,72,137,251,139,67,8,72,41,196,15,182

2668. .byte 75,12,131,233,1,120,17,72,139,132,203,192,0,0,0,72

2669. .byte 137,4,204,131,233,1,121,239,15,182,67,15,72,139,187,144

2670. .byte 0,0,0,72,139,179,152,0,0,0,72,139,147,160,0,0

2671. .byte 0,72,139,139,168,0,0,0,76,139,131,176,0,0,0,76

2672. .byte 139,139,184,0,0,0,133,192,116,40,15,40,67,16,15,40

2673. .byte 75,32,15,40,83,48,15,40,91,64,131,248,4,118,19,15

2674. .byte 40,99,80,15,40,107,96,15,40,115,112,15,40,187,128,0

2675. .byte 0,0,255,19,72,137,131,144,0,0,0,15,41,67,16,72

2676. .byte 137,147,152,0,0,0,15,41,75,32,72,139,93,248,201,195

2678. .section .note.GNU-stack,"",@progbits

2679. .ident "DynASM 1.3.0"

2681. .section .debug_frame,"",@progbits

2682. .Lframe0:

2683. .long .LECIE0-.LSCIE0

2684. .LSCIE0:

2685. .long 0xffffffff

2686. .byte 0x1

2687. .string ""

2688. .uleb128 0x1

2689. .sleb128 -8

2690. .byte 0x10

2691. .byte 0xc

2692. .uleb128 0x7

2693. .uleb128 8

2694. .byte 0x80+0x10

2695. .uleb128 0x1

2696. .align 8

2697. .LECIE0:

2699. .LSFDE0:

2700. .long .LEFDE0-.LASFDE0

2701. .LASFDE0:

2702. .long .Lframe0

2703. .quad .Lbegin

2704. .quad 15760

2705. .byte 0xe

2706. .uleb128 80

2707. .byte 0x86

2708. .uleb128 0x2

2709. .byte 0x83

2710. .uleb128 0x3

2711. .byte 0x8f

2712. .uleb128 0x4

2713. .byte 0x8e

2714. .uleb128 0x5

2715. .align 8

2716. .LEFDE0:

2718. .LSFDE1:

2719. .long .LEFDE1-.LASFDE1

2720. .LASFDE1:

2721. .long .Lframe0

2722. .quad lj_vm_ffi_call

2723. .quad 160

2724. .byte 0xe

2725. .uleb128 16

2726. .byte 0x86

2727. .uleb128 0x2

2728. .byte 0xd

2729. .uleb128 0x6

2730. .byte 0x83

2731. .uleb128 0x3

2732. .align 8

2733. .LEFDE1:

2735. .section .eh_frame,"a",@progbits

2736. .Lframe1:

2737. .long .LECIE1-.LSCIE1

2738. .LSCIE1:

2739. .long 0

2740. .byte 0x1

2741. .string "zPR"

2742. .uleb128 0x1

2743. .sleb128 -8

2744. .byte 0x10

2745. .uleb128 6

2746. .byte 0x1b

2747. .long lj_err_unwind_dwarf-.

2748. .byte 0x1b

2749. .byte 0xc

2750. .uleb128 0x7

2751. .uleb128 8

2752. .byte 0x80+0x10

2753. .uleb128 0x1

2754. .align 8

2755. .LECIE1:

2757. .LSFDE2:

2758. .long .LEFDE2-.LASFDE2

2759. .LASFDE2:

2760. .long .LASFDE2-.Lframe1

2761. .long .Lbegin-.

2762. .long 15760

2763. .uleb128 0

2764. .byte 0xe

2765. .uleb128 80

2766. .byte 0x86

2767. .uleb128 0x2

2768. .byte 0x83

2769. .uleb128 0x3

2770. .byte 0x8f

2771. .uleb128 0x4

2772. .byte 0x8e

2773. .uleb128 0x5

2774. .align 8

2775. .LEFDE2:

2777. .Lframe2:

2778. .long .LECIE2-.LSCIE2

2779. .LSCIE2:

2780. .long 0

2781. .byte 0x1

2782. .string "zR"

2783. .uleb128 0x1

2784. .sleb128 -8

2785. .byte 0x10

2786. .uleb128 1

2787. .byte 0x1b

2788. .byte 0xc

2789. .uleb128 0x7

2790. .uleb128 8

2791. .byte 0x80+0x10

2792. .uleb128 0x1

2793. .align 8

2794. .LECIE2:

2796. .LSFDE3:

2797. .long .LEFDE3-.LASFDE3

2798. .LASFDE3:

2799. .long .LASFDE3-.Lframe2

2800. .long lj_vm_ffi_call-.

2801. .long 160

2802. .uleb128 0

2803. .byte 0xe

2804. .uleb128 16

2805. .byte 0x86

2806. .uleb128 0x2

2807. .byte 0xd

2808. .uleb128 0x6

2809. .byte 0x83

2810. .uleb128 0x3

2811. .align 8

2812. .LEFDE3:

One Level Up                               Top Level

One Level Up                               Top Level

src/lj_vm.s - luajit-2.0-src

1. .file "buildvm_x86.dasc"

2. .text

3. .p2align 4

5. .globl lj_vm_asm_begin

6. .hidden lj_vm_asm_begin

7. .type lj_vm_asm_begin, @object

8. .size lj_vm_asm_begin, 0

9. lj_vm_asm_begin:

10. .Lbegin:

12. .globl lj_BC_ISLT

13. .hidden lj_BC_ISLT

14. .type lj_BC_ISLT, @function

15. .size lj_BC_ISLT, 72

16. lj_BC_ISLT:

17. .byte 129,124,202,4,255,255,254,255,15,131,63,29,0,0,129,124

18. .byte 194,4,255,255,254,255,15,131,49,29,0,0,242,15,16,4

19. .byte 194,131,195,4,102,15,46,4,202,118,11,15,183,67,254,141

20. .byte 156,131,0,0,254,255,139,3,15,182,204,15,182,232,131,195

21. .byte 4,193,232,16,65,255,36,238

23. .globl lj_BC_ISGE

24. .hidden lj_BC_ISGE

25. .type lj_BC_ISGE, @function

26. .size lj_BC_ISGE, 72

27. lj_BC_ISGE:

28. .byte 129,124,202,4,255,255,254,255,15,131,247,28,0,0,129,124

29. .byte 194,4,255,255,254,255,15,131,233,28,0,0,242,15,16,4

30. .byte 194,131,195,4,102,15,46,4,202,119,11,15,183,67,254,141

31. .byte 156,131,0,0,254,255,139,3,15,182,204,15,182,232,131,195

32. .byte 4,193,232,16,65,255,36,238

34. .globl lj_BC_ISLE

35. .hidden lj_BC_ISLE

36. .type lj_BC_ISLE, @function

37. .size lj_BC_ISLE, 72

38. lj_BC_ISLE:

39. .byte 129,124,202,4,255,255,254,255,15,131,175,28,0,0,129,124

40. .byte 194,4,255,255,254,255,15,131,161,28,0,0,242,15,16,4

41. .byte 194,131,195,4,102,15,46,4,202,114,11,15,183,67,254,141

42. .byte 156,131,0,0,254,255,139,3,15,182,204,15,182,232,131,195

43. .byte 4,193,232,16,65,255,36,238

45. .globl lj_BC_ISGT

46. .hidden lj_BC_ISGT

47. .type lj_BC_ISGT, @function

48. .size lj_BC_ISGT, 72

49. lj_BC_ISGT:

50. .byte 129,124,202,4,255,255,254,255,15,131,103,28,0,0,129,124

51. .byte 194,4,255,255,254,255,15,131,89,28,0,0,242,15,16,4

52. .byte 194,131,195,4,102,15,46,4,202,115,11,15,183,67,254,141

53. .byte 156,131,0,0,254,255,139,3,15,182,204,15,182,232,131,195

54. .byte 4,193,232,16,65,255,36,238

56. .globl lj_BC_ISEQV

57. .hidden lj_BC_ISEQV

58. .type lj_BC_ISEQV, @function

59. .size lj_BC_ISEQV, 139

60. lj_BC_ISEQV:

61. .byte 139,108,194,4,131,195,4,129,253,255,255,254,255,115,53,129

62. .byte 124,202,4,255,255,254,255,115,43,242,15,16,4,202,102,15

63. .byte 46,4,194,122,13,117,11,15,183,67,254,141,156,131,0,0

64. .byte 254,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

65. .byte 65,255,36,238,131,253,245,15,132,88,28,0,0,131,124,202

66. .byte 4,245,15,132,77,28,0,0,57,108,202,4,117,212,131,253

67. .byte 253,115,196,139,12,202,139,4,194,57,193,116,186,131,253,244

68. .byte 119,192,131,253,243,114,187,139,105,16,133,237,116,180,246,69

69. .byte 6,16,117,174,49,237,233,253,27,0,0

71. .globl lj_BC_ISNEV

72. .hidden lj_BC_ISNEV

73. .type lj_BC_ISNEV, @function

74. .size lj_BC_ISNEV, 142

75. lj_BC_ISNEV:

76. .byte 139,108,194,4,131,195,4,129,253,255,255,254,255,115,53,129

77. .byte 124,202,4,255,255,254,255,115,43,242,15,16,4,202,102,15

78. .byte 46,4,194,122,2,116,11,15,183,67,254,141,156,131,0,0

79. .byte 254,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

80. .byte 65,255,36,238,131,253,245,15,132,205,27,0,0,131,124,202

81. .byte 4,245,15,132,194,27,0,0,57,108,202,4,117,201,131,253

82. .byte 253,115,207,139,12,202,139,4,194,57,193,116,197,131,253,244

83. .byte 119,181,131,253,243,114,176,139,105,16,133,237,116,169,246,69

84. .byte 6,16,117,163,189,1,0,0,0,233,111,27,0,0

86. .globl lj_BC_ISEQS

87. .hidden lj_BC_ISEQS

88. .type lj_BC_ISEQS, @function

89. .size lj_BC_ISEQS, 63

90. lj_BC_ISEQS:

91. .byte 72,247,208,139,108,202,4,131,195,4,131,253,251,117,38,139

92. .byte 12,202,65,59,12,135,117,11,15,183,67,254,141,156,131,0

93. .byte 0,254,255,139,3,15,182,204,15,182,232,131,195,4,193,232

94. .byte 16,65,255,36,238,131,253,245,117,233,233,77,27,0,0

96. .globl lj_BC_ISNES

97. .hidden lj_BC_ISNES

98. .type lj_BC_ISNES, @function

99. .size lj_BC_ISNES, 63

100. lj_BC_ISNES:

101. .byte 72,247,208,139,108,202,4,131,195,4,131,253,251,117,38,139

102. .byte 12,202,65,59,12,135,116,11,15,183,67,254,141,156,131,0

103. .byte 0,254,255,139,3,15,182,204,15,182,232,131,195,4,193,232

104. .byte 16,65,255,36,238,131,253,245,117,222,233,14,27,0,0

106. .globl lj_BC_ISEQN

107. .hidden lj_BC_ISEQN

108. .type lj_BC_ISEQN, @function

109. .size lj_BC_ISEQN, 69

110. lj_BC_ISEQN:

111. .byte 139,108,202,4,131,195,4,129,253,255,255,254,255,115,44,242

112. .byte 65,15,16,4,199,102,15,46,4,202,122,13,117,11,15,183

113. .byte 67,254,141,156,131,0,0,254,255,139,3,15,182,204,15,182

114. .byte 232,131,195,4,193,232,16,65,255,36,238,131,253,245,117,233

115. .byte 233,201,26,0,0

117. .globl lj_BC_ISNEN

118. .hidden lj_BC_ISNEN

119. .type lj_BC_ISNEN, @function

120. .size lj_BC_ISNEN, 69

121. lj_BC_ISNEN:

122. .byte 139,108,202,4,131,195,4,129,253,255,255,254,255,115,44,242

123. .byte 65,15,16,4,199,102,15,46,4,202,122,2,116,11,15,183

124. .byte 67,254,141,156,131,0,0,254,255,139,3,15,182,204,15,182

125. .byte 232,131,195,4,193,232,16,65,255,36,238,131,253,245,117,222

126. .byte 233,132,26,0,0

128. .globl lj_BC_ISEQP

129. .hidden lj_BC_ISEQP

130. .type lj_BC_ISEQP, @function

131. .size lj_BC_ISEQP, 53

132. lj_BC_ISEQP:

133. .byte 72,247,208,139,108,202,4,131,195,4,57,197,117,29,15,183

134. .byte 67,254,141,156,131,0,0,254,255,139,3,15,182,204,15,182

135. .byte 232,131,195,4,193,232,16,65,255,36,238,131,253,245,117,233

136. .byte 233,79,26,0,0

138. .globl lj_BC_ISNEP

139. .hidden lj_BC_ISNEP

140. .type lj_BC_ISNEP, @function

141. .size lj_BC_ISNEP, 52

142. lj_BC_ISNEP:

143. .byte 72,247,208,139,108,202,4,131,195,4,57,197,116,20,131,253

144. .byte 245,15,132,56,26,0,0,15,183,67,254,141,156,131,0,0

145. .byte 254,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

146. .byte 65,255,36,238

148. .globl lj_BC_ISTC

149. .hidden lj_BC_ISTC

150. .type lj_BC_ISTC, @function

151. .size lj_BC_ISTC, 51

152. lj_BC_ISTC:

153. .byte 139,108,194,4,131,195,4,131,253,254,115,21,137,108,202,4

154. .byte 139,44,194,137,44,202,15,183,67,254,141,156,131,0,0,254

155. .byte 255,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

156. .byte 255,36,238

158. .globl lj_BC_ISFC

159. .hidden lj_BC_ISFC

160. .type lj_BC_ISFC, @function

161. .size lj_BC_ISFC, 51

162. lj_BC_ISFC:

163. .byte 139,108,194,4,131,195,4,131,253,254,114,21,137,108,202,4

164. .byte 139,44,194,137,44,202,15,183,67,254,141,156,131,0,0,254

165. .byte 255,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

166. .byte 255,36,238

168. .globl lj_BC_IST

169. .hidden lj_BC_IST

170. .type lj_BC_IST, @function

171. .size lj_BC_IST, 41

172. lj_BC_IST:

173. .byte 139,108,194,4,131,195,4,131,253,254,115,11,15,183,67,254

174. .byte 141,156,131,0,0,254,255,139,3,15,182,204,15,182,232,131

175. .byte 195,4,193,232,16,65,255,36,238

177. .globl lj_BC_ISF

178. .hidden lj_BC_ISF

179. .type lj_BC_ISF, @function

180. .size lj_BC_ISF, 41

181. lj_BC_ISF:

182. .byte 139,108,194,4,131,195,4,131,253,254,114,11,15,183,67,254

183. .byte 141,156,131,0,0,254,255,139,3,15,182,204,15,182,232,131

184. .byte 195,4,193,232,16,65,255,36,238

186. .globl lj_BC_MOV

187. .hidden lj_BC_MOV

188. .type lj_BC_MOV, @function

189. .size lj_BC_MOV, 26

190. lj_BC_MOV:

191. .byte 72,139,44,194,72,137,44,202,139,3,15,182,204,15,182,232

192. .byte 131,195,4,193,232,16,65,255,36,238

194. .globl lj_BC_NOT

195. .hidden lj_BC_NOT

196. .type lj_BC_NOT, @function

197. .size lj_BC_NOT, 32

198. lj_BC_NOT:

199. .byte 49,237,131,124,194,4,254,131,213,253,137,108,202,4,139,3

200. .byte 15,182,204,15,182,232,131,195,4,193,232,16,65,255,36,238

202. .globl lj_BC_UNM

203. .hidden lj_BC_UNM

204. .type lj_BC_UNM, @function

205. .size lj_BC_UNM, 60

206. lj_BC_UNM:

207. .byte 129,124,194,4,255,255,254,255,15,131,69,25,0,0,242,15

208. .byte 16,4,194,72,184,0,0,0,0,0,0,0,128,102,72,15

209. .byte 110,200,15,87,193,242,15,17,4,202,139,3,15,182,204,15

210. .byte 182,232,131,195,4,193,232,16,65,255,36,238

212. .globl lj_BC_LEN

213. .hidden lj_BC_LEN

214. .type lj_BC_LEN, @function

215. .size lj_BC_LEN, 74

216. lj_BC_LEN:

217. .byte 131,124,194,4,251,117,34,139,4,194,15,87,192,242,15,42

218. .byte 64,12,242,15,17,4,202,139,3,15,182,204,15,182,232,131

219. .byte 195,4,193,232,16,65,255,36,238,131,124,194,4,244,15,133

220. .byte 47,25,0,0,139,60,194,137,213,232

221. .long lj_tab_len-.-4

222. .byte 242,15,42,192,137,234,15,182,75,253,235,200

224. .globl lj_BC_ADDVN

225. .hidden lj_BC_ADDVN

226. .type lj_BC_ADDVN, @function

227. .size lj_BC_ADDVN, 54

228. lj_BC_ADDVN:

229. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

230. .byte 169,24,0,0,242,15,16,4,234,242,65,15,88,4,199,242

231. .byte 15,17,4,202,139,3,15,182,204,15,182,232,131,195,4,193

232. .byte 232,16,65,255,36,238

234. .globl lj_BC_SUBVN

235. .hidden lj_BC_SUBVN

236. .type lj_BC_SUBVN, @function

237. .size lj_BC_SUBVN, 54

238. lj_BC_SUBVN:

239. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

240. .byte 115,24,0,0,242,15,16,4,234,242,65,15,92,4,199,242

241. .byte 15,17,4,202,139,3,15,182,204,15,182,232,131,195,4,193

242. .byte 232,16,65,255,36,238

244. .globl lj_BC_MULVN

245. .hidden lj_BC_MULVN

246. .type lj_BC_MULVN, @function

247. .size lj_BC_MULVN, 54

248. lj_BC_MULVN:

249. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

250. .byte 61,24,0,0,242,15,16,4,234,242,65,15,89,4,199,242

251. .byte 15,17,4,202,139,3,15,182,204,15,182,232,131,195,4,193

252. .byte 232,16,65,255,36,238

254. .globl lj_BC_DIVVN

255. .hidden lj_BC_DIVVN

256. .type lj_BC_DIVVN, @function

257. .size lj_BC_DIVVN, 54

258. lj_BC_DIVVN:

259. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

260. .byte 7,24,0,0,242,15,16,4,234,242,65,15,94,4,199,242

261. .byte 15,17,4,202,139,3,15,182,204,15,182,232,131,195,4,193

262. .byte 232,16,65,255,36,238

264. .globl lj_BC_MODVN

265. .hidden lj_BC_MODVN

266. .type lj_BC_MODVN, @function

267. .size lj_BC_MODVN, 59

268. lj_BC_MODVN:

269. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

270. .byte 209,23,0,0,242,15,16,4,234,242,65,15,16,12,199,232

271. .byte 237,46,0,0,242,15,17,4,202,139,3,15,182,204,15,182

272. .byte 232,131,195,4,193,232,16,65,255,36,238

274. .globl lj_BC_ADDNV

275. .hidden lj_BC_ADDNV

276. .type lj_BC_ADDNV, @function

277. .size lj_BC_ADDNV, 54

278. lj_BC_ADDNV:

279. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

280. .byte 156,23,0,0,242,65,15,16,4,199,242,15,88,4,234,242

281. .byte 15,17,4,202,139,3,15,182,204,15,182,232,131,195,4,193

282. .byte 232,16,65,255,36,238

284. .globl lj_BC_SUBNV

285. .hidden lj_BC_SUBNV

286. .type lj_BC_SUBNV, @function

287. .size lj_BC_SUBNV, 54

288. lj_BC_SUBNV:

289. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

290. .byte 102,23,0,0,242,65,15,16,4,199,242,15,92,4,234,242

291. .byte 15,17,4,202,139,3,15,182,204,15,182,232,131,195,4,193

292. .byte 232,16,65,255,36,238

294. .globl lj_BC_MULNV

295. .hidden lj_BC_MULNV

296. .type lj_BC_MULNV, @function

297. .size lj_BC_MULNV, 54

298. lj_BC_MULNV:

299. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

300. .byte 48,23,0,0,242,65,15,16,4,199,242,15,89,4,234,242

301. .byte 15,17,4,202,139,3,15,182,204,15,182,232,131,195,4,193

302. .byte 232,16,65,255,36,238

304. .globl lj_BC_DIVNV

305. .hidden lj_BC_DIVNV

306. .type lj_BC_DIVNV, @function

307. .size lj_BC_DIVNV, 54

308. lj_BC_DIVNV:

309. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

310. .byte 250,22,0,0,242,65,15,16,4,199,242,15,94,4,234,242

311. .byte 15,17,4,202,139,3,15,182,204,15,182,232,131,195,4,193

312. .byte 232,16,65,255,36,238

314. .globl lj_BC_MODNV

315. .hidden lj_BC_MODNV

316. .type lj_BC_MODNV, @function

317. .size lj_BC_MODNV, 36

318. lj_BC_MODNV:

319. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

320. .byte 196,22,0,0,242,65,15,16,4,199,242,15,16,12,234,233

321. .byte 232,254,255,255

323. .globl lj_BC_ADDVV

324. .hidden lj_BC_ADDVV

325. .type lj_BC_ADDVV, @function

326. .size lj_BC_ADDVV, 67

327. lj_BC_ADDVV:

328. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

329. .byte 177,22,0,0,129,124,194,4,255,255,254,255,15,131,163,22

330. .byte 0,0,242,15,16,4,234,242,15,88,4,194,242,15,17,4

331. .byte 202,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

332. .byte 255,36,238

334. .globl lj_BC_SUBVV

335. .hidden lj_BC_SUBVV

336. .type lj_BC_SUBVV, @function

337. .size lj_BC_SUBVV, 67

338. lj_BC_SUBVV:

339. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

340. .byte 110,22,0,0,129,124,194,4,255,255,254,255,15,131,96,22

341. .byte 0,0,242,15,16,4,234,242,15,92,4,194,242,15,17,4

342. .byte 202,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

343. .byte 255,36,238

345. .globl lj_BC_MULVV

346. .hidden lj_BC_MULVV

347. .type lj_BC_MULVV, @function

348. .size lj_BC_MULVV, 67

349. lj_BC_MULVV:

350. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

351. .byte 43,22,0,0,129,124,194,4,255,255,254,255,15,131,29,22

352. .byte 0,0,242,15,16,4,234,242,15,89,4,194,242,15,17,4

353. .byte 202,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

354. .byte 255,36,238

356. .globl lj_BC_DIVVV

357. .hidden lj_BC_DIVVV

358. .type lj_BC_DIVVV, @function

359. .size lj_BC_DIVVV, 67

360. lj_BC_DIVVV:

361. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

362. .byte 232,21,0,0,129,124,194,4,255,255,254,255,15,131,218,21

363. .byte 0,0,242,15,16,4,234,242,15,94,4,194,242,15,17,4

364. .byte 202,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

365. .byte 255,36,238

367. .globl lj_BC_MODVV

368. .hidden lj_BC_MODVV

369. .type lj_BC_MODVV, @function

370. .size lj_BC_MODVV, 49

371. lj_BC_MODVV:

372. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

373. .byte 165,21,0,0,129,124,194,4,255,255,254,255,15,131,151,21

374. .byte 0,0,242,15,16,4,234,242,15,16,12,194,233,171,253,255

375. .byte 255

377. .globl lj_BC_POW

378. .hidden lj_BC_POW

379. .type lj_BC_POW, @function

380. .size lj_BC_POW, 72

381. lj_BC_POW:

382. .byte 15,182,236,15,182,192,129,124,234,4,255,255,254,255,15,131

383. .byte 116,21,0,0,129,124,194,4,255,255,254,255,15,131,102,21

384. .byte 0,0,242,15,16,4,234,242,15,16,12,194,232,47,45,0

385. .byte 0,242,15,17,4,202,139,3,15,182,204,15,182,232,131,195

386. .byte 4,193,232,16,65,255,36,238

388. .globl lj_BC_CAT

389. .hidden lj_BC_CAT

390. .type lj_BC_CAT, @function

391. .size lj_BC_CAT, 76

392. lj_BC_CAT:

393. .byte 15,182,236,15,182,192,139,124,36,24,137,87,16,141,52,194

394. .byte 137,194,41,234,137,253,137,92,36,28,232

395. .long lj_meta_cat-.-4

396. .byte 139,85,16,133,192,15,133,71,21,0,0,15,182,107,255,15

397. .byte 182,75,253,72,139,4,234,72,137,4,202,139,3,15,182,204

398. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238

400. .globl lj_BC_KSTR

401. .hidden lj_BC_KSTR

402. .type lj_BC_KSTR, @function

403. .size lj_BC_KSTR, 36

404. lj_BC_KSTR:

405. .byte 72,247,208,65,139,4,135,199,68,202,4,251,255,255,255,137

406. .byte 4,202,139,3,15,182,204,15,182,232,131,195,4,193,232,16

407. .byte 65,255,36,238

409. .globl lj_BC_KCDATA

410. .hidden lj_BC_KCDATA

411. .type lj_BC_KCDATA, @function

412. .size lj_BC_KCDATA, 36

413. lj_BC_KCDATA:

414. .byte 72,247,208,65,139,4,135,199,68,202,4,245,255,255,255,137

415. .byte 4,202,139,3,15,182,204,15,182,232,131,195,4,193,232,16

416. .byte 65,255,36,238

418. .globl lj_BC_KSHORT

419. .hidden lj_BC_KSHORT

420. .type lj_BC_KSHORT, @function

421. .size lj_BC_KSHORT, 30

422. lj_BC_KSHORT:

423. .byte 15,191,192,242,15,42,192,242,15,17,4,202,139,3,15,182

424. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238

426. .globl lj_BC_KNUM

427. .hidden lj_BC_KNUM

428. .type lj_BC_KNUM, @function

429. .size lj_BC_KNUM, 29

430. lj_BC_KNUM:

431. .byte 242,65,15,16,4,199,242,15,17,4,202,139,3,15,182,204

432. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238

434. .globl lj_BC_KPRI

435. .hidden lj_BC_KPRI

436. .type lj_BC_KPRI, @function

437. .size lj_BC_KPRI, 25

438. lj_BC_KPRI:

439. .byte 72,247,208,137,68,202,4,139,3,15,182,204,15,182,232,131

440. .byte 195,4,193,232,16,65,255,36,238

442. .globl lj_BC_KNIL

443. .hidden lj_BC_KNIL

444. .type lj_BC_KNIL, @function

445. .size lj_BC_KNIL, 43

446. lj_BC_KNIL:

447. .byte 141,76,202,12,141,68,194,4,189,255,255,255,255,137,105,248

448. .byte 137,41,131,193,8,57,193,118,247,139,3,15,182,204,15,182

449. .byte 232,131,195,4,193,232,16,65,255,36,238

451. .globl lj_BC_UGET

452. .hidden lj_BC_UGET

453. .type lj_BC_UGET, @function

454. .size lj_BC_UGET, 36

455. lj_BC_UGET:

456. .byte 139,106,248,139,108,133,20,139,109,16,72,139,69,0,72,137

457. .byte 4,202,139,3,15,182,204,15,182,232,131,195,4,193,232,16

458. .byte 65,255,36,238

460. .globl lj_BC_USETV

461. .hidden lj_BC_USETV

462. .type lj_BC_USETV, @function

463. .size lj_BC_USETV, 87

464. lj_BC_USETV:

465. .byte 139,106,248,139,108,141,20,128,125,6,0,139,109,16,139,12

466. .byte 194,139,68,194,4,137,77,0,137,69,4,116,6,246,69,252

467. .byte 4,117,18,139,3,15,182,204,15,182,232,131,195,4,193,232

468. .byte 16,65,255,36,238,131,232,252,131,248,246,118,230,246,65,4

469. .byte 3,116,224,137,238,137,213,65,141,190,72,244,255,255,232

470. .long lj_gc_barrieruv-.-4

471. .byte 137,234,235,204

473. .globl lj_BC_USETS

474. .hidden lj_BC_USETS

475. .type lj_BC_USETS, @function

476. .size lj_BC_USETS, 82

477. lj_BC_USETS:

478. .byte 72,247,208,139,106,248,139,108,141,20,65,139,12,135,139,69

479. .byte 16,137,8,199,64,4,251,255,255,255,246,69,4,4,117,18

480. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

481. .byte 36,238,246,65,4,3,116,232,128,125,6,0,116,226,137,213

482. .byte 137,198,65,141,190,72,244,255,255,232

483. .long lj_gc_barrieruv-.-4

484. .byte 137,234,235,206

486. .globl lj_BC_USETN

487. .hidden lj_BC_USETN

488. .type lj_BC_USETN, @function

489. .size lj_BC_USETN, 38

490. lj_BC_USETN:

491. .byte 139,106,248,242,65,15,16,4,199,139,108,141,20,139,77,16

492. .byte 242,15,17,1,139,3,15,182,204,15,182,232,131,195,4,193

493. .byte 232,16,65,255,36,238

495. .globl lj_BC_USETP

496. .hidden lj_BC_USETP

497. .type lj_BC_USETP, @function

498. .size lj_BC_USETP, 34

499. lj_BC_USETP:

500. .byte 72,247,208,139,106,248,139,108,141,20,139,77,16,137,65,4

501. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

502. .byte 36,238

504. .globl lj_BC_UCLO

505. .hidden lj_BC_UCLO

506. .type lj_BC_UCLO, @function

507. .size lj_BC_UCLO, 51

508. lj_BC_UCLO:

509. .byte 141,156,131,0,0,254,255,139,108,36,24,131,125,40,0,116

510. .byte 16,137,85,16,141,52,202,137,239,232

511. .long lj_func_closeuv-.-4

512. .byte 139,85,16,139,3,15,182,204,15,182,232,131,195,4,193,232

513. .byte 16,65,255,36,238

515. .globl lj_BC_FNEW

516. .hidden lj_BC_FNEW

517. .type lj_BC_FNEW, @function

518. .size lj_BC_FNEW, 64

519. lj_BC_FNEW:

520. .byte 72,247,208,139,108,36,24,137,85,16,139,82,248,65,139,52

521. .byte 135,137,239,137,92,36,28,232

522. .long lj_func_newL_gc-.-4

523. .byte 139,85,16,15,182,75,253,137,4,202,199,68,202,4,247,255

524. .byte 255,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

525. .byte 65,255,36,238

527. .globl lj_BC_TNEW

528. .hidden lj_BC_TNEW

529. .type lj_BC_TNEW, @function

530. .size lj_BC_TNEW, 109

531. lj_BC_TNEW:

532. .byte 139,108,36,24,137,85,16,65,139,142,104,244,255,255,65,59

533. .byte 142,108,244,255,255,137,92,36,28,115,69,137,194,37,255,7

534. .byte 0,0,193,234,11,61,255,7,0,0,116,45,137,239,137,198

535. .byte 232

536. .long lj_tab_new-.-4

537. .byte 139,85,16,15,182,75,253,137,4,202,199,68,202,4,244,255

538. .byte 255,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

539. .byte 65,255,36,238,184,1,8,0,0,235,204,137,239,232

540. .long lj_gc_step_fixtop-.-4

541. .byte 15,183,67,254,235,174

543. .globl lj_BC_TDUP

544. .hidden lj_BC_TDUP

545. .type lj_BC_TDUP, @function

546. .size lj_BC_TDUP, 93

547. lj_BC_TDUP:

548. .byte 72,247,208,139,108,36,24,65,139,142,104,244,255,255,137,92

549. .byte 36,28,65,59,142,108,244,255,255,137,85,16,115,47,65,139

550. .byte 52,135,137,239,232

551. .long lj_tab_dup-.-4

552. .byte 139,85,16,15,182,75,253,137,4,202,199,68,202,4,244,255

553. .byte 255,255,139,3,15,182,204,15,182,232,131,195,4,193,232,16

554. .byte 65,255,36,238,137,239,232

555. .long lj_gc_step_fixtop-.-4

556. .byte 15,183,67,254,72,247,208,235,193

558. .globl lj_BC_GGET

559. .hidden lj_BC_GGET

560. .type lj_BC_GGET, @function

561. .size lj_BC_GGET, 18

562. lj_BC_GGET:

563. .byte 72,247,208,139,106,248,139,109,8,65,139,4,135,233,193,0

564. .byte 0,0

566. .globl lj_BC_GSET

567. .hidden lj_BC_GSET

568. .type lj_BC_GSET, @function

569. .size lj_BC_GSET, 18

570. lj_BC_GSET:

571. .byte 72,247,208,139,106,248,139,109,8,65,139,4,135,233,59,2

572. .byte 0,0

574. .globl lj_BC_TGETV

575. .hidden lj_BC_TGETV

576. .type lj_BC_TGETV, @function

577. .size lj_BC_TGETV, 148

578. lj_BC_TGETV:

579. .byte 15,182,236,15,182,192,131,124,234,4,244,15,133,243,15,0

580. .byte 0,139,44,234,129,124,194,4,255,255,254,255,115,102,242,15

581. .byte 16,4,194,242,15,45,192,242,15,42,200,102,15,46,193,15

582. .byte 133,207,15,0,0,59,69,24,15,131,198,15,0,0,193,224

583. .byte 3,3,69,8,131,120,4,255,116,25,72,139,40,72,137,44

584. .byte 202,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

585. .byte 255,36,238,131,125,16,0,116,17,139,77,16,246,65,6,1

586. .byte 15,132,142,15,0,0,15,182,75,253,199,68,202,4,255,255

587. .byte 255,255,235,205,131,124,194,4,251,15,133,117,15,0,0,139

588. .byte 4,194,235,27

590. .globl lj_BC_TGETS

591. .hidden lj_BC_TGETS

592. .type lj_BC_TGETS, @function

593. .size lj_BC_TGETS, 124

594. lj_BC_TGETS:

595. .byte 15,182,236,15,182,192,72,247,208,65,139,4,135,131,124,234

596. .byte 4,244,15,133,28,15,0,0,139,44,234,139,77,28,35,72

597. .byte 8,107,201,24,3,77,20,131,121,12,251,117,54,57,65,8

598. .byte 117,49,131,121,4,255,116,50,15,182,67,253,72,139,41,72

599. .byte 137,44,194,139,3,15,182,204,15,182,232,131,195,4,193,232

600. .byte 16,65,255,36,238,15,182,67,253,199,68,194,4,255,255,255

601. .byte 255,235,224,139,73,16,133,201,117,189,139,77,16,133,201,116

602. .byte 228,246,65,6,1,117,222,233,184,14,0,0

604. .globl lj_BC_TGETB

605. .hidden lj_BC_TGETB

606. .type lj_BC_TGETB, @function

607. .size lj_BC_TGETB, 99

608. lj_BC_TGETB:

609. .byte 15,182,236,15,182,192,131,124,234,4,244,15,133,208,14,0

610. .byte 0,139,44,234,59,69,24,15,131,196,14,0,0,193,224,3

611. .byte 3,69,8,131,120,4,255,116,25,72,139,40,72,137,44,202

612. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

613. .byte 36,238,131,125,16,0,116,17,139,77,16,246,65,6,1,15

614. .byte 132,140,14,0,0,15,182,75,253,199,68,202,4,255,255,255

615. .byte 255,235,205

617. .globl lj_BC_TSETV

618. .hidden lj_BC_TSETV

619. .type lj_BC_TSETV, @function

620. .size lj_BC_TSETV, 173

621. lj_BC_TSETV:

622. .byte 15,182,236,15,182,192,131,124,234,4,244,15,133,29,15,0

623. .byte 0,139,44,234,129,124,194,4,255,255,254,255,115,100,242,15

624. .byte 16,4,194,242,15,45,192,242,15,42,200,102,15,46,193,15

625. .byte 133,249,14,0,0,59,69,24,15,131,240,14,0,0,193,224

626. .byte 3,3,69,8,131,120,4,255,116,31,246,69,4,4,117,66

627. .byte 72,139,44,202,72,137,40,139,3,15,182,204,15,182,232,131

628. .byte 195,4,193,232,16,65,255,36,238,131,125,16,0,116,219,139

629. .byte 77,16,246,65,6,2,15,132,178,14,0,0,15,182,75,253

630. .byte 235,200,131,124,194,4,251,15,133,161,14,0,0,139,4,194

631. .byte 235,54,128,101,4,251,65,139,142,132,244,255,255,65,137,174

632. .byte 132,244,255,255,137,77,12,15,182,75,253,235,163

634. .globl lj_BC_TSETS

635. .hidden lj_BC_TSETS

636. .type lj_BC_TSETS, @function

637. .size lj_BC_TSETS, 229

638. lj_BC_TSETS:

639. .byte 15,182,236,15,182,192,72,247,208,65,139,4,135,131,124,234

640. .byte 4,244,15,133,45,14,0,0,139,44,234,139,77,28,35,72

641. .byte 8,107,201,24,198,69,6,0,3,77,20,131,121,12,251,117

642. .byte 77,57,65,8,117,72,131,121,4,255,116,39,246,69,4,4

643. .byte 15,133,133,0,0,0,15,182,67,253,72,139,44,194,72,137

644. .byte 41,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

645. .byte 255,36,238,131,125,16,0,116,211,137,12,36,139,77,16,246

646. .byte 65,6,2,15,132,204,13,0,0,139,12,36,235,190,139,73

647. .byte 16,133,201,117,166,139,77,16,133,201,116,10,246,65,6,2

648. .byte 15,132,175,13,0,0,137,4,36,199,68,36,4,251,255,255

649. .byte 255,137,108,36,8,139,124,36,24,137,87,16,72,141,20,36

650. .byte 137,238,137,253,137,92,36,28,232

651. .long lj_tab_newkey-.-4

652. .byte 139,85,16,139,108,36,8,137,193,233,113,255,255,255,128,101

653. .byte 4,251,65,139,134,132,244,255,255,65,137,174,132,244,255,255

654. .byte 137,69,12,233,97,255,255,255

656. .globl lj_BC_TSETB

657. .hidden lj_BC_TSETB

658. .type lj_BC_TSETB, @function

659. .size lj_BC_TSETB, 124

660. lj_BC_TSETB:

661. .byte 15,182,236,15,182,192,131,124,234,4,244,15,133,120,13,0

662. .byte 0,139,44,234,59,69,24,15,131,108,13,0,0,193,224,3

663. .byte 3,69,8,131,120,4,255,116,31,246,69,4,4,117,50,72

664. .byte 139,12,202,72,137,8,139,3,15,182,204,15,182,232,131,195

665. .byte 4,193,232,16,65,255,36,238,131,125,16,0,116,219,139,77

666. .byte 16,246,65,6,2,15,132,46,13,0,0,15,182,75,253,235

667. .byte 200,128,101,4,251,65,139,142,132,244,255,255,65,137,174,132

668. .byte 244,255,255,137,77,12,15,182,75,253,235,179

670. .globl lj_BC_TSETM

671. .hidden lj_BC_TSETM

672. .type lj_BC_TSETM, @function

673. .size lj_BC_TSETM, 142

674. lj_BC_TSETM:

675. .byte 68,137,60,36,69,139,60,199,141,12,202,139,105,248,246,69

676. .byte 4,4,117,99,139,68,36,4,131,232,1,116,37,68,1,248

677. .byte 59,69,24,119,51,68,41,248,65,193,231,3,68,3,125,8

678. .byte 72,139,41,131,193,8,73,137,47,65,131,199,8,131,232,1

679. .byte 117,238,68,139,60,36,139,3,15,182,204,15,182,232,131,195

680. .byte 4,193,232,16,65,255,36,238,139,124,36,24,137,87,16,137

681. .byte 238,137,194,137,253,137,92,36,28,232

682. .long lj_tab_reasize-.-4

683. .byte 139,85,16,15,182,75,253,235,145,128,101,4,251,65,139,134

684. .byte 132,244,255,255,65,137,174,132,244,255,255,137,69,12,235,134

686. .globl lj_BC_CALLM

687. .hidden lj_BC_CALLM

688. .type lj_BC_CALLM, @function

689. .size lj_BC_CALLM, 46

690. lj_BC_CALLM:

691. .byte 15,182,192,3,68,36,4,131,124,202,4,247,139,44,202,15

692. .byte 133,242,13,0,0,141,84,202,8,137,90,252,139,93,16,139

693. .byte 11,15,182,233,15,182,205,131,195,4,65,255,36,238

695. .globl lj_BC_CALL

696. .hidden lj_BC_CALL

697. .type lj_BC_CALL, @function

698. .size lj_BC_CALL, 42

699. lj_BC_CALL:

700. .byte 15,182,192,131,124,202,4,247,139,44,202,15,133,200,13,0

701. .byte 0,141,84,202,8,137,90,252,139,93,16,139,11,15,182,233

702. .byte 15,182,205,131,195,4,65,255,36,238

704. .globl lj_BC_CALLMT

705. .hidden lj_BC_CALLMT

706. .type lj_BC_CALLMT, @function

707. .size lj_BC_CALLMT, 4

708. lj_BC_CALLMT:

709. .byte 3,68,36,4

711. .globl lj_BC_CALLT

712. .hidden lj_BC_CALLT

713. .type lj_BC_CALLT, @function

714. .size lj_BC_CALLT, 148

715. lj_BC_CALLT:

716. .byte 141,76,202,8,65,137,215,139,105,248,131,121,252,247,15,133

717. .byte 155,13,0,0,139,90,252,247,195,3,0,0,0,117,91,137

718. .byte 106,248,137,68,36,4,131,232,1,116,21,72,139,41,131,193

719. .byte 8,73,137,47,65,131,199,8,131,232,1,117,238,139,106,248

720. .byte 139,68,36,4,128,125,6,1,119,18,139,93,16,139,11,15

721. .byte 182,233,15,182,205,131,195,4,65,255,36,238,247,195,3,0

722. .byte 0,0,117,230,15,182,75,253,72,247,209,68,139,124,202,248

723. .byte 69,139,127,16,69,139,127,208,235,208,131,235,3,247,195,7

724. .byte 0,0,0,117,10,41,218,65,137,215,139,90,252,235,144,131

725. .byte 195,3,235,139

727. .globl lj_BC_ITERC

728. .hidden lj_BC_ITERC

729. .type lj_BC_ITERC, @function

730. .size lj_BC_ITERC, 68

731. lj_BC_ITERC:

732. .byte 141,76,202,8,72,139,105,232,72,139,65,240,72,137,41,72

733. .byte 137,65,8,139,105,224,139,65,228,137,105,248,137,65,252,131

734. .byte 248,247,184,3,0,0,0,15,133,238,12,0,0,137,202,137

735. .byte 90,252,139,93,16,139,11,15,182,233,15,182,205,131,195,4

736. .byte 65,255,36,238

738. .globl lj_BC_ITERN

739. .hidden lj_BC_ITERN

740. .type lj_BC_ITERN, @function

741. .size lj_BC_ITERN, 165

742. lj_BC_ITERN:

743. .byte 68,137,60,36,68,137,116,36,4,139,108,202,240,139,68,202

744. .byte 248,68,139,117,24,131,195,4,68,139,125,8,68,57,240,115

745. .byte 76,65,131,124,199,4,255,116,63,242,15,42,192,73,139,44

746. .byte 199,72,137,108,202,8,131,192,1,242,15,17,4,202,137,68

747. .byte 202,248,15,183,67,254,141,156,131,0,0,254,255,68,139,116

748. .byte 36,4,68,139,60,36,139,3,15,182,204,15,182,232,131,195

749. .byte 4,193,232,16,65,255,36,238,131,192,1,235,175,68,41,240

750. .byte 59,69,28,119,216,68,107,248,24,68,3,125,20,65,131,127

751. .byte 4,255,116,28,70,141,116,48,1,73,139,111,8,73,139,7

752. .byte 72,137,44,202,72,137,68,202,8,68,137,116,202,248,235,162

753. .byte 131,192,1,235,203

755. .globl lj_BC_VARG

756. .hidden lj_BC_VARG

757. .type lj_BC_VARG, @function

758. .size lj_BC_VARG, 191

759. lj_BC_VARG:

760. .byte 15,182,236,15,182,192,68,137,60,36,68,141,124,194,11,141

761. .byte 12,202,68,43,122,252,133,237,116,68,141,108,233,248,65,57

762. .byte 215,115,23,73,139,71,248,65,131,199,8,72,137,1,131,193

763. .byte 8,57,233,115,19,65,57,215,114,233,199,65,4,255,255,255

764. .byte 255,131,193,8,57,233,114,242,68,139,60,36,139,3,15,182

765. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238,199,68

766. .byte 36,4,1,0,0,0,137,208,68,41,248,118,219,137,197,193

767. .byte 237,3,131,197,1,137,108,36,4,139,108,36,24,1,200,59

768. .byte 69,32,119,21,73,139,71,248,65,131,199,8,72,137,1,131

769. .byte 193,8,65,57,215,114,237,235,175,137,85,16,137,77,24,137

770. .byte 92,36,28,65,41,215,139,116,36,4,131,238,1,137,239,232

771. .long lj_state_growstack-.-4

772. .byte 139,85,16,139,77,24,65,1,215,235,197

774. .globl lj_BC_ISNEXT

775. .hidden lj_BC_ISNEXT

776. .type lj_BC_ISNEXT, @function

777. .size lj_BC_ISNEXT, 88

778. lj_BC_ISNEXT:

779. .byte 131,124,202,236,247,117,65,139,108,202,232,131,124,202,244,244

780. .byte 117,54,131,124,202,252,255,117,47,128,125,6,4,117,41,141

781. .byte 156,131,0,0,254,255,199,68,202,248,0,0,0,0,199,68

782. .byte 202,252,255,127,254,255,139,3,15,182,204,15,182,232,131,195

783. .byte 4,193,232,16,65,255,36,238,198,67,252,84,141,156,131,0

784. .byte 0,254,255,198,3,65,235,222

786. .globl lj_BC_RETM

787. .hidden lj_BC_RETM

788. .type lj_BC_RETM, @function

789. .size lj_BC_RETM, 4

790. lj_BC_RETM:

791. .byte 3,68,36,4

793. .globl lj_BC_RET

794. .hidden lj_BC_RET

795. .type lj_BC_RET, @function

796. .size lj_BC_RET, 136

797. lj_BC_RET:

798. .byte 193,225,3,139,90,252,137,68,36,4,247,195,3,0,0,0

799. .byte 117,94,65,137,215,131,232,1,116,17,73,139,44,15,73,137

800. .byte 111,248,65,131,199,8,131,232,1,117,239,139,68,36,4,15

801. .byte 182,107,255,57,197,119,40,15,182,75,253,72,247,209,141,20

802. .byte 202,68,139,122,248,69,139,127,16,69,139,127,208,139,3,15

803. .byte 182,204,15,182,232,131,195,4,193,232,16,65,255,36,238,65

804. .byte 199,71,252,255,255,255,255,65,131,199,8,131,192,1,235,195

805. .byte 141,107,253,247,197,7,0,0,0,15,133,36,5,0,0,41

806. .byte 234,1,233,233,123,255,255,255

808. .globl lj_BC_RET0

809. .hidden lj_BC_RET0

810. .type lj_BC_RET0, @function

811. .size lj_BC_RET0, 92

812. lj_BC_RET0:

813. .byte 139,90,252,137,68,36,4,247,195,3,0,0,0,117,58,56

814. .byte 67,255,119,40,15,182,75,253,72,247,209,141,20,202,68,139

815. .byte 122,248,69,139,127,16,69,139,127,208,139,3,15,182,204,15

816. .byte 182,232,131,195,4,193,232,16,65,255,36,238,199,68,194,244

817. .byte 255,255,255,255,131,192,1,235,198,141,107,253,247,197,7,0

818. .byte 0,0,15,133,195,4,0,0,41,234,235,164

820. .globl lj_BC_RET1

821. .hidden lj_BC_RET1

822. .type lj_BC_RET1, @function

823. .size lj_BC_RET1, 105

824. lj_BC_RET1:

825. .byte 193,225,3,139,90,252,137,68,36,4,247,195,3,0,0,0

826. .byte 117,66,72,139,44,10,72,137,106,248,56,67,255,119,40,15

827. .byte 182,75,253,72,247,209,141,20,202,68,139,122,248,69,139,127

828. .byte 16,69,139,127,208,139,3,15,182,204,15,182,232,131,195,4

829. .byte 193,232,16,65,255,36,238,199,68,194,244,255,255,255,255,131

830. .byte 192,1,235,198,141,107,253,247,197,7,0,0,0,15,133,92

831. .byte 4,0,0,41,234,1,233,235,154

833. .globl lj_BC_FORI

834. .hidden lj_BC_FORI

835. .type lj_BC_FORI, @function

836. .size lj_BC_FORI, 97

837. lj_BC_FORI:

838. .byte 141,12,202,129,121,4,255,255,254,255,15,131,11,10,0,0

839. .byte 129,121,12,255,255,254,255,15,131,254,9,0,0,139,105,20

840. .byte 129,253,255,255,254,255,15,131,239,9,0,0,242,15,16,1

841. .byte 242,15,16,73,8,124,36,102,15,46,200,242,15,17,65,24

842. .byte 115,7,141,156,131,0,0,254,255,139,3,15,182,204,15,182

843. .byte 232,131,195,4,193,232,16,65,255,36,238,102,15,46,193,235

844. .byte 218

846. .globl lj_BC_JFORI

847. .hidden lj_BC_JFORI

848. .type lj_BC_JFORI, @function

849. .size lj_BC_JFORI, 105

850. lj_BC_JFORI:

851. .byte 141,12,202,129,121,4,255,255,254,255,15,131,170,9,0,0

852. .byte 129,121,12,255,255,254,255,15,131,157,9,0,0,139,105,20

853. .byte 129,253,255,255,254,255,15,131,142,9,0,0,242,15,16,1

854. .byte 242,15,16,73,8,124,44,102,15,46,200,242,15,17,65,24

855. .byte 141,156,131,0,0,254,255,15,183,67,254,15,131,118,1,0

856. .byte 0,139,3,15,182,204,15,182,232,131,195,4,193,232,16,65

857. .byte 255,36,238,102,15,46,193,235,210

859. .globl lj_BC_FORL

860. .hidden lj_BC_FORL

861. .type lj_BC_FORL, @function

862. .size lj_BC_FORL, 20

863. lj_BC_FORL:

864. .byte 137,221,209,237,131,229,126,102,65,131,108,46,128,2,15,130

865. .byte 187,28,0,0

867. .globl lj_BC_IFORL

868. .hidden lj_BC_IFORL

869. .type lj_BC_IFORL, @function

870. .size lj_BC_IFORL, 96

871. lj_BC_IFORL:

872. .byte 141,12,202,129,121,12,255,255,254,255,15,131,222,34,0,0

873. .byte 129,121,20,255,255,254,255,15,131,209,34,0,0,139,105,20

874. .byte 242,15,16,1,242,15,16,73,8,242,15,88,65,16,242,15

875. .byte 17,1,133,237,120,36,102,15,46,200,242,15,17,65,24,114

876. .byte 7,141,156,131,0,0,254,255,139,3,15,182,204,15,182,232

877. .byte 131,195,4,193,232,16,65,255,36,238,102,15,46,193,235,218

879. .globl lj_BC_JFORL

880. .hidden lj_BC_JFORL

881. .type lj_BC_JFORL, @function

882. .size lj_BC_JFORL, 93

883. lj_BC_JFORL:

884. .byte 141,12,202,129,121,12,255,255,254,255,15,131,126,34,0,0

885. .byte 129,121,20,255,255,254,255,15,131,113,34,0,0,139,105,20

886. .byte 242,15,16,1,242,15,16,73,8,242,15,88,65,16,242,15

887. .byte 17,1,133,237,120,33,102,15,46,200,242,15,17,65,24,15

888. .byte 131,165,0,0,0,139,3,15,182,204,15,182,232,131,195,4

889. .byte 193,232,16,65,255,36,238,102,15,46,193,235,221

891. .globl lj_BC_ITERL

892. .hidden lj_BC_ITERL

893. .type lj_BC_ITERL, @function

894. .size lj_BC_ITERL, 20

895. lj_BC_ITERL:

896. .byte 137,221,209,237,131,229,126,102,65,131,108,46,128,2,15,130

897. .byte 234,27,0,0

899. .globl lj_BC_IITERL

900. .hidden lj_BC_IITERL

901. .type lj_BC_IITERL, @function

902. .size lj_BC_IITERL, 44

903. lj_BC_IITERL:

904. .byte 141,12,202,139,105,4,131,253,255,116,15,141,156,131,0,0

905. .byte 254,255,139,1,137,105,252,137,65,248,139,3,15,182,204,15

906. .byte 182,232,131,195,4,193,232,16,65,255,36,238

908. .globl lj_BC_JITERL

909. .hidden lj_BC_JITERL

910. .type lj_BC_JITERL, @function

911. .size lj_BC_JITERL, 39

912. lj_BC_JITERL:

913. .byte 141,12,202,139,105,4,131,253,255,116,10,137,105,252,139,41

914. .byte 137,105,248,235,56,139,3,15,182,204,15,182,232,131,195,4

915. .byte 193,232,16,65,255,36,238

917. .globl lj_BC_LOOP

918. .hidden lj_BC_LOOP

919. .type lj_BC_LOOP, @function

920. .size lj_BC_LOOP, 20

921. lj_BC_LOOP:

922. .byte 137,221,209,237,131,229,126,102,65,131,108,46,128,2,15,130

923. .byte 131,27,0,0

925. .globl lj_BC_ILOOP

926. .hidden lj_BC_ILOOP

927. .type lj_BC_ILOOP, @function

928. .size lj_BC_ILOOP, 18

929. lj_BC_ILOOP:

930. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

931. .byte 36,238

933. .globl lj_BC_JLOOP

934. .hidden lj_BC_JLOOP

935. .type lj_BC_JLOOP, @function

936. .size lj_BC_JLOOP, 47

937. lj_BC_JLOOP:

938. .byte 65,139,142,16,247,255,255,139,4,129,72,139,64,64,139,108

939. .byte 36,24,65,137,150,64,245,255,255,65,137,174,60,245,255,255

940. .byte 76,137,36,36,76,137,108,36,8,72,131,236,16,255,224

942. .globl lj_BC_JMP

943. .hidden lj_BC_JMP

944. .type lj_BC_JMP, @function

945. .size lj_BC_JMP, 25

946. lj_BC_JMP:

947. .byte 141,156,131,0,0,254,255,139,3,15,182,204,15,182,232,131

948. .byte 195,4,193,232,16,65,255,36,238

950. .globl lj_BC_FUNCF

951. .hidden lj_BC_FUNCF

952. .type lj_BC_FUNCF, @function

953. .size lj_BC_FUNCF, 20

954. lj_BC_FUNCF:

955. .byte 137,221,209,237,131,229,126,102,65,131,108,46,128,1,15,130

956. .byte 77,27,0,0

958. .globl lj_BC_IFUNCF

959. .hidden lj_BC_IFUNCF

960. .type lj_BC_IFUNCF, @function

961. .size lj_BC_IFUNCF, 63

962. lj_BC_IFUNCF:

963. .byte 68,139,123,204,139,108,36,24,141,12,202,59,77,32,15,135

964. .byte 209,2,0,0,15,182,75,194,57,200,118,18,139,3,15,182

965. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238,199,68

966. .byte 194,252,255,255,255,255,131,192,1,57,200,118,241,235,221

968. .globl lj_BC_JFUNCF

969. .hidden lj_BC_JFUNCF

970. .type lj_BC_JFUNCF, @function

971. .size lj_BC_JFUNCF, 54

972. lj_BC_JFUNCF:

973. .byte 68,139,123,204,139,108,36,24,141,12,202,59,77,32,15,135

974. .byte 146,2,0,0,15,182,75,194,57,200,118,9,15,183,67,254

975. .byte 233,64,255,255,255,199,68,194,252,255,255,255,255,131,192,1

976. .byte 57,200,118,241,235,230

978. .globl lj_BC_FUNCV

979. .hidden lj_BC_FUNCV

980. .type lj_BC_FUNCV, @function

981. .size lj_BC_FUNCV, 0

982. lj_BC_FUNCV:

984. .globl lj_BC_IFUNCV

985. .hidden lj_BC_IFUNCV

986. .type lj_BC_IFUNCV, @function

987. .size lj_BC_IFUNCV, 125

988. lj_BC_IFUNCV:

989. .byte 141,44,197,3,0,0,0,141,4,194,68,139,122,248,137,104

990. .byte 252,68,137,120,248,139,108,36,24,141,12,200,59,77,32,15

991. .byte 135,70,2,0,0,137,209,137,194,15,182,107,194,133,237,116

992. .byte 37,131,193,8,57,209,115,52,68,139,121,248,68,137,56,68

993. .byte 139,121,252,68,137,120,4,131,192,8,199,65,252,255,255,255

994. .byte 255,131,237,1,117,219,68,139,123,204,139,3,15,182,204,15

995. .byte 182,232,131,195,4,193,232,16,65,255,36,238,199,64,4,255

996. .byte 255,255,255,131,192,8,131,237,1,117,241,235,217

998. .globl lj_BC_JFUNCV

999. .hidden lj_BC_JFUNCV

1000. .type lj_BC_JFUNCV, @function

1001. .size lj_BC_JFUNCV, 1

1002. lj_BC_JFUNCV:

1003. .byte 204

1005. .globl lj_BC_FUNCC

1006. .hidden lj_BC_FUNCC

1007. .type lj_BC_FUNCC, @function

1008. .size lj_BC_FUNCC, 79

1009. lj_BC_FUNCC:

1010. .byte 139,106,248,76,139,125,24,139,108,36,24,141,68,194,248,137

1011. .byte 85,16,141,136,160,0,0,0,59,77,32,137,69,24,137,239

1012. .byte 15,135,192,1,0,0,65,199,134,48,245,255,255,254,255,255

1013. .byte 255,65,255,215,65,199,134,48,245,255,255,255,255,255,255,139

1014. .byte 85,16,141,12,194,247,217,3,77,24,139,90,252,235,119

1016. .globl lj_BC_FUNCCW

1017. .hidden lj_BC_FUNCCW

1018. .type lj_BC_FUNCCW, @function

1019. .size lj_BC_FUNCCW, 86

1020. lj_BC_FUNCCW:

1021. .byte 139,106,248,76,139,125,24,139,108,36,24,141,68,194,248,137

1022. .byte 85,16,141,136,160,0,0,0,59,77,32,137,69,24,76,137

1023. .byte 254,137,239,15,135,110,1,0,0,65,199,134,48,245,255,255

1024. .byte 254,255,255,255,65,255,150,32,245,255,255,65,199,134,48,245

1025. .byte 255,255,255,255,255,255,139,85,16,141,12,194,247,217,3,77

1026. .byte 24,139,90,252,235,33

1028. .globl lj_vm_returnp

1029. .hidden lj_vm_returnp

1030. .type lj_vm_returnp, @function

1031. .size lj_vm_returnp, 33

1032. lj_vm_returnp:

1033. .byte 247,195,4,0,0,0,15,132,216,2,0,0,131,227,248,41

1034. .byte 218,72,141,76,25,248,139,90,252,199,68,10,4,253,255,255

1035. .byte 255

1037. .globl lj_vm_returnc

1038. .hidden lj_vm_returnc

1039. .type lj_vm_returnc, @function

1040. .size lj_vm_returnc, 25

1041. lj_vm_returnc:

1042. .byte 131,192,1,15,132,167,0,0,0,137,68,36,4,247,195,3

1043. .byte 0,0,0,15,132,111,250,255,255

1045. .globl lj_vm_return

1046. .hidden lj_vm_return

1047. .type lj_vm_return, @function

1048. .size lj_vm_return, 75

1049. lj_vm_return:

1050. .byte 131,243,1,247,195,3,0,0,0,117,187,65,199,134,48,245

1051. .byte 255,255,254,255,255,255,131,227,248,41,211,247,219,131,232,1

1052. .byte 116,16,72,139,44,10,72,137,106,248,131,194,8,131,232,1

1053. .byte 117,240,139,108,36,24,137,93,16,139,68,36,4,139,76,36

1054. .byte 16,57,193,117,28,131,234,8,137,85,24

1056. .globl lj_vm_leave_cp

1057. .hidden lj_vm_leave_cp

1058. .type lj_vm_leave_cp, @function

1059. .size lj_vm_leave_cp, 11

1060. lj_vm_leave_cp:

1061. .byte 72,139,76,36,32,72,137,77,48,49,192

1063. .globl lj_vm_leave_unw

1064. .hidden lj_vm_leave_unw

1065. .type lj_vm_leave_unw, @function

1066. .size lj_vm_leave_unw, 65

1067. lj_vm_leave_unw:

1068. .byte 72,131,196,40,65,94,65,95,91,93,195,114,20,59,85,32

1069. .byte 119,26,199,66,252,255,255,255,255,131,194,8,131,192,1,235

1070. .byte 202,133,201,116,202,41,193,141,20,202,235,195,137,85,24,137

1071. .byte 68,36,4,137,206,137,239,232

1072. .long lj_state_growstack-.-4

1073. .byte 139,85,24,235,162

1075. .globl lj_vm_unwind_yield

1076. .hidden lj_vm_unwind_yield

1077. .type lj_vm_unwind_yield, @function

1078. .size lj_vm_unwind_yield, 4

1079. lj_vm_unwind_yield:

1080. .byte 176,1,235,5

1082. .globl lj_vm_unwind_c

1083. .hidden lj_vm_unwind_c

1084. .type lj_vm_unwind_c, @function

1085. .size lj_vm_unwind_c, 5

1086. lj_vm_unwind_c:

1087. .byte 137,240,72,137,252

1089. .globl lj_vm_unwind_c_eh

1090. .hidden lj_vm_unwind_c_eh

1091. .type lj_vm_unwind_c_eh, @function

1092. .size lj_vm_unwind_c_eh, 19

1093. lj_vm_unwind_c_eh:

1094. .byte 139,108,36,24,139,109,8,199,133,232,0,0,0,254,255,255

1095. .byte 255,235,163

1097. .globl lj_vm_unwind_rethrow

1098. .hidden lj_vm_unwind_rethrow

1099. .type lj_vm_unwind_rethrow, @function

1100. .size lj_vm_unwind_rethrow, 21

1101. lj_vm_unwind_rethrow:

1102. .byte 139,124,36,24,137,198,72,131,196,40,65,94,65,95,91,93

1103. .byte 233

1104. .long lj_err_throw-.-4

1106. .globl lj_vm_unwind_ff

1107. .hidden lj_vm_unwind_ff

1108. .type lj_vm_unwind_ff, @function

1109. .size lj_vm_unwind_ff, 7

1110. lj_vm_unwind_ff:

1111. .byte 72,131,231,252,72,137,252

1113. .globl lj_vm_unwind_ff_eh

1114. .hidden lj_vm_unwind_ff_eh

1115. .type lj_vm_unwind_ff_eh, @function

1116. .size lj_vm_unwind_ff_eh, 56

1117. lj_vm_unwind_ff_eh:

1118. .byte 139,108,36,24,72,199,193,248,255,255,255,184,2,0,0,0

1119. .byte 139,85,16,68,139,117,8,65,129,198,184,11,0,0,139,90

1120. .byte 252,199,66,252,254,255,255,255,65,199,134,48,245,255,255,255

1121. .byte 255,255,255,233,224,254,255,255

1123. .globl lj_vm_growstack_c

1124. .hidden lj_vm_growstack_c

1125. .type lj_vm_growstack_c, @function

1126. .size lj_vm_growstack_c, 7

1127. lj_vm_growstack_c:

1128. .byte 190,20,0,0,0,235,28

1130. .globl lj_vm_growstack_v

1131. .hidden lj_vm_growstack_v

1132. .type lj_vm_growstack_v, @function

1133. .size lj_vm_growstack_v, 5

1134. lj_vm_growstack_v:

1135. .byte 131,232,8,235,4

1137. .globl lj_vm_growstack_f

1138. .hidden lj_vm_growstack_f

1139. .type lj_vm_growstack_f, @function

1140. .size lj_vm_growstack_f, 65

1141. lj_vm_growstack_f:

1142. .byte 141,68,194,248,15,182,75,195,131,195,4,137,85,16,137,69

1143. .byte 24,137,92,36,28,137,206,137,239,232

1144. .long lj_state_growstack-.-4

1145. .byte 139,85,16,139,69,24,139,106,248,41,208,193,232,3,131,192

1146. .byte 1,139,93,16,139,11,15,182,233,15,182,205,131,195,4,65

1147. .byte 255,36,238

1149. .globl lj_vm_resume

1150. .hidden lj_vm_resume

1151. .type lj_vm_resume, @function

1152. .size lj_vm_resume, 125

1153. lj_vm_resume:

1154. .byte 85,83,65,87,65,86,72,131,236,40,137,253,137,124,36,24

1155. .byte 137,241,187,5,0,0,0,49,192,76,141,124,36,1,68,139

1156. .byte 117,8,65,129,198,184,11,0,0,76,137,125,48,137,68,36

1157. .byte 28,72,137,68,36,32,137,68,36,16,137,68,36,20,56,69

1158. .byte 7,15,132,130,0,0,0,65,199,134,48,245,255,255,255,255

1159. .byte 255,255,136,69,7,139,85,16,139,69,24,41,200,193,232,3

1160. .byte 131,192,1,41,209,139,90,252,137,68,36,4,247,195,3,0

1161. .byte 0,0,15,132,163,248,255,255,233,47,254,255,255

1163. .globl lj_vm_pcall

1164. .hidden lj_vm_pcall

1165. .type lj_vm_pcall, @function

1166. .size lj_vm_pcall, 21

1167. lj_vm_pcall:

1168. .byte 85,83,65,87,65,86,72,131,236,40,187,5,0,0,0,137

1169. .byte 76,36,20,235,15

1171. .globl lj_vm_call

1172. .hidden lj_vm_call

1173. .type lj_vm_call, @function

1174. .size lj_vm_call, 84

1175. lj_vm_call:

1176. .byte 85,83,65,87,65,86,72,131,236,40,187,1,0,0,0,137

1177. .byte 84,36,16,137,253,137,124,36,24,137,241,76,139,125,48,76

1178. .byte 137,124,36,32,137,108,36,28,72,137,101,48,68,139,117,8

1179. .byte 65,129,198,184,11,0,0,65,199,134,48,245,255,255,255,255

1180. .byte 255,255,139,85,16,1,203,41,211,139,69,24,41,200,193,232

1181. .byte 3,131,192,1

1183. .globl lj_vm_call_dispatch

1184. .hidden lj_vm_call_dispatch

1185. .type lj_vm_call_dispatch, @function

1186. .size lj_vm_call_dispatch, 13

1187. lj_vm_call_dispatch:

1188. .byte 139,105,248,131,121,252,247,15,133,45,3,0,0

1190. .globl lj_vm_call_dispatch_f

1191. .hidden lj_vm_call_dispatch_f

1192. .type lj_vm_call_dispatch_f, @function

1193. .size lj_vm_call_dispatch_f, 23

1194. lj_vm_call_dispatch_f:

1195. .byte 137,202,137,90,252,139,93,16,139,11,15,182,233,15,182,205

1196. .byte 131,195,4,65,255,36,238

1198. .globl lj_vm_cpcall

1199. .hidden lj_vm_cpcall

1200. .type lj_vm_cpcall, @function

1201. .size lj_vm_cpcall, 76

1202. lj_vm_cpcall:

1203. .byte 85,83,65,87,65,86,72,131,236,40,137,253,137,124,36,24

1204. .byte 137,108,36,28,68,139,125,36,68,43,125,24,199,68,36,20

1205. .byte 0,0,0,0,68,137,124,36,16,76,139,125,48,76,137,124

1206. .byte 36,32,72,137,101,48,255,209,133,192,15,132,173,253,255,255

1207. .byte 137,193,187,5,0,0,0,233,104,255,255,255

1209. .globl lj_cont_dispatch

1210. .hidden lj_cont_dispatch

1211. .type lj_cont_dispatch, @function

1212. .size lj_cont_dispatch, 74

1213. lj_cont_dispatch:

1214. .byte 1,209,131,227,248,137,213,41,218,199,68,193,252,255,255,255

1215. .byte 255,137,200,139,93,244,72,99,77,240,131,249,1,118,24,76

1216. .byte 141,61,75,228,255,255,76,1,249,68,139,122,248,69,139,127

1217. .byte 16,69,139,127,208,255,225,15,132,68,29,0,0,41,213,193

1218. .byte 237,3,141,69,255,233,104,21,0,0

1220. .globl lj_cont_cat

1221. .hidden lj_cont_cat

1222. .type lj_cont_cat, @function

1223. .size lj_cont_cat, 46

1224. lj_cont_cat:

1225. .byte 15,182,75,255,131,237,16,141,12,202,41,233,15,132,132,0

1226. .byte 0,0,247,217,193,233,3,139,124,36,24,137,87,16,137,202

1227. .byte 72,139,8,72,137,77,0,137,238,233,195,236,255,255

1229. .globl lj_vmeta_tgets

1230. .hidden lj_vmeta_tgets

1231. .type lj_vmeta_tgets, @function

1232. .size lj_vmeta_tgets, 41

1233. lj_vmeta_tgets:

1234. .byte 137,4,36,199,68,36,4,251,255,255,255,72,141,4,36,128

1235. .byte 123,252,52,117,46,65,141,142,232,244,255,255,137,41,199,65

1236. .byte 4,244,255,255,255,137,205,235,33

1238. .globl lj_vmeta_tgetb

1239. .hidden lj_vmeta_tgetb

1240. .type lj_vmeta_tgetb, @function

1241. .size lj_vmeta_tgetb, 19

1242. lj_vmeta_tgetb:

1243. .byte 15,182,67,254,242,15,42,192,242,15,17,4,36,72,141,4

1244. .byte 36,235,7

1246. .globl lj_vmeta_tgetv

1247. .hidden lj_vmeta_tgetv

1248. .type lj_vmeta_tgetv, @function

1249. .size lj_vmeta_tgetv, 44

1250. lj_vmeta_tgetv:

1251. .byte 15,182,67,254,141,4,194,15,182,107,255,141,44,234,139,124

1252. .byte 36,24,137,87,16,137,238,72,137,194,137,253,137,92,36,28

1253. .byte 232

1254. .long lj_meta_tget-.-4

1255. .byte 139,85,16,133,192,116,29

1257. .globl lj_cont_ra

1258. .hidden lj_cont_ra

1259. .type lj_cont_ra, @function

1260. .size lj_cont_ra, 53

1261. lj_cont_ra:

1262. .byte 15,182,75,253,72,139,40,72,137,44,202,139,3,15,182,204

1263. .byte 15,182,232,131,195,4,193,232,16,65,255,36,238,139,77,24

1264. .byte 137,89,244,141,89,2,41,211,139,105,248,184,3,0,0,0

1265. .byte 233,136,254,255,255

1267. .globl lj_vmeta_tsets

1268. .hidden lj_vmeta_tsets

1269. .type lj_vmeta_tsets, @function

1270. .size lj_vmeta_tsets, 41

1271. lj_vmeta_tsets:

1272. .byte 137,4,36,199,68,36,4,251,255,255,255,72,141,4,36,128

1273. .byte 123,252,53,117,46,65,141,142,232,244,255,255,137,41,199,65

1274. .byte 4,244,255,255,255,137,205,235,33

1276. .globl lj_vmeta_tsetb

1277. .hidden lj_vmeta_tsetb

1278. .type lj_vmeta_tsetb, @function

1279. .size lj_vmeta_tsetb, 19

1280. lj_vmeta_tsetb:

1281. .byte 15,182,67,254,242,15,42,192,242,15,17,4,36,72,141,4

1282. .byte 36,235,7

1284. .globl lj_vmeta_tsetv

1285. .hidden lj_vmeta_tsetv

1286. .type lj_vmeta_tsetv, @function

1287. .size lj_vmeta_tsetv, 55

1288. lj_vmeta_tsetv:

1289. .byte 15,182,67,254,141,4,194,15,182,107,255,141,44,234,139,124

1290. .byte 36,24,137,87,16,137,238,72,137,194,137,253,137,92,36,28

1291. .byte 232

1292. .long lj_meta_tset-.-4

1293. .byte 139,85,16,133,192,116,29,15,182,75,253,72,139,44,202,72

1294. .byte 137,40

1296. .globl lj_cont_nop

1297. .hidden lj_cont_nop

1298. .type lj_cont_nop, @function

1299. .size lj_cont_nop, 54

1300. lj_cont_nop:

1301. .byte 139,3,15,182,204,15,182,232,131,195,4,193,232,16,65,255

1302. .byte 36,238,139,77,24,137,89,244,15,182,67,253,72,139,44,194

1303. .byte 72,137,105,16,141,89,2,41,211,139,105,248,184,4,0,0

1304. .byte 0,233,223,253,255,255

1306. .globl lj_vmeta_comp

1307. .hidden lj_vmeta_comp

1308. .type lj_vmeta_comp, @function

1309. .size lj_vmeta_comp, 74

1310. lj_vmeta_comp:

1311. .byte 139,108,36,24,137,85,16,141,52,202,141,20,194,137,239,15

1312. .byte 182,75,252,137,92,36,28,232

1313. .long lj_meta_comp-.-4

1314. .byte 139,85,16,131,248,1,15,135,178,0,0,0,141,91,4,114

1315. .byte 11,15,183,67,254,141,156,131,0,0,254,255,139,3,15,182

1316. .byte 204,15,182,232,131,195,4,193,232,16,65,255,36,238

1318. .globl lj_cont_condt

1319. .hidden lj_cont_condt

1320. .type lj_cont_condt, @function

1321. .size lj_cont_condt, 11

1322. lj_cont_condt:

1323. .byte 131,195,4,131,120,4,254,114,218,235,227

1325. .globl lj_cont_condf

1326. .hidden lj_cont_condf

1327. .type lj_cont_condf, @function

1328. .size lj_cont_condf, 6

1329. lj_cont_condf:

1330. .byte 131,120,4,254,235,205

1332. .globl lj_vmeta_equal

1333. .hidden lj_vmeta_equal

1334. .type lj_vmeta_equal, @function

1335. .size lj_vmeta_equal, 29

1336. lj_vmeta_equal:

1337. .byte 131,235,4,137,206,137,233,139,108,36,24,137,85,16,137,194

1338. .byte 137,239,137,92,36,28,232

1339. .long lj_meta_equal-.-4

1340. .byte 235,164

1342. .globl lj_vmeta_equal_cd

1343. .hidden lj_vmeta_equal_cd

1344. .type lj_vmeta_equal_cd, @function

1345. .size lj_vmeta_equal_cd, 26

1346. lj_vmeta_equal_cd:

1347. .byte 131,235,4,139,108,36,24,137,85,16,137,239,139,115,252,137

1348. .byte 92,36,28,232

1349. .long lj_meta_equal_cd-.-4

1350. .byte 235,138

1352. .globl lj_vmeta_arith_vno

1353. .hidden lj_vmeta_arith_vno

1354. .type lj_vmeta_arith_vno, @function

1355. .size lj_vmeta_arith_vno, 0

1356. lj_vmeta_arith_vno:

1358. .globl lj_vmeta_arith_vn

1359. .hidden lj_vmeta_arith_vn

1360. .type lj_vmeta_arith_vn, @function

1361. .size lj_vmeta_arith_vn, 6

1362. lj_vmeta_arith_vn:

1363. .byte 65,141,4,199,235,20

1365. .globl lj_vmeta_arith_nvo

1366. .hidden lj_vmeta_arith_nvo

1367. .type lj_vmeta_arith_nvo, @function

1368. .size lj_vmeta_arith_nvo, 0

1369. lj_vmeta_arith_nvo:

1371. .globl lj_vmeta_arith_nv

1372. .hidden lj_vmeta_arith_nv

1373. .type lj_vmeta_arith_nv, @function

1374. .size lj_vmeta_arith_nv, 10

1375. lj_vmeta_arith_nv:

1376. .byte 65,141,4,199,141,44,234,149,235,13

1378. .globl lj_vmeta_unm

1379. .hidden lj_vmeta_unm

1380. .type lj_vmeta_unm, @function

1381. .size lj_vmeta_unm, 7

1382. lj_vmeta_unm:

1383. .byte 141,4,194,137,197,235,6

1385. .globl lj_vmeta_arith_vvo

1386. .hidden lj_vmeta_arith_vvo

1387. .type lj_vmeta_arith_vvo, @function

1388. .size lj_vmeta_arith_vvo, 0

1389. lj_vmeta_arith_vvo:

1391. .globl lj_vmeta_arith_vv

1392. .hidden lj_vmeta_arith_vv

1393. .type lj_vmeta_arith_vv, @function

1394. .size lj_vmeta_arith_vv, 49

1395. lj_vmeta_arith_vv:

1396. .byte 141,4,194,141,44,234,141,12,202,68,15,182,67,252,137,206

1397. .byte 137,193,139,124,36,24,137,87,16,137,234,137,253,137,92,36

1398. .byte 28,232

1399. .long lj_meta_arith-.-4

1400. .byte 139,85,16,133,192,15,132,240,254,255,255

1402. .globl lj_vmeta_binop

1403. .hidden lj_vmeta_binop

1404. .type lj_vmeta_binop, @function

1405. .size lj_vmeta_binop, 20

1406. lj_vmeta_binop:

1407. .byte 137,193,41,208,137,89,244,141,88,2,184,3,0,0,0,233

1408. .byte 228,252,255,255

1410. .globl lj_vmeta_len

1411. .hidden lj_vmeta_len

1412. .type lj_vmeta_len, @function

1413. .size lj_vmeta_len, 26

1414. lj_vmeta_len:

1415. .byte 139,108,36,24,137,85,16,141,52,194,137,239,137,92,36,28

1416. .byte 232

1417. .long lj_meta_len-.-4

1418. .byte 139,85,16,235,210

1420. .globl lj_vmeta_call_ra

1421. .hidden lj_vmeta_call_ra

1422. .type lj_vmeta_call_ra, @function

1423. .size lj_vmeta_call_ra, 4

1424. lj_vmeta_call_ra:

1425. .byte 141,76,202,8

1427. .globl lj_vmeta_call

1428. .hidden lj_vmeta_call

1429. .type lj_vmeta_call, @function

1430. .size lj_vmeta_call, 81

1431. lj_vmeta_call:

1432. .byte 137,76,36,4,137,4,36,131,233,8,139,108,36,24,137,85

1433. .byte 16,137,206,141,20,193,137,239,137,92,36,28,232

1434. .long lj_meta_call-.-4

1435. .byte 139,85,16,139,76,36,4,139,4,36,139,105,248,131,192,1

1436. .byte 65,57,215,15,132,43,242,255,255,137,202,137,90,252,139,93

1437. .byte 16,139,11,15,182,233,15,182,205,131,195,4,65,255,36,238

1439. .globl lj_vmeta_for

1440. .hidden lj_vmeta_for

1441. .type lj_vmeta_for, @function

1442. .size lj_vmeta_for, 43

1443. lj_vmeta_for:

1444. .byte 139,108,36,24,137,85,16,137,206,137,239,137,92,36,28,232

1445. .long lj_meta_for-.-4

1446. .byte 139,85,16,139,67,252,15,182,204,15,182,232,193,232,16,65

1447. .byte 255,164,238,216,4,0,0

1449. .globl lj_ff_assert

1450. .hidden lj_ff_assert

1451. .type lj_ff_assert, @function

1452. .size lj_ff_assert, 67

1453. lj_ff_assert:

1454. .byte 131,248,2,15,130,12,18,0,0,139,106,4,131,253,254,15

1455. .byte 131,0,18,0,0,139,90,252,137,68,36,4,137,106,252,139

1456. .byte 42,137,106,248,131,232,2,116,17,137,209,131,193,8,72,139

1457. .byte 41,72,137,105,248,131,232,1,117,241,139,68,36,4,233,86

1458. .byte 6,0,0

1460. .globl lj_ff_type

1461. .hidden lj_ff_type

1462. .type lj_ff_type, @function

1463. .size lj_ff_type, 66

1464. lj_ff_type:

1465. .byte 131,248,2,15,130,201,17,0,0,139,106,4,137,233,193,249

1466. .byte 15,131,249,254,116,37,184,13,0,0,0,247,213,57,232,15

1467. .byte 71,197,139,106,248,139,68,197,32,139,90,252,199,66,252,251

1468. .byte 255,255,255,137,66,248,233,18,6,0,0,184,3,0,0,0

1469. .byte 235,224

1471. .globl lj_ff_getmetatable

1472. .hidden lj_ff_getmetatable

1473. .type lj_ff_getmetatable, @function

1474. .size lj_ff_getmetatable, 162

1475. lj_ff_getmetatable:

1476. .byte 131,248,2,15,130,135,17,0,0,139,106,4,139,90,252,131

1477. .byte 253,244,117,97,139,42,139,109,16,133,237,199,66,252,255,255

1478. .byte 255,255,15,132,227,5,0,0,65,139,134,140,245,255,255,199

1479. .byte 66,252,244,255,255,255,137,106,248,139,77,28,35,72,8,107

1480. .byte 201,24,3,77,20,131,121,12,251,117,5,57,65,8,116,12

1481. .byte 139,73,16,133,201,117,238,233,175,5,0,0,139,105,4,131

1482. .byte 253,255,15,132,163,5,0,0,139,1,137,106,252,137,66,248

1483. .byte 233,150,5,0,0,131,253,243,116,154,131,253,242,119,20,129

1484. .byte 253,255,255,254,255,118,7,189,252,255,255,255,235,5,189,242

1485. .byte 255,255,255,247,213,65,139,172,174,160,245,255,255,233,119,255

1486. .byte 255,255

1488. .globl lj_ff_setmetatable

1489. .hidden lj_ff_setmetatable

1490. .type lj_ff_setmetatable, @function

1491. .size lj_ff_setmetatable, 92

1492. lj_ff_setmetatable:

1493. .byte 131,248,3,15,130,229,16,0,0,131,122,4,244,15,133,219

1494. .byte 16,0,0,139,42,131,125,16,0,15,133,207,16,0,0,131

1495. .byte 122,12,244,15,133,197,16,0,0,139,66,8,137,69,16,139

1496. .byte 90,252,199,66,252,244,255,255,255,137,106,248,246,69,4,4

1497. .byte 116,21,128,101,4,251,65,139,134,132,244,255,255,65,137,174

1498. .byte 132,244,255,255,137,69,12,233,13,5,0,0

1500. .globl lj_ff_rawget

1501. .hidden lj_ff_rawget

1502. .type lj_ff_rawget, @function

1503. .size lj_ff_rawget, 52

1504. lj_ff_rawget:

1505. .byte 131,248,3,15,130,137,16,0,0,131,122,4,244,15,133,127

1506. .byte 16,0,0,137,213,139,50,141,82,8,139,124,36,24,232

1507. .long lj_tab_get-.-4

1508. .byte 137,234,72,139,40,139,90,252,72,137,106,248,233,217,4,0

1509. .byte 0

1511. .globl lj_ff_tonumber

1512. .hidden lj_ff_tonumber

1513. .type lj_ff_tonumber, @function

1514. .size lj_ff_tonumber, 31

1515. lj_ff_tonumber:

1516. .byte 131,248,2,15,133,85,16,0,0,129,122,4,255,255,254,255

1517. .byte 15,131,72,16,0,0,242,15,16,2,233,178,4,0,0

1519. .globl lj_ff_tostring

1520. .hidden lj_ff_tostring

1521. .type lj_ff_tostring, @function

1522. .size lj_ff_tostring, 108

1523. lj_ff_tostring:

1524. .byte 131,248,2,15,130,54,16,0,0,139,90,252,131,122,4,251

1525. .byte 117,17,139,2,199,66,252,251,255,255,255,137,66,248,233,151

1526. .byte 4,0,0,129,122,4,255,255,254,255,15,135,15,16,0,0

1527. .byte 65,131,190,212,245,255,255,0,15,133,1,16,0,0,65,139

1528. .byte 174,104,244,255,255,65,59,174,108,244,255,255,114,5,232,123

1529. .byte 16,0,0,139,108,36,24,137,85,16,137,92,36,28,137,214

1530. .byte 137,239,232

1531. .long lj_str_fromnum-.-4

1532. .byte 139,85,16,235,168

1534. .globl lj_ff_next

1535. .hidden lj_ff_next

1536. .type lj_ff_next, @function

1537. .size lj_ff_next, 72

1538. lj_ff_next:

1539. .byte 131,248,2,15,130,202,15,0,0,116,71,131,122,4,244,15

1540. .byte 133,190,15,0,0,139,108,36,24,137,85,16,137,85,24,139

1541. .byte 90,252,139,50,141,82,8,137,239,137,92,36,28,232

1542. .long lj_tab_next-.-4

1543. .byte 139,85,16,133,192,116,34,72,139,106,8,72,139,66,16,72

1544. .byte 137,106,248,72,137,2

1546. .globl lj_fff_res2

1547. .hidden lj_fff_res2

1548. .type lj_fff_res2, @function

1549. .size lj_fff_res2, 31

1550. lj_fff_res2:

1551. .byte 184,3,0,0,0,233,1,4,0,0,199,66,12,255,255,255

1552. .byte 255,235,176,199,66,252,255,255,255,255,233,231,3,0,0

1554. .globl lj_ff_pairs

1555. .hidden lj_ff_pairs

1556. .type lj_ff_pairs, @function

1557. .size lj_ff_pairs, 57

1558. lj_ff_pairs:

1559. .byte 131,248,2,15,130,99,15,0,0,139,42,131,122,4,244,15

1560. .byte 133,87,15,0,0,139,106,248,139,69,32,139,90,252,199,66

1561. .byte 252,247,255,255,255,137,66,248,199,66,12,255,255,255,255,184

1562. .byte 4,0,0,0,233,179,3,0,0

1564. .globl lj_ff_ipairs_aux

1565. .hidden lj_ff_ipairs_aux

1566. .type lj_ff_ipairs_aux, @function

1567. .size lj_ff_ipairs_aux, 121

1568. lj_ff_ipairs_aux:

1569. .byte 131,248,3,15,130,42,15,0,0,131,122,4,244,15,133,32

1570. .byte 15,0,0,129,122,12,255,255,254,255,15,131,19,15,0,0

1571. .byte 139,90,252,242,15,16,66,8,72,189,0,0,0,0,0,0

1572. .byte 240,63,102,72,15,110,205,242,15,88,193,242,15,45,192,242

1573. .byte 15,17,66,248,139,42,59,69,24,115,23,193,224,3,3,69

1574. .byte 8,131,120,4,255,116,34,72,139,40,72,137,42,233,70,255

1575. .byte 255,255,131,125,28,0,116,17,137,239,137,213,137,198,232

1576. .long lj_tab_getinth-.-4

1577. .byte 137,234,133,192,117,216

1579. .globl lj_fff_res0

1580. .hidden lj_fff_res0

1581. .type lj_fff_res0, @function

1582. .size lj_fff_res0, 10

1583. lj_fff_res0:

1584. .byte 184,1,0,0,0,233,48,3,0,0

1586. .globl lj_ff_ipairs

1587. .hidden lj_ff_ipairs

1588. .type lj_ff_ipairs, @function

1589. .size lj_ff_ipairs, 58

1590. lj_ff_ipairs:

1591. .byte 131,248,2,15,130,167,14,0,0,139,42,131,122,4,244,15

1592. .byte 133,155,14,0,0,139,106,248,139,69,32,139,90,252,199,66

1593. .byte 252,247,255,255,255,137,66,248,15,87,192,242,15,17,66,8

1594. .byte 184,4,0,0,0,233,246,2,0,0

1596. .globl lj_ff_pcall

1597. .hidden lj_ff_pcall

1598. .type lj_ff_pcall, @function

1599. .size lj_ff_pcall, 41

1600. lj_ff_pcall:

1601. .byte 131,248,2,15,130,109,14,0,0,141,74,8,131,232,1,187

1602. .byte 14,0,0,0,65,15,182,174,217,244,255,255,193,237,4,131

1603. .byte 229,1,1,235,233,130,248,255,255

1605. .globl lj_ff_xpcall

1606. .hidden lj_ff_xpcall

1607. .type lj_ff_xpcall, @function

1608. .size lj_ff_xpcall, 55

1609. lj_ff_xpcall:

1610. .byte 131,248,3,15,130,68,14,0,0,131,122,12,247,15,133,58

1611. .byte 14,0,0,139,106,4,137,106,12,199,66,4,247,255,255,255

1612. .byte 139,42,139,90,8,137,106,8,137,26,141,74,16,131,232,2

1613. .byte 187,22,0,0,0,235,180

1615. .globl lj_ff_coroutine_resume

1616. .hidden lj_ff_coroutine_resume

1617. .type lj_ff_coroutine_resume, @function

1618. .size lj_ff_coroutine_resume, 303

1619. lj_ff_coroutine_resume:

1620. .byte 131,248,2,15,130,13,14,0,0,139,42,139,90,252,137,92

1621. .byte 36,28,137,44,36,131,122,4,249,15,133,247,13,0,0,72

1622. .byte 131,125,48,0,15,133,236,13,0,0,128,125,7,1,15,135

1623. .byte 226,13,0,0,139,77,24,116,9,59,77,16,15,132,212,13

1624. .byte 0,0,141,92,193,240,59,93,32,15,135,199,13,0,0,137

1625. .byte 93,24,139,108,36,24,137,85,16,131,194,8,137,85,24,141

1626. .byte 108,194,232,72,41,221,57,203,116,15,72,139,4,43,72,137

1627. .byte 67,248,131,235,8,57,203,117,241,137,206,139,60,36,232,226

1628. .byte 246,255,255,65,199,134,48,245,255,255,255,255,255,255,139,108

1629. .byte 36,24,139,28,36,139,85,16,131,248,1,119,90,139,75,16

1630. .byte 68,139,123,24,137,75,24,68,137,251,41,203,116,31,141,4

1631. .byte 26,193,235,3,59,69,32,119,91,137,213,72,41,205,72,139

1632. .byte 1,72,137,4,41,131,193,8,68,57,249,117,241,141,67,2

1633. .byte 199,66,252,253,255,255,255,139,92,36,28,137,68,36,4,72

1634. .byte 199,193,248,255,255,255,247,195,3,0,0,0,15,132,142,239

1635. .byte 255,255,233,26,245,255,255,199,66,252,254,255,255,255,139,75

1636. .byte 24,131,233,8,137,75,24,72,139,1,72,137,2,184,3,0

1637. .byte 0,0,235,195,139,12,36,68,137,121,24,137,222,137,239,232

1638. .long lj_state_growstack-.-4

1639. .byte 139,28,36,139,85,16,233,110,255,255,255

1641. .globl lj_ff_coroutine_wrap_aux

1642. .hidden lj_ff_coroutine_wrap_aux

1643. .type lj_ff_coroutine_wrap_aux, @function

1644. .size lj_ff_coroutine_wrap_aux, 250

1645. lj_ff_coroutine_wrap_aux:

1646. .byte 139,106,248,139,109,32,139,90,252,137,92,36,28,137,44,36

1647. .byte 72,131,125,48,0,15,133,204,12,0,0,128,125,7,1,15

1648. .byte 135,194,12,0,0,139,77,24,116,9,59,77,16,15,132,180

1649. .byte 12,0,0,141,92,193,248,59,93,32,15,135,167,12,0,0

1650. .byte 137,93,24,139,108,36,24,137,85,16,137,85,24,141,108,194

1651. .byte 240,72,41,221,57,203,116,15,72,139,4,43,72,137,67,248

1652. .byte 131,235,8,57,203,117,241,137,206,139,60,36,232,197,245,255

1653. .byte 255,65,199,134,48,245,255,255,255,255,255,255,139,108,36,24

1654. .byte 139,28,36,139,85,16,131,248,1,119,78,139,75,16,68,139

1655. .byte 123,24,137,75,24,68,137,251,41,203,116,31,141,4,26,193

1656. .byte 235,3,59,69,32,119,59,137,213,72,41,205,72,139,1,72

1657. .byte 137,4,41,131,193,8,68,57,249,117,241,141,67,1,139,92

1658. .byte 36,28,137,68,36,4,49,201,247,195,3,0,0,0,15,132

1659. .byte 125,238,255,255,233,9,244,255,255,137,222,137,239,232

1660. .long lj_ffh_coroutine_wrap_err-.-4

1661. .byte 139,12,36,68,137,121,24,137,222,137,239,232

1662. .long lj_state_growstack-.-4

1663. .byte 139,28,36,139,85,16,235,145

1665. .globl lj_ff_coroutine_yield

1666. .hidden lj_ff_coroutine_yield

1667. .type lj_ff_coroutine_yield, @function

1668. .size lj_ff_coroutine_yield, 44

1669. lj_ff_coroutine_yield:

1670. .byte 139,108,36,24,72,247,69,48,1,0,0,0,15,132,219,11

1671. .byte 0,0,137,85,16,141,68,194,248,137,69,24,49,192,72,137

1672. .byte 69,48,176,1,136,69,7,233,18,244,255,255

1674. .globl lj_fff_resi

1675. .hidden lj_fff_resi

1676. .type lj_fff_resi, @function

1677. .size lj_fff_resi, 0

1678. lj_fff_resi:

1680. .globl lj_fff_resn

1681. .hidden lj_fff_resn

1682. .type lj_fff_resn, @function

1683. .size lj_fff_resn, 8

1684. lj_fff_resn:

1685. .byte 139,90,252,221,90,248,235,52

1687. .globl lj_ff_math_abs

1688. .hidden lj_ff_math_abs

1689. .type lj_ff_math_abs, @function

1690. .size lj_ff_math_abs, 44

1691. lj_ff_math_abs:

1692. .byte 131,248,2,15,130,176,11,0,0,129,122,4,255,255,254,255

1693. .byte 15,131,163,11,0,0,242,15,16,2,72,184,255,255,255,255

1694. .byte 255,255,255,127,102,72,15,110,200,15,84,193

1696. .globl lj_fff_resxmm0

1697. .hidden lj_fff_resxmm0

1698. .type lj_fff_resxmm0, @function

1699. .size lj_fff_resxmm0, 8

1700. lj_fff_resxmm0:

1701. .byte 139,90,252,242,15,17,66,248

1703. .globl lj_fff_res1

1704. .hidden lj_fff_res1

1705. .type lj_fff_res1, @function

1706. .size lj_fff_res1, 5

1707. lj_fff_res1:

1708. .byte 184,2,0,0,0

1710. .globl lj_fff_res

1711. .hidden lj_fff_res

1712. .type lj_fff_res, @function

1713. .size lj_fff_res, 4

1714. lj_fff_res:

1715. .byte 137,68,36,4

1717. .globl lj_fff_res_

1718. .hidden lj_fff_res_

1719. .type lj_fff_res_, @function

1720. .size lj_fff_res_, 66

1721. lj_fff_res_:

1722. .byte 247,195,3,0,0,0,117,46,56,67,255,119,28,15,182,75

1723. .byte 253,72,247,209,141,20,202,139,3,15,182,204,15,182,232,131

1724. .byte 195,4,193,232,16,65,255,36,238,199,68,194,244,255,255,255

1725. .byte 255,131,192,1,235,210,72,199,193,248,255,255,255,233,53,243

1726. .byte 255,255

1728. .globl lj_ff_math_floor

1729. .hidden lj_ff_math_floor

1730. .type lj_ff_math_floor, @function

1731. .size lj_ff_math_floor, 24

1732. lj_ff_math_floor:

1733. .byte 129,122,4,255,255,254,255,15,131,45,11,0,0,242,15,16

1734. .byte 2,232,49,14,0,0,235,149

1736. .globl lj_ff_math_ceil

1737. .hidden lj_ff_math_ceil

1738. .type lj_ff_math_ceil, @function

1739. .size lj_ff_math_ceil, 27

1740. lj_ff_math_ceil:

1741. .byte 129,122,4,255,255,254,255,15,131,21,11,0,0,242,15,16

1742. .byte 2,232,116,14,0,0,233,122,255,255,255

1744. .globl lj_ff_math_sqrt

1745. .hidden lj_ff_math_sqrt

1746. .type lj_ff_math_sqrt, @function

1747. .size lj_ff_math_sqrt, 31

1748. lj_ff_math_sqrt:

1749. .byte 131,248,2,15,130,254,10,0,0,129,122,4,255,255,254,255

1750. .byte 15,131,241,10,0,0,242,15,81,2,233,91,255,255,255

1752. .globl lj_ff_math_log

1753. .hidden lj_ff_math_log

1754. .type lj_ff_math_log, @function

1755. .size lj_ff_math_log, 33

1756. lj_ff_math_log:

1757. .byte 131,248,2,15,133,223,10,0,0,129,122,4,255,255,254,255

1758. .byte 15,131,210,10,0,0,217,237,221,2,217,241,233,6,255,255

1759. .byte 255

1761. .globl lj_ff_math_log10

1762. .hidden lj_ff_math_log10

1763. .type lj_ff_math_log10, @function

1764. .size lj_ff_math_log10, 33

1765. lj_ff_math_log10:

1766. .byte 131,248,2,15,130,190,10,0,0,129,122,4,255,255,254,255

1767. .byte 15,131,177,10,0,0,217,236,221,2,217,241,233,229,254,255

1768. .byte 255

1770. .globl lj_ff_math_exp

1771. .hidden lj_ff_math_exp

1772. .type lj_ff_math_exp, @function

1773. .size lj_ff_math_exp, 34

1774. lj_ff_math_exp:

1775. .byte 131,248,2,15,130,157,10,0,0,129,122,4,255,255,254,255

1776. .byte 15,131,144,10,0,0,221,2,232,57,15,0,0,233,195,254

1777. .byte 255,255

1779. .globl lj_ff_math_sin

1780. .hidden lj_ff_math_sin

1781. .type lj_ff_math_sin, @function

1782. .size lj_ff_math_sin, 31

1783. lj_ff_math_sin:

1784. .byte 131,248,2,15,130,123,10,0,0,129,122,4,255,255,254,255

1785. .byte 15,131,110,10,0,0,221,2,217,254,233,164,254,255,255

1787. .globl lj_ff_math_cos

1788. .hidden lj_ff_math_cos

1789. .type lj_ff_math_cos, @function

1790. .size lj_ff_math_cos, 31

1791. lj_ff_math_cos:

1792. .byte 131,248,2,15,130,92,10,0,0,129,122,4,255,255,254,255

1793. .byte 15,131,79,10,0,0,221,2,217,255,233,133,254,255,255

1795. .globl lj_ff_math_tan

1796. .hidden lj_ff_math_tan

1797. .type lj_ff_math_tan, @function

1798. .size lj_ff_math_tan, 33

1799. lj_ff_math_tan:

1800. .byte 131,248,2,15,130,61,10,0,0,129,122,4,255,255,254,255

1801. .byte 15,131,48,10,0,0,221,2,217,242,221,216,233,100,254,255

1802. .byte 255

1804. .globl lj_ff_math_asin

1805. .hidden lj_ff_math_asin

1806. .type lj_ff_math_asin, @function

1807. .size lj_ff_math_asin, 41

1808. lj_ff_math_asin:

1809. .byte 131,248,2,15,130,28,10,0,0,129,122,4,255,255,254,255

1810. .byte 15,131,15,10,0,0,221,2,217,192,216,200,217,232,222,225

1811. .byte 217,250,217,243,233,59,254,255,255

1813. .globl lj_ff_math_acos

1814. .hidden lj_ff_math_acos

1815. .type lj_ff_math_acos, @function

1816. .size lj_ff_math_acos, 43

1817. lj_ff_math_acos:

1818. .byte 131,248,2,15,130,243,9,0,0,129,122,4,255,255,254,255

1819. .byte 15,131,230,9,0,0,221,2,217,192,216,200,217,232,222,225

1820. .byte 217,250,217,201,217,243,233,16,254,255,255

1822. .globl lj_ff_math_atan

1823. .hidden lj_ff_math_atan

1824. .type lj_ff_math_atan, @function

1825. .size lj_ff_math_atan, 33

1826. lj_ff_math_atan:

1827. .byte 131,248,2,15,130,200,9,0,0,129,122,4,255,255,254,255

1828. .byte 15,131,187,9,0,0,221,2,217,232,217,243,233,239,253,255

1829. .byte 255

1831. .globl lj_ff_math_sinh

1832. .hidden lj_ff_math_sinh

1833. .type lj_ff_math_sinh, @function

1834. .size lj_ff_math_sinh, 40

1835. lj_ff_math_sinh:

1836. .byte 131,248,2,15,130,167,9,0,0,129,122,4,255,255,254,255

1837. .byte 15,131,154,9,0,0,242,15,16,2,137,213,232

1838. .long lj_vm_sinh-.-4

1839. .byte 137,234,233,251,253,255,255

1841. .globl lj_ff_math_cosh

1842. .hidden lj_ff_math_cosh

1843. .type lj_ff_math_cosh, @function

1844. .size lj_ff_math_cosh, 40

1845. lj_ff_math_cosh:

1846. .byte 131,248,2,15,130,127,9,0,0,129,122,4,255,255,254,255

1847. .byte 15,131,114,9,0,0,242,15,16,2,137,213,232

1848. .long lj_vm_cosh-.-4

1849. .byte 137,234,233,211,253,255,255

1851. .globl lj_ff_math_tanh

1852. .hidden lj_ff_math_tanh

1853. .type lj_ff_math_tanh, @function

1854. .size lj_ff_math_tanh, 40

1855. lj_ff_math_tanh:

1856. .byte 131,248,2,15,130,87,9,0,0,129,122,4,255,255,254,255

1857. .byte 15,131,74,9,0,0,242,15,16,2,137,213,232

1858. .long lj_vm_tanh-.-4

1859. .byte 137,234,233,171,253,255,255

1861. .globl lj_ff_math_deg

1862. .hidden lj_ff_math_deg

1863. .type lj_ff_math_deg, @function

1864. .size lj_ff_math_deg, 0

1865. lj_ff_math_deg:

1867. .globl lj_ff_math_rad

1868. .hidden lj_ff_math_rad

1869. .type lj_ff_math_rad, @function

1870. .size lj_ff_math_rad, 39

1871. lj_ff_math_rad:

1872. .byte 131,248,2,15,130,47,9,0,0,129,122,4,255,255,254,255

1873. .byte 15,131,34,9,0,0,242,15,16,2,139,106,248,242,15,89

1874. .byte 69,32,233,132,253,255,255

1876. .globl lj_ff_math_atan2

1877. .hidden lj_ff_math_atan2

1878. .type lj_ff_math_atan2, @function

1879. .size lj_ff_math_atan2, 47

1880. lj_ff_math_atan2:

1881. .byte 131,248,3,15,130,8,9,0,0,129,122,4,255,255,254,255

1882. .byte 15,131,251,8,0,0,129,122,12,255,255,254,255,15,131,238

1883. .byte 8,0,0,221,2,221,66,8,217,243,233,33,253,255,255

1885. .globl lj_ff_math_ldexp

1886. .hidden lj_ff_math_ldexp

1887. .type lj_ff_math_ldexp, @function

1888. .size lj_ff_math_ldexp, 49

1889. lj_ff_math_ldexp:

1890. .byte 131,248,3,15,130,217,8,0,0,129,122,4,255,255,254,255

1891. .byte 15,131,204,8,0,0,129,122,12,255,255,254,255,15,131,191

1892. .byte 8,0,0,221,66,8,221,2,217,253,221,217,233,240,252,255

1893. .byte 255

1895. .globl lj_ff_math_frexp

1896. .hidden lj_ff_math_frexp

1897. .type lj_ff_math_frexp, @function

1898. .size lj_ff_math_frexp, 148

1899. lj_ff_math_frexp:

1900. .byte 131,248,2,15,130,168,8,0,0,139,106,4,129,253,255,255

1901. .byte 254,255,15,131,153,8,0,0,139,90,252,139,2,137,106,252

1902. .byte 137,66,248,209,229,129,253,0,0,224,255,115,58,9,232,116

1903. .byte 54,184,254,3,0,0,129,253,0,0,32,0,114,46,193,237

1904. .byte 21,41,197,242,15,42,197,139,106,252,129,229,255,255,15,128

1905. .byte 129,205,0,0,224,63,137,106,252,242,15,17,2,184,3,0

1906. .byte 0,0,233,202,252,255,255,15,87,192,235,237,242,15,16,2

1907. .byte 72,189,0,0,0,0,0,0,80,67,102,72,15,110,205,242

1908. .byte 15,89,193,242,15,17,66,248,139,106,252,184,52,4,0,0

1909. .byte 209,229,235,170

1911. .globl lj_ff_math_modf

1912. .hidden lj_ff_math_modf

1913. .type lj_ff_math_modf, @function

1914. .size lj_ff_math_modf, 99

1915. lj_ff_math_modf:

1916. .byte 131,248,2,15,130,20,8,0,0,129,122,4,255,255,254,255

1917. .byte 15,131,7,8,0,0,242,15,16,2,139,106,4,139,90,252

1918. .byte 209,229,129,253,0,0,224,255,116,52,15,40,224,232,174,11

1919. .byte 0,0,242,15,92,224,242,15,17,66,248,242,15,17,34,139

1920. .byte 66,252,139,106,4,49,232,120,10,184,3,0,0,0,233,74

1921. .byte 252,255,255,129,245,0,0,0,128,137,106,4,235,235,15,87

1922. .byte 228,235,211

1924. .globl lj_ff_math_fmod

1925. .hidden lj_ff_math_fmod

1926. .type lj_ff_math_fmod, @function

1927. .size lj_ff_math_fmod, 57

1928. lj_ff_math_fmod:

1929. .byte 131,248,3,15,130,177,7,0,0,129,122,4,255,255,254,255

1930. .byte 15,131,164,7,0,0,129,122,12,255,255,254,255,15,131,151

1931. .byte 7,0,0,221,66,8,221,2,217,248,223,224,102,37,0,4

1932. .byte 117,246,221,217,233,192,251,255,255

1934. .globl lj_ff_math_pow

1935. .hidden lj_ff_math_pow

1936. .type lj_ff_math_pow, @function

1937. .size lj_ff_math_pow, 54

1938. lj_ff_math_pow:

1939. .byte 131,248,3,15,130,120,7,0,0,129,122,4,255,255,254,255

1940. .byte 15,131,107,7,0,0,129,122,12,255,255,254,255,15,131,94

1941. .byte 7,0,0,242,15,16,2,242,15,16,74,8,232,52,12,0

1942. .byte 0,233,190,251,255,255

1944. .globl lj_ff_math_min

1945. .hidden lj_ff_math_min

1946. .type lj_ff_math_min, @function

1947. .size lj_ff_math_min, 59

1948. lj_ff_math_min:

1949. .byte 185,2,0,0,0,129,122,4,255,255,254,255,15,131,57,7

1950. .byte 0,0,242,15,16,2,57,193,15,131,160,251,255,255,129,124

1951. .byte 202,252,255,255,254,255,15,131,31,7,0,0,242,15,16,76

1952. .byte 202,248,242,15,93,193,131,193,1,235,219

1954. .globl lj_ff_math_max

1955. .hidden lj_ff_math_max

1956. .type lj_ff_math_max, @function

1957. .size lj_ff_math_max, 59

1958. lj_ff_math_max:

1959. .byte 185,2,0,0,0,129,122,4,255,255,254,255,15,131,254,6

1960. .byte 0,0,242,15,16,2,57,193,15,131,101,251,255,255,129,124

1961. .byte 202,252,255,255,254,255,15,131,228,6,0,0,242,15,16,76

1962. .byte 202,248,242,15,95,193,131,193,1,235,219

1964. .globl lj_ff_string_len

1965. .hidden lj_ff_string_len

1966. .type lj_ff_string_len, @function

1967. .size lj_ff_string_len, 31

1968. lj_ff_string_len:

1969. .byte 131,248,2,15,130,204,6,0,0,131,122,4,251,15,133,194

1970. .byte 6,0,0,139,42,242,15,42,69,12,233,41,251,255,255

1972. .globl lj_ff_string_byte

1973. .hidden lj_ff_string_byte

1974. .type lj_ff_string_byte, @function

1975. .size lj_ff_string_byte, 47

1976. lj_ff_string_byte:

1977. .byte 131,248,2,15,133,173,6,0,0,131,122,4,251,15,133,163

1978. .byte 6,0,0,139,42,139,90,252,131,125,12,1,15,130,218,247

1979. .byte 255,255,15,182,109,16,242,15,42,197,233,250,250,255,255

1981. .globl lj_ff_string_char

1982. .hidden lj_ff_string_char

1983. .type lj_ff_string_char, @function

1984. .size lj_ff_string_char, 76

1985. lj_ff_string_char:

1986. .byte 65,139,174,104,244,255,255,65,59,174,108,244,255,255,114,5

1987. .byte 232,1,7,0,0,131,248,2,15,133,105,6,0,0,129,122

1988. .byte 4,255,255,254,255,15,131,92,6,0,0,242,15,44,42,129

1989. .byte 253,255,0,0,0,15,135,76,6,0,0,137,108,36,4,199

1990. .byte 68,36,8,1,0,0,0,72,141,68,36,4

1992. .globl lj_fff_newstr

1993. .hidden lj_fff_newstr

1994. .type lj_fff_newstr, @function

1995. .size lj_fff_newstr, 46

1996. lj_fff_newstr:

1997. .byte 139,108,36,24,137,85,16,139,84,36,8,72,137,198,137,239

1998. .byte 137,92,36,28,232

1999. .long lj_str_new-.-4

2000. .byte 139,85,16,139,90,252,199,66,252,251,255,255,255,137,66,248

2001. .byte 233,136,250,255,255

2003. .globl lj_ff_string_sub

2004. .hidden lj_ff_string_sub

2005. .type lj_ff_string_sub, @function

2006. .size lj_ff_string_sub, 165

2007. lj_ff_string_sub:

2008. .byte 65,139,174,104,244,255,255,65,59,174,108,244,255,255,114,5

2009. .byte 232,135,6,0,0,199,68,36,4,255,255,255,255,131,248,3

2010. .byte 15,130,231,5,0,0,118,22,129,122,20,255,255,254,255,15

2011. .byte 131,216,5,0,0,242,15,44,106,16,137,108,36,4,131,122

2012. .byte 4,251,15,133,197,5,0,0,129,122,12,255,255,254,255,15

2013. .byte 131,184,5,0,0,139,42,137,108,36,8,139,109,12,242,15

2014. .byte 44,74,8,139,68,36,4,57,197,114,30,133,201,126,38,139

2015. .byte 108,36,8,41,200,124,46,141,108,13,15,131,192,1,137,68

2016. .byte 36,8,137,232,233,73,255,255,255,124,6,141,68,40,1,235

2017. .byte 218,137,232,235,214,116,7,1,233,131,193,1,127,209,185,1

2018. .byte 0,0,0,235,202

2020. .globl lj_fff_emptystr

2021. .hidden lj_fff_emptystr

2022. .type lj_fff_emptystr, @function

2023. .size lj_fff_emptystr, 4

2024. lj_fff_emptystr:

2025. .byte 49,192,235,213

2027. .globl lj_ff_string_rep

2028. .hidden lj_ff_string_rep

2029. .type lj_ff_string_rep, @function

2030. .size lj_ff_string_rep, 127

2031. lj_ff_string_rep:

2032. .byte 65,139,174,104,244,255,255,65,59,174,108,244,255,255,114,5

2033. .byte 232,222,5,0,0,131,248,3,15,133,70,5,0,0,131,122

2034. .byte 4,251,15,133,60,5,0,0,129,122,12,255,255,254,255,139

2035. .byte 42,15,131,45,5,0,0,242,15,44,66,8,133,192,126,188

2036. .byte 131,125,12,1,114,182,15,133,12,5,0,0,65,57,134,172

2037. .byte 244,255,255,15,130,255,4,0,0,15,182,77,16,65,139,174

2038. .byte 160,244,255,255,137,68,36,8,136,77,0,131,197,1,131,232

2039. .byte 1,117,245,65,139,134,160,244,255,255,233,170,254,255,255

2041. .globl lj_ff_string_reverse

2042. .hidden lj_ff_string_reverse

2043. .type lj_ff_string_reverse, @function

2044. .size lj_ff_string_reverse, 110

2045. lj_ff_string_reverse:

2046. .byte 131,248,2,15,130,220,4,0,0,65,139,174,104,244,255,255

2047. .byte 65,59,174,108,244,255,255,114,5,232,86,5,0,0,131,122

2048. .byte 4,251,15,133,189,4,0,0,139,42,139,69,12,133,192,15

2049. .byte 132,72,255,255,255,65,57,134,172,244,255,255,15,130,158,4

2050. .byte 0,0,131,197,16,137,92,36,4,137,68,36,8,65,139,158

2051. .byte 160,244,255,255,15,182,77,0,131,197,1,131,232,1,136,12

2052. .byte 3,117,241,137,216,139,92,36,4,233,60,254,255,255

2054. .globl lj_ff_string_lower

2055. .hidden lj_ff_string_lower

2056. .type lj_ff_string_lower, @function

2057. .size lj_ff_string_lower, 115

2058. lj_ff_string_lower:

2059. .byte 131,248,2,15,130,110,4,0,0,65,139,174,104,244,255,255

2060. .byte 65,59,174,108,244,255,255,114,5,232,232,4,0,0,131,122

2061. .byte 4,251,15,133,79,4,0,0,139,42,139,69,12,65,57,134

2062. .byte 172,244,255,255,15,130,56,4,0,0,131,197,16,137,92,36

2063. .byte 4,137,68,36,8,65,139,158,160,244,255,255,235,21,15,182

2064. .byte 76,5,0,131,249,65,114,8,131,249,90,119,3,131,241,32

2065. .byte 136,12,3,131,232,1,121,230,137,216,139,92,36,4,233,201

2066. .byte 253,255,255

2068. .globl lj_ff_string_upper

2069. .hidden lj_ff_string_upper

2070. .type lj_ff_string_upper, @function

2071. .size lj_ff_string_upper, 115

2072. lj_ff_string_upper:

2073. .byte 131,248,2,15,130,251,3,0,0,65,139,174,104,244,255,255

2074. .byte 65,59,174,108,244,255,255,114,5,232,117,4,0,0,131,122

2075. .byte 4,251,15,133,220,3,0,0,139,42,139,69,12,65,57,134

2076. .byte 172,244,255,255,15,130,197,3,0,0,131,197,16,137,92,36

2077. .byte 4,137,68,36,8,65,139,158,160,244,255,255,235,21,15,182

2078. .byte 76,5,0,131,249,97,114,8,131,249,122,119,3,131,241,32

2079. .byte 136,12,3,131,232,1,121,230,137,216,139,92,36,4,233,86

2080. .byte 253,255,255

2082. .globl lj_ff_table_getn

2083. .hidden lj_ff_table_getn

2084. .type lj_ff_table_getn, @function

2085. .size lj_ff_table_getn, 39

2086. lj_ff_table_getn:

2087. .byte 131,248,2,15,130,136,3,0,0,131,122,4,244,15,133,126

2088. .byte 3,0,0,137,213,139,58,232

2089. .long lj_tab_len-.-4

2090. .byte 137,234,242,15,42,192,233,221,247,255,255

2092. .globl lj_ff_bit_tobit

2093. .hidden lj_ff_bit_tobit

2094. .type lj_ff_bit_tobit, @function

2095. .size lj_ff_bit_tobit, 54

2096. lj_ff_bit_tobit:

2097. .byte 131,248,2,15,130,97,3,0,0,129,122,4,255,255,254,255

2098. .byte 15,131,84,3,0,0,242,15,16,2,72,189,0,0,0,0

2099. .byte 0,0,56,67,102,72,15,110,205,242,15,88,193,102,15,126

2100. .byte 197,233,139,1,0,0

2102. .globl lj_ff_bit_band

2103. .hidden lj_ff_bit_band

2104. .type lj_ff_bit_band, @function

2105. .size lj_ff_bit_band, 97

2106. lj_ff_bit_band:

2107. .byte 131,248,2,15,130,43,3,0,0,72,189,0,0,0,0,0

2108. .byte 0,56,67,102,72,15,110,205,129,122,4,255,255,254,255,15

2109. .byte 131,15,3,0,0,242,15,16,2,242,15,88,193,102,15,126

2110. .byte 197,137,68,36,4,141,68,194,240,57,208,15,134,74,1,0

2111. .byte 0,129,120,4,255,255,254,255,15,131,70,1,0,0,242,15

2112. .byte 16,0,242,15,88,193,102,15,126,193,33,205,131,232,8,235

2113. .byte 216

2115. .globl lj_ff_bit_bor

2116. .hidden lj_ff_bit_bor

2117. .type lj_ff_bit_bor, @function

2118. .size lj_ff_bit_bor, 97

2119. lj_ff_bit_bor:

2120. .byte 131,248,2,15,130,202,2,0,0,72,189,0,0,0,0,0

2121. .byte 0,56,67,102,72,15,110,205,129,122,4,255,255,254,255,15

2122. .byte 131,174,2,0,0,242,15,16,2,242,15,88,193,102,15,126

2123. .byte 197,137,68,36,4,141,68,194,240,57,208,15,134,233,0,0

2124. .byte 0,129,120,4,255,255,254,255,15,131,229,0,0,0,242,15

2125. .byte 16,0,242,15,88,193,102,15,126,193,9,205,131,232,8,235

2126. .byte 216

2128. .globl lj_ff_bit_bxor

2129. .hidden lj_ff_bit_bxor

2130. .type lj_ff_bit_bxor, @function

2131. .size lj_ff_bit_bxor, 97

2132. lj_ff_bit_bxor:

2133. .byte 131,248,2,15,130,105,2,0,0,72,189,0,0,0,0,0

2134. .byte 0,56,67,102,72,15,110,205,129,122,4,255,255,254,255,15

2135. .byte 131,77,2,0,0,242,15,16,2,242,15,88,193,102,15,126

2136. .byte 197,137,68,36,4,141,68,194,240,57,208,15,134,136,0,0

2137. .byte 0,129,120,4,255,255,254,255,15,131,132,0,0,0,242,15

2138. .byte 16,0,242,15,88,193,102,15,126,193,49,205,131,232,8,235

2139. .byte 216

2141. .globl lj_ff_bit_bswap

2142. .hidden lj_ff_bit_bswap

2143. .type lj_ff_bit_bswap, @function

2144. .size lj_ff_bit_bswap, 53

2145. lj_ff_bit_bswap:

2146. .byte 131,248,2,15,130,8,2,0,0,129,122,4,255,255,254,255

2147. .byte 15,131,251,1,0,0,242,15,16,2,72,189,0,0,0,0

2148. .byte 0,0,56,67,102,72,15,110,205,242,15,88,193,102,15,126

2149. .byte 197,15,205,235,51

2151. .globl lj_ff_bit_bnot

2152. .hidden lj_ff_bit_bnot

2153. .type lj_ff_bit_bnot, @function

2154. .size lj_ff_bit_bnot, 51

2155. lj_ff_bit_bnot:

2156. .byte 131,248,2,15,130,211,1,0,0,129,122,4,255,255,254,255

2157. .byte 15,131,198,1,0,0,242,15,16,2,72,189,0,0,0,0

2158. .byte 0,0,56,67,102,72,15,110,205,242,15,88,193,102,15,126

2159. .byte 197,247,213

2161. .globl lj_fff_resbit

2162. .hidden lj_fff_resbit

2163. .type lj_fff_resbit, @function

2164. .size lj_fff_resbit, 9

2165. lj_fff_resbit:

2166. .byte 242,15,42,197,233,19,246,255,255

2168. .globl lj_fff_fallback_bit_op

2169. .hidden lj_fff_fallback_bit_op

2170. .type lj_fff_fallback_bit_op, @function

2171. .size lj_fff_fallback_bit_op, 9

2172. lj_fff_fallback_bit_op:

2173. .byte 139,68,36,4,233,151,1,0,0

2175. .globl lj_ff_bit_lshift

2176. .hidden lj_ff_bit_lshift

2177. .type lj_ff_bit_lshift, @function

2178. .size lj_ff_bit_lshift, 79

2179. lj_ff_bit_lshift:

2180. .byte 131,248,3,15,130,142,1,0,0,129,122,4,255,255,254,255

2181. .byte 15,131,129,1,0,0,129,122,12,255,255,254,255,15,131,116

2182. .byte 1,0,0,242,15,16,2,242,15,16,74,8,72,189,0,0

2183. .byte 0,0,0,0,56,67,102,72,15,110,213,242,15,88,194,242

2184. .byte 15,88,202,102,15,126,197,102,15,126,201,211,229,235,159

2186. .globl lj_ff_bit_rshift

2187. .hidden lj_ff_bit_rshift

2188. .type lj_ff_bit_rshift, @function

2189. .size lj_ff_bit_rshift, 82

2190. lj_ff_bit_rshift:

2191. .byte 131,248,3,15,130,63,1,0,0,129,122,4,255,255,254,255

2192. .byte 15,131,50,1,0,0,129,122,12,255,255,254,255,15,131,37

2193. .byte 1,0,0,242,15,16,2,242,15,16,74,8,72,189,0,0

2194. .byte 0,0,0,0,56,67,102,72,15,110,213,242,15,88,194,242

2195. .byte 15,88,202,102,15,126,197,102,15,126,201,211,237,233,77,255

2196. .byte 255,255

2198. .globl lj_ff_bit_arshift

2199. .hidden lj_ff_bit_arshift

2200. .type lj_ff_bit_arshift, @function

2201. .size lj_ff_bit_arshift, 82

2202. lj_ff_bit_arshift:

2203. .byte 131,248,3,15,130,237,0,0,0,129,122,4,255,255,254,255

2204. .byte 15,131,224,0,0,0,129,122,12,255,255,254,255,15,131,211

2205. .byte 0,0,0,242,15,16,2,242,15,16,74,8,72,189,0,0

2206. .byte 0,0,0,0,56,67,102,72,15,110,213,242,15,88,194,242

2207. .byte 15,88,202,102,15,126,197,102,15,126,201,211,253,233,251,254

2208. .byte 255,255

2210. .globl lj_ff_bit_rol

2211. .hidden lj_ff_bit_rol

2212. .type lj_ff_bit_rol, @function

2213. .size lj_ff_bit_rol, 82

2214. lj_ff_bit_rol:

2215. .byte 131,248,3,15,130,155,0,0,0,129,122,4,255,255,254,255

2216. .byte 15,131,142,0,0,0,129,122,12,255,255,254,255,15,131,129

2217. .byte 0,0,0,242,15,16,2,242,15,16,74,8,72,189,0,0

2218. .byte 0,0,0,0,56,67,102,72,15,110,213,242,15,88,194,242

2219. .byte 15,88,202,102,15,126,197,102,15,126,201,211,197,233,169,254

2220. .byte 255,255

2222. .globl lj_ff_bit_ror

2223. .hidden lj_ff_bit_ror

2224. .type lj_ff_bit_ror, @function

2225. .size lj_ff_bit_ror, 70

2226. lj_ff_bit_ror:

2227. .byte 131,248,3,114,77,129,122,4,255,255,254,255,115,68,129,122

2228. .byte 12,255,255,254,255,115,59,242,15,16,2,242,15,16,74,8

2229. .byte 72,189,0,0,0,0,0,0,56,67,102,72,15,110,213,242

2230. .byte 15,88,194,242,15,88,202,102,15,126,197,102,15,126,201,211

2231. .byte 205,233,99,254,255,255

2233. .globl lj_fff_fallback_2

2234. .hidden lj_fff_fallback_2

2235. .type lj_fff_fallback_2, @function

2236. .size lj_fff_fallback_2, 7

2237. lj_fff_fallback_2:

2238. .byte 184,3,0,0,0,235,5

2240. .globl lj_fff_fallback_1

2241. .hidden lj_fff_fallback_1

2242. .type lj_fff_fallback_1, @function

2243. .size lj_fff_fallback_1, 5

2244. lj_fff_fallback_1:

2245. .byte 184,2,0,0,0

2247. .globl lj_fff_fallback

2248. .hidden lj_fff_fallback

2249. .type lj_fff_fallback, @function

2250. .size lj_fff_fallback, 87

2251. lj_fff_fallback:

2252. .byte 139,108,36,24,139,90,252,137,92,36,28,137,85,16,141,68

2253. .byte 194,248,141,136,160,0,0,0,137,69,24,139,66,248,59,77

2254. .byte 32,119,89,137,239,255,80,24,139,85,16,133,192,15,143,77

2255. .byte 244,255,255,139,77,24,41,209,193,233,3,133,192,141,65,1

2256. .byte 139,106,248,117,18,139,93,16,139,11,15,182,233,15,182,205

2257. .byte 131,195,4,65,255,36,238

2259. .globl lj_vm_call_tail

2260. .hidden lj_vm_call_tail

2261. .type lj_vm_call_tail, @function

2262. .size lj_vm_call_tail, 56

2263. lj_vm_call_tail:

2264. .byte 137,209,247,195,3,0,0,0,117,15,15,182,107,253,72,247

2265. .byte 213,141,20,234,233,197,233,255,255,137,221,131,229,248,41,234

2266. .byte 233,185,233,255,255,190,20,0,0,0,137,239,232

2267. .long lj_state_growstack-.-4

2268. .byte 139,85,16,49,192,235,164

2270. .globl lj_fff_gcstep

2271. .hidden lj_fff_gcstep

2272. .type lj_fff_gcstep, @function

2273. .size lj_fff_gcstep, 52

2274. lj_fff_gcstep:

2275. .byte 93,72,137,108,36,8,139,108,36,24,137,92,36,28,137,85

2276. .byte 16,141,68,194,248,137,239,137,69,24,232

2277. .long lj_gc_step-.-4

2278. .byte 139,85,16,139,69,24,41,208,193,232,3,131,192,1,72,139

2279. .byte 108,36,8,85,195

2281. .globl lj_vm_record

2282. .hidden lj_vm_record

2283. .type lj_vm_record, @function

2284. .size lj_vm_record, 29

2285. lj_vm_record:

2286. .byte 65,15,182,134,217,244,255,255,168,32,117,83,168,16,117,56

2287. .byte 168,12,116,52,65,255,142,16,245,255,255,235,43

2289. .globl lj_vm_rethook

2290. .hidden lj_vm_rethook

2291. .type lj_vm_rethook, @function

2292. .size lj_vm_rethook, 14

2293. lj_vm_rethook:

2294. .byte 65,15,182,134,217,244,255,255,168,16,117,54,235,29

2296. .globl lj_vm_inshook

2297. .hidden lj_vm_inshook

2298. .type lj_vm_inshook, @function

2299. .size lj_vm_inshook, 68

2300. lj_vm_inshook:

2301. .byte 65,15,182,134,217,244,255,255,168,16,117,40,168,12,116,36

2302. .byte 65,255,142,16,245,255,255,116,4,168,4,116,23,139,108,36

2303. .byte 24,137,85,16,137,222,137,239,232

2304. .long lj_dispatch_ins-.-4

2305. .byte 139,85,16,15,182,75,253,15,182,107,252,15,183,67,254,65

2306. .byte 255,164,238,216,4,0,0

2308. .globl lj_cont_hook

2309. .hidden lj_cont_hook

2310. .type lj_cont_hook, @function

2311. .size lj_cont_hook, 12

2312. lj_cont_hook:

2313. .byte 131,195,4,139,77,232,137,76,36,4,235,224

2315. .globl lj_vm_hotloop

2316. .hidden lj_vm_hotloop

2317. .type lj_vm_hotloop, @function

2318. .size lj_vm_hotloop, 50

2319. lj_vm_hotloop:

2320. .byte 139,106,248,139,109,16,15,182,69,199,141,4,194,139,108,36

2321. .byte 24,137,85,16,137,69,24,137,222,65,141,190,224,245,255,255

2322. .byte 73,137,174,64,246,255,255,137,92,36,28,232

2323. .long lj_trace_hot-.-4

2324. .byte 235,171

2326. .globl lj_vm_callhook

2327. .hidden lj_vm_callhook

2328. .type lj_vm_callhook, @function

2329. .size lj_vm_callhook, 6

2330. lj_vm_callhook:

2331. .byte 137,92,36,28,235,7

2333. .globl lj_vm_hotcall

2334. .hidden lj_vm_hotcall

2335. .type lj_vm_hotcall, @function

2336. .size lj_vm_hotcall, 67

2337. lj_vm_hotcall:

2338. .byte 137,92,36,28,131,203,1,141,68,194,248,139,108,36,24,137

2339. .byte 85,16,137,69,24,137,222,137,239,232

2340. .long lj_dispatch_call-.-4

2341. .byte 199,68,36,28,0,0,0,0,131,227,254,139,85,16,72,137

2342. .byte 193,139,69,24,41,208,72,137,205,15,182,75,253,193,232,3

2343. .byte 131,192,1,255,229

2345. .globl lj_vm_exit_handler

2346. .hidden lj_vm_exit_handler

2347. .type lj_vm_exit_handler, @function

2348. .size lj_vm_exit_handler, 247

2349. lj_vm_exit_handler:

2350. .byte 65,85,65,84,65,83,65,82,65,81,65,80,87,86,85,72

2351. .byte 141,108,36,88,85,83,82,81,80,15,182,69,248,138,101,240

2352. .byte 76,137,125,248,76,137,117,240,68,139,117,0,65,139,142,48

2353. .byte 245,255,255,65,199,134,48,245,255,255,252,255,255,255,65,137

2354. .byte 134,60,255,255,255,65,137,142,56,255,255,255,72,129,236,128

2355. .byte 0,0,0,72,131,197,128,242,68,15,17,125,248,242,68,15

2356. .byte 17,117,240,242,68,15,17,109,232,242,68,15,17,101,224,242

2357. .byte 68,15,17,93,216,242,68,15,17,85,208,242,68,15,17,77

2358. .byte 200,242,68,15,17,69,192,242,15,17,125,184,242,15,17,117

2359. .byte 176,242,15,17,109,168,242,15,17,101,160,242,15,17,93,152

2360. .byte 242,15,17,85,144,242,15,17,77,136,242,15,17,69,128,65

2361. .byte 139,174,60,245,255,255,65,139,150,64,245,255,255,73,137,174

2362. .byte 64,246,255,255,65,199,134,60,245,255,255,0,0,0,0,137

2363. .byte 85,16,72,137,230,65,141,190,224,245,255,255,232

2364. .long lj_trace_exit-.-4

2365. .byte 72,139,77,48,72,131,225,252,72,137,204,137,105,24,139,85

2366. .byte 16,139,89,28,235,4

2368. .globl lj_vm_exit_interp

2369. .hidden lj_vm_exit_interp

2370. .type lj_vm_exit_interp, @function

2371. .size lj_vm_exit_interp, 93

2372. lj_vm_exit_interp:

2373. .byte 72,131,196,16,76,139,108,36,8,76,139,36,36,133,192,120

2374. .byte 65,137,68,36,4,68,139,122,248,69,139,127,16,69,139,127

2375. .byte 208,65,199,134,60,245,255,255,0,0,0,0,65,199,134,48

2376. .byte 245,255,255,255,255,255,255,139,3,15,182,204,15,182,232,131

2377. .byte 195,4,193,232,16,131,253,85,114,4,139,68,36,4,65,255

2378. .byte 36,238,247,216,137,239,137,198,232

2379. .long lj_err_throw-.-4

2381. .globl lj_vm_floor

2382. .hidden lj_vm_floor

2383. .type lj_vm_floor, @function

2384. .size lj_vm_floor, 0

2385. lj_vm_floor:

2387. .globl lj_vm_floor_sse

2388. .hidden lj_vm_floor_sse

2389. .type lj_vm_floor_sse, @function

2390. .size lj_vm_floor_sse, 91

2391. lj_vm_floor_sse:

2392. .byte 72,184,255,255,255,255,255,255,255,127,102,72,15,110,208,72

2393. .byte 184,0,0,0,0,0,0,48,67,102,72,15,110,216,15,40

2394. .byte 200,102,15,84,202,102,15,46,217,118,47,102,15,85,208,242

2395. .byte 15,88,203,242,15,92,203,102,15,86,202,72,184,0,0,0

2396. .byte 0,0,0,240,63,102,72,15,110,208,242,15,194,193,1,102

2397. .byte 15,84,194,242,15,92,200,15,40,193,195

2399. .globl lj_vm_ceil

2400. .hidden lj_vm_ceil

2401. .type lj_vm_ceil, @function

2402. .size lj_vm_ceil, 0

2403. lj_vm_ceil:

2405. .globl lj_vm_ceil_sse

2406. .hidden lj_vm_ceil_sse

2407. .type lj_vm_ceil_sse, @function

2408. .size lj_vm_ceil_sse, 91

2409. lj_vm_ceil_sse:

2410. .byte 72,184,255,255,255,255,255,255,255,127,102,72,15,110,208,72

2411. .byte 184,0,0,0,0,0,0,48,67,102,72,15,110,216,15,40

2412. .byte 200,102,15,84,202,102,15,46,217,118,47,102,15,85,208,242

2413. .byte 15,88,203,242,15,92,203,102,15,86,202,72,184,0,0,0

2414. .byte 0,0,0,240,191,102,72,15,110,208,242,15,194,193,6,102

2415. .byte 15,84,194,242,15,92,200,15,40,193,195

2417. .globl lj_vm_trunc

2418. .hidden lj_vm_trunc

2419. .type lj_vm_trunc, @function

2420. .size lj_vm_trunc, 0

2421. lj_vm_trunc:

2423. .globl lj_vm_trunc_sse

2424. .hidden lj_vm_trunc_sse

2425. .type lj_vm_trunc_sse, @function

2426. .size lj_vm_trunc_sse, 94

2427. lj_vm_trunc_sse:

2428. .byte 72,184,255,255,255,255,255,255,255,127,102,72,15,110,208,72

2429. .byte 184,0,0,0,0,0,0,48,67,102,72,15,110,216,15,40

2430. .byte 200,102,15,84,202,102,15,46,217,118,50,102,15,85,208,15

2431. .byte 40,193,242,15,88,203,242,15,92,203,72,184,0,0,0,0

2432. .byte 0,0,240,63,102,72,15,110,216,242,15,194,193,1,102,15

2433. .byte 84,195,242,15,92,200,102,15,86,202,15,40,193,195

2435. .globl lj_vm_mod

2436. .hidden lj_vm_mod

2437. .type lj_vm_mod, @function

2438. .size lj_vm_mod, 118

2439. lj_vm_mod:

2440. .byte 15,40,232,242,15,94,193,72,184,255,255,255,255,255,255,255

2441. .byte 127,102,72,15,110,208,72,184,0,0,0,0,0,0,48,67

2442. .byte 102,72,15,110,216,15,40,224,102,15,84,226,102,15,46,220

2443. .byte 118,56,102,15,85,208,242,15,88,227,242,15,92,227,102,15

2444. .byte 86,226,72,184,0,0,0,0,0,0,240,63,102,72,15,110

2445. .byte 208,242,15,194,196,1,102,15,84,194,242,15,92,224,15,40

2446. .byte 197,242,15,89,204,242,15,92,193,195,242,15,89,200,15,40

2447. .byte 197,242,15,92,193,195

2449. .globl lj_vm_log2

2450. .hidden lj_vm_log2

2451. .type lj_vm_log2, @function

2452. .size lj_vm_log2, 25

2453. lj_vm_log2:

2454. .byte 242,15,17,68,36,248,217,232,221,68,36,248,217,241,221,92

2455. .byte 36,248,242,15,16,68,36,248,195

2457. .globl lj_vm_exp_x87

2458. .hidden lj_vm_exp_x87

2459. .type lj_vm_exp_x87, @function

2460. .size lj_vm_exp_x87, 4

2461. lj_vm_exp_x87:

2462. .byte 217,234,222,201

2464. .globl lj_vm_exp2_x87

2465. .hidden lj_vm_exp2_x87

2466. .type lj_vm_exp2_x87, @function

2467. .size lj_vm_exp2_x87, 24

2468. lj_vm_exp2_x87:

2469. .byte 217,84,36,248,129,124,36,248,0,0,128,127,116,28,129,124

2470. .byte 36,248,0,0,128,255,116,19

2472. .globl lj_vm_exp2raw

2473. .hidden lj_vm_exp2raw

2474. .type lj_vm_exp2raw, @function

2475. .size lj_vm_exp2raw, 24

2476. lj_vm_exp2raw:

2477. .byte 217,192,217,252,220,233,217,201,217,240,217,232,222,193,217,253

2478. .byte 221,217,195,221,216,217,238,195

2480. .globl lj_vm_pow

2481. .hidden lj_vm_pow

2482. .type lj_vm_pow, @function

2483. .size lj_vm_pow, 0

2484. lj_vm_pow:

2486. .globl lj_vm_pow_sse

2487. .hidden lj_vm_pow_sse

2488. .type lj_vm_pow_sse, @function

2489. .size lj_vm_pow_sse, 24

2490. lj_vm_pow_sse:

2491. .byte 242,15,45,193,242,15,42,208,102,15,46,202,15,133,104,0

2492. .byte 0,0,15,138,199,0,0,0

2494. .globl lj_vm_powi_sse

2495. .hidden lj_vm_powi_sse

2496. .type lj_vm_powi_sse, @function

2497. .size lj_vm_powi_sse, 320

2498. lj_vm_powi_sse:

2499. .byte 131,248,1,126,43,169,1,0,0,0,117,8,242,15,89,192

2500. .byte 209,232,235,241,209,232,116,23,15,40,200,242,15,89,192,209

2501. .byte 232,116,8,115,246,242,15,89,200,235,240,242,15,89,193,195

2502. .byte 116,253,114,30,247,216,232,202,255,255,255,72,184,0,0,0

2503. .byte 0,0,0,240,63,102,72,15,110,200,242,15,94,200,15,40

2504. .byte 193,195,72,184,0,0,0,0,0,0,240,63,102,72,15,110

2505. .byte 192,195,102,72,15,126,200,72,209,224,72,193,192,12,72,61

2506. .byte 254,15,0,0,116,106,102,72,15,126,192,72,209,224,15,132

2507. .byte 164,0,0,0,72,193,192,12,72,61,254,15,0,0,15,132

2508. .byte 160,0,0,0,242,15,17,76,36,240,242,15,17,68,36,248

2509. .byte 221,68,36,240,221,68,36,248,217,241,217,192,217,252,220,233

2510. .byte 217,201,217,240,217,232,222,193,217,253,221,217,221,92,36,248

2511. .byte 242,15,16,68,36,248,195,72,184,0,0,0,0,0,0,240

2512. .byte 63,102,72,15,110,208,102,15,46,194,116,3,15,40,193,195

2513. .byte 72,184,255,255,255,255,255,255,255,127,102,72,15,110,208,102

2514. .byte 15,84,194,72,184,0,0,0,0,0,0,240,63,102,72,15

2515. .byte 110,208,102,15,46,194,116,215,102,15,80,193,15,87,192,136

2516. .byte 196,15,146,208,48,224,117,199,72,184,0,0,0,0,0,0

2517. .byte 240,127,102,72,15,110,192,195,102,15,80,193,133,192,117,232

2518. .byte 15,87,192,195,102,15,80,193,133,192,116,220,15,87,192,195

2520. .globl lj_vm_foldfpm

2521. .hidden lj_vm_foldfpm

2522. .type lj_vm_foldfpm, @function

2523. .size lj_vm_foldfpm, 131

2524. lj_vm_foldfpm:

2525. .byte 131,255,1,15,130,200,252,255,255,15,132,29,253,255,255,131

2526. .byte 255,3,15,130,111,253,255,255,119,5,242,15,81,192,195,242

2527. .byte 15,17,68,36,248,221,68,36,248,131,255,5,119,16,116,7

2528. .byte 232,63,254,255,255,235,64,232,60,254,255,255,235,57,131,255

2529. .byte 7,116,10,119,16,217,237,217,201,217,241,235,42,217,232,217

2530. .byte 201,217,241,235,34,131,255,9,116,10,119,12,217,236,217,201

2531. .byte 217,241,235,19,217,254,235,15,131,255,11,116,6,119,19,217

2532. .byte 255,235,4,217,242,221,216,221,92,36,248,242,15,16,68,36

2533. .byte 248,195,204

2535. .globl lj_vm_foldarith

2536. .hidden lj_vm_foldarith

2537. .type lj_vm_foldarith, @function

2538. .size lj_vm_foldarith, 160

2539. lj_vm_foldarith:

2540. .byte 131,255,1,116,7,119,10,242,15,88,193,195,242,15,92,193

2541. .byte 195,131,255,3,116,7,119,10,242,15,89,193,195,242,15,94

2542. .byte 193,195,131,255,5,15,130,55,253,255,255,15,132,244,253,255

2543. .byte 255,131,255,7,116,21,119,38,72,184,0,0,0,0,0,0

2544. .byte 0,128,102,72,15,110,200,15,87,193,195,72,184,255,255,255

2545. .byte 255,255,255,255,127,102,72,15,110,200,15,84,193,195,131,255

2546. .byte 9,119,43,242,15,17,68,36,248,242,15,17,76,36,240,221

2547. .byte 68,36,248,221,68,36,240,116,13,217,243,221,92,36,248,242

2548. .byte 15,16,68,36,248,195,217,201,217,253,221,217,235,237,131,255

2549. .byte 11,116,7,119,10,242,15,93,193,195,242,15,95,193,195,204

2551. .globl lj_vm_cpuid

2552. .hidden lj_vm_cpuid

2553. .type lj_vm_cpuid, @function

2554. .size lj_vm_cpuid, 18

2555. lj_vm_cpuid:

2556. .byte 137,248,83,15,162,137,6,137,94,4,137,78,8,137,86,12

2557. .byte 91,195

2559. .globl lj_assert_bad_for_arg_type

2560. .hidden lj_assert_bad_for_arg_type

2561. .type lj_assert_bad_for_arg_type, @function

2562. .size lj_assert_bad_for_arg_type, 2

2563. lj_assert_bad_for_arg_type:

2564. .byte 204,204

2566. .globl lj_vm_ffi_callback

2567. .hidden lj_vm_ffi_callback

2568. .type lj_vm_ffi_callback, @function

2569. .size lj_vm_ffi_callback, 179

2570. lj_vm_ffi_callback:

2571. .byte 83,65,87,65,86,72,131,236,40,68,141,181,184,11,0,0

2572. .byte 139,157,252,0,0,0,15,183,192,137,131,208,0,0,0,72

2573. .byte 137,123,112,72,137,115,120,72,137,147,128,0,0,0,72,137

2574. .byte 139,136,0,0,0,242,15,17,67,48,242,15,17,75,56,242

2575. .byte 15,17,83,64,242,15,17,91,72,72,141,68,36,80,76,137

2576. .byte 131,144,0,0,0,76,137,139,152,0,0,0,242,15,17,99

2577. .byte 80,242,15,17,107,88,242,15,17,115,96,242,15,17,123,104

2578. .byte 72,137,131,176,0,0,0,72,137,230,137,92,36,28,137,223

2579. .byte 232

2580. .long lj_ccallback_enter-.-4

2581. .byte 65,199,134,48,245,255,255,255,255,255,255,139,80,16,139,64

2582. .byte 24,41,208,139,106,248,193,232,3,131,192,1,139,93,16,139

2583. .byte 11,15,182,233,15,182,205,131,195,4,65,255,36,238

2585. .globl lj_cont_ffi_callback

2586. .hidden lj_cont_ffi_callback

2587. .type lj_cont_ffi_callback, @function

2588. .size lj_cont_ffi_callback, 44

2589. lj_cont_ffi_callback:

2590. .byte 139,76,36,24,65,139,158,68,245,255,255,72,137,75,16,137

2591. .byte 81,16,137,105,24,137,223,137,198,232

2592. .long lj_ccallback_leave-.-4

2593. .byte 72,139,67,112,242,15,16,67,48,233,255,223,255,255

2595. .globl lj_vm_ffi_call

2596. .hidden lj_vm_ffi_call

2597. .type lj_vm_ffi_call, @function

2598. .size lj_vm_ffi_call, 160

2599. lj_vm_ffi_call:

2600. .byte 85,72,137,229,83,72,137,251,139,67,8,72,41,196,15,182

2601. .byte 75,12,131,233,1,120,17,72,139,132,203,192,0,0,0,72

2602. .byte 137,4,204,131,233,1,121,239,15,182,67,15,72,139,187,144

2603. .byte 0,0,0,72,139,179,152,0,0,0,72,139,147,160,0,0

2604. .byte 0,72,139,139,168,0,0,0,76,139,131,176,0,0,0,76

2605. .byte 139,139,184,0,0,0,133,192,116,40,15,40,67,16,15,40

2606. .byte 75,32,15,40,83,48,15,40,91,64,131,248,4,118,19,15

2607. .byte 40,99,80,15,40,107,96,15,40,115,112,15,40,187,128,0

2608. .byte 0,0,255,19,72,137,131,144,0,0,0,15,41,67,16,72

2609. .byte 137,147,152,0,0,0,15,41,75,32,72,139,93,248,201,195

2611. .section .note.GNU-stack,"",@progbits

2612. .ident "DynASM 1.3.0"

2614. .section .debug_frame,"",@progbits

2615. .Lframe0:

2616. .long .LECIE0-.LSCIE0

2617. .LSCIE0:

2618. .long 0xffffffff

2619. .byte 0x1

2620. .string ""

2621. .uleb128 0x1

2622. .sleb128 -8

2623. .byte 0x10

2624. .byte 0xc

2625. .uleb128 0x7

2626. .uleb128 8

2627. .byte 0x80+0x10

2628. .uleb128 0x1

2629. .align 8

2630. .LECIE0:

2632. .LSFDE0:

2633. .long .LEFDE0-.LASFDE0

2634. .LASFDE0:

2635. .long .Lframe0

2636. .quad .Lbegin

2637. .quad 14652

2638. .byte 0xe

2639. .uleb128 80

2640. .byte 0x86

2641. .uleb128 0x2

2642. .byte 0x83

2643. .uleb128 0x3

2644. .byte 0x8f

2645. .uleb128 0x4

2646. .byte 0x8e

2647. .uleb128 0x5

2648. .align 8

2649. .LEFDE0:

2651. .LSFDE1:

2652. .long .LEFDE1-.LASFDE1

2653. .LASFDE1:

2654. .long .Lframe0

2655. .quad lj_vm_ffi_call

2656. .quad 160

2657. .byte 0xe

2658. .uleb128 16

2659. .byte 0x86

2660. .uleb128 0x2

2661. .byte 0xd

2662. .uleb128 0x6

2663. .byte 0x83

2664. .uleb128 0x3

2665. .align 8

2666. .LEFDE1:

2668. .section .eh_frame,"a",@progbits

2669. .Lframe1:

2670. .long .LECIE1-.LSCIE1

2671. .LSCIE1:

2672. .long 0

2673. .byte 0x1

2674. .string "zPR"

2675. .uleb128 0x1

2676. .sleb128 -8

2677. .byte 0x10

2678. .uleb128 6

2679. .byte 0x1b

2680. .long lj_err_unwind_dwarf-.

2681. .byte 0x1b

2682. .byte 0xc

2683. .uleb128 0x7

2684. .uleb128 8

2685. .byte 0x80+0x10

2686. .uleb128 0x1

2687. .align 8

2688. .LECIE1:

2690. .LSFDE2:

2691. .long .LEFDE2-.LASFDE2

2692. .LASFDE2:

2693. .long .LASFDE2-.Lframe1

2694. .long .Lbegin-.

2695. .long 14652

2696. .uleb128 0

2697. .byte 0xe

2698. .uleb128 80

2699. .byte 0x86

2700. .uleb128 0x2

2701. .byte 0x83

2702. .uleb128 0x3

2703. .byte 0x8f

2704. .uleb128 0x4

2705. .byte 0x8e

2706. .uleb128 0x5

2707. .align 8

2708. .LEFDE2:

2710. .Lframe2:

2711. .long .LECIE2-.LSCIE2

2712. .LSCIE2:

2713. .long 0

2714. .byte 0x1

2715. .string "zR"

2716. .uleb128 0x1

2717. .sleb128 -8

2718. .byte 0x10

2719. .uleb128 1

2720. .byte 0x1b

2721. .byte 0xc

2722. .uleb128 0x7

2723. .uleb128 8

2724. .byte 0x80+0x10

2725. .uleb128 0x1

2726. .align 8

2727. .LECIE2:

2729. .LSFDE3:

2730. .long .LEFDE3-.LASFDE3

2731. .LASFDE3:

2732. .long .LASFDE3-.Lframe2

2733. .long lj_vm_ffi_call-.

2734. .long 160

2735. .uleb128 0

2736. .byte 0xe

2737. .uleb128 16

2738. .byte 0x86

2739. .uleb128 0x2

2740. .byte 0xd

2741. .uleb128 0x6

2742. .byte 0x83

2743. .uleb128 0x3

2744. .align 8

2745. .LEFDE3:

One Level Up                               Top Level

One Level Up                               Top Level

src/lua.hpp - luajit-2.0-src

1. // C++ wrapper for LuaJIT header files.

3. extern "C" {

4. #include "lua.h"

5. #include "lauxlib.h"

6. #include "lualib.h"

7. #include "luajit.h"

8. }

One Level Up                               Top Level

One Level Up                               Top Level

src/msvcbuild.bat - luajit-2.0-src

Global variables defined

- ALL_LIB

- DASM

- DASMDIR

- DASMFLAGS

- DASMFLAGS

- LJARCH

- LJARCH

- LJCOMPILE

- LJCOMPILE

- LJCOMPILE

- LJDLLNAME

- LJLIB

- LJLIBNAME

- LJLINK

- LJLINK

- LJMT

Source code

1. @rem Script to build LuaJIT with MSVC.

2. @rem Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h

3. @rem

4. @rem Either open a "Visual Studio .NET Command Prompt"

5. @rem (Note that the Express Edition does not contain an x64 compiler)

6. @rem -or-

7. @rem Open a "Windows SDK Command Shell" and set the compiler environment:

8. @rem     setenv /release /x86

9. @rem   -or-

10. @rem     setenv /release /x64

11. @rem

12. @rem Then cd to this directory and run this script.

14. @if not defined INCLUDE goto :FAIL

16. @setlocal

17. ‌@set LJCOMPILE=cl /nologo /c /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE

18. ‌@set LJLINK=link /nologo

19. ‌@set LJMT=mt /nologo

20. ‌@set LJLIB=lib /nologo /nodefaultlib

21. ‌@set DASMDIR=..\dynasm

22. ‌@set DASM=%DASMDIR%\dynasm.lua

23. ‌@set LJDLLNAME=lua51.dll

24. ‌@set LJLIBNAME=lua51.lib

25. ‌@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c

27. %LJCOMPILE% host\minilua.c

28. @if errorlevel 1 goto :BAD

29. %LJLINK% /out:minilua.exe minilua.obj

30. @if errorlevel 1 goto :BAD

31. if exist minilua.exe.manifest^

32. %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe

34. ‌@set DASMFLAGS=-D WIN -D JIT -D FFI -D P64

35. ‌@set LJARCH=x64

36. @minilua

37. @if errorlevel 8 goto :X64

38. ‌@set DASMFLAGS=-D WIN -D JIT -D FFI

39. ‌@set LJARCH=x86

40. ‌@set LJCOMPILE=%LJCOMPILE% /arch:SSE2

41. :X64

42. minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_x86.dasc

43. @if errorlevel 1 goto :BAD

45. %LJCOMPILE% /I "." /I %DASMDIR% host\buildvm*.c

46. @if errorlevel 1 goto :BAD

47. %LJLINK% /out:buildvm.exe buildvm*.obj

48. @if errorlevel 1 goto :BAD

49. if exist buildvm.exe.manifest^

50. %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe

52. buildvm -m peobj -o lj_vm.obj

53. @if errorlevel 1 goto :BAD

54. buildvm -m bcdef -o lj_bcdef.h %ALL_LIB%

55. @if errorlevel 1 goto :BAD

56. buildvm -m ffdef -o lj_ffdef.h %ALL_LIB%

57. @if errorlevel 1 goto :BAD

58. buildvm -m libdef -o lj_libdef.h %ALL_LIB%

59. @if errorlevel 1 goto :BAD

60. buildvm -m recdef -o lj_recdef.h %ALL_LIB%

61. @if errorlevel 1 goto :BAD

62. buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB%

63. @if errorlevel 1 goto :BAD

64. buildvm -m folddef -o lj_folddef.h lj_opt_fold.c

65. @if errorlevel 1 goto :BAD

67. @if "%1" neq "debug" goto :NODEBUG

68. @shift

69. ‌@set LJCOMPILE=%LJCOMPILE% /Zi

70. ‌@set LJLINK=%LJLINK% /debug

71. :NODEBUG

72. @if "%1"=="amalg" goto :AMALGDLL

73. @if "%1"=="static" goto :STATIC

74. %LJCOMPILE% /MD /DLUA_BUILD_AS_DLL lj_*.c lib_*.c

75. @if errorlevel 1 goto :BAD

76. %LJLINK% /DLL /out:%LJDLLNAME% lj_*.obj lib_*.obj

77. @if errorlevel 1 goto :BAD

78. @goto :MTDLL

79. :STATIC

80. %LJCOMPILE% lj_*.c lib_*.c

81. @if errorlevel 1 goto :BAD

82. %LJLIB% /OUT:%LJLIBNAME% lj_*.obj lib_*.obj

83. @if errorlevel 1 goto :BAD

84. @goto :MTDLL

85. :AMALGDLL

86. %LJCOMPILE% /MD /DLUA_BUILD_AS_DLL ljamalg.c

87. @if errorlevel 1 goto :BAD

88. %LJLINK% /DLL /out:%LJDLLNAME% ljamalg.obj lj_vm.obj

89. @if errorlevel 1 goto :BAD

90. :MTDLL

91. if exist %LJDLLNAME%.manifest^

92. %LJMT% -manifest %LJDLLNAME%.manifest -outputresource:%LJDLLNAME%;2

94. %LJCOMPILE% luajit.c

95. @if errorlevel 1 goto :BAD

96. %LJLINK% /out:luajit.exe luajit.obj %LJLIBNAME%

97. @if errorlevel 1 goto :BAD

98. if exist luajit.exe.manifest^

99. %LJMT% -manifest luajit.exe.manifest -outputresource:luajit.exe

101. @del *.obj *.manifest minilua.exe buildvm.exe

102. @echo.

103. @echo === Successfully built LuaJIT for Windows/%LJARCH% ===

105. @goto :END

106. :BAD

107. @echo.

108. @echo *******************************************************

109. @echo *** Build FAILED -- Please check the error messages ***

110. @echo *******************************************************

111. @goto :END

112. :FAIL

113. @echo You must open a "Visual Studio .NET Command Prompt" to run this script

114. :END

One Level Up                               Top Level

One Level Up                               Top Level

src/ps4build.bat - luajit-2.0-src

Global variables defined

- ALL_LIB

- DASM

- DASMDIR

- DASMFLAGS

- INCLUDE

- LJCOMPILE

- LJCOMPILE

- LJCOMPILE

- LJCOMPILE

- LJLIB

- LJLINK

- LJMT

- TARGETLIB

- TARGETLIB

Source code

1. @rem Script to build LuaJIT with the PS4 SDK.

2. @rem Donated to the public domain.

3. @rem

4. @rem Open a "Visual Studio .NET Command Prompt" (64 bit host compiler)

5. @rem Then cd to this directory and run this script.

7. @if not defined INCLUDE goto :FAIL

8. @if not defined SCE_ORBIS_SDK_DIR goto :FAIL

10. @setlocal

11. @rem ---- Host compiler ----

12. ‌@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE

13. ‌@set LJLINK=link /nologo

14. ‌@set LJMT=mt /nologo

15. ‌@set DASMDIR=..\dynasm

16. ‌@set DASM=%DASMDIR%\dynasm.lua

17. ‌@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c

19. %LJCOMPILE% host\minilua.c

20. @if errorlevel 1 goto :BAD

21. %LJLINK% /out:minilua.exe minilua.obj

22. @if errorlevel 1 goto :BAD

23. if exist minilua.exe.manifest^

24. %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe

26. @rem Check for 64 bit host compiler.

27. @minilua

28. @if not errorlevel 8 goto :FAIL

30. ‌@set DASMFLAGS=-D P64

31. minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_x86.dasc

32. @if errorlevel 1 goto :BAD

34. %LJCOMPILE% /I "." /I %DASMDIR% -DLUAJIT_TARGET=LUAJIT_ARCH_X64 -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI host\buildvm*.c

35. @if errorlevel 1 goto :BAD

36. %LJLINK% /out:buildvm.exe buildvm*.obj

37. @if errorlevel 1 goto :BAD

38. if exist buildvm.exe.manifest^

39. %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe

41. buildvm -m elfasm -o lj_vm.s

42. @if errorlevel 1 goto :BAD

43. buildvm -m bcdef -o lj_bcdef.h %ALL_LIB%

44. @if errorlevel 1 goto :BAD

45. buildvm -m ffdef -o lj_ffdef.h %ALL_LIB%

46. @if errorlevel 1 goto :BAD

47. buildvm -m libdef -o lj_libdef.h %ALL_LIB%

48. @if errorlevel 1 goto :BAD

49. buildvm -m recdef -o lj_recdef.h %ALL_LIB%

50. @if errorlevel 1 goto :BAD

51. buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB%

52. @if errorlevel 1 goto :BAD

53. buildvm -m folddef -o lj_folddef.h lj_opt_fold.c

54. @if errorlevel 1 goto :BAD

56. @rem ---- Cross compiler ----

57. ‌@set LJCOMPILE="%SCE_ORBIS_SDK_DIR%\host_tools\bin\orbis-clang" -c -Wall -DLUAJIT_DISABLE_FFI

58. ‌@set LJLIB="%SCE_ORBIS_SDK_DIR%\host_tools\bin\orbis-ar" rcus

59. ‌@set INCLUDE=""

61. orbis-as -o lj_vm.o lj_vm.s

63. @if "%1" neq "debug" goto :NODEBUG

64. @shift

65. ‌@set LJCOMPILE=%LJCOMPILE% -g -O0

66. ‌@set TARGETLIB=libluajitD.a

67. goto :BUILD

68. :NODEBUG

69. ‌@set LJCOMPILE=%LJCOMPILE% -O2

70. ‌@set TARGETLIB=libluajit.a

71. :BUILD

72. del %TARGETLIB%

73. @if "%1"=="amalg" goto :AMALG

74. for %%f in (lj_*.c lib_*.c) do (

75. %LJCOMPILE% %%f

76. @if errorlevel 1 goto :BAD

77. )

79. %LJLIB% %TARGETLIB% lj_*.o lib_*.o

80. @if errorlevel 1 goto :BAD

81. @goto :NOAMALG

82. :AMALG

83. %LJCOMPILE% ljamalg.c

84. @if errorlevel 1 goto :BAD

85. %LJLIB% %TARGETLIB% ljamalg.o lj_vm.o

86. @if errorlevel 1 goto :BAD

87. :NOAMALG

89. @del *.o *.obj *.manifest minilua.exe buildvm.exe

90. @echo.

91. @echo === Successfully built LuaJIT for PS4 ===

93. @goto :END

94. :BAD

95. @echo.

96. @echo *******************************************************

97. @echo *** Build FAILED -- Please check the error messages ***

98. @echo *******************************************************

99. @goto :END

100. :FAIL

101. @echo To run this script you must open a "Visual Studio .NET Command Prompt"

102. @echo (64 bit host compiler). The PS4 Orbis SDK must be installed, too.

103. :END

One Level Up                               Top Level

One Level Up                               Top Level

src/psvitabuild.bat - luajit-2.0-src

Global variables defined

- ALL_LIB

- DASM

- DASMDIR

- DASMFLAGS

- INCLUDE

- LJCOMPILE

- LJCOMPILE

- LJCOMPILE

- LJCOMPILE

- LJLIB

- LJLINK

- LJMT

- TARGETLIB

- TARGETLIB

Source code

1. @rem Script to build LuaJIT with the PS Vita SDK.

2. @rem Donated to the public domain.

3. @rem

4. @rem Open a "Visual Studio .NET Command Prompt" (32 bit host compiler)

5. @rem Then cd to this directory and run this script.

7. @if not defined INCLUDE goto :FAIL

8. @if not defined SCE_PSP2_SDK_DIR goto :FAIL

10. @setlocal

11. @rem ---- Host compiler ----

12. ‌@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE

13. ‌@set LJLINK=link /nologo

14. ‌@set LJMT=mt /nologo

15. ‌@set DASMDIR=..\dynasm

16. ‌@set DASM=%DASMDIR%\dynasm.lua

17. ‌@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c

19. %LJCOMPILE% host\minilua.c

20. @if errorlevel 1 goto :BAD

21. %LJLINK% /out:minilua.exe minilua.obj

22. @if errorlevel 1 goto :BAD

23. if exist minilua.exe.manifest^

24. %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe

26. @rem Check for 32 bit host compiler.

27. @minilua

28. @if errorlevel 8 goto :FAIL

30. ‌@set DASMFLAGS=-D FPU -D HFABI

31. minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_arm.dasc

32. @if errorlevel 1 goto :BAD

34. %LJCOMPILE% /I "." /I %DASMDIR% -DLUAJIT_TARGET=LUAJIT_ARCH_ARM -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLJ_TARGET_PSVITA=1 host\buildvm*.c

35. @if errorlevel 1 goto :BAD

36. %LJLINK% /out:buildvm.exe buildvm*.obj

37. @if errorlevel 1 goto :BAD

38. if exist buildvm.exe.manifest^

39. %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe

41. buildvm -m elfasm -o lj_vm.s

42. @if errorlevel 1 goto :BAD

43. buildvm -m bcdef -o lj_bcdef.h %ALL_LIB%

44. @if errorlevel 1 goto :BAD

45. buildvm -m ffdef -o lj_ffdef.h %ALL_LIB%

46. @if errorlevel 1 goto :BAD

47. buildvm -m libdef -o lj_libdef.h %ALL_LIB%

48. @if errorlevel 1 goto :BAD

49. buildvm -m recdef -o lj_recdef.h %ALL_LIB%

50. @if errorlevel 1 goto :BAD

51. buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB%

52. @if errorlevel 1 goto :BAD

53. buildvm -m folddef -o lj_folddef.h lj_opt_fold.c

54. @if errorlevel 1 goto :BAD

56. @rem ---- Cross compiler ----

57. ‌@set LJCOMPILE="%SCE_PSP2_SDK_DIR%\host_tools\build\bin\psp2snc" -c -w -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC

58. ‌@set LJLIB="%SCE_PSP2_SDK_DIR%\host_tools\build\bin\psp2ld32" -r --output=

59. ‌@set INCLUDE=""

61. "%SCE_PSP2_SDK_DIR%\host_tools\build\bin\psp2as" -o lj_vm.o lj_vm.s

63. @if "%1" neq "debug" goto :NODEBUG

64. @shift

65. ‌@set LJCOMPILE=%LJCOMPILE% -g -O0

66. ‌@set TARGETLIB=libluajitD.a

67. goto :BUILD

68. :NODEBUG

69. ‌@set LJCOMPILE=%LJCOMPILE% -O2

70. ‌@set TARGETLIB=libluajit.a

71. :BUILD

72. del %TARGETLIB%

74. %LJCOMPILE% ljamalg.c

75. @if errorlevel 1 goto :BAD

76. %LJLIB%%TARGETLIB% ljamalg.o lj_vm.o

77. @if errorlevel 1 goto :BAD

79. @del *.o *.obj *.manifest minilua.exe buildvm.exe

80. @echo.

81. @echo === Successfully built LuaJIT for PS Vita ===

83. @goto :END

84. :BAD

85. @echo.

86. @echo *******************************************************

87. @echo *** Build FAILED -- Please check the error messages ***

88. @echo *******************************************************

89. @goto :END

90. :FAIL

91. @echo To run this script you must open a "Visual Studio .NET Command Prompt"

92. @echo (32 bit host compiler). The PS Vita SDK must be installed, too.

93. :END

One Level Up                               Top Level

One Level Up                               Top Level

src/xedkbuild.bat - luajit-2.0-src

Global variables defined

- ALL_LIB

- DASM

- DASMDIR

- DASMFLAGS

- LJCOMPILE

- LJCOMPILE

- LJLIB

- LJLINK

- LJMT

Source code

1. @rem Script to build LuaJIT with the Xbox 360 SDK.

2. @rem Donated to the public domain.

3. @rem

4. @rem Open a "Visual Studio .NET Command Prompt" (32 bit host compiler)

5. @rem Then cd to this directory and run this script.

7. @if not defined INCLUDE goto :FAIL

8. @if not defined XEDK goto :FAIL

10. @setlocal

11. @rem ---- Host compiler ----

12. ‌@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE

13. ‌@set LJLINK=link /nologo

14. ‌@set LJMT=mt /nologo

15. ‌@set DASMDIR=..\dynasm

16. ‌@set DASM=%DASMDIR%\dynasm.lua

17. ‌@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c

19. %LJCOMPILE% host\minilua.c

20. @if errorlevel 1 goto :BAD

21. %LJLINK% /out:minilua.exe minilua.obj

22. @if errorlevel 1 goto :BAD

23. if exist minilua.exe.manifest^

24. %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe

26. @rem Error out for 64 bit host compiler

27. @minilua

28. @if errorlevel 8 goto :FAIL

30. ‌@set DASMFLAGS=-D GPR64 -D FRAME32 -D PPE -D SQRT -D DUALNUM

31. minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_ppc.dasc

32. @if errorlevel 1 goto :BAD

34. %LJCOMPILE% /I "." /I %DASMDIR% /D_XBOX_VER=200 /DLUAJIT_TARGET=LUAJIT_ARCH_PPC  host\buildvm*.c

35. @if errorlevel 1 goto :BAD

36. %LJLINK% /out:buildvm.exe buildvm*.obj

37. @if errorlevel 1 goto :BAD

38. if exist buildvm.exe.manifest^

39. %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe

41. buildvm -m peobj -o lj_vm.obj

42. @if errorlevel 1 goto :BAD

43. buildvm -m bcdef -o lj_bcdef.h %ALL_LIB%

44. @if errorlevel 1 goto :BAD

45. buildvm -m ffdef -o lj_ffdef.h %ALL_LIB%

46. @if errorlevel 1 goto :BAD

47. buildvm -m libdef -o lj_libdef.h %ALL_LIB%

48. @if errorlevel 1 goto :BAD

49. buildvm -m recdef -o lj_recdef.h %ALL_LIB%

50. @if errorlevel 1 goto :BAD

51. buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB%

52. @if errorlevel 1 goto :BAD

53. buildvm -m folddef -o lj_folddef.h lj_opt_fold.c

54. @if errorlevel 1 goto :BAD

56. @rem ---- Cross compiler ----

57. ‌@set LJCOMPILE="%XEDK%\bin\win32\cl" /nologo /c /MT /O2 /W3 /GF /Gm- /GR- /GS- /Gy /openmp- /D_CRT_SECURE_NO_DEPRECATE /DNDEBUG /D_XBOX /D_LIB /DLUAJIT_USE_SYSMALLOC

58. ‌@set LJLIB="%XEDK%\bin\win32\lib" /nologo

59. @set "INCLUDE=%XEDK%\include\xbox"

61. @if "%1" neq "debug" goto :NODEBUG

62. @shift

63. @set "LJCOMPILE=%LJCOMPILE% /Zi"

64. :NODEBUG

65. @if "%1"=="amalg" goto :AMALG

66. %LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c

67. @if errorlevel 1 goto :BAD

68. %LJLIB% /OUT:luajit20.lib lj_*.obj lib_*.obj

69. @if errorlevel 1 goto :BAD

70. @goto :NOAMALG

71. :AMALG

72. %LJCOMPILE% /DLUA_BUILD_AS_DLL ljamalg.c

73. @if errorlevel 1 goto :BAD

74. %LJLIB% /OUT:luajit20.lib ljamalg.obj lj_vm.obj

75. @if errorlevel 1 goto :BAD

76. :NOAMALG

78. @del *.obj *.manifest minilua.exe buildvm.exe

79. @echo.

80. @echo === Successfully built LuaJIT for Xbox 360 ===

82. @goto :END

83. :BAD

84. @echo.

85. @echo *******************************************************

86. @echo *** Build FAILED -- Please check the error messages ***

87. @echo *******************************************************

88. @goto :END

89. :FAIL

90. @echo To run this script you must open a "Visual Studio .NET Command Prompt"

91. @echo (32 bit host compiler). The Xbox 360 SDK must be installed, too.

92. :END

One Level Up                               Top Level