2
0

cgcpu.pas 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symtype,
  23. cgbase,cgobj,
  24. aasmbase,aasmcpu,aasmtai,
  25. cpubase,cpuinfo,node,cg64f32;
  26. type
  27. tcgppc = class(tcg)
  28. procedure init_register_allocators;override;
  29. procedure done_register_allocators;override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  36. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  37. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  38. procedure a_call_name(list : taasmoutput;const s : string);override;
  39. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  40. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister); override;
  41. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  42. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  43. size: tcgsize; a: aword; src, dst: tregister); override;
  44. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  45. size: tcgsize; src1, src2, dst: tregister); override;
  46. { move instructions }
  47. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  48. procedure a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  49. procedure a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  50. procedure a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  51. { fpu move instructions }
  52. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  53. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  54. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  55. { comparison operations }
  56. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  57. l : tasmlabel);override;
  58. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  59. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  60. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  61. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  62. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);override;
  63. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  64. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  65. procedure g_restore_frame_pointer(list : taasmoutput);override;
  66. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  67. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  68. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  69. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  70. { that's the case, we can use rlwinm to do an AND operation }
  71. function get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  72. procedure g_save_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);override;
  73. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);override;
  74. procedure g_save_all_registers(list : taasmoutput);override;
  75. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  76. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  77. private
  78. (* NOT IN USE: *)
  79. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  80. (* NOT IN USE: *)
  81. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  82. { Make sure ref is a valid reference for the PowerPC and sets the }
  83. { base to the value of the index if (base = R_NO). }
  84. { Returns true if the reference contained a base, index and an }
  85. { offset or symbol, in which case the base will have been changed }
  86. { to a tempreg (which has to be freed by the caller) containing }
  87. { the sum of part of the original reference }
  88. function fixref(list: taasmoutput; var ref: treference): boolean;
  89. { returns whether a reference can be used immediately in a powerpc }
  90. { instruction }
  91. function issimpleref(const ref: treference): boolean;
  92. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  93. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  94. ref: treference);
  95. { creates the correct branch instruction for a given combination }
  96. { of asmcondflags and destination addressing mode }
  97. procedure a_jmp(list: taasmoutput; op: tasmop;
  98. c: tasmcondflag; crval: longint; l: tasmlabel);
  99. end;
  100. tcg64fppc = class(tcg64f32)
  101. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  102. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  103. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  104. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  105. end;
  106. const
  107. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  108. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  109. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  110. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  111. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  112. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  113. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  114. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  115. implementation
  116. uses
  117. globtype,globals,verbose,systems,cutils,
  118. symconst,symdef,symsym,
  119. rgobj,tgobj,cpupi,rgcpu,procinfo;
  120. procedure tcgppc.init_register_allocators;
  121. begin
  122. rg := trgcpu.create(29,chr(ord(RS_R3))+chr(ord(RS_R4))+chr(ord(RS_R5))+chr(ord(RS_R6))+chr(ord(RS_R7))+chr(ord(RS_R8))+
  123. chr(ord(RS_R9))+chr(ord(RS_R10))+chr(ord(RS_R11))+chr(ord(RS_R12))+chr(ord(RS_R31))+chr(ord(RS_R30))+chr(ord(RS_R29))+
  124. chr(ord(RS_R28))+chr(ord(RS_R27))+chr(ord(RS_R26))+chr(ord(RS_R25))+chr(ord(RS_R24))+chr(ord(RS_R23))+chr(ord(RS_R22))+
  125. chr(ord(RS_R21))+chr(ord(RS_R20))+chr(ord(RS_R19))+chr(ord(RS_R18))+chr(ord(RS_R17))+chr(ord(RS_R16))+chr(ord(RS_R15))+
  126. chr(ord(RS_R14))+chr(ord(RS_R13)));
  127. end;
  128. procedure tcgppc.done_register_allocators;
  129. begin
  130. rg.free;
  131. end;
  132. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  133. var
  134. ref: treference;
  135. begin
  136. case locpara.loc of
  137. LOC_REGISTER,LOC_CREGISTER:
  138. a_load_const_reg(list,size,a,locpara.register);
  139. LOC_REFERENCE:
  140. begin
  141. reference_reset(ref);
  142. ref.base:=locpara.reference.index;
  143. ref.offset:=locpara.reference.offset;
  144. a_load_const_ref(list,size,a,ref);
  145. end;
  146. else
  147. internalerror(2002081101);
  148. end;
  149. if locpara.sp_fixup<>0 then
  150. internalerror(2002081102);
  151. end;
  152. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  153. var
  154. ref: treference;
  155. tmpreg: tregister;
  156. begin
  157. case locpara.loc of
  158. LOC_REGISTER,LOC_CREGISTER:
  159. a_load_ref_reg(list,size,size,r,locpara.register);
  160. LOC_REFERENCE:
  161. begin
  162. reference_reset(ref);
  163. ref.base:=locpara.reference.index;
  164. ref.offset:=locpara.reference.offset;
  165. tmpreg := rg.getregisterint(list,size);
  166. a_load_ref_reg(list,size,size,r,tmpreg);
  167. a_load_reg_ref(list,size,size,tmpreg,ref);
  168. rg.ungetregisterint(list,tmpreg);
  169. end;
  170. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  171. case size of
  172. OS_F32, OS_F64:
  173. a_loadfpu_ref_reg(list,size,r,locpara.register);
  174. else
  175. internalerror(2002072801);
  176. end;
  177. else
  178. internalerror(2002081103);
  179. end;
  180. if locpara.sp_fixup<>0 then
  181. internalerror(2002081104);
  182. end;
  183. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  184. var
  185. ref: treference;
  186. tmpreg: tregister;
  187. begin
  188. case locpara.loc of
  189. LOC_REGISTER,LOC_CREGISTER:
  190. a_loadaddr_ref_reg(list,r,locpara.register);
  191. LOC_REFERENCE:
  192. begin
  193. reference_reset(ref);
  194. ref.base := locpara.reference.index;
  195. ref.offset := locpara.reference.offset;
  196. tmpreg := rg.getregisterint(list,OS_ADDR);
  197. a_loadaddr_ref_reg(list,r,tmpreg);
  198. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  199. rg.ungetregisterint(list,tmpreg);
  200. end;
  201. else
  202. internalerror(2002080701);
  203. end;
  204. end;
  205. { calling a procedure by name }
  206. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  207. var
  208. href : treference;
  209. begin
  210. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  211. if it is a cross-TOC call. If so, it also replaces the NOP
  212. with some restore code.}
  213. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  214. if target_info.system=system_powerpc_macos then
  215. list.concat(taicpu.op_none(A_NOP));
  216. if not(pi_do_call in current_procinfo.flags) then
  217. internalerror(2003060703);
  218. end;
  219. { calling a procedure by address }
  220. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  221. var
  222. tmpreg : tregister;
  223. tmpref : treference;
  224. begin
  225. if target_info.system=system_powerpc_macos then
  226. begin
  227. {Generate instruction to load the procedure address from
  228. the transition vector.}
  229. //TODO: Support cross-TOC calls.
  230. tmpreg := rg.getregisterint(list,OS_INT);
  231. reference_reset(tmpref);
  232. tmpref.offset := 0;
  233. //tmpref.symaddr := refs_full;
  234. tmpref.base:= reg;
  235. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  236. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  237. rg.ungetregisterint(list,tmpreg);
  238. end
  239. else
  240. list.concat(taicpu.op_reg(A_MTCTR,reg));
  241. list.concat(taicpu.op_none(A_BCTRL));
  242. //if target_info.system=system_powerpc_macos then
  243. // //NOP is not needed here.
  244. // list.concat(taicpu.op_none(A_NOP));
  245. if not(pi_do_call in current_procinfo.flags) then
  246. internalerror(2003060704);
  247. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  248. end;
  249. {********************** load instructions ********************}
  250. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  251. begin
  252. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  253. internalerror(2002090902);
  254. if (longint(a) >= low(smallint)) and
  255. (longint(a) <= high(smallint)) then
  256. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  257. else if ((a and $ffff) <> 0) then
  258. begin
  259. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  260. if ((a shr 16) <> 0) or
  261. (smallint(a and $ffff) < 0) then
  262. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  263. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  264. end
  265. else
  266. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  267. end;
  268. procedure tcgppc.a_load_reg_ref(list : taasmoutput; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  269. const
  270. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  271. { indexed? updating?}
  272. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  273. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  274. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  275. var
  276. op: TAsmOp;
  277. ref2: TReference;
  278. freereg: boolean;
  279. begin
  280. ref2 := ref;
  281. freereg := fixref(list,ref2);
  282. if tosize in [OS_S8..OS_S16] then
  283. { storing is the same for signed and unsigned values }
  284. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  285. { 64 bit stuff should be handled separately }
  286. if tosize in [OS_64,OS_S64] then
  287. internalerror(200109236);
  288. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  289. a_load_store(list,op,reg,ref2);
  290. if freereg then
  291. rg.ungetregisterint(list,ref2.base);
  292. End;
  293. procedure tcgppc.a_load_ref_reg(list : taasmoutput; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  294. const
  295. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  296. { indexed? updating?}
  297. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  298. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  299. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  300. { 64bit stuff should be handled separately }
  301. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  302. { there's no load-byte-with-sign-extend :( }
  303. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  304. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  305. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  306. var
  307. op: tasmop;
  308. tmpreg: tregister;
  309. ref2, tmpref: treference;
  310. freereg: boolean;
  311. begin
  312. { TODO: optimize/take into consideration fromsize/tosize. Will }
  313. { probably only matter for OS_S8 loads though }
  314. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  315. internalerror(2002090902);
  316. ref2 := ref;
  317. freereg := fixref(list,ref2);
  318. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  319. a_load_store(list,op,reg,ref2);
  320. if freereg then
  321. rg.ungetregisterint(list,ref2.base);
  322. { sign extend shortint if necessary, since there is no }
  323. { load instruction that does that automatically (JM) }
  324. if fromsize = OS_S8 then
  325. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  326. end;
  327. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  328. var
  329. instr: taicpu;
  330. begin
  331. if (reg1<>reg2) or
  332. (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  333. ((tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  334. (tosize <> fromsize) and
  335. not(fromsize in [OS_32,OS_S32])) then
  336. begin
  337. case tosize of
  338. OS_8:
  339. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  340. reg2,reg1,0,31-8+1,31);
  341. OS_S8:
  342. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  343. OS_16:
  344. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  345. reg2,reg1,0,31-16+1,31);
  346. OS_S16:
  347. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  348. OS_32,OS_S32:
  349. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  350. else internalerror(2002090901);
  351. end;
  352. list.concat(instr);
  353. rg.add_move_instruction(instr);
  354. end;
  355. end;
  356. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  357. begin
  358. list.concat(taicpu.op_reg_reg(A_FMR,reg2,reg1));
  359. end;
  360. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  361. const
  362. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  363. { indexed? updating?}
  364. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  365. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  366. var
  367. op: tasmop;
  368. ref2: treference;
  369. freereg: boolean;
  370. begin
  371. { several functions call this procedure with OS_32 or OS_64 }
  372. { so this makes life easier (FK) }
  373. case size of
  374. OS_32,OS_F32:
  375. size:=OS_F32;
  376. OS_64,OS_F64,OS_C64:
  377. size:=OS_F64;
  378. else
  379. internalerror(200201121);
  380. end;
  381. ref2 := ref;
  382. freereg := fixref(list,ref2);
  383. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  384. a_load_store(list,op,reg,ref2);
  385. if freereg then
  386. rg.ungetregisterint(list,ref2.base);
  387. end;
  388. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  389. const
  390. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  391. { indexed? updating?}
  392. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  393. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  394. var
  395. op: tasmop;
  396. ref2: treference;
  397. freereg: boolean;
  398. begin
  399. if not(size in [OS_F32,OS_F64]) then
  400. internalerror(200201122);
  401. ref2 := ref;
  402. freereg := fixref(list,ref2);
  403. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  404. a_load_store(list,op,reg,ref2);
  405. if freereg then
  406. rg.ungetregisterint(list,ref2.base);
  407. end;
  408. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  409. begin
  410. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  411. end;
  412. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  413. begin
  414. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  415. end;
  416. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  417. size: tcgsize; a: aword; src, dst: tregister);
  418. var
  419. l1,l2: longint;
  420. oplo, ophi: tasmop;
  421. scratchreg: tregister;
  422. useReg, gotrlwi: boolean;
  423. procedure do_lo_hi;
  424. begin
  425. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  426. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  427. end;
  428. begin
  429. if op = OP_SUB then
  430. begin
  431. {$ifopt q+}
  432. {$q-}
  433. {$define overflowon}
  434. {$endif}
  435. a_op_const_reg_reg(list,OP_ADD,size,aword(-longint(a)),src,dst);
  436. {$ifdef overflowon}
  437. {$q+}
  438. {$undef overflowon}
  439. {$endif}
  440. exit;
  441. end;
  442. ophi := TOpCG2AsmOpConstHi[op];
  443. oplo := TOpCG2AsmOpConstLo[op];
  444. gotrlwi := get_rlwi_const(a,l1,l2);
  445. if (op in [OP_AND,OP_OR,OP_XOR]) then
  446. begin
  447. if (a = 0) then
  448. begin
  449. if op = OP_AND then
  450. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  451. else
  452. a_load_reg_reg(list,size,size,src,dst);
  453. exit;
  454. end
  455. else if (a = high(aword)) then
  456. begin
  457. case op of
  458. OP_OR:
  459. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  460. OP_XOR:
  461. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  462. OP_AND:
  463. a_load_reg_reg(list,size,size,src,dst);
  464. end;
  465. exit;
  466. end
  467. else if (a <= high(word)) and
  468. ((op <> OP_AND) or
  469. not gotrlwi) then
  470. begin
  471. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  472. exit;
  473. end;
  474. { all basic constant instructions also have a shifted form that }
  475. { works only on the highest 16bits, so if lo(a) is 0, we can }
  476. { use that one }
  477. if (word(a) = 0) and
  478. (not(op = OP_AND) or
  479. not gotrlwi) then
  480. begin
  481. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  482. exit;
  483. end;
  484. end
  485. else if (op = OP_ADD) then
  486. if a = 0 then
  487. exit
  488. else if (longint(a) >= low(smallint)) and
  489. (longint(a) <= high(smallint)) then
  490. begin
  491. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  492. exit;
  493. end;
  494. { otherwise, the instructions we can generate depend on the }
  495. { operation }
  496. useReg := false;
  497. case op of
  498. OP_DIV,OP_IDIV:
  499. if (a = 0) then
  500. internalerror(200208103)
  501. else if (a = 1) then
  502. begin
  503. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  504. exit
  505. end
  506. else if ispowerof2(a,l1) then
  507. begin
  508. case op of
  509. OP_DIV:
  510. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  511. OP_IDIV:
  512. begin
  513. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  514. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  515. end;
  516. end;
  517. exit;
  518. end
  519. else
  520. usereg := true;
  521. OP_IMUL, OP_MUL:
  522. if (a = 0) then
  523. begin
  524. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  525. exit
  526. end
  527. else if (a = 1) then
  528. begin
  529. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  530. exit
  531. end
  532. else if ispowerof2(a,l1) then
  533. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  534. else if (longint(a) >= low(smallint)) and
  535. (longint(a) <= high(smallint)) then
  536. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  537. else
  538. usereg := true;
  539. OP_ADD:
  540. begin
  541. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  542. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  543. smallint((a shr 16) + ord(smallint(a) < 0))));
  544. end;
  545. OP_OR:
  546. { try to use rlwimi }
  547. if gotrlwi and
  548. (src = dst) then
  549. begin
  550. scratchreg := rg.getregisterint(list,OS_INT);
  551. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  552. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  553. scratchreg,0,l1,l2));
  554. rg.ungetregisterint(list,scratchreg);
  555. end
  556. else
  557. do_lo_hi;
  558. OP_AND:
  559. { try to use rlwinm }
  560. if gotrlwi then
  561. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  562. src,0,l1,l2))
  563. else
  564. useReg := true;
  565. OP_XOR:
  566. do_lo_hi;
  567. OP_SHL,OP_SHR,OP_SAR:
  568. begin
  569. if (a and 31) <> 0 Then
  570. list.concat(taicpu.op_reg_reg_const(
  571. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  572. else
  573. a_load_reg_reg(list,size,size,src,dst);
  574. if (a shr 5) <> 0 then
  575. internalError(68991);
  576. end
  577. else
  578. internalerror(200109091);
  579. end;
  580. { if all else failed, load the constant in a register and then }
  581. { perform the operation }
  582. if useReg then
  583. begin
  584. scratchreg := rg.getregisterint(list,OS_INT);
  585. a_load_const_reg(list,OS_32,a,scratchreg);
  586. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  587. rg.ungetregisterint(list,scratchreg);
  588. end;
  589. end;
  590. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  591. size: tcgsize; src1, src2, dst: tregister);
  592. const
  593. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  594. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  595. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  596. begin
  597. case op of
  598. OP_NEG,OP_NOT:
  599. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  600. else
  601. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  602. end;
  603. end;
  604. {*************** compare instructructions ****************}
  605. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  606. l : tasmlabel);
  607. var
  608. p: taicpu;
  609. scratch_register: TRegister;
  610. signed: boolean;
  611. begin
  612. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  613. { in the following case, we generate more efficient code when }
  614. { signed is true }
  615. if (cmp_op in [OC_EQ,OC_NE]) and
  616. (a > $ffff) then
  617. signed := true;
  618. if signed then
  619. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  620. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,longint(a)))
  621. else
  622. begin
  623. scratch_register := rg.getregisterint(list,OS_INT);
  624. a_load_const_reg(list,OS_32,a,scratch_register);
  625. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  626. rg.ungetregisterint(list,scratch_register);
  627. end
  628. else
  629. if (a <= $ffff) then
  630. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,a))
  631. else
  632. begin
  633. scratch_register := rg.getregisterint(list,OS_INT);
  634. a_load_const_reg(list,OS_32,a,scratch_register);
  635. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  636. rg.ungetregisterint(list,scratch_register);
  637. end;
  638. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  639. end;
  640. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  641. reg1,reg2 : tregister;l : tasmlabel);
  642. var
  643. p: taicpu;
  644. op: tasmop;
  645. begin
  646. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  647. op := A_CMPW
  648. else
  649. op := A_CMPLW;
  650. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  651. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  652. end;
  653. procedure tcgppc.g_save_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);
  654. begin
  655. {$warning FIX ME}
  656. end;
  657. procedure tcgppc.g_restore_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);
  658. begin
  659. {$warning FIX ME}
  660. end;
  661. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  662. begin
  663. {$warning FIX ME}
  664. end;
  665. procedure tcgppc.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  666. begin
  667. {$warning FIX ME}
  668. end;
  669. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  670. begin
  671. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  672. end;
  673. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  674. begin
  675. a_jmp(list,A_B,C_None,0,l);
  676. end;
  677. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  678. var
  679. c: tasmcond;
  680. begin
  681. c := flags_to_cond(f);
  682. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  683. end;
  684. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  685. var
  686. testbit: byte;
  687. bitvalue: boolean;
  688. begin
  689. { get the bit to extract from the conditional register + its }
  690. { requested value (0 or 1) }
  691. testbit := ((f.cr-RS_CR0) * 4);
  692. case f.flag of
  693. F_EQ,F_NE:
  694. begin
  695. inc(testbit,2);
  696. bitvalue := f.flag = F_EQ;
  697. end;
  698. F_LT,F_GE:
  699. begin
  700. bitvalue := f.flag = F_LT;
  701. end;
  702. F_GT,F_LE:
  703. begin
  704. inc(testbit);
  705. bitvalue := f.flag = F_GT;
  706. end;
  707. else
  708. internalerror(200112261);
  709. end;
  710. { load the conditional register in the destination reg }
  711. list.concat(taicpu.op_reg(A_MFCR,reg));
  712. { we will move the bit that has to be tested to bit 0 by rotating }
  713. { left }
  714. testbit := (testbit + 1) and 31;
  715. { extract bit }
  716. list.concat(taicpu.op_reg_reg_const_const_const(
  717. A_RLWINM,reg,reg,testbit,31,31));
  718. { if we need the inverse, xor with 1 }
  719. if not bitvalue then
  720. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  721. end;
  722. (*
  723. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  724. var
  725. testbit: byte;
  726. bitvalue: boolean;
  727. begin
  728. { get the bit to extract from the conditional register + its }
  729. { requested value (0 or 1) }
  730. case f.simple of
  731. false:
  732. begin
  733. { we don't generate this in the compiler }
  734. internalerror(200109062);
  735. end;
  736. true:
  737. case f.cond of
  738. C_None:
  739. internalerror(200109063);
  740. C_LT..C_NU:
  741. begin
  742. testbit := (ord(f.cr) - ord(R_CR0))*4;
  743. inc(testbit,AsmCondFlag2BI[f.cond]);
  744. bitvalue := AsmCondFlagTF[f.cond];
  745. end;
  746. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  747. begin
  748. testbit := f.crbit
  749. bitvalue := AsmCondFlagTF[f.cond];
  750. end;
  751. else
  752. internalerror(200109064);
  753. end;
  754. end;
  755. { load the conditional register in the destination reg }
  756. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  757. { we will move the bit that has to be tested to bit 31 -> rotate }
  758. { left by bitpos+1 (remember, this is big-endian!) }
  759. if bitpos <> 31 then
  760. inc(bitpos)
  761. else
  762. bitpos := 0;
  763. { extract bit }
  764. list.concat(taicpu.op_reg_reg_const_const_const(
  765. A_RLWINM,reg,reg,bitpos,31,31));
  766. { if we need the inverse, xor with 1 }
  767. if not bitvalue then
  768. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  769. end;
  770. *)
  771. { *********** entry/exit code and address loading ************ }
  772. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  773. { generated the entry code of a procedure/function. Note: localsize is the }
  774. { sum of the size necessary for local variables and the maximum possible }
  775. { combined size of ALL the parameters of a procedure called by the current }
  776. { one. }
  777. { This procedure may be called before, as well as after
  778. g_return_from_proc is called.}
  779. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  780. href,href2 : treference;
  781. usesfpr,usesgpr,gotgot : boolean;
  782. parastart : aword;
  783. offset : aword;
  784. // r,r2,rsp:Tregister;
  785. regcounter2: Tsuperregister;
  786. regidx : tregisterindex;
  787. hp: tparaitem;
  788. begin
  789. { CR and LR only have to be saved in case they are modified by the current }
  790. { procedure, but currently this isn't checked, so save them always }
  791. { following is the entry code as described in "Altivec Programming }
  792. { Interface Manual", bar the saving of AltiVec registers }
  793. a_reg_alloc(list,NR_STACK_POINTER_REG);
  794. a_reg_alloc(list,NR_R0);
  795. if current_procinfo.procdef.parast.symtablelevel>1 then
  796. a_reg_alloc(list,NR_R11);
  797. usesfpr:=false;
  798. if not (po_assembler in current_procinfo.procdef.procoptions) then
  799. {$warning FIXME!!}
  800. { FIXME: has to be R_F8 instad of R_F14 for SYSV abi }
  801. for regcounter:=RS_F14 to RS_F31 do
  802. begin
  803. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  804. if regidx in rg.used_in_proc_other then
  805. begin
  806. usesfpr:= true;
  807. firstregfpu:=regcounter;
  808. break;
  809. end;
  810. end;
  811. usesgpr:=false;
  812. if not (po_assembler in current_procinfo.procdef.procoptions) then
  813. for regcounter2:=firstsaveintreg to RS_R31 do
  814. begin
  815. if regcounter2 in rg.used_in_proc_int then
  816. begin
  817. usesgpr:=true;
  818. firstreggpr:=regcounter2;
  819. break;
  820. end;
  821. end;
  822. { save link register? }
  823. if not (po_assembler in current_procinfo.procdef.procoptions) then
  824. if (pi_do_call in current_procinfo.flags) then
  825. begin
  826. { save return address... }
  827. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  828. { ... in caller's frame }
  829. case target_info.abi of
  830. abi_powerpc_aix:
  831. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  832. abi_powerpc_sysv:
  833. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  834. end;
  835. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  836. a_reg_dealloc(list,NR_R0);
  837. end;
  838. { save the CR if necessary in callers frame. }
  839. if not (po_assembler in current_procinfo.procdef.procoptions) then
  840. if target_info.abi = abi_powerpc_aix then
  841. if false then { Not needed at the moment. }
  842. begin
  843. a_reg_alloc(list,NR_R0);
  844. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  845. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  846. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  847. a_reg_dealloc(list,NR_R0);
  848. end;
  849. { !!! always allocate space for all registers for now !!! }
  850. if not (po_assembler in current_procinfo.procdef.procoptions) then
  851. { if usesfpr or usesgpr then }
  852. begin
  853. a_reg_alloc(list,NR_R12);
  854. { save end of fpr save area }
  855. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  856. end;
  857. if (localsize <> 0) then
  858. begin
  859. if (localsize <= high(smallint)) then
  860. begin
  861. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  862. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  863. end
  864. else
  865. begin
  866. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  867. { can't use getregisterint here, the register colouring }
  868. { is already done when we get here }
  869. href.index := NR_R11;
  870. a_reg_alloc(list,href.index);
  871. a_load_const_reg(list,OS_S32,-localsize,href.index);
  872. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  873. a_reg_dealloc(list,href.index);
  874. end;
  875. end;
  876. { no GOT pointer loaded yet }
  877. gotgot:=false;
  878. if usesfpr then
  879. begin
  880. { save floating-point registers
  881. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  882. begin
  883. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g');
  884. gotgot:=true;
  885. end
  886. else
  887. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14));
  888. }
  889. reference_reset_base(href,NR_R12,-8);
  890. for regcounter:=firstregfpu to RS_F31 do
  891. begin
  892. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  893. if regidx in rg.used_in_proc_other then
  894. begin
  895. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  896. dec(href.offset,8);
  897. end;
  898. end;
  899. { compute end of gpr save area }
  900. a_op_const_reg(list,OP_ADD,OS_ADDR,aword(href.offset+8),NR_R12);
  901. end;
  902. { save gprs and fetch GOT pointer }
  903. if usesgpr then
  904. begin
  905. {
  906. if cs_create_pic in aktmoduleswitches then
  907. begin
  908. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g');
  909. gotgot:=true;
  910. end
  911. else
  912. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14))
  913. }
  914. reference_reset_base(href,NR_R12,-4);
  915. for regcounter2:=firstsaveintreg to RS_R31 do
  916. begin
  917. if regcounter2 in rg.used_in_proc_int then
  918. begin
  919. usesgpr:=true;
  920. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter2,R_SUBNONE),href);
  921. dec(href.offset,4);
  922. end;
  923. end;
  924. {
  925. r.enum:=R_INTREGISTER;
  926. r.:=;
  927. reference_reset_base(href,NR_R12,-((NR_R31-firstreggpr) shr 8+1)*4);
  928. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  929. }
  930. end;
  931. if assigned(current_procinfo.procdef.parast) then
  932. begin
  933. if not (po_assembler in current_procinfo.procdef.procoptions) then
  934. begin
  935. { copy memory parameters to local parast }
  936. hp:=tparaitem(current_procinfo.procdef.para.first);
  937. while assigned(hp) do
  938. begin
  939. if (hp.paraloc[calleeside].loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  940. begin
  941. if tvarsym(hp.parasym).localloc.loc<>LOC_REFERENCE then
  942. internalerror(200310011);
  943. reference_reset_base(href,tvarsym(hp.parasym).localloc.reference.index,tvarsym(hp.parasym).localloc.reference.offset);
  944. reference_reset_base(href2,NR_R12,hp.paraloc[callerside].reference.offset);
  945. cg.a_load_ref_ref(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,href);
  946. end
  947. {$ifdef dummy}
  948. else if (hp.calleeparaloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  949. begin
  950. rg.getexplicitregisterint(list,hp.calleeparaloc.register);
  951. end
  952. {$endif dummy}
  953. ;
  954. hp := tparaitem(hp.next);
  955. end;
  956. end;
  957. end;
  958. if usesfpr or usesgpr then
  959. a_reg_dealloc(list,NR_R12);
  960. { PIC code support, }
  961. if cs_create_pic in aktmoduleswitches then
  962. begin
  963. { if we didn't get the GOT pointer till now, we've to calculate it now }
  964. if not(gotgot) then
  965. begin
  966. {!!!!!!!!!!!!!}
  967. end;
  968. a_reg_alloc(list,NR_R31);
  969. { place GOT ptr in r31 }
  970. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  971. end;
  972. { save the CR if necessary ( !!! always done currently ) }
  973. { still need to find out where this has to be done for SystemV
  974. a_reg_alloc(list,R_0);
  975. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  976. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  977. new_reference(STACK_POINTER_REG,LA_CR)));
  978. a_reg_dealloc(list,R_0); }
  979. { now comes the AltiVec context save, not yet implemented !!! }
  980. { if we're in a nested procedure, we've to save R11 }
  981. if current_procinfo.procdef.parast.symtablelevel>2 then
  982. begin
  983. reference_reset_base(href,NR_STACK_POINTER_REG,PARENT_FRAMEPOINTER_OFFSET);
  984. list.concat(taicpu.op_reg_ref(A_STW,NR_R11,href));
  985. end;
  986. end;
  987. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  988. { This procedure may be called before, as well as after
  989. g_stackframe_entry is called.}
  990. var
  991. regcounter,firstregfpu,firstreggpr: TsuperRegister;
  992. href : treference;
  993. usesfpr,usesgpr,genret : boolean;
  994. regcounter2:Tsuperregister;
  995. localsize: aword;
  996. regidx : tregisterindex;
  997. begin
  998. { AltiVec context restore, not yet implemented !!! }
  999. usesfpr:=false;
  1000. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1001. for regcounter:=RS_F14 to RS_F31 do
  1002. begin
  1003. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1004. if regidx in rg.used_in_proc_other then
  1005. begin
  1006. usesfpr:=true;
  1007. firstregfpu:=regcounter;
  1008. break;
  1009. end;
  1010. end;
  1011. usesgpr:=false;
  1012. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1013. for regcounter2:=firstsaveintreg to RS_R31 do
  1014. begin
  1015. if regcounter2 in rg.used_in_proc_int then
  1016. begin
  1017. usesgpr:=true;
  1018. firstreggpr:=regcounter2;
  1019. break;
  1020. end;
  1021. end;
  1022. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1023. { no return (blr) generated yet }
  1024. genret:=true;
  1025. if usesgpr or usesfpr then
  1026. begin
  1027. { address of gpr save area to r11 }
  1028. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12);
  1029. if usesfpr then
  1030. begin
  1031. reference_reset_base(href,NR_R12,-8);
  1032. for regcounter := firstregfpu to RS_F31 do
  1033. begin
  1034. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1035. if regidx in rg.used_in_proc_other then
  1036. begin
  1037. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1038. dec(href.offset,8);
  1039. end;
  1040. end;
  1041. inc(href.offset,4);
  1042. end
  1043. else
  1044. reference_reset_base(href,NR_R12,-4);
  1045. for regcounter2:=firstsaveintreg to RS_R31 do
  1046. begin
  1047. if regcounter2 in rg.used_in_proc_int then
  1048. begin
  1049. usesgpr:=true;
  1050. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter2,R_SUBNONE));
  1051. dec(href.offset,4);
  1052. end;
  1053. end;
  1054. (*
  1055. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr)) shr 8+1)*4);
  1056. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1057. *)
  1058. end;
  1059. (*
  1060. { restore fprs and return }
  1061. if usesfpr then
  1062. begin
  1063. { address of fpr save area to r11 }
  1064. r:=NR_R12;
  1065. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1066. {
  1067. if (pi_do_call in current_procinfo.flags) then
  1068. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1069. '_x')
  1070. else
  1071. { leaf node => lr haven't to be restored }
  1072. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1073. '_l');
  1074. genret:=false;
  1075. }
  1076. end;
  1077. *)
  1078. { if we didn't generate the return code, we've to do it now }
  1079. if genret then
  1080. begin
  1081. { adjust r1 }
  1082. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1083. { load link register? }
  1084. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1085. begin
  1086. if (pi_do_call in current_procinfo.flags) then
  1087. begin
  1088. case target_info.abi of
  1089. abi_powerpc_aix:
  1090. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1091. abi_powerpc_sysv:
  1092. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1093. end;
  1094. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1095. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1096. end;
  1097. { restore the CR if necessary from callers frame}
  1098. if target_info.abi = abi_powerpc_aix then
  1099. if false then { Not needed at the moment. }
  1100. begin
  1101. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1102. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1103. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1104. a_reg_dealloc(list,NR_R0);
  1105. end;
  1106. end;
  1107. list.concat(taicpu.op_none(A_BLR));
  1108. end;
  1109. end;
  1110. function save_regs(list : taasmoutput):longint;
  1111. {Generates code which saves used non-volatile registers in
  1112. the save area right below the address the stackpointer point to.
  1113. Returns the actual used save area size.}
  1114. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1115. usesfpr,usesgpr: boolean;
  1116. href : treference;
  1117. offset: integer;
  1118. regcounter2: Tsuperregister;
  1119. regidx : tregisterindex;
  1120. begin
  1121. usesfpr:=false;
  1122. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1123. for regcounter:=RS_F14 to RS_F31 do
  1124. begin
  1125. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1126. if regidx in rg.used_in_proc_other then
  1127. begin
  1128. usesfpr:=true;
  1129. firstregfpu:=regcounter;
  1130. break;
  1131. end;
  1132. end;
  1133. usesgpr:=false;
  1134. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1135. for regcounter2:=firstsaveintreg to RS_R31 do
  1136. begin
  1137. if regcounter2 in rg.used_in_proc_int then
  1138. begin
  1139. usesgpr:=true;
  1140. firstreggpr:=regcounter2;
  1141. break;
  1142. end;
  1143. end;
  1144. offset:= 0;
  1145. { save floating-point registers }
  1146. if usesfpr then
  1147. for regcounter := firstregfpu to RS_F31 do
  1148. begin
  1149. offset:= offset - 8;
  1150. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1151. list.concat(taicpu.op_reg_ref(A_STFD, regcounter, href));
  1152. end;
  1153. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1154. { save gprs in gpr save area }
  1155. if usesgpr then
  1156. if firstreggpr < RS_R30 then
  1157. begin
  1158. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1159. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1160. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1161. {STMW stores multiple registers}
  1162. end
  1163. else
  1164. begin
  1165. for regcounter := firstreggpr to RS_R31 do
  1166. begin
  1167. offset:= offset - 4;
  1168. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1169. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1170. end;
  1171. end;
  1172. { now comes the AltiVec context save, not yet implemented !!! }
  1173. save_regs:= -offset;
  1174. end;
  1175. procedure restore_regs(list : taasmoutput);
  1176. {Generates code which restores used non-volatile registers from
  1177. the save area right below the address the stackpointer point to.}
  1178. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1179. usesfpr,usesgpr: boolean;
  1180. href : treference;
  1181. offset: integer;
  1182. regcounter2: Tsuperregister;
  1183. regidx : tregisterindex;
  1184. begin
  1185. usesfpr:=false;
  1186. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1187. for regcounter:=RS_F14 to RS_F31 do
  1188. begin
  1189. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1190. if regidx in rg.used_in_proc_other then
  1191. begin
  1192. usesfpr:=true;
  1193. firstregfpu:=regcounter;
  1194. break;
  1195. end;
  1196. end;
  1197. usesgpr:=false;
  1198. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1199. for regcounter2:=RS_R13 to RS_R31 do
  1200. begin
  1201. if regcounter2 in rg.used_in_proc_int then
  1202. begin
  1203. usesgpr:=true;
  1204. firstreggpr:=regcounter2;
  1205. break;
  1206. end;
  1207. end;
  1208. offset:= 0;
  1209. { restore fp registers }
  1210. if usesfpr then
  1211. for regcounter := firstregfpu to RS_F31 do
  1212. begin
  1213. offset:= offset - 8;
  1214. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1215. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1216. end;
  1217. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1218. { restore gprs }
  1219. if usesgpr then
  1220. if firstreggpr < RS_R30 then
  1221. begin
  1222. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1223. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1224. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1225. {LMW loads multiple registers}
  1226. end
  1227. else
  1228. begin
  1229. for regcounter := firstreggpr to RS_R31 do
  1230. begin
  1231. offset:= offset - 4;
  1232. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1233. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1234. end;
  1235. end;
  1236. { now comes the AltiVec context restore, not yet implemented !!! }
  1237. end;
  1238. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1239. (* NOT IN USE *)
  1240. { generated the entry code of a procedure/function. Note: localsize is the }
  1241. { sum of the size necessary for local variables and the maximum possible }
  1242. { combined size of ALL the parameters of a procedure called by the current }
  1243. { one }
  1244. const
  1245. macosLinkageAreaSize = 24;
  1246. var regcounter: TRegister;
  1247. href : treference;
  1248. registerSaveAreaSize : longint;
  1249. begin
  1250. if (localsize mod 8) <> 0 then
  1251. internalerror(58991);
  1252. { CR and LR only have to be saved in case they are modified by the current }
  1253. { procedure, but currently this isn't checked, so save them always }
  1254. { following is the entry code as described in "Altivec Programming }
  1255. { Interface Manual", bar the saving of AltiVec registers }
  1256. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1257. a_reg_alloc(list,NR_R0);
  1258. { save return address in callers frame}
  1259. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1260. { ... in caller's frame }
  1261. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1262. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1263. a_reg_dealloc(list,NR_R0);
  1264. { save non-volatile registers in callers frame}
  1265. registerSaveAreaSize:= save_regs(list);
  1266. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1267. a_reg_alloc(list,NR_R0);
  1268. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1269. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1270. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1271. a_reg_dealloc(list,NR_R0);
  1272. (*
  1273. { save pointer to incoming arguments }
  1274. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1275. *)
  1276. (*
  1277. a_reg_alloc(list,R_12);
  1278. { 0 or 8 based on SP alignment }
  1279. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1280. R_12,STACK_POINTER_REG,0,28,28));
  1281. { add in stack length }
  1282. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1283. -localsize));
  1284. { establish new alignment }
  1285. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1286. a_reg_dealloc(list,R_12);
  1287. *)
  1288. { allocate stack frame }
  1289. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1290. inc(localsize,tg.lasttemp);
  1291. localsize:=align(localsize,16);
  1292. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1293. if (localsize <> 0) then
  1294. begin
  1295. if (localsize <= high(smallint)) then
  1296. begin
  1297. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1298. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1299. end
  1300. else
  1301. begin
  1302. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1303. href.index := NR_R11;
  1304. a_reg_alloc(list,href.index);
  1305. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1306. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1307. a_reg_dealloc(list,href.index);
  1308. end;
  1309. end;
  1310. end;
  1311. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1312. (* NOT IN USE *)
  1313. var
  1314. href : treference;
  1315. begin
  1316. a_reg_alloc(list,NR_R0);
  1317. { restore stack pointer }
  1318. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1319. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1320. (*
  1321. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1322. *)
  1323. { restore the CR if necessary from callers frame
  1324. ( !!! always done currently ) }
  1325. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1326. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1327. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1328. a_reg_dealloc(list,NR_R0);
  1329. (*
  1330. { restore return address from callers frame }
  1331. reference_reset_base(href,STACK_POINTER_REG,8);
  1332. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1333. *)
  1334. { restore non-volatile registers from callers frame }
  1335. restore_regs(list);
  1336. (*
  1337. { return to caller }
  1338. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1339. list.concat(taicpu.op_none(A_BLR));
  1340. *)
  1341. { restore return address from callers frame }
  1342. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1343. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1344. { return to caller }
  1345. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1346. list.concat(taicpu.op_none(A_BLR));
  1347. end;
  1348. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  1349. begin
  1350. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  1351. end;
  1352. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1353. var
  1354. ref2, tmpref: treference;
  1355. freereg: boolean;
  1356. tmpreg:Tregister;
  1357. begin
  1358. ref2 := ref;
  1359. freereg := fixref(list,ref2);
  1360. if assigned(ref2.symbol) then
  1361. begin
  1362. if target_info.system = system_powerpc_macos then
  1363. begin
  1364. if macos_direct_globals then
  1365. begin
  1366. reference_reset(tmpref);
  1367. tmpref.offset := ref2.offset;
  1368. tmpref.symbol := ref2.symbol;
  1369. tmpref.base := NR_NO;
  1370. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1371. end
  1372. else
  1373. begin
  1374. reference_reset(tmpref);
  1375. tmpref.symbol := ref2.symbol;
  1376. tmpref.offset := 0;
  1377. tmpref.base := NR_RTOC;
  1378. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1379. if ref2.offset <> 0 then
  1380. begin
  1381. reference_reset(tmpref);
  1382. tmpref.offset := ref2.offset;
  1383. tmpref.base:= r;
  1384. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1385. end;
  1386. end;
  1387. if ref2.base <> NR_NO then
  1388. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1389. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1390. end
  1391. else
  1392. begin
  1393. { add the symbol's value to the base of the reference, and if the }
  1394. { reference doesn't have a base, create one }
  1395. reference_reset(tmpref);
  1396. tmpref.offset := ref2.offset;
  1397. tmpref.symbol := ref2.symbol;
  1398. tmpref.symaddr := refs_ha;
  1399. if ref2.base<> NR_NO then
  1400. begin
  1401. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1402. ref2.base,tmpref));
  1403. if freereg then
  1404. begin
  1405. rg.ungetregisterint(list,ref2.base);
  1406. freereg := false;
  1407. end;
  1408. end
  1409. else
  1410. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1411. tmpref.base := NR_NO;
  1412. tmpref.symaddr := refs_l;
  1413. { can be folded with one of the next instructions by the }
  1414. { optimizer probably }
  1415. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1416. end
  1417. end
  1418. else if ref2.offset <> 0 Then
  1419. if ref2.base <> NR_NO then
  1420. a_op_const_reg_reg(list,OP_ADD,OS_32,aword(ref2.offset),ref2.base,r)
  1421. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1422. { occurs, so now only ref.offset has to be loaded }
  1423. else
  1424. a_load_const_reg(list,OS_32,ref2.offset,r)
  1425. else if ref.index <> NR_NO Then
  1426. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1427. else if (ref2.base <> NR_NO) and
  1428. (r <> ref2.base) then
  1429. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1430. if freereg then
  1431. rg.ungetregisterint(list,ref2.base);
  1432. end;
  1433. { ************* concatcopy ************ }
  1434. {$ifndef ppc603}
  1435. const
  1436. maxmoveunit = 8;
  1437. {$else ppc603}
  1438. const
  1439. maxmoveunit = 4;
  1440. {$endif ppc603}
  1441. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1442. var
  1443. countreg: TRegister;
  1444. src, dst: TReference;
  1445. lab: tasmlabel;
  1446. count, count2: aword;
  1447. orgsrc, orgdst: boolean;
  1448. size: tcgsize;
  1449. begin
  1450. {$ifdef extdebug}
  1451. if len > high(longint) then
  1452. internalerror(2002072704);
  1453. {$endif extdebug}
  1454. { make sure short loads are handled as optimally as possible }
  1455. if not loadref then
  1456. if (len <= maxmoveunit) and
  1457. (byte(len) in [1,2,4,8]) then
  1458. begin
  1459. if len < 8 then
  1460. begin
  1461. size := int_cgsize(len);
  1462. a_load_ref_ref(list,size,size,source,dest);
  1463. if delsource then
  1464. begin
  1465. reference_release(list,source);
  1466. tg.ungetiftemp(list,source);
  1467. end;
  1468. end
  1469. else
  1470. begin
  1471. a_reg_alloc(list,NR_F0);
  1472. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1473. if delsource then
  1474. begin
  1475. reference_release(list,source);
  1476. tg.ungetiftemp(list,source);
  1477. end;
  1478. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1479. a_reg_dealloc(list,NR_F0);
  1480. end;
  1481. exit;
  1482. end;
  1483. count := len div maxmoveunit;
  1484. reference_reset(src);
  1485. reference_reset(dst);
  1486. { load the address of source into src.base }
  1487. if loadref then
  1488. begin
  1489. src.base := rg.getregisterint(list,OS_ADDR);
  1490. a_load_ref_reg(list,OS_32,OS_32,source,src.base);
  1491. orgsrc := false;
  1492. end
  1493. else if (count > 4) or
  1494. not issimpleref(source) or
  1495. ((source.index <> NR_NO) and
  1496. ((source.offset + longint(len)) > high(smallint))) then
  1497. begin
  1498. src.base := rg.getregisterint(list,OS_ADDR);
  1499. a_loadaddr_ref_reg(list,source,src.base);
  1500. orgsrc := false;
  1501. end
  1502. else
  1503. begin
  1504. src := source;
  1505. orgsrc := true;
  1506. end;
  1507. if not orgsrc and delsource then
  1508. reference_release(list,source);
  1509. { load the address of dest into dst.base }
  1510. if (count > 4) or
  1511. not issimpleref(dest) or
  1512. ((dest.index <> NR_NO) and
  1513. ((dest.offset + longint(len)) > high(smallint))) then
  1514. begin
  1515. dst.base := rg.getregisterint(list,OS_ADDR);
  1516. a_loadaddr_ref_reg(list,dest,dst.base);
  1517. orgdst := false;
  1518. end
  1519. else
  1520. begin
  1521. dst := dest;
  1522. orgdst := true;
  1523. end;
  1524. {$ifndef ppc603}
  1525. if count > 4 then
  1526. { generate a loop }
  1527. begin
  1528. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1529. { have to be set to 8. I put an Inc there so debugging may be }
  1530. { easier (should offset be different from zero here, it will be }
  1531. { easy to notice in the generated assembler }
  1532. inc(dst.offset,8);
  1533. inc(src.offset,8);
  1534. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1535. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1536. countreg := rg.getregisterint(list,OS_INT);
  1537. a_load_const_reg(list,OS_32,count,countreg);
  1538. { explicitely allocate R_0 since it can be used safely here }
  1539. { (for holding date that's being copied) }
  1540. a_reg_alloc(list,NR_F0);
  1541. objectlibrary.getlabel(lab);
  1542. a_label(list, lab);
  1543. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1544. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1545. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1546. a_jmp(list,A_BC,C_NE,0,lab);
  1547. rg.ungetregisterint(list,countreg);
  1548. a_reg_dealloc(list,NR_F0);
  1549. len := len mod 8;
  1550. end;
  1551. count := len div 8;
  1552. if count > 0 then
  1553. { unrolled loop }
  1554. begin
  1555. a_reg_alloc(list,NR_F0);
  1556. for count2 := 1 to count do
  1557. begin
  1558. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1559. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1560. inc(src.offset,8);
  1561. inc(dst.offset,8);
  1562. end;
  1563. a_reg_dealloc(list,NR_F0);
  1564. len := len mod 8;
  1565. end;
  1566. if (len and 4) <> 0 then
  1567. begin
  1568. a_reg_alloc(list,NR_R0);
  1569. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1570. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1571. inc(src.offset,4);
  1572. inc(dst.offset,4);
  1573. a_reg_dealloc(list,NR_R0);
  1574. end;
  1575. {$else not ppc603}
  1576. if count > 4 then
  1577. { generate a loop }
  1578. begin
  1579. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1580. { have to be set to 4. I put an Inc there so debugging may be }
  1581. { easier (should offset be different from zero here, it will be }
  1582. { easy to notice in the generated assembler }
  1583. inc(dst.offset,4);
  1584. inc(src.offset,4);
  1585. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1586. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1587. countreg := rg.getregisterint(list,OS_INT);
  1588. a_load_const_reg(list,OS_32,count,countreg);
  1589. { explicitely allocate R_0 since it can be used safely here }
  1590. { (for holding date that's being copied) }
  1591. a_reg_alloc(list,NR_R0);
  1592. objectlibrary.getlabel(lab);
  1593. a_label(list, lab);
  1594. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1595. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1596. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1597. a_jmp(list,A_BC,C_NE,0,lab);
  1598. rg.ungetregisterint(list,countreg);
  1599. a_reg_dealloc(list,NR_R0);
  1600. len := len mod 4;
  1601. end;
  1602. count := len div 4;
  1603. if count > 0 then
  1604. { unrolled loop }
  1605. begin
  1606. a_reg_alloc(list,NR_R0);
  1607. for count2 := 1 to count do
  1608. begin
  1609. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1610. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1611. inc(src.offset,4);
  1612. inc(dst.offset,4);
  1613. end;
  1614. a_reg_dealloc(list,r);
  1615. len := len mod 4;
  1616. end;
  1617. {$endif not ppc603}
  1618. { copy the leftovers }
  1619. if (len and 2) <> 0 then
  1620. begin
  1621. a_reg_alloc(list,NR_R0);
  1622. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1623. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1624. inc(src.offset,2);
  1625. inc(dst.offset,2);
  1626. a_reg_dealloc(list,NR_R0);
  1627. end;
  1628. if (len and 1) <> 0 then
  1629. begin
  1630. a_reg_alloc(list,NR_R0);
  1631. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1632. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1633. a_reg_dealloc(list,NR_R0);
  1634. end;
  1635. if orgsrc then
  1636. begin
  1637. if delsource then
  1638. reference_release(list,source);
  1639. end
  1640. else
  1641. rg.ungetregisterint(list,src.base);
  1642. if not orgdst then
  1643. rg.ungetregisterint(list,dst.base);
  1644. if delsource then
  1645. tg.ungetiftemp(list,source);
  1646. end;
  1647. procedure tcgppc.g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);
  1648. var
  1649. power,len : longint;
  1650. {$ifndef __NOWINPECOFF__}
  1651. again,ok : tasmlabel;
  1652. {$endif}
  1653. // r,r2,rsp:Tregister;
  1654. begin
  1655. {$warning !!!! FIX ME !!!!}
  1656. internalerror(200305231);
  1657. (* !!!!
  1658. lenref:=ref;
  1659. inc(lenref.offset,4);
  1660. { get stack space }
  1661. r.enum:=R_INTREGISTER;
  1662. r.number:=NR_EDI;
  1663. rsp.enum:=R_INTREGISTER;
  1664. rsp.number:=NR_ESP;
  1665. r2.enum:=R_INTREGISTER;
  1666. rg.getexplicitregisterint(list,NR_EDI);
  1667. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1668. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1669. if (elesize<>1) then
  1670. begin
  1671. if ispowerof2(elesize, power) then
  1672. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1673. else
  1674. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1675. end;
  1676. {$ifndef __NOWINPECOFF__}
  1677. { windows guards only a few pages for stack growing, }
  1678. { so we have to access every page first }
  1679. if target_info.system=system_i386_win32 then
  1680. begin
  1681. objectlibrary.getlabel(again);
  1682. objectlibrary.getlabel(ok);
  1683. a_label(list,again);
  1684. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1685. a_jmp_cond(list,OC_B,ok);
  1686. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1687. r2.number:=NR_EAX;
  1688. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1689. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1690. a_jmp_always(list,again);
  1691. a_label(list,ok);
  1692. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1693. rg.ungetregisterint(list,r);
  1694. { now reload EDI }
  1695. rg.getexplicitregisterint(list,NR_EDI);
  1696. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1697. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1698. if (elesize<>1) then
  1699. begin
  1700. if ispowerof2(elesize, power) then
  1701. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1702. else
  1703. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1704. end;
  1705. end
  1706. else
  1707. {$endif __NOWINPECOFF__}
  1708. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1709. { align stack on 4 bytes }
  1710. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1711. { load destination }
  1712. a_load_reg_reg(list,OS_INT,OS_INT,rsp,r);
  1713. { don't destroy the registers! }
  1714. r2.number:=NR_ECX;
  1715. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1716. r2.number:=NR_ESI;
  1717. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1718. { load count }
  1719. r2.number:=NR_ECX;
  1720. a_load_ref_reg(list,OS_INT,lenref,r2);
  1721. { load source }
  1722. r2.number:=NR_ESI;
  1723. a_load_ref_reg(list,OS_INT,ref,r2);
  1724. { scheduled .... }
  1725. r2.number:=NR_ECX;
  1726. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1727. { calculate size }
  1728. len:=elesize;
  1729. opsize:=S_B;
  1730. if (len and 3)=0 then
  1731. begin
  1732. opsize:=S_L;
  1733. len:=len shr 2;
  1734. end
  1735. else
  1736. if (len and 1)=0 then
  1737. begin
  1738. opsize:=S_W;
  1739. len:=len shr 1;
  1740. end;
  1741. if ispowerof2(len, power) then
  1742. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1743. else
  1744. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1745. list.concat(Taicpu.op_none(A_REP,S_NO));
  1746. case opsize of
  1747. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1748. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1749. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1750. end;
  1751. rg.ungetregisterint(list,r);
  1752. r2.number:=NR_ESI;
  1753. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1754. r2.number:=NR_ECX;
  1755. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1756. { patch the new address }
  1757. a_load_reg_ref(list,OS_INT,rsp,ref);
  1758. !!!! *)
  1759. end;
  1760. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1761. var
  1762. hl : tasmlabel;
  1763. begin
  1764. if not(cs_check_overflow in aktlocalswitches) then
  1765. exit;
  1766. objectlibrary.getlabel(hl);
  1767. if not ((def.deftype=pointerdef) or
  1768. ((def.deftype=orddef) and
  1769. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1770. bool8bit,bool16bit,bool32bit]))) then
  1771. begin
  1772. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1773. a_jmp(list,A_BC,C_OV,7,hl)
  1774. end
  1775. else
  1776. a_jmp_cond(list,OC_AE,hl);
  1777. a_call_name(list,'FPC_OVERFLOW');
  1778. a_label(list,hl);
  1779. end;
  1780. {***************** This is private property, keep out! :) *****************}
  1781. function tcgppc.issimpleref(const ref: treference): boolean;
  1782. begin
  1783. if (ref.base = NR_NO) and
  1784. (ref.index <> NR_NO) then
  1785. internalerror(200208101);
  1786. result :=
  1787. not(assigned(ref.symbol)) and
  1788. (((ref.index = NR_NO) and
  1789. (ref.offset >= low(smallint)) and
  1790. (ref.offset <= high(smallint))) or
  1791. ((ref.index <> NR_NO) and
  1792. (ref.offset = 0)));
  1793. end;
  1794. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1795. var
  1796. tmpreg: tregister;
  1797. orgindex: tregister;
  1798. freeindex: boolean;
  1799. begin
  1800. result := false;
  1801. if (ref.base = NR_NO) then
  1802. begin
  1803. ref.base := ref.index;
  1804. ref.base := NR_NO;
  1805. end;
  1806. if (ref.base <> NR_NO) then
  1807. begin
  1808. if (ref.index <> NR_NO) and
  1809. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1810. begin
  1811. result := true;
  1812. { references are often freed before they are used. Since we allocate }
  1813. { a register here, we must first reallocate the index register, since }
  1814. { otherwise it may be overwritten (and it's still used afterwards) }
  1815. freeindex := false;
  1816. if (getsupreg(ref.index) >= first_int_supreg) and
  1817. (getsupreg(ref.index) in rg.unusedregsint) then
  1818. begin
  1819. rg.getexplicitregisterint(list,ref.index);
  1820. orgindex := ref.index;
  1821. freeindex := true;
  1822. end;
  1823. tmpreg := rg.getregisterint(list,OS_ADDR);
  1824. if not assigned(ref.symbol) and
  1825. (cardinal(ref.offset-low(smallint)) <=
  1826. high(smallint)-low(smallint)) then
  1827. begin
  1828. list.concat(taicpu.op_reg_reg_const(
  1829. A_ADDI,tmpreg,ref.base,ref.offset));
  1830. ref.offset := 0;
  1831. end
  1832. else
  1833. begin
  1834. list.concat(taicpu.op_reg_reg_reg(
  1835. A_ADD,tmpreg,ref.base,ref.index));
  1836. ref.index := NR_NO;
  1837. end;
  1838. ref.base := tmpreg;
  1839. if freeindex then
  1840. rg.ungetregisterint(list,orgindex);
  1841. end
  1842. end
  1843. else
  1844. if ref.index <> NR_NO then
  1845. internalerror(200208102);
  1846. end;
  1847. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1848. { that's the case, we can use rlwinm to do an AND operation }
  1849. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1850. var
  1851. temp : longint;
  1852. testbit : aword;
  1853. compare: boolean;
  1854. begin
  1855. get_rlwi_const := false;
  1856. if (a = 0) or (a = $ffffffff) then
  1857. exit;
  1858. { start with the lowest bit }
  1859. testbit := 1;
  1860. { check its value }
  1861. compare := boolean(a and testbit);
  1862. { find out how long the run of bits with this value is }
  1863. { (it's impossible that all bits are 1 or 0, because in that case }
  1864. { this function wouldn't have been called) }
  1865. l1 := 31;
  1866. while (((a and testbit) <> 0) = compare) do
  1867. begin
  1868. testbit := testbit shl 1;
  1869. dec(l1);
  1870. end;
  1871. { check the length of the run of bits that comes next }
  1872. compare := not compare;
  1873. l2 := l1;
  1874. while (((a and testbit) <> 0) = compare) and
  1875. (l2 >= 0) do
  1876. begin
  1877. testbit := testbit shl 1;
  1878. dec(l2);
  1879. end;
  1880. { and finally the check whether the rest of the bits all have the }
  1881. { same value }
  1882. compare := not compare;
  1883. temp := l2;
  1884. if temp >= 0 then
  1885. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1886. exit;
  1887. { we have done "not(not(compare))", so compare is back to its }
  1888. { initial value. If the lowest bit was 0, a is of the form }
  1889. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1890. { because l2 now contains the position of the last zero of the }
  1891. { first run instead of that of the first 1) so switch l1 and l2 }
  1892. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1893. if not compare then
  1894. begin
  1895. temp := l1;
  1896. l1 := l2+1;
  1897. l2 := temp;
  1898. end
  1899. else
  1900. { otherwise, l1 currently contains the position of the last }
  1901. { zero instead of that of the first 1 of the second run -> +1 }
  1902. inc(l1);
  1903. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1904. l1 := l1 and 31;
  1905. l2 := l2 and 31;
  1906. get_rlwi_const := true;
  1907. end;
  1908. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1909. ref: treference);
  1910. var
  1911. tmpreg: tregister;
  1912. tmpregUsed: Boolean;
  1913. tmpref: treference;
  1914. largeOffset: Boolean;
  1915. begin
  1916. tmpreg := NR_NO;
  1917. if target_info.system = system_powerpc_macos then
  1918. begin
  1919. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1920. high(smallint)-low(smallint));
  1921. tmpreg := rg.getregisterint(list,OS_ADDR);
  1922. tmpregUsed:= false;
  1923. if assigned(ref.symbol) then
  1924. begin //Load symbol's value
  1925. reference_reset(tmpref);
  1926. tmpref.symbol := ref.symbol;
  1927. tmpref.base := NR_RTOC;
  1928. if macos_direct_globals then
  1929. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1930. else
  1931. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1932. tmpregUsed:= true;
  1933. end;
  1934. if largeOffset then
  1935. begin //Add hi part of offset
  1936. reference_reset(tmpref);
  1937. tmpref.offset := Hi(ref.offset);
  1938. if tmpregUsed then
  1939. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1940. tmpreg,tmpref))
  1941. else
  1942. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1943. tmpregUsed:= true;
  1944. end;
  1945. if tmpregUsed then
  1946. begin
  1947. //Add content of base register
  1948. if ref.base <> NR_NO then
  1949. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1950. ref.base,tmpreg));
  1951. //Make ref ready to be used by op
  1952. ref.symbol:= nil;
  1953. ref.base:= tmpreg;
  1954. if largeOffset then
  1955. ref.offset := Lo(ref.offset);
  1956. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1957. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1958. end
  1959. else
  1960. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1961. end
  1962. else {if target_info.system <> system_powerpc_macos}
  1963. begin
  1964. if assigned(ref.symbol) or
  1965. (cardinal(ref.offset-low(smallint)) >
  1966. high(smallint)-low(smallint)) then
  1967. begin
  1968. tmpreg := rg.getregisterint(list,OS_ADDR);
  1969. reference_reset(tmpref);
  1970. tmpref.symbol := ref.symbol;
  1971. tmpref.offset := ref.offset;
  1972. tmpref.symaddr := refs_ha;
  1973. if ref.base <> NR_NO then
  1974. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1975. ref.base,tmpref))
  1976. else
  1977. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1978. ref.base := tmpreg;
  1979. ref.symaddr := refs_l;
  1980. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1981. end
  1982. else
  1983. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1984. end;
  1985. if (tmpreg <> NR_NO) then
  1986. rg.ungetregisterint(list,tmpreg);
  1987. end;
  1988. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1989. crval: longint; l: tasmlabel);
  1990. var
  1991. p: taicpu;
  1992. begin
  1993. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  1994. if op <> A_B then
  1995. create_cond_norm(c,crval,p.condition);
  1996. p.is_jmp := true;
  1997. list.concat(p)
  1998. end;
  1999. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  2000. begin
  2001. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  2002. end;
  2003. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  2004. begin
  2005. a_op64_const_reg_reg(list,op,value,reg,reg);
  2006. end;
  2007. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2008. begin
  2009. case op of
  2010. OP_AND,OP_OR,OP_XOR:
  2011. begin
  2012. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2013. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2014. end;
  2015. OP_ADD:
  2016. begin
  2017. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2018. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2019. end;
  2020. OP_SUB:
  2021. begin
  2022. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2023. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2024. end;
  2025. else
  2026. internalerror(2002072801);
  2027. end;
  2028. end;
  2029. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  2030. const
  2031. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2032. (A_SUBIC,A_SUBC,A_ADDME));
  2033. var
  2034. tmpreg: tregister;
  2035. tmpreg64: tregister64;
  2036. issub: boolean;
  2037. begin
  2038. case op of
  2039. OP_AND,OP_OR,OP_XOR:
  2040. begin
  2041. cg.a_op_const_reg_reg(list,op,OS_32,aword(value),regsrc.reglo,regdst.reglo);
  2042. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2043. regdst.reghi);
  2044. end;
  2045. OP_ADD, OP_SUB:
  2046. begin
  2047. if (int64(value) < 0) then
  2048. begin
  2049. if op = OP_ADD then
  2050. op := OP_SUB
  2051. else
  2052. op := OP_ADD;
  2053. int64(value) := -int64(value);
  2054. end;
  2055. if (longint(value) <> 0) then
  2056. begin
  2057. issub := op = OP_SUB;
  2058. if (int64(value) > 0) and
  2059. (int64(value)-ord(issub) <= 32767) then
  2060. begin
  2061. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2062. regdst.reglo,regsrc.reglo,longint(value)));
  2063. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2064. regdst.reghi,regsrc.reghi));
  2065. end
  2066. else if ((value shr 32) = 0) then
  2067. begin
  2068. tmpreg := rg.getregisterint(list,OS_32);
  2069. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2070. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2071. regdst.reglo,regsrc.reglo,tmpreg));
  2072. rg.ungetregisterint(list,tmpreg);
  2073. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2074. regdst.reghi,regsrc.reghi));
  2075. end
  2076. else
  2077. begin
  2078. tmpreg64.reglo := rg.getregisterint(list,OS_32);
  2079. tmpreg64.reghi := rg.getregisterint(list,OS_32);
  2080. a_load64_const_reg(list,value,tmpreg64);
  2081. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2082. rg.ungetregisterint(list,tmpreg64.reglo);
  2083. rg.ungetregisterint(list,tmpreg64.reghi);
  2084. end
  2085. end
  2086. else
  2087. begin
  2088. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2089. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2090. regdst.reghi);
  2091. end;
  2092. end;
  2093. else
  2094. internalerror(2002072802);
  2095. end;
  2096. end;
  2097. begin
  2098. cg := tcgppc.create;
  2099. cg64 :=tcg64fppc.create;
  2100. end.
  2101. {
  2102. $Log$
  2103. Revision 1.127 2003-10-01 20:34:49 peter
  2104. * procinfo unit contains tprocinfo
  2105. * cginfo renamed to cgbase
  2106. * moved cgmessage to verbose
  2107. * fixed ppc and sparc compiles
  2108. Revision 1.126 2003/09/14 16:37:20 jonas
  2109. * fixed some ppc problems
  2110. Revision 1.125 2003/09/03 21:04:14 peter
  2111. * some fixes for ppc
  2112. Revision 1.124 2003/09/03 19:35:24 peter
  2113. * powerpc compiles again
  2114. Revision 1.123 2003/09/03 15:55:01 peter
  2115. * NEWRA branch merged
  2116. Revision 1.122.2.1 2003/08/31 21:08:16 peter
  2117. * first batch of sparc fixes
  2118. Revision 1.122 2003/08/18 21:27:00 jonas
  2119. * some newra optimizations (eliminate lots of moves between registers)
  2120. Revision 1.121 2003/08/18 11:50:55 olle
  2121. + cleaning up in proc entry and exit, now calc_stack_frame always is used.
  2122. Revision 1.120 2003/08/17 16:59:20 jonas
  2123. * fixed regvars so they work with newra (at least for ppc)
  2124. * fixed some volatile register bugs
  2125. + -dnotranslation option for -dnewra, which causes the registers not to
  2126. be translated from virtual to normal registers. Requires support in
  2127. the assembler writer as well, which is only implemented in aggas/
  2128. agppcgas currently
  2129. Revision 1.119 2003/08/11 21:18:20 peter
  2130. * start of sparc support for newra
  2131. Revision 1.118 2003/08/08 15:50:45 olle
  2132. * merged macos entry/exit code generation into the general one.
  2133. Revision 1.117 2002/10/01 05:24:28 olle
  2134. * made a_load_store more robust and to accept large offsets and cleaned up code
  2135. Revision 1.116 2003/07/23 11:02:23 jonas
  2136. * don't use rg.getregisterint() anymore in g_stackframe_entry_*, because
  2137. the register colouring has already occurred then, use a hard-coded
  2138. register instead
  2139. Revision 1.115 2003/07/20 20:39:20 jonas
  2140. * fixed newra bug due to the fact that we sometimes need a temp reg
  2141. when loading/storing to memory (base+index+offset is not possible)
  2142. and because a reference is often freed before it is last used, this
  2143. temp register was soemtimes the same as one of the reference regs
  2144. Revision 1.114 2003/07/20 16:15:58 jonas
  2145. * fixed bug in g_concatcopy with -dnewra
  2146. Revision 1.113 2003/07/06 20:25:03 jonas
  2147. * fixed ppc compiler
  2148. Revision 1.112 2003/07/05 20:11:42 jonas
  2149. * create_paraloc_info() is now called separately for the caller and
  2150. callee info
  2151. * fixed ppc cycle
  2152. Revision 1.111 2003/07/02 22:18:04 peter
  2153. * paraloc splitted in callerparaloc,calleeparaloc
  2154. * sparc calling convention updates
  2155. Revision 1.110 2003/06/18 10:12:36 olle
  2156. * macos: fixes of loading-code
  2157. Revision 1.109 2003/06/14 22:32:43 jonas
  2158. * ppc compiles with -dnewra, haven't tried to compile anything with it
  2159. yet though
  2160. Revision 1.108 2003/06/13 21:19:31 peter
  2161. * current_procdef removed, use current_procinfo.procdef instead
  2162. Revision 1.107 2003/06/09 14:54:26 jonas
  2163. * (de)allocation of registers for parameters is now performed properly
  2164. (and checked on the ppc)
  2165. - removed obsolete allocation of all parameter registers at the start
  2166. of a procedure (and deallocation at the end)
  2167. Revision 1.106 2003/06/08 18:19:27 jonas
  2168. - removed duplicate identifier
  2169. Revision 1.105 2003/06/07 18:57:04 jonas
  2170. + added freeintparaloc
  2171. * ppc get/freeintparaloc now check whether the parameter regs are
  2172. properly allocated/deallocated (and get an extra list para)
  2173. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  2174. * fixed lot of missing pi_do_call's
  2175. Revision 1.104 2003/06/04 11:58:58 jonas
  2176. * calculate localsize also in g_return_from_proc since it's now called
  2177. before g_stackframe_entry (still have to fix macos)
  2178. * compilation fixes (cycle doesn't work yet though)
  2179. Revision 1.103 2003/06/01 21:38:06 peter
  2180. * getregisterfpu size parameter added
  2181. * op_const_reg size parameter added
  2182. * sparc updates
  2183. Revision 1.102 2003/06/01 13:42:18 jonas
  2184. * fix for bug in fixref that Peter found during the Sparc conversion
  2185. Revision 1.101 2003/05/30 18:52:10 jonas
  2186. * fixed bug with intregvars
  2187. * locapara.loc can also be LOC_CFPUREGISTER -> also fixed
  2188. rcgppc.a_param_ref, which previously got bogus size values
  2189. Revision 1.100 2003/05/29 21:17:27 jonas
  2190. * compile with -dppc603 to not use unaligned float loads in move() and
  2191. g_concatcopy, because the 603 and 604 take an exception for those
  2192. (and netbsd doesn't even handle those in the kernel). There are
  2193. still some of those left that could cause problems though (e.g.
  2194. in the set helpers)
  2195. Revision 1.99 2003/05/29 10:06:09 jonas
  2196. * also free temps in g_concatcopy if delsource is true
  2197. Revision 1.98 2003/05/28 23:58:18 jonas
  2198. * added missing initialization of rg.usedintin,byproc
  2199. * ppc now also saves/restores used fpu registers
  2200. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  2201. i386
  2202. Revision 1.97 2003/05/28 23:18:31 florian
  2203. * started to fix and clean up the sparc port
  2204. Revision 1.96 2003/05/24 11:59:42 jonas
  2205. * fixed integer typeconversion problems
  2206. Revision 1.95 2003/05/23 18:51:26 jonas
  2207. * fixed support for nested procedures and more parameters than those
  2208. which fit in registers (untested/probably not working: calling a
  2209. nested procedure from a deeper nested procedure)
  2210. Revision 1.94 2003/05/20 23:54:00 florian
  2211. + basic darwin support added
  2212. Revision 1.93 2003/05/15 22:14:42 florian
  2213. * fixed last commit, changing lastsaveintreg to r31 caused some strange problems
  2214. Revision 1.92 2003/05/15 21:37:00 florian
  2215. * sysv entry code saves r13 now as well
  2216. Revision 1.91 2003/05/15 19:39:09 florian
  2217. * fixed ppc compiler which was broken by Peter's changes
  2218. Revision 1.90 2003/05/12 18:43:50 jonas
  2219. * fixed g_concatcopy
  2220. Revision 1.89 2003/05/11 20:59:23 jonas
  2221. * fixed bug with large offsets in entrycode
  2222. Revision 1.88 2003/05/11 11:45:08 jonas
  2223. * fixed shifts
  2224. Revision 1.87 2003/05/11 11:07:33 jonas
  2225. * fixed optimizations in a_op_const_reg_reg()
  2226. Revision 1.86 2003/04/27 11:21:36 peter
  2227. * aktprocdef renamed to current_procinfo.procdef
  2228. * procinfo renamed to current_procinfo
  2229. * procinfo will now be stored in current_module so it can be
  2230. cleaned up properly
  2231. * gen_main_procsym changed to create_main_proc and release_main_proc
  2232. to also generate a tprocinfo structure
  2233. * fixed unit implicit initfinal
  2234. Revision 1.85 2003/04/26 22:56:11 jonas
  2235. * fix to a_op64_const_reg_reg
  2236. Revision 1.84 2003/04/26 16:08:41 jonas
  2237. * fixed g_flags2reg
  2238. Revision 1.83 2003/04/26 15:25:29 florian
  2239. * fixed cmp_reg_reg_reg, cmp operands were emitted in the wrong order
  2240. Revision 1.82 2003/04/25 20:55:34 florian
  2241. * stack frame calculations are now completly done using the code generator
  2242. routines instead of generating directly assembler so also large stack frames
  2243. are handle properly
  2244. Revision 1.81 2003/04/24 11:24:00 florian
  2245. * fixed several issues with nested procedures
  2246. Revision 1.80 2003/04/23 22:18:01 peter
  2247. * fixes to get rtl compiled
  2248. Revision 1.79 2003/04/23 12:35:35 florian
  2249. * fixed several issues with powerpc
  2250. + applied a patch from Jonas for nested function calls (PowerPC only)
  2251. * ...
  2252. Revision 1.78 2003/04/16 09:26:55 jonas
  2253. * assembler procedures now again get a stackframe if they have local
  2254. variables. No space is reserved for a function result however.
  2255. Also, the register parameters aren't automatically saved on the stack
  2256. anymore in assembler procedures.
  2257. Revision 1.77 2003/04/06 16:39:11 jonas
  2258. * don't generate entry/exit code for assembler procedures
  2259. Revision 1.76 2003/03/22 18:01:13 jonas
  2260. * fixed linux entry/exit code generation
  2261. Revision 1.75 2003/03/19 14:26:26 jonas
  2262. * fixed R_TOC bugs introduced by new register allocator conversion
  2263. Revision 1.74 2003/03/13 22:57:45 olle
  2264. * change in a_loadaddr_ref_reg
  2265. Revision 1.73 2003/03/12 22:43:38 jonas
  2266. * more powerpc and generic fixes related to the new register allocator
  2267. Revision 1.72 2003/03/11 21:46:24 jonas
  2268. * lots of new regallocator fixes, both in generic and ppc-specific code
  2269. (ppc compiler still can't compile the linux system unit though)
  2270. Revision 1.71 2003/02/19 22:00:16 daniel
  2271. * Code generator converted to new register notation
  2272. - Horribily outdated todo.txt removed
  2273. Revision 1.70 2003/01/13 17:17:50 olle
  2274. * changed global var access, TOC now contain pointers to globals
  2275. * fixed handling of function pointers
  2276. Revision 1.69 2003/01/09 22:00:53 florian
  2277. * fixed some PowerPC issues
  2278. Revision 1.68 2003/01/08 18:43:58 daniel
  2279. * Tregister changed into a record
  2280. Revision 1.67 2002/12/15 19:22:01 florian
  2281. * fixed some crashes and a rte 201
  2282. Revision 1.66 2002/11/28 10:55:16 olle
  2283. * macos: changing code gen for references to globals
  2284. Revision 1.65 2002/11/07 15:50:23 jonas
  2285. * fixed bctr(l) problems
  2286. Revision 1.64 2002/11/04 18:24:19 olle
  2287. * macos: globals are located in TOC and relative r2, instead of absolute
  2288. Revision 1.63 2002/10/28 22:24:28 olle
  2289. * macos entry/exit: only used registers are saved
  2290. - macos entry/exit: stackptr not saved in r31 anymore
  2291. * macos entry/exit: misc fixes
  2292. Revision 1.62 2002/10/19 23:51:48 olle
  2293. * macos stack frame size computing updated
  2294. + macos epilogue: control register now restored
  2295. * macos prologue and epilogue: fp reg now saved and restored
  2296. Revision 1.61 2002/10/19 12:50:36 olle
  2297. * reorganized prologue and epilogue routines
  2298. Revision 1.60 2002/10/02 21:49:51 florian
  2299. * all A_BL instructions replaced by calls to a_call_name
  2300. Revision 1.59 2002/10/02 13:24:58 jonas
  2301. * changed a_call_* so that no superfluous code is generated anymore
  2302. Revision 1.58 2002/09/17 18:54:06 jonas
  2303. * a_load_reg_reg() now has two size parameters: source and dest. This
  2304. allows some optimizations on architectures that don't encode the
  2305. register size in the register name.
  2306. Revision 1.57 2002/09/10 21:22:25 jonas
  2307. + added some internal errors
  2308. * fixed bug in sysv exit code
  2309. Revision 1.56 2002/09/08 20:11:56 jonas
  2310. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  2311. Revision 1.55 2002/09/08 13:03:26 jonas
  2312. * several large offset-related fixes
  2313. Revision 1.54 2002/09/07 17:54:58 florian
  2314. * first part of PowerPC fixes
  2315. Revision 1.53 2002/09/07 15:25:14 peter
  2316. * old logs removed and tabs fixed
  2317. Revision 1.52 2002/09/02 10:14:51 jonas
  2318. + a_call_reg()
  2319. * small fix in a_call_ref()
  2320. Revision 1.51 2002/09/02 06:09:02 jonas
  2321. * fixed range error
  2322. Revision 1.50 2002/09/01 21:04:49 florian
  2323. * several powerpc related stuff fixed
  2324. Revision 1.49 2002/09/01 12:09:27 peter
  2325. + a_call_reg, a_call_loc added
  2326. * removed exprasmlist references
  2327. Revision 1.48 2002/08/31 21:38:02 jonas
  2328. * fixed a_call_ref (it should load ctr, not lr)
  2329. Revision 1.47 2002/08/31 21:30:45 florian
  2330. * fixed several problems caused by Jonas' commit :)
  2331. Revision 1.46 2002/08/31 19:25:50 jonas
  2332. + implemented a_call_ref()
  2333. Revision 1.45 2002/08/18 22:16:14 florian
  2334. + the ppc gas assembler writer adds now registers aliases
  2335. to the assembler file
  2336. Revision 1.44 2002/08/17 18:23:53 florian
  2337. * some assembler writer bugs fixed
  2338. Revision 1.43 2002/08/17 09:23:49 florian
  2339. * first part of procinfo rewrite
  2340. Revision 1.42 2002/08/16 14:24:59 carl
  2341. * issameref() to test if two references are the same (then emit no opcodes)
  2342. + ret_in_reg to replace ret_in_acc
  2343. (fix some register allocation bugs at the same time)
  2344. + save_std_register now has an extra parameter which is the
  2345. usedinproc registers
  2346. Revision 1.41 2002/08/15 08:13:54 carl
  2347. - a_load_sym_ofs_reg removed
  2348. * loadvmt now calls loadaddr_ref_reg instead
  2349. Revision 1.40 2002/08/11 14:32:32 peter
  2350. * renamed current_library to objectlibrary
  2351. Revision 1.39 2002/08/11 13:24:18 peter
  2352. * saving of asmsymbols in ppu supported
  2353. * asmsymbollist global is removed and moved into a new class
  2354. tasmlibrarydata that will hold the info of a .a file which
  2355. corresponds with a single module. Added librarydata to tmodule
  2356. to keep the library info stored for the module. In the future the
  2357. objectfiles will also be stored to the tasmlibrarydata class
  2358. * all getlabel/newasmsymbol and friends are moved to the new class
  2359. Revision 1.38 2002/08/11 11:39:31 jonas
  2360. + powerpc-specific genlinearlist
  2361. Revision 1.37 2002/08/10 17:15:31 jonas
  2362. * various fixes and optimizations
  2363. Revision 1.36 2002/08/06 20:55:23 florian
  2364. * first part of ppc calling conventions fix
  2365. Revision 1.35 2002/08/06 07:12:05 jonas
  2366. * fixed bug in g_flags2reg()
  2367. * and yet more constant operation fixes :)
  2368. Revision 1.34 2002/08/05 08:58:53 jonas
  2369. * fixed compilation problems
  2370. Revision 1.33 2002/08/04 12:57:55 jonas
  2371. * more misc. fixes, mostly constant-related
  2372. }