ncgutil.pas 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Helper routines for all code generators
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,cpuinfo,
  22. globtype,
  23. cpubase,cgbase,parabase,cgutils,
  24. aasmbase,aasmtai,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bit}
  27. ,cg64f32
  28. {$endif cpu64bit}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. pusedregvars = ^tusedregvars;
  33. tusedregvars = record
  34. intregvars, fpuregvars, mmregvars: Tsuperregisterworklist;
  35. end;
  36. {
  37. Not used currently, implemented because I thought we had to
  38. synchronise around if/then/else as well, but not needed. May
  39. still be useful for SSA once we get around to implementing
  40. that (JM)
  41. pusedregvarscommon = ^tusedregvarscommon;
  42. tusedregvarscommon = record
  43. allregvars, commonregvars, myregvars: tusedregvars;
  44. end;
  45. }
  46. procedure firstcomplex(p : tbinarynode);
  47. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  48. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  49. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  50. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  51. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  52. procedure location_force_mmregscalar(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  53. { Retrieve the location of the data pointed to in location l, when the location is
  54. a register it is expected to contain the address of the data }
  55. procedure location_get_data_ref(list:TAAsmoutput;const l:tlocation;var ref:treference;loadref:boolean);
  56. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  57. procedure alloc_proc_symbol(pd: tprocdef);
  58. procedure gen_proc_symbol(list:Taasmoutput);
  59. procedure gen_proc_symbol_end(list:Taasmoutput);
  60. procedure gen_proc_entry_code(list:Taasmoutput);
  61. procedure gen_proc_exit_code(list:Taasmoutput);
  62. procedure gen_stack_check_size_para(list:Taasmoutput);
  63. procedure gen_stack_check_call(list:Taasmoutput);
  64. procedure gen_save_used_regs(list:TAAsmoutput);
  65. procedure gen_restore_used_regs(list:TAAsmoutput);
  66. procedure gen_initialize_code(list:TAAsmoutput);
  67. procedure gen_finalize_code(list:TAAsmoutput);
  68. procedure gen_entry_code(list:TAAsmoutput);
  69. procedure gen_exit_code(list:TAAsmoutput);
  70. procedure gen_load_para_value(list:TAAsmoutput);
  71. procedure gen_load_return_value(list:TAAsmoutput);
  72. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  73. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  74. procedure gen_load_vmt_register(list:taasmoutput;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  75. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  76. { adds the regvars used in n and its children to rv.allregvars,
  77. those which were already in rv.allregvars to rv.commonregvars and
  78. uses rv.myregvars as scratch (so that two uses of the same regvar
  79. in a single tree to make it appear in commonregvars). Useful to
  80. find out which regvars are used in two different node trees
  81. (e.g. in the "else" and "then" path, or in various case blocks }
  82. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  83. procedure gen_sync_regvars(list:TAAsmoutput; var rv: tusedregvars);
  84. {#
  85. Allocate the buffers for exception management and setjmp environment.
  86. Return a pointer to these buffers, send them to the utility routine
  87. so they are registered, and then call setjmp.
  88. Then compare the result of setjmp with 0, and if not equal
  89. to zero, then jump to exceptlabel.
  90. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  91. It is to note that this routine may be called *after* the stackframe of a
  92. routine has been called, therefore on machines where the stack cannot
  93. be modified, all temps should be allocated on the heap instead of the
  94. stack.
  95. }
  96. const
  97. EXCEPT_BUF_SIZE = 3*sizeof(aint);
  98. type
  99. texceptiontemps=record
  100. jmpbuf,
  101. envbuf,
  102. reasonbuf : treference;
  103. end;
  104. procedure get_exception_temps(list:taasmoutput;var t:texceptiontemps);
  105. procedure unget_exception_temps(list:taasmoutput;const t:texceptiontemps);
  106. procedure new_exception(list:TAAsmoutput;const t:texceptiontemps;exceptlabel:tasmlabel);
  107. procedure free_exception(list:TAAsmoutput;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  108. procedure insertbssdata(sym : tglobalvarsym);
  109. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  110. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  111. { rtti and init/final }
  112. procedure generate_rtti(p:Ttypesym);
  113. procedure generate_inittable(p:tsym);
  114. procedure location_free(list: taasmoutput; const location : TLocation);
  115. function getprocalign : longint;
  116. procedure gen_pic_helpers(list : taasmoutput);
  117. procedure gen_got_load(list : taasmoutput);
  118. implementation
  119. uses
  120. version,
  121. cutils,cclasses,
  122. globals,systems,verbose,
  123. ppu,defutil,
  124. procinfo,paramgr,fmodule,
  125. regvars,dwarf,dbgbase,
  126. pass_1,pass_2,
  127. ncon,nld,nutils,
  128. tgobj,cgobj
  129. {$ifdef powerpc}
  130. , cpupi
  131. {$endif}
  132. {$ifdef powerpc64}
  133. , cpupi
  134. {$endif}
  135. ;
  136. {*****************************************************************************
  137. Misc Helpers
  138. *****************************************************************************}
  139. procedure location_free(list: taasmoutput; const location : TLocation);
  140. begin
  141. case location.loc of
  142. LOC_VOID:
  143. ;
  144. LOC_REGISTER,
  145. LOC_CREGISTER:
  146. begin
  147. if getsupreg(location.register)<first_int_imreg then
  148. cg.ungetcpuregister(list,location.register);
  149. end;
  150. LOC_FPUREGISTER,
  151. LOC_CFPUREGISTER:
  152. begin
  153. if getsupreg(location.register)<first_fpu_imreg then
  154. cg.ungetcpuregister(list,location.register);
  155. end;
  156. LOC_MMREGISTER,
  157. LOC_CMMREGISTER :
  158. begin
  159. if getsupreg(location.register)<first_mm_imreg then
  160. cg.ungetcpuregister(list,location.register);
  161. end;
  162. LOC_REFERENCE,
  163. LOC_CREFERENCE :
  164. begin
  165. {$ifdef cputargethasfixedstack}
  166. location_freetemp(list,location);
  167. {$endif cputargethasfixedstack}
  168. end;
  169. else
  170. internalerror(2004110211);
  171. end;
  172. end;
  173. { DO NOT RELY on the fact that the tnode is not yet swaped
  174. because of inlining code PM }
  175. procedure firstcomplex(p : tbinarynode);
  176. var
  177. hp : tnode;
  178. begin
  179. { always calculate boolean AND and OR from left to right }
  180. if (p.nodetype in [orn,andn]) and
  181. is_boolean(p.left.resulttype.def) then
  182. begin
  183. if nf_swaped in p.flags then
  184. internalerror(234234);
  185. end
  186. else
  187. if (
  188. (p.expectloc=LOC_FPUREGISTER) and
  189. (p.right.registersfpu > p.left.registersfpu)
  190. ) or
  191. (
  192. (
  193. (
  194. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  195. (p.expectloc<>LOC_FPUREGISTER)
  196. ) and
  197. (p.left.registersint<p.right.registersint)
  198. )
  199. ) then
  200. begin
  201. hp:=p.left;
  202. p.left:=p.right;
  203. p.right:=hp;
  204. if nf_swaped in p.flags then
  205. exclude(p.flags,nf_swaped)
  206. else
  207. include(p.flags,nf_swaped);
  208. end;
  209. end;
  210. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  211. {
  212. produces jumps to true respectively false labels using boolean expressions
  213. depending on whether the loading of regvars is currently being
  214. synchronized manually (such as in an if-node) or automatically (most of
  215. the other cases where this procedure is called), loadregvars can be
  216. "lr_load_regvars" or "lr_dont_load_regvars"
  217. }
  218. var
  219. opsize : tcgsize;
  220. storepos : tfileposinfo;
  221. begin
  222. if nf_error in p.flags then
  223. exit;
  224. storepos:=aktfilepos;
  225. aktfilepos:=p.fileinfo;
  226. if is_boolean(p.resulttype.def) then
  227. begin
  228. {$ifdef OLDREGVARS}
  229. if loadregvars = lr_load_regvars then
  230. load_all_regvars(list);
  231. {$endif OLDREGVARS}
  232. if is_constboolnode(p) then
  233. begin
  234. if tordconstnode(p).value<>0 then
  235. cg.a_jmp_always(list,truelabel)
  236. else
  237. cg.a_jmp_always(list,falselabel)
  238. end
  239. else
  240. begin
  241. opsize:=def_cgsize(p.resulttype.def);
  242. case p.location.loc of
  243. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  244. begin
  245. {$ifdef OLDREGVARS}
  246. if (p.location.loc = LOC_CREGISTER) then
  247. load_regvar_reg(list,p.location.register);
  248. {$endif OLDREGVARS}
  249. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,truelabel);
  250. cg.a_jmp_always(list,falselabel);
  251. end;
  252. LOC_JUMP:
  253. ;
  254. {$ifdef cpuflags}
  255. LOC_FLAGS :
  256. begin
  257. cg.a_jmp_flags(list,p.location.resflags,truelabel);
  258. cg.a_jmp_always(list,falselabel);
  259. end;
  260. {$endif cpuflags}
  261. else
  262. begin
  263. printnode(output,p);
  264. internalerror(200308241);
  265. end;
  266. end;
  267. end;
  268. end
  269. else
  270. internalerror(200112305);
  271. aktfilepos:=storepos;
  272. end;
  273. (*
  274. This code needs fixing. It is not safe to use rgint; on the m68000 it
  275. would be rgaddr.
  276. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  277. begin
  278. case t.loc of
  279. LOC_REGISTER:
  280. begin
  281. { can't be a regvar, since it would be LOC_CREGISTER then }
  282. exclude(regs,getsupreg(t.register));
  283. if t.register64.reghi<>NR_NO then
  284. exclude(regs,getsupreg(t.register64.reghi));
  285. end;
  286. LOC_CREFERENCE,LOC_REFERENCE:
  287. begin
  288. if not(cs_regvars in aktglobalswitches) or
  289. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  290. exclude(regs,getsupreg(t.reference.base));
  291. if not(cs_regvars in aktglobalswitches) or
  292. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  293. exclude(regs,getsupreg(t.reference.index));
  294. end;
  295. end;
  296. end;
  297. *)
  298. {*****************************************************************************
  299. EXCEPTION MANAGEMENT
  300. *****************************************************************************}
  301. procedure get_exception_temps(list:taasmoutput;var t:texceptiontemps);
  302. var
  303. sym : ttypesym;
  304. begin
  305. if jmp_buf_size=-1 then
  306. begin
  307. searchsystype('JMP_BUF',sym);
  308. jmp_buf_size:=sym.restype.def.size;
  309. end;
  310. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistent,t.envbuf);
  311. tg.GetTemp(list,jmp_buf_size,tt_persistent,t.jmpbuf);
  312. tg.GetTemp(list,sizeof(aint),tt_persistent,t.reasonbuf);
  313. end;
  314. procedure unget_exception_temps(list:taasmoutput;const t:texceptiontemps);
  315. begin
  316. tg.Ungettemp(list,t.jmpbuf);
  317. tg.ungettemp(list,t.envbuf);
  318. tg.ungettemp(list,t.reasonbuf);
  319. end;
  320. procedure new_exception(list:TAAsmoutput;const t:texceptiontemps;exceptlabel:tasmlabel);
  321. var
  322. paraloc1,paraloc2,paraloc3 : tcgpara;
  323. begin
  324. paraloc1.init;
  325. paraloc2.init;
  326. paraloc3.init;
  327. paramanager.getintparaloc(pocall_default,1,paraloc1);
  328. paramanager.getintparaloc(pocall_default,2,paraloc2);
  329. paramanager.getintparaloc(pocall_default,3,paraloc3);
  330. paramanager.allocparaloc(list,paraloc3);
  331. cg.a_paramaddr_ref(list,t.envbuf,paraloc3);
  332. paramanager.allocparaloc(list,paraloc2);
  333. cg.a_paramaddr_ref(list,t.jmpbuf,paraloc2);
  334. { push type of exceptionframe }
  335. paramanager.allocparaloc(list,paraloc1);
  336. cg.a_param_const(list,OS_S32,1,paraloc1);
  337. paramanager.freeparaloc(list,paraloc3);
  338. paramanager.freeparaloc(list,paraloc2);
  339. paramanager.freeparaloc(list,paraloc1);
  340. cg.allocallcpuregisters(list);
  341. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  342. cg.deallocallcpuregisters(list);
  343. paramanager.getintparaloc(pocall_default,1,paraloc1);
  344. paramanager.allocparaloc(list,paraloc1);
  345. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  346. paramanager.freeparaloc(list,paraloc1);
  347. cg.allocallcpuregisters(list);
  348. cg.a_call_name(list,'FPC_SETJMP');
  349. cg.deallocallcpuregisters(list);
  350. cg.g_exception_reason_save(list, t.reasonbuf);
  351. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  352. paraloc1.done;
  353. paraloc2.done;
  354. paraloc3.done;
  355. end;
  356. procedure free_exception(list:TAAsmoutput;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  357. begin
  358. cg.allocallcpuregisters(list);
  359. cg.a_call_name(list,'FPC_POPADDRSTACK');
  360. cg.deallocallcpuregisters(list);
  361. if not onlyfree then
  362. begin
  363. cg.g_exception_reason_load(list, t.reasonbuf);
  364. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  365. end;
  366. end;
  367. {*****************************************************************************
  368. TLocation
  369. *****************************************************************************}
  370. {$ifndef cpu64bit}
  371. { 32-bit version }
  372. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  373. var
  374. hregister,
  375. hregisterhi : tregister;
  376. hreg64 : tregister64;
  377. hl : tasmlabel;
  378. oldloc : tlocation;
  379. const_location: boolean;
  380. begin
  381. oldloc:=l;
  382. if dst_size=OS_NO then
  383. internalerror(200309144);
  384. { handle transformations to 64bit separate }
  385. if dst_size in [OS_64,OS_S64] then
  386. begin
  387. if not (l.size in [OS_64,OS_S64]) then
  388. begin
  389. { load a smaller size to OS_64 }
  390. if l.loc=LOC_REGISTER then
  391. begin
  392. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  393. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  394. end
  395. else
  396. hregister:=cg.getintregister(list,OS_INT);
  397. { load value in low register }
  398. case l.loc of
  399. LOC_FLAGS :
  400. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  401. LOC_JUMP :
  402. begin
  403. cg.a_label(list,truelabel);
  404. cg.a_load_const_reg(list,OS_INT,1,hregister);
  405. objectlibrary.getjumplabel(hl);
  406. cg.a_jmp_always(list,hl);
  407. cg.a_label(list,falselabel);
  408. cg.a_load_const_reg(list,OS_INT,0,hregister);
  409. cg.a_label(list,hl);
  410. end;
  411. else
  412. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  413. end;
  414. { reset hi part, take care of the signed bit of the current value }
  415. hregisterhi:=cg.getintregister(list,OS_INT);
  416. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  417. begin
  418. if l.loc=LOC_CONSTANT then
  419. begin
  420. if (longint(l.value)<0) then
  421. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  422. else
  423. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  424. end
  425. else
  426. begin
  427. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  428. hregisterhi);
  429. end;
  430. end
  431. else
  432. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  433. location_reset(l,LOC_REGISTER,dst_size);
  434. l.register64.reglo:=hregister;
  435. l.register64.reghi:=hregisterhi;
  436. end
  437. else
  438. begin
  439. { 64bit to 64bit }
  440. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  441. begin
  442. hregister:=l.register64.reglo;
  443. hregisterhi:=l.register64.reghi;
  444. const_location := true;
  445. end
  446. else
  447. begin
  448. hregister:=cg.getintregister(list,OS_INT);
  449. hregisterhi:=cg.getintregister(list,OS_INT);
  450. const_location := false;
  451. end;
  452. hreg64.reglo:=hregister;
  453. hreg64.reghi:=hregisterhi;
  454. { load value in new register }
  455. cg64.a_load64_loc_reg(list,l,hreg64);
  456. if not const_location then
  457. location_reset(l,LOC_REGISTER,dst_size)
  458. else
  459. location_reset(l,LOC_CREGISTER,dst_size);
  460. l.register64.reglo:=hregister;
  461. l.register64.reghi:=hregisterhi;
  462. end;
  463. end
  464. else
  465. begin
  466. {Do not bother to recycle the existing register. The register
  467. allocator eliminates unnecessary moves, so it's not needed
  468. and trying to recycle registers can cause problems because
  469. the registers changes size and may need aditional constraints.
  470. Not if it's about LOC_CREGISTER's (JM)
  471. }
  472. const_location :=
  473. (maybeconst) and
  474. (l.loc = LOC_CREGISTER) and
  475. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  476. ((l.size = dst_size) or
  477. (TCGSize2Size[l.size] = TCGSize2Size[OS_INT]));
  478. if not const_location then
  479. hregister:=cg.getintregister(list,dst_size)
  480. else
  481. hregister := l.register;
  482. { load value in new register }
  483. case l.loc of
  484. LOC_FLAGS :
  485. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  486. LOC_JUMP :
  487. begin
  488. cg.a_label(list,truelabel);
  489. cg.a_load_const_reg(list,dst_size,1,hregister);
  490. objectlibrary.getjumplabel(hl);
  491. cg.a_jmp_always(list,hl);
  492. cg.a_label(list,falselabel);
  493. cg.a_load_const_reg(list,dst_size,0,hregister);
  494. cg.a_label(list,hl);
  495. end;
  496. else
  497. begin
  498. { load_loc_reg can only handle size >= l.size, when the
  499. new size is smaller then we need to adjust the size
  500. of the orignal and maybe recalculate l.register for i386 }
  501. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  502. begin
  503. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  504. l.register:=cg.makeregsize(list,l.register,dst_size);
  505. { for big endian systems, the reference's offset must }
  506. { be increased in this case, since they have the }
  507. { MSB first in memory and e.g. byte(word_var) should }
  508. { return the second byte in this case (JM) }
  509. if (target_info.endian = ENDIAN_BIG) and
  510. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  511. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  512. {$ifdef x86}
  513. l.size:=dst_size;
  514. {$endif x86}
  515. end;
  516. cg.a_load_loc_reg(list,dst_size,l,hregister);
  517. {$ifndef x86}
  518. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  519. l.size:=dst_size;
  520. {$endif not x86}
  521. end;
  522. end;
  523. if not const_location then
  524. location_reset(l,LOC_REGISTER,dst_size)
  525. else
  526. location_reset(l,LOC_CREGISTER,dst_size);
  527. l.register:=hregister;
  528. end;
  529. { Release temp when it was a reference }
  530. if oldloc.loc=LOC_REFERENCE then
  531. location_freetemp(list,oldloc);
  532. end;
  533. {$else cpu64bit}
  534. { 64-bit version }
  535. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  536. var
  537. hregister : tregister;
  538. hl : tasmlabel;
  539. oldloc : tlocation;
  540. begin
  541. oldloc:=l;
  542. hregister:=cg.getintregister(list,dst_size);
  543. { load value in new register }
  544. case l.loc of
  545. LOC_FLAGS :
  546. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  547. LOC_JUMP :
  548. begin
  549. cg.a_label(list,truelabel);
  550. cg.a_load_const_reg(list,dst_size,1,hregister);
  551. objectlibrary.getjumplabel(hl);
  552. cg.a_jmp_always(list,hl);
  553. cg.a_label(list,falselabel);
  554. cg.a_load_const_reg(list,dst_size,0,hregister);
  555. cg.a_label(list,hl);
  556. end;
  557. else
  558. begin
  559. { load_loc_reg can only handle size >= l.size, when the
  560. new size is smaller then we need to adjust the size
  561. of the orignal and maybe recalculate l.register for i386 }
  562. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  563. begin
  564. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  565. l.register:=cg.makeregsize(list,l.register,dst_size);
  566. { for big endian systems, the reference's offset must }
  567. { be increased in this case, since they have the }
  568. { MSB first in memory and e.g. byte(word_var) should }
  569. { return the second byte in this case (JM) }
  570. if (target_info.endian = ENDIAN_BIG) and
  571. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  572. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  573. {$ifdef x86}
  574. l.size:=dst_size;
  575. {$endif x86}
  576. end;
  577. cg.a_load_loc_reg(list,dst_size,l,hregister);
  578. {$ifndef x86}
  579. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  580. l.size:=dst_size;
  581. {$endif not x86}
  582. end;
  583. end;
  584. if (l.loc <> LOC_CREGISTER) or
  585. not maybeconst then
  586. location_reset(l,LOC_REGISTER,dst_size)
  587. else
  588. location_reset(l,LOC_CREGISTER,dst_size);
  589. l.register:=hregister;
  590. { Release temp when it was a reference }
  591. if oldloc.loc=LOC_REFERENCE then
  592. location_freetemp(list,oldloc);
  593. end;
  594. {$endif cpu64bit}
  595. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  596. var
  597. reg : tregister;
  598. href : treference;
  599. begin
  600. if (l.loc<>LOC_FPUREGISTER) and
  601. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  602. begin
  603. { if it's in an mm register, store to memory first }
  604. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  605. begin
  606. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  607. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  608. location_reset(l,LOC_REFERENCE,l.size);
  609. l.reference:=href;
  610. end;
  611. reg:=cg.getfpuregister(list,l.size);
  612. cg.a_loadfpu_loc_reg(list,l,reg);
  613. location_freetemp(list,l);
  614. location_reset(l,LOC_FPUREGISTER,l.size);
  615. l.register:=reg;
  616. end;
  617. end;
  618. procedure location_force_mmregscalar(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  619. var
  620. reg : tregister;
  621. href : treference;
  622. begin
  623. if (l.loc<>LOC_MMREGISTER) and
  624. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  625. begin
  626. { if it's in an fpu register, store to memory first }
  627. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  628. begin
  629. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  630. cg.a_loadfpu_reg_ref(list,l.size,l.register,href);
  631. location_reset(l,LOC_REFERENCE,l.size);
  632. l.reference:=href;
  633. end;
  634. reg:=cg.getmmregister(list,l.size);
  635. cg.a_loadmm_loc_reg(list,l.size,l,reg,mms_movescalar);
  636. location_freetemp(list,l);
  637. location_reset(l,LOC_MMREGISTER,l.size);
  638. l.register:=reg;
  639. end;
  640. end;
  641. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  642. var
  643. r : treference;
  644. begin
  645. case l.loc of
  646. LOC_FPUREGISTER,
  647. LOC_CFPUREGISTER :
  648. begin
  649. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  650. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  651. location_reset(l,LOC_REFERENCE,l.size);
  652. l.reference:=r;
  653. end;
  654. LOC_MMREGISTER,
  655. LOC_CMMREGISTER:
  656. begin
  657. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  658. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  659. location_reset(l,LOC_REFERENCE,l.size);
  660. l.reference:=r;
  661. end;
  662. LOC_CONSTANT,
  663. LOC_REGISTER,
  664. LOC_CREGISTER :
  665. begin
  666. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  667. {$ifndef cpu64bit}
  668. if l.size in [OS_64,OS_S64] then
  669. cg64.a_load64_loc_ref(list,l,r)
  670. else
  671. {$endif cpu64bit}
  672. cg.a_load_loc_ref(list,l.size,l,r);
  673. location_reset(l,LOC_REFERENCE,l.size);
  674. l.reference:=r;
  675. end;
  676. LOC_CREFERENCE,
  677. LOC_REFERENCE : ;
  678. else
  679. internalerror(200203219);
  680. end;
  681. end;
  682. procedure location_get_data_ref(list:TAAsmoutput;const l:tlocation;var ref:treference;loadref:boolean);
  683. begin
  684. case l.loc of
  685. LOC_REGISTER,
  686. LOC_CREGISTER :
  687. begin
  688. if not loadref then
  689. internalerror(200410231);
  690. reference_reset_base(ref,l.register,0);
  691. end;
  692. LOC_REFERENCE,
  693. LOC_CREFERENCE :
  694. begin
  695. if loadref then
  696. begin
  697. reference_reset_base(ref,cg.getaddressregister(list),0);
  698. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  699. end
  700. else
  701. ref:=l.reference;
  702. end;
  703. else
  704. internalerror(200309181);
  705. end;
  706. end;
  707. {*****************************************************************************
  708. Maybe_Save
  709. *****************************************************************************}
  710. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  711. begin
  712. {$ifdef i386}
  713. if (needed>=maxfpuregs) and
  714. (l.loc = LOC_FPUREGISTER) then
  715. begin
  716. location_force_mem(list,l);
  717. maybe_pushfpu:=true;
  718. end
  719. else
  720. maybe_pushfpu:=false;
  721. {$else i386}
  722. maybe_pushfpu:=false;
  723. {$endif i386}
  724. end;
  725. {****************************************************************************
  726. Init/Finalize Code
  727. ****************************************************************************}
  728. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  729. var
  730. href : treference;
  731. hreg : tregister;
  732. list : TAAsmoutput;
  733. hsym : tparavarsym;
  734. l : longint;
  735. localcopyloc : tlocation;
  736. begin
  737. list:=taasmoutput(arg);
  738. if (tsym(p).typ=paravarsym) and
  739. (tparavarsym(p).varspez=vs_value) and
  740. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  741. begin
  742. location_get_data_ref(list,tparavarsym(p).localloc,href,true);
  743. if is_open_array(tparavarsym(p).vartype.def) or
  744. is_array_of_const(tparavarsym(p).vartype.def) then
  745. begin
  746. { cdecl functions don't have a high pointer so it is not possible to generate
  747. a local copy }
  748. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  749. begin
  750. hsym:=tparavarsym(tsym(p).owner.search('high'+p.name));
  751. if not assigned(hsym) then
  752. internalerror(200306061);
  753. hreg:=cg.getaddressregister(list);
  754. cg.g_copyvaluepara_openarray(list,href,hsym.localloc,tarraydef(tparavarsym(p).vartype.def).elesize,hreg);
  755. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).localloc);
  756. end;
  757. end
  758. else
  759. begin
  760. { Allocate space for the local copy }
  761. l:=tparavarsym(p).getsize;
  762. localcopyloc.loc:=LOC_REFERENCE;
  763. localcopyloc.size:=int_cgsize(l);
  764. tg.GetLocal(list,l,tparavarsym(p).vartype.def,localcopyloc.reference);
  765. { Copy data }
  766. if is_shortstring(tparavarsym(p).vartype.def) then
  767. begin
  768. { this code is only executed before the code for the body and the entry/exit code is generated
  769. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  770. }
  771. include(current_procinfo.flags,pi_do_call);
  772. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vartype.def).len)
  773. end
  774. else
  775. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vartype.def.size);
  776. { update localloc of varsym }
  777. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  778. tparavarsym(p).localloc:=localcopyloc;
  779. end;
  780. end;
  781. end;
  782. { initializes the regvars from staticsymtable with 0 }
  783. procedure initialize_regvars(p : tnamedindexitem;arg:pointer);
  784. begin
  785. if (tsym(p).typ=globalvarsym) then
  786. begin
  787. case tglobalvarsym(p).localloc.loc of
  788. LOC_CREGISTER :
  789. cg.a_load_const_reg(taasmoutput(arg),reg_cgsize(tglobalvarsym(p).localloc.register),0,
  790. tglobalvarsym(p).localloc.register);
  791. LOC_REFERENCE : ;
  792. LOC_CMMREGISTER :
  793. { clear the whole register }
  794. cg.a_opmm_reg_reg(taasmoutput(arg),OP_XOR,reg_cgsize(tglobalvarsym(p).localloc.register),
  795. tglobalvarsym(p).localloc.register,
  796. tglobalvarsym(p).localloc.register,
  797. nil);
  798. LOC_CFPUREGISTER :
  799. ;
  800. else
  801. internalerror(200410124);
  802. end;
  803. end;
  804. end;
  805. { generates the code for initialisation of local data }
  806. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  807. var
  808. oldexprasmlist : TAAsmoutput;
  809. hp : tnode;
  810. begin
  811. if (tsym(p).typ in [globalvarsym,localvarsym]) and
  812. (tabstractvarsym(p).refs>0) and
  813. not(is_class(tabstractvarsym(p).vartype.def)) and
  814. tabstractvarsym(p).vartype.def.needs_inittable then
  815. begin
  816. oldexprasmlist:=exprasmlist;
  817. exprasmlist:=taasmoutput(arg);
  818. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  819. firstpass(hp);
  820. secondpass(hp);
  821. hp.free;
  822. exprasmlist:=oldexprasmlist;
  823. end;
  824. end;
  825. procedure finalize_sym(asmlist:taasmoutput;sym:tsym);
  826. var
  827. hp : tnode;
  828. oldexprasmlist : TAAsmoutput;
  829. begin
  830. include(current_procinfo.flags,pi_needs_implicit_finally);
  831. oldexprasmlist:=exprasmlist;
  832. exprasmlist:=asmlist;
  833. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  834. firstpass(hp);
  835. secondpass(hp);
  836. hp.free;
  837. exprasmlist:=oldexprasmlist;
  838. end;
  839. { generates the code for finalisation of local variables }
  840. procedure finalize_local_vars(p : tnamedindexitem;arg:pointer);
  841. begin
  842. if (tsym(p).typ=localvarsym) and
  843. (tlocalvarsym(p).refs>0) and
  844. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  845. not(is_class(tlocalvarsym(p).vartype.def)) and
  846. tlocalvarsym(p).vartype.def.needs_inittable then
  847. finalize_sym(taasmoutput(arg),tsym(p));
  848. end;
  849. { generates the code for finalisation of local typedconsts }
  850. procedure finalize_local_typedconst(p : tnamedindexitem;arg:pointer);
  851. var
  852. i : longint;
  853. pd : tprocdef;
  854. begin
  855. case tsym(p).typ of
  856. typedconstsym :
  857. begin
  858. if ttypedconstsym(p).is_writable and
  859. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  860. finalize_sym(taasmoutput(arg),tsym(p));
  861. end;
  862. procsym :
  863. begin
  864. for i:=1 to tprocsym(p).procdef_count do
  865. begin
  866. pd:=tprocsym(p).procdef[i];
  867. if assigned(pd.localst) and
  868. (pd.procsym=tprocsym(p)) and
  869. (pd.localst.symtabletype<>staticsymtable) then
  870. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  871. end;
  872. end;
  873. end;
  874. end;
  875. { generates the code for finalization of static symtable and
  876. all local (static) typedconsts }
  877. procedure finalize_static_data(p : tnamedindexitem;arg:pointer);
  878. var
  879. i : longint;
  880. pd : tprocdef;
  881. begin
  882. case tsym(p).typ of
  883. globalvarsym :
  884. begin
  885. if (tglobalvarsym(p).refs>0) and
  886. not(vo_is_funcret in tglobalvarsym(p).varoptions) and
  887. not(is_class(tglobalvarsym(p).vartype.def)) and
  888. tglobalvarsym(p).vartype.def.needs_inittable then
  889. finalize_sym(taasmoutput(arg),tsym(p));
  890. end;
  891. typedconstsym :
  892. begin
  893. if ttypedconstsym(p).is_writable and
  894. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  895. finalize_sym(taasmoutput(arg),tsym(p));
  896. end;
  897. procsym :
  898. begin
  899. for i:=1 to tprocsym(p).procdef_count do
  900. begin
  901. pd:=tprocsym(p).procdef[i];
  902. if assigned(pd.localst) and
  903. (pd.procsym=tprocsym(p)) and
  904. (pd.localst.symtabletype<>staticsymtable) then
  905. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  906. end;
  907. end;
  908. end;
  909. end;
  910. { generates the code for incrementing the reference count of parameters and
  911. initialize out parameters }
  912. procedure init_paras(p : tnamedindexitem;arg:pointer);
  913. var
  914. href : treference;
  915. tmpreg : tregister;
  916. list : TAAsmoutput;
  917. begin
  918. list:=taasmoutput(arg);
  919. if (tsym(p).typ=paravarsym) and
  920. not is_class_or_interface(tparavarsym(p).vartype.def) and
  921. tparavarsym(p).vartype.def.needs_inittable then
  922. begin
  923. case tparavarsym(p).varspez of
  924. vs_value :
  925. begin
  926. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  927. cg.g_incrrefcount(list,tparavarsym(p).vartype.def,href);
  928. end;
  929. vs_out :
  930. begin
  931. tmpreg:=cg.getaddressregister(list);
  932. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).localloc,tmpreg);
  933. reference_reset_base(href,tmpreg,0);
  934. cg.g_initialize(list,tparavarsym(p).vartype.def,href);
  935. end;
  936. end;
  937. end;
  938. end;
  939. { generates the code for decrementing the reference count of parameters }
  940. procedure final_paras(p : tnamedindexitem;arg:pointer);
  941. var
  942. list : TAAsmoutput;
  943. href : treference;
  944. begin
  945. if not(tsym(p).typ=paravarsym) then
  946. exit;
  947. list:=taasmoutput(arg);
  948. if not is_class_or_interface(tparavarsym(p).vartype.def) and
  949. tparavarsym(p).vartype.def.needs_inittable then
  950. begin
  951. if (tparavarsym(p).varspez=vs_value) then
  952. begin
  953. include(current_procinfo.flags,pi_needs_implicit_finally);
  954. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  955. cg.g_decrrefcount(list,tparavarsym(p).vartype.def,href);
  956. end;
  957. end
  958. else
  959. if (tparavarsym(p).varspez=vs_value) and
  960. (is_open_array(tparavarsym(p).vartype.def) or
  961. is_array_of_const(tparavarsym(p).vartype.def)) then
  962. begin
  963. { cdecl functions don't have a high pointer so it is not possible to generate
  964. a local copy }
  965. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  966. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  967. end;
  968. end;
  969. { Initialize temp ansi/widestrings,interfaces }
  970. procedure inittempvariables(list:taasmoutput);
  971. var
  972. hp : ptemprecord;
  973. href : treference;
  974. begin
  975. hp:=tg.templist;
  976. while assigned(hp) do
  977. begin
  978. if assigned(hp^.def) and
  979. hp^.def.needs_inittable then
  980. begin
  981. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  982. cg.g_initialize(list,hp^.def,href);
  983. end;
  984. hp:=hp^.next;
  985. end;
  986. end;
  987. procedure finalizetempvariables(list:taasmoutput);
  988. var
  989. hp : ptemprecord;
  990. href : treference;
  991. begin
  992. hp:=tg.templist;
  993. while assigned(hp) do
  994. begin
  995. if assigned(hp^.def) and
  996. hp^.def.needs_inittable then
  997. begin
  998. include(current_procinfo.flags,pi_needs_implicit_finally);
  999. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  1000. cg.g_finalize(list,hp^.def,href);
  1001. end;
  1002. hp:=hp^.next;
  1003. end;
  1004. end;
  1005. procedure gen_load_return_value(list:TAAsmoutput);
  1006. var
  1007. {$ifndef cpu64bit}
  1008. href : treference;
  1009. {$endif cpu64bit}
  1010. ressym : tabstractnormalvarsym;
  1011. resloc,
  1012. restmploc : tlocation;
  1013. hreg : tregister;
  1014. funcretloc : tlocation;
  1015. begin
  1016. { Is the loading needed? }
  1017. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  1018. (
  1019. (po_assembler in current_procinfo.procdef.procoptions) and
  1020. (not(assigned(current_procinfo.procdef.funcretsym)) or
  1021. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  1022. ) then
  1023. exit;
  1024. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  1025. { constructors return self }
  1026. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1027. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.search('self'))
  1028. else
  1029. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1030. if (ressym.refs>0) then
  1031. begin
  1032. {$ifdef OLDREGVARS}
  1033. case ressym.localloc.loc of
  1034. LOC_CFPUREGISTER,
  1035. LOC_FPUREGISTER:
  1036. begin
  1037. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1038. restmploc.register:=ressym.localloc.register;
  1039. end;
  1040. LOC_CREGISTER,
  1041. LOC_REGISTER:
  1042. begin
  1043. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1044. restmploc.register:=ressym.localloc.register;
  1045. end;
  1046. LOC_MMREGISTER:
  1047. begin
  1048. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1049. restmploc.register:=ressym.localloc.register;
  1050. end;
  1051. LOC_REFERENCE:
  1052. begin
  1053. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1054. restmploc.reference:=ressym.localloc.reference;
  1055. end;
  1056. else
  1057. internalerror(200309184);
  1058. end;
  1059. {$else}
  1060. restmploc:=ressym.localloc;
  1061. {$endif}
  1062. { Here, we return the function result. In most architectures, the value is
  1063. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1064. function returns in a register and the caller receives it in an other one }
  1065. case funcretloc.loc of
  1066. LOC_REGISTER:
  1067. begin
  1068. {$ifndef cpu64bit}
  1069. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1070. begin
  1071. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1072. if resloc.loc<>LOC_REGISTER then
  1073. internalerror(200409141);
  1074. { Load low and high register separate to generate better register
  1075. allocation info }
  1076. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1077. begin
  1078. cg.getcpuregister(list,resloc.register64.reglo);
  1079. cg.ungetcpuregister(list,resloc.register64.reglo);
  1080. { for the optimizer }
  1081. cg.a_reg_alloc(list,resloc.register64.reglo);
  1082. end;
  1083. case restmploc.loc of
  1084. LOC_REFERENCE :
  1085. begin
  1086. href:=restmploc.reference;
  1087. if target_info.endian=ENDIAN_BIG then
  1088. inc(href.offset,4);
  1089. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1090. end;
  1091. LOC_CREGISTER :
  1092. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1093. else
  1094. internalerror(200409203);
  1095. end;
  1096. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1097. begin
  1098. cg.getcpuregister(list,resloc.register64.reghi);
  1099. cg.ungetcpuregister(list,resloc.register64.reghi);
  1100. { for the optimizer }
  1101. cg.a_reg_alloc(list,resloc.register64.reghi);
  1102. end;
  1103. case restmploc.loc of
  1104. LOC_REFERENCE :
  1105. begin
  1106. href:=restmploc.reference;
  1107. if target_info.endian=ENDIAN_LITTLE then
  1108. inc(href.offset,4);
  1109. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1110. end;
  1111. LOC_CREGISTER :
  1112. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1113. else
  1114. internalerror(200409204);
  1115. end;
  1116. end
  1117. else
  1118. {$endif cpu64bit}
  1119. begin
  1120. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1121. if getsupreg(funcretloc.register)<first_int_imreg then
  1122. begin
  1123. cg.getcpuregister(list,funcretloc.register);
  1124. cg.ungetcpuregister(list,hreg);
  1125. { for the optimizer }
  1126. cg.a_reg_alloc(list,funcretloc.register);
  1127. end;
  1128. { it could be that a structure is passed in memory but the function is expected to
  1129. return a pointer to this memory }
  1130. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1131. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1132. else
  1133. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1134. end;
  1135. end;
  1136. LOC_FPUREGISTER:
  1137. begin
  1138. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1139. begin
  1140. cg.getcpuregister(list,funcretloc.register);
  1141. cg.ungetcpuregister(list,funcretloc.register);
  1142. end;
  1143. { we can't do direct moves between fpu and mm registers }
  1144. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1145. location_force_fpureg(list,restmploc,false);
  1146. cg.a_loadfpu_loc_reg(list,restmploc,funcretloc.register);
  1147. end;
  1148. LOC_MMREGISTER:
  1149. begin
  1150. if getsupreg(funcretloc.register)<first_mm_imreg then
  1151. begin
  1152. cg.getcpuregister(list,funcretloc.register);
  1153. cg.ungetcpuregister(list,funcretloc.register);
  1154. end;
  1155. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1156. end;
  1157. LOC_INVALID,
  1158. LOC_REFERENCE:
  1159. ;
  1160. else
  1161. internalerror(200405025);
  1162. end;
  1163. end;
  1164. end;
  1165. procedure gen_alloc_regvar(list:TAAsmoutput;sym: tabstractnormalvarsym);
  1166. begin
  1167. case sym.localloc.loc of
  1168. LOC_CREGISTER:
  1169. begin
  1170. {$ifndef cpu64bit}
  1171. if sym.localloc.size in [OS_64,OS_S64] then
  1172. begin
  1173. sym.localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1174. sym.localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1175. end
  1176. else
  1177. {$endif cpu64bit}
  1178. sym.localloc.register:=cg.getintregister(list,sym.localloc.size);
  1179. end;
  1180. LOC_CFPUREGISTER:
  1181. begin
  1182. sym.localloc.register:=cg.getfpuregister(list,sym.localloc.size);
  1183. end;
  1184. LOC_CMMREGISTER:
  1185. begin
  1186. sym. localloc.register:=cg.getmmregister(list,sym.localloc.size);
  1187. end;
  1188. end;
  1189. if (pi_has_goto in current_procinfo.flags) then
  1190. begin
  1191. { Allocate register already, to prevent first allocation to be
  1192. inside a loop }
  1193. {$ifndef cpu64bit}
  1194. if sym.localloc.size in [OS_64,OS_S64] then
  1195. begin
  1196. cg.a_reg_sync(list,sym.localloc.register64.reglo);
  1197. cg.a_reg_sync(list,sym.localloc.register64.reghi);
  1198. end
  1199. else
  1200. {$endif cpu64bit}
  1201. cg.a_reg_sync(list,sym.localloc.register);
  1202. end;
  1203. if cs_asm_source in aktglobalswitches then
  1204. begin
  1205. case sym.localloc.loc of
  1206. LOC_REGISTER,
  1207. LOC_CREGISTER :
  1208. begin
  1209. if (cs_no_regalloc in aktglobalswitches) then
  1210. list.concat(Tai_comment.Create(strpnew('Var '+sym.realname+' located in register '+
  1211. std_regname(sym.localloc.register))))
  1212. else
  1213. list.concat(Tai_comment.Create(strpnew('Var '+sym.realname+' located in register')));
  1214. end;
  1215. end
  1216. end;
  1217. end;
  1218. procedure gen_load_para_value(list:TAAsmoutput);
  1219. procedure get_para(const paraloc:TCGParaLocation);
  1220. begin
  1221. case paraloc.loc of
  1222. LOC_REGISTER :
  1223. begin
  1224. if getsupreg(paraloc.register)<first_int_imreg then
  1225. cg.getcpuregister(list,paraloc.register);
  1226. end;
  1227. LOC_MMREGISTER :
  1228. begin
  1229. if getsupreg(paraloc.register)<first_mm_imreg then
  1230. cg.getcpuregister(list,paraloc.register);
  1231. end;
  1232. LOC_FPUREGISTER :
  1233. begin
  1234. if getsupreg(paraloc.register)<first_fpu_imreg then
  1235. cg.getcpuregister(list,paraloc.register);
  1236. end;
  1237. end;
  1238. end;
  1239. procedure unget_para(const paraloc:TCGParaLocation);
  1240. begin
  1241. case paraloc.loc of
  1242. LOC_REGISTER :
  1243. begin
  1244. if getsupreg(paraloc.register)<first_int_imreg then
  1245. cg.ungetcpuregister(list,paraloc.register);
  1246. end;
  1247. LOC_MMREGISTER :
  1248. begin
  1249. if getsupreg(paraloc.register)<first_mm_imreg then
  1250. cg.ungetcpuregister(list,paraloc.register);
  1251. end;
  1252. LOC_FPUREGISTER :
  1253. begin
  1254. if getsupreg(paraloc.register)<first_fpu_imreg then
  1255. cg.ungetcpuregister(list,paraloc.register);
  1256. end;
  1257. end;
  1258. end;
  1259. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1260. var
  1261. href : treference;
  1262. begin
  1263. case paraloc.loc of
  1264. LOC_REGISTER :
  1265. begin
  1266. {$IFDEF POWERPC64}
  1267. if (paraloc.shiftval <> 0) then
  1268. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1269. {$ENDIF POWERPC64}
  1270. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1271. end;
  1272. LOC_MMREGISTER :
  1273. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1274. LOC_FPUREGISTER :
  1275. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1276. LOC_REFERENCE :
  1277. begin
  1278. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1279. { use concatcopy, because it can also be a float which fails when
  1280. load_ref_ref is used. Don't copy data when the references are equal }
  1281. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1282. cg.g_concatcopy(list,href,ref,sizeleft);
  1283. end;
  1284. else
  1285. internalerror(2002081302);
  1286. end;
  1287. end;
  1288. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1289. var
  1290. href : treference;
  1291. begin
  1292. case paraloc.loc of
  1293. LOC_REGISTER :
  1294. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1295. LOC_MMREGISTER :
  1296. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1297. LOC_FPUREGISTER :
  1298. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1299. LOC_REFERENCE :
  1300. begin
  1301. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1302. case getregtype(reg) of
  1303. R_INTREGISTER :
  1304. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1305. R_FPUREGISTER :
  1306. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1307. R_MMREGISTER :
  1308. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1309. else
  1310. internalerror(2004101012);
  1311. end;
  1312. end;
  1313. else
  1314. internalerror(2002081302);
  1315. end;
  1316. end;
  1317. var
  1318. i : longint;
  1319. currpara : tparavarsym;
  1320. paraloc : pcgparalocation;
  1321. href : treference;
  1322. sizeleft : aint;
  1323. {$ifdef sparc}
  1324. tempref : treference;
  1325. {$endif sparc}
  1326. begin
  1327. if (po_assembler in current_procinfo.procdef.procoptions) then
  1328. exit;
  1329. { Allocate registers used by parameters }
  1330. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1331. begin
  1332. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1333. paraloc:=currpara.paraloc[calleeside].location;
  1334. while assigned(paraloc) do
  1335. begin
  1336. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1337. get_para(paraloc^);
  1338. paraloc:=paraloc^.next;
  1339. end;
  1340. end;
  1341. { Copy parameters to local references/registers }
  1342. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1343. begin
  1344. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1345. paraloc:=currpara.paraloc[calleeside].location;
  1346. { skip e.g. empty records }
  1347. if not assigned(paraloc) then
  1348. internalerror(200408203);
  1349. if (paraloc^.loc = LOC_VOID) then
  1350. continue;
  1351. case currpara.localloc.loc of
  1352. LOC_REFERENCE :
  1353. begin
  1354. { If the parameter location is reused we don't need to copy
  1355. anything }
  1356. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1357. begin
  1358. href:=currpara.localloc.reference;
  1359. sizeleft:=currpara.paraloc[calleeside].intsize;
  1360. while assigned(paraloc) do
  1361. begin
  1362. unget_para(paraloc^);
  1363. if (paraloc^.size=OS_NO) then
  1364. begin
  1365. { Can only be a reference that contains the rest
  1366. of the parameter }
  1367. if (paraloc^.loc<>LOC_REFERENCE) or
  1368. assigned(paraloc^.next) then
  1369. internalerror(2005013010);
  1370. gen_load_ref(paraloc^,href,sizeleft);
  1371. inc(href.offset,sizeleft);
  1372. sizeleft:=0;
  1373. end
  1374. else
  1375. begin
  1376. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1377. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1378. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1379. end;
  1380. paraloc:=paraloc^.next;
  1381. end;
  1382. end;
  1383. end;
  1384. LOC_CREGISTER :
  1385. begin
  1386. {$ifndef cpu64bit}
  1387. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1388. is_64bit(currpara.vartype.def) then
  1389. begin
  1390. case paraloc^.loc of
  1391. LOC_REGISTER:
  1392. begin
  1393. if not assigned(paraloc^.next) then
  1394. internalerror(200410104);
  1395. if (target_info.endian=ENDIAN_BIG) then
  1396. begin
  1397. { paraloc^ -> high
  1398. paraloc^.next -> low }
  1399. unget_para(paraloc^);
  1400. gen_alloc_regvar(list,currpara);
  1401. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1402. unget_para(paraloc^.next^);
  1403. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1404. end
  1405. else
  1406. begin
  1407. { paraloc^ -> low
  1408. paraloc^.next -> high }
  1409. unget_para(paraloc^);
  1410. gen_alloc_regvar(list,currpara);
  1411. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1412. unget_para(paraloc^.next^);
  1413. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1414. end;
  1415. end;
  1416. LOC_REFERENCE:
  1417. begin
  1418. gen_alloc_regvar(list,currpara);
  1419. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset);
  1420. cg64.a_load64_ref_reg(list,href,currpara.localloc.register64);
  1421. unget_para(paraloc^);
  1422. end;
  1423. else
  1424. internalerror(2005101501);
  1425. end
  1426. end
  1427. else
  1428. {$endif cpu64bit}
  1429. begin
  1430. if assigned(paraloc^.next) then
  1431. internalerror(200410105);
  1432. unget_para(paraloc^);
  1433. gen_alloc_regvar(list,currpara);
  1434. gen_load_reg(paraloc^,currpara.localloc.register);
  1435. end;
  1436. end;
  1437. LOC_CFPUREGISTER :
  1438. begin
  1439. {$ifdef sparc}
  1440. { Sparc passes floats in int registers, when loading to fpu register
  1441. we need a temp }
  1442. sizeleft := TCGSize2Size[currpara.localloc.size];
  1443. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1444. href:=tempref;
  1445. while assigned(paraloc) do
  1446. begin
  1447. unget_para(paraloc^);
  1448. gen_load_ref(paraloc^,href,sizeleft);
  1449. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1450. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1451. paraloc:=paraloc^.next;
  1452. end;
  1453. gen_alloc_regvar(list,currpara);
  1454. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1455. tg.UnGetTemp(list,tempref);
  1456. {$else sparc}
  1457. unget_para(paraloc^);
  1458. gen_alloc_regvar(list,currpara);
  1459. gen_load_reg(paraloc^,currpara.localloc.register);
  1460. if assigned(paraloc^.next) then
  1461. internalerror(200410109);
  1462. {$endif sparc}
  1463. end;
  1464. LOC_CMMREGISTER :
  1465. begin
  1466. unget_para(paraloc^);
  1467. gen_alloc_regvar(list,currpara);
  1468. gen_load_reg(paraloc^,currpara.localloc.register);
  1469. { data could come in two memory locations, for now
  1470. we simply ignore the sanity check (FK)
  1471. if assigned(paraloc^.next) then
  1472. internalerror(200410108);
  1473. }
  1474. end;
  1475. end;
  1476. end;
  1477. { generate copies of call by value parameters, must be done before
  1478. the initialization and body is parsed because the refcounts are
  1479. incremented using the local copies }
  1480. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1481. {$ifdef powerpc}
  1482. { unget the register that contains the stack pointer before the procedure entry, }
  1483. { which is used to access the parameters in their original callee-side location }
  1484. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1485. cg.a_reg_dealloc(list,NR_R12);
  1486. {$endif powerpc}
  1487. {$ifdef powerpc64}
  1488. { unget the register that contains the stack pointer before the procedure entry, }
  1489. { which is used to access the parameters in their original callee-side location }
  1490. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1491. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1492. {$endif powerpc64}
  1493. end;
  1494. procedure gen_initialize_code(list:TAAsmoutput);
  1495. begin
  1496. { initialize local data like ansistrings }
  1497. case current_procinfo.procdef.proctypeoption of
  1498. potype_unitinit:
  1499. begin
  1500. { this is also used for initialization of variables in a
  1501. program which does not have a globalsymtable }
  1502. if assigned(current_module.globalsymtable) then
  1503. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1504. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1505. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1506. end;
  1507. { units have seperate code for initilization and finalization }
  1508. potype_unitfinalize: ;
  1509. { program init/final is generated in separate procedure }
  1510. potype_proginit:
  1511. begin
  1512. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1513. end;
  1514. else
  1515. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1516. end;
  1517. { initialisizes temp. ansi/wide string data }
  1518. inittempvariables(list);
  1519. { initialize ansi/widesstring para's }
  1520. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1521. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1522. {$ifdef OLDREGVARS}
  1523. load_regvars(list,nil);
  1524. {$endif OLDREGVARS}
  1525. end;
  1526. procedure gen_finalize_code(list:TAAsmoutput);
  1527. begin
  1528. {$ifdef OLDREGVARS}
  1529. cleanup_regvars(list);
  1530. {$endif OLDREGVARS}
  1531. { finalize temporary data }
  1532. finalizetempvariables(list);
  1533. { finalize local data like ansistrings}
  1534. case current_procinfo.procdef.proctypeoption of
  1535. potype_unitfinalize:
  1536. begin
  1537. { this is also used for initialization of variables in a
  1538. program which does not have a globalsymtable }
  1539. if assigned(current_module.globalsymtable) then
  1540. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1541. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1542. end;
  1543. { units/progs have separate code for initialization and finalization }
  1544. potype_unitinit: ;
  1545. { program init/final is generated in separate procedure }
  1546. potype_proginit: ;
  1547. else
  1548. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1549. end;
  1550. { finalize paras data }
  1551. if assigned(current_procinfo.procdef.parast) and
  1552. not(po_assembler in current_procinfo.procdef.procoptions) then
  1553. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1554. end;
  1555. procedure gen_entry_code(list:TAAsmoutput);
  1556. var
  1557. paraloc1,
  1558. paraloc2 : tcgpara;
  1559. begin
  1560. paraloc1.init;
  1561. paraloc2.init;
  1562. { the actual profile code can clobber some registers,
  1563. therefore if the context must be saved, do it before
  1564. the actual call to the profile code
  1565. }
  1566. if (cs_profile in aktmoduleswitches) and
  1567. not(po_assembler in current_procinfo.procdef.procoptions) then
  1568. begin
  1569. { non-win32 can call mcout even in main }
  1570. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1571. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1572. begin
  1573. cg.allocallcpuregisters(list);
  1574. cg.g_profilecode(list);
  1575. cg.deallocallcpuregisters(list);
  1576. end;
  1577. end;
  1578. { call startup helpers from main program }
  1579. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1580. begin
  1581. if (target_info.system = system_powerpc_darwin) or
  1582. (target_info.system = system_powerpc_macos) then
  1583. begin
  1584. { the parameters are already in the right registers }
  1585. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1586. end;
  1587. { initialize units }
  1588. cg.allocallcpuregisters(list);
  1589. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1590. cg.deallocallcpuregisters(list);
  1591. end;
  1592. list.concat(Tai_force_line.Create);
  1593. {$ifdef OLDREGVARS}
  1594. load_regvars(list,nil);
  1595. {$endif OLDREGVARS}
  1596. paraloc1.done;
  1597. paraloc2.done;
  1598. end;
  1599. procedure gen_exit_code(list:TAAsmoutput);
  1600. begin
  1601. { call __EXIT for main program }
  1602. if (not DLLsource) and
  1603. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1604. cg.a_call_name(list,'FPC_DO_EXIT');
  1605. end;
  1606. {****************************************************************************
  1607. Entry/Exit
  1608. ****************************************************************************}
  1609. procedure alloc_proc_symbol(pd: tprocdef);
  1610. var
  1611. item: tstringlistitem;
  1612. begin
  1613. item := tstringlistitem(pd.aliasnames.first);
  1614. while assigned(item) do
  1615. begin
  1616. if (cs_profile in aktmoduleswitches) or
  1617. (po_global in current_procinfo.procdef.procoptions) then
  1618. objectlibrary.newasmsymbol(item.str,AB_GLOBAL,AT_FUNCTION)
  1619. else
  1620. objectlibrary.newasmsymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1621. item := tstringlistitem(item.next);
  1622. end;
  1623. end;
  1624. procedure gen_proc_symbol(list:Taasmoutput);
  1625. var
  1626. hs : string;
  1627. begin
  1628. repeat
  1629. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1630. if hs='' then
  1631. break;
  1632. if (cs_profile in aktmoduleswitches) or
  1633. (po_global in current_procinfo.procdef.procoptions) then
  1634. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1635. else
  1636. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1637. if target_info.use_function_relative_addresses then
  1638. list.concat(Tai_function_name.create(hs));
  1639. until false;
  1640. current_procinfo.procdef.procstarttai:=tai(list.last);
  1641. end;
  1642. procedure gen_proc_symbol_end(list:Taasmoutput);
  1643. begin
  1644. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1645. begin
  1646. { Insert Ident of the compiler in the main .text section }
  1647. if (not (cs_create_smart in aktmoduleswitches)) then
  1648. begin
  1649. list.insert(Tai_align.Create(const_align(32)));
  1650. list.insert(Tai_string.Create('FPC '+full_version_string+
  1651. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  1652. end;
  1653. { Reference all DEBUGINFO sections from the main .text section }
  1654. if (cs_debuginfo in aktmoduleswitches) then
  1655. debuginfo.referencesections(list);
  1656. end;
  1657. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1658. current_procinfo.procdef.procendtai:=tai(list.last);
  1659. end;
  1660. procedure gen_proc_entry_code(list:Taasmoutput);
  1661. var
  1662. hitemp,
  1663. lotemp : longint;
  1664. begin
  1665. { generate call frame marker for dwarf call frame info }
  1666. dwarfcfi.start_frame(list);
  1667. { All temps are know, write offsets used for information }
  1668. if (cs_asm_source in aktglobalswitches) then
  1669. begin
  1670. if tg.direction>0 then
  1671. begin
  1672. lotemp:=current_procinfo.tempstart;
  1673. hitemp:=tg.lasttemp;
  1674. end
  1675. else
  1676. begin
  1677. lotemp:=tg.lasttemp;
  1678. hitemp:=current_procinfo.tempstart;
  1679. end;
  1680. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1681. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1682. end;
  1683. { generate target specific proc entry code }
  1684. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1685. end;
  1686. procedure gen_proc_exit_code(list:Taasmoutput);
  1687. var
  1688. parasize : longint;
  1689. begin
  1690. { c style clearstack does not need to remove parameters from the stack, only the
  1691. return value when it was pushed by arguments }
  1692. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1693. begin
  1694. parasize:=0;
  1695. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1696. inc(parasize,sizeof(aint));
  1697. end
  1698. else
  1699. parasize:=current_procinfo.para_stack_size;
  1700. { generate target specific proc exit code }
  1701. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1702. { release return registers, needed for optimizer }
  1703. if not is_void(current_procinfo.procdef.rettype.def) then
  1704. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1705. { end of frame marker for call frame info }
  1706. dwarfcfi.end_frame(list);
  1707. end;
  1708. procedure gen_stack_check_size_para(list:Taasmoutput);
  1709. var
  1710. paraloc1 : tcgpara;
  1711. begin
  1712. paraloc1.init;
  1713. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1714. paramanager.allocparaloc(list,paraloc1);
  1715. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1716. paramanager.freeparaloc(list,paraloc1);
  1717. paraloc1.done;
  1718. end;
  1719. procedure gen_stack_check_call(list:Taasmoutput);
  1720. var
  1721. paraloc1 : tcgpara;
  1722. begin
  1723. paraloc1.init;
  1724. { Also alloc the register needed for the parameter }
  1725. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1726. paramanager.allocparaloc(list,paraloc1);
  1727. paramanager.freeparaloc(list,paraloc1);
  1728. { Call the helper }
  1729. cg.allocallcpuregisters(list);
  1730. cg.a_call_name(list,'FPC_STACKCHECK');
  1731. cg.deallocallcpuregisters(list);
  1732. paraloc1.done;
  1733. end;
  1734. procedure gen_save_used_regs(list:TAAsmoutput);
  1735. begin
  1736. { Pure assembler routines need to save the registers themselves }
  1737. if (po_assembler in current_procinfo.procdef.procoptions) then
  1738. exit;
  1739. { oldfpccall expects all registers to be destroyed }
  1740. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1741. cg.g_save_standard_registers(list);
  1742. end;
  1743. procedure gen_restore_used_regs(list:TAAsmoutput);
  1744. begin
  1745. { Pure assembler routines need to save the registers themselves }
  1746. if (po_assembler in current_procinfo.procdef.procoptions) then
  1747. exit;
  1748. { oldfpccall expects all registers to be destroyed }
  1749. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1750. cg.g_restore_standard_registers(list);
  1751. end;
  1752. procedure gen_got_load(list : taasmoutput);
  1753. begin
  1754. { if loading got is necessary for more cpus, it can be moved
  1755. to the cg }
  1756. {$ifdef i386}
  1757. { allocate PIC register }
  1758. if (cs_create_pic in aktmoduleswitches) and
  1759. (tf_pic_uses_got in target_info.flags) and
  1760. (pi_needs_got in current_procinfo.flags) then
  1761. begin
  1762. current_module.requires_ebx_pic_helper:=true;
  1763. cg.a_call_name_static(list,'fpc_geteipasebx');
  1764. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,objectlibrary.newasmsymbol('_GLOBAL_OFFSET_TABLE_',AB_EXTERNAL,AT_DATA),0,NR_PIC_OFFSET_REG));
  1765. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  1766. { ecx could be used in leave procedures }
  1767. current_procinfo.got:=NR_EBX;
  1768. end;
  1769. {$endif i386}
  1770. end;
  1771. {****************************************************************************
  1772. External handling
  1773. ****************************************************************************}
  1774. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  1775. begin
  1776. { add the procedure to the al_procedures }
  1777. maybe_new_object_file(list);
  1778. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1779. list.concat(Tai_align.create(aktalignment.procalign));
  1780. if (po_global in pd.procoptions) then
  1781. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1782. else
  1783. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1784. cg.a_jmp_name(list,externalname);
  1785. end;
  1786. {****************************************************************************
  1787. Const Data
  1788. ****************************************************************************}
  1789. procedure insertbssdata(sym : tglobalvarsym);
  1790. var
  1791. l,varalign : longint;
  1792. storefilepos : tfileposinfo;
  1793. list : Taasmoutput;
  1794. sectype : Tasmsectiontype;
  1795. begin
  1796. storefilepos:=aktfilepos;
  1797. aktfilepos:=sym.fileinfo;
  1798. l:=sym.getsize;
  1799. if tf_section_threadvars in target_info.flags then
  1800. begin
  1801. if (vo_is_thread_var in sym.varoptions) then
  1802. begin
  1803. list:=asmlist[al_threadvars];
  1804. sectype:=sec_threadvar;
  1805. end
  1806. else
  1807. begin
  1808. list:=asmlist[al_globals];
  1809. sectype:=sec_bss;
  1810. end;
  1811. end
  1812. else
  1813. begin
  1814. if (vo_is_thread_var in sym.varoptions) then
  1815. inc(l,sizeof(aint));
  1816. list:=asmlist[al_globals];
  1817. sectype:=sec_bss;
  1818. end;
  1819. varalign:=var_align(l);
  1820. maybe_new_object_file(list);
  1821. new_section(list,sectype,lower(sym.mangledname),varalign);
  1822. if (sym.owner.symtabletype=globalsymtable) or
  1823. maybe_smartlink_symbol or
  1824. DLLSource or
  1825. (assigned(current_procinfo) and
  1826. (po_inline in current_procinfo.procdef.procoptions)) or
  1827. (vo_is_exported in sym.varoptions) or
  1828. (vo_is_C_var in sym.varoptions) then
  1829. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  1830. else
  1831. list.concat(Tai_datablock.create(sym.mangledname,l));
  1832. aktfilepos:=storefilepos;
  1833. end;
  1834. procedure init_regvar_loc(sym:tabstractnormalvarsym;cgsize:tcgsize);
  1835. begin
  1836. with sym do
  1837. begin
  1838. localloc.size:=cgsize;
  1839. case varregable of
  1840. vr_intreg :
  1841. begin
  1842. localloc.loc:=LOC_CREGISTER;
  1843. end;
  1844. vr_fpureg :
  1845. begin
  1846. localloc.loc:=LOC_CFPUREGISTER;
  1847. end;
  1848. vr_mmreg :
  1849. begin
  1850. localloc.loc:=LOC_CMMREGISTER;
  1851. end;
  1852. else
  1853. internalerror(2004101010);
  1854. end;
  1855. end;
  1856. end;
  1857. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  1858. var
  1859. sym : tsym;
  1860. isaddr : boolean;
  1861. cgsize : tcgsize;
  1862. begin
  1863. sym:=tsym(st.symindex.first);
  1864. while assigned(sym) do
  1865. begin
  1866. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1867. begin
  1868. with tabstractnormalvarsym(sym) do
  1869. begin
  1870. { Parameters passed to assembler procedures need to be kept
  1871. in the original location }
  1872. if (sym.typ=paravarsym) and
  1873. (po_assembler in current_procinfo.procdef.procoptions) then
  1874. begin
  1875. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1876. end
  1877. else
  1878. begin
  1879. isaddr:=(st.symtabletype=parasymtable) and
  1880. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1881. if isaddr then
  1882. cgsize:=OS_ADDR
  1883. else
  1884. cgsize:=def_cgsize(vartype.def);
  1885. {$ifndef OLDREGVARS}
  1886. { When there is assembler code we can't use regvars }
  1887. if is_regvar then
  1888. begin
  1889. init_regvar_loc(tabstractnormalvarsym(sym),cgsize);
  1890. if (st.symtabletype <> parasymtable) then
  1891. gen_alloc_regvar(list,tabstractnormalvarsym(sym));
  1892. end
  1893. else
  1894. {$endif NOT OLDREGVARS}
  1895. begin
  1896. localloc.loc:=LOC_REFERENCE;
  1897. localloc.size:=cgsize;
  1898. case st.symtabletype of
  1899. parasymtable :
  1900. begin
  1901. { Reuse the parameter location for values to are at a single location on the stack }
  1902. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1903. begin
  1904. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1905. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1906. end
  1907. else
  1908. begin
  1909. if isaddr then
  1910. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1911. else
  1912. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1913. end;
  1914. end;
  1915. localsymtable,
  1916. stt_exceptsymtable :
  1917. begin
  1918. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1919. end;
  1920. staticsymtable :
  1921. begin
  1922. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1923. if not(vo_is_dll_var in varoptions) and ((tf_section_threadvars in target_info.flags) or
  1924. not(vo_is_thread_var in varoptions)) then
  1925. reference_reset_symbol(localloc.reference,objectlibrary.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA),0);
  1926. end;
  1927. else
  1928. internalerror(200410103);
  1929. end;
  1930. end;
  1931. end;
  1932. if cs_asm_source in aktglobalswitches then
  1933. begin
  1934. case localloc.loc of
  1935. LOC_REFERENCE :
  1936. begin
  1937. if not assigned(localloc.reference.symbol) then
  1938. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1939. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1940. end;
  1941. end;
  1942. end;
  1943. end;
  1944. end;
  1945. sym:=tsym(sym.indexnext);
  1946. end;
  1947. end;
  1948. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  1949. var
  1950. rv: pusedregvars absolute arg;
  1951. begin
  1952. if (n.nodetype = loadn) and
  1953. (tloadnode(n).symtableentry.typ in [globalvarsym,localvarsym,paravarsym]) then
  1954. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1955. case loc of
  1956. LOC_CREGISTER:
  1957. {$ifndef cpu64bit}
  1958. if size in [OS_64,OS_S64] then
  1959. begin
  1960. rv^.intregvars.addnodup(getsupreg(register64.reglo));
  1961. rv^.intregvars.addnodup(getsupreg(register64.reghi));
  1962. end
  1963. else
  1964. {$endif cpu64bit}
  1965. rv^.intregvars.addnodup(getsupreg(register));
  1966. LOC_CFPUREGISTER:
  1967. rv^.fpuregvars.addnodup(getsupreg(register));
  1968. LOC_CMMREGISTER:
  1969. rv^.mmregvars.addnodup(getsupreg(register));
  1970. end;
  1971. result := fen_true;
  1972. end;
  1973. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  1974. begin
  1975. foreachnodestatic(n,@do_get_used_regvars,@rv);
  1976. end;
  1977. {
  1978. See comments at declaration of pusedregvarscommon
  1979. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  1980. var
  1981. rv: pusedregvarscommon absolute arg;
  1982. begin
  1983. if (n.nodetype = loadn) and
  1984. (tloadnode(n).symtableentry.typ in [globalvarsym,localvarsym,paravarsym]) then
  1985. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  1986. case loc of
  1987. LOC_CREGISTER:
  1988. { if not yet encountered in this node tree }
  1989. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1990. { but nevertheless already encountered somewhere }
  1991. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1992. { then it's a regvar used in two or more node trees }
  1993. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1994. LOC_CFPUREGISTER:
  1995. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  1996. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  1997. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  1998. LOC_CMMREGISTER:
  1999. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2000. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2001. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2002. end;
  2003. result := fen_true;
  2004. end;
  2005. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  2006. begin
  2007. rv.myregvars.intregvars.clear;
  2008. rv.myregvars.fpuregvars.clear;
  2009. rv.myregvars.mmregvars.clear;
  2010. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  2011. end;
  2012. }
  2013. procedure gen_sync_regvars(list:TAAsmoutput; var rv: tusedregvars);
  2014. var
  2015. count: longint;
  2016. begin
  2017. for count := 1 to rv.intregvars.length do
  2018. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  2019. for count := 1 to rv.fpuregvars.length do
  2020. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  2021. for count := 1 to rv.mmregvars.length do
  2022. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  2023. end;
  2024. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  2025. var
  2026. sym : tsym;
  2027. begin
  2028. sym:=tsym(st.symindex.first);
  2029. while assigned(sym) do
  2030. begin
  2031. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  2032. begin
  2033. with tabstractnormalvarsym(sym) do
  2034. begin
  2035. { Note: We need to keep the data available in memory
  2036. for the sub procedures that can access local data
  2037. in the parent procedures }
  2038. case localloc.loc of
  2039. LOC_CREGISTER :
  2040. {$ifndef cpu64bit}
  2041. if (pi_has_goto in current_procinfo.flags) then
  2042. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  2043. begin
  2044. cg.a_reg_sync(list,localloc.register64.reglo);
  2045. cg.a_reg_sync(list,localloc.register64.reghi);
  2046. end
  2047. else
  2048. {$endif cpu64bit}
  2049. cg.a_reg_sync(list,localloc.register);
  2050. LOC_CFPUREGISTER,
  2051. LOC_CMMREGISTER:
  2052. if (pi_has_goto in current_procinfo.flags) then
  2053. cg.a_reg_sync(list,localloc.register);
  2054. LOC_REFERENCE :
  2055. begin
  2056. case st.symtabletype of
  2057. localsymtable,
  2058. parasymtable,
  2059. stt_exceptsymtable :
  2060. tg.Ungetlocal(list,localloc.reference);
  2061. end;
  2062. end;
  2063. end;
  2064. end;
  2065. end;
  2066. sym:=tsym(sym.indexnext);
  2067. end;
  2068. end;
  2069. { persistent rtti generation }
  2070. procedure generate_rtti(p:Ttypesym);
  2071. var
  2072. rsym : trttisym;
  2073. def : tstoreddef;
  2074. begin
  2075. { rtti can only be generated for classes that are always typesyms }
  2076. def:=tstoreddef(ttypesym(p).restype.def);
  2077. { there is an error, skip rtti info }
  2078. if (def.deftype=errordef) or (Errorcount>0) then
  2079. exit;
  2080. { only create rtti once for each definition }
  2081. if not(df_has_rttitable in def.defoptions) then
  2082. begin
  2083. { definition should be in the same symtable as the symbol }
  2084. if p.owner<>def.owner then
  2085. internalerror(200108262);
  2086. { create rttisym }
  2087. rsym:=trttisym.create(p.name,fullrtti);
  2088. p.owner.insert(rsym);
  2089. { register rttisym in definition }
  2090. include(def.defoptions,df_has_rttitable);
  2091. def.rttitablesym:=rsym;
  2092. { write rtti data }
  2093. def.write_child_rtti_data(fullrtti);
  2094. maybe_new_object_file(asmlist[al_rtti]);
  2095. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2096. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  2097. def.write_rtti_data(fullrtti);
  2098. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  2099. end;
  2100. end;
  2101. { persistent init table generation }
  2102. procedure generate_inittable(p:tsym);
  2103. var
  2104. rsym : trttisym;
  2105. def : tstoreddef;
  2106. begin
  2107. { anonymous types are also allowed for records that can be varsym }
  2108. case p.typ of
  2109. typesym :
  2110. def:=tstoreddef(ttypesym(p).restype.def);
  2111. globalvarsym,
  2112. localvarsym,
  2113. paravarsym :
  2114. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  2115. else
  2116. internalerror(200108263);
  2117. end;
  2118. { only create inittable once for each definition }
  2119. if not(df_has_inittable in def.defoptions) then
  2120. begin
  2121. { definition should be in the same symtable as the symbol }
  2122. if p.owner<>def.owner then
  2123. internalerror(200108264);
  2124. { create rttisym }
  2125. rsym:=trttisym.create(p.name,initrtti);
  2126. p.owner.insert(rsym);
  2127. { register rttisym in definition }
  2128. include(def.defoptions,df_has_inittable);
  2129. def.inittablesym:=rsym;
  2130. { write inittable data }
  2131. def.write_child_rtti_data(initrtti);
  2132. maybe_new_object_file(asmlist[al_rtti]);
  2133. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2134. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  2135. def.write_rtti_data(initrtti);
  2136. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  2137. end;
  2138. end;
  2139. procedure gen_intf_wrapper(list:taasmoutput;_class:tobjectdef);
  2140. var
  2141. i,j,
  2142. proccount : longint;
  2143. tmps : string;
  2144. begin
  2145. for i:=1 to _class.implementedinterfaces.count do
  2146. begin
  2147. { only if implemented by this class }
  2148. if _class.implementedinterfaces.implindex(i)=i then
  2149. begin
  2150. proccount:=_class.implementedinterfaces.implproccount(i);
  2151. for j:=1 to proccount do
  2152. begin
  2153. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2154. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  2155. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  2156. { create wrapper code }
  2157. new_section(list,sec_code,lower(tmps),0);
  2158. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  2159. end;
  2160. end;
  2161. end;
  2162. end;
  2163. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  2164. var
  2165. def : tstoreddef;
  2166. begin
  2167. def:=tstoreddef(st.defindex.first);
  2168. while assigned(def) do
  2169. begin
  2170. if is_class(def) then
  2171. gen_intf_wrapper(list,tobjectdef(def));
  2172. def:=tstoreddef(def.indexnext);
  2173. end;
  2174. end;
  2175. procedure gen_load_vmt_register(list:taasmoutput;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2176. var
  2177. href : treference;
  2178. begin
  2179. if is_object(objdef) then
  2180. begin
  2181. case selfloc.loc of
  2182. LOC_CREFERENCE,
  2183. LOC_REFERENCE:
  2184. begin
  2185. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2186. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2187. end;
  2188. else
  2189. internalerror(200305056);
  2190. end;
  2191. end
  2192. else
  2193. begin
  2194. case selfloc.loc of
  2195. LOC_REGISTER:
  2196. begin
  2197. {$ifdef cpu_uses_separate_address_registers}
  2198. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2199. begin
  2200. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2201. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2202. end
  2203. else
  2204. {$endif cpu_uses_separate_address_registers}
  2205. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2206. end;
  2207. LOC_CREGISTER,
  2208. LOC_CREFERENCE,
  2209. LOC_REFERENCE:
  2210. begin
  2211. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2212. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2213. end;
  2214. else
  2215. internalerror(200305057);
  2216. end;
  2217. end;
  2218. vmtreg:=cg.getaddressregister(list);
  2219. cg.g_maybe_testself(list,href.base);
  2220. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2221. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2222. end;
  2223. function getprocalign : longint;
  2224. begin
  2225. { gprof uses 16 byte granularity }
  2226. if (cs_profile in aktmoduleswitches) then
  2227. result:=16
  2228. else
  2229. result:=aktalignment.procalign;
  2230. end;
  2231. procedure gen_pic_helpers(list : taasmoutput);
  2232. var
  2233. href : treference;
  2234. begin
  2235. { if other cpus require such helpers as well, it can be solved more cleaner }
  2236. {$ifdef i386}
  2237. if current_module.requires_ebx_pic_helper then
  2238. begin
  2239. new_section(list,sec_code,'fpc_geteipasebx',0);
  2240. list.concat(tai_symbol.Createname('fpc_geteipasebx',AT_FUNCTION,getprocalign));
  2241. reference_reset(href);
  2242. href.base:=NR_ESP;
  2243. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_EBX));
  2244. list.concat(taicpu.op_none(A_RET,S_NO));
  2245. end;
  2246. if current_module.requires_ecx_pic_helper then
  2247. begin
  2248. new_section(list,sec_code,'fpc_geteipasecx',0);
  2249. list.concat(tai_symbol.Createname('fpc_geteipasecx',AT_FUNCTION,getprocalign));
  2250. reference_reset(href);
  2251. href.base:=NR_ESP;
  2252. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_ECX));
  2253. list.concat(taicpu.op_none(A_RET,S_NO));
  2254. end;
  2255. {$endif i386}
  2256. end;
  2257. end.