n8086add.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. procedure second_addordinal; override;
  32. procedure second_add64bit;override;
  33. procedure second_addfarpointer;
  34. procedure second_cmp64bit;override;
  35. procedure second_cmp32bit;
  36. procedure second_cmpfarpointer;
  37. procedure second_cmpordinal;override;
  38. procedure second_mul(unsigned: boolean);
  39. end;
  40. implementation
  41. uses
  42. globtype,systems,
  43. cutils,verbose,globals,constexp,pass_1,
  44. symconst,symdef,symtype,paramgr,defutil,
  45. aasmbase,aasmtai,aasmdata,aasmcpu,
  46. cgbase,procinfo,
  47. ncal,ncon,nset,cgutils,tgobj,
  48. cga,ncgutil,cgobj,cg64f32,cgx86,
  49. hlcgobj;
  50. {*****************************************************************************
  51. simplify
  52. *****************************************************************************}
  53. function ti8086addnode.simplify(forinline: boolean): tnode;
  54. var
  55. t : tnode;
  56. lt,rt: tnodetype;
  57. rd,ld: tdef;
  58. rv,lv,v: tconstexprint;
  59. begin
  60. { load easier access variables }
  61. rd:=right.resultdef;
  62. ld:=left.resultdef;
  63. rt:=right.nodetype;
  64. lt:=left.nodetype;
  65. if (
  66. (lt = pointerconstn) and is_farpointer(ld) and
  67. is_constintnode(right) and
  68. (nodetype in [addn,subn])
  69. ) or
  70. (
  71. (rt = pointerconstn) and is_farpointer(rd) and
  72. is_constintnode(left) and
  73. (nodetype=addn)
  74. ) then
  75. begin
  76. t:=nil;
  77. { load values }
  78. case lt of
  79. ordconstn:
  80. lv:=tordconstnode(left).value;
  81. pointerconstn:
  82. lv:=tpointerconstnode(left).value;
  83. niln:
  84. lv:=0;
  85. else
  86. internalerror(2002080202);
  87. end;
  88. case rt of
  89. ordconstn:
  90. rv:=tordconstnode(right).value;
  91. pointerconstn:
  92. rv:=tpointerconstnode(right).value;
  93. niln:
  94. rv:=0;
  95. else
  96. internalerror(2002080203);
  97. end;
  98. case nodetype of
  99. addn:
  100. begin
  101. v:=lv+rv;
  102. if lt=pointerconstn then
  103. t := cpointerconstnode.create((qword(lv) and $FFFF0000) or word(qword(v)),resultdef)
  104. else if rt=pointerconstn then
  105. t := cpointerconstnode.create((qword(rv) and $FFFF0000) or word(qword(v)),resultdef)
  106. else
  107. internalerror(2014040604);
  108. end;
  109. subn:
  110. begin
  111. v:=lv-rv;
  112. if (lt=pointerconstn) then
  113. { pointer-pointer results in an integer }
  114. if (rt=pointerconstn) then
  115. begin
  116. if not(nf_has_pointerdiv in flags) then
  117. internalerror(2008030101);
  118. { todo: implement pointer-pointer as well }
  119. internalerror(2014040607);
  120. //t := cpointerconstnode.create(qword(v),resultdef);
  121. end
  122. else
  123. t := cpointerconstnode.create((qword(lv) and $FFFF0000) or word(qword(v)),resultdef)
  124. else
  125. internalerror(2014040606);
  126. end;
  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(200109051);
  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. { at this point, left.location.loc should be LOC_REGISTER }
  224. if right.location.loc=LOC_REGISTER then
  225. begin
  226. { when swapped another result register }
  227. if (nodetype=subn) and (nf_swapped in flags) then
  228. begin
  229. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  230. left.location.register64,
  231. right.location.register64);
  232. location_swap(left.location,right.location);
  233. toggleflag(nf_swapped);
  234. end
  235. else
  236. begin
  237. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  238. right.location.register64,
  239. left.location.register64);
  240. end;
  241. end
  242. else
  243. begin
  244. { right.location<>LOC_REGISTER }
  245. if (nodetype=subn) and (nf_swapped in flags) then
  246. begin
  247. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  248. cg64.a_load64low_loc_reg(current_asmdata.CurrAsmList,right.location,r);
  249. emit_reg_reg(op1,S_W,left.location.register64.reglo,r);
  250. emit_reg_reg(op2,S_W,GetNextReg(left.location.register64.reglo),GetNextReg(r));
  251. emit_reg_reg(A_MOV,S_W,r,left.location.register64.reglo);
  252. emit_reg_reg(A_MOV,S_W,GetNextReg(r),GetNextReg(left.location.register64.reglo));
  253. cg64.a_load64high_loc_reg(current_asmdata.CurrAsmList,right.location,r);
  254. { the carry flag is still ok }
  255. emit_reg_reg(op2,S_W,left.location.register64.reghi,r);
  256. emit_reg_reg(op2,S_W,GetNextReg(left.location.register64.reghi),GetNextReg(r));
  257. emit_reg_reg(A_MOV,S_W,r,left.location.register64.reghi);
  258. emit_reg_reg(A_MOV,S_W,GetNextReg(r),GetNextReg(left.location.register64.reghi));
  259. end
  260. else
  261. begin
  262. cg64.a_op64_loc_reg(current_asmdata.CurrAsmList,op,location.size,right.location,
  263. left.location.register64);
  264. end;
  265. location_freetemp(current_asmdata.CurrAsmList,right.location);
  266. end;
  267. { only in case of overflow operations }
  268. { produce overflow code }
  269. { we must put it here directly, because sign of operation }
  270. { is in unsigned VAR!! }
  271. if mboverflow then
  272. begin
  273. if cs_check_overflow in current_settings.localswitches then
  274. begin
  275. current_asmdata.getjumplabel(hl4);
  276. if unsigned then
  277. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4)
  278. else
  279. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NO,hl4);
  280. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  281. cg.a_label(current_asmdata.CurrAsmList,hl4);
  282. end;
  283. end;
  284. location_copy(location,left.location);
  285. end;
  286. function ti8086addnode.first_addpointer: tnode;
  287. begin
  288. if is_hugepointer(left.resultdef) xor is_hugepointer(right.resultdef) then
  289. result:=first_addhugepointer
  290. else
  291. result:=inherited;
  292. end;
  293. function ti8086addnode.first_addhugepointer: tnode;
  294. var
  295. procname:string;
  296. begin
  297. result:=nil;
  298. case nodetype of
  299. addn:
  300. procname:='fpc_hugeptr_add_longint';
  301. subn:
  302. procname:='fpc_hugeptr_sub_longint';
  303. else
  304. internalerror(2014070301);
  305. end;
  306. if cs_hugeptr_arithmetic_normalization in current_settings.localswitches then
  307. procname:=procname+'_normalized';
  308. if is_hugepointer(left.resultdef) then
  309. result := ccallnode.createintern(procname,
  310. ccallparanode.create(right,
  311. ccallparanode.create(left,nil)))
  312. else
  313. result := ccallnode.createintern(procname,
  314. ccallparanode.create(left,
  315. ccallparanode.create(right,nil)));
  316. left := nil;
  317. right := nil;
  318. firstpass(result);
  319. end;
  320. function ti8086addnode.first_cmppointer: tnode;
  321. begin
  322. if is_hugepointer(left.resultdef) or is_hugepointer(right.resultdef) then
  323. result:=first_cmphugepointer
  324. else
  325. result:=inherited;
  326. end;
  327. function ti8086addnode.first_cmphugepointer: tnode;
  328. var
  329. procname:string;
  330. begin
  331. result:=nil;
  332. if not (cs_hugeptr_comparison_normalization in current_settings.localswitches) then
  333. begin
  334. expectloc:=LOC_FLAGS;
  335. exit;
  336. end;
  337. case nodetype of
  338. equaln:
  339. procname:='fpc_hugeptr_cmp_normalized_e';
  340. unequaln:
  341. procname:='fpc_hugeptr_cmp_normalized_ne';
  342. ltn:
  343. procname:='fpc_hugeptr_cmp_normalized_b';
  344. lten:
  345. procname:='fpc_hugeptr_cmp_normalized_be';
  346. gtn:
  347. procname:='fpc_hugeptr_cmp_normalized_a';
  348. gten:
  349. procname:='fpc_hugeptr_cmp_normalized_ae';
  350. else
  351. internalerror(2014070401);
  352. end;
  353. result := ccallnode.createintern(procname,
  354. ccallparanode.create(right,
  355. ccallparanode.create(left,nil)));
  356. left := nil;
  357. right := nil;
  358. firstpass(result);
  359. end;
  360. procedure ti8086addnode.second_addfarpointer;
  361. var
  362. tmpreg : tregister;
  363. pointernode: tnode;
  364. begin
  365. pass_left_right;
  366. force_reg_left_right(false,true);
  367. set_result_location_reg;
  368. if (left.resultdef.typ=pointerdef) and (right.resultdef.typ<>pointerdef) then
  369. pointernode:=left
  370. else if (left.resultdef.typ<>pointerdef) and (right.resultdef.typ=pointerdef) then
  371. pointernode:=right
  372. else
  373. internalerror(2014040601);
  374. if not (nodetype in [addn,subn]) then
  375. internalerror(2014040602);
  376. if nodetype=addn then
  377. begin
  378. if (right.location.loc<>LOC_CONSTANT) then
  379. begin
  380. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,
  381. left.location.register,right.location.register,location.register);
  382. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  383. GetNextReg(pointernode.location.register),GetNextReg(location.register));
  384. end
  385. else
  386. begin
  387. if pointernode=left then
  388. begin
  389. { farptr_reg + int_const }
  390. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,
  391. right.location.value,left.location.register,location.register);
  392. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  393. GetNextReg(left.location.register),GetNextReg(location.register));
  394. end
  395. else
  396. begin
  397. { int_reg + farptr_const }
  398. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  399. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,
  400. right.location.value,tmpreg);
  401. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,
  402. left.location.register,tmpreg,location.register);
  403. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  404. GetNextReg(tmpreg),GetNextReg(location.register));
  405. end;
  406. end;
  407. end
  408. else { subtract is a special case since its not commutative }
  409. begin
  410. if (nf_swapped in flags) then
  411. swapleftright;
  412. { left can only be a pointer in this case, since (int-pointer) is not supported }
  413. if pointernode<>left then
  414. internalerror(2014040603);
  415. if left.location.loc<>LOC_CONSTANT then
  416. begin
  417. if right.location.loc<>LOC_CONSTANT then
  418. begin
  419. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_16,
  420. right.location.register,left.location.register,location.register);
  421. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  422. GetNextReg(pointernode.location.register),GetNextReg(location.register));
  423. end
  424. else
  425. begin
  426. { farptr_reg - int_const }
  427. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_16,
  428. right.location.value,left.location.register,location.register);
  429. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  430. GetNextReg(left.location.register),GetNextReg(location.register));
  431. end;
  432. end
  433. else
  434. begin
  435. { farptr_const - int_reg }
  436. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  437. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,
  438. left.location.value,tmpreg);
  439. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_16,
  440. right.location.register,tmpreg,location.register);
  441. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,
  442. GetNextReg(tmpreg),GetNextReg(location.register));
  443. end;
  444. end;
  445. end;
  446. procedure ti8086addnode.second_cmp64bit;
  447. var
  448. hregister,
  449. hregister2 : tregister;
  450. href : treference;
  451. unsigned : boolean;
  452. procedure firstjmp64bitcmp;
  453. var
  454. oldnodetype : tnodetype;
  455. begin
  456. {$ifdef OLDREGVARS}
  457. load_all_regvars(current_asmdata.CurrAsmList);
  458. {$endif OLDREGVARS}
  459. { the jump the sequence is a little bit hairy }
  460. case nodetype of
  461. ltn,gtn:
  462. begin
  463. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  464. { cheat a little bit for the negative test }
  465. toggleflag(nf_swapped);
  466. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  467. toggleflag(nf_swapped);
  468. end;
  469. lten,gten:
  470. begin
  471. oldnodetype:=nodetype;
  472. if nodetype=lten then
  473. nodetype:=ltn
  474. else
  475. nodetype:=gtn;
  476. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  477. { cheat for the negative test }
  478. if nodetype=ltn then
  479. nodetype:=gtn
  480. else
  481. nodetype:=ltn;
  482. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  483. nodetype:=oldnodetype;
  484. end;
  485. equaln:
  486. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  487. unequaln:
  488. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  489. end;
  490. end;
  491. procedure middlejmp64bitcmp;
  492. var
  493. oldnodetype : tnodetype;
  494. begin
  495. {$ifdef OLDREGVARS}
  496. load_all_regvars(current_asmdata.CurrAsmList);
  497. {$endif OLDREGVARS}
  498. { the jump the sequence is a little bit hairy }
  499. case nodetype of
  500. ltn,gtn:
  501. begin
  502. { the comparisaion of the low word have to be }
  503. { always unsigned! }
  504. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  505. { cheat a little bit for the negative test }
  506. toggleflag(nf_swapped);
  507. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrFalseLabel);
  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(true),current_procinfo.CurrTrueLabel);
  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(true),current_procinfo.CurrFalseLabel);
  524. nodetype:=oldnodetype;
  525. end;
  526. equaln:
  527. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  528. unequaln:
  529. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  530. end;
  531. end;
  532. procedure lastjmp64bitcmp;
  533. begin
  534. { the jump the sequence is a little bit hairy }
  535. case nodetype of
  536. ltn,gtn,lten,gten:
  537. begin
  538. { the comparisaion of the low word have to be }
  539. { always unsigned! }
  540. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  541. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  542. end;
  543. equaln:
  544. begin
  545. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  546. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  547. end;
  548. unequaln:
  549. begin
  550. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  551. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  552. end;
  553. end;
  554. end;
  555. begin
  556. pass_left_right;
  557. unsigned:=((left.resultdef.typ=orddef) and
  558. (torddef(left.resultdef).ordtype=u64bit)) or
  559. ((right.resultdef.typ=orddef) and
  560. (torddef(right.resultdef).ordtype=u64bit));
  561. { left and right no register? }
  562. { then one must be demanded }
  563. if (left.location.loc<>LOC_REGISTER) then
  564. begin
  565. if (right.location.loc<>LOC_REGISTER) then
  566. begin
  567. { we can reuse a CREGISTER for comparison }
  568. if (left.location.loc<>LOC_CREGISTER) then
  569. begin
  570. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  571. hregister2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  572. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,joinreg64(hregister,hregister2));
  573. location_freetemp(current_asmdata.CurrAsmList,left.location);
  574. location_reset(left.location,LOC_REGISTER,left.location.size);
  575. left.location.register64.reglo:=hregister;
  576. left.location.register64.reghi:=hregister2;
  577. end;
  578. end
  579. else
  580. begin
  581. location_swap(left.location,right.location);
  582. toggleflag(nf_swapped);
  583. end;
  584. end;
  585. { at this point, left.location.loc should be LOC_REGISTER }
  586. if right.location.loc=LOC_REGISTER then
  587. begin
  588. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register64.reghi),GetNextReg(left.location.register64.reghi));
  589. firstjmp64bitcmp;
  590. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  591. middlejmp64bitcmp;
  592. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register64.reglo),GetNextReg(left.location.register64.reglo));
  593. middlejmp64bitcmp;
  594. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  595. lastjmp64bitcmp;
  596. end
  597. else
  598. begin
  599. case right.location.loc of
  600. LOC_CREGISTER :
  601. begin
  602. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register64.reghi),GetNextReg(left.location.register64.reghi));
  603. firstjmp64bitcmp;
  604. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  605. middlejmp64bitcmp;
  606. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register64.reglo),GetNextReg(left.location.register64.reglo));
  607. middlejmp64bitcmp;
  608. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  609. lastjmp64bitcmp;
  610. end;
  611. LOC_CREFERENCE,
  612. LOC_REFERENCE :
  613. begin
  614. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  615. href:=right.location.reference;
  616. inc(href.offset,6);
  617. emit_ref_reg(A_CMP,S_W,href,GetNextReg(left.location.register64.reghi));
  618. firstjmp64bitcmp;
  619. dec(href.offset,2);
  620. emit_ref_reg(A_CMP,S_W,href,left.location.register64.reghi);
  621. middlejmp64bitcmp;
  622. dec(href.offset,2);
  623. emit_ref_reg(A_CMP,S_W,href,GetNextReg(left.location.register64.reglo));
  624. middlejmp64bitcmp;
  625. emit_ref_reg(A_CMP,S_W,right.location.reference,left.location.register64.reglo);
  626. lastjmp64bitcmp;
  627. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  628. location_freetemp(current_asmdata.CurrAsmList,right.location);
  629. end;
  630. LOC_CONSTANT :
  631. begin
  632. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value64 shr 48) and $FFFF),GetNextReg(left.location.register64.reghi)));
  633. firstjmp64bitcmp;
  634. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value64 shr 32) and $FFFF),left.location.register64.reghi));
  635. middlejmp64bitcmp;
  636. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value64 shr 16) and $FFFF),GetNextReg(left.location.register64.reglo)));
  637. middlejmp64bitcmp;
  638. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value64 and $FFFF),left.location.register64.reglo));
  639. lastjmp64bitcmp;
  640. end;
  641. else
  642. internalerror(200203282);
  643. end;
  644. end;
  645. { we have LOC_JUMP as result }
  646. location_reset(location,LOC_JUMP,OS_NO)
  647. end;
  648. procedure ti8086addnode.second_cmp32bit;
  649. var
  650. hregister : tregister;
  651. href : treference;
  652. unsigned : boolean;
  653. procedure firstjmp32bitcmp;
  654. var
  655. oldnodetype : tnodetype;
  656. begin
  657. {$ifdef OLDREGVARS}
  658. load_all_regvars(current_asmdata.CurrAsmList);
  659. {$endif OLDREGVARS}
  660. { the jump the sequence is a little bit hairy }
  661. case nodetype of
  662. ltn,gtn:
  663. begin
  664. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  665. { cheat a little bit for the negative test }
  666. toggleflag(nf_swapped);
  667. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  668. toggleflag(nf_swapped);
  669. end;
  670. lten,gten:
  671. begin
  672. oldnodetype:=nodetype;
  673. if nodetype=lten then
  674. nodetype:=ltn
  675. else
  676. nodetype:=gtn;
  677. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  678. { cheat for the negative test }
  679. if nodetype=ltn then
  680. nodetype:=gtn
  681. else
  682. nodetype:=ltn;
  683. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  684. nodetype:=oldnodetype;
  685. end;
  686. equaln:
  687. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  688. unequaln:
  689. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  690. end;
  691. end;
  692. procedure secondjmp32bitcmp;
  693. begin
  694. { the jump the sequence is a little bit hairy }
  695. case nodetype of
  696. ltn,gtn,lten,gten:
  697. begin
  698. { the comparisaion of the low dword have to be }
  699. { always unsigned! }
  700. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  701. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  702. end;
  703. equaln:
  704. begin
  705. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  706. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  707. end;
  708. unequaln:
  709. begin
  710. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  711. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  712. end;
  713. end;
  714. end;
  715. begin
  716. pass_left_right;
  717. unsigned:=((left.resultdef.typ=orddef) and
  718. (torddef(left.resultdef).ordtype=u32bit)) or
  719. ((right.resultdef.typ=orddef) and
  720. (torddef(right.resultdef).ordtype=u32bit));
  721. { left and right no register? }
  722. { then one must be demanded }
  723. if (left.location.loc<>LOC_REGISTER) then
  724. begin
  725. if (right.location.loc<>LOC_REGISTER) then
  726. begin
  727. { we can reuse a CREGISTER for comparison }
  728. if (left.location.loc<>LOC_CREGISTER) then
  729. begin
  730. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  731. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_32,left.location,hregister);
  732. location_freetemp(current_asmdata.CurrAsmList,left.location);
  733. location_reset(left.location,LOC_REGISTER,left.location.size);
  734. left.location.register:=hregister;
  735. end;
  736. end
  737. else
  738. begin
  739. location_swap(left.location,right.location);
  740. toggleflag(nf_swapped);
  741. end;
  742. end;
  743. { at this point, left.location.loc should be LOC_REGISTER }
  744. if right.location.loc=LOC_REGISTER then
  745. begin
  746. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register),GetNextReg(left.location.register));
  747. firstjmp32bitcmp;
  748. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  749. secondjmp32bitcmp;
  750. end
  751. else
  752. begin
  753. case right.location.loc of
  754. LOC_CREGISTER :
  755. begin
  756. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register),GetNextReg(left.location.register));
  757. firstjmp32bitcmp;
  758. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  759. secondjmp32bitcmp;
  760. end;
  761. LOC_CREFERENCE,
  762. LOC_REFERENCE :
  763. begin
  764. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  765. href:=right.location.reference;
  766. inc(href.offset,2);
  767. emit_ref_reg(A_CMP,S_W,href,GetNextReg(left.location.register));
  768. firstjmp32bitcmp;
  769. dec(href.offset,2);
  770. emit_ref_reg(A_CMP,S_W,href,left.location.register);
  771. secondjmp32bitcmp;
  772. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  773. location_freetemp(current_asmdata.CurrAsmList,right.location);
  774. end;
  775. LOC_CONSTANT :
  776. begin
  777. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value shr 16) and $FFFF),GetNextReg(left.location.register)));
  778. firstjmp32bitcmp;
  779. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value and $FFFF),left.location.register));
  780. secondjmp32bitcmp;
  781. end;
  782. else
  783. internalerror(200203282);
  784. end;
  785. end;
  786. { we have LOC_JUMP as result }
  787. location_reset(location,LOC_JUMP,OS_NO)
  788. end;
  789. procedure ti8086addnode.second_cmpfarpointer;
  790. begin
  791. { handle = and <> as a 32-bit comparison }
  792. if nodetype in [equaln,unequaln] then
  793. begin
  794. second_cmp32bit;
  795. exit;
  796. end;
  797. pass_left_right;
  798. { <, >, <= and >= compare the 16-bit offset only }
  799. if (right.location.loc=LOC_CONSTANT) and
  800. (left.location.loc in [LOC_REFERENCE, LOC_CREFERENCE])
  801. then
  802. begin
  803. emit_const_ref(A_CMP, S_W, word(right.location.value), left.location.reference);
  804. location_freetemp(current_asmdata.CurrAsmList,left.location);
  805. end
  806. else
  807. begin
  808. { left location is not a register? }
  809. if left.location.loc<>LOC_REGISTER then
  810. begin
  811. { if right is register then we can swap the locations }
  812. if right.location.loc=LOC_REGISTER then
  813. begin
  814. location_swap(left.location,right.location);
  815. toggleflag(nf_swapped);
  816. end
  817. else
  818. begin
  819. { maybe we can reuse a constant register when the
  820. operation is a comparison that doesn't change the
  821. value of the register }
  822. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u16inttype,true);
  823. end;
  824. end;
  825. emit_generic_code(A_CMP,OS_16,true,false,false);
  826. location_freetemp(current_asmdata.CurrAsmList,right.location);
  827. location_freetemp(current_asmdata.CurrAsmList,left.location);
  828. end;
  829. location_reset(location,LOC_FLAGS,OS_NO);
  830. location.resflags:=getresflags(true);
  831. end;
  832. procedure ti8086addnode.second_cmpordinal;
  833. begin
  834. if is_farpointer(left.resultdef) then
  835. second_cmpfarpointer
  836. else if is_32bit(left.resultdef) or is_hugepointer(left.resultdef) then
  837. second_cmp32bit
  838. else
  839. inherited second_cmpordinal;
  840. end;
  841. {*****************************************************************************
  842. x86 MUL
  843. *****************************************************************************}
  844. procedure ti8086addnode.second_mul(unsigned: boolean);
  845. var reg:Tregister;
  846. ref:Treference;
  847. use_ref:boolean;
  848. hl4 : tasmlabel;
  849. const
  850. asmops: array[boolean] of tasmop = (A_IMUL, A_MUL);
  851. begin
  852. pass_left_right;
  853. { MUL is faster than IMUL on the 8086 & 8088 (and equal in speed on 286+),
  854. but it's only safe to use in place of IMUL when overflow checking is off
  855. and we're doing a 16-bit>16-bit multiplication }
  856. if not (cs_check_overflow in current_settings.localswitches) and
  857. (not is_32bitint(resultdef)) then
  858. unsigned:=true;
  859. {The location.register will be filled in later (JM)}
  860. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  861. { Mul supports registers and references, so if not register/reference,
  862. load the location into a register. }
  863. use_ref:=false;
  864. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  865. reg:=left.location.register
  866. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  867. begin
  868. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  869. ref:=left.location.reference;
  870. use_ref:=true;
  871. end
  872. else
  873. begin
  874. {LOC_CONSTANT for example.}
  875. reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  876. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,osuinttype,left.location,reg);
  877. end;
  878. {Allocate AX.}
  879. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  880. {Load the right value.}
  881. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,osuinttype,right.location,NR_AX);
  882. {Also allocate DX, since it is also modified by a mul (JM).}
  883. cg.getcpuregister(current_asmdata.CurrAsmList,NR_DX);
  884. if use_ref then
  885. emit_ref(asmops[unsigned],S_W,ref)
  886. else
  887. emit_reg(asmops[unsigned],S_W,reg);
  888. if (cs_check_overflow in current_settings.localswitches) and
  889. { 16->32 bit cannot overflow }
  890. (not is_32bitint(resultdef)) then
  891. begin
  892. current_asmdata.getjumplabel(hl4);
  893. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4);
  894. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  895. cg.a_label(current_asmdata.CurrAsmList,hl4);
  896. end;
  897. {Free AX,DX}
  898. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_DX);
  899. if is_32bitint(resultdef) then
  900. begin
  901. {Allocate an imaginary 32-bit register, which consists of a pair of
  902. 16-bit registers and store DX:AX into it}
  903. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  904. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_DX,GetNextReg(location.register));
  905. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  906. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  907. end
  908. else
  909. begin
  910. {Allocate a new register and store the result in AX in it.}
  911. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  912. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  913. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  914. end;
  915. location_freetemp(current_asmdata.CurrAsmList,left.location);
  916. location_freetemp(current_asmdata.CurrAsmList,right.location);
  917. end;
  918. begin
  919. caddnode:=ti8086addnode;
  920. end.