codegen.rs 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609
  1. use cpu::cpu::{
  2. tlb_data, FLAG_CARRY, FLAG_OVERFLOW, FLAG_SIGN, FLAG_ZERO, OPSIZE_8, OPSIZE_16, OPSIZE_32,
  3. TLB_GLOBAL, TLB_HAS_CODE, TLB_NO_USER, TLB_READONLY, TLB_VALID,
  4. };
  5. use cpu::global_pointers;
  6. use cpu::memory;
  7. use jit::{Instruction, InstructionOperand, InstructionOperandDest, JitContext};
  8. use modrm;
  9. use modrm::ModrmByte;
  10. use profiler;
  11. use regs;
  12. use wasmgen::wasm_builder::{WasmBuilder, WasmLocal, WasmLocalI64};
  13. pub fn gen_add_cs_offset(ctx: &mut JitContext) {
  14. if !ctx.cpu.has_flat_segmentation() {
  15. ctx.builder
  16. .load_fixed_i32(global_pointers::get_seg_offset(regs::CS));
  17. ctx.builder.add_i32();
  18. }
  19. }
  20. pub fn gen_get_eip(builder: &mut WasmBuilder) {
  21. builder.load_fixed_i32(global_pointers::instruction_pointer as u32);
  22. }
  23. pub fn gen_set_eip_to_after_current_instruction(ctx: &mut JitContext) {
  24. ctx.builder
  25. .const_i32(global_pointers::instruction_pointer as i32);
  26. gen_get_eip(ctx.builder);
  27. ctx.builder.const_i32(!0xFFF);
  28. ctx.builder.and_i32();
  29. ctx.builder.const_i32(ctx.cpu.eip as i32 & 0xFFF);
  30. ctx.builder.or_i32();
  31. ctx.builder.store_aligned_i32(0);
  32. }
  33. pub fn gen_set_previous_eip_offset_from_eip_with_low_bits(
  34. builder: &mut WasmBuilder,
  35. low_bits: i32,
  36. ) {
  37. // previous_ip = instruction_pointer & ~0xFFF | low_bits;
  38. dbg_assert!(low_bits & !0xFFF == 0);
  39. builder.const_i32(global_pointers::previous_ip as i32);
  40. gen_get_eip(builder);
  41. builder.const_i32(!0xFFF);
  42. builder.and_i32();
  43. builder.const_i32(low_bits);
  44. builder.or_i32();
  45. builder.store_aligned_i32(0);
  46. }
  47. pub fn gen_set_eip_low_bits(builder: &mut WasmBuilder, low_bits: i32) {
  48. // instruction_pointer = instruction_pointer & ~0xFFF | low_bits;
  49. dbg_assert!(low_bits & !0xFFF == 0);
  50. builder.const_i32(global_pointers::instruction_pointer as i32);
  51. gen_get_eip(builder);
  52. builder.const_i32(!0xFFF);
  53. builder.and_i32();
  54. builder.const_i32(low_bits);
  55. builder.or_i32();
  56. builder.store_aligned_i32(0);
  57. }
  58. pub fn gen_set_eip_low_bits_and_jump_rel32(builder: &mut WasmBuilder, low_bits: i32, n: i32) {
  59. // instruction_pointer = (instruction_pointer & ~0xFFF | low_bits) + n;
  60. dbg_assert!(low_bits & !0xFFF == 0);
  61. builder.const_i32(global_pointers::instruction_pointer as i32);
  62. gen_get_eip(builder);
  63. builder.const_i32(!0xFFF);
  64. builder.and_i32();
  65. builder.const_i32(low_bits);
  66. builder.or_i32();
  67. if n != 0 {
  68. builder.const_i32(n);
  69. builder.add_i32();
  70. }
  71. builder.store_aligned_i32(0);
  72. }
  73. pub fn gen_relative_jump(builder: &mut WasmBuilder, n: i32) {
  74. // add n to instruction_pointer
  75. if n != 0 {
  76. builder.const_i32(global_pointers::instruction_pointer as i32);
  77. gen_get_eip(builder);
  78. builder.const_i32(n);
  79. builder.add_i32();
  80. builder.store_aligned_i32(0);
  81. }
  82. }
  83. pub fn gen_page_switch_check(
  84. ctx: &mut JitContext,
  85. next_block_addr: u32,
  86. last_instruction_addr: u32,
  87. ) {
  88. // After switching a page while in jitted code, check if the page mapping still holds
  89. gen_get_eip(ctx.builder);
  90. let address_local = ctx.builder.set_new_local();
  91. gen_get_phys_eip_plus_mem(ctx, &address_local);
  92. ctx.builder.free_local(address_local);
  93. ctx.builder
  94. .const_i32(next_block_addr as i32 + unsafe { memory::mem8 } as i32);
  95. ctx.builder.ne_i32();
  96. if cfg!(debug_assertions) {
  97. ctx.builder.if_void();
  98. gen_profiler_stat_increment(ctx.builder, profiler::stat::FAILED_PAGE_CHANGE);
  99. gen_debug_track_jit_exit(ctx.builder, last_instruction_addr);
  100. ctx.builder.br(ctx.exit_label);
  101. ctx.builder.block_end();
  102. }
  103. else {
  104. ctx.builder.br_if(ctx.exit_label);
  105. }
  106. }
  107. pub fn gen_update_instruction_counter(ctx: &mut JitContext) {
  108. ctx.builder
  109. .const_i32(global_pointers::instruction_counter as i32);
  110. ctx.builder
  111. .load_fixed_i32(global_pointers::instruction_counter as u32);
  112. ctx.builder.get_local(&ctx.instruction_counter);
  113. ctx.builder.add_i32();
  114. ctx.builder.store_aligned_i32(0);
  115. }
  116. pub fn gen_get_reg8(ctx: &mut JitContext, r: u32) {
  117. match r {
  118. regs::AL | regs::CL | regs::DL | regs::BL => {
  119. ctx.builder.get_local(&ctx.register_locals[r as usize]);
  120. ctx.builder.const_i32(0xFF);
  121. ctx.builder.and_i32();
  122. },
  123. regs::AH | regs::CH | regs::DH | regs::BH => {
  124. ctx.builder
  125. .get_local(&ctx.register_locals[(r - 4) as usize]);
  126. ctx.builder.const_i32(8);
  127. ctx.builder.shr_u_i32();
  128. ctx.builder.const_i32(0xFF);
  129. ctx.builder.and_i32();
  130. },
  131. _ => assert!(false),
  132. }
  133. }
  134. /// Return a new local referencing one of the 8 bit registers or a direct reference to one of the
  135. /// register locals. Higher bits might be garbage (suitable for gen_cmp8 etc.). Must be freed with
  136. /// gen_free_reg8_or_alias.
  137. pub fn gen_get_reg8_or_alias_to_reg32(ctx: &mut JitContext, r: u32) -> WasmLocal {
  138. match r {
  139. regs::AL | regs::CL | regs::DL | regs::BL => ctx.register_locals[r as usize].unsafe_clone(),
  140. regs::AH | regs::CH | regs::DH | regs::BH => {
  141. ctx.builder
  142. .get_local(&ctx.register_locals[(r - 4) as usize]);
  143. ctx.builder.const_i32(8);
  144. ctx.builder.shr_u_i32();
  145. ctx.builder.set_new_local()
  146. },
  147. _ => panic!(),
  148. }
  149. }
  150. pub fn gen_free_reg8_or_alias(ctx: &mut JitContext, r: u32, local: WasmLocal) {
  151. match r {
  152. regs::AL | regs::CL | regs::DL | regs::BL => {},
  153. regs::AH | regs::CH | regs::DH | regs::BH => ctx.builder.free_local(local),
  154. _ => panic!(),
  155. }
  156. }
  157. pub fn gen_get_reg16(ctx: &mut JitContext, r: u32) {
  158. ctx.builder.get_local(&ctx.register_locals[r as usize]);
  159. ctx.builder.const_i32(0xFFFF);
  160. ctx.builder.and_i32();
  161. }
  162. pub fn gen_get_reg32(ctx: &mut JitContext, r: u32) {
  163. ctx.builder.get_local(&ctx.register_locals[r as usize]);
  164. }
  165. pub fn gen_set_reg8(ctx: &mut JitContext, r: u32) {
  166. match r {
  167. regs::AL | regs::CL | regs::DL | regs::BL => {
  168. // reg32[r] = stack_value & 0xFF | reg32[r] & ~0xFF
  169. ctx.builder.const_i32(0xFF);
  170. ctx.builder.and_i32();
  171. ctx.builder.get_local(&ctx.register_locals[r as usize]);
  172. ctx.builder.const_i32(!0xFF);
  173. ctx.builder.and_i32();
  174. ctx.builder.or_i32();
  175. ctx.builder.set_local(&ctx.register_locals[r as usize]);
  176. },
  177. regs::AH | regs::CH | regs::DH | regs::BH => {
  178. // reg32[r] = stack_value << 8 & 0xFF00 | reg32[r] & ~0xFF00
  179. ctx.builder.const_i32(8);
  180. ctx.builder.shl_i32();
  181. ctx.builder.const_i32(0xFF00);
  182. ctx.builder.and_i32();
  183. ctx.builder
  184. .get_local(&ctx.register_locals[(r - 4) as usize]);
  185. ctx.builder.const_i32(!0xFF00);
  186. ctx.builder.and_i32();
  187. ctx.builder.or_i32();
  188. ctx.builder
  189. .set_local(&ctx.register_locals[(r - 4) as usize]);
  190. },
  191. _ => assert!(false),
  192. }
  193. }
  194. pub fn gen_set_reg8_unmasked(ctx: &mut JitContext, r: u32) {
  195. if cfg!(debug_assertions) {
  196. let val = ctx.builder.set_new_local();
  197. ctx.builder.get_local(&val);
  198. ctx.builder.const_i32(!0xFF);
  199. ctx.builder.and_i32();
  200. ctx.builder.if_void();
  201. ctx.builder.unreachable();
  202. ctx.builder.block_end();
  203. ctx.builder.get_local(&val);
  204. ctx.builder.free_local(val);
  205. }
  206. match r {
  207. regs::AL | regs::CL | regs::DL | regs::BL => {
  208. // reg32[r] = stack_value | reg32[r] & ~0xFF
  209. ctx.builder.get_local(&ctx.register_locals[r as usize]);
  210. ctx.builder.const_i32(!0xFF);
  211. ctx.builder.and_i32();
  212. ctx.builder.or_i32();
  213. ctx.builder.set_local(&ctx.register_locals[r as usize]);
  214. },
  215. regs::AH | regs::CH | regs::DH | regs::BH => {
  216. // reg32[r] = stack_value << 8 | reg32[r] & ~0xFF00
  217. ctx.builder.const_i32(8);
  218. ctx.builder.shl_i32();
  219. ctx.builder.const_i32(0xFF00);
  220. ctx.builder.and_i32();
  221. ctx.builder
  222. .get_local(&ctx.register_locals[(r - 4) as usize]);
  223. ctx.builder.const_i32(!0xFF00);
  224. ctx.builder.and_i32();
  225. ctx.builder.or_i32();
  226. ctx.builder
  227. .set_local(&ctx.register_locals[(r - 4) as usize]);
  228. },
  229. _ => assert!(false),
  230. }
  231. }
  232. pub fn gen_set_reg16(ctx: &mut JitContext, r: u32) {
  233. gen_set_reg16_local(ctx.builder, &ctx.register_locals[r as usize]);
  234. }
  235. pub fn gen_set_reg16_unmasked(ctx: &mut JitContext, r: u32) {
  236. if cfg!(debug_assertions) {
  237. let val = ctx.builder.set_new_local();
  238. ctx.builder.get_local(&val);
  239. ctx.builder.const_i32(!0xFFFF);
  240. ctx.builder.and_i32();
  241. ctx.builder.if_void();
  242. ctx.builder.unreachable();
  243. ctx.builder.block_end();
  244. ctx.builder.get_local(&val);
  245. ctx.builder.free_local(val);
  246. }
  247. ctx.builder.get_local(&ctx.reg(r));
  248. ctx.builder.const_i32(!0xFFFF);
  249. ctx.builder.and_i32();
  250. ctx.builder.or_i32();
  251. ctx.builder.set_local(&ctx.reg(r));
  252. }
  253. pub fn gen_set_reg16_local(builder: &mut WasmBuilder, local: &WasmLocal) {
  254. // reg32[r] = v & 0xFFFF | reg32[r] & ~0xFFFF
  255. builder.const_i32(0xFFFF);
  256. builder.and_i32();
  257. builder.get_local(local);
  258. builder.const_i32(!0xFFFF);
  259. builder.and_i32();
  260. builder.or_i32();
  261. builder.set_local(local);
  262. }
  263. pub fn gen_set_reg32(ctx: &mut JitContext, r: u32) {
  264. ctx.builder.set_local(&ctx.register_locals[r as usize]);
  265. }
  266. pub fn decr_exc_asize(ctx: &mut JitContext) {
  267. gen_get_reg32(ctx, regs::ECX);
  268. ctx.builder.const_i32(1);
  269. ctx.builder.sub_i32();
  270. if ctx.cpu.asize_32() {
  271. gen_set_reg32(ctx, regs::ECX);
  272. }
  273. else {
  274. gen_set_reg16(ctx, regs::CX);
  275. }
  276. }
  277. pub fn gen_read_reg_xmm128_into_scratch(ctx: &mut JitContext, r: u32) {
  278. ctx.builder
  279. .const_i32(global_pointers::sse_scratch_register as i32);
  280. let dest = global_pointers::get_reg_xmm_offset(r);
  281. ctx.builder.const_i32(dest as i32);
  282. ctx.builder.load_aligned_i64(0);
  283. ctx.builder.store_aligned_i64(0);
  284. ctx.builder
  285. .const_i32(global_pointers::sse_scratch_register as i32 + 8);
  286. let dest = global_pointers::get_reg_xmm_offset(r) + 8;
  287. ctx.builder.const_i32(dest as i32);
  288. ctx.builder.load_aligned_i64(0);
  289. ctx.builder.store_aligned_i64(0);
  290. }
  291. pub fn gen_get_sreg(ctx: &mut JitContext, r: u32) {
  292. ctx.builder
  293. .load_fixed_u16(global_pointers::get_sreg_offset(r))
  294. }
  295. pub fn gen_get_ss_offset(ctx: &mut JitContext) {
  296. ctx.builder
  297. .load_fixed_i32(global_pointers::get_seg_offset(regs::SS));
  298. }
  299. pub fn gen_get_flags(builder: &mut WasmBuilder) {
  300. builder.load_fixed_i32(global_pointers::flags as u32);
  301. }
  302. fn gen_get_flags_changed(builder: &mut WasmBuilder) {
  303. builder.load_fixed_i32(global_pointers::flags_changed as u32);
  304. }
  305. fn gen_get_last_result(builder: &mut WasmBuilder, previous_instruction: &Instruction) {
  306. match previous_instruction {
  307. Instruction::Add {
  308. dest: InstructionOperandDest::WasmLocal(l),
  309. opsize: OPSIZE_32,
  310. ..
  311. }
  312. | Instruction::AdcSbb {
  313. dest: InstructionOperandDest::WasmLocal(l),
  314. opsize: OPSIZE_32,
  315. ..
  316. }
  317. | Instruction::Sub {
  318. dest: InstructionOperandDest::WasmLocal(l),
  319. opsize: OPSIZE_32,
  320. ..
  321. }
  322. | Instruction::Bitwise {
  323. dest: InstructionOperandDest::WasmLocal(l),
  324. opsize: OPSIZE_32,
  325. }
  326. | Instruction::NonZeroShift {
  327. dest: InstructionOperandDest::WasmLocal(l),
  328. opsize: OPSIZE_32,
  329. } => builder.get_local(&l),
  330. Instruction::Cmp {
  331. dest: InstructionOperandDest::WasmLocal(l),
  332. source,
  333. opsize: OPSIZE_32,
  334. } => {
  335. if source.is_zero() {
  336. builder.get_local(&l)
  337. }
  338. else {
  339. builder.load_fixed_i32(global_pointers::last_result as u32)
  340. }
  341. },
  342. _ => builder.load_fixed_i32(global_pointers::last_result as u32),
  343. }
  344. }
  345. fn gen_get_last_op_size(builder: &mut WasmBuilder) {
  346. builder.load_fixed_i32(global_pointers::last_op_size as u32);
  347. }
  348. fn gen_get_last_op1(builder: &mut WasmBuilder, previous_instruction: &Instruction) {
  349. match previous_instruction {
  350. Instruction::Cmp {
  351. dest: InstructionOperandDest::WasmLocal(l),
  352. source: _,
  353. opsize: OPSIZE_32,
  354. } => builder.get_local(&l),
  355. _ => builder.load_fixed_i32(global_pointers::last_op1 as u32),
  356. }
  357. }
  358. pub fn gen_get_page_fault(builder: &mut WasmBuilder) {
  359. builder.load_fixed_u8(global_pointers::page_fault as u32);
  360. }
  361. /// sign-extend a byte value on the stack and leave it on the stack
  362. pub fn sign_extend_i8(builder: &mut WasmBuilder) {
  363. builder.const_i32(24);
  364. builder.shl_i32();
  365. builder.const_i32(24);
  366. builder.shr_s_i32();
  367. }
  368. /// sign-extend a two byte value on the stack and leave it on the stack
  369. pub fn sign_extend_i16(builder: &mut WasmBuilder) {
  370. builder.const_i32(16);
  371. builder.shl_i32();
  372. builder.const_i32(16);
  373. builder.shr_s_i32();
  374. }
  375. pub fn gen_fn0_const(builder: &mut WasmBuilder, name: &str) { builder.call_fn0(name) }
  376. pub fn gen_fn1_const(builder: &mut WasmBuilder, name: &str, arg0: u32) {
  377. builder.const_i32(arg0 as i32);
  378. builder.call_fn1(name);
  379. }
  380. pub fn gen_fn2_const(builder: &mut WasmBuilder, name: &str, arg0: u32, arg1: u32) {
  381. builder.const_i32(arg0 as i32);
  382. builder.const_i32(arg1 as i32);
  383. builder.call_fn2(name);
  384. }
  385. pub fn gen_fn3_const(builder: &mut WasmBuilder, name: &str, arg0: u32, arg1: u32, arg2: u32) {
  386. builder.const_i32(arg0 as i32);
  387. builder.const_i32(arg1 as i32);
  388. builder.const_i32(arg2 as i32);
  389. builder.call_fn3(name);
  390. }
  391. // helper functions for gen/generate_jit.js
  392. pub fn gen_modrm_fn0(builder: &mut WasmBuilder, name: &str) {
  393. // generates: fn( _ )
  394. builder.call_fn1(name);
  395. }
  396. pub fn gen_modrm_fn1(builder: &mut WasmBuilder, name: &str, arg0: u32) {
  397. // generates: fn( _, arg0 )
  398. builder.const_i32(arg0 as i32);
  399. builder.call_fn2(name);
  400. }
  401. pub fn gen_modrm_fn2(builder: &mut WasmBuilder, name: &str, arg0: u32, arg1: u32) {
  402. // generates: fn( _, arg0, arg1 )
  403. builder.const_i32(arg0 as i32);
  404. builder.const_i32(arg1 as i32);
  405. builder.call_fn3(name);
  406. }
  407. pub fn gen_modrm_resolve(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  408. modrm::gen(ctx, modrm_byte)
  409. }
  410. pub fn gen_modrm_resolve_with_local(
  411. ctx: &mut JitContext,
  412. modrm_byte: ModrmByte,
  413. gen: &dyn Fn(&mut JitContext, &WasmLocal),
  414. ) {
  415. if let Some(r) = modrm::get_as_reg_index_if_possible(ctx, &modrm_byte) {
  416. gen(ctx, &ctx.reg(r));
  417. }
  418. else {
  419. gen_modrm_resolve(ctx, modrm_byte);
  420. let address = ctx.builder.set_new_local();
  421. gen(ctx, &address);
  422. ctx.builder.free_local(address);
  423. }
  424. }
  425. pub fn gen_set_reg8_r(ctx: &mut JitContext, dest: u32, src: u32) {
  426. // generates: reg8[r_dest] = reg8[r_src]
  427. if src != dest {
  428. gen_get_reg8(ctx, src);
  429. gen_set_reg8_unmasked(ctx, dest);
  430. }
  431. }
  432. pub fn gen_set_reg16_r(ctx: &mut JitContext, dest: u32, src: u32) {
  433. // generates: reg16[r_dest] = reg16[r_src]
  434. if src != dest {
  435. gen_get_reg16(ctx, src);
  436. gen_set_reg16_unmasked(ctx, dest);
  437. }
  438. }
  439. pub fn gen_set_reg32_r(ctx: &mut JitContext, dest: u32, src: u32) {
  440. // generates: reg32[r_dest] = reg32[r_src]
  441. if src != dest {
  442. gen_get_reg32(ctx, src);
  443. gen_set_reg32(ctx, dest);
  444. }
  445. }
  446. pub fn gen_modrm_resolve_safe_read8(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  447. gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read8(ctx, addr));
  448. }
  449. pub fn gen_modrm_resolve_safe_read16(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  450. gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read16(ctx, addr));
  451. }
  452. pub fn gen_modrm_resolve_safe_read32(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  453. gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read32(ctx, addr));
  454. }
  455. pub fn gen_modrm_resolve_safe_read64(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  456. gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read64(ctx, addr));
  457. }
  458. pub fn gen_modrm_resolve_safe_read128(
  459. ctx: &mut JitContext,
  460. modrm_byte: ModrmByte,
  461. where_to_write: u32,
  462. ) {
  463. gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {
  464. gen_safe_read128(ctx, addr, where_to_write)
  465. });
  466. }
  467. pub fn gen_safe_read8(ctx: &mut JitContext, address_local: &WasmLocal) {
  468. gen_safe_read(ctx, BitSize::BYTE, address_local, None);
  469. }
  470. pub fn gen_safe_read16(ctx: &mut JitContext, address_local: &WasmLocal) {
  471. gen_safe_read(ctx, BitSize::WORD, address_local, None);
  472. }
  473. pub fn gen_safe_read32(ctx: &mut JitContext, address_local: &WasmLocal) {
  474. gen_safe_read(ctx, BitSize::DWORD, address_local, None);
  475. }
  476. pub fn gen_safe_read64(ctx: &mut JitContext, address_local: &WasmLocal) {
  477. gen_safe_read(ctx, BitSize::QWORD, &address_local, None);
  478. }
  479. pub fn gen_safe_read128(ctx: &mut JitContext, address_local: &WasmLocal, where_to_write: u32) {
  480. gen_safe_read(ctx, BitSize::DQWORD, &address_local, Some(where_to_write));
  481. }
  482. // only used internally for gen_safe_write
  483. enum GenSafeWriteValue<'a> {
  484. I32(&'a WasmLocal),
  485. I64(&'a WasmLocalI64),
  486. TwoI64s(&'a WasmLocalI64, &'a WasmLocalI64),
  487. }
  488. enum GenSafeReadWriteValue {
  489. I32(WasmLocal),
  490. I64(WasmLocalI64),
  491. }
  492. #[derive(Copy, Clone, Eq, PartialEq)]
  493. pub enum BitSize {
  494. BYTE,
  495. WORD,
  496. DWORD,
  497. QWORD,
  498. DQWORD,
  499. }
  500. impl BitSize {
  501. pub fn bytes(&self) -> u32 {
  502. match self {
  503. BitSize::BYTE => 1,
  504. BitSize::WORD => 2,
  505. BitSize::DWORD => 4,
  506. BitSize::QWORD => 8,
  507. BitSize::DQWORD => 16,
  508. }
  509. }
  510. }
  511. pub fn gen_safe_write8(ctx: &mut JitContext, address_local: &WasmLocal, value_local: &WasmLocal) {
  512. gen_safe_write(
  513. ctx,
  514. BitSize::BYTE,
  515. address_local,
  516. GenSafeWriteValue::I32(value_local),
  517. )
  518. }
  519. pub fn gen_safe_write16(ctx: &mut JitContext, address_local: &WasmLocal, value_local: &WasmLocal) {
  520. gen_safe_write(
  521. ctx,
  522. BitSize::WORD,
  523. address_local,
  524. GenSafeWriteValue::I32(value_local),
  525. )
  526. }
  527. pub fn gen_safe_write32(ctx: &mut JitContext, address_local: &WasmLocal, value_local: &WasmLocal) {
  528. gen_safe_write(
  529. ctx,
  530. BitSize::DWORD,
  531. address_local,
  532. GenSafeWriteValue::I32(value_local),
  533. )
  534. }
  535. pub fn gen_safe_write64(
  536. ctx: &mut JitContext,
  537. address_local: &WasmLocal,
  538. value_local: &WasmLocalI64,
  539. ) {
  540. gen_safe_write(
  541. ctx,
  542. BitSize::QWORD,
  543. address_local,
  544. GenSafeWriteValue::I64(value_local),
  545. )
  546. }
  547. pub fn gen_safe_write128(
  548. ctx: &mut JitContext,
  549. address_local: &WasmLocal,
  550. value_local_low: &WasmLocalI64,
  551. value_local_high: &WasmLocalI64,
  552. ) {
  553. gen_safe_write(
  554. ctx,
  555. BitSize::DQWORD,
  556. address_local,
  557. GenSafeWriteValue::TwoI64s(value_local_low, value_local_high),
  558. )
  559. }
  560. fn gen_safe_read(
  561. ctx: &mut JitContext,
  562. bits: BitSize,
  563. address_local: &WasmLocal,
  564. where_to_write: Option<u32>,
  565. ) {
  566. // Execute a virtual memory read. All slow paths (memory-mapped IO, tlb miss, page fault and
  567. // read across page boundary are handled in safe_read_jit_slow
  568. // entry <- tlb_data[addr >> 12 << 2]
  569. // if entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0x1000 - bytes: goto fast
  570. // entry <- safe_read_jit_slow(addr, instruction_pointer)
  571. // if page_fault: goto exit-with-pagefault
  572. // fast: mem[(entry & ~0xFFF) ^ addr]
  573. let cont = ctx.builder.block_void();
  574. ctx.builder.get_local(&address_local);
  575. ctx.builder.const_i32(12);
  576. ctx.builder.shr_u_i32();
  577. ctx.builder.const_i32(2);
  578. ctx.builder.shl_i32();
  579. ctx.builder
  580. .load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });
  581. let entry_local = ctx.builder.tee_new_local();
  582. ctx.builder.const_i32(
  583. (0xFFF
  584. & !TLB_READONLY
  585. & !TLB_GLOBAL
  586. & !TLB_HAS_CODE
  587. & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32,
  588. );
  589. ctx.builder.and_i32();
  590. ctx.builder.const_i32(TLB_VALID as i32);
  591. ctx.builder.eq_i32();
  592. if bits != BitSize::BYTE {
  593. ctx.builder.get_local(&address_local);
  594. ctx.builder.const_i32(0xFFF);
  595. ctx.builder.and_i32();
  596. ctx.builder.const_i32(0x1000 - bits.bytes() as i32);
  597. ctx.builder.le_i32();
  598. ctx.builder.and_i32();
  599. }
  600. ctx.builder.br_if(cont);
  601. if cfg!(feature = "profiler") {
  602. ctx.builder.get_local(&address_local);
  603. ctx.builder.get_local(&entry_local);
  604. ctx.builder.call_fn2("report_safe_read_jit_slow");
  605. }
  606. ctx.builder.get_local(&address_local);
  607. ctx.builder
  608. .const_i32(ctx.start_of_current_instruction as i32 & 0xFFF);
  609. match bits {
  610. BitSize::BYTE => {
  611. ctx.builder.call_fn2_ret("safe_read8_slow_jit");
  612. },
  613. BitSize::WORD => {
  614. ctx.builder.call_fn2_ret("safe_read16_slow_jit");
  615. },
  616. BitSize::DWORD => {
  617. ctx.builder.call_fn2_ret("safe_read32s_slow_jit");
  618. },
  619. BitSize::QWORD => {
  620. ctx.builder.call_fn2_ret("safe_read64s_slow_jit");
  621. },
  622. BitSize::DQWORD => {
  623. ctx.builder.call_fn2_ret("safe_read128s_slow_jit");
  624. },
  625. }
  626. ctx.builder.tee_local(&entry_local);
  627. ctx.builder.const_i32(1);
  628. ctx.builder.and_i32();
  629. if cfg!(feature = "profiler") {
  630. ctx.builder.if_void();
  631. gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
  632. ctx.builder.block_end();
  633. ctx.builder.get_local(&entry_local);
  634. ctx.builder.const_i32(1);
  635. ctx.builder.and_i32();
  636. }
  637. ctx.builder.br_if(ctx.exit_with_fault_label);
  638. ctx.builder.block_end();
  639. gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_READ_FAST); // XXX: Both fast and slow
  640. ctx.builder.get_local(&entry_local);
  641. ctx.builder.const_i32(!0xFFF);
  642. ctx.builder.and_i32();
  643. ctx.builder.get_local(&address_local);
  644. ctx.builder.xor_i32();
  645. // where_to_write is only used by dqword
  646. dbg_assert!((where_to_write != None) == (bits == BitSize::DQWORD));
  647. match bits {
  648. BitSize::BYTE => {
  649. ctx.builder.load_u8(0);
  650. },
  651. BitSize::WORD => {
  652. ctx.builder.load_unaligned_u16(0);
  653. },
  654. BitSize::DWORD => {
  655. ctx.builder.load_unaligned_i32(0);
  656. },
  657. BitSize::QWORD => {
  658. ctx.builder.load_unaligned_i64(0);
  659. },
  660. BitSize::DQWORD => {
  661. let where_to_write = where_to_write.unwrap();
  662. let virt_address_local = ctx.builder.set_new_local();
  663. ctx.builder.const_i32(0);
  664. ctx.builder.get_local(&virt_address_local);
  665. ctx.builder.load_unaligned_i64(0);
  666. ctx.builder.store_unaligned_i64(where_to_write);
  667. ctx.builder.const_i32(0);
  668. ctx.builder.get_local(&virt_address_local);
  669. ctx.builder.load_unaligned_i64(8);
  670. ctx.builder.store_unaligned_i64(where_to_write + 8);
  671. ctx.builder.free_local(virt_address_local);
  672. },
  673. }
  674. ctx.builder.free_local(entry_local);
  675. }
  676. pub fn gen_get_phys_eip_plus_mem(ctx: &mut JitContext, address_local: &WasmLocal) {
  677. // Similar to gen_safe_read, but return the physical eip + memory::mem rather than reading from memory
  678. // In functions that need to use this value we need to fix it by substracting memory::mem
  679. // this is done in order to remove one instruction from the fast path of memory accesses (no need to add
  680. // memory::mem anymore ).
  681. // We need to account for this in gen_page_switch_check and we compare with next_block_addr + memory::mem8
  682. // We cannot the same while processing an AbsoluteEip flow control change so there we need to fix the value
  683. // by subscracting memory::mem. Overall, since AbsoluteEip is encountered less often than memory accesses so
  684. // this ends up improving perf.
  685. // Does not (need to) handle mapped memory
  686. // XXX: Currently does not use ctx.start_of_current_instruction, but rather assumes that eip is
  687. // already correct (pointing at the current instruction)
  688. let cont = ctx.builder.block_void();
  689. ctx.builder.get_local(&address_local);
  690. ctx.builder.const_i32(12);
  691. ctx.builder.shr_u_i32();
  692. ctx.builder.const_i32(2);
  693. ctx.builder.shl_i32();
  694. ctx.builder
  695. .load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });
  696. let entry_local = ctx.builder.tee_new_local();
  697. ctx.builder.const_i32(
  698. (0xFFF
  699. & !TLB_READONLY
  700. & !TLB_GLOBAL
  701. & !TLB_HAS_CODE
  702. & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32,
  703. );
  704. ctx.builder.and_i32();
  705. ctx.builder.const_i32(TLB_VALID as i32);
  706. ctx.builder.eq_i32();
  707. ctx.builder.br_if(cont);
  708. if cfg!(feature = "profiler") {
  709. ctx.builder.get_local(&address_local);
  710. ctx.builder.get_local(&entry_local);
  711. ctx.builder.call_fn2("report_safe_read_jit_slow");
  712. }
  713. ctx.builder.get_local(&address_local);
  714. ctx.builder.call_fn1_ret("get_phys_eip_slow_jit");
  715. ctx.builder.tee_local(&entry_local);
  716. ctx.builder.const_i32(1);
  717. ctx.builder.and_i32();
  718. if cfg!(feature = "profiler") {
  719. ctx.builder.if_void();
  720. gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction); // XXX
  721. ctx.builder.block_end();
  722. ctx.builder.get_local(&entry_local);
  723. ctx.builder.const_i32(1);
  724. ctx.builder.and_i32();
  725. }
  726. ctx.builder.br_if(ctx.exit_with_fault_label);
  727. ctx.builder.block_end();
  728. gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_READ_FAST); // XXX: Both fast and slow
  729. ctx.builder.get_local(&entry_local);
  730. ctx.builder.const_i32(!0xFFF);
  731. ctx.builder.and_i32();
  732. ctx.builder.get_local(&address_local);
  733. ctx.builder.xor_i32();
  734. ctx.builder.free_local(entry_local);
  735. }
  736. fn gen_safe_write(
  737. ctx: &mut JitContext,
  738. bits: BitSize,
  739. address_local: &WasmLocal,
  740. value_local: GenSafeWriteValue,
  741. ) {
  742. // Execute a virtual memory write. All slow paths (memory-mapped IO, tlb miss, page fault,
  743. // write across page boundary and page containing jitted code are handled in safe_write_jit_slow
  744. // entry <- tlb_data[addr >> 12 << 2]
  745. // if entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0x1000 - bytes: goto fast
  746. // entry <- safe_write_jit_slow(addr, value, instruction_pointer)
  747. // if page_fault: goto exit-with-pagefault
  748. // fast: mem[(entry & ~0xFFF) ^ addr] <- value
  749. let cont = ctx.builder.block_void();
  750. ctx.builder.get_local(&address_local);
  751. ctx.builder.const_i32(12);
  752. ctx.builder.shr_u_i32();
  753. ctx.builder.const_i32(2);
  754. ctx.builder.shl_i32();
  755. ctx.builder
  756. .load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });
  757. let entry_local = ctx.builder.tee_new_local();
  758. ctx.builder
  759. .const_i32((0xFFF & !TLB_GLOBAL & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32);
  760. ctx.builder.and_i32();
  761. ctx.builder.const_i32(TLB_VALID as i32);
  762. ctx.builder.eq_i32();
  763. if bits != BitSize::BYTE {
  764. ctx.builder.get_local(&address_local);
  765. ctx.builder.const_i32(0xFFF);
  766. ctx.builder.and_i32();
  767. ctx.builder.const_i32(0x1000 - bits.bytes() as i32);
  768. ctx.builder.le_i32();
  769. ctx.builder.and_i32();
  770. }
  771. ctx.builder.br_if(cont);
  772. if cfg!(feature = "profiler") {
  773. ctx.builder.get_local(&address_local);
  774. ctx.builder.get_local(&entry_local);
  775. ctx.builder.call_fn2("report_safe_write_jit_slow");
  776. }
  777. ctx.builder.get_local(&address_local);
  778. match value_local {
  779. GenSafeWriteValue::I32(local) => ctx.builder.get_local(local),
  780. GenSafeWriteValue::I64(local) => ctx.builder.get_local_i64(local),
  781. GenSafeWriteValue::TwoI64s(local1, local2) => {
  782. ctx.builder.get_local_i64(local1);
  783. ctx.builder.get_local_i64(local2)
  784. },
  785. }
  786. ctx.builder
  787. .const_i32(ctx.start_of_current_instruction as i32 & 0xFFF);
  788. match bits {
  789. BitSize::BYTE => {
  790. ctx.builder.call_fn3_ret("safe_write8_slow_jit");
  791. },
  792. BitSize::WORD => {
  793. ctx.builder.call_fn3_ret("safe_write16_slow_jit");
  794. },
  795. BitSize::DWORD => {
  796. ctx.builder.call_fn3_ret("safe_write32_slow_jit");
  797. },
  798. BitSize::QWORD => {
  799. ctx.builder
  800. .call_fn3_i32_i64_i32_ret("safe_write64_slow_jit");
  801. },
  802. BitSize::DQWORD => {
  803. ctx.builder
  804. .call_fn4_i32_i64_i64_i32_ret("safe_write128_slow_jit");
  805. },
  806. }
  807. ctx.builder.tee_local(&entry_local);
  808. ctx.builder.const_i32(1);
  809. ctx.builder.and_i32();
  810. if cfg!(feature = "profiler") {
  811. ctx.builder.if_void();
  812. gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
  813. ctx.builder.block_end();
  814. ctx.builder.get_local(&entry_local);
  815. ctx.builder.const_i32(1);
  816. ctx.builder.and_i32();
  817. }
  818. ctx.builder.br_if(ctx.exit_with_fault_label);
  819. ctx.builder.block_end();
  820. gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_WRITE_FAST); // XXX: Both fast and slow
  821. ctx.builder.get_local(&entry_local);
  822. ctx.builder.const_i32(!0xFFF);
  823. ctx.builder.and_i32();
  824. ctx.builder.get_local(&address_local);
  825. ctx.builder.xor_i32();
  826. match value_local {
  827. GenSafeWriteValue::I32(local) => ctx.builder.get_local(local),
  828. GenSafeWriteValue::I64(local) => ctx.builder.get_local_i64(local),
  829. GenSafeWriteValue::TwoI64s(local1, local2) => {
  830. assert!(bits == BitSize::DQWORD);
  831. let virt_address_local = ctx.builder.tee_new_local();
  832. ctx.builder.get_local_i64(local1);
  833. ctx.builder.store_unaligned_i64(0);
  834. ctx.builder.get_local(&virt_address_local);
  835. ctx.builder.get_local_i64(local2);
  836. ctx.builder.store_unaligned_i64(8);
  837. ctx.builder.free_local(virt_address_local);
  838. },
  839. }
  840. match bits {
  841. BitSize::BYTE => {
  842. ctx.builder.store_u8(0);
  843. },
  844. BitSize::WORD => {
  845. ctx.builder.store_unaligned_u16(0);
  846. },
  847. BitSize::DWORD => {
  848. ctx.builder.store_unaligned_i32(0);
  849. },
  850. BitSize::QWORD => {
  851. ctx.builder.store_unaligned_i64(0);
  852. },
  853. BitSize::DQWORD => {}, // handled above
  854. }
  855. ctx.builder.free_local(entry_local);
  856. }
  857. pub fn gen_safe_read_write(
  858. ctx: &mut JitContext,
  859. bits: BitSize,
  860. address_local: &WasmLocal,
  861. f: &dyn Fn(&mut JitContext),
  862. ) {
  863. // Execute a virtual memory read+write. All slow paths (memory-mapped IO, tlb miss, page fault,
  864. // write across page boundary and page containing jitted code are handled in
  865. // safe_read_write_jit_slow
  866. // entry <- tlb_data[addr >> 12 << 2]
  867. // can_use_fast_path <- entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0x1000 - bytes
  868. // if can_use_fast_path: goto fast
  869. // entry <- safe_read_write_jit_slow(addr, instruction_pointer)
  870. // if page_fault: goto exit-with-pagefault
  871. // fast: value <- f(mem[(entry & ~0xFFF) ^ addr])
  872. // if !can_use_fast_path { safe_write_jit_slow(addr, value, instruction_pointer) }
  873. // mem[(entry & ~0xFFF) ^ addr] <- value
  874. let cont = ctx.builder.block_void();
  875. ctx.builder.get_local(address_local);
  876. ctx.builder.const_i32(12);
  877. ctx.builder.shr_u_i32();
  878. ctx.builder.const_i32(2);
  879. ctx.builder.shl_i32();
  880. ctx.builder
  881. .load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });
  882. let entry_local = ctx.builder.tee_new_local();
  883. ctx.builder
  884. .const_i32((0xFFF & !TLB_GLOBAL & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32);
  885. ctx.builder.and_i32();
  886. ctx.builder.const_i32(TLB_VALID as i32);
  887. ctx.builder.eq_i32();
  888. if bits != BitSize::BYTE {
  889. ctx.builder.get_local(&address_local);
  890. ctx.builder.const_i32(0xFFF);
  891. ctx.builder.and_i32();
  892. ctx.builder.const_i32(0x1000 - bits.bytes() as i32);
  893. ctx.builder.le_i32();
  894. ctx.builder.and_i32();
  895. }
  896. let can_use_fast_path_local = ctx.builder.tee_new_local();
  897. ctx.builder.br_if(cont);
  898. if cfg!(feature = "profiler") {
  899. ctx.builder.get_local(&address_local);
  900. ctx.builder.get_local(&entry_local);
  901. ctx.builder.call_fn2("report_safe_read_write_jit_slow");
  902. }
  903. ctx.builder.get_local(&address_local);
  904. ctx.builder
  905. .const_i32(ctx.start_of_current_instruction as i32 & 0xFFF);
  906. match bits {
  907. BitSize::BYTE => {
  908. ctx.builder.call_fn2_ret("safe_read_write8_slow_jit");
  909. },
  910. BitSize::WORD => {
  911. ctx.builder.call_fn2_ret("safe_read_write16_slow_jit");
  912. },
  913. BitSize::DWORD => {
  914. ctx.builder.call_fn2_ret("safe_read_write32s_slow_jit");
  915. },
  916. BitSize::QWORD => {
  917. ctx.builder.call_fn2_ret("safe_read_write64_slow_jit");
  918. },
  919. BitSize::DQWORD => {
  920. dbg_assert!(false);
  921. },
  922. }
  923. ctx.builder.tee_local(&entry_local);
  924. ctx.builder.const_i32(1);
  925. ctx.builder.and_i32();
  926. if cfg!(feature = "profiler") {
  927. ctx.builder.if_void();
  928. gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
  929. ctx.builder.block_end();
  930. ctx.builder.get_local(&entry_local);
  931. ctx.builder.const_i32(1);
  932. ctx.builder.and_i32();
  933. }
  934. ctx.builder.br_if(ctx.exit_with_fault_label);
  935. ctx.builder.block_end();
  936. gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_READ_WRITE_FAST); // XXX: Also slow
  937. ctx.builder.get_local(&entry_local);
  938. ctx.builder.const_i32(!0xFFF);
  939. ctx.builder.and_i32();
  940. ctx.builder.get_local(&address_local);
  941. ctx.builder.xor_i32();
  942. ctx.builder.free_local(entry_local);
  943. let phys_addr_local = ctx.builder.tee_new_local();
  944. match bits {
  945. BitSize::BYTE => {
  946. ctx.builder.load_u8(0);
  947. },
  948. BitSize::WORD => {
  949. ctx.builder.load_unaligned_u16(0);
  950. },
  951. BitSize::DWORD => {
  952. ctx.builder.load_unaligned_i32(0);
  953. },
  954. BitSize::QWORD => {
  955. ctx.builder.load_unaligned_i64(0);
  956. },
  957. BitSize::DQWORD => assert!(false), // not used
  958. }
  959. // value is now on stack
  960. f(ctx);
  961. // TODO: Could get rid of this local by returning one from f
  962. let value_local = if bits == BitSize::QWORD {
  963. GenSafeReadWriteValue::I64(ctx.builder.set_new_local_i64())
  964. }
  965. else {
  966. GenSafeReadWriteValue::I32(ctx.builder.set_new_local())
  967. };
  968. ctx.builder.get_local(&can_use_fast_path_local);
  969. ctx.builder.eqz_i32();
  970. ctx.builder.if_void();
  971. {
  972. ctx.builder.get_local(&address_local);
  973. match &value_local {
  974. GenSafeReadWriteValue::I32(l) => ctx.builder.get_local(l),
  975. GenSafeReadWriteValue::I64(l) => ctx.builder.get_local_i64(l),
  976. }
  977. ctx.builder
  978. .const_i32(ctx.start_of_current_instruction as i32);
  979. match bits {
  980. BitSize::BYTE => {
  981. ctx.builder.call_fn3_ret("safe_write8_slow_jit");
  982. },
  983. BitSize::WORD => {
  984. ctx.builder.call_fn3_ret("safe_write16_slow_jit");
  985. },
  986. BitSize::DWORD => {
  987. ctx.builder.call_fn3_ret("safe_write32_slow_jit");
  988. },
  989. BitSize::QWORD => {
  990. ctx.builder
  991. .call_fn3_i32_i64_i32_ret("safe_write64_slow_jit");
  992. },
  993. BitSize::DQWORD => {
  994. dbg_assert!(false);
  995. },
  996. }
  997. if cfg!(debug_assertions) {
  998. ctx.builder.const_i32(1);
  999. ctx.builder.and_i32();
  1000. ctx.builder.if_void();
  1001. {
  1002. // handled above
  1003. ctx.builder.const_i32(match bits {
  1004. BitSize::BYTE => 8,
  1005. BitSize::WORD => 16,
  1006. BitSize::DWORD => 32,
  1007. BitSize::QWORD => 64,
  1008. _ => {
  1009. dbg_assert!(false);
  1010. 0
  1011. },
  1012. });
  1013. ctx.builder.get_local(&address_local);
  1014. ctx.builder.call_fn2("bug_gen_safe_read_write_page_fault");
  1015. }
  1016. ctx.builder.block_end();
  1017. }
  1018. else {
  1019. ctx.builder.drop_();
  1020. }
  1021. }
  1022. ctx.builder.block_end();
  1023. ctx.builder.get_local(&phys_addr_local);
  1024. match &value_local {
  1025. GenSafeReadWriteValue::I32(l) => ctx.builder.get_local(l),
  1026. GenSafeReadWriteValue::I64(l) => ctx.builder.get_local_i64(l),
  1027. }
  1028. match bits {
  1029. BitSize::BYTE => {
  1030. ctx.builder.store_u8(0);
  1031. },
  1032. BitSize::WORD => {
  1033. ctx.builder.store_unaligned_u16(0);
  1034. },
  1035. BitSize::DWORD => {
  1036. ctx.builder.store_unaligned_i32(0);
  1037. },
  1038. BitSize::QWORD => {
  1039. ctx.builder.store_unaligned_i64(0);
  1040. },
  1041. BitSize::DQWORD => {
  1042. dbg_assert!(false);
  1043. },
  1044. }
  1045. match value_local {
  1046. GenSafeReadWriteValue::I32(l) => ctx.builder.free_local(l),
  1047. GenSafeReadWriteValue::I64(l) => ctx.builder.free_local_i64(l),
  1048. }
  1049. ctx.builder.free_local(can_use_fast_path_local);
  1050. ctx.builder.free_local(phys_addr_local);
  1051. }
  1052. #[cfg(debug_assertions)]
  1053. #[no_mangle]
  1054. pub fn bug_gen_safe_read_write_page_fault(bits: i32, addr: u32) {
  1055. dbg_log!("bug: gen_safe_read_write_page_fault {} {:x}", bits, addr);
  1056. dbg_assert!(false);
  1057. }
  1058. pub fn gen_jmp_rel16(builder: &mut WasmBuilder, rel16: u16) {
  1059. let cs_offset_addr = global_pointers::get_seg_offset(regs::CS);
  1060. builder.load_fixed_i32(cs_offset_addr);
  1061. let local = builder.set_new_local();
  1062. // generate:
  1063. // *instruction_pointer = cs_offset + ((*instruction_pointer - cs_offset + rel16) & 0xFFFF);
  1064. {
  1065. builder.const_i32(global_pointers::instruction_pointer as i32);
  1066. gen_get_eip(builder);
  1067. builder.get_local(&local);
  1068. builder.sub_i32();
  1069. builder.const_i32(rel16 as i32);
  1070. builder.add_i32();
  1071. builder.const_i32(0xFFFF);
  1072. builder.and_i32();
  1073. builder.get_local(&local);
  1074. builder.add_i32();
  1075. builder.store_aligned_i32(0);
  1076. }
  1077. builder.free_local(local);
  1078. }
  1079. pub fn gen_pop16_ss16(ctx: &mut JitContext) {
  1080. // sp = segment_offsets[SS] + reg16[SP] (or just reg16[SP] if has_flat_segmentation)
  1081. gen_get_reg16(ctx, regs::SP);
  1082. if !ctx.cpu.has_flat_segmentation() {
  1083. gen_get_ss_offset(ctx);
  1084. ctx.builder.add_i32();
  1085. }
  1086. // result = safe_read16(sp)
  1087. let address_local = ctx.builder.set_new_local();
  1088. gen_safe_read16(ctx, &address_local);
  1089. ctx.builder.free_local(address_local);
  1090. // reg16[SP] += 2;
  1091. gen_get_reg16(ctx, regs::SP);
  1092. ctx.builder.const_i32(2);
  1093. ctx.builder.add_i32();
  1094. gen_set_reg16(ctx, regs::SP);
  1095. // return value is already on stack
  1096. }
  1097. pub fn gen_pop16_ss32(ctx: &mut JitContext) {
  1098. // esp = segment_offsets[SS] + reg32[ESP] (or just reg32[ESP] if has_flat_segmentation)
  1099. gen_get_reg32(ctx, regs::ESP);
  1100. if !ctx.cpu.has_flat_segmentation() {
  1101. gen_get_ss_offset(ctx);
  1102. ctx.builder.add_i32();
  1103. }
  1104. // result = safe_read16(esp)
  1105. let address_local = ctx.builder.set_new_local();
  1106. gen_safe_read16(ctx, &address_local);
  1107. ctx.builder.free_local(address_local);
  1108. // reg32[ESP] += 2;
  1109. gen_get_reg32(ctx, regs::ESP);
  1110. ctx.builder.const_i32(2);
  1111. ctx.builder.add_i32();
  1112. gen_set_reg32(ctx, regs::ESP);
  1113. // return value is already on stack
  1114. }
  1115. pub fn gen_pop16(ctx: &mut JitContext) {
  1116. if ctx.cpu.ssize_32() {
  1117. gen_pop16_ss32(ctx);
  1118. }
  1119. else {
  1120. gen_pop16_ss16(ctx);
  1121. }
  1122. }
  1123. pub fn gen_pop32s_ss16(ctx: &mut JitContext) {
  1124. // sp = reg16[SP]
  1125. gen_get_reg16(ctx, regs::SP);
  1126. // result = safe_read32s(segment_offsets[SS] + sp) (or just sp if has_flat_segmentation)
  1127. if !ctx.cpu.has_flat_segmentation() {
  1128. gen_get_ss_offset(ctx);
  1129. ctx.builder.add_i32();
  1130. }
  1131. let address_local = ctx.builder.set_new_local();
  1132. gen_safe_read32(ctx, &address_local);
  1133. ctx.builder.free_local(address_local);
  1134. // reg16[SP] = sp + 4;
  1135. gen_get_reg16(ctx, regs::SP);
  1136. ctx.builder.const_i32(4);
  1137. ctx.builder.add_i32();
  1138. gen_set_reg16(ctx, regs::SP);
  1139. // return value is already on stack
  1140. }
  1141. pub fn gen_pop32s_ss32(ctx: &mut JitContext) {
  1142. if !ctx.cpu.has_flat_segmentation() {
  1143. gen_get_reg32(ctx, regs::ESP);
  1144. gen_get_ss_offset(ctx);
  1145. ctx.builder.add_i32();
  1146. let address_local = ctx.builder.set_new_local();
  1147. gen_safe_read32(ctx, &address_local);
  1148. ctx.builder.free_local(address_local);
  1149. }
  1150. else {
  1151. let reg = ctx.register_locals[regs::ESP as usize].unsafe_clone();
  1152. gen_safe_read32(ctx, &reg);
  1153. }
  1154. gen_get_reg32(ctx, regs::ESP);
  1155. ctx.builder.const_i32(4);
  1156. ctx.builder.add_i32();
  1157. gen_set_reg32(ctx, regs::ESP);
  1158. // return value is already on stack
  1159. }
  1160. pub fn gen_pop32s(ctx: &mut JitContext) {
  1161. if ctx.cpu.ssize_32() {
  1162. gen_pop32s_ss32(ctx);
  1163. }
  1164. else {
  1165. gen_pop32s_ss16(ctx);
  1166. }
  1167. }
  1168. pub fn gen_adjust_stack_reg(ctx: &mut JitContext, offset: u32) {
  1169. if ctx.cpu.ssize_32() {
  1170. gen_get_reg32(ctx, regs::ESP);
  1171. ctx.builder.const_i32(offset as i32);
  1172. ctx.builder.add_i32();
  1173. gen_set_reg32(ctx, regs::ESP);
  1174. }
  1175. else {
  1176. gen_get_reg16(ctx, regs::SP);
  1177. ctx.builder.const_i32(offset as i32);
  1178. ctx.builder.add_i32();
  1179. gen_set_reg16(ctx, regs::SP);
  1180. }
  1181. }
  1182. pub fn gen_leave(ctx: &mut JitContext, os32: bool) {
  1183. // [e]bp = safe_read{16,32}([e]bp)
  1184. if ctx.cpu.ssize_32() {
  1185. gen_get_reg32(ctx, regs::EBP);
  1186. }
  1187. else {
  1188. gen_get_reg16(ctx, regs::BP);
  1189. }
  1190. let old_vbp = ctx.builder.tee_new_local();
  1191. if !ctx.cpu.has_flat_segmentation() {
  1192. gen_get_ss_offset(ctx);
  1193. ctx.builder.add_i32();
  1194. }
  1195. if os32 {
  1196. let address_local = ctx.builder.set_new_local();
  1197. gen_safe_read32(ctx, &address_local);
  1198. ctx.builder.free_local(address_local);
  1199. gen_set_reg32(ctx, regs::EBP);
  1200. }
  1201. else {
  1202. let address_local = ctx.builder.set_new_local();
  1203. gen_safe_read16(ctx, &address_local);
  1204. ctx.builder.free_local(address_local);
  1205. gen_set_reg16(ctx, regs::BP);
  1206. }
  1207. // [e]sp = [e]bp + (os32 ? 4 : 2)
  1208. if ctx.cpu.ssize_32() {
  1209. ctx.builder.get_local(&old_vbp);
  1210. ctx.builder.const_i32(if os32 { 4 } else { 2 });
  1211. ctx.builder.add_i32();
  1212. gen_set_reg32(ctx, regs::ESP);
  1213. }
  1214. else {
  1215. ctx.builder.get_local(&old_vbp);
  1216. ctx.builder.const_i32(if os32 { 4 } else { 2 });
  1217. ctx.builder.add_i32();
  1218. gen_set_reg16(ctx, regs::SP);
  1219. }
  1220. ctx.builder.free_local(old_vbp);
  1221. }
  1222. pub fn gen_task_switch_test(ctx: &mut JitContext) {
  1223. // generate if(cr[0] & (CR0_EM | CR0_TS)) { task_switch_test_jit(); goto exit_with_fault; }
  1224. let cr0_offset = global_pointers::get_creg_offset(0);
  1225. dbg_assert!(regs::CR0_EM | regs::CR0_TS <= 0xFF);
  1226. ctx.builder.load_fixed_u8(cr0_offset);
  1227. ctx.builder.const_i32((regs::CR0_EM | regs::CR0_TS) as i32);
  1228. ctx.builder.and_i32();
  1229. ctx.builder.if_void();
  1230. {
  1231. gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
  1232. gen_fn1_const(
  1233. ctx.builder,
  1234. "task_switch_test_jit",
  1235. ctx.start_of_current_instruction,
  1236. );
  1237. ctx.builder.br(ctx.exit_with_fault_label);
  1238. }
  1239. ctx.builder.block_end();
  1240. }
  1241. pub fn gen_task_switch_test_mmx(ctx: &mut JitContext) {
  1242. // generate if(cr[0] & (CR0_EM | CR0_TS)) { task_switch_test_mmx_jit(); goto exit_with_fault; }
  1243. let cr0_offset = global_pointers::get_creg_offset(0);
  1244. dbg_assert!(regs::CR0_EM | regs::CR0_TS <= 0xFF);
  1245. ctx.builder.load_fixed_u8(cr0_offset);
  1246. ctx.builder.const_i32((regs::CR0_EM | regs::CR0_TS) as i32);
  1247. ctx.builder.and_i32();
  1248. ctx.builder.if_void();
  1249. {
  1250. gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
  1251. gen_fn1_const(
  1252. ctx.builder,
  1253. "task_switch_test_mmx_jit",
  1254. ctx.start_of_current_instruction,
  1255. );
  1256. ctx.builder.br(ctx.exit_with_fault_label);
  1257. }
  1258. ctx.builder.block_end();
  1259. }
  1260. pub fn gen_push16(ctx: &mut JitContext, value_local: &WasmLocal) {
  1261. if ctx.cpu.ssize_32() {
  1262. gen_get_reg32(ctx, regs::ESP);
  1263. }
  1264. else {
  1265. gen_get_reg16(ctx, regs::SP);
  1266. };
  1267. ctx.builder.const_i32(2);
  1268. ctx.builder.sub_i32();
  1269. let reg_updated_local = if !ctx.cpu.ssize_32() || !ctx.cpu.has_flat_segmentation() {
  1270. let reg_updated_local = ctx.builder.tee_new_local();
  1271. if !ctx.cpu.ssize_32() {
  1272. ctx.builder.const_i32(0xFFFF);
  1273. ctx.builder.and_i32();
  1274. }
  1275. if !ctx.cpu.has_flat_segmentation() {
  1276. gen_get_ss_offset(ctx);
  1277. ctx.builder.add_i32();
  1278. }
  1279. let sp_local = ctx.builder.set_new_local();
  1280. gen_safe_write16(ctx, &sp_local, &value_local);
  1281. ctx.builder.free_local(sp_local);
  1282. ctx.builder.get_local(&reg_updated_local);
  1283. reg_updated_local
  1284. }
  1285. else {
  1286. // short path: The address written to is equal to ESP/SP minus two
  1287. let reg_updated_local = ctx.builder.tee_new_local();
  1288. gen_safe_write16(ctx, &reg_updated_local, &value_local);
  1289. reg_updated_local
  1290. };
  1291. if ctx.cpu.ssize_32() {
  1292. gen_set_reg32(ctx, regs::ESP);
  1293. }
  1294. else {
  1295. gen_set_reg16(ctx, regs::SP);
  1296. };
  1297. ctx.builder.free_local(reg_updated_local);
  1298. }
  1299. pub fn gen_push32(ctx: &mut JitContext, value_local: &WasmLocal) {
  1300. if ctx.cpu.ssize_32() {
  1301. gen_get_reg32(ctx, regs::ESP);
  1302. }
  1303. else {
  1304. gen_get_reg16(ctx, regs::SP);
  1305. };
  1306. ctx.builder.const_i32(4);
  1307. ctx.builder.sub_i32();
  1308. let new_sp_local = if !ctx.cpu.ssize_32() || !ctx.cpu.has_flat_segmentation() {
  1309. let new_sp_local = ctx.builder.tee_new_local();
  1310. if !ctx.cpu.ssize_32() {
  1311. ctx.builder.const_i32(0xFFFF);
  1312. ctx.builder.and_i32();
  1313. }
  1314. if !ctx.cpu.has_flat_segmentation() {
  1315. gen_get_ss_offset(ctx);
  1316. ctx.builder.add_i32();
  1317. }
  1318. let sp_local = ctx.builder.set_new_local();
  1319. gen_safe_write32(ctx, &sp_local, &value_local);
  1320. ctx.builder.free_local(sp_local);
  1321. ctx.builder.get_local(&new_sp_local);
  1322. new_sp_local
  1323. }
  1324. else {
  1325. // short path: The address written to is equal to ESP/SP minus four
  1326. let new_sp_local = ctx.builder.tee_new_local();
  1327. gen_safe_write32(ctx, &new_sp_local, &value_local);
  1328. new_sp_local
  1329. };
  1330. if ctx.cpu.ssize_32() {
  1331. gen_set_reg32(ctx, regs::ESP);
  1332. }
  1333. else {
  1334. gen_set_reg16(ctx, regs::SP);
  1335. };
  1336. ctx.builder.free_local(new_sp_local);
  1337. }
  1338. pub fn gen_get_real_eip(ctx: &mut JitContext) {
  1339. gen_get_eip(ctx.builder);
  1340. ctx.builder.const_i32(!0xFFF);
  1341. ctx.builder.and_i32();
  1342. ctx.builder.const_i32(ctx.cpu.eip as i32 & 0xFFF);
  1343. ctx.builder.or_i32();
  1344. if !ctx.cpu.has_flat_segmentation() {
  1345. ctx.builder
  1346. .load_fixed_i32(global_pointers::get_seg_offset(regs::CS));
  1347. ctx.builder.sub_i32();
  1348. }
  1349. }
  1350. pub fn gen_set_last_op1(builder: &mut WasmBuilder, source: &WasmLocal) {
  1351. builder.const_i32(global_pointers::last_op1 as i32);
  1352. builder.get_local(&source);
  1353. builder.store_aligned_i32(0);
  1354. }
  1355. pub fn gen_set_last_result(builder: &mut WasmBuilder, source: &WasmLocal) {
  1356. builder.const_i32(global_pointers::last_result as i32);
  1357. builder.get_local(&source);
  1358. builder.store_aligned_i32(0);
  1359. }
  1360. pub fn gen_clear_flags_changed_bits(builder: &mut WasmBuilder, bits_to_clear: i32) {
  1361. builder.const_i32(global_pointers::flags_changed as i32);
  1362. gen_get_flags_changed(builder);
  1363. builder.const_i32(!bits_to_clear);
  1364. builder.and_i32();
  1365. builder.store_aligned_i32(0);
  1366. }
  1367. pub fn gen_set_last_op_size_and_flags_changed(
  1368. builder: &mut WasmBuilder,
  1369. last_op_size: i32,
  1370. flags_changed: i32,
  1371. ) {
  1372. dbg_assert!(last_op_size == OPSIZE_8 || last_op_size == OPSIZE_16 || last_op_size == OPSIZE_32);
  1373. dbg_assert!(global_pointers::last_op_size as i32 % 8 == 0);
  1374. dbg_assert!(global_pointers::last_op_size as i32 + 4 == global_pointers::flags_changed as i32);
  1375. builder.const_i32(global_pointers::last_op_size as i32);
  1376. builder.const_i64(last_op_size as u32 as i64 | (flags_changed as u32 as i64) << 32);
  1377. builder.store_aligned_i64(0);
  1378. }
  1379. pub fn gen_set_flags_bits(builder: &mut WasmBuilder, bits_to_set: i32) {
  1380. builder.const_i32(global_pointers::flags as i32);
  1381. gen_get_flags(builder);
  1382. builder.const_i32(bits_to_set);
  1383. builder.or_i32();
  1384. builder.store_aligned_i32(0);
  1385. }
  1386. pub fn gen_clear_flags_bits(builder: &mut WasmBuilder, bits_to_clear: i32) {
  1387. builder.const_i32(global_pointers::flags as i32);
  1388. gen_get_flags(builder);
  1389. builder.const_i32(!bits_to_clear);
  1390. builder.and_i32();
  1391. builder.store_aligned_i32(0);
  1392. }
  1393. #[derive(PartialEq)]
  1394. pub enum ConditionNegate {
  1395. True,
  1396. False,
  1397. }
  1398. pub fn gen_getzf(ctx: &mut JitContext, negate: ConditionNegate) {
  1399. match &ctx.previous_instruction {
  1400. Instruction::Cmp {
  1401. dest: InstructionOperandDest::WasmLocal(dest),
  1402. source: InstructionOperand::WasmLocal(source),
  1403. opsize: OPSIZE_32,
  1404. } => {
  1405. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1406. ctx.builder.get_local(dest);
  1407. ctx.builder.get_local(source);
  1408. if negate == ConditionNegate::False {
  1409. ctx.builder.eq_i32();
  1410. }
  1411. else {
  1412. ctx.builder.ne_i32();
  1413. }
  1414. },
  1415. Instruction::Cmp {
  1416. dest: InstructionOperandDest::WasmLocal(dest),
  1417. source: InstructionOperand::Immediate(i),
  1418. opsize: OPSIZE_32,
  1419. } => {
  1420. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1421. ctx.builder.get_local(dest);
  1422. if *i != 0 {
  1423. ctx.builder.const_i32(*i);
  1424. if negate == ConditionNegate::False {
  1425. ctx.builder.eq_i32();
  1426. }
  1427. else {
  1428. ctx.builder.ne_i32();
  1429. }
  1430. }
  1431. else {
  1432. if negate == ConditionNegate::False {
  1433. ctx.builder.eqz_i32();
  1434. }
  1435. }
  1436. },
  1437. Instruction::Cmp { .. }
  1438. | Instruction::Sub { .. }
  1439. | Instruction::Add { .. }
  1440. | Instruction::AdcSbb { .. }
  1441. | Instruction::NonZeroShift { .. } => {
  1442. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1443. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1444. if negate == ConditionNegate::False {
  1445. ctx.builder.eqz_i32();
  1446. }
  1447. },
  1448. Instruction::Bitwise { opsize, .. } => {
  1449. let &opsize = opsize;
  1450. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1451. // Note: Necessary because test{8,16} don't mask their neither last_result nor any of their operands
  1452. // TODO: Use local instead of last_result for 8-bit/16-bit
  1453. if opsize == OPSIZE_32 {
  1454. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1455. }
  1456. else if opsize == OPSIZE_16 {
  1457. ctx.builder
  1458. .load_fixed_u16(global_pointers::last_result as u32);
  1459. }
  1460. else if opsize == OPSIZE_8 {
  1461. ctx.builder
  1462. .load_fixed_u8(global_pointers::last_result as u32);
  1463. }
  1464. if negate == ConditionNegate::False {
  1465. ctx.builder.eqz_i32();
  1466. }
  1467. },
  1468. &Instruction::Other => {
  1469. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
  1470. gen_get_flags_changed(ctx.builder);
  1471. ctx.builder.const_i32(FLAG_ZERO);
  1472. ctx.builder.and_i32();
  1473. ctx.builder.if_i32();
  1474. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1475. let last_result = ctx.builder.tee_new_local();
  1476. ctx.builder.const_i32(-1);
  1477. ctx.builder.xor_i32();
  1478. ctx.builder.get_local(&last_result);
  1479. ctx.builder.free_local(last_result);
  1480. ctx.builder.const_i32(1);
  1481. ctx.builder.sub_i32();
  1482. ctx.builder.and_i32();
  1483. gen_get_last_op_size(ctx.builder);
  1484. ctx.builder.shr_u_i32();
  1485. ctx.builder.const_i32(1);
  1486. ctx.builder.and_i32();
  1487. ctx.builder.else_();
  1488. gen_get_flags(ctx.builder);
  1489. ctx.builder.const_i32(FLAG_ZERO);
  1490. ctx.builder.and_i32();
  1491. ctx.builder.block_end();
  1492. if negate == ConditionNegate::True {
  1493. ctx.builder.eqz_i32();
  1494. }
  1495. },
  1496. }
  1497. }
  1498. pub fn gen_getcf(ctx: &mut JitContext, negate: ConditionNegate) {
  1499. match &ctx.previous_instruction {
  1500. Instruction::Cmp { source, opsize, .. }
  1501. | Instruction::Sub {
  1502. source,
  1503. opsize,
  1504. is_dec: false,
  1505. ..
  1506. } => {
  1507. // Note: x < y and x < x - y can be used interchangeably (see getcf)
  1508. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1509. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1510. match (opsize, source) {
  1511. (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),
  1512. (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(i),
  1513. _ => gen_get_last_result(ctx.builder, &ctx.previous_instruction),
  1514. }
  1515. if negate == ConditionNegate::True {
  1516. ctx.builder.geu_i32();
  1517. }
  1518. else {
  1519. ctx.builder.ltu_i32();
  1520. }
  1521. },
  1522. Instruction::Add {
  1523. source,
  1524. opsize,
  1525. is_inc: false,
  1526. ..
  1527. } => {
  1528. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1529. match (opsize, source) {
  1530. (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),
  1531. (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(i),
  1532. _ => gen_get_last_op1(ctx.builder, &ctx.previous_instruction),
  1533. }
  1534. if negate == ConditionNegate::True {
  1535. ctx.builder.geu_i32();
  1536. }
  1537. else {
  1538. ctx.builder.ltu_i32();
  1539. }
  1540. },
  1541. Instruction::Add { is_inc: true, .. } | Instruction::Sub { is_dec: true, .. } => {
  1542. gen_get_flags(ctx.builder);
  1543. ctx.builder.const_i32(FLAG_CARRY);
  1544. ctx.builder.and_i32();
  1545. if negate == ConditionNegate::True {
  1546. ctx.builder.eqz_i32();
  1547. }
  1548. },
  1549. Instruction::Bitwise { .. } => {
  1550. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1551. ctx.builder
  1552. .const_i32(if negate == ConditionNegate::True { 1 } else { 0 });
  1553. },
  1554. Instruction::NonZeroShift { .. } | Instruction::AdcSbb { .. } => {
  1555. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1556. gen_get_flags(ctx.builder);
  1557. ctx.builder.const_i32(FLAG_CARRY);
  1558. ctx.builder.and_i32();
  1559. if negate == ConditionNegate::True {
  1560. ctx.builder.eqz_i32();
  1561. }
  1562. },
  1563. &Instruction::Other => {
  1564. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
  1565. gen_get_flags_changed(ctx.builder);
  1566. let flags_changed = ctx.builder.tee_new_local();
  1567. ctx.builder.const_i32(FLAG_CARRY);
  1568. ctx.builder.and_i32();
  1569. ctx.builder.if_i32();
  1570. ctx.builder.get_local(&flags_changed);
  1571. ctx.builder.const_i32(31);
  1572. ctx.builder.shr_s_i32();
  1573. ctx.builder.free_local(flags_changed);
  1574. let sub_mask = ctx.builder.set_new_local();
  1575. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1576. ctx.builder.get_local(&sub_mask);
  1577. ctx.builder.xor_i32();
  1578. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1579. ctx.builder.get_local(&sub_mask);
  1580. ctx.builder.xor_i32();
  1581. ctx.builder.ltu_i32();
  1582. ctx.builder.else_();
  1583. gen_get_flags(ctx.builder);
  1584. ctx.builder.const_i32(FLAG_CARRY);
  1585. ctx.builder.and_i32();
  1586. ctx.builder.block_end();
  1587. ctx.builder.free_local(sub_mask);
  1588. if negate == ConditionNegate::True {
  1589. ctx.builder.eqz_i32();
  1590. }
  1591. },
  1592. }
  1593. }
  1594. pub fn gen_getsf(ctx: &mut JitContext, negate: ConditionNegate) {
  1595. match &ctx.previous_instruction {
  1596. Instruction::Cmp { opsize, .. }
  1597. | Instruction::Sub { opsize, .. }
  1598. | Instruction::Add { opsize, .. }
  1599. | Instruction::AdcSbb { opsize, .. }
  1600. | Instruction::Bitwise { opsize, .. }
  1601. | Instruction::NonZeroShift { opsize, .. } => {
  1602. let &opsize = opsize;
  1603. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1604. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1605. if opsize == OPSIZE_32 {
  1606. ctx.builder.const_i32(0);
  1607. if negate == ConditionNegate::True {
  1608. ctx.builder.ge_i32();
  1609. }
  1610. else {
  1611. ctx.builder.lt_i32();
  1612. }
  1613. }
  1614. else {
  1615. // TODO: use register (see get_last_result)
  1616. ctx.builder
  1617. .const_i32(if opsize == OPSIZE_16 { 0x8000 } else { 0x80 });
  1618. ctx.builder.and_i32();
  1619. if negate == ConditionNegate::True {
  1620. ctx.builder.eqz_i32();
  1621. }
  1622. }
  1623. },
  1624. &Instruction::Other => {
  1625. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
  1626. gen_get_flags_changed(ctx.builder);
  1627. ctx.builder.const_i32(FLAG_SIGN);
  1628. ctx.builder.and_i32();
  1629. ctx.builder.if_i32();
  1630. {
  1631. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1632. gen_get_last_op_size(ctx.builder);
  1633. ctx.builder.shr_u_i32();
  1634. ctx.builder.const_i32(1);
  1635. ctx.builder.and_i32();
  1636. }
  1637. ctx.builder.else_();
  1638. {
  1639. gen_get_flags(ctx.builder);
  1640. ctx.builder.const_i32(FLAG_SIGN);
  1641. ctx.builder.and_i32();
  1642. }
  1643. ctx.builder.block_end();
  1644. if negate == ConditionNegate::True {
  1645. ctx.builder.eqz_i32();
  1646. }
  1647. },
  1648. }
  1649. }
  1650. pub fn gen_getof(ctx: &mut JitContext) {
  1651. match &ctx.previous_instruction {
  1652. Instruction::Cmp { opsize, .. } | Instruction::Sub { opsize, .. } => {
  1653. // TODO: a better formula might be possible
  1654. let &opsize = opsize;
  1655. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1656. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1657. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1658. ctx.builder.xor_i32();
  1659. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1660. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1661. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1662. ctx.builder.sub_i32();
  1663. ctx.builder.xor_i32();
  1664. ctx.builder.and_i32();
  1665. ctx.builder.const_i32(if opsize == OPSIZE_32 {
  1666. 0x8000_0000u32 as i32
  1667. }
  1668. else if opsize == OPSIZE_16 {
  1669. 0x8000
  1670. }
  1671. else {
  1672. 0x80
  1673. });
  1674. ctx.builder.and_i32();
  1675. },
  1676. Instruction::Add { opsize, .. } => {
  1677. // TODO: a better formula might be possible
  1678. let &opsize = opsize;
  1679. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1680. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1681. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1682. ctx.builder.xor_i32();
  1683. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1684. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1685. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1686. ctx.builder.sub_i32();
  1687. ctx.builder.xor_i32();
  1688. ctx.builder.and_i32();
  1689. ctx.builder.const_i32(if opsize == OPSIZE_32 {
  1690. 0x8000_0000u32 as i32
  1691. }
  1692. else if opsize == OPSIZE_16 {
  1693. 0x8000
  1694. }
  1695. else {
  1696. 0x80
  1697. });
  1698. ctx.builder.and_i32();
  1699. },
  1700. Instruction::Bitwise { .. } => {
  1701. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1702. ctx.builder.const_i32(0);
  1703. },
  1704. Instruction::NonZeroShift { .. } | Instruction::AdcSbb { .. } => {
  1705. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1706. gen_get_flags(ctx.builder);
  1707. ctx.builder.const_i32(FLAG_OVERFLOW);
  1708. ctx.builder.and_i32();
  1709. },
  1710. &Instruction::Other => {
  1711. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
  1712. gen_get_flags_changed(ctx.builder);
  1713. let flags_changed = ctx.builder.tee_new_local();
  1714. ctx.builder.const_i32(FLAG_OVERFLOW);
  1715. ctx.builder.and_i32();
  1716. ctx.builder.if_i32();
  1717. {
  1718. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1719. let last_op1 = ctx.builder.tee_new_local();
  1720. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1721. let last_result = ctx.builder.tee_new_local();
  1722. ctx.builder.xor_i32();
  1723. ctx.builder.get_local(&last_result);
  1724. ctx.builder.get_local(&last_op1);
  1725. ctx.builder.sub_i32();
  1726. gen_get_flags_changed(ctx.builder);
  1727. ctx.builder.const_i32(31);
  1728. ctx.builder.shr_u_i32();
  1729. ctx.builder.sub_i32();
  1730. ctx.builder.get_local(&last_result);
  1731. ctx.builder.xor_i32();
  1732. ctx.builder.and_i32();
  1733. gen_get_last_op_size(ctx.builder);
  1734. ctx.builder.shr_u_i32();
  1735. ctx.builder.const_i32(1);
  1736. ctx.builder.and_i32();
  1737. ctx.builder.free_local(last_op1);
  1738. ctx.builder.free_local(last_result);
  1739. }
  1740. ctx.builder.else_();
  1741. {
  1742. gen_get_flags(ctx.builder);
  1743. ctx.builder.const_i32(FLAG_OVERFLOW);
  1744. ctx.builder.and_i32();
  1745. }
  1746. ctx.builder.block_end();
  1747. ctx.builder.free_local(flags_changed);
  1748. },
  1749. }
  1750. }
  1751. pub fn gen_test_be(ctx: &mut JitContext, negate: ConditionNegate) {
  1752. match &ctx.previous_instruction {
  1753. Instruction::Cmp {
  1754. dest,
  1755. source,
  1756. opsize,
  1757. } => {
  1758. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1759. match dest {
  1760. InstructionOperandDest::WasmLocal(l) => {
  1761. ctx.builder.get_local(l);
  1762. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1763. ctx.builder
  1764. .const_i32(if *opsize == OPSIZE_8 { 0xFF } else { 0xFFFF });
  1765. ctx.builder.and_i32();
  1766. }
  1767. },
  1768. InstructionOperandDest::Other => {
  1769. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1770. },
  1771. }
  1772. match source {
  1773. InstructionOperand::WasmLocal(l) => {
  1774. ctx.builder.get_local(l);
  1775. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1776. ctx.builder
  1777. .const_i32(if *opsize == OPSIZE_8 { 0xFF } else { 0xFFFF });
  1778. ctx.builder.and_i32();
  1779. }
  1780. },
  1781. InstructionOperand::Other => {
  1782. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1783. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1784. ctx.builder.sub_i32();
  1785. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1786. ctx.builder
  1787. .const_i32(if *opsize == OPSIZE_8 { 0xFF } else { 0xFFFF });
  1788. ctx.builder.and_i32();
  1789. }
  1790. },
  1791. &InstructionOperand::Immediate(i) => {
  1792. dbg_assert!(*opsize != OPSIZE_8 || i >= 0 && i < 0x100);
  1793. dbg_assert!(*opsize != OPSIZE_16 || i >= 0 && i < 0x10000);
  1794. ctx.builder.const_i32(i);
  1795. },
  1796. }
  1797. if negate == ConditionNegate::True {
  1798. ctx.builder.gtu_i32();
  1799. }
  1800. else {
  1801. ctx.builder.leu_i32();
  1802. }
  1803. },
  1804. Instruction::Sub {
  1805. opsize,
  1806. source,
  1807. is_dec: false,
  1808. ..
  1809. } => {
  1810. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1811. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1812. match (opsize, source) {
  1813. (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),
  1814. (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(i),
  1815. _ => {
  1816. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1817. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1818. ctx.builder.sub_i32();
  1819. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1820. ctx.builder
  1821. .const_i32(if *opsize == OPSIZE_8 { 0xFF } else { 0xFFFF });
  1822. ctx.builder.and_i32();
  1823. }
  1824. },
  1825. }
  1826. if negate == ConditionNegate::True {
  1827. ctx.builder.gtu_i32();
  1828. }
  1829. else {
  1830. ctx.builder.leu_i32();
  1831. }
  1832. },
  1833. &Instruction::Bitwise { .. } => {
  1834. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1835. gen_getzf(ctx, negate);
  1836. },
  1837. &Instruction::Add { .. } | &Instruction::Sub { is_dec: true, .. } => {
  1838. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1839. // not the best code generation, but reasonable for this fairly uncommon case
  1840. gen_getcf(ctx, ConditionNegate::False);
  1841. gen_getzf(ctx, ConditionNegate::False);
  1842. ctx.builder.or_i32();
  1843. if negate == ConditionNegate::True {
  1844. ctx.builder.eqz_i32();
  1845. }
  1846. },
  1847. Instruction::Other | Instruction::NonZeroShift { .. } | Instruction::AdcSbb { .. } => {
  1848. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
  1849. gen_getcf(ctx, ConditionNegate::False);
  1850. gen_getzf(ctx, ConditionNegate::False);
  1851. ctx.builder.or_i32();
  1852. if negate == ConditionNegate::True {
  1853. ctx.builder.eqz_i32();
  1854. }
  1855. },
  1856. }
  1857. }
  1858. pub fn gen_test_l(ctx: &mut JitContext, negate: ConditionNegate) {
  1859. match &ctx.previous_instruction {
  1860. Instruction::Cmp {
  1861. dest,
  1862. source,
  1863. opsize,
  1864. } => {
  1865. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1866. match dest {
  1867. InstructionOperandDest::WasmLocal(l) => {
  1868. ctx.builder.get_local(l);
  1869. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1870. ctx.builder
  1871. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  1872. ctx.builder.shl_i32();
  1873. }
  1874. },
  1875. InstructionOperandDest::Other => {
  1876. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1877. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1878. ctx.builder
  1879. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  1880. ctx.builder.shl_i32();
  1881. }
  1882. },
  1883. }
  1884. match source {
  1885. InstructionOperand::WasmLocal(l) => {
  1886. ctx.builder.get_local(l);
  1887. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1888. ctx.builder
  1889. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  1890. ctx.builder.shl_i32();
  1891. }
  1892. },
  1893. InstructionOperand::Other => {
  1894. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1895. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1896. ctx.builder.sub_i32();
  1897. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1898. ctx.builder
  1899. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  1900. ctx.builder.shl_i32();
  1901. }
  1902. },
  1903. &InstructionOperand::Immediate(i) => {
  1904. ctx.builder.const_i32(i);
  1905. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1906. ctx.builder
  1907. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  1908. ctx.builder.shl_i32();
  1909. }
  1910. },
  1911. }
  1912. if negate == ConditionNegate::True {
  1913. ctx.builder.ge_i32();
  1914. }
  1915. else {
  1916. ctx.builder.lt_i32();
  1917. }
  1918. },
  1919. Instruction::Sub { opsize, source, .. } => {
  1920. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1921. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1922. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1923. ctx.builder
  1924. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  1925. ctx.builder.shl_i32();
  1926. }
  1927. match (opsize, source) {
  1928. (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),
  1929. (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(
  1930. i << if *opsize == OPSIZE_32 {
  1931. 0
  1932. }
  1933. else if *opsize == OPSIZE_16 {
  1934. 16
  1935. }
  1936. else {
  1937. 24
  1938. },
  1939. ),
  1940. _ => {
  1941. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  1942. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  1943. ctx.builder.sub_i32();
  1944. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1945. ctx.builder
  1946. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  1947. ctx.builder.shl_i32();
  1948. }
  1949. },
  1950. }
  1951. if negate == ConditionNegate::True {
  1952. ctx.builder.ge_i32();
  1953. }
  1954. else {
  1955. ctx.builder.lt_i32();
  1956. }
  1957. },
  1958. &Instruction::Bitwise { .. } => {
  1959. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1960. gen_getsf(ctx, negate);
  1961. },
  1962. &Instruction::Other
  1963. | Instruction::Add { .. }
  1964. | Instruction::NonZeroShift { .. }
  1965. | Instruction::AdcSbb { .. } => {
  1966. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
  1967. if let Instruction::Add { .. } = ctx.previous_instruction {
  1968. gen_profiler_stat_increment(
  1969. ctx.builder,
  1970. profiler::stat::CONDITION_UNOPTIMISED_UNHANDLED_L,
  1971. );
  1972. }
  1973. gen_getsf(ctx, ConditionNegate::False);
  1974. ctx.builder.eqz_i32();
  1975. gen_getof(ctx);
  1976. ctx.builder.eqz_i32();
  1977. ctx.builder.xor_i32();
  1978. if negate == ConditionNegate::True {
  1979. ctx.builder.eqz_i32();
  1980. }
  1981. },
  1982. }
  1983. }
  1984. pub fn gen_test_le(ctx: &mut JitContext, negate: ConditionNegate) {
  1985. match &ctx.previous_instruction {
  1986. Instruction::Cmp {
  1987. dest,
  1988. source,
  1989. opsize,
  1990. } => {
  1991. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  1992. match dest {
  1993. InstructionOperandDest::WasmLocal(l) => {
  1994. ctx.builder.get_local(l);
  1995. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  1996. ctx.builder
  1997. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  1998. ctx.builder.shl_i32();
  1999. }
  2000. },
  2001. InstructionOperandDest::Other => {
  2002. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  2003. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  2004. ctx.builder
  2005. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  2006. ctx.builder.shl_i32();
  2007. }
  2008. },
  2009. }
  2010. match source {
  2011. InstructionOperand::WasmLocal(l) => {
  2012. ctx.builder.get_local(l);
  2013. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  2014. ctx.builder
  2015. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  2016. ctx.builder.shl_i32();
  2017. }
  2018. },
  2019. InstructionOperand::Other => {
  2020. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  2021. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  2022. ctx.builder.sub_i32();
  2023. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  2024. ctx.builder
  2025. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  2026. ctx.builder.shl_i32();
  2027. }
  2028. },
  2029. &InstructionOperand::Immediate(i) => {
  2030. ctx.builder.const_i32(i);
  2031. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  2032. ctx.builder
  2033. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  2034. ctx.builder.shl_i32();
  2035. }
  2036. },
  2037. }
  2038. if negate == ConditionNegate::True {
  2039. ctx.builder.gt_i32();
  2040. }
  2041. else {
  2042. ctx.builder.le_i32();
  2043. }
  2044. },
  2045. Instruction::Sub { opsize, source, .. } => {
  2046. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  2047. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  2048. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  2049. ctx.builder
  2050. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  2051. ctx.builder.shl_i32();
  2052. }
  2053. match (opsize, source) {
  2054. (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),
  2055. (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(
  2056. i << if *opsize == OPSIZE_32 {
  2057. 0
  2058. }
  2059. else if *opsize == OPSIZE_16 {
  2060. 16
  2061. }
  2062. else {
  2063. 24
  2064. },
  2065. ),
  2066. _ => {
  2067. gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
  2068. gen_get_last_result(ctx.builder, &ctx.previous_instruction);
  2069. ctx.builder.sub_i32();
  2070. if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
  2071. ctx.builder
  2072. .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });
  2073. ctx.builder.shl_i32();
  2074. }
  2075. },
  2076. }
  2077. if negate == ConditionNegate::True {
  2078. ctx.builder.gt_i32();
  2079. }
  2080. else {
  2081. ctx.builder.le_i32();
  2082. }
  2083. },
  2084. &Instruction::Bitwise { .. } => {
  2085. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
  2086. // TODO: Could probably be improved (<= 0)
  2087. gen_test_l(ctx, ConditionNegate::False);
  2088. gen_getzf(ctx, ConditionNegate::False);
  2089. ctx.builder.or_i32();
  2090. if negate == ConditionNegate::True {
  2091. ctx.builder.eqz_i32();
  2092. }
  2093. },
  2094. Instruction::Other
  2095. | Instruction::Add { .. }
  2096. | Instruction::NonZeroShift { .. }
  2097. | Instruction::AdcSbb { .. } => {
  2098. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
  2099. if let Instruction::Add { .. } = ctx.previous_instruction {
  2100. gen_profiler_stat_increment(
  2101. ctx.builder,
  2102. profiler::stat::CONDITION_UNOPTIMISED_UNHANDLED_LE,
  2103. );
  2104. }
  2105. gen_test_l(ctx, ConditionNegate::False);
  2106. gen_getzf(ctx, ConditionNegate::False);
  2107. ctx.builder.or_i32();
  2108. if negate == ConditionNegate::True {
  2109. ctx.builder.eqz_i32();
  2110. }
  2111. },
  2112. }
  2113. }
  2114. pub fn gen_test_loopnz(ctx: &mut JitContext, is_asize_32: bool) {
  2115. gen_test_loop(ctx, is_asize_32);
  2116. ctx.builder.eqz_i32();
  2117. gen_getzf(ctx, ConditionNegate::False);
  2118. ctx.builder.or_i32();
  2119. ctx.builder.eqz_i32();
  2120. }
  2121. pub fn gen_test_loopz(ctx: &mut JitContext, is_asize_32: bool) {
  2122. gen_test_loop(ctx, is_asize_32);
  2123. ctx.builder.eqz_i32();
  2124. gen_getzf(ctx, ConditionNegate::False);
  2125. ctx.builder.eqz_i32();
  2126. ctx.builder.or_i32();
  2127. ctx.builder.eqz_i32();
  2128. }
  2129. pub fn gen_test_loop(ctx: &mut JitContext, is_asize_32: bool) {
  2130. if is_asize_32 {
  2131. gen_get_reg32(ctx, regs::ECX);
  2132. }
  2133. else {
  2134. gen_get_reg16(ctx, regs::CX);
  2135. }
  2136. }
  2137. pub fn gen_test_jcxz(ctx: &mut JitContext, is_asize_32: bool) {
  2138. if is_asize_32 {
  2139. gen_get_reg32(ctx, regs::ECX);
  2140. }
  2141. else {
  2142. gen_get_reg16(ctx, regs::CX);
  2143. }
  2144. ctx.builder.eqz_i32();
  2145. }
  2146. pub fn gen_fpu_get_sti(ctx: &mut JitContext, i: u32) {
  2147. ctx.builder
  2148. .const_i32(global_pointers::sse_scratch_register as i32);
  2149. ctx.builder.const_i32(i as i32);
  2150. ctx.builder.call_fn2("fpu_get_sti");
  2151. ctx.builder
  2152. .load_fixed_i64(global_pointers::sse_scratch_register as u32);
  2153. ctx.builder
  2154. .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);
  2155. }
  2156. pub fn gen_fpu_load_m32(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  2157. ctx.builder
  2158. .const_i32(global_pointers::sse_scratch_register as i32);
  2159. gen_modrm_resolve_safe_read32(ctx, modrm_byte);
  2160. ctx.builder.call_fn2("f32_to_f80");
  2161. ctx.builder
  2162. .load_fixed_i64(global_pointers::sse_scratch_register as u32);
  2163. ctx.builder
  2164. .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);
  2165. }
  2166. pub fn gen_fpu_load_m64(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  2167. ctx.builder
  2168. .const_i32(global_pointers::sse_scratch_register as i32);
  2169. gen_modrm_resolve_safe_read64(ctx, modrm_byte);
  2170. ctx.builder.call_fn2_i32_i64("f64_to_f80");
  2171. ctx.builder
  2172. .load_fixed_i64(global_pointers::sse_scratch_register as u32);
  2173. ctx.builder
  2174. .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);
  2175. }
  2176. pub fn gen_fpu_load_i16(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  2177. ctx.builder
  2178. .const_i32(global_pointers::sse_scratch_register as i32);
  2179. gen_modrm_resolve_safe_read16(ctx, modrm_byte);
  2180. sign_extend_i16(ctx.builder);
  2181. ctx.builder.call_fn2("i32_to_f80");
  2182. ctx.builder
  2183. .load_fixed_i64(global_pointers::sse_scratch_register as u32);
  2184. ctx.builder
  2185. .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);
  2186. }
  2187. pub fn gen_fpu_load_i32(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  2188. ctx.builder
  2189. .const_i32(global_pointers::sse_scratch_register as i32);
  2190. gen_modrm_resolve_safe_read32(ctx, modrm_byte);
  2191. ctx.builder.call_fn2("i32_to_f80");
  2192. ctx.builder
  2193. .load_fixed_i64(global_pointers::sse_scratch_register as u32);
  2194. ctx.builder
  2195. .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);
  2196. }
  2197. pub fn gen_fpu_load_i64(ctx: &mut JitContext, modrm_byte: ModrmByte) {
  2198. ctx.builder
  2199. .const_i32(global_pointers::sse_scratch_register as i32);
  2200. gen_modrm_resolve_safe_read64(ctx, modrm_byte);
  2201. ctx.builder.call_fn2_i32_i64("i64_to_f80");
  2202. ctx.builder
  2203. .load_fixed_i64(global_pointers::sse_scratch_register as u32);
  2204. ctx.builder
  2205. .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);
  2206. }
  2207. pub fn gen_trigger_de(ctx: &mut JitContext) {
  2208. gen_fn1_const(
  2209. ctx.builder,
  2210. "trigger_de_jit",
  2211. ctx.start_of_current_instruction,
  2212. );
  2213. gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
  2214. ctx.builder.br(ctx.exit_with_fault_label);
  2215. }
  2216. pub fn gen_trigger_ud(ctx: &mut JitContext) {
  2217. gen_fn1_const(
  2218. ctx.builder,
  2219. "trigger_ud_jit",
  2220. ctx.start_of_current_instruction,
  2221. );
  2222. gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
  2223. ctx.builder.br(ctx.exit_with_fault_label);
  2224. }
  2225. pub fn gen_trigger_gp(ctx: &mut JitContext, error_code: u32) {
  2226. gen_fn2_const(
  2227. ctx.builder,
  2228. "trigger_gp_jit",
  2229. error_code,
  2230. ctx.start_of_current_instruction,
  2231. );
  2232. gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
  2233. ctx.builder.br(ctx.exit_with_fault_label);
  2234. }
  2235. pub fn gen_condition_fn_negated(ctx: &mut JitContext, condition: u8) {
  2236. gen_condition_fn(ctx, condition ^ 1)
  2237. }
  2238. pub fn gen_condition_fn(ctx: &mut JitContext, condition: u8) {
  2239. if condition & 0xF0 == 0x00 || condition & 0xF0 == 0x70 || condition & 0xF0 == 0x80 {
  2240. match condition & 0xF {
  2241. 0x0 => {
  2242. gen_getof(ctx);
  2243. },
  2244. 0x1 => {
  2245. gen_getof(ctx);
  2246. ctx.builder.eqz_i32();
  2247. },
  2248. 0x2 => {
  2249. gen_getcf(ctx, ConditionNegate::False);
  2250. },
  2251. 0x3 => {
  2252. gen_getcf(ctx, ConditionNegate::True);
  2253. },
  2254. 0x4 => {
  2255. gen_getzf(ctx, ConditionNegate::False);
  2256. },
  2257. 0x5 => {
  2258. gen_getzf(ctx, ConditionNegate::True);
  2259. },
  2260. 0x6 => {
  2261. gen_test_be(ctx, ConditionNegate::False);
  2262. },
  2263. 0x7 => {
  2264. gen_test_be(ctx, ConditionNegate::True);
  2265. },
  2266. 0x8 => {
  2267. gen_getsf(ctx, ConditionNegate::False);
  2268. },
  2269. 0x9 => {
  2270. gen_getsf(ctx, ConditionNegate::True);
  2271. },
  2272. 0xA => {
  2273. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
  2274. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED_PF);
  2275. ctx.builder.call_fn0_ret("test_p");
  2276. },
  2277. 0xB => {
  2278. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
  2279. gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED_PF);
  2280. ctx.builder.call_fn0_ret("test_np");
  2281. },
  2282. 0xC => {
  2283. gen_test_l(ctx, ConditionNegate::False);
  2284. },
  2285. 0xD => {
  2286. gen_test_l(ctx, ConditionNegate::True);
  2287. },
  2288. 0xE => {
  2289. gen_test_le(ctx, ConditionNegate::False);
  2290. },
  2291. 0xF => {
  2292. gen_test_le(ctx, ConditionNegate::True);
  2293. },
  2294. _ => {
  2295. dbg_assert!(false);
  2296. },
  2297. }
  2298. }
  2299. else {
  2300. // loop, loopnz, loopz, jcxz
  2301. dbg_assert!(condition & !0x3 == 0xE0);
  2302. if condition == 0xE0 {
  2303. gen_test_loopnz(ctx, ctx.cpu.asize_32());
  2304. }
  2305. else if condition == 0xE1 {
  2306. gen_test_loopz(ctx, ctx.cpu.asize_32());
  2307. }
  2308. else if condition == 0xE2 {
  2309. gen_test_loop(ctx, ctx.cpu.asize_32());
  2310. }
  2311. else if condition == 0xE3 {
  2312. gen_test_jcxz(ctx, ctx.cpu.asize_32());
  2313. }
  2314. }
  2315. }
  2316. pub fn gen_move_registers_from_locals_to_memory(ctx: &mut JitContext) {
  2317. if cfg!(feature = "profiler") {
  2318. let instruction = memory::read32s(ctx.start_of_current_instruction) as u32;
  2319. ::opstats::gen_opstat_unguarded_register(ctx.builder, instruction);
  2320. }
  2321. for i in 0..8 {
  2322. ctx.builder
  2323. .const_i32(global_pointers::get_reg32_offset(i as u32) as i32);
  2324. ctx.builder.get_local(&ctx.register_locals[i]);
  2325. ctx.builder.store_aligned_i32(0);
  2326. }
  2327. }
  2328. pub fn gen_move_registers_from_memory_to_locals(ctx: &mut JitContext) {
  2329. if cfg!(feature = "profiler") {
  2330. let instruction = memory::read32s(ctx.start_of_current_instruction) as u32;
  2331. ::opstats::gen_opstat_unguarded_register(ctx.builder, instruction);
  2332. }
  2333. for i in 0..8 {
  2334. ctx.builder
  2335. .const_i32(global_pointers::get_reg32_offset(i as u32) as i32);
  2336. ctx.builder.load_aligned_i32(0);
  2337. ctx.builder.set_local(&ctx.register_locals[i]);
  2338. }
  2339. }
  2340. pub fn gen_profiler_stat_increment(builder: &mut WasmBuilder, stat: profiler::stat) {
  2341. if !cfg!(feature = "profiler") {
  2342. return;
  2343. }
  2344. let addr = unsafe { profiler::stat_array.as_mut_ptr().offset(stat as isize) } as u32;
  2345. builder.increment_fixed_i64(addr, 1)
  2346. }
  2347. pub fn gen_debug_track_jit_exit(builder: &mut WasmBuilder, address: u32) {
  2348. if cfg!(feature = "profiler") {
  2349. gen_fn1_const(builder, "track_jit_exit", address);
  2350. }
  2351. }