emitnative.c 122 KB

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