n386add.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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,cgx86,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:=cg.getintregister(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. cg.ungetregister(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:=cg.getintregister(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. cg.ungetregister(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_NO,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:=cg.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:=cg.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. cg.ungetregister(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. cg.ungetregister(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. cg.allocexplicitregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  359. cg.a_call_name(exprasmlist,'FPC_SHORTSTR_COMPARE');
  360. cg.deallocexplicitregisters(exprasmlist,R_INTREGISTER,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. tcgx86(cg).dec_fpu_stack;
  588. end
  589. else
  590. begin
  591. emit_none(op,S_NO);
  592. tcgx86(cg).dec_fpu_stack;
  593. tcgx86(cg).dec_fpu_stack;
  594. end;
  595. { on comparison load flags }
  596. if cmpop then
  597. begin
  598. cg.getexplicitregister(exprasmlist,NR_AX);
  599. emit_reg(A_FNSTSW,S_NO,NR_AX);
  600. emit_none(A_SAHF,S_NO);
  601. cg.ungetregister(exprasmlist,NR_AX);
  602. if nf_swaped in flags then
  603. begin
  604. case nodetype of
  605. equaln : resflags:=F_E;
  606. unequaln : resflags:=F_NE;
  607. ltn : resflags:=F_A;
  608. lten : resflags:=F_AE;
  609. gtn : resflags:=F_B;
  610. gten : resflags:=F_BE;
  611. end;
  612. end
  613. else
  614. begin
  615. case nodetype of
  616. equaln : resflags:=F_E;
  617. unequaln : resflags:=F_NE;
  618. ltn : resflags:=F_B;
  619. lten : resflags:=F_BE;
  620. gtn : resflags:=F_A;
  621. gten : resflags:=F_AE;
  622. end;
  623. end;
  624. location_reset(location,LOC_FLAGS,OS_NO);
  625. location.resflags:=resflags;
  626. end
  627. else
  628. begin
  629. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  630. location.register:=NR_ST;
  631. end;
  632. end;
  633. {*****************************************************************************
  634. AddSmallSet
  635. *****************************************************************************}
  636. procedure ti386addnode.second_addsmallset;
  637. var
  638. opsize : TOpSize;
  639. op : TAsmOp;
  640. cmpop,
  641. pushedfpu,
  642. extra_not,
  643. noswap : boolean;
  644. begin
  645. pass_left_and_right(pushedfpu);
  646. { when a setdef is passed, it has to be a smallset }
  647. if ((left.resulttype.def.deftype=setdef) and
  648. (tsetdef(left.resulttype.def).settype<>smallset)) or
  649. ((right.resulttype.def.deftype=setdef) and
  650. (tsetdef(right.resulttype.def).settype<>smallset)) then
  651. internalerror(200203301);
  652. cmpop:=false;
  653. noswap:=false;
  654. extra_not:=false;
  655. opsize:=S_L;
  656. case nodetype of
  657. addn :
  658. begin
  659. { this is a really ugly hack!!!!!!!!!! }
  660. { this could be done later using EDI }
  661. { as it is done for subn }
  662. { instead of two registers!!!! }
  663. { adding elements is not commutative }
  664. if (nf_swaped in flags) and (left.nodetype=setelementn) then
  665. swapleftright;
  666. { are we adding set elements ? }
  667. if right.nodetype=setelementn then
  668. begin
  669. { no range support for smallsets! }
  670. if assigned(tsetelementnode(right).right) then
  671. internalerror(43244);
  672. { bts requires both elements to be registers }
  673. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  674. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],true);
  675. op:=A_BTS;
  676. noswap:=true;
  677. end
  678. else
  679. op:=A_OR;
  680. end;
  681. symdifn :
  682. op:=A_XOR;
  683. muln :
  684. op:=A_AND;
  685. subn :
  686. begin
  687. op:=A_AND;
  688. if (not(nf_swaped in flags)) and
  689. (right.location.loc=LOC_CONSTANT) then
  690. right.location.value := not(right.location.value)
  691. else if (nf_swaped in flags) and
  692. (left.location.loc=LOC_CONSTANT) then
  693. left.location.value := not(left.location.value)
  694. else
  695. extra_not:=true;
  696. end;
  697. equaln,
  698. unequaln :
  699. begin
  700. op:=A_CMP;
  701. cmpop:=true;
  702. end;
  703. lten,gten:
  704. begin
  705. If (not(nf_swaped in flags) and
  706. (nodetype = lten)) or
  707. ((nf_swaped in flags) and
  708. (nodetype = gten)) then
  709. swapleftright;
  710. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],true);
  711. emit_op_right_left(A_AND,opsize);
  712. op:=A_CMP;
  713. cmpop:=true;
  714. { warning: ugly hack, we need a JE so change the node to equaln }
  715. nodetype:=equaln;
  716. end;
  717. xorn :
  718. op:=A_XOR;
  719. orn :
  720. op:=A_OR;
  721. andn :
  722. op:=A_AND;
  723. else
  724. internalerror(2003042215);
  725. end;
  726. { left must be a register }
  727. left_must_be_reg(opsize,noswap);
  728. emit_generic_code(op,opsize,true,extra_not,false);
  729. location_freetemp(exprasmlist,right.location);
  730. location_release(exprasmlist,right.location);
  731. if cmpop then
  732. begin
  733. location_freetemp(exprasmlist,left.location);
  734. location_release(exprasmlist,left.location);
  735. end;
  736. set_result_location(cmpop,true);
  737. end;
  738. {*****************************************************************************
  739. Add64bit
  740. *****************************************************************************}
  741. procedure ti386addnode.second_add64bit;
  742. var
  743. op : TOpCG;
  744. op1,op2 : TAsmOp;
  745. opsize : TOpSize;
  746. hregister,
  747. hregister2 : tregister;
  748. href : treference;
  749. hl4 : tasmlabel;
  750. pushedfpu,
  751. mboverflow,
  752. cmpop,
  753. unsigned,delete:boolean;
  754. r:Tregister;
  755. procedure firstjmp64bitcmp;
  756. var
  757. oldnodetype : tnodetype;
  758. begin
  759. load_all_regvars(exprasmlist);
  760. { the jump the sequence is a little bit hairy }
  761. case nodetype of
  762. ltn,gtn:
  763. begin
  764. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),truelabel);
  765. { cheat a little bit for the negative test }
  766. toggleflag(nf_swaped);
  767. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),falselabel);
  768. toggleflag(nf_swaped);
  769. end;
  770. lten,gten:
  771. begin
  772. oldnodetype:=nodetype;
  773. if nodetype=lten then
  774. nodetype:=ltn
  775. else
  776. nodetype:=gtn;
  777. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),truelabel);
  778. { cheat for the negative test }
  779. if nodetype=ltn then
  780. nodetype:=gtn
  781. else
  782. nodetype:=ltn;
  783. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),falselabel);
  784. nodetype:=oldnodetype;
  785. end;
  786. equaln:
  787. cg.a_jmp_flags(exprasmlist,F_NE,falselabel);
  788. unequaln:
  789. cg.a_jmp_flags(exprasmlist,F_NE,truelabel);
  790. end;
  791. end;
  792. procedure secondjmp64bitcmp;
  793. begin
  794. { the jump the sequence is a little bit hairy }
  795. case nodetype of
  796. ltn,gtn,lten,gten:
  797. begin
  798. { the comparisaion of the low dword have to be }
  799. { always unsigned! }
  800. cg.a_jmp_flags(exprasmlist,getresflags(true),truelabel);
  801. cg.a_jmp_always(exprasmlist,falselabel);
  802. end;
  803. equaln:
  804. begin
  805. cg.a_jmp_flags(exprasmlist,F_NE,falselabel);
  806. cg.a_jmp_always(exprasmlist,truelabel);
  807. end;
  808. unequaln:
  809. begin
  810. cg.a_jmp_flags(exprasmlist,F_NE,truelabel);
  811. cg.a_jmp_always(exprasmlist,falselabel);
  812. end;
  813. end;
  814. end;
  815. begin
  816. firstcomplex(self);
  817. pass_left_and_right(pushedfpu);
  818. op1:=A_NONE;
  819. op2:=A_NONE;
  820. mboverflow:=false;
  821. cmpop:=false;
  822. opsize:=S_L;
  823. unsigned:=((left.resulttype.def.deftype=orddef) and
  824. (torddef(left.resulttype.def).typ=u64bit)) or
  825. ((right.resulttype.def.deftype=orddef) and
  826. (torddef(right.resulttype.def).typ=u64bit));
  827. case nodetype of
  828. addn :
  829. begin
  830. op:=OP_ADD;
  831. mboverflow:=true;
  832. end;
  833. subn :
  834. begin
  835. op:=OP_SUB;
  836. op1:=A_SUB;
  837. op2:=A_SBB;
  838. mboverflow:=true;
  839. end;
  840. ltn,lten,
  841. gtn,gten,
  842. equaln,unequaln:
  843. begin
  844. op:=OP_NONE;
  845. cmpop:=true;
  846. end;
  847. xorn:
  848. op:=OP_XOR;
  849. orn:
  850. op:=OP_OR;
  851. andn:
  852. op:=OP_AND;
  853. else
  854. begin
  855. { everything should be handled in pass_1 (JM) }
  856. internalerror(200109051);
  857. end;
  858. end;
  859. { left and right no register? }
  860. { then one must be demanded }
  861. if (left.location.loc<>LOC_REGISTER) then
  862. begin
  863. if (right.location.loc<>LOC_REGISTER) then
  864. begin
  865. { we can reuse a CREGISTER for comparison }
  866. if not((left.location.loc=LOC_CREGISTER) and cmpop) then
  867. begin
  868. delete:=left.location.loc<>LOC_CREGISTER;
  869. hregister:=cg.getintregister(exprasmlist,OS_INT);
  870. hregister2:=cg.getintregister(exprasmlist,OS_INT);
  871. cg64.a_load64_loc_reg(exprasmlist,left.location,joinreg64(hregister,hregister2),delete);
  872. location_reset(left.location,LOC_REGISTER,OS_64);
  873. left.location.registerlow:=hregister;
  874. left.location.registerhigh:=hregister2;
  875. end;
  876. end
  877. else
  878. begin
  879. location_swap(left.location,right.location);
  880. toggleflag(nf_swaped);
  881. end;
  882. end;
  883. { at this point, left.location.loc should be LOC_REGISTER }
  884. if right.location.loc=LOC_REGISTER then
  885. begin
  886. { when swapped another result register }
  887. if (nodetype=subn) and (nf_swaped in flags) then
  888. begin
  889. cg64.a_op64_reg_reg(exprasmlist,op,
  890. left.location.register64,
  891. right.location.register64);
  892. location_swap(left.location,right.location);
  893. toggleflag(nf_swaped);
  894. end
  895. else if cmpop then
  896. begin
  897. emit_reg_reg(A_CMP,S_L,right.location.registerhigh,left.location.registerhigh);
  898. firstjmp64bitcmp;
  899. emit_reg_reg(A_CMP,S_L,right.location.registerlow,left.location.registerlow);
  900. secondjmp64bitcmp;
  901. end
  902. else
  903. begin
  904. cg64.a_op64_reg_reg(exprasmlist,op,
  905. right.location.register64,
  906. left.location.register64);
  907. end;
  908. location_release(exprasmlist,right.location);
  909. end
  910. else
  911. begin
  912. { right.location<>LOC_REGISTER }
  913. if (nodetype=subn) and (nf_swaped in flags) then
  914. begin
  915. r:=cg.getintregister(exprasmlist,OS_INT);
  916. cg64.a_load64low_loc_reg(exprasmlist,right.location,r);
  917. emit_reg_reg(op1,opsize,left.location.registerlow,r);
  918. emit_reg_reg(A_MOV,opsize,r,left.location.registerlow);
  919. cg64.a_load64high_loc_reg(exprasmlist,right.location,r);
  920. { the carry flag is still ok }
  921. emit_reg_reg(op2,opsize,left.location.registerhigh,r);
  922. emit_reg_reg(A_MOV,opsize,r,left.location.registerhigh);
  923. cg.ungetregister(exprasmlist,r);
  924. if right.location.loc<>LOC_CREGISTER then
  925. begin
  926. location_freetemp(exprasmlist,right.location);
  927. location_release(exprasmlist,right.location);
  928. end;
  929. end
  930. else if cmpop then
  931. begin
  932. case right.location.loc of
  933. LOC_CREGISTER :
  934. begin
  935. emit_reg_reg(A_CMP,S_L,right.location.registerhigh,left.location.registerhigh);
  936. firstjmp64bitcmp;
  937. emit_reg_reg(A_CMP,S_L,right.location.registerlow,left.location.registerlow);
  938. secondjmp64bitcmp;
  939. end;
  940. LOC_CREFERENCE,
  941. LOC_REFERENCE :
  942. begin
  943. href:=right.location.reference;
  944. inc(href.offset,4);
  945. emit_ref_reg(A_CMP,S_L,href,left.location.registerhigh);
  946. firstjmp64bitcmp;
  947. emit_ref_reg(A_CMP,S_L,right.location.reference,left.location.registerlow);
  948. secondjmp64bitcmp;
  949. cg.a_jmp_always(exprasmlist,falselabel);
  950. location_freetemp(exprasmlist,right.location);
  951. location_release(exprasmlist,right.location);
  952. end;
  953. LOC_CONSTANT :
  954. begin
  955. exprasmlist.concat(taicpu.op_const_reg(A_CMP,S_L,hi(right.location.valueqword),left.location.registerhigh));
  956. firstjmp64bitcmp;
  957. exprasmlist.concat(taicpu.op_const_reg(A_CMP,S_L,lo(right.location.valueqword),left.location.registerlow));
  958. secondjmp64bitcmp;
  959. end;
  960. else
  961. internalerror(200203282);
  962. end;
  963. end
  964. else
  965. begin
  966. cg64.a_op64_loc_reg(exprasmlist,op,right.location,
  967. left.location.register64);
  968. if (right.location.loc<>LOC_CREGISTER) then
  969. begin
  970. location_freetemp(exprasmlist,right.location);
  971. location_release(exprasmlist,right.location);
  972. end;
  973. end;
  974. end;
  975. if (left.location.loc<>LOC_CREGISTER) and cmpop then
  976. begin
  977. location_freetemp(exprasmlist,left.location);
  978. location_release(exprasmlist,left.location);
  979. end;
  980. { only in case of overflow operations }
  981. { produce overflow code }
  982. { we must put it here directly, because sign of operation }
  983. { is in unsigned VAR!! }
  984. if mboverflow then
  985. begin
  986. if cs_check_overflow in aktlocalswitches then
  987. begin
  988. objectlibrary.getlabel(hl4);
  989. if unsigned then
  990. cg.a_jmp_flags(exprasmlist,F_AE,hl4)
  991. else
  992. cg.a_jmp_flags(exprasmlist,F_NO,hl4);
  993. cg.a_call_name(exprasmlist,'FPC_OVERFLOW');
  994. cg.a_label(exprasmlist,hl4);
  995. end;
  996. end;
  997. { we have LOC_JUMP as result }
  998. if cmpop then
  999. location_reset(location,LOC_JUMP,OS_NO)
  1000. else
  1001. location_copy(location,left.location);
  1002. end;
  1003. {*****************************************************************************
  1004. AddMMX
  1005. *****************************************************************************}
  1006. {$ifdef SUPPORT_MMX}
  1007. procedure ti386addnode.second_addmmx;
  1008. var
  1009. op : TAsmOp;
  1010. pushedfpu,
  1011. cmpop : boolean;
  1012. mmxbase : tmmxtype;
  1013. r,hregister : tregister;
  1014. begin
  1015. (*
  1016. pass_left_and_right(pushedfpu);
  1017. cmpop:=false;
  1018. mmxbase:=mmx_type(left.resulttype.def);
  1019. case nodetype of
  1020. addn :
  1021. begin
  1022. if (cs_mmx_saturation in aktlocalswitches) then
  1023. begin
  1024. case mmxbase of
  1025. mmxs8bit:
  1026. op:=A_PADDSB;
  1027. mmxu8bit:
  1028. op:=A_PADDUSB;
  1029. mmxs16bit,mmxfixed16:
  1030. op:=A_PADDSB;
  1031. mmxu16bit:
  1032. op:=A_PADDUSW;
  1033. end;
  1034. end
  1035. else
  1036. begin
  1037. case mmxbase of
  1038. mmxs8bit,mmxu8bit:
  1039. op:=A_PADDB;
  1040. mmxs16bit,mmxu16bit,mmxfixed16:
  1041. op:=A_PADDW;
  1042. mmxs32bit,mmxu32bit:
  1043. op:=A_PADDD;
  1044. end;
  1045. end;
  1046. end;
  1047. muln :
  1048. begin
  1049. case mmxbase of
  1050. mmxs16bit,mmxu16bit:
  1051. op:=A_PMULLW;
  1052. mmxfixed16:
  1053. op:=A_PMULHW;
  1054. end;
  1055. end;
  1056. subn :
  1057. begin
  1058. if (cs_mmx_saturation in aktlocalswitches) then
  1059. begin
  1060. case mmxbase of
  1061. mmxs8bit:
  1062. op:=A_PSUBSB;
  1063. mmxu8bit:
  1064. op:=A_PSUBUSB;
  1065. mmxs16bit,mmxfixed16:
  1066. op:=A_PSUBSB;
  1067. mmxu16bit:
  1068. op:=A_PSUBUSW;
  1069. end;
  1070. end
  1071. else
  1072. begin
  1073. case mmxbase of
  1074. mmxs8bit,mmxu8bit:
  1075. op:=A_PSUBB;
  1076. mmxs16bit,mmxu16bit,mmxfixed16:
  1077. op:=A_PSUBW;
  1078. mmxs32bit,mmxu32bit:
  1079. op:=A_PSUBD;
  1080. end;
  1081. end;
  1082. end;
  1083. xorn:
  1084. op:=A_PXOR;
  1085. orn:
  1086. op:=A_POR;
  1087. andn:
  1088. op:=A_PAND;
  1089. else
  1090. internalerror(2003042214);
  1091. end;
  1092. { left and right no register? }
  1093. { then one must be demanded }
  1094. if (left.location.loc<>LOC_MMXREGISTER) then
  1095. begin
  1096. if (right.location.loc=LOC_MMXREGISTER) then
  1097. begin
  1098. location_swap(left.location,right.location);
  1099. toggleflag(nf_swaped);
  1100. end
  1101. else
  1102. begin
  1103. { register variable ? }
  1104. if (left.location.loc=LOC_CMMXREGISTER) then
  1105. begin
  1106. hregister:=rg.getregistermm(exprasmlist);
  1107. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  1108. end
  1109. else
  1110. begin
  1111. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1112. internalerror(200203245);
  1113. location_release(exprasmlist,left.location);
  1114. hregister:=rg.getregistermm(exprasmlist);
  1115. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  1116. end;
  1117. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  1118. left.location.register:=hregister;
  1119. end;
  1120. end;
  1121. { at this point, left.location.loc should be LOC_MMXREGISTER }
  1122. if right.location.loc<>LOC_MMXREGISTER then
  1123. begin
  1124. if (nodetype=subn) and (nf_swaped in flags) then
  1125. begin
  1126. if right.location.loc=LOC_CMMXREGISTER then
  1127. begin
  1128. emit_reg_reg(A_MOVQ,S_NO,right.location.register,NR_MM7);
  1129. emit_reg_reg(op,S_NO,left.location.register,NR_MM7);
  1130. emit_reg_reg(A_MOVQ,S_NO,NR_MM7,left.location.register);
  1131. end
  1132. else
  1133. begin
  1134. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1135. internalerror(200203247);
  1136. emit_ref_reg(A_MOVQ,S_NO,right.location.reference,NR_MM7);
  1137. emit_reg_reg(op,S_NO,left.location.register,NR_MM7);
  1138. emit_reg_reg(A_MOVQ,S_NO,NR_MM7,left.location.register);
  1139. location_release(exprasmlist,right.location);
  1140. end;
  1141. end
  1142. else
  1143. begin
  1144. if (right.location.loc=LOC_CMMXREGISTER) then
  1145. emit_reg_reg(op,S_NO,right.location.register,left.location.register)
  1146. else
  1147. begin
  1148. if not(right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1149. internalerror(200203246);
  1150. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  1151. location_release(exprasmlist,right.location);
  1152. end;
  1153. end;
  1154. end
  1155. else
  1156. begin
  1157. { right.location=LOC_MMXREGISTER }
  1158. if (nodetype=subn) and (nf_swaped in flags) then
  1159. begin
  1160. emit_reg_reg(op,S_NO,left.location.register,right.location.register);
  1161. location_swap(left.location,right.location);
  1162. toggleflag(nf_swaped);
  1163. end
  1164. else
  1165. begin
  1166. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  1167. end;
  1168. end;
  1169. location_freetemp(exprasmlist,right.location);
  1170. location_release(exprasmlist,right.location);
  1171. if cmpop then
  1172. begin
  1173. location_freetemp(exprasmlist,left.location);
  1174. location_release(exprasmlist,left.location);
  1175. end;
  1176. set_result_location(cmpop,true);
  1177. *)
  1178. end;
  1179. {$endif SUPPORT_MMX}
  1180. {*****************************************************************************
  1181. MUL
  1182. *****************************************************************************}
  1183. procedure ti386addnode.second_mul;
  1184. var r:Tregister;
  1185. begin
  1186. {The location.register will be filled in later (JM)}
  1187. location_reset(location,LOC_REGISTER,OS_INT);
  1188. {Get a temp register and load the left value into it
  1189. and free the location.}
  1190. r:=cg.getintregister(exprasmlist,OS_INT);
  1191. cg.a_load_loc_reg(exprasmlist,OS_INT,left.location,r);
  1192. location_release(exprasmlist,left.location);
  1193. {Allocate EAX.}
  1194. cg.getexplicitregister(exprasmlist,NR_EAX);
  1195. {Load the right value.}
  1196. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,NR_EAX);
  1197. location_release(exprasmlist,right.location);
  1198. {The mul instruction frees register r.}
  1199. cg.ungetregister(exprasmlist,r);
  1200. {Also allocate EDX, since it is also modified by a mul (JM).}
  1201. cg.getexplicitregister(exprasmlist,NR_EDX);
  1202. emit_reg(A_MUL,S_L,r);
  1203. {Free EDX}
  1204. cg.ungetregister(exprasmlist,NR_EDX);
  1205. {Free EAX}
  1206. cg.ungetregister(exprasmlist,NR_EAX);
  1207. {Allocate a new register and store the result in EAX in it.}
  1208. location.register:=cg.getintregister(exprasmlist,OS_INT);
  1209. emit_reg_reg(A_MOV,S_L,NR_EAX,location.register);
  1210. location_freetemp(exprasmlist,left.location);
  1211. location_freetemp(exprasmlist,right.location);
  1212. end;
  1213. {*****************************************************************************
  1214. pass_2
  1215. *****************************************************************************}
  1216. procedure ti386addnode.pass_2;
  1217. { is also being used for xor, and "mul", "sub, or and comparative }
  1218. { operators }
  1219. var
  1220. pushedfpu,
  1221. mboverflow,cmpop : boolean;
  1222. op : tasmop;
  1223. opsize : topsize;
  1224. { true, if unsigned types are compared }
  1225. unsigned : boolean;
  1226. { is_in_dest if the result is put directly into }
  1227. { the resulting refernce or varregister }
  1228. {is_in_dest : boolean;}
  1229. { true, if for sets subtractions the extra not should generated }
  1230. extra_not : boolean;
  1231. begin
  1232. { to make it more readable, string and set (not smallset!) have their
  1233. own procedures }
  1234. case left.resulttype.def.deftype of
  1235. orddef :
  1236. begin
  1237. { handling boolean expressions }
  1238. if is_boolean(left.resulttype.def) and
  1239. is_boolean(right.resulttype.def) then
  1240. begin
  1241. second_addboolean;
  1242. exit;
  1243. end
  1244. { 64bit operations }
  1245. else if is_64bit(left.resulttype.def) then
  1246. begin
  1247. second_add64bit;
  1248. exit;
  1249. end;
  1250. end;
  1251. stringdef :
  1252. begin
  1253. second_addstring;
  1254. exit;
  1255. end;
  1256. setdef :
  1257. begin
  1258. { normalsets are already handled in pass1 }
  1259. if (tsetdef(left.resulttype.def).settype<>smallset) then
  1260. internalerror(200109041);
  1261. second_addsmallset;
  1262. exit;
  1263. end;
  1264. arraydef :
  1265. begin
  1266. {$ifdef SUPPORT_MMX}
  1267. if is_mmx_able_array(left.resulttype.def) then
  1268. begin
  1269. second_addmmx;
  1270. exit;
  1271. end;
  1272. {$endif SUPPORT_MMX}
  1273. end;
  1274. floatdef :
  1275. begin
  1276. second_addfloat;
  1277. exit;
  1278. end;
  1279. end;
  1280. { defaults }
  1281. {is_in_dest:=false;}
  1282. extra_not:=false;
  1283. mboverflow:=false;
  1284. cmpop:=false;
  1285. unsigned:=not(is_signed(left.resulttype.def)) or
  1286. not(is_signed(right.resulttype.def));
  1287. opsize:=def_opsize(left.resulttype.def);
  1288. pass_left_and_right(pushedfpu);
  1289. if (left.resulttype.def.deftype=pointerdef) or
  1290. (right.resulttype.def.deftype=pointerdef) or
  1291. (is_class_or_interface(right.resulttype.def) and is_class_or_interface(left.resulttype.def)) or
  1292. (left.resulttype.def.deftype=classrefdef) or
  1293. (left.resulttype.def.deftype=procvardef) or
  1294. ((left.resulttype.def.deftype=enumdef) and
  1295. (left.resulttype.def.size=4)) or
  1296. ((left.resulttype.def.deftype=orddef) and
  1297. (torddef(left.resulttype.def).typ in [s32bit,u32bit])) or
  1298. ((right.resulttype.def.deftype=orddef) and
  1299. (torddef(right.resulttype.def).typ in [s32bit,u32bit])) then
  1300. begin
  1301. case nodetype of
  1302. addn :
  1303. begin
  1304. op:=A_ADD;
  1305. mboverflow:=true;
  1306. end;
  1307. muln :
  1308. begin
  1309. if unsigned then
  1310. op:=A_MUL
  1311. else
  1312. op:=A_IMUL;
  1313. mboverflow:=true;
  1314. end;
  1315. subn :
  1316. begin
  1317. op:=A_SUB;
  1318. mboverflow:=true;
  1319. end;
  1320. ltn,lten,
  1321. gtn,gten,
  1322. equaln,unequaln :
  1323. begin
  1324. op:=A_CMP;
  1325. cmpop:=true;
  1326. end;
  1327. xorn :
  1328. op:=A_XOR;
  1329. orn :
  1330. op:=A_OR;
  1331. andn :
  1332. op:=A_AND;
  1333. else
  1334. internalerror(200304229);
  1335. end;
  1336. { filter MUL, which requires special handling }
  1337. if op=A_MUL then
  1338. begin
  1339. second_mul;
  1340. exit;
  1341. end;
  1342. { Convert flags to register first }
  1343. if (left.location.loc=LOC_FLAGS) then
  1344. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  1345. if (right.location.loc=LOC_FLAGS) then
  1346. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],false);
  1347. left_must_be_reg(opsize,false);
  1348. emit_generic_code(op,opsize,unsigned,extra_not,mboverflow);
  1349. location_freetemp(exprasmlist,right.location);
  1350. location_release(exprasmlist,right.location);
  1351. if cmpop and
  1352. (left.location.loc<>LOC_CREGISTER) then
  1353. begin
  1354. location_freetemp(exprasmlist,left.location);
  1355. location_release(exprasmlist,left.location);
  1356. end;
  1357. set_result_location(cmpop,unsigned);
  1358. end
  1359. { 8/16 bit enum,char,wchar types }
  1360. else
  1361. if ((left.resulttype.def.deftype=orddef) and
  1362. (torddef(left.resulttype.def).typ in [uchar,uwidechar])) or
  1363. ((left.resulttype.def.deftype=enumdef) and
  1364. ((left.resulttype.def.size=1) or
  1365. (left.resulttype.def.size=2))) then
  1366. begin
  1367. case nodetype of
  1368. ltn,lten,gtn,gten,
  1369. equaln,unequaln :
  1370. cmpop:=true;
  1371. else
  1372. internalerror(2003042210);
  1373. end;
  1374. left_must_be_reg(opsize,false);
  1375. emit_op_right_left(A_CMP,opsize);
  1376. location_freetemp(exprasmlist,right.location);
  1377. location_release(exprasmlist,right.location);
  1378. if left.location.loc<>LOC_CREGISTER then
  1379. begin
  1380. location_freetemp(exprasmlist,left.location);
  1381. location_release(exprasmlist,left.location);
  1382. end;
  1383. set_result_location(true,true);
  1384. end
  1385. else
  1386. internalerror(2003042211);
  1387. end;
  1388. begin
  1389. caddnode:=ti386addnode;
  1390. end.
  1391. {
  1392. $Log$
  1393. Revision 1.82 2003-10-09 21:31:37 daniel
  1394. * Register allocator splitted, ans abstract now
  1395. Revision 1.81 2003/10/08 09:13:16 florian
  1396. * fixed full bool evalution and bool xor, if the left or right side have LOC_JUMP
  1397. Revision 1.80 2003/10/01 20:34:49 peter
  1398. * procinfo unit contains tprocinfo
  1399. * cginfo renamed to cgbase
  1400. * moved cgmessage to verbose
  1401. * fixed ppc and sparc compiles
  1402. Revision 1.79 2003/09/28 21:48:20 peter
  1403. * fix register leaks
  1404. Revision 1.78 2003/09/28 13:35:40 peter
  1405. * shortstr compare updated for different calling conventions
  1406. Revision 1.77 2003/09/10 08:31:48 marco
  1407. * Patch from Peter for paraloc
  1408. Revision 1.76 2003/09/03 15:55:01 peter
  1409. * NEWRA branch merged
  1410. Revision 1.75.2.2 2003/08/31 13:50:16 daniel
  1411. * Remove sorting and use pregenerated indexes
  1412. * Some work on making things compile
  1413. Revision 1.75.2.1 2003/08/29 17:29:00 peter
  1414. * next batch of updates
  1415. Revision 1.75 2003/08/03 20:38:00 daniel
  1416. * Made code generator reverse or/add/and/xor/imul instructions when
  1417. possible to reduce the slowdown of spills.
  1418. Revision 1.74 2003/08/03 20:19:43 daniel
  1419. - Removed cmpop from Ti386addnode.second_addstring
  1420. Revision 1.73 2003/07/06 15:31:21 daniel
  1421. * Fixed register allocator. *Lots* of fixes.
  1422. Revision 1.72 2003/06/17 16:51:30 peter
  1423. * cycle fixes
  1424. Revision 1.71 2003/06/07 18:57:04 jonas
  1425. + added freeintparaloc
  1426. * ppc get/freeintparaloc now check whether the parameter regs are
  1427. properly allocated/deallocated (and get an extra list para)
  1428. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1429. * fixed lot of missing pi_do_call's
  1430. Revision 1.70 2003/06/03 13:01:59 daniel
  1431. * Register allocator finished
  1432. Revision 1.69 2003/05/30 23:49:18 jonas
  1433. * a_load_loc_reg now has an extra size parameter for the destination
  1434. register (properly fixes what I worked around in revision 1.106 of
  1435. ncgutil.pas)
  1436. Revision 1.68 2003/05/26 19:38:28 peter
  1437. * generic fpc_shorstr_concat
  1438. + fpc_shortstr_append_shortstr optimization
  1439. Revision 1.67 2003/05/22 21:32:29 peter
  1440. * removed some unit dependencies
  1441. Revision 1.66 2003/04/26 09:12:55 peter
  1442. * add string returns in LOC_REFERENCE
  1443. Revision 1.65 2003/04/23 20:16:04 peter
  1444. + added currency support based on int64
  1445. + is_64bit for use in cg units instead of is_64bitint
  1446. * removed cgmessage from n386add, replace with internalerrors
  1447. Revision 1.64 2003/04/23 09:51:16 daniel
  1448. * Removed usage of edi in a lot of places when new register allocator used
  1449. + Added newra versions of g_concatcopy and secondadd_float
  1450. Revision 1.63 2003/04/22 23:50:23 peter
  1451. * firstpass uses expectloc
  1452. * checks if there are differences between the expectloc and
  1453. location.loc from secondpass in EXTDEBUG
  1454. Revision 1.62 2003/04/22 10:09:35 daniel
  1455. + Implemented the actual register allocator
  1456. + Scratch registers unavailable when new register allocator used
  1457. + maybe_save/maybe_restore unavailable when new register allocator used
  1458. Revision 1.61 2003/04/17 10:02:48 daniel
  1459. * Tweaked register allocate/deallocate positition to less interferences
  1460. are generated.
  1461. Revision 1.60 2003/03/28 19:16:57 peter
  1462. * generic constructor working for i386
  1463. * remove fixed self register
  1464. * esi added as address register for i386
  1465. Revision 1.59 2003/03/13 19:52:23 jonas
  1466. * and more new register allocator fixes (in the i386 code generator this
  1467. time). At least now the ppc cross compiler can compile the linux
  1468. system unit again, but I haven't tested it.
  1469. Revision 1.58 2003/03/08 20:36:41 daniel
  1470. + Added newra version of Ti386shlshrnode
  1471. + Added interference graph construction code
  1472. Revision 1.57 2003/03/08 13:59:17 daniel
  1473. * Work to handle new register notation in ag386nsm
  1474. + Added newra version of Ti386moddivnode
  1475. Revision 1.56 2003/03/08 10:53:48 daniel
  1476. * Created newra version of secondmul in n386add.pas
  1477. Revision 1.55 2003/02/19 22:00:15 daniel
  1478. * Code generator converted to new register notation
  1479. - Horribily outdated todo.txt removed
  1480. Revision 1.54 2003/01/13 18:37:44 daniel
  1481. * Work on register conversion
  1482. Revision 1.53 2003/01/08 18:43:57 daniel
  1483. * Tregister changed into a record
  1484. Revision 1.52 2002/11/25 17:43:26 peter
  1485. * splitted defbase in defutil,symutil,defcmp
  1486. * merged isconvertable and is_equal into compare_defs(_ext)
  1487. * made operator search faster by walking the list only once
  1488. Revision 1.51 2002/11/15 01:58:56 peter
  1489. * merged changes from 1.0.7 up to 04-11
  1490. - -V option for generating bug report tracing
  1491. - more tracing for option parsing
  1492. - errors for cdecl and high()
  1493. - win32 import stabs
  1494. - win32 records<=8 are returned in eax:edx (turned off by default)
  1495. - heaptrc update
  1496. - more info for temp management in .s file with EXTDEBUG
  1497. Revision 1.50 2002/10/20 13:11:27 jonas
  1498. * re-enabled optimized version of comparisons with the empty string that
  1499. I accidentally disabled in revision 1.26
  1500. Revision 1.49 2002/08/23 16:14:49 peter
  1501. * tempgen cleanup
  1502. * tt_noreuse temp type added that will be used in genentrycode
  1503. Revision 1.48 2002/08/14 18:41:48 jonas
  1504. - remove valuelow/valuehigh fields from tlocation, because they depend
  1505. on the endianess of the host operating system -> difficult to get
  1506. right. Use lo/hi(location.valueqword) instead (remember to use
  1507. valueqword and not value!!)
  1508. Revision 1.47 2002/08/11 14:32:29 peter
  1509. * renamed current_library to objectlibrary
  1510. Revision 1.46 2002/08/11 13:24:16 peter
  1511. * saving of asmsymbols in ppu supported
  1512. * asmsymbollist global is removed and moved into a new class
  1513. tasmlibrarydata that will hold the info of a .a file which
  1514. corresponds with a single module. Added librarydata to tmodule
  1515. to keep the library info stored for the module. In the future the
  1516. objectfiles will also be stored to the tasmlibrarydata class
  1517. * all getlabel/newasmsymbol and friends are moved to the new class
  1518. Revision 1.45 2002/07/26 11:17:52 jonas
  1519. * the optimization of converting a multiplication with a power of two to
  1520. a shl is moved from n386add/secondpass to nadd/resulttypepass
  1521. Revision 1.44 2002/07/20 11:58:00 florian
  1522. * types.pas renamed to defbase.pas because D6 contains a types
  1523. unit so this would conflicts if D6 programms are compiled
  1524. + Willamette/SSE2 instructions to assembler added
  1525. Revision 1.43 2002/07/11 14:41:32 florian
  1526. * start of the new generic parameter handling
  1527. Revision 1.42 2002/07/07 09:52:33 florian
  1528. * powerpc target fixed, very simple units can be compiled
  1529. * some basic stuff for better callparanode handling, far from being finished
  1530. Revision 1.41 2002/07/01 18:46:31 peter
  1531. * internal linker
  1532. * reorganized aasm layer
  1533. Revision 1.40 2002/07/01 16:23:55 peter
  1534. * cg64 patch
  1535. * basics for currency
  1536. * asnode updates for class and interface (not finished)
  1537. Revision 1.39 2002/05/18 13:34:22 peter
  1538. * readded missing revisions
  1539. Revision 1.38 2002/05/16 19:46:51 carl
  1540. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1541. + try to fix temp allocation (still in ifdef)
  1542. + generic constructor calls
  1543. + start of tassembler / tmodulebase class cleanup
  1544. Revision 1.36 2002/05/13 19:54:37 peter
  1545. * removed n386ld and n386util units
  1546. * maybe_save/maybe_restore added instead of the old maybe_push
  1547. Revision 1.35 2002/05/12 16:53:17 peter
  1548. * moved entry and exitcode to ncgutil and cgobj
  1549. * foreach gets extra argument for passing local data to the
  1550. iterator function
  1551. * -CR checks also class typecasts at runtime by changing them
  1552. into as
  1553. * fixed compiler to cycle with the -CR option
  1554. * fixed stabs with elf writer, finally the global variables can
  1555. be watched
  1556. * removed a lot of routines from cga unit and replaced them by
  1557. calls to cgobj
  1558. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1559. u32bit then the other is typecasted also to u32bit without giving
  1560. a rangecheck warning/error.
  1561. * fixed pascal calling method with reversing also the high tree in
  1562. the parast, detected by tcalcst3 test
  1563. Revision 1.34 2002/04/25 20:16:40 peter
  1564. * moved more routines from cga/n386util
  1565. Revision 1.33 2002/04/05 15:09:13 jonas
  1566. * fixed web bug 1915
  1567. Revision 1.32 2002/04/04 19:06:10 peter
  1568. * removed unused units
  1569. * use tlocation.size in cg.a_*loc*() routines
  1570. Revision 1.31 2002/04/02 17:11:35 peter
  1571. * tlocation,treference update
  1572. * LOC_CONSTANT added for better constant handling
  1573. * secondadd splitted in multiple routines
  1574. * location_force_reg added for loading a location to a register
  1575. of a specified size
  1576. * secondassignment parses now first the right and then the left node
  1577. (this is compatible with Kylix). This saves a lot of push/pop especially
  1578. with string operations
  1579. * adapted some routines to use the new cg methods
  1580. Revision 1.29 2002/03/04 19:10:13 peter
  1581. * removed compiler warnings
  1582. }