n8086add.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 needoverflowcheck 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. else
  531. internalerror(2019051024);
  532. end;
  533. end;
  534. procedure middlejmp64bitcmp;
  535. var
  536. oldnodetype : tnodetype;
  537. begin
  538. {$ifdef OLDREGVARS}
  539. load_all_regvars(current_asmdata.CurrAsmList);
  540. {$endif OLDREGVARS}
  541. { the jump the sequence is a little bit hairy }
  542. case nodetype of
  543. ltn,gtn:
  544. begin
  545. { the comparisaion of the low word have to be }
  546. { always unsigned! }
  547. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  548. { cheat a little bit for the negative test }
  549. toggleflag(nf_swapped);
  550. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.falselabel);
  551. toggleflag(nf_swapped);
  552. end;
  553. lten,gten:
  554. begin
  555. oldnodetype:=nodetype;
  556. if nodetype=lten then
  557. nodetype:=ltn
  558. else
  559. nodetype:=gtn;
  560. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  561. { cheat for the negative test }
  562. if nodetype=ltn then
  563. nodetype:=gtn
  564. else
  565. nodetype:=ltn;
  566. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.falselabel);
  567. nodetype:=oldnodetype;
  568. end;
  569. equaln:
  570. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  571. unequaln:
  572. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  573. else
  574. internalerror(2019051023);
  575. end;
  576. end;
  577. procedure lastjmp64bitcmp;
  578. begin
  579. { the jump the sequence is a little bit hairy }
  580. case nodetype of
  581. ltn,gtn,lten,gten:
  582. begin
  583. { the comparisaion of the low word have to be }
  584. { always unsigned! }
  585. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  586. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  587. end;
  588. equaln:
  589. begin
  590. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  591. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  592. end;
  593. unequaln:
  594. begin
  595. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  596. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  597. end;
  598. else
  599. internalerror(2019051022);
  600. end;
  601. end;
  602. begin
  603. truelabel:=nil;
  604. falselabel:=nil;
  605. pass_left_right;
  606. unsigned:=((left.resultdef.typ=orddef) and
  607. (torddef(left.resultdef).ordtype=u64bit)) or
  608. ((right.resultdef.typ=orddef) and
  609. (torddef(right.resultdef).ordtype=u64bit));
  610. { we have LOC_JUMP as result }
  611. current_asmdata.getjumplabel(truelabel);
  612. current_asmdata.getjumplabel(falselabel);
  613. location_reset_jump(location,truelabel,falselabel);
  614. { left and right no register? }
  615. { then one must be demanded }
  616. if (left.location.loc<>LOC_REGISTER) then
  617. begin
  618. if (right.location.loc<>LOC_REGISTER) then
  619. begin
  620. { we can reuse a CREGISTER for comparison }
  621. if (left.location.loc<>LOC_CREGISTER) then
  622. begin
  623. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  624. hregister2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  625. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,joinreg64(hregister,hregister2));
  626. location_freetemp(current_asmdata.CurrAsmList,left.location);
  627. location_reset(left.location,LOC_REGISTER,left.location.size);
  628. left.location.register64.reglo:=hregister;
  629. left.location.register64.reghi:=hregister2;
  630. end;
  631. end
  632. else
  633. begin
  634. location_swap(left.location,right.location);
  635. toggleflag(nf_swapped);
  636. end;
  637. end;
  638. { at this point, left.location.loc should be LOC_REGISTER }
  639. if right.location.loc=LOC_REGISTER then
  640. begin
  641. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reghi),cg.GetNextReg(left.location.register64.reghi));
  642. firstjmp64bitcmp;
  643. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  644. middlejmp64bitcmp;
  645. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reglo),cg.GetNextReg(left.location.register64.reglo));
  646. middlejmp64bitcmp;
  647. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  648. lastjmp64bitcmp;
  649. end
  650. else
  651. begin
  652. case right.location.loc of
  653. LOC_CREGISTER :
  654. begin
  655. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reghi),cg.GetNextReg(left.location.register64.reghi));
  656. firstjmp64bitcmp;
  657. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  658. middlejmp64bitcmp;
  659. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register64.reglo),cg.GetNextReg(left.location.register64.reglo));
  660. middlejmp64bitcmp;
  661. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  662. lastjmp64bitcmp;
  663. end;
  664. LOC_CREFERENCE,
  665. LOC_REFERENCE :
  666. begin
  667. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  668. href:=right.location.reference;
  669. inc(href.offset,6);
  670. emit_ref_reg(A_CMP,S_W,href,cg.GetNextReg(left.location.register64.reghi));
  671. firstjmp64bitcmp;
  672. dec(href.offset,2);
  673. emit_ref_reg(A_CMP,S_W,href,left.location.register64.reghi);
  674. middlejmp64bitcmp;
  675. dec(href.offset,2);
  676. emit_ref_reg(A_CMP,S_W,href,cg.GetNextReg(left.location.register64.reglo));
  677. middlejmp64bitcmp;
  678. emit_ref_reg(A_CMP,S_W,right.location.reference,left.location.register64.reglo);
  679. lastjmp64bitcmp;
  680. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  681. location_freetemp(current_asmdata.CurrAsmList,right.location);
  682. end;
  683. LOC_CONSTANT :
  684. begin
  685. 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)));
  686. firstjmp64bitcmp;
  687. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value64 shr 32) and $FFFF),left.location.register64.reghi));
  688. middlejmp64bitcmp;
  689. 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)));
  690. middlejmp64bitcmp;
  691. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value64 and $FFFF),left.location.register64.reglo));
  692. lastjmp64bitcmp;
  693. end;
  694. else
  695. internalerror(200203282);
  696. end;
  697. end;
  698. end;
  699. procedure ti8086addnode.second_cmp32bit;
  700. var
  701. truelabel,
  702. falselabel: tasmlabel;
  703. hregister : tregister;
  704. href : treference;
  705. unsigned : boolean;
  706. procedure firstjmp32bitcmp;
  707. var
  708. oldnodetype : tnodetype;
  709. begin
  710. {$ifdef OLDREGVARS}
  711. load_all_regvars(current_asmdata.CurrAsmList);
  712. {$endif OLDREGVARS}
  713. { the jump the sequence is a little bit hairy }
  714. case nodetype of
  715. ltn,gtn:
  716. begin
  717. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  718. { cheat a little bit for the negative test }
  719. toggleflag(nf_swapped);
  720. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  721. toggleflag(nf_swapped);
  722. end;
  723. lten,gten:
  724. begin
  725. oldnodetype:=nodetype;
  726. if nodetype=lten then
  727. nodetype:=ltn
  728. else
  729. nodetype:=gtn;
  730. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  731. { cheat for the negative test }
  732. if nodetype=ltn then
  733. nodetype:=gtn
  734. else
  735. nodetype:=ltn;
  736. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  737. nodetype:=oldnodetype;
  738. end;
  739. equaln:
  740. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  741. unequaln:
  742. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  743. else
  744. internalerror(2019051021);
  745. end;
  746. end;
  747. procedure secondjmp32bitcmp;
  748. begin
  749. { the jump the sequence is a little bit hairy }
  750. case nodetype of
  751. ltn,gtn,lten,gten:
  752. begin
  753. { the comparisaion of the low dword have to be }
  754. { always unsigned! }
  755. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  756. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  757. end;
  758. equaln:
  759. begin
  760. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  761. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  762. end;
  763. unequaln:
  764. begin
  765. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  766. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  767. end;
  768. else
  769. internalerror(2019051020);
  770. end;
  771. end;
  772. begin
  773. truelabel:=nil;
  774. falselabel:=nil;
  775. pass_left_right;
  776. unsigned:=((left.resultdef.typ=orddef) and
  777. (torddef(left.resultdef).ordtype=u32bit)) or
  778. ((right.resultdef.typ=orddef) and
  779. (torddef(right.resultdef).ordtype=u32bit)) or
  780. is_hugepointer(left.resultdef);
  781. { we have LOC_JUMP as result }
  782. current_asmdata.getjumplabel(truelabel);
  783. current_asmdata.getjumplabel(falselabel);
  784. location_reset_jump(location,truelabel,falselabel);
  785. { left and right no register? }
  786. { then one must be demanded }
  787. if (left.location.loc<>LOC_REGISTER) then
  788. begin
  789. if (right.location.loc<>LOC_REGISTER) then
  790. begin
  791. { we can reuse a CREGISTER for comparison }
  792. if (left.location.loc<>LOC_CREGISTER) then
  793. begin
  794. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  795. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_32,left.location,hregister);
  796. location_freetemp(current_asmdata.CurrAsmList,left.location);
  797. location_reset(left.location,LOC_REGISTER,left.location.size);
  798. left.location.register:=hregister;
  799. end;
  800. end
  801. else
  802. begin
  803. location_swap(left.location,right.location);
  804. toggleflag(nf_swapped);
  805. end;
  806. end;
  807. { at this point, left.location.loc should be LOC_REGISTER }
  808. if right.location.loc=LOC_REGISTER then
  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. else
  816. begin
  817. case right.location.loc of
  818. LOC_CREGISTER :
  819. begin
  820. emit_reg_reg(A_CMP,S_W,cg.GetNextReg(right.location.register),cg.GetNextReg(left.location.register));
  821. firstjmp32bitcmp;
  822. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  823. secondjmp32bitcmp;
  824. end;
  825. LOC_CREFERENCE,
  826. LOC_REFERENCE :
  827. begin
  828. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  829. href:=right.location.reference;
  830. inc(href.offset,2);
  831. emit_ref_reg(A_CMP,S_W,href,cg.GetNextReg(left.location.register));
  832. firstjmp32bitcmp;
  833. dec(href.offset,2);
  834. emit_ref_reg(A_CMP,S_W,href,left.location.register);
  835. secondjmp32bitcmp;
  836. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  837. location_freetemp(current_asmdata.CurrAsmList,right.location);
  838. end;
  839. LOC_CONSTANT :
  840. begin
  841. if (right.location.value=0) and (nodetype in [equaln,unequaln]) and (left.location.loc=LOC_REGISTER) then
  842. begin
  843. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_W,cg.GetNextReg(left.location.register),left.location.register));
  844. secondjmp32bitcmp;
  845. end
  846. else
  847. begin
  848. 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)));
  849. firstjmp32bitcmp;
  850. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value and $FFFF),left.location.register));
  851. secondjmp32bitcmp;
  852. end;
  853. end;
  854. else
  855. internalerror(200203282);
  856. end;
  857. end;
  858. end;
  859. procedure ti8086addnode.second_cmpfarpointer;
  860. begin
  861. { handle = and <> as a 32-bit comparison }
  862. if nodetype in [equaln,unequaln] then
  863. begin
  864. second_cmp32bit;
  865. exit;
  866. end;
  867. pass_left_right;
  868. { <, >, <= and >= compare the 16-bit offset only }
  869. if (right.location.loc=LOC_CONSTANT) and
  870. (left.location.loc in [LOC_REFERENCE, LOC_CREFERENCE])
  871. then
  872. begin
  873. emit_const_ref(A_CMP, S_W, word(right.location.value), left.location.reference);
  874. location_freetemp(current_asmdata.CurrAsmList,left.location);
  875. end
  876. else
  877. begin
  878. { left location is not a register? }
  879. if left.location.loc<>LOC_REGISTER then
  880. begin
  881. { if right is register then we can swap the locations }
  882. if right.location.loc=LOC_REGISTER then
  883. begin
  884. location_swap(left.location,right.location);
  885. toggleflag(nf_swapped);
  886. end
  887. else
  888. begin
  889. { maybe we can reuse a constant register when the
  890. operation is a comparison that doesn't change the
  891. value of the register }
  892. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u16inttype,true);
  893. end;
  894. end;
  895. emit_generic_code(A_CMP,OS_16,true,false,false);
  896. location_freetemp(current_asmdata.CurrAsmList,right.location);
  897. location_freetemp(current_asmdata.CurrAsmList,left.location);
  898. end;
  899. location_reset(location,LOC_FLAGS,OS_NO);
  900. location.resflags:=getresflags(true);
  901. end;
  902. procedure ti8086addnode.second_cmpordinal;
  903. begin
  904. if is_farpointer(left.resultdef) then
  905. second_cmpfarpointer
  906. else if is_32bit(left.resultdef) or is_hugepointer(left.resultdef) or is_farprocvar(left.resultdef) then
  907. second_cmp32bit
  908. else
  909. inherited second_cmpordinal;
  910. end;
  911. {*****************************************************************************
  912. x86 MUL
  913. *****************************************************************************}
  914. procedure ti8086addnode.second_mul(unsigned: boolean);
  915. var reg:Tregister;
  916. ref:Treference;
  917. use_ref:boolean;
  918. hl4 : tasmlabel;
  919. overflowcheck: boolean;
  920. const
  921. asmops: array[boolean] of tasmop = (A_IMUL, A_MUL);
  922. begin
  923. reg:=NR_NO;
  924. reference_reset(ref,sizeof(pint),[]);
  925. pass_left_right;
  926. overflowcheck:=needoverflowcheck;
  927. { MUL is faster than IMUL on the 8086 & 8088 (and equal in speed on 286+),
  928. but it's only safe to use in place of IMUL when overflow checking is off
  929. and we're doing a 16-bit>16-bit multiplication }
  930. if not overflowcheck and
  931. (not is_32bitint(resultdef)) then
  932. unsigned:=true;
  933. {The location.register will be filled in later (JM)}
  934. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  935. { Mul supports registers and references, so if not register/reference,
  936. load the location into a register. }
  937. use_ref:=false;
  938. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  939. reg:=left.location.register
  940. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  941. begin
  942. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  943. ref:=left.location.reference;
  944. use_ref:=true;
  945. end
  946. else
  947. begin
  948. {LOC_CONSTANT for example.}
  949. reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  950. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,osuinttype,left.location,reg);
  951. end;
  952. {Allocate AX.}
  953. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  954. {Load the right value.}
  955. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,osuinttype,right.location,NR_AX);
  956. {Also allocate DX, since it is also modified by a mul (JM).}
  957. cg.getcpuregister(current_asmdata.CurrAsmList,NR_DX);
  958. if use_ref then
  959. emit_ref(asmops[unsigned],S_W,ref)
  960. else
  961. emit_reg(asmops[unsigned],S_W,reg);
  962. if overflowcheck and
  963. { 16->32 bit cannot overflow }
  964. (not is_32bitint(resultdef)) then
  965. begin
  966. current_asmdata.getjumplabel(hl4);
  967. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4);
  968. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  969. cg.a_label(current_asmdata.CurrAsmList,hl4);
  970. end;
  971. {Free AX,DX}
  972. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_DX);
  973. if is_32bitint(resultdef) then
  974. begin
  975. {Allocate an imaginary 32-bit register, which consists of a pair of
  976. 16-bit registers and store DX:AX into it}
  977. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  978. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_DX,cg.GetNextReg(location.register));
  979. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  980. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  981. end
  982. else
  983. begin
  984. {Allocate a new register and store the result in AX in it.}
  985. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  986. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  987. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  988. end;
  989. location_freetemp(current_asmdata.CurrAsmList,left.location);
  990. location_freetemp(current_asmdata.CurrAsmList,right.location);
  991. end;
  992. begin
  993. caddnode:=ti8086addnode;
  994. end.