n8086add.pas 41 KB

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