cgcpu.pas 91 KB

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