cgcpu.pas 93 KB

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