cgai386.pas 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Helper routines for the i386 code generator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgai386;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cpubase,cpuasm,
  23. symconst,symtype,symdef,aasm;
  24. {$define TESTGETTEMP to store const that
  25. are written into temps for later release PM }
  26. function def_opsize(p1:tdef):topsize;
  27. function def2def_opsize(p1,p2:tdef):topsize;
  28. function def_getreg(p1:tdef):tregister;
  29. function makereg8(r:tregister):tregister;
  30. function makereg16(r:tregister):tregister;
  31. function makereg32(r:tregister):tregister;
  32. procedure locflags2reg(var l:tlocation;opsize:topsize);
  33. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: tasmlabel);
  34. procedure emitlab(var l : tasmlabel);
  35. procedure emitjmp(c : tasmcond;var l : tasmlabel);
  36. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  37. procedure emit_none(i : tasmop;s : topsize);
  38. procedure emit_const(i : tasmop;s : topsize;c : longint);
  39. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  40. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  41. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  42. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  43. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  44. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  45. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  46. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  47. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  48. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  49. procedure emit_sym_ofs(i : tasmop;s : topsize;op : tasmsymbol;ofs : longint);
  50. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;reg : tregister);
  51. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;ref : preference);
  52. procedure emitcall(const routine:string);
  53. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  54. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  55. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  56. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  57. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  58. procedure emit_push_loc(const t:tlocation);
  59. procedure emit_push_mem_size(const t: treference; size: longint);
  60. { pushes qword location to the stack }
  61. procedure emit_pushq_loc(const t : tlocation);
  62. procedure release_qword_loc(const t : tlocation);
  63. { remove non regvar registers in loc from regs (in the format }
  64. { pushusedregisters uses) }
  65. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  66. { releases the registers of a location }
  67. procedure release_loc(const t : tlocation);
  68. procedure emit_pushw_loc(const t:tlocation);
  69. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  70. procedure emit_to_mem(var t:tlocation;def:tdef);
  71. procedure emit_to_reg16(var hr:tregister);
  72. procedure emit_to_reg32(var hr:tregister);
  73. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  74. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  75. procedure copyshortstring(const dref,sref : treference;len : byte;
  76. loadref, del_sref: boolean);
  77. procedure finalize(t : tdef;const ref : treference;is_already_ref : boolean);
  78. procedure incrstringref(t : tdef;const ref : treference);
  79. procedure decrstringref(t : tdef;const ref : treference);
  80. procedure push_int(l : longint);
  81. procedure emit_push_mem(const ref : treference);
  82. procedure emitpushreferenceaddr(const ref : treference);
  83. procedure incrcomintfref(t: tdef; const ref: treference);
  84. procedure decrcomintfref(t: tdef; const ref: treference);
  85. procedure floatload(t : tfloattype;const ref : treference);
  86. procedure floatstore(t : tfloattype;const ref : treference);
  87. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  88. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  89. procedure maybe_loadself;
  90. procedure emitloadord2reg(const location:Tlocation;orddef:torddef;destreg:Tregister;delloc:boolean);
  91. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
  92. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  93. stackframe:longint;
  94. var parasize:longint;var nostackframe:boolean;
  95. inlined : boolean);
  96. procedure genexitcode(alist : TAAsmoutput;parasize:longint;
  97. nostackframe,inlined:boolean);
  98. { if a unit doesn't have a explicit init/final code, }
  99. { we've to generate one, if the units has ansistrings }
  100. { in the interface or implementation }
  101. procedure genimplicitunitfinal(alist : TAAsmoutput);
  102. procedure genimplicitunitinit(alist : TAAsmoutput);
  103. {$ifdef test_dest_loc}
  104. const
  105. { used to avoid temporary assignments }
  106. dest_loc_known : boolean = false;
  107. in_dest_loc : boolean = false;
  108. dest_loc_tree : ptree = nil;
  109. var
  110. dest_loc : tlocation;
  111. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  112. {$endif test_dest_loc}
  113. implementation
  114. uses
  115. {$ifdef delphi}
  116. sysutils,
  117. {$else}
  118. strings,
  119. {$endif}
  120. cutils,cclasses,
  121. globtype,systems,globals,verbose,
  122. fmodule,
  123. symbase,symsym,symtable,types,
  124. tgcpu,temp_gen,hcodegen,regvars
  125. {$ifdef GDB}
  126. ,gdb
  127. {$endif}
  128. {$ifndef NOTARGETWIN32}
  129. ,t_win32
  130. {$endif}
  131. ;
  132. {*****************************************************************************
  133. Helpers
  134. *****************************************************************************}
  135. function def_opsize(p1:tdef):topsize;
  136. begin
  137. case p1.size of
  138. 1 : def_opsize:=S_B;
  139. 2 : def_opsize:=S_W;
  140. 4 : def_opsize:=S_L;
  141. else
  142. internalerror(130820001);
  143. end;
  144. end;
  145. function def2def_opsize(p1,p2:tdef):topsize;
  146. var
  147. o1 : topsize;
  148. begin
  149. case p1.size of
  150. 1 : o1:=S_B;
  151. 2 : o1:=S_W;
  152. 4 : o1:=S_L;
  153. { I don't know if we need it (FK) }
  154. 8 : o1:=S_L;
  155. else
  156. internalerror(130820002);
  157. end;
  158. if assigned(p2) then
  159. begin
  160. case p2.size of
  161. 1 : o1:=S_B;
  162. 2 : begin
  163. if o1=S_B then
  164. o1:=S_BW
  165. else
  166. o1:=S_W;
  167. end;
  168. 4,8:
  169. begin
  170. case o1 of
  171. S_B : o1:=S_BL;
  172. S_W : o1:=S_WL;
  173. end;
  174. end;
  175. end;
  176. end;
  177. def2def_opsize:=o1;
  178. end;
  179. function def_getreg(p1:tdef):tregister;
  180. begin
  181. case p1.size of
  182. 1 : def_getreg:=reg32toreg8(getregister32);
  183. 2 : def_getreg:=reg32toreg16(getregister32);
  184. 4 : def_getreg:=getregister32;
  185. else
  186. internalerror(130820003);
  187. end;
  188. end;
  189. function makereg8(r:tregister):tregister;
  190. begin
  191. case r of
  192. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  193. makereg8:=reg32toreg8(r);
  194. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  195. makereg8:=reg16toreg8(r);
  196. R_AL,R_BL,R_CL,R_DL :
  197. makereg8:=r;
  198. end;
  199. end;
  200. function makereg16(r:tregister):tregister;
  201. begin
  202. case r of
  203. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  204. makereg16:=reg32toreg16(r);
  205. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  206. makereg16:=r;
  207. R_AL,R_BL,R_CL,R_DL :
  208. makereg16:=reg8toreg16(r);
  209. end;
  210. end;
  211. function makereg32(r:tregister):tregister;
  212. begin
  213. case r of
  214. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  215. makereg32:=r;
  216. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  217. makereg32:=reg16toreg32(r);
  218. R_AL,R_BL,R_CL,R_DL :
  219. makereg32:=reg8toreg32(r);
  220. end;
  221. end;
  222. procedure locflags2reg(var l:tlocation;opsize:topsize);
  223. var
  224. hregister : tregister;
  225. begin
  226. if (l.loc=LOC_FLAGS) then
  227. begin
  228. hregister:=getregister32;
  229. case opsize of
  230. S_W : hregister:=reg32toreg16(hregister);
  231. S_B : hregister:=reg32toreg8(hregister);
  232. end;
  233. emit_flag2reg(l.resflags,hregister);
  234. l.loc:=LOC_REGISTER;
  235. l.register:=hregister;
  236. end
  237. else internalerror(270720001);
  238. end;
  239. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: tasmlabel);
  240. var
  241. hregister : tregister;
  242. hl : tasmlabel;
  243. begin
  244. if l.loc = LOC_JUMP then
  245. begin
  246. hregister:=getregister32;
  247. case opsize of
  248. S_W : hregister:=reg32toreg16(hregister);
  249. S_B : hregister:=reg32toreg8(hregister);
  250. end;
  251. l.loc:=LOC_REGISTER;
  252. l.register:=hregister;
  253. emitlab(truelabel);
  254. truelabel:=otl;
  255. emit_const_reg(A_MOV,opsize,1,hregister);
  256. getlabel(hl);
  257. emitjmp(C_None,hl);
  258. emitlab(falselabel);
  259. falselabel:=ofl;
  260. emit_reg_reg(A_XOR,S_L,makereg32(hregister),
  261. makereg32(hregister));
  262. emitlab(hl);
  263. end
  264. else internalerror(270720002);
  265. end;
  266. {*****************************************************************************
  267. Emit Assembler
  268. *****************************************************************************}
  269. procedure emitlab(var l : tasmlabel);
  270. begin
  271. if not l.is_set then
  272. exprasmList.concat(Tai_label.Create(l))
  273. else
  274. internalerror(7453984);
  275. end;
  276. procedure emitjmp(c : tasmcond;var l : tasmlabel);
  277. var
  278. ai : taicpu;
  279. begin
  280. if c=C_None then
  281. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  282. else
  283. begin
  284. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  285. ai.SetCondition(c);
  286. end;
  287. ai.is_jmp:=true;
  288. exprasmList.concat(ai);
  289. end;
  290. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  291. var
  292. ai : taicpu;
  293. hreg : tregister;
  294. begin
  295. hreg:=makereg8(hregister);
  296. ai:=Taicpu.Op_reg(A_Setcc,S_B,hreg);
  297. ai.SetCondition(flag_2_cond[flag]);
  298. exprasmList.concat(ai);
  299. if hreg<>hregister then
  300. begin
  301. if hregister in regset16bit then
  302. emit_to_reg16(hreg)
  303. else
  304. emit_to_reg32(hreg);
  305. end;
  306. end;
  307. procedure emit_none(i : tasmop;s : topsize);
  308. begin
  309. exprasmList.concat(Taicpu.Op_none(i,s));
  310. end;
  311. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  312. begin
  313. exprasmList.concat(Taicpu.Op_reg(i,s,reg));
  314. end;
  315. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  316. begin
  317. exprasmList.concat(Taicpu.Op_ref(i,s,ref));
  318. end;
  319. procedure emit_const(i : tasmop;s : topsize;c : longint);
  320. begin
  321. exprasmList.concat(Taicpu.Op_const(i,s,c));
  322. end;
  323. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  324. begin
  325. exprasmList.concat(Taicpu.Op_const_reg(i,s,c,reg));
  326. end;
  327. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  328. begin
  329. exprasmList.concat(Taicpu.Op_const_ref(i,s,c,ref));
  330. end;
  331. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  332. begin
  333. exprasmList.concat(Taicpu.Op_ref_reg(i,s,ref,reg));
  334. end;
  335. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  336. begin
  337. exprasmList.concat(Taicpu.Op_reg_ref(i,s,reg,ref));
  338. end;
  339. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  340. begin
  341. if (reg1<>reg2) or (i<>A_MOV) then
  342. exprasmList.concat(Taicpu.Op_reg_reg(i,s,reg1,reg2));
  343. end;
  344. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  345. begin
  346. exprasmList.concat(Taicpu.Op_const_reg_reg(i,s,c,reg1,reg2));
  347. end;
  348. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  349. begin
  350. exprasmList.concat(Taicpu.Op_reg_reg_reg(i,s,reg1,reg2,reg3));
  351. end;
  352. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  353. begin
  354. exprasmList.concat(Taicpu.Op_sym(i,s,op));
  355. end;
  356. procedure emit_sym_ofs(i : tasmop;s : topsize;op : tasmsymbol;ofs : longint);
  357. begin
  358. exprasmList.concat(Taicpu.Op_sym_ofs(i,s,op,ofs));
  359. end;
  360. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;reg : tregister);
  361. begin
  362. exprasmList.concat(Taicpu.Op_sym_ofs_reg(i,s,op,ofs,reg));
  363. end;
  364. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;ref : preference);
  365. begin
  366. exprasmList.concat(Taicpu.Op_sym_ofs_ref(i,s,op,ofs,ref));
  367. end;
  368. procedure emitcall(const routine:string);
  369. begin
  370. exprasmList.concat(Taicpu.Op_sym(A_CALL,S_NO,newasmsymbol(routine)));
  371. end;
  372. { only usefull in startup code }
  373. procedure emitinsertcall(const routine:string);
  374. begin
  375. exprasmList.insert(Taicpu.Op_sym(A_CALL,S_NO,newasmsymbol(routine)));
  376. end;
  377. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  378. var
  379. hreg : tregister;
  380. pushedeax : boolean;
  381. begin
  382. pushedeax:=false;
  383. case t.loc of
  384. LOC_REGISTER,
  385. LOC_CREGISTER : begin
  386. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,siz,
  387. t.register,newreference(ref)));
  388. ungetregister32(t.register); { the register is not needed anymore }
  389. end;
  390. LOC_MEM,
  391. LOC_REFERENCE : begin
  392. if t.reference.is_immediate then
  393. emit_const_ref(A_MOV,siz,
  394. t.reference.offset,newreference(ref))
  395. else
  396. begin
  397. case siz of
  398. S_B : begin
  399. { we can't do a getregister in the code generator }
  400. { without problems!!! }
  401. if usablereg32>0 then
  402. hreg:=reg32toreg8(getregister32)
  403. else
  404. begin
  405. emit_reg(A_PUSH,S_L,R_EAX);
  406. pushedeax:=true;
  407. hreg:=R_AL;
  408. end;
  409. end;
  410. S_W : hreg:=R_DI;
  411. S_L : hreg:=R_EDI;
  412. end;
  413. if hreg in [R_DI,R_EDI] then
  414. getexplicitregister32(R_EDI);
  415. emit_ref_reg(A_MOV,siz,
  416. newreference(t.reference),hreg);
  417. del_reference(t.reference);
  418. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,siz,
  419. hreg,newreference(ref)));
  420. if siz=S_B then
  421. begin
  422. if pushedeax then
  423. emit_reg(A_POP,S_L,R_EAX)
  424. else
  425. ungetregister(hreg);
  426. end;
  427. if hreg in [R_DI,R_EDI] then
  428. ungetregister32(R_EDI);
  429. { we can release the registers }
  430. { but only AFTER the MOV! Important for the optimizer!
  431. (JM)}
  432. del_reference(ref);
  433. end;
  434. if freetemp then
  435. ungetiftemp(t.reference);
  436. end;
  437. else
  438. internalerror(330);
  439. end;
  440. end;
  441. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  442. begin
  443. case t.loc of
  444. LOC_REGISTER,
  445. LOC_CREGISTER : begin
  446. emit_reg_reg(A_MOV,S_L,t.register,reg);
  447. ungetregister32(t.register); { the register is not needed anymore }
  448. end;
  449. LOC_MEM,
  450. LOC_REFERENCE : begin
  451. if t.reference.is_immediate then
  452. emit_const_reg(A_MOV,S_L,
  453. t.reference.offset,reg)
  454. else
  455. begin
  456. emit_ref_reg(A_MOV,S_L,
  457. newreference(t.reference),reg);
  458. end;
  459. end;
  460. else
  461. internalerror(330);
  462. end;
  463. end;
  464. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  465. begin
  466. case t.loc of
  467. LOC_REGISTER,
  468. LOC_CREGISTER : begin
  469. emit_reg_reg(A_MOV,RegSize(Reg),
  470. reg,t.register);
  471. end;
  472. LOC_MEM,
  473. LOC_REFERENCE : begin
  474. if t.reference.is_immediate then
  475. internalerror(334)
  476. else
  477. begin
  478. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,RegSize(Reg),
  479. Reg,newreference(t.reference)));
  480. end;
  481. end;
  482. else
  483. internalerror(330);
  484. end;
  485. end;
  486. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  487. begin
  488. case t.loc of
  489. LOC_MEM,
  490. LOC_REFERENCE : begin
  491. if t.reference.is_immediate then
  492. internalerror(331)
  493. else
  494. begin
  495. emit_ref_reg(A_LEA,S_L,
  496. newreference(t.reference),reg);
  497. end;
  498. if freetemp then
  499. ungetiftemp(t.reference);
  500. end;
  501. else
  502. internalerror(332);
  503. end;
  504. end;
  505. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  506. begin
  507. case t.loc of
  508. LOC_REGISTER,
  509. LOC_CREGISTER : begin
  510. emit_reg_reg(A_MOV,S_L,
  511. reglow,t.registerlow);
  512. emit_reg_reg(A_MOV,S_L,
  513. reghigh,t.registerhigh);
  514. end;
  515. LOC_MEM,
  516. LOC_REFERENCE : begin
  517. if t.reference.is_immediate then
  518. internalerror(334)
  519. else
  520. begin
  521. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  522. Reglow,newreference(t.reference)));
  523. inc(t.reference.offset,4);
  524. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  525. Reghigh,newreference(t.reference)));
  526. end;
  527. end;
  528. else
  529. internalerror(330);
  530. end;
  531. end;
  532. procedure emit_pushq_loc(const t : tlocation);
  533. var
  534. hr : preference;
  535. begin
  536. case t.loc of
  537. LOC_REGISTER,
  538. LOC_CREGISTER:
  539. begin
  540. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,
  541. t.registerhigh));
  542. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,
  543. t.registerlow));
  544. end;
  545. LOC_MEM,
  546. LOC_REFERENCE:
  547. begin
  548. hr:=newreference(t.reference);
  549. inc(hr^.offset,4);
  550. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  551. hr));
  552. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  553. newreference(t.reference)));
  554. ungetiftemp(t.reference);
  555. end;
  556. else internalerror(331);
  557. end;
  558. end;
  559. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  560. begin
  561. case t.loc of
  562. LOC_REGISTER:
  563. { can't be a regvar, since it would be LOC_CREGISTER then }
  564. regs := regs and not($80 shr byte(t.register));
  565. LOC_MEM,LOC_REFERENCE:
  566. begin
  567. if not(cs_regalloc in aktglobalswitches) or
  568. (t.reference.base in usableregs) then
  569. regs := regs and
  570. not($80 shr byte(t.reference.base));
  571. if not(cs_regalloc in aktglobalswitches) or
  572. (t.reference.index in usableregs) then
  573. regs := regs and
  574. not($80 shr byte(t.reference.index));
  575. end;
  576. end;
  577. end;
  578. procedure release_loc(const t : tlocation);
  579. begin
  580. case t.loc of
  581. LOC_REGISTER,
  582. LOC_CREGISTER:
  583. begin
  584. ungetregister32(t.register);
  585. end;
  586. LOC_MEM,
  587. LOC_REFERENCE:
  588. del_reference(t.reference);
  589. else internalerror(332);
  590. end;
  591. end;
  592. procedure release_qword_loc(const t : tlocation);
  593. begin
  594. case t.loc of
  595. LOC_REGISTER,
  596. LOC_CREGISTER:
  597. begin
  598. ungetregister32(t.registerhigh);
  599. ungetregister32(t.registerlow);
  600. end;
  601. LOC_MEM,
  602. LOC_REFERENCE:
  603. del_reference(t.reference);
  604. else internalerror(331);
  605. end;
  606. end;
  607. procedure emit_push_loc(const t:tlocation);
  608. begin
  609. case t.loc of
  610. LOC_REGISTER,
  611. LOC_CREGISTER : begin
  612. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,makereg32(t.register)));
  613. ungetregister(t.register); { the register is not needed anymore }
  614. end;
  615. LOC_MEM,
  616. LOC_REFERENCE : begin
  617. if t.reference.is_immediate then
  618. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,t.reference.offset))
  619. else
  620. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(t.reference)));
  621. del_reference(t.reference);
  622. ungetiftemp(t.reference);
  623. end;
  624. else
  625. internalerror(330);
  626. end;
  627. end;
  628. procedure emit_pushw_loc(const t:tlocation);
  629. var
  630. opsize : topsize;
  631. begin
  632. case t.loc of
  633. LOC_REGISTER,
  634. LOC_CREGISTER : begin
  635. if target_info.stackalignment=4 then
  636. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,makereg32(t.register)))
  637. else
  638. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_W,makereg16(t.register)));
  639. ungetregister(t.register); { the register is not needed anymore }
  640. end;
  641. LOC_MEM,
  642. LOC_REFERENCE : begin
  643. if target_info.stackalignment=4 then
  644. opsize:=S_L
  645. else
  646. opsize:=S_W;
  647. if t.reference.is_immediate then
  648. exprasmList.concat(Taicpu.Op_const(A_PUSH,opsize,t.reference.offset))
  649. else
  650. exprasmList.concat(Taicpu.Op_ref(A_PUSH,opsize,newreference(t.reference)));
  651. del_reference(t.reference);
  652. ungetiftemp(t.reference);
  653. end;
  654. else
  655. internalerror(330);
  656. end;
  657. end;
  658. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  659. begin
  660. case t.loc of
  661. LOC_MEM,
  662. LOC_REFERENCE : begin
  663. if t.reference.is_immediate then
  664. internalerror(331)
  665. else
  666. begin
  667. getexplicitregister32(R_EDI);
  668. emit_ref_reg(A_LEA,S_L,
  669. newreference(t.reference),R_EDI);
  670. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  671. R_EDI,newreference(ref)));
  672. ungetregister32(R_EDI);
  673. end;
  674. { release the registers }
  675. del_reference(t.reference);
  676. if freetemp then
  677. ungetiftemp(t.reference);
  678. end;
  679. else
  680. internalerror(332);
  681. end;
  682. end;
  683. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  684. begin
  685. case t.loc of
  686. LOC_MEM,
  687. LOC_REFERENCE : begin
  688. if t.reference.is_immediate then
  689. internalerror(331)
  690. else
  691. begin
  692. getexplicitregister32(R_EDI);
  693. emit_ref_reg(A_LEA,S_L,
  694. newreference(t.reference),R_EDI);
  695. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  696. ungetregister32(R_EDI);
  697. end;
  698. if freetemp then
  699. ungetiftemp(t.reference);
  700. end;
  701. else
  702. internalerror(332);
  703. end;
  704. end;
  705. procedure emit_push_mem_size(const t: treference; size: longint);
  706. var
  707. s: topsize;
  708. begin
  709. if t.is_immediate then
  710. begin
  711. if (size=4) or
  712. (target_info.stackalignment=4) then
  713. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,t.offset))
  714. else
  715. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_W,t.offset));
  716. end
  717. else
  718. if size < 4 then
  719. begin
  720. getexplicitregister32(R_EDI);
  721. case size of
  722. 1: s := S_BL;
  723. 2: s := S_WL;
  724. else internalerror(200008071);
  725. end;
  726. exprasmList.concat(Taicpu.Op_ref_reg(A_MOVZX,s,
  727. newreference(t),R_EDI));
  728. if target_info.stackalignment=4 then
  729. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI))
  730. else
  731. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_W,R_DI));
  732. ungetregister32(R_EDI);
  733. end
  734. else
  735. if size = 4 then
  736. emit_push_mem(t)
  737. else
  738. internalerror(200008072);
  739. end;
  740. procedure emit_to_mem(var t:tlocation;def:tdef);
  741. var
  742. r : treference;
  743. begin
  744. case t.loc of
  745. LOC_FPU : begin
  746. reset_reference(t.reference);
  747. gettempofsizereference(10,t.reference);
  748. floatstore(tfloatdef(def).typ,t.reference);
  749. end;
  750. LOC_REGISTER:
  751. begin
  752. if is_64bitint(def) then
  753. begin
  754. gettempofsizereference(8,r);
  755. emit_reg_ref(A_MOV,S_L,t.registerlow,newreference(r));
  756. inc(r.offset,4);
  757. emit_reg_ref(A_MOV,S_L,t.registerhigh,newreference(r));
  758. dec(r.offset,4);
  759. t.reference:=r;
  760. end
  761. else
  762. internalerror(1405001);
  763. end;
  764. LOC_MEM,
  765. LOC_REFERENCE : ;
  766. LOC_CFPUREGISTER : begin
  767. emit_reg(A_FLD,S_NO,correct_fpuregister(t.register,fpuvaroffset));
  768. inc(fpuvaroffset);
  769. reset_reference(t.reference);
  770. gettempofsizereference(10,t.reference);
  771. floatstore(tfloatdef(def).typ,t.reference);
  772. end;
  773. else
  774. internalerror(333);
  775. end;
  776. t.loc:=LOC_MEM;
  777. end;
  778. procedure emit_to_reg16(var hr:tregister);
  779. begin
  780. { ranges are a little bit bug sensitive ! }
  781. case hr of
  782. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP,R_EBP:
  783. begin
  784. hr:=reg32toreg16(hr);
  785. end;
  786. R_AL,R_BL,R_CL,R_DL:
  787. begin
  788. hr:=reg8toreg16(hr);
  789. emit_const_reg(A_AND,S_W,$ff,hr);
  790. end;
  791. R_AH,R_BH,R_CH,R_DH:
  792. begin
  793. hr:=reg8toreg16(hr);
  794. emit_const_reg(A_AND,S_W,$ff00,hr);
  795. end;
  796. end;
  797. end;
  798. procedure emit_to_reg32(var hr:tregister);
  799. begin
  800. { ranges are a little bit bug sensitive ! }
  801. case hr of
  802. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP,R_BP:
  803. begin
  804. hr:=reg16toreg32(hr);
  805. emit_const_reg(A_AND,S_L,$ffff,hr);
  806. end;
  807. R_AL,R_BL,R_CL,R_DL:
  808. begin
  809. hr:=reg8toreg32(hr);
  810. emit_const_reg(A_AND,S_L,$ff,hr);
  811. end;
  812. R_AH,R_BH,R_CH,R_DH:
  813. begin
  814. hr:=reg8toreg32(hr);
  815. emit_const_reg(A_AND,S_L,$ff00,hr);
  816. end;
  817. end;
  818. end;
  819. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  820. var
  821. hr : preference;
  822. begin
  823. { if we load a 64 bit reference, we must be careful because }
  824. { we could overwrite the registers of the reference by }
  825. { accident }
  826. getexplicitregister32(R_EDI);
  827. if r.base=rl then
  828. begin
  829. emit_reg_reg(A_MOV,S_L,r.base,
  830. R_EDI);
  831. r.base:=R_EDI;
  832. end
  833. else if r.index=rl then
  834. begin
  835. emit_reg_reg(A_MOV,S_L,r.index,
  836. R_EDI);
  837. r.index:=R_EDI;
  838. end;
  839. emit_ref_reg(A_MOV,S_L,
  840. newreference(r),rl);
  841. hr:=newreference(r);
  842. inc(hr^.offset,4);
  843. emit_ref_reg(A_MOV,S_L,
  844. hr,rh);
  845. ungetregister32(R_EDI);
  846. end;
  847. {*****************************************************************************
  848. Emit String Functions
  849. *****************************************************************************}
  850. procedure incrcomintfref(t: tdef; const ref: treference);
  851. var
  852. pushedregs : tpushed;
  853. begin
  854. pushusedregisters(pushedregs,$ff);
  855. emit_ref(A_PUSH,S_L,newreference(ref));
  856. saveregvars($ff);
  857. if is_interfacecom(t) then
  858. emitcall('FPC_INTF_INCR_REF')
  859. else
  860. internalerror(1859);
  861. popusedregisters(pushedregs);
  862. end;
  863. procedure decrcomintfref(t: tdef; const ref: treference);
  864. var
  865. pushedregs : tpushed;
  866. begin
  867. pushusedregisters(pushedregs,$ff);
  868. emitpushreferenceaddr(ref);
  869. saveregvars($ff);
  870. if is_interfacecom(t) then
  871. begin
  872. emitcall('FPC_INTF_DECR_REF');
  873. end
  874. else internalerror(1859);
  875. popusedregisters(pushedregs);
  876. end;
  877. procedure copyshortstring(const dref,sref : treference;len : byte;
  878. loadref, del_sref: boolean);
  879. begin
  880. emitpushreferenceaddr(dref);
  881. { if it's deleted right before it's used, the optimizer can move }
  882. { the reg deallocations to the right places (JM) }
  883. if del_sref then
  884. del_reference(sref);
  885. if loadref then
  886. emit_push_mem(sref)
  887. else
  888. emitpushreferenceaddr(sref);
  889. push_int(len);
  890. emitcall('FPC_SHORTSTR_COPY');
  891. maybe_loadself;
  892. end;
  893. {$ifdef unused}
  894. procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
  895. begin
  896. emitpushreferenceaddr(dref);
  897. if loadref then
  898. emit_push_mem(sref)
  899. else
  900. emitpushreferenceaddr(sref);
  901. push_int(len);
  902. saveregvars($ff);
  903. emitcall('FPC_LONGSTR_COPY');
  904. maybe_loadself;
  905. end;
  906. {$endif unused}
  907. procedure incrstringref(t : tdef;const ref : treference);
  908. var
  909. pushedregs : tpushed;
  910. begin
  911. pushusedregisters(pushedregs,$ff);
  912. emitpushreferenceaddr(ref);
  913. saveregvars($ff);
  914. if is_ansistring(t) then
  915. begin
  916. emitcall('FPC_ANSISTR_INCR_REF');
  917. end
  918. else if is_widestring(t) then
  919. begin
  920. emitcall('FPC_WIDESTR_INCR_REF');
  921. end
  922. else internalerror(1859);
  923. popusedregisters(pushedregs);
  924. end;
  925. procedure decrstringref(t : tdef;const ref : treference);
  926. var
  927. pushedregs : tpushed;
  928. begin
  929. pushusedregisters(pushedregs,$ff);
  930. emitpushreferenceaddr(ref);
  931. saveregvars($ff);
  932. if is_ansistring(t) then
  933. begin
  934. emitcall('FPC_ANSISTR_DECR_REF');
  935. end
  936. else if is_widestring(t) then
  937. begin
  938. emitcall('FPC_WIDESTR_DECR_REF');
  939. end
  940. else internalerror(1859);
  941. popusedregisters(pushedregs);
  942. end;
  943. {*****************************************************************************
  944. Emit Push Functions
  945. *****************************************************************************}
  946. procedure push_int(l : longint);
  947. begin
  948. if (l = 0) and
  949. not(aktoptprocessor in [Class386, ClassP6]) and
  950. not(cs_littlesize in aktglobalswitches)
  951. Then
  952. begin
  953. getexplicitregister32(R_EDI);
  954. emit_reg_reg(A_XOR,S_L,R_EDI,R_EDI);
  955. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  956. ungetregister32(R_EDI);
  957. end
  958. else
  959. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,l));
  960. end;
  961. procedure emit_push_mem(const ref : treference);
  962. begin
  963. if ref.is_immediate then
  964. push_int(ref.offset)
  965. else
  966. begin
  967. if not(aktoptprocessor in [Class386, ClassP6]) and
  968. not(cs_littlesize in aktglobalswitches)
  969. then
  970. begin
  971. getexplicitregister32(R_EDI);
  972. emit_ref_reg(A_MOV,S_L,newreference(ref),R_EDI);
  973. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  974. ungetregister32(R_EDI);
  975. end
  976. else exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(ref)));
  977. end;
  978. end;
  979. procedure emitpushreferenceaddr(const ref : treference);
  980. var
  981. href : treference;
  982. begin
  983. { this will fail for references to other segments !!! }
  984. if ref.is_immediate then
  985. { is this right ? }
  986. begin
  987. { push_int(ref.offset)}
  988. gettempofsizereference(4,href);
  989. emit_const_ref(A_MOV,S_L,ref.offset,newreference(href));
  990. emitpushreferenceaddr(href);
  991. del_reference(href);
  992. end
  993. else
  994. begin
  995. if ref.segment<>R_NO then
  996. CGMessage(cg_e_cant_use_far_pointer_there);
  997. if (ref.base=R_NO) and (ref.index=R_NO) then
  998. exprasmList.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset))
  999. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  1000. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  1001. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.index))
  1002. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  1003. (ref.offset=0) and (ref.symbol=nil) then
  1004. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.base))
  1005. else
  1006. begin
  1007. getexplicitregister32(R_EDI);
  1008. emit_ref_reg(A_LEA,S_L,newreference(ref),R_EDI);
  1009. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1010. ungetregister32(R_EDI);
  1011. end;
  1012. end;
  1013. end;
  1014. {*****************************************************************************
  1015. Emit Float Functions
  1016. *****************************************************************************}
  1017. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  1018. begin
  1019. case t of
  1020. s32real : begin
  1021. op:=A_FLD;
  1022. s:=S_FS;
  1023. end;
  1024. s64real : begin
  1025. op:=A_FLD;
  1026. { ???? }
  1027. s:=S_FL;
  1028. end;
  1029. s80real : begin
  1030. op:=A_FLD;
  1031. s:=S_FX;
  1032. end;
  1033. s64comp : begin
  1034. op:=A_FILD;
  1035. s:=S_IQ;
  1036. end;
  1037. else internalerror(17);
  1038. end;
  1039. end;
  1040. procedure floatload(t : tfloattype;const ref : treference);
  1041. var
  1042. op : tasmop;
  1043. s : topsize;
  1044. begin
  1045. floatloadops(t,op,s);
  1046. exprasmList.concat(Taicpu.Op_ref(op,s,
  1047. newreference(ref)));
  1048. inc(fpuvaroffset);
  1049. end;
  1050. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  1051. begin
  1052. case t of
  1053. s32real : begin
  1054. op:=A_FSTP;
  1055. s:=S_FS;
  1056. end;
  1057. s64real : begin
  1058. op:=A_FSTP;
  1059. s:=S_FL;
  1060. end;
  1061. s80real : begin
  1062. op:=A_FSTP;
  1063. s:=S_FX;
  1064. end;
  1065. s64comp : begin
  1066. op:=A_FISTP;
  1067. s:=S_IQ;
  1068. end;
  1069. else
  1070. internalerror(17);
  1071. end;
  1072. end;
  1073. procedure floatstore(t : tfloattype;const ref : treference);
  1074. var
  1075. op : tasmop;
  1076. s : topsize;
  1077. begin
  1078. floatstoreops(t,op,s);
  1079. exprasmList.concat(Taicpu.Op_ref(op,s,
  1080. newreference(ref)));
  1081. dec(fpuvaroffset);
  1082. end;
  1083. {*****************************************************************************
  1084. Emit Functions
  1085. *****************************************************************************}
  1086. procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
  1087. const
  1088. isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
  1089. ishr : array[0..3] of byte=(2,0,1,0);
  1090. var
  1091. ecxpushed : boolean;
  1092. helpsize : longint;
  1093. i : byte;
  1094. reg8,reg32 : tregister;
  1095. swap : boolean;
  1096. procedure maybepushecx;
  1097. begin
  1098. if not(R_ECX in unused) then
  1099. begin
  1100. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1101. ecxpushed:=true;
  1102. end
  1103. else getexplicitregister32(R_ECX);
  1104. end;
  1105. begin
  1106. {$IfNDef regallocfix}
  1107. If delsource then
  1108. del_reference(source);
  1109. {$EndIf regallocfix}
  1110. if (not loadref) and
  1111. ((size<=8) or
  1112. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  1113. begin
  1114. helpsize:=size shr 2;
  1115. getexplicitregister32(R_EDI);
  1116. for i:=1 to helpsize do
  1117. begin
  1118. emit_ref_reg(A_MOV,S_L,newreference(source),R_EDI);
  1119. {$ifdef regallocfix}
  1120. If (size = 4) and delsource then
  1121. del_reference(source);
  1122. {$endif regallocfix}
  1123. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest)));
  1124. inc(source.offset,4);
  1125. inc(dest.offset,4);
  1126. dec(size,4);
  1127. end;
  1128. if size>1 then
  1129. begin
  1130. emit_ref_reg(A_MOV,S_W,newreference(source),R_DI);
  1131. {$ifdef regallocfix}
  1132. If (size = 2) and delsource then
  1133. del_reference(source);
  1134. {$endif regallocfix}
  1135. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_W,R_DI,newreference(dest)));
  1136. inc(source.offset,2);
  1137. inc(dest.offset,2);
  1138. dec(size,2);
  1139. end;
  1140. ungetregister32(R_EDI);
  1141. if size>0 then
  1142. begin
  1143. { and now look for an 8 bit register }
  1144. swap:=false;
  1145. if R_EAX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EAX))
  1146. else if R_EDX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EDX))
  1147. else if R_EBX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EBX))
  1148. else if R_ECX in unused then reg8:=reg32toreg8(getexplicitregister32(R_ECX))
  1149. else
  1150. begin
  1151. swap:=true;
  1152. { we need only to check 3 registers, because }
  1153. { one is always not index or base }
  1154. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  1155. begin
  1156. reg8:=R_AL;
  1157. reg32:=R_EAX;
  1158. end
  1159. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  1160. begin
  1161. reg8:=R_BL;
  1162. reg32:=R_EBX;
  1163. end
  1164. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  1165. begin
  1166. reg8:=R_CL;
  1167. reg32:=R_ECX;
  1168. end;
  1169. end;
  1170. if swap then
  1171. { was earlier XCHG, of course nonsense }
  1172. begin
  1173. getexplicitregister32(R_EDI);
  1174. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  1175. end;
  1176. emit_ref_reg(A_MOV,S_B,newreference(source),reg8);
  1177. {$ifdef regallocfix}
  1178. If delsource then
  1179. del_reference(source);
  1180. {$endif regallocfix}
  1181. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_B,reg8,newreference(dest)));
  1182. if swap then
  1183. begin
  1184. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  1185. ungetregister32(R_EDI);
  1186. end
  1187. else
  1188. ungetregister(reg8);
  1189. end;
  1190. end
  1191. else
  1192. begin
  1193. getexplicitregister32(R_EDI);
  1194. emit_ref_reg(A_LEA,S_L,newreference(dest),R_EDI);
  1195. {$ifdef regallocfix}
  1196. {is this ok?? (JM)}
  1197. del_reference(dest);
  1198. {$endif regallocfix}
  1199. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1200. if loadref then
  1201. emit_ref_reg(A_MOV,S_L,newreference(source),R_ESI)
  1202. else
  1203. begin
  1204. emit_ref_reg(A_LEA,S_L,newreference(source),R_ESI);
  1205. {$ifdef regallocfix}
  1206. if delsource then
  1207. del_reference(source);
  1208. {$endif regallocfix}
  1209. end;
  1210. exprasmList.concat(Taicpu.Op_none(A_CLD,S_NO));
  1211. ecxpushed:=false;
  1212. if cs_littlesize in aktglobalswitches then
  1213. begin
  1214. maybepushecx;
  1215. emit_const_reg(A_MOV,S_L,size,R_ECX);
  1216. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1217. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1218. end
  1219. else
  1220. begin
  1221. helpsize:=size shr 2;
  1222. size:=size and 3;
  1223. if helpsize>1 then
  1224. begin
  1225. maybepushecx;
  1226. emit_const_reg(A_MOV,S_L,helpsize,R_ECX);
  1227. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1228. end;
  1229. if helpsize>0 then
  1230. exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1231. if size>1 then
  1232. begin
  1233. dec(size,2);
  1234. exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1235. end;
  1236. if size=1 then
  1237. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1238. end;
  1239. ungetregister32(R_EDI);
  1240. exprasmList.concat(Tairegalloc.DeAlloc(R_ESI));
  1241. if ecxpushed then
  1242. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX))
  1243. else
  1244. ungetregister32(R_ECX);
  1245. { loading SELF-reference again }
  1246. maybe_loadself;
  1247. end;
  1248. if delsource then
  1249. ungetiftemp(source);
  1250. end;
  1251. procedure emitloadord2reg(const location:Tlocation;orddef:torddef;
  1252. destreg:Tregister;delloc:boolean);
  1253. {A lot smaller and less bug sensitive than the original unfolded loads.}
  1254. var tai:Taicpu;
  1255. r:Preference;
  1256. begin
  1257. tai := nil;
  1258. case location.loc of
  1259. LOC_REGISTER,LOC_CREGISTER:
  1260. begin
  1261. case orddef.typ of
  1262. u8bit:
  1263. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_BL,location.register,destreg);
  1264. s8bit:
  1265. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_BL,location.register,destreg);
  1266. u16bit:
  1267. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_WL,location.register,destreg);
  1268. s16bit:
  1269. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_WL,location.register,destreg);
  1270. u32bit,s32bit:
  1271. if location.register <> destreg then
  1272. tai:=Taicpu.Op_reg_reg(A_MOV,S_L,location.register,destreg);
  1273. end;
  1274. if delloc then
  1275. ungetregister(location.register);
  1276. end;
  1277. LOC_MEM,
  1278. LOC_REFERENCE:
  1279. begin
  1280. if location.reference.is_immediate then
  1281. tai:=Taicpu.Op_const_reg(A_MOV,S_L,location.reference.offset,destreg)
  1282. else
  1283. begin
  1284. r:=newreference(location.reference);
  1285. case orddef.typ of
  1286. u8bit:
  1287. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_BL,r,destreg);
  1288. s8bit:
  1289. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_BL,r,destreg);
  1290. u16bit:
  1291. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_WL,r,destreg);
  1292. s16bit:
  1293. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_WL,r,destreg);
  1294. u32bit:
  1295. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1296. s32bit:
  1297. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1298. end;
  1299. end;
  1300. if delloc then
  1301. del_reference(location.reference);
  1302. end
  1303. else
  1304. internalerror(6);
  1305. end;
  1306. if assigned(tai) then
  1307. exprasmList.concat(tai);
  1308. end;
  1309. { if necessary ESI is reloaded after a call}
  1310. procedure maybe_loadself;
  1311. var
  1312. hp : preference;
  1313. p : pprocinfo;
  1314. i : longint;
  1315. begin
  1316. if assigned(procinfo^._class) then
  1317. begin
  1318. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1319. if lexlevel>normal_function_level then
  1320. begin
  1321. new(hp);
  1322. reset_reference(hp^);
  1323. hp^.offset:=procinfo^.framepointer_offset;
  1324. hp^.base:=procinfo^.framepointer;
  1325. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1326. p:=procinfo^.parent;
  1327. for i:=3 to lexlevel-1 do
  1328. begin
  1329. new(hp);
  1330. reset_reference(hp^);
  1331. hp^.offset:=p^.framepointer_offset;
  1332. hp^.base:=R_ESI;
  1333. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1334. p:=p^.parent;
  1335. end;
  1336. new(hp);
  1337. reset_reference(hp^);
  1338. hp^.offset:=p^.selfpointer_offset;
  1339. hp^.base:=R_ESI;
  1340. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1341. end
  1342. else
  1343. begin
  1344. new(hp);
  1345. reset_reference(hp^);
  1346. hp^.offset:=procinfo^.selfpointer_offset;
  1347. hp^.base:=procinfo^.framepointer;
  1348. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1349. end;
  1350. end;
  1351. end;
  1352. {*****************************************************************************
  1353. Entry/Exit Code Functions
  1354. *****************************************************************************}
  1355. procedure genprofilecode;
  1356. var
  1357. pl : tasmlabel;
  1358. begin
  1359. if (po_assembler in aktprocsym.definition.procoptions) then
  1360. exit;
  1361. case target_info.target of
  1362. target_i386_freebsd,
  1363. target_i386_linux:
  1364. begin
  1365. getaddrlabel(pl);
  1366. emitinsertcall('mcount');
  1367. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  1368. exprasmList.insert(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX));
  1369. exprasmList.insert(Tai_section.Create(sec_code));
  1370. exprasmList.insert(Tai_const.Create_32bit(0));
  1371. exprasmList.insert(Tai_label.Create(pl));
  1372. exprasmList.insert(Tai_align.Create(4));
  1373. exprasmList.insert(Tai_section.Create(sec_data));
  1374. end;
  1375. target_i386_go32v2:
  1376. begin
  1377. emitinsertcall('MCOUNT');
  1378. end;
  1379. end;
  1380. end;
  1381. procedure generate_interrupt_stackframe_entry;
  1382. begin
  1383. { save the registers of an interrupt procedure }
  1384. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EAX));
  1385. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1386. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1387. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDX));
  1388. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1389. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1390. { .... also the segment registers }
  1391. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_DS));
  1392. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_ES));
  1393. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_FS));
  1394. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_GS));
  1395. end;
  1396. procedure generate_interrupt_stackframe_exit;
  1397. begin
  1398. { restore the registers of an interrupt procedure }
  1399. { this was all with entrycode instead of exitcode !!}
  1400. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EAX));
  1401. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  1402. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX));
  1403. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDX));
  1404. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  1405. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  1406. { .... also the segment registers }
  1407. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_DS));
  1408. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_ES));
  1409. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_FS));
  1410. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_GS));
  1411. { this restores the flags }
  1412. procinfo^.aktexitcode.concat(Taicpu.Op_none(A_IRET,S_NO));
  1413. end;
  1414. { generates the code for threadvar initialisation }
  1415. procedure initialize_threadvar(p : tnamedindexitem);
  1416. var
  1417. hr : treference;
  1418. begin
  1419. if (tsym(p).typ=varsym) and
  1420. (vo_is_thread_var in tvarsym(p).varoptions) then
  1421. begin
  1422. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,tvarsym(p).getsize));
  1423. reset_reference(hr);
  1424. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1425. emitpushreferenceaddr(hr);
  1426. saveregvars($ff);
  1427. emitcall('FPC_INIT_THREADVAR');
  1428. end;
  1429. end;
  1430. { initilizes data of type t }
  1431. { if is_already_ref is true then the routines assumes }
  1432. { that r points to the data to initialize }
  1433. procedure initialize(t : tdef;const ref : treference;is_already_ref : boolean);
  1434. var
  1435. hr : treference;
  1436. begin
  1437. if is_ansistring(t) or
  1438. is_widestring(t) or
  1439. is_interfacecom(t) then
  1440. begin
  1441. emit_const_ref(A_MOV,S_L,0,
  1442. newreference(ref));
  1443. end
  1444. else
  1445. begin
  1446. reset_reference(hr);
  1447. hr.symbol:=tstoreddef(t).get_inittable_label;
  1448. emitpushreferenceaddr(hr);
  1449. if is_already_ref then
  1450. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1451. newreference(ref)))
  1452. else
  1453. emitpushreferenceaddr(ref);
  1454. emitcall('FPC_INITIALIZE');
  1455. end;
  1456. end;
  1457. { finalizes data of type t }
  1458. { if is_already_ref is true then the routines assumes }
  1459. { that r points to the data to finalizes }
  1460. procedure finalize(t : tdef;const ref : treference;is_already_ref : boolean);
  1461. var
  1462. r : treference;
  1463. begin
  1464. if is_ansistring(t) or
  1465. is_widestring(t) then
  1466. begin
  1467. decrstringref(t,ref);
  1468. end
  1469. else if is_interfacecom(t) then
  1470. begin
  1471. decrcomintfref(t,ref);
  1472. end
  1473. else
  1474. begin
  1475. reset_reference(r);
  1476. r.symbol:=tstoreddef(t).get_inittable_label;
  1477. emitpushreferenceaddr(r);
  1478. if is_already_ref then
  1479. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1480. newreference(ref)))
  1481. else
  1482. emitpushreferenceaddr(ref);
  1483. emitcall('FPC_FINALIZE');
  1484. end;
  1485. end;
  1486. { generates the code for initialisation of local data }
  1487. procedure initialize_data(p : tnamedindexitem);
  1488. var
  1489. hr : treference;
  1490. begin
  1491. if (tsym(p).typ=varsym) and
  1492. assigned(tvarsym(p).vartype.def) and
  1493. not(is_class(tvarsym(p).vartype.def)) and
  1494. tvarsym(p).vartype.def.needs_inittable then
  1495. begin
  1496. if assigned(procinfo) then
  1497. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1498. reset_reference(hr);
  1499. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  1500. begin
  1501. hr.base:=procinfo^.framepointer;
  1502. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1503. end
  1504. else
  1505. begin
  1506. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1507. end;
  1508. initialize(tvarsym(p).vartype.def,hr,false);
  1509. end;
  1510. end;
  1511. { generates the code for incrementing the reference count of parameters and
  1512. initialize out parameters }
  1513. procedure init_paras(p : tnamedindexitem);
  1514. var
  1515. hrv : treference;
  1516. hr: treference;
  1517. begin
  1518. if (tsym(p).typ=varsym) and
  1519. not is_class(tvarsym(p).vartype.def) and
  1520. tvarsym(p).vartype.def.needs_inittable then
  1521. begin
  1522. if (tvarsym(p).varspez=vs_value) then
  1523. begin
  1524. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1525. reset_reference(hrv);
  1526. hrv.base:=procinfo^.framepointer;
  1527. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1528. if is_ansistring(tvarsym(p).vartype.def) or
  1529. is_widestring(tvarsym(p).vartype.def) then
  1530. begin
  1531. incrstringref(tvarsym(p).vartype.def,hrv)
  1532. end
  1533. else if is_interfacecom(tvarsym(p).vartype.def) then
  1534. begin
  1535. incrcomintfref(tvarsym(p).vartype.def,hrv)
  1536. end
  1537. else
  1538. begin
  1539. reset_reference(hr);
  1540. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_inittable_label;
  1541. emitpushreferenceaddr(hr);
  1542. emitpushreferenceaddr(hrv);
  1543. emitcall('FPC_ADDREF');
  1544. end;
  1545. end
  1546. else if (tvarsym(p).varspez=vs_out) then
  1547. begin
  1548. reset_reference(hrv);
  1549. hrv.base:=procinfo^.framepointer;
  1550. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1551. getexplicitregister32(R_EDI);
  1552. exprasmList.concat(Taicpu.Op_ref_reg(A_MOV,S_L,newreference(hrv),R_EDI));
  1553. reset_reference(hr);
  1554. hr.base:=R_EDI;
  1555. initialize(tvarsym(p).vartype.def,hr,false);
  1556. end;
  1557. end;
  1558. end;
  1559. { generates the code for decrementing the reference count of parameters }
  1560. procedure final_paras(p : tnamedindexitem);
  1561. var
  1562. hrv : treference;
  1563. hr: treference;
  1564. begin
  1565. if (tsym(p).typ=varsym) and
  1566. not is_class(tvarsym(p).vartype.def) and
  1567. tvarsym(p).vartype.def.needs_inittable then
  1568. begin
  1569. if (tvarsym(p).varspez=vs_value) then
  1570. begin
  1571. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1572. reset_reference(hrv);
  1573. hrv.base:=procinfo^.framepointer;
  1574. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1575. if is_ansistring(tvarsym(p).vartype.def) or
  1576. is_widestring(tvarsym(p).vartype.def) then
  1577. begin
  1578. decrstringref(tvarsym(p).vartype.def,hrv)
  1579. end
  1580. else if is_interfacecom(tvarsym(p).vartype.def) then
  1581. begin
  1582. decrcomintfref(tvarsym(p).vartype.def,hrv)
  1583. end
  1584. else
  1585. begin
  1586. reset_reference(hr);
  1587. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_inittable_label;
  1588. emitpushreferenceaddr(hr);
  1589. emitpushreferenceaddr(hrv);
  1590. emitcall('FPC_DECREF');
  1591. end;
  1592. end;
  1593. end;
  1594. end;
  1595. { generates the code for finalisation of local data }
  1596. procedure finalize_data(p : tnamedindexitem);
  1597. var
  1598. hr : treference;
  1599. begin
  1600. if (tsym(p).typ=varsym) and
  1601. assigned(tvarsym(p).vartype.def) and
  1602. not(is_class(tvarsym(p).vartype.def)) and
  1603. tvarsym(p).vartype.def.needs_inittable then
  1604. begin
  1605. if assigned(procinfo) then
  1606. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1607. reset_reference(hr);
  1608. case tsym(p).owner.symtabletype of
  1609. localsymtable,inlinelocalsymtable:
  1610. begin
  1611. hr.base:=procinfo^.framepointer;
  1612. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1613. end;
  1614. else
  1615. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1616. end;
  1617. finalize(tvarsym(p).vartype.def,hr,false);
  1618. end;
  1619. end;
  1620. { generates the code to make local copies of the value parameters }
  1621. procedure copyvalueparas(p : tnamedindexitem);
  1622. var
  1623. href1,href2 : treference;
  1624. r : preference;
  1625. power,len : longint;
  1626. opsize : topsize;
  1627. {$ifndef NOTARGETWIN32}
  1628. again,ok : tasmlabel;
  1629. {$endif}
  1630. begin
  1631. if (tsym(p).typ=varsym) and
  1632. (tvarsym(p).varspez=vs_value) and
  1633. (push_addr_param(tvarsym(p).vartype.def)) then
  1634. begin
  1635. if is_open_array(tvarsym(p).vartype.def) or
  1636. is_array_of_const(tvarsym(p).vartype.def) then
  1637. begin
  1638. { get stack space }
  1639. new(r);
  1640. reset_reference(r^);
  1641. r^.base:=procinfo^.framepointer;
  1642. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1643. getexplicitregister32(R_EDI);
  1644. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1645. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1646. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1647. begin
  1648. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1649. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1650. else
  1651. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1652. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1653. end;
  1654. {$ifndef NOTARGETWIN32}
  1655. { windows guards only a few pages for stack growing, }
  1656. { so we have to access every page first }
  1657. if target_info.target=target_i386_win32 then
  1658. begin
  1659. getlabel(again);
  1660. getlabel(ok);
  1661. emitlab(again);
  1662. exprasmList.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI));
  1663. emitjmp(C_C,ok);
  1664. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1665. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1666. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI));
  1667. emitjmp(C_None,again);
  1668. emitlab(ok);
  1669. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1670. ungetregister32(R_EDI);
  1671. { now reload EDI }
  1672. new(r);
  1673. reset_reference(r^);
  1674. r^.base:=procinfo^.framepointer;
  1675. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1676. getexplicitregister32(R_EDI);
  1677. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1678. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1679. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1680. begin
  1681. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1682. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1683. else
  1684. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1685. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1686. end;
  1687. end
  1688. else
  1689. {$endif NOTARGETWIN32}
  1690. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1691. { load destination }
  1692. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  1693. { don't destroy the registers! }
  1694. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ECX));
  1695. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ESI));
  1696. { load count }
  1697. new(r);
  1698. reset_reference(r^);
  1699. r^.base:=procinfo^.framepointer;
  1700. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1701. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ECX));
  1702. { load source }
  1703. new(r);
  1704. reset_reference(r^);
  1705. r^.base:=procinfo^.framepointer;
  1706. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1707. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ESI));
  1708. { scheduled .... }
  1709. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_ECX));
  1710. { calculate size }
  1711. len:=tarraydef(tvarsym(p).vartype.def).elesize;
  1712. opsize:=S_B;
  1713. if (len and 3)=0 then
  1714. begin
  1715. opsize:=S_L;
  1716. len:=len shr 2;
  1717. end
  1718. else
  1719. if (len and 1)=0 then
  1720. begin
  1721. opsize:=S_W;
  1722. len:=len shr 1;
  1723. end;
  1724. if ispowerof2(len, power) then
  1725. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_ECX))
  1726. else
  1727. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,R_ECX));
  1728. exprasmList.concat(Taicpu.op_none(A_REP,S_NO));
  1729. case opsize of
  1730. S_B : exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1731. S_W : exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1732. S_L : exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1733. end;
  1734. ungetregister32(R_EDI);
  1735. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ESI));
  1736. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ECX));
  1737. { patch the new address }
  1738. new(r);
  1739. reset_reference(r^);
  1740. r^.base:=procinfo^.framepointer;
  1741. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1742. exprasmList.concat(Taicpu.op_reg_ref(A_MOV,S_L,R_ESP,r));
  1743. end
  1744. else
  1745. if is_shortstring(tvarsym(p).vartype.def) then
  1746. begin
  1747. reset_reference(href1);
  1748. href1.base:=procinfo^.framepointer;
  1749. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1750. reset_reference(href2);
  1751. href2.base:=procinfo^.framepointer;
  1752. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1753. copyshortstring(href2,href1,tstringdef(tvarsym(p).vartype.def).len,true,false);
  1754. end
  1755. else
  1756. begin
  1757. reset_reference(href1);
  1758. href1.base:=procinfo^.framepointer;
  1759. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1760. reset_reference(href2);
  1761. href2.base:=procinfo^.framepointer;
  1762. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1763. concatcopy(href1,href2,tvarsym(p).vartype.def.size,true,true);
  1764. end;
  1765. end;
  1766. end;
  1767. procedure inittempvariables;
  1768. var
  1769. hp : ptemprecord;
  1770. r : preference;
  1771. begin
  1772. hp:=templist;
  1773. while assigned(hp) do
  1774. begin
  1775. if hp^.temptype in [tt_ansistring,tt_freeansistring,tt_interfacecom] then
  1776. begin
  1777. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1778. new(r);
  1779. reset_reference(r^);
  1780. r^.base:=procinfo^.framepointer;
  1781. r^.offset:=hp^.pos;
  1782. emit_const_ref(A_MOV,S_L,0,r);
  1783. end;
  1784. hp:=hp^.next;
  1785. end;
  1786. end;
  1787. procedure finalizetempvariables;
  1788. var
  1789. hp : ptemprecord;
  1790. hr : treference;
  1791. begin
  1792. hp:=templist;
  1793. while assigned(hp) do
  1794. begin
  1795. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  1796. begin
  1797. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1798. reset_reference(hr);
  1799. hr.base:=procinfo^.framepointer;
  1800. hr.offset:=hp^.pos;
  1801. emitpushreferenceaddr(hr);
  1802. emitcall('FPC_ANSISTR_DECR_REF');
  1803. end
  1804. else if hp^.temptype=tt_interfacecom then
  1805. begin
  1806. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1807. reset_reference(hr);
  1808. hr.base:=procinfo^.framepointer;
  1809. hr.offset:=hp^.pos;
  1810. emitpushreferenceaddr(hr);
  1811. emitcall('FPC_INTF_DECR_REF');
  1812. end;
  1813. hp:=hp^.next;
  1814. end;
  1815. end;
  1816. {$ifdef dummy}
  1817. var
  1818. ls : longint;
  1819. procedure largest_size(p : tnamedindexitem);
  1820. begin
  1821. if (tsym(p).typ=varsym) and
  1822. (tvarsym(p).getvaluesize>ls) then
  1823. ls:=tvarsym(p).getvaluesize;
  1824. end;
  1825. {$endif dummy}
  1826. procedure alignstack(alist : TAAsmoutput);
  1827. begin
  1828. {$ifdef dummy}
  1829. if (cs_optimize in aktglobalswitches) and
  1830. (aktoptprocessor in [classp5,classp6]) then
  1831. begin
  1832. ls:=0;
  1833. aktprocsym.definition.localst.foreach({$ifndef TP}@{$endif}largest_size);
  1834. if ls>=8 then
  1835. aList.insert(Taicpu.Op_const_reg(A_AND,S_L,-8,R_ESP));
  1836. end;
  1837. {$endif dummy}
  1838. end;
  1839. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  1840. stackframe:longint;
  1841. var parasize:longint;var nostackframe:boolean;
  1842. inlined : boolean);
  1843. {
  1844. Generates the entry code for a procedure
  1845. }
  1846. var
  1847. hs : string;
  1848. {$ifdef GDB}
  1849. stab_function_name : tai_stab_function_name;
  1850. {$endif GDB}
  1851. hr : preference;
  1852. p : tsymtable;
  1853. r : treference;
  1854. oldlist,
  1855. oldexprasmlist : TAAsmoutput;
  1856. again : tasmlabel;
  1857. i : longint;
  1858. tempbuf,tempaddr : treference;
  1859. begin
  1860. oldexprasmlist:=exprasmlist;
  1861. exprasmlist:=alist;
  1862. if (not inlined) and (aktprocsym.definition.proctypeoption=potype_proginit) then
  1863. begin
  1864. emitinsertcall('FPC_INITIALIZEUNITS');
  1865. oldlist:=exprasmlist;
  1866. exprasmlist:=TAAsmoutput.Create;
  1867. p:=symtablestack;
  1868. while assigned(p) do
  1869. begin
  1870. p.foreach_static({$ifndef TP}@{$endif}initialize_threadvar);
  1871. p:=p.next;
  1872. end;
  1873. oldList.insertlist(exprasmlist);
  1874. exprasmlist.free;
  1875. exprasmlist:=oldlist;
  1876. end;
  1877. {$ifdef GDB}
  1878. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  1879. exprasmList.insert(Tai_force_line.Create);
  1880. {$endif GDB}
  1881. { a constructor needs a help procedure }
  1882. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  1883. begin
  1884. if is_class(procinfo^._class) then
  1885. begin
  1886. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1887. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1888. emitinsertcall('FPC_NEW_CLASS');
  1889. end
  1890. else if is_object(procinfo^._class) then
  1891. begin
  1892. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1893. emitinsertcall('FPC_HELP_CONSTRUCTOR');
  1894. getexplicitregister32(R_EDI);
  1895. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  1896. end
  1897. else
  1898. Internalerror(200006161);
  1899. end;
  1900. { don't load ESI, does the caller }
  1901. { we must do it for local function }
  1902. { that can be called from a foreach_static }
  1903. { of another object than self !! PM }
  1904. if assigned(procinfo^._class) and { !!!!! shouldn't we load ESI always? }
  1905. (lexlevel>normal_function_level) then
  1906. maybe_loadself;
  1907. { When message method contains self as a parameter,
  1908. we must load it into ESI }
  1909. If (po_containsself in aktprocsym.definition.procoptions) then
  1910. begin
  1911. new(hr);
  1912. reset_reference(hr^);
  1913. hr^.offset:=procinfo^.selfpointer_offset;
  1914. hr^.base:=procinfo^.framepointer;
  1915. exprasmList.insert(Taicpu.Op_ref_reg(A_MOV,S_L,hr,R_ESI));
  1916. exprasmList.insert(Tairegalloc.Alloc(R_ESI));
  1917. end;
  1918. { should we save edi,esi,ebx like C ? }
  1919. if (po_savestdregs in aktprocsym.definition.procoptions) then
  1920. begin
  1921. if (aktprocsym.definition.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  1922. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1923. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1924. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1925. end;
  1926. { for the save all registers we can simply use a pusha,popa which
  1927. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1928. if (po_saveregisters in aktprocsym.definition.procoptions) then
  1929. begin
  1930. exprasmList.insert(Taicpu.Op_none(A_PUSHA,S_L));
  1931. end;
  1932. { omit stack frame ? }
  1933. if (not inlined) then
  1934. if (procinfo^.framepointer=stack_pointer) then
  1935. begin
  1936. CGMessage(cg_d_stackframe_omited);
  1937. nostackframe:=true;
  1938. if (aktprocsym.definition.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1939. parasize:=0
  1940. else
  1941. parasize:=aktprocsym.definition.parast.datasize+procinfo^.para_offset-4;
  1942. if stackframe<>0 then
  1943. exprasmList.insert(Taicpu.op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  1944. end
  1945. else
  1946. begin
  1947. alignstack(alist);
  1948. if (aktprocsym.definition.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1949. parasize:=0
  1950. else
  1951. parasize:=aktprocsym.definition.parast.datasize+procinfo^.para_offset-8;
  1952. nostackframe:=false;
  1953. if stackframe<>0 then
  1954. begin
  1955. {$ifdef unused}
  1956. if (cs_littlesize in aktglobalswitches) and (stackframe<=65535) then
  1957. begin
  1958. if (cs_check_stack in aktlocalswitches) and
  1959. not(target_info.target in [target_386_freebsd,
  1960. target_i386_linux,target_i386_win32]) then
  1961. begin
  1962. emitinsertcall('FPC_STACKCHECK');
  1963. exprasmList.insert(Taicpu.Op_const(A_PUSH,S_L,stackframe));
  1964. end;
  1965. if cs_profile in aktmoduleswitches then
  1966. genprofilecode;
  1967. { %edi is already saved when pocdecl is used
  1968. if ((target_info.target=target_linux) or (target_info.target=target_freebsd)) and
  1969. ((aktprocsym.definition.options and poexports)<>0) then
  1970. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI)); }
  1971. { ATTENTION:
  1972. never use ENTER in linux !!! (or freebsd MvdV)
  1973. the stack page fault does not support it PM }
  1974. exprasmList.insert(Taicpu.Op_const_const(A_ENTER,S_NO,stackframe,0)))
  1975. end
  1976. else
  1977. {$endif unused}
  1978. begin
  1979. {$ifndef NOTARGETWIN32}
  1980. { windows guards only a few pages for stack growing, }
  1981. { so we have to access every page first }
  1982. if (target_info.target=target_i386_win32) and
  1983. (stackframe>=winstackpagesize) then
  1984. begin
  1985. if stackframe div winstackpagesize<=5 then
  1986. begin
  1987. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe-4,R_ESP));
  1988. for i:=1 to stackframe div winstackpagesize do
  1989. begin
  1990. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  1991. exprasmList.concat(Taicpu.op_const_ref(A_MOV,S_L,0,hr));
  1992. end;
  1993. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1994. end
  1995. else
  1996. begin
  1997. getlabel(again);
  1998. getexplicitregister32(R_EDI);
  1999. exprasmList.concat(Taicpu.op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI));
  2000. emitlab(again);
  2001. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  2002. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2003. exprasmList.concat(Taicpu.op_reg(A_DEC,S_L,R_EDI));
  2004. emitjmp(C_NZ,again);
  2005. ungetregister32(R_EDI);
  2006. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP));
  2007. end
  2008. end
  2009. else
  2010. {$endif NOTARGETWIN32}
  2011. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  2012. if (cs_check_stack in aktlocalswitches) and
  2013. not(target_info.target in [target_i386_freebsd,
  2014. target_i386_linux,target_i386_win32]) then
  2015. begin
  2016. emitinsertcall('FPC_STACKCHECK');
  2017. exprasmList.insert(Taicpu.Op_const(A_PUSH,S_L,stackframe));
  2018. end;
  2019. if cs_profile in aktmoduleswitches then
  2020. genprofilecode;
  2021. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2022. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2023. end;
  2024. end { endif stackframe <> 0 }
  2025. else
  2026. begin
  2027. if cs_profile in aktmoduleswitches then
  2028. genprofilecode;
  2029. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2030. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2031. end;
  2032. end;
  2033. if (po_interrupt in aktprocsym.definition.procoptions) then
  2034. generate_interrupt_stackframe_entry;
  2035. { initialize return value }
  2036. if (not is_void(procinfo^.returntype.def)) and
  2037. (procinfo^.returntype.def.needs_inittable) then
  2038. begin
  2039. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2040. reset_reference(r);
  2041. r.offset:=procinfo^.return_offset;
  2042. r.base:=procinfo^.framepointer;
  2043. initialize(procinfo^.returntype.def,r,ret_in_param(procinfo^.returntype.def));
  2044. end;
  2045. { initialisize local data like ansistrings }
  2046. case aktprocsym.definition.proctypeoption of
  2047. potype_unitinit:
  2048. begin
  2049. { using current_module.globalsymtable is hopefully }
  2050. { more robust than symtablestack and symtablestack.next }
  2051. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2052. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2053. end;
  2054. { units have seperate code for initilization and finalization }
  2055. potype_unitfinalize: ;
  2056. else
  2057. aktprocsym.definition.localst.foreach_static({$ifndef TP}@{$endif}initialize_data);
  2058. end;
  2059. { initialisizes temp. ansi/wide string data }
  2060. inittempvariables;
  2061. { generate copies of call by value parameters }
  2062. if not(po_assembler in aktprocsym.definition.procoptions) and
  2063. (([pocall_cdecl,pocall_cppdecl]*aktprocsym.definition.proccalloptions)=[]) then
  2064. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas);
  2065. if assigned( aktprocsym.definition.parast) then
  2066. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}init_paras);
  2067. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  2068. if not inlined and
  2069. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2070. { but it's useless in init/final code of units }
  2071. not(aktprocsym.definition.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2072. begin
  2073. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  2074. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,36,R_ESP));
  2075. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  2076. reset_reference(tempaddr);
  2077. tempaddr.base:=R_EDI;
  2078. emitpushreferenceaddr(tempaddr);
  2079. reset_reference(tempbuf);
  2080. tempbuf.base:=R_EDI;
  2081. tempbuf.offset:=12;
  2082. emitpushreferenceaddr(tempbuf);
  2083. { Type of stack-frame must be pushed}
  2084. exprasmList.concat(Taicpu.op_const(A_PUSH,S_L,1));
  2085. emitcall('FPC_PUSHEXCEPTADDR');
  2086. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2087. emitcall('FPC_SETJMP');
  2088. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2089. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2090. emitjmp(C_NE,aktexitlabel);
  2091. { probably we've to reload self here }
  2092. maybe_loadself;
  2093. end;
  2094. if not inlined then
  2095. begin
  2096. if (cs_profile in aktmoduleswitches) or
  2097. (aktprocsym.definition.owner.symtabletype=globalsymtable) or
  2098. (assigned(procinfo^._class) and (procinfo^._class.owner.symtabletype=globalsymtable)) then
  2099. make_global:=true;
  2100. hs:=aktprocsym.definition.aliasnames.getfirst;
  2101. {$ifdef GDB}
  2102. if (cs_debuginfo in aktmoduleswitches) and target_info.use_function_relative_addresses then
  2103. stab_function_name := Tai_stab_function_name.Create(strpnew(hs));
  2104. {$EndIf GDB}
  2105. while hs<>'' do
  2106. begin
  2107. if make_global then
  2108. exprasmList.insert(Tai_symbol.Createname_global(hs,0))
  2109. else
  2110. exprasmList.insert(Tai_symbol.Createname(hs,0));
  2111. {$ifdef GDB}
  2112. if (cs_debuginfo in aktmoduleswitches) and
  2113. target_info.use_function_relative_addresses then
  2114. exprasmList.insert(Tai_stab_function_name.Create(strpnew(hs)));
  2115. {$endif GDB}
  2116. hs:=aktprocsym.definition.aliasnames.getfirst;
  2117. end;
  2118. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  2119. aktprocsym.is_global := True;
  2120. {$ifdef GDB}
  2121. if (cs_debuginfo in aktmoduleswitches) then
  2122. begin
  2123. if target_info.use_function_relative_addresses then
  2124. exprasmList.insert(stab_function_name);
  2125. exprasmList.insert(Tai_stabs.Create(aktprocsym.stabstring));
  2126. aktprocsym.isstabwritten:=true;
  2127. end;
  2128. {$endif GDB}
  2129. { Align, gprof uses 16 byte granularity }
  2130. if (cs_profile in aktmoduleswitches) then
  2131. exprasmList.insert(Tai_align.Create_op(16,$90))
  2132. else
  2133. if not(cs_littlesize in aktglobalswitches) then
  2134. exprasmList.insert(Tai_align.Create(16));
  2135. end;
  2136. if inlined then
  2137. load_regvars(exprasmlist,nil);
  2138. exprasmlist:=oldexprasmlist;
  2139. end;
  2140. procedure handle_return_value(inlined : boolean;var uses_eax,uses_edx : boolean);
  2141. var
  2142. hr : preference;
  2143. op : Tasmop;
  2144. s : Topsize;
  2145. begin
  2146. if not is_void(procinfo^.returntype.def) then
  2147. begin
  2148. {if ((procinfo^.flags and pi_operator)<>0) and
  2149. assigned(otsym) then
  2150. procinfo^.funcret_is_valid:=
  2151. procinfo^.funcret_is_valid or (otsym.refs>0);}
  2152. if (procinfo^.funcret_state<>vs_assigned) and not inlined { and
  2153. ((procinfo^.flags and pi_uses_asm)=0)} then
  2154. CGMessage(sym_w_function_result_not_set);
  2155. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2156. if (procinfo^.returntype.def.deftype in [orddef,enumdef]) then
  2157. begin
  2158. uses_eax:=true;
  2159. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2160. case procinfo^.returntype.def.size of
  2161. 8:
  2162. begin
  2163. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2164. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2165. exprasmList.concat(Tairegalloc.Alloc(R_EDX));
  2166. emit_ref_reg(A_MOV,S_L,hr,R_EDX);
  2167. uses_edx:=true;
  2168. end;
  2169. 4:
  2170. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2171. 2:
  2172. emit_ref_reg(A_MOV,S_W,hr,R_AX);
  2173. 1:
  2174. emit_ref_reg(A_MOV,S_B,hr,R_AL);
  2175. end;
  2176. end
  2177. else
  2178. if ret_in_acc(procinfo^.returntype.def) then
  2179. begin
  2180. uses_eax:=true;
  2181. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2182. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2183. end
  2184. else
  2185. if (procinfo^.returntype.def.deftype=floatdef) then
  2186. begin
  2187. floatloadops(tfloatdef(procinfo^.returntype.def).typ,op,s);
  2188. exprasmList.concat(Taicpu.Op_ref(op,s,hr));
  2189. end
  2190. else
  2191. dispose(hr);
  2192. end
  2193. end;
  2194. procedure handle_fast_exit_return_value;
  2195. var
  2196. hr : preference;
  2197. op : Tasmop;
  2198. s : Topsize;
  2199. begin
  2200. if not is_void(procinfo^.returntype.def) then
  2201. begin
  2202. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2203. if (procinfo^.returntype.def.deftype in [orddef,enumdef]) then
  2204. begin
  2205. case procinfo^.returntype.def.size of
  2206. 8:
  2207. begin
  2208. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2209. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2210. emit_reg_ref(A_MOV,S_L,R_EDX,hr);
  2211. end;
  2212. 4:
  2213. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2214. 2:
  2215. emit_reg_ref(A_MOV,S_W,R_AX,hr);
  2216. 1:
  2217. emit_reg_ref(A_MOV,S_B,R_AL,hr);
  2218. end;
  2219. end
  2220. else
  2221. if ret_in_acc(procinfo^.returntype.def) then
  2222. begin
  2223. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2224. end
  2225. else
  2226. if (procinfo^.returntype.def.deftype=floatdef) then
  2227. begin
  2228. floatstoreops(tfloatdef(procinfo^.returntype.def).typ,op,s);
  2229. exprasmlist.concat(taicpu.op_ref(op,s,hr));
  2230. end
  2231. else
  2232. dispose(hr);
  2233. end
  2234. end;
  2235. procedure genexitcode(alist : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  2236. var
  2237. {$ifdef GDB}
  2238. mangled_length : longint;
  2239. p : pchar;
  2240. st : string[2];
  2241. {$endif GDB}
  2242. stabsendlabel,nofinal,okexitlabel,
  2243. noreraiselabel,nodestroycall : tasmlabel;
  2244. hr : treference;
  2245. uses_eax,uses_edx,uses_esi : boolean;
  2246. oldexprasmlist : TAAsmoutput;
  2247. ai : taicpu;
  2248. pd : tprocdef;
  2249. begin
  2250. oldexprasmlist:=exprasmlist;
  2251. exprasmlist:=alist;
  2252. if aktexit2label.is_used and
  2253. ((procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0) then
  2254. begin
  2255. exprasmlist.concat(taicpu.op_sym(A_JMP,S_NO,aktexitlabel));
  2256. exprasmlist.concat(tai_label.create(aktexit2label));
  2257. handle_fast_exit_return_value;
  2258. end;
  2259. if aktexitlabel.is_used then
  2260. exprasmList.concat(Tai_label.Create(aktexitlabel));
  2261. { call the destructor help procedure }
  2262. if (aktprocsym.definition.proctypeoption=potype_destructor) and
  2263. assigned(procinfo^._class) then
  2264. begin
  2265. if is_class(procinfo^._class) then
  2266. begin
  2267. emitinsertcall('FPC_DISPOSE_CLASS');
  2268. end
  2269. else if is_object(procinfo^._class) then
  2270. begin
  2271. emitinsertcall('FPC_HELP_DESTRUCTOR');
  2272. getexplicitregister32(R_EDI);
  2273. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  2274. { must the object be finalized ? }
  2275. if procinfo^._class.needs_inittable then
  2276. begin
  2277. getlabel(nofinal);
  2278. exprasmList.insert(Tai_label.Create(nofinal));
  2279. emitinsertcall('FPC_FINALIZE');
  2280. ungetregister32(R_EDI);
  2281. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  2282. exprasmList.insert(Taicpu.Op_sym(A_PUSH,S_L,procinfo^._class.get_inittable_label));
  2283. ai:=Taicpu.Op_sym(A_Jcc,S_NO,nofinal);
  2284. ai.SetCondition(C_Z);
  2285. exprasmList.insert(ai);
  2286. reset_reference(hr);
  2287. hr.base:=R_EBP;
  2288. hr.offset:=8;
  2289. exprasmList.insert(Taicpu.Op_const_ref(A_CMP,S_L,0,newreference(hr)));
  2290. end;
  2291. end
  2292. else
  2293. begin
  2294. Internalerror(200006161);
  2295. end;
  2296. end;
  2297. { finalize temporary data }
  2298. finalizetempvariables;
  2299. { finalize local data like ansistrings}
  2300. case aktprocsym.definition.proctypeoption of
  2301. potype_unitfinalize:
  2302. begin
  2303. { using current_module.globalsymtable is hopefully }
  2304. { more robust than symtablestack and symtablestack.next }
  2305. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2306. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2307. end;
  2308. { units have seperate code for initialization and finalization }
  2309. potype_unitinit: ;
  2310. else
  2311. aktprocsym.definition.localst.foreach_static({$ifndef TP}@{$endif}finalize_data);
  2312. end;
  2313. { finalize paras data }
  2314. if assigned(aktprocsym.definition.parast) then
  2315. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}final_paras);
  2316. { do we need to handle exceptions because of ansi/widestrings ? }
  2317. if not inlined and
  2318. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2319. { but it's useless in init/final code of units }
  2320. not(aktprocsym.definition.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2321. begin
  2322. { the exception helper routines modify all registers }
  2323. aktprocsym.definition.usedregisters:=$ff;
  2324. getlabel(noreraiselabel);
  2325. emitcall('FPC_POPADDRSTACK');
  2326. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2327. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_EAX));
  2328. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2329. ungetregister32(R_EAX);
  2330. emitjmp(C_E,noreraiselabel);
  2331. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  2332. begin
  2333. if assigned(procinfo^._class) then
  2334. begin
  2335. pd:=procinfo^._class.searchdestructor;
  2336. if assigned(pd) then
  2337. begin
  2338. getlabel(nodestroycall);
  2339. emit_const_ref(A_CMP,S_L,0,new_reference(procinfo^.framepointer,
  2340. procinfo^.selfpointer_offset));
  2341. emitjmp(C_E,nodestroycall);
  2342. if is_class(procinfo^._class) then
  2343. begin
  2344. emit_const(A_PUSH,S_L,1);
  2345. emit_reg(A_PUSH,S_L,R_ESI);
  2346. end
  2347. else if is_object(procinfo^._class) then
  2348. begin
  2349. emit_reg(A_PUSH,S_L,R_ESI);
  2350. emit_sym(A_PUSH,S_L,newasmsymbol(procinfo^._class.vmt_mangledname));
  2351. end
  2352. else
  2353. begin
  2354. Internalerror(200006161);
  2355. end;
  2356. if (po_virtualmethod in pd.procoptions) then
  2357. begin
  2358. emit_ref_reg(A_MOV,S_L,new_reference(R_ESI,0),R_EDI);
  2359. emit_ref(A_CALL,S_NO,new_reference(R_EDI,procinfo^._class.vmtmethodoffset(pd.extnumber)));
  2360. end
  2361. else
  2362. emitcall(pd.mangledname);
  2363. { not necessary because the result is never assigned in the
  2364. case of an exception (FK)
  2365. emit_const_reg(A_MOV,S_L,0,R_ESI);
  2366. emit_const_ref(A_MOV,S_L,0,new_reference(procinfo^.framepointer,8));
  2367. }
  2368. emitlab(nodestroycall);
  2369. end;
  2370. end
  2371. end
  2372. else
  2373. { must be the return value finalized before reraising the exception? }
  2374. if (not is_void(procinfo^.returntype.def)) and
  2375. (procinfo^.returntype.def.needs_inittable) and
  2376. ((procinfo^.returntype.def.deftype<>objectdef) or
  2377. not is_class(procinfo^.returntype.def)) then
  2378. begin
  2379. reset_reference(hr);
  2380. hr.offset:=procinfo^.return_offset;
  2381. hr.base:=procinfo^.framepointer;
  2382. finalize(procinfo^.returntype.def,hr,ret_in_param(procinfo^.returntype.def));
  2383. end;
  2384. emitcall('FPC_RERAISE');
  2385. emitlab(noreraiselabel);
  2386. end;
  2387. { call __EXIT for main program }
  2388. if (not DLLsource) and (not inlined) and (aktprocsym.definition.proctypeoption=potype_proginit) then
  2389. begin
  2390. emitcall('FPC_DO_EXIT');
  2391. end;
  2392. { handle return value }
  2393. uses_eax:=false;
  2394. uses_edx:=false;
  2395. uses_esi:=false;
  2396. if not(po_assembler in aktprocsym.definition.procoptions) then
  2397. if (aktprocsym.definition.proctypeoption<>potype_constructor) then
  2398. handle_return_value(inlined,uses_eax,uses_edx)
  2399. else
  2400. begin
  2401. { successful constructor deletes the zero flag }
  2402. { and returns self in eax }
  2403. { eax must be set to zero if the allocation failed !!! }
  2404. getlabel(okexitlabel);
  2405. emitjmp(C_NONE,okexitlabel);
  2406. emitlab(faillabel);
  2407. if is_class(procinfo^._class) then
  2408. begin
  2409. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,8),R_ESI);
  2410. emitcall('FPC_HELP_FAIL_CLASS');
  2411. end
  2412. else if is_object(procinfo^._class) then
  2413. begin
  2414. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,12),R_ESI);
  2415. getexplicitregister32(R_EDI);
  2416. emit_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI);
  2417. emitcall('FPC_HELP_FAIL');
  2418. ungetregister32(R_EDI);
  2419. end
  2420. else
  2421. Internalerror(200006161);
  2422. emitlab(okexitlabel);
  2423. { for classes this is done after the call to }
  2424. { AfterConstruction }
  2425. if is_object(procinfo^._class) then
  2426. begin
  2427. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2428. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  2429. uses_eax:=true;
  2430. end;
  2431. emit_reg_reg(A_TEST,S_L,R_ESI,R_ESI);
  2432. uses_esi:=true;
  2433. end;
  2434. if aktexit2label.is_used and not aktexit2label.is_set then
  2435. emitlab(aktexit2label);
  2436. if ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  2437. begin
  2438. getlabel(stabsendlabel);
  2439. emitlab(stabsendlabel);
  2440. end;
  2441. { gives problems for long mangled names }
  2442. {List.concat(Tai_symbol.Create(aktprocsym.definition.mangledname+'_end'));}
  2443. { should we restore edi ? }
  2444. { for all i386 gcc implementations }
  2445. if (po_savestdregs in aktprocsym.definition.procoptions) then
  2446. begin
  2447. if (aktprocsym.definition.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2448. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  2449. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  2450. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  2451. { here we could reset R_EBX
  2452. but that is risky because it only works
  2453. if genexitcode is called after genentrycode
  2454. so lets skip this for the moment PM
  2455. aktprocsym.definition.usedregisters:=
  2456. aktprocsym.definition.usedregisters or not ($80 shr byte(R_EBX));
  2457. }
  2458. end;
  2459. { for the save all registers we can simply use a pusha,popa which
  2460. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  2461. if (po_saveregisters in aktprocsym.definition.procoptions) then
  2462. begin
  2463. if uses_esi then
  2464. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,4)));
  2465. if uses_edx then
  2466. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,20)));
  2467. if uses_eax then
  2468. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,28)));
  2469. exprasmList.concat(Taicpu.Op_none(A_POPA,S_L));
  2470. end;
  2471. if not(nostackframe) then
  2472. begin
  2473. if not inlined then
  2474. exprasmList.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  2475. end
  2476. else
  2477. begin
  2478. if (gettempsize<>0) and not inlined then
  2479. exprasmList.insert(Taicpu.op_const_reg(A_ADD,S_L,gettempsize,R_ESP));
  2480. end;
  2481. { parameters are limited to 65535 bytes because }
  2482. { ret allows only imm16 }
  2483. if (parasize>65535) and not(pocall_clearstack in aktprocsym.definition.proccalloptions) then
  2484. CGMessage(cg_e_parasize_too_big);
  2485. { at last, the return is generated }
  2486. if not inlined then
  2487. if (po_interrupt in aktprocsym.definition.procoptions) then
  2488. begin
  2489. if uses_esi then
  2490. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,16)));
  2491. if uses_edx then
  2492. begin
  2493. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2494. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,12)));
  2495. end;
  2496. if uses_eax then
  2497. begin
  2498. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2499. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,0)));
  2500. end;
  2501. generate_interrupt_stackframe_exit;
  2502. end
  2503. else
  2504. begin
  2505. {Routines with the poclearstack flag set use only a ret.}
  2506. { also routines with parasize=0 }
  2507. if (pocall_clearstack in aktprocsym.definition.proccalloptions) then
  2508. begin
  2509. {$ifndef OLD_C_STACK}
  2510. { complex return values are removed from stack in C code PM }
  2511. if ret_in_param(aktprocsym.definition.rettype.def) then
  2512. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,4))
  2513. else
  2514. {$endif not OLD_C_STACK}
  2515. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2516. end
  2517. else if (parasize=0) then
  2518. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO))
  2519. else
  2520. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  2521. end;
  2522. if not inlined then
  2523. exprasmList.concat(Tai_symbol_end.Createname(aktprocsym.definition.mangledname));
  2524. {$ifdef GDB}
  2525. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  2526. begin
  2527. aktprocsym.concatstabto(exprasmlist);
  2528. if assigned(procinfo^._class) then
  2529. if (not assigned(procinfo^.parent) or
  2530. not assigned(procinfo^.parent^._class)) then
  2531. begin
  2532. if (po_classmethod in aktprocsym.definition.procoptions) or
  2533. ((po_virtualmethod in aktprocsym.definition.procoptions) and
  2534. (potype_constructor=aktprocsym.definition.proctypeoption)) or
  2535. (po_staticmethod in aktprocsym.definition.procoptions) then
  2536. begin
  2537. exprasmList.concat(Tai_stabs.Create(strpnew(
  2538. '"pvmt:p'+tstoreddef(pvmttype.def).numberstring+'",'+
  2539. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2540. end
  2541. else
  2542. begin
  2543. if not(is_class(procinfo^._class)) then
  2544. st:='v'
  2545. else
  2546. st:='p';
  2547. exprasmList.concat(Tai_stabs.Create(strpnew(
  2548. '"$t:'+st+procinfo^._class.numberstring+'",'+
  2549. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2550. end;
  2551. end
  2552. else
  2553. begin
  2554. if not is_class(procinfo^._class) then
  2555. st:='*'
  2556. else
  2557. st:='';
  2558. exprasmList.concat(Tai_stabs.Create(strpnew(
  2559. '"$t:r'+st+procinfo^._class.numberstring+'",'+
  2560. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI]))));
  2561. end;
  2562. { define calling EBP as pseudo local var PM }
  2563. { this enables test if the function is a local one !! }
  2564. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  2565. exprasmList.concat(Tai_stabs.Create(strpnew(
  2566. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  2567. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset))));
  2568. if (not is_void(aktprocsym.definition.rettype.def)) then
  2569. begin
  2570. if ret_in_param(aktprocsym.definition.rettype.def) then
  2571. exprasmList.concat(Tai_stabs.Create(strpnew(
  2572. '"'+aktprocsym.name+':X*'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2573. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2574. else
  2575. exprasmList.concat(Tai_stabs.Create(strpnew(
  2576. '"'+aktprocsym.name+':X'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2577. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2578. if (m_result in aktmodeswitches) then
  2579. if ret_in_param(aktprocsym.definition.rettype.def) then
  2580. exprasmList.concat(Tai_stabs.Create(strpnew(
  2581. '"RESULT:X*'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2582. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2583. else
  2584. exprasmList.concat(Tai_stabs.Create(strpnew(
  2585. '"RESULT:X'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2586. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2587. end;
  2588. mangled_length:=length(aktprocsym.definition.mangledname);
  2589. getmem(p,2*mangled_length+50);
  2590. strpcopy(p,'192,0,0,');
  2591. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2592. if (target_info.use_function_relative_addresses) then
  2593. begin
  2594. strpcopy(strend(p),'-');
  2595. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2596. end;
  2597. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2598. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  2599. +aktprocsym.definition.mangledname))));
  2600. p[0]:='2';p[1]:='2';p[2]:='4';
  2601. strpcopy(strend(p),'_end');}
  2602. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  2603. if (target_info.use_function_relative_addresses) then
  2604. begin
  2605. strpcopy(strend(p),'-');
  2606. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2607. end;
  2608. exprasmList.concatlist(withdebuglist);
  2609. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2610. { strpnew('224,0,0,'
  2611. +aktprocsym.definition.mangledname+'_end'))));}
  2612. freemem(p,2*mangled_length+50);
  2613. end;
  2614. {$endif GDB}
  2615. if inlined then
  2616. cleanup_regvars(exprasmlist);
  2617. exprasmlist:=oldexprasmlist;
  2618. end;
  2619. procedure genimplicitunitfinal(alist : TAAsmoutput);
  2620. begin
  2621. { using current_module.globalsymtable is hopefully }
  2622. { more robust than symtablestack and symtablestack.next }
  2623. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2624. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2625. exprasmList.insert(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  2626. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_finalize',0));
  2627. {$ifdef GDB}
  2628. if (cs_debuginfo in aktmoduleswitches) and
  2629. target_info.use_function_relative_addresses then
  2630. exprasmList.insert(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  2631. {$endif GDB}
  2632. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2633. aList.concatlist(exprasmlist);
  2634. end;
  2635. procedure genimplicitunitinit(alist : TAAsmoutput);
  2636. begin
  2637. { using current_module.globalsymtable is hopefully }
  2638. { more robust than symtablestack and symtablestack.next }
  2639. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2640. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2641. exprasmList.insert(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  2642. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_init',0));
  2643. {$ifdef GDB}
  2644. if (cs_debuginfo in aktmoduleswitches) and
  2645. target_info.use_function_relative_addresses then
  2646. exprasmList.insert(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  2647. {$endif GDB}
  2648. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2649. aList.concatlist(exprasmlist);
  2650. end;
  2651. {$ifdef test_dest_loc}
  2652. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  2653. begin
  2654. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  2655. begin
  2656. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  2657. set_location(p^.location,dest_loc);
  2658. in_dest_loc:=true;
  2659. end
  2660. else
  2661. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  2662. begin
  2663. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference)));
  2664. set_location(p^.location,dest_loc);
  2665. in_dest_loc:=true;
  2666. end
  2667. else
  2668. internalerror(20080);
  2669. end;
  2670. {$endif test_dest_loc}
  2671. end.
  2672. {
  2673. $Log$
  2674. Revision 1.21 2001-04-18 22:02:00 peter
  2675. * registration of targets and assemblers
  2676. Revision 1.20 2001/04/13 01:22:17 peter
  2677. * symtable change to classes
  2678. * range check generation and errors fixed, make cycle DEBUG=1 works
  2679. * memory leaks fixed
  2680. Revision 1.19 2001/04/05 21:33:07 peter
  2681. * fast exit fix merged
  2682. Revision 1.18 2001/04/02 21:20:35 peter
  2683. * resulttype rewrite
  2684. Revision 1.17 2001/01/05 17:36:58 florian
  2685. * the info about exception frames is stored now on the stack
  2686. instead on the heap
  2687. Revision 1.16 2000/12/25 00:07:31 peter
  2688. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2689. tlinkedlist objects)
  2690. Revision 1.15 2000/12/05 11:44:32 jonas
  2691. + new integer regvar handling, should be much more efficient
  2692. Revision 1.14 2000/11/29 00:30:43 florian
  2693. * unused units removed from uses clause
  2694. * some changes for widestrings
  2695. Revision 1.13 2000/11/28 00:28:07 pierre
  2696. * stabs fixing
  2697. Revision 1.12 2000/11/22 15:12:06 jonas
  2698. * fixed inline-related problems (partially "merges")
  2699. Revision 1.11 2000/11/17 10:30:24 florian
  2700. * passing interfaces as parameters fixed
  2701. Revision 1.10 2000/11/07 23:40:48 florian
  2702. + AfterConstruction and BeforeDestruction impemented
  2703. Revision 1.9 2000/11/06 23:49:20 florian
  2704. * fixed init_paras call
  2705. Revision 1.8 2000/11/06 23:15:01 peter
  2706. * added copyvaluepara call again
  2707. Revision 1.7 2000/11/04 14:25:23 florian
  2708. + merged Attila's changes for interfaces, not tested yet
  2709. Revision 1.6 2000/10/31 22:02:55 peter
  2710. * symtable splitted, no real code changes
  2711. Revision 1.5 2000/10/24 22:23:04 peter
  2712. * emitcall -> emitinsertcall for profiling (merged)
  2713. Revision 1.4 2000/10/24 12:47:45 jonas
  2714. * allocate registers which hold function result
  2715. Revision 1.3 2000/10/24 08:54:25 michael
  2716. + Extra patch from peter
  2717. Revision 1.2 2000/10/24 07:20:03 pierre
  2718. * fix for bug 1193 (merged)
  2719. Revision 1.1 2000/10/15 09:47:42 peter
  2720. * moved to i386/
  2721. Revision 1.19 2000/10/14 10:14:46 peter
  2722. * moehrendorf oct 2000 rewrite
  2723. Revision 1.18 2000/10/10 14:55:28 jonas
  2724. * added missing regallocs for edi in emit_mov_ref_reg64 (merged)
  2725. Revision 1.17 2000/10/01 19:48:23 peter
  2726. * lot of compile updates for cg11
  2727. Revision 1.16 2000/09/30 16:08:45 peter
  2728. * more cg11 updates
  2729. Revision 1.15 2000/09/24 15:06:12 peter
  2730. * use defines.inc
  2731. Revision 1.14 2000/09/16 12:22:52 peter
  2732. * freebsd support merged
  2733. Revision 1.13 2000/08/27 16:11:49 peter
  2734. * moved some util functions from globals,cobjects to cutils
  2735. * splitted files into finput,fmodule
  2736. Revision 1.12 2000/08/24 19:07:54 peter
  2737. * don't initialize if localvarsym is set because that varsym will
  2738. already be initialized
  2739. * first initialize local data before copy of value para's (merged)
  2740. Revision 1.11 2000/08/19 20:09:33 peter
  2741. * check size after checking openarray in push_value_para (merged)
  2742. Revision 1.10 2000/08/16 13:06:06 florian
  2743. + support of 64 bit integer constants
  2744. Revision 1.9 2000/08/10 18:42:03 peter
  2745. * fixed for constants in emit_push_mem_size for go32v2 (merged)
  2746. Revision 1.8 2000/08/07 11:29:40 jonas
  2747. + emit_push_mem_size() which pushes a value in memory of a certain size
  2748. * pushsetelement() and pushvaluepara() use this new procedure, because
  2749. otherwise they could sometimes try to push data past the end of the
  2750. heap, causing a crash
  2751. (merged from fixes branch)
  2752. Revision 1.7 2000/08/03 13:17:25 jonas
  2753. + allow regvars to be used inside inlined procs, which required the
  2754. following changes:
  2755. + load regvars in genentrycode/free them in genexitcode (cgai386)
  2756. * moved all regvar related code to new regvars unit
  2757. + added pregvarinfo type to hcodegen
  2758. + added regvarinfo field to tprocinfo (symdef/symdefh)
  2759. * deallocate the regvars of the caller in secondprocinline before
  2760. inlining the called procedure and reallocate them afterwards
  2761. Revision 1.6 2000/08/02 08:05:04 jonas
  2762. * fixed web bug1087
  2763. * allocate R_ECX explicitely if it's used
  2764. (merged from fixes branch)
  2765. Revision 1.5 2000/07/27 09:25:05 jonas
  2766. * moved locflags2reg() procedure from cg386add to cgai386
  2767. + added locjump2reg() procedure to cgai386
  2768. * fixed internalerror(2002) when the result of a case expression has
  2769. LOC_JUMP
  2770. (all merged from fixes branch)
  2771. Revision 1.4 2000/07/21 15:14:02 jonas
  2772. + added is_addr field for labels, if they are only used for getting the address
  2773. (e.g. for io checks) and corresponding getaddrlabel() procedure
  2774. Revision 1.3 2000/07/13 12:08:25 michael
  2775. + patched to 1.1.0 with former 1.09patch from peter
  2776. Revision 1.2 2000/07/13 11:32:37 michael
  2777. + removed logs
  2778. }