cgcpu.pas 75 KB

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