emitnative.c 125 KB

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