ncgutil.pas 106 KB

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