ncgutil.pas 120 KB

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