codegen.rs 87 KB

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