n386add.pas 59 KB

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