ncgutil.pas 129 KB

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