n386add.pas 59 KB

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