cg386add.pas 107 KB

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