cg386add.pas 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate i386 assembler for in add node
  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 cg386add;
  19. interface
  20. {$define usecreateset}
  21. uses
  22. tree;
  23. procedure secondadd(var p : ptree);
  24. implementation
  25. uses
  26. globtype,systems,
  27. cobjects,verbose,globals,
  28. symconst,symtable,aasm,types,
  29. hcodegen,temp_gen,pass_2,
  30. cpubase,cpuasm,
  31. cgai386,tgeni386;
  32. {*****************************************************************************
  33. Helpers
  34. *****************************************************************************}
  35. procedure locflags2reg(var l:tlocation;opsize:topsize);
  36. var
  37. hregister : tregister;
  38. begin
  39. if (l.loc=LOC_FLAGS) then
  40. begin
  41. case opsize of
  42. S_L : hregister:=getregister32;
  43. S_W : hregister:=reg32toreg16(getregister32);
  44. S_B : hregister:=reg32toreg8(getregister32);
  45. end;
  46. emit_flag2reg(l.resflags,hregister);
  47. l.loc:=LOC_REGISTER;
  48. l.register:=hregister;
  49. end;
  50. end;
  51. function getresflags(p : ptree;unsigned : boolean) : tresflags;
  52. begin
  53. if not(unsigned) then
  54. begin
  55. if p^.swaped then
  56. case p^.treetype of
  57. equaln : getresflags:=F_E;
  58. unequaln : getresflags:=F_NE;
  59. ltn : getresflags:=F_G;
  60. lten : getresflags:=F_GE;
  61. gtn : getresflags:=F_L;
  62. gten : getresflags:=F_LE;
  63. end
  64. else
  65. case p^.treetype of
  66. equaln : getresflags:=F_E;
  67. unequaln : getresflags:=F_NE;
  68. ltn : getresflags:=F_L;
  69. lten : getresflags:=F_LE;
  70. gtn : getresflags:=F_G;
  71. gten : getresflags:=F_GE;
  72. end;
  73. end
  74. else
  75. begin
  76. if p^.swaped then
  77. case p^.treetype of
  78. equaln : getresflags:=F_E;
  79. unequaln : getresflags:=F_NE;
  80. ltn : getresflags:=F_A;
  81. lten : getresflags:=F_AE;
  82. gtn : getresflags:=F_B;
  83. gten : getresflags:=F_BE;
  84. end
  85. else
  86. case p^.treetype of
  87. equaln : getresflags:=F_E;
  88. unequaln : getresflags:=F_NE;
  89. ltn : getresflags:=F_B;
  90. lten : getresflags:=F_BE;
  91. gtn : getresflags:=F_A;
  92. gten : getresflags:=F_AE;
  93. end;
  94. end;
  95. end;
  96. procedure SetResultLocation(cmpop,unsigned:boolean;var p :ptree);
  97. begin
  98. { remove temporary location if not a set or string }
  99. { that's a bad hack (FK) who did this ? }
  100. if (p^.left^.resulttype^.deftype<>stringdef) and
  101. ((p^.left^.resulttype^.deftype<>setdef) or (psetdef(p^.left^.resulttype)^.settype=smallset)) and
  102. (p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  103. ungetiftemp(p^.left^.location.reference);
  104. if (p^.right^.resulttype^.deftype<>stringdef) and
  105. ((p^.right^.resulttype^.deftype<>setdef) or (psetdef(p^.right^.resulttype)^.settype=smallset)) and
  106. (p^.right^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  107. ungetiftemp(p^.right^.location.reference);
  108. { in case of comparison operation the put result in the flags }
  109. if cmpop then
  110. begin
  111. clear_location(p^.location);
  112. p^.location.loc:=LOC_FLAGS;
  113. p^.location.resflags:=getresflags(p,unsigned);
  114. end;
  115. end;
  116. {*****************************************************************************
  117. Addstring
  118. *****************************************************************************}
  119. procedure addstring(var p : ptree);
  120. var
  121. pushedregs : tpushed;
  122. href : treference;
  123. pushed,
  124. cmpop : boolean;
  125. begin
  126. { string operations are not commutative }
  127. if p^.swaped then
  128. swaptree(p);
  129. case pstringdef(p^.left^.resulttype)^.string_typ of
  130. st_ansistring:
  131. begin
  132. case p^.treetype of
  133. addn:
  134. begin
  135. cmpop:=false;
  136. secondpass(p^.left);
  137. { to avoid problem with maybe_push and restore }
  138. set_location(p^.location,p^.left^.location);
  139. pushed:=maybe_push(p^.right^.registers32,p,false);
  140. secondpass(p^.right);
  141. if pushed then
  142. begin
  143. restore(p,false);
  144. set_location(p^.left^.location,p^.location);
  145. end;
  146. { get the temp location, must be done before regs are
  147. released/pushed because after the release the regs are
  148. still used for the push (PFV) }
  149. clear_location(p^.location);
  150. p^.location.loc:=LOC_MEM;
  151. gettempansistringreference(p^.location.reference);
  152. decrstringref(cansistringdef,p^.location.reference);
  153. { release used registers }
  154. del_location(p^.right^.location);
  155. del_location(p^.left^.location);
  156. { push the still used registers }
  157. pushusedregisters(pushedregs,$ff);
  158. { push data }
  159. emitpushreferenceaddr(p^.location.reference);
  160. emit_push_loc(p^.right^.location);
  161. emit_push_loc(p^.left^.location);
  162. emitcall('FPC_ANSISTR_CONCAT');
  163. popusedregisters(pushedregs);
  164. maybe_loadesi;
  165. ungetiftempansi(p^.left^.location.reference);
  166. ungetiftempansi(p^.right^.location.reference);
  167. end;
  168. ltn,lten,gtn,gten,
  169. equaln,unequaln:
  170. begin
  171. cmpop:=true;
  172. secondpass(p^.left);
  173. pushed:=maybe_push(p^.right^.registers32,p^.left,false);
  174. secondpass(p^.right);
  175. if pushed then
  176. restore(p^.left,false);
  177. { release used registers }
  178. del_location(p^.right^.location);
  179. del_location(p^.left^.location);
  180. { push the still used registers }
  181. pushusedregisters(pushedregs,$ff);
  182. { push data }
  183. case p^.right^.location.loc of
  184. LOC_REFERENCE,LOC_MEM:
  185. emit_push_mem(p^.right^.location.reference);
  186. LOC_REGISTER,LOC_CREGISTER:
  187. emit_reg(A_PUSH,S_L,p^.right^.location.register);
  188. end;
  189. case p^.left^.location.loc of
  190. LOC_REFERENCE,LOC_MEM:
  191. emit_push_mem(p^.left^.location.reference);
  192. LOC_REGISTER,LOC_CREGISTER:
  193. emit_reg(A_PUSH,S_L,p^.left^.location.register);
  194. end;
  195. emitcall('FPC_ANSISTR_COMPARE');
  196. emit_reg_reg(A_OR,S_L,R_EAX,R_EAX);
  197. popusedregisters(pushedregs);
  198. maybe_loadesi;
  199. ungetiftempansi(p^.left^.location.reference);
  200. ungetiftempansi(p^.right^.location.reference);
  201. end;
  202. end;
  203. { the result of ansicompare is signed }
  204. SetResultLocation(cmpop,false,p);
  205. end;
  206. st_shortstring:
  207. begin
  208. case p^.treetype of
  209. addn:
  210. begin
  211. cmpop:=false;
  212. secondpass(p^.left);
  213. { if str_concat is set in expr
  214. s:=s+ ... no need to create a temp string (PM) }
  215. if (p^.left^.treetype<>addn) and not (p^.use_strconcat) then
  216. begin
  217. { can only reference be }
  218. { string in register would be funny }
  219. { therefore produce a temporary string }
  220. gettempofsizereference(256,href);
  221. copyshortstring(href,p^.left^.location.reference,255,false);
  222. { release the registers }
  223. del_reference(p^.left^.location.reference);
  224. ungetiftemp(p^.left^.location.reference);
  225. { does not hurt: }
  226. clear_location(p^.left^.location);
  227. p^.left^.location.loc:=LOC_MEM;
  228. p^.left^.location.reference:=href;
  229. end;
  230. secondpass(p^.right);
  231. { on the right we do not need the register anymore too }
  232. {$IfNDef regallocfix}
  233. del_reference(p^.right^.location.reference);
  234. pushusedregisters(pushedregs,$ff);
  235. {$Else regallocfix}
  236. pushusedregisters(pushedregs,$ff
  237. xor ($80 shr byte(p^.right^.location.reference.base))
  238. xor ($80 shr byte(p^.right^.location.reference.index)));
  239. {$EndIf regallocfix}
  240. emitpushreferenceaddr(p^.left^.location.reference);
  241. emitpushreferenceaddr(p^.right^.location.reference);
  242. {$IfDef regallocfix}
  243. del_reference(p^.right^.location.reference);
  244. {$EndIf regallocfix}
  245. emitcall('FPC_SHORTSTR_CONCAT');
  246. maybe_loadesi;
  247. popusedregisters(pushedregs);
  248. set_location(p^.location,p^.left^.location);
  249. ungetiftemp(p^.right^.location.reference);
  250. end;
  251. ltn,lten,gtn,gten,
  252. equaln,unequaln :
  253. begin
  254. cmpop:=true;
  255. { generate better code for s='' and s<>'' }
  256. if (p^.treetype in [equaln,unequaln]) and
  257. (((p^.left^.treetype=stringconstn) and (str_length(p^.left)=0)) or
  258. ((p^.right^.treetype=stringconstn) and (str_length(p^.right)=0))) then
  259. begin
  260. secondpass(p^.left);
  261. { are too few registers free? }
  262. pushed:=maybe_push(p^.right^.registers32,p^.left,false);
  263. secondpass(p^.right);
  264. if pushed then
  265. restore(p^.left,false);
  266. { only one node can be stringconstn }
  267. { else pass 1 would have evaluted }
  268. { this node }
  269. if p^.left^.treetype=stringconstn then
  270. emit_const_ref(
  271. A_CMP,S_B,0,newreference(p^.right^.location.reference))
  272. else
  273. emit_const_ref(
  274. A_CMP,S_B,0,newreference(p^.left^.location.reference));
  275. del_reference(p^.right^.location.reference);
  276. del_reference(p^.left^.location.reference);
  277. end
  278. else
  279. begin
  280. pushusedregisters(pushedregs,$ff);
  281. secondpass(p^.left);
  282. emitpushreferenceaddr(p^.left^.location.reference);
  283. del_reference(p^.left^.location.reference);
  284. secondpass(p^.right);
  285. emitpushreferenceaddr(p^.right^.location.reference);
  286. del_reference(p^.right^.location.reference);
  287. emitcall('FPC_SHORTSTR_COMPARE');
  288. maybe_loadesi;
  289. popusedregisters(pushedregs);
  290. end;
  291. ungetiftemp(p^.left^.location.reference);
  292. ungetiftemp(p^.right^.location.reference);
  293. end;
  294. else CGMessage(type_e_mismatch);
  295. end;
  296. SetResultLocation(cmpop,true,p);
  297. end;
  298. end;
  299. end;
  300. {*****************************************************************************
  301. Addset
  302. *****************************************************************************}
  303. procedure addset(var p : ptree);
  304. var
  305. createset,
  306. cmpop,
  307. pushed : boolean;
  308. href : treference;
  309. pushedregs : tpushed;
  310. begin
  311. cmpop:=false;
  312. { not commutative }
  313. if p^.swaped then
  314. swaptree(p);
  315. { optimize first loading of a set }
  316. {$ifdef usecreateset}
  317. if (p^.right^.treetype=setelementn) and
  318. not(assigned(p^.right^.right)) and
  319. is_emptyset(p^.left) then
  320. createset:=true
  321. else
  322. {$endif}
  323. begin
  324. createset:=false;
  325. secondpass(p^.left);
  326. end;
  327. { are too few registers free? }
  328. pushed:=maybe_push(p^.right^.registers32,p^.left,false);
  329. secondpass(p^.right);
  330. if codegenerror then
  331. exit;
  332. if pushed then
  333. restore(p^.left,false);
  334. set_location(p^.location,p^.left^.location);
  335. { handle operations }
  336. {$IfDef regallocfix}
  337. pushusedregisters(pushedregs,$ff
  338. xor ($80 shr byte(p^.left^.location.reference.base))
  339. xor ($80 shr byte(p^.left^.location.reference.index))
  340. xor ($80 shr byte(p^.right^.location.reference.base))
  341. xor ($80 shr byte(p^.right^.location.reference.index)));
  342. {$EndIf regallocfix}
  343. case p^.treetype of
  344. equaln,
  345. unequaln
  346. {$IfNDef NoSetInclusion}
  347. ,lten, gten
  348. {$EndIf NoSetInclusion}
  349. : begin
  350. cmpop:=true;
  351. del_location(p^.left^.location);
  352. del_location(p^.right^.location);
  353. pushusedregisters(pushedregs,$ff);
  354. {$IfNDef NoSetInclusion}
  355. If (p^.treetype in [equaln, unequaln, lten]) Then
  356. Begin
  357. {$EndIf NoSetInclusion}
  358. emitpushreferenceaddr(p^.right^.location.reference);
  359. emitpushreferenceaddr(p^.left^.location.reference);
  360. {$IfNDef NoSetInclusion}
  361. End
  362. Else {gten = lten, if the arguments are reversed}
  363. Begin
  364. emitpushreferenceaddr(p^.left^.location.reference);
  365. emitpushreferenceaddr(p^.right^.location.reference);
  366. End;
  367. Case p^.treetype of
  368. equaln, unequaln:
  369. {$EndIf NoSetInclusion}
  370. emitcall('FPC_SET_COMP_SETS');
  371. {$IfNDef NoSetInclusion}
  372. lten, gten:
  373. Begin
  374. emitcall('FPC_SET_CONTAINS_SETS');
  375. { we need a jne afterwards, not a jnbe/jnae }
  376. p^.treetype := equaln;
  377. End;
  378. End;
  379. {$EndIf NoSetInclusion}
  380. maybe_loadesi;
  381. popusedregisters(pushedregs);
  382. ungetiftemp(p^.left^.location.reference);
  383. ungetiftemp(p^.right^.location.reference);
  384. end;
  385. addn : begin
  386. { add can be an other SET or Range or Element ! }
  387. {$IfNDef regallocfix}
  388. del_location(p^.left^.location);
  389. { del_location(p^.right^.location);
  390. done in pushsetelement below PM }
  391. del_location(p^.right^.location);
  392. pushusedregisters(pushedregs,$ff);
  393. {$EndIf regallocfix}
  394. href.symbol:=nil;
  395. gettempofsizereference(32,href);
  396. if createset then
  397. begin
  398. {$IfDef regallocfix}
  399. del_location(p^.left^.location);
  400. { del_location(p^.right^.location);
  401. done in pushsetelement below PM }
  402. {$EndIf regallocfix}
  403. pushsetelement(p^.right^.left);
  404. emitpushreferenceaddr(href);
  405. emitcall('FPC_SET_CREATE_ELEMENT');
  406. end
  407. else
  408. begin
  409. { add a range or a single element? }
  410. if p^.right^.treetype=setelementn then
  411. begin
  412. {$IfNDef regallocfix}
  413. concatcopy(p^.left^.location.reference,href,32,false,false);
  414. {$Else regallocfix}
  415. concatcopy(p^.left^.location.reference,href,32,true,false);
  416. {$EndIf regallocfix}
  417. if assigned(p^.right^.right) then
  418. begin
  419. pushsetelement(p^.right^.right);
  420. pushsetelement(p^.right^.left);
  421. emitpushreferenceaddr(href);
  422. emitcall('FPC_SET_SET_RANGE');
  423. end
  424. else
  425. begin
  426. pushsetelement(p^.right^.left);
  427. emitpushreferenceaddr(href);
  428. emitcall('FPC_SET_SET_BYTE');
  429. end;
  430. end
  431. else
  432. begin
  433. { must be an other set }
  434. emitpushreferenceaddr(href);
  435. emitpushreferenceaddr(p^.right^.location.reference);
  436. {$IfDef regallocfix}
  437. del_location(p^.right^.location);
  438. {$EndIf regallocfix}
  439. emitpushreferenceaddr(p^.left^.location.reference);
  440. {$IfDef regallocfix}
  441. del_location(p^.left^.location);
  442. {$EndIf regallocfix}
  443. emitcall('FPC_SET_ADD_SETS');
  444. end;
  445. end;
  446. maybe_loadesi;
  447. popusedregisters(pushedregs);
  448. ungetiftemp(p^.left^.location.reference);
  449. ungetiftemp(p^.right^.location.reference);
  450. p^.location.loc:=LOC_MEM;
  451. p^.location.reference:=href;
  452. end;
  453. subn,
  454. symdifn,
  455. muln : begin
  456. {$IfNDef regallocfix}
  457. del_location(p^.left^.location);
  458. del_location(p^.right^.location);
  459. pushusedregisters(pushedregs,$ff);
  460. {$EndIf regallocfix}
  461. href.symbol:=nil;
  462. gettempofsizereference(32,href);
  463. emitpushreferenceaddr(href);
  464. emitpushreferenceaddr(p^.right^.location.reference);
  465. {$IfDef regallocfix}
  466. del_location(p^.right^.location);
  467. {$EndIf regallocfix}
  468. emitpushreferenceaddr(p^.left^.location.reference);
  469. {$IfDef regallocfix}
  470. del_location(p^.left^.location);
  471. {$EndIf regallocfix}
  472. case p^.treetype of
  473. subn : emitcall('FPC_SET_SUB_SETS');
  474. symdifn : emitcall('FPC_SET_SYMDIF_SETS');
  475. muln : emitcall('FPC_SET_MUL_SETS');
  476. end;
  477. maybe_loadesi;
  478. popusedregisters(pushedregs);
  479. ungetiftemp(p^.left^.location.reference);
  480. ungetiftemp(p^.right^.location.reference);
  481. p^.location.loc:=LOC_MEM;
  482. p^.location.reference:=href;
  483. end;
  484. else
  485. CGMessage(type_e_mismatch);
  486. end;
  487. SetResultLocation(cmpop,true,p);
  488. end;
  489. {*****************************************************************************
  490. SecondAdd
  491. *****************************************************************************}
  492. procedure secondadd(var p : ptree);
  493. { is also being used for xor, and "mul", "sub, or and comparative }
  494. { operators }
  495. label do_normal;
  496. var
  497. hregister,hregister2 : tregister;
  498. noswap,popeax,popedx,
  499. pushed,mboverflow,cmpop : boolean;
  500. op,op2 : tasmop;
  501. flags : tresflags;
  502. otl,ofl,hl : pasmlabel;
  503. power : longint;
  504. opsize : topsize;
  505. hl4: pasmlabel;
  506. hr : preference;
  507. { true, if unsigned types are compared }
  508. unsigned : boolean;
  509. { true, if a small set is handled with the longint code }
  510. is_set : boolean;
  511. { is_in_dest if the result is put directly into }
  512. { the resulting refernce or varregister }
  513. is_in_dest : boolean;
  514. { true, if for sets subtractions the extra not should generated }
  515. extra_not : boolean;
  516. {$ifdef SUPPORT_MMX}
  517. mmxbase : tmmxtype;
  518. {$endif SUPPORT_MMX}
  519. pushedreg : tpushed;
  520. hloc : tlocation;
  521. procedure firstjmp64bitcmp;
  522. var
  523. oldtreetype : ttreetyp;
  524. begin
  525. { the jump the sequence is a little bit hairy }
  526. case p^.treetype of
  527. ltn,gtn:
  528. begin
  529. emitjmp(flag_2_cond[getresflags(p,unsigned)],truelabel);
  530. { cheat a little bit for the negative test }
  531. p^.swaped:=not(p^.swaped);
  532. emitjmp(flag_2_cond[getresflags(p,unsigned)],falselabel);
  533. p^.swaped:=not(p^.swaped);
  534. end;
  535. lten,gten:
  536. begin
  537. oldtreetype:=p^.treetype;
  538. if p^.treetype=lten then
  539. p^.treetype:=ltn
  540. else
  541. p^.treetype:=gtn;
  542. emitjmp(flag_2_cond[getresflags(p,unsigned)],truelabel);
  543. { cheat for the negative test }
  544. if p^.treetype=ltn then
  545. p^.treetype:=gtn
  546. else
  547. p^.treetype:=ltn;
  548. emitjmp(flag_2_cond[getresflags(p,unsigned)],falselabel);
  549. p^.treetype:=oldtreetype;
  550. end;
  551. equaln:
  552. emitjmp(C_NE,falselabel);
  553. unequaln:
  554. emitjmp(C_NE,truelabel);
  555. end;
  556. end;
  557. procedure secondjmp64bitcmp;
  558. begin
  559. { the jump the sequence is a little bit hairy }
  560. case p^.treetype of
  561. ltn,gtn,lten,gten:
  562. begin
  563. { the comparisaion of the low dword have to be }
  564. { always unsigned! }
  565. emitjmp(flag_2_cond[getresflags(p,true)],truelabel);
  566. emitjmp(C_None,falselabel);
  567. end;
  568. equaln:
  569. begin
  570. emitjmp(C_NE,falselabel);
  571. emitjmp(C_None,truelabel);
  572. end;
  573. unequaln:
  574. begin
  575. emitjmp(C_NE,truelabel);
  576. emitjmp(C_None,falselabel);
  577. end;
  578. end;
  579. end;
  580. begin
  581. { to make it more readable, string and set (not smallset!) have their
  582. own procedures }
  583. case p^.left^.resulttype^.deftype of
  584. stringdef : begin
  585. addstring(p);
  586. exit;
  587. end;
  588. setdef : begin
  589. { normalsets are handled separate }
  590. if not(psetdef(p^.left^.resulttype)^.settype=smallset) then
  591. begin
  592. addset(p);
  593. exit;
  594. end;
  595. end;
  596. end;
  597. { defaults }
  598. unsigned:=false;
  599. is_in_dest:=false;
  600. extra_not:=false;
  601. noswap:=false;
  602. opsize:=S_L;
  603. { are we a (small)set, must be set here because the side can be
  604. swapped ! (PFV) }
  605. is_set:=(p^.left^.resulttype^.deftype=setdef);
  606. { calculate the operator which is more difficult }
  607. firstcomplex(p);
  608. { handling boolean expressions extra: }
  609. if is_boolean(p^.left^.resulttype) and
  610. is_boolean(p^.right^.resulttype) then
  611. begin
  612. if (porddef(p^.left^.resulttype)^.typ=bool8bit) or
  613. (porddef(p^.right^.resulttype)^.typ=bool8bit) then
  614. opsize:=S_B
  615. else
  616. if (porddef(p^.left^.resulttype)^.typ=bool16bit) or
  617. (porddef(p^.right^.resulttype)^.typ=bool16bit) then
  618. opsize:=S_W
  619. else
  620. opsize:=S_L;
  621. case p^.treetype of
  622. andn,
  623. orn : begin
  624. clear_location(p^.location);
  625. p^.location.loc:=LOC_JUMP;
  626. cmpop:=false;
  627. case p^.treetype of
  628. andn : begin
  629. otl:=truelabel;
  630. getlabel(truelabel);
  631. secondpass(p^.left);
  632. maketojumpbool(p^.left);
  633. emitlab(truelabel);
  634. truelabel:=otl;
  635. end;
  636. orn : begin
  637. ofl:=falselabel;
  638. getlabel(falselabel);
  639. secondpass(p^.left);
  640. maketojumpbool(p^.left);
  641. emitlab(falselabel);
  642. falselabel:=ofl;
  643. end;
  644. else
  645. CGMessage(type_e_mismatch);
  646. end;
  647. secondpass(p^.right);
  648. maketojumpbool(p^.right);
  649. end;
  650. unequaln,ltn,lten,gtn,gten,
  651. equaln,xorn : begin
  652. if p^.left^.treetype=ordconstn then
  653. swaptree(p);
  654. if p^.left^.location.loc=LOC_JUMP then
  655. begin
  656. otl:=truelabel;
  657. getlabel(truelabel);
  658. ofl:=falselabel;
  659. getlabel(falselabel);
  660. end;
  661. secondpass(p^.left);
  662. { if in flags then copy first to register, because the
  663. flags can be destroyed }
  664. case p^.left^.location.loc of
  665. LOC_FLAGS:
  666. locflags2reg(p^.left^.location,opsize);
  667. LOC_JUMP:
  668. begin
  669. case opsize of
  670. S_L : hregister:=getregister32;
  671. S_W : hregister:=reg32toreg16(getregister32);
  672. S_B : hregister:=reg32toreg8(getregister32);
  673. end;
  674. p^.left^.location.loc:=LOC_REGISTER;
  675. p^.left^.location.register:=hregister;
  676. emitlab(truelabel);
  677. truelabel:=otl;
  678. emit_const_reg(A_MOV,opsize,1,hregister);
  679. getlabel(hl);
  680. emitjmp(C_None,hl);
  681. emitlab(falselabel);
  682. falselabel:=ofl;
  683. emit_reg_reg(A_XOR,S_L,makereg32(hregister),
  684. makereg32(hregister));
  685. emitlab(hl);
  686. end;
  687. end;
  688. set_location(p^.location,p^.left^.location);
  689. pushed:=maybe_push(p^.right^.registers32,p,false);
  690. if p^.right^.location.loc=LOC_JUMP then
  691. begin
  692. otl:=truelabel;
  693. getlabel(truelabel);
  694. ofl:=falselabel;
  695. getlabel(falselabel);
  696. end;
  697. secondpass(p^.right);
  698. if pushed then
  699. begin
  700. restore(p,false);
  701. set_location(p^.left^.location,p^.location);
  702. end;
  703. case p^.right^.location.loc of
  704. LOC_FLAGS:
  705. locflags2reg(p^.right^.location,opsize);
  706. LOC_JUMP:
  707. begin
  708. case opsize of
  709. S_L : hregister:=getregister32;
  710. S_W : hregister:=reg32toreg16(getregister32);
  711. S_B : hregister:=reg32toreg8(getregister32);
  712. end;
  713. p^.right^.location.loc:=LOC_REGISTER;
  714. p^.right^.location.register:=hregister;
  715. emitlab(truelabel);
  716. truelabel:=otl;
  717. emit_const_reg(A_MOV,opsize,1,hregister);
  718. getlabel(hl);
  719. emitjmp(C_None,hl);
  720. emitlab(falselabel);
  721. falselabel:=ofl;
  722. emit_reg_reg(A_XOR,S_L,makereg32(hregister),
  723. makereg32(hregister));
  724. emitlab(hl);
  725. end;
  726. end;
  727. goto do_normal;
  728. end
  729. else
  730. CGMessage(type_e_mismatch);
  731. end
  732. end
  733. else
  734. begin
  735. { in case of constant put it to the left }
  736. if (p^.left^.treetype=ordconstn) then
  737. swaptree(p);
  738. secondpass(p^.left);
  739. { this will be complicated as
  740. a lot of code below assumes that
  741. p^.location and p^.left^.location are the same }
  742. {$ifdef test_dest_loc}
  743. if dest_loc_known and (dest_loc_tree=p) and
  744. ((dest_loc.loc=LOC_REGISTER) or (dest_loc.loc=LOC_CREGISTER)) then
  745. begin
  746. set_location(p^.location,dest_loc);
  747. in_dest_loc:=true;
  748. is_in_dest:=true;
  749. end
  750. else
  751. {$endif test_dest_loc}
  752. set_location(p^.location,p^.left^.location);
  753. { are too few registers free? }
  754. pushed:=maybe_push(p^.right^.registers32,p,is_64bitint(p^.left^.resulttype));
  755. secondpass(p^.right);
  756. if pushed then
  757. begin
  758. restore(p,is_64bitint(p^.left^.resulttype));
  759. set_location(p^.left^.location,p^.location);
  760. end;
  761. if (p^.left^.resulttype^.deftype=pointerdef) or
  762. (p^.right^.resulttype^.deftype=pointerdef) or
  763. ((p^.right^.resulttype^.deftype=objectdef) and
  764. pobjectdef(p^.right^.resulttype)^.is_class and
  765. (p^.left^.resulttype^.deftype=objectdef) and
  766. pobjectdef(p^.left^.resulttype)^.is_class
  767. ) or
  768. (p^.left^.resulttype^.deftype=classrefdef) or
  769. (p^.left^.resulttype^.deftype=procvardef) or
  770. ((p^.left^.resulttype^.deftype=enumdef) and
  771. (p^.left^.resulttype^.size=4)) or
  772. ((p^.left^.resulttype^.deftype=orddef) and
  773. (porddef(p^.left^.resulttype)^.typ=s32bit)) or
  774. ((p^.right^.resulttype^.deftype=orddef) and
  775. (porddef(p^.right^.resulttype)^.typ=s32bit)) or
  776. ((p^.left^.resulttype^.deftype=orddef) and
  777. (porddef(p^.left^.resulttype)^.typ=u32bit)) or
  778. ((p^.right^.resulttype^.deftype=orddef) and
  779. (porddef(p^.right^.resulttype)^.typ=u32bit)) or
  780. { as well as small sets }
  781. is_set then
  782. begin
  783. do_normal:
  784. mboverflow:=false;
  785. cmpop:=false;
  786. {$ifndef cardinalmulfix}
  787. unsigned :=
  788. (p^.left^.resulttype^.deftype=pointerdef) or
  789. (p^.right^.resulttype^.deftype=pointerdef) or
  790. ((p^.left^.resulttype^.deftype=orddef) and
  791. (porddef(p^.left^.resulttype)^.typ=u32bit)) or
  792. ((p^.right^.resulttype^.deftype=orddef) and
  793. (porddef(p^.right^.resulttype)^.typ=u32bit));
  794. {$else cardinalmulfix}
  795. unsigned := not(is_signed(p^.left^.resulttype)) or
  796. not(is_signed(p^.right^.resulttype));
  797. {$endif cardinalmulfix}
  798. case p^.treetype of
  799. addn : begin
  800. if is_set then
  801. begin
  802. { adding elements is not commutative }
  803. if p^.swaped and (p^.left^.treetype=setelementn) then
  804. swaptree(p);
  805. { are we adding set elements ? }
  806. if p^.right^.treetype=setelementn then
  807. begin
  808. { no range support for smallsets! }
  809. if assigned(p^.right^.right) then
  810. internalerror(43244);
  811. { bts requires both elements to be registers }
  812. if p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  813. begin
  814. ungetiftemp(p^.left^.location.reference);
  815. del_location(p^.left^.location);
  816. {!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!}
  817. hregister:=getregister32;
  818. emit_ref_reg(A_MOV,opsize,
  819. newreference(p^.left^.location.reference),hregister);
  820. clear_location(p^.left^.location);
  821. p^.left^.location.loc:=LOC_REGISTER;
  822. p^.left^.location.register:=hregister;
  823. set_location(p^.location,p^.left^.location);
  824. end;
  825. if p^.right^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  826. begin
  827. ungetiftemp(p^.right^.location.reference);
  828. del_location(p^.right^.location);
  829. hregister:=getregister32;
  830. emit_ref_reg(A_MOV,opsize,
  831. newreference(p^.right^.location.reference),hregister);
  832. clear_location(p^.right^.location);
  833. p^.right^.location.loc:=LOC_REGISTER;
  834. p^.right^.location.register:=hregister;
  835. end;
  836. op:=A_BTS;
  837. noswap:=true;
  838. end
  839. else
  840. op:=A_OR;
  841. mboverflow:=false;
  842. unsigned:=false;
  843. end
  844. else
  845. begin
  846. op:=A_ADD;
  847. mboverflow:=true;
  848. end;
  849. end;
  850. symdifn : begin
  851. { the symetric diff is only for sets }
  852. if is_set then
  853. begin
  854. op:=A_XOR;
  855. mboverflow:=false;
  856. unsigned:=false;
  857. end
  858. else
  859. CGMessage(type_e_mismatch);
  860. end;
  861. muln : begin
  862. if is_set then
  863. begin
  864. op:=A_AND;
  865. mboverflow:=false;
  866. unsigned:=false;
  867. end
  868. else
  869. begin
  870. if unsigned then
  871. op:=A_MUL
  872. else
  873. op:=A_IMUL;
  874. mboverflow:=true;
  875. end;
  876. end;
  877. subn : begin
  878. if is_set then
  879. begin
  880. op:=A_AND;
  881. mboverflow:=false;
  882. unsigned:=false;
  883. {$IfNDef NoSetConstNot}
  884. If (p^.right^.treetype = setconstn) then
  885. p^.right^.location.reference.offset := not(p^.right^.location.reference.offset)
  886. Else
  887. {$EndIf NoNosetConstNot}
  888. extra_not:=true;
  889. end
  890. else
  891. begin
  892. op:=A_SUB;
  893. mboverflow:=true;
  894. end;
  895. end;
  896. ltn,lten,
  897. gtn,gten,
  898. equaln,unequaln : begin
  899. {$IfNDef NoSetInclusion}
  900. If is_set Then
  901. Case p^.treetype of
  902. lten,gten:
  903. Begin
  904. If p^.treetype = lten then
  905. swaptree(p);
  906. if p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  907. begin
  908. ungetiftemp(p^.left^.location.reference);
  909. del_reference(p^.left^.location.reference);
  910. hregister:=getregister32;
  911. emit_ref_reg(A_MOV,opsize,
  912. newreference(p^.left^.location.reference),hregister);
  913. clear_location(p^.left^.location);
  914. p^.left^.location.loc:=LOC_REGISTER;
  915. p^.left^.location.register:=hregister;
  916. set_location(p^.location,p^.left^.location);
  917. end
  918. else
  919. if p^.left^.location.loc = LOC_CREGISTER Then
  920. {save the register var in a temp register, because
  921. its value is going to be modified}
  922. begin
  923. hregister := getregister32;
  924. emit_reg_reg(A_MOV,opsize,
  925. p^.left^.location.register,hregister);
  926. clear_location(p^.left^.location);
  927. p^.left^.location.loc:=LOC_REGISTER;
  928. p^.left^.location.register:=hregister;
  929. set_location(p^.location,p^.left^.location);
  930. end;
  931. {here, p^.left^.location should be LOC_REGISTER}
  932. If p^.right^.location.loc in [LOC_MEM,LOC_REFERENCE] Then
  933. emit_ref_reg(A_AND,opsize,
  934. newreference(p^.right^.location.reference),p^.left^.location.register)
  935. Else
  936. emit_reg_reg(A_AND,opsize,
  937. p^.right^.location.register,p^.left^.location.register);
  938. {warning: ugly hack ahead: we need a "jne" after the cmp, so
  939. change the treetype from lten/gten to equaln}
  940. p^.treetype := equaln
  941. End;
  942. {no < or > support for sets}
  943. ltn,gtn: CGMessage(type_e_mismatch);
  944. End;
  945. {$EndIf NoSetInclusion}
  946. op:=A_CMP;
  947. cmpop:=true;
  948. end;
  949. xorn : op:=A_XOR;
  950. orn : op:=A_OR;
  951. andn : op:=A_AND;
  952. else
  953. CGMessage(type_e_mismatch);
  954. end;
  955. { filter MUL, which requires special handling }
  956. if op=A_MUL then
  957. begin
  958. popeax:=false;
  959. popedx:=false;
  960. { here you need to free the symbol first }
  961. { This is VERY bad!!!! Now the optimizer assumes the current values of }
  962. { those registers are NOT needed anymore, which causes bugs! The }
  963. { optimizer now contains a workaround which tries to fix all wrong }
  964. { deallocations, but this kind of things should NOT be done (JM) }
  965. clear_location(p^.location);
  966. release_loc(p^.right^.location);
  967. release_loc(p^.left^.location);
  968. p^.location.register:=getregister32;
  969. p^.location.loc:=LOC_REGISTER;
  970. {$IfNDef NoShlMul}
  971. if p^.right^.treetype=ordconstn then
  972. swaptree(p);
  973. If (p^.left^.treetype = ordconstn) and
  974. ispowerof2(p^.left^.value, power) and
  975. not(cs_check_overflow in aktlocalswitches) then
  976. Begin
  977. emitloadord2reg(p^.right^.location,u32bitdef,p^.location.register,true);
  978. emit_const_reg(A_SHL,S_L,power,p^.location.register)
  979. End
  980. Else
  981. Begin
  982. {$EndIf NoShlMul}
  983. if not(R_EAX in unused) and (p^.location.register<>R_EAX) then
  984. begin
  985. emit_reg(A_PUSH,S_L,R_EAX);
  986. popeax:=true;
  987. end;
  988. if not(R_EDX in unused) and (p^.location.register<>R_EDX) then
  989. begin
  990. emit_reg(A_PUSH,S_L,R_EDX);
  991. popedx:=true;
  992. end;
  993. { p^.left^.location can be R_EAX !!! }
  994. {$ifndef noAllocEdi}
  995. getexplicitregister32(R_EDI);
  996. {$endif noAllocEdi}
  997. emitloadord2reg(p^.left^.location,u32bitdef,R_EDI,true);
  998. emitloadord2reg(p^.right^.location,u32bitdef,R_EAX,true);
  999. {$ifndef noAllocEdi}
  1000. if R_EDX in unused then
  1001. exprasmlist^.concat(new(pairegalloc,alloc(R_EDX)));
  1002. {$endif noAllocEdi}
  1003. emit_reg(A_MUL,S_L,R_EDI);
  1004. {$ifndef noAllocEdi}
  1005. ungetregister32(R_EDI);
  1006. if R_EDX in unused then
  1007. exprasmlist^.concat(new(pairegalloc,dealloc(R_EDX)));
  1008. {$endif noAllocEdi}
  1009. emit_reg_reg(A_MOV,S_L,R_EAX,p^.location.register);
  1010. if popedx then
  1011. emit_reg(A_POP,S_L,R_EDX);
  1012. if popeax then
  1013. emit_reg(A_POP,S_L,R_EAX);
  1014. {$IfNDef NoShlMul}
  1015. End;
  1016. {$endif NoShlMul}
  1017. SetResultLocation(false,true,p);
  1018. exit;
  1019. end;
  1020. { Convert flags to register first }
  1021. if (p^.left^.location.loc=LOC_FLAGS) then
  1022. locflags2reg(p^.left^.location,opsize);
  1023. if (p^.right^.location.loc=LOC_FLAGS) then
  1024. locflags2reg(p^.right^.location,opsize);
  1025. { left and right no register? }
  1026. { then one must be demanded }
  1027. if (p^.left^.location.loc<>LOC_REGISTER) and
  1028. (p^.right^.location.loc<>LOC_REGISTER) then
  1029. begin
  1030. { register variable ? }
  1031. if (p^.left^.location.loc=LOC_CREGISTER) then
  1032. begin
  1033. { it is OK if this is the destination }
  1034. if is_in_dest then
  1035. begin
  1036. hregister:=p^.location.register;
  1037. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  1038. hregister);
  1039. end
  1040. else
  1041. if cmpop then
  1042. begin
  1043. { do not disturb the register }
  1044. hregister:=p^.location.register;
  1045. end
  1046. else
  1047. begin
  1048. case opsize of
  1049. S_L : hregister:=getregister32;
  1050. S_B : hregister:=reg32toreg8(getregister32);
  1051. end;
  1052. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  1053. hregister);
  1054. end
  1055. end
  1056. else
  1057. begin
  1058. ungetiftemp(p^.left^.location.reference);
  1059. del_reference(p^.left^.location.reference);
  1060. if is_in_dest then
  1061. begin
  1062. hregister:=p^.location.register;
  1063. emit_ref_reg(A_MOV,opsize,
  1064. newreference(p^.left^.location.reference),hregister);
  1065. end
  1066. else
  1067. begin
  1068. { first give free, then demand new register }
  1069. case opsize of
  1070. S_L : hregister:=getregister32;
  1071. S_W : hregister:=reg32toreg16(getregister32);
  1072. S_B : hregister:=reg32toreg8(getregister32);
  1073. end;
  1074. emit_ref_reg(A_MOV,opsize,
  1075. newreference(p^.left^.location.reference),hregister);
  1076. end;
  1077. end;
  1078. clear_location(p^.location);
  1079. p^.location.loc:=LOC_REGISTER;
  1080. p^.location.register:=hregister;
  1081. end
  1082. else
  1083. { if on the right the register then swap }
  1084. if not(noswap) and (p^.right^.location.loc=LOC_REGISTER) then
  1085. begin
  1086. swap_location(p^.location,p^.right^.location);
  1087. { newly swapped also set swapped flag }
  1088. p^.swaped:=not(p^.swaped);
  1089. end;
  1090. { at this point, p^.location.loc should be LOC_REGISTER }
  1091. { and p^.location.register should be a valid register }
  1092. { containing the left result }
  1093. if p^.right^.location.loc<>LOC_REGISTER then
  1094. begin
  1095. if (p^.treetype=subn) and p^.swaped then
  1096. begin
  1097. if p^.right^.location.loc=LOC_CREGISTER then
  1098. begin
  1099. if extra_not then
  1100. emit_reg(A_NOT,opsize,p^.location.register);
  1101. {$ifndef noAllocEdi}
  1102. getexplicitregister32(R_EDI);
  1103. {$endif noAllocEdi}
  1104. emit_reg_reg(A_MOV,opsize,p^.right^.location.register,R_EDI);
  1105. emit_reg_reg(op,opsize,p^.location.register,R_EDI);
  1106. emit_reg_reg(A_MOV,opsize,R_EDI,p^.location.register);
  1107. {$ifndef noAllocEdi}
  1108. ungetregister32(R_EDI);
  1109. {$endif noAllocEdi}
  1110. end
  1111. else
  1112. begin
  1113. if extra_not then
  1114. emit_reg(A_NOT,opsize,p^.location.register);
  1115. {$ifndef noAllocEdi}
  1116. getexplicitregister32(R_EDI);
  1117. {$endif noAllocEdi}
  1118. emit_ref_reg(A_MOV,opsize,
  1119. newreference(p^.right^.location.reference),R_EDI);
  1120. emit_reg_reg(op,opsize,p^.location.register,R_EDI);
  1121. emit_reg_reg(A_MOV,opsize,R_EDI,p^.location.register);
  1122. {$ifndef noAllocEdi}
  1123. ungetregister32(R_EDI);
  1124. {$endif noAllocEdi}
  1125. ungetiftemp(p^.right^.location.reference);
  1126. del_reference(p^.right^.location.reference);
  1127. end;
  1128. end
  1129. else
  1130. begin
  1131. if (p^.right^.treetype=ordconstn) and
  1132. (op=A_CMP) and
  1133. (p^.right^.value=0) then
  1134. begin
  1135. emit_reg_reg(A_TEST,opsize,p^.location.register,
  1136. p^.location.register);
  1137. end
  1138. else if (p^.right^.treetype=ordconstn) and
  1139. (op=A_ADD) and
  1140. (p^.right^.value=1) and
  1141. not(cs_check_overflow in aktlocalswitches) then
  1142. begin
  1143. emit_reg(A_INC,opsize,
  1144. p^.location.register);
  1145. end
  1146. else if (p^.right^.treetype=ordconstn) and
  1147. (op=A_SUB) and
  1148. (p^.right^.value=1) and
  1149. not(cs_check_overflow in aktlocalswitches) then
  1150. begin
  1151. emit_reg(A_DEC,opsize,
  1152. p^.location.register);
  1153. end
  1154. else if (p^.right^.treetype=ordconstn) and
  1155. (op=A_IMUL) and
  1156. (ispowerof2(p^.right^.value,power)) and
  1157. not(cs_check_overflow in aktlocalswitches) then
  1158. begin
  1159. emit_const_reg(A_SHL,opsize,power,
  1160. p^.location.register);
  1161. end
  1162. else
  1163. begin
  1164. if (p^.right^.location.loc=LOC_CREGISTER) then
  1165. begin
  1166. if extra_not then
  1167. begin
  1168. {$ifndef noAllocEdi}
  1169. getexplicitregister32(R_EDI);
  1170. {$endif noAllocEdi}
  1171. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,R_EDI);
  1172. emit_reg(A_NOT,S_L,R_EDI);
  1173. emit_reg_reg(A_AND,S_L,R_EDI,
  1174. p^.location.register);
  1175. {$ifndef noAllocEdi}
  1176. ungetregister32(R_EDI);
  1177. {$endif noAllocEdi}
  1178. end
  1179. else
  1180. begin
  1181. emit_reg_reg(op,opsize,p^.right^.location.register,
  1182. p^.location.register);
  1183. end;
  1184. end
  1185. else
  1186. begin
  1187. if extra_not then
  1188. begin
  1189. {$ifndef noAllocEdi}
  1190. getexplicitregister32(R_EDI);
  1191. {$endif noAllocEdi}
  1192. emit_ref_reg(A_MOV,S_L,newreference(
  1193. p^.right^.location.reference),R_EDI);
  1194. emit_reg(A_NOT,S_L,R_EDI);
  1195. emit_reg_reg(A_AND,S_L,R_EDI,
  1196. p^.location.register);
  1197. {$ifndef noAllocEdi}
  1198. ungetregister32(R_EDI);
  1199. {$endif noAllocEdi}
  1200. end
  1201. else
  1202. begin
  1203. emit_ref_reg(op,opsize,newreference(
  1204. p^.right^.location.reference),p^.location.register);
  1205. end;
  1206. ungetiftemp(p^.right^.location.reference);
  1207. del_reference(p^.right^.location.reference);
  1208. end;
  1209. end;
  1210. end;
  1211. end
  1212. else
  1213. begin
  1214. { when swapped another result register }
  1215. if (p^.treetype=subn) and p^.swaped then
  1216. begin
  1217. if extra_not then
  1218. emit_reg(A_NOT,S_L,p^.location.register);
  1219. emit_reg_reg(op,opsize,
  1220. p^.location.register,p^.right^.location.register);
  1221. swap_location(p^.location,p^.right^.location);
  1222. { newly swapped also set swapped flag }
  1223. { just to maintain ordering }
  1224. p^.swaped:=not(p^.swaped);
  1225. end
  1226. else
  1227. begin
  1228. if extra_not then
  1229. emit_reg(A_NOT,S_L,p^.right^.location.register);
  1230. emit_reg_reg(op,opsize,
  1231. p^.right^.location.register,
  1232. p^.location.register);
  1233. end;
  1234. case opsize of
  1235. S_L : ungetregister32(p^.right^.location.register);
  1236. S_B : ungetregister32(reg8toreg32(p^.right^.location.register));
  1237. end;
  1238. end;
  1239. if cmpop then
  1240. case opsize of
  1241. S_L : ungetregister32(p^.location.register);
  1242. S_B : ungetregister32(reg8toreg32(p^.location.register));
  1243. end;
  1244. { only in case of overflow operations }
  1245. { produce overflow code }
  1246. { we must put it here directly, because sign of operation }
  1247. { is in unsigned VAR!! }
  1248. if mboverflow then
  1249. begin
  1250. if cs_check_overflow in aktlocalswitches then
  1251. begin
  1252. getlabel(hl4);
  1253. if unsigned then
  1254. emitjmp(C_NB,hl4)
  1255. else
  1256. emitjmp(C_NO,hl4);
  1257. emitcall('FPC_OVERFLOW');
  1258. emitlab(hl4);
  1259. end;
  1260. end;
  1261. end
  1262. else
  1263. { Char type }
  1264. if ((p^.left^.resulttype^.deftype=orddef) and
  1265. (porddef(p^.left^.resulttype)^.typ=uchar)) or
  1266. { enumeration type 16 bit }
  1267. ((p^.left^.resulttype^.deftype=enumdef) and
  1268. (p^.left^.resulttype^.size=1)) then
  1269. begin
  1270. case p^.treetype of
  1271. ltn,lten,gtn,gten,
  1272. equaln,unequaln :
  1273. cmpop:=true;
  1274. else CGMessage(type_e_mismatch);
  1275. end;
  1276. unsigned:=true;
  1277. { left and right no register? }
  1278. { the one must be demanded }
  1279. if (p^.location.loc<>LOC_REGISTER) and
  1280. (p^.right^.location.loc<>LOC_REGISTER) then
  1281. begin
  1282. if p^.location.loc=LOC_CREGISTER then
  1283. begin
  1284. if cmpop then
  1285. { do not disturb register }
  1286. hregister:=p^.location.register
  1287. else
  1288. begin
  1289. hregister:=reg32toreg8(getregister32);
  1290. emit_reg_reg(A_MOV,S_B,p^.location.register,
  1291. hregister);
  1292. end;
  1293. end
  1294. else
  1295. begin
  1296. del_reference(p^.location.reference);
  1297. { first give free then demand new register }
  1298. hregister:=reg32toreg8(getregister32);
  1299. emit_ref_reg(A_MOV,S_B,newreference(p^.location.reference),
  1300. hregister);
  1301. end;
  1302. clear_location(p^.location);
  1303. p^.location.loc:=LOC_REGISTER;
  1304. p^.location.register:=hregister;
  1305. end;
  1306. { now p always a register }
  1307. if (p^.right^.location.loc=LOC_REGISTER) and
  1308. (p^.location.loc<>LOC_REGISTER) then
  1309. begin
  1310. swap_location(p^.location,p^.right^.location);
  1311. { newly swapped also set swapped flag }
  1312. p^.swaped:=not(p^.swaped);
  1313. end;
  1314. if p^.right^.location.loc<>LOC_REGISTER then
  1315. begin
  1316. if p^.right^.location.loc=LOC_CREGISTER then
  1317. begin
  1318. emit_reg_reg(A_CMP,S_B,
  1319. p^.right^.location.register,p^.location.register);
  1320. end
  1321. else
  1322. begin
  1323. emit_ref_reg(A_CMP,S_B,newreference(
  1324. p^.right^.location.reference),p^.location.register);
  1325. del_reference(p^.right^.location.reference);
  1326. end;
  1327. end
  1328. else
  1329. begin
  1330. emit_reg_reg(A_CMP,S_B,p^.right^.location.register,
  1331. p^.location.register);
  1332. ungetregister32(reg8toreg32(p^.right^.location.register));
  1333. end;
  1334. ungetregister32(reg8toreg32(p^.location.register));
  1335. end
  1336. else
  1337. { 16 bit enumeration type }
  1338. if ((p^.left^.resulttype^.deftype=enumdef) and
  1339. (p^.left^.resulttype^.size=2)) then
  1340. begin
  1341. case p^.treetype of
  1342. ltn,lten,gtn,gten,
  1343. equaln,unequaln :
  1344. cmpop:=true;
  1345. else CGMessage(type_e_mismatch);
  1346. end;
  1347. unsigned:=true;
  1348. { left and right no register? }
  1349. { the one must be demanded }
  1350. if (p^.location.loc<>LOC_REGISTER) and
  1351. (p^.right^.location.loc<>LOC_REGISTER) then
  1352. begin
  1353. if p^.location.loc=LOC_CREGISTER then
  1354. begin
  1355. if cmpop then
  1356. { do not disturb register }
  1357. hregister:=p^.location.register
  1358. else
  1359. begin
  1360. hregister:=reg32toreg16(getregister32);
  1361. emit_reg_reg(A_MOV,S_W,p^.location.register,
  1362. hregister);
  1363. end;
  1364. end
  1365. else
  1366. begin
  1367. del_reference(p^.location.reference);
  1368. { first give free then demand new register }
  1369. hregister:=reg32toreg16(getregister32);
  1370. emit_ref_reg(A_MOV,S_W,newreference(p^.location.reference),
  1371. hregister);
  1372. end;
  1373. clear_location(p^.location);
  1374. p^.location.loc:=LOC_REGISTER;
  1375. p^.location.register:=hregister;
  1376. end;
  1377. { now p always a register }
  1378. if (p^.right^.location.loc=LOC_REGISTER) and
  1379. (p^.location.loc<>LOC_REGISTER) then
  1380. begin
  1381. swap_location(p^.location,p^.right^.location);
  1382. { newly swapped also set swapped flag }
  1383. p^.swaped:=not(p^.swaped);
  1384. end;
  1385. if p^.right^.location.loc<>LOC_REGISTER then
  1386. begin
  1387. if p^.right^.location.loc=LOC_CREGISTER then
  1388. begin
  1389. emit_reg_reg(A_CMP,S_W,
  1390. p^.right^.location.register,p^.location.register);
  1391. end
  1392. else
  1393. begin
  1394. emit_ref_reg(A_CMP,S_W,newreference(
  1395. p^.right^.location.reference),p^.location.register);
  1396. del_reference(p^.right^.location.reference);
  1397. end;
  1398. end
  1399. else
  1400. begin
  1401. emit_reg_reg(A_CMP,S_W,p^.right^.location.register,
  1402. p^.location.register);
  1403. ungetregister32(reg16toreg32(p^.right^.location.register));
  1404. end;
  1405. ungetregister32(reg16toreg32(p^.location.register));
  1406. end
  1407. else
  1408. { 64 bit types }
  1409. if is_64bitint(p^.left^.resulttype) then
  1410. begin
  1411. mboverflow:=false;
  1412. cmpop:=false;
  1413. unsigned:=((p^.left^.resulttype^.deftype=orddef) and
  1414. (porddef(p^.left^.resulttype)^.typ=u64bit)) or
  1415. ((p^.right^.resulttype^.deftype=orddef) and
  1416. (porddef(p^.right^.resulttype)^.typ=u64bit));
  1417. case p^.treetype of
  1418. addn : begin
  1419. begin
  1420. op:=A_ADD;
  1421. op2:=A_ADC;
  1422. mboverflow:=true;
  1423. end;
  1424. end;
  1425. subn : begin
  1426. op:=A_SUB;
  1427. op2:=A_SBB;
  1428. mboverflow:=true;
  1429. end;
  1430. ltn,lten,
  1431. gtn,gten,
  1432. equaln,unequaln:
  1433. begin
  1434. op:=A_CMP;
  1435. op2:=A_CMP;
  1436. cmpop:=true;
  1437. end;
  1438. xorn:
  1439. begin
  1440. op:=A_XOR;
  1441. op2:=A_XOR;
  1442. end;
  1443. orn:
  1444. begin
  1445. op:=A_OR;
  1446. op2:=A_OR;
  1447. end;
  1448. andn:
  1449. begin
  1450. op:=A_AND;
  1451. op2:=A_AND;
  1452. end;
  1453. muln:
  1454. ;
  1455. else
  1456. CGMessage(type_e_mismatch);
  1457. end;
  1458. if p^.treetype=muln then
  1459. begin
  1460. { save p^.lcoation, because we change it now }
  1461. set_location(hloc,p^.location);
  1462. release_qword_loc(p^.location);
  1463. release_qword_loc(p^.right^.location);
  1464. p^.location.registerlow:=getexplicitregister32(R_EAX);
  1465. p^.location.registerhigh:=getexplicitregister32(R_EDX);
  1466. pushusedregisters(pushedreg,$ff
  1467. and not($80 shr byte(p^.location.registerlow))
  1468. and not($80 shr byte(p^.location.registerhigh)));
  1469. if cs_check_overflow in aktlocalswitches then
  1470. push_int(1)
  1471. else
  1472. push_int(0);
  1473. { the left operand is in hloc, because the
  1474. location of left is p^.location but p^.location
  1475. is already destroyed
  1476. }
  1477. emit_pushq_loc(hloc);
  1478. clear_location(hloc);
  1479. emit_pushq_loc(p^.right^.location);
  1480. if porddef(p^.resulttype)^.typ=u64bit then
  1481. emitcall('FPC_MUL_QWORD')
  1482. else
  1483. emitcall('FPC_MUL_INT64');
  1484. emit_reg_reg(A_MOV,S_L,R_EAX,p^.location.registerlow);
  1485. emit_reg_reg(A_MOV,S_L,R_EDX,p^.location.registerhigh);
  1486. popusedregisters(pushedreg);
  1487. p^.location.loc:=LOC_REGISTER;
  1488. end
  1489. else
  1490. begin
  1491. { left and right no register? }
  1492. { then one must be demanded }
  1493. if (p^.left^.location.loc<>LOC_REGISTER) and
  1494. (p^.right^.location.loc<>LOC_REGISTER) then
  1495. begin
  1496. { register variable ? }
  1497. if (p^.left^.location.loc=LOC_CREGISTER) then
  1498. begin
  1499. { it is OK if this is the destination }
  1500. if is_in_dest then
  1501. begin
  1502. hregister:=p^.location.registerlow;
  1503. hregister2:=p^.location.registerhigh;
  1504. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,
  1505. hregister);
  1506. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,
  1507. hregister2);
  1508. end
  1509. else
  1510. if cmpop then
  1511. begin
  1512. { do not disturb the register }
  1513. hregister:=p^.location.registerlow;
  1514. hregister2:=p^.location.registerhigh;
  1515. end
  1516. else
  1517. begin
  1518. hregister:=getregister32;
  1519. hregister2:=getregister32;
  1520. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,
  1521. hregister);
  1522. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,
  1523. hregister2);
  1524. end
  1525. end
  1526. else
  1527. begin
  1528. ungetiftemp(p^.left^.location.reference);
  1529. del_reference(p^.left^.location.reference);
  1530. if is_in_dest then
  1531. begin
  1532. hregister:=p^.location.registerlow;
  1533. hregister2:=p^.location.registerhigh;
  1534. emit_ref_reg(A_MOV,S_L,
  1535. newreference(p^.left^.location.reference),hregister);
  1536. hr:=newreference(p^.left^.location.reference);
  1537. inc(hr^.offset,4);
  1538. emit_ref_reg(A_MOV,S_L,
  1539. hr,hregister2);
  1540. end
  1541. else
  1542. begin
  1543. hregister:=getregister32;
  1544. hregister2:=getregister32;
  1545. emit_ref_reg(A_MOV,S_L,
  1546. newreference(p^.left^.location.reference),hregister);
  1547. hr:=newreference(p^.left^.location.reference);
  1548. inc(hr^.offset,4);
  1549. emit_ref_reg(A_MOV,S_L,
  1550. hr,hregister2);
  1551. end;
  1552. end;
  1553. clear_location(p^.location);
  1554. p^.location.loc:=LOC_REGISTER;
  1555. p^.location.registerlow:=hregister;
  1556. p^.location.registerhigh:=hregister2;
  1557. end
  1558. else
  1559. { if on the right the register then swap }
  1560. if not(noswap) and (p^.right^.location.loc=LOC_REGISTER) then
  1561. begin
  1562. swap_location(p^.location,p^.right^.location);
  1563. { newly swapped also set swapped flag }
  1564. p^.swaped:=not(p^.swaped);
  1565. end;
  1566. { at this point, p^.location.loc should be LOC_REGISTER }
  1567. { and p^.location.register should be a valid register }
  1568. { containing the left result }
  1569. if p^.right^.location.loc<>LOC_REGISTER then
  1570. begin
  1571. if (p^.treetype=subn) and p^.swaped then
  1572. begin
  1573. if p^.right^.location.loc=LOC_CREGISTER then
  1574. begin
  1575. {$ifndef noAllocEdi}
  1576. getexplicitregister32(R_EDI);
  1577. {$endif noAllocEdi}
  1578. emit_reg_reg(A_MOV,opsize,p^.right^.location.register,R_EDI);
  1579. emit_reg_reg(op,opsize,p^.location.register,R_EDI);
  1580. emit_reg_reg(A_MOV,opsize,R_EDI,p^.location.register);
  1581. {$ifndef noAllocEdi}
  1582. ungetregister32(R_EDI);
  1583. getexplicitregister32(R_EDI);
  1584. {$endif noAllocEdi}
  1585. emit_reg_reg(A_MOV,opsize,p^.right^.location.registerhigh,R_EDI);
  1586. { the carry flag is still ok }
  1587. emit_reg_reg(op2,opsize,p^.location.registerhigh,R_EDI);
  1588. emit_reg_reg(A_MOV,opsize,R_EDI,p^.location.registerhigh);
  1589. {$ifndef noAllocEdi}
  1590. ungetregister32(R_EDI);
  1591. {$endif noAllocEdi}
  1592. end
  1593. else
  1594. begin
  1595. {$ifndef noAllocEdi}
  1596. getexplicitregister32(R_EDI);
  1597. {$endif noAllocEdi}
  1598. emit_ref_reg(A_MOV,opsize,
  1599. newreference(p^.right^.location.reference),R_EDI);
  1600. emit_reg_reg(op,opsize,p^.location.registerlow,R_EDI);
  1601. emit_reg_reg(A_MOV,opsize,R_EDI,p^.location.registerlow);
  1602. {$ifndef noAllocEdi}
  1603. ungetregister32(R_EDI);
  1604. getexplicitregister32(R_EDI);
  1605. {$endif noAllocEdi}
  1606. hr:=newreference(p^.right^.location.reference);
  1607. inc(hr^.offset,4);
  1608. emit_ref_reg(A_MOV,opsize,
  1609. hr,R_EDI);
  1610. { here the carry flag is still preserved }
  1611. emit_reg_reg(op2,opsize,p^.location.registerhigh,R_EDI);
  1612. emit_reg_reg(A_MOV,opsize,R_EDI,
  1613. p^.location.registerhigh);
  1614. {$ifndef noAllocEdi}
  1615. ungetregister32(R_EDI);
  1616. {$endif noAllocEdi}
  1617. ungetiftemp(p^.right^.location.reference);
  1618. del_reference(p^.right^.location.reference);
  1619. end;
  1620. end
  1621. else if cmpop then
  1622. begin
  1623. if (p^.right^.location.loc=LOC_CREGISTER) then
  1624. begin
  1625. emit_reg_reg(A_CMP,S_L,p^.right^.location.registerhigh,
  1626. p^.location.registerhigh);
  1627. firstjmp64bitcmp;
  1628. emit_reg_reg(A_CMP,S_L,p^.right^.location.registerlow,
  1629. p^.location.registerlow);
  1630. secondjmp64bitcmp;
  1631. end
  1632. else
  1633. begin
  1634. hr:=newreference(p^.right^.location.reference);
  1635. inc(hr^.offset,4);
  1636. emit_ref_reg(A_CMP,S_L,
  1637. hr,p^.location.registerhigh);
  1638. firstjmp64bitcmp;
  1639. emit_ref_reg(A_CMP,S_L,newreference(
  1640. p^.right^.location.reference),p^.location.registerlow);
  1641. secondjmp64bitcmp;
  1642. emitjmp(C_None,falselabel);
  1643. ungetiftemp(p^.right^.location.reference);
  1644. del_reference(p^.right^.location.reference);
  1645. end;
  1646. end
  1647. else
  1648. begin
  1649. {
  1650. if (p^.right^.treetype=ordconstn) and
  1651. (op=A_CMP) and
  1652. (p^.right^.value=0) then
  1653. begin
  1654. emit_reg_reg(A_TEST,opsize,p^.location.register,
  1655. p^.location.register);
  1656. end
  1657. else if (p^.right^.treetype=ordconstn) and
  1658. (op=A_IMUL) and
  1659. (ispowerof2(p^.right^.value,power)) then
  1660. begin
  1661. emit_const_reg(A_SHL,opsize,power,
  1662. p^.location.register);
  1663. end
  1664. else
  1665. }
  1666. begin
  1667. if (p^.right^.location.loc=LOC_CREGISTER) then
  1668. begin
  1669. emit_reg_reg(op,S_L,p^.right^.location.registerlow,
  1670. p^.location.registerlow);
  1671. emit_reg_reg(op2,S_L,p^.right^.location.registerhigh,
  1672. p^.location.registerhigh);
  1673. end
  1674. else
  1675. begin
  1676. emit_ref_reg(op,S_L,newreference(
  1677. p^.right^.location.reference),p^.location.registerlow);
  1678. hr:=newreference(p^.right^.location.reference);
  1679. inc(hr^.offset,4);
  1680. emit_ref_reg(op2,S_L,
  1681. hr,p^.location.registerhigh);
  1682. ungetiftemp(p^.right^.location.reference);
  1683. del_reference(p^.right^.location.reference);
  1684. end;
  1685. end;
  1686. end;
  1687. end
  1688. else
  1689. begin
  1690. { when swapped another result register }
  1691. if (p^.treetype=subn) and p^.swaped then
  1692. begin
  1693. emit_reg_reg(op,S_L,
  1694. p^.location.registerlow,
  1695. p^.right^.location.registerlow);
  1696. emit_reg_reg(op2,S_L,
  1697. p^.location.registerhigh,
  1698. p^.right^.location.registerhigh);
  1699. swap_location(p^.location,p^.right^.location);
  1700. { newly swapped also set swapped flag }
  1701. { just to maintain ordering }
  1702. p^.swaped:=not(p^.swaped);
  1703. end
  1704. else if cmpop then
  1705. begin
  1706. emit_reg_reg(A_CMP,S_L,
  1707. p^.right^.location.registerhigh,
  1708. p^.location.registerhigh);
  1709. firstjmp64bitcmp;
  1710. emit_reg_reg(A_CMP,S_L,
  1711. p^.right^.location.registerlow,
  1712. p^.location.registerlow);
  1713. secondjmp64bitcmp;
  1714. end
  1715. else
  1716. begin
  1717. emit_reg_reg(op,S_L,
  1718. p^.right^.location.registerlow,
  1719. p^.location.registerlow);
  1720. emit_reg_reg(op2,S_L,
  1721. p^.right^.location.registerhigh,
  1722. p^.location.registerhigh);
  1723. end;
  1724. ungetregister32(p^.right^.location.registerlow);
  1725. ungetregister32(p^.right^.location.registerhigh);
  1726. end;
  1727. if cmpop then
  1728. begin
  1729. ungetregister32(p^.location.registerlow);
  1730. ungetregister32(p^.location.registerhigh);
  1731. end;
  1732. { only in case of overflow operations }
  1733. { produce overflow code }
  1734. { we must put it here directly, because sign of operation }
  1735. { is in unsigned VAR!! }
  1736. if mboverflow then
  1737. begin
  1738. if cs_check_overflow in aktlocalswitches then
  1739. begin
  1740. getlabel(hl4);
  1741. if unsigned then
  1742. emitjmp(C_NB,hl4)
  1743. else
  1744. emitjmp(C_NO,hl4);
  1745. emitcall('FPC_OVERFLOW');
  1746. emitlab(hl4);
  1747. end;
  1748. end;
  1749. { we have LOC_JUMP as result }
  1750. if cmpop then
  1751. begin
  1752. clear_location(p^.location);
  1753. p^.location.loc:=LOC_JUMP;
  1754. cmpop:=false;
  1755. end;
  1756. end;
  1757. end
  1758. else
  1759. { Floating point }
  1760. if (p^.left^.resulttype^.deftype=floatdef) and
  1761. (pfloatdef(p^.left^.resulttype)^.typ<>f32bit) then
  1762. begin
  1763. { real constants to the right, but only if it
  1764. isn't on the FPU stack, i.e. 1.0 or 0.0! }
  1765. if (p^.left^.treetype=realconstn) and
  1766. (p^.left^.location.loc<>LOC_FPU) then
  1767. swaptree(p);
  1768. cmpop:=false;
  1769. case p^.treetype of
  1770. addn : op:=A_FADDP;
  1771. muln : op:=A_FMULP;
  1772. subn : op:=A_FSUBP;
  1773. slashn : op:=A_FDIVP;
  1774. ltn,lten,gtn,gten,
  1775. equaln,unequaln : begin
  1776. op:=A_FCOMPP;
  1777. cmpop:=true;
  1778. end;
  1779. else CGMessage(type_e_mismatch);
  1780. end;
  1781. if (p^.right^.location.loc<>LOC_FPU) then
  1782. begin
  1783. if p^.right^.location.loc=LOC_CFPUREGISTER then
  1784. begin
  1785. emit_reg( A_FLD,S_NO,
  1786. correct_fpuregister(p^.right^.location.register,fpuvaroffset));
  1787. inc(fpuvaroffset);
  1788. end
  1789. else
  1790. floatload(pfloatdef(p^.right^.resulttype)^.typ,p^.right^.location.reference);
  1791. if (p^.left^.location.loc<>LOC_FPU) then
  1792. begin
  1793. if p^.left^.location.loc=LOC_CFPUREGISTER then
  1794. begin
  1795. emit_reg( A_FLD,S_NO,
  1796. correct_fpuregister(p^.left^.location.register,fpuvaroffset));
  1797. inc(fpuvaroffset);
  1798. end
  1799. else
  1800. floatload(pfloatdef(p^.left^.resulttype)^.typ,p^.left^.location.reference)
  1801. end
  1802. { left was on the stack => swap }
  1803. else
  1804. p^.swaped:=not(p^.swaped);
  1805. { releases the right reference }
  1806. del_reference(p^.right^.location.reference);
  1807. end
  1808. { the nominator in st0 }
  1809. else if (p^.left^.location.loc<>LOC_FPU) then
  1810. begin
  1811. if p^.left^.location.loc=LOC_CFPUREGISTER then
  1812. begin
  1813. emit_reg( A_FLD,S_NO,
  1814. correct_fpuregister(p^.left^.location.register,fpuvaroffset));
  1815. inc(fpuvaroffset);
  1816. end
  1817. else
  1818. floatload(pfloatdef(p^.left^.resulttype)^.typ,p^.left^.location.reference)
  1819. end
  1820. { fpu operands are always in the wrong order on the stack }
  1821. else
  1822. p^.swaped:=not(p^.swaped);
  1823. { releases the left reference }
  1824. if (p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  1825. del_reference(p^.left^.location.reference);
  1826. { if we swaped the tree nodes, then use the reverse operator }
  1827. if p^.swaped then
  1828. begin
  1829. if (p^.treetype=slashn) then
  1830. op:=A_FDIVRP
  1831. else if (p^.treetype=subn) then
  1832. op:=A_FSUBRP;
  1833. end;
  1834. { to avoid the pentium bug
  1835. if (op=FDIVP) and (opt_processors=pentium) then
  1836. emitcall('EMUL_FDIVP')
  1837. else
  1838. }
  1839. { the Intel assemblers want operands }
  1840. if op<>A_FCOMPP then
  1841. begin
  1842. emit_reg_reg(op,S_NO,R_ST,R_ST1);
  1843. dec(fpuvaroffset);
  1844. end
  1845. else
  1846. begin
  1847. emit_none(op,S_NO);
  1848. dec(fpuvaroffset,2);
  1849. end;
  1850. { on comparison load flags }
  1851. if cmpop then
  1852. begin
  1853. if not(R_EAX in unused) then
  1854. begin
  1855. {$ifndef noAllocEdi}
  1856. getexplicitregister32(R_EDI);
  1857. {$endif noAllocEdi}
  1858. emit_reg_reg(A_MOV,S_L,R_EAX,R_EDI);
  1859. end;
  1860. emit_reg(A_FNSTSW,S_NO,R_AX);
  1861. emit_none(A_SAHF,S_NO);
  1862. if not(R_EAX in unused) then
  1863. begin
  1864. emit_reg_reg(A_MOV,S_L,R_EDI,R_EAX);
  1865. {$ifndef noAllocEdi}
  1866. ungetregister32(R_EDI);
  1867. {$endif noAllocEdi}
  1868. end;
  1869. if p^.swaped then
  1870. begin
  1871. case p^.treetype of
  1872. equaln : flags:=F_E;
  1873. unequaln : flags:=F_NE;
  1874. ltn : flags:=F_A;
  1875. lten : flags:=F_AE;
  1876. gtn : flags:=F_B;
  1877. gten : flags:=F_BE;
  1878. end;
  1879. end
  1880. else
  1881. begin
  1882. case p^.treetype of
  1883. equaln : flags:=F_E;
  1884. unequaln : flags:=F_NE;
  1885. ltn : flags:=F_B;
  1886. lten : flags:=F_BE;
  1887. gtn : flags:=F_A;
  1888. gten : flags:=F_AE;
  1889. end;
  1890. end;
  1891. clear_location(p^.location);
  1892. p^.location.loc:=LOC_FLAGS;
  1893. p^.location.resflags:=flags;
  1894. cmpop:=false;
  1895. end
  1896. else
  1897. begin
  1898. clear_location(p^.location);
  1899. p^.location.loc:=LOC_FPU;
  1900. end;
  1901. end
  1902. {$ifdef SUPPORT_MMX}
  1903. else
  1904. { MMX Arrays }
  1905. if is_mmx_able_array(p^.left^.resulttype) then
  1906. begin
  1907. cmpop:=false;
  1908. mmxbase:=mmx_type(p^.left^.resulttype);
  1909. case p^.treetype of
  1910. addn : begin
  1911. if (cs_mmx_saturation in aktlocalswitches) then
  1912. begin
  1913. case mmxbase of
  1914. mmxs8bit:
  1915. op:=A_PADDSB;
  1916. mmxu8bit:
  1917. op:=A_PADDUSB;
  1918. mmxs16bit,mmxfixed16:
  1919. op:=A_PADDSB;
  1920. mmxu16bit:
  1921. op:=A_PADDUSW;
  1922. end;
  1923. end
  1924. else
  1925. begin
  1926. case mmxbase of
  1927. mmxs8bit,mmxu8bit:
  1928. op:=A_PADDB;
  1929. mmxs16bit,mmxu16bit,mmxfixed16:
  1930. op:=A_PADDW;
  1931. mmxs32bit,mmxu32bit:
  1932. op:=A_PADDD;
  1933. end;
  1934. end;
  1935. end;
  1936. muln : begin
  1937. case mmxbase of
  1938. mmxs16bit,mmxu16bit:
  1939. op:=A_PMULLW;
  1940. mmxfixed16:
  1941. op:=A_PMULHW;
  1942. end;
  1943. end;
  1944. subn : begin
  1945. if (cs_mmx_saturation in aktlocalswitches) then
  1946. begin
  1947. case mmxbase of
  1948. mmxs8bit:
  1949. op:=A_PSUBSB;
  1950. mmxu8bit:
  1951. op:=A_PSUBUSB;
  1952. mmxs16bit,mmxfixed16:
  1953. op:=A_PSUBSB;
  1954. mmxu16bit:
  1955. op:=A_PSUBUSW;
  1956. end;
  1957. end
  1958. else
  1959. begin
  1960. case mmxbase of
  1961. mmxs8bit,mmxu8bit:
  1962. op:=A_PSUBB;
  1963. mmxs16bit,mmxu16bit,mmxfixed16:
  1964. op:=A_PSUBW;
  1965. mmxs32bit,mmxu32bit:
  1966. op:=A_PSUBD;
  1967. end;
  1968. end;
  1969. end;
  1970. {
  1971. ltn,lten,gtn,gten,
  1972. equaln,unequaln :
  1973. begin
  1974. op:=A_CMP;
  1975. cmpop:=true;
  1976. end;
  1977. }
  1978. xorn:
  1979. op:=A_PXOR;
  1980. orn:
  1981. op:=A_POR;
  1982. andn:
  1983. op:=A_PAND;
  1984. else CGMessage(type_e_mismatch);
  1985. end;
  1986. { left and right no register? }
  1987. { then one must be demanded }
  1988. if (p^.left^.location.loc<>LOC_MMXREGISTER) and
  1989. (p^.right^.location.loc<>LOC_MMXREGISTER) then
  1990. begin
  1991. { register variable ? }
  1992. if (p^.left^.location.loc=LOC_CMMXREGISTER) then
  1993. begin
  1994. { it is OK if this is the destination }
  1995. if is_in_dest then
  1996. begin
  1997. hregister:=p^.location.register;
  1998. emit_reg_reg(A_MOVQ,S_NO,p^.left^.location.register,
  1999. hregister);
  2000. end
  2001. else
  2002. begin
  2003. hregister:=getregistermmx;
  2004. emit_reg_reg(A_MOVQ,S_NO,p^.left^.location.register,
  2005. hregister);
  2006. end
  2007. end
  2008. else
  2009. begin
  2010. del_reference(p^.left^.location.reference);
  2011. if is_in_dest then
  2012. begin
  2013. hregister:=p^.location.register;
  2014. emit_ref_reg(A_MOVQ,S_NO,
  2015. newreference(p^.left^.location.reference),hregister);
  2016. end
  2017. else
  2018. begin
  2019. hregister:=getregistermmx;
  2020. emit_ref_reg(A_MOVQ,S_NO,
  2021. newreference(p^.left^.location.reference),hregister);
  2022. end;
  2023. end;
  2024. clear_location(p^.location);
  2025. p^.location.loc:=LOC_MMXREGISTER;
  2026. p^.location.register:=hregister;
  2027. end
  2028. else
  2029. { if on the right the register then swap }
  2030. if (p^.right^.location.loc=LOC_MMXREGISTER) then
  2031. begin
  2032. swap_location(p^.location,p^.right^.location);
  2033. { newly swapped also set swapped flag }
  2034. p^.swaped:=not(p^.swaped);
  2035. end;
  2036. { at this point, p^.location.loc should be LOC_MMXREGISTER }
  2037. { and p^.location.register should be a valid register }
  2038. { containing the left result }
  2039. if p^.right^.location.loc<>LOC_MMXREGISTER then
  2040. begin
  2041. if (p^.treetype=subn) and p^.swaped then
  2042. begin
  2043. if p^.right^.location.loc=LOC_CMMXREGISTER then
  2044. begin
  2045. emit_reg_reg(A_MOVQ,S_NO,p^.right^.location.register,R_MM7);
  2046. emit_reg_reg(op,S_NO,p^.location.register,R_MM0);
  2047. emit_reg_reg(A_MOVQ,S_NO,R_MM7,p^.location.register);
  2048. end
  2049. else
  2050. begin
  2051. emit_ref_reg(A_MOVQ,S_NO,
  2052. newreference(p^.right^.location.reference),R_MM7);
  2053. emit_reg_reg(op,S_NO,p^.location.register,
  2054. R_MM7);
  2055. emit_reg_reg(A_MOVQ,S_NO,
  2056. R_MM7,p^.location.register);
  2057. del_reference(p^.right^.location.reference);
  2058. end;
  2059. end
  2060. else
  2061. begin
  2062. if (p^.right^.location.loc=LOC_CREGISTER) then
  2063. begin
  2064. emit_reg_reg(op,S_NO,p^.right^.location.register,
  2065. p^.location.register);
  2066. end
  2067. else
  2068. begin
  2069. emit_ref_reg(op,S_NO,newreference(
  2070. p^.right^.location.reference),p^.location.register);
  2071. del_reference(p^.right^.location.reference);
  2072. end;
  2073. end;
  2074. end
  2075. else
  2076. begin
  2077. { when swapped another result register }
  2078. if (p^.treetype=subn) and p^.swaped then
  2079. begin
  2080. emit_reg_reg(op,S_NO,
  2081. p^.location.register,p^.right^.location.register);
  2082. swap_location(p^.location,p^.right^.location);
  2083. { newly swapped also set swapped flag }
  2084. { just to maintain ordering }
  2085. p^.swaped:=not(p^.swaped);
  2086. end
  2087. else
  2088. begin
  2089. emit_reg_reg(op,S_NO,
  2090. p^.right^.location.register,
  2091. p^.location.register);
  2092. end;
  2093. ungetregistermmx(p^.right^.location.register);
  2094. end;
  2095. end
  2096. {$endif SUPPORT_MMX}
  2097. else CGMessage(type_e_mismatch);
  2098. end;
  2099. SetResultLocation(cmpop,unsigned,p);
  2100. end;
  2101. end.
  2102. {
  2103. $Log$
  2104. Revision 1.89 2000-01-13 16:52:47 jonas
  2105. * moved deallocation of registers used in reference that points to string after
  2106. copyshortstring (this routine doesn't require extra regs)
  2107. Revision 1.88 2000/01/09 19:44:53 florian
  2108. * bug in secondadd(subn) with swaped mmx operands fixed
  2109. Revision 1.87 2000/01/09 16:35:39 jonas
  2110. + comment about badly placed release_loc calls for a_mul which
  2111. causes wrong regdeallocations. Don't know how to fix :(
  2112. Revision 1.86 2000/01/09 12:34:59 jonas
  2113. * changed edi allocation to use getexplicitregister32/ungetregister
  2114. (adapted tgeni386 a bit for this) and enabled it by default
  2115. * fixed very big and stupid bug of mine in cg386mat that broke the
  2116. include() code (and make cycle :( ) if you compiled without
  2117. -dnewoptimizations
  2118. Revision 1.85 2000/01/09 01:44:18 jonas
  2119. + (de)allocation info for EDI to fix reported bug on mailinglist.
  2120. Also some (de)allocation info for ESI added. Between -dallocEDI
  2121. because at this time of the night bugs could easily slip in ;)
  2122. Revision 1.84 2000/01/07 01:14:20 peter
  2123. * updated copyright to 2000
  2124. Revision 1.83 1999/12/11 18:53:31 jonas
  2125. * fixed type conversions of results of operations with cardinals
  2126. (between -dcardinalmulfix)
  2127. Revision 1.82 1999/11/06 14:34:17 peter
  2128. * truncated log to 20 revs
  2129. Revision 1.81 1999/09/28 19:43:45 florian
  2130. * the maybe_push fix of Pierre wasn't 100%, the tree parameter
  2131. must contain a valid location (which is saved if necessary)
  2132. Revision 1.80 1999/09/26 13:26:01 florian
  2133. * exception patch of Romio nevertheless the excpetion handling
  2134. needs some corections regarding register saving
  2135. * gettempansistring is again a procedure
  2136. Revision 1.79 1999/09/21 20:53:21 florian
  2137. * fixed 1/s problem from mailing list
  2138. Revision 1.78 1999/09/07 07:52:19 peter
  2139. * > < >= <= support for boolean
  2140. * boolean constants are now calculated like integer constants
  2141. Revision 1.77 1999/08/30 12:00:45 pierre
  2142. * problem with maybe_push/restore solved hopefully
  2143. Revision 1.76 1999/08/23 23:31:00 pierre
  2144. * double del_location removed in add_set
  2145. Revision 1.75 1999/08/23 10:35:13 jonas
  2146. * fixed <= and >= for sets
  2147. Revision 1.74 1999/08/19 13:08:43 pierre
  2148. * emit_??? used
  2149. Revision 1.73 1999/08/07 11:29:26 peter
  2150. * better fix for muln register allocation
  2151. Revision 1.72 1999/08/04 13:45:17 florian
  2152. + floating point register variables !!
  2153. * pairegalloc is now generated for register variables
  2154. Revision 1.71 1999/08/04 00:22:40 florian
  2155. * renamed i386asm and i386base to cpuasm and cpubase
  2156. Revision 1.70 1999/08/03 22:02:31 peter
  2157. * moved bitmask constants to sets
  2158. * some other type/const renamings
  2159. Revision 1.69 1999/07/05 20:13:06 peter
  2160. * removed temp defines
  2161. Revision 1.68 1999/07/02 12:18:46 jonas
  2162. * released setconstnot (changed to $ifndef nosetconstnot)
  2163. * released shlmul (changed to $ifndef no shlmul)
  2164. Revision 1.67 1999/06/14 17:47:45 peter
  2165. * merged
  2166. Revision 1.66.2.1 1999/06/14 17:24:40 peter
  2167. * fixed saving of registers with decr_ansistr
  2168. Revision 1.66 1999/06/09 23:22:37 peter
  2169. + del_location
  2170. Revision 1.65 1999/06/09 23:00:11 peter
  2171. * small ansistring fixes
  2172. * val_ansistr_sint destsize changed to longint
  2173. * don't write low/hi ascii with -al
  2174. Revision 1.64 1999/06/02 10:11:39 florian
  2175. * make cycle fixed i.e. compilation with 0.99.10
  2176. * some fixes for qword
  2177. * start of register calling conventions
  2178. Revision 1.63 1999/05/31 20:35:45 peter
  2179. * ansistring fixes, decr_ansistr called after all temp ansi reuses
  2180. }