cgcpu.pas 94 KB

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