n386add.pas 64 KB

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