cgcpu.pas 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the PowerPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,symtype,symdef,
  22. cgbase,cgobj,cgppc,
  23. aasmbase,aasmcpu,aasmtai,aasmdata,
  24. cpubase,cpuinfo,cgutils,cg64f32,rgcpu,
  25. parabase;
  26. type
  27. tcgppc = class(tcgppcgen)
  28. procedure init_register_allocators;override;
  29. procedure done_register_allocators;override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_const(list : TAsmList;size : tcgsize;a : aint;const paraloc : tcgpara);override;
  36. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);override;
  37. procedure a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : tcgpara);override;
  38. procedure a_call_name(list : TAsmList;const s : string);override;
  39. procedure a_call_reg(list : TAsmList;reg: tregister); override;
  40. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  41. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  42. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  43. size: tcgsize; a: aint; src, dst: tregister); override;
  44. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  45. size: tcgsize; src1, src2, dst: tregister); override;
  46. { move instructions }
  47. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);override;
  48. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  49. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  50. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  51. procedure a_load_subsetreg_reg(list : TAsmList; subsetregsize, subsetsize: tcgsize;
  52. startbit: byte; tosize: tcgsize; subsetreg, destreg: tregister); override;
  53. procedure a_load_reg_subsetreg(list : TAsmList; fromsize: tcgsize; subsetregsize,
  54. subsetsize: tcgsize; startbit: byte; fromreg, subsetreg: tregister); override;
  55. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetregsize, fromsubsetsize: tcgsize; fromstartbit: byte;
  56. tosubsetregsize, tosubsetsize: tcgsize; tostartbit: byte; fromsubsetreg, tosubsetreg: tregister); override;
  57. { fpu move instructions }
  58. procedure a_loadfpu_reg_reg(list: TAsmList; size: tcgsize; reg1, reg2: tregister); override;
  59. procedure a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister); override;
  60. procedure a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference); override;
  61. { comparison operations }
  62. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  63. l : tasmlabel);override;
  64. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  65. procedure a_jmp_name(list : TAsmList;const s : string); override;
  66. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  67. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  68. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  69. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  70. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  71. procedure g_save_standard_registers(list:TAsmList); override;
  72. procedure g_restore_standard_registers(list:TAsmList); override;
  73. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  74. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);override;
  75. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  76. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  77. { that's the case, we can use rlwinm to do an AND operation }
  78. function get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  79. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  80. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  81. private
  82. (* NOT IN USE: *)
  83. procedure g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  84. (* NOT IN USE: *)
  85. procedure g_return_from_proc_mac(list : TAsmList;parasize : aint);
  86. { clear out potential overflow bits from 8 or 16 bit operations }
  87. { the upper 24/16 bits of a register after an operation }
  88. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  89. { Make sure ref is a valid reference for the PowerPC and sets the }
  90. { base to the value of the index if (base = R_NO). }
  91. { Returns true if the reference contained a base, index and an }
  92. { offset or symbol, in which case the base will have been changed }
  93. { to a tempreg (which has to be freed by the caller) containing }
  94. { the sum of part of the original reference }
  95. function fixref(list: TAsmList; var ref: treference): boolean;
  96. { returns whether a reference can be used immediately in a powerpc }
  97. { instruction }
  98. function issimpleref(const ref: treference): boolean;
  99. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  100. procedure a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  101. ref: treference);
  102. { creates the correct branch instruction for a given combination }
  103. { of asmcondflags and destination addressing mode }
  104. procedure a_jmp(list: TAsmList; op: tasmop;
  105. c: tasmcondflag; crval: longint; l: tasmlabel);
  106. function save_regs(list : TAsmList):longint;
  107. procedure restore_regs(list : TAsmList);
  108. function get_darwin_call_stub(const s: string): tasmsymbol;
  109. end;
  110. tcg64fppc = class(tcg64f32)
  111. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  112. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  113. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  114. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  115. end;
  116. const
  117. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDI,A_ANDI_,A_DIVWU,
  118. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  119. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  120. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDIS,A_ANDIS_,
  121. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  122. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  123. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  124. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  125. implementation
  126. uses
  127. globals,verbose,systems,cutils,
  128. symconst,symsym,fmodule,
  129. rgobj,tgobj,cpupi,procinfo,paramgr;
  130. procedure tcgppc.init_register_allocators;
  131. begin
  132. inherited init_register_allocators;
  133. if target_info.system=system_powerpc_darwin then
  134. begin
  135. {
  136. if pi_needs_got in current_procinfo.flags then
  137. begin
  138. current_procinfo.got:=NR_R31;
  139. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  140. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  141. RS_R9,RS_R10,RS_R11,RS_R12,RS_R30,RS_R29,
  142. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  143. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  144. RS_R14,RS_R13],first_int_imreg,[]);
  145. end
  146. else}
  147. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  148. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  149. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  150. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  151. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  152. RS_R14,RS_R13],first_int_imreg,[]);
  153. end
  154. else
  155. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  156. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  157. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  158. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  159. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  160. RS_R14,RS_R13],first_int_imreg,[]);
  161. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  162. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  163. RS_F10,RS_F11,RS_F12,RS_F13,RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,
  164. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,
  165. RS_F17,RS_F16,RS_F15,RS_F14],first_fpu_imreg,[]);
  166. {$warning FIX ME}
  167. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  168. [RS_M0,RS_M1,RS_M2],first_mm_imreg,[]);
  169. end;
  170. procedure tcgppc.done_register_allocators;
  171. begin
  172. rg[R_INTREGISTER].free;
  173. rg[R_FPUREGISTER].free;
  174. rg[R_MMREGISTER].free;
  175. inherited done_register_allocators;
  176. end;
  177. procedure tcgppc.a_param_const(list : TAsmList;size : tcgsize;a : aint;const paraloc : tcgpara);
  178. var
  179. ref: treference;
  180. begin
  181. paraloc.check_simple_location;
  182. case paraloc.location^.loc of
  183. LOC_REGISTER,LOC_CREGISTER:
  184. a_load_const_reg(list,size,a,paraloc.location^.register);
  185. LOC_REFERENCE:
  186. begin
  187. reference_reset(ref);
  188. ref.base:=paraloc.location^.reference.index;
  189. ref.offset:=paraloc.location^.reference.offset;
  190. a_load_const_ref(list,size,a,ref);
  191. end;
  192. else
  193. internalerror(2002081101);
  194. end;
  195. end;
  196. procedure tcgppc.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);
  197. var
  198. tmpref, ref: treference;
  199. location: pcgparalocation;
  200. sizeleft: aint;
  201. begin
  202. location := paraloc.location;
  203. tmpref := r;
  204. sizeleft := paraloc.intsize;
  205. while assigned(location) do
  206. begin
  207. case location^.loc of
  208. LOC_REGISTER,LOC_CREGISTER:
  209. begin
  210. {$ifndef cpu64bit}
  211. if (sizeleft <> 3) then
  212. begin
  213. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  214. end
  215. else
  216. begin
  217. a_load_ref_reg(list,OS_16,OS_16,tmpref,location^.register);
  218. a_reg_alloc(list,NR_R0);
  219. inc(tmpref.offset,2);
  220. a_load_ref_reg(list,OS_8,OS_8,tmpref,newreg(R_INTREGISTER,RS_R0,R_SUBNONE));
  221. a_op_const_reg(list,OP_SHL,OS_INT,16,location^.register);
  222. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location^.register,newreg(R_INTREGISTER,RS_R0,R_SUBNONE),8,16,31-8));
  223. a_reg_dealloc(list,NR_R0);
  224. dec(tmpref.offset,2);
  225. end;
  226. {$else not cpu64bit}
  227. {$error add 64 bit support for non power of 2 loads in a_param_ref}
  228. {$endif not cpu64bit}
  229. end;
  230. LOC_REFERENCE:
  231. begin
  232. reference_reset_base(ref,location^.reference.index,location^.reference.offset);
  233. g_concatcopy(list,tmpref,ref,sizeleft);
  234. if assigned(location^.next) then
  235. internalerror(2005010710);
  236. end;
  237. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  238. case location^.size of
  239. OS_F32, OS_F64:
  240. a_loadfpu_ref_reg(list,location^.size,tmpref,location^.register);
  241. else
  242. internalerror(2002072801);
  243. end;
  244. LOC_VOID:
  245. begin
  246. // nothing to do
  247. end;
  248. else
  249. internalerror(2002081103);
  250. end;
  251. inc(tmpref.offset,tcgsize2size[location^.size]);
  252. dec(sizeleft,tcgsize2size[location^.size]);
  253. location := location^.next;
  254. end;
  255. end;
  256. procedure tcgppc.a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : tcgpara);
  257. var
  258. ref: treference;
  259. tmpreg: tregister;
  260. begin
  261. paraloc.check_simple_location;
  262. case paraloc.location^.loc of
  263. LOC_REGISTER,LOC_CREGISTER:
  264. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  265. LOC_REFERENCE:
  266. begin
  267. reference_reset(ref);
  268. ref.base := paraloc.location^.reference.index;
  269. ref.offset := paraloc.location^.reference.offset;
  270. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  271. a_loadaddr_ref_reg(list,r,tmpreg);
  272. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  273. end;
  274. else
  275. internalerror(2002080701);
  276. end;
  277. end;
  278. function tcgppc.get_darwin_call_stub(const s: string): tasmsymbol;
  279. var
  280. stubname: string;
  281. href: treference;
  282. l1: tasmsymbol;
  283. begin
  284. { function declared in the current unit? }
  285. { doesn't work correctly, because this will also return a hit if we }
  286. { previously took the address of an external procedure. It doesn't }
  287. { really matter, the linker will remove all unnecessary stubs. }
  288. { result := current_asmdata.getasmsymbol(s);
  289. if not(assigned(result)) then
  290. begin }
  291. stubname := 'L'+s+'$stub';
  292. result := current_asmdata.getasmsymbol(stubname);
  293. { end; }
  294. if assigned(result) then
  295. exit;
  296. if current_asmdata.asmlists[al_imports]=nil then
  297. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  298. current_asmdata.asmlists[al_imports].concat(Tai_section.create(sec_stub,'',0));
  299. current_asmdata.asmlists[al_imports].concat(Tai_align.Create(16));
  300. result := current_asmdata.RefAsmSymbol(stubname);
  301. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  302. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  303. l1 := current_asmdata.RefAsmSymbol('L'+s+'$lazy_ptr');
  304. reference_reset_symbol(href,l1,0);
  305. href.refaddr := addr_hi;
  306. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LIS,NR_R11,href));
  307. href.refaddr := addr_lo;
  308. href.base := NR_R11;
  309. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LWZU,NR_R12,href));
  310. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg(A_MTCTR,NR_R12));
  311. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_BCTR));
  312. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_lazy_symbol_pointer,''));
  313. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(l1,0));
  314. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  315. current_asmdata.asmlists[al_imports].concat(tai_const.createname(strpnew('dyld_stub_binding_helper'),0));
  316. end;
  317. { calling a procedure by name }
  318. procedure tcgppc.a_call_name(list : TAsmList;const s : string);
  319. begin
  320. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  321. if it is a cross-TOC call. If so, it also replaces the NOP
  322. with some restore code.}
  323. if (target_info.system <> system_powerpc_darwin) then
  324. begin
  325. list.concat(taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s)));
  326. if target_info.system=system_powerpc_macos then
  327. list.concat(taicpu.op_none(A_NOP));
  328. end
  329. else
  330. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s)));
  331. {
  332. the compiler does not properly set this flag anymore in pass 1, and
  333. for now we only need it after pass 2 (I hope) (JM)
  334. if not(pi_do_call in current_procinfo.flags) then
  335. internalerror(2003060703);
  336. }
  337. include(current_procinfo.flags,pi_do_call);
  338. end;
  339. { calling a procedure by address }
  340. procedure tcgppc.a_call_reg(list : TAsmList;reg: tregister);
  341. var
  342. tmpreg : tregister;
  343. tmpref : treference;
  344. begin
  345. if target_info.system=system_powerpc_macos then
  346. begin
  347. {Generate instruction to load the procedure address from
  348. the transition vector.}
  349. //TODO: Support cross-TOC calls.
  350. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  351. reference_reset(tmpref);
  352. tmpref.offset := 0;
  353. //tmpref.symaddr := refs_full;
  354. tmpref.base:= reg;
  355. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  356. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  357. end
  358. else
  359. list.concat(taicpu.op_reg(A_MTCTR,reg));
  360. list.concat(taicpu.op_none(A_BCTRL));
  361. //if target_info.system=system_powerpc_macos then
  362. // //NOP is not needed here.
  363. // list.concat(taicpu.op_none(A_NOP));
  364. include(current_procinfo.flags,pi_do_call);
  365. {
  366. if not(pi_do_call in current_procinfo.flags) then
  367. internalerror(2003060704);
  368. }
  369. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  370. end;
  371. {********************** load instructions ********************}
  372. procedure tcgppc.a_load_const_reg(list : TAsmList; size: TCGSize; a : aint; reg : TRegister);
  373. begin
  374. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  375. internalerror(2002090902);
  376. if (a >= low(smallint)) and
  377. (a <= high(smallint)) then
  378. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  379. else if ((a and $ffff) <> 0) then
  380. begin
  381. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  382. if ((a shr 16) <> 0) or
  383. (smallint(a and $ffff) < 0) then
  384. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  385. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  386. end
  387. else
  388. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  389. end;
  390. procedure tcgppc.a_load_reg_ref(list : TAsmList; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  391. const
  392. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  393. { indexed? updating?}
  394. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  395. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  396. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  397. var
  398. op: TAsmOp;
  399. ref2: TReference;
  400. begin
  401. ref2 := ref;
  402. fixref(list,ref2);
  403. if tosize in [OS_S8..OS_S16] then
  404. { storing is the same for signed and unsigned values }
  405. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  406. { 64 bit stuff should be handled separately }
  407. if tosize in [OS_64,OS_S64] then
  408. internalerror(200109236);
  409. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  410. a_load_store(list,op,reg,ref2);
  411. End;
  412. procedure tcgppc.a_load_ref_reg(list : TAsmList; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  413. const
  414. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  415. { indexed? updating?}
  416. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  417. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  418. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  419. { 64bit stuff should be handled separately }
  420. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  421. { 128bit stuff too }
  422. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  423. { there's no load-byte-with-sign-extend :( }
  424. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  425. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  426. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  427. var
  428. op: tasmop;
  429. ref2: treference;
  430. begin
  431. { TODO: optimize/take into consideration fromsize/tosize. Will }
  432. { probably only matter for OS_S8 loads though }
  433. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  434. internalerror(2002090902);
  435. ref2 := ref;
  436. fixref(list,ref2);
  437. { the caller is expected to have adjusted the reference already }
  438. { in this case }
  439. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  440. fromsize := tosize;
  441. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  442. a_load_store(list,op,reg,ref2);
  443. { sign extend shortint if necessary, since there is no }
  444. { load instruction that does that automatically (JM) }
  445. if fromsize = OS_S8 then
  446. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  447. end;
  448. procedure tcgppc.a_load_reg_reg(list : TAsmList;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  449. var
  450. instr: taicpu;
  451. begin
  452. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  453. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and
  454. (fromsize <> tosize)) or
  455. { needs to mask out the sign in the top 16 bits }
  456. ((fromsize = OS_S8) and
  457. (tosize = OS_16)) then
  458. case tosize of
  459. OS_8:
  460. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  461. reg2,reg1,0,31-8+1,31);
  462. OS_S8:
  463. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  464. OS_16:
  465. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  466. reg2,reg1,0,31-16+1,31);
  467. OS_S16:
  468. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  469. OS_32,OS_S32:
  470. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  471. else internalerror(2002090901);
  472. end
  473. else
  474. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  475. list.concat(instr);
  476. rg[R_INTREGISTER].add_move_instruction(instr);
  477. end;
  478. procedure tcgppc.a_load_subsetreg_reg(list : TAsmList; subsetregsize, subsetsize: tcgsize;
  479. startbit: byte; tosize: tcgsize; subsetreg, destreg: tregister);
  480. begin
  481. if (tcgsize2size[subsetsize] <> sizeof(aint)) then
  482. begin
  483. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,destreg,
  484. subsetreg,(32-startbit) and 31,32-tcgsize2size[subsetsize]*8,31));
  485. a_load_reg_reg(list,tcgsize2unsigned[subsetsize],subsetsize,destreg,destreg);
  486. a_load_reg_reg(list,subsetsize,tosize,destreg,destreg);
  487. end
  488. else
  489. a_load_reg_reg(list,subsetsize,tosize,subsetreg,destreg);
  490. end;
  491. procedure tcgppc.a_load_reg_subsetreg(list : TAsmList; fromsize: tcgsize; subsetregsize,
  492. subsetsize: tcgsize; startbit: byte; fromreg, subsetreg: tregister);
  493. begin
  494. if ((tcgsize2size[subsetsize]) <> sizeof(aint)) then
  495. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,subsetreg,fromreg,
  496. startbit,32-startbit-tcgsize2size[subsetsize]*8,31-startbit))
  497. else
  498. a_load_reg_reg(list,fromsize,subsetsize,fromreg,subsetreg);
  499. end;
  500. procedure tcgppc.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetregsize, fromsubsetsize: tcgsize; fromstartbit: byte;
  501. tosubsetregsize, tosubsetsize: tcgsize; tostartbit: byte; fromsubsetreg, tosubsetreg: tregister);
  502. begin
  503. if (tcgsize2size[fromsubsetsize] >= tcgsize2size[tosubsetsize]) then
  504. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,tosubsetreg, fromsubsetreg,
  505. (tostartbit-fromstartbit) and 31,
  506. 32-tostartbit-tcgsize2size[tosubsetsize]*8,31-tostartbit))
  507. else
  508. inherited a_load_subsetreg_subsetreg(list,fromsubsetregsize,fromsubsetsize,fromstartbit,tosubsetregsize,tosubsetsize,tostartbit,fromsubsetreg,tosubsetreg);
  509. end;
  510. procedure tcgppc.a_loadfpu_reg_reg(list: TAsmList; size: tcgsize; reg1, reg2: tregister);
  511. var
  512. instr: taicpu;
  513. begin
  514. instr := taicpu.op_reg_reg(A_FMR,reg2,reg1);
  515. list.concat(instr);
  516. rg[R_FPUREGISTER].add_move_instruction(instr);
  517. end;
  518. procedure tcgppc.a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister);
  519. const
  520. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  521. { indexed? updating?}
  522. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  523. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  524. var
  525. op: tasmop;
  526. ref2: treference;
  527. begin
  528. { several functions call this procedure with OS_32 or OS_64 }
  529. { so this makes life easier (FK) }
  530. case size of
  531. OS_32,OS_F32:
  532. size:=OS_F32;
  533. OS_64,OS_F64,OS_C64:
  534. size:=OS_F64;
  535. else
  536. internalerror(200201121);
  537. end;
  538. ref2 := ref;
  539. fixref(list,ref2);
  540. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  541. a_load_store(list,op,reg,ref2);
  542. end;
  543. procedure tcgppc.a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference);
  544. const
  545. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  546. { indexed? updating?}
  547. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  548. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  549. var
  550. op: tasmop;
  551. ref2: treference;
  552. begin
  553. if not(size in [OS_F32,OS_F64]) then
  554. internalerror(200201122);
  555. ref2 := ref;
  556. fixref(list,ref2);
  557. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  558. a_load_store(list,op,reg,ref2);
  559. end;
  560. procedure tcgppc.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  561. begin
  562. a_op_const_reg_reg(list,op,size,a,reg,reg);
  563. end;
  564. procedure tcgppc.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  565. begin
  566. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  567. end;
  568. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  569. const
  570. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  571. begin
  572. if (op in overflowops) and
  573. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  574. a_load_reg_reg(list,OS_32,size,dst,dst);
  575. end;
  576. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  577. size: tcgsize; a: aint; src, dst: tregister);
  578. var
  579. l1,l2: longint;
  580. oplo, ophi: tasmop;
  581. scratchreg: tregister;
  582. useReg, gotrlwi: boolean;
  583. procedure do_lo_hi;
  584. begin
  585. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  586. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  587. end;
  588. begin
  589. if (op = OP_MOVE) then
  590. internalerror(2006031401);
  591. if op = OP_SUB then
  592. begin
  593. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  594. exit;
  595. end;
  596. ophi := TOpCG2AsmOpConstHi[op];
  597. oplo := TOpCG2AsmOpConstLo[op];
  598. gotrlwi := get_rlwi_const(a,l1,l2);
  599. if (op in [OP_AND,OP_OR,OP_XOR]) then
  600. begin
  601. if (a = 0) then
  602. begin
  603. if op = OP_AND then
  604. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  605. else
  606. a_load_reg_reg(list,size,size,src,dst);
  607. exit;
  608. end
  609. else if (a = -1) then
  610. begin
  611. case op of
  612. OP_OR:
  613. case size of
  614. OS_8, OS_S8:
  615. list.concat(taicpu.op_reg_const(A_LI,dst,255));
  616. OS_16, OS_S16:
  617. a_load_const_reg(list,OS_16,65535,dst);
  618. else
  619. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  620. end;
  621. OP_XOR:
  622. case size of
  623. OS_8, OS_S8:
  624. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,255));
  625. OS_16, OS_S16:
  626. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,65535));
  627. else
  628. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  629. end;
  630. OP_AND:
  631. a_load_reg_reg(list,size,size,src,dst);
  632. end;
  633. exit;
  634. end
  635. else if (aword(a) <= high(word)) and
  636. ((op <> OP_AND) or
  637. not gotrlwi) then
  638. begin
  639. if ((size = OS_8) and
  640. (byte(a) <> a)) or
  641. ((size = OS_S8) and
  642. (shortint(a) <> a)) then
  643. internalerror(200604142);
  644. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  645. { and/or/xor -> cannot overflow in high 16 bits }
  646. exit;
  647. end;
  648. { all basic constant instructions also have a shifted form that }
  649. { works only on the highest 16bits, so if lo(a) is 0, we can }
  650. { use that one }
  651. if (word(a) = 0) and
  652. (not(op = OP_AND) or
  653. not gotrlwi) then
  654. begin
  655. if (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  656. internalerror(200604141);
  657. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  658. exit;
  659. end;
  660. end
  661. else if (op = OP_ADD) then
  662. if a = 0 then
  663. begin
  664. a_load_reg_reg(list,size,size,src,dst);
  665. exit
  666. end
  667. else if (a >= low(smallint)) and
  668. (a <= high(smallint)) then
  669. begin
  670. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  671. maybeadjustresult(list,op,size,dst);
  672. exit;
  673. end;
  674. { otherwise, the instructions we can generate depend on the }
  675. { operation }
  676. useReg := false;
  677. case op of
  678. OP_DIV,OP_IDIV:
  679. if (a = 0) then
  680. internalerror(200208103)
  681. else if (a = 1) then
  682. begin
  683. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  684. exit
  685. end
  686. else if ispowerof2(a,l1) then
  687. begin
  688. case op of
  689. OP_DIV:
  690. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  691. OP_IDIV:
  692. begin
  693. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  694. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  695. end;
  696. end;
  697. exit;
  698. end
  699. else
  700. usereg := true;
  701. OP_IMUL, OP_MUL:
  702. if (a = 0) then
  703. begin
  704. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  705. exit
  706. end
  707. else if (a = 1) then
  708. begin
  709. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  710. exit
  711. end
  712. else if ispowerof2(a,l1) then
  713. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  714. else if (longint(a) >= low(smallint)) and
  715. (longint(a) <= high(smallint)) then
  716. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  717. else
  718. usereg := true;
  719. OP_ADD:
  720. begin
  721. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  722. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  723. smallint((a shr 16) + ord(smallint(a) < 0))));
  724. end;
  725. OP_OR:
  726. { try to use rlwimi }
  727. if gotrlwi and
  728. (src = dst) then
  729. begin
  730. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  731. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  732. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  733. scratchreg,0,l1,l2));
  734. end
  735. else
  736. do_lo_hi;
  737. OP_AND:
  738. { try to use rlwinm }
  739. if gotrlwi then
  740. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  741. src,0,l1,l2))
  742. else
  743. useReg := true;
  744. OP_XOR:
  745. do_lo_hi;
  746. OP_SHL,OP_SHR,OP_SAR:
  747. begin
  748. if (a and 31) <> 0 Then
  749. list.concat(taicpu.op_reg_reg_const(
  750. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  751. else
  752. a_load_reg_reg(list,size,size,src,dst);
  753. if (a shr 5) <> 0 then
  754. internalError(68991);
  755. end
  756. else
  757. internalerror(200109091);
  758. end;
  759. { if all else failed, load the constant in a register and then }
  760. { perform the operation }
  761. if useReg then
  762. begin
  763. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  764. a_load_const_reg(list,OS_32,a,scratchreg);
  765. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  766. end;
  767. maybeadjustresult(list,op,size,dst);
  768. end;
  769. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  770. size: tcgsize; src1, src2, dst: tregister);
  771. const
  772. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  773. (A_NONE,A_MR,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  774. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  775. begin
  776. if (op = OP_MOVE) then
  777. internalerror(2006031402);
  778. case op of
  779. OP_NEG,OP_NOT:
  780. begin
  781. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  782. if (op = OP_NOT) and
  783. not(size in [OS_32,OS_S32]) then
  784. { zero/sign extend result again }
  785. a_load_reg_reg(list,OS_32,size,dst,dst);
  786. end;
  787. else
  788. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  789. end;
  790. maybeadjustresult(list,op,size,dst);
  791. end;
  792. {*************** compare instructructions ****************}
  793. procedure tcgppc.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  794. l : tasmlabel);
  795. var
  796. scratch_register: TRegister;
  797. signed: boolean;
  798. begin
  799. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE,OC_EQ,OC_NE];
  800. { in the following case, we generate more efficient code when }
  801. { signed is false }
  802. if (cmp_op in [OC_EQ,OC_NE]) and
  803. (aword(a) >= $8000) and
  804. (aword(a) <= $ffff) then
  805. signed := false;
  806. if signed then
  807. if (a >= low(smallint)) and (a <= high(smallint)) Then
  808. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  809. else
  810. begin
  811. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  812. a_load_const_reg(list,OS_32,a,scratch_register);
  813. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  814. end
  815. else
  816. if (aword(a) <= $ffff) then
  817. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  818. else
  819. begin
  820. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  821. a_load_const_reg(list,OS_32,a,scratch_register);
  822. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  823. end;
  824. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  825. end;
  826. procedure tcgppc.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  827. reg1,reg2 : tregister;l : tasmlabel);
  828. var
  829. op: tasmop;
  830. begin
  831. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  832. op := A_CMPW
  833. else
  834. op := A_CMPLW;
  835. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  836. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  837. end;
  838. procedure tcgppc.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  839. begin
  840. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  841. end;
  842. procedure tcgppc.a_jmp_name(list : TAsmList;const s : string);
  843. var
  844. p : taicpu;
  845. begin
  846. if (target_info.system = system_powerpc_darwin) then
  847. p := taicpu.op_sym(A_B,get_darwin_call_stub(s))
  848. else
  849. p := taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  850. p.is_jmp := true;
  851. list.concat(p)
  852. end;
  853. procedure tcgppc.a_jmp_always(list : TAsmList;l: tasmlabel);
  854. begin
  855. a_jmp(list,A_B,C_None,0,l);
  856. end;
  857. procedure tcgppc.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  858. var
  859. c: tasmcond;
  860. begin
  861. c := flags_to_cond(f);
  862. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  863. end;
  864. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  865. var
  866. testbit: byte;
  867. bitvalue: boolean;
  868. begin
  869. { get the bit to extract from the conditional register + its }
  870. { requested value (0 or 1) }
  871. testbit := ((f.cr-RS_CR0) * 4);
  872. case f.flag of
  873. F_EQ,F_NE:
  874. begin
  875. inc(testbit,2);
  876. bitvalue := f.flag = F_EQ;
  877. end;
  878. F_LT,F_GE:
  879. begin
  880. bitvalue := f.flag = F_LT;
  881. end;
  882. F_GT,F_LE:
  883. begin
  884. inc(testbit);
  885. bitvalue := f.flag = F_GT;
  886. end;
  887. else
  888. internalerror(200112261);
  889. end;
  890. { load the conditional register in the destination reg }
  891. list.concat(taicpu.op_reg(A_MFCR,reg));
  892. { we will move the bit that has to be tested to bit 0 by rotating }
  893. { left }
  894. testbit := (testbit + 1) and 31;
  895. { extract bit }
  896. list.concat(taicpu.op_reg_reg_const_const_const(
  897. A_RLWINM,reg,reg,testbit,31,31));
  898. { if we need the inverse, xor with 1 }
  899. if not bitvalue then
  900. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  901. end;
  902. (*
  903. procedure tcgppc.g_cond2reg(list: TAsmList; const f: TAsmCond; reg: TRegister);
  904. var
  905. testbit: byte;
  906. bitvalue: boolean;
  907. begin
  908. { get the bit to extract from the conditional register + its }
  909. { requested value (0 or 1) }
  910. case f.simple of
  911. false:
  912. begin
  913. { we don't generate this in the compiler }
  914. internalerror(200109062);
  915. end;
  916. true:
  917. case f.cond of
  918. C_None:
  919. internalerror(200109063);
  920. C_LT..C_NU:
  921. begin
  922. testbit := (ord(f.cr) - ord(R_CR0))*4;
  923. inc(testbit,AsmCondFlag2BI[f.cond]);
  924. bitvalue := AsmCondFlagTF[f.cond];
  925. end;
  926. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  927. begin
  928. testbit := f.crbit
  929. bitvalue := AsmCondFlagTF[f.cond];
  930. end;
  931. else
  932. internalerror(200109064);
  933. end;
  934. end;
  935. { load the conditional register in the destination reg }
  936. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  937. { we will move the bit that has to be tested to bit 31 -> rotate }
  938. { left by bitpos+1 (remember, this is big-endian!) }
  939. if bitpos <> 31 then
  940. inc(bitpos)
  941. else
  942. bitpos := 0;
  943. { extract bit }
  944. list.concat(taicpu.op_reg_reg_const_const_const(
  945. A_RLWINM,reg,reg,bitpos,31,31));
  946. { if we need the inverse, xor with 1 }
  947. if not bitvalue then
  948. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  949. end;
  950. *)
  951. { *********** entry/exit code and address loading ************ }
  952. procedure tcgppc.g_save_standard_registers(list:TAsmList);
  953. begin
  954. { this work is done in g_proc_entry }
  955. end;
  956. procedure tcgppc.g_restore_standard_registers(list:TAsmList);
  957. begin
  958. { this work is done in g_proc_exit }
  959. end;
  960. procedure tcgppc.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  961. { generated the entry code of a procedure/function. Note: localsize is the }
  962. { sum of the size necessary for local variables and the maximum possible }
  963. { combined size of ALL the parameters of a procedure called by the current }
  964. { one. }
  965. { This procedure may be called before, as well as after g_return_from_proc }
  966. { is called. NOTE registers are not to be allocated through the register }
  967. { allocator here, because the register colouring has already occured !! }
  968. var regcounter,firstregfpu,firstregint: TSuperRegister;
  969. href : treference;
  970. usesfpr,usesgpr,gotgot : boolean;
  971. cond : tasmcond;
  972. instr : taicpu;
  973. begin
  974. { CR and LR only have to be saved in case they are modified by the current }
  975. { procedure, but currently this isn't checked, so save them always }
  976. { following is the entry code as described in "Altivec Programming }
  977. { Interface Manual", bar the saving of AltiVec registers }
  978. a_reg_alloc(list,NR_STACK_POINTER_REG);
  979. usesgpr := false;
  980. usesfpr := false;
  981. if not(po_assembler in current_procinfo.procdef.procoptions) then
  982. begin
  983. { save link register? }
  984. if (pi_do_call in current_procinfo.flags) or
  985. ([cs_lineinfo,cs_debuginfo] * aktmoduleswitches <> []) then
  986. begin
  987. a_reg_alloc(list,NR_R0);
  988. { save return address... }
  989. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  990. { ... in caller's frame }
  991. case target_info.abi of
  992. abi_powerpc_aix:
  993. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  994. abi_powerpc_sysv:
  995. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  996. end;
  997. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  998. a_reg_dealloc(list,NR_R0);
  999. end;
  1000. (*
  1001. { save the CR if necessary in callers frame. }
  1002. if target_info.abi = abi_powerpc_aix then
  1003. if false then { Not needed at the moment. }
  1004. begin
  1005. a_reg_alloc(list,NR_R0);
  1006. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1007. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1008. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1009. a_reg_dealloc(list,NR_R0);
  1010. end;
  1011. *)
  1012. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  1013. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  1014. usesgpr := firstregint <> 32;
  1015. usesfpr := firstregfpu <> 32;
  1016. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1017. begin
  1018. a_reg_alloc(list,NR_R12);
  1019. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  1020. end;
  1021. end;
  1022. { no GOT pointer loaded yet }
  1023. gotgot:=false;
  1024. if usesfpr then
  1025. begin
  1026. { save floating-point registers
  1027. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  1028. begin
  1029. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g'));
  1030. gotgot:=true;
  1031. end
  1032. else
  1033. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)));
  1034. }
  1035. reference_reset_base(href,NR_R1,-8);
  1036. for regcounter:=firstregfpu to RS_F31 do
  1037. begin
  1038. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  1039. dec(href.offset,8);
  1040. end;
  1041. { compute start of gpr save area }
  1042. inc(href.offset,4);
  1043. end
  1044. else
  1045. { compute start of gpr save area }
  1046. reference_reset_base(href,NR_R1,-4);
  1047. { save gprs and fetch GOT pointer }
  1048. if usesgpr then
  1049. begin
  1050. {
  1051. if cs_create_pic in aktmoduleswitches then
  1052. begin
  1053. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g'));
  1054. gotgot:=true;
  1055. end
  1056. else
  1057. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)))
  1058. }
  1059. if (firstregint <= RS_R22) or
  1060. ((cs_opt_size in aktoptimizerswitches) and
  1061. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  1062. (firstregint <= RS_R29)) then
  1063. begin
  1064. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  1065. list.concat(taicpu.op_reg_ref(A_STMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  1066. end
  1067. else
  1068. for regcounter:=firstregint to RS_R31 do
  1069. begin
  1070. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter,R_SUBNONE),href);
  1071. dec(href.offset,4);
  1072. end;
  1073. end;
  1074. { done in ncgutil because it may only be released after the parameters }
  1075. { have been moved to their final resting place }
  1076. { if (tppcprocinfo(current_procinfo).needs_frame_pointer) then }
  1077. { a_reg_dealloc(list,NR_R12); }
  1078. { if we didn't get the GOT pointer till now, we've to calculate it now }
  1079. (*
  1080. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  1081. case target_info.system of
  1082. system_powerpc_darwin:
  1083. begin
  1084. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1085. fillchar(cond,sizeof(cond),0);
  1086. cond.simple:=false;
  1087. cond.bo:=20;
  1088. cond.bi:=31;
  1089. instr:=taicpu.op_sym(A_BCL,current_procinfo.CurrGOTLabel);
  1090. instr.setcondition(cond);
  1091. list.concat(instr);
  1092. a_label(list,current_procinfo.CurrGOTLabel);
  1093. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  1094. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  1095. end;
  1096. else
  1097. begin
  1098. a_reg_alloc(list,NR_R31);
  1099. { place GOT ptr in r31 }
  1100. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  1101. end;
  1102. end;
  1103. *)
  1104. if (not nostackframe) and
  1105. (localsize <> 0) then
  1106. begin
  1107. if (localsize <= high(smallint)) then
  1108. begin
  1109. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1110. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1111. end
  1112. else
  1113. begin
  1114. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1115. { can't use getregisterint here, the register colouring }
  1116. { is already done when we get here }
  1117. href.index := NR_R11;
  1118. a_reg_alloc(list,href.index);
  1119. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1120. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1121. a_reg_dealloc(list,href.index);
  1122. end;
  1123. end;
  1124. { save the CR if necessary ( !!! never done currently ) }
  1125. { still need to find out where this has to be done for SystemV
  1126. a_reg_alloc(list,R_0);
  1127. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  1128. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  1129. new_reference(STACK_POINTER_REG,LA_CR)));
  1130. a_reg_dealloc(list,R_0);
  1131. }
  1132. { now comes the AltiVec context save, not yet implemented !!! }
  1133. end;
  1134. procedure tcgppc.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1135. { This procedure may be called before, as well as after g_stackframe_entry }
  1136. { is called. NOTE registers are not to be allocated through the register }
  1137. { allocator here, because the register colouring has already occured !! }
  1138. var
  1139. regcounter,firstregfpu,firstregint: TsuperRegister;
  1140. href : treference;
  1141. usesfpr,usesgpr,genret : boolean;
  1142. localsize: aint;
  1143. begin
  1144. { AltiVec context restore, not yet implemented !!! }
  1145. usesfpr:=false;
  1146. usesgpr:=false;
  1147. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1148. begin
  1149. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  1150. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  1151. usesgpr := firstregint <> 32;
  1152. usesfpr := firstregfpu <> 32;
  1153. end;
  1154. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1155. { adjust r1 }
  1156. { (register allocator is no longer valid at this time and an add of 0 }
  1157. { is translated into a move, which is then registered with the register }
  1158. { allocator, causing a crash }
  1159. if (not nostackframe) and
  1160. (localsize <> 0) then
  1161. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1162. { no return (blr) generated yet }
  1163. genret:=true;
  1164. if usesfpr then
  1165. begin
  1166. reference_reset_base(href,NR_R1,-8);
  1167. for regcounter := firstregfpu to RS_F31 do
  1168. begin
  1169. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1170. dec(href.offset,8);
  1171. end;
  1172. inc(href.offset,4);
  1173. end
  1174. else
  1175. reference_reset_base(href,NR_R1,-4);
  1176. if (usesgpr) then
  1177. begin
  1178. if (firstregint <= RS_R22) or
  1179. ((cs_opt_size in aktoptimizerswitches) and
  1180. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  1181. (firstregint <= RS_R29)) then
  1182. begin
  1183. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  1184. list.concat(taicpu.op_reg_ref(A_LMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  1185. end
  1186. else
  1187. for regcounter:=firstregint to RS_R31 do
  1188. begin
  1189. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter,R_SUBNONE));
  1190. dec(href.offset,4);
  1191. end;
  1192. end;
  1193. (*
  1194. { restore fprs and return }
  1195. if usesfpr then
  1196. begin
  1197. { address of fpr save area to r11 }
  1198. r:=NR_R12;
  1199. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1200. {
  1201. if (pi_do_call in current_procinfo.flags) then
  1202. a_call_name(current_asmdata.RefAsmSymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_x'))
  1203. else
  1204. { leaf node => lr haven't to be restored }
  1205. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+'_l');
  1206. genret:=false;
  1207. }
  1208. end;
  1209. *)
  1210. { if we didn't generate the return code, we've to do it now }
  1211. if genret then
  1212. begin
  1213. { load link register? }
  1214. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1215. begin
  1216. if (pi_do_call in current_procinfo.flags) then
  1217. begin
  1218. case target_info.abi of
  1219. abi_powerpc_aix:
  1220. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1221. abi_powerpc_sysv:
  1222. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1223. end;
  1224. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1225. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1226. end;
  1227. (*
  1228. { restore the CR if necessary from callers frame}
  1229. if target_info.abi = abi_powerpc_aix then
  1230. if false then { Not needed at the moment. }
  1231. begin
  1232. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1233. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1234. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1235. a_reg_dealloc(list,NR_R0);
  1236. end;
  1237. *)
  1238. end;
  1239. list.concat(taicpu.op_none(A_BLR));
  1240. end;
  1241. end;
  1242. function tcgppc.save_regs(list : TAsmList):longint;
  1243. {Generates code which saves used non-volatile registers in
  1244. the save area right below the address the stackpointer point to.
  1245. Returns the actual used save area size.}
  1246. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1247. usesfpr,usesgpr: boolean;
  1248. href : treference;
  1249. offset: aint;
  1250. regcounter2, firstfpureg: Tsuperregister;
  1251. begin
  1252. usesfpr:=false;
  1253. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1254. begin
  1255. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1256. case target_info.abi of
  1257. abi_powerpc_aix:
  1258. firstfpureg := RS_F14;
  1259. abi_powerpc_sysv:
  1260. firstfpureg := RS_F9;
  1261. else
  1262. internalerror(2003122903);
  1263. end;
  1264. for regcounter:=firstfpureg to RS_F31 do
  1265. begin
  1266. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1267. begin
  1268. usesfpr:=true;
  1269. firstregfpu:=regcounter;
  1270. break;
  1271. end;
  1272. end;
  1273. end;
  1274. usesgpr:=false;
  1275. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1276. for regcounter2:=RS_R13 to RS_R31 do
  1277. begin
  1278. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1279. begin
  1280. usesgpr:=true;
  1281. firstreggpr:=regcounter2;
  1282. break;
  1283. end;
  1284. end;
  1285. offset:= 0;
  1286. { save floating-point registers }
  1287. if usesfpr then
  1288. for regcounter := firstregfpu to RS_F31 do
  1289. begin
  1290. offset:= offset - 8;
  1291. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1292. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1293. end;
  1294. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1295. { save gprs in gpr save area }
  1296. if usesgpr then
  1297. if firstreggpr < RS_R30 then
  1298. begin
  1299. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1300. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1301. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1302. {STMW stores multiple registers}
  1303. end
  1304. else
  1305. begin
  1306. for regcounter := firstreggpr to RS_R31 do
  1307. begin
  1308. offset:= offset - 4;
  1309. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1310. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1311. end;
  1312. end;
  1313. { now comes the AltiVec context save, not yet implemented !!! }
  1314. save_regs:= -offset;
  1315. end;
  1316. procedure tcgppc.restore_regs(list : TAsmList);
  1317. {Generates code which restores used non-volatile registers from
  1318. the save area right below the address the stackpointer point to.}
  1319. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1320. usesfpr,usesgpr: boolean;
  1321. href : treference;
  1322. offset: integer;
  1323. regcounter2, firstfpureg: Tsuperregister;
  1324. begin
  1325. usesfpr:=false;
  1326. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1327. begin
  1328. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1329. case target_info.abi of
  1330. abi_powerpc_aix:
  1331. firstfpureg := RS_F14;
  1332. abi_powerpc_sysv:
  1333. firstfpureg := RS_F9;
  1334. else
  1335. internalerror(2003122903);
  1336. end;
  1337. for regcounter:=firstfpureg to RS_F31 do
  1338. begin
  1339. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1340. begin
  1341. usesfpr:=true;
  1342. firstregfpu:=regcounter;
  1343. break;
  1344. end;
  1345. end;
  1346. end;
  1347. usesgpr:=false;
  1348. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1349. for regcounter2:=RS_R13 to RS_R31 do
  1350. begin
  1351. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1352. begin
  1353. usesgpr:=true;
  1354. firstreggpr:=regcounter2;
  1355. break;
  1356. end;
  1357. end;
  1358. offset:= 0;
  1359. { restore fp registers }
  1360. if usesfpr then
  1361. for regcounter := firstregfpu to RS_F31 do
  1362. begin
  1363. offset:= offset - 8;
  1364. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1365. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1366. end;
  1367. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1368. { restore gprs }
  1369. if usesgpr then
  1370. if firstreggpr < RS_R30 then
  1371. begin
  1372. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1373. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1374. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1375. {LMW loads multiple registers}
  1376. end
  1377. else
  1378. begin
  1379. for regcounter := firstreggpr to RS_R31 do
  1380. begin
  1381. offset:= offset - 4;
  1382. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1383. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1384. end;
  1385. end;
  1386. { now comes the AltiVec context restore, not yet implemented !!! }
  1387. end;
  1388. procedure tcgppc.g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  1389. (* NOT IN USE *)
  1390. { generated the entry code of a procedure/function. Note: localsize is the }
  1391. { sum of the size necessary for local variables and the maximum possible }
  1392. { combined size of ALL the parameters of a procedure called by the current }
  1393. { one }
  1394. const
  1395. macosLinkageAreaSize = 24;
  1396. var
  1397. href : treference;
  1398. registerSaveAreaSize : longint;
  1399. begin
  1400. if (localsize mod 8) <> 0 then
  1401. internalerror(58991);
  1402. { CR and LR only have to be saved in case they are modified by the current }
  1403. { procedure, but currently this isn't checked, so save them always }
  1404. { following is the entry code as described in "Altivec Programming }
  1405. { Interface Manual", bar the saving of AltiVec registers }
  1406. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1407. a_reg_alloc(list,NR_R0);
  1408. { save return address in callers frame}
  1409. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1410. { ... in caller's frame }
  1411. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1412. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1413. a_reg_dealloc(list,NR_R0);
  1414. { save non-volatile registers in callers frame}
  1415. registerSaveAreaSize:= save_regs(list);
  1416. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1417. a_reg_alloc(list,NR_R0);
  1418. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1419. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1420. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1421. a_reg_dealloc(list,NR_R0);
  1422. (*
  1423. { save pointer to incoming arguments }
  1424. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1425. *)
  1426. (*
  1427. a_reg_alloc(list,R_12);
  1428. { 0 or 8 based on SP alignment }
  1429. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1430. R_12,STACK_POINTER_REG,0,28,28));
  1431. { add in stack length }
  1432. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1433. -localsize));
  1434. { establish new alignment }
  1435. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1436. a_reg_dealloc(list,R_12);
  1437. *)
  1438. { allocate stack frame }
  1439. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1440. inc(localsize,tg.lasttemp);
  1441. localsize:=align(localsize,16);
  1442. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1443. if (localsize <> 0) then
  1444. begin
  1445. if (localsize <= high(smallint)) then
  1446. begin
  1447. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1448. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1449. end
  1450. else
  1451. begin
  1452. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1453. href.index := NR_R11;
  1454. a_reg_alloc(list,href.index);
  1455. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1456. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1457. a_reg_dealloc(list,href.index);
  1458. end;
  1459. end;
  1460. end;
  1461. procedure tcgppc.g_return_from_proc_mac(list : TAsmList;parasize : aint);
  1462. (* NOT IN USE *)
  1463. var
  1464. href : treference;
  1465. begin
  1466. a_reg_alloc(list,NR_R0);
  1467. { restore stack pointer }
  1468. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1469. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1470. (*
  1471. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1472. *)
  1473. { restore the CR if necessary from callers frame
  1474. ( !!! always done currently ) }
  1475. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1476. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1477. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1478. a_reg_dealloc(list,NR_R0);
  1479. (*
  1480. { restore return address from callers frame }
  1481. reference_reset_base(href,STACK_POINTER_REG,8);
  1482. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1483. *)
  1484. { restore non-volatile registers from callers frame }
  1485. restore_regs(list);
  1486. (*
  1487. { return to caller }
  1488. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1489. list.concat(taicpu.op_none(A_BLR));
  1490. *)
  1491. { restore return address from callers frame }
  1492. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1493. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1494. { return to caller }
  1495. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1496. list.concat(taicpu.op_none(A_BLR));
  1497. end;
  1498. procedure tcgppc.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1499. var
  1500. ref2, tmpref: treference;
  1501. begin
  1502. ref2 := ref;
  1503. fixref(list,ref2);
  1504. if assigned(ref2.symbol) then
  1505. begin
  1506. if target_info.system = system_powerpc_macos then
  1507. begin
  1508. if macos_direct_globals then
  1509. begin
  1510. reference_reset(tmpref);
  1511. tmpref.offset := ref2.offset;
  1512. tmpref.symbol := ref2.symbol;
  1513. tmpref.base := NR_NO;
  1514. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1515. end
  1516. else
  1517. begin
  1518. reference_reset(tmpref);
  1519. tmpref.symbol := ref2.symbol;
  1520. tmpref.offset := 0;
  1521. tmpref.base := NR_RTOC;
  1522. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1523. if ref2.offset <> 0 then
  1524. begin
  1525. reference_reset(tmpref);
  1526. tmpref.offset := ref2.offset;
  1527. tmpref.base:= r;
  1528. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1529. end;
  1530. end;
  1531. if ref2.base <> NR_NO then
  1532. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1533. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1534. end
  1535. else
  1536. begin
  1537. { add the symbol's value to the base of the reference, and if the }
  1538. { reference doesn't have a base, create one }
  1539. reference_reset(tmpref);
  1540. tmpref.offset := ref2.offset;
  1541. tmpref.symbol := ref2.symbol;
  1542. tmpref.relsymbol := ref2.relsymbol;
  1543. tmpref.refaddr := addr_hi;
  1544. if ref2.base<> NR_NO then
  1545. begin
  1546. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1547. ref2.base,tmpref));
  1548. end
  1549. else
  1550. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1551. tmpref.base := NR_NO;
  1552. tmpref.refaddr := addr_lo;
  1553. { can be folded with one of the next instructions by the }
  1554. { optimizer probably }
  1555. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1556. end
  1557. end
  1558. else if ref2.offset <> 0 Then
  1559. if ref2.base <> NR_NO then
  1560. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1561. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1562. { occurs, so now only ref.offset has to be loaded }
  1563. else
  1564. a_load_const_reg(list,OS_32,ref2.offset,r)
  1565. else if ref2.index <> NR_NO Then
  1566. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1567. else if (ref2.base <> NR_NO) and
  1568. (r <> ref2.base) then
  1569. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1570. else
  1571. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1572. end;
  1573. { ************* concatcopy ************ }
  1574. {$ifndef ppc603}
  1575. const
  1576. maxmoveunit = 8;
  1577. {$else ppc603}
  1578. const
  1579. maxmoveunit = 4;
  1580. {$endif ppc603}
  1581. procedure tcgppc.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1582. var
  1583. countreg: TRegister;
  1584. src, dst: TReference;
  1585. lab: tasmlabel;
  1586. count, count2: aint;
  1587. size: tcgsize;
  1588. copyreg: tregister;
  1589. begin
  1590. {$ifdef extdebug}
  1591. if len > high(longint) then
  1592. internalerror(2002072704);
  1593. {$endif extdebug}
  1594. if (references_equal(source,dest)) then
  1595. exit;
  1596. { make sure short loads are handled as optimally as possible }
  1597. if (len <= maxmoveunit) and
  1598. (byte(len) in [1,2,4,8]) then
  1599. begin
  1600. if len < 8 then
  1601. begin
  1602. size := int_cgsize(len);
  1603. a_load_ref_ref(list,size,size,source,dest);
  1604. end
  1605. else
  1606. begin
  1607. copyreg := getfpuregister(list,OS_F64);
  1608. a_loadfpu_ref_reg(list,OS_F64,source,copyreg);
  1609. a_loadfpu_reg_ref(list,OS_F64,copyreg,dest);
  1610. end;
  1611. exit;
  1612. end;
  1613. count := len div maxmoveunit;
  1614. reference_reset(src);
  1615. reference_reset(dst);
  1616. { load the address of source into src.base }
  1617. if (count > 4) or
  1618. not issimpleref(source) or
  1619. ((source.index <> NR_NO) and
  1620. ((source.offset + longint(len)) > high(smallint))) then
  1621. begin
  1622. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1623. a_loadaddr_ref_reg(list,source,src.base);
  1624. end
  1625. else
  1626. begin
  1627. src := source;
  1628. end;
  1629. { load the address of dest into dst.base }
  1630. if (count > 4) or
  1631. not issimpleref(dest) or
  1632. ((dest.index <> NR_NO) and
  1633. ((dest.offset + longint(len)) > high(smallint))) then
  1634. begin
  1635. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1636. a_loadaddr_ref_reg(list,dest,dst.base);
  1637. end
  1638. else
  1639. begin
  1640. dst := dest;
  1641. end;
  1642. {$ifndef ppc603}
  1643. if count > 4 then
  1644. { generate a loop }
  1645. begin
  1646. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1647. { have to be set to 8. I put an Inc there so debugging may be }
  1648. { easier (should offset be different from zero here, it will be }
  1649. { easy to notice in the generated assembler }
  1650. inc(dst.offset,8);
  1651. inc(src.offset,8);
  1652. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1653. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1654. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1655. a_load_const_reg(list,OS_32,count,countreg);
  1656. copyreg := getfpuregister(list,OS_F64);
  1657. a_reg_sync(list,copyreg);
  1658. current_asmdata.getjumplabel(lab);
  1659. a_label(list, lab);
  1660. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1661. list.concat(taicpu.op_reg_ref(A_LFDU,copyreg,src));
  1662. list.concat(taicpu.op_reg_ref(A_STFDU,copyreg,dst));
  1663. a_jmp(list,A_BC,C_NE,0,lab);
  1664. a_reg_sync(list,copyreg);
  1665. len := len mod 8;
  1666. end;
  1667. count := len div 8;
  1668. if count > 0 then
  1669. { unrolled loop }
  1670. begin
  1671. copyreg := getfpuregister(list,OS_F64);
  1672. for count2 := 1 to count do
  1673. begin
  1674. a_loadfpu_ref_reg(list,OS_F64,src,copyreg);
  1675. a_loadfpu_reg_ref(list,OS_F64,copyreg,dst);
  1676. inc(src.offset,8);
  1677. inc(dst.offset,8);
  1678. end;
  1679. len := len mod 8;
  1680. end;
  1681. if (len and 4) <> 0 then
  1682. begin
  1683. a_reg_alloc(list,NR_R0);
  1684. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1685. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1686. inc(src.offset,4);
  1687. inc(dst.offset,4);
  1688. a_reg_dealloc(list,NR_R0);
  1689. end;
  1690. {$else not ppc603}
  1691. if count > 4 then
  1692. { generate a loop }
  1693. begin
  1694. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1695. { have to be set to 4. I put an Inc there so debugging may be }
  1696. { easier (should offset be different from zero here, it will be }
  1697. { easy to notice in the generated assembler }
  1698. inc(dst.offset,4);
  1699. inc(src.offset,4);
  1700. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1701. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1702. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1703. a_load_const_reg(list,OS_32,count,countreg);
  1704. { explicitely allocate R_0 since it can be used safely here }
  1705. { (for holding date that's being copied) }
  1706. a_reg_alloc(list,NR_R0);
  1707. current_asmdata.getjumplabel(lab);
  1708. a_label(list, lab);
  1709. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1710. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1711. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1712. a_jmp(list,A_BC,C_NE,0,lab);
  1713. a_reg_dealloc(list,NR_R0);
  1714. len := len mod 4;
  1715. end;
  1716. count := len div 4;
  1717. if count > 0 then
  1718. { unrolled loop }
  1719. begin
  1720. a_reg_alloc(list,NR_R0);
  1721. for count2 := 1 to count do
  1722. begin
  1723. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1724. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1725. inc(src.offset,4);
  1726. inc(dst.offset,4);
  1727. end;
  1728. a_reg_dealloc(list,NR_R0);
  1729. len := len mod 4;
  1730. end;
  1731. {$endif not ppc603}
  1732. { copy the leftovers }
  1733. if (len and 2) <> 0 then
  1734. begin
  1735. a_reg_alloc(list,NR_R0);
  1736. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1737. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1738. inc(src.offset,2);
  1739. inc(dst.offset,2);
  1740. a_reg_dealloc(list,NR_R0);
  1741. end;
  1742. if (len and 1) <> 0 then
  1743. begin
  1744. a_reg_alloc(list,NR_R0);
  1745. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1746. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1747. a_reg_dealloc(list,NR_R0);
  1748. end;
  1749. end;
  1750. procedure tcgppc.g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef);
  1751. var
  1752. hl : tasmlabel;
  1753. begin
  1754. if not(cs_check_overflow in aktlocalswitches) then
  1755. exit;
  1756. current_asmdata.getjumplabel(hl);
  1757. if not ((def.deftype=pointerdef) or
  1758. ((def.deftype=orddef) and
  1759. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1760. bool8bit,bool16bit,bool32bit]))) then
  1761. begin
  1762. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1763. a_jmp(list,A_BC,C_NO,7,hl)
  1764. end
  1765. else
  1766. a_jmp_cond(list,OC_AE,hl);
  1767. a_call_name(list,'FPC_OVERFLOW');
  1768. a_label(list,hl);
  1769. end;
  1770. procedure tcgppc.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1771. procedure loadvmttor11;
  1772. var
  1773. href : treference;
  1774. begin
  1775. reference_reset_base(href,NR_R3,0);
  1776. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  1777. end;
  1778. procedure op_onr11methodaddr;
  1779. var
  1780. href : treference;
  1781. begin
  1782. if (procdef.extnumber=$ffff) then
  1783. Internalerror(200006139);
  1784. { call/jmp vmtoffs(%eax) ; method offs }
  1785. reference_reset_base(href,NR_R11,procdef._class.vmtmethodoffset(procdef.extnumber));
  1786. if not((longint(href.offset) >= low(smallint)) and
  1787. (longint(href.offset) <= high(smallint))) then
  1788. begin
  1789. list.concat(taicpu.op_reg_reg_const(A_ADDIS,NR_R11,NR_R11,
  1790. smallint((href.offset shr 16)+ord(smallint(href.offset and $ffff) < 0))));
  1791. href.offset := smallint(href.offset and $ffff);
  1792. end;
  1793. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R11,href));
  1794. list.concat(taicpu.op_reg(A_MTCTR,NR_R11));
  1795. list.concat(taicpu.op_none(A_BCTR));
  1796. end;
  1797. var
  1798. make_global : boolean;
  1799. begin
  1800. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1801. Internalerror(200006137);
  1802. if not assigned(procdef._class) or
  1803. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1804. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1805. Internalerror(200006138);
  1806. if procdef.owner.symtabletype<>objectsymtable then
  1807. Internalerror(200109191);
  1808. make_global:=false;
  1809. if (not current_module.is_unit) or
  1810. (cs_create_smart in aktmoduleswitches) or
  1811. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1812. make_global:=true;
  1813. if make_global then
  1814. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1815. else
  1816. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1817. { set param1 interface to self }
  1818. g_adjust_self_value(list,procdef,ioffset);
  1819. { case 4 }
  1820. if po_virtualmethod in procdef.procoptions then
  1821. begin
  1822. loadvmttor11;
  1823. op_onr11methodaddr;
  1824. end
  1825. { case 0 }
  1826. else
  1827. if not(target_info.system = system_powerpc_darwin) then
  1828. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)))
  1829. else
  1830. list.concat(taicpu.op_sym(A_B,get_darwin_call_stub(procdef.mangledname)));
  1831. List.concat(Tai_symbol_end.Createname(labelname));
  1832. end;
  1833. {***************** This is private property, keep out! :) *****************}
  1834. function tcgppc.issimpleref(const ref: treference): boolean;
  1835. begin
  1836. if (ref.base = NR_NO) and
  1837. (ref.index <> NR_NO) then
  1838. internalerror(200208101);
  1839. result :=
  1840. not(assigned(ref.symbol)) and
  1841. (((ref.index = NR_NO) and
  1842. (ref.offset >= low(smallint)) and
  1843. (ref.offset <= high(smallint))) or
  1844. ((ref.index <> NR_NO) and
  1845. (ref.offset = 0)));
  1846. end;
  1847. function tcgppc.fixref(list: TAsmList; var ref: treference): boolean;
  1848. var
  1849. tmpreg: tregister;
  1850. begin
  1851. result := false;
  1852. if (target_info.system = system_powerpc_darwin) and
  1853. assigned(ref.symbol) and
  1854. (ref.symbol.bind = AB_EXTERNAL) then
  1855. begin
  1856. tmpreg := g_indirect_sym_load(list,ref.symbol.name);
  1857. if (ref.base = NR_NO) then
  1858. ref.base := tmpreg
  1859. else if (ref.index = NR_NO) then
  1860. ref.index := tmpreg
  1861. else
  1862. begin
  1863. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1864. ref.base := tmpreg;
  1865. end;
  1866. ref.symbol := nil;
  1867. end;
  1868. if (ref.base = NR_NO) then
  1869. begin
  1870. ref.base := ref.index;
  1871. ref.index := NR_NO;
  1872. end;
  1873. if (ref.base <> NR_NO) then
  1874. begin
  1875. if (ref.index <> NR_NO) and
  1876. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1877. begin
  1878. result := true;
  1879. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1880. list.concat(taicpu.op_reg_reg_reg(
  1881. A_ADD,tmpreg,ref.base,ref.index));
  1882. ref.index := NR_NO;
  1883. ref.base := tmpreg;
  1884. end
  1885. end
  1886. else
  1887. if ref.index <> NR_NO then
  1888. internalerror(200208102);
  1889. end;
  1890. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1891. { that's the case, we can use rlwinm to do an AND operation }
  1892. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1893. var
  1894. temp : longint;
  1895. testbit : aint;
  1896. compare: boolean;
  1897. begin
  1898. get_rlwi_const := false;
  1899. if (a = 0) or (a = -1) then
  1900. exit;
  1901. { start with the lowest bit }
  1902. testbit := 1;
  1903. { check its value }
  1904. compare := boolean(a and testbit);
  1905. { find out how long the run of bits with this value is }
  1906. { (it's impossible that all bits are 1 or 0, because in that case }
  1907. { this function wouldn't have been called) }
  1908. l1 := 31;
  1909. while (((a and testbit) <> 0) = compare) do
  1910. begin
  1911. testbit := testbit shl 1;
  1912. dec(l1);
  1913. end;
  1914. { check the length of the run of bits that comes next }
  1915. compare := not compare;
  1916. l2 := l1;
  1917. while (((a and testbit) <> 0) = compare) and
  1918. (l2 >= 0) do
  1919. begin
  1920. testbit := testbit shl 1;
  1921. dec(l2);
  1922. end;
  1923. { and finally the check whether the rest of the bits all have the }
  1924. { same value }
  1925. compare := not compare;
  1926. temp := l2;
  1927. if temp >= 0 then
  1928. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1929. exit;
  1930. { we have done "not(not(compare))", so compare is back to its }
  1931. { initial value. If the lowest bit was 0, a is of the form }
  1932. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1933. { because l2 now contains the position of the last zero of the }
  1934. { first run instead of that of the first 1) so switch l1 and l2 }
  1935. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1936. if not compare then
  1937. begin
  1938. temp := l1;
  1939. l1 := l2+1;
  1940. l2 := temp;
  1941. end
  1942. else
  1943. { otherwise, l1 currently contains the position of the last }
  1944. { zero instead of that of the first 1 of the second run -> +1 }
  1945. inc(l1);
  1946. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1947. l1 := l1 and 31;
  1948. l2 := l2 and 31;
  1949. get_rlwi_const := true;
  1950. end;
  1951. procedure tcgppc.a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  1952. ref: treference);
  1953. var
  1954. tmpreg: tregister;
  1955. tmpref: treference;
  1956. largeOffset: Boolean;
  1957. begin
  1958. tmpreg := NR_NO;
  1959. if target_info.system = system_powerpc_macos then
  1960. begin
  1961. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1962. high(smallint)-low(smallint));
  1963. if assigned(ref.symbol) then
  1964. begin {Load symbol's value}
  1965. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1966. reference_reset(tmpref);
  1967. tmpref.symbol := ref.symbol;
  1968. tmpref.base := NR_RTOC;
  1969. if macos_direct_globals then
  1970. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1971. else
  1972. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1973. end;
  1974. if largeOffset then
  1975. begin {Add hi part of offset}
  1976. reference_reset(tmpref);
  1977. if Smallint(Lo(ref.offset)) < 0 then
  1978. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1979. else
  1980. tmpref.offset := Hi(ref.offset);
  1981. if (tmpreg <> NR_NO) then
  1982. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1983. else
  1984. begin
  1985. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1986. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1987. end;
  1988. end;
  1989. if (tmpreg <> NR_NO) then
  1990. begin
  1991. {Add content of base register}
  1992. if ref.base <> NR_NO then
  1993. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1994. ref.base,tmpreg));
  1995. {Make ref ready to be used by op}
  1996. ref.symbol:= nil;
  1997. ref.base:= tmpreg;
  1998. if largeOffset then
  1999. ref.offset := Smallint(Lo(ref.offset));
  2000. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2001. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  2002. end
  2003. else
  2004. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2005. end
  2006. else {if target_info.system <> system_powerpc_macos}
  2007. begin
  2008. if assigned(ref.symbol) or
  2009. (cardinal(ref.offset-low(smallint)) >
  2010. high(smallint)-low(smallint)) then
  2011. begin
  2012. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2013. reference_reset(tmpref);
  2014. tmpref.symbol := ref.symbol;
  2015. tmpref.relsymbol := ref.relsymbol;
  2016. tmpref.offset := ref.offset;
  2017. tmpref.refaddr := addr_hi;
  2018. if ref.base <> NR_NO then
  2019. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  2020. ref.base,tmpref))
  2021. else
  2022. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  2023. ref.base := tmpreg;
  2024. ref.refaddr := addr_lo;
  2025. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2026. end
  2027. else
  2028. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2029. end;
  2030. end;
  2031. procedure tcgppc.a_jmp(list: TAsmList; op: tasmop; c: tasmcondflag;
  2032. crval: longint; l: tasmlabel);
  2033. var
  2034. p: taicpu;
  2035. begin
  2036. p := taicpu.op_sym(op,l);
  2037. if op <> A_B then
  2038. create_cond_norm(c,crval,p.condition);
  2039. p.is_jmp := true;
  2040. list.concat(p)
  2041. end;
  2042. procedure tcg64fppc.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  2043. begin
  2044. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  2045. end;
  2046. procedure tcg64fppc.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  2047. begin
  2048. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  2049. end;
  2050. procedure tcg64fppc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2051. begin
  2052. case op of
  2053. OP_AND,OP_OR,OP_XOR:
  2054. begin
  2055. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2056. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2057. end;
  2058. OP_ADD:
  2059. begin
  2060. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2061. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2062. end;
  2063. OP_SUB:
  2064. begin
  2065. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2066. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2067. end;
  2068. else
  2069. internalerror(2002072801);
  2070. end;
  2071. end;
  2072. procedure tcg64fppc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  2073. const
  2074. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2075. (A_SUBIC,A_SUBC,A_ADDME));
  2076. var
  2077. tmpreg: tregister;
  2078. tmpreg64: tregister64;
  2079. issub: boolean;
  2080. begin
  2081. case op of
  2082. OP_AND,OP_OR,OP_XOR:
  2083. begin
  2084. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  2085. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2086. regdst.reghi);
  2087. end;
  2088. OP_ADD, OP_SUB:
  2089. begin
  2090. if (value < 0) then
  2091. begin
  2092. if op = OP_ADD then
  2093. op := OP_SUB
  2094. else
  2095. op := OP_ADD;
  2096. value := -value;
  2097. end;
  2098. if (longint(value) <> 0) then
  2099. begin
  2100. issub := op = OP_SUB;
  2101. if (value > 0) and
  2102. (value-ord(issub) <= 32767) then
  2103. begin
  2104. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2105. regdst.reglo,regsrc.reglo,longint(value)));
  2106. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2107. regdst.reghi,regsrc.reghi));
  2108. end
  2109. else if ((value shr 32) = 0) then
  2110. begin
  2111. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2112. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2113. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2114. regdst.reglo,regsrc.reglo,tmpreg));
  2115. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2116. regdst.reghi,regsrc.reghi));
  2117. end
  2118. else
  2119. begin
  2120. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2121. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2122. a_load64_const_reg(list,value,tmpreg64);
  2123. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  2124. end
  2125. end
  2126. else
  2127. begin
  2128. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2129. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2130. regdst.reghi);
  2131. end;
  2132. end;
  2133. else
  2134. internalerror(2002072802);
  2135. end;
  2136. end;
  2137. begin
  2138. cg := tcgppc.create;
  2139. cg64 :=tcg64fppc.create;
  2140. end.