ncgutil.pas 110 KB

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