ncgutil.pas 108 KB

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