emitnative.c 95 KB

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