cgcpu.pas 86 KB

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