n386add.pas 59 KB

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