n8086add.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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_JUMP;
  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)) or
  721. is_hugepointer(left.resultdef);
  722. { left and right no register? }
  723. { then one must be demanded }
  724. if (left.location.loc<>LOC_REGISTER) then
  725. begin
  726. if (right.location.loc<>LOC_REGISTER) then
  727. begin
  728. { we can reuse a CREGISTER for comparison }
  729. if (left.location.loc<>LOC_CREGISTER) then
  730. begin
  731. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  732. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_32,left.location,hregister);
  733. location_freetemp(current_asmdata.CurrAsmList,left.location);
  734. location_reset(left.location,LOC_REGISTER,left.location.size);
  735. left.location.register:=hregister;
  736. end;
  737. end
  738. else
  739. begin
  740. location_swap(left.location,right.location);
  741. toggleflag(nf_swapped);
  742. end;
  743. end;
  744. { at this point, left.location.loc should be LOC_REGISTER }
  745. if right.location.loc=LOC_REGISTER then
  746. begin
  747. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register),GetNextReg(left.location.register));
  748. firstjmp32bitcmp;
  749. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  750. secondjmp32bitcmp;
  751. end
  752. else
  753. begin
  754. case right.location.loc of
  755. LOC_CREGISTER :
  756. begin
  757. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register),GetNextReg(left.location.register));
  758. firstjmp32bitcmp;
  759. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  760. secondjmp32bitcmp;
  761. end;
  762. LOC_CREFERENCE,
  763. LOC_REFERENCE :
  764. begin
  765. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  766. href:=right.location.reference;
  767. inc(href.offset,2);
  768. emit_ref_reg(A_CMP,S_W,href,GetNextReg(left.location.register));
  769. firstjmp32bitcmp;
  770. dec(href.offset,2);
  771. emit_ref_reg(A_CMP,S_W,href,left.location.register);
  772. secondjmp32bitcmp;
  773. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  774. location_freetemp(current_asmdata.CurrAsmList,right.location);
  775. end;
  776. LOC_CONSTANT :
  777. begin
  778. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value shr 16) and $FFFF),GetNextReg(left.location.register)));
  779. firstjmp32bitcmp;
  780. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value and $FFFF),left.location.register));
  781. secondjmp32bitcmp;
  782. end;
  783. else
  784. internalerror(200203282);
  785. end;
  786. end;
  787. { we have LOC_JUMP as result }
  788. location_reset(location,LOC_JUMP,OS_NO)
  789. end;
  790. procedure ti8086addnode.second_cmpfarpointer;
  791. begin
  792. { handle = and <> as a 32-bit comparison }
  793. if nodetype in [equaln,unequaln] then
  794. begin
  795. second_cmp32bit;
  796. exit;
  797. end;
  798. pass_left_right;
  799. { <, >, <= and >= compare the 16-bit offset only }
  800. if (right.location.loc=LOC_CONSTANT) and
  801. (left.location.loc in [LOC_REFERENCE, LOC_CREFERENCE])
  802. then
  803. begin
  804. emit_const_ref(A_CMP, S_W, word(right.location.value), left.location.reference);
  805. location_freetemp(current_asmdata.CurrAsmList,left.location);
  806. end
  807. else
  808. begin
  809. { left location is not a register? }
  810. if left.location.loc<>LOC_REGISTER then
  811. begin
  812. { if right is register then we can swap the locations }
  813. if right.location.loc=LOC_REGISTER then
  814. begin
  815. location_swap(left.location,right.location);
  816. toggleflag(nf_swapped);
  817. end
  818. else
  819. begin
  820. { maybe we can reuse a constant register when the
  821. operation is a comparison that doesn't change the
  822. value of the register }
  823. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u16inttype,true);
  824. end;
  825. end;
  826. emit_generic_code(A_CMP,OS_16,true,false,false);
  827. location_freetemp(current_asmdata.CurrAsmList,right.location);
  828. location_freetemp(current_asmdata.CurrAsmList,left.location);
  829. end;
  830. location_reset(location,LOC_FLAGS,OS_NO);
  831. location.resflags:=getresflags(true);
  832. end;
  833. procedure ti8086addnode.second_cmpordinal;
  834. begin
  835. if is_farpointer(left.resultdef) then
  836. second_cmpfarpointer
  837. else if is_32bit(left.resultdef) or is_hugepointer(left.resultdef) then
  838. second_cmp32bit
  839. else
  840. inherited second_cmpordinal;
  841. end;
  842. {*****************************************************************************
  843. x86 MUL
  844. *****************************************************************************}
  845. procedure ti8086addnode.second_mul(unsigned: boolean);
  846. var reg:Tregister;
  847. ref:Treference;
  848. use_ref:boolean;
  849. hl4 : tasmlabel;
  850. const
  851. asmops: array[boolean] of tasmop = (A_IMUL, A_MUL);
  852. begin
  853. pass_left_right;
  854. { MUL is faster than IMUL on the 8086 & 8088 (and equal in speed on 286+),
  855. but it's only safe to use in place of IMUL when overflow checking is off
  856. and we're doing a 16-bit>16-bit multiplication }
  857. if not (cs_check_overflow in current_settings.localswitches) and
  858. (not is_32bitint(resultdef)) then
  859. unsigned:=true;
  860. {The location.register will be filled in later (JM)}
  861. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  862. { Mul supports registers and references, so if not register/reference,
  863. load the location into a register. }
  864. use_ref:=false;
  865. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  866. reg:=left.location.register
  867. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  868. begin
  869. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  870. ref:=left.location.reference;
  871. use_ref:=true;
  872. end
  873. else
  874. begin
  875. {LOC_CONSTANT for example.}
  876. reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  877. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,osuinttype,left.location,reg);
  878. end;
  879. {Allocate AX.}
  880. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  881. {Load the right value.}
  882. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,osuinttype,right.location,NR_AX);
  883. {Also allocate DX, since it is also modified by a mul (JM).}
  884. cg.getcpuregister(current_asmdata.CurrAsmList,NR_DX);
  885. if use_ref then
  886. emit_ref(asmops[unsigned],S_W,ref)
  887. else
  888. emit_reg(asmops[unsigned],S_W,reg);
  889. if (cs_check_overflow in current_settings.localswitches) and
  890. { 16->32 bit cannot overflow }
  891. (not is_32bitint(resultdef)) then
  892. begin
  893. current_asmdata.getjumplabel(hl4);
  894. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4);
  895. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  896. cg.a_label(current_asmdata.CurrAsmList,hl4);
  897. end;
  898. {Free AX,DX}
  899. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_DX);
  900. if is_32bitint(resultdef) then
  901. begin
  902. {Allocate an imaginary 32-bit register, which consists of a pair of
  903. 16-bit registers and store DX:AX into it}
  904. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  905. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_DX,GetNextReg(location.register));
  906. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  907. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  908. end
  909. else
  910. begin
  911. {Allocate a new register and store the result in AX in it.}
  912. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  913. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  914. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  915. end;
  916. location_freetemp(current_asmdata.CurrAsmList,left.location);
  917. location_freetemp(current_asmdata.CurrAsmList,right.location);
  918. end;
  919. begin
  920. caddnode:=ti8086addnode;
  921. end.