n386add.pas 59 KB

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