ncgutil.pas 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367
  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. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  916. if (tparavarsym(p).varspez=vs_value) then
  917. begin
  918. include(current_procinfo.flags,pi_needs_implicit_finally);
  919. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  920. cg.g_decrrefcount(list,tparavarsym(p).vartype.def,href);
  921. end;
  922. end
  923. else
  924. if (tparavarsym(p).varspez=vs_value) and
  925. (is_open_array(tparavarsym(p).vartype.def) or
  926. is_array_of_const(tparavarsym(p).vartype.def)) then
  927. begin
  928. { cdecl functions don't have a high pointer so it is not possible to generate
  929. a local copy }
  930. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  931. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  932. end;
  933. end;
  934. { Initialize temp ansi/widestrings,interfaces }
  935. procedure inittempvariables(list:taasmoutput);
  936. var
  937. hp : ptemprecord;
  938. href : treference;
  939. begin
  940. hp:=tg.templist;
  941. while assigned(hp) do
  942. begin
  943. if assigned(hp^.def) and
  944. hp^.def.needs_inittable then
  945. begin
  946. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  947. cg.g_initialize(list,hp^.def,href);
  948. end;
  949. hp:=hp^.next;
  950. end;
  951. end;
  952. procedure finalizetempvariables(list:taasmoutput);
  953. var
  954. hp : ptemprecord;
  955. href : treference;
  956. begin
  957. hp:=tg.templist;
  958. while assigned(hp) do
  959. begin
  960. if assigned(hp^.def) and
  961. hp^.def.needs_inittable then
  962. begin
  963. include(current_procinfo.flags,pi_needs_implicit_finally);
  964. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  965. cg.g_finalize(list,hp^.def,href);
  966. end;
  967. hp:=hp^.next;
  968. end;
  969. end;
  970. procedure gen_load_return_value(list:TAAsmoutput);
  971. var
  972. {$ifndef cpu64bit}
  973. href : treference;
  974. {$endif cpu64bit}
  975. ressym : tabstractnormalvarsym;
  976. resloc,
  977. restmploc : tlocation;
  978. hreg : tregister;
  979. funcretloc : tlocation;
  980. begin
  981. { Is the loading needed? }
  982. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  983. (
  984. (po_assembler in current_procinfo.procdef.procoptions) and
  985. (not(assigned(current_procinfo.procdef.funcretsym)) or
  986. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  987. ) then
  988. exit;
  989. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  990. { constructors return self }
  991. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  992. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.search('self'))
  993. else
  994. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  995. if (ressym.refs>0) then
  996. begin
  997. {$ifdef OLDREGVARS}
  998. case ressym.localloc.loc of
  999. LOC_CFPUREGISTER,
  1000. LOC_FPUREGISTER:
  1001. begin
  1002. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1003. restmploc.register:=ressym.localloc.register;
  1004. end;
  1005. LOC_CREGISTER,
  1006. LOC_REGISTER:
  1007. begin
  1008. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1009. restmploc.register:=ressym.localloc.register;
  1010. end;
  1011. LOC_MMREGISTER:
  1012. begin
  1013. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1014. restmploc.register:=ressym.localloc.register;
  1015. end;
  1016. LOC_REFERENCE:
  1017. begin
  1018. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1019. restmploc.reference:=ressym.localloc.reference;
  1020. end;
  1021. else
  1022. internalerror(200309184);
  1023. end;
  1024. {$else}
  1025. restmploc:=ressym.localloc;
  1026. {$endif}
  1027. { Here, we return the function result. In most architectures, the value is
  1028. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1029. function returns in a register and the caller receives it in an other one }
  1030. case funcretloc.loc of
  1031. LOC_REGISTER:
  1032. begin
  1033. {$ifndef cpu64bit}
  1034. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1035. begin
  1036. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1037. if resloc.loc<>LOC_REGISTER then
  1038. internalerror(200409141);
  1039. { Load low and high register separate to generate better register
  1040. allocation info }
  1041. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1042. begin
  1043. cg.getcpuregister(list,resloc.register64.reglo);
  1044. cg.ungetcpuregister(list,resloc.register64.reglo);
  1045. { for the optimizer }
  1046. cg.a_reg_alloc(list,resloc.register64.reglo);
  1047. end;
  1048. case restmploc.loc of
  1049. LOC_REFERENCE :
  1050. begin
  1051. href:=restmploc.reference;
  1052. if target_info.endian=ENDIAN_BIG then
  1053. inc(href.offset,4);
  1054. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1055. end;
  1056. LOC_CREGISTER :
  1057. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1058. else
  1059. internalerror(200409203);
  1060. end;
  1061. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1062. begin
  1063. cg.getcpuregister(list,resloc.register64.reghi);
  1064. cg.ungetcpuregister(list,resloc.register64.reghi);
  1065. { for the optimizer }
  1066. cg.a_reg_alloc(list,resloc.register64.reghi);
  1067. end;
  1068. case restmploc.loc of
  1069. LOC_REFERENCE :
  1070. begin
  1071. href:=restmploc.reference;
  1072. if target_info.endian=ENDIAN_LITTLE then
  1073. inc(href.offset,4);
  1074. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1075. end;
  1076. LOC_CREGISTER :
  1077. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1078. else
  1079. internalerror(200409204);
  1080. end;
  1081. end
  1082. else
  1083. {$endif cpu64bit}
  1084. begin
  1085. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1086. if getsupreg(funcretloc.register)<first_int_imreg then
  1087. begin
  1088. cg.getcpuregister(list,funcretloc.register);
  1089. cg.ungetcpuregister(list,hreg);
  1090. { for the optimizer }
  1091. cg.a_reg_alloc(list,funcretloc.register);
  1092. end;
  1093. { it could be that a structure is passed in memory but the function is expected to
  1094. return a pointer to this memory }
  1095. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1096. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1097. else
  1098. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1099. end;
  1100. end;
  1101. LOC_FPUREGISTER:
  1102. begin
  1103. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1104. begin
  1105. cg.getcpuregister(list,funcretloc.register);
  1106. cg.ungetcpuregister(list,funcretloc.register);
  1107. end;
  1108. cg.a_loadfpu_loc_reg(list,restmploc,funcretloc.register);
  1109. end;
  1110. LOC_MMREGISTER:
  1111. begin
  1112. if getsupreg(funcretloc.register)<first_mm_imreg then
  1113. begin
  1114. cg.getcpuregister(list,funcretloc.register);
  1115. cg.ungetcpuregister(list,funcretloc.register);
  1116. end;
  1117. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1118. end;
  1119. LOC_INVALID,
  1120. LOC_REFERENCE:
  1121. ;
  1122. else
  1123. internalerror(200405025);
  1124. end;
  1125. end;
  1126. end;
  1127. procedure gen_load_para_value(list:TAAsmoutput);
  1128. procedure get_para(const paraloc:TCGParaLocation);
  1129. begin
  1130. case paraloc.loc of
  1131. LOC_REGISTER :
  1132. begin
  1133. if getsupreg(paraloc.register)<first_int_imreg then
  1134. cg.getcpuregister(list,paraloc.register);
  1135. end;
  1136. LOC_MMREGISTER :
  1137. begin
  1138. if getsupreg(paraloc.register)<first_mm_imreg then
  1139. cg.getcpuregister(list,paraloc.register);
  1140. end;
  1141. LOC_FPUREGISTER :
  1142. begin
  1143. if getsupreg(paraloc.register)<first_fpu_imreg then
  1144. cg.getcpuregister(list,paraloc.register);
  1145. end;
  1146. end;
  1147. end;
  1148. procedure unget_para(const paraloc:TCGParaLocation);
  1149. begin
  1150. case paraloc.loc of
  1151. LOC_REGISTER :
  1152. begin
  1153. if getsupreg(paraloc.register)<first_int_imreg then
  1154. cg.ungetcpuregister(list,paraloc.register);
  1155. end;
  1156. LOC_MMREGISTER :
  1157. begin
  1158. if getsupreg(paraloc.register)<first_mm_imreg then
  1159. cg.ungetcpuregister(list,paraloc.register);
  1160. end;
  1161. LOC_FPUREGISTER :
  1162. begin
  1163. if getsupreg(paraloc.register)<first_fpu_imreg then
  1164. cg.ungetcpuregister(list,paraloc.register);
  1165. end;
  1166. end;
  1167. end;
  1168. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1169. var
  1170. href : treference;
  1171. begin
  1172. case paraloc.loc of
  1173. LOC_REGISTER :
  1174. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1175. LOC_MMREGISTER :
  1176. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1177. LOC_FPUREGISTER :
  1178. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1179. LOC_REFERENCE :
  1180. begin
  1181. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1182. { use concatcopy, because it can also be a float which fails when
  1183. load_ref_ref is used. Don't copy data when the references are equal }
  1184. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1185. cg.g_concatcopy(list,href,ref,sizeleft);
  1186. end;
  1187. else
  1188. internalerror(2002081302);
  1189. end;
  1190. end;
  1191. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1192. var
  1193. href : treference;
  1194. begin
  1195. case paraloc.loc of
  1196. LOC_REGISTER :
  1197. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1198. LOC_MMREGISTER :
  1199. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1200. LOC_FPUREGISTER :
  1201. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1202. LOC_REFERENCE :
  1203. begin
  1204. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1205. case getregtype(reg) of
  1206. R_INTREGISTER :
  1207. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1208. R_FPUREGISTER :
  1209. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1210. R_MMREGISTER :
  1211. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1212. else
  1213. internalerror(2004101012);
  1214. end;
  1215. end;
  1216. else
  1217. internalerror(2002081302);
  1218. end;
  1219. end;
  1220. var
  1221. i : longint;
  1222. currpara : tparavarsym;
  1223. paraloc : pcgparalocation;
  1224. href : treference;
  1225. sizeleft : aint;
  1226. {$ifdef sparc}
  1227. tempref : treference;
  1228. {$endif sparc}
  1229. begin
  1230. if (po_assembler in current_procinfo.procdef.procoptions) then
  1231. exit;
  1232. { Allocate registers used by parameters }
  1233. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1234. begin
  1235. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1236. paraloc:=currpara.paraloc[calleeside].location;
  1237. while assigned(paraloc) do
  1238. begin
  1239. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1240. get_para(paraloc^);
  1241. paraloc:=paraloc^.next;
  1242. end;
  1243. end;
  1244. { Copy parameters to local references/registers }
  1245. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1246. begin
  1247. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1248. paraloc:=currpara.paraloc[calleeside].location;
  1249. { skip e.g. empty records }
  1250. if (paraloc^.loc = LOC_VOID) then
  1251. continue;
  1252. if not assigned(paraloc) then
  1253. internalerror(200408203);
  1254. case currpara.localloc.loc of
  1255. LOC_REFERENCE :
  1256. begin
  1257. { If the parameter location is reused we don't need to copy
  1258. anything }
  1259. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1260. begin
  1261. href:=currpara.localloc.reference;
  1262. sizeleft:=currpara.paraloc[calleeside].intsize;
  1263. while assigned(paraloc) do
  1264. begin
  1265. unget_para(paraloc^);
  1266. if (paraloc^.size=OS_NO) then
  1267. begin
  1268. { Can only be a reference that contains the rest
  1269. of the parameter }
  1270. if (paraloc^.loc<>LOC_REFERENCE) or
  1271. assigned(paraloc^.next) then
  1272. internalerror(2005013010);
  1273. gen_load_ref(paraloc^,href,sizeleft);
  1274. inc(href.offset,sizeleft);
  1275. sizeleft:=0;
  1276. end
  1277. else
  1278. begin
  1279. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1280. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1281. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1282. end;
  1283. paraloc:=paraloc^.next;
  1284. end;
  1285. end;
  1286. end;
  1287. LOC_CREGISTER :
  1288. begin
  1289. {$ifndef cpu64bit}
  1290. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1291. is_64bit(currpara.vartype.def) then
  1292. begin
  1293. if not assigned(paraloc^.next) then
  1294. internalerror(200410104);
  1295. if (target_info.endian=ENDIAN_BIG) then
  1296. begin
  1297. { paraloc^ -> high
  1298. paraloc^.next -> low }
  1299. unget_para(paraloc^);
  1300. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1301. unget_para(paraloc^.next^);
  1302. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1303. end
  1304. else
  1305. begin
  1306. { paraloc^ -> low
  1307. paraloc^.next -> high }
  1308. unget_para(paraloc^);
  1309. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1310. unget_para(paraloc^.next^);
  1311. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1312. end;
  1313. end
  1314. else
  1315. {$endif cpu64bit}
  1316. begin
  1317. if assigned(paraloc^.next) then
  1318. internalerror(200410105);
  1319. unget_para(paraloc^);
  1320. gen_load_reg(paraloc^,currpara.localloc.register);
  1321. end;
  1322. end;
  1323. LOC_CFPUREGISTER :
  1324. begin
  1325. {$ifdef sparc}
  1326. { Sparc passes floats in int registers, when loading to fpu register
  1327. we need a temp }
  1328. sizeleft := TCGSize2Size[currpara.localloc.size];
  1329. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1330. href:=tempref;
  1331. while assigned(paraloc) do
  1332. begin
  1333. unget_para(paraloc^);
  1334. gen_load_ref(paraloc^,href,sizeleft);
  1335. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1336. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1337. paraloc:=paraloc^.next;
  1338. end;
  1339. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1340. tg.UnGetTemp(list,tempref);
  1341. {$else sparc}
  1342. unget_para(paraloc^);
  1343. gen_load_reg(paraloc^,currpara.localloc.register);
  1344. if assigned(paraloc^.next) then
  1345. internalerror(200410109);
  1346. {$endif sparc}
  1347. end;
  1348. LOC_CMMREGISTER :
  1349. begin
  1350. unget_para(paraloc^);
  1351. gen_load_reg(paraloc^,currpara.localloc.register);
  1352. if assigned(paraloc^.next) then
  1353. internalerror(200410108);
  1354. end;
  1355. end;
  1356. end;
  1357. { generate copies of call by value parameters, must be done before
  1358. the initialization and body is parsed because the refcounts are
  1359. incremented using the local copies }
  1360. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1361. {$ifdef powerpc}
  1362. { unget the register that contains the stack pointer before the procedure entry, }
  1363. { which is used to access the parameters in their original callee-side location }
  1364. cg.a_reg_dealloc(list,NR_R12);
  1365. {$endif powerpc}
  1366. end;
  1367. procedure gen_initialize_code(list:TAAsmoutput);
  1368. begin
  1369. { initialize local data like ansistrings }
  1370. case current_procinfo.procdef.proctypeoption of
  1371. potype_unitinit:
  1372. begin
  1373. { this is also used for initialization of variables in a
  1374. program which does not have a globalsymtable }
  1375. if assigned(current_module.globalsymtable) then
  1376. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1377. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1378. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1379. end;
  1380. { units have seperate code for initilization and finalization }
  1381. potype_unitfinalize: ;
  1382. { program init/final is generated in separate procedure }
  1383. potype_proginit:
  1384. begin
  1385. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1386. end;
  1387. else
  1388. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1389. end;
  1390. { initialisizes temp. ansi/wide string data }
  1391. inittempvariables(list);
  1392. { initialize ansi/widesstring para's }
  1393. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1394. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1395. {$ifdef OLDREGVARS}
  1396. load_regvars(list,nil);
  1397. {$endif OLDREGVARS}
  1398. end;
  1399. procedure gen_finalize_code(list:TAAsmoutput);
  1400. begin
  1401. {$ifdef OLDREGVARS}
  1402. cleanup_regvars(list);
  1403. {$endif OLDREGVARS}
  1404. { finalize temporary data }
  1405. finalizetempvariables(list);
  1406. { finalize local data like ansistrings}
  1407. case current_procinfo.procdef.proctypeoption of
  1408. potype_unitfinalize:
  1409. begin
  1410. { this is also used for initialization of variables in a
  1411. program which does not have a globalsymtable }
  1412. if assigned(current_module.globalsymtable) then
  1413. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1414. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1415. end;
  1416. { units/progs have separate code for initialization and finalization }
  1417. potype_unitinit: ;
  1418. { program init/final is generated in separate procedure }
  1419. potype_proginit: ;
  1420. else
  1421. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1422. end;
  1423. { finalize paras data }
  1424. if assigned(current_procinfo.procdef.parast) and
  1425. not(po_assembler in current_procinfo.procdef.procoptions) then
  1426. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1427. end;
  1428. procedure gen_entry_code(list:TAAsmoutput);
  1429. var
  1430. href : treference;
  1431. paraloc1,
  1432. paraloc2 : 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. { initialize profiling for win32 }
  1457. if (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1458. (cs_profile in aktmoduleswitches) then
  1459. begin
  1460. reference_reset_symbol(href,objectlibrary.newasmsymbol('etext',AB_EXTERNAL,AT_DATA),0);
  1461. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1462. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1463. paramanager.allocparaloc(list,paraloc2);
  1464. cg.a_paramaddr_ref(list,href,paraloc2);
  1465. reference_reset_symbol(href,objectlibrary.newasmsymbol('__image_base__',AB_EXTERNAL,AT_DATA),0);
  1466. paramanager.allocparaloc(list,paraloc1);
  1467. cg.a_paramaddr_ref(list,href,paraloc1);
  1468. paramanager.freeparaloc(list,paraloc2);
  1469. paramanager.freeparaloc(list,paraloc1);
  1470. cg.alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1471. cg.a_call_name(list,'_monstartup');
  1472. cg.dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1473. end;
  1474. { initialize units }
  1475. cg.alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1476. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1477. cg.dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1478. {$ifdef GDB}
  1479. if (cs_debuginfo in aktmoduleswitches) then
  1480. if target_info.system <> system_powerpc_macos then
  1481. begin
  1482. { include reference to all debuginfo sections of used units }
  1483. hp:=tused_unit(usedunits.first);
  1484. while assigned(hp) do
  1485. begin
  1486. If (hp.u.flags and uf_has_debuginfo)=uf_has_debuginfo then
  1487. current_procinfo.aktlocaldata.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',hp.u.globalsymtable,''),AT_DATA,0));
  1488. hp:=tused_unit(hp.next);
  1489. end;
  1490. { include reference to debuginfo for this program }
  1491. current_procinfo.aktlocaldata.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  1492. end;
  1493. {$endif GDB}
  1494. end;
  1495. {$ifdef GDB}
  1496. if (cs_debuginfo in aktmoduleswitches) then
  1497. list.concat(Tai_force_line.Create);
  1498. {$endif GDB}
  1499. {$ifdef OLDREGVARS}
  1500. load_regvars(list,nil);
  1501. {$endif OLDREGVARS}
  1502. paraloc1.done;
  1503. paraloc2.done;
  1504. end;
  1505. procedure gen_exit_code(list:TAAsmoutput);
  1506. begin
  1507. { call __EXIT for main program }
  1508. if (not DLLsource) and
  1509. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1510. cg.a_call_name(list,'FPC_DO_EXIT');
  1511. end;
  1512. {****************************************************************************
  1513. Entry/Exit
  1514. ****************************************************************************}
  1515. procedure gen_proc_symbol(list:Taasmoutput);
  1516. var
  1517. hs : string;
  1518. begin
  1519. { add symbol entry point as well as debug information }
  1520. { will be inserted in front of the rest of this list. }
  1521. { Insert alignment and assembler names }
  1522. { Align, gprof uses 16 byte granularity }
  1523. if (cs_profile in aktmoduleswitches) then
  1524. list.concat(Tai_align.create(16))
  1525. else
  1526. list.concat(Tai_align.create(aktalignment.procalign));
  1527. {$ifdef GDB}
  1528. if (cs_debuginfo in aktmoduleswitches) then
  1529. begin
  1530. if (po_global in current_procinfo.procdef.procoptions) then
  1531. Tprocsym(current_procinfo.procdef.procsym).is_global:=true;
  1532. current_procinfo.procdef.concatstabto(list);
  1533. Tprocsym(current_procinfo.procdef.procsym).isstabwritten:=true;
  1534. end;
  1535. {$endif GDB}
  1536. repeat
  1537. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1538. if hs='' then
  1539. break;
  1540. {$ifdef GDB}
  1541. if (cs_debuginfo in aktmoduleswitches) and
  1542. target_info.use_function_relative_addresses then
  1543. list.concat(Tai_stab_function_name.create(strpnew(hs)));
  1544. {$endif GDB}
  1545. if (cs_profile in aktmoduleswitches) or
  1546. (po_global in current_procinfo.procdef.procoptions) then
  1547. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1548. else
  1549. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1550. until false;
  1551. end;
  1552. procedure gen_proc_symbol_end(list:Taasmoutput);
  1553. {$ifdef GDB}
  1554. var
  1555. stabsendlabel : tasmlabel;
  1556. mangled_length : longint;
  1557. p : pchar;
  1558. {$endif GDB}
  1559. begin
  1560. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1561. {$ifdef GDB}
  1562. if (cs_debuginfo in aktmoduleswitches) then
  1563. begin
  1564. objectlibrary.getlabel(stabsendlabel);
  1565. cg.a_label(list,stabsendlabel);
  1566. { define calling EBP as pseudo local var PM }
  1567. { this enables test if the function is a local one !! }
  1568. {if assigned(current_procinfo.parent) and
  1569. (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  1570. list.concat(Tai_stabs.Create(strpnew(
  1571. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1572. tostr(N_LSYM)+',0,0,'+tostr(current_procinfo.parent_framepointer_offset)))); }
  1573. if assigned(current_procinfo.procdef.funcretsym) and
  1574. (tabstractnormalvarsym(current_procinfo.procdef.funcretsym).refs>0) then
  1575. begin
  1576. if tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.loc=LOC_REFERENCE then
  1577. begin
  1578. {$warning Need to add gdb support for ret in param register calling}
  1579. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1580. begin
  1581. list.concat(Tai_stabs.Create(strpnew(
  1582. '"'+current_procinfo.procdef.procsym.name+':X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1583. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1584. if (m_result in aktmodeswitches) then
  1585. list.concat(Tai_stabs.Create(strpnew(
  1586. '"RESULT:X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1587. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))))
  1588. end
  1589. else
  1590. begin
  1591. list.concat(Tai_stabs.Create(strpnew(
  1592. '"'+current_procinfo.procdef.procsym.name+':X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1593. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1594. if (m_result in aktmodeswitches) then
  1595. list.concat(Tai_stabs.Create(strpnew(
  1596. '"RESULT:X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1597. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1598. end;
  1599. end;
  1600. end;
  1601. mangled_length:=length(current_procinfo.procdef.mangledname);
  1602. getmem(p,2*mangled_length+50);
  1603. strpcopy(p,'192,0,0,');
  1604. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1605. if (target_info.use_function_relative_addresses) then
  1606. begin
  1607. strpcopy(strend(p),'-');
  1608. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1609. end;
  1610. list.concat(Tai_stabn.Create(strnew(p)));
  1611. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1612. +current_procinfo.procdef.mangledname))));
  1613. p[0]:='2';p[1]:='2';p[2]:='4';
  1614. strpcopy(strend(p),'_end');}
  1615. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1616. if (target_info.use_function_relative_addresses) then
  1617. begin
  1618. strpcopy(strend(p),'-');
  1619. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1620. end;
  1621. list.concatlist(withdebuglist);
  1622. list.concat(Tai_stabn.Create(strnew(p)));
  1623. { strpnew('224,0,0,'
  1624. +current_procinfo.procdef.mangledname+'_end'))));}
  1625. freemem(p,2*mangled_length+50);
  1626. end;
  1627. {$endif GDB}
  1628. end;
  1629. procedure gen_proc_entry_code(list:Taasmoutput);
  1630. var
  1631. hitemp,
  1632. lotemp : longint;
  1633. begin
  1634. { generate call frame marker for dwarf call frame info }
  1635. dwarfcfi.start_frame(list);
  1636. { All temps are know, write offsets used for information }
  1637. if (cs_asm_source in aktglobalswitches) then
  1638. begin
  1639. if tg.direction>0 then
  1640. begin
  1641. lotemp:=current_procinfo.tempstart;
  1642. hitemp:=tg.lasttemp;
  1643. end
  1644. else
  1645. begin
  1646. lotemp:=tg.lasttemp;
  1647. hitemp:=current_procinfo.tempstart;
  1648. end;
  1649. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1650. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1651. end;
  1652. { generate target specific proc entry code }
  1653. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1654. end;
  1655. procedure gen_proc_exit_code(list:Taasmoutput);
  1656. var
  1657. parasize : longint;
  1658. begin
  1659. { c style clearstack does not need to remove parameters from the stack, only the
  1660. return value when it was pushed by arguments }
  1661. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1662. begin
  1663. parasize:=0;
  1664. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1665. inc(parasize,sizeof(aint));
  1666. end
  1667. else
  1668. parasize:=current_procinfo.para_stack_size;
  1669. { generate target specific proc exit code }
  1670. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1671. { release return registers, needed for optimizer }
  1672. if not is_void(current_procinfo.procdef.rettype.def) then
  1673. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1674. { end of frame marker for call frame info }
  1675. dwarfcfi.end_frame(list);
  1676. end;
  1677. procedure gen_stack_check_size_para(list:Taasmoutput);
  1678. var
  1679. paraloc1 : tcgpara;
  1680. begin
  1681. paraloc1.init;
  1682. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1683. paramanager.allocparaloc(list,paraloc1);
  1684. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1685. paramanager.freeparaloc(list,paraloc1);
  1686. paraloc1.done;
  1687. end;
  1688. procedure gen_stack_check_call(list:Taasmoutput);
  1689. var
  1690. paraloc1 : tcgpara;
  1691. begin
  1692. paraloc1.init;
  1693. { Also alloc the register needed for the parameter }
  1694. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1695. paramanager.allocparaloc(list,paraloc1);
  1696. paramanager.freeparaloc(list,paraloc1);
  1697. { Call the helper }
  1698. cg.alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1699. cg.a_call_name(list,'FPC_STACKCHECK');
  1700. cg.dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1701. paraloc1.done;
  1702. end;
  1703. procedure gen_save_used_regs(list:TAAsmoutput);
  1704. begin
  1705. { Pure assembler routines need to save the registers themselves }
  1706. if (po_assembler in current_procinfo.procdef.procoptions) then
  1707. exit;
  1708. { oldfpccall expects all registers to be destroyed }
  1709. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1710. cg.g_save_standard_registers(list);
  1711. end;
  1712. procedure gen_restore_used_regs(list:TAAsmoutput);
  1713. begin
  1714. { Pure assembler routines need to save the registers themselves }
  1715. if (po_assembler in current_procinfo.procdef.procoptions) then
  1716. exit;
  1717. { oldfpccall expects all registers to be destroyed }
  1718. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1719. cg.g_restore_standard_registers(list);
  1720. end;
  1721. {****************************************************************************
  1722. External handling
  1723. ****************************************************************************}
  1724. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  1725. begin
  1726. { add the procedure to the codesegment }
  1727. maybe_new_object_file(list);
  1728. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1729. list.concat(Tai_align.create(aktalignment.procalign));
  1730. if (po_global in pd.procoptions) then
  1731. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1732. else
  1733. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1734. cg.a_jmp_name(list,externalname);
  1735. end;
  1736. {****************************************************************************
  1737. Const Data
  1738. ****************************************************************************}
  1739. procedure insertbssdata(sym : tglobalvarsym);
  1740. var
  1741. l,varalign : longint;
  1742. storefilepos : tfileposinfo;
  1743. begin
  1744. storefilepos:=aktfilepos;
  1745. aktfilepos:=sym.fileinfo;
  1746. l:=sym.getsize;
  1747. if (vo_is_thread_var in sym.varoptions) then
  1748. inc(l,sizeof(aint));
  1749. varalign:=var_align(l);
  1750. maybe_new_object_file(bssSegment);
  1751. new_section(bssSegment,sec_bss,lower(sym.mangledname),varalign);
  1752. {$ifdef GDB}
  1753. if (cs_debuginfo in aktmoduleswitches) then
  1754. sym.concatstabto(bssSegment);
  1755. {$endif GDB}
  1756. if (sym.owner.symtabletype=globalsymtable) or
  1757. maybe_smartlink_symbol or
  1758. DLLSource or
  1759. (assigned(current_procinfo) and
  1760. (po_inline in current_procinfo.procdef.procoptions)) or
  1761. (vo_is_exported in sym.varoptions) or
  1762. (vo_is_C_var in sym.varoptions) then
  1763. bssSegment.concat(Tai_datablock.Create_global(sym.mangledname,l))
  1764. else
  1765. bssSegment.concat(Tai_datablock.Create(sym.mangledname,l));
  1766. aktfilepos:=storefilepos;
  1767. end;
  1768. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  1769. var
  1770. sym : tsym;
  1771. isaddr : boolean;
  1772. cgsize : tcgsize;
  1773. begin
  1774. sym:=tsym(st.symindex.first);
  1775. while assigned(sym) do
  1776. begin
  1777. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1778. begin
  1779. with tabstractnormalvarsym(sym) do
  1780. begin
  1781. { Parameters passed to assembler procedures need to be kept
  1782. in the original location }
  1783. if (sym.typ=paravarsym) and
  1784. (po_assembler in current_procinfo.procdef.procoptions) then
  1785. begin
  1786. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1787. end
  1788. else
  1789. begin
  1790. isaddr:=(st.symtabletype=parasymtable) and
  1791. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1792. if isaddr then
  1793. cgsize:=OS_ADDR
  1794. else
  1795. cgsize:=def_cgsize(vartype.def);
  1796. {$ifndef OLDREGVARS}
  1797. { When there is assembler code we can't use regvars }
  1798. if is_regvar then
  1799. begin
  1800. localloc.size:=cgsize;
  1801. case varregable of
  1802. vr_intreg :
  1803. begin
  1804. localloc.loc:=LOC_CREGISTER;
  1805. {$ifndef cpu64bit}
  1806. if cgsize in [OS_64,OS_S64] then
  1807. begin
  1808. localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1809. localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1810. end
  1811. else
  1812. {$endif cpu64bit}
  1813. localloc.register:=cg.getintregister(list,cgsize);
  1814. end;
  1815. vr_fpureg :
  1816. begin
  1817. localloc.loc:=LOC_CFPUREGISTER;
  1818. localloc.register:=cg.getfpuregister(list,cgsize);
  1819. end;
  1820. vr_mmreg :
  1821. begin
  1822. localloc.loc:=LOC_CMMREGISTER;
  1823. localloc.register:=cg.getmmregister(list,cgsize);
  1824. end;
  1825. else
  1826. internalerror(2004101010);
  1827. end;
  1828. { Allocate register already, to prevent first allocation to be
  1829. inside a loop }
  1830. {$ifndef cpu64bit}
  1831. if cgsize in [OS_64,OS_S64] then
  1832. begin
  1833. cg.a_reg_sync(list,localloc.register64.reglo);
  1834. cg.a_reg_sync(list,localloc.register64.reghi);
  1835. end
  1836. else
  1837. {$endif cpu64bit}
  1838. cg.a_reg_sync(list,localloc.register);
  1839. end
  1840. else
  1841. {$endif NOT OLDREGVARS}
  1842. begin
  1843. localloc.loc:=LOC_REFERENCE;
  1844. localloc.size:=cgsize;
  1845. case st.symtabletype of
  1846. parasymtable :
  1847. begin
  1848. { Reuse the parameter location for values to are at a single location on the stack }
  1849. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1850. begin
  1851. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1852. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1853. end
  1854. else
  1855. begin
  1856. if isaddr then
  1857. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1858. else
  1859. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1860. end;
  1861. end;
  1862. localsymtable,
  1863. stt_exceptsymtable :
  1864. begin
  1865. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1866. end;
  1867. staticsymtable :
  1868. begin
  1869. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1870. if not((target_info.system=system_powerpc_darwin) and
  1871. (cs_create_pic in aktmoduleswitches)) and
  1872. not(vo_is_dll_var in varoptions) and
  1873. not(vo_is_thread_var in varoptions) then
  1874. reference_reset_symbol(localloc.reference,objectlibrary.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA),0);
  1875. end;
  1876. else
  1877. internalerror(200410103);
  1878. end;
  1879. end;
  1880. end;
  1881. if cs_asm_source in aktglobalswitches then
  1882. begin
  1883. case localloc.loc of
  1884. LOC_REGISTER,
  1885. LOC_CREGISTER :
  1886. begin
  1887. if (cs_no_regalloc in aktglobalswitches) then
  1888. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register '+
  1889. std_regname(localloc.register))))
  1890. else
  1891. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register')));
  1892. end;
  1893. LOC_REFERENCE :
  1894. begin
  1895. if not assigned(localloc.reference.symbol) then
  1896. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1897. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1898. end;
  1899. end;
  1900. end;
  1901. end;
  1902. end;
  1903. sym:=tsym(sym.indexnext);
  1904. end;
  1905. end;
  1906. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  1907. var
  1908. sym : tsym;
  1909. begin
  1910. sym:=tsym(st.symindex.first);
  1911. while assigned(sym) do
  1912. begin
  1913. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1914. begin
  1915. with tabstractnormalvarsym(sym) do
  1916. begin
  1917. { Note: We need to keep the data available in memory
  1918. for the sub procedures that can access local data
  1919. in the parent procedures }
  1920. case localloc.loc of
  1921. LOC_CREGISTER :
  1922. {$ifndef cpu64bit}
  1923. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  1924. begin
  1925. cg.a_reg_sync(list,localloc.register64.reglo);
  1926. cg.a_reg_sync(list,localloc.register64.reghi);
  1927. end
  1928. else
  1929. {$endif cpu64bit}
  1930. cg.a_reg_sync(list,localloc.register);
  1931. LOC_REFERENCE :
  1932. begin
  1933. case st.symtabletype of
  1934. localsymtable,
  1935. parasymtable,
  1936. stt_exceptsymtable :
  1937. tg.Ungetlocal(list,localloc.reference);
  1938. end;
  1939. end;
  1940. end;
  1941. end;
  1942. end;
  1943. sym:=tsym(sym.indexnext);
  1944. end;
  1945. end;
  1946. {$ifdef PASS2INLINE}
  1947. procedure gen_alloc_inline_parast(list:TAAsmoutput;pd:tprocdef);
  1948. var
  1949. sym : tsym;
  1950. calleeparaloc,
  1951. callerparaloc : pcgparalocation;
  1952. begin
  1953. if (po_assembler in pd.procoptions) then
  1954. exit;
  1955. sym:=tsym(pd.parast.symindex.first);
  1956. while assigned(sym) do
  1957. begin
  1958. if sym.typ=paravarsym then
  1959. begin
  1960. with tparavarsym(sym) do
  1961. begin
  1962. { for localloc <> LOC_REFERENCE, we need regvar support inside inlined procedures }
  1963. localloc.loc:=LOC_REFERENCE;
  1964. localloc.size:=int_cgsize(paramanager.push_size(varspez,vartype.def,pd.proccalloption));
  1965. tg.GetLocal(list,tcgsize2size[localloc.size],vartype.def,localloc.reference);
  1966. calleeparaloc:=paraloc[calleeside].location;
  1967. callerparaloc:=paraloc[callerside].location;
  1968. while assigned(calleeparaloc) do
  1969. begin
  1970. if not assigned(callerparaloc) then
  1971. internalerror(200408281);
  1972. if calleeparaloc^.loc<>callerparaloc^.loc then
  1973. internalerror(200408282);
  1974. case calleeparaloc^.loc of
  1975. LOC_FPUREGISTER:
  1976. begin
  1977. calleeparaloc^.register:=cg.getfpuregister(list,calleeparaloc^.size);
  1978. callerparaloc^.register:=calleeparaloc^.register;
  1979. end;
  1980. LOC_REGISTER:
  1981. begin
  1982. calleeparaloc^.register:=cg.getintregister(list,calleeparaloc^.size);
  1983. callerparaloc^.register:=calleeparaloc^.register;
  1984. end;
  1985. LOC_MMREGISTER:
  1986. begin
  1987. calleeparaloc^.register:=cg.getmmregister(list,calleeparaloc^.size);
  1988. callerparaloc^.register:=calleeparaloc^.register;
  1989. end;
  1990. LOC_REFERENCE:
  1991. begin
  1992. calleeparaloc^.reference.offset := localloc.reference.offset;
  1993. calleeparaloc^.reference.index := localloc.reference.base;
  1994. callerparaloc^.reference.offset := localloc.reference.offset;
  1995. callerparaloc^.reference.index := localloc.reference.base;
  1996. end;
  1997. end;
  1998. calleeparaloc:=calleeparaloc^.next;
  1999. callerparaloc:=callerparaloc^.next;
  2000. end;
  2001. if cs_asm_source in aktglobalswitches then
  2002. begin
  2003. case localloc.loc of
  2004. LOC_REFERENCE :
  2005. list.concat(Tai_comment.Create(strpnew('Para '+realname+' allocated at '+
  2006. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  2007. end;
  2008. end;
  2009. end;
  2010. end;
  2011. sym:=tsym(sym.indexnext);
  2012. end;
  2013. end;
  2014. procedure gen_alloc_inline_funcret(list:TAAsmoutput;pd:tprocdef);
  2015. var
  2016. callerparaloc : tlocation;
  2017. begin
  2018. if not assigned(pd.funcretsym) or
  2019. (po_assembler in pd.procoptions) then
  2020. exit;
  2021. { for localloc <> LOC_REFERENCE, we need regvar support inside inlined procedures }
  2022. with tabstractnormalvarsym(pd.funcretsym) do
  2023. begin
  2024. localloc.loc:=LOC_REFERENCE;
  2025. localloc.size:=int_cgsize(paramanager.push_size(varspez,vartype.def,pd.proccalloption));
  2026. tg.GetLocal(list,tcgsize2size[localloc.size],vartype.def,localloc.reference);
  2027. callerparaloc:=pd.funcretloc[callerside];
  2028. case pd.funcretloc[calleeside].loc of
  2029. LOC_FPUREGISTER:
  2030. begin
  2031. pd.funcretloc[calleeside].register:=cg.getfpuregister(list,pd.funcretloc[calleeside].size);
  2032. pd.funcretloc[callerside].register:=pd.funcretloc[calleeside].register;
  2033. end;
  2034. LOC_REGISTER:
  2035. begin
  2036. {$ifndef cpu64bit}
  2037. if callerparaloc.size in [OS_64,OS_S64] then
  2038. begin
  2039. end
  2040. else
  2041. {$endif cpu64bit}
  2042. begin
  2043. pd.funcretloc[calleeside].register:=cg.getintregister(list,pd.funcretloc[calleeside].size);
  2044. pd.funcretloc[callerside].register:=pd.funcretloc[calleeside].register;
  2045. end;
  2046. end;
  2047. LOC_MMREGISTER:
  2048. begin
  2049. pd.funcretloc[calleeside].register:=cg.getmmregister(list,pd.funcretloc[calleeside].size);
  2050. pd.funcretloc[callerside].register:=pd.funcretloc[calleeside].register;
  2051. end;
  2052. LOC_REFERENCE:
  2053. begin
  2054. pd.funcretloc[calleeside].reference.offset := localloc.reference.offset;
  2055. pd.funcretloc[calleeside].reference.index := localloc.reference.base;
  2056. pd.funcretloc[callerside].reference.offset := localloc.reference.offset;
  2057. pd.funcretloc[callerside].reference.index := localloc.reference.base;
  2058. end;
  2059. LOC_VOID:
  2060. ;
  2061. else
  2062. internalerror(200411191);
  2063. end;
  2064. if cs_asm_source in aktglobalswitches then
  2065. begin
  2066. case localloc.loc of
  2067. LOC_REFERENCE :
  2068. list.concat(Tai_comment.Create(strpnew('Funcret '+realname+' allocated at '+
  2069. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  2070. end;
  2071. end;
  2072. end;
  2073. end;
  2074. {$endif PASS2INLINE}
  2075. { persistent rtti generation }
  2076. procedure generate_rtti(p:Ttypesym);
  2077. var
  2078. rsym : trttisym;
  2079. def : tstoreddef;
  2080. begin
  2081. { rtti can only be generated for classes that are always typesyms }
  2082. def:=tstoreddef(ttypesym(p).restype.def);
  2083. { there is an error, skip rtti info }
  2084. if (def.deftype=errordef) or (Errorcount>0) then
  2085. exit;
  2086. { only create rtti once for each definition }
  2087. if not(df_has_rttitable in def.defoptions) then
  2088. begin
  2089. { definition should be in the same symtable as the symbol }
  2090. if p.owner<>def.owner then
  2091. internalerror(200108262);
  2092. { create rttisym }
  2093. rsym:=trttisym.create(p.name,fullrtti);
  2094. p.owner.insert(rsym);
  2095. { register rttisym in definition }
  2096. include(def.defoptions,df_has_rttitable);
  2097. def.rttitablesym:=rsym;
  2098. { write rtti data }
  2099. def.write_child_rtti_data(fullrtti);
  2100. maybe_new_object_file(rttilist);
  2101. new_section(rttilist,sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2102. rttiList.concat(Tai_symbol.Create_global(rsym.get_label,0));
  2103. def.write_rtti_data(fullrtti);
  2104. rttiList.concat(Tai_symbol_end.Create(rsym.get_label));
  2105. end;
  2106. end;
  2107. { persistent init table generation }
  2108. procedure generate_inittable(p:tsym);
  2109. var
  2110. rsym : trttisym;
  2111. def : tstoreddef;
  2112. begin
  2113. { anonymous types are also allowed for records that can be varsym }
  2114. case p.typ of
  2115. typesym :
  2116. def:=tstoreddef(ttypesym(p).restype.def);
  2117. globalvarsym,
  2118. localvarsym,
  2119. paravarsym :
  2120. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  2121. else
  2122. internalerror(200108263);
  2123. end;
  2124. { only create inittable once for each definition }
  2125. if not(df_has_inittable in def.defoptions) then
  2126. begin
  2127. { definition should be in the same symtable as the symbol }
  2128. if p.owner<>def.owner then
  2129. internalerror(200108264);
  2130. { create rttisym }
  2131. rsym:=trttisym.create(p.name,initrtti);
  2132. p.owner.insert(rsym);
  2133. { register rttisym in definition }
  2134. include(def.defoptions,df_has_inittable);
  2135. def.inittablesym:=rsym;
  2136. { write inittable data }
  2137. def.write_child_rtti_data(initrtti);
  2138. maybe_new_object_file(rttilist);
  2139. new_section(rttilist,sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2140. rttiList.concat(Tai_symbol.Create_global(rsym.get_label,0));
  2141. def.write_rtti_data(initrtti);
  2142. rttiList.concat(Tai_symbol_end.Create(rsym.get_label));
  2143. end;
  2144. end;
  2145. procedure gen_intf_wrapper(list:taasmoutput;_class:tobjectdef);
  2146. var
  2147. i,j,
  2148. proccount : longint;
  2149. tmps : string;
  2150. begin
  2151. for i:=1 to _class.implementedinterfaces.count do
  2152. begin
  2153. { only if implemented by this class }
  2154. if _class.implementedinterfaces.implindex(i)=i then
  2155. begin
  2156. proccount:=_class.implementedinterfaces.implproccount(i);
  2157. for j:=1 to proccount do
  2158. begin
  2159. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2160. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  2161. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  2162. { create wrapper code }
  2163. new_section(list,sec_code,lower(tmps),0);
  2164. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  2165. end;
  2166. end;
  2167. end;
  2168. end;
  2169. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  2170. var
  2171. def : tstoreddef;
  2172. begin
  2173. def:=tstoreddef(st.defindex.first);
  2174. while assigned(def) do
  2175. begin
  2176. if is_class(def) then
  2177. gen_intf_wrapper(list,tobjectdef(def));
  2178. def:=tstoreddef(def.indexnext);
  2179. end;
  2180. end;
  2181. end.