n8086add.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 use_generic_mul32to64: boolean; override;
  26. procedure second_addordinal; override;
  27. procedure second_add64bit;override;
  28. procedure second_cmp64bit;override;
  29. procedure second_cmp32bit;
  30. procedure second_cmpordinal;override;
  31. procedure second_mul(unsigned: boolean);
  32. end;
  33. implementation
  34. uses
  35. globtype,systems,
  36. cutils,verbose,globals,
  37. symconst,symdef,paramgr,defutil,
  38. aasmbase,aasmtai,aasmdata,aasmcpu,
  39. cgbase,procinfo,
  40. ncon,nset,cgutils,tgobj,
  41. cga,ncgutil,cgobj,cg64f32,cgx86,
  42. hlcgobj;
  43. {*****************************************************************************
  44. use_generic_mul32to64
  45. *****************************************************************************}
  46. function ti8086addnode.use_generic_mul32to64: boolean;
  47. begin
  48. result := True;
  49. end;
  50. { handles all multiplications }
  51. procedure ti8086addnode.second_addordinal;
  52. var
  53. unsigned: boolean;
  54. begin
  55. unsigned:=not(is_signed(left.resultdef)) or
  56. not(is_signed(right.resultdef));
  57. if nodetype=muln then
  58. second_mul(unsigned)
  59. else
  60. inherited second_addordinal;
  61. end;
  62. {*****************************************************************************
  63. Add64bit
  64. *****************************************************************************}
  65. procedure ti8086addnode.second_add64bit;
  66. var
  67. op : TOpCG;
  68. op1,op2 : TAsmOp;
  69. opsize : TOpSize;
  70. hregister,
  71. hregister2 : tregister;
  72. hl4 : tasmlabel;
  73. mboverflow,
  74. unsigned:boolean;
  75. r:Tregister;
  76. begin
  77. pass_left_right;
  78. op1:=A_NONE;
  79. op2:=A_NONE;
  80. mboverflow:=false;
  81. opsize:=S_L;
  82. unsigned:=((left.resultdef.typ=orddef) and
  83. (torddef(left.resultdef).ordtype=u64bit)) or
  84. ((right.resultdef.typ=orddef) and
  85. (torddef(right.resultdef).ordtype=u64bit));
  86. case nodetype of
  87. addn :
  88. begin
  89. op:=OP_ADD;
  90. mboverflow:=true;
  91. end;
  92. subn :
  93. begin
  94. op:=OP_SUB;
  95. op1:=A_SUB;
  96. op2:=A_SBB;
  97. mboverflow:=true;
  98. end;
  99. xorn:
  100. op:=OP_XOR;
  101. orn:
  102. op:=OP_OR;
  103. andn:
  104. op:=OP_AND;
  105. else
  106. begin
  107. { everything should be handled in pass_1 (JM) }
  108. internalerror(200109051);
  109. end;
  110. end;
  111. { left and right no register? }
  112. { then one must be demanded }
  113. if (left.location.loc<>LOC_REGISTER) then
  114. begin
  115. if (right.location.loc<>LOC_REGISTER) then
  116. begin
  117. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  118. hregister2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  119. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,joinreg64(hregister,hregister2));
  120. location_reset(left.location,LOC_REGISTER,left.location.size);
  121. left.location.register64.reglo:=hregister;
  122. left.location.register64.reghi:=hregister2;
  123. end
  124. else
  125. begin
  126. location_swap(left.location,right.location);
  127. toggleflag(nf_swapped);
  128. end;
  129. end;
  130. { at this point, left.location.loc should be LOC_REGISTER }
  131. if right.location.loc=LOC_REGISTER then
  132. begin
  133. { when swapped another result register }
  134. if (nodetype=subn) and (nf_swapped in flags) then
  135. begin
  136. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  137. left.location.register64,
  138. right.location.register64);
  139. location_swap(left.location,right.location);
  140. toggleflag(nf_swapped);
  141. end
  142. else
  143. begin
  144. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  145. right.location.register64,
  146. left.location.register64);
  147. end;
  148. end
  149. else
  150. begin
  151. { right.location<>LOC_REGISTER }
  152. if (nodetype=subn) and (nf_swapped in flags) then
  153. begin
  154. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  155. cg64.a_load64low_loc_reg(current_asmdata.CurrAsmList,right.location,r);
  156. emit_reg_reg(op1,opsize,left.location.register64.reglo,r);
  157. emit_reg_reg(op2,opsize,GetNextReg(left.location.register64.reglo),GetNextReg(r));
  158. emit_reg_reg(A_MOV,opsize,r,left.location.register64.reglo);
  159. emit_reg_reg(A_MOV,opsize,GetNextReg(r),GetNextReg(left.location.register64.reglo));
  160. cg64.a_load64high_loc_reg(current_asmdata.CurrAsmList,right.location,r);
  161. { the carry flag is still ok }
  162. emit_reg_reg(op2,opsize,left.location.register64.reghi,r);
  163. emit_reg_reg(op2,opsize,GetNextReg(left.location.register64.reghi),GetNextReg(r));
  164. emit_reg_reg(A_MOV,opsize,r,left.location.register64.reghi);
  165. emit_reg_reg(A_MOV,opsize,GetNextReg(r),GetNextReg(left.location.register64.reghi));
  166. end
  167. else
  168. begin
  169. cg64.a_op64_loc_reg(current_asmdata.CurrAsmList,op,location.size,right.location,
  170. left.location.register64);
  171. end;
  172. location_freetemp(current_asmdata.CurrAsmList,right.location);
  173. end;
  174. { only in case of overflow operations }
  175. { produce overflow code }
  176. { we must put it here directly, because sign of operation }
  177. { is in unsigned VAR!! }
  178. if mboverflow then
  179. begin
  180. if cs_check_overflow in current_settings.localswitches then
  181. begin
  182. current_asmdata.getjumplabel(hl4);
  183. if unsigned then
  184. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4)
  185. else
  186. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NO,hl4);
  187. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  188. cg.a_label(current_asmdata.CurrAsmList,hl4);
  189. end;
  190. end;
  191. location_copy(location,left.location);
  192. end;
  193. procedure ti8086addnode.second_cmp64bit;
  194. var
  195. hregister,
  196. hregister2 : tregister;
  197. href : treference;
  198. unsigned : boolean;
  199. procedure firstjmp64bitcmp;
  200. var
  201. oldnodetype : tnodetype;
  202. begin
  203. {$ifdef OLDREGVARS}
  204. load_all_regvars(current_asmdata.CurrAsmList);
  205. {$endif OLDREGVARS}
  206. { the jump the sequence is a little bit hairy }
  207. case nodetype of
  208. ltn,gtn:
  209. begin
  210. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  211. { cheat a little bit for the negative test }
  212. toggleflag(nf_swapped);
  213. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  214. toggleflag(nf_swapped);
  215. end;
  216. lten,gten:
  217. begin
  218. oldnodetype:=nodetype;
  219. if nodetype=lten then
  220. nodetype:=ltn
  221. else
  222. nodetype:=gtn;
  223. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  224. { cheat for the negative test }
  225. if nodetype=ltn then
  226. nodetype:=gtn
  227. else
  228. nodetype:=ltn;
  229. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  230. nodetype:=oldnodetype;
  231. end;
  232. equaln:
  233. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  234. unequaln:
  235. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  236. end;
  237. end;
  238. procedure middlejmp64bitcmp;
  239. var
  240. oldnodetype : tnodetype;
  241. begin
  242. {$ifdef OLDREGVARS}
  243. load_all_regvars(current_asmdata.CurrAsmList);
  244. {$endif OLDREGVARS}
  245. { the jump the sequence is a little bit hairy }
  246. case nodetype of
  247. ltn,gtn:
  248. begin
  249. { the comparisaion of the low word have to be }
  250. { always unsigned! }
  251. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  252. { cheat a little bit for the negative test }
  253. toggleflag(nf_swapped);
  254. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrFalseLabel);
  255. toggleflag(nf_swapped);
  256. end;
  257. lten,gten:
  258. begin
  259. oldnodetype:=nodetype;
  260. if nodetype=lten then
  261. nodetype:=ltn
  262. else
  263. nodetype:=gtn;
  264. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  265. { cheat for the negative test }
  266. if nodetype=ltn then
  267. nodetype:=gtn
  268. else
  269. nodetype:=ltn;
  270. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrFalseLabel);
  271. nodetype:=oldnodetype;
  272. end;
  273. equaln:
  274. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  275. unequaln:
  276. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  277. end;
  278. end;
  279. procedure lastjmp64bitcmp;
  280. begin
  281. { the jump the sequence is a little bit hairy }
  282. case nodetype of
  283. ltn,gtn,lten,gten:
  284. begin
  285. { the comparisaion of the low word have to be }
  286. { always unsigned! }
  287. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  288. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  289. end;
  290. equaln:
  291. begin
  292. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  293. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  294. end;
  295. unequaln:
  296. begin
  297. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  298. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  299. end;
  300. end;
  301. end;
  302. begin
  303. pass_left_right;
  304. unsigned:=((left.resultdef.typ=orddef) and
  305. (torddef(left.resultdef).ordtype=u64bit)) or
  306. ((right.resultdef.typ=orddef) and
  307. (torddef(right.resultdef).ordtype=u64bit));
  308. { left and right no register? }
  309. { then one must be demanded }
  310. if (left.location.loc<>LOC_REGISTER) then
  311. begin
  312. if (right.location.loc<>LOC_REGISTER) then
  313. begin
  314. { we can reuse a CREGISTER for comparison }
  315. if (left.location.loc<>LOC_CREGISTER) then
  316. begin
  317. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  318. hregister2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  319. cg64.a_load64_loc_reg(current_asmdata.CurrAsmList,left.location,joinreg64(hregister,hregister2));
  320. location_freetemp(current_asmdata.CurrAsmList,left.location);
  321. location_reset(left.location,LOC_REGISTER,left.location.size);
  322. left.location.register64.reglo:=hregister;
  323. left.location.register64.reghi:=hregister2;
  324. end;
  325. end
  326. else
  327. begin
  328. location_swap(left.location,right.location);
  329. toggleflag(nf_swapped);
  330. end;
  331. end;
  332. { at this point, left.location.loc should be LOC_REGISTER }
  333. if right.location.loc=LOC_REGISTER then
  334. begin
  335. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register64.reghi),GetNextReg(left.location.register64.reghi));
  336. firstjmp64bitcmp;
  337. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  338. middlejmp64bitcmp;
  339. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register64.reglo),GetNextReg(left.location.register64.reglo));
  340. middlejmp64bitcmp;
  341. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  342. lastjmp64bitcmp;
  343. end
  344. else
  345. begin
  346. case right.location.loc of
  347. LOC_CREGISTER :
  348. begin
  349. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register64.reghi),GetNextReg(left.location.register64.reghi));
  350. firstjmp64bitcmp;
  351. emit_reg_reg(A_CMP,S_W,right.location.register64.reghi,left.location.register64.reghi);
  352. middlejmp64bitcmp;
  353. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register64.reglo),GetNextReg(left.location.register64.reglo));
  354. middlejmp64bitcmp;
  355. emit_reg_reg(A_CMP,S_W,right.location.register64.reglo,left.location.register64.reglo);
  356. lastjmp64bitcmp;
  357. end;
  358. LOC_CREFERENCE,
  359. LOC_REFERENCE :
  360. begin
  361. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  362. href:=right.location.reference;
  363. inc(href.offset,6);
  364. emit_ref_reg(A_CMP,S_W,href,GetNextReg(left.location.register64.reghi));
  365. firstjmp64bitcmp;
  366. dec(href.offset,2);
  367. emit_ref_reg(A_CMP,S_W,href,left.location.register64.reghi);
  368. middlejmp64bitcmp;
  369. dec(href.offset,2);
  370. emit_ref_reg(A_CMP,S_W,href,GetNextReg(left.location.register64.reglo));
  371. middlejmp64bitcmp;
  372. emit_ref_reg(A_CMP,S_W,right.location.reference,left.location.register64.reglo);
  373. lastjmp64bitcmp;
  374. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  375. location_freetemp(current_asmdata.CurrAsmList,right.location);
  376. end;
  377. LOC_CONSTANT :
  378. begin
  379. 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)));
  380. firstjmp64bitcmp;
  381. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value64 shr 32) and $FFFF),left.location.register64.reghi));
  382. middlejmp64bitcmp;
  383. 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)));
  384. middlejmp64bitcmp;
  385. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value64 and $FFFF),left.location.register64.reglo));
  386. lastjmp64bitcmp;
  387. end;
  388. else
  389. internalerror(200203282);
  390. end;
  391. end;
  392. { we have LOC_JUMP as result }
  393. location_reset(location,LOC_JUMP,OS_NO)
  394. end;
  395. procedure ti8086addnode.second_cmp32bit;
  396. var
  397. hregister : tregister;
  398. href : treference;
  399. unsigned : boolean;
  400. procedure firstjmp32bitcmp;
  401. var
  402. oldnodetype : tnodetype;
  403. begin
  404. {$ifdef OLDREGVARS}
  405. load_all_regvars(current_asmdata.CurrAsmList);
  406. {$endif OLDREGVARS}
  407. { the jump the sequence is a little bit hairy }
  408. case nodetype of
  409. ltn,gtn:
  410. begin
  411. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  412. { cheat a little bit for the negative test }
  413. toggleflag(nf_swapped);
  414. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  415. toggleflag(nf_swapped);
  416. end;
  417. lten,gten:
  418. begin
  419. oldnodetype:=nodetype;
  420. if nodetype=lten then
  421. nodetype:=ltn
  422. else
  423. nodetype:=gtn;
  424. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  425. { cheat for the negative test }
  426. if nodetype=ltn then
  427. nodetype:=gtn
  428. else
  429. nodetype:=ltn;
  430. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  431. nodetype:=oldnodetype;
  432. end;
  433. equaln:
  434. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  435. unequaln:
  436. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  437. end;
  438. end;
  439. procedure secondjmp32bitcmp;
  440. begin
  441. { the jump the sequence is a little bit hairy }
  442. case nodetype of
  443. ltn,gtn,lten,gten:
  444. begin
  445. { the comparisaion of the low dword have to be }
  446. { always unsigned! }
  447. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  448. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  449. end;
  450. equaln:
  451. begin
  452. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  453. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  454. end;
  455. unequaln:
  456. begin
  457. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  458. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  459. end;
  460. end;
  461. end;
  462. begin
  463. pass_left_right;
  464. unsigned:=((left.resultdef.typ=orddef) and
  465. (torddef(left.resultdef).ordtype=u32bit)) or
  466. ((right.resultdef.typ=orddef) and
  467. (torddef(right.resultdef).ordtype=u32bit));
  468. { left and right no register? }
  469. { then one must be demanded }
  470. if (left.location.loc<>LOC_REGISTER) then
  471. begin
  472. if (right.location.loc<>LOC_REGISTER) then
  473. begin
  474. { we can reuse a CREGISTER for comparison }
  475. if (left.location.loc<>LOC_CREGISTER) then
  476. begin
  477. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  478. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_32,left.location,hregister);
  479. location_freetemp(current_asmdata.CurrAsmList,left.location);
  480. location_reset(left.location,LOC_REGISTER,left.location.size);
  481. left.location.register:=hregister;
  482. end;
  483. end
  484. else
  485. begin
  486. location_swap(left.location,right.location);
  487. toggleflag(nf_swapped);
  488. end;
  489. end;
  490. { at this point, left.location.loc should be LOC_REGISTER }
  491. if right.location.loc=LOC_REGISTER then
  492. begin
  493. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register),GetNextReg(left.location.register));
  494. firstjmp32bitcmp;
  495. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  496. secondjmp32bitcmp;
  497. end
  498. else
  499. begin
  500. case right.location.loc of
  501. LOC_CREGISTER :
  502. begin
  503. emit_reg_reg(A_CMP,S_W,GetNextReg(right.location.register),GetNextReg(left.location.register));
  504. firstjmp32bitcmp;
  505. emit_reg_reg(A_CMP,S_W,right.location.register,left.location.register);
  506. secondjmp32bitcmp;
  507. end;
  508. LOC_CREFERENCE,
  509. LOC_REFERENCE :
  510. begin
  511. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  512. href:=right.location.reference;
  513. inc(href.offset,2);
  514. emit_ref_reg(A_CMP,S_W,href,GetNextReg(left.location.register));
  515. firstjmp32bitcmp;
  516. emit_ref_reg(A_CMP,S_W,right.location.reference,left.location.register);
  517. secondjmp32bitcmp;
  518. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  519. location_freetemp(current_asmdata.CurrAsmList,right.location);
  520. end;
  521. LOC_CONSTANT :
  522. begin
  523. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint((right.location.value shr 16) and $FFFF),GetNextReg(left.location.register)));
  524. firstjmp32bitcmp;
  525. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_W,aint(right.location.value and $FFFF),left.location.register));
  526. secondjmp32bitcmp;
  527. end;
  528. else
  529. internalerror(200203282);
  530. end;
  531. end;
  532. { we have LOC_JUMP as result }
  533. location_reset(location,LOC_JUMP,OS_NO)
  534. end;
  535. procedure ti8086addnode.second_cmpordinal;
  536. begin
  537. if is_32bit(left.resultdef) then
  538. second_cmp32bit
  539. else
  540. inherited second_cmpordinal;
  541. end;
  542. {*****************************************************************************
  543. x86 MUL
  544. *****************************************************************************}
  545. procedure ti8086addnode.second_mul(unsigned: boolean);
  546. procedure add_mov(instr: Taicpu);
  547. begin
  548. { Notify the register allocator that we have written a move instruction so
  549. it can try to eliminate it. }
  550. if (instr.oper[0]^.reg<>current_procinfo.framepointer) and (instr.oper[0]^.reg<>NR_STACK_POINTER_REG) then
  551. tcgx86(cg).add_move_instruction(instr);
  552. current_asmdata.CurrAsmList.concat(instr);
  553. end;
  554. var reg:Tregister;
  555. ref:Treference;
  556. use_ref:boolean;
  557. hl4 : tasmlabel;
  558. const
  559. asmops: array[boolean] of tasmop = (A_IMUL, A_MUL);
  560. begin
  561. pass_left_right;
  562. {The location.register will be filled in later (JM)}
  563. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  564. { Mul supports registers and references, so if not register/reference,
  565. load the location into a register. }
  566. use_ref:=false;
  567. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  568. reg:=left.location.register
  569. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  570. begin
  571. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  572. ref:=left.location.reference;
  573. use_ref:=true;
  574. end
  575. else
  576. begin
  577. {LOC_CONSTANT for example.}
  578. reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  579. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,osuinttype,left.location,reg);
  580. end;
  581. {Allocate AX.}
  582. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  583. {Load the right value.}
  584. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,osuinttype,right.location,NR_AX);
  585. {Also allocate DX, since it is also modified by a mul (JM).}
  586. cg.getcpuregister(current_asmdata.CurrAsmList,NR_DX);
  587. if use_ref then
  588. emit_ref(asmops[unsigned],S_W,ref)
  589. else
  590. emit_reg(asmops[unsigned],S_W,reg);
  591. if (cs_check_overflow in current_settings.localswitches) and
  592. { 16->32 bit cannot overflow }
  593. (not is_32bitint(resultdef)) then
  594. begin
  595. current_asmdata.getjumplabel(hl4);
  596. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4);
  597. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  598. cg.a_label(current_asmdata.CurrAsmList,hl4);
  599. end;
  600. {Free AX,DX}
  601. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_DX);
  602. if is_32bitint(resultdef) then
  603. begin
  604. {Allocate an imaginary 32-bit register, which consists of a pair of
  605. 16-bit registers and store DX:AX into it}
  606. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  607. add_mov(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,location.register));
  608. add_mov(Taicpu.Op_reg_reg(A_MOV,S_W,NR_DX,GetNextReg(location.register)));
  609. end
  610. else
  611. begin
  612. {Allocate a new register and store the result in AX in it.}
  613. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  614. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  615. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register);
  616. end;
  617. location_freetemp(current_asmdata.CurrAsmList,left.location);
  618. location_freetemp(current_asmdata.CurrAsmList,right.location);
  619. end;
  620. begin
  621. caddnode:=ti8086addnode;
  622. end.