ncgutil.pas 127 KB

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