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