ncgutil.pas 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Helper routines for all code generators
  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 ncgutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. cginfo,cpubase,aasm,
  24. rgobj;
  25. type
  26. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  27. tmaybesave = record
  28. saved : boolean;
  29. ref : treference;
  30. end;
  31. procedure firstcomplex(p : tbinarynode);
  32. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  33. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: tregisterset);
  34. procedure location_force_reg(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  35. procedure location_force_mem(list: TAAsmoutput;var l:tlocation);
  36. procedure maybe_save(list:taasmoutput;needed:integer;var l:tlocation;var s:tmaybesave);
  37. procedure maybe_restore(list:taasmoutput;var l:tlocation;const s:tmaybesave);
  38. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  39. procedure push_value_para(p:tnode;inlined,is_cdecl:boolean;
  40. para_offset:longint;alignment : longint);
  41. procedure genentrycode(list : TAAsmoutput;
  42. make_global:boolean;
  43. stackframe:longint;
  44. var parasize:longint;var nostackframe:boolean;
  45. inlined : boolean);
  46. procedure genexitcode(list : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  47. procedure genimplicitunitinit(list : TAAsmoutput);
  48. procedure genimplicitunitfinal(list : TAAsmoutput);
  49. implementation
  50. uses
  51. {$ifdef Delphi}
  52. Sysutils,
  53. {$else}
  54. strings,
  55. {$endif}
  56. cutils,cclasses,globtype,globals,systems,verbose,
  57. symbase,symconst,symtype,symsym,symdef,symtable,types,
  58. fmodule,
  59. cgbase,regvars,tainst,cpuasm,
  60. {$ifdef GDB}
  61. gdb,
  62. {$endif GDB}
  63. ncon,
  64. tgobj,cpuinfo,cgobj,cgcpu,cg64f32;
  65. {*****************************************************************************
  66. Misc Helpers
  67. *****************************************************************************}
  68. { DO NOT RELY on the fact that the tnode is not yet swaped
  69. because of inlining code PM }
  70. procedure firstcomplex(p : tbinarynode);
  71. var
  72. hp : tnode;
  73. begin
  74. { always calculate boolean AND and OR from left to right }
  75. if (p.nodetype in [orn,andn]) and
  76. (p.left.resulttype.def.deftype=orddef) and
  77. (torddef(p.left.resulttype.def).typ in [bool8bit,bool16bit,bool32bit]) then
  78. begin
  79. { p.swaped:=false}
  80. if nf_swaped in p.flags then
  81. internalerror(234234);
  82. end
  83. else
  84. if (((p.location.loc=LOC_FPUREGISTER) and
  85. (p.right.registersfpu > p.left.registersfpu)) or
  86. ((((p.left.registersfpu = 0) and
  87. (p.right.registersfpu = 0)) or
  88. (p.location.loc<>LOC_FPUREGISTER)) and
  89. (p.left.registers32<p.right.registers32))) and
  90. { the following check is appropriate, because all }
  91. { 4 registers are rarely used and it is thereby }
  92. { achieved that the extra code is being dropped }
  93. { by exchanging not commutative operators }
  94. (p.right.registers32<=c_countusableregsint) then
  95. begin
  96. hp:=p.left;
  97. p.left:=p.right;
  98. p.right:=hp;
  99. if nf_swaped in p.flags then
  100. exclude(p.flags,nf_swaped)
  101. else
  102. include(p.flags,nf_swaped);
  103. end;
  104. end;
  105. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  106. {
  107. produces jumps to true respectively false labels using boolean expressions
  108. depending on whether the loading of regvars is currently being
  109. synchronized manually (such as in an if-node) or automatically (most of
  110. the other cases where this procedure is called), loadregvars can be
  111. "lr_load_regvars" or "lr_dont_load_regvars"
  112. }
  113. var
  114. opsize : tcgsize;
  115. storepos : tfileposinfo;
  116. begin
  117. if nf_error in p.flags then
  118. exit;
  119. storepos:=aktfilepos;
  120. aktfilepos:=p.fileinfo;
  121. if is_boolean(p.resulttype.def) then
  122. begin
  123. if loadregvars = lr_load_regvars then
  124. load_all_regvars(list);
  125. if is_constboolnode(p) then
  126. begin
  127. if tordconstnode(p).value<>0 then
  128. cg.a_jmp_always(list,truelabel)
  129. else
  130. cg.a_jmp_always(list,falselabel)
  131. end
  132. else
  133. begin
  134. opsize:=def_cgsize(p.resulttype.def);
  135. case p.location.loc of
  136. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  137. begin
  138. if (p.location.loc = LOC_CREGISTER) then
  139. load_regvar_reg(list,p.location.register);
  140. cg.a_cmp_const_loc_label(list,opsize,OC_NE,
  141. 0,p.location,truelabel);
  142. { !!! should happen right after cmp (JM) }
  143. location_release(list,p.location);
  144. cg.a_jmp_always(list,falselabel);
  145. end;
  146. LOC_FLAGS :
  147. begin
  148. cg.a_jmp_flags(list,p.location.resflags,
  149. truelabel);
  150. cg.a_jmp_always(list,falselabel);
  151. end;
  152. end;
  153. end;
  154. end
  155. else
  156. internalerror(200112305);
  157. aktfilepos:=storepos;
  158. end;
  159. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: tregisterset);
  160. begin
  161. case t.loc of
  162. LOC_REGISTER:
  163. begin
  164. { can't be a regvar, since it would be LOC_CREGISTER then }
  165. exclude(regs,t.register);
  166. if t.registerhigh <> R_NO then
  167. exclude(regs,t.registerhigh);
  168. end;
  169. LOC_CREFERENCE,LOC_REFERENCE:
  170. begin
  171. if not(cs_regalloc in aktglobalswitches) or
  172. (t.reference.base in rg.usableregsint) then
  173. exclude(regs,t.reference.base);
  174. if not(cs_regalloc in aktglobalswitches) or
  175. (t.reference.index in rg.usableregsint) then
  176. exclude(regs,t.reference.index);
  177. end;
  178. end;
  179. end;
  180. {*****************************************************************************
  181. TLocation
  182. *****************************************************************************}
  183. { 32-bit version }
  184. procedure location_force_reg32(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  185. var
  186. hregister,
  187. hregisterhi : tregister;
  188. hreg64 : tregister64;
  189. hl : tasmlabel;
  190. begin
  191. { handle transformations to 64bit separate }
  192. if dst_size in [OS_64,OS_S64] then
  193. begin
  194. if not (l.size in [OS_64,OS_S64]) then
  195. begin
  196. { load a smaller size to OS_64 }
  197. if l.loc=LOC_REGISTER then
  198. hregister:=rg.makeregsize(l.registerlow,OS_INT)
  199. else
  200. hregister:=rg.getregisterint(list);
  201. { load value in low register }
  202. case l.loc of
  203. LOC_FLAGS :
  204. cg.g_flags2reg(list,l.resflags,hregister);
  205. LOC_JUMP :
  206. begin
  207. cg.a_label(list,truelabel);
  208. cg.a_load_const_reg(list,OS_INT,1,hregister);
  209. getlabel(hl);
  210. cg.a_jmp_always(list,hl);
  211. cg.a_label(list,falselabel);
  212. cg.a_load_const_reg(list,OS_INT,0,hregister);
  213. cg.a_label(list,hl);
  214. end;
  215. else
  216. cg.a_load_loc_reg(list,l,hregister);
  217. end;
  218. { reset hi part, take care of the signed bit of the current value }
  219. hregisterhi:=rg.getregisterint(list);
  220. if (dst_size=OS_S64) and
  221. (l.size in [OS_S8,OS_S16,OS_S32]) then
  222. begin
  223. if l.loc=LOC_CONSTANT then
  224. begin
  225. if (longint(l.value)<0) then
  226. cg.a_load_const_reg(list,OS_32,$ffffffff,hregisterhi)
  227. else
  228. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  229. end
  230. else
  231. begin
  232. cg.a_load_reg_reg(list,OS_32,hregister,hregisterhi);
  233. cg.a_op_const_reg(list,OP_SAR,31,hregisterhi);
  234. end;
  235. end
  236. else
  237. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  238. location_reset(l,LOC_REGISTER,dst_size);
  239. l.registerlow:=hregister;
  240. l.registerhigh:=hregisterhi;
  241. end
  242. else
  243. begin
  244. { 64bit to 64bit }
  245. if (l.loc=LOC_REGISTER) or
  246. ((l.loc=LOC_CREGISTER) and maybeconst) then
  247. begin
  248. hregister:=l.registerlow;
  249. hregisterhi:=l.registerhigh;
  250. end
  251. else
  252. begin
  253. hregister:=rg.getregisterint(list);
  254. hregisterhi:=rg.getregisterint(list);
  255. end;
  256. hreg64.reglo:=hregister;
  257. hreg64.reghi:=hregisterhi;
  258. { load value in new register }
  259. cg64.a_load64_loc_reg(list,l,hreg64);
  260. location_reset(l,LOC_REGISTER,dst_size);
  261. l.registerlow:=hregister;
  262. l.registerhigh:=hregisterhi;
  263. end;
  264. end
  265. else
  266. begin
  267. { transformations to 32bit or smaller }
  268. if l.loc=LOC_REGISTER then
  269. begin
  270. { if the previous was 64bit release the high register }
  271. if l.size in [OS_64,OS_S64] then
  272. begin
  273. rg.ungetregisterint(list,l.registerhigh);
  274. l.registerhigh:=R_NO;
  275. end;
  276. hregister:=l.register;
  277. end
  278. else
  279. begin
  280. { get new register }
  281. if (l.loc=LOC_CREGISTER) and
  282. maybeconst and
  283. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  284. hregister:=l.register
  285. else
  286. hregister:=rg.getregisterint(list);
  287. end;
  288. hregister:=rg.makeregsize(hregister,dst_size);
  289. { load value in new register }
  290. case l.loc of
  291. LOC_FLAGS :
  292. cg.g_flags2reg(list,l.resflags,hregister);
  293. LOC_JUMP :
  294. begin
  295. cg.a_label(list,truelabel);
  296. cg.a_load_const_reg(list,dst_size,1,hregister);
  297. getlabel(hl);
  298. cg.a_jmp_always(list,hl);
  299. cg.a_label(list,falselabel);
  300. cg.a_load_const_reg(list,dst_size,0,hregister);
  301. cg.a_label(list,hl);
  302. end;
  303. else
  304. begin
  305. { load_loc_reg can only handle size >= l.size, when the
  306. new size is smaller then we need to adjust the size
  307. of the orignal and maybe recalculate l.register for i386 }
  308. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  309. begin
  310. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  311. l.register:=rg.makeregsize(l.register,dst_size);
  312. l.size:=dst_size;
  313. end;
  314. cg.a_load_loc_reg(list,l,hregister);
  315. end;
  316. end;
  317. location_reset(l,LOC_REGISTER,dst_size);
  318. l.register:=hregister;
  319. end;
  320. end;
  321. { 64-bit version }
  322. procedure location_force_reg64(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  323. var
  324. hregister : tregister;
  325. hl : tasmlabel;
  326. begin
  327. { handle transformations to 64bit separate }
  328. if dst_size in [OS_64,OS_S64] then
  329. begin
  330. { load a smaller size to OS_64 }
  331. if l.loc=LOC_REGISTER then
  332. hregister:=rg.makeregsize(l.register,OS_INT)
  333. else
  334. hregister:=rg.getregisterint(list);
  335. { load value in low register }
  336. case l.loc of
  337. LOC_FLAGS :
  338. cg.g_flags2reg(list,l.resflags,hregister);
  339. LOC_JUMP :
  340. begin
  341. cg.a_label(list,truelabel);
  342. cg.a_load_const_reg(list,OS_INT,1,hregister);
  343. getlabel(hl);
  344. cg.a_jmp_always(list,hl);
  345. cg.a_label(list,falselabel);
  346. cg.a_load_const_reg(list,OS_INT,0,hregister);
  347. cg.a_label(list,hl);
  348. end;
  349. else
  350. cg.a_load_loc_reg(list,l,hregister);
  351. end;
  352. location_reset(l,LOC_REGISTER,dst_size);
  353. l.register:=hregister;
  354. end
  355. else
  356. begin
  357. { transformations to 32bit or smaller }
  358. if l.loc=LOC_REGISTER then
  359. begin
  360. hregister:=l.register;
  361. end
  362. else
  363. begin
  364. { get new register }
  365. if (l.loc=LOC_CREGISTER) and
  366. maybeconst and
  367. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  368. hregister:=l.register
  369. else
  370. hregister:=rg.getregisterint(list);
  371. end;
  372. hregister:=rg.makeregsize(hregister,dst_size);
  373. { load value in new register }
  374. case l.loc of
  375. LOC_FLAGS :
  376. cg.g_flags2reg(list,l.resflags,hregister);
  377. LOC_JUMP :
  378. begin
  379. cg.a_label(list,truelabel);
  380. cg.a_load_const_reg(list,dst_size,1,hregister);
  381. getlabel(hl);
  382. cg.a_jmp_always(list,hl);
  383. cg.a_label(list,falselabel);
  384. cg.a_load_const_reg(list,dst_size,0,hregister);
  385. cg.a_label(list,hl);
  386. end;
  387. else
  388. begin
  389. { load_loc_reg can only handle size >= l.size, when the
  390. new size is smaller then we need to adjust the size
  391. of the orignal and maybe recalculate l.register for i386 }
  392. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  393. begin
  394. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  395. l.register:=rg.makeregsize(l.register,dst_size);
  396. l.size:=dst_size;
  397. end;
  398. cg.a_load_loc_reg(list,l,hregister);
  399. end;
  400. end;
  401. location_reset(l,LOC_REGISTER,dst_size);
  402. l.register:=hregister;
  403. end;
  404. end;
  405. procedure location_force_reg(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  406. begin
  407. { release previous location before demanding a new register }
  408. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  409. begin
  410. location_freetemp(list,l);
  411. location_release(list,l);
  412. end;
  413. if sizeof(aword) < 8 then
  414. location_force_reg32(list, l, dst_size, maybeconst)
  415. else
  416. location_force_reg64(list, l, dst_size, maybeconst);
  417. end;
  418. procedure location_force_mem(list: TAAsmoutput;var l:tlocation);
  419. var
  420. r : treference;
  421. begin
  422. case l.loc of
  423. LOC_FPUREGISTER,
  424. LOC_CFPUREGISTER :
  425. begin
  426. tg.gettempofsizereference(list,TCGSize2Size[l.size],r);
  427. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  428. location_reset(l,LOC_REFERENCE,l.size);
  429. l.reference:=r;
  430. end;
  431. LOC_CONSTANT,
  432. LOC_REGISTER,
  433. LOC_CREGISTER :
  434. begin
  435. tg.gettempofsizereference(list,TCGSize2Size[l.size],r);
  436. if l.size in [OS_64,OS_S64] then
  437. cg64.a_load64_loc_ref(list,l,r)
  438. else
  439. cg.a_load_loc_ref(list,l,r);
  440. location_reset(l,LOC_REFERENCE,l.size);
  441. l.reference:=r;
  442. end;
  443. LOC_CREFERENCE,
  444. LOC_REFERENCE : ;
  445. else
  446. internalerror(200203219);
  447. end;
  448. end;
  449. {*****************************************************************************
  450. Maybe_Save
  451. *****************************************************************************}
  452. procedure maybe_save(list:taasmoutput;needed:integer;var l:tlocation;var s:tmaybesave);
  453. begin
  454. s.saved:=false;
  455. if l.loc=LOC_CREGISTER then
  456. begin
  457. s.saved:=true;
  458. exit;
  459. end;
  460. if needed>rg.countunusedregsint then
  461. begin
  462. case l.loc of
  463. LOC_REGISTER :
  464. begin
  465. if l.size in [OS_64,OS_S64] then
  466. begin
  467. tg.gettempofsizereference(exprasmlist,8,s.ref);
  468. cg64.a_load64_reg_ref(exprasmlist,joinreg64(l.registerlow,l.registerhigh),s.ref);
  469. end
  470. else
  471. begin
  472. tg.gettempofsizereference(exprasmlist,TCGSize2Size[l.size],s.ref);
  473. cg.a_load_reg_ref(exprasmlist,l.size,l.register,s.ref);
  474. end;
  475. location_release(exprasmlist,l);
  476. s.saved:=true;
  477. end;
  478. LOC_REFERENCE,
  479. LOC_CREFERENCE :
  480. begin
  481. if ((l.reference.base<>R_NO) or
  482. (l.reference.index<>R_NO)) then
  483. begin
  484. { load address into a single base register }
  485. cg.a_loadaddr_ref_reg(list,l.reference,l.reference.base);
  486. { save base register }
  487. tg.gettempofsizereference(exprasmlist,TCGSize2Size[OS_ADDR],s.ref);
  488. cg.a_load_reg_ref(exprasmlist,OS_ADDR,l.reference.base,s.ref);
  489. { release }
  490. location_release(exprasmlist,l);
  491. s.saved:=true;
  492. end;
  493. end;
  494. end;
  495. end;
  496. end;
  497. procedure maybe_restore(list:taasmoutput;var l:tlocation;const s:tmaybesave);
  498. begin
  499. if not s.saved then
  500. exit;
  501. if l.loc=LOC_CREGISTER then
  502. begin
  503. load_regvar_reg(list,l.register);
  504. exit;
  505. end;
  506. case l.loc of
  507. LOC_REGISTER :
  508. begin
  509. if l.size in [OS_64,OS_S64] then
  510. begin
  511. l.registerlow:=rg.getregisterint(exprasmlist);
  512. l.registerhigh:=rg.getregisterint(exprasmlist);
  513. cg64.a_load64_ref_reg(exprasmlist,s.ref,joinreg64(l.registerlow,l.registerhigh));
  514. end
  515. else
  516. begin
  517. l.register:=rg.getregisterint(exprasmlist);
  518. cg.a_load_ref_reg(exprasmlist,OS_INT,s.ref,l.register);
  519. end;
  520. end;
  521. LOC_CREFERENCE,
  522. LOC_REFERENCE :
  523. begin
  524. reference_reset(l.reference);
  525. l.reference.base:=rg.getaddressregister(exprasmlist);
  526. cg.a_load_ref_reg(exprasmlist,OS_ADDR,s.ref,l.reference.base);
  527. end;
  528. end;
  529. tg.ungetiftemp(exprasmlist,s.ref);
  530. end;
  531. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  532. begin
  533. if needed>=maxfpuregs then
  534. begin
  535. if l.loc = LOC_FPUREGISTER then
  536. begin
  537. location_force_mem(list,l);
  538. maybe_pushfpu:=true;
  539. end
  540. else
  541. maybe_pushfpu:=false;
  542. end
  543. else
  544. maybe_pushfpu:=false;
  545. end;
  546. {*****************************************************************************
  547. Push Value Para
  548. *****************************************************************************}
  549. procedure push_value_para(p:tnode;inlined,is_cdecl:boolean;
  550. para_offset:longint;alignment : longint);
  551. var
  552. tempreference : treference;
  553. href : treference;
  554. hreg : tregister;
  555. sizetopush,
  556. size : longint;
  557. cgsize : tcgsize;
  558. begin
  559. { Move flags and jump in register to make it less complex }
  560. if p.location.loc in [LOC_FLAGS,LOC_JUMP] then
  561. location_force_reg(exprasmlist,p.location,def_cgsize(p.resulttype.def),false);
  562. { Handle Floating point types differently }
  563. if p.resulttype.def.deftype=floatdef then
  564. begin
  565. case p.location.loc of
  566. LOC_FPUREGISTER,
  567. LOC_CFPUREGISTER:
  568. begin
  569. size:=align(tfloatdef(p.resulttype.def).size,alignment);
  570. inc(pushedparasize,size);
  571. if not inlined then
  572. cg.a_op_const_reg(exprasmlist,OP_SUB,size,STACK_POINTER_REG);
  573. {$ifdef GDB}
  574. if (cs_debuginfo in aktmoduleswitches) and
  575. (exprasmList.first=exprasmList.last) then
  576. exprasmList.concat(Tai_force_line.Create);
  577. {$endif GDB}
  578. { this is the easiest case for inlined !! }
  579. if inlined then
  580. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize)
  581. else
  582. reference_reset_base(href,stack_pointer_reg,0);
  583. cg.a_loadfpu_reg_ref(exprasmlist,
  584. def_cgsize(p.resulttype.def),p.location.register,href);
  585. end;
  586. LOC_REFERENCE,
  587. LOC_CREFERENCE :
  588. begin
  589. sizetopush:=align(p.resulttype.def.size,alignment);
  590. tempreference:=p.location.reference;
  591. inc(tempreference.offset,sizetopush);
  592. while (sizetopush>0) do
  593. begin
  594. if sizetopush>=4 then
  595. begin
  596. cgsize:=OS_32;
  597. inc(pushedparasize,4);
  598. dec(tempreference.offset,4);
  599. dec(sizetopush,4);
  600. end
  601. else
  602. begin
  603. cgsize:=OS_16;
  604. inc(pushedparasize,2);
  605. dec(tempreference.offset,2);
  606. dec(sizetopush,2);
  607. end;
  608. if inlined then
  609. begin
  610. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  611. cg.a_load_ref_ref(exprasmlist,cgsize,tempreference,href);
  612. end
  613. else
  614. cg.a_param_ref(exprasmlist,cgsize,tempreference,-1);
  615. end;
  616. end;
  617. else
  618. internalerror(200204243);
  619. end;
  620. end
  621. else
  622. begin
  623. { call by value open array ? }
  624. if is_cdecl and
  625. push_addr_param(p.resulttype.def) then
  626. begin
  627. if not (p.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  628. internalerror(200204241);
  629. { push on stack }
  630. size:=align(p.resulttype.def.size,alignment);
  631. inc(pushedparasize,size);
  632. cg.a_op_const_reg(exprasmlist,OP_SUB,size,STACK_POINTER_REG);
  633. reference_reset_base(href,STACK_POINTER_REG,0);
  634. cg.g_concatcopy(exprasmlist,p.location.reference,href,size,false,false);
  635. end
  636. else
  637. begin
  638. case p.location.loc of
  639. LOC_CONSTANT,
  640. LOC_REGISTER,
  641. LOC_CREGISTER,
  642. LOC_REFERENCE,
  643. LOC_CREFERENCE :
  644. begin
  645. cgsize:=def_cgsize(p.resulttype.def);
  646. if cgsize in [OS_64,OS_S64] then
  647. begin
  648. inc(pushedparasize,8);
  649. if inlined then
  650. begin
  651. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  652. cg64.a_load64_loc_ref(exprasmlist,p.location,href);
  653. end
  654. else
  655. cg64.a_param64_loc(exprasmlist,p.location,-1);
  656. end
  657. else
  658. begin
  659. case cgsize of
  660. OS_8,OS_S8 :
  661. begin
  662. if alignment=4 then
  663. cgsize:=OS_32
  664. else
  665. cgsize:=OS_16;
  666. end;
  667. OS_16,OS_S16 :
  668. begin
  669. if alignment=4 then
  670. cgsize:=OS_32;
  671. end;
  672. end;
  673. { update register to use to match alignment }
  674. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  675. begin
  676. hreg:=p.location.register;
  677. p.location.register:=rg.makeregsize(p.location.register,cgsize);
  678. end;
  679. inc(pushedparasize,alignment);
  680. if inlined then
  681. begin
  682. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  683. cg.a_load_loc_ref(exprasmlist,p.location,href);
  684. end
  685. else
  686. cg.a_param_loc(exprasmlist,p.location,-1);
  687. { restore old register }
  688. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  689. p.location.register:=hreg;
  690. end;
  691. location_release(exprasmlist,p.location);
  692. end;
  693. {$ifdef SUPPORT_MMX}
  694. LOC_MMXREGISTER,
  695. LOC_CMMXREGISTER:
  696. begin
  697. inc(pushedparasize,8);
  698. if inlined then
  699. begin
  700. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  701. cg.a_loadmm_reg_ref(exprasmlist,p.location.register,href);
  702. end
  703. else
  704. cg.a_parammm_reg(exprasmlist,p.location.register);
  705. end;
  706. {$endif SUPPORT_MMX}
  707. else
  708. internalerror(200204241);
  709. end;
  710. end;
  711. end;
  712. end;
  713. {****************************************************************************
  714. Entry/Exit Code
  715. ****************************************************************************}
  716. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  717. var
  718. href1,href2 : treference;
  719. list : taasmoutput;
  720. begin
  721. list:=taasmoutput(arg);
  722. if (tsym(p).typ=varsym) and
  723. (tvarsym(p).varspez=vs_value) and
  724. (push_addr_param(tvarsym(p).vartype.def)) then
  725. begin
  726. reference_reset_base(href1,procinfo^.framepointer,tvarsym(p).address+procinfo^.para_offset);
  727. if is_open_array(tvarsym(p).vartype.def) or
  728. is_array_of_const(tvarsym(p).vartype.def) then
  729. cg.g_copyvaluepara_openarray(list,href1,tarraydef(tvarsym(p).vartype.def).elesize)
  730. else
  731. begin
  732. reference_reset_base(href2,procinfo^.framepointer,-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup);
  733. if is_shortstring(tvarsym(p).vartype.def) then
  734. cg.g_copyshortstring(list,href1,href2,tstringdef(tvarsym(p).vartype.def).len,false,true)
  735. else
  736. cg.g_concatcopy(list,href1,href2,tvarsym(p).vartype.def.size,true,true);
  737. end;
  738. end;
  739. end;
  740. procedure initialize_threadvar(p : tnamedindexitem;arg:pointer);
  741. var
  742. href : treference;
  743. list : taasmoutput;
  744. begin
  745. list:=taasmoutput(arg);
  746. if (tsym(p).typ=varsym) and
  747. (vo_is_thread_var in tvarsym(p).varoptions) then
  748. begin
  749. cg.a_param_const(list,OS_INT,tvarsym(p).getsize,2);
  750. reference_reset_symbol(href,newasmsymbol(tvarsym(p).mangledname),0);
  751. cg.a_paramaddr_ref(list,href,2);
  752. rg.saveregvars(list,all_registers);
  753. cg.a_call_name(list,'FPC_INIT_THREADVAR');
  754. end;
  755. end;
  756. { generates the code for initialisation of local data }
  757. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  758. var
  759. href : treference;
  760. list : taasmoutput;
  761. begin
  762. list:=taasmoutput(arg);
  763. if (tsym(p).typ=varsym) and
  764. assigned(tvarsym(p).vartype.def) and
  765. not(is_class(tvarsym(p).vartype.def)) and
  766. tvarsym(p).vartype.def.needs_inittable then
  767. begin
  768. if assigned(procinfo) then
  769. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  770. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  771. reference_reset_base(href,procinfo^.framepointer,-tvarsym(p).address+tvarsym(p).owner.address_fixup)
  772. else
  773. reference_reset_symbol(href,newasmsymbol(tvarsym(p).mangledname),0);
  774. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  775. end;
  776. end;
  777. { generates the code for finalisation of local data }
  778. procedure finalize_data(p : tnamedindexitem;arg:pointer);
  779. var
  780. href : treference;
  781. list : taasmoutput;
  782. begin
  783. list:=taasmoutput(arg);
  784. if (tsym(p).typ=varsym) and
  785. assigned(tvarsym(p).vartype.def) and
  786. not(is_class(tvarsym(p).vartype.def)) and
  787. tvarsym(p).vartype.def.needs_inittable then
  788. begin
  789. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  790. reference_reset_base(href,procinfo^.framepointer,-tvarsym(p).address+tvarsym(p).owner.address_fixup)
  791. else
  792. reference_reset_symbol(href,newasmsymbol(tvarsym(p).mangledname),0);
  793. cg.g_finalize(list,tvarsym(p).vartype.def,href,false);
  794. end;
  795. end;
  796. { generates the code for incrementing the reference count of parameters and
  797. initialize out parameters }
  798. procedure init_paras(p : tnamedindexitem;arg:pointer);
  799. var
  800. href : treference;
  801. tmpreg : tregister;
  802. list : taasmoutput;
  803. begin
  804. list:=taasmoutput(arg);
  805. if (tsym(p).typ=varsym) and
  806. not is_class(tvarsym(p).vartype.def) and
  807. tvarsym(p).vartype.def.needs_inittable then
  808. begin
  809. case tvarsym(p).varspez of
  810. vs_value :
  811. begin
  812. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  813. if assigned(tvarsym(p).localvarsym) then
  814. reference_reset_base(href,procinfo^.framepointer,
  815. -tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup)
  816. else
  817. reference_reset_base(href,procinfo^.framepointer,tvarsym(p).address+procinfo^.para_offset);
  818. cg.g_incrrefcount(list,tvarsym(p).vartype.def,href);
  819. end;
  820. vs_out :
  821. begin
  822. reference_reset_base(href,procinfo^.framepointer,tvarsym(p).address+procinfo^.para_offset);
  823. tmpreg:=cg.get_scratch_reg_address(list);
  824. cg.a_load_ref_reg(list,OS_ADDR,href,tmpreg);
  825. reference_reset_base(href,tmpreg,0);
  826. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  827. cg.free_scratch_reg(list,tmpreg);
  828. end;
  829. end;
  830. end;
  831. end;
  832. { generates the code for decrementing the reference count of parameters }
  833. procedure final_paras(p : tnamedindexitem;arg:pointer);
  834. var
  835. href : treference;
  836. list : taasmoutput;
  837. begin
  838. list:=taasmoutput(arg);
  839. if (tsym(p).typ=varsym) and
  840. not is_class(tvarsym(p).vartype.def) and
  841. tvarsym(p).vartype.def.needs_inittable then
  842. begin
  843. if (tvarsym(p).varspez=vs_value) then
  844. begin
  845. if assigned(tvarsym(p).localvarsym) then
  846. reference_reset_base(href,procinfo^.framepointer,
  847. -tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup)
  848. else
  849. reference_reset_base(href,procinfo^.framepointer,tvarsym(p).address+procinfo^.para_offset);
  850. cg.g_decrrefcount(list,tvarsym(p).vartype.def,href);
  851. end;
  852. end;
  853. end;
  854. { Initialize temp ansi/widestrings,interfaces }
  855. procedure inittempvariables(list:taasmoutput);
  856. var
  857. hp : ptemprecord;
  858. href : treference;
  859. begin
  860. hp:=tg.templist;
  861. while assigned(hp) do
  862. begin
  863. if hp^.temptype in [tt_ansistring,tt_freeansistring,
  864. tt_widestring,tt_freewidestring,
  865. tt_interfacecom] then
  866. begin
  867. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  868. reference_reset_base(href,procinfo^.framepointer,hp^.pos);
  869. cg.a_load_const_ref(list,OS_ADDR,0,href);
  870. end;
  871. hp:=hp^.next;
  872. end;
  873. end;
  874. procedure finalizetempvariables(list:taasmoutput);
  875. var
  876. hp : ptemprecord;
  877. href : treference;
  878. begin
  879. hp:=tg.templist;
  880. while assigned(hp) do
  881. begin
  882. case hp^.temptype of
  883. tt_ansistring,
  884. tt_freeansistring :
  885. begin
  886. reference_reset_base(href,procinfo^.framepointer,hp^.pos);
  887. cg.a_paramaddr_ref(list,href,1);
  888. cg.a_call_name(list,'FPC_ANSISTR_DECR_REF');
  889. end;
  890. tt_widestring,
  891. tt_freewidestring :
  892. begin
  893. reference_reset_base(href,procinfo^.framepointer,hp^.pos);
  894. cg.a_paramaddr_ref(list,href,1);
  895. cg.a_call_name(list,'FPC_WIDESTR_DECR_REF');
  896. end;
  897. tt_interfacecom :
  898. begin
  899. reference_reset_base(href,procinfo^.framepointer,hp^.pos);
  900. cg.a_paramaddr_ref(list,href,1);
  901. cg.a_call_name(list,'FPC_INTF_DECR_REF');
  902. end;
  903. end;
  904. hp:=hp^.next;
  905. end;
  906. end;
  907. procedure handle_return_value(list:TAAsmoutput; inlined : boolean;var uses_acc,uses_acchi : boolean);
  908. var
  909. href : treference;
  910. hreg : tregister;
  911. cgsize : TCGSize;
  912. begin
  913. if not is_void(aktprocdef.rettype.def) then
  914. begin
  915. if (tfuncretsym(aktprocdef.funcretsym).funcretstate<>vs_assigned) and
  916. (not inlined) then
  917. CGMessage(sym_w_function_result_not_set);
  918. reference_reset_base(href,procinfo^.framepointer,procinfo^.return_offset);
  919. cgsize:=def_cgsize(aktprocdef.rettype.def);
  920. case aktprocdef.rettype.def.deftype of
  921. orddef,
  922. enumdef :
  923. begin
  924. uses_acc:=true;
  925. cg.a_reg_alloc(list,accumulator);
  926. if cgsize in [OS_64,OS_S64] then
  927. begin
  928. uses_acchi:=true;
  929. cg.a_reg_alloc(list,accumulatorhigh);
  930. cg64.a_load64_ref_reg(list,href,joinreg64(accumulator,accumulatorhigh));
  931. end
  932. else
  933. begin
  934. hreg:=rg.makeregsize(accumulator,cgsize);
  935. cg.a_load_ref_reg(list,cgsize,href,hreg);
  936. end;
  937. end;
  938. floatdef :
  939. begin
  940. cg.a_loadfpu_ref_reg(list,cgsize,href,fpuresultreg);
  941. end;
  942. else
  943. begin
  944. if ret_in_acc(aktprocdef.rettype.def) then
  945. begin
  946. uses_acc:=true;
  947. cg.a_reg_alloc(list,accumulator);
  948. cg.a_load_ref_reg(list,cgsize,href,accumulator);
  949. end
  950. end;
  951. end;
  952. end;
  953. end;
  954. procedure handle_fast_exit_return_value(list:TAAsmoutput);
  955. var
  956. href : treference;
  957. hreg : tregister;
  958. cgsize : TCGSize;
  959. begin
  960. if not is_void(aktprocdef.rettype.def) then
  961. begin
  962. reference_reset_base(href,procinfo^.framepointer,procinfo^.return_offset);
  963. cgsize:=def_cgsize(aktprocdef.rettype.def);
  964. case aktprocdef.rettype.def.deftype of
  965. orddef,
  966. enumdef :
  967. begin
  968. if cgsize in [OS_64,OS_S64] then
  969. cg64.a_load64_reg_ref(list,joinreg64(accumulator,accumulatorhigh),href)
  970. else
  971. begin
  972. hreg:=rg.makeregsize(accumulator,cgsize);
  973. cg.a_load_reg_ref(list,cgsize,hreg,href);
  974. end;
  975. end;
  976. floatdef :
  977. begin
  978. cg.a_loadfpu_reg_ref(list,cgsize,fpuresultreg,href);
  979. end;
  980. else
  981. begin
  982. if ret_in_acc(aktprocdef.rettype.def) then
  983. cg.a_load_reg_ref(list,cgsize,accumulator,href);
  984. end;
  985. end;
  986. end;
  987. end;
  988. procedure genentrycode(list : TAAsmoutput;
  989. make_global:boolean;
  990. stackframe:longint;
  991. var parasize:longint;var nostackframe:boolean;
  992. inlined : boolean);
  993. var
  994. hs : string;
  995. href : treference;
  996. p : tsymtable;
  997. tempbuf : treference;
  998. tmpreg : tregister;
  999. begin
  1000. { Insert alignment and assembler names }
  1001. if not inlined then
  1002. begin
  1003. { Align, gprof uses 16 byte granularity }
  1004. if (cs_profile in aktmoduleswitches) then
  1005. list.concat(Tai_align.Create_op(16,$90))
  1006. else
  1007. list.concat(Tai_align.Create(aktalignment.procalign));
  1008. if (cs_profile in aktmoduleswitches) or
  1009. (aktprocdef.owner.symtabletype=globalsymtable) or
  1010. (assigned(procinfo^._class) and (procinfo^._class.owner.symtabletype=globalsymtable)) then
  1011. make_global:=true;
  1012. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  1013. aktprocsym.is_global := True;
  1014. {$ifdef GDB}
  1015. if (cs_debuginfo in aktmoduleswitches) then
  1016. begin
  1017. aktprocdef.concatstabto(list);
  1018. aktprocsym.isstabwritten:=true;
  1019. end;
  1020. {$endif GDB}
  1021. repeat
  1022. hs:=aktprocdef.aliasnames.getfirst;
  1023. if hs='' then
  1024. break;
  1025. {$ifdef GDB}
  1026. if (cs_debuginfo in aktmoduleswitches) and
  1027. target_info.use_function_relative_addresses then
  1028. list.concat(Tai_stab_function_name.Create(strpnew(hs)));
  1029. {$endif GDB}
  1030. if make_global then
  1031. list.concat(Tai_symbol.Createname_global(hs,0))
  1032. else
  1033. list.concat(Tai_symbol.Createname(hs,0));
  1034. until false;
  1035. { omit stack frame ? }
  1036. if (procinfo^.framepointer=STACK_POINTER_REG) then
  1037. begin
  1038. CGMessage(cg_d_stackframe_omited);
  1039. nostackframe:=true;
  1040. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1041. parasize:=0
  1042. else
  1043. parasize:=aktprocdef.parast.datasize+procinfo^.para_offset-4;
  1044. if stackframe<>0 then
  1045. cg.a_op_const_reg(list,OP_SUB,stackframe,procinfo^.framepointer);
  1046. end
  1047. else
  1048. begin
  1049. nostackframe:=false;
  1050. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1051. parasize:=0
  1052. else
  1053. parasize:=aktprocdef.parast.datasize+procinfo^.para_offset-target_info.first_parm_offset;
  1054. if (po_interrupt in aktprocdef.procoptions) then
  1055. cg.g_interrupt_stackframe_entry(list);
  1056. cg.g_stackframe_entry(list,stackframe);
  1057. if (cs_check_stack in aktlocalswitches) then
  1058. cg.g_stackcheck(list,stackframe);
  1059. end;
  1060. if (cs_profile in aktmoduleswitches) and
  1061. not(po_assembler in aktprocdef.procoptions) then
  1062. cg.g_profilecode(list);
  1063. end;
  1064. { for the save all registers we can simply use a pusha,popa which
  1065. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1066. if (po_saveregisters in aktprocdef.procoptions) then
  1067. cg.g_save_all_registers(list)
  1068. else
  1069. { should we save edi,esi,ebx like C ? }
  1070. if (po_savestdregs in aktprocdef.procoptions) then
  1071. cg.g_save_standard_registers(list);
  1072. { a constructor needs a help procedure }
  1073. if (aktprocdef.proctypeoption=potype_constructor) then
  1074. cg.g_call_constructor_helper(list);
  1075. { don't load ESI, does the caller }
  1076. { we must do it for local function }
  1077. { that can be called from a foreach_static }
  1078. { of another object than self !! PM }
  1079. if assigned(procinfo^._class) and { !!!!! shouldn't we load ESI always? }
  1080. (lexlevel>normal_function_level) then
  1081. cg.g_maybe_loadself(list);
  1082. { When message method contains self as a parameter,
  1083. we must load it into ESI }
  1084. If (po_containsself in aktprocdef.procoptions) then
  1085. begin
  1086. list.concat(Tairegalloc.Alloc(self_pointer_reg));
  1087. reference_reset_base(href,procinfo^.framepointer,procinfo^.selfpointer_offset);
  1088. cg.a_load_ref_reg(list,OS_ADDR,href,self_pointer_reg);
  1089. end;
  1090. { initialize return value }
  1091. if (not is_void(aktprocdef.rettype.def)) and
  1092. (aktprocdef.rettype.def.needs_inittable) then
  1093. begin
  1094. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1095. reference_reset_base(href,procinfo^.framepointer,procinfo^.return_offset);
  1096. cg.g_initialize(list,aktprocdef.rettype.def,href,ret_in_param(aktprocdef.rettype.def));
  1097. end;
  1098. { initialisize local data like ansistrings }
  1099. case aktprocdef.proctypeoption of
  1100. potype_unitinit:
  1101. begin
  1102. { using current_module.globalsymtable is hopefully }
  1103. { more robust than symtablestack and symtablestack.next }
  1104. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1105. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1106. end;
  1107. { units have seperate code for initilization and finalization }
  1108. potype_unitfinalize: ;
  1109. else
  1110. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1111. end;
  1112. { initialisizes temp. ansi/wide string data }
  1113. inittempvariables(list);
  1114. { generate copies of call by value parameters }
  1115. if not(po_assembler in aktprocdef.procoptions) and
  1116. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_palmossyscall,pocall_system]) then
  1117. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1118. if assigned( aktprocdef.parast) then
  1119. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1120. if (not inlined) then
  1121. begin
  1122. { call startup helpers from main program }
  1123. if (aktprocdef.proctypeoption=potype_proginit) then
  1124. begin
  1125. { initialize profiling for win32 }
  1126. if (target_info.target in [target_I386_WIN32,target_I386_wdosx]) and
  1127. (cs_profile in aktmoduleswitches) then
  1128. cg.a_call_name(list,'__monstartup');
  1129. { add local threadvars in units (only if needed because not all platforms
  1130. have threadvar support) }
  1131. if have_local_threadvars then
  1132. cg.a_call_name(list,'FPC_INITIALIZELOCALTHREADVARS');
  1133. { add global threadvars }
  1134. p:=symtablestack;
  1135. while assigned(p) do
  1136. begin
  1137. p.foreach_static({$ifndef TP}@{$endif}initialize_threadvar,list);
  1138. p:=p.next;
  1139. end;
  1140. { initialize units }
  1141. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1142. end;
  1143. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  1144. if ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  1145. { but it's useless in init/final code of units }
  1146. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  1147. begin
  1148. include(rg.usedinproc,accumulator);
  1149. { allocate exception frame buffer }
  1150. cg.a_op_const_reg(list,OP_SUB,36,STACK_POINTER_REG);
  1151. tmpreg:=rg.getaddressregister(list);
  1152. cg.a_load_reg_reg(list,OS_ADDR,STACK_POINTER_REG,tmpreg);
  1153. reference_reset_base(tempbuf,tmpreg,0);
  1154. cg.g_push_exception(list,tempbuf,1,aktexitlabel);
  1155. reference_release(list,tempbuf);
  1156. { probably we've to reload self here }
  1157. cg.g_maybe_loadself(list);
  1158. end;
  1159. {$ifdef GDB}
  1160. if (cs_debuginfo in aktmoduleswitches) then
  1161. list.concat(Tai_force_line.Create);
  1162. {$endif GDB}
  1163. end;
  1164. if inlined then
  1165. load_regvars(list,nil);
  1166. end;
  1167. procedure genexitcode(list : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  1168. var
  1169. {$ifdef GDB}
  1170. stabsendlabel : tasmlabel;
  1171. mangled_length : longint;
  1172. p : pchar;
  1173. st : string[2];
  1174. {$endif GDB}
  1175. okexitlabel,
  1176. noreraiselabel,nodestroycall : tasmlabel;
  1177. tmpreg : tregister;
  1178. href : treference;
  1179. usesacc,
  1180. usesacchi,
  1181. usesself : boolean;
  1182. pd : tprocdef;
  1183. begin
  1184. if aktexit2label.is_used and
  1185. ((procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0) then
  1186. begin
  1187. cg.a_jmp_always(list,aktexitlabel);
  1188. cg.a_label(list,aktexit2label);
  1189. handle_fast_exit_return_value(list);
  1190. end;
  1191. if aktexitlabel.is_used then
  1192. list.concat(Tai_label.Create(aktexitlabel));
  1193. cleanup_regvars(list);
  1194. { call the destructor help procedure }
  1195. if (aktprocdef.proctypeoption=potype_destructor) and
  1196. assigned(procinfo^._class) then
  1197. cg.g_call_destructor_helper(list);
  1198. { finalize temporary data }
  1199. finalizetempvariables(list);
  1200. { finalize local data like ansistrings}
  1201. case aktprocdef.proctypeoption of
  1202. potype_unitfinalize:
  1203. begin
  1204. { using current_module.globalsymtable is hopefully }
  1205. { more robust than symtablestack and symtablestack.next }
  1206. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1207. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1208. end;
  1209. { units have seperate code for initialization and finalization }
  1210. potype_unitinit: ;
  1211. else
  1212. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1213. end;
  1214. { finalize paras data }
  1215. if assigned(aktprocdef.parast) then
  1216. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1217. { do we need to handle exceptions because of ansi/widestrings ? }
  1218. if not inlined and
  1219. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  1220. { but it's useless in init/final code of units }
  1221. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  1222. begin
  1223. { the exception helper routines modify all registers }
  1224. aktprocdef.usedregisters:=all_registers;
  1225. getlabel(noreraiselabel);
  1226. cg.g_pop_exception(list,noreraiselabel);
  1227. if (aktprocdef.proctypeoption=potype_constructor) then
  1228. begin
  1229. if assigned(procinfo^._class) then
  1230. begin
  1231. pd:=procinfo^._class.searchdestructor;
  1232. if assigned(pd) then
  1233. begin
  1234. getlabel(nodestroycall);
  1235. reference_reset_base(href,procinfo^.framepointer,procinfo^.selfpointer_offset);
  1236. cg.a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nodestroycall);
  1237. if is_class(procinfo^._class) then
  1238. begin
  1239. cg.a_param_const(list,OS_INT,1,2);
  1240. cg.a_param_reg(list,OS_ADDR,self_pointer_reg,1);
  1241. end
  1242. else if is_object(procinfo^._class) then
  1243. begin
  1244. cg.a_param_reg(list,OS_ADDR,self_pointer_reg,2);
  1245. reference_reset_symbol(href,newasmsymbol(procinfo^._class.vmt_mangledname),0);
  1246. cg.a_paramaddr_ref(list,href,1);
  1247. end
  1248. else
  1249. Internalerror(200006164);
  1250. if (po_virtualmethod in pd.procoptions) then
  1251. begin
  1252. reference_reset_base(href,self_pointer_reg,0);
  1253. tmpreg:=cg.get_scratch_reg_address(list);
  1254. cg.a_load_ref_reg(list,OS_ADDR,href,tmpreg);
  1255. reference_reset_base(href,tmpreg,procinfo^._class.vmtmethodoffset(pd.extnumber));
  1256. cg.free_scratch_reg(list,tmpreg);
  1257. cg.a_call_ref(list,href);
  1258. end
  1259. else
  1260. cg.a_call_name(list,pd.mangledname);
  1261. { not necessary because the result is never assigned in the
  1262. case of an exception (FK) }
  1263. cg.a_label(list,nodestroycall);
  1264. end;
  1265. end
  1266. end
  1267. else
  1268. begin
  1269. { no constructor }
  1270. { must be the return value finalized before reraising the exception? }
  1271. if (not is_void(aktprocdef.rettype.def)) and
  1272. (aktprocdef.rettype.def.needs_inittable) and
  1273. ((aktprocdef.rettype.def.deftype<>objectdef) or
  1274. not is_class(aktprocdef.rettype.def)) then
  1275. begin
  1276. reference_reset_base(href,procinfo^.framepointer,procinfo^.return_offset);
  1277. cg.g_finalize(list,aktprocdef.rettype.def,href,ret_in_param(aktprocdef.rettype.def));
  1278. end;
  1279. end;
  1280. cg.a_call_name(list,'FPC_RERAISE');
  1281. cg.a_label(list,noreraiselabel);
  1282. end;
  1283. { call __EXIT for main program }
  1284. if (not DLLsource) and
  1285. (not inlined) and
  1286. (aktprocdef.proctypeoption=potype_proginit) then
  1287. cg.a_call_name(list,'FPC_DO_EXIT');
  1288. { handle return value, this is not done for assembler routines when
  1289. they didn't reference the result variable }
  1290. usesacc:=false;
  1291. usesacchi:=false;
  1292. usesself:=false;
  1293. if not(po_assembler in aktprocdef.procoptions) or
  1294. (assigned(aktprocdef.funcretsym) and
  1295. (tfuncretsym(aktprocdef.funcretsym).refcount>1)) then
  1296. begin
  1297. if (aktprocdef.proctypeoption<>potype_constructor) then
  1298. handle_return_value(list,inlined,usesacc,usesacchi)
  1299. else
  1300. begin
  1301. { successful constructor deletes the zero flag }
  1302. { and returns self in eax }
  1303. { eax must be set to zero if the allocation failed !!! }
  1304. getlabel(okexitlabel);
  1305. cg.a_jmp_always(list,okexitlabel);
  1306. cg.a_label(list,faillabel);
  1307. cg.g_call_fail_helper(list);
  1308. cg.a_label(list,okexitlabel);
  1309. { for classes this is done after the call to }
  1310. { AfterConstruction }
  1311. if is_object(procinfo^._class) then
  1312. begin
  1313. cg.a_reg_alloc(list,accumulator);
  1314. cg.a_load_reg_reg(list,OS_ADDR,self_pointer_reg,accumulator);
  1315. usesacc:=true;
  1316. end;
  1317. {$ifdef i386}
  1318. list.concat(taicpu.op_reg_reg(A_TEST,S_L,R_ESI,R_ESI));
  1319. {$else}
  1320. {$warning constructor returns in flags for i386}
  1321. {$endif i386}
  1322. usesself:=true;
  1323. end;
  1324. end;
  1325. if aktexit2label.is_used and not aktexit2label.is_set then
  1326. cg.a_label(list,aktexit2label);
  1327. {$ifdef GDB}
  1328. if ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  1329. begin
  1330. getlabel(stabsendlabel);
  1331. cg.a_label(list,stabsendlabel);
  1332. end;
  1333. {$endif GDB}
  1334. { for the save all registers we can simply use a pusha,popa which
  1335. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1336. if (po_saveregisters in aktprocdef.procoptions) then
  1337. cg.g_restore_all_registers(list,usesself,usesacc,usesacchi)
  1338. else
  1339. { should we restore edi ? }
  1340. if (po_savestdregs in aktprocdef.procoptions) then
  1341. cg.g_restore_standard_registers(list);
  1342. { remove stackframe }
  1343. if not inlined then
  1344. begin
  1345. if (not nostackframe) then
  1346. cg.g_restore_frame_pointer(list)
  1347. else
  1348. if (tg.gettempsize<>0) then
  1349. cg.a_op_const_reg(list,OP_ADD,tg.gettempsize,STACK_POINTER_REG);
  1350. end;
  1351. { at last, the return is generated }
  1352. if not inlined then
  1353. begin
  1354. if (po_interrupt in aktprocdef.procoptions) then
  1355. cg.g_interrupt_stackframe_exit(list,usesself,usesacc,usesacchi)
  1356. else
  1357. cg.g_return_from_proc(list,parasize);
  1358. end;
  1359. if not inlined then
  1360. list.concat(Tai_symbol_end.Createname(aktprocdef.mangledname));
  1361. {$ifdef GDB}
  1362. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  1363. begin
  1364. if assigned(procinfo^._class) then
  1365. if (not assigned(procinfo^.parent) or
  1366. not assigned(procinfo^.parent^._class)) then
  1367. begin
  1368. if (po_classmethod in aktprocdef.procoptions) or
  1369. ((po_virtualmethod in aktprocdef.procoptions) and
  1370. (potype_constructor=aktprocdef.proctypeoption)) or
  1371. (po_staticmethod in aktprocdef.procoptions) then
  1372. begin
  1373. list.concat(Tai_stabs.Create(strpnew(
  1374. '"pvmt:p'+tstoreddef(pvmttype.def).numberstring+'",'+
  1375. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  1376. end
  1377. else
  1378. begin
  1379. if not(is_class(procinfo^._class)) then
  1380. st:='v'
  1381. else
  1382. st:='p';
  1383. list.concat(Tai_stabs.Create(strpnew(
  1384. '"$t:'+st+procinfo^._class.numberstring+'",'+
  1385. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  1386. end;
  1387. end
  1388. else
  1389. begin
  1390. if not is_class(procinfo^._class) then
  1391. st:='*'
  1392. else
  1393. st:='';
  1394. list.concat(Tai_stabs.Create(strpnew(
  1395. '"$t:r'+st+procinfo^._class.numberstring+'",'+
  1396. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[SELF_POINTER_REG]))));
  1397. end;
  1398. { define calling EBP as pseudo local var PM }
  1399. { this enables test if the function is a local one !! }
  1400. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  1401. list.concat(Tai_stabs.Create(strpnew(
  1402. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1403. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset))));
  1404. if (not is_void(aktprocdef.rettype.def)) then
  1405. begin
  1406. if ret_in_param(aktprocdef.rettype.def) then
  1407. list.concat(Tai_stabs.Create(strpnew(
  1408. '"'+aktprocsym.name+':X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1409. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  1410. else
  1411. list.concat(Tai_stabs.Create(strpnew(
  1412. '"'+aktprocsym.name+':X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1413. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  1414. if (m_result in aktmodeswitches) then
  1415. if ret_in_param(aktprocdef.rettype.def) then
  1416. list.concat(Tai_stabs.Create(strpnew(
  1417. '"RESULT:X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1418. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  1419. else
  1420. list.concat(Tai_stabs.Create(strpnew(
  1421. '"RESULT:X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1422. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  1423. end;
  1424. mangled_length:=length(aktprocdef.mangledname);
  1425. getmem(p,2*mangled_length+50);
  1426. strpcopy(p,'192,0,0,');
  1427. strpcopy(strend(p),aktprocdef.mangledname);
  1428. if (target_info.use_function_relative_addresses) then
  1429. begin
  1430. strpcopy(strend(p),'-');
  1431. strpcopy(strend(p),aktprocdef.mangledname);
  1432. end;
  1433. list.concat(Tai_stabn.Create(strnew(p)));
  1434. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1435. +aktprocdef.mangledname))));
  1436. p[0]:='2';p[1]:='2';p[2]:='4';
  1437. strpcopy(strend(p),'_end');}
  1438. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1439. if (target_info.use_function_relative_addresses) then
  1440. begin
  1441. strpcopy(strend(p),'-');
  1442. strpcopy(strend(p),aktprocdef.mangledname);
  1443. end;
  1444. list.concatlist(withdebuglist);
  1445. list.concat(Tai_stabn.Create(strnew(p)));
  1446. { strpnew('224,0,0,'
  1447. +aktprocdef.mangledname+'_end'))));}
  1448. freemem(p,2*mangled_length+50);
  1449. end;
  1450. {$endif GDB}
  1451. if inlined then
  1452. cleanup_regvars(list);
  1453. end;
  1454. procedure genimplicitunitinit(list : TAAsmoutput);
  1455. begin
  1456. { using current_module.globalsymtable is hopefully }
  1457. { more robust than symtablestack and symtablestack.next }
  1458. tsymtable(current_module.globalsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1459. tsymtable(current_module.localsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1460. list.insert(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  1461. list.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_init',0));
  1462. {$ifdef GDB}
  1463. if (cs_debuginfo in aktmoduleswitches) and
  1464. target_info.use_function_relative_addresses then
  1465. list.insert(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  1466. {$endif GDB}
  1467. cg.g_return_from_proc(list,0);
  1468. end;
  1469. procedure genimplicitunitfinal(list : TAAsmoutput);
  1470. begin
  1471. { using current_module.globalsymtable is hopefully }
  1472. { more robust than symtablestack and symtablestack.next }
  1473. tsymtable(current_module.globalsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1474. tsymtable(current_module.localsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1475. list.insert(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  1476. list.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_finalize',0));
  1477. {$ifdef GDB}
  1478. if (cs_debuginfo in aktmoduleswitches) and
  1479. target_info.use_function_relative_addresses then
  1480. list.insert(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  1481. {$endif GDB}
  1482. cg.g_return_from_proc(list,0);
  1483. end;
  1484. end.
  1485. {
  1486. $Log$
  1487. Revision 1.18 2002-07-01 16:23:53 peter
  1488. * cg64 patch
  1489. * basics for currency
  1490. * asnode updates for class and interface (not finished)
  1491. Revision 1.17 2002/05/20 13:30:40 carl
  1492. * bugfix of hdisponen (base must be set, not index)
  1493. * more portability fixes
  1494. Revision 1.16 2002/05/18 13:34:09 peter
  1495. * readded missing revisions
  1496. Revision 1.15 2002/05/16 19:46:37 carl
  1497. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1498. + try to fix temp allocation (still in ifdef)
  1499. + generic constructor calls
  1500. + start of tassembler / tmodulebase class cleanup
  1501. Revision 1.13 2002/05/13 19:54:37 peter
  1502. * removed n386ld and n386util units
  1503. * maybe_save/maybe_restore added instead of the old maybe_push
  1504. Revision 1.12 2002/05/12 19:58:36 carl
  1505. * some small portability fixes
  1506. Revision 1.11 2002/05/12 16:53:07 peter
  1507. * moved entry and exitcode to ncgutil and cgobj
  1508. * foreach gets extra argument for passing local data to the
  1509. iterator function
  1510. * -CR checks also class typecasts at runtime by changing them
  1511. into as
  1512. * fixed compiler to cycle with the -CR option
  1513. * fixed stabs with elf writer, finally the global variables can
  1514. be watched
  1515. * removed a lot of routines from cga unit and replaced them by
  1516. calls to cgobj
  1517. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1518. u32bit then the other is typecasted also to u32bit without giving
  1519. a rangecheck warning/error.
  1520. * fixed pascal calling method with reversing also the high tree in
  1521. the parast, detected by tcalcst3 test
  1522. Revision 1.10 2002/04/21 19:02:03 peter
  1523. * removed newn and disposen nodes, the code is now directly
  1524. inlined from pexpr
  1525. * -an option that will write the secondpass nodes to the .s file, this
  1526. requires EXTDEBUG define to actually write the info
  1527. * fixed various internal errors and crashes due recent code changes
  1528. Revision 1.9 2002/04/21 15:24:38 carl
  1529. + a_jmp_cond -> a_jmp_always (a_jmp_cond is NOT portable)
  1530. + changeregsize -> rg.makeregsize
  1531. Revision 1.8 2002/04/19 15:39:34 peter
  1532. * removed some more routines from cga
  1533. * moved location_force_reg/mem to ncgutil
  1534. * moved arrayconstructnode secondpass to ncgld
  1535. Revision 1.7 2002/04/15 18:58:47 carl
  1536. + target_info.size_of_pointer -> pointer_Size
  1537. Revision 1.6 2002/04/06 18:10:42 jonas
  1538. * several powerpc-related additions and fixes
  1539. Revision 1.5 2002/04/04 19:05:57 peter
  1540. * removed unused units
  1541. * use tlocation.size in cg.a_*loc*() routines
  1542. Revision 1.4 2002/04/02 17:11:28 peter
  1543. * tlocation,treference update
  1544. * LOC_CONSTANT added for better constant handling
  1545. * secondadd splitted in multiple routines
  1546. * location_force_reg added for loading a location to a register
  1547. of a specified size
  1548. * secondassignment parses now first the right and then the left node
  1549. (this is compatible with Kylix). This saves a lot of push/pop especially
  1550. with string operations
  1551. * adapted some routines to use the new cg methods
  1552. Revision 1.3 2002/03/31 20:26:34 jonas
  1553. + a_loadfpu_* and a_loadmm_* methods in tcg
  1554. * register allocation is now handled by a class and is mostly processor
  1555. independent (+rgobj.pas and i386/rgcpu.pas)
  1556. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1557. * some small improvements and fixes to the optimizer
  1558. * some register allocation fixes
  1559. * some fpuvaroffset fixes in the unary minus node
  1560. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1561. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1562. also better optimizable)
  1563. * fixed and optimized register saving/restoring for new/dispose nodes
  1564. * LOC_FPU locations now also require their "register" field to be set to
  1565. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1566. - list field removed of the tnode class because it's not used currently
  1567. and can cause hard-to-find bugs
  1568. Revision 1.2 2002/03/04 19:10:11 peter
  1569. * removed compiler warnings
  1570. }