ncgutil.pas 110 KB

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