emitnative.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. // Essentially normal Python has 1 type: Python objects
  27. // Viper has more than 1 type, and is just a more complicated (a superset of) Python.
  28. // If you declare everything in Viper as a Python object (ie omit type decls) then
  29. // it should in principle be exactly the same as Python native.
  30. // Having types means having more opcodes, like binary_op_nat_nat, binary_op_nat_obj etc.
  31. // In practice we won't have a VM but rather do this in asm which is actually very minimal.
  32. // Because it breaks strict Python equivalence it should be a completely separate
  33. // decorator. It breaks equivalence because overflow on integers wraps around.
  34. // It shouldn't break equivalence if you don't use the new types, but since the
  35. // type decls might be used in normal Python for other reasons, it's probably safest,
  36. // cleanest and clearest to make it a separate decorator.
  37. // Actually, it does break equivalence because integers default to native integers,
  38. // not Python objects.
  39. // for x in l[0:8]: can be compiled into a native loop if l has pointer type
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <assert.h>
  43. #include "py/emit.h"
  44. #include "py/bc.h"
  45. #if MICROPY_DEBUG_VERBOSE // print debugging info
  46. #define DEBUG_PRINT (1)
  47. #define DEBUG_printf DEBUG_printf
  48. #else // don't print debugging info
  49. #define DEBUG_printf(...) (void)0
  50. #endif
  51. // wrapper around everything in this file
  52. #if N_X64 || N_X86 || N_THUMB || N_ARM || N_XTENSA
  53. // C stack layout for native functions:
  54. // 0: nlr_buf_t [optional]
  55. // emit->code_state_start: mp_code_state_t
  56. // emit->stack_start: Python object stack | emit->n_state
  57. // locals (reversed, L0 at end) |
  58. //
  59. // C stack layout for native generator functions:
  60. // 0=emit->stack_start: nlr_buf_t
  61. //
  62. // Then REG_GENERATOR_STATE points to:
  63. // 0=emit->code_state_start: mp_code_state_t
  64. // emit->stack_start: Python object stack | emit->n_state
  65. // locals (reversed, L0 at end) |
  66. //
  67. // C stack layout for viper functions:
  68. // 0: nlr_buf_t [optional]
  69. // emit->code_state_start: fun_obj, old_globals [optional]
  70. // emit->stack_start: Python object stack | emit->n_state
  71. // locals (reversed, L0 at end) |
  72. // (L0-L2 may be in regs instead)
  73. // Word index of nlr_buf_t.ret_val
  74. #define NLR_BUF_IDX_RET_VAL (1)
  75. // Whether the viper function needs access to fun_obj
  76. #define NEED_FUN_OBJ(emit) ((emit)->scope->exc_stack_size > 0 \
  77. || ((emit)->scope->scope_flags & (MP_SCOPE_FLAG_REFGLOBALS | MP_SCOPE_FLAG_HASCONSTS)))
  78. // Whether the native/viper function needs to be wrapped in an exception handler
  79. #define NEED_GLOBAL_EXC_HANDLER(emit) ((emit)->scope->exc_stack_size > 0 \
  80. || ((emit)->scope->scope_flags & (MP_SCOPE_FLAG_GENERATOR | MP_SCOPE_FLAG_REFGLOBALS)))
  81. // Whether registers can be used to store locals (only true if there are no
  82. // exception handlers, because otherwise an nlr_jump will restore registers to
  83. // their state at the start of the function and updates to locals will be lost)
  84. #define CAN_USE_REGS_FOR_LOCALS(emit) ((emit)->scope->exc_stack_size == 0 && !(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR))
  85. // Indices within the local C stack for various variables
  86. #define LOCAL_IDX_EXC_VAL(emit) (NLR_BUF_IDX_RET_VAL)
  87. #define LOCAL_IDX_EXC_HANDLER_PC(emit) (NLR_BUF_IDX_LOCAL_1)
  88. #define LOCAL_IDX_EXC_HANDLER_UNWIND(emit) (NLR_BUF_IDX_LOCAL_2)
  89. #define LOCAL_IDX_RET_VAL(emit) (NLR_BUF_IDX_LOCAL_3)
  90. #define LOCAL_IDX_FUN_OBJ(emit) ((emit)->code_state_start + offsetof(mp_code_state_t, fun_bc) / sizeof(uintptr_t))
  91. #define LOCAL_IDX_OLD_GLOBALS(emit) ((emit)->code_state_start + offsetof(mp_code_state_t, ip) / sizeof(uintptr_t))
  92. #define LOCAL_IDX_GEN_PC(emit) ((emit)->code_state_start + offsetof(mp_code_state_t, ip) / sizeof(uintptr_t))
  93. #define LOCAL_IDX_LOCAL_VAR(emit, local_num) ((emit)->stack_start + (emit)->n_state - 1 - (local_num))
  94. #define REG_GENERATOR_STATE (REG_LOCAL_3)
  95. #define EMIT_NATIVE_VIPER_TYPE_ERROR(emit, ...) do { \
  96. *emit->error_slot = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, __VA_ARGS__); \
  97. } while (0)
  98. typedef enum {
  99. STACK_VALUE,
  100. STACK_REG,
  101. STACK_IMM,
  102. } stack_info_kind_t;
  103. // these enums must be distinct and the bottom 4 bits
  104. // must correspond to the correct MP_NATIVE_TYPE_xxx value
  105. typedef enum {
  106. VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
  107. VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
  108. VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
  109. VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
  110. VTYPE_PTR = 0x00 | MP_NATIVE_TYPE_PTR,
  111. VTYPE_PTR8 = 0x00 | MP_NATIVE_TYPE_PTR8,
  112. VTYPE_PTR16 = 0x00 | MP_NATIVE_TYPE_PTR16,
  113. VTYPE_PTR32 = 0x00 | MP_NATIVE_TYPE_PTR32,
  114. VTYPE_PTR_NONE = 0x50 | MP_NATIVE_TYPE_PTR,
  115. VTYPE_UNBOUND = 0x60 | MP_NATIVE_TYPE_OBJ,
  116. VTYPE_BUILTIN_CAST = 0x70 | MP_NATIVE_TYPE_OBJ,
  117. } vtype_kind_t;
  118. int mp_native_type_from_qstr(qstr qst) {
  119. switch (qst) {
  120. case MP_QSTR_object: return MP_NATIVE_TYPE_OBJ;
  121. case MP_QSTR_bool: return MP_NATIVE_TYPE_BOOL;
  122. case MP_QSTR_int: return MP_NATIVE_TYPE_INT;
  123. case MP_QSTR_uint: return MP_NATIVE_TYPE_UINT;
  124. case MP_QSTR_ptr: return MP_NATIVE_TYPE_PTR;
  125. case MP_QSTR_ptr8: return MP_NATIVE_TYPE_PTR8;
  126. case MP_QSTR_ptr16: return MP_NATIVE_TYPE_PTR16;
  127. case MP_QSTR_ptr32: return MP_NATIVE_TYPE_PTR32;
  128. default: return -1;
  129. }
  130. }
  131. STATIC qstr vtype_to_qstr(vtype_kind_t vtype) {
  132. switch (vtype) {
  133. case VTYPE_PYOBJ: return MP_QSTR_object;
  134. case VTYPE_BOOL: return MP_QSTR_bool;
  135. case VTYPE_INT: return MP_QSTR_int;
  136. case VTYPE_UINT: return MP_QSTR_uint;
  137. case VTYPE_PTR: return MP_QSTR_ptr;
  138. case VTYPE_PTR8: return MP_QSTR_ptr8;
  139. case VTYPE_PTR16: return MP_QSTR_ptr16;
  140. case VTYPE_PTR32: return MP_QSTR_ptr32;
  141. case VTYPE_PTR_NONE: default: return MP_QSTR_None;
  142. }
  143. }
  144. typedef struct _stack_info_t {
  145. vtype_kind_t vtype;
  146. stack_info_kind_t kind;
  147. union {
  148. int u_reg;
  149. mp_int_t u_imm;
  150. } data;
  151. } stack_info_t;
  152. #define UNWIND_LABEL_UNUSED (0x7fff)
  153. #define UNWIND_LABEL_DO_FINAL_UNWIND (0x7ffe)
  154. typedef struct _exc_stack_entry_t {
  155. uint16_t label : 15;
  156. uint16_t is_finally : 1;
  157. uint16_t unwind_label : 15;
  158. uint16_t is_active : 1;
  159. } exc_stack_entry_t;
  160. struct _emit_t {
  161. mp_obj_t *error_slot;
  162. uint *label_slot;
  163. uint exit_label;
  164. int pass;
  165. bool do_viper_types;
  166. mp_uint_t local_vtype_alloc;
  167. vtype_kind_t *local_vtype;
  168. mp_uint_t stack_info_alloc;
  169. stack_info_t *stack_info;
  170. vtype_kind_t saved_stack_vtype;
  171. size_t exc_stack_alloc;
  172. size_t exc_stack_size;
  173. exc_stack_entry_t *exc_stack;
  174. int prelude_offset;
  175. int start_offset;
  176. int n_state;
  177. uint16_t code_state_start;
  178. uint16_t stack_start;
  179. int stack_size;
  180. uint16_t const_table_cur_obj;
  181. uint16_t const_table_num_obj;
  182. uint16_t const_table_cur_raw_code;
  183. mp_uint_t *const_table;
  184. bool last_emit_was_return_value;
  185. scope_t *scope;
  186. ASM_T *as;
  187. };
  188. STATIC const uint8_t reg_local_table[REG_LOCAL_NUM] = {REG_LOCAL_1, REG_LOCAL_2, REG_LOCAL_3};
  189. STATIC void emit_native_global_exc_entry(emit_t *emit);
  190. STATIC void emit_native_global_exc_exit(emit_t *emit);
  191. emit_t *EXPORT_FUN(new)(mp_obj_t *error_slot, uint *label_slot, mp_uint_t max_num_labels) {
  192. emit_t *emit = m_new0(emit_t, 1);
  193. emit->error_slot = error_slot;
  194. emit->label_slot = label_slot;
  195. emit->stack_info_alloc = 8;
  196. emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
  197. emit->exc_stack_alloc = 8;
  198. emit->exc_stack = m_new(exc_stack_entry_t, emit->exc_stack_alloc);
  199. emit->as = m_new0(ASM_T, 1);
  200. mp_asm_base_init(&emit->as->base, max_num_labels);
  201. return emit;
  202. }
  203. void EXPORT_FUN(free)(emit_t *emit) {
  204. mp_asm_base_deinit(&emit->as->base, false);
  205. m_del_obj(ASM_T, emit->as);
  206. m_del(exc_stack_entry_t, emit->exc_stack, emit->exc_stack_alloc);
  207. m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
  208. m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
  209. m_del_obj(emit_t, emit);
  210. }
  211. STATIC void emit_call_with_imm_arg(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val, int arg_reg);
  212. STATIC void emit_native_mov_reg_const(emit_t *emit, int reg_dest, int const_val) {
  213. ASM_LOAD_REG_REG_OFFSET(emit->as, reg_dest, REG_FUN_TABLE, const_val);
  214. }
  215. STATIC void emit_native_mov_state_reg(emit_t *emit, int local_num, int reg_src) {
  216. if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
  217. ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, REG_GENERATOR_STATE, local_num);
  218. } else {
  219. ASM_MOV_LOCAL_REG(emit->as, local_num, reg_src);
  220. }
  221. }
  222. STATIC void emit_native_mov_reg_state(emit_t *emit, int reg_dest, int local_num) {
  223. if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
  224. ASM_LOAD_REG_REG_OFFSET(emit->as, reg_dest, REG_GENERATOR_STATE, local_num);
  225. } else {
  226. ASM_MOV_REG_LOCAL(emit->as, reg_dest, local_num);
  227. }
  228. }
  229. STATIC void emit_native_mov_reg_state_addr(emit_t *emit, int reg_dest, int local_num) {
  230. if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
  231. ASM_MOV_REG_IMM(emit->as, reg_dest, local_num * ASM_WORD_SIZE);
  232. ASM_ADD_REG_REG(emit->as, reg_dest, REG_GENERATOR_STATE);
  233. } else {
  234. ASM_MOV_REG_LOCAL_ADDR(emit->as, reg_dest, local_num);
  235. }
  236. }
  237. #define emit_native_mov_state_imm_via(emit, local_num, imm, reg_temp) \
  238. do { \
  239. ASM_MOV_REG_IMM((emit)->as, (reg_temp), (imm)); \
  240. emit_native_mov_state_reg((emit), (local_num), (reg_temp)); \
  241. } while (false)
  242. STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
  243. DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
  244. emit->pass = pass;
  245. emit->do_viper_types = scope->emit_options == MP_EMIT_OPT_VIPER;
  246. emit->stack_size = 0;
  247. emit->const_table_cur_obj = 1; // first entry is for mp_fun_table
  248. emit->const_table_cur_raw_code = 0;
  249. emit->last_emit_was_return_value = false;
  250. emit->scope = scope;
  251. // allocate memory for keeping track of the types of locals
  252. if (emit->local_vtype_alloc < scope->num_locals) {
  253. emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
  254. emit->local_vtype_alloc = scope->num_locals;
  255. }
  256. // set default type for arguments
  257. mp_uint_t num_args = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
  258. if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
  259. num_args += 1;
  260. }
  261. if (scope->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) {
  262. num_args += 1;
  263. }
  264. for (mp_uint_t i = 0; i < num_args; i++) {
  265. emit->local_vtype[i] = VTYPE_PYOBJ;
  266. }
  267. // Set viper type for arguments
  268. if (emit->do_viper_types) {
  269. for (int i = 0; i < emit->scope->id_info_len; ++i) {
  270. id_info_t *id = &emit->scope->id_info[i];
  271. if (id->flags & ID_FLAG_IS_PARAM) {
  272. assert(id->local_num < emit->local_vtype_alloc);
  273. emit->local_vtype[id->local_num] = id->flags >> ID_FLAG_VIPER_TYPE_POS;
  274. }
  275. }
  276. }
  277. // local variables begin unbound, and have unknown type
  278. for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
  279. emit->local_vtype[i] = VTYPE_UNBOUND;
  280. }
  281. // values on stack begin unbound
  282. for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
  283. emit->stack_info[i].kind = STACK_VALUE;
  284. emit->stack_info[i].vtype = VTYPE_UNBOUND;
  285. }
  286. mp_asm_base_start_pass(&emit->as->base, pass == MP_PASS_EMIT ? MP_ASM_PASS_EMIT : MP_ASM_PASS_COMPUTE);
  287. // generate code for entry to function
  288. // Work out start of code state (mp_code_state_t or reduced version for viper)
  289. emit->code_state_start = 0;
  290. if (NEED_GLOBAL_EXC_HANDLER(emit)) {
  291. emit->code_state_start = sizeof(nlr_buf_t) / sizeof(uintptr_t);
  292. }
  293. if (emit->do_viper_types) {
  294. // Work out size of state (locals plus stack)
  295. // n_state counts all stack and locals, even those in registers
  296. emit->n_state = scope->num_locals + scope->stack_size;
  297. int num_locals_in_regs = 0;
  298. if (CAN_USE_REGS_FOR_LOCALS(emit)) {
  299. num_locals_in_regs = scope->num_locals;
  300. if (num_locals_in_regs > REG_LOCAL_NUM) {
  301. num_locals_in_regs = REG_LOCAL_NUM;
  302. }
  303. // Need a spot for REG_LOCAL_3 if 4 or more args (see below)
  304. if (scope->num_pos_args >= 4) {
  305. --num_locals_in_regs;
  306. }
  307. }
  308. // Work out where the locals and Python stack start within the C stack
  309. if (NEED_GLOBAL_EXC_HANDLER(emit)) {
  310. // Reserve 2 words for function object and old globals
  311. emit->stack_start = emit->code_state_start + 2;
  312. } else if (scope->scope_flags & MP_SCOPE_FLAG_HASCONSTS) {
  313. // Reserve 1 word for function object, to access const table
  314. emit->stack_start = emit->code_state_start + 1;
  315. } else {
  316. emit->stack_start = emit->code_state_start + 0;
  317. }
  318. // Entry to function
  319. ASM_ENTRY(emit->as, emit->stack_start + emit->n_state - num_locals_in_regs);
  320. #if N_X86
  321. asm_x86_mov_arg_to_r32(emit->as, 0, REG_ARG_1);
  322. #endif
  323. // Load REG_FUN_TABLE with a pointer to mp_fun_table, found in the const_table
  324. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_LOCAL_3, REG_ARG_1, offsetof(mp_obj_fun_bc_t, const_table) / sizeof(uintptr_t));
  325. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_LOCAL_3, 0);
  326. // Store function object (passed as first arg) to stack if needed
  327. if (NEED_FUN_OBJ(emit)) {
  328. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_FUN_OBJ(emit), REG_ARG_1);
  329. }
  330. // Put n_args in REG_ARG_1, n_kw in REG_ARG_2, args array in REG_LOCAL_3
  331. #if N_X86
  332. asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_1);
  333. asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_2);
  334. asm_x86_mov_arg_to_r32(emit->as, 3, REG_LOCAL_3);
  335. #else
  336. ASM_MOV_REG_REG(emit->as, REG_ARG_1, REG_ARG_2);
  337. ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_ARG_3);
  338. ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_4);
  339. #endif
  340. // Check number of args matches this function, and call mp_arg_check_num_sig if not
  341. ASM_JUMP_IF_REG_NONZERO(emit->as, REG_ARG_2, *emit->label_slot + 4, true);
  342. ASM_MOV_REG_IMM(emit->as, REG_ARG_3, scope->num_pos_args);
  343. ASM_JUMP_IF_REG_EQ(emit->as, REG_ARG_1, REG_ARG_3, *emit->label_slot + 5);
  344. mp_asm_base_label_assign(&emit->as->base, *emit->label_slot + 4);
  345. ASM_MOV_REG_IMM(emit->as, REG_ARG_3, MP_OBJ_FUN_MAKE_SIG(scope->num_pos_args, scope->num_pos_args, false));
  346. ASM_CALL_IND(emit->as, MP_F_ARG_CHECK_NUM_SIG);
  347. mp_asm_base_label_assign(&emit->as->base, *emit->label_slot + 5);
  348. // Store arguments into locals (reg or stack), converting to native if needed
  349. for (int i = 0; i < emit->scope->num_pos_args; i++) {
  350. int r = REG_ARG_1;
  351. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_1, REG_LOCAL_3, i);
  352. if (emit->local_vtype[i] != VTYPE_PYOBJ) {
  353. emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, emit->local_vtype[i], REG_ARG_2);
  354. r = REG_RET;
  355. }
  356. // REG_LOCAL_3 points to the args array so be sure not to overwrite it if it's still needed
  357. if (i < REG_LOCAL_NUM && CAN_USE_REGS_FOR_LOCALS(emit) && (i != 2 || emit->scope->num_pos_args == 3)) {
  358. ASM_MOV_REG_REG(emit->as, reg_local_table[i], r);
  359. } else {
  360. emit_native_mov_state_reg(emit, LOCAL_IDX_LOCAL_VAR(emit, i), r);
  361. }
  362. }
  363. // Get 3rd local from the stack back into REG_LOCAL_3 if this reg couldn't be written to above
  364. if (emit->scope->num_pos_args >= 4 && CAN_USE_REGS_FOR_LOCALS(emit)) {
  365. ASM_MOV_REG_LOCAL(emit->as, REG_LOCAL_3, LOCAL_IDX_LOCAL_VAR(emit, 2));
  366. }
  367. emit_native_global_exc_entry(emit);
  368. } else {
  369. // work out size of state (locals plus stack)
  370. emit->n_state = scope->num_locals + scope->stack_size;
  371. if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
  372. emit->code_state_start = 0;
  373. emit->stack_start = sizeof(mp_code_state_t) / sizeof(mp_uint_t);
  374. mp_asm_base_data(&emit->as->base, ASM_WORD_SIZE, (uintptr_t)emit->prelude_offset);
  375. mp_asm_base_data(&emit->as->base, ASM_WORD_SIZE, (uintptr_t)emit->start_offset);
  376. ASM_ENTRY(emit->as, sizeof(nlr_buf_t) / sizeof(uintptr_t));
  377. // Put address of code_state into REG_GENERATOR_STATE
  378. #if N_X86
  379. asm_x86_mov_arg_to_r32(emit->as, 0, REG_GENERATOR_STATE);
  380. #else
  381. ASM_MOV_REG_REG(emit->as, REG_GENERATOR_STATE, REG_ARG_1);
  382. #endif
  383. // Put throw value into LOCAL_IDX_EXC_VAL slot, for yield/yield-from
  384. #if N_X86
  385. asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_2);
  386. #endif
  387. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_ARG_2);
  388. // Load REG_FUN_TABLE with a pointer to mp_fun_table, found in the const_table
  389. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_GENERATOR_STATE, LOCAL_IDX_FUN_OBJ(emit));
  390. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_TEMP0, offsetof(mp_obj_fun_bc_t, const_table) / sizeof(uintptr_t));
  391. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_TEMP0, emit->scope->num_pos_args + emit->scope->num_kwonly_args);
  392. } else {
  393. // The locals and stack start after the code_state structure
  394. emit->stack_start = emit->code_state_start + sizeof(mp_code_state_t) / sizeof(mp_uint_t);
  395. // Allocate space on C-stack for code_state structure, which includes state
  396. ASM_ENTRY(emit->as, emit->stack_start + emit->n_state);
  397. // Prepare incoming arguments for call to mp_setup_code_state
  398. #if N_X86
  399. asm_x86_mov_arg_to_r32(emit->as, 0, REG_ARG_1);
  400. asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_2);
  401. asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_3);
  402. asm_x86_mov_arg_to_r32(emit->as, 3, REG_ARG_4);
  403. #endif
  404. // Load REG_FUN_TABLE with a pointer to mp_fun_table, found in the const_table
  405. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_LOCAL_3, REG_ARG_1, offsetof(mp_obj_fun_bc_t, const_table) / sizeof(uintptr_t));
  406. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_LOCAL_3, emit->scope->num_pos_args + emit->scope->num_kwonly_args);
  407. // Set code_state.fun_bc
  408. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_FUN_OBJ(emit), REG_ARG_1);
  409. // Set code_state.ip (offset from start of this function to prelude info)
  410. // TODO this encoding may change size in the final pass, need to make it fixed
  411. emit_native_mov_state_imm_via(emit, emit->code_state_start + offsetof(mp_code_state_t, ip) / sizeof(uintptr_t), emit->prelude_offset, REG_ARG_1);
  412. // Put address of code_state into first arg
  413. ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, emit->code_state_start);
  414. // Call mp_setup_code_state to prepare code_state structure
  415. #if N_THUMB
  416. asm_thumb_bl_ind(emit->as, MP_F_SETUP_CODE_STATE, ASM_THUMB_REG_R4);
  417. #elif N_ARM
  418. asm_arm_bl_ind(emit->as, MP_F_SETUP_CODE_STATE, ASM_ARM_REG_R4);
  419. #else
  420. ASM_CALL_IND(emit->as, MP_F_SETUP_CODE_STATE);
  421. #endif
  422. }
  423. emit_native_global_exc_entry(emit);
  424. // cache some locals in registers, but only if no exception handlers
  425. if (CAN_USE_REGS_FOR_LOCALS(emit)) {
  426. for (int i = 0; i < REG_LOCAL_NUM && i < scope->num_locals; ++i) {
  427. ASM_MOV_REG_LOCAL(emit->as, reg_local_table[i], LOCAL_IDX_LOCAL_VAR(emit, i));
  428. }
  429. }
  430. // set the type of closed over variables
  431. for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
  432. id_info_t *id = &scope->id_info[i];
  433. if (id->kind == ID_INFO_KIND_CELL) {
  434. emit->local_vtype[id->local_num] = VTYPE_PYOBJ;
  435. }
  436. }
  437. if (pass == MP_PASS_EMIT) {
  438. // write argument names as qstr objects
  439. // see comment in corresponding part of emitbc.c about the logic here
  440. for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
  441. qstr qst = MP_QSTR__star_;
  442. for (int j = 0; j < scope->id_info_len; ++j) {
  443. id_info_t *id = &scope->id_info[j];
  444. if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
  445. qst = id->qst;
  446. break;
  447. }
  448. }
  449. emit->const_table[i] = (mp_uint_t)MP_OBJ_NEW_QSTR(qst);
  450. }
  451. }
  452. }
  453. }
  454. STATIC void emit_native_end_pass(emit_t *emit) {
  455. emit_native_global_exc_exit(emit);
  456. if (!emit->do_viper_types) {
  457. emit->prelude_offset = mp_asm_base_get_code_pos(&emit->as->base);
  458. mp_asm_base_data(&emit->as->base, 1, 0x80 | ((emit->n_state >> 7) & 0x7f));
  459. mp_asm_base_data(&emit->as->base, 1, emit->n_state & 0x7f);
  460. mp_asm_base_data(&emit->as->base, 1, 0); // n_exc_stack
  461. mp_asm_base_data(&emit->as->base, 1, emit->scope->scope_flags);
  462. mp_asm_base_data(&emit->as->base, 1, emit->scope->num_pos_args);
  463. mp_asm_base_data(&emit->as->base, 1, emit->scope->num_kwonly_args);
  464. mp_asm_base_data(&emit->as->base, 1, emit->scope->num_def_pos_args);
  465. // write code info
  466. #if MICROPY_PERSISTENT_CODE
  467. mp_asm_base_data(&emit->as->base, 1, 5);
  468. mp_asm_base_data(&emit->as->base, 1, emit->scope->simple_name);
  469. mp_asm_base_data(&emit->as->base, 1, emit->scope->simple_name >> 8);
  470. mp_asm_base_data(&emit->as->base, 1, emit->scope->source_file);
  471. mp_asm_base_data(&emit->as->base, 1, emit->scope->source_file >> 8);
  472. #else
  473. mp_asm_base_data(&emit->as->base, 1, 1);
  474. #endif
  475. // bytecode prelude: initialise closed over variables
  476. for (int i = 0; i < emit->scope->id_info_len; i++) {
  477. id_info_t *id = &emit->scope->id_info[i];
  478. if (id->kind == ID_INFO_KIND_CELL) {
  479. assert(id->local_num < 255);
  480. mp_asm_base_data(&emit->as->base, 1, id->local_num); // write the local which should be converted to a cell
  481. }
  482. }
  483. mp_asm_base_data(&emit->as->base, 1, 255); // end of list sentinel
  484. }
  485. ASM_END_PASS(emit->as);
  486. // check stack is back to zero size
  487. assert(emit->stack_size == 0);
  488. assert(emit->exc_stack_size == 0);
  489. // Deal with const table accounting
  490. assert(emit->pass <= MP_PASS_STACK_SIZE || (emit->const_table_num_obj == emit->const_table_cur_obj));
  491. emit->const_table_num_obj = emit->const_table_cur_obj;
  492. if (emit->pass == MP_PASS_CODE_SIZE) {
  493. size_t const_table_alloc = emit->const_table_num_obj + emit->const_table_cur_raw_code;
  494. size_t nqstr = 0;
  495. if (!emit->do_viper_types) {
  496. // Add room for qstr names of arguments
  497. nqstr = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
  498. const_table_alloc += nqstr;
  499. }
  500. emit->const_table = m_new(mp_uint_t, const_table_alloc);
  501. // Store mp_fun_table pointer just after qstrs
  502. emit->const_table[nqstr] = (mp_uint_t)(uintptr_t)mp_fun_table;
  503. }
  504. if (emit->pass == MP_PASS_EMIT) {
  505. void *f = mp_asm_base_get_code(&emit->as->base);
  506. mp_uint_t f_len = mp_asm_base_get_code_size(&emit->as->base);
  507. mp_emit_glue_assign_native(emit->scope->raw_code,
  508. emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
  509. f, f_len, emit->const_table,
  510. emit->scope->num_pos_args, emit->scope->scope_flags, 0);
  511. }
  512. }
  513. STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
  514. return emit->last_emit_was_return_value;
  515. }
  516. STATIC void ensure_extra_stack(emit_t *emit, size_t delta) {
  517. if (emit->stack_size + delta > emit->stack_info_alloc) {
  518. size_t new_alloc = (emit->stack_size + delta + 8) & ~3;
  519. emit->stack_info = m_renew(stack_info_t, emit->stack_info, emit->stack_info_alloc, new_alloc);
  520. emit->stack_info_alloc = new_alloc;
  521. }
  522. }
  523. STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
  524. assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
  525. assert((mp_int_t)emit->stack_size + stack_size_delta <= (mp_int_t)emit->stack_info_alloc);
  526. emit->stack_size += stack_size_delta;
  527. if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
  528. emit->scope->stack_size = emit->stack_size;
  529. }
  530. #ifdef DEBUG_PRINT
  531. DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
  532. for (int i = 0; i < emit->stack_size; i++) {
  533. stack_info_t *si = &emit->stack_info[i];
  534. DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
  535. }
  536. DEBUG_printf("\n");
  537. #endif
  538. }
  539. STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
  540. DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
  541. if (delta > 0) {
  542. ensure_extra_stack(emit, delta);
  543. }
  544. // If we are adjusting the stack in a positive direction (pushing) then we
  545. // need to fill in values for the stack kind and vtype of the newly-pushed
  546. // entries. These should be set to "value" (ie not reg or imm) because we
  547. // should only need to adjust the stack due to a jump to this part in the
  548. // code (and hence we have settled the stack before the jump).
  549. for (mp_int_t i = 0; i < delta; i++) {
  550. stack_info_t *si = &emit->stack_info[emit->stack_size + i];
  551. si->kind = STACK_VALUE;
  552. // TODO we don't know the vtype to use here. At the moment this is a
  553. // hack to get the case of multi comparison working.
  554. if (delta == 1) {
  555. si->vtype = emit->saved_stack_vtype;
  556. } else {
  557. si->vtype = VTYPE_PYOBJ;
  558. }
  559. }
  560. adjust_stack(emit, delta);
  561. }
  562. STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
  563. (void)emit;
  564. (void)source_line;
  565. }
  566. // this must be called at start of emit functions
  567. STATIC void emit_native_pre(emit_t *emit) {
  568. emit->last_emit_was_return_value = false;
  569. }
  570. // depth==0 is top, depth==1 is before top, etc
  571. STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
  572. return &emit->stack_info[emit->stack_size - 1 - depth];
  573. }
  574. // depth==0 is top, depth==1 is before top, etc
  575. STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
  576. if (emit->do_viper_types) {
  577. return peek_stack(emit, depth)->vtype;
  578. } else {
  579. // Type is always PYOBJ even if the intermediate stored value is not
  580. return VTYPE_PYOBJ;
  581. }
  582. }
  583. // pos=1 is TOS, pos=2 is next, etc
  584. // use pos=0 for no skipping
  585. STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
  586. skip_stack_pos = emit->stack_size - skip_stack_pos;
  587. for (int i = 0; i < emit->stack_size; i++) {
  588. if (i != skip_stack_pos) {
  589. stack_info_t *si = &emit->stack_info[i];
  590. if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
  591. si->kind = STACK_VALUE;
  592. emit_native_mov_state_reg(emit, emit->stack_start + i, si->data.u_reg);
  593. }
  594. }
  595. }
  596. }
  597. STATIC void need_reg_all(emit_t *emit) {
  598. for (int i = 0; i < emit->stack_size; i++) {
  599. stack_info_t *si = &emit->stack_info[i];
  600. if (si->kind == STACK_REG) {
  601. si->kind = STACK_VALUE;
  602. emit_native_mov_state_reg(emit, emit->stack_start + i, si->data.u_reg);
  603. }
  604. }
  605. }
  606. STATIC vtype_kind_t load_reg_stack_imm(emit_t *emit, int reg_dest, const stack_info_t *si, bool convert_to_pyobj) {
  607. if (!convert_to_pyobj && emit->do_viper_types) {
  608. ASM_MOV_REG_IMM(emit->as, reg_dest, si->data.u_imm);
  609. return si->vtype;
  610. } else {
  611. if (si->vtype == VTYPE_PYOBJ) {
  612. ASM_MOV_REG_IMM(emit->as, reg_dest, si->data.u_imm);
  613. } else if (si->vtype == VTYPE_BOOL) {
  614. emit_native_mov_reg_const(emit, reg_dest, MP_F_CONST_FALSE_OBJ + si->data.u_imm);
  615. } else if (si->vtype == VTYPE_INT || si->vtype == VTYPE_UINT) {
  616. ASM_MOV_REG_IMM(emit->as, reg_dest, (uintptr_t)MP_OBJ_NEW_SMALL_INT(si->data.u_imm));
  617. } else if (si->vtype == VTYPE_PTR_NONE) {
  618. emit_native_mov_reg_const(emit, reg_dest, MP_F_CONST_NONE_OBJ);
  619. } else {
  620. mp_raise_NotImplementedError("conversion to object");
  621. }
  622. return VTYPE_PYOBJ;
  623. }
  624. }
  625. STATIC void need_stack_settled(emit_t *emit) {
  626. DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
  627. for (int i = 0; i < emit->stack_size; i++) {
  628. stack_info_t *si = &emit->stack_info[i];
  629. if (si->kind == STACK_REG) {
  630. DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
  631. si->kind = STACK_VALUE;
  632. emit_native_mov_state_reg(emit, emit->stack_start + i, si->data.u_reg);
  633. }
  634. }
  635. for (int i = 0; i < emit->stack_size; i++) {
  636. stack_info_t *si = &emit->stack_info[i];
  637. if (si->kind == STACK_IMM) {
  638. DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
  639. si->kind = STACK_VALUE;
  640. si->vtype = load_reg_stack_imm(emit, REG_TEMP0, si, false);
  641. emit_native_mov_state_reg(emit, emit->stack_start + i, REG_TEMP0);
  642. }
  643. }
  644. }
  645. // pos=1 is TOS, pos=2 is next, etc
  646. STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
  647. need_reg_single(emit, reg_dest, pos);
  648. stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
  649. *vtype = si->vtype;
  650. switch (si->kind) {
  651. case STACK_VALUE:
  652. emit_native_mov_reg_state(emit, reg_dest, emit->stack_start + emit->stack_size - pos);
  653. break;
  654. case STACK_REG:
  655. if (si->data.u_reg != reg_dest) {
  656. ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
  657. }
  658. break;
  659. case STACK_IMM:
  660. *vtype = load_reg_stack_imm(emit, reg_dest, si, false);
  661. break;
  662. }
  663. }
  664. // does an efficient X=pop(); discard(); push(X)
  665. // needs a (non-temp) register in case the poped element was stored in the stack
  666. STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
  667. stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
  668. si[0] = si[1];
  669. if (si->kind == STACK_VALUE) {
  670. // if folded element was on the stack we need to put it in a register
  671. emit_native_mov_reg_state(emit, reg_dest, emit->stack_start + emit->stack_size - 1);
  672. si->kind = STACK_REG;
  673. si->data.u_reg = reg_dest;
  674. }
  675. adjust_stack(emit, -1);
  676. }
  677. // If stacked value is in a register and the register is not r1 or r2, then
  678. // *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
  679. STATIC void emit_pre_pop_reg_flexible(emit_t *emit, vtype_kind_t *vtype, int *reg_dest, int not_r1, int not_r2) {
  680. emit->last_emit_was_return_value = false;
  681. stack_info_t *si = peek_stack(emit, 0);
  682. if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
  683. *vtype = si->vtype;
  684. *reg_dest = si->data.u_reg;
  685. need_reg_single(emit, *reg_dest, 1);
  686. } else {
  687. emit_access_stack(emit, 1, vtype, *reg_dest);
  688. }
  689. adjust_stack(emit, -1);
  690. }
  691. STATIC void emit_pre_pop_discard(emit_t *emit) {
  692. emit->last_emit_was_return_value = false;
  693. adjust_stack(emit, -1);
  694. }
  695. STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
  696. emit->last_emit_was_return_value = false;
  697. emit_access_stack(emit, 1, vtype, reg_dest);
  698. adjust_stack(emit, -1);
  699. }
  700. STATIC void emit_pre_pop_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb) {
  701. emit_pre_pop_reg(emit, vtypea, rega);
  702. emit_pre_pop_reg(emit, vtypeb, regb);
  703. }
  704. STATIC void emit_pre_pop_reg_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb, vtype_kind_t *vtypec, int regc) {
  705. emit_pre_pop_reg(emit, vtypea, rega);
  706. emit_pre_pop_reg(emit, vtypeb, regb);
  707. emit_pre_pop_reg(emit, vtypec, regc);
  708. }
  709. STATIC void emit_post(emit_t *emit) {
  710. (void)emit;
  711. }
  712. STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
  713. stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
  714. si->vtype = new_vtype;
  715. }
  716. STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
  717. ensure_extra_stack(emit, 1);
  718. stack_info_t *si = &emit->stack_info[emit->stack_size];
  719. si->vtype = vtype;
  720. si->kind = STACK_REG;
  721. si->data.u_reg = reg;
  722. adjust_stack(emit, 1);
  723. }
  724. STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
  725. ensure_extra_stack(emit, 1);
  726. stack_info_t *si = &emit->stack_info[emit->stack_size];
  727. si->vtype = vtype;
  728. si->kind = STACK_IMM;
  729. si->data.u_imm = imm;
  730. adjust_stack(emit, 1);
  731. }
  732. STATIC void emit_post_push_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb) {
  733. emit_post_push_reg(emit, vtypea, rega);
  734. emit_post_push_reg(emit, vtypeb, regb);
  735. }
  736. STATIC void emit_post_push_reg_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb, vtype_kind_t vtypec, int regc) {
  737. emit_post_push_reg(emit, vtypea, rega);
  738. emit_post_push_reg(emit, vtypeb, regb);
  739. emit_post_push_reg(emit, vtypec, regc);
  740. }
  741. STATIC void emit_post_push_reg_reg_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb, vtype_kind_t vtypec, int regc, vtype_kind_t vtyped, int regd) {
  742. emit_post_push_reg(emit, vtypea, rega);
  743. emit_post_push_reg(emit, vtypeb, regb);
  744. emit_post_push_reg(emit, vtypec, regc);
  745. emit_post_push_reg(emit, vtyped, regd);
  746. }
  747. STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
  748. need_reg_all(emit);
  749. ASM_CALL_IND(emit->as, fun_kind);
  750. }
  751. STATIC void emit_call_with_imm_arg(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val, int arg_reg) {
  752. need_reg_all(emit);
  753. ASM_MOV_REG_IMM(emit->as, arg_reg, arg_val);
  754. ASM_CALL_IND(emit->as, fun_kind);
  755. }
  756. STATIC void emit_call_with_2_imm_args(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val1, int arg_reg1, mp_int_t arg_val2, int arg_reg2) {
  757. need_reg_all(emit);
  758. ASM_MOV_REG_IMM(emit->as, arg_reg1, arg_val1);
  759. ASM_MOV_REG_IMM(emit->as, arg_reg2, arg_val2);
  760. ASM_CALL_IND(emit->as, fun_kind);
  761. }
  762. // vtype of all n_pop objects is VTYPE_PYOBJ
  763. // Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
  764. // If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
  765. // Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
  766. STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
  767. need_reg_all(emit);
  768. // First, store any immediate values to their respective place on the stack.
  769. for (mp_uint_t i = 0; i < n_pop; i++) {
  770. stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
  771. // must push any imm's to stack
  772. // must convert them to VTYPE_PYOBJ for viper code
  773. if (si->kind == STACK_IMM) {
  774. si->kind = STACK_VALUE;
  775. si->vtype = load_reg_stack_imm(emit, reg_dest, si, true);
  776. emit_native_mov_state_reg(emit, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
  777. }
  778. // verify that this value is on the stack
  779. assert(si->kind == STACK_VALUE);
  780. }
  781. // Second, convert any non-VTYPE_PYOBJ to that type.
  782. for (mp_uint_t i = 0; i < n_pop; i++) {
  783. stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
  784. if (si->vtype != VTYPE_PYOBJ) {
  785. mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
  786. emit_native_mov_reg_state(emit, REG_ARG_1, local_num);
  787. emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, si->vtype, REG_ARG_2); // arg2 = type
  788. emit_native_mov_state_reg(emit, local_num, REG_RET);
  789. si->vtype = VTYPE_PYOBJ;
  790. DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
  791. }
  792. }
  793. // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
  794. adjust_stack(emit, -n_pop);
  795. emit_native_mov_reg_state_addr(emit, reg_dest, emit->stack_start + emit->stack_size);
  796. }
  797. // vtype of all n_push objects is VTYPE_PYOBJ
  798. STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
  799. need_reg_all(emit);
  800. ensure_extra_stack(emit, n_push);
  801. for (mp_uint_t i = 0; i < n_push; i++) {
  802. emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
  803. emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
  804. }
  805. emit_native_mov_reg_state_addr(emit, reg_dest, emit->stack_start + emit->stack_size);
  806. adjust_stack(emit, n_push);
  807. }
  808. STATIC void emit_native_push_exc_stack(emit_t *emit, uint label, bool is_finally) {
  809. if (emit->exc_stack_size + 1 > emit->exc_stack_alloc) {
  810. size_t new_alloc = emit->exc_stack_alloc + 4;
  811. emit->exc_stack = m_renew(exc_stack_entry_t, emit->exc_stack, emit->exc_stack_alloc, new_alloc);
  812. emit->exc_stack_alloc = new_alloc;
  813. }
  814. exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size++];
  815. e->label = label;
  816. e->is_finally = is_finally;
  817. e->unwind_label = UNWIND_LABEL_UNUSED;
  818. e->is_active = true;
  819. ASM_MOV_REG_PCREL(emit->as, REG_RET, label);
  820. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_RET);
  821. }
  822. STATIC void emit_native_leave_exc_stack(emit_t *emit, bool start_of_handler) {
  823. assert(emit->exc_stack_size > 0);
  824. // Get current exception handler and deactivate it
  825. exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
  826. e->is_active = false;
  827. // Find next innermost active exception handler, to restore as current handler
  828. for (--e; e >= emit->exc_stack && !e->is_active; --e) {
  829. }
  830. // Update the PC of the new exception handler
  831. if (e < emit->exc_stack) {
  832. // No active handler, clear handler PC to zero
  833. if (start_of_handler) {
  834. // Optimisation: PC is already cleared by global exc handler
  835. return;
  836. }
  837. ASM_XOR_REG_REG(emit->as, REG_RET, REG_RET);
  838. } else {
  839. // Found new active handler, get its PC
  840. ASM_MOV_REG_PCREL(emit->as, REG_RET, e->label);
  841. }
  842. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_RET);
  843. }
  844. STATIC exc_stack_entry_t *emit_native_pop_exc_stack(emit_t *emit) {
  845. assert(emit->exc_stack_size > 0);
  846. exc_stack_entry_t *e = &emit->exc_stack[--emit->exc_stack_size];
  847. assert(e->is_active == false);
  848. return e;
  849. }
  850. STATIC void emit_load_reg_with_ptr(emit_t *emit, int reg, mp_uint_t ptr, size_t table_off) {
  851. if (!emit->do_viper_types) {
  852. // Skip qstr names of arguments
  853. table_off += emit->scope->num_pos_args + emit->scope->num_kwonly_args;
  854. }
  855. if (emit->pass == MP_PASS_EMIT) {
  856. emit->const_table[table_off] = ptr;
  857. }
  858. emit_native_mov_reg_state(emit, REG_TEMP0, LOCAL_IDX_FUN_OBJ(emit));
  859. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_TEMP0, offsetof(mp_obj_fun_bc_t, const_table) / sizeof(uintptr_t));
  860. ASM_LOAD_REG_REG_OFFSET(emit->as, reg, REG_TEMP0, table_off);
  861. }
  862. STATIC void emit_load_reg_with_object(emit_t *emit, int reg, mp_obj_t obj) {
  863. size_t table_off = emit->const_table_cur_obj++;
  864. emit_load_reg_with_ptr(emit, reg, (mp_uint_t)obj, table_off);
  865. }
  866. STATIC void emit_load_reg_with_raw_code(emit_t *emit, int reg, mp_raw_code_t *rc) {
  867. size_t table_off = emit->const_table_num_obj + emit->const_table_cur_raw_code++;
  868. emit_load_reg_with_ptr(emit, reg, (mp_uint_t)rc, table_off);
  869. }
  870. STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
  871. DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
  872. bool is_finally = false;
  873. if (emit->exc_stack_size > 0) {
  874. exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
  875. is_finally = e->is_finally && e->label == l;
  876. }
  877. if (is_finally) {
  878. // Label is at start of finally handler: store TOS into exception slot
  879. vtype_kind_t vtype;
  880. emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
  881. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_TEMP0);
  882. }
  883. emit_native_pre(emit);
  884. // need to commit stack because we can jump here from elsewhere
  885. need_stack_settled(emit);
  886. mp_asm_base_label_assign(&emit->as->base, l);
  887. emit_post(emit);
  888. if (is_finally) {
  889. // Label is at start of finally handler: pop exception stack
  890. emit_native_leave_exc_stack(emit, false);
  891. }
  892. }
  893. STATIC void emit_native_global_exc_entry(emit_t *emit) {
  894. // Note: 4 labels are reserved for this function, starting at *emit->label_slot
  895. emit->exit_label = *emit->label_slot;
  896. if (NEED_GLOBAL_EXC_HANDLER(emit)) {
  897. mp_uint_t nlr_label = *emit->label_slot + 1;
  898. mp_uint_t start_label = *emit->label_slot + 2;
  899. mp_uint_t global_except_label = *emit->label_slot + 3;
  900. if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
  901. // Set new globals
  902. emit_native_mov_reg_state(emit, REG_ARG_1, LOCAL_IDX_FUN_OBJ(emit));
  903. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_1, REG_ARG_1, offsetof(mp_obj_fun_bc_t, globals) / sizeof(uintptr_t));
  904. emit_call(emit, MP_F_NATIVE_SWAP_GLOBALS);
  905. // Save old globals (or NULL if globals didn't change)
  906. emit_native_mov_state_reg(emit, LOCAL_IDX_OLD_GLOBALS(emit), REG_RET);
  907. }
  908. if (emit->scope->exc_stack_size == 0) {
  909. if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
  910. // Optimisation: if globals didn't change don't push the nlr context
  911. ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, start_label, false);
  912. }
  913. // Wrap everything in an nlr context
  914. ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 0);
  915. emit_call(emit, MP_F_NLR_PUSH);
  916. ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, start_label, true);
  917. } else {
  918. // Clear the unwind state
  919. ASM_XOR_REG_REG(emit->as, REG_TEMP0, REG_TEMP0);
  920. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_UNWIND(emit), REG_TEMP0);
  921. // Put PC of start code block into REG_LOCAL_1
  922. ASM_MOV_REG_PCREL(emit->as, REG_LOCAL_1, start_label);
  923. // Wrap everything in an nlr context
  924. emit_native_label_assign(emit, nlr_label);
  925. ASM_MOV_REG_LOCAL(emit->as, REG_LOCAL_2, LOCAL_IDX_EXC_HANDLER_UNWIND(emit));
  926. ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 0);
  927. emit_call(emit, MP_F_NLR_PUSH);
  928. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_UNWIND(emit), REG_LOCAL_2);
  929. ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, global_except_label, true);
  930. // Clear PC of current code block, and jump there to resume execution
  931. ASM_XOR_REG_REG(emit->as, REG_TEMP0, REG_TEMP0);
  932. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_TEMP0);
  933. ASM_JUMP_REG(emit->as, REG_LOCAL_1);
  934. // Global exception handler: check for valid exception handler
  935. emit_native_label_assign(emit, global_except_label);
  936. ASM_MOV_REG_LOCAL(emit->as, REG_LOCAL_1, LOCAL_IDX_EXC_HANDLER_PC(emit));
  937. ASM_JUMP_IF_REG_NONZERO(emit->as, REG_LOCAL_1, nlr_label, false);
  938. }
  939. if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
  940. // Restore old globals
  941. emit_native_mov_reg_state(emit, REG_ARG_1, LOCAL_IDX_OLD_GLOBALS(emit));
  942. emit_call(emit, MP_F_NATIVE_SWAP_GLOBALS);
  943. }
  944. if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
  945. // Store return value in state[0]
  946. ASM_MOV_REG_LOCAL(emit->as, REG_TEMP0, LOCAL_IDX_EXC_VAL(emit));
  947. ASM_STORE_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_GENERATOR_STATE, offsetof(mp_code_state_t, state) / sizeof(uintptr_t));
  948. // Load return kind
  949. ASM_MOV_REG_IMM(emit->as, REG_RET, MP_VM_RETURN_EXCEPTION);
  950. ASM_EXIT(emit->as);
  951. } else {
  952. // Re-raise exception out to caller
  953. ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit));
  954. emit_call(emit, MP_F_NATIVE_RAISE);
  955. }
  956. // Label for start of function
  957. emit_native_label_assign(emit, start_label);
  958. if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
  959. emit_native_mov_reg_state(emit, REG_TEMP0, LOCAL_IDX_GEN_PC(emit));
  960. ASM_JUMP_REG(emit->as, REG_TEMP0);
  961. emit->start_offset = mp_asm_base_get_code_pos(&emit->as->base);
  962. // This is the first entry of the generator
  963. // Check LOCAL_IDX_EXC_VAL for any injected value
  964. ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit));
  965. emit_call(emit, MP_F_NATIVE_RAISE);
  966. }
  967. }
  968. }
  969. STATIC void emit_native_global_exc_exit(emit_t *emit) {
  970. // Label for end of function
  971. emit_native_label_assign(emit, emit->exit_label);
  972. if (NEED_GLOBAL_EXC_HANDLER(emit)) {
  973. // Get old globals
  974. if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
  975. emit_native_mov_reg_state(emit, REG_ARG_1, LOCAL_IDX_OLD_GLOBALS(emit));
  976. if (emit->scope->exc_stack_size == 0) {
  977. // Optimisation: if globals didn't change then don't restore them and don't do nlr_pop
  978. ASM_JUMP_IF_REG_ZERO(emit->as, REG_ARG_1, emit->exit_label + 1, false);
  979. }
  980. // Restore old globals
  981. emit_call(emit, MP_F_NATIVE_SWAP_GLOBALS);
  982. }
  983. // Pop the nlr context
  984. emit_call(emit, MP_F_NLR_POP);
  985. if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
  986. if (emit->scope->exc_stack_size == 0) {
  987. // Destination label for above optimisation
  988. emit_native_label_assign(emit, emit->exit_label + 1);
  989. }
  990. }
  991. // Load return value
  992. ASM_MOV_REG_LOCAL(emit->as, REG_RET, LOCAL_IDX_RET_VAL(emit));
  993. }
  994. ASM_EXIT(emit->as);
  995. }
  996. STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
  997. DEBUG_printf("import_name %s\n", qstr_str(qst));
  998. // get arguments from stack: arg2 = fromlist, arg3 = level
  999. // If using viper types these arguments must be converted to proper objects, and
  1000. // to accomplish this viper types are turned off for the emit_pre_pop_reg_reg call.
  1001. bool orig_do_viper_types = emit->do_viper_types;
  1002. emit->do_viper_types = false;
  1003. vtype_kind_t vtype_fromlist;
  1004. vtype_kind_t vtype_level;
  1005. emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3);
  1006. assert(vtype_fromlist == VTYPE_PYOBJ);
  1007. assert(vtype_level == VTYPE_PYOBJ);
  1008. emit->do_viper_types = orig_do_viper_types;
  1009. emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
  1010. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1011. }
  1012. STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
  1013. DEBUG_printf("import_from %s\n", qstr_str(qst));
  1014. emit_native_pre(emit);
  1015. vtype_kind_t vtype_module;
  1016. emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
  1017. assert(vtype_module == VTYPE_PYOBJ);
  1018. emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
  1019. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1020. }
  1021. STATIC void emit_native_import_star(emit_t *emit) {
  1022. DEBUG_printf("import_star\n");
  1023. vtype_kind_t vtype_module;
  1024. emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
  1025. assert(vtype_module == VTYPE_PYOBJ);
  1026. emit_call(emit, MP_F_IMPORT_ALL);
  1027. emit_post(emit);
  1028. }
  1029. STATIC void emit_native_import(emit_t *emit, qstr qst, int kind) {
  1030. if (kind == MP_EMIT_IMPORT_NAME) {
  1031. emit_native_import_name(emit, qst);
  1032. } else if (kind == MP_EMIT_IMPORT_FROM) {
  1033. emit_native_import_from(emit, qst);
  1034. } else {
  1035. emit_native_import_star(emit);
  1036. }
  1037. }
  1038. STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
  1039. DEBUG_printf("load_const_tok(tok=%u)\n", tok);
  1040. if (tok == MP_TOKEN_ELLIPSIS) {
  1041. emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj));
  1042. } else {
  1043. emit_native_pre(emit);
  1044. if (tok == MP_TOKEN_KW_NONE) {
  1045. emit_post_push_imm(emit, VTYPE_PTR_NONE, 0);
  1046. } else {
  1047. emit_post_push_imm(emit, VTYPE_BOOL, tok == MP_TOKEN_KW_FALSE ? 0 : 1);
  1048. }
  1049. }
  1050. }
  1051. STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
  1052. DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
  1053. emit_native_pre(emit);
  1054. emit_post_push_imm(emit, VTYPE_INT, arg);
  1055. }
  1056. STATIC void emit_native_load_const_str(emit_t *emit, qstr qst) {
  1057. emit_native_pre(emit);
  1058. // TODO: Eventually we want to be able to work with raw pointers in viper to
  1059. // do native array access. For now we just load them as any other object.
  1060. /*
  1061. if (emit->do_viper_types) {
  1062. // load a pointer to the asciiz string?
  1063. emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
  1064. } else
  1065. */
  1066. {
  1067. emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
  1068. }
  1069. }
  1070. STATIC void emit_native_load_const_obj(emit_t *emit, mp_obj_t obj) {
  1071. emit_native_pre(emit);
  1072. need_reg_single(emit, REG_RET, 0);
  1073. emit_load_reg_with_object(emit, REG_RET, obj);
  1074. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1075. }
  1076. STATIC void emit_native_load_null(emit_t *emit) {
  1077. emit_native_pre(emit);
  1078. emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
  1079. }
  1080. STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
  1081. DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
  1082. vtype_kind_t vtype = emit->local_vtype[local_num];
  1083. if (vtype == VTYPE_UNBOUND) {
  1084. EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
  1085. }
  1086. emit_native_pre(emit);
  1087. if (local_num < REG_LOCAL_NUM && CAN_USE_REGS_FOR_LOCALS(emit)) {
  1088. emit_post_push_reg(emit, vtype, reg_local_table[local_num]);
  1089. } else {
  1090. need_reg_single(emit, REG_TEMP0, 0);
  1091. emit_native_mov_reg_state(emit, REG_TEMP0, LOCAL_IDX_LOCAL_VAR(emit, local_num));
  1092. emit_post_push_reg(emit, vtype, REG_TEMP0);
  1093. }
  1094. }
  1095. STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
  1096. DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
  1097. need_reg_single(emit, REG_RET, 0);
  1098. emit_native_load_fast(emit, qst, local_num);
  1099. vtype_kind_t vtype;
  1100. int reg_base = REG_RET;
  1101. emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
  1102. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
  1103. // closed over vars are always Python objects
  1104. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1105. }
  1106. STATIC void emit_native_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
  1107. if (kind == MP_EMIT_IDOP_LOCAL_FAST) {
  1108. emit_native_load_fast(emit, qst, local_num);
  1109. } else {
  1110. emit_native_load_deref(emit, qst, local_num);
  1111. }
  1112. }
  1113. STATIC void emit_native_load_global(emit_t *emit, qstr qst, int kind) {
  1114. MP_STATIC_ASSERT(MP_F_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_F_LOAD_NAME);
  1115. MP_STATIC_ASSERT(MP_F_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_F_LOAD_GLOBAL);
  1116. emit_native_pre(emit);
  1117. if (kind == MP_EMIT_IDOP_GLOBAL_NAME) {
  1118. DEBUG_printf("load_name(%s)\n", qstr_str(qst));
  1119. } else {
  1120. DEBUG_printf("load_global(%s)\n", qstr_str(qst));
  1121. if (emit->do_viper_types) {
  1122. // check for builtin casting operators
  1123. int native_type = mp_native_type_from_qstr(qst);
  1124. if (native_type >= MP_NATIVE_TYPE_INT) {
  1125. emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, native_type);
  1126. return;
  1127. }
  1128. }
  1129. }
  1130. emit_call_with_imm_arg(emit, MP_F_LOAD_NAME + kind, qst, REG_ARG_1);
  1131. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1132. }
  1133. STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
  1134. // depends on type of subject:
  1135. // - integer, function, pointer to integers: error
  1136. // - pointer to structure: get member, quite easy
  1137. // - Python object: call mp_load_attr, and needs to be typed to convert result
  1138. vtype_kind_t vtype_base;
  1139. emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
  1140. assert(vtype_base == VTYPE_PYOBJ);
  1141. emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
  1142. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1143. }
  1144. STATIC void emit_native_load_method(emit_t *emit, qstr qst, bool is_super) {
  1145. if (is_super) {
  1146. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, 3); // arg2 = dest ptr
  1147. emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_2, 2); // arg2 = dest ptr
  1148. emit_call_with_imm_arg(emit, MP_F_LOAD_SUPER_METHOD, qst, REG_ARG_1); // arg1 = method name
  1149. } else {
  1150. vtype_kind_t vtype_base;
  1151. emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
  1152. assert(vtype_base == VTYPE_PYOBJ);
  1153. emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
  1154. emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
  1155. }
  1156. }
  1157. STATIC void emit_native_load_build_class(emit_t *emit) {
  1158. emit_native_pre(emit);
  1159. emit_call(emit, MP_F_LOAD_BUILD_CLASS);
  1160. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1161. }
  1162. STATIC void emit_native_load_subscr(emit_t *emit) {
  1163. DEBUG_printf("load_subscr\n");
  1164. // need to compile: base[index]
  1165. // pop: index, base
  1166. // optimise case where index is an immediate
  1167. vtype_kind_t vtype_base = peek_vtype(emit, 1);
  1168. if (vtype_base == VTYPE_PYOBJ) {
  1169. // standard Python subscr
  1170. // TODO factor this implicit cast code with other uses of it
  1171. vtype_kind_t vtype_index = peek_vtype(emit, 0);
  1172. if (vtype_index == VTYPE_PYOBJ) {
  1173. emit_pre_pop_reg(emit, &vtype_index, REG_ARG_2);
  1174. } else {
  1175. emit_pre_pop_reg(emit, &vtype_index, REG_ARG_1);
  1176. emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype_index, REG_ARG_2); // arg2 = type
  1177. ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
  1178. }
  1179. emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
  1180. emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, (mp_uint_t)MP_OBJ_SENTINEL, REG_ARG_3);
  1181. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1182. } else {
  1183. // viper load
  1184. // TODO The different machine architectures have very different
  1185. // capabilities and requirements for loads, so probably best to
  1186. // write a completely separate load-optimiser for each one.
  1187. stack_info_t *top = peek_stack(emit, 0);
  1188. if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
  1189. // index is an immediate
  1190. mp_int_t index_value = top->data.u_imm;
  1191. emit_pre_pop_discard(emit); // discard index
  1192. int reg_base = REG_ARG_1;
  1193. int reg_index = REG_ARG_2;
  1194. emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
  1195. switch (vtype_base) {
  1196. case VTYPE_PTR8: {
  1197. // pointer to 8-bit memory
  1198. // TODO optimise to use thumb ldrb r1, [r2, r3]
  1199. if (index_value != 0) {
  1200. // index is non-zero
  1201. #if N_THUMB
  1202. if (index_value > 0 && index_value < 32) {
  1203. asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
  1204. break;
  1205. }
  1206. #endif
  1207. ASM_MOV_REG_IMM(emit->as, reg_index, index_value);
  1208. ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
  1209. reg_base = reg_index;
  1210. }
  1211. ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
  1212. break;
  1213. }
  1214. case VTYPE_PTR16: {
  1215. // pointer to 16-bit memory
  1216. if (index_value != 0) {
  1217. // index is a non-zero immediate
  1218. #if N_THUMB
  1219. if (index_value > 0 && index_value < 32) {
  1220. asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
  1221. break;
  1222. }
  1223. #endif
  1224. ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 1);
  1225. ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
  1226. reg_base = reg_index;
  1227. }
  1228. ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
  1229. break;
  1230. }
  1231. case VTYPE_PTR32: {
  1232. // pointer to 32-bit memory
  1233. if (index_value != 0) {
  1234. // index is a non-zero immediate
  1235. #if N_THUMB
  1236. if (index_value > 0 && index_value < 32) {
  1237. asm_thumb_ldr_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
  1238. break;
  1239. }
  1240. #endif
  1241. ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 2);
  1242. ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
  1243. reg_base = reg_index;
  1244. }
  1245. ASM_LOAD32_REG_REG(emit->as, REG_RET, reg_base); // load from (base+4*index)
  1246. break;
  1247. }
  1248. default:
  1249. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1250. "can't load from '%q'", vtype_to_qstr(vtype_base));
  1251. }
  1252. } else {
  1253. // index is not an immediate
  1254. vtype_kind_t vtype_index;
  1255. int reg_index = REG_ARG_2;
  1256. emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
  1257. emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
  1258. if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
  1259. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1260. "can't load with '%q' index", vtype_to_qstr(vtype_index));
  1261. }
  1262. switch (vtype_base) {
  1263. case VTYPE_PTR8: {
  1264. // pointer to 8-bit memory
  1265. // TODO optimise to use thumb ldrb r1, [r2, r3]
  1266. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1267. ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
  1268. break;
  1269. }
  1270. case VTYPE_PTR16: {
  1271. // pointer to 16-bit memory
  1272. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1273. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1274. ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
  1275. break;
  1276. }
  1277. case VTYPE_PTR32: {
  1278. // pointer to word-size memory
  1279. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1280. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1281. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1282. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1283. ASM_LOAD32_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+4*index)
  1284. break;
  1285. }
  1286. default:
  1287. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1288. "can't load from '%q'", vtype_to_qstr(vtype_base));
  1289. }
  1290. }
  1291. emit_post_push_reg(emit, VTYPE_INT, REG_RET);
  1292. }
  1293. }
  1294. STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
  1295. vtype_kind_t vtype;
  1296. if (local_num < REG_LOCAL_NUM && CAN_USE_REGS_FOR_LOCALS(emit)) {
  1297. emit_pre_pop_reg(emit, &vtype, reg_local_table[local_num]);
  1298. } else {
  1299. emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
  1300. emit_native_mov_state_reg(emit, LOCAL_IDX_LOCAL_VAR(emit, local_num), REG_TEMP0);
  1301. }
  1302. emit_post(emit);
  1303. // check types
  1304. if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
  1305. // first time this local is assigned, so give it a type of the object stored in it
  1306. emit->local_vtype[local_num] = vtype;
  1307. } else if (emit->local_vtype[local_num] != vtype) {
  1308. // type of local is not the same as object stored in it
  1309. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1310. "local '%q' has type '%q' but source is '%q'",
  1311. qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
  1312. }
  1313. }
  1314. STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
  1315. DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
  1316. need_reg_single(emit, REG_TEMP0, 0);
  1317. need_reg_single(emit, REG_TEMP1, 0);
  1318. emit_native_load_fast(emit, qst, local_num);
  1319. vtype_kind_t vtype;
  1320. int reg_base = REG_TEMP0;
  1321. emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
  1322. int reg_src = REG_TEMP1;
  1323. emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
  1324. ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
  1325. emit_post(emit);
  1326. }
  1327. STATIC void emit_native_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
  1328. if (kind == MP_EMIT_IDOP_LOCAL_FAST) {
  1329. emit_native_store_fast(emit, qst, local_num);
  1330. } else {
  1331. emit_native_store_deref(emit, qst, local_num);
  1332. }
  1333. }
  1334. STATIC void emit_native_store_global(emit_t *emit, qstr qst, int kind) {
  1335. MP_STATIC_ASSERT(MP_F_STORE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_F_STORE_NAME);
  1336. MP_STATIC_ASSERT(MP_F_STORE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_F_STORE_GLOBAL);
  1337. if (kind == MP_EMIT_IDOP_GLOBAL_NAME) {
  1338. // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
  1339. vtype_kind_t vtype;
  1340. emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
  1341. assert(vtype == VTYPE_PYOBJ);
  1342. } else {
  1343. vtype_kind_t vtype = peek_vtype(emit, 0);
  1344. if (vtype == VTYPE_PYOBJ) {
  1345. emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
  1346. } else {
  1347. emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
  1348. emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype, REG_ARG_2); // arg2 = type
  1349. ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
  1350. }
  1351. }
  1352. emit_call_with_imm_arg(emit, MP_F_STORE_NAME + kind, qst, REG_ARG_1); // arg1 = name
  1353. emit_post(emit);
  1354. }
  1355. STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
  1356. vtype_kind_t vtype_base, vtype_val;
  1357. emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
  1358. assert(vtype_base == VTYPE_PYOBJ);
  1359. assert(vtype_val == VTYPE_PYOBJ);
  1360. emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
  1361. emit_post(emit);
  1362. }
  1363. STATIC void emit_native_store_subscr(emit_t *emit) {
  1364. DEBUG_printf("store_subscr\n");
  1365. // need to compile: base[index] = value
  1366. // pop: index, base, value
  1367. // optimise case where index is an immediate
  1368. vtype_kind_t vtype_base = peek_vtype(emit, 1);
  1369. if (vtype_base == VTYPE_PYOBJ) {
  1370. // standard Python subscr
  1371. vtype_kind_t vtype_index = peek_vtype(emit, 0);
  1372. vtype_kind_t vtype_value = peek_vtype(emit, 2);
  1373. if (vtype_index != VTYPE_PYOBJ || vtype_value != VTYPE_PYOBJ) {
  1374. // need to implicitly convert non-objects to objects
  1375. // TODO do this properly
  1376. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_1, 3);
  1377. adjust_stack(emit, 3);
  1378. }
  1379. emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
  1380. emit_call(emit, MP_F_OBJ_SUBSCR);
  1381. } else {
  1382. // viper store
  1383. // TODO The different machine architectures have very different
  1384. // capabilities and requirements for stores, so probably best to
  1385. // write a completely separate store-optimiser for each one.
  1386. stack_info_t *top = peek_stack(emit, 0);
  1387. if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
  1388. // index is an immediate
  1389. mp_int_t index_value = top->data.u_imm;
  1390. emit_pre_pop_discard(emit); // discard index
  1391. vtype_kind_t vtype_value;
  1392. int reg_base = REG_ARG_1;
  1393. int reg_index = REG_ARG_2;
  1394. int reg_value = REG_ARG_3;
  1395. emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
  1396. #if N_X86
  1397. // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
  1398. emit_pre_pop_reg(emit, &vtype_value, reg_value);
  1399. #else
  1400. emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
  1401. #endif
  1402. if (vtype_value != VTYPE_BOOL && vtype_value != VTYPE_INT && vtype_value != VTYPE_UINT) {
  1403. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1404. "can't store '%q'", vtype_to_qstr(vtype_value));
  1405. }
  1406. switch (vtype_base) {
  1407. case VTYPE_PTR8: {
  1408. // pointer to 8-bit memory
  1409. // TODO optimise to use thumb strb r1, [r2, r3]
  1410. if (index_value != 0) {
  1411. // index is non-zero
  1412. #if N_THUMB
  1413. if (index_value > 0 && index_value < 32) {
  1414. asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
  1415. break;
  1416. }
  1417. #endif
  1418. ASM_MOV_REG_IMM(emit->as, reg_index, index_value);
  1419. #if N_ARM
  1420. asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
  1421. return;
  1422. #endif
  1423. ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
  1424. reg_base = reg_index;
  1425. }
  1426. ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
  1427. break;
  1428. }
  1429. case VTYPE_PTR16: {
  1430. // pointer to 16-bit memory
  1431. if (index_value != 0) {
  1432. // index is a non-zero immediate
  1433. #if N_THUMB
  1434. if (index_value > 0 && index_value < 32) {
  1435. asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
  1436. break;
  1437. }
  1438. #endif
  1439. ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 1);
  1440. ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
  1441. reg_base = reg_index;
  1442. }
  1443. ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
  1444. break;
  1445. }
  1446. case VTYPE_PTR32: {
  1447. // pointer to 32-bit memory
  1448. if (index_value != 0) {
  1449. // index is a non-zero immediate
  1450. #if N_THUMB
  1451. if (index_value > 0 && index_value < 32) {
  1452. asm_thumb_str_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
  1453. break;
  1454. }
  1455. #endif
  1456. #if N_ARM
  1457. ASM_MOV_REG_IMM(emit->as, reg_index, index_value);
  1458. asm_arm_str_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
  1459. return;
  1460. #endif
  1461. ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 2);
  1462. ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
  1463. reg_base = reg_index;
  1464. }
  1465. ASM_STORE32_REG_REG(emit->as, reg_value, reg_base); // store value to (base+4*index)
  1466. break;
  1467. }
  1468. default:
  1469. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1470. "can't store to '%q'", vtype_to_qstr(vtype_base));
  1471. }
  1472. } else {
  1473. // index is not an immediate
  1474. vtype_kind_t vtype_index, vtype_value;
  1475. int reg_index = REG_ARG_2;
  1476. int reg_value = REG_ARG_3;
  1477. emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
  1478. emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
  1479. if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
  1480. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1481. "can't store with '%q' index", vtype_to_qstr(vtype_index));
  1482. }
  1483. #if N_X86
  1484. // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
  1485. emit_pre_pop_reg(emit, &vtype_value, reg_value);
  1486. #else
  1487. emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
  1488. #endif
  1489. if (vtype_value != VTYPE_BOOL && vtype_value != VTYPE_INT && vtype_value != VTYPE_UINT) {
  1490. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1491. "can't store '%q'", vtype_to_qstr(vtype_value));
  1492. }
  1493. switch (vtype_base) {
  1494. case VTYPE_PTR8: {
  1495. // pointer to 8-bit memory
  1496. // TODO optimise to use thumb strb r1, [r2, r3]
  1497. #if N_ARM
  1498. asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
  1499. break;
  1500. #endif
  1501. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1502. ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
  1503. break;
  1504. }
  1505. case VTYPE_PTR16: {
  1506. // pointer to 16-bit memory
  1507. #if N_ARM
  1508. asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
  1509. break;
  1510. #endif
  1511. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1512. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1513. ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
  1514. break;
  1515. }
  1516. case VTYPE_PTR32: {
  1517. // pointer to 32-bit memory
  1518. #if N_ARM
  1519. asm_arm_str_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
  1520. break;
  1521. #endif
  1522. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1523. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1524. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1525. ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
  1526. ASM_STORE32_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+4*index)
  1527. break;
  1528. }
  1529. default:
  1530. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1531. "can't store to '%q'", vtype_to_qstr(vtype_base));
  1532. }
  1533. }
  1534. }
  1535. }
  1536. STATIC void emit_native_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
  1537. if (kind == MP_EMIT_IDOP_LOCAL_FAST) {
  1538. // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
  1539. // to mark deleted vars but then every var would need to be checked on
  1540. // each access. Very inefficient, so just set value to None to enable GC.
  1541. emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
  1542. emit_native_store_fast(emit, qst, local_num);
  1543. } else {
  1544. // TODO implement me!
  1545. }
  1546. }
  1547. STATIC void emit_native_delete_global(emit_t *emit, qstr qst, int kind) {
  1548. MP_STATIC_ASSERT(MP_F_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_F_DELETE_NAME);
  1549. MP_STATIC_ASSERT(MP_F_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_F_DELETE_GLOBAL);
  1550. emit_native_pre(emit);
  1551. emit_call_with_imm_arg(emit, MP_F_DELETE_NAME + kind, qst, REG_ARG_1);
  1552. emit_post(emit);
  1553. }
  1554. STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
  1555. vtype_kind_t vtype_base;
  1556. emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
  1557. assert(vtype_base == VTYPE_PYOBJ);
  1558. emit_call_with_2_imm_args(emit, MP_F_STORE_ATTR, qst, REG_ARG_2, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3); // arg2 = attribute name, arg3 = value (null for delete)
  1559. emit_post(emit);
  1560. }
  1561. STATIC void emit_native_delete_subscr(emit_t *emit) {
  1562. vtype_kind_t vtype_index, vtype_base;
  1563. emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
  1564. assert(vtype_index == VTYPE_PYOBJ);
  1565. assert(vtype_base == VTYPE_PYOBJ);
  1566. emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3);
  1567. }
  1568. STATIC void emit_native_subscr(emit_t *emit, int kind) {
  1569. if (kind == MP_EMIT_SUBSCR_LOAD) {
  1570. emit_native_load_subscr(emit);
  1571. } else if (kind == MP_EMIT_SUBSCR_STORE) {
  1572. emit_native_store_subscr(emit);
  1573. } else {
  1574. emit_native_delete_subscr(emit);
  1575. }
  1576. }
  1577. STATIC void emit_native_attr(emit_t *emit, qstr qst, int kind) {
  1578. if (kind == MP_EMIT_ATTR_LOAD) {
  1579. emit_native_load_attr(emit, qst);
  1580. } else if (kind == MP_EMIT_ATTR_STORE) {
  1581. emit_native_store_attr(emit, qst);
  1582. } else {
  1583. emit_native_delete_attr(emit, qst);
  1584. }
  1585. }
  1586. STATIC void emit_native_dup_top(emit_t *emit) {
  1587. DEBUG_printf("dup_top\n");
  1588. vtype_kind_t vtype;
  1589. int reg = REG_TEMP0;
  1590. emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
  1591. emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
  1592. }
  1593. STATIC void emit_native_dup_top_two(emit_t *emit) {
  1594. vtype_kind_t vtype0, vtype1;
  1595. emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
  1596. emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
  1597. }
  1598. STATIC void emit_native_pop_top(emit_t *emit) {
  1599. DEBUG_printf("pop_top\n");
  1600. emit_pre_pop_discard(emit);
  1601. emit_post(emit);
  1602. }
  1603. STATIC void emit_native_rot_two(emit_t *emit) {
  1604. DEBUG_printf("rot_two\n");
  1605. vtype_kind_t vtype0, vtype1;
  1606. emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
  1607. emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
  1608. }
  1609. STATIC void emit_native_rot_three(emit_t *emit) {
  1610. DEBUG_printf("rot_three\n");
  1611. vtype_kind_t vtype0, vtype1, vtype2;
  1612. emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
  1613. emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
  1614. }
  1615. STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
  1616. DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
  1617. emit_native_pre(emit);
  1618. // need to commit stack because we are jumping elsewhere
  1619. need_stack_settled(emit);
  1620. ASM_JUMP(emit->as, label);
  1621. emit_post(emit);
  1622. }
  1623. STATIC void emit_native_jump_helper(emit_t *emit, bool cond, mp_uint_t label, bool pop) {
  1624. vtype_kind_t vtype = peek_vtype(emit, 0);
  1625. if (vtype == VTYPE_PYOBJ) {
  1626. emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
  1627. if (!pop) {
  1628. adjust_stack(emit, 1);
  1629. }
  1630. emit_call(emit, MP_F_OBJ_IS_TRUE);
  1631. } else {
  1632. emit_pre_pop_reg(emit, &vtype, REG_RET);
  1633. if (!pop) {
  1634. adjust_stack(emit, 1);
  1635. }
  1636. if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
  1637. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1638. "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
  1639. }
  1640. }
  1641. // For non-pop need to save the vtype so that emit_native_adjust_stack_size
  1642. // can use it. This is a bit of a hack.
  1643. if (!pop) {
  1644. emit->saved_stack_vtype = vtype;
  1645. }
  1646. // need to commit stack because we may jump elsewhere
  1647. need_stack_settled(emit);
  1648. // Emit the jump
  1649. if (cond) {
  1650. ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label, vtype == VTYPE_PYOBJ);
  1651. } else {
  1652. ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label, vtype == VTYPE_PYOBJ);
  1653. }
  1654. if (!pop) {
  1655. adjust_stack(emit, -1);
  1656. }
  1657. emit_post(emit);
  1658. }
  1659. STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
  1660. DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
  1661. emit_native_jump_helper(emit, cond, label, true);
  1662. }
  1663. STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
  1664. DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
  1665. emit_native_jump_helper(emit, cond, label, false);
  1666. }
  1667. STATIC void emit_native_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
  1668. if (except_depth > 0) {
  1669. exc_stack_entry_t *first_finally = NULL;
  1670. exc_stack_entry_t *prev_finally = NULL;
  1671. exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
  1672. for (; except_depth > 0; --except_depth, --e) {
  1673. if (e->is_finally && e->is_active) {
  1674. // Found an active finally handler
  1675. if (first_finally == NULL) {
  1676. first_finally = e;
  1677. }
  1678. if (prev_finally != NULL) {
  1679. // Mark prev finally as needed to unwind a jump
  1680. prev_finally->unwind_label = e->label;
  1681. }
  1682. prev_finally = e;
  1683. }
  1684. }
  1685. if (prev_finally == NULL) {
  1686. // No finally, handle the jump ourselves
  1687. // First, restore the exception handler address for the jump
  1688. if (e < emit->exc_stack) {
  1689. ASM_XOR_REG_REG(emit->as, REG_RET, REG_RET);
  1690. } else {
  1691. ASM_MOV_REG_PCREL(emit->as, REG_RET, e->label);
  1692. }
  1693. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_RET);
  1694. } else {
  1695. // Last finally should do our jump for us
  1696. // Mark finally as needing to decide the type of jump
  1697. prev_finally->unwind_label = UNWIND_LABEL_DO_FINAL_UNWIND;
  1698. ASM_MOV_REG_PCREL(emit->as, REG_RET, label & ~MP_EMIT_BREAK_FROM_FOR);
  1699. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_UNWIND(emit), REG_RET);
  1700. // Cancel any active exception (see also emit_native_pop_except)
  1701. emit_native_mov_reg_const(emit, REG_RET, MP_F_CONST_NONE_OBJ);
  1702. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_RET);
  1703. // Jump to the innermost active finally
  1704. label = first_finally->label;
  1705. }
  1706. }
  1707. emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR);
  1708. }
  1709. STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
  1710. // the context manager is on the top of the stack
  1711. // stack: (..., ctx_mgr)
  1712. // get __exit__ method
  1713. vtype_kind_t vtype;
  1714. emit_access_stack(emit, 1, &vtype, REG_ARG_1); // arg1 = ctx_mgr
  1715. assert(vtype == VTYPE_PYOBJ);
  1716. emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
  1717. emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, MP_QSTR___exit__, REG_ARG_2);
  1718. // stack: (..., ctx_mgr, __exit__, self)
  1719. emit_pre_pop_reg(emit, &vtype, REG_ARG_3); // self
  1720. emit_pre_pop_reg(emit, &vtype, REG_ARG_2); // __exit__
  1721. emit_pre_pop_reg(emit, &vtype, REG_ARG_1); // ctx_mgr
  1722. emit_post_push_reg(emit, vtype, REG_ARG_2); // __exit__
  1723. emit_post_push_reg(emit, vtype, REG_ARG_3); // self
  1724. // stack: (..., __exit__, self)
  1725. // REG_ARG_1=ctx_mgr
  1726. // get __enter__ method
  1727. emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
  1728. emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, MP_QSTR___enter__, REG_ARG_2); // arg2 = method name
  1729. // stack: (..., __exit__, self, __enter__, self)
  1730. // call __enter__ method
  1731. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 2); // pointer to items, including meth and self
  1732. emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, 0, REG_ARG_1, 0, REG_ARG_2);
  1733. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // push return value of __enter__
  1734. // stack: (..., __exit__, self, as_value)
  1735. // need to commit stack because we may jump elsewhere
  1736. need_stack_settled(emit);
  1737. emit_native_push_exc_stack(emit, label, true);
  1738. emit_native_dup_top(emit);
  1739. // stack: (..., __exit__, self, as_value, as_value)
  1740. }
  1741. STATIC void emit_native_setup_block(emit_t *emit, mp_uint_t label, int kind) {
  1742. if (kind == MP_EMIT_SETUP_BLOCK_WITH) {
  1743. emit_native_setup_with(emit, label);
  1744. } else {
  1745. // Set up except and finally
  1746. emit_native_pre(emit);
  1747. need_stack_settled(emit);
  1748. emit_native_push_exc_stack(emit, label, kind == MP_EMIT_SETUP_BLOCK_FINALLY);
  1749. emit_post(emit);
  1750. }
  1751. }
  1752. STATIC void emit_native_with_cleanup(emit_t *emit, mp_uint_t label) {
  1753. // Note: 3 labels are reserved for this function, starting at *emit->label_slot
  1754. // stack: (..., __exit__, self, as_value)
  1755. emit_native_pre(emit);
  1756. emit_native_leave_exc_stack(emit, false);
  1757. adjust_stack(emit, -1);
  1758. // stack: (..., __exit__, self)
  1759. // Label for case where __exit__ is called from an unwind jump
  1760. emit_native_label_assign(emit, *emit->label_slot + 2);
  1761. // call __exit__
  1762. emit_post_push_imm(emit, VTYPE_PTR_NONE, 0);
  1763. emit_post_push_imm(emit, VTYPE_PTR_NONE, 0);
  1764. emit_post_push_imm(emit, VTYPE_PTR_NONE, 0);
  1765. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 5);
  1766. emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, 3, REG_ARG_1, 0, REG_ARG_2);
  1767. // Replace exc with None and finish
  1768. emit_native_jump(emit, *emit->label_slot);
  1769. // nlr_catch
  1770. // Don't use emit_native_label_assign because this isn't a real finally label
  1771. mp_asm_base_label_assign(&emit->as->base, label);
  1772. // Leave with's exception handler
  1773. emit_native_leave_exc_stack(emit, true);
  1774. // Adjust stack counter for: __exit__, self (implicitly discard as_value which is above self)
  1775. emit_native_adjust_stack_size(emit, 2);
  1776. // stack: (..., __exit__, self)
  1777. ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit)); // get exc
  1778. // Check if exc is None and jump to non-exc handler if it is
  1779. emit_native_mov_reg_const(emit, REG_ARG_2, MP_F_CONST_NONE_OBJ);
  1780. ASM_JUMP_IF_REG_EQ(emit->as, REG_ARG_1, REG_ARG_2, *emit->label_slot + 2);
  1781. ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_2, REG_ARG_1, 0); // get type(exc)
  1782. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_ARG_2); // push type(exc)
  1783. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_ARG_1); // push exc value
  1784. emit_post_push_imm(emit, VTYPE_PTR_NONE, 0); // traceback info
  1785. // Stack: (..., __exit__, self, type(exc), exc, traceback)
  1786. // call __exit__ method
  1787. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 5);
  1788. emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, 3, REG_ARG_1, 0, REG_ARG_2);
  1789. // Stack: (...)
  1790. // If REG_RET is true then we need to replace exception with None (swallow exception)
  1791. if (REG_ARG_1 != REG_RET) {
  1792. ASM_MOV_REG_REG(emit->as, REG_ARG_1, REG_RET);
  1793. }
  1794. emit_call(emit, MP_F_OBJ_IS_TRUE);
  1795. ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, *emit->label_slot + 1, true);
  1796. // Replace exception with None
  1797. emit_native_label_assign(emit, *emit->label_slot);
  1798. emit_native_mov_reg_const(emit, REG_TEMP0, MP_F_CONST_NONE_OBJ);
  1799. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_TEMP0);
  1800. // end of with cleanup nlr_catch block
  1801. emit_native_label_assign(emit, *emit->label_slot + 1);
  1802. // Exception is in nlr_buf.ret_val slot
  1803. }
  1804. STATIC void emit_native_end_finally(emit_t *emit) {
  1805. // logic:
  1806. // exc = pop_stack
  1807. // if exc == None: pass
  1808. // else: raise exc
  1809. // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
  1810. emit_native_pre(emit);
  1811. ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit));
  1812. emit_call(emit, MP_F_NATIVE_RAISE);
  1813. // Get state for this finally and see if we need to unwind
  1814. exc_stack_entry_t *e = emit_native_pop_exc_stack(emit);
  1815. if (e->unwind_label != UNWIND_LABEL_UNUSED) {
  1816. ASM_MOV_REG_LOCAL(emit->as, REG_RET, LOCAL_IDX_EXC_HANDLER_UNWIND(emit));
  1817. ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, *emit->label_slot, false);
  1818. if (e->unwind_label == UNWIND_LABEL_DO_FINAL_UNWIND) {
  1819. ASM_JUMP_REG(emit->as, REG_RET);
  1820. } else {
  1821. emit_native_jump(emit, e->unwind_label);
  1822. }
  1823. emit_native_label_assign(emit, *emit->label_slot);
  1824. }
  1825. emit_post(emit);
  1826. }
  1827. STATIC void emit_native_get_iter(emit_t *emit, bool use_stack) {
  1828. // perhaps the difficult one, as we want to rewrite for loops using native code
  1829. // in cases where we iterate over a Python object, can we use normal runtime calls?
  1830. vtype_kind_t vtype;
  1831. emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
  1832. assert(vtype == VTYPE_PYOBJ);
  1833. if (use_stack) {
  1834. emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_2, MP_OBJ_ITER_BUF_NSLOTS);
  1835. emit_call(emit, MP_F_NATIVE_GETITER);
  1836. } else {
  1837. // mp_getiter will allocate the iter_buf on the heap
  1838. ASM_MOV_REG_IMM(emit->as, REG_ARG_2, 0);
  1839. emit_call(emit, MP_F_NATIVE_GETITER);
  1840. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1841. }
  1842. }
  1843. STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
  1844. emit_native_pre(emit);
  1845. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_1, MP_OBJ_ITER_BUF_NSLOTS);
  1846. adjust_stack(emit, MP_OBJ_ITER_BUF_NSLOTS);
  1847. emit_call(emit, MP_F_NATIVE_ITERNEXT);
  1848. #ifdef NDEBUG
  1849. MP_STATIC_ASSERT(MP_OBJ_STOP_ITERATION == 0);
  1850. ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label, false);
  1851. #else
  1852. ASM_MOV_REG_IMM(emit->as, REG_TEMP1, (mp_uint_t)MP_OBJ_STOP_ITERATION);
  1853. ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
  1854. #endif
  1855. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1856. }
  1857. STATIC void emit_native_for_iter_end(emit_t *emit) {
  1858. // adjust stack counter (we get here from for_iter ending, which popped the value for us)
  1859. emit_native_pre(emit);
  1860. adjust_stack(emit, -MP_OBJ_ITER_BUF_NSLOTS);
  1861. emit_post(emit);
  1862. }
  1863. STATIC void emit_native_pop_block(emit_t *emit) {
  1864. emit_native_pre(emit);
  1865. if (!emit->exc_stack[emit->exc_stack_size - 1].is_finally) {
  1866. emit_native_leave_exc_stack(emit, false);
  1867. }
  1868. emit_post(emit);
  1869. }
  1870. STATIC void emit_native_pop_except(emit_t *emit) {
  1871. // Cancel any active exception so subsequent handlers don't see it
  1872. emit_native_mov_reg_const(emit, REG_TEMP0, MP_F_CONST_NONE_OBJ);
  1873. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_TEMP0);
  1874. }
  1875. STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
  1876. vtype_kind_t vtype;
  1877. emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
  1878. if (vtype == VTYPE_PYOBJ) {
  1879. emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
  1880. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  1881. } else {
  1882. adjust_stack(emit, 1);
  1883. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  1884. "unary op %q not implemented", mp_unary_op_method_name[op]);
  1885. }
  1886. }
  1887. STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
  1888. DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
  1889. vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
  1890. vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
  1891. if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
  1892. // for integers, inplace and normal ops are equivalent, so use just normal ops
  1893. if (MP_BINARY_OP_INPLACE_OR <= op && op <= MP_BINARY_OP_INPLACE_POWER) {
  1894. op += MP_BINARY_OP_OR - MP_BINARY_OP_INPLACE_OR;
  1895. }
  1896. #if N_X64 || N_X86
  1897. // special cases for x86 and shifting
  1898. if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_RSHIFT) {
  1899. #if N_X64
  1900. emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
  1901. #else
  1902. emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
  1903. #endif
  1904. if (op == MP_BINARY_OP_LSHIFT) {
  1905. ASM_LSL_REG(emit->as, REG_RET);
  1906. } else {
  1907. ASM_ASR_REG(emit->as, REG_RET);
  1908. }
  1909. emit_post_push_reg(emit, VTYPE_INT, REG_RET);
  1910. return;
  1911. }
  1912. #endif
  1913. // special cases for floor-divide and module because we dispatch to helper functions
  1914. if (op == MP_BINARY_OP_FLOOR_DIVIDE || op == MP_BINARY_OP_MODULO) {
  1915. emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
  1916. if (op == MP_BINARY_OP_FLOOR_DIVIDE) {
  1917. emit_call(emit, MP_F_SMALL_INT_FLOOR_DIVIDE);
  1918. } else {
  1919. emit_call(emit, MP_F_SMALL_INT_MODULO);
  1920. }
  1921. emit_post_push_reg(emit, VTYPE_INT, REG_RET);
  1922. return;
  1923. }
  1924. int reg_rhs = REG_ARG_3;
  1925. emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
  1926. emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
  1927. if (0) {
  1928. // dummy
  1929. #if !(N_X64 || N_X86)
  1930. } else if (op == MP_BINARY_OP_LSHIFT) {
  1931. ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
  1932. emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
  1933. } else if (op == MP_BINARY_OP_RSHIFT) {
  1934. ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
  1935. emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
  1936. #endif
  1937. } else if (op == MP_BINARY_OP_OR) {
  1938. ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
  1939. emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
  1940. } else if (op == MP_BINARY_OP_XOR) {
  1941. ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
  1942. emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
  1943. } else if (op == MP_BINARY_OP_AND) {
  1944. ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
  1945. emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
  1946. } else if (op == MP_BINARY_OP_ADD) {
  1947. ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
  1948. emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
  1949. } else if (op == MP_BINARY_OP_SUBTRACT) {
  1950. ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
  1951. emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
  1952. } else if (op == MP_BINARY_OP_MULTIPLY) {
  1953. ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
  1954. emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
  1955. } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
  1956. // comparison ops are (in enum order):
  1957. // MP_BINARY_OP_LESS
  1958. // MP_BINARY_OP_MORE
  1959. // MP_BINARY_OP_EQUAL
  1960. // MP_BINARY_OP_LESS_EQUAL
  1961. // MP_BINARY_OP_MORE_EQUAL
  1962. // MP_BINARY_OP_NOT_EQUAL
  1963. need_reg_single(emit, REG_RET, 0);
  1964. #if N_X64
  1965. asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
  1966. asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
  1967. static byte ops[6] = {
  1968. ASM_X64_CC_JL,
  1969. ASM_X64_CC_JG,
  1970. ASM_X64_CC_JE,
  1971. ASM_X64_CC_JLE,
  1972. ASM_X64_CC_JGE,
  1973. ASM_X64_CC_JNE,
  1974. };
  1975. asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
  1976. #elif N_X86
  1977. asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
  1978. asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
  1979. static byte ops[6] = {
  1980. ASM_X86_CC_JL,
  1981. ASM_X86_CC_JG,
  1982. ASM_X86_CC_JE,
  1983. ASM_X86_CC_JLE,
  1984. ASM_X86_CC_JGE,
  1985. ASM_X86_CC_JNE,
  1986. };
  1987. asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
  1988. #elif N_THUMB
  1989. asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
  1990. static uint16_t ops[6] = {
  1991. ASM_THUMB_OP_ITE_GE,
  1992. ASM_THUMB_OP_ITE_GT,
  1993. ASM_THUMB_OP_ITE_EQ,
  1994. ASM_THUMB_OP_ITE_GT,
  1995. ASM_THUMB_OP_ITE_GE,
  1996. ASM_THUMB_OP_ITE_EQ,
  1997. };
  1998. static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
  1999. asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
  2000. asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
  2001. asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
  2002. #elif N_ARM
  2003. asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
  2004. static uint ccs[6] = {
  2005. ASM_ARM_CC_LT,
  2006. ASM_ARM_CC_GT,
  2007. ASM_ARM_CC_EQ,
  2008. ASM_ARM_CC_LE,
  2009. ASM_ARM_CC_GE,
  2010. ASM_ARM_CC_NE,
  2011. };
  2012. asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
  2013. #elif N_XTENSA
  2014. static uint8_t ccs[6] = {
  2015. ASM_XTENSA_CC_LT,
  2016. 0x80 | ASM_XTENSA_CC_LT, // for GT we'll swap args
  2017. ASM_XTENSA_CC_EQ,
  2018. 0x80 | ASM_XTENSA_CC_GE, // for LE we'll swap args
  2019. ASM_XTENSA_CC_GE,
  2020. ASM_XTENSA_CC_NE,
  2021. };
  2022. uint8_t cc = ccs[op - MP_BINARY_OP_LESS];
  2023. if ((cc & 0x80) == 0) {
  2024. asm_xtensa_setcc_reg_reg_reg(emit->as, cc, REG_RET, REG_ARG_2, reg_rhs);
  2025. } else {
  2026. asm_xtensa_setcc_reg_reg_reg(emit->as, cc & ~0x80, REG_RET, reg_rhs, REG_ARG_2);
  2027. }
  2028. #else
  2029. #error not implemented
  2030. #endif
  2031. emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
  2032. } else {
  2033. // TODO other ops not yet implemented
  2034. adjust_stack(emit, 1);
  2035. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  2036. "binary op %q not implemented", mp_binary_op_method_name[op]);
  2037. }
  2038. } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
  2039. emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
  2040. bool invert = false;
  2041. if (op == MP_BINARY_OP_NOT_IN) {
  2042. invert = true;
  2043. op = MP_BINARY_OP_IN;
  2044. } else if (op == MP_BINARY_OP_IS_NOT) {
  2045. invert = true;
  2046. op = MP_BINARY_OP_IS;
  2047. }
  2048. emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
  2049. if (invert) {
  2050. ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
  2051. emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
  2052. }
  2053. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  2054. } else {
  2055. adjust_stack(emit, -1);
  2056. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  2057. "can't do binary op between '%q' and '%q'",
  2058. vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
  2059. }
  2060. }
  2061. #if MICROPY_PY_BUILTINS_SLICE
  2062. STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args);
  2063. #endif
  2064. STATIC void emit_native_build(emit_t *emit, mp_uint_t n_args, int kind) {
  2065. // for viper: call runtime, with types of args
  2066. // if wrapped in byte_array, or something, allocates memory and fills it
  2067. MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_F_BUILD_TUPLE);
  2068. MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_F_BUILD_LIST);
  2069. MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_F_BUILD_MAP);
  2070. MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_SET == MP_F_BUILD_SET);
  2071. #if MICROPY_PY_BUILTINS_SLICE
  2072. if (kind == MP_EMIT_BUILD_SLICE) {
  2073. emit_native_build_slice(emit, n_args);
  2074. return;
  2075. }
  2076. #endif
  2077. emit_native_pre(emit);
  2078. if (kind == MP_EMIT_BUILD_TUPLE || kind == MP_EMIT_BUILD_LIST || kind == MP_EMIT_BUILD_SET) {
  2079. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
  2080. }
  2081. emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE + kind, n_args, REG_ARG_1);
  2082. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple/list/map/set
  2083. }
  2084. STATIC void emit_native_store_map(emit_t *emit) {
  2085. vtype_kind_t vtype_key, vtype_value, vtype_map;
  2086. emit_pre_pop_reg_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3, &vtype_map, REG_ARG_1); // key, value, map
  2087. assert(vtype_key == VTYPE_PYOBJ);
  2088. assert(vtype_value == VTYPE_PYOBJ);
  2089. assert(vtype_map == VTYPE_PYOBJ);
  2090. emit_call(emit, MP_F_STORE_MAP);
  2091. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
  2092. }
  2093. #if MICROPY_PY_BUILTINS_SLICE
  2094. STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
  2095. DEBUG_printf("build_slice %d\n", n_args);
  2096. if (n_args == 2) {
  2097. vtype_kind_t vtype_start, vtype_stop;
  2098. emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
  2099. assert(vtype_start == VTYPE_PYOBJ);
  2100. assert(vtype_stop == VTYPE_PYOBJ);
  2101. emit_native_mov_reg_const(emit, REG_ARG_3, MP_F_CONST_NONE_OBJ); // arg3 = step
  2102. } else {
  2103. assert(n_args == 3);
  2104. vtype_kind_t vtype_start, vtype_stop, vtype_step;
  2105. emit_pre_pop_reg_reg_reg(emit, &vtype_step, REG_ARG_3, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop, arg3 = step
  2106. assert(vtype_start == VTYPE_PYOBJ);
  2107. assert(vtype_stop == VTYPE_PYOBJ);
  2108. assert(vtype_step == VTYPE_PYOBJ);
  2109. }
  2110. emit_call(emit, MP_F_NEW_SLICE);
  2111. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  2112. }
  2113. #endif
  2114. STATIC void emit_native_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_index) {
  2115. mp_fun_kind_t f;
  2116. if (kind == SCOPE_LIST_COMP) {
  2117. vtype_kind_t vtype_item;
  2118. emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
  2119. assert(vtype_item == VTYPE_PYOBJ);
  2120. f = MP_F_LIST_APPEND;
  2121. #if MICROPY_PY_BUILTINS_SET
  2122. } else if (kind == SCOPE_SET_COMP) {
  2123. vtype_kind_t vtype_item;
  2124. emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
  2125. assert(vtype_item == VTYPE_PYOBJ);
  2126. f = MP_F_STORE_SET;
  2127. #endif
  2128. } else {
  2129. // SCOPE_DICT_COMP
  2130. vtype_kind_t vtype_key, vtype_value;
  2131. emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
  2132. assert(vtype_key == VTYPE_PYOBJ);
  2133. assert(vtype_value == VTYPE_PYOBJ);
  2134. f = MP_F_STORE_MAP;
  2135. }
  2136. vtype_kind_t vtype_collection;
  2137. emit_access_stack(emit, collection_index, &vtype_collection, REG_ARG_1);
  2138. assert(vtype_collection == VTYPE_PYOBJ);
  2139. emit_call(emit, f);
  2140. emit_post(emit);
  2141. }
  2142. STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
  2143. DEBUG_printf("unpack_sequence %d\n", n_args);
  2144. vtype_kind_t vtype_base;
  2145. emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
  2146. assert(vtype_base == VTYPE_PYOBJ);
  2147. emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
  2148. emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
  2149. }
  2150. STATIC void emit_native_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
  2151. DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
  2152. vtype_kind_t vtype_base;
  2153. emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
  2154. assert(vtype_base == VTYPE_PYOBJ);
  2155. emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_left + n_right + 1); // arg3 = dest ptr
  2156. emit_call_with_imm_arg(emit, MP_F_UNPACK_EX, n_left | (n_right << 8), REG_ARG_2); // arg2 = n_left + n_right
  2157. }
  2158. STATIC void emit_native_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
  2159. // call runtime, with type info for args, or don't support dict/default params, or only support Python objects for them
  2160. emit_native_pre(emit);
  2161. if (n_pos_defaults == 0 && n_kw_defaults == 0) {
  2162. need_reg_all(emit);
  2163. ASM_MOV_REG_IMM(emit->as, REG_ARG_2, (mp_uint_t)MP_OBJ_NULL);
  2164. ASM_MOV_REG_IMM(emit->as, REG_ARG_3, (mp_uint_t)MP_OBJ_NULL);
  2165. } else {
  2166. vtype_kind_t vtype_def_tuple, vtype_def_dict;
  2167. emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
  2168. assert(vtype_def_tuple == VTYPE_PYOBJ);
  2169. assert(vtype_def_dict == VTYPE_PYOBJ);
  2170. need_reg_all(emit);
  2171. }
  2172. emit_load_reg_with_raw_code(emit, REG_ARG_1, scope->raw_code);
  2173. ASM_CALL_IND(emit->as, MP_F_MAKE_FUNCTION_FROM_RAW_CODE);
  2174. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  2175. }
  2176. STATIC void emit_native_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
  2177. emit_native_pre(emit);
  2178. if (n_pos_defaults == 0 && n_kw_defaults == 0) {
  2179. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
  2180. ASM_MOV_REG_IMM(emit->as, REG_ARG_2, n_closed_over);
  2181. } else {
  2182. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
  2183. ASM_MOV_REG_IMM(emit->as, REG_ARG_2, 0x100 | n_closed_over);
  2184. }
  2185. emit_load_reg_with_raw_code(emit, REG_ARG_1, scope->raw_code);
  2186. ASM_CALL_IND(emit->as, MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
  2187. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  2188. }
  2189. STATIC void emit_native_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
  2190. DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
  2191. // TODO: in viper mode, call special runtime routine with type info for args,
  2192. // and wanted type info for return, to remove need for boxing/unboxing
  2193. emit_native_pre(emit);
  2194. vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
  2195. if (vtype_fun == VTYPE_BUILTIN_CAST) {
  2196. // casting operator
  2197. assert(n_positional == 1 && n_keyword == 0);
  2198. assert(!star_flags);
  2199. DEBUG_printf(" cast to %d\n", vtype_fun);
  2200. vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
  2201. switch (peek_vtype(emit, 0)) {
  2202. case VTYPE_PYOBJ: {
  2203. vtype_kind_t vtype;
  2204. emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
  2205. emit_pre_pop_discard(emit);
  2206. emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, vtype_cast, REG_ARG_2); // arg2 = type
  2207. emit_post_push_reg(emit, vtype_cast, REG_RET);
  2208. break;
  2209. }
  2210. case VTYPE_BOOL:
  2211. case VTYPE_INT:
  2212. case VTYPE_UINT:
  2213. case VTYPE_PTR:
  2214. case VTYPE_PTR8:
  2215. case VTYPE_PTR16:
  2216. case VTYPE_PTR32:
  2217. case VTYPE_PTR_NONE:
  2218. emit_fold_stack_top(emit, REG_ARG_1);
  2219. emit_post_top_set_vtype(emit, vtype_cast);
  2220. break;
  2221. default:
  2222. // this can happen when casting a cast: int(int)
  2223. mp_raise_NotImplementedError("casting");
  2224. }
  2225. } else {
  2226. assert(vtype_fun == VTYPE_PYOBJ);
  2227. if (star_flags) {
  2228. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
  2229. emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 0, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
  2230. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  2231. } else {
  2232. if (n_positional != 0 || n_keyword != 0) {
  2233. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
  2234. }
  2235. emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
  2236. emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
  2237. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  2238. }
  2239. }
  2240. }
  2241. STATIC void emit_native_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
  2242. if (star_flags) {
  2243. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
  2244. emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 1, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
  2245. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  2246. } else {
  2247. emit_native_pre(emit);
  2248. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 2 + n_positional + 2 * n_keyword); // pointer to items, including meth and self
  2249. emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
  2250. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
  2251. }
  2252. }
  2253. STATIC void emit_native_return_value(emit_t *emit) {
  2254. DEBUG_printf("return_value\n");
  2255. if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
  2256. // Save pointer to current stack position for caller to access return value
  2257. emit_get_stack_pointer_to_reg_for_pop(emit, REG_TEMP0, 1);
  2258. emit_native_mov_state_reg(emit, offsetof(mp_code_state_t, sp) / sizeof(uintptr_t), REG_TEMP0);
  2259. // Put return type in return value slot
  2260. ASM_MOV_REG_IMM(emit->as, REG_TEMP0, MP_VM_RETURN_NORMAL);
  2261. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_RET_VAL(emit), REG_TEMP0);
  2262. // Do the unwinding jump to get to the return handler
  2263. emit_native_unwind_jump(emit, emit->exit_label, emit->exc_stack_size);
  2264. emit->last_emit_was_return_value = true;
  2265. return;
  2266. }
  2267. if (emit->do_viper_types) {
  2268. vtype_kind_t return_vtype = emit->scope->scope_flags >> MP_SCOPE_FLAG_VIPERRET_POS;
  2269. if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
  2270. emit_pre_pop_discard(emit);
  2271. if (return_vtype == VTYPE_PYOBJ) {
  2272. emit_native_mov_reg_const(emit, REG_RET, MP_F_CONST_NONE_OBJ);
  2273. } else {
  2274. ASM_MOV_REG_IMM(emit->as, REG_ARG_1, 0);
  2275. }
  2276. } else {
  2277. vtype_kind_t vtype;
  2278. emit_pre_pop_reg(emit, &vtype, return_vtype == VTYPE_PYOBJ ? REG_RET : REG_ARG_1);
  2279. if (vtype != return_vtype) {
  2280. EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
  2281. "return expected '%q' but got '%q'",
  2282. vtype_to_qstr(return_vtype), vtype_to_qstr(vtype));
  2283. }
  2284. }
  2285. if (return_vtype != VTYPE_PYOBJ) {
  2286. emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, return_vtype, REG_ARG_2);
  2287. }
  2288. } else {
  2289. vtype_kind_t vtype;
  2290. emit_pre_pop_reg(emit, &vtype, REG_RET);
  2291. assert(vtype == VTYPE_PYOBJ);
  2292. }
  2293. if (NEED_GLOBAL_EXC_HANDLER(emit)) {
  2294. // Save return value for the global exception handler to use
  2295. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_RET_VAL(emit), REG_RET);
  2296. }
  2297. emit_native_unwind_jump(emit, emit->exit_label, emit->exc_stack_size);
  2298. emit->last_emit_was_return_value = true;
  2299. }
  2300. STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
  2301. assert(n_args == 1);
  2302. vtype_kind_t vtype_exc;
  2303. emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
  2304. if (vtype_exc != VTYPE_PYOBJ) {
  2305. EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
  2306. }
  2307. // TODO probably make this 1 call to the runtime (which could even call convert, native_raise(obj, type))
  2308. emit_call(emit, MP_F_NATIVE_RAISE);
  2309. }
  2310. STATIC void emit_native_yield(emit_t *emit, int kind) {
  2311. // Note: 1 (yield) or 3 (yield from) labels are reserved for this function, starting at *emit->label_slot
  2312. if (emit->do_viper_types) {
  2313. mp_raise_NotImplementedError("native yield");
  2314. }
  2315. emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
  2316. need_stack_settled(emit);
  2317. if (kind == MP_EMIT_YIELD_FROM) {
  2318. // Top of yield-from loop, conceptually implementing:
  2319. // for item in generator:
  2320. // yield item
  2321. // Jump to start of loop
  2322. emit_native_jump(emit, *emit->label_slot + 2);
  2323. // Label for top of loop
  2324. emit_native_label_assign(emit, *emit->label_slot + 1);
  2325. }
  2326. // Save pointer to current stack position for caller to access yielded value
  2327. emit_get_stack_pointer_to_reg_for_pop(emit, REG_TEMP0, 1);
  2328. emit_native_mov_state_reg(emit, offsetof(mp_code_state_t, sp) / sizeof(uintptr_t), REG_TEMP0);
  2329. // Put return type in return value slot
  2330. ASM_MOV_REG_IMM(emit->as, REG_TEMP0, MP_VM_RETURN_YIELD);
  2331. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_RET_VAL(emit), REG_TEMP0);
  2332. // Save re-entry PC
  2333. ASM_MOV_REG_PCREL(emit->as, REG_TEMP0, *emit->label_slot);
  2334. emit_native_mov_state_reg(emit, LOCAL_IDX_GEN_PC(emit), REG_TEMP0);
  2335. // Jump to exit handler
  2336. ASM_JUMP(emit->as, emit->exit_label);
  2337. // Label re-entry point
  2338. mp_asm_base_label_assign(&emit->as->base, *emit->label_slot);
  2339. // Re-open any active exception handler
  2340. if (emit->exc_stack_size > 0) {
  2341. // Find innermost active exception handler, to restore as current handler
  2342. exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
  2343. for (; e >= emit->exc_stack; --e) {
  2344. if (e->is_active) {
  2345. // Found active handler, get its PC
  2346. ASM_MOV_REG_PCREL(emit->as, REG_RET, e->label);
  2347. ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_RET);
  2348. }
  2349. }
  2350. }
  2351. emit_native_adjust_stack_size(emit, 1); // send_value
  2352. if (kind == MP_EMIT_YIELD_VALUE) {
  2353. // Check LOCAL_IDX_EXC_VAL for any injected value
  2354. ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit));
  2355. emit_call(emit, MP_F_NATIVE_RAISE);
  2356. } else {
  2357. // Label loop entry
  2358. emit_native_label_assign(emit, *emit->label_slot + 2);
  2359. // Get the next item from the delegate generator
  2360. vtype_kind_t vtype;
  2361. emit_pre_pop_reg(emit, &vtype, REG_ARG_2); // send_value
  2362. emit_access_stack(emit, 1, &vtype, REG_ARG_1); // generator
  2363. ASM_MOV_REG_LOCAL(emit->as, REG_ARG_3, LOCAL_IDX_EXC_VAL(emit)); // throw_value
  2364. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_ARG_3);
  2365. emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 1); // ret_value
  2366. emit_call(emit, MP_F_NATIVE_YIELD_FROM);
  2367. // If returned non-zero then generator continues
  2368. ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, *emit->label_slot + 1, true);
  2369. // Pop exhausted gen, replace with ret_value
  2370. emit_native_adjust_stack_size(emit, 1); // ret_value
  2371. emit_fold_stack_top(emit, REG_ARG_1);
  2372. }
  2373. }
  2374. STATIC void emit_native_start_except_handler(emit_t *emit) {
  2375. // Protected block has finished so leave the current exception handler
  2376. emit_native_leave_exc_stack(emit, true);
  2377. // Get and push nlr_buf.ret_val
  2378. ASM_MOV_REG_LOCAL(emit->as, REG_TEMP0, LOCAL_IDX_EXC_VAL(emit));
  2379. emit_post_push_reg(emit, VTYPE_PYOBJ, REG_TEMP0);
  2380. }
  2381. STATIC void emit_native_end_except_handler(emit_t *emit) {
  2382. adjust_stack(emit, -1); // pop the exception (end_finally didn't use it)
  2383. }
  2384. const emit_method_table_t EXPORT_FUN(method_table) = {
  2385. emit_native_start_pass,
  2386. emit_native_end_pass,
  2387. emit_native_last_emit_was_return_value,
  2388. emit_native_adjust_stack_size,
  2389. emit_native_set_source_line,
  2390. {
  2391. emit_native_load_local,
  2392. emit_native_load_global,
  2393. },
  2394. {
  2395. emit_native_store_local,
  2396. emit_native_store_global,
  2397. },
  2398. {
  2399. emit_native_delete_local,
  2400. emit_native_delete_global,
  2401. },
  2402. emit_native_label_assign,
  2403. emit_native_import,
  2404. emit_native_load_const_tok,
  2405. emit_native_load_const_small_int,
  2406. emit_native_load_const_str,
  2407. emit_native_load_const_obj,
  2408. emit_native_load_null,
  2409. emit_native_load_method,
  2410. emit_native_load_build_class,
  2411. emit_native_subscr,
  2412. emit_native_attr,
  2413. emit_native_dup_top,
  2414. emit_native_dup_top_two,
  2415. emit_native_pop_top,
  2416. emit_native_rot_two,
  2417. emit_native_rot_three,
  2418. emit_native_jump,
  2419. emit_native_pop_jump_if,
  2420. emit_native_jump_if_or_pop,
  2421. emit_native_unwind_jump,
  2422. emit_native_setup_block,
  2423. emit_native_with_cleanup,
  2424. emit_native_end_finally,
  2425. emit_native_get_iter,
  2426. emit_native_for_iter,
  2427. emit_native_for_iter_end,
  2428. emit_native_pop_block,
  2429. emit_native_pop_except,
  2430. emit_native_unary_op,
  2431. emit_native_binary_op,
  2432. emit_native_build,
  2433. emit_native_store_map,
  2434. emit_native_store_comp,
  2435. emit_native_unpack_sequence,
  2436. emit_native_unpack_ex,
  2437. emit_native_make_function,
  2438. emit_native_make_closure,
  2439. emit_native_call_function,
  2440. emit_native_call_method,
  2441. emit_native_return_value,
  2442. emit_native_raise_varargs,
  2443. emit_native_yield,
  2444. emit_native_start_except_handler,
  2445. emit_native_end_except_handler,
  2446. };
  2447. #endif