cgcpu.pas 86 KB

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