emitnative.c 93 KB

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