n8086add.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Code generation for add nodes on the i8086
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit n8086add;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,cpubase,nx86add;
  22. type
  23. { ti8086addnode }
  24. ti8086addnode = class(tx86addnode)
  25. function simplify(forinline: boolean) : tnode;override;
  26. function use_generic_mul32to64: boolean; override;
  27. function first_addpointer: tnode; override;
  28. function first_addhugepointer: tnode;
  29. function first_cmppointer: tnode; override;
  30. function first_cmphugepointer: tnode;
  31. function first_cmpfarpointer: tnode;
  32. procedure second_addordinal; override;
  33. procedure second_add64bit;override;
  34. procedure second_addfarpointer;
  35. procedure second_cmp64bit;override;
  36. procedure second_cmp32bit;
  37. procedure second_cmpfarpointer;
  38. procedure second_cmpordinal;override;
  39. procedure second_mul(unsigned: boolean);
  40. end;
  41. implementation
  42. uses
  43. globtype,systems,
  44. cutils,verbose,globals,constexp,pass_1,
  45. symconst,symdef,symtype,symcpu,paramgr,defutil,
  46. aasmbase,aasmtai,aasmdata,aasmcpu,
  47. cgbase,procinfo,
  48. ncal,ncon,nset,cgutils,tgobj,
  49. cga,ncgutil,cgobj,cg64f32,cgx86,
  50. hlcgobj;
  51. {*****************************************************************************
  52. simplify
  53. *****************************************************************************}
  54. function ti8086addnode.simplify(forinline: boolean): tnode;
  55. var
  56. t : tnode;
  57. lt,rt: tnodetype;
  58. rd,ld: tdef;
  59. rv,lv,v: tconstexprint;
  60. begin
  61. { load easier access variables }
  62. rd:=right.resultdef;
  63. ld:=left.resultdef;
  64. rt:=right.nodetype;
  65. lt:=left.nodetype;
  66. if (
  67. (lt = pointerconstn) and is_farpointer(ld) and
  68. is_constintnode(right) and
  69. (nodetype in [addn,subn])
  70. ) or
  71. (
  72. (rt = pointerconstn) and is_farpointer(rd) and
  73. is_constintnode(left) and
  74. (nodetype=addn)
  75. ) or
  76. (
  77. (lt in [pointerconstn,niln]) and is_farpointer(ld) and
  78. (rt in [pointerconstn,niln]) and is_farpointer(rd) and
  79. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln])
  80. ) then
  81. begin
  82. t:=nil;
  83. { load values }
  84. case lt of
  85. ordconstn:
  86. lv:=tordconstnode(left).value;
  87. pointerconstn:
  88. lv:=tpointerconstnode(left).value;
  89. niln:
  90. lv:=0;
  91. else
  92. internalerror(2002080201);
  93. end;
  94. case rt of
  95. ordconstn:
  96. rv:=tordconstnode(right).value;
  97. pointerconstn:
  98. rv:=tpointerconstnode(right).value;
  99. niln:
  100. rv:=0;
  101. else
  102. internalerror(2002080204);
  103. end;
  104. case nodetype of
  105. addn:
  106. begin
  107. v:=lv+rv;
  108. if lt=pointerconstn then
  109. t := cpointerconstnode.create((qword(lv) and $FFFF0000) or word(qword(v)),resultdef)
  110. else if rt=pointerconstn then
  111. t := cpointerconstnode.create((qword(rv) and $FFFF0000) or word(qword(v)),resultdef)
  112. else
  113. internalerror(2014040604);
  114. end;
  115. subn:
  116. begin
  117. v:=lv-rv;
  118. if (lt=pointerconstn) then
  119. { pointer-pointer results in an integer }
  120. if (rt=pointerconstn) then
  121. begin
  122. if not(anf_has_pointerdiv in addnodeflags) then
  123. internalerror(2008030102);
  124. { todo: implement pointer-pointer as well }
  125. internalerror(2014040607);
  126. //t := cpointerconstnode.create(qword(v),resultdef);
  127. end
  128. else
  129. t := cpointerconstnode.create((qword(lv) and $FFFF0000) or word(qword(v)),resultdef)
  130. else
  131. internalerror(2014040606);
  132. end;
  133. ltn:
  134. t:=cordconstnode.create(ord(word(qword(lv))<word(qword(rv))),pasbool1type,true);
  135. lten:
  136. t:=cordconstnode.create(ord(word(qword(lv))<=word(qword(rv))),pasbool1type,true);
  137. gtn:
  138. t:=cordconstnode.create(ord(word(qword(lv))>word(qword(rv))),pasbool1type,true);
  139. gten:
  140. t:=cordconstnode.create(ord(word(qword(lv))>=word(qword(rv))),pasbool1type,true);
  141. equaln:
  142. t:=cordconstnode.create(ord(lv=rv),pasbool1type,true);
  143. unequaln:
  144. t:=cordconstnode.create(ord(lv<>rv),pasbool1type,true);
  145. else
  146. internalerror(2014040605);
  147. end;
  148. result:=t;
  149. exit;
  150. end
  151. else
  152. Result:=inherited simplify(forinline);
  153. end;
  154. {*****************************************************************************
  155. use_generic_mul32to64
  156. *****************************************************************************}
  157. function ti8086addnode.use_generic_mul32to64: boolean;
  158. begin
  159. result := True;
  160. end;
  161. { handles all multiplications }
  162. procedure ti8086addnode.second_addordinal;
  163. var
  164. unsigned: boolean;
  165. begin
  166. unsigned:=not(is_signed(left.resultdef)) or
  167. not(is_signed(right.resultdef));
  168. if nodetype=muln then
  169. second_mul(unsigned)
  170. else if is_farpointer(left.resultdef) xor is_farpointer(right.resultdef) then
  171. second_addfarpointer
  172. else
  173. inherited second_addordinal;
  174. end;
  175. {*****************************************************************************
  176. Add64bit
  177. *****************************************************************************}
  178. procedure ti8086addnode.second_add64bit;
  179. var
  180. op : TOpCG;
  181. op1,op2 : TAsmOp;
  182. hregister,
  183. hregister2 : tregister;
  184. hl4 : tasmlabel;
  185. mboverflow,
  186. unsigned:boolean;
  187. r:Tregister;
  188. begin
  189. pass_left_right;
  190. op1:=A_NONE;
  191. op2:=A_NONE;
  192. mboverflow:=false;
  193. unsigned:=((left.resultdef.typ=orddef) and
  194. (torddef(left.resultdef).ordtype=u64bit)) or
  195. ((right.resultdef.typ=orddef) and
  196. (torddef(right.resultdef).ordtype=u64bit));
  197. case nodetype of
  198. addn :
  199. begin
  200. op:=OP_ADD;
  201. mboverflow:=true;
  202. end;
  203. subn :
  204. begin
  205. op:=OP_SUB;
  206. op1:=A_SUB;
  207. op2:=A_SBB;
  208. mboverflow:=true;
  209. end;
  210. xorn:
  211. op:=OP_XOR;
  212. orn:
  213. op:=OP_OR;
  214. andn:
  215. op:=OP_AND;
  216. else
  217. begin
  218. { everything should be handled in pass_1 (JM) }
  219. internalerror(2001090503);
  220. end;
  221. end;
  222. { left and right no register? }
  223. { then one must be demanded }
  224. if (left.location.loc<>LOC_REGISTER) then
  225. begin
  226. if (right.location.loc<>LOC_REGISTER) then
  227. begin
  228. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  229. hregister2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  230. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,joinreg64(hregister,hregister2));
  231. location_reset(left.location,LOC_REGISTER,left.location.size);
  232. left.location.register64.reglo:=hregister;
  233. left.location.register64.reghi:=hregister2;
  234. end
  235. else
  236. begin
  237. location_swap(left.location,right.location);
  238. toggleflag(nf_swapped);
  239. end;
  240. end;
  241. if mboverflow and needoverflowcheck then
  242. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  243. { at this point, left.location.loc should be LOC_REGISTER }
  244. if right.location.loc=LOC_REGISTER then
  245. begin
  246. { when swapped another result register }
  247. if (nodetype=subn) and (nf_swapped in flags) then
  248. begin
  249. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  250. left.location.register64,
  251. right.location.register64);
  252. location_swap(left.location,right.location);
  253. toggleflag(nf_swapped);
  254. end
  255. else
  256. begin
  257. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  258. right.location.register64,
  259. left.location.register64);
  260. end;
  261. end
  262. else
  263. begin
  264. { right.location<>LOC_REGISTER }
  265. if (nodetype=subn) and (nf_swapped in flags) then
  266. begin
  267. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  268. cg64.a_load64low_loc_reg(current_asmdata.CurrAsmList,right.location,r);
  269. emit_reg_reg(op1,S_W,left.location.register64.reglo,r);
  270. emit_reg_reg(op2,S_W,cg.GetNextReg(left.location.register64.reglo),cg.GetNextReg(r));
  271. emit_reg_reg(A_MOV,S_W,r,left.location.register64.reglo);
  272. emit_reg_reg(A_MOV,S_W,cg.GetNextReg(r),cg.GetNextReg(left.location.register64.reglo));
  273. cg64.a_load64high_loc_reg(current_asmdata.CurrAsmList,right.location,r);
  274. { the carry flag is still ok }
  275. emit_reg_reg(op2,S_W,left.location.register64.reghi,r);
  276. emit_reg_reg(op2,S_W,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(r));
  277. emit_reg_reg(A_MOV,S_W,r,left.location.register64.reghi);
  278. emit_reg_reg(A_MOV,S_W,cg.GetNextReg(r),cg.GetNextReg(left.location.register64.reghi));
  279. end
  280. else
  281. begin
  282. cg64.a_op64_loc_reg(current_asmdata.CurrAsmList,op,location.size,right.location,
  283. left.location.register64);
  284. end;
  285. location_freetemp(current_asmdata.CurrAsmList,right.location);
  286. end;
  287. { only in case of overflow operations }
  288. { produce overflow code }
  289. { we must put it here directly, because sign of operation }
  290. { is in unsigned VAR!! }
  291. if mboverflow then
  292. begin
  293. if needoverflowcheck then
  294. begin
  295. current_asmdata.getjumplabel(hl4);
  296. if unsigned then
  297. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4)
  298. else
  299. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NO,hl4);
  300. cg.a_reg_dealloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  301. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  302. cg.a_label(current_asmdata.CurrAsmList,hl4);
  303. end;
  304. end;
  305. location_copy(location,left.location);
  306. end;
  307. function ti8086addnode.first_addpointer: tnode;
  308. begin
  309. if is_hugepointer(left.resultdef) or is_hugepointer(right.resultdef) then
  310. result:=first_addhugepointer
  311. else
  312. result:=inherited;
  313. end;
  314. function ti8086addnode.first_addhugepointer: tnode;
  315. var
  316. procname:string;
  317. begin
  318. result:=nil;
  319. if (nodetype=subn) and is_hugepointer(left.resultdef) and is_hugepointer(right.resultdef) then
  320. procname:='fpc_hugeptr_sub_hugeptr'
  321. else
  322. begin
  323. case nodetype of
  324. addn:
  325. procname:='fpc_hugeptr_add_longint';
  326. subn:
  327. procname:='fpc_hugeptr_sub_longint';
  328. else
  329. internalerror(2014070301);
  330. end;
  331. if cs_hugeptr_arithmetic_normalization in current_settings.localswitches then
  332. procname:=procname+'_normalized';
  333. end;
  334. if is_hugepointer(left.resultdef) then
  335. result := ccallnode.createintern(procname,
  336. ccallparanode.create(right,
  337. ccallparanode.create(left,nil)))
  338. else
  339. result := ccallnode.createintern(procname,
  340. ccallparanode.create(left,
  341. ccallparanode.create(right,nil)));
  342. left := nil;
  343. right := nil;
  344. firstpass(result);
  345. end;
  346. function ti8086addnode.first_cmppointer: tnode;
  347. begin
  348. if is_hugepointer(left.resultdef) or is_hugepointer(right.resultdef) then
  349. result:=first_cmphugepointer
  350. else if is_farpointer(left.resultdef) or is_farpointer(right.resultdef) then
  351. result:=first_cmpfarpointer
  352. else
  353. result:=inherited;
  354. end;
  355. function ti8086addnode.first_cmphugepointer: tnode;
  356. var
  357. procname:string;
  358. begin
  359. result:=nil;
  360. if not (cs_hugeptr_comparison_normalization in current_settings.localswitches) then
  361. begin
  362. expectloc:=LOC_JUMP;
  363. exit;
  364. end;
  365. case nodetype of
  366. equaln:
  367. procname:='fpc_hugeptr_cmp_normalized_e';
  368. unequaln:
  369. procname:='fpc_hugeptr_cmp_normalized_ne';
  370. ltn:
  371. procname:='fpc_hugeptr_cmp_normalized_b';
  372. lten:
  373. procname:='fpc_hugeptr_cmp_normalized_be';
  374. gtn:
  375. procname:='fpc_hugeptr_cmp_normalized_a';
  376. gten:
  377. procname:='fpc_hugeptr_cmp_normalized_ae';
  378. else
  379. internalerror(2014070401);
  380. end;
  381. result := ccallnode.createintern(procname,
  382. ccallparanode.create(right,
  383. ccallparanode.create(left,nil)));
  384. left := nil;
  385. right := nil;
  386. firstpass(result);
  387. end;
  388. function ti8086addnode.first_cmpfarpointer: tnode;
  389. begin
  390. { = and <> are handled as a 32-bit comparison }
  391. if nodetype in [equaln,unequaln] then
  392. begin
  393. result:=nil;
  394. expectloc:=LOC_JUMP;
  395. end
  396. else
  397. begin
  398. result:=nil;
  399. expectloc:=LOC_FLAGS;
  400. end;
  401. end;
  402. procedure ti8086addnode.second_addfarpointer;
  403. var
  404. tmpreg : tregister;
  405. pointernode: tnode;
  406. begin
  407. pass_left_right;
  408. force_reg_left_right(true,true);
  409. set_result_location_reg;
  410. if (left.resultdef.typ=pointerdef) and (right.resultdef.typ<>pointerdef) then
  411. pointernode:=left
  412. else if (left.resultdef.typ<>pointerdef) and (right.resultdef.typ=pointerdef) then
  413. pointernode:=right
  414. else
  415. internalerror(2014040608);
  416. if not (nodetype in [addn,subn]) then
  417. internalerror(2014040602);
  418. if nodetype=addn then
  419. begin
  420. if (right.location.loc<>LOC_CONSTANT) then
  421. begin
  422. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,
  423. left.location.register,right.location.register,location.register);
  424. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  425. cg.GetNextReg(pointernode.location.register),cg.GetNextReg(location.register));
  426. end
  427. else
  428. begin
  429. if pointernode=left then
  430. begin
  431. { farptr_reg + int_const }
  432. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,
  433. right.location.value,left.location.register,location.register);
  434. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  435. cg.GetNextReg(left.location.register),cg.GetNextReg(location.register));
  436. end
  437. else
  438. begin
  439. { int_reg + farptr_const }
  440. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  441. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,
  442. right.location.value,tmpreg);
  443. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,
  444. left.location.register,tmpreg,location.register);
  445. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  446. cg.GetNextReg(tmpreg),cg.GetNextReg(location.register));
  447. end;
  448. end;
  449. end
  450. else { subtract is a special case since its not commutative }
  451. begin
  452. if (nf_swapped in flags) then
  453. swapleftright;
  454. { left can only be a pointer in this case, since (int-pointer) is not supported }
  455. if pointernode<>left then
  456. internalerror(2014040603);
  457. if left.location.loc<>LOC_CONSTANT then
  458. begin
  459. if right.location.loc<>LOC_CONSTANT then
  460. begin
  461. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_16,
  462. right.location.register,left.location.register,location.register);
  463. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  464. cg.GetNextReg(pointernode.location.register),cg.GetNextReg(location.register));
  465. end
  466. else
  467. begin
  468. { farptr_reg - int_const }
  469. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_16,
  470. right.location.value,left.location.register,location.register);
  471. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  472. cg.GetNextReg(left.location.register),cg.GetNextReg(location.register));
  473. end;
  474. end
  475. else
  476. begin
  477. { farptr_const - int_reg }
  478. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  479. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,
  480. left.location.value,tmpreg);
  481. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_16,
  482. right.location.register,tmpreg,location.register);
  483. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  484. cg.GetNextReg(tmpreg),cg.GetNextReg(location.register));
  485. end;
  486. end;
  487. end;
  488. procedure ti8086addnode.second_cmp64bit;
  489. var
  490. truelabel,
  491. falselabel : tasmlabel;
  492. hregister,
  493. hregister2 : tregister;
  494. href : treference;
  495. unsigned : boolean;
  496. procedure firstjmp64bitcmp;
  497. var
  498. oldnodetype : tnodetype;
  499. begin
  500. { the jump the sequence is a little bit hairy }
  501. case nodetype of
  502. ltn,gtn:
  503. begin
  504. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  505. { cheat a little bit for the negative test }
  506. toggleflag(nf_swapped);
  507. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  508. toggleflag(nf_swapped);
  509. end;
  510. lten,gten:
  511. begin
  512. oldnodetype:=nodetype;
  513. if nodetype=lten then
  514. nodetype:=ltn
  515. else
  516. nodetype:=gtn;
  517. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  518. { cheat for the negative test }
  519. if nodetype=ltn then
  520. nodetype:=gtn
  521. else
  522. nodetype:=ltn;
  523. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  524. nodetype:=oldnodetype;
  525. end;
  526. equaln:
  527. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  528. unequaln:
  529. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  530. else
  531. internalerror(2019051024);
  532. end;
  533. end;
  534. procedure middlejmp64bitcmp;
  535. var
  536. oldnodetype : tnodetype;
  537. begin
  538. { the jump the sequence is a little bit hairy }
  539. case nodetype of
  540. ltn,gtn:
  541. begin
  542. { the comparisaion of the low word have to be }
  543. { always unsigned! }
  544. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  545. { cheat a little bit for the negative test }
  546. toggleflag(nf_swapped);
  547. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.falselabel);
  548. toggleflag(nf_swapped);
  549. end;
  550. lten,gten:
  551. begin
  552. oldnodetype:=nodetype;
  553. if nodetype=lten then
  554. nodetype:=ltn
  555. else
  556. nodetype:=gtn;
  557. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  558. { cheat for the negative test }
  559. if nodetype=ltn then
  560. nodetype:=gtn
  561. else
  562. nodetype:=ltn;
  563. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.falselabel);
  564. nodetype:=oldnodetype;
  565. end;
  566. equaln:
  567. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  568. unequaln:
  569. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  570. else
  571. internalerror(2019051023);
  572. end;
  573. end;
  574. procedure lastjmp64bitcmp;
  575. begin
  576. { the jump the sequence is a little bit hairy }
  577. case nodetype of
  578. ltn,gtn,lten,gten:
  579. begin
  580. { the comparisaion of the low word have to be }
  581. { always unsigned! }
  582. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  583. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  584. end;
  585. equaln:
  586. begin
  587. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  588. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  589. end;
  590. unequaln:
  591. begin
  592. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  593. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  594. end;
  595. else
  596. internalerror(2019051022);
  597. end;
  598. end;
  599. begin
  600. truelabel:=nil;
  601. falselabel:=nil;
  602. pass_left_right;
  603. unsigned:=((left.resultdef.typ=orddef) and
  604. (torddef(left.resultdef).ordtype=u64bit)) or
  605. ((right.resultdef.typ=orddef) and
  606. (torddef(right.resultdef).ordtype=u64bit));
  607. { we have LOC_JUMP as result }
  608. current_asmdata.getjumplabel(truelabel);
  609. current_asmdata.getjumplabel(falselabel);
  610. location_reset_jump(location,truelabel,falselabel);
  611. { left and right no register? }
  612. { then one must be demanded }
  613. if (left.location.loc<>LOC_REGISTER) then
  614. begin
  615. if (right.location.loc<>LOC_REGISTER) then
  616. begin
  617. { we can reuse a CREGISTER for comparison }
  618. if (left.location.loc<>LOC_CREGISTER) then
  619. begin
  620. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  621. hregister2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  622. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,joinreg64(hregister,hregister2));
  623. location_freetemp(current_asmdata.CurrAsmList,left.location);
  624. location_reset(left.location,LOC_REGISTER,left.location.size);
  625. left.location.register64.reglo:=hregister;
  626. left.location.register64.reghi:=hregister2;
  627. end;
  628. end
  629. else
  630. begin
  631. location_swap(left.location,right.location);
  632. toggleflag(nf_swapped);
  633. end;
  634. end;
  635. { at this point, left.location.loc should be LOC_REGISTER }
  636. if right.location.loc=LOC_REGISTER then
  637. begin
  638. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reghi),cg.GetNextReg(left.location.register64.reghi));
  639. firstjmp64bitcmp;
  640. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  641. middlejmp64bitcmp;
  642. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reglo),cg.GetNextReg(left.location.register64.reglo));
  643. middlejmp64bitcmp;
  644. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  645. lastjmp64bitcmp;
  646. end
  647. else
  648. begin
  649. case right.location.loc of
  650. LOC_CREGISTER :
  651. begin
  652. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reghi),cg.GetNextReg(left.location.register64.reghi));
  653. firstjmp64bitcmp;
  654. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  655. middlejmp64bitcmp;
  656. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reglo),cg.GetNextReg(left.location.register64.reglo));
  657. middlejmp64bitcmp;
  658. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  659. lastjmp64bitcmp;
  660. end;
  661. LOC_CREFERENCE,
  662. LOC_REFERENCE :
  663. begin
  664. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  665. href:=right.location.reference;
  666. inc(href.offset,6);
  667. emit_ref_reg(A_CMP,S_W,href,cg.GetNextReg(left.location.register64.reghi));
  668. firstjmp64bitcmp;
  669. dec(href.offset,2);
  670. emit_ref_reg(A_CMP,S_W,href,left.location.register64.reghi);
  671. middlejmp64bitcmp;
  672. dec(href.offset,2);
  673. emit_ref_reg(A_CMP,S_W,href,cg.GetNextReg(left.location.register64.reglo));
  674. middlejmp64bitcmp;
  675. emit_ref_reg(A_CMP,S_W,right.location.reference,left.location.register64.reglo);
  676. lastjmp64bitcmp;
  677. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  678. location_freetemp(current_asmdata.CurrAsmList,right.location);
  679. end;
  680. LOC_CONSTANT :
  681. begin
  682. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value64 shr 48) and $FFFF),cg.GetNextReg(left.location.register64.reghi)));
  683. firstjmp64bitcmp;
  684. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value64 shr 32) and $FFFF),left.location.register64.reghi));
  685. middlejmp64bitcmp;
  686. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value64 shr 16) and $FFFF),cg.GetNextReg(left.location.register64.reglo)));
  687. middlejmp64bitcmp;
  688. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value64 and $FFFF),left.location.register64.reglo));
  689. lastjmp64bitcmp;
  690. end;
  691. else
  692. internalerror(200203282);
  693. end;
  694. end;
  695. end;
  696. procedure ti8086addnode.second_cmp32bit;
  697. var
  698. truelabel,
  699. falselabel: tasmlabel;
  700. hregister : tregister;
  701. href : treference;
  702. unsigned : boolean;
  703. procedure firstjmp32bitcmp;
  704. var
  705. oldnodetype : tnodetype;
  706. begin
  707. { the jump the sequence is a little bit hairy }
  708. case nodetype of
  709. ltn,gtn:
  710. begin
  711. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  712. { cheat a little bit for the negative test }
  713. toggleflag(nf_swapped);
  714. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  715. toggleflag(nf_swapped);
  716. end;
  717. lten,gten:
  718. begin
  719. oldnodetype:=nodetype;
  720. if nodetype=lten then
  721. nodetype:=ltn
  722. else
  723. nodetype:=gtn;
  724. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  725. { cheat for the negative test }
  726. if nodetype=ltn then
  727. nodetype:=gtn
  728. else
  729. nodetype:=ltn;
  730. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  731. nodetype:=oldnodetype;
  732. end;
  733. equaln:
  734. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  735. unequaln:
  736. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  737. else
  738. internalerror(2019051021);
  739. end;
  740. end;
  741. procedure secondjmp32bitcmp;
  742. begin
  743. { the jump the sequence is a little bit hairy }
  744. case nodetype of
  745. ltn,gtn,lten,gten:
  746. begin
  747. { the comparisaion of the low dword have to be }
  748. { always unsigned! }
  749. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  750. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  751. end;
  752. equaln:
  753. begin
  754. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  755. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  756. end;
  757. unequaln:
  758. begin
  759. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  760. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  761. end;
  762. else
  763. internalerror(2019051020);
  764. end;
  765. end;
  766. begin
  767. truelabel:=nil;
  768. falselabel:=nil;
  769. pass_left_right;
  770. unsigned:=((left.resultdef.typ=orddef) and
  771. (torddef(left.resultdef).ordtype=u32bit)) or
  772. ((right.resultdef.typ=orddef) and
  773. (torddef(right.resultdef).ordtype=u32bit)) or
  774. is_hugepointer(left.resultdef);
  775. { we have LOC_JUMP as result }
  776. current_asmdata.getjumplabel(truelabel);
  777. current_asmdata.getjumplabel(falselabel);
  778. location_reset_jump(location,truelabel,falselabel);
  779. { left and right no register? }
  780. { then one must be demanded }
  781. if (left.location.loc<>LOC_REGISTER) then
  782. begin
  783. if (right.location.loc<>LOC_REGISTER) then
  784. begin
  785. { we can reuse a CREGISTER for comparison }
  786. if (left.location.loc<>LOC_CREGISTER) then
  787. begin
  788. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  789. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_32,left.location,hregister);
  790. location_freetemp(current_asmdata.CurrAsmList,left.location);
  791. location_reset(left.location,LOC_REGISTER,left.location.size);
  792. left.location.register:=hregister;
  793. end;
  794. end
  795. else
  796. begin
  797. location_swap(left.location,right.location);
  798. toggleflag(nf_swapped);
  799. end;
  800. end;
  801. { at this point, left.location.loc should be LOC_REGISTER }
  802. if right.location.loc=LOC_REGISTER then
  803. begin
  804. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register),cg.GetNextReg(left.location.register));
  805. firstjmp32bitcmp;
  806. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  807. secondjmp32bitcmp;
  808. end
  809. else
  810. begin
  811. case right.location.loc of
  812. LOC_CREGISTER :
  813. begin
  814. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register),cg.GetNextReg(left.location.register));
  815. firstjmp32bitcmp;
  816. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  817. secondjmp32bitcmp;
  818. end;
  819. LOC_CREFERENCE,
  820. LOC_REFERENCE :
  821. begin
  822. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  823. href:=right.location.reference;
  824. inc(href.offset,2);
  825. emit_ref_reg(A_CMP,S_W,href,cg.GetNextReg(left.location.register));
  826. firstjmp32bitcmp;
  827. dec(href.offset,2);
  828. emit_ref_reg(A_CMP,S_W,href,left.location.register);
  829. secondjmp32bitcmp;
  830. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  831. location_freetemp(current_asmdata.CurrAsmList,right.location);
  832. end;
  833. LOC_CONSTANT :
  834. begin
  835. if (right.location.value=0) and (nodetype in [equaln,unequaln]) and (left.location.loc=LOC_REGISTER) then
  836. begin
  837. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_W,cg.GetNextReg(left.location.register),left.location.register));
  838. secondjmp32bitcmp;
  839. end
  840. else
  841. begin
  842. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value shr 16) and $FFFF),cg.GetNextReg(left.location.register)));
  843. firstjmp32bitcmp;
  844. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value and $FFFF),left.location.register));
  845. secondjmp32bitcmp;
  846. end;
  847. end;
  848. else
  849. internalerror(2002032801);
  850. end;
  851. end;
  852. end;
  853. procedure ti8086addnode.second_cmpfarpointer;
  854. begin
  855. { handle = and <> as a 32-bit comparison }
  856. if nodetype in [equaln,unequaln] then
  857. begin
  858. second_cmp32bit;
  859. exit;
  860. end;
  861. pass_left_right;
  862. { <, >, <= and >= compare the 16-bit offset only }
  863. if (right.location.loc=LOC_CONSTANT) and
  864. (left.location.loc in [LOC_REFERENCE, LOC_CREFERENCE])
  865. then
  866. begin
  867. emit_const_ref(A_CMP, S_W, word(right.location.value), left.location.reference);
  868. location_freetemp(current_asmdata.CurrAsmList,left.location);
  869. end
  870. else
  871. begin
  872. { left location is not a register? }
  873. if left.location.loc<>LOC_REGISTER then
  874. begin
  875. { if right is register then we can swap the locations }
  876. if right.location.loc=LOC_REGISTER then
  877. begin
  878. location_swap(left.location,right.location);
  879. toggleflag(nf_swapped);
  880. end
  881. else
  882. begin
  883. { maybe we can reuse a constant register when the
  884. operation is a comparison that doesn't change the
  885. value of the register }
  886. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u16inttype,true);
  887. end;
  888. end;
  889. emit_generic_code(A_CMP,OS_16,true,false,false);
  890. location_freetemp(current_asmdata.CurrAsmList,right.location);
  891. location_freetemp(current_asmdata.CurrAsmList,left.location);
  892. end;
  893. location_reset(location,LOC_FLAGS,OS_NO);
  894. location.resflags:=getresflags(true);
  895. end;
  896. procedure ti8086addnode.second_cmpordinal;
  897. begin
  898. if is_farpointer(left.resultdef) then
  899. second_cmpfarpointer
  900. else if is_32bit(left.resultdef) or is_hugepointer(left.resultdef) or is_farprocvar(left.resultdef) then
  901. second_cmp32bit
  902. else
  903. inherited second_cmpordinal;
  904. end;
  905. {*****************************************************************************
  906. x86 MUL
  907. *****************************************************************************}
  908. procedure ti8086addnode.second_mul(unsigned: boolean);
  909. var reg:Tregister;
  910. ref:Treference;
  911. use_ref:boolean;
  912. hl4 : tasmlabel;
  913. overflowcheck: boolean;
  914. const
  915. asmops: array[boolean] of tasmop = (A_IMUL, A_MUL);
  916. begin
  917. reg:=NR_NO;
  918. reference_reset(ref,sizeof(pint),[]);
  919. pass_left_right;
  920. overflowcheck:=needoverflowcheck;
  921. { MUL is faster than IMUL on the 8086 & 8088 (and equal in speed on 286+),
  922. but it's only safe to use in place of IMUL when overflow checking is off
  923. and we're doing a 16-bit>16-bit multiplication }
  924. if not overflowcheck and
  925. (not is_32bitint(resultdef)) then
  926. unsigned:=true;
  927. {The location.register will be filled in later (JM)}
  928. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  929. { Mul supports registers and references, so if not register/reference,
  930. load the location into a register. }
  931. use_ref:=false;
  932. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  933. reg:=left.location.register
  934. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  935. begin
  936. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  937. ref:=left.location.reference;
  938. use_ref:=true;
  939. end
  940. else
  941. begin
  942. {LOC_CONSTANT for example.}
  943. reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  944. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,osuinttype,left.location,reg);
  945. end;
  946. {Allocate AX.}
  947. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  948. {Load the right value.}
  949. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,osuinttype,right.location,NR_AX);
  950. {Also allocate DX, since it is also modified by a mul (JM).}
  951. cg.getcpuregister(current_asmdata.CurrAsmList,NR_DX);
  952. if overflowcheck and
  953. { 16->32 bit cannot overflow }
  954. (not is_32bitint(resultdef)) then
  955. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  956. if use_ref then
  957. emit_ref(asmops[unsigned],S_W,ref)
  958. else
  959. emit_reg(asmops[unsigned],S_W,reg);
  960. if overflowcheck and
  961. { 16->32 bit cannot overflow }
  962. (not is_32bitint(resultdef)) then
  963. begin
  964. current_asmdata.getjumplabel(hl4);
  965. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4);
  966. cg.a_reg_dealloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  967. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  968. cg.a_label(current_asmdata.CurrAsmList,hl4);
  969. end;
  970. {Free AX,DX}
  971. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_DX);
  972. if is_32bitint(resultdef) then
  973. begin
  974. {Allocate an imaginary 32-bit register, which consists of a pair of
  975. 16-bit registers and store DX:AX into it}
  976. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  977. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_DX,cg.GetNextReg(location.register));
  978. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  979. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  980. end
  981. else
  982. begin
  983. {Allocate a new register and store the result in AX in it.}
  984. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  985. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  986. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  987. end;
  988. location_freetemp(current_asmdata.CurrAsmList,left.location);
  989. location_freetemp(current_asmdata.CurrAsmList,right.location);
  990. end;
  991. begin
  992. caddnode:=ti8086addnode;
  993. end.