ncgutil.pas 127 KB

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