cgcpu.pas 80 KB

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