emitnative.c 95 KB

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