cgai386.pas 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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. unit cgai386;
  18. interface
  19. uses
  20. cobjects,tree,
  21. cpubase,cpuasm,
  22. symconst,symtable,aasm,win_targ;
  23. {$define TESTGETTEMP to store const that
  24. are written into temps for later release PM }
  25. function def_opsize(p1:pdef):topsize;
  26. function def2def_opsize(p1,p2:pdef):topsize;
  27. function def_getreg(p1:pdef):tregister;
  28. function makereg8(r:tregister):tregister;
  29. function makereg16(r:tregister):tregister;
  30. function makereg32(r:tregister):tregister;
  31. procedure emitlab(var l : pasmlabel);
  32. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  33. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  34. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  35. procedure emitcall(const routine:string);
  36. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference);
  37. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  38. procedure emit_push_loc(const t:tlocation);
  39. { pushes qword location to the stack }
  40. procedure emit_pushq_loc(const t : tlocation);
  41. procedure release_qword_loc(const t : tlocation);
  42. { releases the registers of a location }
  43. procedure release_loc(const t : tlocation);
  44. procedure emit_pushw_loc(const t:tlocation);
  45. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference);
  46. procedure emit_push_lea_loc(const t:tlocation);
  47. procedure emit_to_reference(var p:ptree);
  48. procedure emit_to_reg16(var hr:tregister);
  49. procedure emit_to_reg32(var hr:tregister);
  50. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  51. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  52. procedure copyshortstring(const dref,sref : treference;len : byte;loadref:boolean);
  53. procedure loadansistring(p : ptree);
  54. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  55. procedure decrstringref(t : pdef;const ref : treference);
  56. function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
  57. procedure push_int(l : longint);
  58. procedure emit_push_mem(const ref : treference);
  59. procedure emitpushreferenceaddr(const ref : treference);
  60. procedure pushsetelement(p : ptree);
  61. procedure restore(p : ptree;isint64 : boolean);
  62. procedure push_value_para(p:ptree;inlined:boolean;para_offset:longint;alignment : longint);
  63. {$ifdef TEMPS_NOT_PUSH}
  64. { does the same as restore/maybe_push, but uses temp. space instead of pushing }
  65. function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
  66. procedure restorefromtemp(p : ptree;isint64 : boolean);
  67. {$endif TEMPS_NOT_PUSH}
  68. procedure floatload(t : tfloattype;const ref : treference);
  69. procedure floatstore(t : tfloattype;const ref : treference);
  70. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  71. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  72. procedure maybe_loadesi;
  73. procedure maketojumpbool(p : ptree);
  74. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;destreg:Tregister;delloc:boolean);
  75. procedure emitoverflowcheck(p:ptree);
  76. procedure emitrangecheck(p:ptree;todef:pdef);
  77. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
  78. procedure firstcomplex(p : ptree);
  79. procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  80. stackframe:longint;
  81. var parasize:longint;var nostackframe:boolean;
  82. inlined : boolean);
  83. procedure genexitcode(alist : paasmoutput;parasize:longint;
  84. nostackframe,inlined:boolean);
  85. {$ifdef test_dest_loc}
  86. const
  87. { used to avoid temporary assignments }
  88. dest_loc_known : boolean = false;
  89. in_dest_loc : boolean = false;
  90. dest_loc_tree : ptree = nil;
  91. var
  92. dest_loc : tlocation;
  93. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  94. {$endif test_dest_loc}
  95. implementation
  96. uses
  97. strings,globtype,systems,globals,verbose,files,types,pbase,
  98. tgeni386,temp_gen,hcodegen,ppu
  99. {$ifdef GDB}
  100. ,gdb
  101. {$endif}
  102. ;
  103. {*****************************************************************************
  104. Helpers
  105. *****************************************************************************}
  106. function def_opsize(p1:pdef):topsize;
  107. begin
  108. case p1^.size of
  109. 1 : def_opsize:=S_B;
  110. 2 : def_opsize:=S_W;
  111. 4 : def_opsize:=S_L;
  112. else
  113. internalerror(78);
  114. end;
  115. end;
  116. function def2def_opsize(p1,p2:pdef):topsize;
  117. var
  118. o1 : topsize;
  119. begin
  120. case p1^.size of
  121. 1 : o1:=S_B;
  122. 2 : o1:=S_W;
  123. 4 : o1:=S_L;
  124. { I don't know if we need it (FK) }
  125. 8 : o1:=S_L;
  126. else
  127. internalerror(78);
  128. end;
  129. if assigned(p2) then
  130. begin
  131. case p2^.size of
  132. 1 : o1:=S_B;
  133. 2 : begin
  134. if o1=S_B then
  135. o1:=S_BW
  136. else
  137. o1:=S_W;
  138. end;
  139. 4,8:
  140. begin
  141. case o1 of
  142. S_B : o1:=S_BL;
  143. S_W : o1:=S_WL;
  144. end;
  145. end;
  146. end;
  147. end;
  148. def2def_opsize:=o1;
  149. end;
  150. function def_getreg(p1:pdef):tregister;
  151. begin
  152. case p1^.size of
  153. 1 : def_getreg:=reg32toreg8(getregister32);
  154. 2 : def_getreg:=reg32toreg16(getregister32);
  155. 4 : def_getreg:=getregister32;
  156. else
  157. internalerror(78);
  158. end;
  159. end;
  160. function makereg8(r:tregister):tregister;
  161. begin
  162. case r of
  163. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  164. makereg8:=reg32toreg8(r);
  165. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  166. makereg8:=reg16toreg8(r);
  167. R_AL,R_BL,R_CL,R_DL :
  168. makereg8:=r;
  169. end;
  170. end;
  171. function makereg16(r:tregister):tregister;
  172. begin
  173. case r of
  174. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  175. makereg16:=reg32toreg16(r);
  176. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  177. makereg16:=r;
  178. R_AL,R_BL,R_CL,R_DL :
  179. makereg16:=reg8toreg16(r);
  180. end;
  181. end;
  182. function makereg32(r:tregister):tregister;
  183. begin
  184. case r of
  185. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  186. makereg32:=r;
  187. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  188. makereg32:=reg16toreg32(r);
  189. R_AL,R_BL,R_CL,R_DL :
  190. makereg32:=reg8toreg32(r);
  191. end;
  192. end;
  193. {*****************************************************************************
  194. Emit Assembler
  195. *****************************************************************************}
  196. procedure emitlab(var l : pasmlabel);
  197. begin
  198. if not l^.is_set then
  199. exprasmlist^.concat(new(pai_label,init(l)))
  200. else
  201. internalerror(7453984);
  202. end;
  203. {$ifdef nojmpfix}
  204. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  205. var
  206. ai : Pai386;
  207. begin
  208. if c=C_None then
  209. exprasmlist^.concat(new(pai386,op_sym(A_JMP,S_NO,l)))
  210. else
  211. begin
  212. ai:=new(pai386,op_sym(A_Jcc,S_NO,l));
  213. ai^.SetCondition(c);
  214. ai^.is_jmp:=true;
  215. exprasmlist^.concat(ai);
  216. end;
  217. end;
  218. {$else nojmpfix}
  219. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  220. var
  221. ai : Pai386;
  222. begin
  223. if c=C_None then
  224. ai := new(pai386,op_sym(A_JMP,S_NO,l))
  225. else
  226. begin
  227. ai:=new(pai386,op_sym(A_Jcc,S_NO,l));
  228. ai^.SetCondition(c);
  229. end;
  230. ai^.is_jmp:=true;
  231. exprasmlist^.concat(ai);
  232. end;
  233. {$endif nojmpfix}
  234. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  235. var
  236. ai : pai386;
  237. hreg : tregister;
  238. begin
  239. hreg:=makereg8(hregister);
  240. ai:=new(pai386,op_reg(A_Setcc,S_B,hreg));
  241. ai^.SetCondition(flag_2_cond[flag]);
  242. exprasmlist^.concat(ai);
  243. if hreg<>hregister then
  244. begin
  245. if hregister in regset16bit then
  246. emit_to_reg16(hreg)
  247. else
  248. emit_to_reg32(hreg);
  249. end;
  250. end;
  251. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  252. begin
  253. if (reg1<>reg2) or (i<>A_MOV) then
  254. exprasmlist^.concat(new(pai386,op_reg_reg(i,s,reg1,reg2)));
  255. end;
  256. procedure emitcall(const routine:string);
  257. begin
  258. exprasmlist^.concat(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol(routine))));
  259. end;
  260. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference);
  261. begin
  262. case t.loc of
  263. LOC_REGISTER,
  264. LOC_CREGISTER : begin
  265. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  266. t.register,newreference(ref))));
  267. ungetregister32(t.register); { the register is not needed anymore }
  268. end;
  269. LOC_MEM,
  270. LOC_REFERENCE : begin
  271. if t.reference.is_immediate then
  272. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,
  273. t.reference.offset,newreference(ref))))
  274. else
  275. begin
  276. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  277. newreference(t.reference),R_EDI)));
  278. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  279. R_EDI,newreference(ref))));
  280. { we can release the registers }
  281. { but only AFTER the MOV! Important for the optimizer!
  282. (JM)}
  283. del_reference(ref);
  284. end;
  285. ungetiftemp(t.reference);
  286. end;
  287. else
  288. internalerror(330);
  289. end;
  290. end;
  291. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  292. begin
  293. case t.loc of
  294. LOC_REGISTER,
  295. LOC_CREGISTER : begin
  296. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  297. t.register,reg)));
  298. ungetregister32(t.register); { the register is not needed anymore }
  299. end;
  300. LOC_MEM,
  301. LOC_REFERENCE : begin
  302. if t.reference.is_immediate then
  303. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  304. t.reference.offset,reg)))
  305. else
  306. begin
  307. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  308. newreference(t.reference),reg)));
  309. end;
  310. ungetiftemp(t.reference);
  311. end;
  312. else
  313. internalerror(330);
  314. end;
  315. end;
  316. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  317. begin
  318. case t.loc of
  319. LOC_REGISTER,
  320. LOC_CREGISTER : begin
  321. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,RegSize(Reg),
  322. reg,t.register)));
  323. end;
  324. LOC_MEM,
  325. LOC_REFERENCE : begin
  326. if t.reference.is_immediate then
  327. internalerror(334)
  328. else
  329. begin
  330. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,RegSize(Reg),
  331. Reg,newreference(t.reference))));
  332. end;
  333. end;
  334. else
  335. internalerror(330);
  336. end;
  337. end;
  338. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  339. begin
  340. case t.loc of
  341. LOC_REGISTER,
  342. LOC_CREGISTER : begin
  343. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  344. reglow,t.registerlow)));
  345. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  346. reghigh,t.registerhigh)));
  347. end;
  348. LOC_MEM,
  349. LOC_REFERENCE : begin
  350. if t.reference.is_immediate then
  351. internalerror(334)
  352. else
  353. begin
  354. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  355. Reglow,newreference(t.reference))));
  356. inc(t.reference.offset,4);
  357. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  358. Reghigh,newreference(t.reference))));
  359. end;
  360. end;
  361. else
  362. internalerror(330);
  363. end;
  364. end;
  365. procedure emit_pushq_loc(const t : tlocation);
  366. var
  367. hr : preference;
  368. begin
  369. case t.loc of
  370. LOC_REGISTER,
  371. LOC_CREGISTER:
  372. begin
  373. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  374. t.registerhigh)));
  375. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  376. t.registerlow)));
  377. end;
  378. LOC_MEM,
  379. LOC_REFERENCE:
  380. begin
  381. hr:=newreference(t.reference);
  382. inc(hr^.offset,4);
  383. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  384. hr)));
  385. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  386. newreference(t.reference))));
  387. ungetiftemp(t.reference);
  388. end;
  389. else internalerror(331);
  390. end;
  391. end;
  392. procedure release_loc(const t : tlocation);
  393. begin
  394. case t.loc of
  395. LOC_REGISTER,
  396. LOC_CREGISTER:
  397. begin
  398. ungetregister32(t.register);
  399. end;
  400. LOC_MEM,
  401. LOC_REFERENCE:
  402. del_reference(t.reference);
  403. else internalerror(332);
  404. end;
  405. end;
  406. procedure release_qword_loc(const t : tlocation);
  407. begin
  408. case t.loc of
  409. LOC_REGISTER,
  410. LOC_CREGISTER:
  411. begin
  412. ungetregister32(t.registerhigh);
  413. ungetregister32(t.registerlow);
  414. end;
  415. LOC_MEM,
  416. LOC_REFERENCE:
  417. del_reference(t.reference);
  418. else internalerror(331);
  419. end;
  420. end;
  421. procedure emit_push_loc(const t:tlocation);
  422. begin
  423. case t.loc of
  424. LOC_REGISTER,
  425. LOC_CREGISTER : begin
  426. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,makereg32(t.register))));
  427. ungetregister(t.register); { the register is not needed anymore }
  428. end;
  429. LOC_MEM,
  430. LOC_REFERENCE : begin
  431. if t.reference.is_immediate then
  432. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,t.reference.offset)))
  433. else
  434. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(t.reference))));
  435. del_reference(t.reference);
  436. ungetiftemp(t.reference);
  437. end;
  438. else
  439. internalerror(330);
  440. end;
  441. end;
  442. procedure emit_pushw_loc(const t:tlocation);
  443. var
  444. opsize : topsize;
  445. begin
  446. case t.loc of
  447. LOC_REGISTER,
  448. LOC_CREGISTER : begin
  449. if target_os.stackalignment=4 then
  450. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,makereg32(t.register))))
  451. else
  452. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,makereg16(t.register))));
  453. ungetregister(t.register); { the register is not needed anymore }
  454. end;
  455. LOC_MEM,
  456. LOC_REFERENCE : begin
  457. if target_os.stackalignment=4 then
  458. opsize:=S_L
  459. else
  460. opsize:=S_W;
  461. if t.reference.is_immediate then
  462. exprasmlist^.concat(new(pai386,op_const(A_PUSH,opsize,t.reference.offset)))
  463. else
  464. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,opsize,newreference(t.reference))));
  465. del_reference(t.reference);
  466. ungetiftemp(t.reference);
  467. end;
  468. else
  469. internalerror(330);
  470. end;
  471. end;
  472. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference);
  473. begin
  474. case t.loc of
  475. LOC_MEM,
  476. LOC_REFERENCE : begin
  477. if t.reference.is_immediate then
  478. internalerror(331)
  479. else
  480. begin
  481. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  482. newreference(t.reference),R_EDI)));
  483. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  484. R_EDI,newreference(ref))));
  485. end;
  486. ungetiftemp(t.reference);
  487. end;
  488. else
  489. internalerror(332);
  490. end;
  491. end;
  492. procedure emit_push_lea_loc(const t:tlocation);
  493. begin
  494. case t.loc of
  495. LOC_MEM,
  496. LOC_REFERENCE : begin
  497. if t.reference.is_immediate then
  498. internalerror(331)
  499. else
  500. begin
  501. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  502. newreference(t.reference),R_EDI)));
  503. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  504. end;
  505. { Wrong !!
  506. ungetiftemp(t.reference);}
  507. end;
  508. else
  509. internalerror(332);
  510. end;
  511. end;
  512. procedure emit_to_reference(var p:ptree);
  513. begin
  514. case p^.location.loc of
  515. LOC_FPU : begin
  516. reset_reference(p^.location.reference);
  517. gettempofsizereference(10,p^.location.reference);
  518. floatstore(pfloatdef(p^.resulttype)^.typ,p^.location.reference);
  519. p^.location.loc:=LOC_REFERENCE;
  520. end;
  521. LOC_MEM,
  522. LOC_REFERENCE : ;
  523. else
  524. internalerror(333);
  525. end;
  526. end;
  527. procedure emit_to_reg16(var hr:tregister);
  528. begin
  529. { ranges are a little bit bug sensitive ! }
  530. case hr of
  531. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP,R_EBP:
  532. begin
  533. hr:=reg32toreg16(hr);
  534. end;
  535. R_AL,R_BL,R_CL,R_DL:
  536. begin
  537. hr:=reg8toreg16(hr);
  538. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_W,$ff,hr)));
  539. end;
  540. R_AH,R_BH,R_CH,R_DH:
  541. begin
  542. hr:=reg8toreg16(hr);
  543. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_W,$ff00,hr)));
  544. end;
  545. end;
  546. end;
  547. procedure emit_to_reg32(var hr:tregister);
  548. begin
  549. { ranges are a little bit bug sensitive ! }
  550. case hr of
  551. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP,R_BP:
  552. begin
  553. hr:=reg16toreg32(hr);
  554. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_L,$ffff,hr)));
  555. end;
  556. R_AL,R_BL,R_CL,R_DL:
  557. begin
  558. hr:=reg8toreg32(hr);
  559. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_L,$ff,hr)));
  560. end;
  561. R_AH,R_BH,R_CH,R_DH:
  562. begin
  563. hr:=reg8toreg32(hr);
  564. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_L,$ff00,hr)));
  565. end;
  566. end;
  567. end;
  568. {*****************************************************************************
  569. Emit String Functions
  570. *****************************************************************************}
  571. procedure copyshortstring(const dref,sref : treference;len : byte;loadref:boolean);
  572. begin
  573. emitpushreferenceaddr(dref);
  574. if loadref then
  575. emit_push_mem(sref)
  576. else
  577. emitpushreferenceaddr(sref);
  578. push_int(len);
  579. emitcall('FPC_SHORTSTR_COPY');
  580. maybe_loadesi;
  581. end;
  582. procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
  583. begin
  584. emitpushreferenceaddr(dref);
  585. if loadref then
  586. emit_push_mem(sref)
  587. else
  588. emitpushreferenceaddr(sref);
  589. push_int(len);
  590. emitcall('FPC_LONGSTR_COPY');
  591. maybe_loadesi;
  592. end;
  593. procedure decrstringref(t : pdef;const ref : treference);
  594. var
  595. pushedregs : tpushed;
  596. begin
  597. pushusedregisters(pushedregs,$ff);
  598. emitpushreferenceaddr(ref);
  599. if is_ansistring(t) then
  600. begin
  601. exprasmlist^.concat(new(pai386,
  602. op_sym(A_CALL,S_NO,newasmsymbol('FPC_ANSISTR_DECR_REF'))));
  603. end
  604. else if is_widestring(t) then
  605. begin
  606. exprasmlist^.concat(new(pai386,
  607. op_sym(A_CALL,S_NO,newasmsymbol('FPC_WIDESTR_DECR_REF'))));
  608. end
  609. else internalerror(1859);
  610. popusedregisters(pushedregs);
  611. end;
  612. procedure loadansistring(p : ptree);
  613. {
  614. copies an ansistring from p^.right to p^.left, we
  615. assume, that both sides are ansistring, firstassignement have
  616. to take care of that, an ansistring can't be a register variable
  617. }
  618. var
  619. pushed : tpushed;
  620. ungettemp : boolean;
  621. begin
  622. { before pushing any parameter, we have to save all used }
  623. { registers, but before that we have to release the }
  624. { registers of that node to save uneccessary pushed }
  625. { so be careful, if you think you can optimize that code (FK) }
  626. { nevertheless, this has to be changed, because otherwise the }
  627. { register is released before it's contents are pushed -> }
  628. { problems with the optimizer (JM) }
  629. del_reference(p^.left^.location.reference);
  630. ungettemp:=false;
  631. case p^.right^.location.loc of
  632. LOC_REGISTER,LOC_CREGISTER:
  633. begin
  634. {$IfNDef regallocfix}
  635. ungetregister32(p^.right^.location.register);
  636. pushusedregisters(pushed,$ff);
  637. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.right^.location.register)));
  638. {$Else regallocfix}
  639. pushusedregisters(pushed, $ff xor ($80 shr byte(p^.right^.location.register)));
  640. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.right^.location.register)));
  641. ungetregister32(p^.right^.location.register);
  642. {$EndIf regallocfix}
  643. end;
  644. LOC_REFERENCE,LOC_MEM:
  645. begin
  646. {$IfNDef regallocfix}
  647. del_reference(p^.right^.location.reference);
  648. pushusedregisters(pushed,$ff);
  649. emit_push_mem(p^.right^.location.reference);
  650. {$Else regallocfix}
  651. pushusedregisters(pushed,$ff
  652. xor ($80 shr byte(p^.right^.location.reference.base))
  653. xor ($80 shr byte(p^.right^.location.reference.index)));
  654. emit_push_mem(p^.right^.location.reference);
  655. del_reference(p^.right^.location.reference);
  656. {$EndIf regallocfix}
  657. ungettemp:=true;
  658. end;
  659. end;
  660. emitpushreferenceaddr(p^.left^.location.reference);
  661. del_reference(p^.left^.location.reference);
  662. emitcall('FPC_ANSISTR_ASSIGN');
  663. maybe_loadesi;
  664. popusedregisters(pushed);
  665. if ungettemp then
  666. ungetiftemp(p^.right^.location.reference);
  667. end;
  668. {*****************************************************************************
  669. Emit Push Functions
  670. *****************************************************************************}
  671. function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
  672. var
  673. pushed : boolean;
  674. {hregister : tregister; }
  675. {$ifdef TEMPS_NOT_PUSH}
  676. href : treference;
  677. {$endif TEMPS_NOT_PUSH}
  678. begin
  679. if needed>usablereg32 then
  680. begin
  681. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  682. begin
  683. if isint64 then
  684. begin
  685. {$ifdef TEMPS_NOT_PUSH}
  686. gettempofsizereference(href,8);
  687. p^.temp_offset:=href.offset;
  688. href.offset:=href.offset+4;
  689. exprasmlist^.concat(new(pai386,op_reg(A_MOV,S_L,p^.location.registerhigh,href)));
  690. href.offset:=href.offset-4;
  691. {$else TEMPS_NOT_PUSH}
  692. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.registerhigh)));
  693. {$endif TEMPS_NOT_PUSH}
  694. ungetregister32(p^.location.registerhigh);
  695. end
  696. {$ifdef TEMPS_NOT_PUSH}
  697. else
  698. begin
  699. gettempofsizereference(href,4);
  700. p^.temp_offset:=href.offset;
  701. end
  702. {$endif TEMPS_NOT_PUSH}
  703. ;
  704. pushed:=true;
  705. {$ifdef TEMPS_NOT_PUSH}
  706. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,p^.location.register,href)));
  707. {$else TEMPS_NOT_PUSH}
  708. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.register)));
  709. {$endif TEMPS_NOT_PUSH}
  710. ungetregister32(p^.location.register);
  711. end
  712. else if (p^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  713. ((p^.location.reference.base<>R_NO) or
  714. (p^.location.reference.index<>R_NO)
  715. ) then
  716. begin
  717. del_reference(p^.location.reference);
  718. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(p^.location.reference),
  719. R_EDI)));
  720. {$ifdef TEMPS_NOT_PUSH}
  721. gettempofsizereference(href,4);
  722. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,href)));
  723. p^.temp_offset:=href.offset;
  724. {$else TEMPS_NOT_PUSH}
  725. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  726. {$endif TEMPS_NOT_PUSH}
  727. pushed:=true;
  728. end
  729. else pushed:=false;
  730. end
  731. else pushed:=false;
  732. maybe_push:=pushed;
  733. end;
  734. {$ifdef TEMPS_NOT_PUSH}
  735. function maybe_savetotemp(needed : byte;p : ptree;isint64 : boolean) : boolean;
  736. var
  737. pushed : boolean;
  738. href : treference;
  739. begin
  740. if needed>usablereg32 then
  741. begin
  742. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  743. begin
  744. if isint64(p^.resulttype) then
  745. begin
  746. gettempofsizereference(href,8);
  747. p^.temp_offset:=href.offset;
  748. href.offset:=href.offset+4;
  749. exprasmlist^.concat(new(pai386,op_reg(A_MOV,S_L,p^.location.registerhigh,href)));
  750. href.offset:=href.offset-4;
  751. ungetregister32(p^.location.registerhigh);
  752. end
  753. else
  754. begin
  755. gettempofsizereference(href,4);
  756. p^.temp_offset:=href.offset;
  757. end;
  758. pushed:=true;
  759. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,p^.location.register,href)));
  760. ungetregister32(p^.location.register);
  761. end
  762. else if (p^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  763. ((p^.location.reference.base<>R_NO) or
  764. (p^.location.reference.index<>R_NO)
  765. ) then
  766. begin
  767. del_reference(p^.location.reference);
  768. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(p^.location.reference),
  769. R_EDI)));
  770. gettempofsizereference(href,4);
  771. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,href)));
  772. p^.temp_offset:=href.offset;
  773. pushed:=true;
  774. end
  775. else pushed:=false;
  776. end
  777. else pushed:=false;
  778. maybe_push:=pushed;
  779. end;
  780. {$endif TEMPS_NOT_PUSH}
  781. procedure push_int(l : longint);
  782. begin
  783. if (l = 0) and
  784. not(aktoptprocessor in [Class386, ClassP6]) and
  785. not(cs_littlesize in aktglobalswitches)
  786. Then
  787. begin
  788. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_EDI,R_EDI)));
  789. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  790. end
  791. else
  792. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,l)));
  793. end;
  794. procedure emit_push_mem(const ref : treference);
  795. begin
  796. if ref.is_immediate then
  797. push_int(ref.offset)
  798. else
  799. begin
  800. if not(aktoptprocessor in [Class386, ClassP6]) and
  801. not(cs_littlesize in aktglobalswitches)
  802. then
  803. begin
  804. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(ref),R_EDI)));
  805. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  806. end
  807. else exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(ref))));
  808. end;
  809. end;
  810. procedure emitpushreferenceaddr(const ref : treference);
  811. var
  812. href : treference;
  813. begin
  814. { this will fail for references to other segments !!! }
  815. if ref.is_immediate then
  816. { is this right ? }
  817. begin
  818. { push_int(ref.offset)}
  819. gettempofsizereference(4,href);
  820. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,ref.offset,newreference(href))));
  821. emitpushreferenceaddr(href);
  822. del_reference(href);
  823. end
  824. else
  825. begin
  826. if ref.segment<>R_NO then
  827. CGMessage(cg_e_cant_use_far_pointer_there);
  828. if (ref.base=R_NO) and (ref.index=R_NO) then
  829. exprasmlist^.concat(new(pai386,op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset)))
  830. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  831. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  832. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,ref.index)))
  833. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  834. (ref.offset=0) and (ref.symbol=nil) then
  835. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,ref.base)))
  836. else
  837. begin
  838. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(ref),R_EDI)));
  839. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  840. end;
  841. end;
  842. end;
  843. procedure pushsetelement(p : ptree);
  844. {
  845. copies p a set element on the stack
  846. }
  847. var
  848. hr,hr16,hr32 : tregister;
  849. begin
  850. { copy the element on the stack, slightly complicated }
  851. if p^.treetype=ordconstn then
  852. begin
  853. if target_os.stackalignment=4 then
  854. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,p^.value)))
  855. else
  856. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_W,p^.value)));
  857. end
  858. else
  859. begin
  860. case p^.location.loc of
  861. LOC_REGISTER,
  862. LOC_CREGISTER :
  863. begin
  864. hr:=p^.location.register;
  865. case hr of
  866. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  867. begin
  868. hr16:=reg32toreg16(hr);
  869. hr32:=hr;
  870. end;
  871. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  872. begin
  873. hr16:=hr;
  874. hr32:=reg16toreg32(hr);
  875. end;
  876. R_AL,R_BL,R_CL,R_DL :
  877. begin
  878. hr16:=reg8toreg16(hr);
  879. hr32:=reg8toreg32(hr);
  880. end;
  881. end;
  882. if target_os.stackalignment=4 then
  883. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,hr32)))
  884. else
  885. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,hr16)));
  886. ungetregister32(hr32);
  887. end;
  888. else
  889. begin
  890. if target_os.stackalignment=4 then
  891. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(p^.location.reference))))
  892. else
  893. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_W,newreference(p^.location.reference))));
  894. del_reference(p^.location.reference);
  895. end;
  896. end;
  897. end;
  898. end;
  899. procedure restore(p : ptree;isint64 : boolean);
  900. var
  901. hregister : tregister;
  902. {$ifdef TEMPS_NOT_PUSH}
  903. href : treference;
  904. {$endif TEMPS_NOT_PUSH}
  905. begin
  906. hregister:=getregister32;
  907. {$ifdef TEMPS_NOT_PUSH}
  908. reset_reference(href);
  909. href.base:=procinfo.frame_pointer;
  910. href.offset:=p^.temp_offset;
  911. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,href,hregister)));
  912. {$else TEMPS_NOT_PUSH}
  913. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,hregister)));
  914. {$endif TEMPS_NOT_PUSH}
  915. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  916. begin
  917. p^.location.register:=hregister;
  918. if isint64 then
  919. begin
  920. p^.location.registerhigh:=getregister32;
  921. {$ifdef TEMPS_NOT_PUSH}
  922. href.offset:=p^.temp_offset+4;
  923. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,p^.location.registerhigh)));
  924. { set correctly for release ! }
  925. href.offset:=p^.temp_offset;
  926. {$else TEMPS_NOT_PUSH}
  927. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,p^.location.registerhigh)));
  928. {$endif TEMPS_NOT_PUSH}
  929. end;
  930. end
  931. else
  932. begin
  933. reset_reference(p^.location.reference);
  934. p^.location.reference.index:=hregister;
  935. set_location(p^.left^.location,p^.location);
  936. end;
  937. {$ifdef TEMPS_NOT_PUSH}
  938. ungetiftemp(href);
  939. {$endif TEMPS_NOT_PUSH}
  940. end;
  941. {$ifdef TEMPS_NOT_PUSH}
  942. procedure restorefromtemp(p : ptree;isint64 : boolean);
  943. var
  944. hregister : tregister;
  945. href : treference;
  946. begin
  947. hregister:=getregister32;
  948. reset_reference(href);
  949. href.base:=procinfo.frame_pointer;
  950. href.offset:=p^.temp_offset;
  951. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,href,hregister)));
  952. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  953. begin
  954. p^.location.register:=hregister;
  955. if isint64 then
  956. begin
  957. p^.location.registerhigh:=getregister32;
  958. href.offset:=p^.temp_offset+4;
  959. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,p^.location.registerhigh)));
  960. { set correctly for release ! }
  961. href.offset:=p^.temp_offset;
  962. end;
  963. end
  964. else
  965. begin
  966. reset_reference(p^.location.reference);
  967. p^.location.reference.index:=hregister;
  968. set_location(p^.left^.location,p^.location);
  969. end;
  970. ungetiftemp(href);
  971. end;
  972. {$endif TEMPS_NOT_PUSH}
  973. procedure push_value_para(p:ptree;inlined:boolean;para_offset:longint;alignment : longint);
  974. var
  975. tempreference : treference;
  976. r : preference;
  977. opsize : topsize;
  978. op : tasmop;
  979. hreg : tregister;
  980. size : longint;
  981. hlabel : pasmlabel;
  982. begin
  983. case p^.location.loc of
  984. LOC_REGISTER,
  985. LOC_CREGISTER:
  986. begin
  987. case p^.location.register of
  988. R_EAX,R_EBX,R_ECX,R_EDX,R_ESI,
  989. R_EDI,R_ESP,R_EBP :
  990. begin
  991. if p^.resulttype^.size=8 then
  992. begin
  993. inc(pushedparasize,8);
  994. if inlined then
  995. begin
  996. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  997. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  998. p^.location.registerlow,r)));
  999. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize+4);
  1000. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1001. p^.location.registerhigh,r)));
  1002. end
  1003. else
  1004. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.registerhigh)));
  1005. ungetregister32(p^.location.registerhigh);
  1006. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.registerlow)));
  1007. ungetregister32(p^.location.registerlow);
  1008. end
  1009. else
  1010. begin
  1011. inc(pushedparasize,4);
  1012. if inlined then
  1013. begin
  1014. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1015. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  1016. p^.location.register,r)));
  1017. end
  1018. else
  1019. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.register)));
  1020. ungetregister32(p^.location.register);
  1021. end;
  1022. end;
  1023. R_AX,R_BX,R_CX,R_DX,R_SI,R_DI:
  1024. begin
  1025. if alignment=4 then
  1026. begin
  1027. opsize:=S_L;
  1028. hreg:=reg16toreg32(p^.location.register);
  1029. inc(pushedparasize,4);
  1030. end
  1031. else
  1032. begin
  1033. opsize:=S_W;
  1034. hreg:=p^.location.register;
  1035. inc(pushedparasize,2);
  1036. end;
  1037. if inlined then
  1038. begin
  1039. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1040. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1041. end
  1042. else
  1043. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,opsize,hreg)));
  1044. ungetregister32(reg16toreg32(p^.location.register));
  1045. end;
  1046. R_AL,R_BL,R_CL,R_DL:
  1047. begin
  1048. if alignment=4 then
  1049. begin
  1050. opsize:=S_L;
  1051. hreg:=reg8toreg32(p^.location.register);
  1052. inc(pushedparasize,4);
  1053. end
  1054. else
  1055. begin
  1056. opsize:=S_W;
  1057. hreg:=reg8toreg16(p^.location.register);
  1058. inc(pushedparasize,2);
  1059. end;
  1060. { we must push always 16 bit }
  1061. if inlined then
  1062. begin
  1063. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1064. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1065. end
  1066. else
  1067. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,opsize,hreg)));
  1068. ungetregister32(reg8toreg32(p^.location.register));
  1069. end;
  1070. else internalerror(1899);
  1071. end;
  1072. end;
  1073. LOC_FPU:
  1074. begin
  1075. size:=align(pfloatdef(p^.resulttype)^.size,alignment);
  1076. inc(pushedparasize,size);
  1077. if not inlined then
  1078. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,size,R_ESP)));
  1079. {$ifdef GDB}
  1080. if (cs_debuginfo in aktmoduleswitches) and
  1081. (exprasmlist^.first=exprasmlist^.last) then
  1082. exprasmlist^.concat(new(pai_force_line,init));
  1083. {$endif GDB}
  1084. r:=new_reference(R_ESP,0);
  1085. floatstoreops(pfloatdef(p^.resulttype)^.typ,op,opsize);
  1086. { this is the easiest case for inlined !! }
  1087. if inlined then
  1088. begin
  1089. r^.base:=procinfo.framepointer;
  1090. r^.offset:=para_offset-pushedparasize;
  1091. end;
  1092. exprasmlist^.concat(new(pai386,op_ref(op,opsize,r)));
  1093. dec(fpuvaroffset);
  1094. end;
  1095. LOC_CFPUREGISTER:
  1096. begin
  1097. exprasmlist^.concat(new(pai386,op_reg(A_FLD,S_NO,
  1098. correct_fpuregister(p^.location.register,fpuvaroffset))));
  1099. size:=align(pfloatdef(p^.resulttype)^.size,alignment);
  1100. inc(pushedparasize,size);
  1101. if not inlined then
  1102. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,size,R_ESP)));
  1103. {$ifdef GDB}
  1104. if (cs_debuginfo in aktmoduleswitches) and
  1105. (exprasmlist^.first=exprasmlist^.last) then
  1106. exprasmlist^.concat(new(pai_force_line,init));
  1107. {$endif GDB}
  1108. r:=new_reference(R_ESP,0);
  1109. floatstoreops(pfloatdef(p^.resulttype)^.typ,op,opsize);
  1110. { this is the easiest case for inlined !! }
  1111. if inlined then
  1112. begin
  1113. r^.base:=procinfo.framepointer;
  1114. r^.offset:=para_offset-pushedparasize;
  1115. end;
  1116. exprasmlist^.concat(new(pai386,op_ref(op,opsize,r)));
  1117. end;
  1118. LOC_REFERENCE,LOC_MEM:
  1119. begin
  1120. tempreference:=p^.location.reference;
  1121. del_reference(p^.location.reference);
  1122. case p^.resulttype^.deftype of
  1123. enumdef,
  1124. orddef :
  1125. begin
  1126. case p^.resulttype^.size of
  1127. 8 : begin
  1128. inc(pushedparasize,8);
  1129. if inlined then
  1130. begin
  1131. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1132. newreference(tempreference),R_EDI)));
  1133. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1134. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1135. inc(tempreference.offset,4);
  1136. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1137. newreference(tempreference),R_EDI)));
  1138. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize+4);
  1139. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1140. end
  1141. else
  1142. begin
  1143. inc(tempreference.offset,4);
  1144. emit_push_mem(tempreference);
  1145. dec(tempreference.offset,4);
  1146. emit_push_mem(tempreference);
  1147. end;
  1148. end;
  1149. 4 : begin
  1150. inc(pushedparasize,4);
  1151. if inlined then
  1152. begin
  1153. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1154. newreference(tempreference),R_EDI)));
  1155. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1156. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1157. end
  1158. else
  1159. emit_push_mem(tempreference);
  1160. end;
  1161. 1,2 : begin
  1162. if alignment=4 then
  1163. begin
  1164. opsize:=S_L;
  1165. hreg:=R_EDI;
  1166. inc(pushedparasize,4);
  1167. end
  1168. else
  1169. begin
  1170. opsize:=S_W;
  1171. hreg:=R_DI;
  1172. inc(pushedparasize,2);
  1173. end;
  1174. if inlined then
  1175. begin
  1176. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
  1177. newreference(tempreference),hreg)));
  1178. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1179. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1180. end
  1181. else
  1182. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,opsize,
  1183. newreference(tempreference))));
  1184. end;
  1185. else
  1186. internalerror(234231);
  1187. end;
  1188. end;
  1189. floatdef :
  1190. begin
  1191. case pfloatdef(p^.resulttype)^.typ of
  1192. f32bit,
  1193. s32real :
  1194. begin
  1195. inc(pushedparasize,4);
  1196. if inlined then
  1197. begin
  1198. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1199. newreference(tempreference),R_EDI)));
  1200. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1201. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1202. end
  1203. else
  1204. emit_push_mem(tempreference);
  1205. end;
  1206. s64real,
  1207. s64comp :
  1208. begin
  1209. inc(pushedparasize,4);
  1210. inc(tempreference.offset,4);
  1211. if inlined then
  1212. begin
  1213. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1214. newreference(tempreference),R_EDI)));
  1215. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1216. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1217. end
  1218. else
  1219. emit_push_mem(tempreference);
  1220. inc(pushedparasize,4);
  1221. dec(tempreference.offset,4);
  1222. if inlined then
  1223. begin
  1224. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1225. newreference(tempreference),R_EDI)));
  1226. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1227. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1228. end
  1229. else
  1230. emit_push_mem(tempreference);
  1231. end;
  1232. s80real :
  1233. begin
  1234. inc(pushedparasize,4);
  1235. if alignment=4 then
  1236. inc(tempreference.offset,8)
  1237. else
  1238. inc(tempreference.offset,6);
  1239. if inlined then
  1240. begin
  1241. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1242. newreference(tempreference),R_EDI)));
  1243. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1244. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1245. end
  1246. else
  1247. emit_push_mem(tempreference);
  1248. dec(tempreference.offset,4);
  1249. inc(pushedparasize,4);
  1250. if inlined then
  1251. begin
  1252. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1253. newreference(tempreference),R_EDI)));
  1254. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1255. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1256. end
  1257. else
  1258. emit_push_mem(tempreference);
  1259. if alignment=4 then
  1260. begin
  1261. opsize:=S_L;
  1262. hreg:=R_EDI;
  1263. inc(pushedparasize,4);
  1264. dec(tempreference.offset,4);
  1265. end
  1266. else
  1267. begin
  1268. opsize:=S_W;
  1269. hreg:=R_DI;
  1270. inc(pushedparasize,2);
  1271. dec(tempreference.offset,2);
  1272. end;
  1273. if inlined then
  1274. begin
  1275. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
  1276. newreference(tempreference),hreg)));
  1277. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1278. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1279. end
  1280. else
  1281. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,opsize,
  1282. newreference(tempreference))));
  1283. end;
  1284. end;
  1285. end;
  1286. pointerdef,
  1287. procvardef,
  1288. classrefdef:
  1289. begin
  1290. inc(pushedparasize,4);
  1291. if inlined then
  1292. begin
  1293. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1294. newreference(tempreference),R_EDI)));
  1295. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1296. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1297. end
  1298. else
  1299. emit_push_mem(tempreference);
  1300. end;
  1301. arraydef,
  1302. recorddef,
  1303. stringdef,
  1304. setdef,
  1305. objectdef :
  1306. begin
  1307. { even some structured types are 32 bit }
  1308. if is_widestring(p^.resulttype) or
  1309. is_ansistring(p^.resulttype) or
  1310. is_smallset(p^.resulttype) or
  1311. ((p^.resulttype^.deftype in [recorddef,arraydef]) and (p^.resulttype^.size<=4)
  1312. and ((p^.resulttype^.deftype<>arraydef) or not
  1313. (parraydef(p^.resulttype)^.IsConstructor or
  1314. parraydef(p^.resulttype)^.isArrayOfConst or
  1315. is_open_array(p^.resulttype)))
  1316. ) or
  1317. ((p^.resulttype^.deftype=objectdef) and
  1318. pobjectdef(p^.resulttype)^.is_class) then
  1319. begin
  1320. inc(pushedparasize,4);
  1321. if inlined then
  1322. begin
  1323. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1324. concatcopy(tempreference,r^,4,false,false);
  1325. end
  1326. else
  1327. emit_push_mem(tempreference);
  1328. end
  1329. { call by value open array ? }
  1330. else
  1331. internalerror(8954);
  1332. end;
  1333. else
  1334. CGMessage(cg_e_illegal_expression);
  1335. end;
  1336. end;
  1337. LOC_JUMP:
  1338. begin
  1339. getlabel(hlabel);
  1340. if alignment=4 then
  1341. begin
  1342. opsize:=S_L;
  1343. inc(pushedparasize,4);
  1344. end
  1345. else
  1346. begin
  1347. opsize:=S_W;
  1348. inc(pushedparasize,2);
  1349. end;
  1350. emitlab(truelabel);
  1351. if inlined then
  1352. begin
  1353. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1354. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,opsize,1,r)));
  1355. end
  1356. else
  1357. exprasmlist^.concat(new(pai386,op_const(A_PUSH,opsize,1)));
  1358. emitjmp(C_None,hlabel);
  1359. emitlab(falselabel);
  1360. if inlined then
  1361. begin
  1362. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1363. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,opsize,0,r)));
  1364. end
  1365. else
  1366. exprasmlist^.concat(new(pai386,op_const(A_PUSH,opsize,0)));
  1367. emitlab(hlabel);
  1368. end;
  1369. LOC_FLAGS:
  1370. begin
  1371. if not(R_EAX in unused) then
  1372. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EAX,R_EDI)));
  1373. emit_flag2reg(p^.location.resflags,R_AL);
  1374. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BW,R_AL,R_AX)));
  1375. if alignment=4 then
  1376. begin
  1377. opsize:=S_L;
  1378. hreg:=R_EAX;
  1379. inc(pushedparasize,4);
  1380. end
  1381. else
  1382. begin
  1383. opsize:=S_W;
  1384. hreg:=R_AX;
  1385. inc(pushedparasize,2);
  1386. end;
  1387. if inlined then
  1388. begin
  1389. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1390. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1391. end
  1392. else
  1393. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,opsize,hreg)));
  1394. if not(R_EAX in unused) then
  1395. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EDI,R_EAX)));
  1396. end;
  1397. {$ifdef SUPPORT_MMX}
  1398. LOC_MMXREGISTER,
  1399. LOC_CMMXREGISTER:
  1400. begin
  1401. inc(pushedparasize,8); { was missing !!! (PM) }
  1402. exprasmlist^.concat(new(pai386,op_const_reg(
  1403. A_SUB,S_L,8,R_ESP)));
  1404. {$ifdef GDB}
  1405. if (cs_debuginfo in aktmoduleswitches) and
  1406. (exprasmlist^.first=exprasmlist^.last) then
  1407. exprasmlist^.concat(new(pai_force_line,init));
  1408. {$endif GDB}
  1409. if inlined then
  1410. begin
  1411. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1412. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOVQ,S_NO,
  1413. p^.location.register,r)));
  1414. end
  1415. else
  1416. begin
  1417. r:=new_reference(R_ESP,0);
  1418. exprasmlist^.concat(new(pai386,op_reg_ref(
  1419. A_MOVQ,S_NO,p^.location.register,r)));
  1420. end;
  1421. end;
  1422. {$endif SUPPORT_MMX}
  1423. end;
  1424. end;
  1425. {*****************************************************************************
  1426. Emit Float Functions
  1427. *****************************************************************************}
  1428. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  1429. begin
  1430. case t of
  1431. s32real : begin
  1432. op:=A_FLD;
  1433. s:=S_FS;
  1434. end;
  1435. s64real : begin
  1436. op:=A_FLD;
  1437. { ???? }
  1438. s:=S_FL;
  1439. end;
  1440. s80real : begin
  1441. op:=A_FLD;
  1442. s:=S_FX;
  1443. end;
  1444. s64comp : begin
  1445. op:=A_FILD;
  1446. s:=S_IQ;
  1447. end;
  1448. else internalerror(17);
  1449. end;
  1450. end;
  1451. procedure floatload(t : tfloattype;const ref : treference);
  1452. var
  1453. op : tasmop;
  1454. s : topsize;
  1455. begin
  1456. floatloadops(t,op,s);
  1457. exprasmlist^.concat(new(pai386,op_ref(op,s,
  1458. newreference(ref))));
  1459. inc(fpuvaroffset);
  1460. end;
  1461. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  1462. begin
  1463. case t of
  1464. s32real : begin
  1465. op:=A_FSTP;
  1466. s:=S_FS;
  1467. end;
  1468. s64real : begin
  1469. op:=A_FSTP;
  1470. s:=S_FL;
  1471. end;
  1472. s80real : begin
  1473. op:=A_FSTP;
  1474. s:=S_FX;
  1475. end;
  1476. s64comp : begin
  1477. op:=A_FISTP;
  1478. s:=S_IQ;
  1479. end;
  1480. else
  1481. internalerror(17);
  1482. end;
  1483. end;
  1484. procedure floatstore(t : tfloattype;const ref : treference);
  1485. var
  1486. op : tasmop;
  1487. s : topsize;
  1488. begin
  1489. floatstoreops(t,op,s);
  1490. exprasmlist^.concat(new(pai386,op_ref(op,s,
  1491. newreference(ref))));
  1492. dec(fpuvaroffset);
  1493. end;
  1494. {*****************************************************************************
  1495. Emit Functions
  1496. *****************************************************************************}
  1497. procedure maketojumpbool(p : ptree);
  1498. {
  1499. produces jumps to true respectively false labels using boolean expressions
  1500. }
  1501. var
  1502. opsize : topsize;
  1503. storepos : tfileposinfo;
  1504. begin
  1505. if p^.error then
  1506. exit;
  1507. storepos:=aktfilepos;
  1508. aktfilepos:=p^.fileinfo;
  1509. if is_boolean(p^.resulttype) then
  1510. begin
  1511. if is_constboolnode(p) then
  1512. begin
  1513. if p^.value<>0 then
  1514. emitjmp(C_None,truelabel)
  1515. else
  1516. emitjmp(C_None,falselabel);
  1517. end
  1518. else
  1519. begin
  1520. opsize:=def_opsize(p^.resulttype);
  1521. case p^.location.loc of
  1522. LOC_CREGISTER,LOC_REGISTER : begin
  1523. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,opsize,p^.location.register,
  1524. p^.location.register)));
  1525. ungetregister(p^.location.register);
  1526. emitjmp(C_NZ,truelabel);
  1527. emitjmp(C_None,falselabel);
  1528. end;
  1529. LOC_MEM,LOC_REFERENCE : begin
  1530. exprasmlist^.concat(new(pai386,op_const_ref(
  1531. A_CMP,opsize,0,newreference(p^.location.reference))));
  1532. del_reference(p^.location.reference);
  1533. emitjmp(C_NZ,truelabel);
  1534. emitjmp(C_None,falselabel);
  1535. end;
  1536. LOC_FLAGS : begin
  1537. emitjmp(flag_2_cond[p^.location.resflags],truelabel);
  1538. emitjmp(C_None,falselabel);
  1539. end;
  1540. end;
  1541. end;
  1542. end
  1543. else
  1544. CGMessage(type_e_mismatch);
  1545. aktfilepos:=storepos;
  1546. end;
  1547. { produces if necessary overflowcode }
  1548. procedure emitoverflowcheck(p:ptree);
  1549. var
  1550. hl : pasmlabel;
  1551. begin
  1552. if not(cs_check_overflow in aktlocalswitches) then
  1553. exit;
  1554. getlabel(hl);
  1555. if not ((p^.resulttype^.deftype=pointerdef) or
  1556. ((p^.resulttype^.deftype=orddef) and
  1557. (porddef(p^.resulttype)^.typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1558. bool8bit,bool16bit,bool32bit]))) then
  1559. emitjmp(C_NO,hl)
  1560. else
  1561. emitjmp(C_NB,hl);
  1562. emitcall('FPC_OVERFLOW');
  1563. emitlab(hl);
  1564. end;
  1565. { produces range check code, while one of the operands is a 64 bit
  1566. integer }
  1567. procedure emitrangecheck64(p : ptree;todef : pdef);
  1568. begin
  1569. internalerror(28699);
  1570. end;
  1571. { produces if necessary rangecheckcode }
  1572. procedure emitrangecheck(p:ptree;todef:pdef);
  1573. {
  1574. generate range checking code for the value at location t. The
  1575. type used is the checked against todefs ranges. fromdef (p.resulttype)
  1576. is the original type used at that location, when both defs are
  1577. equal the check is also insert (needed for succ,pref,inc,dec)
  1578. }
  1579. var
  1580. neglabel,
  1581. poslabel : pasmlabel;
  1582. href : treference;
  1583. rstr : string;
  1584. hreg : tregister;
  1585. opsize : topsize;
  1586. op : tasmop;
  1587. fromdef : pdef;
  1588. lto,hto,
  1589. lfrom,hfrom : longint;
  1590. doublebound,
  1591. is_reg,
  1592. popecx : boolean;
  1593. begin
  1594. { range checking on and range checkable value? }
  1595. if not(cs_check_range in aktlocalswitches) or
  1596. not(todef^.deftype in [orddef,enumdef,arraydef]) then
  1597. exit;
  1598. { only check when assigning to scalar, subranges are different,
  1599. when todef=fromdef then the check is always generated }
  1600. fromdef:=p^.resulttype;
  1601. if is_64bitint(fromdef) or is_64bitint(todef) then
  1602. begin
  1603. emitrangecheck64(p,todef);
  1604. exit;
  1605. end;
  1606. {we also need lto and hto when checking if we need to use doublebound!
  1607. (JM)}
  1608. getrange(todef,lto,hto);
  1609. if todef<>fromdef then
  1610. begin
  1611. getrange(p^.resulttype,lfrom,hfrom);
  1612. { first check for not being u32bit, then if the to is bigger than
  1613. from }
  1614. if (lto<hto) and (lfrom<hfrom) and
  1615. (lto<=lfrom) and (hto>=hfrom) then
  1616. exit;
  1617. end;
  1618. { generate the rangecheck code for the def where we are going to
  1619. store the result }
  1620. doublebound:=false;
  1621. case todef^.deftype of
  1622. orddef :
  1623. begin
  1624. porddef(todef)^.genrangecheck;
  1625. rstr:=porddef(todef)^.getrangecheckstring;
  1626. doublebound:=(porddef(todef)^.typ=u32bit) and (lto>hto);
  1627. end;
  1628. enumdef :
  1629. begin
  1630. penumdef(todef)^.genrangecheck;
  1631. rstr:=penumdef(todef)^.getrangecheckstring;
  1632. end;
  1633. arraydef :
  1634. begin
  1635. parraydef(todef)^.genrangecheck;
  1636. rstr:=parraydef(todef)^.getrangecheckstring;
  1637. end;
  1638. end;
  1639. { get op and opsize }
  1640. opsize:=def2def_opsize(fromdef,u32bitdef);
  1641. if opsize in [S_B,S_W,S_L] then
  1642. op:=A_MOV
  1643. else
  1644. if is_signed(fromdef) then
  1645. op:=A_MOVSX
  1646. else
  1647. op:=A_MOVZX;
  1648. is_reg:=(p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]);
  1649. if is_reg then
  1650. hreg:=p^.location.register;
  1651. if not target_os.use_bound_instruction then
  1652. begin
  1653. { FPC_BOUNDCHECK needs to be called with
  1654. %ecx - value
  1655. %edi - pointer to the ranges }
  1656. popecx:=false;
  1657. if not(is_reg) or
  1658. (p^.location.register<>R_ECX) then
  1659. begin
  1660. if not(R_ECX in unused) then
  1661. begin
  1662. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  1663. popecx:=true;
  1664. end;
  1665. if is_reg then
  1666. exprasmlist^.concat(new(pai386,op_reg_reg(op,opsize,p^.location.register,R_ECX)))
  1667. else
  1668. exprasmlist^.concat(new(pai386,op_ref_reg(op,opsize,newreference(p^.location.reference),R_ECX)));
  1669. end;
  1670. if doublebound then
  1671. begin
  1672. getlabel(neglabel);
  1673. getlabel(poslabel);
  1674. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,R_ECX,R_ECX)));
  1675. emitjmp(C_L,neglabel);
  1676. end;
  1677. { insert bound instruction only }
  1678. exprasmlist^.concat(new(pai386,op_sym_ofs_reg(A_MOV,S_L,newasmsymbol(rstr),0,R_EDI)));
  1679. emitcall('FPC_BOUNDCHECK');
  1680. { u32bit needs 2 checks }
  1681. if doublebound then
  1682. begin
  1683. emitjmp(C_None,poslabel);
  1684. emitlab(neglabel);
  1685. exprasmlist^.concat(new(pai386,op_sym_ofs_reg(A_MOV,S_L,newasmsymbol(rstr),8,R_EDI)));
  1686. emitcall('FPC_BOUNDCHECK');
  1687. emitlab(poslabel);
  1688. end;
  1689. if popecx then
  1690. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  1691. end
  1692. else
  1693. begin
  1694. reset_reference(href);
  1695. href.symbol:=newasmsymbol(rstr);
  1696. { load the value in a register }
  1697. if is_reg then
  1698. begin
  1699. { be sure that hreg is a 32 bit reg, if not load it in %edi }
  1700. if p^.location.register in [R_EAX..R_EDI] then
  1701. hreg:=p^.location.register
  1702. else
  1703. begin
  1704. exprasmlist^.concat(new(pai386,op_reg_reg(op,opsize,p^.location.register,R_EDI)));
  1705. hreg:=R_EDI;
  1706. end;
  1707. end
  1708. else
  1709. begin
  1710. exprasmlist^.concat(new(pai386,op_ref_reg(op,opsize,newreference(p^.location.reference),R_EDI)));
  1711. hreg:=R_EDI;
  1712. end;
  1713. if doublebound then
  1714. begin
  1715. getlabel(neglabel);
  1716. getlabel(poslabel);
  1717. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,hreg,hreg)));
  1718. emitjmp(C_L,neglabel);
  1719. end;
  1720. { insert bound instruction only }
  1721. exprasmlist^.concat(new(pai386,op_reg_ref(A_BOUND,S_L,hreg,newreference(href))));
  1722. { u32bit needs 2 checks }
  1723. if doublebound then
  1724. begin
  1725. href.offset:=8;
  1726. emitjmp(C_None,poslabel);
  1727. emitlab(neglabel);
  1728. exprasmlist^.concat(new(pai386,op_reg_ref(A_BOUND,S_L,hreg,newreference(href))));
  1729. emitlab(poslabel);
  1730. end;
  1731. end;
  1732. end;
  1733. procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
  1734. const
  1735. isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
  1736. ishr : array[0..3] of byte=(2,0,1,0);
  1737. var
  1738. ecxpushed : boolean;
  1739. helpsize : longint;
  1740. i : byte;
  1741. reg8,reg32 : tregister;
  1742. swap : boolean;
  1743. procedure maybepushecx;
  1744. begin
  1745. if not(R_ECX in unused) then
  1746. begin
  1747. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  1748. ecxpushed:=true;
  1749. end;
  1750. end;
  1751. begin
  1752. {$IfNDef regallocfix}
  1753. If delsource then
  1754. del_reference(source);
  1755. {$EndIf regallocfix}
  1756. if (not loadref) and
  1757. ((size<=8) or
  1758. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  1759. begin
  1760. helpsize:=size shr 2;
  1761. for i:=1 to helpsize do
  1762. begin
  1763. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(source),R_EDI)));
  1764. {$ifdef regallocfix}
  1765. If (size = 4) and delsource then
  1766. del_reference(source);
  1767. {$endif regallocfix}
  1768. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest))));
  1769. inc(source.offset,4);
  1770. inc(dest.offset,4);
  1771. dec(size,4);
  1772. end;
  1773. if size>1 then
  1774. begin
  1775. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(source),R_DI)));
  1776. {$ifdef regallocfix}
  1777. If (size = 2) and delsource then
  1778. del_reference(source);
  1779. {$endif regallocfix}
  1780. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,R_DI,newreference(dest))));
  1781. inc(source.offset,2);
  1782. inc(dest.offset,2);
  1783. dec(size,2);
  1784. end;
  1785. if size>0 then
  1786. begin
  1787. { and now look for an 8 bit register }
  1788. swap:=false;
  1789. if R_EAX in unused then reg8:=R_AL
  1790. else if R_EBX in unused then reg8:=R_BL
  1791. else if R_ECX in unused then reg8:=R_CL
  1792. else if R_EDX in unused then reg8:=R_DL
  1793. else
  1794. begin
  1795. swap:=true;
  1796. { we need only to check 3 registers, because }
  1797. { one is always not index or base }
  1798. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  1799. begin
  1800. reg8:=R_AL;
  1801. reg32:=R_EAX;
  1802. end
  1803. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  1804. begin
  1805. reg8:=R_BL;
  1806. reg32:=R_EBX;
  1807. end
  1808. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  1809. begin
  1810. reg8:=R_CL;
  1811. reg32:=R_ECX;
  1812. end;
  1813. end;
  1814. if swap then
  1815. { was earlier XCHG, of course nonsense }
  1816. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  1817. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_B,newreference(source),reg8)));
  1818. {$ifdef regallocfix}
  1819. If delsource then
  1820. del_reference(source);
  1821. {$endif regallocfix}
  1822. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_B,reg8,newreference(dest))));
  1823. if swap then
  1824. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  1825. end;
  1826. end
  1827. else
  1828. begin
  1829. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(dest),R_EDI)));
  1830. {$ifdef regallocfix}
  1831. {is this ok?? (JM)}
  1832. del_reference(dest);
  1833. {$endif regallocfix}
  1834. if loadref then
  1835. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(source),R_ESI)))
  1836. else
  1837. begin
  1838. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(source),R_ESI)));
  1839. {$ifdef regallocfix}
  1840. if delsource then
  1841. del_reference(source);
  1842. {$endif regallocfix}
  1843. end;
  1844. exprasmlist^.concat(new(pai386,op_none(A_CLD,S_NO)));
  1845. ecxpushed:=false;
  1846. if cs_littlesize in aktglobalswitches then
  1847. begin
  1848. maybepushecx;
  1849. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,size,R_ECX)));
  1850. exprasmlist^.concat(new(pai386,op_none(A_REP,S_NO)));
  1851. exprasmlist^.concat(new(pai386,op_none(A_MOVSB,S_NO)));
  1852. end
  1853. else
  1854. begin
  1855. helpsize:=size shr 2;
  1856. size:=size and 3;
  1857. if helpsize>1 then
  1858. begin
  1859. maybepushecx;
  1860. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,helpsize,R_ECX)));
  1861. exprasmlist^.concat(new(pai386,op_none(A_REP,S_NO)));
  1862. end;
  1863. if helpsize>0 then
  1864. exprasmlist^.concat(new(pai386,op_none(A_MOVSD,S_NO)));
  1865. if size>1 then
  1866. begin
  1867. dec(size,2);
  1868. exprasmlist^.concat(new(pai386,op_none(A_MOVSW,S_NO)));
  1869. end;
  1870. if size=1 then
  1871. exprasmlist^.concat(new(pai386,op_none(A_MOVSB,S_NO)));
  1872. end;
  1873. if ecxpushed then
  1874. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  1875. { loading SELF-reference again }
  1876. maybe_loadesi;
  1877. end;
  1878. if delsource then
  1879. ungetiftemp(source);
  1880. end;
  1881. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;
  1882. destreg:Tregister;delloc:boolean);
  1883. {A lot smaller and less bug sensitive than the original unfolded loads.}
  1884. var tai:Pai386;
  1885. r:Preference;
  1886. begin
  1887. case location.loc of
  1888. LOC_REGISTER,LOC_CREGISTER:
  1889. begin
  1890. case orddef^.typ of
  1891. u8bit:
  1892. tai:=new(pai386,op_reg_reg(A_MOVZX,S_BL,location.register,destreg));
  1893. s8bit:
  1894. tai:=new(pai386,op_reg_reg(A_MOVSX,S_BL,location.register,destreg));
  1895. u16bit:
  1896. tai:=new(pai386,op_reg_reg(A_MOVZX,S_WL,location.register,destreg));
  1897. s16bit:
  1898. tai:=new(pai386,op_reg_reg(A_MOVSX,S_WL,location.register,destreg));
  1899. u32bit:
  1900. tai:=new(pai386,op_reg_reg(A_MOV,S_L,location.register,destreg));
  1901. s32bit:
  1902. tai:=new(pai386,op_reg_reg(A_MOV,S_L,location.register,destreg));
  1903. end;
  1904. if delloc then
  1905. ungetregister(location.register);
  1906. end;
  1907. LOC_MEM,
  1908. LOC_REFERENCE:
  1909. begin
  1910. if location.reference.is_immediate then
  1911. tai:=new(pai386,op_const_reg(A_MOV,S_L,location.reference.offset,destreg))
  1912. else
  1913. begin
  1914. r:=newreference(location.reference);
  1915. case orddef^.typ of
  1916. u8bit:
  1917. tai:=new(pai386,op_ref_reg(A_MOVZX,S_BL,r,destreg));
  1918. s8bit:
  1919. tai:=new(pai386,op_ref_reg(A_MOVSX,S_BL,r,destreg));
  1920. u16bit:
  1921. tai:=new(pai386,op_ref_reg(A_MOVZX,S_WL,r,destreg));
  1922. s16bit:
  1923. tai:=new(pai386,op_ref_reg(A_MOVSX,S_WL,r,destreg));
  1924. u32bit:
  1925. tai:=new(pai386,op_ref_reg(A_MOV,S_L,r,destreg));
  1926. s32bit:
  1927. tai:=new(pai386,op_ref_reg(A_MOV,S_L,r,destreg));
  1928. end;
  1929. end;
  1930. if delloc then
  1931. del_reference(location.reference);
  1932. end
  1933. else
  1934. internalerror(6);
  1935. end;
  1936. exprasmlist^.concat(tai);
  1937. end;
  1938. { if necessary ESI is reloaded after a call}
  1939. procedure maybe_loadesi;
  1940. var
  1941. hp : preference;
  1942. p : pprocinfo;
  1943. i : longint;
  1944. begin
  1945. if assigned(procinfo._class) then
  1946. begin
  1947. if lexlevel>normal_function_level then
  1948. begin
  1949. new(hp);
  1950. reset_reference(hp^);
  1951. hp^.offset:=procinfo.framepointer_offset;
  1952. hp^.base:=procinfo.framepointer;
  1953. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,R_ESI)));
  1954. p:=procinfo.parent;
  1955. for i:=3 to lexlevel-1 do
  1956. begin
  1957. new(hp);
  1958. reset_reference(hp^);
  1959. hp^.offset:=p^.framepointer_offset;
  1960. hp^.base:=R_ESI;
  1961. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,R_ESI)));
  1962. p:=p^.parent;
  1963. end;
  1964. new(hp);
  1965. reset_reference(hp^);
  1966. hp^.offset:=p^.ESI_offset;
  1967. hp^.base:=R_ESI;
  1968. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,R_ESI)));
  1969. end
  1970. else
  1971. begin
  1972. new(hp);
  1973. reset_reference(hp^);
  1974. hp^.offset:=procinfo.ESI_offset;
  1975. hp^.base:=procinfo.framepointer;
  1976. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,R_ESI)));
  1977. end;
  1978. end;
  1979. end;
  1980. procedure firstcomplex(p : ptree);
  1981. var
  1982. hp : ptree;
  1983. begin
  1984. { always calculate boolean AND and OR from left to right }
  1985. if (p^.treetype in [orn,andn]) and
  1986. (p^.left^.resulttype^.deftype=orddef) and
  1987. (porddef(p^.left^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit]) then
  1988. p^.swaped:=false
  1989. else
  1990. if (p^.left^.registers32<p^.right^.registers32) and
  1991. { the following check is appropriate, because all }
  1992. { 4 registers are rarely used and it is thereby }
  1993. { achieved that the extra code is being dropped }
  1994. { by exchanging not commutative operators }
  1995. (p^.right^.registers32<=4) then
  1996. begin
  1997. hp:=p^.left;
  1998. p^.left:=p^.right;
  1999. p^.right:=hp;
  2000. p^.swaped:=true;
  2001. end
  2002. else
  2003. p^.swaped:=false;
  2004. end;
  2005. {*****************************************************************************
  2006. Entry/Exit Code Functions
  2007. *****************************************************************************}
  2008. procedure genprofilecode;
  2009. var
  2010. pl : pasmlabel;
  2011. begin
  2012. if (po_assembler in aktprocsym^.definition^.procoptions) then
  2013. exit;
  2014. case target_info.target of
  2015. target_i386_linux:
  2016. begin
  2017. getlabel(pl);
  2018. emitcall('mcount');
  2019. exprasmlist^.insert(new(pai386,op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX)));
  2020. exprasmlist^.insert(new(pai_section,init(sec_code)));
  2021. exprasmlist^.insert(new(pai_const,init_32bit(0)));
  2022. exprasmlist^.insert(new(pai_label,init(pl)));
  2023. exprasmlist^.insert(new(pai_align,init(4)));
  2024. exprasmlist^.insert(new(pai_section,init(sec_data)));
  2025. end;
  2026. target_i386_go32v2:
  2027. begin
  2028. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('MCOUNT'))));
  2029. end;
  2030. end;
  2031. end;
  2032. procedure generate_interrupt_stackframe_entry;
  2033. begin
  2034. { save the registers of an interrupt procedure }
  2035. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  2036. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBX)));
  2037. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  2038. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDX)));
  2039. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  2040. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  2041. { .... also the segment registers }
  2042. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_W,R_DS)));
  2043. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_W,R_ES)));
  2044. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_W,R_FS)));
  2045. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_W,R_GS)));
  2046. end;
  2047. procedure generate_interrupt_stackframe_exit;
  2048. begin
  2049. { restore the registers of an interrupt procedure }
  2050. { this was all with entrycode instead of exitcode !!}
  2051. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EAX)));
  2052. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EBX)));
  2053. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  2054. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EDX)));
  2055. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  2056. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  2057. { .... also the segment registers }
  2058. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_DS)));
  2059. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_ES)));
  2060. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_FS)));
  2061. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_GS)));
  2062. { this restores the flags }
  2063. procinfo.aktexitcode^.concat(new(pai386,op_none(A_IRET,S_NO)));
  2064. end;
  2065. { generates the code for threadvar initialisation }
  2066. procedure initialize_threadvar(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2067. var
  2068. hr : treference;
  2069. begin
  2070. if (psym(p)^.typ=varsym) and
  2071. (vo_is_thread_var in pvarsym(p)^.varoptions) then
  2072. begin
  2073. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pvarsym(p)^.getsize)));
  2074. reset_reference(hr);
  2075. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  2076. emitpushreferenceaddr(hr);
  2077. exprasmlist^.concat(new(pai386,
  2078. op_sym(A_CALL,S_NO,newasmsymbol('FPC_INIT_THREADVAR'))));
  2079. end;
  2080. end;
  2081. { initilizes data of type t }
  2082. { if is_already_ref is true then the routines assumes }
  2083. { that r points to the data to initialize }
  2084. procedure initialize(t : pdef;const ref : treference;is_already_ref : boolean);
  2085. var
  2086. hr : treference;
  2087. begin
  2088. if is_ansistring(t) or
  2089. is_widestring(t) then
  2090. begin
  2091. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,0,
  2092. newreference(ref))));
  2093. end
  2094. else
  2095. begin
  2096. reset_reference(hr);
  2097. hr.symbol:=t^.get_inittable_label;
  2098. emitpushreferenceaddr(hr);
  2099. if is_already_ref then
  2100. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  2101. newreference(ref))))
  2102. else
  2103. emitpushreferenceaddr(ref);
  2104. exprasmlist^.concat(new(pai386,
  2105. op_sym(A_CALL,S_NO,newasmsymbol('FPC_INITIALIZE'))));
  2106. end;
  2107. end;
  2108. { finalizes data of type t }
  2109. { if is_already_ref is true then the routines assumes }
  2110. { that r points to the data to finalizes }
  2111. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  2112. var
  2113. r : treference;
  2114. begin
  2115. if is_ansistring(t) or
  2116. is_widestring(t) then
  2117. begin
  2118. decrstringref(t,ref);
  2119. end
  2120. else
  2121. begin
  2122. reset_reference(r);
  2123. r.symbol:=t^.get_inittable_label;
  2124. emitpushreferenceaddr(r);
  2125. if is_already_ref then
  2126. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  2127. newreference(ref))))
  2128. else
  2129. emitpushreferenceaddr(ref);
  2130. emitcall('FPC_FINALIZE');
  2131. end;
  2132. end;
  2133. { generates the code for initialisation of local data }
  2134. procedure initialize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2135. var
  2136. hr : treference;
  2137. begin
  2138. if (psym(p)^.typ=varsym) and
  2139. assigned(pvarsym(p)^.definition) and
  2140. not((pvarsym(p)^.definition^.deftype=objectdef) and
  2141. pobjectdef(pvarsym(p)^.definition)^.is_class) and
  2142. pvarsym(p)^.definition^.needs_inittable then
  2143. begin
  2144. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2145. reset_reference(hr);
  2146. if psym(p)^.owner^.symtabletype=localsymtable then
  2147. begin
  2148. hr.base:=procinfo.framepointer;
  2149. hr.offset:=-pvarsym(p)^.address;
  2150. end
  2151. else
  2152. begin
  2153. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  2154. end;
  2155. initialize(pvarsym(p)^.definition,hr,false);
  2156. end;
  2157. end;
  2158. { generates the code for incrementing the reference count of parameters }
  2159. procedure incr_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2160. var
  2161. hr : treference;
  2162. begin
  2163. if (psym(p)^.typ=varsym) and
  2164. not((pvarsym(p)^.definition^.deftype=objectdef) and
  2165. pobjectdef(pvarsym(p)^.definition)^.is_class) and
  2166. pvarsym(p)^.definition^.needs_inittable and
  2167. ((pvarsym(p)^.varspez=vs_value) {or
  2168. (pvarsym(p)^.varspez=vs_const) and
  2169. not(dont_copy_const_param(pvarsym(p)^.definition))}) then
  2170. begin
  2171. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2172. reset_reference(hr);
  2173. hr.symbol:=pvarsym(p)^.definition^.get_inittable_label;
  2174. emitpushreferenceaddr(hr);
  2175. reset_reference(hr);
  2176. hr.base:=procinfo.framepointer;
  2177. hr.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2178. emitpushreferenceaddr(hr);
  2179. reset_reference(hr);
  2180. exprasmlist^.concat(new(pai386,
  2181. op_sym(A_CALL,S_NO,newasmsymbol('FPC_ADDREF'))));
  2182. end;
  2183. end;
  2184. { generates the code for finalisation of local data }
  2185. procedure finalize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2186. var
  2187. hr : treference;
  2188. begin
  2189. if (psym(p)^.typ=varsym) and
  2190. assigned(pvarsym(p)^.definition) and
  2191. not((pvarsym(p)^.definition^.deftype=objectdef) and
  2192. pobjectdef(pvarsym(p)^.definition)^.is_class) and
  2193. pvarsym(p)^.definition^.needs_inittable then
  2194. begin
  2195. { not all kind of parameters need to be finalized }
  2196. if (psym(p)^.owner^.symtabletype=parasymtable) and
  2197. ((pvarsym(p)^.varspez=vs_var) or
  2198. (pvarsym(p)^.varspez=vs_const) { and
  2199. (dont_copy_const_param(pvarsym(p)^.definition)) } ) then
  2200. exit;
  2201. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2202. reset_reference(hr);
  2203. case psym(p)^.owner^.symtabletype of
  2204. localsymtable:
  2205. begin
  2206. hr.base:=procinfo.framepointer;
  2207. hr.offset:=-pvarsym(p)^.address;
  2208. end;
  2209. parasymtable:
  2210. begin
  2211. hr.base:=procinfo.framepointer;
  2212. hr.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2213. end;
  2214. else
  2215. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  2216. end;
  2217. finalize(pvarsym(p)^.definition,hr,false);
  2218. end;
  2219. end;
  2220. { generates the code to make local copies of the value parameters }
  2221. procedure copyvalueparas(p : pnamedindexobject);{$ifndef fpc}far;{$endif}
  2222. var
  2223. href1,href2 : treference;
  2224. r : preference;
  2225. len : longint;
  2226. opsize : topsize;
  2227. again,ok : pasmlabel;
  2228. begin
  2229. if (psym(p)^.typ=varsym) and
  2230. (pvarsym(p)^.varspez=vs_value) and
  2231. (push_addr_param(pvarsym(p)^.definition)) then
  2232. begin
  2233. if is_open_array(pvarsym(p)^.definition) or
  2234. is_array_of_const(pvarsym(p)^.definition) then
  2235. begin
  2236. { get stack space }
  2237. new(r);
  2238. reset_reference(r^);
  2239. r^.base:=procinfo.framepointer;
  2240. r^.offset:=pvarsym(p)^.address+4+procinfo.call_offset;
  2241. exprasmlist^.concat(new(pai386,
  2242. op_ref_reg(A_MOV,S_L,r,R_EDI)));
  2243. exprasmlist^.concat(new(pai386,
  2244. op_reg(A_INC,S_L,R_EDI)));
  2245. exprasmlist^.concat(new(pai386,
  2246. op_const_reg(A_IMUL,S_L,
  2247. parraydef(pvarsym(p)^.definition)^.definition^.size,R_EDI)));
  2248. { windows guards only a few pages for stack growing, }
  2249. { so we have to access every page first }
  2250. if target_os.id=os_i386_win32 then
  2251. begin
  2252. getlabel(again);
  2253. getlabel(ok);
  2254. emitlab(again);
  2255. exprasmlist^.concat(new(pai386,
  2256. op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI)));
  2257. emitjmp(C_C,ok);
  2258. exprasmlist^.concat(new(pai386,
  2259. op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
  2260. exprasmlist^.concat(new(pai386,
  2261. op_reg(A_PUSH,S_L,R_EAX)));
  2262. exprasmlist^.concat(new(pai386,
  2263. op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI)));
  2264. emitjmp(C_None,again);
  2265. emitlab(ok);
  2266. exprasmlist^.concat(new(pai386,
  2267. op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
  2268. { now reload EDI }
  2269. new(r);
  2270. reset_reference(r^);
  2271. r^.base:=procinfo.framepointer;
  2272. r^.offset:=pvarsym(p)^.address+4+procinfo.call_offset;
  2273. exprasmlist^.concat(new(pai386,
  2274. op_ref_reg(A_MOV,S_L,r,R_EDI)));
  2275. exprasmlist^.concat(new(pai386,
  2276. op_reg(A_INC,S_L,R_EDI)));
  2277. exprasmlist^.concat(new(pai386,
  2278. op_const_reg(A_IMUL,S_L,
  2279. parraydef(pvarsym(p)^.definition)^.definition^.size,R_EDI)));
  2280. end
  2281. else
  2282. begin
  2283. exprasmlist^.concat(new(pai386,
  2284. op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
  2285. { load destination }
  2286. exprasmlist^.concat(new(pai386,
  2287. op_reg_reg(A_MOV,S_L,R_ESP,R_EDI)));
  2288. end;
  2289. { don't destroy the registers! }
  2290. exprasmlist^.concat(new(pai386,
  2291. op_reg(A_PUSH,S_L,R_ECX)));
  2292. exprasmlist^.concat(new(pai386,
  2293. op_reg(A_PUSH,S_L,R_ESI)));
  2294. { load count }
  2295. new(r);
  2296. reset_reference(r^);
  2297. r^.base:=procinfo.framepointer;
  2298. r^.offset:=pvarsym(p)^.address+4+procinfo.call_offset;
  2299. exprasmlist^.concat(new(pai386,
  2300. op_ref_reg(A_MOV,S_L,r,R_ECX)));
  2301. { load source }
  2302. new(r);
  2303. reset_reference(r^);
  2304. r^.base:=procinfo.framepointer;
  2305. r^.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2306. exprasmlist^.concat(new(pai386,
  2307. op_ref_reg(A_MOV,S_L,r,R_ESI)));
  2308. { scheduled .... }
  2309. exprasmlist^.concat(new(pai386,
  2310. op_reg(A_INC,S_L,R_ECX)));
  2311. { calculate size }
  2312. len:=parraydef(pvarsym(p)^.definition)^.definition^.size;
  2313. opsize:=S_B;
  2314. if (len and 3)=0 then
  2315. begin
  2316. opsize:=S_L;
  2317. len:=len shr 2;
  2318. end
  2319. else
  2320. if (len and 1)=0 then
  2321. begin
  2322. opsize:=S_W;
  2323. len:=len shr 1;
  2324. end;
  2325. exprasmlist^.concat(new(pai386,
  2326. op_const_reg(A_IMUL,S_L,len,R_ECX)));
  2327. exprasmlist^.concat(new(pai386,
  2328. op_none(A_REP,S_NO)));
  2329. case opsize of
  2330. S_B : exprasmlist^.concat(new(pai386,op_none(A_MOVSB,S_NO)));
  2331. S_W : exprasmlist^.concat(new(pai386,op_none(A_MOVSW,S_NO)));
  2332. S_L : exprasmlist^.concat(new(pai386,op_none(A_MOVSD,S_NO)));
  2333. end;
  2334. exprasmlist^.concat(new(pai386,
  2335. op_reg(A_POP,S_L,R_ESI)));
  2336. exprasmlist^.concat(new(pai386,
  2337. op_reg(A_POP,S_L,R_ECX)));
  2338. { patch the new address }
  2339. new(r);
  2340. reset_reference(r^);
  2341. r^.base:=procinfo.framepointer;
  2342. r^.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2343. exprasmlist^.concat(new(pai386,
  2344. op_reg_ref(A_MOV,S_L,R_ESP,r)));
  2345. end
  2346. else
  2347. if is_shortstring(pvarsym(p)^.definition) then
  2348. begin
  2349. reset_reference(href1);
  2350. href1.base:=procinfo.framepointer;
  2351. href1.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2352. reset_reference(href2);
  2353. href2.base:=procinfo.framepointer;
  2354. href2.offset:=-pvarsym(p)^.localvarsym^.address;
  2355. copyshortstring(href2,href1,pstringdef(pvarsym(p)^.definition)^.len,true);
  2356. end
  2357. else
  2358. begin
  2359. reset_reference(href1);
  2360. href1.base:=procinfo.framepointer;
  2361. href1.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2362. reset_reference(href2);
  2363. href2.base:=procinfo.framepointer;
  2364. href2.offset:=-pvarsym(p)^.localvarsym^.address;
  2365. concatcopy(href1,href2,pvarsym(p)^.definition^.size,true,true);
  2366. end;
  2367. end;
  2368. end;
  2369. procedure inittempansistrings;
  2370. var
  2371. hp : ptemprecord;
  2372. r : preference;
  2373. begin
  2374. hp:=templist;
  2375. while assigned(hp) do
  2376. begin
  2377. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  2378. begin
  2379. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2380. new(r);
  2381. reset_reference(r^);
  2382. r^.base:=procinfo.framepointer;
  2383. r^.offset:=hp^.pos;
  2384. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,0,r)));
  2385. end;
  2386. hp:=hp^.next;
  2387. end;
  2388. end;
  2389. procedure finalizetempansistrings;
  2390. var
  2391. hp : ptemprecord;
  2392. hr : treference;
  2393. begin
  2394. hp:=templist;
  2395. while assigned(hp) do
  2396. begin
  2397. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  2398. begin
  2399. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2400. reset_reference(hr);
  2401. hr.base:=procinfo.framepointer;
  2402. hr.offset:=hp^.pos;
  2403. emitpushreferenceaddr(hr);
  2404. exprasmlist^.concat(new(pai386,
  2405. op_sym(A_CALL,S_NO,newasmsymbol('FPC_ANSISTR_DECR_REF'))));
  2406. end;
  2407. hp:=hp^.next;
  2408. end;
  2409. end;
  2410. procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  2411. stackframe:longint;
  2412. var parasize:longint;var nostackframe:boolean;
  2413. inlined : boolean);
  2414. {
  2415. Generates the entry code for a procedure
  2416. }
  2417. var
  2418. hs : string;
  2419. {$ifdef GDB}
  2420. stab_function_name : Pai_stab_function_name;
  2421. {$endif GDB}
  2422. hr : preference;
  2423. p : psymtable;
  2424. r : treference;
  2425. oldlist,
  2426. oldexprasmlist : paasmoutput;
  2427. again : pasmlabel;
  2428. i : longint;
  2429. begin
  2430. oldexprasmlist:=exprasmlist;
  2431. exprasmlist:=alist;
  2432. if (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  2433. begin
  2434. exprasmlist^.insert(new(pai386,
  2435. op_sym(A_CALL,S_NO,newasmsymbol('FPC_INITIALIZEUNITS'))));
  2436. if target_info.target=target_I386_WIN32 then
  2437. begin
  2438. new(hr);
  2439. reset_reference(hr^);
  2440. hr^.symbol:=newasmsymbol('U_SYSWIN32_ISCONSOLE');
  2441. if apptype=at_cui then
  2442. exprasmlist^.insert(new(pai386,op_const_ref(A_MOV,S_B,
  2443. 1,hr)))
  2444. else
  2445. exprasmlist^.insert(new(pai386,op_const_ref(A_MOV,S_B,
  2446. 0,hr)));
  2447. end;
  2448. oldlist:=exprasmlist;
  2449. exprasmlist:=new(paasmoutput,init);
  2450. p:=symtablestack;
  2451. while assigned(p) do
  2452. begin
  2453. p^.foreach({$ifndef TP}@{$endif}initialize_threadvar);
  2454. p:=p^.next;
  2455. end;
  2456. oldlist^.insertlist(exprasmlist);
  2457. dispose(exprasmlist,done);
  2458. exprasmlist:=oldlist;
  2459. end;
  2460. { a constructor needs a help procedure }
  2461. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  2462. begin
  2463. if procinfo._class^.is_class then
  2464. begin
  2465. exprasmlist^.insert(new(pai386,op_cond_sym(A_Jcc,C_Z,S_NO,quickexitlabel)));
  2466. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('FPC_NEW_CLASS'))));
  2467. end
  2468. else
  2469. begin
  2470. exprasmlist^.insert(new(pai386,op_cond_sym(A_Jcc,C_Z,S_NO,quickexitlabel)));
  2471. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('FPC_HELP_CONSTRUCTOR'))));
  2472. exprasmlist^.insert(new(pai386,op_const_reg(A_MOV,S_L,procinfo._class^.vmt_offset,R_EDI)));
  2473. end;
  2474. end;
  2475. { don't load ESI, does the caller }
  2476. { When message method contains self as a parameter,
  2477. we must load it into ESI }
  2478. If (po_containsself in aktprocsym^.definition^.procoptions) then
  2479. begin
  2480. new(hr);
  2481. reset_reference(hr^);
  2482. hr^.offset:=procinfo.ESI_offset;
  2483. hr^.base:=procinfo.framepointer;
  2484. exprasmlist^.insert(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_ESI)));
  2485. end;
  2486. { should we save edi,esi,ebx like C ? }
  2487. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  2488. begin
  2489. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2490. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBX)));
  2491. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  2492. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  2493. end;
  2494. { omit stack frame ? }
  2495. if not inlined then
  2496. if procinfo.framepointer=stack_pointer then
  2497. begin
  2498. CGMessage(cg_d_stackframe_omited);
  2499. nostackframe:=true;
  2500. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  2501. parasize:=0
  2502. else
  2503. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset-4;
  2504. end
  2505. else
  2506. begin
  2507. if (aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  2508. parasize:=0
  2509. else
  2510. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset-8;
  2511. nostackframe:=false;
  2512. if stackframe<>0 then
  2513. begin
  2514. {$ifdef unused}
  2515. if (cs_littlesize in aktglobalswitches) and (stackframe<=65535) then
  2516. begin
  2517. if (cs_check_stack in aktlocalswitches) and
  2518. not(target_info.target in [target_i386_linux,target_i386_win32]) then
  2519. begin
  2520. exprasmlist^.insert(new(pai386,
  2521. op_sym(A_CALL,S_NO,newasmsymbol('FPC_STACKCHECK'))));
  2522. exprasmlist^.insert(new(pai386,op_const(A_PUSH,S_L,stackframe)));
  2523. end;
  2524. if cs_profile in aktmoduleswitches then
  2525. genprofilecode;
  2526. { %edi is already saved when pocdecl is used
  2527. if (target_info.target=target_linux) and
  2528. ((aktprocsym^.definition^.options and poexports)<>0) then
  2529. exprasmlist^.insert(new(Pai386,op_reg(A_PUSH,S_L,R_EDI))); }
  2530. exprasmlist^.insert(new(pai386,op_const_const(A_ENTER,S_NO,stackframe,0)))
  2531. end
  2532. else
  2533. {$endif unused}
  2534. begin
  2535. { windows guards only a few pages for stack growing, }
  2536. { so we have to access every page first }
  2537. if (target_os.id=os_i386_win32) and
  2538. (stackframe>=winstackpagesize) then
  2539. begin
  2540. if stackframe div winstackpagesize<=5 then
  2541. begin
  2542. exprasmlist^.insert(new(pai386,op_const_reg(A_SUB,S_L,stackframe-4,R_ESP)));
  2543. for i:=1 to stackframe div winstackpagesize do
  2544. begin
  2545. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  2546. exprasmlist^.concat(new(pai386,
  2547. op_const_ref(A_MOV,S_L,0,hr)));
  2548. end;
  2549. exprasmlist^.concat(new(pai386,
  2550. op_reg(A_PUSH,S_L,R_EAX)));
  2551. end
  2552. else
  2553. begin
  2554. getlabel(again);
  2555. exprasmlist^.concat(new(pai386,
  2556. op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI)));
  2557. emitlab(again);
  2558. exprasmlist^.concat(new(pai386,
  2559. op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
  2560. exprasmlist^.concat(new(pai386,
  2561. op_reg(A_PUSH,S_L,R_EAX)));
  2562. exprasmlist^.concat(new(pai386,
  2563. op_reg(A_DEC,S_L,R_EDI)));
  2564. emitjmp(C_NZ,again);
  2565. exprasmlist^.concat(new(pai386,
  2566. op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP)));
  2567. end
  2568. end
  2569. else
  2570. exprasmlist^.insert(new(pai386,op_const_reg(A_SUB,S_L,stackframe,R_ESP)));
  2571. if (cs_check_stack in aktlocalswitches) and
  2572. not(target_info.target in [target_i386_linux,target_i386_win32]) then
  2573. begin
  2574. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('FPC_STACKCHECK'))));
  2575. exprasmlist^.insert(new(pai386,op_const(A_PUSH,S_L,stackframe)));
  2576. end;
  2577. if cs_profile in aktmoduleswitches then
  2578. genprofilecode;
  2579. exprasmlist^.insert(new(pai386,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  2580. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  2581. end;
  2582. end { endif stackframe <> 0 }
  2583. else
  2584. begin
  2585. if cs_profile in aktmoduleswitches then
  2586. genprofilecode;
  2587. exprasmlist^.insert(new(pai386,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  2588. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  2589. end;
  2590. end;
  2591. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  2592. generate_interrupt_stackframe_entry;
  2593. { initialize return value }
  2594. if (procinfo.retdef<>pdef(voiddef)) and
  2595. (procinfo.retdef^.needs_inittable) and
  2596. ((procinfo.retdef^.deftype<>objectdef) or
  2597. not(pobjectdef(procinfo.retdef)^.is_class)) then
  2598. begin
  2599. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2600. reset_reference(r);
  2601. r.offset:=procinfo.retoffset;
  2602. r.base:=procinfo.framepointer;
  2603. initialize(procinfo.retdef,r,ret_in_param(procinfo.retdef));
  2604. end;
  2605. { generate copies of call by value parameters }
  2606. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  2607. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}copyvalueparas);
  2608. { initialisizes local data }
  2609. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}initialize_data);
  2610. { add a reference to all call by value/const parameters }
  2611. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}incr_data);
  2612. { initilisizes temp. ansi/wide string data }
  2613. inittempansistrings;
  2614. { do we need an exception frame because of ansi/widestrings ? }
  2615. if (procinfo.flags and pi_needs_implicit_finally)<>0 then
  2616. begin
  2617. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  2618. { Type of stack-frame must be pushed}
  2619. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,1)));
  2620. emitcall('FPC_PUSHEXCEPTADDR');
  2621. exprasmlist^.concat(new(pai386,
  2622. op_reg(A_PUSH,S_L,R_EAX)));
  2623. emitcall('FPC_SETJMP');
  2624. exprasmlist^.concat(new(pai386,
  2625. op_reg(A_PUSH,S_L,R_EAX)));
  2626. exprasmlist^.concat(new(pai386,
  2627. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  2628. emitjmp(C_NE,aktexitlabel);
  2629. end;
  2630. if (cs_profile in aktmoduleswitches) or
  2631. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  2632. (assigned(procinfo._class) and (procinfo._class^.owner^.symtabletype=globalsymtable)) then
  2633. make_global:=true;
  2634. if not inlined then
  2635. begin
  2636. hs:=proc_names.get;
  2637. {$ifdef GDB}
  2638. if (cs_debuginfo in aktmoduleswitches) then
  2639. exprasmlist^.insert(new(pai_force_line,init));
  2640. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  2641. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  2642. {$EndIf GDB}
  2643. while hs<>'' do
  2644. begin
  2645. if make_global then
  2646. exprasmlist^.insert(new(pai_symbol,initname_global(hs,0)))
  2647. else
  2648. exprasmlist^.insert(new(pai_symbol,initname(hs,0)));
  2649. {$ifdef GDB}
  2650. if (cs_debuginfo in aktmoduleswitches) and
  2651. target_os.use_function_relative_addresses then
  2652. exprasmlist^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  2653. {$endif GDB}
  2654. hs:=proc_names.get;
  2655. end;
  2656. end;
  2657. {$ifdef GDB}
  2658. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  2659. begin
  2660. if target_os.use_function_relative_addresses then
  2661. exprasmlist^.insert(stab_function_name);
  2662. if make_global or ((procinfo.flags and pi_is_global) <> 0) then
  2663. aktprocsym^.is_global := True;
  2664. exprasmlist^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  2665. aktprocsym^.isstabwritten:=true;
  2666. end;
  2667. {$endif GDB}
  2668. { Align }
  2669. if (not inlined) then
  2670. begin
  2671. { gprof uses 16 byte granularity !! }
  2672. if (cs_profile in aktmoduleswitches) then
  2673. exprasmlist^.insert(new(pai_align,init_op(16,$90)))
  2674. else
  2675. if not(cs_littlesize in aktglobalswitches) then
  2676. exprasmlist^.insert(new(pai_align,init(4)));
  2677. end;
  2678. exprasmlist:=oldexprasmlist;
  2679. end;
  2680. procedure handle_return_value(inlined : boolean);
  2681. var
  2682. hr : preference;
  2683. op : Tasmop;
  2684. s : Topsize;
  2685. begin
  2686. if procinfo.retdef<>pdef(voiddef) then
  2687. begin
  2688. if ((procinfo.flags and pi_operator)<>0) and
  2689. assigned(opsym) then
  2690. procinfo.funcret_is_valid:=
  2691. procinfo.funcret_is_valid or (opsym^.refs>0);
  2692. if not(procinfo.funcret_is_valid) and not inlined { and
  2693. ((procinfo.flags and pi_uses_asm)=0)} then
  2694. CGMessage(sym_w_function_result_not_set);
  2695. hr:=new_reference(procinfo.framepointer,procinfo.retoffset);
  2696. if (procinfo.retdef^.deftype in [orddef,enumdef]) then
  2697. begin
  2698. case procinfo.retdef^.size of
  2699. 8:
  2700. begin
  2701. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_EAX)));
  2702. hr:=new_reference(procinfo.framepointer,procinfo.retoffset+4);
  2703. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_EDX)));
  2704. end;
  2705. 4:
  2706. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_EAX)));
  2707. 2:
  2708. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,hr,R_AX)));
  2709. 1:
  2710. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_B,hr,R_AL)));
  2711. end;
  2712. end
  2713. else
  2714. if ret_in_acc(procinfo.retdef) then
  2715. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_EAX)))
  2716. else
  2717. if (procinfo.retdef^.deftype=floatdef) then
  2718. begin
  2719. floatloadops(pfloatdef(procinfo.retdef)^.typ,op,s);
  2720. exprasmlist^.concat(new(pai386,op_ref(op,s,hr)))
  2721. end
  2722. else
  2723. dispose(hr);
  2724. end
  2725. end;
  2726. procedure genexitcode(alist : paasmoutput;parasize:longint;nostackframe,inlined:boolean);
  2727. var
  2728. {$ifdef GDB}
  2729. mangled_length : longint;
  2730. p : pchar;
  2731. {$endif GDB}
  2732. noreraiselabel : pasmlabel;
  2733. hr : treference;
  2734. oldexprasmlist : paasmoutput;
  2735. begin
  2736. oldexprasmlist:=exprasmlist;
  2737. exprasmlist:=alist;
  2738. if aktexitlabel^.is_used then
  2739. exprasmlist^.insert(new(pai_label,init(aktexitlabel)));
  2740. { call the destructor help procedure }
  2741. if (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  2742. begin
  2743. if procinfo._class^.is_class then
  2744. begin
  2745. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,
  2746. newasmsymbol('FPC_DISPOSE_CLASS'))));
  2747. end
  2748. else
  2749. begin
  2750. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,
  2751. newasmsymbol('FPC_HELP_DESTRUCTOR'))));
  2752. exprasmlist^.insert(new(pai386,op_const_reg(A_MOV,S_L,procinfo._class^.vmt_offset,R_EDI)));
  2753. end;
  2754. end;
  2755. { finalize temporary data }
  2756. finalizetempansistrings;
  2757. { finalize local data }
  2758. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}finalize_data);
  2759. { finalize paras data }
  2760. if assigned(aktprocsym^.definition^.parast) then
  2761. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}finalize_data);
  2762. { do we need to handle exceptions because of ansi/widestrings ? }
  2763. if (procinfo.flags and pi_needs_implicit_finally)<>0 then
  2764. begin
  2765. getlabel(noreraiselabel);
  2766. exprasmlist^.concat(new(pai386,
  2767. op_sym(A_CALL,S_NO,newasmsymbol('FPC_POPADDRSTACK'))));
  2768. exprasmlist^.concat(new(pai386,
  2769. op_reg(A_POP,S_L,R_EAX)));
  2770. exprasmlist^.concat(new(pai386,
  2771. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  2772. emitjmp(C_E,noreraiselabel);
  2773. { must be the return value finalized before reraising the exception? }
  2774. if (procinfo.retdef<>pdef(voiddef)) and
  2775. (procinfo.retdef^.needs_inittable) and
  2776. ((procinfo.retdef^.deftype<>objectdef) or
  2777. not(pobjectdef(procinfo.retdef)^.is_class)) then
  2778. begin
  2779. reset_reference(hr);
  2780. hr.offset:=procinfo.retoffset;
  2781. hr.base:=procinfo.framepointer;
  2782. finalize(procinfo.retdef,hr,ret_in_param(procinfo.retdef));
  2783. end;
  2784. exprasmlist^.concat(new(pai386,
  2785. op_sym(A_CALL,S_NO,newasmsymbol('FPC_RERAISE'))));
  2786. exprasmlist^.concat(new(pai_label,init(noreraiselabel)));
  2787. end;
  2788. { call __EXIT for main program }
  2789. if (not DLLsource) and (not inlined) and (aktprocsym^.definition^.proctypeoption=potype_proginit) then
  2790. begin
  2791. exprasmlist^.concat(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('FPC_DO_EXIT'))));
  2792. end;
  2793. { handle return value }
  2794. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  2795. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  2796. handle_return_value(inlined)
  2797. else
  2798. begin
  2799. { successful constructor deletes the zero flag }
  2800. { and returns self in eax }
  2801. exprasmlist^.concat(new(pai_label,init(quickexitlabel)));
  2802. { eax must be set to zero if the allocation failed !!! }
  2803. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_ESI,R_EAX)));
  2804. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,R_EAX,R_EAX)));
  2805. end;
  2806. { stabs uses the label also ! }
  2807. if aktexit2label^.is_used or
  2808. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  2809. exprasmlist^.concat(new(pai_label,init(aktexit2label)));
  2810. { gives problems for long mangled names }
  2811. {list^.concat(new(pai_symbol,init(aktprocsym^.definition^.mangledname+'_end')));}
  2812. { should we restore edi ? }
  2813. { for all i386 gcc implementations }
  2814. if (po_savestdregs in aktprocsym^.definition^.procoptions) then
  2815. begin
  2816. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2817. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EBX)));
  2818. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  2819. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  2820. { here we could reset R_EBX
  2821. but that is risky because it only works
  2822. if genexitcode is called after genentrycode
  2823. so lets skip this for the moment PM
  2824. aktprocsym^.definition^.usedregisters:=
  2825. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  2826. }
  2827. end;
  2828. if not(nostackframe) and not inlined then
  2829. exprasmlist^.concat(new(pai386,op_none(A_LEAVE,S_NO)));
  2830. { parameters are limited to 65535 bytes because }
  2831. { ret allows only imm16 }
  2832. if (parasize>65535) and not(pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  2833. CGMessage(cg_e_parasize_too_big);
  2834. { at last, the return is generated }
  2835. if not inlined then
  2836. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  2837. generate_interrupt_stackframe_exit
  2838. else
  2839. begin
  2840. {Routines with the poclearstack flag set use only a ret.}
  2841. { also routines with parasize=0 }
  2842. if (parasize=0) or (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  2843. exprasmlist^.concat(new(pai386,op_none(A_RET,S_NO)))
  2844. else
  2845. exprasmlist^.concat(new(pai386,op_const(A_RET,S_NO,parasize)));
  2846. end;
  2847. exprasmlist^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
  2848. {$ifdef GDB}
  2849. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  2850. begin
  2851. aktprocsym^.concatstabto(exprasmlist);
  2852. if assigned(procinfo._class) then
  2853. if (not assigned(procinfo.parent) or
  2854. not assigned(procinfo.parent^._class)) then
  2855. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2856. '"$t:v'+procinfo._class^.numberstring+'",'+
  2857. tostr(N_PSYM)+',0,0,'+tostr(procinfo.esi_offset)))))
  2858. else
  2859. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2860. '"$t:r'+procinfo._class^.numberstring+'",'+
  2861. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
  2862. if (pdef(aktprocsym^.definition^.retdef) <> pdef(voiddef)) then
  2863. if ret_in_param(aktprocsym^.definition^.retdef) then
  2864. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2865. '"'+aktprocsym^.name+':X*'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  2866. tostr(N_PSYM)+',0,0,'+tostr(procinfo.retoffset)))))
  2867. else
  2868. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2869. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  2870. tostr(N_PSYM)+',0,0,'+tostr(procinfo.retoffset)))));
  2871. mangled_length:=length(aktprocsym^.definition^.mangledname);
  2872. getmem(p,mangled_length+50);
  2873. strpcopy(p,'192,0,0,');
  2874. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  2875. exprasmlist^.concat(new(pai_stabn,init(strnew(p))));
  2876. {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  2877. +aktprocsym^.definition^.mangledname))));
  2878. p[0]:='2';p[1]:='2';p[2]:='4';
  2879. strpcopy(strend(p),'_end');}
  2880. freemem(p,mangled_length+50);
  2881. exprasmlist^.concat(new(pai_stabn,init(
  2882. strpnew('224,0,0,'+aktexit2label^.name))));
  2883. { strpnew('224,0,0,'
  2884. +aktprocsym^.definition^.mangledname+'_end'))));}
  2885. end;
  2886. {$endif GDB}
  2887. exprasmlist:=oldexprasmlist;
  2888. end;
  2889. {$ifdef test_dest_loc}
  2890. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  2891. begin
  2892. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  2893. begin
  2894. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  2895. set_location(p^.location,dest_loc);
  2896. in_dest_loc:=true;
  2897. end
  2898. else
  2899. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  2900. begin
  2901. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference))));
  2902. set_location(p^.location,dest_loc);
  2903. in_dest_loc:=true;
  2904. end
  2905. else
  2906. internalerror(20080);
  2907. end;
  2908. {$endif test_dest_loc}
  2909. end.
  2910. {
  2911. $Log$
  2912. Revision 1.26 1999-08-05 14:58:04 florian
  2913. * some fixes for the floating point registers
  2914. * more things for the new code generator
  2915. Revision 1.25 1999/08/04 13:45:24 florian
  2916. + floating point register variables !!
  2917. * pairegalloc is now generated for register variables
  2918. Revision 1.24 1999/08/04 00:22:55 florian
  2919. * renamed i386asm and i386base to cpuasm and cpubase
  2920. Revision 1.23 1999/08/03 22:02:49 peter
  2921. * moved bitmask constants to sets
  2922. * some other type/const renamings
  2923. Revision 1.22 1999/08/01 23:36:39 florian
  2924. * some changes to compile the new code generator
  2925. Revision 1.21 1999/08/01 17:32:31 florian
  2926. * more fixes for inittable call
  2927. Revision 1.20 1999/08/01 17:17:37 florian
  2928. * tried to fix a bug with init table
  2929. Revision 1.19 1999/07/29 20:53:58 peter
  2930. * write .size also
  2931. Revision 1.18 1999/07/26 12:13:46 florian
  2932. * exit in try..finally blocks needed a second fix
  2933. * a raise in a try..finally lead into a endless loop, fixed
  2934. Revision 1.17 1999/07/22 09:37:38 florian
  2935. + resourcestring implemented
  2936. + start of longstring support
  2937. Revision 1.16 1999/07/18 10:41:59 florian
  2938. * fix of my previous commit nevertheless it doesn't work completly
  2939. Revision 1.15 1999/07/18 10:19:44 florian
  2940. * made it compilable with Dlephi 4 again
  2941. + fixed problem with large stack allocations on win32
  2942. Revision 1.14 1999/07/06 21:48:11 florian
  2943. * a lot bug fixes:
  2944. - po_external isn't any longer necessary for procedure compatibility
  2945. - m_tp_procvar is in -Sd now available
  2946. - error messages of procedure variables improved
  2947. - return values with init./finalization fixed
  2948. - data types with init./finalization aren't any longer allowed in variant
  2949. record
  2950. Revision 1.13 1999/07/05 20:25:22 peter
  2951. * merged
  2952. Revision 1.12 1999/07/05 20:13:13 peter
  2953. * removed temp defines
  2954. Revision 1.11 1999/07/05 11:56:56 jonas
  2955. * merged
  2956. Revision 1.5.2.5 1999/07/05 20:03:31 peter
  2957. * removed warning/notes
  2958. Revision 1.5.2.3 1999/07/04 21:50:17 jonas
  2959. * everything between $ifdef jmpfix:
  2960. * when a jxx instruction is disposed, decrease the refcount of the label
  2961. it referenced
  2962. * for jmp instructions to a label, set is_jmp also to true (was only done
  2963. for Jcc instructions)
  2964. Revision 1.9 1999/07/01 15:49:11 florian
  2965. * int64/qword type release
  2966. + lo/hi for int64/qword
  2967. Revision 1.8 1999/06/28 22:29:15 florian
  2968. * qword division fixed
  2969. + code for qword/int64 type casting added:
  2970. range checking isn't implemented yet
  2971. Revision 1.7 1999/06/17 13:19:50 pierre
  2972. * merged from 0_99_12 branch
  2973. Revision 1.5.2.2 1999/06/17 12:38:39 pierre
  2974. * wrong warning for operators removed
  2975. Revision 1.6 1999/06/14 17:47:48 peter
  2976. * merged
  2977. Revision 1.5.2.1 1999/06/14 17:27:08 peter
  2978. * fixed posavestd regs which popped at the wrong place
  2979. Revision 1.5 1999/06/03 16:21:15 pierre
  2980. * fixes a bug due to int64 code in maybe_savetotemp
  2981. Revision 1.4 1999/06/02 22:44:06 pierre
  2982. * previous wrong log corrected
  2983. Revision 1.3 1999/06/02 22:25:29 pierre
  2984. * changed $ifdef FPC @ into $ifndef TP
  2985. Revision 1.2 1999/06/02 10:11:49 florian
  2986. * make cycle fixed i.e. compilation with 0.99.10
  2987. * some fixes for qword
  2988. * start of register calling conventions
  2989. Revision 1.1 1999/06/01 19:33:18 peter
  2990. * reinserted
  2991. Revision 1.158 1999/06/01 14:45:46 peter
  2992. * @procvar is now always needed for FPC
  2993. Revision 1.157 1999/05/27 19:44:20 peter
  2994. * removed oldasm
  2995. * plabel -> pasmlabel
  2996. * -a switches to source writing automaticly
  2997. * assembler readers OOPed
  2998. * asmsymbol automaticly external
  2999. * jumptables and other label fixes for asm readers
  3000. Revision 1.156 1999/05/24 08:55:24 florian
  3001. * non working safecall directiv implemented, I don't know if we
  3002. need it
  3003. Revision 1.155 1999/05/23 19:55:14 florian
  3004. * qword/int64 multiplication fixed
  3005. + qword/int64 subtraction
  3006. Revision 1.154 1999/05/23 18:42:05 florian
  3007. * better error recovering in typed constants
  3008. * some problems with arrays of const fixed, some problems
  3009. due my previous
  3010. - the location type of array constructor is now LOC_MEM
  3011. - the pushing of high fixed
  3012. - parameter copying fixed
  3013. - zero temp. allocation removed
  3014. * small problem in the assembler writers fixed:
  3015. ref to nil wasn't written correctly
  3016. Revision 1.153 1999/05/21 13:54:55 peter
  3017. * NEWLAB for label as symbol
  3018. Revision 1.152 1999/05/19 22:00:45 florian
  3019. * some new routines for register management:
  3020. maybe_savetotemp,restorefromtemp, saveusedregisters,
  3021. restoreusedregisters
  3022. Revision 1.151 1999/05/19 20:40:11 florian
  3023. * fixed a couple of array related bugs:
  3024. - var a : array[0..1] of char; p : pchar; p:=a+123; works now
  3025. - open arrays with an odd size doesn't work: movsb wasn't generated
  3026. - introduced some new array type helper routines (is_special_array) etc.
  3027. - made the array type checking in isconvertable more strict, often
  3028. open array can be used where is wasn't allowed etc...
  3029. Revision 1.150 1999/05/19 15:26:30 florian
  3030. * if a non local variables isn't initialized the compiler doesn't write
  3031. any longer "local var. seems not to be ..."
  3032. Revision 1.149 1999/05/19 13:59:07 jonas
  3033. * no more "enter" generated when -Og is used (caused sometimes crashes under
  3034. Linux, don't know why)
  3035. Revision 1.148 1999/05/18 21:58:30 florian
  3036. * fixed some bugs related to temp. ansistrings and functions results
  3037. which return records/objects/arrays which need init/final.
  3038. Revision 1.147 1999/05/18 14:15:28 peter
  3039. * containsself fixes
  3040. * checktypes()
  3041. Revision 1.146 1999/05/17 22:42:26 florian
  3042. * FPC_ANSISTR_DECR_REF needs a reference!
  3043. Revision 1.145 1999/05/17 21:57:06 florian
  3044. * new temporary ansistring handling
  3045. Revision 1.144 1999/05/15 21:33:18 peter
  3046. * redesigned temp_gen temp allocation so temp allocation for
  3047. ansistring works correct. It also does a best fit instead of first fit
  3048. Revision 1.143 1999/05/13 21:59:22 peter
  3049. * removed oldppu code
  3050. * warning if objpas is loaded from uses
  3051. * first things for new deref writing
  3052. Revision 1.142 1999/05/12 00:19:46 peter
  3053. * removed R_DEFAULT_SEG
  3054. * uniform float names
  3055. Revision 1.141 1999/05/07 00:33:44 pierre
  3056. explicit type conv to pobject checked with cond TESTOBJEXT2
  3057. Revision 1.140 1999/05/06 09:05:17 peter
  3058. * generic write_float and str_float
  3059. * fixed constant float conversions
  3060. Revision 1.139 1999/05/04 21:44:35 florian
  3061. * changes to compile it with Delphi 4.0
  3062. Revision 1.138 1999/05/02 09:35:36 florian
  3063. + method message handlers which contain an explicit self can't be called
  3064. directly anymore
  3065. + self is now loaded at the start of the an message handler with an explicit
  3066. self
  3067. + $useoverlay fixed: i386 was renamed to i386base
  3068. Revision 1.137 1999/05/01 13:24:16 peter
  3069. * merged nasm compiler
  3070. * old asm moved to oldasm/
  3071. Revision 1.136 1999/04/28 06:01:56 florian
  3072. * changes of Bruessel:
  3073. + message handler can now take an explicit self
  3074. * typinfo fixed: sometimes the type names weren't written
  3075. * the type checking for pointer comparisations and subtraction
  3076. and are now more strict (was also buggy)
  3077. * small bug fix to link.pas to support compiling on another
  3078. drive
  3079. * probable bug in popt386 fixed: call/jmp => push/jmp
  3080. transformation didn't count correctly the jmp references
  3081. + threadvar support
  3082. * warning if ln/sqrt gets an invalid constant argument
  3083. Revision 1.135 1999/04/26 13:31:26 peter
  3084. * release storenumber,double_checksum
  3085. Revision 1.134 1999/04/21 21:53:08 pierre
  3086. * previous log corrected
  3087. Revision 1.133 1999/04/21 16:31:38 pierre
  3088. + TEMPS_NOT_PUSH conditionnal code :
  3089. put needed registers into temp space instead of pushing them
  3090. Revision 1.132 1999/04/21 09:43:30 peter
  3091. * storenumber works
  3092. * fixed some typos in double_checksum
  3093. + incompatible types type1 and type2 message (with storenumber)
  3094. Revision 1.131 1999/04/19 09:45:49 pierre
  3095. + cdecl or stdcall push all args with longint size
  3096. * tempansi stuff cleaned up
  3097. Revision 1.130 1999/04/17 13:14:50 peter
  3098. * concat_external added for new init/final
  3099. Revision 1.129 1999/04/16 20:44:35 florian
  3100. * the boolean operators =;<>;xor with LOC_JUMP and LOC_FLAGS
  3101. operands fixed, small things for new ansistring management
  3102. Revision 1.128 1999/04/16 13:42:31 jonas
  3103. * more regalloc fixes (still not complete)
  3104. Revision 1.127 1999/04/16 10:28:23 pierre
  3105. + added posavestdregs used for cdecl AND stdcall functions
  3106. (saves ESI EDI and EBX for i386)
  3107. Revision 1.126 1999/04/16 09:56:06 pierre
  3108. * unused local var commented
  3109. Revision 1.125 1999/04/15 13:08:30 pierre
  3110. * misplaced statement in concatcopy corrected
  3111. Revision 1.124 1999/04/15 12:19:55 peter
  3112. + finalization support
  3113. Revision 1.123 1999/04/09 00:00:52 pierre
  3114. + uses ungetiftempansi
  3115. Revision 1.122 1999/04/08 15:57:47 peter
  3116. + subrange checking for readln()
  3117. Revision 1.121 1999/03/31 13:55:08 peter
  3118. * assembler inlining working for ag386bin
  3119. Revision 1.120 1999/03/26 00:05:27 peter
  3120. * released valintern
  3121. + deffile is now removed when compiling is finished
  3122. * ^( compiles now correct
  3123. + static directive
  3124. * shrd fixed
  3125. Revision 1.119 1999/03/24 23:16:55 peter
  3126. * fixed bugs 212,222,225,227,229,231,233
  3127. Revision 1.118 1999/03/16 17:52:49 jonas
  3128. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  3129. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  3130. * in cgai386: also small fixes to emitrangecheck
  3131. Revision 1.117 1999/03/09 19:29:12 peter
  3132. * ecxpushed was not reset in concatcopy
  3133. Revision 1.116 1999/03/09 11:45:40 pierre
  3134. * small arrays and records (size <=4) are copied directly
  3135. Revision 1.115 1999/03/03 12:15:13 pierre
  3136. * U_SYSWIN32_ISCONSOLE adde to external list
  3137. Revision 1.114 1999/03/02 18:21:33 peter
  3138. + flags support for add and case
  3139. Revision 1.113 1999/03/01 15:46:19 peter
  3140. * ag386bin finally make cycles correct
  3141. * prefixes are now also normal opcodes
  3142. Revision 1.112 1999/03/01 13:39:44 pierre
  3143. * temp for int_value const parameters
  3144. Revision 1.111 1999/02/25 21:02:32 peter
  3145. * ag386bin updates
  3146. + coff writer
  3147. Revision 1.110 1999/02/22 02:15:17 peter
  3148. * updates for ag386bin
  3149. Revision 1.109 1999/02/16 00:46:09 peter
  3150. * optimized concatcopy with ecx=1 and ecx=0
  3151. Revision 1.108 1999/02/15 13:13:14 pierre
  3152. * fix for bug0216
  3153. Revision 1.107 1999/02/12 10:43:58 florian
  3154. * internal error 10 with ansistrings fixed
  3155. Revision 1.106 1999/02/03 09:50:22 pierre
  3156. * conditionnal code to try to release temp for consts that are not in memory
  3157. Revision 1.105 1999/02/02 11:47:56 peter
  3158. * fixed ansi2short
  3159. Revision 1.104 1999/01/25 09:29:36 florian
  3160. * very rare problem with in-operator fixed, mainly it was a problem of
  3161. emit_to_reg32 (typo in case ranges)
  3162. Revision 1.103 1999/01/21 22:10:42 peter
  3163. * fixed array of const
  3164. * generic platform independent high() support
  3165. Revision 1.102 1999/01/19 10:19:00 florian
  3166. * bug with mul. of dwords fixed, reported by Alexander Stohr
  3167. * some changes to compile with TP
  3168. + small enhancements for the new code generator
  3169. Revision 1.101 1999/01/15 11:36:48 pierre
  3170. * double temp disallocation on ansistring removed
  3171. Revision 1.100 1998/12/30 13:41:08 peter
  3172. * released valuepara
  3173. Revision 1.99 1998/12/22 13:11:00 florian
  3174. * memory leaks for ansistring type casts fixed
  3175. Revision 1.98 1998/12/19 00:23:46 florian
  3176. * ansistring memory leaks fixed
  3177. Revision 1.97 1998/12/11 16:10:08 florian
  3178. + shifting for 64 bit ints added
  3179. * bug in getexplicitregister32 fixed: usableregs wasn't decremented !!
  3180. Revision 1.96 1998/12/11 00:03:11 peter
  3181. + globtype,tokens,version unit splitted from globals
  3182. Revision 1.95 1998/12/10 09:47:19 florian
  3183. + basic operations with int64/qord (compiler with -dint64)
  3184. + rtti of enumerations extended: names are now written
  3185. Revision 1.94 1998/12/03 10:17:27 peter
  3186. * target_os.use_bound_instruction boolean
  3187. Revision 1.93 1998/11/30 19:48:56 peter
  3188. * some more rangecheck fixes
  3189. Revision 1.92 1998/11/30 16:34:44 pierre
  3190. * corrected problems with rangecheck
  3191. + added needed code for no rangecheck in CRC32 functions in ppu unit
  3192. * enumdef lso need its rangenr reset to zero
  3193. when calling reset_global_defs
  3194. Revision 1.91 1998/11/30 09:43:07 pierre
  3195. * some range check bugs fixed (still not working !)
  3196. + added DLL writing support for win32 (also accepts variables)
  3197. + TempAnsi for code that could be used for Temporary ansi strings
  3198. handling
  3199. Revision 1.90 1998/11/29 12:43:45 peter
  3200. * commented the fpc_init_stack_check becuase it is not in the RTL
  3201. Revision 1.89 1998/11/27 14:50:34 peter
  3202. + open strings, $P switch support
  3203. Revision 1.88 1998/11/26 21:33:07 peter
  3204. * rangecheck updates
  3205. Revision 1.87 1998/11/26 13:10:41 peter
  3206. * new int - int conversion -dNEWCNV
  3207. * some function renamings
  3208. Revision 1.86 1998/11/26 09:53:37 florian
  3209. * for classes no init/final. code is necessary, fixed
  3210. Revision 1.85 1998/11/20 15:35:56 florian
  3211. * problems with rtti fixed, hope it works
  3212. Revision 1.84 1998/11/18 17:45:25 peter
  3213. * fixes for VALUEPARA
  3214. Revision 1.83 1998/11/18 15:44:12 peter
  3215. * VALUEPARA for tp7 compatible value parameters
  3216. Revision 1.82 1998/11/17 00:36:41 peter
  3217. * more ansistring fixes
  3218. Revision 1.81 1998/11/16 19:23:33 florian
  3219. * isconsole is now set by win32 applications
  3220. Revision 1.80 1998/11/16 15:35:40 peter
  3221. * rename laod/copystring -> load/copyshortstring
  3222. * fixed int-bool cnv bug
  3223. + char-ansistring conversion
  3224. Revision 1.79 1998/11/16 11:28:56 pierre
  3225. * stackcheck removed for i386_win32
  3226. * exportlist does not crash at least !!
  3227. (was need for tests dir !)z
  3228. Revision 1.78 1998/11/15 16:32:34 florian
  3229. * some stuff of Pavel implement (win32 dll creation)
  3230. * bug with ansistring function results fixed
  3231. Revision 1.77 1998/11/13 15:40:17 pierre
  3232. + added -Se in Makefile cvstest target
  3233. + lexlevel cleanup
  3234. normal_function_level main_program_level and unit_init_level defined
  3235. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  3236. (test added in code !)
  3237. * -Un option was wrong
  3238. * _FAIL and _SELF only keyword inside
  3239. constructors and methods respectively
  3240. Revision 1.76 1998/11/12 16:43:33 florian
  3241. * functions with ansi strings as result didn't work, solved
  3242. Revision 1.75 1998/11/12 11:19:44 pierre
  3243. * fix for first line of function break
  3244. Revision 1.74 1998/11/12 09:46:18 pierre
  3245. + break main stops before calls to unit inits
  3246. + break at constructors stops before call to FPC_NEW_CLASS
  3247. or FPC_HELP_CONSTRUCTOR
  3248. Revision 1.73 1998/11/10 10:50:55 pierre
  3249. * temporary fix for long mangled procsym names
  3250. Revision 1.72 1998/11/05 12:02:40 peter
  3251. * released useansistring
  3252. * removed -Sv, its now available in fpc modes
  3253. Revision 1.71 1998/10/29 15:42:45 florian
  3254. + partial disposing of temp. ansistrings
  3255. Revision 1.70 1998/10/25 23:32:49 peter
  3256. * fixed unsigned mul
  3257. Revision 1.69 1998/10/20 13:11:33 peter
  3258. + def_getreg to get a register with the same size as definition
  3259. Revision 1.68 1998/10/20 08:06:48 pierre
  3260. * several memory corruptions due to double freemem solved
  3261. => never use p^.loc.location:=p^.left^.loc.location;
  3262. + finally I added now by default
  3263. that ra386dir translates global and unit symbols
  3264. + added a first field in tsymtable and
  3265. a nextsym field in tsym
  3266. (this allows to obtain ordered type info for
  3267. records and objects in gdb !)
  3268. Revision 1.67 1998/10/16 13:12:50 pierre
  3269. * added vmt_offsets in destructors code also !!!
  3270. * vmt_offset code for m68k
  3271. Revision 1.66 1998/10/16 08:48:40 peter
  3272. * fixed some misplaced $endif GDB
  3273. Revision 1.65 1998/10/15 12:37:40 pierre
  3274. + passes vmt offset to HELP_CONSTRUCTOR for objects
  3275. Revision 1.64 1998/10/13 16:50:13 pierre
  3276. * undid some changes of Peter that made the compiler wrong
  3277. for m68k (I had to reinsert some ifdefs)
  3278. * removed several memory leaks under m68k
  3279. * removed the meory leaks for assembler readers
  3280. * cross compiling shoud work again better
  3281. ( crosscompiling sysamiga works
  3282. but as68k still complain about some code !)
  3283. Revision 1.63 1998/10/13 13:10:13 peter
  3284. * new style for m68k/i386 infos and enums
  3285. Revision 1.62 1998/10/08 17:17:17 pierre
  3286. * current_module old scanner tagged as invalid if unit is recompiled
  3287. + added ppheap for better info on tracegetmem of heaptrc
  3288. (adds line column and file index)
  3289. * several memory leaks removed ith help of heaptrc !!
  3290. Revision 1.61 1998/10/08 13:48:41 peter
  3291. * fixed memory leaks for do nothing source
  3292. * fixed unit interdependency
  3293. Revision 1.60 1998/10/07 10:37:43 peter
  3294. * fixed stabs
  3295. Revision 1.59 1998/10/06 17:16:45 pierre
  3296. * some memory leaks fixed (thanks to Peter for heaptrc !)
  3297. Revision 1.58 1998/10/05 21:33:16 peter
  3298. * fixed 161,165,166,167,168
  3299. Revision 1.57 1998/10/01 09:22:54 peter
  3300. * fixed value openarray
  3301. * ungettemp of arrayconstruct
  3302. Revision 1.56 1998/09/28 16:57:19 pierre
  3303. * changed all length(p^.value_str^) into str_length(p)
  3304. to get it work with and without ansistrings
  3305. * changed sourcefiles field of tmodule to a pointer
  3306. Revision 1.55 1998/09/28 16:18:15 florian
  3307. * two fixes to get ansi strings work
  3308. Revision 1.54 1998/09/20 17:46:49 florian
  3309. * some things regarding ansistrings fixed
  3310. Revision 1.53 1998/09/20 09:38:44 florian
  3311. * hasharray for defs fixed
  3312. * ansistring code generation corrected (init/final, assignement)
  3313. Revision 1.52 1998/09/17 09:42:31 peter
  3314. + pass_2 for cg386
  3315. * Message() -> CGMessage() for pass_1/pass_2
  3316. Revision 1.51 1998/09/14 10:44:05 peter
  3317. * all internal RTL functions start with FPC_
  3318. Revision 1.50 1998/09/07 18:46:01 peter
  3319. * update smartlinking, uses getdatalabel
  3320. * renamed ptree.value vars to value_str,value_real,value_set
  3321. Revision 1.49 1998/09/05 22:10:52 florian
  3322. + switch -vb
  3323. * while/repeat loops accept now also word/longbool conditions
  3324. * makebooltojump did an invalid ungetregister32, fixed
  3325. Revision 1.48 1998/09/04 08:41:52 peter
  3326. * updated some error CGMessages
  3327. Revision 1.47 1998/09/03 17:08:41 pierre
  3328. * better lines for stabs
  3329. (no scroll back to if before else part
  3330. no return to case line at jump outside case)
  3331. + source lines also if not in order
  3332. Revision 1.46 1998/09/03 16:03:16 florian
  3333. + rtti generation
  3334. * init table generation changed
  3335. Revision 1.45 1998/09/01 12:48:03 peter
  3336. * use pdef^.size instead of orddef^.typ
  3337. Revision 1.44 1998/09/01 09:07:11 peter
  3338. * m68k fixes, splitted cg68k like cgi386
  3339. Revision 1.43 1998/08/21 08:40:52 pierre
  3340. * EBX,EDI,ESI saved for CDECL on all i386 targets
  3341. Revision 1.42 1998/08/19 16:07:41 jonas
  3342. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  3343. Revision 1.41 1998/08/19 00:40:43 peter
  3344. * small crash prevention
  3345. Revision 1.40 1998/08/17 10:10:06 peter
  3346. - removed OLDPPU
  3347. Revision 1.39 1998/08/15 16:51:39 peter
  3348. * save also esi,ebx for cdecl procedures
  3349. Revision 1.38 1998/08/14 18:18:42 peter
  3350. + dynamic set contruction
  3351. * smallsets are now working (always longint size)
  3352. Revision 1.37 1998/08/11 00:00:30 peter
  3353. * fixed dup log
  3354. Revision 1.36 1998/08/10 14:49:52 peter
  3355. + localswitches, moduleswitches, globalswitches splitting
  3356. Revision 1.35 1998/08/05 16:00:11 florian
  3357. * some fixes for ansi strings
  3358. Revision 1.34 1998/07/30 11:18:14 florian
  3359. + first implementation of try ... except on .. do end;
  3360. * limitiation of 65535 bytes parameters for cdecl removed
  3361. Revision 1.33 1998/07/27 21:57:12 florian
  3362. * fix to allow tv like stream registration:
  3363. @tmenu.load doesn't work if load had parameters or if load was only
  3364. declared in an anchestor class of tmenu
  3365. Revision 1.32 1998/07/27 11:23:40 florian
  3366. + procedures with the directive cdecl and with target linux save now
  3367. the register EDI (like GCC procedures).
  3368. Revision 1.31 1998/07/20 18:40:11 florian
  3369. * handling of ansi string constants should now work
  3370. Revision 1.30 1998/07/18 22:54:26 florian
  3371. * some ansi/wide/longstring support fixed:
  3372. o parameter passing
  3373. o returning as result from functions
  3374. Revision 1.29 1998/07/06 13:21:13 michael
  3375. + Fixed Initialization/Finalizarion calls
  3376. Revision 1.28 1998/06/25 08:48:11 florian
  3377. * first version of rtti support
  3378. Revision 1.27 1998/06/24 14:48:32 peter
  3379. * ifdef newppu -> ifndef oldppu
  3380. Revision 1.26 1998/06/16 08:56:19 peter
  3381. + targetcpu
  3382. * cleaner pmodules for newppu
  3383. Revision 1.25 1998/06/08 13:13:40 pierre
  3384. + temporary variables now in temp_gen.pas unit
  3385. because it is processor independent
  3386. * mppc68k.bat modified to undefine i386 and support_mmx
  3387. (which are defaults for i386)
  3388. Revision 1.24 1998/06/07 15:30:23 florian
  3389. + first working rtti
  3390. + data init/final. for local variables
  3391. Revision 1.23 1998/06/05 17:49:53 peter
  3392. * cleanup of cgai386
  3393. Revision 1.22 1998/06/04 09:55:34 pierre
  3394. * demangled name of procsym reworked to become
  3395. independant of the mangling scheme
  3396. Revision 1.21 1998/06/03 22:48:51 peter
  3397. + wordbool,longbool
  3398. * rename bis,von -> high,low
  3399. * moved some systemunit loading/creating to psystem.pas
  3400. Revision 1.20 1998/05/30 14:31:03 peter
  3401. + $ASMMODE
  3402. Revision 1.19 1998/05/23 01:21:02 peter
  3403. + aktasmmode, aktoptprocessor, aktoutputformat
  3404. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  3405. + $LIBNAME to set the library name where the unit will be put in
  3406. * splitted cgi386 a bit (codeseg to large for bp7)
  3407. * nasm, tasm works again. nasm moved to ag386nsm.pas
  3408. Revision 1.18 1998/05/20 09:42:32 pierre
  3409. + UseTokenInfo now default
  3410. * unit in interface uses and implementation uses gives error now
  3411. * only one error for unknown symbol (uses lastsymknown boolean)
  3412. the problem came from the label code !
  3413. + first inlined procedures and function work
  3414. (warning there might be allowed cases were the result is still wrong !!)
  3415. * UseBrower updated gives a global list of all position of all used symbols
  3416. with switch -gb
  3417. Revision 1.17 1998/05/11 13:07:53 peter
  3418. + $ifdef NEWPPU for the new ppuformat
  3419. + $define GDB not longer required
  3420. * removed all warnings and stripped some log comments
  3421. * no findfirst/findnext anymore to remove smartlink *.o files
  3422. Revision 1.16 1998/05/07 00:17:00 peter
  3423. * smartlinking for sets
  3424. + consts labels are now concated/generated in hcodegen
  3425. * moved some cpu code to cga and some none cpu depended code from cga
  3426. to tree and hcodegen and cleanup of hcodegen
  3427. * assembling .. output reduced for smartlinking ;)
  3428. Revision 1.15 1998/05/06 08:38:35 pierre
  3429. * better position info with UseTokenInfo
  3430. UseTokenInfo greatly simplified
  3431. + added check for changed tree after first time firstpass
  3432. (if we could remove all the cases were it happen
  3433. we could skip all firstpass if firstpasscount > 1)
  3434. Only with ExtDebug
  3435. Revision 1.14 1998/05/04 17:54:24 peter
  3436. + smartlinking works (only case jumptable left todo)
  3437. * redesign of systems.pas to support assemblers and linkers
  3438. + Unitname is now also in the PPU-file, increased version to 14
  3439. Revision 1.13 1998/05/01 16:38:43 florian
  3440. * handling of private and protected fixed
  3441. + change_keywords_to_tp implemented to remove
  3442. keywords which aren't supported by tp
  3443. * break and continue are now symbols of the system unit
  3444. + widestring, longstring and ansistring type released
  3445. Revision 1.12 1998/05/01 07:43:52 florian
  3446. + basics for rtti implemented
  3447. + switch $m (generate rtti for published sections)
  3448. Revision 1.11 1998/04/29 13:41:17 peter
  3449. + assembler functions are not profiled
  3450. Revision 1.10 1998/04/29 10:33:47 pierre
  3451. + added some code for ansistring (not complete nor working yet)
  3452. * corrected operator overloading
  3453. * corrected nasm output
  3454. + started inline procedures
  3455. + added starstarn : use ** for exponentiation (^ gave problems)
  3456. + started UseTokenInfo cond to get accurate positions
  3457. Revision 1.9 1998/04/21 10:16:46 peter
  3458. * patches from strasbourg
  3459. * objects is not used anymore in the fpc compiled version
  3460. Revision 1.8 1998/04/13 08:42:50 florian
  3461. * call by reference and call by value open arrays fixed
  3462. Revision 1.7 1998/04/09 23:27:26 peter
  3463. * fixed profiling results
  3464. Revision 1.6 1998/04/09 14:28:03 jonas
  3465. + basic k6 and 6x86 optimizing support (-O7 and -O8)
  3466. Revision 1.5 1998/04/08 16:58:01 pierre
  3467. * several bugfixes
  3468. ADD ADC and AND are also sign extended
  3469. nasm output OK (program still crashes at end
  3470. and creates wrong assembler files !!)
  3471. procsym types sym in tdef removed !!
  3472. }