ncgutil.pas 89 KB

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