cgcpu.pas 100 KB

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