ncgutil.pas 108 KB

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