ncgutil.pas 109 KB

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