n386add.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Code generation for add nodes on the i386
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit n386add;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nadd,cpubase;
  23. type
  24. ti386addnode = class(taddnode)
  25. procedure pass_2;override;
  26. protected
  27. function first_addstring : tnode; override;
  28. private
  29. procedure pass_left_and_right(var pushedfpu:boolean);
  30. function getresflags(unsigned : boolean) : tresflags;
  31. procedure left_must_be_reg(opsize:TOpSize;noswap:boolean);
  32. procedure emit_op_right_left(op:TAsmOp;opsize:TOpSize);
  33. procedure emit_generic_code(op:TAsmOp;opsize:TOpSize;unsigned,extra_not,mboverflow:boolean);
  34. procedure set_result_location(cmpop,unsigned:boolean);
  35. procedure second_addstring;
  36. procedure second_addboolean;
  37. procedure second_addfloat;
  38. procedure second_addsmallset;
  39. procedure second_mul;
  40. {$ifdef SUPPORT_MMX}
  41. procedure second_addmmx;
  42. {$endif SUPPORT_MMX}
  43. procedure second_add64bit;
  44. end;
  45. implementation
  46. uses
  47. globtype,systems,
  48. cutils,verbose,globals,
  49. symconst,symdef,paramgr,
  50. aasmbase,aasmtai,aasmcpu,defutil,htypechk,
  51. cgbase,pass_2,regvars,
  52. ncon,nset,
  53. cga,ncgutil,tgobj,rgobj,cgobj,cg64f32,rgcpu;
  54. {*****************************************************************************
  55. Helpers
  56. *****************************************************************************}
  57. const
  58. opsize_2_cgsize : array[S_B..S_L] of tcgsize = (OS_8,OS_16,OS_32);
  59. procedure ti386addnode.pass_left_and_right(var pushedfpu:boolean);
  60. begin
  61. { calculate the operator which is more difficult }
  62. firstcomplex(self);
  63. { in case of constant put it to the left }
  64. if (left.nodetype=ordconstn) then
  65. swapleftright;
  66. secondpass(left);
  67. { are too few registers free? }
  68. if location.loc=LOC_FPUREGISTER then
  69. pushedfpu:=maybe_pushfpu(exprasmlist,right.registersfpu,left.location)
  70. else
  71. pushedfpu:=false;
  72. secondpass(right);
  73. end;
  74. function ti386addnode.getresflags(unsigned : boolean) : tresflags;
  75. begin
  76. case nodetype of
  77. equaln : getresflags:=F_E;
  78. unequaln : getresflags:=F_NE;
  79. else
  80. if not(unsigned) then
  81. begin
  82. if nf_swaped in flags then
  83. case nodetype of
  84. ltn : getresflags:=F_G;
  85. lten : getresflags:=F_GE;
  86. gtn : getresflags:=F_L;
  87. gten : getresflags:=F_LE;
  88. end
  89. else
  90. case nodetype of
  91. ltn : getresflags:=F_L;
  92. lten : getresflags:=F_LE;
  93. gtn : getresflags:=F_G;
  94. gten : getresflags:=F_GE;
  95. end;
  96. end
  97. else
  98. begin
  99. if nf_swaped in flags then
  100. case nodetype of
  101. ltn : getresflags:=F_A;
  102. lten : getresflags:=F_AE;
  103. gtn : getresflags:=F_B;
  104. gten : getresflags:=F_BE;
  105. end
  106. else
  107. case nodetype of
  108. ltn : getresflags:=F_B;
  109. lten : getresflags:=F_BE;
  110. gtn : getresflags:=F_A;
  111. gten : getresflags:=F_AE;
  112. end;
  113. end;
  114. end;
  115. end;
  116. procedure ti386addnode.left_must_be_reg(opsize:TOpSize;noswap:boolean);
  117. begin
  118. { left location is not a register? }
  119. if (left.location.loc<>LOC_REGISTER) then
  120. begin
  121. { if right is register then we can swap the locations }
  122. if (not noswap) and
  123. (right.location.loc=LOC_REGISTER) then
  124. begin
  125. location_swap(left.location,right.location);
  126. toggleflag(nf_swaped);
  127. end
  128. else
  129. begin
  130. { maybe we can reuse a constant register when the
  131. operation is a comparison that doesn't change the
  132. value of the register }
  133. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  134. end;
  135. end;
  136. end;
  137. procedure ti386addnode.emit_op_right_left(op:TAsmOp;opsize:TOpsize);
  138. begin
  139. { left must be a register }
  140. case right.location.loc of
  141. LOC_REGISTER,
  142. LOC_CREGISTER :
  143. exprasmlist.concat(taicpu.op_reg_reg(op,opsize,right.location.register,left.location.register));
  144. LOC_REFERENCE,
  145. LOC_CREFERENCE :
  146. exprasmlist.concat(taicpu.op_ref_reg(op,opsize,right.location.reference,left.location.register));
  147. LOC_CONSTANT :
  148. exprasmlist.concat(taicpu.op_const_reg(op,opsize,right.location.value,left.location.register));
  149. else
  150. internalerror(200203232);
  151. end;
  152. end;
  153. procedure ti386addnode.set_result_location(cmpop,unsigned:boolean);
  154. begin
  155. if cmpop then
  156. begin
  157. location_reset(location,LOC_FLAGS,OS_NO);
  158. location.resflags:=getresflags(unsigned);
  159. end
  160. else
  161. location_copy(location,left.location);
  162. end;
  163. procedure ti386addnode.emit_generic_code(op:TAsmOp;opsize:TOpSize;unsigned,extra_not,mboverflow:boolean);
  164. var
  165. power : longint;
  166. hl4 : tasmlabel;
  167. r : Tregister;
  168. begin
  169. { at this point, left.location.loc should be LOC_REGISTER }
  170. if right.location.loc=LOC_REGISTER then
  171. begin
  172. { right.location is a LOC_REGISTER }
  173. { when swapped another result register }
  174. if (nodetype=subn) and (nf_swaped in flags) then
  175. begin
  176. if extra_not then
  177. emit_reg(A_NOT,S_L,left.location.register);
  178. emit_reg_reg(op,opsize,left.location.register,right.location.register);
  179. { newly swapped also set swapped flag }
  180. location_swap(left.location,right.location);
  181. toggleflag(nf_swaped);
  182. end
  183. else
  184. begin
  185. if extra_not then
  186. emit_reg(A_NOT,S_L,right.location.register);
  187. if (op=A_ADD) or (op=A_OR) or (op=A_AND) or (op=A_XOR) or (op=A_IMUL) then
  188. location_swap(left.location,right.location);
  189. emit_reg_reg(op,opsize,right.location.register,left.location.register);
  190. end;
  191. end
  192. else
  193. begin
  194. { right.location is not a LOC_REGISTER }
  195. if (nodetype=subn) and (nf_swaped in flags) then
  196. begin
  197. if extra_not then
  198. emit_reg(A_NOT,opsize,left.location.register);
  199. r:=rg.getregisterint(exprasmlist,OS_INT);
  200. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,r);
  201. emit_reg_reg(op,opsize,left.location.register,r);
  202. emit_reg_reg(A_MOV,opsize,r,left.location.register);
  203. rg.ungetregisterint(exprasmlist,r);
  204. end
  205. else
  206. begin
  207. { Optimizations when right.location is a constant value }
  208. if (op=A_CMP) and
  209. (nodetype in [equaln,unequaln]) and
  210. (right.location.loc=LOC_CONSTANT) and
  211. (right.location.value=0) then
  212. begin
  213. emit_reg_reg(A_TEST,opsize,left.location.register,left.location.register);
  214. end
  215. else
  216. if (op=A_ADD) and
  217. (right.location.loc=LOC_CONSTANT) and
  218. (right.location.value=1) and
  219. not(cs_check_overflow in aktlocalswitches) then
  220. begin
  221. emit_reg(A_INC,opsize,left.location.register);
  222. end
  223. else
  224. if (op=A_SUB) and
  225. (right.location.loc=LOC_CONSTANT) and
  226. (right.location.value=1) and
  227. not(cs_check_overflow in aktlocalswitches) then
  228. begin
  229. emit_reg(A_DEC,opsize,left.location.register);
  230. end
  231. else
  232. if (op=A_IMUL) and
  233. (right.location.loc=LOC_CONSTANT) and
  234. (ispowerof2(right.location.value,power)) and
  235. not(cs_check_overflow in aktlocalswitches) then
  236. begin
  237. emit_const_reg(A_SHL,opsize,power,left.location.register);
  238. end
  239. else
  240. begin
  241. if extra_not then
  242. begin
  243. r:=rg.getregisterint(exprasmlist,OS_INT);
  244. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,r);
  245. emit_reg(A_NOT,S_L,r);
  246. emit_reg_reg(A_AND,S_L,r,left.location.register);
  247. rg.ungetregisterint(exprasmlist,r);
  248. end
  249. else
  250. begin
  251. emit_op_right_left(op,opsize);
  252. end;
  253. end;
  254. end;
  255. end;
  256. { only in case of overflow operations }
  257. { produce overflow code }
  258. { we must put it here directly, because sign of operation }
  259. { is in unsigned VAR!! }
  260. if mboverflow then
  261. begin
  262. if cs_check_overflow in aktlocalswitches then
  263. begin
  264. objectlibrary.getlabel(hl4);
  265. if unsigned then
  266. cg.a_jmp_flags(exprasmlist,F_AE,hl4)
  267. else
  268. cg.a_jmp_flags(exprasmlist,F_GE,hl4);
  269. cg.a_call_name(exprasmlist,'FPC_OVERFLOW');
  270. cg.a_label(exprasmlist,hl4);
  271. end;
  272. end;
  273. end;
  274. {*****************************************************************************
  275. Addstring
  276. *****************************************************************************}
  277. { note: if you implemented an fpc_shortstr_concat similar to the }
  278. { one in i386.inc, you have to override first_addstring like in }
  279. { ti386addnode.first_string and implement the shortstring concat }
  280. { manually! The generic routine is different from the i386 one (JM) }
  281. function ti386addnode.first_addstring : tnode;
  282. begin
  283. { special cases for shortstrings, handled in pass_2 (JM) }
  284. { can't handle fpc_shortstr_compare with compilerproc either because it }
  285. { returns its results in the flags instead of in eax }
  286. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) and
  287. is_shortstring(left.resulttype.def) and
  288. not(((left.nodetype=stringconstn) and (str_length(left)=0)) or
  289. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  290. begin
  291. expectloc:=LOC_FLAGS;
  292. calcregisters(self,0,0,0);
  293. result := nil;
  294. exit;
  295. end;
  296. { otherwise, use the generic code }
  297. result := inherited first_addstring;
  298. end;
  299. procedure ti386addnode.second_addstring;
  300. var
  301. paraloc1,
  302. paraloc2 : tparalocation;
  303. hregister1,
  304. hregister2 : tregister;
  305. begin
  306. { string operations are not commutative }
  307. if nf_swaped in flags then
  308. swapleftright;
  309. case tstringdef(left.resulttype.def).string_typ of
  310. st_shortstring:
  311. begin
  312. case nodetype of
  313. ltn,lten,gtn,gten,equaln,unequaln :
  314. begin
  315. {$warning forced stdcall calling}
  316. paraloc1:=paramanager.getintparaloc(pocall_stdcall,1);
  317. paraloc2:=paramanager.getintparaloc(pocall_stdcall,2);
  318. { process parameters }
  319. secondpass(left);
  320. location_release(exprasmlist,left.location);
  321. if paraloc2.loc=LOC_REGISTER then
  322. begin
  323. hregister2:=rg.getaddressregister(exprasmlist);
  324. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,hregister2);
  325. end
  326. else
  327. begin
  328. paramanager.allocparaloc(exprasmlist,paraloc2);
  329. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraloc2);
  330. end;
  331. secondpass(right);
  332. location_release(exprasmlist,right.location);
  333. if paraloc1.loc=LOC_REGISTER then
  334. begin
  335. hregister1:=rg.getaddressregister(exprasmlist);
  336. cg.a_loadaddr_ref_reg(exprasmlist,right.location.reference,hregister1);
  337. end
  338. else
  339. begin
  340. paramanager.allocparaloc(exprasmlist,paraloc1);
  341. cg.a_paramaddr_ref(exprasmlist,right.location.reference,paraloc1);
  342. end;
  343. { push parameters }
  344. if paraloc1.loc=LOC_REGISTER then
  345. begin
  346. rg.ungetregisterint(exprasmlist,hregister2);
  347. paramanager.allocparaloc(exprasmlist,paraloc2);
  348. cg.a_param_reg(exprasmlist,OS_ADDR,hregister2,paraloc2);
  349. end;
  350. if paraloc2.loc=LOC_REGISTER then
  351. begin
  352. rg.ungetregisterint(exprasmlist,hregister1);
  353. paramanager.allocparaloc(exprasmlist,paraloc1);
  354. cg.a_param_reg(exprasmlist,OS_ADDR,hregister1,paraloc1);
  355. end;
  356. paramanager.freeparaloc(exprasmlist,paraloc1);
  357. paramanager.freeparaloc(exprasmlist,paraloc2);
  358. rg.allocexplicitregistersint(exprasmlist,paramanager.get_volatile_registers_int(pocall_default));
  359. cg.a_call_name(exprasmlist,'FPC_SHORTSTR_COMPARE');
  360. rg.deallocexplicitregistersint(exprasmlist,paramanager.get_volatile_registers_int(pocall_default));
  361. location_freetemp(exprasmlist,left.location);
  362. location_freetemp(exprasmlist,right.location);
  363. end;
  364. end;
  365. set_result_location(true,true);
  366. end;
  367. else
  368. { rest should be handled in first pass (JM) }
  369. internalerror(200108303);
  370. end;
  371. end;
  372. {*****************************************************************************
  373. AddBoolean
  374. *****************************************************************************}
  375. procedure ti386addnode.second_addboolean;
  376. var
  377. op : TAsmOp;
  378. opsize : TOpsize;
  379. cmpop,
  380. isjump : boolean;
  381. otl,ofl : tasmlabel;
  382. begin
  383. { calculate the operator which is more difficult }
  384. firstcomplex(self);
  385. cmpop:=false;
  386. if (torddef(left.resulttype.def).typ=bool8bit) or
  387. (torddef(right.resulttype.def).typ=bool8bit) then
  388. opsize:=S_B
  389. else
  390. if (torddef(left.resulttype.def).typ=bool16bit) or
  391. (torddef(right.resulttype.def).typ=bool16bit) then
  392. opsize:=S_W
  393. else
  394. opsize:=S_L;
  395. if (cs_full_boolean_eval in aktlocalswitches) or
  396. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  397. begin
  398. if left.nodetype in [ordconstn,realconstn] then
  399. swapleftright;
  400. isjump:=(left.expectloc=LOC_JUMP);
  401. if isjump then
  402. begin
  403. otl:=truelabel;
  404. objectlibrary.getlabel(truelabel);
  405. ofl:=falselabel;
  406. objectlibrary.getlabel(falselabel);
  407. end;
  408. secondpass(left);
  409. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  410. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  411. if isjump then
  412. begin
  413. truelabel:=otl;
  414. falselabel:=ofl;
  415. end
  416. else if left.location.loc=LOC_JUMP then
  417. internalerror(200310081);
  418. isjump:=(right.expectloc=LOC_JUMP);
  419. if isjump then
  420. begin
  421. otl:=truelabel;
  422. objectlibrary.getlabel(truelabel);
  423. ofl:=falselabel;
  424. objectlibrary.getlabel(falselabel);
  425. end;
  426. secondpass(right);
  427. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  428. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],false);
  429. if isjump then
  430. begin
  431. truelabel:=otl;
  432. falselabel:=ofl;
  433. end
  434. else if left.location.loc=LOC_JUMP then
  435. internalerror(200310082);
  436. { left must be a register }
  437. left_must_be_reg(opsize,false);
  438. { compare the }
  439. case nodetype of
  440. ltn,lten,gtn,gten,
  441. equaln,unequaln :
  442. begin
  443. op:=A_CMP;
  444. cmpop:=true;
  445. end;
  446. xorn :
  447. op:=A_XOR;
  448. orn :
  449. op:=A_OR;
  450. andn :
  451. op:=A_AND;
  452. else
  453. internalerror(200203247);
  454. end;
  455. emit_op_right_left(op,opsize);
  456. location_freetemp(exprasmlist,right.location);
  457. location_release(exprasmlist,right.location);
  458. if cmpop then
  459. begin
  460. location_freetemp(exprasmlist,left.location);
  461. location_release(exprasmlist,left.location);
  462. end;
  463. set_result_location(cmpop,true);
  464. end
  465. else
  466. begin
  467. case nodetype of
  468. andn,
  469. orn :
  470. begin
  471. location_reset(location,LOC_JUMP,OS_NO);
  472. case nodetype of
  473. andn :
  474. begin
  475. otl:=truelabel;
  476. objectlibrary.getlabel(truelabel);
  477. secondpass(left);
  478. maketojumpbool(exprasmlist,left,lr_load_regvars);
  479. cg.a_label(exprasmlist,truelabel);
  480. truelabel:=otl;
  481. end;
  482. orn :
  483. begin
  484. ofl:=falselabel;
  485. objectlibrary.getlabel(falselabel);
  486. secondpass(left);
  487. maketojumpbool(exprasmlist,left,lr_load_regvars);
  488. cg.a_label(exprasmlist,falselabel);
  489. falselabel:=ofl;
  490. end;
  491. else
  492. internalerror(2003042212);
  493. end;
  494. secondpass(right);
  495. maketojumpbool(exprasmlist,right,lr_load_regvars);
  496. end;
  497. else
  498. internalerror(2003042213);
  499. end;
  500. end;
  501. end;
  502. {*****************************************************************************
  503. AddFloat
  504. *****************************************************************************}
  505. procedure ti386addnode.second_addfloat;
  506. var
  507. op : TAsmOp;
  508. resflags : tresflags;
  509. pushedfpu,
  510. cmpop : boolean;
  511. r : Tregister;
  512. begin
  513. pass_left_and_right(pushedfpu);
  514. cmpop:=false;
  515. case nodetype of
  516. addn :
  517. op:=A_FADDP;
  518. muln :
  519. op:=A_FMULP;
  520. subn :
  521. op:=A_FSUBP;
  522. slashn :
  523. op:=A_FDIVP;
  524. ltn,lten,gtn,gten,
  525. equaln,unequaln :
  526. begin
  527. op:=A_FCOMPP;
  528. cmpop:=true;
  529. end;
  530. else
  531. internalerror(2003042214);
  532. end;
  533. if (right.location.loc<>LOC_FPUREGISTER) then
  534. begin
  535. cg.a_loadfpu_loc_reg(exprasmlist,right.location,NR_ST);
  536. if (right.location.loc <> LOC_CFPUREGISTER) and
  537. pushedfpu then
  538. location_freetemp(exprasmlist,left.location);
  539. if (left.location.loc<>LOC_FPUREGISTER) then
  540. begin
  541. cg.a_loadfpu_loc_reg(exprasmlist,left.location,NR_ST);
  542. if (left.location.loc <> LOC_CFPUREGISTER) and
  543. pushedfpu then
  544. location_freetemp(exprasmlist,left.location);
  545. end
  546. else
  547. begin
  548. { left was on the stack => swap }
  549. toggleflag(nf_swaped);
  550. end;
  551. { releases the right reference }
  552. location_release(exprasmlist,right.location);
  553. end
  554. { the nominator in st0 }
  555. else if (left.location.loc<>LOC_FPUREGISTER) then
  556. begin
  557. cg.a_loadfpu_loc_reg(exprasmlist,left.location,NR_ST);
  558. if (left.location.loc <> LOC_CFPUREGISTER) and
  559. pushedfpu then
  560. location_freetemp(exprasmlist,left.location);
  561. end
  562. else
  563. begin
  564. { fpu operands are always in the wrong order on the stack }
  565. toggleflag(nf_swaped);
  566. end;
  567. { releases the left reference }
  568. if (left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  569. location_release(exprasmlist,left.location);
  570. { if we swaped the tree nodes, then use the reverse operator }
  571. if nf_swaped in flags then
  572. begin
  573. if (nodetype=slashn) then
  574. op:=A_FDIVRP
  575. else if (nodetype=subn) then
  576. op:=A_FSUBRP;
  577. end;
  578. { to avoid the pentium bug
  579. if (op=FDIVP) and (opt_processors=pentium) then
  580. cg.a_call_name(exprasmlist,'EMUL_FDIVP')
  581. else
  582. }
  583. { the Intel assemblers want operands }
  584. if op<>A_FCOMPP then
  585. begin
  586. emit_reg_reg(op,S_NO,NR_ST,NR_ST1);
  587. dec(trgcpu(rg).fpuvaroffset);
  588. end
  589. else
  590. begin
  591. emit_none(op,S_NO);
  592. dec(trgcpu(rg).fpuvaroffset,2);
  593. end;
  594. { on comparison load flags }
  595. if cmpop then
  596. begin
  597. r:=rg.getexplicitregisterint(exprasmlist,NR_AX);
  598. emit_reg(A_FNSTSW,S_NO,r);
  599. emit_none(A_SAHF,S_NO);
  600. rg.ungetregisterint(exprasmlist,r);
  601. if nf_swaped in flags then
  602. begin
  603. case nodetype of
  604. equaln : resflags:=F_E;
  605. unequaln : resflags:=F_NE;
  606. ltn : resflags:=F_A;
  607. lten : resflags:=F_AE;
  608. gtn : resflags:=F_B;
  609. gten : resflags:=F_BE;
  610. end;
  611. end
  612. else
  613. begin
  614. case nodetype of
  615. equaln : resflags:=F_E;
  616. unequaln : resflags:=F_NE;
  617. ltn : resflags:=F_B;
  618. lten : resflags:=F_BE;
  619. gtn : resflags:=F_A;
  620. gten : resflags:=F_AE;
  621. end;
  622. end;
  623. location_reset(location,LOC_FLAGS,OS_NO);
  624. location.resflags:=resflags;
  625. end
  626. else
  627. begin
  628. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  629. location.register:=NR_ST;
  630. end;
  631. end;
  632. {*****************************************************************************
  633. AddSmallSet
  634. *****************************************************************************}
  635. procedure ti386addnode.second_addsmallset;
  636. var
  637. opsize : TOpSize;
  638. op : TAsmOp;
  639. cmpop,
  640. pushedfpu,
  641. extra_not,
  642. noswap : boolean;
  643. begin
  644. pass_left_and_right(pushedfpu);
  645. { when a setdef is passed, it has to be a smallset }
  646. if ((left.resulttype.def.deftype=setdef) and
  647. (tsetdef(left.resulttype.def).settype<>smallset)) or
  648. ((right.resulttype.def.deftype=setdef) and
  649. (tsetdef(right.resulttype.def).settype<>smallset)) then
  650. internalerror(200203301);
  651. cmpop:=false;
  652. noswap:=false;
  653. extra_not:=false;
  654. opsize:=S_L;
  655. case nodetype of
  656. addn :
  657. begin
  658. { this is a really ugly hack!!!!!!!!!! }
  659. { this could be done later using EDI }
  660. { as it is done for subn }
  661. { instead of two registers!!!! }
  662. { adding elements is not commutative }
  663. if (nf_swaped in flags) and (left.nodetype=setelementn) then
  664. swapleftright;
  665. { are we adding set elements ? }
  666. if right.nodetype=setelementn then
  667. begin
  668. { no range support for smallsets! }
  669. if assigned(tsetelementnode(right).right) then
  670. internalerror(43244);
  671. { bts requires both elements to be registers }
  672. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  673. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],true);
  674. op:=A_BTS;
  675. noswap:=true;
  676. end
  677. else
  678. op:=A_OR;
  679. end;
  680. symdifn :
  681. op:=A_XOR;
  682. muln :
  683. op:=A_AND;
  684. subn :
  685. begin
  686. op:=A_AND;
  687. if (not(nf_swaped in flags)) and
  688. (right.location.loc=LOC_CONSTANT) then
  689. right.location.value := not(right.location.value)
  690. else if (nf_swaped in flags) and
  691. (left.location.loc=LOC_CONSTANT) then
  692. left.location.value := not(left.location.value)
  693. else
  694. extra_not:=true;
  695. end;
  696. equaln,
  697. unequaln :
  698. begin
  699. op:=A_CMP;
  700. cmpop:=true;
  701. end;
  702. lten,gten:
  703. begin
  704. If (not(nf_swaped in flags) and
  705. (nodetype = lten)) or
  706. ((nf_swaped in flags) and
  707. (nodetype = gten)) then
  708. swapleftright;
  709. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],true);
  710. emit_op_right_left(A_AND,opsize);
  711. op:=A_CMP;
  712. cmpop:=true;
  713. { warning: ugly hack, we need a JE so change the node to equaln }
  714. nodetype:=equaln;
  715. end;
  716. xorn :
  717. op:=A_XOR;
  718. orn :
  719. op:=A_OR;
  720. andn :
  721. op:=A_AND;
  722. else
  723. internalerror(2003042215);
  724. end;
  725. { left must be a register }
  726. left_must_be_reg(opsize,noswap);
  727. emit_generic_code(op,opsize,true,extra_not,false);
  728. location_freetemp(exprasmlist,right.location);
  729. location_release(exprasmlist,right.location);
  730. if cmpop then
  731. begin
  732. location_freetemp(exprasmlist,left.location);
  733. location_release(exprasmlist,left.location);
  734. end;
  735. set_result_location(cmpop,true);
  736. end;
  737. {*****************************************************************************
  738. Add64bit
  739. *****************************************************************************}
  740. procedure ti386addnode.second_add64bit;
  741. var
  742. op : TOpCG;
  743. op1,op2 : TAsmOp;
  744. opsize : TOpSize;
  745. hregister,
  746. hregister2 : tregister;
  747. href : treference;
  748. hl4 : tasmlabel;
  749. pushedfpu,
  750. mboverflow,
  751. cmpop,
  752. unsigned,delete:boolean;
  753. r:Tregister;
  754. procedure firstjmp64bitcmp;
  755. var
  756. oldnodetype : tnodetype;
  757. begin
  758. load_all_regvars(exprasmlist);
  759. { the jump the sequence is a little bit hairy }
  760. case nodetype of
  761. ltn,gtn:
  762. begin
  763. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),truelabel);
  764. { cheat a little bit for the negative test }
  765. toggleflag(nf_swaped);
  766. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),falselabel);
  767. toggleflag(nf_swaped);
  768. end;
  769. lten,gten:
  770. begin
  771. oldnodetype:=nodetype;
  772. if nodetype=lten then
  773. nodetype:=ltn
  774. else
  775. nodetype:=gtn;
  776. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),truelabel);
  777. { cheat for the negative test }
  778. if nodetype=ltn then
  779. nodetype:=gtn
  780. else
  781. nodetype:=ltn;
  782. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),falselabel);
  783. nodetype:=oldnodetype;
  784. end;
  785. equaln:
  786. cg.a_jmp_flags(exprasmlist,F_NE,falselabel);
  787. unequaln:
  788. cg.a_jmp_flags(exprasmlist,F_NE,truelabel);
  789. end;
  790. end;
  791. procedure secondjmp64bitcmp;
  792. begin
  793. { the jump the sequence is a little bit hairy }
  794. case nodetype of
  795. ltn,gtn,lten,gten:
  796. begin
  797. { the comparisaion of the low dword have to be }
  798. { always unsigned! }
  799. cg.a_jmp_flags(exprasmlist,getresflags(true),truelabel);
  800. cg.a_jmp_always(exprasmlist,falselabel);
  801. end;
  802. equaln:
  803. begin
  804. cg.a_jmp_flags(exprasmlist,F_NE,falselabel);
  805. cg.a_jmp_always(exprasmlist,truelabel);
  806. end;
  807. unequaln:
  808. begin
  809. cg.a_jmp_flags(exprasmlist,F_NE,truelabel);
  810. cg.a_jmp_always(exprasmlist,falselabel);
  811. end;
  812. end;
  813. end;
  814. begin
  815. firstcomplex(self);
  816. pass_left_and_right(pushedfpu);
  817. op1:=A_NONE;
  818. op2:=A_NONE;
  819. mboverflow:=false;
  820. cmpop:=false;
  821. opsize:=S_L;
  822. unsigned:=((left.resulttype.def.deftype=orddef) and
  823. (torddef(left.resulttype.def).typ=u64bit)) or
  824. ((right.resulttype.def.deftype=orddef) and
  825. (torddef(right.resulttype.def).typ=u64bit));
  826. case nodetype of
  827. addn :
  828. begin
  829. op:=OP_ADD;
  830. mboverflow:=true;
  831. end;
  832. subn :
  833. begin
  834. op:=OP_SUB;
  835. op1:=A_SUB;
  836. op2:=A_SBB;
  837. mboverflow:=true;
  838. end;
  839. ltn,lten,
  840. gtn,gten,
  841. equaln,unequaln:
  842. begin
  843. op:=OP_NONE;
  844. cmpop:=true;
  845. end;
  846. xorn:
  847. op:=OP_XOR;
  848. orn:
  849. op:=OP_OR;
  850. andn:
  851. op:=OP_AND;
  852. else
  853. begin
  854. { everything should be handled in pass_1 (JM) }
  855. internalerror(200109051);
  856. end;
  857. end;
  858. { left and right no register? }
  859. { then one must be demanded }
  860. if (left.location.loc<>LOC_REGISTER) then
  861. begin
  862. if (right.location.loc<>LOC_REGISTER) then
  863. begin
  864. { we can reuse a CREGISTER for comparison }
  865. if not((left.location.loc=LOC_CREGISTER) and cmpop) then
  866. begin
  867. delete:=left.location.loc<>LOC_CREGISTER;
  868. hregister:=rg.getregisterint(exprasmlist,OS_INT);
  869. hregister2:=rg.getregisterint(exprasmlist,OS_INT);
  870. cg64.a_load64_loc_reg(exprasmlist,left.location,joinreg64(hregister,hregister2),delete);
  871. location_reset(left.location,LOC_REGISTER,OS_64);
  872. left.location.registerlow:=hregister;
  873. left.location.registerhigh:=hregister2;
  874. end;
  875. end
  876. else
  877. begin
  878. location_swap(left.location,right.location);
  879. toggleflag(nf_swaped);
  880. end;
  881. end;
  882. { at this point, left.location.loc should be LOC_REGISTER }
  883. if right.location.loc=LOC_REGISTER then
  884. begin
  885. { when swapped another result register }
  886. if (nodetype=subn) and (nf_swaped in flags) then
  887. begin
  888. cg64.a_op64_reg_reg(exprasmlist,op,
  889. left.location.register64,
  890. right.location.register64);
  891. location_swap(left.location,right.location);
  892. toggleflag(nf_swaped);
  893. end
  894. else if cmpop then
  895. begin
  896. emit_reg_reg(A_CMP,S_L,right.location.registerhigh,left.location.registerhigh);
  897. firstjmp64bitcmp;
  898. emit_reg_reg(A_CMP,S_L,right.location.registerlow,left.location.registerlow);
  899. secondjmp64bitcmp;
  900. end
  901. else
  902. begin
  903. cg64.a_op64_reg_reg(exprasmlist,op,
  904. right.location.register64,
  905. left.location.register64);
  906. end;
  907. location_release(exprasmlist,right.location);
  908. end
  909. else
  910. begin
  911. { right.location<>LOC_REGISTER }
  912. if (nodetype=subn) and (nf_swaped in flags) then
  913. begin
  914. r:=rg.getregisterint(exprasmlist,OS_INT);
  915. cg64.a_load64low_loc_reg(exprasmlist,right.location,r);
  916. emit_reg_reg(op1,opsize,left.location.registerlow,r);
  917. emit_reg_reg(A_MOV,opsize,r,left.location.registerlow);
  918. cg64.a_load64high_loc_reg(exprasmlist,right.location,r);
  919. { the carry flag is still ok }
  920. emit_reg_reg(op2,opsize,left.location.registerhigh,r);
  921. emit_reg_reg(A_MOV,opsize,r,left.location.registerhigh);
  922. rg.ungetregisterint(exprasmlist,r);
  923. if right.location.loc<>LOC_CREGISTER then
  924. begin
  925. location_freetemp(exprasmlist,right.location);
  926. location_release(exprasmlist,right.location);
  927. end;
  928. end
  929. else if cmpop then
  930. begin
  931. case right.location.loc of
  932. LOC_CREGISTER :
  933. begin
  934. emit_reg_reg(A_CMP,S_L,right.location.registerhigh,left.location.registerhigh);
  935. firstjmp64bitcmp;
  936. emit_reg_reg(A_CMP,S_L,right.location.registerlow,left.location.registerlow);
  937. secondjmp64bitcmp;
  938. end;
  939. LOC_CREFERENCE,
  940. LOC_REFERENCE :
  941. begin
  942. href:=right.location.reference;
  943. inc(href.offset,4);
  944. emit_ref_reg(A_CMP,S_L,href,left.location.registerhigh);
  945. firstjmp64bitcmp;
  946. emit_ref_reg(A_CMP,S_L,right.location.reference,left.location.registerlow);
  947. secondjmp64bitcmp;
  948. cg.a_jmp_always(exprasmlist,falselabel);
  949. location_freetemp(exprasmlist,right.location);
  950. location_release(exprasmlist,right.location);
  951. end;
  952. LOC_CONSTANT :
  953. begin
  954. exprasmlist.concat(taicpu.op_const_reg(A_CMP,S_L,hi(right.location.valueqword),left.location.registerhigh));
  955. firstjmp64bitcmp;
  956. exprasmlist.concat(taicpu.op_const_reg(A_CMP,S_L,lo(right.location.valueqword),left.location.registerlow));
  957. secondjmp64bitcmp;
  958. end;
  959. else
  960. internalerror(200203282);
  961. end;
  962. end
  963. else
  964. begin
  965. cg64.a_op64_loc_reg(exprasmlist,op,right.location,
  966. left.location.register64);
  967. if (right.location.loc<>LOC_CREGISTER) then
  968. begin
  969. location_freetemp(exprasmlist,right.location);
  970. location_release(exprasmlist,right.location);
  971. end;
  972. end;
  973. end;
  974. if (left.location.loc<>LOC_CREGISTER) and cmpop then
  975. begin
  976. location_freetemp(exprasmlist,left.location);
  977. location_release(exprasmlist,left.location);
  978. end;
  979. { only in case of overflow operations }
  980. { produce overflow code }
  981. { we must put it here directly, because sign of operation }
  982. { is in unsigned VAR!! }
  983. if mboverflow then
  984. begin
  985. if cs_check_overflow in aktlocalswitches then
  986. begin
  987. objectlibrary.getlabel(hl4);
  988. if unsigned then
  989. cg.a_jmp_flags(exprasmlist,F_AE,hl4)
  990. else
  991. cg.a_jmp_flags(exprasmlist,F_GE,hl4);
  992. cg.a_call_name(exprasmlist,'FPC_OVERFLOW');
  993. cg.a_label(exprasmlist,hl4);
  994. end;
  995. end;
  996. { we have LOC_JUMP as result }
  997. if cmpop then
  998. location_reset(location,LOC_JUMP,OS_NO)
  999. else
  1000. location_copy(location,left.location);
  1001. end;
  1002. {*****************************************************************************
  1003. AddMMX
  1004. *****************************************************************************}
  1005. {$ifdef SUPPORT_MMX}
  1006. procedure ti386addnode.second_addmmx;
  1007. var
  1008. op : TAsmOp;
  1009. pushedfpu,
  1010. cmpop : boolean;
  1011. mmxbase : tmmxtype;
  1012. r,hregister : tregister;
  1013. begin
  1014. pass_left_and_right(pushedfpu);
  1015. cmpop:=false;
  1016. mmxbase:=mmx_type(left.resulttype.def);
  1017. case nodetype of
  1018. addn :
  1019. begin
  1020. if (cs_mmx_saturation in aktlocalswitches) then
  1021. begin
  1022. case mmxbase of
  1023. mmxs8bit:
  1024. op:=A_PADDSB;
  1025. mmxu8bit:
  1026. op:=A_PADDUSB;
  1027. mmxs16bit,mmxfixed16:
  1028. op:=A_PADDSB;
  1029. mmxu16bit:
  1030. op:=A_PADDUSW;
  1031. end;
  1032. end
  1033. else
  1034. begin
  1035. case mmxbase of
  1036. mmxs8bit,mmxu8bit:
  1037. op:=A_PADDB;
  1038. mmxs16bit,mmxu16bit,mmxfixed16:
  1039. op:=A_PADDW;
  1040. mmxs32bit,mmxu32bit:
  1041. op:=A_PADDD;
  1042. end;
  1043. end;
  1044. end;
  1045. muln :
  1046. begin
  1047. case mmxbase of
  1048. mmxs16bit,mmxu16bit:
  1049. op:=A_PMULLW;
  1050. mmxfixed16:
  1051. op:=A_PMULHW;
  1052. end;
  1053. end;
  1054. subn :
  1055. begin
  1056. if (cs_mmx_saturation in aktlocalswitches) then
  1057. begin
  1058. case mmxbase of
  1059. mmxs8bit:
  1060. op:=A_PSUBSB;
  1061. mmxu8bit:
  1062. op:=A_PSUBUSB;
  1063. mmxs16bit,mmxfixed16:
  1064. op:=A_PSUBSB;
  1065. mmxu16bit:
  1066. op:=A_PSUBUSW;
  1067. end;
  1068. end
  1069. else
  1070. begin
  1071. case mmxbase of
  1072. mmxs8bit,mmxu8bit:
  1073. op:=A_PSUBB;
  1074. mmxs16bit,mmxu16bit,mmxfixed16:
  1075. op:=A_PSUBW;
  1076. mmxs32bit,mmxu32bit:
  1077. op:=A_PSUBD;
  1078. end;
  1079. end;
  1080. end;
  1081. xorn:
  1082. op:=A_PXOR;
  1083. orn:
  1084. op:=A_POR;
  1085. andn:
  1086. op:=A_PAND;
  1087. else
  1088. internalerror(2003042214);
  1089. end;
  1090. { left and right no register? }
  1091. { then one must be demanded }
  1092. if (left.location.loc<>LOC_MMXREGISTER) then
  1093. begin
  1094. if (right.location.loc=LOC_MMXREGISTER) then
  1095. begin
  1096. location_swap(left.location,right.location);
  1097. toggleflag(nf_swaped);
  1098. end
  1099. else
  1100. begin
  1101. { register variable ? }
  1102. if (left.location.loc=LOC_CMMXREGISTER) then
  1103. begin
  1104. hregister:=rg.getregistermm(exprasmlist);
  1105. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  1106. end
  1107. else
  1108. begin
  1109. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1110. internalerror(200203245);
  1111. location_release(exprasmlist,left.location);
  1112. hregister:=rg.getregistermm(exprasmlist);
  1113. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  1114. end;
  1115. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  1116. left.location.register:=hregister;
  1117. end;
  1118. end;
  1119. { at this point, left.location.loc should be LOC_MMXREGISTER }
  1120. if right.location.loc<>LOC_MMXREGISTER then
  1121. begin
  1122. if (nodetype=subn) and (nf_swaped in flags) then
  1123. begin
  1124. if right.location.loc=LOC_CMMXREGISTER then
  1125. begin
  1126. emit_reg_reg(A_MOVQ,S_NO,right.location.register,NR_MM7);
  1127. emit_reg_reg(op,S_NO,left.location.register,NR_MM7);
  1128. emit_reg_reg(A_MOVQ,S_NO,NR_MM7,left.location.register);
  1129. end
  1130. else
  1131. begin
  1132. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1133. internalerror(200203247);
  1134. emit_ref_reg(A_MOVQ,S_NO,right.location.reference,NR_MM7);
  1135. emit_reg_reg(op,S_NO,left.location.register,NR_MM7);
  1136. emit_reg_reg(A_MOVQ,S_NO,NR_MM7,left.location.register);
  1137. location_release(exprasmlist,right.location);
  1138. end;
  1139. end
  1140. else
  1141. begin
  1142. if (right.location.loc=LOC_CMMXREGISTER) then
  1143. emit_reg_reg(op,S_NO,right.location.register,left.location.register)
  1144. else
  1145. begin
  1146. if not(right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1147. internalerror(200203246);
  1148. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  1149. location_release(exprasmlist,right.location);
  1150. end;
  1151. end;
  1152. end
  1153. else
  1154. begin
  1155. { right.location=LOC_MMXREGISTER }
  1156. if (nodetype=subn) and (nf_swaped in flags) then
  1157. begin
  1158. emit_reg_reg(op,S_NO,left.location.register,right.location.register);
  1159. location_swap(left.location,right.location);
  1160. toggleflag(nf_swaped);
  1161. end
  1162. else
  1163. begin
  1164. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  1165. end;
  1166. end;
  1167. location_freetemp(exprasmlist,right.location);
  1168. location_release(exprasmlist,right.location);
  1169. if cmpop then
  1170. begin
  1171. location_freetemp(exprasmlist,left.location);
  1172. location_release(exprasmlist,left.location);
  1173. end;
  1174. set_result_location(cmpop,true);
  1175. end;
  1176. {$endif SUPPORT_MMX}
  1177. {*****************************************************************************
  1178. MUL
  1179. *****************************************************************************}
  1180. procedure ti386addnode.second_mul;
  1181. var r:Tregister;
  1182. begin
  1183. {The location.register will be filled in later (JM)}
  1184. location_reset(location,LOC_REGISTER,OS_INT);
  1185. {Get a temp register and load the left value into it
  1186. and free the location.}
  1187. r:=rg.getregisterint(exprasmlist,OS_INT);
  1188. cg.a_load_loc_reg(exprasmlist,OS_INT,left.location,r);
  1189. location_release(exprasmlist,left.location);
  1190. {Allocate EAX.}
  1191. rg.getexplicitregisterint(exprasmlist,NR_EAX);
  1192. {Load the right value.}
  1193. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,NR_EAX);
  1194. location_release(exprasmlist,right.location);
  1195. {The mul instruction frees register r.}
  1196. rg.ungetregisterint(exprasmlist,r);
  1197. {Also allocate EDX, since it is also modified by a mul (JM).}
  1198. rg.getexplicitregisterint(exprasmlist,NR_EDX);
  1199. emit_reg(A_MUL,S_L,r);
  1200. {Free EDX}
  1201. rg.ungetregisterint(exprasmlist,NR_EDX);
  1202. {Free EAX}
  1203. rg.ungetregisterint(exprasmlist,NR_EAX);
  1204. {Allocate a new register and store the result in EAX in it.}
  1205. location.register:=rg.getregisterint(exprasmlist,OS_INT);
  1206. emit_reg_reg(A_MOV,S_L,NR_EAX,location.register);
  1207. location_freetemp(exprasmlist,left.location);
  1208. location_freetemp(exprasmlist,right.location);
  1209. end;
  1210. {*****************************************************************************
  1211. pass_2
  1212. *****************************************************************************}
  1213. procedure ti386addnode.pass_2;
  1214. { is also being used for xor, and "mul", "sub, or and comparative }
  1215. { operators }
  1216. var
  1217. pushedfpu,
  1218. mboverflow,cmpop : boolean;
  1219. op : tasmop;
  1220. opsize : topsize;
  1221. { true, if unsigned types are compared }
  1222. unsigned : boolean;
  1223. { is_in_dest if the result is put directly into }
  1224. { the resulting refernce or varregister }
  1225. {is_in_dest : boolean;}
  1226. { true, if for sets subtractions the extra not should generated }
  1227. extra_not : boolean;
  1228. begin
  1229. { to make it more readable, string and set (not smallset!) have their
  1230. own procedures }
  1231. case left.resulttype.def.deftype of
  1232. orddef :
  1233. begin
  1234. { handling boolean expressions }
  1235. if is_boolean(left.resulttype.def) and
  1236. is_boolean(right.resulttype.def) then
  1237. begin
  1238. second_addboolean;
  1239. exit;
  1240. end
  1241. { 64bit operations }
  1242. else if is_64bit(left.resulttype.def) then
  1243. begin
  1244. second_add64bit;
  1245. exit;
  1246. end;
  1247. end;
  1248. stringdef :
  1249. begin
  1250. second_addstring;
  1251. exit;
  1252. end;
  1253. setdef :
  1254. begin
  1255. { normalsets are already handled in pass1 }
  1256. if (tsetdef(left.resulttype.def).settype<>smallset) then
  1257. internalerror(200109041);
  1258. second_addsmallset;
  1259. exit;
  1260. end;
  1261. arraydef :
  1262. begin
  1263. {$ifdef SUPPORT_MMX}
  1264. if is_mmx_able_array(left.resulttype.def) then
  1265. begin
  1266. second_addmmx;
  1267. exit;
  1268. end;
  1269. {$endif SUPPORT_MMX}
  1270. end;
  1271. floatdef :
  1272. begin
  1273. second_addfloat;
  1274. exit;
  1275. end;
  1276. end;
  1277. { defaults }
  1278. {is_in_dest:=false;}
  1279. extra_not:=false;
  1280. mboverflow:=false;
  1281. cmpop:=false;
  1282. unsigned:=not(is_signed(left.resulttype.def)) or
  1283. not(is_signed(right.resulttype.def));
  1284. opsize:=def_opsize(left.resulttype.def);
  1285. pass_left_and_right(pushedfpu);
  1286. if (left.resulttype.def.deftype=pointerdef) or
  1287. (right.resulttype.def.deftype=pointerdef) or
  1288. (is_class_or_interface(right.resulttype.def) and is_class_or_interface(left.resulttype.def)) or
  1289. (left.resulttype.def.deftype=classrefdef) or
  1290. (left.resulttype.def.deftype=procvardef) or
  1291. ((left.resulttype.def.deftype=enumdef) and
  1292. (left.resulttype.def.size=4)) or
  1293. ((left.resulttype.def.deftype=orddef) and
  1294. (torddef(left.resulttype.def).typ in [s32bit,u32bit])) or
  1295. ((right.resulttype.def.deftype=orddef) and
  1296. (torddef(right.resulttype.def).typ in [s32bit,u32bit])) then
  1297. begin
  1298. case nodetype of
  1299. addn :
  1300. begin
  1301. op:=A_ADD;
  1302. mboverflow:=true;
  1303. end;
  1304. muln :
  1305. begin
  1306. if unsigned then
  1307. op:=A_MUL
  1308. else
  1309. op:=A_IMUL;
  1310. mboverflow:=true;
  1311. end;
  1312. subn :
  1313. begin
  1314. op:=A_SUB;
  1315. mboverflow:=true;
  1316. end;
  1317. ltn,lten,
  1318. gtn,gten,
  1319. equaln,unequaln :
  1320. begin
  1321. op:=A_CMP;
  1322. cmpop:=true;
  1323. end;
  1324. xorn :
  1325. op:=A_XOR;
  1326. orn :
  1327. op:=A_OR;
  1328. andn :
  1329. op:=A_AND;
  1330. else
  1331. internalerror(200304229);
  1332. end;
  1333. { filter MUL, which requires special handling }
  1334. if op=A_MUL then
  1335. begin
  1336. second_mul;
  1337. exit;
  1338. end;
  1339. { Convert flags to register first }
  1340. if (left.location.loc=LOC_FLAGS) then
  1341. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  1342. if (right.location.loc=LOC_FLAGS) then
  1343. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],false);
  1344. left_must_be_reg(opsize,false);
  1345. emit_generic_code(op,opsize,unsigned,extra_not,mboverflow);
  1346. location_freetemp(exprasmlist,right.location);
  1347. location_release(exprasmlist,right.location);
  1348. if cmpop and
  1349. (left.location.loc<>LOC_CREGISTER) then
  1350. begin
  1351. location_freetemp(exprasmlist,left.location);
  1352. location_release(exprasmlist,left.location);
  1353. end;
  1354. set_result_location(cmpop,unsigned);
  1355. end
  1356. { 8/16 bit enum,char,wchar types }
  1357. else
  1358. if ((left.resulttype.def.deftype=orddef) and
  1359. (torddef(left.resulttype.def).typ in [uchar,uwidechar])) or
  1360. ((left.resulttype.def.deftype=enumdef) and
  1361. ((left.resulttype.def.size=1) or
  1362. (left.resulttype.def.size=2))) then
  1363. begin
  1364. case nodetype of
  1365. ltn,lten,gtn,gten,
  1366. equaln,unequaln :
  1367. cmpop:=true;
  1368. else
  1369. internalerror(2003042210);
  1370. end;
  1371. left_must_be_reg(opsize,false);
  1372. emit_op_right_left(A_CMP,opsize);
  1373. location_freetemp(exprasmlist,right.location);
  1374. location_release(exprasmlist,right.location);
  1375. if left.location.loc<>LOC_CREGISTER then
  1376. begin
  1377. location_freetemp(exprasmlist,left.location);
  1378. location_release(exprasmlist,left.location);
  1379. end;
  1380. set_result_location(true,true);
  1381. end
  1382. else
  1383. internalerror(2003042211);
  1384. end;
  1385. begin
  1386. caddnode:=ti386addnode;
  1387. end.
  1388. {
  1389. $Log$
  1390. Revision 1.81 2003-10-08 09:13:16 florian
  1391. * fixed full bool evalution and bool xor, if the left or right side have LOC_JUMP
  1392. Revision 1.80 2003/10/01 20:34:49 peter
  1393. * procinfo unit contains tprocinfo
  1394. * cginfo renamed to cgbase
  1395. * moved cgmessage to verbose
  1396. * fixed ppc and sparc compiles
  1397. Revision 1.79 2003/09/28 21:48:20 peter
  1398. * fix register leaks
  1399. Revision 1.78 2003/09/28 13:35:40 peter
  1400. * shortstr compare updated for different calling conventions
  1401. Revision 1.77 2003/09/10 08:31:48 marco
  1402. * Patch from Peter for paraloc
  1403. Revision 1.76 2003/09/03 15:55:01 peter
  1404. * NEWRA branch merged
  1405. Revision 1.75.2.2 2003/08/31 13:50:16 daniel
  1406. * Remove sorting and use pregenerated indexes
  1407. * Some work on making things compile
  1408. Revision 1.75.2.1 2003/08/29 17:29:00 peter
  1409. * next batch of updates
  1410. Revision 1.75 2003/08/03 20:38:00 daniel
  1411. * Made code generator reverse or/add/and/xor/imul instructions when
  1412. possible to reduce the slowdown of spills.
  1413. Revision 1.74 2003/08/03 20:19:43 daniel
  1414. - Removed cmpop from Ti386addnode.second_addstring
  1415. Revision 1.73 2003/07/06 15:31:21 daniel
  1416. * Fixed register allocator. *Lots* of fixes.
  1417. Revision 1.72 2003/06/17 16:51:30 peter
  1418. * cycle fixes
  1419. Revision 1.71 2003/06/07 18:57:04 jonas
  1420. + added freeintparaloc
  1421. * ppc get/freeintparaloc now check whether the parameter regs are
  1422. properly allocated/deallocated (and get an extra list para)
  1423. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1424. * fixed lot of missing pi_do_call's
  1425. Revision 1.70 2003/06/03 13:01:59 daniel
  1426. * Register allocator finished
  1427. Revision 1.69 2003/05/30 23:49:18 jonas
  1428. * a_load_loc_reg now has an extra size parameter for the destination
  1429. register (properly fixes what I worked around in revision 1.106 of
  1430. ncgutil.pas)
  1431. Revision 1.68 2003/05/26 19:38:28 peter
  1432. * generic fpc_shorstr_concat
  1433. + fpc_shortstr_append_shortstr optimization
  1434. Revision 1.67 2003/05/22 21:32:29 peter
  1435. * removed some unit dependencies
  1436. Revision 1.66 2003/04/26 09:12:55 peter
  1437. * add string returns in LOC_REFERENCE
  1438. Revision 1.65 2003/04/23 20:16:04 peter
  1439. + added currency support based on int64
  1440. + is_64bit for use in cg units instead of is_64bitint
  1441. * removed cgmessage from n386add, replace with internalerrors
  1442. Revision 1.64 2003/04/23 09:51:16 daniel
  1443. * Removed usage of edi in a lot of places when new register allocator used
  1444. + Added newra versions of g_concatcopy and secondadd_float
  1445. Revision 1.63 2003/04/22 23:50:23 peter
  1446. * firstpass uses expectloc
  1447. * checks if there are differences between the expectloc and
  1448. location.loc from secondpass in EXTDEBUG
  1449. Revision 1.62 2003/04/22 10:09:35 daniel
  1450. + Implemented the actual register allocator
  1451. + Scratch registers unavailable when new register allocator used
  1452. + maybe_save/maybe_restore unavailable when new register allocator used
  1453. Revision 1.61 2003/04/17 10:02:48 daniel
  1454. * Tweaked register allocate/deallocate positition to less interferences
  1455. are generated.
  1456. Revision 1.60 2003/03/28 19:16:57 peter
  1457. * generic constructor working for i386
  1458. * remove fixed self register
  1459. * esi added as address register for i386
  1460. Revision 1.59 2003/03/13 19:52:23 jonas
  1461. * and more new register allocator fixes (in the i386 code generator this
  1462. time). At least now the ppc cross compiler can compile the linux
  1463. system unit again, but I haven't tested it.
  1464. Revision 1.58 2003/03/08 20:36:41 daniel
  1465. + Added newra version of Ti386shlshrnode
  1466. + Added interference graph construction code
  1467. Revision 1.57 2003/03/08 13:59:17 daniel
  1468. * Work to handle new register notation in ag386nsm
  1469. + Added newra version of Ti386moddivnode
  1470. Revision 1.56 2003/03/08 10:53:48 daniel
  1471. * Created newra version of secondmul in n386add.pas
  1472. Revision 1.55 2003/02/19 22:00:15 daniel
  1473. * Code generator converted to new register notation
  1474. - Horribily outdated todo.txt removed
  1475. Revision 1.54 2003/01/13 18:37:44 daniel
  1476. * Work on register conversion
  1477. Revision 1.53 2003/01/08 18:43:57 daniel
  1478. * Tregister changed into a record
  1479. Revision 1.52 2002/11/25 17:43:26 peter
  1480. * splitted defbase in defutil,symutil,defcmp
  1481. * merged isconvertable and is_equal into compare_defs(_ext)
  1482. * made operator search faster by walking the list only once
  1483. Revision 1.51 2002/11/15 01:58:56 peter
  1484. * merged changes from 1.0.7 up to 04-11
  1485. - -V option for generating bug report tracing
  1486. - more tracing for option parsing
  1487. - errors for cdecl and high()
  1488. - win32 import stabs
  1489. - win32 records<=8 are returned in eax:edx (turned off by default)
  1490. - heaptrc update
  1491. - more info for temp management in .s file with EXTDEBUG
  1492. Revision 1.50 2002/10/20 13:11:27 jonas
  1493. * re-enabled optimized version of comparisons with the empty string that
  1494. I accidentally disabled in revision 1.26
  1495. Revision 1.49 2002/08/23 16:14:49 peter
  1496. * tempgen cleanup
  1497. * tt_noreuse temp type added that will be used in genentrycode
  1498. Revision 1.48 2002/08/14 18:41:48 jonas
  1499. - remove valuelow/valuehigh fields from tlocation, because they depend
  1500. on the endianess of the host operating system -> difficult to get
  1501. right. Use lo/hi(location.valueqword) instead (remember to use
  1502. valueqword and not value!!)
  1503. Revision 1.47 2002/08/11 14:32:29 peter
  1504. * renamed current_library to objectlibrary
  1505. Revision 1.46 2002/08/11 13:24:16 peter
  1506. * saving of asmsymbols in ppu supported
  1507. * asmsymbollist global is removed and moved into a new class
  1508. tasmlibrarydata that will hold the info of a .a file which
  1509. corresponds with a single module. Added librarydata to tmodule
  1510. to keep the library info stored for the module. In the future the
  1511. objectfiles will also be stored to the tasmlibrarydata class
  1512. * all getlabel/newasmsymbol and friends are moved to the new class
  1513. Revision 1.45 2002/07/26 11:17:52 jonas
  1514. * the optimization of converting a multiplication with a power of two to
  1515. a shl is moved from n386add/secondpass to nadd/resulttypepass
  1516. Revision 1.44 2002/07/20 11:58:00 florian
  1517. * types.pas renamed to defbase.pas because D6 contains a types
  1518. unit so this would conflicts if D6 programms are compiled
  1519. + Willamette/SSE2 instructions to assembler added
  1520. Revision 1.43 2002/07/11 14:41:32 florian
  1521. * start of the new generic parameter handling
  1522. Revision 1.42 2002/07/07 09:52:33 florian
  1523. * powerpc target fixed, very simple units can be compiled
  1524. * some basic stuff for better callparanode handling, far from being finished
  1525. Revision 1.41 2002/07/01 18:46:31 peter
  1526. * internal linker
  1527. * reorganized aasm layer
  1528. Revision 1.40 2002/07/01 16:23:55 peter
  1529. * cg64 patch
  1530. * basics for currency
  1531. * asnode updates for class and interface (not finished)
  1532. Revision 1.39 2002/05/18 13:34:22 peter
  1533. * readded missing revisions
  1534. Revision 1.38 2002/05/16 19:46:51 carl
  1535. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1536. + try to fix temp allocation (still in ifdef)
  1537. + generic constructor calls
  1538. + start of tassembler / tmodulebase class cleanup
  1539. Revision 1.36 2002/05/13 19:54:37 peter
  1540. * removed n386ld and n386util units
  1541. * maybe_save/maybe_restore added instead of the old maybe_push
  1542. Revision 1.35 2002/05/12 16:53:17 peter
  1543. * moved entry and exitcode to ncgutil and cgobj
  1544. * foreach gets extra argument for passing local data to the
  1545. iterator function
  1546. * -CR checks also class typecasts at runtime by changing them
  1547. into as
  1548. * fixed compiler to cycle with the -CR option
  1549. * fixed stabs with elf writer, finally the global variables can
  1550. be watched
  1551. * removed a lot of routines from cga unit and replaced them by
  1552. calls to cgobj
  1553. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1554. u32bit then the other is typecasted also to u32bit without giving
  1555. a rangecheck warning/error.
  1556. * fixed pascal calling method with reversing also the high tree in
  1557. the parast, detected by tcalcst3 test
  1558. Revision 1.34 2002/04/25 20:16:40 peter
  1559. * moved more routines from cga/n386util
  1560. Revision 1.33 2002/04/05 15:09:13 jonas
  1561. * fixed web bug 1915
  1562. Revision 1.32 2002/04/04 19:06:10 peter
  1563. * removed unused units
  1564. * use tlocation.size in cg.a_*loc*() routines
  1565. Revision 1.31 2002/04/02 17:11:35 peter
  1566. * tlocation,treference update
  1567. * LOC_CONSTANT added for better constant handling
  1568. * secondadd splitted in multiple routines
  1569. * location_force_reg added for loading a location to a register
  1570. of a specified size
  1571. * secondassignment parses now first the right and then the left node
  1572. (this is compatible with Kylix). This saves a lot of push/pop especially
  1573. with string operations
  1574. * adapted some routines to use the new cg methods
  1575. Revision 1.29 2002/03/04 19:10:13 peter
  1576. * removed compiler warnings
  1577. }