n386add.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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;
  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. procedure second_mul;
  40. {$ifdef SUPPORT_MMX}
  41. procedure second_addmmx;
  42. {$endif SUPPORT_MMX}
  43. procedure second_add64bit;
  44. end;
  45. implementation
  46. uses
  47. globtype,systems,
  48. cutils,verbose,globals,
  49. symconst,symdef,paramgr,
  50. aasmbase,aasmtai,aasmcpu,defutil,htypechk,
  51. cgbase,pass_2,regvars,
  52. ncon,nset,
  53. cga,ncgutil,tgobj,rgobj,cgobj,cg64f32,rgcpu;
  54. {*****************************************************************************
  55. Helpers
  56. *****************************************************************************}
  57. const
  58. opsize_2_cgsize : array[S_B..S_L] of tcgsize = (OS_8,OS_16,OS_32);
  59. procedure ti386addnode.pass_left_and_right(var pushedfpu:boolean);
  60. begin
  61. { calculate the operator which is more difficult }
  62. firstcomplex(self);
  63. { in case of constant put it to the left }
  64. if (left.nodetype=ordconstn) then
  65. swapleftright;
  66. secondpass(left);
  67. { are too few registers free? }
  68. if location.loc=LOC_FPUREGISTER then
  69. pushedfpu:=maybe_pushfpu(exprasmlist,right.registersfpu,left.location)
  70. else
  71. pushedfpu:=false;
  72. secondpass(right);
  73. end;
  74. function ti386addnode.getresflags(unsigned : boolean) : tresflags;
  75. begin
  76. case nodetype of
  77. equaln : getresflags:=F_E;
  78. unequaln : getresflags:=F_NE;
  79. else
  80. if not(unsigned) then
  81. begin
  82. if nf_swaped in flags then
  83. case nodetype of
  84. ltn : getresflags:=F_G;
  85. lten : getresflags:=F_GE;
  86. gtn : getresflags:=F_L;
  87. gten : getresflags:=F_LE;
  88. end
  89. else
  90. case nodetype of
  91. ltn : getresflags:=F_L;
  92. lten : getresflags:=F_LE;
  93. gtn : getresflags:=F_G;
  94. gten : getresflags:=F_GE;
  95. end;
  96. end
  97. else
  98. begin
  99. if nf_swaped in flags then
  100. case nodetype of
  101. ltn : getresflags:=F_A;
  102. lten : getresflags:=F_AE;
  103. gtn : getresflags:=F_B;
  104. gten : getresflags:=F_BE;
  105. end
  106. else
  107. case nodetype of
  108. ltn : getresflags:=F_B;
  109. lten : getresflags:=F_BE;
  110. gtn : getresflags:=F_A;
  111. gten : getresflags:=F_AE;
  112. end;
  113. end;
  114. end;
  115. end;
  116. procedure ti386addnode.left_must_be_reg(opsize:TOpSize;noswap:boolean);
  117. begin
  118. { left location is not a register? }
  119. if (left.location.loc<>LOC_REGISTER) then
  120. begin
  121. { if right is register then we can swap the locations }
  122. if (not noswap) and
  123. (right.location.loc=LOC_REGISTER) then
  124. begin
  125. location_swap(left.location,right.location);
  126. toggleflag(nf_swaped);
  127. end
  128. else
  129. begin
  130. { maybe we can reuse a constant register when the
  131. operation is a comparison that doesn't change the
  132. value of the register }
  133. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  134. end;
  135. end;
  136. end;
  137. procedure ti386addnode.emit_op_right_left(op:TAsmOp;opsize:TOpsize);
  138. begin
  139. { left must be a register }
  140. case right.location.loc of
  141. LOC_REGISTER,
  142. LOC_CREGISTER :
  143. exprasmlist.concat(taicpu.op_reg_reg(op,opsize,right.location.register,left.location.register));
  144. LOC_REFERENCE,
  145. LOC_CREFERENCE :
  146. exprasmlist.concat(taicpu.op_ref_reg(op,opsize,right.location.reference,left.location.register));
  147. LOC_CONSTANT :
  148. exprasmlist.concat(taicpu.op_const_reg(op,opsize,right.location.value,left.location.register));
  149. else
  150. internalerror(200203232);
  151. end;
  152. end;
  153. procedure ti386addnode.set_result_location(cmpop,unsigned:boolean);
  154. begin
  155. if cmpop then
  156. begin
  157. location_reset(location,LOC_FLAGS,OS_NO);
  158. location.resflags:=getresflags(unsigned);
  159. end
  160. else
  161. location_copy(location,left.location);
  162. end;
  163. procedure ti386addnode.emit_generic_code(op:TAsmOp;opsize:TOpSize;unsigned,extra_not,mboverflow:boolean);
  164. var
  165. power : longint;
  166. hl4 : tasmlabel;
  167. r : Tregister;
  168. begin
  169. { at this point, left.location.loc should be LOC_REGISTER }
  170. if right.location.loc=LOC_REGISTER then
  171. begin
  172. { right.location is a LOC_REGISTER }
  173. { when swapped another result register }
  174. if (nodetype=subn) and (nf_swaped in flags) then
  175. begin
  176. if extra_not then
  177. emit_reg(A_NOT,S_L,left.location.register);
  178. emit_reg_reg(op,opsize,left.location.register,right.location.register);
  179. { newly swapped also set swapped flag }
  180. location_swap(left.location,right.location);
  181. toggleflag(nf_swaped);
  182. end
  183. else
  184. begin
  185. if extra_not then
  186. emit_reg(A_NOT,S_L,right.location.register);
  187. if (op=A_ADD) or (op=A_OR) or (op=A_AND) or (op=A_XOR) or (op=A_IMUL) then
  188. location_swap(left.location,right.location);
  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. r:=rg.getregisterint(exprasmlist,OS_INT);
  200. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,r);
  201. emit_reg_reg(op,opsize,left.location.register,r);
  202. emit_reg_reg(A_MOV,opsize,r,left.location.register);
  203. rg.ungetregisterint(exprasmlist,r);
  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. r:=rg.getregisterint(exprasmlist,OS_INT);
  244. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,r);
  245. emit_reg(A_NOT,S_L,r);
  246. emit_reg_reg(A_AND,S_L,r,left.location.register);
  247. rg.ungetregisterint(exprasmlist,r);
  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. objectlibrary.getlabel(hl4);
  265. if unsigned then
  266. cg.a_jmp_flags(exprasmlist,F_AE,hl4)
  267. else
  268. cg.a_jmp_flags(exprasmlist,F_GE,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 in [ltn,lten,gtn,gten,equaln,unequaln]) and
  287. is_shortstring(left.resulttype.def) and
  288. not(((left.nodetype=stringconstn) and (str_length(left)=0)) or
  289. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  290. begin
  291. expectloc:=LOC_FLAGS;
  292. calcregisters(self,0,0,0);
  293. result := nil;
  294. exit;
  295. end;
  296. { otherwise, use the generic code }
  297. result := inherited first_addstring;
  298. end;
  299. procedure ti386addnode.second_addstring;
  300. var
  301. paraloc1,
  302. paraloc2 : tparalocation;
  303. hregister1,
  304. hregister2 : tregister;
  305. begin
  306. { string operations are not commutative }
  307. if nf_swaped in flags then
  308. swapleftright;
  309. case tstringdef(left.resulttype.def).string_typ of
  310. st_shortstring:
  311. begin
  312. case nodetype of
  313. ltn,lten,gtn,gten,equaln,unequaln :
  314. begin
  315. {$warning forced stdcall calling}
  316. paraloc1:=paramanager.getintparaloc(pocall_stdcall,1);
  317. paraloc2:=paramanager.getintparaloc(pocall_stdcall,2);
  318. { process parameters }
  319. secondpass(left);
  320. location_release(exprasmlist,left.location);
  321. if paraloc2.loc=LOC_REGISTER then
  322. begin
  323. hregister2:=rg.getaddressregister(exprasmlist);
  324. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,hregister2);
  325. end
  326. else
  327. begin
  328. paramanager.allocparaloc(exprasmlist,paraloc2);
  329. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraloc2);
  330. end;
  331. secondpass(right);
  332. location_release(exprasmlist,right.location);
  333. if paraloc1.loc=LOC_REGISTER then
  334. begin
  335. hregister1:=rg.getaddressregister(exprasmlist);
  336. cg.a_loadaddr_ref_reg(exprasmlist,right.location.reference,hregister1);
  337. end
  338. else
  339. begin
  340. paramanager.allocparaloc(exprasmlist,paraloc1);
  341. cg.a_paramaddr_ref(exprasmlist,right.location.reference,paraloc1);
  342. end;
  343. { push parameters }
  344. if paraloc1.loc=LOC_REGISTER then
  345. begin
  346. rg.ungetregisterint(exprasmlist,hregister2);
  347. paramanager.allocparaloc(exprasmlist,paraloc2);
  348. cg.a_param_reg(exprasmlist,OS_ADDR,hregister2,paraloc2);
  349. end;
  350. if paraloc2.loc=LOC_REGISTER then
  351. begin
  352. rg.ungetregisterint(exprasmlist,hregister1);
  353. paramanager.allocparaloc(exprasmlist,paraloc1);
  354. cg.a_param_reg(exprasmlist,OS_ADDR,hregister1,paraloc1);
  355. end;
  356. paramanager.freeparaloc(exprasmlist,paraloc1);
  357. paramanager.freeparaloc(exprasmlist,paraloc2);
  358. rg.allocexplicitregistersint(exprasmlist,paramanager.get_volatile_registers_int(pocall_default));
  359. cg.a_call_name(exprasmlist,'FPC_SHORTSTR_COMPARE');
  360. rg.deallocexplicitregistersint(exprasmlist,paramanager.get_volatile_registers_int(pocall_default));
  361. location_freetemp(exprasmlist,left.location);
  362. location_freetemp(exprasmlist,right.location);
  363. end;
  364. end;
  365. set_result_location(true,true);
  366. end;
  367. else
  368. { rest should be handled in first pass (JM) }
  369. internalerror(200108303);
  370. end;
  371. end;
  372. {*****************************************************************************
  373. AddBoolean
  374. *****************************************************************************}
  375. procedure ti386addnode.second_addboolean;
  376. var
  377. op : TAsmOp;
  378. opsize : TOpsize;
  379. cmpop,
  380. isjump : boolean;
  381. otl,ofl : tasmlabel;
  382. begin
  383. { calculate the operator which is more difficult }
  384. firstcomplex(self);
  385. cmpop:=false;
  386. if (torddef(left.resulttype.def).typ=bool8bit) or
  387. (torddef(right.resulttype.def).typ=bool8bit) then
  388. opsize:=S_B
  389. else
  390. if (torddef(left.resulttype.def).typ=bool16bit) or
  391. (torddef(right.resulttype.def).typ=bool16bit) then
  392. opsize:=S_W
  393. else
  394. opsize:=S_L;
  395. if (cs_full_boolean_eval in aktlocalswitches) or
  396. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  397. begin
  398. if left.nodetype in [ordconstn,realconstn] then
  399. swapleftright;
  400. isjump:=(left.location.loc=LOC_JUMP);
  401. if isjump then
  402. begin
  403. otl:=truelabel;
  404. objectlibrary.getlabel(truelabel);
  405. ofl:=falselabel;
  406. objectlibrary.getlabel(falselabel);
  407. end;
  408. secondpass(left);
  409. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  410. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  411. if isjump then
  412. begin
  413. truelabel:=otl;
  414. falselabel:=ofl;
  415. end;
  416. isjump:=(right.location.loc=LOC_JUMP);
  417. if isjump then
  418. begin
  419. otl:=truelabel;
  420. objectlibrary.getlabel(truelabel);
  421. ofl:=falselabel;
  422. objectlibrary.getlabel(falselabel);
  423. end;
  424. secondpass(right);
  425. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  426. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],false);
  427. if isjump then
  428. begin
  429. truelabel:=otl;
  430. falselabel:=ofl;
  431. end;
  432. { left must be a register }
  433. left_must_be_reg(opsize,false);
  434. { compare the }
  435. case nodetype of
  436. ltn,lten,gtn,gten,
  437. equaln,unequaln :
  438. begin
  439. op:=A_CMP;
  440. cmpop:=true;
  441. end;
  442. xorn :
  443. op:=A_XOR;
  444. orn :
  445. op:=A_OR;
  446. andn :
  447. op:=A_AND;
  448. else
  449. internalerror(200203247);
  450. end;
  451. emit_op_right_left(op,opsize);
  452. location_freetemp(exprasmlist,right.location);
  453. location_release(exprasmlist,right.location);
  454. if cmpop then
  455. begin
  456. location_freetemp(exprasmlist,left.location);
  457. location_release(exprasmlist,left.location);
  458. end;
  459. set_result_location(cmpop,true);
  460. end
  461. else
  462. begin
  463. case nodetype of
  464. andn,
  465. orn :
  466. begin
  467. location_reset(location,LOC_JUMP,OS_NO);
  468. case nodetype of
  469. andn :
  470. begin
  471. otl:=truelabel;
  472. objectlibrary.getlabel(truelabel);
  473. secondpass(left);
  474. maketojumpbool(exprasmlist,left,lr_load_regvars);
  475. cg.a_label(exprasmlist,truelabel);
  476. truelabel:=otl;
  477. end;
  478. orn :
  479. begin
  480. ofl:=falselabel;
  481. objectlibrary.getlabel(falselabel);
  482. secondpass(left);
  483. maketojumpbool(exprasmlist,left,lr_load_regvars);
  484. cg.a_label(exprasmlist,falselabel);
  485. falselabel:=ofl;
  486. end;
  487. else
  488. internalerror(2003042212);
  489. end;
  490. secondpass(right);
  491. maketojumpbool(exprasmlist,right,lr_load_regvars);
  492. end;
  493. else
  494. internalerror(2003042213);
  495. end;
  496. end;
  497. end;
  498. {*****************************************************************************
  499. AddFloat
  500. *****************************************************************************}
  501. procedure ti386addnode.second_addfloat;
  502. var
  503. op : TAsmOp;
  504. resflags : tresflags;
  505. pushedfpu,
  506. cmpop : boolean;
  507. r : Tregister;
  508. begin
  509. pass_left_and_right(pushedfpu);
  510. cmpop:=false;
  511. case nodetype of
  512. addn :
  513. op:=A_FADDP;
  514. muln :
  515. op:=A_FMULP;
  516. subn :
  517. op:=A_FSUBP;
  518. slashn :
  519. op:=A_FDIVP;
  520. ltn,lten,gtn,gten,
  521. equaln,unequaln :
  522. begin
  523. op:=A_FCOMPP;
  524. cmpop:=true;
  525. end;
  526. else
  527. internalerror(2003042214);
  528. end;
  529. if (right.location.loc<>LOC_FPUREGISTER) then
  530. begin
  531. cg.a_loadfpu_loc_reg(exprasmlist,right.location,NR_ST);
  532. if (right.location.loc <> LOC_CFPUREGISTER) and
  533. pushedfpu then
  534. location_freetemp(exprasmlist,left.location);
  535. if (left.location.loc<>LOC_FPUREGISTER) then
  536. begin
  537. cg.a_loadfpu_loc_reg(exprasmlist,left.location,NR_ST);
  538. if (left.location.loc <> LOC_CFPUREGISTER) and
  539. pushedfpu then
  540. location_freetemp(exprasmlist,left.location);
  541. end
  542. else
  543. begin
  544. { left was on the stack => swap }
  545. toggleflag(nf_swaped);
  546. end;
  547. { releases the right reference }
  548. location_release(exprasmlist,right.location);
  549. end
  550. { the nominator in st0 }
  551. else if (left.location.loc<>LOC_FPUREGISTER) then
  552. begin
  553. cg.a_loadfpu_loc_reg(exprasmlist,left.location,NR_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. { fpu operands are always in the wrong order on the stack }
  561. toggleflag(nf_swaped);
  562. end;
  563. { releases the left reference }
  564. if (left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  565. location_release(exprasmlist,left.location);
  566. { if we swaped the tree nodes, then use the reverse operator }
  567. if nf_swaped in flags then
  568. begin
  569. if (nodetype=slashn) then
  570. op:=A_FDIVRP
  571. else if (nodetype=subn) then
  572. op:=A_FSUBRP;
  573. end;
  574. { to avoid the pentium bug
  575. if (op=FDIVP) and (opt_processors=pentium) then
  576. cg.a_call_name(exprasmlist,'EMUL_FDIVP')
  577. else
  578. }
  579. { the Intel assemblers want operands }
  580. if op<>A_FCOMPP then
  581. begin
  582. emit_reg_reg(op,S_NO,NR_ST,NR_ST1);
  583. dec(trgcpu(rg).fpuvaroffset);
  584. end
  585. else
  586. begin
  587. emit_none(op,S_NO);
  588. dec(trgcpu(rg).fpuvaroffset,2);
  589. end;
  590. { on comparison load flags }
  591. if cmpop then
  592. begin
  593. r:=rg.getexplicitregisterint(exprasmlist,NR_AX);
  594. emit_reg(A_FNSTSW,S_NO,r);
  595. emit_none(A_SAHF,S_NO);
  596. rg.ungetregisterint(exprasmlist,r);
  597. if nf_swaped in flags then
  598. begin
  599. case nodetype of
  600. equaln : resflags:=F_E;
  601. unequaln : resflags:=F_NE;
  602. ltn : resflags:=F_A;
  603. lten : resflags:=F_AE;
  604. gtn : resflags:=F_B;
  605. gten : resflags:=F_BE;
  606. end;
  607. end
  608. else
  609. begin
  610. case nodetype of
  611. equaln : resflags:=F_E;
  612. unequaln : resflags:=F_NE;
  613. ltn : resflags:=F_B;
  614. lten : resflags:=F_BE;
  615. gtn : resflags:=F_A;
  616. gten : resflags:=F_AE;
  617. end;
  618. end;
  619. location_reset(location,LOC_FLAGS,OS_NO);
  620. location.resflags:=resflags;
  621. end
  622. else
  623. begin
  624. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  625. location.register:=NR_ST;
  626. end;
  627. end;
  628. {*****************************************************************************
  629. AddSmallSet
  630. *****************************************************************************}
  631. procedure ti386addnode.second_addsmallset;
  632. var
  633. opsize : TOpSize;
  634. op : TAsmOp;
  635. cmpop,
  636. pushedfpu,
  637. extra_not,
  638. noswap : boolean;
  639. begin
  640. pass_left_and_right(pushedfpu);
  641. { when a setdef is passed, it has to be a smallset }
  642. if ((left.resulttype.def.deftype=setdef) and
  643. (tsetdef(left.resulttype.def).settype<>smallset)) or
  644. ((right.resulttype.def.deftype=setdef) and
  645. (tsetdef(right.resulttype.def).settype<>smallset)) then
  646. internalerror(200203301);
  647. cmpop:=false;
  648. noswap:=false;
  649. extra_not:=false;
  650. opsize:=S_L;
  651. case nodetype of
  652. addn :
  653. begin
  654. { this is a really ugly hack!!!!!!!!!! }
  655. { this could be done later using EDI }
  656. { as it is done for subn }
  657. { instead of two registers!!!! }
  658. { adding elements is not commutative }
  659. if (nf_swaped in flags) and (left.nodetype=setelementn) then
  660. swapleftright;
  661. { are we adding set elements ? }
  662. if right.nodetype=setelementn then
  663. begin
  664. { no range support for smallsets! }
  665. if assigned(tsetelementnode(right).right) then
  666. internalerror(43244);
  667. { bts requires both elements to be registers }
  668. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  669. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],true);
  670. op:=A_BTS;
  671. noswap:=true;
  672. end
  673. else
  674. op:=A_OR;
  675. end;
  676. symdifn :
  677. op:=A_XOR;
  678. muln :
  679. op:=A_AND;
  680. subn :
  681. begin
  682. op:=A_AND;
  683. if (not(nf_swaped in flags)) and
  684. (right.location.loc=LOC_CONSTANT) then
  685. right.location.value := not(right.location.value)
  686. else if (nf_swaped in flags) and
  687. (left.location.loc=LOC_CONSTANT) then
  688. left.location.value := not(left.location.value)
  689. else
  690. extra_not:=true;
  691. end;
  692. equaln,
  693. unequaln :
  694. begin
  695. op:=A_CMP;
  696. cmpop:=true;
  697. end;
  698. lten,gten:
  699. begin
  700. If (not(nf_swaped in flags) and
  701. (nodetype = lten)) or
  702. ((nf_swaped in flags) and
  703. (nodetype = gten)) then
  704. swapleftright;
  705. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],true);
  706. emit_op_right_left(A_AND,opsize);
  707. op:=A_CMP;
  708. cmpop:=true;
  709. { warning: ugly hack, we need a JE so change the node to equaln }
  710. nodetype:=equaln;
  711. end;
  712. xorn :
  713. op:=A_XOR;
  714. orn :
  715. op:=A_OR;
  716. andn :
  717. op:=A_AND;
  718. else
  719. internalerror(2003042215);
  720. end;
  721. { left must be a register }
  722. left_must_be_reg(opsize,noswap);
  723. emit_generic_code(op,opsize,true,extra_not,false);
  724. location_freetemp(exprasmlist,right.location);
  725. location_release(exprasmlist,right.location);
  726. if cmpop then
  727. begin
  728. location_freetemp(exprasmlist,left.location);
  729. location_release(exprasmlist,left.location);
  730. end;
  731. set_result_location(cmpop,true);
  732. end;
  733. {*****************************************************************************
  734. Add64bit
  735. *****************************************************************************}
  736. procedure ti386addnode.second_add64bit;
  737. var
  738. op : TOpCG;
  739. op1,op2 : TAsmOp;
  740. opsize : TOpSize;
  741. hregister,
  742. hregister2 : tregister;
  743. href : treference;
  744. hl4 : tasmlabel;
  745. pushedfpu,
  746. mboverflow,
  747. cmpop,
  748. unsigned,delete:boolean;
  749. r:Tregister;
  750. procedure firstjmp64bitcmp;
  751. var
  752. oldnodetype : tnodetype;
  753. begin
  754. load_all_regvars(exprasmlist);
  755. { the jump the sequence is a little bit hairy }
  756. case nodetype of
  757. ltn,gtn:
  758. begin
  759. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),truelabel);
  760. { cheat a little bit for the negative test }
  761. toggleflag(nf_swaped);
  762. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),falselabel);
  763. toggleflag(nf_swaped);
  764. end;
  765. lten,gten:
  766. begin
  767. oldnodetype:=nodetype;
  768. if nodetype=lten then
  769. nodetype:=ltn
  770. else
  771. nodetype:=gtn;
  772. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),truelabel);
  773. { cheat for the negative test }
  774. if nodetype=ltn then
  775. nodetype:=gtn
  776. else
  777. nodetype:=ltn;
  778. cg.a_jmp_flags(exprasmlist,getresflags(unsigned),falselabel);
  779. nodetype:=oldnodetype;
  780. end;
  781. equaln:
  782. cg.a_jmp_flags(exprasmlist,F_NE,falselabel);
  783. unequaln:
  784. cg.a_jmp_flags(exprasmlist,F_NE,truelabel);
  785. end;
  786. end;
  787. procedure secondjmp64bitcmp;
  788. begin
  789. { the jump the sequence is a little bit hairy }
  790. case nodetype of
  791. ltn,gtn,lten,gten:
  792. begin
  793. { the comparisaion of the low dword have to be }
  794. { always unsigned! }
  795. cg.a_jmp_flags(exprasmlist,getresflags(true),truelabel);
  796. cg.a_jmp_always(exprasmlist,falselabel);
  797. end;
  798. equaln:
  799. begin
  800. cg.a_jmp_flags(exprasmlist,F_NE,falselabel);
  801. cg.a_jmp_always(exprasmlist,truelabel);
  802. end;
  803. unequaln:
  804. begin
  805. cg.a_jmp_flags(exprasmlist,F_NE,truelabel);
  806. cg.a_jmp_always(exprasmlist,falselabel);
  807. end;
  808. end;
  809. end;
  810. begin
  811. firstcomplex(self);
  812. pass_left_and_right(pushedfpu);
  813. op1:=A_NONE;
  814. op2:=A_NONE;
  815. mboverflow:=false;
  816. cmpop:=false;
  817. opsize:=S_L;
  818. unsigned:=((left.resulttype.def.deftype=orddef) and
  819. (torddef(left.resulttype.def).typ=u64bit)) or
  820. ((right.resulttype.def.deftype=orddef) and
  821. (torddef(right.resulttype.def).typ=u64bit));
  822. case nodetype of
  823. addn :
  824. begin
  825. op:=OP_ADD;
  826. mboverflow:=true;
  827. end;
  828. subn :
  829. begin
  830. op:=OP_SUB;
  831. op1:=A_SUB;
  832. op2:=A_SBB;
  833. mboverflow:=true;
  834. end;
  835. ltn,lten,
  836. gtn,gten,
  837. equaln,unequaln:
  838. begin
  839. op:=OP_NONE;
  840. cmpop:=true;
  841. end;
  842. xorn:
  843. op:=OP_XOR;
  844. orn:
  845. op:=OP_OR;
  846. andn:
  847. op:=OP_AND;
  848. else
  849. begin
  850. { everything should be handled in pass_1 (JM) }
  851. internalerror(200109051);
  852. end;
  853. end;
  854. { left and right no register? }
  855. { then one must be demanded }
  856. if (left.location.loc<>LOC_REGISTER) then
  857. begin
  858. if (right.location.loc<>LOC_REGISTER) then
  859. begin
  860. { we can reuse a CREGISTER for comparison }
  861. if not((left.location.loc=LOC_CREGISTER) and cmpop) then
  862. begin
  863. delete:=left.location.loc<>LOC_CREGISTER;
  864. hregister:=rg.getregisterint(exprasmlist,OS_INT);
  865. hregister2:=rg.getregisterint(exprasmlist,OS_INT);
  866. cg64.a_load64_loc_reg(exprasmlist,left.location,joinreg64(hregister,hregister2),delete);
  867. location_reset(left.location,LOC_REGISTER,OS_64);
  868. left.location.registerlow:=hregister;
  869. left.location.registerhigh:=hregister2;
  870. end;
  871. end
  872. else
  873. begin
  874. location_swap(left.location,right.location);
  875. toggleflag(nf_swaped);
  876. end;
  877. end;
  878. { at this point, left.location.loc should be LOC_REGISTER }
  879. if right.location.loc=LOC_REGISTER then
  880. begin
  881. { when swapped another result register }
  882. if (nodetype=subn) and (nf_swaped in flags) then
  883. begin
  884. cg64.a_op64_reg_reg(exprasmlist,op,
  885. left.location.register64,
  886. right.location.register64);
  887. location_swap(left.location,right.location);
  888. toggleflag(nf_swaped);
  889. end
  890. else if cmpop then
  891. begin
  892. emit_reg_reg(A_CMP,S_L,right.location.registerhigh,left.location.registerhigh);
  893. firstjmp64bitcmp;
  894. emit_reg_reg(A_CMP,S_L,right.location.registerlow,left.location.registerlow);
  895. secondjmp64bitcmp;
  896. end
  897. else
  898. begin
  899. cg64.a_op64_reg_reg(exprasmlist,op,
  900. right.location.register64,
  901. left.location.register64);
  902. end;
  903. location_release(exprasmlist,right.location);
  904. end
  905. else
  906. begin
  907. { right.location<>LOC_REGISTER }
  908. if (nodetype=subn) and (nf_swaped in flags) then
  909. begin
  910. r:=rg.getregisterint(exprasmlist,OS_INT);
  911. cg64.a_load64low_loc_reg(exprasmlist,right.location,r);
  912. emit_reg_reg(op1,opsize,left.location.registerlow,r);
  913. emit_reg_reg(A_MOV,opsize,r,left.location.registerlow);
  914. cg64.a_load64high_loc_reg(exprasmlist,right.location,r);
  915. { the carry flag is still ok }
  916. emit_reg_reg(op2,opsize,left.location.registerhigh,r);
  917. emit_reg_reg(A_MOV,opsize,r,left.location.registerhigh);
  918. rg.ungetregisterint(exprasmlist,r);
  919. if right.location.loc<>LOC_CREGISTER then
  920. begin
  921. location_freetemp(exprasmlist,right.location);
  922. location_release(exprasmlist,right.location);
  923. end;
  924. end
  925. else if cmpop then
  926. begin
  927. case right.location.loc of
  928. LOC_CREGISTER :
  929. begin
  930. emit_reg_reg(A_CMP,S_L,right.location.registerhigh,left.location.registerhigh);
  931. firstjmp64bitcmp;
  932. emit_reg_reg(A_CMP,S_L,right.location.registerlow,left.location.registerlow);
  933. secondjmp64bitcmp;
  934. end;
  935. LOC_CREFERENCE,
  936. LOC_REFERENCE :
  937. begin
  938. href:=right.location.reference;
  939. inc(href.offset,4);
  940. emit_ref_reg(A_CMP,S_L,href,left.location.registerhigh);
  941. firstjmp64bitcmp;
  942. emit_ref_reg(A_CMP,S_L,right.location.reference,left.location.registerlow);
  943. secondjmp64bitcmp;
  944. cg.a_jmp_always(exprasmlist,falselabel);
  945. location_freetemp(exprasmlist,right.location);
  946. location_release(exprasmlist,right.location);
  947. end;
  948. LOC_CONSTANT :
  949. begin
  950. exprasmlist.concat(taicpu.op_const_reg(A_CMP,S_L,hi(right.location.valueqword),left.location.registerhigh));
  951. firstjmp64bitcmp;
  952. exprasmlist.concat(taicpu.op_const_reg(A_CMP,S_L,lo(right.location.valueqword),left.location.registerlow));
  953. secondjmp64bitcmp;
  954. end;
  955. else
  956. internalerror(200203282);
  957. end;
  958. end
  959. else
  960. begin
  961. cg64.a_op64_loc_reg(exprasmlist,op,right.location,
  962. left.location.register64);
  963. if (right.location.loc<>LOC_CREGISTER) then
  964. begin
  965. location_freetemp(exprasmlist,right.location);
  966. location_release(exprasmlist,right.location);
  967. end;
  968. end;
  969. end;
  970. if (left.location.loc<>LOC_CREGISTER) and cmpop then
  971. begin
  972. location_freetemp(exprasmlist,left.location);
  973. location_release(exprasmlist,left.location);
  974. end;
  975. { only in case of overflow operations }
  976. { produce overflow code }
  977. { we must put it here directly, because sign of operation }
  978. { is in unsigned VAR!! }
  979. if mboverflow then
  980. begin
  981. if cs_check_overflow in aktlocalswitches then
  982. begin
  983. objectlibrary.getlabel(hl4);
  984. if unsigned then
  985. cg.a_jmp_flags(exprasmlist,F_AE,hl4)
  986. else
  987. cg.a_jmp_flags(exprasmlist,F_GE,hl4);
  988. cg.a_call_name(exprasmlist,'FPC_OVERFLOW');
  989. cg.a_label(exprasmlist,hl4);
  990. end;
  991. end;
  992. { we have LOC_JUMP as result }
  993. if cmpop then
  994. location_reset(location,LOC_JUMP,OS_NO)
  995. else
  996. location_copy(location,left.location);
  997. end;
  998. {*****************************************************************************
  999. AddMMX
  1000. *****************************************************************************}
  1001. {$ifdef SUPPORT_MMX}
  1002. procedure ti386addnode.second_addmmx;
  1003. var
  1004. op : TAsmOp;
  1005. pushedfpu,
  1006. cmpop : boolean;
  1007. mmxbase : tmmxtype;
  1008. r,hregister : tregister;
  1009. begin
  1010. pass_left_and_right(pushedfpu);
  1011. cmpop:=false;
  1012. mmxbase:=mmx_type(left.resulttype.def);
  1013. case nodetype of
  1014. addn :
  1015. begin
  1016. if (cs_mmx_saturation in aktlocalswitches) then
  1017. begin
  1018. case mmxbase of
  1019. mmxs8bit:
  1020. op:=A_PADDSB;
  1021. mmxu8bit:
  1022. op:=A_PADDUSB;
  1023. mmxs16bit,mmxfixed16:
  1024. op:=A_PADDSB;
  1025. mmxu16bit:
  1026. op:=A_PADDUSW;
  1027. end;
  1028. end
  1029. else
  1030. begin
  1031. case mmxbase of
  1032. mmxs8bit,mmxu8bit:
  1033. op:=A_PADDB;
  1034. mmxs16bit,mmxu16bit,mmxfixed16:
  1035. op:=A_PADDW;
  1036. mmxs32bit,mmxu32bit:
  1037. op:=A_PADDD;
  1038. end;
  1039. end;
  1040. end;
  1041. muln :
  1042. begin
  1043. case mmxbase of
  1044. mmxs16bit,mmxu16bit:
  1045. op:=A_PMULLW;
  1046. mmxfixed16:
  1047. op:=A_PMULHW;
  1048. end;
  1049. end;
  1050. subn :
  1051. begin
  1052. if (cs_mmx_saturation in aktlocalswitches) then
  1053. begin
  1054. case mmxbase of
  1055. mmxs8bit:
  1056. op:=A_PSUBSB;
  1057. mmxu8bit:
  1058. op:=A_PSUBUSB;
  1059. mmxs16bit,mmxfixed16:
  1060. op:=A_PSUBSB;
  1061. mmxu16bit:
  1062. op:=A_PSUBUSW;
  1063. end;
  1064. end
  1065. else
  1066. begin
  1067. case mmxbase of
  1068. mmxs8bit,mmxu8bit:
  1069. op:=A_PSUBB;
  1070. mmxs16bit,mmxu16bit,mmxfixed16:
  1071. op:=A_PSUBW;
  1072. mmxs32bit,mmxu32bit:
  1073. op:=A_PSUBD;
  1074. end;
  1075. end;
  1076. end;
  1077. xorn:
  1078. op:=A_PXOR;
  1079. orn:
  1080. op:=A_POR;
  1081. andn:
  1082. op:=A_PAND;
  1083. else
  1084. internalerror(2003042214);
  1085. end;
  1086. { left and right no register? }
  1087. { then one must be demanded }
  1088. if (left.location.loc<>LOC_MMXREGISTER) then
  1089. begin
  1090. if (right.location.loc=LOC_MMXREGISTER) then
  1091. begin
  1092. location_swap(left.location,right.location);
  1093. toggleflag(nf_swaped);
  1094. end
  1095. else
  1096. begin
  1097. { register variable ? }
  1098. if (left.location.loc=LOC_CMMXREGISTER) then
  1099. begin
  1100. hregister:=rg.getregistermm(exprasmlist);
  1101. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  1102. end
  1103. else
  1104. begin
  1105. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1106. internalerror(200203245);
  1107. location_release(exprasmlist,left.location);
  1108. hregister:=rg.getregistermm(exprasmlist);
  1109. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  1110. end;
  1111. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  1112. left.location.register:=hregister;
  1113. end;
  1114. end;
  1115. { at this point, left.location.loc should be LOC_MMXREGISTER }
  1116. if right.location.loc<>LOC_MMXREGISTER then
  1117. begin
  1118. if (nodetype=subn) and (nf_swaped in flags) then
  1119. begin
  1120. if right.location.loc=LOC_CMMXREGISTER then
  1121. begin
  1122. emit_reg_reg(A_MOVQ,S_NO,right.location.register,NR_MM7);
  1123. emit_reg_reg(op,S_NO,left.location.register,NR_MM7);
  1124. emit_reg_reg(A_MOVQ,S_NO,NR_MM7,left.location.register);
  1125. end
  1126. else
  1127. begin
  1128. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1129. internalerror(200203247);
  1130. emit_ref_reg(A_MOVQ,S_NO,right.location.reference,NR_MM7);
  1131. emit_reg_reg(op,S_NO,left.location.register,NR_MM7);
  1132. emit_reg_reg(A_MOVQ,S_NO,NR_MM7,left.location.register);
  1133. location_release(exprasmlist,right.location);
  1134. end;
  1135. end
  1136. else
  1137. begin
  1138. if (right.location.loc=LOC_CMMXREGISTER) then
  1139. emit_reg_reg(op,S_NO,right.location.register,left.location.register)
  1140. else
  1141. begin
  1142. if not(right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1143. internalerror(200203246);
  1144. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  1145. location_release(exprasmlist,right.location);
  1146. end;
  1147. end;
  1148. end
  1149. else
  1150. begin
  1151. { right.location=LOC_MMXREGISTER }
  1152. if (nodetype=subn) and (nf_swaped in flags) then
  1153. begin
  1154. emit_reg_reg(op,S_NO,left.location.register,right.location.register);
  1155. location_swap(left.location,right.location);
  1156. toggleflag(nf_swaped);
  1157. end
  1158. else
  1159. begin
  1160. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  1161. end;
  1162. end;
  1163. location_freetemp(exprasmlist,right.location);
  1164. location_release(exprasmlist,right.location);
  1165. if cmpop then
  1166. begin
  1167. location_freetemp(exprasmlist,left.location);
  1168. location_release(exprasmlist,left.location);
  1169. end;
  1170. set_result_location(cmpop,true);
  1171. end;
  1172. {$endif SUPPORT_MMX}
  1173. {*****************************************************************************
  1174. MUL
  1175. *****************************************************************************}
  1176. procedure ti386addnode.second_mul;
  1177. var r:Tregister;
  1178. begin
  1179. {The location.register will be filled in later (JM)}
  1180. location_reset(location,LOC_REGISTER,OS_INT);
  1181. {Get a temp register and load the left value into it
  1182. and free the location.}
  1183. r:=rg.getregisterint(exprasmlist,OS_INT);
  1184. cg.a_load_loc_reg(exprasmlist,OS_INT,left.location,r);
  1185. location_release(exprasmlist,left.location);
  1186. {Allocate EAX.}
  1187. rg.getexplicitregisterint(exprasmlist,NR_EAX);
  1188. {Load the right value.}
  1189. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,NR_EAX);
  1190. location_release(exprasmlist,right.location);
  1191. {The mul instruction frees register r.}
  1192. rg.ungetregisterint(exprasmlist,r);
  1193. {Also allocate EDX, since it is also modified by a mul (JM).}
  1194. rg.getexplicitregisterint(exprasmlist,NR_EDX);
  1195. emit_reg(A_MUL,S_L,r);
  1196. {Free EDX}
  1197. rg.ungetregisterint(exprasmlist,NR_EDX);
  1198. {Free EAX}
  1199. rg.ungetregisterint(exprasmlist,NR_EAX);
  1200. {Allocate a new register and store the result in EAX in it.}
  1201. location.register:=rg.getregisterint(exprasmlist,OS_INT);
  1202. emit_reg_reg(A_MOV,S_L,NR_EAX,location.register);
  1203. location_freetemp(exprasmlist,left.location);
  1204. location_freetemp(exprasmlist,right.location);
  1205. end;
  1206. {*****************************************************************************
  1207. pass_2
  1208. *****************************************************************************}
  1209. procedure ti386addnode.pass_2;
  1210. { is also being used for xor, and "mul", "sub, or and comparative }
  1211. { operators }
  1212. var
  1213. pushedfpu,
  1214. mboverflow,cmpop : boolean;
  1215. op : tasmop;
  1216. opsize : topsize;
  1217. { true, if unsigned types are compared }
  1218. unsigned : boolean;
  1219. { is_in_dest if the result is put directly into }
  1220. { the resulting refernce or varregister }
  1221. {is_in_dest : boolean;}
  1222. { true, if for sets subtractions the extra not should generated }
  1223. extra_not : boolean;
  1224. begin
  1225. { to make it more readable, string and set (not smallset!) have their
  1226. own procedures }
  1227. case left.resulttype.def.deftype of
  1228. orddef :
  1229. begin
  1230. { handling boolean expressions }
  1231. if is_boolean(left.resulttype.def) and
  1232. is_boolean(right.resulttype.def) then
  1233. begin
  1234. second_addboolean;
  1235. exit;
  1236. end
  1237. { 64bit operations }
  1238. else if is_64bit(left.resulttype.def) then
  1239. begin
  1240. second_add64bit;
  1241. exit;
  1242. end;
  1243. end;
  1244. stringdef :
  1245. begin
  1246. second_addstring;
  1247. exit;
  1248. end;
  1249. setdef :
  1250. begin
  1251. { normalsets are already handled in pass1 }
  1252. if (tsetdef(left.resulttype.def).settype<>smallset) then
  1253. internalerror(200109041);
  1254. second_addsmallset;
  1255. exit;
  1256. end;
  1257. arraydef :
  1258. begin
  1259. {$ifdef SUPPORT_MMX}
  1260. if is_mmx_able_array(left.resulttype.def) then
  1261. begin
  1262. second_addmmx;
  1263. exit;
  1264. end;
  1265. {$endif SUPPORT_MMX}
  1266. end;
  1267. floatdef :
  1268. begin
  1269. second_addfloat;
  1270. exit;
  1271. end;
  1272. end;
  1273. { defaults }
  1274. {is_in_dest:=false;}
  1275. extra_not:=false;
  1276. mboverflow:=false;
  1277. cmpop:=false;
  1278. unsigned:=not(is_signed(left.resulttype.def)) or
  1279. not(is_signed(right.resulttype.def));
  1280. opsize:=def_opsize(left.resulttype.def);
  1281. pass_left_and_right(pushedfpu);
  1282. if (left.resulttype.def.deftype=pointerdef) or
  1283. (right.resulttype.def.deftype=pointerdef) or
  1284. (is_class_or_interface(right.resulttype.def) and is_class_or_interface(left.resulttype.def)) or
  1285. (left.resulttype.def.deftype=classrefdef) or
  1286. (left.resulttype.def.deftype=procvardef) or
  1287. ((left.resulttype.def.deftype=enumdef) and
  1288. (left.resulttype.def.size=4)) or
  1289. ((left.resulttype.def.deftype=orddef) and
  1290. (torddef(left.resulttype.def).typ in [s32bit,u32bit])) or
  1291. ((right.resulttype.def.deftype=orddef) and
  1292. (torddef(right.resulttype.def).typ in [s32bit,u32bit])) then
  1293. begin
  1294. case nodetype of
  1295. addn :
  1296. begin
  1297. op:=A_ADD;
  1298. mboverflow:=true;
  1299. end;
  1300. muln :
  1301. begin
  1302. if unsigned then
  1303. op:=A_MUL
  1304. else
  1305. op:=A_IMUL;
  1306. mboverflow:=true;
  1307. end;
  1308. subn :
  1309. begin
  1310. op:=A_SUB;
  1311. mboverflow:=true;
  1312. end;
  1313. ltn,lten,
  1314. gtn,gten,
  1315. equaln,unequaln :
  1316. begin
  1317. op:=A_CMP;
  1318. cmpop:=true;
  1319. end;
  1320. xorn :
  1321. op:=A_XOR;
  1322. orn :
  1323. op:=A_OR;
  1324. andn :
  1325. op:=A_AND;
  1326. else
  1327. internalerror(200304229);
  1328. end;
  1329. { filter MUL, which requires special handling }
  1330. if op=A_MUL then
  1331. begin
  1332. second_mul;
  1333. exit;
  1334. end;
  1335. { Convert flags to register first }
  1336. if (left.location.loc=LOC_FLAGS) then
  1337. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  1338. if (right.location.loc=LOC_FLAGS) then
  1339. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],false);
  1340. left_must_be_reg(opsize,false);
  1341. emit_generic_code(op,opsize,unsigned,extra_not,mboverflow);
  1342. location_freetemp(exprasmlist,right.location);
  1343. location_release(exprasmlist,right.location);
  1344. if cmpop and
  1345. (left.location.loc<>LOC_CREGISTER) then
  1346. begin
  1347. location_freetemp(exprasmlist,left.location);
  1348. location_release(exprasmlist,left.location);
  1349. end;
  1350. set_result_location(cmpop,unsigned);
  1351. end
  1352. { 8/16 bit enum,char,wchar types }
  1353. else
  1354. if ((left.resulttype.def.deftype=orddef) and
  1355. (torddef(left.resulttype.def).typ in [uchar,uwidechar])) or
  1356. ((left.resulttype.def.deftype=enumdef) and
  1357. ((left.resulttype.def.size=1) or
  1358. (left.resulttype.def.size=2))) then
  1359. begin
  1360. case nodetype of
  1361. ltn,lten,gtn,gten,
  1362. equaln,unequaln :
  1363. cmpop:=true;
  1364. else
  1365. internalerror(2003042210);
  1366. end;
  1367. left_must_be_reg(opsize,false);
  1368. emit_op_right_left(A_CMP,opsize);
  1369. location_freetemp(exprasmlist,right.location);
  1370. location_release(exprasmlist,right.location);
  1371. if left.location.loc<>LOC_CREGISTER then
  1372. begin
  1373. location_freetemp(exprasmlist,left.location);
  1374. location_release(exprasmlist,left.location);
  1375. end;
  1376. set_result_location(true,true);
  1377. end
  1378. else
  1379. internalerror(2003042211);
  1380. end;
  1381. begin
  1382. caddnode:=ti386addnode;
  1383. end.
  1384. {
  1385. $Log$
  1386. Revision 1.80 2003-10-01 20:34:49 peter
  1387. * procinfo unit contains tprocinfo
  1388. * cginfo renamed to cgbase
  1389. * moved cgmessage to verbose
  1390. * fixed ppc and sparc compiles
  1391. Revision 1.79 2003/09/28 21:48:20 peter
  1392. * fix register leaks
  1393. Revision 1.78 2003/09/28 13:35:40 peter
  1394. * shortstr compare updated for different calling conventions
  1395. Revision 1.77 2003/09/10 08:31:48 marco
  1396. * Patch from Peter for paraloc
  1397. Revision 1.76 2003/09/03 15:55:01 peter
  1398. * NEWRA branch merged
  1399. Revision 1.75.2.2 2003/08/31 13:50:16 daniel
  1400. * Remove sorting and use pregenerated indexes
  1401. * Some work on making things compile
  1402. Revision 1.75.2.1 2003/08/29 17:29:00 peter
  1403. * next batch of updates
  1404. Revision 1.75 2003/08/03 20:38:00 daniel
  1405. * Made code generator reverse or/add/and/xor/imul instructions when
  1406. possible to reduce the slowdown of spills.
  1407. Revision 1.74 2003/08/03 20:19:43 daniel
  1408. - Removed cmpop from Ti386addnode.second_addstring
  1409. Revision 1.73 2003/07/06 15:31:21 daniel
  1410. * Fixed register allocator. *Lots* of fixes.
  1411. Revision 1.72 2003/06/17 16:51:30 peter
  1412. * cycle fixes
  1413. Revision 1.71 2003/06/07 18:57:04 jonas
  1414. + added freeintparaloc
  1415. * ppc get/freeintparaloc now check whether the parameter regs are
  1416. properly allocated/deallocated (and get an extra list para)
  1417. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1418. * fixed lot of missing pi_do_call's
  1419. Revision 1.70 2003/06/03 13:01:59 daniel
  1420. * Register allocator finished
  1421. Revision 1.69 2003/05/30 23:49:18 jonas
  1422. * a_load_loc_reg now has an extra size parameter for the destination
  1423. register (properly fixes what I worked around in revision 1.106 of
  1424. ncgutil.pas)
  1425. Revision 1.68 2003/05/26 19:38:28 peter
  1426. * generic fpc_shorstr_concat
  1427. + fpc_shortstr_append_shortstr optimization
  1428. Revision 1.67 2003/05/22 21:32:29 peter
  1429. * removed some unit dependencies
  1430. Revision 1.66 2003/04/26 09:12:55 peter
  1431. * add string returns in LOC_REFERENCE
  1432. Revision 1.65 2003/04/23 20:16:04 peter
  1433. + added currency support based on int64
  1434. + is_64bit for use in cg units instead of is_64bitint
  1435. * removed cgmessage from n386add, replace with internalerrors
  1436. Revision 1.64 2003/04/23 09:51:16 daniel
  1437. * Removed usage of edi in a lot of places when new register allocator used
  1438. + Added newra versions of g_concatcopy and secondadd_float
  1439. Revision 1.63 2003/04/22 23:50:23 peter
  1440. * firstpass uses expectloc
  1441. * checks if there are differences between the expectloc and
  1442. location.loc from secondpass in EXTDEBUG
  1443. Revision 1.62 2003/04/22 10:09:35 daniel
  1444. + Implemented the actual register allocator
  1445. + Scratch registers unavailable when new register allocator used
  1446. + maybe_save/maybe_restore unavailable when new register allocator used
  1447. Revision 1.61 2003/04/17 10:02:48 daniel
  1448. * Tweaked register allocate/deallocate positition to less interferences
  1449. are generated.
  1450. Revision 1.60 2003/03/28 19:16:57 peter
  1451. * generic constructor working for i386
  1452. * remove fixed self register
  1453. * esi added as address register for i386
  1454. Revision 1.59 2003/03/13 19:52:23 jonas
  1455. * and more new register allocator fixes (in the i386 code generator this
  1456. time). At least now the ppc cross compiler can compile the linux
  1457. system unit again, but I haven't tested it.
  1458. Revision 1.58 2003/03/08 20:36:41 daniel
  1459. + Added newra version of Ti386shlshrnode
  1460. + Added interference graph construction code
  1461. Revision 1.57 2003/03/08 13:59:17 daniel
  1462. * Work to handle new register notation in ag386nsm
  1463. + Added newra version of Ti386moddivnode
  1464. Revision 1.56 2003/03/08 10:53:48 daniel
  1465. * Created newra version of secondmul in n386add.pas
  1466. Revision 1.55 2003/02/19 22:00:15 daniel
  1467. * Code generator converted to new register notation
  1468. - Horribily outdated todo.txt removed
  1469. Revision 1.54 2003/01/13 18:37:44 daniel
  1470. * Work on register conversion
  1471. Revision 1.53 2003/01/08 18:43:57 daniel
  1472. * Tregister changed into a record
  1473. Revision 1.52 2002/11/25 17:43:26 peter
  1474. * splitted defbase in defutil,symutil,defcmp
  1475. * merged isconvertable and is_equal into compare_defs(_ext)
  1476. * made operator search faster by walking the list only once
  1477. Revision 1.51 2002/11/15 01:58:56 peter
  1478. * merged changes from 1.0.7 up to 04-11
  1479. - -V option for generating bug report tracing
  1480. - more tracing for option parsing
  1481. - errors for cdecl and high()
  1482. - win32 import stabs
  1483. - win32 records<=8 are returned in eax:edx (turned off by default)
  1484. - heaptrc update
  1485. - more info for temp management in .s file with EXTDEBUG
  1486. Revision 1.50 2002/10/20 13:11:27 jonas
  1487. * re-enabled optimized version of comparisons with the empty string that
  1488. I accidentally disabled in revision 1.26
  1489. Revision 1.49 2002/08/23 16:14:49 peter
  1490. * tempgen cleanup
  1491. * tt_noreuse temp type added that will be used in genentrycode
  1492. Revision 1.48 2002/08/14 18:41:48 jonas
  1493. - remove valuelow/valuehigh fields from tlocation, because they depend
  1494. on the endianess of the host operating system -> difficult to get
  1495. right. Use lo/hi(location.valueqword) instead (remember to use
  1496. valueqword and not value!!)
  1497. Revision 1.47 2002/08/11 14:32:29 peter
  1498. * renamed current_library to objectlibrary
  1499. Revision 1.46 2002/08/11 13:24:16 peter
  1500. * saving of asmsymbols in ppu supported
  1501. * asmsymbollist global is removed and moved into a new class
  1502. tasmlibrarydata that will hold the info of a .a file which
  1503. corresponds with a single module. Added librarydata to tmodule
  1504. to keep the library info stored for the module. In the future the
  1505. objectfiles will also be stored to the tasmlibrarydata class
  1506. * all getlabel/newasmsymbol and friends are moved to the new class
  1507. Revision 1.45 2002/07/26 11:17:52 jonas
  1508. * the optimization of converting a multiplication with a power of two to
  1509. a shl is moved from n386add/secondpass to nadd/resulttypepass
  1510. Revision 1.44 2002/07/20 11:58:00 florian
  1511. * types.pas renamed to defbase.pas because D6 contains a types
  1512. unit so this would conflicts if D6 programms are compiled
  1513. + Willamette/SSE2 instructions to assembler added
  1514. Revision 1.43 2002/07/11 14:41:32 florian
  1515. * start of the new generic parameter handling
  1516. Revision 1.42 2002/07/07 09:52:33 florian
  1517. * powerpc target fixed, very simple units can be compiled
  1518. * some basic stuff for better callparanode handling, far from being finished
  1519. Revision 1.41 2002/07/01 18:46:31 peter
  1520. * internal linker
  1521. * reorganized aasm layer
  1522. Revision 1.40 2002/07/01 16:23:55 peter
  1523. * cg64 patch
  1524. * basics for currency
  1525. * asnode updates for class and interface (not finished)
  1526. Revision 1.39 2002/05/18 13:34:22 peter
  1527. * readded missing revisions
  1528. Revision 1.38 2002/05/16 19:46:51 carl
  1529. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1530. + try to fix temp allocation (still in ifdef)
  1531. + generic constructor calls
  1532. + start of tassembler / tmodulebase class cleanup
  1533. Revision 1.36 2002/05/13 19:54:37 peter
  1534. * removed n386ld and n386util units
  1535. * maybe_save/maybe_restore added instead of the old maybe_push
  1536. Revision 1.35 2002/05/12 16:53:17 peter
  1537. * moved entry and exitcode to ncgutil and cgobj
  1538. * foreach gets extra argument for passing local data to the
  1539. iterator function
  1540. * -CR checks also class typecasts at runtime by changing them
  1541. into as
  1542. * fixed compiler to cycle with the -CR option
  1543. * fixed stabs with elf writer, finally the global variables can
  1544. be watched
  1545. * removed a lot of routines from cga unit and replaced them by
  1546. calls to cgobj
  1547. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1548. u32bit then the other is typecasted also to u32bit without giving
  1549. a rangecheck warning/error.
  1550. * fixed pascal calling method with reversing also the high tree in
  1551. the parast, detected by tcalcst3 test
  1552. Revision 1.34 2002/04/25 20:16:40 peter
  1553. * moved more routines from cga/n386util
  1554. Revision 1.33 2002/04/05 15:09:13 jonas
  1555. * fixed web bug 1915
  1556. Revision 1.32 2002/04/04 19:06:10 peter
  1557. * removed unused units
  1558. * use tlocation.size in cg.a_*loc*() routines
  1559. Revision 1.31 2002/04/02 17:11:35 peter
  1560. * tlocation,treference update
  1561. * LOC_CONSTANT added for better constant handling
  1562. * secondadd splitted in multiple routines
  1563. * location_force_reg added for loading a location to a register
  1564. of a specified size
  1565. * secondassignment parses now first the right and then the left node
  1566. (this is compatible with Kylix). This saves a lot of push/pop especially
  1567. with string operations
  1568. * adapted some routines to use the new cg methods
  1569. Revision 1.29 2002/03/04 19:10:13 peter
  1570. * removed compiler warnings
  1571. }