cg386add.pas 100 KB

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