ncgutil.pas 93 KB

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