n8086add.pas 36 KB

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