n8086add.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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(2002080202);
  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(2002080203);
  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(nf_has_pointerdiv in flags) then
  123. internalerror(2008030101);
  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(200109051);
  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. { at this point, left.location.loc should be LOC_REGISTER }
  242. if right.location.loc=LOC_REGISTER then
  243. begin
  244. { when swapped another result register }
  245. if (nodetype=subn) and (nf_swapped in flags) then
  246. begin
  247. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  248. left.location.register64,
  249. right.location.register64);
  250. location_swap(left.location,right.location);
  251. toggleflag(nf_swapped);
  252. end
  253. else
  254. begin
  255. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  256. right.location.register64,
  257. left.location.register64);
  258. end;
  259. end
  260. else
  261. begin
  262. { right.location<>LOC_REGISTER }
  263. if (nodetype=subn) and (nf_swapped in flags) then
  264. begin
  265. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  266. cg64.a_load64low_loc_reg(current_asmdata.CurrAsmList,right.location,r);
  267. emit_reg_reg(op1,S_W,left.location.register64.reglo,r);
  268. emit_reg_reg(op2,S_W,cg.GetNextReg(left.location.register64.reglo),cg.GetNextReg(r));
  269. emit_reg_reg(A_MOV,S_W,r,left.location.register64.reglo);
  270. emit_reg_reg(A_MOV,S_W,cg.GetNextReg(r),cg.GetNextReg(left.location.register64.reglo));
  271. cg64.a_load64high_loc_reg(current_asmdata.CurrAsmList,right.location,r);
  272. { the carry flag is still ok }
  273. emit_reg_reg(op2,S_W,left.location.register64.reghi,r);
  274. emit_reg_reg(op2,S_W,cg.GetNextReg(left.location.register64.reghi),cg.GetNextReg(r));
  275. emit_reg_reg(A_MOV,S_W,r,left.location.register64.reghi);
  276. emit_reg_reg(A_MOV,S_W,cg.GetNextReg(r),cg.GetNextReg(left.location.register64.reghi));
  277. end
  278. else
  279. begin
  280. cg64.a_op64_loc_reg(current_asmdata.CurrAsmList,op,location.size,right.location,
  281. left.location.register64);
  282. end;
  283. location_freetemp(current_asmdata.CurrAsmList,right.location);
  284. end;
  285. { only in case of overflow operations }
  286. { produce overflow code }
  287. { we must put it here directly, because sign of operation }
  288. { is in unsigned VAR!! }
  289. if mboverflow then
  290. begin
  291. if cs_check_overflow in current_settings.localswitches then
  292. begin
  293. current_asmdata.getjumplabel(hl4);
  294. if unsigned then
  295. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4)
  296. else
  297. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NO,hl4);
  298. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  299. cg.a_label(current_asmdata.CurrAsmList,hl4);
  300. end;
  301. end;
  302. location_copy(location,left.location);
  303. end;
  304. function ti8086addnode.first_addpointer: tnode;
  305. begin
  306. if is_hugepointer(left.resultdef) or is_hugepointer(right.resultdef) then
  307. result:=first_addhugepointer
  308. else
  309. result:=inherited;
  310. end;
  311. function ti8086addnode.first_addhugepointer: tnode;
  312. var
  313. procname:string;
  314. begin
  315. result:=nil;
  316. if (nodetype=subn) and is_hugepointer(left.resultdef) and is_hugepointer(right.resultdef) then
  317. procname:='fpc_hugeptr_sub_hugeptr'
  318. else
  319. begin
  320. case nodetype of
  321. addn:
  322. procname:='fpc_hugeptr_add_longint';
  323. subn:
  324. procname:='fpc_hugeptr_sub_longint';
  325. else
  326. internalerror(2014070301);
  327. end;
  328. if cs_hugeptr_arithmetic_normalization in current_settings.localswitches then
  329. procname:=procname+'_normalized';
  330. end;
  331. if is_hugepointer(left.resultdef) then
  332. result := ccallnode.createintern(procname,
  333. ccallparanode.create(right,
  334. ccallparanode.create(left,nil)))
  335. else
  336. result := ccallnode.createintern(procname,
  337. ccallparanode.create(left,
  338. ccallparanode.create(right,nil)));
  339. left := nil;
  340. right := nil;
  341. firstpass(result);
  342. end;
  343. function ti8086addnode.first_cmppointer: tnode;
  344. begin
  345. if is_hugepointer(left.resultdef) or is_hugepointer(right.resultdef) then
  346. result:=first_cmphugepointer
  347. else if is_farpointer(left.resultdef) or is_farpointer(right.resultdef) then
  348. result:=first_cmpfarpointer
  349. else
  350. result:=inherited;
  351. end;
  352. function ti8086addnode.first_cmphugepointer: tnode;
  353. var
  354. procname:string;
  355. begin
  356. result:=nil;
  357. if not (cs_hugeptr_comparison_normalization in current_settings.localswitches) then
  358. begin
  359. expectloc:=LOC_JUMP;
  360. exit;
  361. end;
  362. case nodetype of
  363. equaln:
  364. procname:='fpc_hugeptr_cmp_normalized_e';
  365. unequaln:
  366. procname:='fpc_hugeptr_cmp_normalized_ne';
  367. ltn:
  368. procname:='fpc_hugeptr_cmp_normalized_b';
  369. lten:
  370. procname:='fpc_hugeptr_cmp_normalized_be';
  371. gtn:
  372. procname:='fpc_hugeptr_cmp_normalized_a';
  373. gten:
  374. procname:='fpc_hugeptr_cmp_normalized_ae';
  375. else
  376. internalerror(2014070401);
  377. end;
  378. result := ccallnode.createintern(procname,
  379. ccallparanode.create(right,
  380. ccallparanode.create(left,nil)));
  381. left := nil;
  382. right := nil;
  383. firstpass(result);
  384. end;
  385. function ti8086addnode.first_cmpfarpointer: tnode;
  386. begin
  387. { = and <> are handled as a 32-bit comparison }
  388. if nodetype in [equaln,unequaln] then
  389. begin
  390. result:=nil;
  391. expectloc:=LOC_JUMP;
  392. end
  393. else
  394. begin
  395. result:=nil;
  396. expectloc:=LOC_FLAGS;
  397. end;
  398. end;
  399. procedure ti8086addnode.second_addfarpointer;
  400. var
  401. tmpreg : tregister;
  402. pointernode: tnode;
  403. begin
  404. pass_left_right;
  405. force_reg_left_right(false,true);
  406. set_result_location_reg;
  407. if (left.resultdef.typ=pointerdef) and (right.resultdef.typ<>pointerdef) then
  408. pointernode:=left
  409. else if (left.resultdef.typ<>pointerdef) and (right.resultdef.typ=pointerdef) then
  410. pointernode:=right
  411. else
  412. internalerror(2014040601);
  413. if not (nodetype in [addn,subn]) then
  414. internalerror(2014040602);
  415. if nodetype=addn then
  416. begin
  417. if (right.location.loc<>LOC_CONSTANT) then
  418. begin
  419. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,
  420. left.location.register,right.location.register,location.register);
  421. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  422. cg.GetNextReg(pointernode.location.register),cg.GetNextReg(location.register));
  423. end
  424. else
  425. begin
  426. if pointernode=left then
  427. begin
  428. { farptr_reg + int_const }
  429. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,
  430. right.location.value,left.location.register,location.register);
  431. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  432. cg.GetNextReg(left.location.register),cg.GetNextReg(location.register));
  433. end
  434. else
  435. begin
  436. { int_reg + farptr_const }
  437. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  438. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,
  439. right.location.value,tmpreg);
  440. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,
  441. left.location.register,tmpreg,location.register);
  442. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  443. cg.GetNextReg(tmpreg),cg.GetNextReg(location.register));
  444. end;
  445. end;
  446. end
  447. else { subtract is a special case since its not commutative }
  448. begin
  449. if (nf_swapped in flags) then
  450. swapleftright;
  451. { left can only be a pointer in this case, since (int-pointer) is not supported }
  452. if pointernode<>left then
  453. internalerror(2014040603);
  454. if left.location.loc<>LOC_CONSTANT then
  455. begin
  456. if right.location.loc<>LOC_CONSTANT then
  457. begin
  458. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_16,
  459. right.location.register,left.location.register,location.register);
  460. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  461. cg.GetNextReg(pointernode.location.register),cg.GetNextReg(location.register));
  462. end
  463. else
  464. begin
  465. { farptr_reg - int_const }
  466. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_16,
  467. right.location.value,left.location.register,location.register);
  468. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  469. cg.GetNextReg(left.location.register),cg.GetNextReg(location.register));
  470. end;
  471. end
  472. else
  473. begin
  474. { farptr_const - int_reg }
  475. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  476. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,
  477. left.location.value,tmpreg);
  478. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_16,
  479. right.location.register,tmpreg,location.register);
  480. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  481. cg.GetNextReg(tmpreg),cg.GetNextReg(location.register));
  482. end;
  483. end;
  484. end;
  485. procedure ti8086addnode.second_cmp64bit;
  486. var
  487. truelabel,
  488. falselabel : tasmlabel;
  489. hregister,
  490. hregister2 : tregister;
  491. href : treference;
  492. unsigned : boolean;
  493. procedure firstjmp64bitcmp;
  494. var
  495. oldnodetype : tnodetype;
  496. begin
  497. {$ifdef OLDREGVARS}
  498. load_all_regvars(current_asmdata.CurrAsmList);
  499. {$endif OLDREGVARS}
  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. end;
  531. end;
  532. procedure middlejmp64bitcmp;
  533. var
  534. oldnodetype : tnodetype;
  535. begin
  536. {$ifdef OLDREGVARS}
  537. load_all_regvars(current_asmdata.CurrAsmList);
  538. {$endif OLDREGVARS}
  539. { the jump the sequence is a little bit hairy }
  540. case nodetype of
  541. ltn,gtn:
  542. begin
  543. { the comparisaion of the low word have to be }
  544. { always unsigned! }
  545. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  546. { cheat a little bit for the negative test }
  547. toggleflag(nf_swapped);
  548. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.falselabel);
  549. toggleflag(nf_swapped);
  550. end;
  551. lten,gten:
  552. begin
  553. oldnodetype:=nodetype;
  554. if nodetype=lten then
  555. nodetype:=ltn
  556. else
  557. nodetype:=gtn;
  558. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  559. { cheat for the negative test }
  560. if nodetype=ltn then
  561. nodetype:=gtn
  562. else
  563. nodetype:=ltn;
  564. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.falselabel);
  565. nodetype:=oldnodetype;
  566. end;
  567. equaln:
  568. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  569. unequaln:
  570. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  571. end;
  572. end;
  573. procedure lastjmp64bitcmp;
  574. begin
  575. { the jump the sequence is a little bit hairy }
  576. case nodetype of
  577. ltn,gtn,lten,gten:
  578. begin
  579. { the comparisaion of the low word have to be }
  580. { always unsigned! }
  581. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  582. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  583. end;
  584. equaln:
  585. begin
  586. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  587. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  588. end;
  589. unequaln:
  590. begin
  591. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  592. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  593. end;
  594. end;
  595. end;
  596. begin
  597. truelabel:=nil;
  598. falselabel:=nil;
  599. pass_left_right;
  600. unsigned:=((left.resultdef.typ=orddef) and
  601. (torddef(left.resultdef).ordtype=u64bit)) or
  602. ((right.resultdef.typ=orddef) and
  603. (torddef(right.resultdef).ordtype=u64bit));
  604. { we have LOC_JUMP as result }
  605. current_asmdata.getjumplabel(truelabel);
  606. current_asmdata.getjumplabel(falselabel);
  607. location_reset_jump(location,truelabel,falselabel);
  608. { left and right no register? }
  609. { then one must be demanded }
  610. if (left.location.loc<>LOC_REGISTER) then
  611. begin
  612. if (right.location.loc<>LOC_REGISTER) then
  613. begin
  614. { we can reuse a CREGISTER for comparison }
  615. if (left.location.loc<>LOC_CREGISTER) then
  616. begin
  617. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  618. hregister2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  619. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,joinreg64(hregister,hregister2));
  620. location_freetemp(current_asmdata.CurrAsmList,left.location);
  621. location_reset(left.location,LOC_REGISTER,left.location.size);
  622. left.location.register64.reglo:=hregister;
  623. left.location.register64.reghi:=hregister2;
  624. end;
  625. end
  626. else
  627. begin
  628. location_swap(left.location,right.location);
  629. toggleflag(nf_swapped);
  630. end;
  631. end;
  632. { at this point, left.location.loc should be LOC_REGISTER }
  633. if right.location.loc=LOC_REGISTER then
  634. begin
  635. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reghi),cg.GetNextReg(left.location.register64.reghi));
  636. firstjmp64bitcmp;
  637. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  638. middlejmp64bitcmp;
  639. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reglo),cg.GetNextReg(left.location.register64.reglo));
  640. middlejmp64bitcmp;
  641. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  642. lastjmp64bitcmp;
  643. end
  644. else
  645. begin
  646. case right.location.loc of
  647. LOC_CREGISTER :
  648. begin
  649. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reghi),cg.GetNextReg(left.location.register64.reghi));
  650. firstjmp64bitcmp;
  651. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  652. middlejmp64bitcmp;
  653. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reglo),cg.GetNextReg(left.location.register64.reglo));
  654. middlejmp64bitcmp;
  655. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  656. lastjmp64bitcmp;
  657. end;
  658. LOC_CREFERENCE,
  659. LOC_REFERENCE :
  660. begin
  661. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  662. href:=right.location.reference;
  663. inc(href.offset,6);
  664. emit_ref_reg(A_CMP,S_W,href,cg.GetNextReg(left.location.register64.reghi));
  665. firstjmp64bitcmp;
  666. dec(href.offset,2);
  667. emit_ref_reg(A_CMP,S_W,href,left.location.register64.reghi);
  668. middlejmp64bitcmp;
  669. dec(href.offset,2);
  670. emit_ref_reg(A_CMP,S_W,href,cg.GetNextReg(left.location.register64.reglo));
  671. middlejmp64bitcmp;
  672. emit_ref_reg(A_CMP,S_W,right.location.reference,left.location.register64.reglo);
  673. lastjmp64bitcmp;
  674. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  675. location_freetemp(current_asmdata.CurrAsmList,right.location);
  676. end;
  677. LOC_CONSTANT :
  678. begin
  679. 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)));
  680. firstjmp64bitcmp;
  681. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value64 shr 32) and $FFFF),left.location.register64.reghi));
  682. middlejmp64bitcmp;
  683. 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)));
  684. middlejmp64bitcmp;
  685. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value64 and $FFFF),left.location.register64.reglo));
  686. lastjmp64bitcmp;
  687. end;
  688. else
  689. internalerror(200203282);
  690. end;
  691. end;
  692. end;
  693. procedure ti8086addnode.second_cmp32bit;
  694. var
  695. truelabel,
  696. falselabel: tasmlabel;
  697. hregister : tregister;
  698. href : treference;
  699. unsigned : boolean;
  700. procedure firstjmp32bitcmp;
  701. var
  702. oldnodetype : tnodetype;
  703. begin
  704. {$ifdef OLDREGVARS}
  705. load_all_regvars(current_asmdata.CurrAsmList);
  706. {$endif OLDREGVARS}
  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. end;
  738. end;
  739. procedure secondjmp32bitcmp;
  740. begin
  741. { the jump the sequence is a little bit hairy }
  742. case nodetype of
  743. ltn,gtn,lten,gten:
  744. begin
  745. { the comparisaion of the low dword have to be }
  746. { always unsigned! }
  747. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  748. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  749. end;
  750. equaln:
  751. begin
  752. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  753. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  754. end;
  755. unequaln:
  756. begin
  757. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  758. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  759. end;
  760. end;
  761. end;
  762. begin
  763. truelabel:=nil;
  764. falselabel:=nil;
  765. pass_left_right;
  766. unsigned:=((left.resultdef.typ=orddef) and
  767. (torddef(left.resultdef).ordtype=u32bit)) or
  768. ((right.resultdef.typ=orddef) and
  769. (torddef(right.resultdef).ordtype=u32bit)) or
  770. is_hugepointer(left.resultdef);
  771. { we have LOC_JUMP as result }
  772. current_asmdata.getjumplabel(truelabel);
  773. current_asmdata.getjumplabel(falselabel);
  774. location_reset_jump(location,truelabel,falselabel);
  775. { left and right no register? }
  776. { then one must be demanded }
  777. if (left.location.loc<>LOC_REGISTER) then
  778. begin
  779. if (right.location.loc<>LOC_REGISTER) then
  780. begin
  781. { we can reuse a CREGISTER for comparison }
  782. if (left.location.loc<>LOC_CREGISTER) then
  783. begin
  784. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  785. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_32,left.location,hregister);
  786. location_freetemp(current_asmdata.CurrAsmList,left.location);
  787. location_reset(left.location,LOC_REGISTER,left.location.size);
  788. left.location.register:=hregister;
  789. end;
  790. end
  791. else
  792. begin
  793. location_swap(left.location,right.location);
  794. toggleflag(nf_swapped);
  795. end;
  796. end;
  797. { at this point, left.location.loc should be LOC_REGISTER }
  798. if right.location.loc=LOC_REGISTER then
  799. begin
  800. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register),cg.GetNextReg(left.location.register));
  801. firstjmp32bitcmp;
  802. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  803. secondjmp32bitcmp;
  804. end
  805. else
  806. begin
  807. case right.location.loc of
  808. LOC_CREGISTER :
  809. begin
  810. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register),cg.GetNextReg(left.location.register));
  811. firstjmp32bitcmp;
  812. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  813. secondjmp32bitcmp;
  814. end;
  815. LOC_CREFERENCE,
  816. LOC_REFERENCE :
  817. begin
  818. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  819. href:=right.location.reference;
  820. inc(href.offset,2);
  821. emit_ref_reg(A_CMP,S_W,href,cg.GetNextReg(left.location.register));
  822. firstjmp32bitcmp;
  823. dec(href.offset,2);
  824. emit_ref_reg(A_CMP,S_W,href,left.location.register);
  825. secondjmp32bitcmp;
  826. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  827. location_freetemp(current_asmdata.CurrAsmList,right.location);
  828. end;
  829. LOC_CONSTANT :
  830. begin
  831. if (right.location.value=0) and (nodetype in [equaln,unequaln]) and (left.location.loc=LOC_REGISTER) then
  832. begin
  833. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_W,cg.GetNextReg(left.location.register),left.location.register));
  834. secondjmp32bitcmp;
  835. end
  836. else
  837. begin
  838. 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)));
  839. firstjmp32bitcmp;
  840. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value and $FFFF),left.location.register));
  841. secondjmp32bitcmp;
  842. end;
  843. end;
  844. else
  845. internalerror(200203282);
  846. end;
  847. end;
  848. end;
  849. procedure ti8086addnode.second_cmpfarpointer;
  850. begin
  851. { handle = and <> as a 32-bit comparison }
  852. if nodetype in [equaln,unequaln] then
  853. begin
  854. second_cmp32bit;
  855. exit;
  856. end;
  857. pass_left_right;
  858. { <, >, <= and >= compare the 16-bit offset only }
  859. if (right.location.loc=LOC_CONSTANT) and
  860. (left.location.loc in [LOC_REFERENCE, LOC_CREFERENCE])
  861. then
  862. begin
  863. emit_const_ref(A_CMP, S_W, word(right.location.value), left.location.reference);
  864. location_freetemp(current_asmdata.CurrAsmList,left.location);
  865. end
  866. else
  867. begin
  868. { left location is not a register? }
  869. if left.location.loc<>LOC_REGISTER then
  870. begin
  871. { if right is register then we can swap the locations }
  872. if right.location.loc=LOC_REGISTER then
  873. begin
  874. location_swap(left.location,right.location);
  875. toggleflag(nf_swapped);
  876. end
  877. else
  878. begin
  879. { maybe we can reuse a constant register when the
  880. operation is a comparison that doesn't change the
  881. value of the register }
  882. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u16inttype,true);
  883. end;
  884. end;
  885. emit_generic_code(A_CMP,OS_16,true,false,false);
  886. location_freetemp(current_asmdata.CurrAsmList,right.location);
  887. location_freetemp(current_asmdata.CurrAsmList,left.location);
  888. end;
  889. location_reset(location,LOC_FLAGS,OS_NO);
  890. location.resflags:=getresflags(true);
  891. end;
  892. procedure ti8086addnode.second_cmpordinal;
  893. begin
  894. if is_farpointer(left.resultdef) then
  895. second_cmpfarpointer
  896. else if is_32bit(left.resultdef) or is_hugepointer(left.resultdef) or is_farprocvar(left.resultdef) then
  897. second_cmp32bit
  898. else
  899. inherited second_cmpordinal;
  900. end;
  901. {*****************************************************************************
  902. x86 MUL
  903. *****************************************************************************}
  904. procedure ti8086addnode.second_mul(unsigned: boolean);
  905. var reg:Tregister;
  906. ref:Treference;
  907. use_ref:boolean;
  908. hl4 : tasmlabel;
  909. const
  910. asmops: array[boolean] of tasmop = (A_IMUL, A_MUL);
  911. begin
  912. reg:=NR_NO;
  913. reference_reset(ref,sizeof(pint),[]);
  914. pass_left_right;
  915. { MUL is faster than IMUL on the 8086 & 8088 (and equal in speed on 286+),
  916. but it's only safe to use in place of IMUL when overflow checking is off
  917. and we're doing a 16-bit>16-bit multiplication }
  918. if not (cs_check_overflow in current_settings.localswitches) and
  919. (not is_32bitint(resultdef)) then
  920. unsigned:=true;
  921. {The location.register will be filled in later (JM)}
  922. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  923. { Mul supports registers and references, so if not register/reference,
  924. load the location into a register. }
  925. use_ref:=false;
  926. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  927. reg:=left.location.register
  928. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  929. begin
  930. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  931. ref:=left.location.reference;
  932. use_ref:=true;
  933. end
  934. else
  935. begin
  936. {LOC_CONSTANT for example.}
  937. reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  938. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,osuinttype,left.location,reg);
  939. end;
  940. {Allocate AX.}
  941. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  942. {Load the right value.}
  943. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,osuinttype,right.location,NR_AX);
  944. {Also allocate DX, since it is also modified by a mul (JM).}
  945. cg.getcpuregister(current_asmdata.CurrAsmList,NR_DX);
  946. if use_ref then
  947. emit_ref(asmops[unsigned],S_W,ref)
  948. else
  949. emit_reg(asmops[unsigned],S_W,reg);
  950. if (cs_check_overflow in current_settings.localswitches) and
  951. { 16->32 bit cannot overflow }
  952. (not is_32bitint(resultdef)) then
  953. begin
  954. current_asmdata.getjumplabel(hl4);
  955. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4);
  956. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  957. cg.a_label(current_asmdata.CurrAsmList,hl4);
  958. end;
  959. {Free AX,DX}
  960. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_DX);
  961. if is_32bitint(resultdef) then
  962. begin
  963. {Allocate an imaginary 32-bit register, which consists of a pair of
  964. 16-bit registers and store DX:AX into it}
  965. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  966. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_DX,cg.GetNextReg(location.register));
  967. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  968. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  969. end
  970. else
  971. begin
  972. {Allocate a new register and store the result in AX in it.}
  973. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  974. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  975. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  976. end;
  977. location_freetemp(current_asmdata.CurrAsmList,left.location);
  978. location_freetemp(current_asmdata.CurrAsmList,right.location);
  979. end;
  980. begin
  981. caddnode:=ti8086addnode;
  982. end.