n386add.pas 61 KB

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