cgcpu.pas 90 KB

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