PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vm/quotations.c

https://github.com/Keyholder/factor
C | 544 lines | 409 code | 88 blank | 47 comment | 109 complexity | b4f56cb4c6285e3df45c1771de301684 MD5 | raw file
  1. #include "master.h"
  2. /* Simple non-optimizing compiler.
  3. This is one of the two compilers implementing Factor; the second one is written
  4. in Factor and performs advanced optimizations. See core/compiler/compiler.factor.
  5. The non-optimizing compiler compiles a quotation at a time by concatenating
  6. machine code chunks; prolog, epilog, call word, jump to word, etc. These machine
  7. code chunks are generated from Factor code in core/cpu/.../bootstrap.factor.
  8. Calls to words and constant quotations (referenced by conditionals and dips)
  9. are direct jumps to machine code blocks. Literals are also referenced directly
  10. without going through the literal table.
  11. It actually does do a little bit of very simple optimization:
  12. 1) Tail call optimization.
  13. 2) If a quotation is determined to not call any other words (except for a few
  14. special words which are open-coded, see below), then no prolog/epilog is
  15. generated.
  16. 3) When in tail position and immediately preceded by literal arguments, the
  17. 'if' and 'dispatch' conditionals are generated inline, instead of as a call to
  18. the 'if' word.
  19. 4) When preceded by a quotation, calls to 'dip', '2dip' and '3dip' are
  20. open-coded as retain stack manipulation surrounding a subroutine call.
  21. 5) When preceded by an array, calls to the 'declare' word are optimized out
  22. entirely. This word is only used by the optimizing compiler, and with the
  23. non-optimizing compiler it would otherwise just decrease performance to have to
  24. push the array and immediately drop it after.
  25. 6) Sub-primitives are primitive words which are implemented in assembly and not
  26. in the VM. They are open-coded and no subroutine call is generated. This
  27. includes stack shufflers, some fixnum arithmetic words, and words such as tag,
  28. slot and eq?. A primitive call is relatively expensive (two subroutine calls)
  29. so this results in a big speedup for relatively little effort. */
  30. bool jit_primitive_call_p(F_ARRAY *array, CELL i)
  31. {
  32. return (i + 2) == array_capacity(array)
  33. && type_of(array_nth(array,i)) == FIXNUM_TYPE
  34. && array_nth(array,i + 1) == userenv[JIT_PRIMITIVE_WORD];
  35. }
  36. bool jit_fast_if_p(F_ARRAY *array, CELL i)
  37. {
  38. return (i + 3) == array_capacity(array)
  39. && type_of(array_nth(array,i)) == QUOTATION_TYPE
  40. && type_of(array_nth(array,i + 1)) == QUOTATION_TYPE
  41. && array_nth(array,i + 2) == userenv[JIT_IF_WORD];
  42. }
  43. bool jit_fast_dispatch_p(F_ARRAY *array, CELL i)
  44. {
  45. return (i + 2) == array_capacity(array)
  46. && type_of(array_nth(array,i)) == ARRAY_TYPE
  47. && array_nth(array,i + 1) == userenv[JIT_DISPATCH_WORD];
  48. }
  49. bool jit_fast_dip_p(F_ARRAY *array, CELL i)
  50. {
  51. return (i + 2) <= array_capacity(array)
  52. && type_of(array_nth(array,i)) == QUOTATION_TYPE
  53. && array_nth(array,i + 1) == userenv[JIT_DIP_WORD];
  54. }
  55. bool jit_fast_2dip_p(F_ARRAY *array, CELL i)
  56. {
  57. return (i + 2) <= array_capacity(array)
  58. && type_of(array_nth(array,i)) == QUOTATION_TYPE
  59. && array_nth(array,i + 1) == userenv[JIT_2DIP_WORD];
  60. }
  61. bool jit_fast_3dip_p(F_ARRAY *array, CELL i)
  62. {
  63. return (i + 2) <= array_capacity(array)
  64. && type_of(array_nth(array,i)) == QUOTATION_TYPE
  65. && array_nth(array,i + 1) == userenv[JIT_3DIP_WORD];
  66. }
  67. bool jit_ignore_declare_p(F_ARRAY *array, CELL i)
  68. {
  69. return (i + 1) < array_capacity(array)
  70. && type_of(array_nth(array,i)) == ARRAY_TYPE
  71. && array_nth(array,i + 1) == userenv[JIT_DECLARE_WORD];
  72. }
  73. F_ARRAY *code_to_emit(CELL code)
  74. {
  75. return untag_object(array_nth(untag_object(code),0));
  76. }
  77. F_REL rel_to_emit(CELL code, CELL code_format, CELL code_length, bool *rel_p)
  78. {
  79. F_ARRAY *quadruple = untag_object(code);
  80. CELL rel_class = array_nth(quadruple,1);
  81. CELL rel_type = array_nth(quadruple,2);
  82. CELL offset = array_nth(quadruple,3);
  83. if(rel_class == F)
  84. {
  85. *rel_p = false;
  86. return 0;
  87. }
  88. else
  89. {
  90. *rel_p = true;
  91. return (to_fixnum(rel_type) << 28)
  92. | (to_fixnum(rel_class) << 24)
  93. | ((code_length + to_fixnum(offset)) * code_format);
  94. }
  95. }
  96. #define EMIT(name) { \
  97. bool rel_p; \
  98. F_REL rel = rel_to_emit(name,code_format,code_count,&rel_p); \
  99. if(rel_p) GROWABLE_BYTE_ARRAY_APPEND(relocation,&rel,sizeof(F_REL)); \
  100. GROWABLE_ARRAY_APPEND(code,code_to_emit(name)); \
  101. }
  102. bool jit_stack_frame_p(F_ARRAY *array)
  103. {
  104. F_FIXNUM length = array_capacity(array);
  105. F_FIXNUM i;
  106. for(i = 0; i < length - 1; i++)
  107. {
  108. CELL obj = array_nth(array,i);
  109. if(type_of(obj) == WORD_TYPE)
  110. {
  111. F_WORD *word = untag_object(obj);
  112. if(word->subprimitive == F && obj != userenv[JIT_DECLARE_WORD])
  113. return true;
  114. }
  115. else if(type_of(obj) == QUOTATION_TYPE)
  116. {
  117. if(jit_fast_dip_p(array,i)
  118. || jit_fast_2dip_p(array,i)
  119. || jit_fast_3dip_p(array,i))
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. void set_quot_xt(F_QUOTATION *quot, F_CODE_BLOCK *code)
  126. {
  127. if(code->block.type != QUOTATION_TYPE)
  128. critical_error("Bad param to set_quot_xt",(CELL)code);
  129. quot->code = code;
  130. quot->xt = (XT)(code + 1);
  131. quot->compiledp = T;
  132. }
  133. /* Might GC */
  134. void jit_compile(CELL quot, bool relocate)
  135. {
  136. if(untag_quotation(quot)->compiledp != F)
  137. return;
  138. CELL code_format = compiled_code_format();
  139. REGISTER_ROOT(quot);
  140. CELL array = untag_quotation(quot)->array;
  141. REGISTER_ROOT(array);
  142. GROWABLE_ARRAY(code);
  143. REGISTER_ROOT(code);
  144. GROWABLE_BYTE_ARRAY(relocation);
  145. REGISTER_ROOT(relocation);
  146. GROWABLE_ARRAY(literals);
  147. REGISTER_ROOT(literals);
  148. if(stack_traces_p())
  149. GROWABLE_ARRAY_ADD(literals,quot);
  150. bool stack_frame = jit_stack_frame_p(untag_object(array));
  151. if(stack_frame)
  152. EMIT(userenv[JIT_PROLOG]);
  153. CELL i;
  154. CELL length = array_capacity(untag_object(array));
  155. bool tail_call = false;
  156. for(i = 0; i < length; i++)
  157. {
  158. CELL obj = array_nth(untag_object(array),i);
  159. F_WORD *word;
  160. F_WRAPPER *wrapper;
  161. switch(type_of(obj))
  162. {
  163. case WORD_TYPE:
  164. word = untag_object(obj);
  165. /* Intrinsics */
  166. if(word->subprimitive != F)
  167. {
  168. if(array_nth(untag_object(word->subprimitive),1) != F)
  169. {
  170. GROWABLE_ARRAY_ADD(literals,T);
  171. }
  172. EMIT(word->subprimitive);
  173. }
  174. else
  175. {
  176. GROWABLE_ARRAY_ADD(literals,obj);
  177. if(i == length - 1)
  178. {
  179. if(stack_frame)
  180. EMIT(userenv[JIT_EPILOG]);
  181. EMIT(userenv[JIT_WORD_JUMP]);
  182. tail_call = true;
  183. }
  184. else
  185. EMIT(userenv[JIT_WORD_CALL]);
  186. }
  187. break;
  188. case WRAPPER_TYPE:
  189. wrapper = untag_object(obj);
  190. GROWABLE_ARRAY_ADD(literals,wrapper->object);
  191. EMIT(userenv[JIT_PUSH_IMMEDIATE]);
  192. break;
  193. case FIXNUM_TYPE:
  194. if(jit_primitive_call_p(untag_object(array),i))
  195. {
  196. EMIT(userenv[JIT_SAVE_STACK]);
  197. GROWABLE_ARRAY_ADD(literals,obj);
  198. EMIT(userenv[JIT_PRIMITIVE]);
  199. i++;
  200. tail_call = true;
  201. break;
  202. }
  203. case QUOTATION_TYPE:
  204. if(jit_fast_if_p(untag_object(array),i))
  205. {
  206. if(stack_frame)
  207. EMIT(userenv[JIT_EPILOG]);
  208. jit_compile(array_nth(untag_object(array),i),relocate);
  209. jit_compile(array_nth(untag_object(array),i + 1),relocate);
  210. GROWABLE_ARRAY_ADD(literals,array_nth(untag_object(array),i));
  211. EMIT(userenv[JIT_IF_1]);
  212. GROWABLE_ARRAY_ADD(literals,array_nth(untag_object(array),i + 1));
  213. EMIT(userenv[JIT_IF_2]);
  214. i += 2;
  215. tail_call = true;
  216. break;
  217. }
  218. else if(jit_fast_dip_p(untag_object(array),i))
  219. {
  220. jit_compile(obj,relocate);
  221. GROWABLE_ARRAY_ADD(literals,array_nth(untag_object(array),i));
  222. EMIT(userenv[JIT_DIP]);
  223. i++;
  224. break;
  225. }
  226. else if(jit_fast_2dip_p(untag_object(array),i))
  227. {
  228. jit_compile(obj,relocate);
  229. GROWABLE_ARRAY_ADD(literals,array_nth(untag_object(array),i));
  230. EMIT(userenv[JIT_2DIP]);
  231. i++;
  232. break;
  233. }
  234. else if(jit_fast_3dip_p(untag_object(array),i))
  235. {
  236. jit_compile(obj,relocate);
  237. GROWABLE_ARRAY_ADD(literals,array_nth(untag_object(array),i));
  238. EMIT(userenv[JIT_3DIP]);
  239. i++;
  240. break;
  241. }
  242. case ARRAY_TYPE:
  243. if(jit_fast_dispatch_p(untag_object(array),i))
  244. {
  245. if(stack_frame)
  246. EMIT(userenv[JIT_EPILOG]);
  247. GROWABLE_ARRAY_ADD(literals,array_nth(untag_object(array),i));
  248. EMIT(userenv[JIT_DISPATCH]);
  249. i++;
  250. tail_call = true;
  251. break;
  252. }
  253. else if(jit_ignore_declare_p(untag_object(array),i))
  254. {
  255. i++;
  256. break;
  257. }
  258. default:
  259. GROWABLE_ARRAY_ADD(literals,obj);
  260. EMIT(userenv[JIT_PUSH_IMMEDIATE]);
  261. break;
  262. }
  263. }
  264. if(!tail_call)
  265. {
  266. if(stack_frame)
  267. EMIT(userenv[JIT_EPILOG]);
  268. EMIT(userenv[JIT_RETURN]);
  269. }
  270. GROWABLE_ARRAY_TRIM(code);
  271. GROWABLE_ARRAY_TRIM(literals);
  272. GROWABLE_BYTE_ARRAY_TRIM(relocation);
  273. F_CODE_BLOCK *compiled = add_code_block(
  274. QUOTATION_TYPE,
  275. untag_object(code),
  276. NULL,
  277. relocation,
  278. literals);
  279. set_quot_xt(untag_object(quot),compiled);
  280. if(relocate)
  281. relocate_code_block(compiled);
  282. UNREGISTER_ROOT(literals);
  283. UNREGISTER_ROOT(relocation);
  284. UNREGISTER_ROOT(code);
  285. UNREGISTER_ROOT(array);
  286. UNREGISTER_ROOT(quot);
  287. }
  288. /* Crappy code duplication. If C had closures (not just function pointers)
  289. it would be easy to get rid of, but I can't think of a good way to deal
  290. with it right now that doesn't involve lots of boilerplate that would be
  291. worse than the duplication itself (eg, putting all state in some global
  292. struct.) */
  293. #define COUNT(name,scan) \
  294. { \
  295. CELL size = array_capacity(code_to_emit(name)) * code_format; \
  296. if(offset == 0) return scan - 1; \
  297. if(offset < size) return scan + 1; \
  298. offset -= size; \
  299. }
  300. F_FIXNUM quot_code_offset_to_scan(CELL quot, F_FIXNUM offset)
  301. {
  302. CELL code_format = compiled_code_format();
  303. CELL array = untag_quotation(quot)->array;
  304. bool stack_frame = jit_stack_frame_p(untag_object(array));
  305. if(stack_frame)
  306. COUNT(userenv[JIT_PROLOG],0)
  307. CELL i;
  308. CELL length = array_capacity(untag_object(array));
  309. bool tail_call = false;
  310. for(i = 0; i < length; i++)
  311. {
  312. CELL obj = array_nth(untag_object(array),i);
  313. F_WORD *word;
  314. switch(type_of(obj))
  315. {
  316. case WORD_TYPE:
  317. /* Intrinsics */
  318. word = untag_object(obj);
  319. if(word->subprimitive != F)
  320. COUNT(word->subprimitive,i)
  321. else if(i == length - 1)
  322. {
  323. if(stack_frame)
  324. COUNT(userenv[JIT_EPILOG],i);
  325. COUNT(userenv[JIT_WORD_JUMP],i)
  326. tail_call = true;
  327. }
  328. else
  329. COUNT(userenv[JIT_WORD_CALL],i)
  330. break;
  331. case WRAPPER_TYPE:
  332. COUNT(userenv[JIT_PUSH_IMMEDIATE],i)
  333. break;
  334. case FIXNUM_TYPE:
  335. if(jit_primitive_call_p(untag_object(array),i))
  336. {
  337. COUNT(userenv[JIT_SAVE_STACK],i);
  338. COUNT(userenv[JIT_PRIMITIVE],i);
  339. i++;
  340. tail_call = true;
  341. break;
  342. }
  343. case QUOTATION_TYPE:
  344. if(jit_fast_if_p(untag_object(array),i))
  345. {
  346. if(stack_frame)
  347. COUNT(userenv[JIT_EPILOG],i)
  348. COUNT(userenv[JIT_IF_1],i)
  349. COUNT(userenv[JIT_IF_2],i)
  350. i += 2;
  351. tail_call = true;
  352. break;
  353. }
  354. else if(jit_fast_dip_p(untag_object(array),i))
  355. {
  356. COUNT(userenv[JIT_DIP],i)
  357. i++;
  358. break;
  359. }
  360. else if(jit_fast_2dip_p(untag_object(array),i))
  361. {
  362. COUNT(userenv[JIT_2DIP],i)
  363. i++;
  364. break;
  365. }
  366. else if(jit_fast_3dip_p(untag_object(array),i))
  367. {
  368. COUNT(userenv[JIT_3DIP],i)
  369. i++;
  370. break;
  371. }
  372. case ARRAY_TYPE:
  373. if(jit_fast_dispatch_p(untag_object(array),i))
  374. {
  375. if(stack_frame)
  376. COUNT(userenv[JIT_EPILOG],i)
  377. i++;
  378. COUNT(userenv[JIT_DISPATCH],i)
  379. tail_call = true;
  380. break;
  381. }
  382. if(jit_ignore_declare_p(untag_object(array),i))
  383. {
  384. if(offset == 0) return i;
  385. i++;
  386. break;
  387. }
  388. default:
  389. COUNT(userenv[JIT_PUSH_IMMEDIATE],i)
  390. break;
  391. }
  392. }
  393. if(!tail_call)
  394. {
  395. if(stack_frame)
  396. COUNT(userenv[JIT_EPILOG],length)
  397. COUNT(userenv[JIT_RETURN],length)
  398. }
  399. return -1;
  400. }
  401. F_FASTCALL CELL lazy_jit_compile_impl(CELL quot, F_STACK_FRAME *stack)
  402. {
  403. stack_chain->callstack_top = stack;
  404. REGISTER_ROOT(quot);
  405. jit_compile(quot,true);
  406. UNREGISTER_ROOT(quot);
  407. return quot;
  408. }
  409. void primitive_jit_compile(void)
  410. {
  411. jit_compile(dpop(),true);
  412. }
  413. /* push a new quotation on the stack */
  414. void primitive_array_to_quotation(void)
  415. {
  416. F_QUOTATION *quot = allot_object(QUOTATION_TYPE,sizeof(F_QUOTATION));
  417. quot->array = dpeek();
  418. quot->xt = lazy_jit_compile;
  419. quot->compiledp = F;
  420. quot->cached_effect = F;
  421. quot->cache_counter = F;
  422. drepl(tag_object(quot));
  423. }
  424. void primitive_quotation_xt(void)
  425. {
  426. F_QUOTATION *quot = untag_quotation(dpeek());
  427. drepl(allot_cell((CELL)quot->xt));
  428. }
  429. void compile_all_words(void)
  430. {
  431. CELL words = find_all_words();
  432. REGISTER_ROOT(words);
  433. CELL i;
  434. CELL length = array_capacity(untag_object(words));
  435. for(i = 0; i < length; i++)
  436. {
  437. F_WORD *word = untag_word(array_nth(untag_array(words),i));
  438. REGISTER_UNTAGGED(word);
  439. if(word->optimizedp == F)
  440. jit_compile_word(word,word->def,false);
  441. UNREGISTER_UNTAGGED(word);
  442. update_word_xt(word);
  443. }
  444. UNREGISTER_ROOT(words);
  445. iterate_code_heap(relocate_code_block);
  446. }