ncgutil.pas 118 KB

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