cgcpu.pas 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726
  1. {
  2. Copyright (c) 1998-2002 by the FPC team
  3. This unit implements the code generator for the 680x0
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,cgobj,globtype,
  22. aasmbase,aasmtai,aasmdata,aasmcpu,
  23. cpubase,cpuinfo,
  24. parabase,cpupara,
  25. node,symconst,symtype,symdef,
  26. cgutils,cg64f32;
  27. type
  28. tcg68k = class(tcg)
  29. procedure init_register_allocators;override;
  30. procedure done_register_allocators;override;
  31. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  32. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  33. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  34. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  35. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  36. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  37. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);override;
  38. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  39. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  40. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  41. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  42. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  43. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  44. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);override;
  45. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  46. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  47. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  48. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  49. procedure a_loadfpu_reg_cgpara(list : TAsmList; size : tcgsize;const reg : tregister;const cgpara : TCGPara); override;
  50. procedure a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);override;
  51. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister); override;
  52. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  53. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  54. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); override;
  55. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  56. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister; l : tasmlabel);override;
  57. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel); override;
  58. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  59. procedure a_jmp_name(list : TAsmList;const s : string); override;
  60. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  61. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  62. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  63. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  64. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  65. { generates overflow checking code for a node }
  66. procedure g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef); override;
  67. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  68. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  69. procedure g_save_registers(list:TAsmList);override;
  70. procedure g_restore_registers(list:TAsmList);override;
  71. procedure g_adjust_self_value(list:TAsmList;procdef:tprocdef;ioffset:tcgint);override;
  72. { # Sign or zero extend the register to a full 32-bit value.
  73. The new value is left in the same register.
  74. }
  75. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  76. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  77. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  78. function fixref(list: TAsmList; var ref: treference; fullyresolve: boolean): boolean;
  79. function force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  80. procedure move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  81. { optimize mul with const to a sequence of shifts and subs/adds, mainly for the '000 to '030 }
  82. function optimize_const_mul_to_shift_sub_add(list: TAsmList; maxops: longint; a: tcgint; size: tcgsize; reg: TRegister): boolean;
  83. protected
  84. procedure call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  85. procedure call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  86. procedure check_register_size(size:tcgsize;reg:tregister);
  87. end;
  88. tcg64f68k = class(tcg64f32)
  89. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG; size: tcgsize; regsrc,regdst : tregister64);override;
  90. procedure a_op64_const_reg(list : TAsmList;op:TOpCG; size: tcgsize; value : int64;regdst : tregister64);override;
  91. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  92. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const ref : treference);override;
  93. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference); override;
  94. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64); override;
  95. end;
  96. { This function returns true if the reference+offset is valid.
  97. Otherwise extra code must be generated to solve the reference.
  98. On the m68k, this verifies that the reference is valid
  99. (e.g : if index register is used, then the max displacement
  100. is 256 bytes, if only base is used, then max displacement
  101. is 32K
  102. }
  103. function isvalidrefoffset(const ref: treference): boolean;
  104. function isvalidreference(const ref: treference): boolean;
  105. procedure create_codegen;
  106. implementation
  107. uses
  108. globals,verbose,systems,cutils,
  109. symsym,symtable,defutil,paramgr,procinfo,
  110. rgobj,tgobj,rgcpu,fmodule;
  111. { Range check must be disabled explicitly as conversions between signed and unsigned
  112. 32-bit values are done without explicit typecasts }
  113. {$R-}
  114. const
  115. { opcode table lookup }
  116. topcg2tasmop: Array[topcg] of tasmop =
  117. (
  118. A_NONE,
  119. A_MOVE,
  120. A_ADD,
  121. A_AND,
  122. A_DIVU,
  123. A_DIVS,
  124. A_MULS,
  125. A_MULU,
  126. A_NEG,
  127. A_NOT,
  128. A_OR,
  129. A_ASR,
  130. A_LSL,
  131. A_LSR,
  132. A_SUB,
  133. A_EOR,
  134. A_ROL,
  135. A_ROR
  136. );
  137. { opcode with extend bits table lookup, used by 64bit cg }
  138. topcg2tasmopx: Array[topcg] of tasmop =
  139. (
  140. A_NONE,
  141. A_NONE,
  142. A_ADDX,
  143. A_NONE,
  144. A_NONE,
  145. A_NONE,
  146. A_NONE,
  147. A_NONE,
  148. A_NEGX,
  149. A_NONE,
  150. A_NONE,
  151. A_NONE,
  152. A_NONE,
  153. A_NONE,
  154. A_SUBX,
  155. A_NONE,
  156. A_NONE,
  157. A_NONE
  158. );
  159. TOpCmp2AsmCond: Array[topcmp] of TAsmCond =
  160. (
  161. C_NONE,
  162. C_EQ,
  163. C_GT,
  164. C_LT,
  165. C_GE,
  166. C_LE,
  167. C_NE,
  168. C_LS,
  169. C_CS,
  170. C_CC,
  171. C_HI
  172. );
  173. function isvalidreference(const ref: treference): boolean;
  174. begin
  175. isvalidreference:=isvalidrefoffset(ref) and
  176. { don't try to generate addressing with symbol and base reg and offset
  177. it might fail in linking stage if the symbol is more than 32k away (KB) }
  178. not (assigned(ref.symbol) and (ref.base <> NR_NO) and (ref.offset <> 0)) and
  179. { coldfire and 68000 cannot handle non-addressregs as bases }
  180. not ((current_settings.cputype in cpu_coldfire+[cpu_mc68000]) and
  181. not isaddressregister(ref.base));
  182. end;
  183. function isvalidrefoffset(const ref: treference): boolean;
  184. begin
  185. isvalidrefoffset := true;
  186. if ref.index <> NR_NO then
  187. begin
  188. // if ref.base <> NR_NO then
  189. // internalerror(2002081401);
  190. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  191. isvalidrefoffset := false
  192. end
  193. else
  194. begin
  195. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  196. isvalidrefoffset := false;
  197. end;
  198. end;
  199. {****************************************************************************}
  200. { TCG68K }
  201. {****************************************************************************}
  202. function use_push(const cgpara:tcgpara):boolean;
  203. begin
  204. result:=(not paramanager.use_fixed_stack) and
  205. assigned(cgpara.location) and
  206. (cgpara.location^.loc=LOC_REFERENCE) and
  207. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  208. end;
  209. procedure tcg68k.init_register_allocators;
  210. var
  211. reg: TSuperRegister;
  212. address_regs: array of TSuperRegister;
  213. begin
  214. inherited init_register_allocators;
  215. address_regs:=nil;
  216. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  217. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7],
  218. first_int_imreg,[]);
  219. { set up the array of address registers to use }
  220. for reg:=RS_A0 to RS_A6 do
  221. begin
  222. { don't hardwire the frame pointer register, because it can vary between target OS }
  223. if (assigned(current_procinfo) and (current_procinfo.framepointer = NR_FRAME_POINTER_REG)
  224. and (reg = RS_FRAME_POINTER_REG))
  225. or ((reg = RS_PIC_OFFSET_REG) and (tf_static_reg_based in target_info.flags)) then
  226. continue;
  227. setlength(address_regs,length(address_regs)+1);
  228. address_regs[length(address_regs)-1]:=reg;
  229. end;
  230. rg[R_ADDRESSREGISTER]:=trgcpu.create(R_ADDRESSREGISTER,R_SUBWHOLE,
  231. address_regs, first_addr_imreg, []);
  232. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  233. [RS_FP0,RS_FP1,RS_FP2,RS_FP3,RS_FP4,RS_FP5,RS_FP6,RS_FP7],
  234. first_fpu_imreg,[]);
  235. end;
  236. procedure tcg68k.done_register_allocators;
  237. begin
  238. rg[R_INTREGISTER].free;
  239. rg[R_FPUREGISTER].free;
  240. rg[R_ADDRESSREGISTER].free;
  241. inherited done_register_allocators;
  242. end;
  243. procedure tcg68k.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  244. var
  245. pushsize : tcgsize;
  246. ref : treference;
  247. begin
  248. { it's probably necessary to port this from x86 later, or provide an m68k solution (KB) }
  249. { TODO: FIX ME! check_register_size()}
  250. // check_register_size(size,r);
  251. if use_push(cgpara) then
  252. begin
  253. cgpara.check_simple_location;
  254. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  255. pushsize:=cgpara.location^.size
  256. else
  257. pushsize:=int_cgsize(cgpara.alignment);
  258. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, ctempposinvalid ,cgpara.alignment, []);
  259. ref.direction := dir_dec;
  260. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize),ref));
  261. end
  262. else
  263. inherited a_load_reg_cgpara(list,size,r,cgpara);
  264. end;
  265. procedure tcg68k.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  266. var
  267. pushsize : tcgsize;
  268. ref : treference;
  269. begin
  270. if use_push(cgpara) then
  271. begin
  272. cgpara.check_simple_location;
  273. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  274. pushsize:=cgpara.location^.size
  275. else
  276. pushsize:=int_cgsize(cgpara.alignment);
  277. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, ctempposinvalid, cgpara.alignment, []);
  278. ref.direction := dir_dec;
  279. a_load_const_ref(list, pushsize, a, ref);
  280. end
  281. else
  282. inherited a_load_const_cgpara(list,size,a,cgpara);
  283. end;
  284. procedure tcg68k.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  285. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  286. var
  287. pushsize : tcgsize;
  288. tmpreg : tregister;
  289. href : treference;
  290. ref : treference;
  291. begin
  292. if not assigned(paraloc) then
  293. exit;
  294. if (paraloc^.loc<>LOC_REFERENCE) or
  295. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  296. (tcgsize2size[paraloc^.size]>sizeof(tcgint)) then
  297. internalerror(200501162);
  298. { Pushes are needed in reverse order, add the size of the
  299. current location to the offset where to load from. This
  300. prevents wrong calculations for the last location when
  301. the size is not a power of 2 }
  302. if assigned(paraloc^.next) then
  303. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  304. { Push the data starting at ofs }
  305. href:=r;
  306. inc(href.offset,ofs);
  307. fixref(list,href,false);
  308. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  309. pushsize:=paraloc^.size
  310. else
  311. pushsize:=int_cgsize(cgpara.alignment);
  312. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, ctempposinvalid, tcgsize2size[pushsize], []);
  313. ref.direction := dir_dec;
  314. a_load_ref_ref(list,int_cgsize(tcgsize2size[paraloc^.size]),pushsize,href,ref);
  315. end;
  316. var
  317. len : tcgint;
  318. ofs : tcgint;
  319. href : treference;
  320. begin
  321. { cgpara.size=OS_NO requires a copy on the stack }
  322. if use_push(cgpara) then
  323. begin
  324. { Record copy? }
  325. if (cgpara.size in [OS_NO,OS_F64]) or (size in [OS_NO,OS_F64]) then
  326. begin
  327. //list.concat(tai_comment.create(strpnew('a_load_ref_cgpara: g_concatcopy')));
  328. cgpara.check_simple_location;
  329. len:=align(cgpara.intsize,cgpara.alignment);
  330. g_stackpointer_alloc(list,len);
  331. ofs:=0;
  332. if (cgpara.intsize<cgpara.alignment) then
  333. ofs:=cgpara.alignment-cgpara.intsize;
  334. reference_reset_base(href,NR_STACK_POINTER_REG,ofs,ctempposinvalid,cgpara.alignment,[]);
  335. g_concatcopy(list,r,href,cgpara.intsize);
  336. end
  337. else
  338. begin
  339. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  340. internalerror(200501161);
  341. { We need to push the data in reverse order,
  342. therefore we use a recursive algorithm }
  343. pushdata(cgpara.location,0);
  344. end
  345. end
  346. else
  347. inherited a_load_ref_cgpara(list,size,r,cgpara);
  348. end;
  349. procedure tcg68k.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  350. var
  351. tmpref : treference;
  352. begin
  353. { 68k always passes arguments on the stack }
  354. if use_push(cgpara) then
  355. begin
  356. //list.concat(tai_comment.create(strpnew('a_loadaddr_ref_cgpara: PEA')));
  357. cgpara.check_simple_location;
  358. tmpref:=r;
  359. fixref(list,tmpref,false);
  360. list.concat(taicpu.op_ref(A_PEA,S_NO,tmpref));
  361. end
  362. else
  363. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  364. end;
  365. function tcg68k.fixref(list: TAsmList; var ref: treference; fullyresolve: boolean): boolean;
  366. var
  367. hreg : tregister;
  368. href : treference;
  369. instr : taicpu;
  370. begin
  371. result:=false;
  372. hreg:=NR_NO;
  373. { NOTE: we don't have to fixup scaling in this function, because the memnode
  374. won't generate scaling on CPUs which don't support it }
  375. if (tf_static_reg_based in target_info.flags) and assigned(ref.symbol) and (ref.base=NR_NO) then
  376. fullyresolve:=true;
  377. { first, deal with the symbol, if we have an index or base register.
  378. in theory, the '020+ could deal with these, but it's better to avoid
  379. long displacements on most members of the 68k family anyway }
  380. if assigned(ref.symbol) and ((ref.base<>NR_NO) or (ref.index<>NR_NO)) then
  381. begin
  382. //list.concat(tai_comment.create(strpnew('fixref: symbol with base or index')));
  383. hreg:=getaddressregister(list);
  384. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment,ref.volatility);
  385. if (tf_static_reg_based in target_info.flags) and (ref.base=NR_NO) then
  386. begin
  387. if ref.symbol.typ in [AT_DATA,AT_DATA_FORCEINDIRECT,AT_DATA_NOINDIRECT] then
  388. href.base:=NR_PIC_OFFSET_REG
  389. else
  390. href.base:=NR_PC;
  391. end;
  392. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,href,hreg));
  393. ref.offset:=0;
  394. ref.symbol:=nil;
  395. { if we have unused base or index, try to use it, otherwise fold the existing base,
  396. also handle the case where the base might be a data register. }
  397. if ref.base=NR_NO then
  398. ref.base:=hreg
  399. else
  400. if (ref.index=NR_NO) and not isintregister(ref.base) then
  401. ref.index:=hreg
  402. else
  403. begin
  404. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.base,hreg));
  405. ref.base:=hreg;
  406. end;
  407. { at this point we have base + (optional) index * scale }
  408. end;
  409. { deal with the case if our base is a dataregister }
  410. if (ref.base<>NR_NO) and not isaddressregister(ref.base) then
  411. begin
  412. hreg:=getaddressregister(list);
  413. if isaddressregister(ref.index) and (ref.scalefactor < 2) then
  414. begin
  415. //list.concat(tai_comment.create(strpnew('fixref: base is dX, resolving with reverse regs')));
  416. reference_reset_base(href,ref.index,0,ref.temppos,ref.alignment,ref.volatility);
  417. href.index:=ref.base;
  418. { we can fold in an 8 bit offset "for free" }
  419. if isvalue8bit(ref.offset) then
  420. begin
  421. href.offset:=ref.offset;
  422. ref.offset:=0;
  423. end;
  424. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,href,hreg));
  425. ref.base:=hreg;
  426. ref.index:=NR_NO;
  427. result:=true;
  428. end
  429. else
  430. begin
  431. //list.concat(tai_comment.create(strpnew('fixref: base is dX, can''t resolve with reverse regs')));
  432. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  433. add_move_instruction(instr);
  434. list.concat(instr);
  435. ref.base:=hreg;
  436. result:=true;
  437. end;
  438. end;
  439. { deal with large offsets on non-020+ }
  440. if not (CPUM68K_HAS_BASEDISP in cpu_capabilities[current_settings.cputype]) then
  441. begin
  442. if ((ref.index<>NR_NO) and not isvalue8bit(ref.offset)) or
  443. ((ref.base<>NR_NO) and not isvalue16bit(ref.offset)) then
  444. begin
  445. //list.concat(tai_comment.create(strpnew('fixref: handling large offsets')));
  446. { if we have a temp register from above, we can just add to it }
  447. if hreg=NR_NO then
  448. hreg:=getaddressregister(list);
  449. if isvalue16bit(ref.offset) then
  450. begin
  451. reference_reset_base(href,ref.base,ref.offset,ref.temppos,ref.alignment,ref.volatility);
  452. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,href,hreg));
  453. end
  454. else
  455. begin
  456. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  457. add_move_instruction(instr);
  458. list.concat(instr);
  459. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  460. end;
  461. ref.offset:=0;
  462. ref.base:=hreg;
  463. result:=true;
  464. end;
  465. end;
  466. { fully resolve the reference to an address register, if we're told to do so
  467. and there's a reason to do so }
  468. if fullyresolve and
  469. ((ref.index<>NR_NO) or assigned(ref.symbol) or (ref.offset<>0)) then
  470. begin
  471. //list.concat(tai_comment.create(strpnew('fixref: fully resolve to register')));
  472. if hreg=NR_NO then
  473. hreg:=getaddressregister(list);
  474. if (tf_static_reg_based in target_info.flags) and (ref.base=NR_NO) then
  475. begin
  476. if ref.symbol.typ in [AT_DATA,AT_DATA_FORCEINDIRECT,AT_DATA_NOINDIRECT] then
  477. ref.base:=NR_PIC_OFFSET_REG
  478. else
  479. ref.base:=NR_PC;
  480. end;
  481. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref,hreg));
  482. ref.base:=hreg;
  483. ref.index:=NR_NO;
  484. ref.scalefactor:=1;
  485. ref.symbol:=nil;
  486. ref.offset:=0;
  487. result:=true;
  488. end;
  489. end;
  490. procedure tcg68k.call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  491. var
  492. paraloc1,paraloc2: tcgpara;
  493. pd : tprocdef;
  494. begin
  495. pd:=search_system_proc(name);
  496. paraloc1.init;
  497. paraloc2.init;
  498. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  499. paramanager.getcgtempparaloc(list,pd,2,paraloc2);
  500. a_load_const_cgpara(list,size,a,paraloc2);
  501. a_load_reg_cgpara(list,OS_32,reg,paraloc1);
  502. paramanager.freecgpara(list,paraloc2);
  503. paramanager.freecgpara(list,paraloc1);
  504. g_call(list,name);
  505. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  506. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg);
  507. paraloc2.done;
  508. paraloc1.done;
  509. end;
  510. procedure tcg68k.call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  511. var
  512. paraloc1,paraloc2: tcgpara;
  513. pd : tprocdef;
  514. begin
  515. pd:=search_system_proc(name);
  516. paraloc1.init;
  517. paraloc2.init;
  518. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  519. paramanager.getcgtempparaloc(list,pd,2,paraloc2);
  520. a_load_reg_cgpara(list,OS_32,reg1,paraloc2);
  521. a_load_reg_cgpara(list,OS_32,reg2,paraloc1);
  522. paramanager.freecgpara(list,paraloc2);
  523. paramanager.freecgpara(list,paraloc1);
  524. g_call(list,name);
  525. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  526. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg2);
  527. paraloc2.done;
  528. paraloc1.done;
  529. end;
  530. procedure tcg68k.a_call_name(list : TAsmList;const s : string; weak: boolean);
  531. var
  532. sym: tasmsymbol;
  533. const
  534. jmp_inst: array[boolean] of tasmop = ( A_JSR, A_BSR );
  535. begin
  536. if not(weak) then
  537. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION)
  538. else
  539. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION);
  540. list.concat(taicpu.op_sym(jmp_inst[tf_code_small in target_info.flags],S_NO,sym));
  541. end;
  542. procedure tcg68k.a_call_reg(list : TAsmList;reg: tregister);
  543. var
  544. tmpref : treference;
  545. tmpreg : tregister;
  546. instr : taicpu;
  547. begin
  548. if isaddressregister(reg) then
  549. begin
  550. { if we have an address register, we can jump to the address directly }
  551. reference_reset_base(tmpref,reg,0,ctempposinvalid,4,[]);
  552. end
  553. else
  554. begin
  555. { if we have a data register, we need to move it to an address register first }
  556. tmpreg:=getaddressregister(list);
  557. reference_reset_base(tmpref,tmpreg,0,ctempposinvalid,4,[]);
  558. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,tmpreg);
  559. add_move_instruction(instr);
  560. list.concat(instr);
  561. end;
  562. list.concat(taicpu.op_ref(A_JSR,S_NO,tmpref));
  563. end;
  564. procedure tcg68k.a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);
  565. var
  566. opsize: topsize;
  567. begin
  568. opsize:=tcgsize2opsize[size];
  569. if isaddressregister(register) then
  570. begin
  571. { an m68k manual I have recommends SUB Ax,Ax to be used instead of CLR for address regs }
  572. { Premature optimization is the root of all evil - this code breaks spilling if the
  573. register contains a spilled regvar, eg. a Pointer which is set to nil, then random
  574. havoc happens... This is kept here for reference now, to allow fixing of the spilling
  575. later. Most of the optimizations below here could be moved to the optimizer. (KB) }
  576. {if a = 0 then
  577. list.concat(taicpu.op_reg_reg(A_SUB,S_L,register,register))
  578. else}
  579. { ISA B/C Coldfire has MOV3Q which can move -1 or 1..7 to any reg }
  580. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  581. ((longint(a) = -1) or ((longint(a) > 0) and (longint(a) < 8))) then
  582. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  583. else
  584. { MOVEA.W will sign extend the value in the dest. reg to full 32 bits
  585. (specific to Ax regs only) }
  586. if isvalue16bit(a) then
  587. list.concat(taicpu.op_const_reg(A_MOVEA,S_W,longint(a),register))
  588. else
  589. list.concat(taicpu.op_const_reg(A_MOVEA,S_L,longint(a),register));
  590. end
  591. else
  592. if a = 0 then
  593. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  594. else
  595. begin
  596. { Prefer MOV3Q if applicable, it allows replacement spilling for register }
  597. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  598. ((longint(a)=-1) or ((longint(a)>0) and (longint(a)<8))) then
  599. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  600. else if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  601. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  602. else
  603. begin
  604. { ISA B/C Coldfire has sign extend/zero extend moves }
  605. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  606. (size in [OS_16, OS_8, OS_S16, OS_S8]) and
  607. ((longint(a) >= low(smallint)) and (longint(a) <= high(smallint))) then
  608. begin
  609. if size in [OS_16, OS_8] then
  610. list.concat(taicpu.op_const_reg(A_MVZ,opsize,longint(a),register))
  611. else
  612. list.concat(taicpu.op_const_reg(A_MVS,opsize,longint(a),register));
  613. end
  614. else
  615. begin
  616. { clear the register first, for unsigned and positive values, so
  617. we don't need to zero extend after }
  618. if (size in [OS_16,OS_8]) or
  619. ((size in [OS_S16,OS_S8]) and (a > 0)) then
  620. list.concat(taicpu.op_reg(A_CLR,S_L,register));
  621. list.concat(taicpu.op_const_reg(A_MOVE,opsize,longint(a),register));
  622. { only sign extend if we need to, zero extension is not necessary because the CLR.L above }
  623. if (size in [OS_S16,OS_S8]) and (a < 0) then
  624. sign_extend(list,size,register);
  625. end;
  626. end;
  627. end;
  628. end;
  629. procedure tcg68k.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  630. var
  631. hreg : tregister;
  632. href : treference;
  633. begin
  634. if needs_unaligned(ref.alignment,tosize) then
  635. begin
  636. inherited;
  637. exit;
  638. end;
  639. a:=longint(a);
  640. href:=ref;
  641. fixref(list,href,false);
  642. if (a=0) and not (current_settings.cputype = cpu_mc68000) then
  643. list.concat(taicpu.op_ref(A_CLR,tcgsize2opsize[tosize],href))
  644. else if (tcgsize2opsize[tosize]=S_L) and
  645. (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  646. ((a=-1) or ((a>0) and (a<8))) then
  647. list.concat(taicpu.op_const_ref(A_MOV3Q,S_L,a,href))
  648. { for coldfire we need to go through a temporary register if we have a
  649. offset, index or symbol given }
  650. else if (current_settings.cputype in cpu_coldfire) and
  651. (
  652. (href.offset<>0) or
  653. { TODO : check whether we really need this second condition }
  654. (href.index<>NR_NO) or
  655. assigned(href.symbol)
  656. ) then
  657. begin
  658. hreg:=getintregister(list,tosize);
  659. a_load_const_reg(list,tosize,a,hreg);
  660. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  661. end
  662. else
  663. { loading via a register is almost always faster if the value is small.
  664. (with the 68040 being the only notable exception, so maybe disable
  665. this on a '040? but the difference is minor) it also results in shorter
  666. code. (KB) }
  667. if isvalue8bit(a) and (tcgsize2opsize[tosize] = S_L) then
  668. begin
  669. hreg:=getintregister(list,OS_INT);
  670. a_load_const_reg(list,OS_INT,a,hreg); // this will use moveq et.al.
  671. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  672. end
  673. else
  674. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[tosize],longint(a),href));
  675. end;
  676. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  677. var
  678. href : treference;
  679. hreg : tregister;
  680. begin
  681. if needs_unaligned(ref.alignment,tosize) then
  682. begin
  683. //list.concat(tai_comment.create(strpnew('a_load_reg_ref calling unaligned')));
  684. a_load_reg_ref_unaligned(list,fromsize,tosize,register,ref);
  685. exit;
  686. end;
  687. href := ref;
  688. hreg := register;
  689. fixref(list,href,false);
  690. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  691. begin
  692. hreg:=getintregister(list,tosize);
  693. a_load_reg_reg(list,fromsize,tosize,register,hreg);
  694. end;
  695. { move to destination reference }
  696. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,href));
  697. end;
  698. procedure tcg68k.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  699. var
  700. tmpref : treference;
  701. tmpreg,
  702. tmpreg2 : tregister;
  703. begin
  704. if not needs_unaligned(ref.alignment,tosize) then
  705. begin
  706. a_load_reg_ref(list,fromsize,tosize,register,ref);
  707. exit;
  708. end;
  709. list.concat(tai_comment.create(strpnew('a_load_reg_ref_unaligned: generating unaligned store')));
  710. tmpreg2:=getaddressregister(list);
  711. tmpref:=ref;
  712. inc(tmpref.offset,tcgsize2size[tosize]-1);
  713. a_loadaddr_ref_reg(list,tmpref,tmpreg2);
  714. reference_reset_base(tmpref,tmpreg2,0,ctempposinvalid,1,ref.volatility);
  715. tmpref.direction:=dir_none;
  716. tmpreg:=getintregister(list,tosize);
  717. a_load_reg_reg(list,fromsize,tosize,register,tmpreg);
  718. case tosize of
  719. OS_16,OS_S16:
  720. begin
  721. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  722. list.concat(taicpu.op_const_reg(A_LSR,S_W,8,tmpreg));
  723. tmpref.direction:=dir_dec;
  724. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  725. end;
  726. OS_32,OS_S32:
  727. begin
  728. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  729. list.concat(taicpu.op_const_reg(A_LSR,S_W,8,tmpreg));
  730. tmpref.direction:=dir_dec;
  731. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  732. list.concat(taicpu.op_reg(A_SWAP,S_L,tmpreg));
  733. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  734. list.concat(taicpu.op_const_reg(A_LSR,S_W,8,tmpreg));
  735. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  736. end
  737. else
  738. internalerror(2016052201);
  739. end;
  740. end;
  741. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  742. var
  743. aref: treference;
  744. bref: treference;
  745. usetemp: boolean;
  746. hreg: TRegister;
  747. begin
  748. usetemp:=TCGSize2OpSize[fromsize]<>TCGSize2OpSize[tosize];
  749. usetemp:=usetemp or (needs_unaligned(sref.alignment,fromsize) or needs_unaligned(dref.alignment,tosize));
  750. aref := sref;
  751. bref := dref;
  752. if usetemp then
  753. begin
  754. { if we need to change the size then always use a temporary register }
  755. hreg:=getintregister(list,fromsize);
  756. if needs_unaligned(sref.alignment,fromsize) then
  757. a_load_ref_reg_unaligned(list,fromsize,tosize,sref,hreg)
  758. else
  759. begin
  760. fixref(list,aref,false);
  761. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],aref,hreg));
  762. sign_extend(list,fromsize,tosize,hreg);
  763. end;
  764. if needs_unaligned(dref.alignment,tosize) then
  765. a_load_reg_ref_unaligned(list,tosize,tosize,hreg,dref)
  766. else
  767. begin
  768. { if we use a temp register, we don't need to fully resolve
  769. the dest ref, not even on coldfire }
  770. fixref(list,bref,false);
  771. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,bref));
  772. end;
  773. end
  774. else
  775. begin
  776. fixref(list,aref,false);
  777. fixref(list,bref,current_settings.cputype in cpu_coldfire);
  778. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  779. end;
  780. end;
  781. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  782. var
  783. instr : taicpu;
  784. hreg : tregister;
  785. opsize : topsize;
  786. begin
  787. { move to destination register }
  788. opsize:=TCGSize2OpSize[fromsize];
  789. if isaddressregister(reg2) and not (opsize in [S_L]) then
  790. begin
  791. hreg:=cg.getintregister(list,OS_ADDR);
  792. instr:=taicpu.op_reg_reg(A_MOVE,TCGSize2OpSize[fromsize],reg1,hreg);
  793. add_move_instruction(instr);
  794. list.concat(instr);
  795. sign_extend(list,fromsize,hreg);
  796. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg2));
  797. end
  798. else
  799. begin
  800. if not isregoverlap(reg1,reg2) then
  801. begin
  802. instr:=taicpu.op_reg_reg(A_MOVE,opsize,reg1,reg2);
  803. add_move_instruction(instr);
  804. list.concat(instr);
  805. end;
  806. sign_extend(list,fromsize,tosize,reg2);
  807. end;
  808. end;
  809. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  810. var
  811. href : treference;
  812. hreg : tregister;
  813. size : tcgsize;
  814. opsize: topsize;
  815. needsext: boolean;
  816. begin
  817. needsext:=tcgsize2size[fromsize]<tcgsize2size[tosize];
  818. if needsext then
  819. size:=fromsize
  820. else
  821. size:=tosize;
  822. if needs_unaligned(ref.alignment,size) then
  823. begin
  824. //list.concat(tai_comment.create(strpnew('a_load_ref_reg calling unaligned')));
  825. a_load_ref_reg_unaligned(list,fromsize,tosize,ref,register);
  826. exit;
  827. end;
  828. href:=ref;
  829. fixref(list,href,false);
  830. opsize:=TCGSize2OpSize[size];
  831. if isaddressregister(register) and not (opsize in [S_L]) then
  832. hreg:=getintregister(list,OS_ADDR)
  833. else
  834. hreg:=register;
  835. if needsext and (CPUM68K_HAS_MVSMVZ in cpu_capabilities[current_settings.cputype]) and not (opsize in [S_L]) then
  836. begin
  837. if fromsize in [OS_S8,OS_S16] then
  838. list.concat(taicpu.op_ref_reg(A_MVS,opsize,href,hreg))
  839. else if fromsize in [OS_8,OS_16] then
  840. list.concat(taicpu.op_ref_reg(A_MVZ,opsize,href,hreg))
  841. else
  842. internalerror(2016050502);
  843. end
  844. else
  845. begin
  846. if needsext and (fromsize in [OS_8,OS_16]) then
  847. begin
  848. //list.concat(tai_comment.create(strpnew('a_load_ref_reg: zero ext')));
  849. a_load_const_reg(list,OS_32,0,hreg);
  850. needsext:=false;
  851. end;
  852. list.concat(taicpu.op_ref_reg(A_MOVE,opsize,href,hreg));
  853. if needsext then
  854. sign_extend(list,size,hreg);
  855. end;
  856. if hreg<>register then
  857. a_load_reg_reg(list,OS_ADDR,OS_ADDR,hreg,register);
  858. end;
  859. procedure tcg68k.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  860. var
  861. tmpref : treference;
  862. tmpreg,
  863. tmpreg2 : tregister;
  864. begin
  865. if not needs_unaligned(ref.alignment,fromsize) then
  866. begin
  867. a_load_ref_reg(list,fromsize,tosize,ref,register);
  868. exit;
  869. end;
  870. list.concat(tai_comment.create(strpnew('a_load_ref_reg_unaligned: generating unaligned load')));
  871. tmpreg2:=getaddressregister(list);
  872. a_loadaddr_ref_reg(list,ref,tmpreg2);
  873. reference_reset_base(tmpref,tmpreg2,0,ctempposinvalid,1,ref.volatility);
  874. tmpref.direction:=dir_inc;
  875. if isaddressregister(register) then
  876. tmpreg:=getintregister(list,OS_ADDR)
  877. else
  878. tmpreg:=register;
  879. case fromsize of
  880. OS_16,OS_S16:
  881. begin
  882. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  883. list.concat(taicpu.op_const_reg(A_LSL,S_W,8,tmpreg));
  884. tmpref.direction:=dir_none;
  885. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  886. sign_extend(list,fromsize,tmpreg);
  887. end;
  888. OS_32,OS_S32:
  889. begin
  890. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  891. list.concat(taicpu.op_const_reg(A_LSL,S_W,8,tmpreg));
  892. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  893. list.concat(taicpu.op_reg(A_SWAP,S_L,tmpreg));
  894. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  895. list.concat(taicpu.op_const_reg(A_LSL,S_W,8,tmpreg));
  896. tmpref.direction:=dir_none;
  897. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  898. end
  899. else
  900. internalerror(2016052103);
  901. end;
  902. if tmpreg<>register then
  903. a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpreg,register);
  904. end;
  905. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  906. var
  907. href : treference;
  908. hreg : tregister;
  909. begin
  910. href:=ref;
  911. fixref(list, href, false);
  912. if not isaddressregister(r) then
  913. begin
  914. hreg:=getaddressregister(list);
  915. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,href,hreg));
  916. a_load_reg_reg(list, OS_ADDR, OS_ADDR, hreg, r);
  917. end
  918. else
  919. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,href,r));
  920. end;
  921. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  922. var
  923. instr : taicpu;
  924. op: tasmop;
  925. href: treference;
  926. hreg: tregister;
  927. begin
  928. if fromsize > tosize then
  929. begin
  930. { we have to do a load-store through an intregister or the stack in this case,
  931. which is probably the fastest way, and simpler than messing around with FPU control
  932. words for one-off custom rounding (KB) }
  933. case tosize of
  934. OS_F32:
  935. begin
  936. //list.concat(tai_comment.create(strpnew('a_loadfpu_reg_reg rounding via intreg')));
  937. hreg := getintregister(list,OS_32);
  938. list.concat(taicpu.op_reg_reg(A_FMOVE, tcgsize2opsize[tosize], reg1, hreg));
  939. list.concat(taicpu.op_reg_reg(A_FMOVE, tcgsize2opsize[tosize], hreg, reg2));
  940. end;
  941. OS_F64:
  942. begin
  943. //list.concat(tai_comment.create(strpnew('a_loadfpu_reg_reg rounding via stack')));
  944. reference_reset_base(href, NR_STACK_POINTER_REG, 0, ctempposinvalid, 0, []);
  945. href.direction:=dir_dec;
  946. list.concat(taicpu.op_reg_ref(A_FMOVE, tcgsize2opsize[tosize], reg1, href));
  947. href.direction:=dir_inc;
  948. list.concat(taicpu.op_ref_reg(A_FMOVE, tcgsize2opsize[tosize], href, reg2));
  949. end;
  950. else
  951. internalerror(2021020802);
  952. end;
  953. end
  954. else
  955. begin
  956. instr:=taicpu.op_reg_reg(A_FMOVE,fpuregopsize,reg1,reg2);
  957. add_move_instruction(instr);
  958. list.concat(instr);
  959. end;
  960. end;
  961. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  962. var
  963. opsize : topsize;
  964. href : treference;
  965. begin
  966. opsize := tcgsize2opsize[fromsize];
  967. href := ref;
  968. fixref(list,href,current_settings.fputype = fpu_coldfire);
  969. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  970. if fromsize > tosize then
  971. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  972. end;
  973. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  974. var
  975. opsize : topsize;
  976. href : treference;
  977. begin
  978. opsize := tcgsize2opsize[tosize];
  979. href := ref;
  980. fixref(list,href,current_settings.fputype = fpu_coldfire);
  981. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg,href));
  982. end;
  983. procedure tcg68k.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const reg : tregister;const cgpara : tcgpara);
  984. var
  985. ref : treference;
  986. begin
  987. if use_push(cgpara) and (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) then
  988. begin
  989. cgpara.check_simple_location;
  990. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, ctempposinvalid, cgpara.alignment, []);
  991. ref.direction := dir_dec;
  992. list.concat(taicpu.op_reg_ref(A_FMOVE,tcgsize2opsize[cgpara.location^.size],reg,ref));
  993. end
  994. else
  995. inherited a_loadfpu_reg_cgpara(list,size,reg,cgpara);
  996. end;
  997. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  998. var
  999. href, href2 : treference;
  1000. freg : tregister;
  1001. begin
  1002. if current_settings.fputype = fpu_soft then
  1003. case cgpara.location^.loc of
  1004. LOC_REFERENCE,LOC_CREFERENCE:
  1005. begin
  1006. case size of
  1007. OS_F64:
  1008. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  1009. OS_F32:
  1010. a_load_ref_cgpara(list,size,ref,cgpara);
  1011. else
  1012. internalerror(2013021201);
  1013. end;
  1014. end;
  1015. else
  1016. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1017. end
  1018. else
  1019. if use_push(cgpara) and (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) then
  1020. begin
  1021. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara copy')));
  1022. cgpara.check_simple_location;
  1023. reference_reset_base(href, NR_STACK_POINTER_REG, 0, ctempposinvalid, cgpara.alignment, []);
  1024. href.direction := dir_dec;
  1025. case size of
  1026. OS_F64:
  1027. begin
  1028. href2:=ref;
  1029. inc(href2.offset,8);
  1030. fixref(list,href2,true);
  1031. href2.direction := dir_dec;
  1032. cg.a_load_ref_ref(list,OS_32,OS_32,href2,href);
  1033. cg.a_load_ref_ref(list,OS_32,OS_32,href2,href);
  1034. end;
  1035. OS_F32:
  1036. cg.a_load_ref_ref(list,OS_32,OS_32,ref,href);
  1037. else
  1038. internalerror(2017052110);
  1039. end;
  1040. end
  1041. else
  1042. begin
  1043. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara inherited')));
  1044. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1045. end;
  1046. end;
  1047. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  1048. var
  1049. scratch_reg : tregister;
  1050. scratch_reg2: tregister;
  1051. opcode : tasmop;
  1052. begin
  1053. optimize_op_const(size, op, a);
  1054. opcode := topcg2tasmop[op];
  1055. if (a >0) and (a<=high(dword)) then
  1056. a:=longint(dword(a))
  1057. else if (a>=low(longint)) then
  1058. a:=longint(a)
  1059. else
  1060. internalerror(201810201);
  1061. case op of
  1062. OP_NONE :
  1063. begin
  1064. { Opcode is optimized away }
  1065. end;
  1066. OP_MOVE :
  1067. begin
  1068. { Optimized, replaced with a simple load }
  1069. a_load_const_reg(list,size,a,reg);
  1070. end;
  1071. OP_ADD,
  1072. OP_SUB:
  1073. begin
  1074. { add/sub works the same way, so have it unified here }
  1075. if (a >= 1) and (a <= 8) then
  1076. if (op = OP_ADD) then
  1077. opcode:=A_ADDQ
  1078. else
  1079. opcode:=A_SUBQ;
  1080. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  1081. end;
  1082. OP_AND,
  1083. OP_OR,
  1084. OP_XOR:
  1085. begin
  1086. scratch_reg := force_to_dataregister(list, size, reg);
  1087. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1088. move_if_needed(list, size, scratch_reg, reg);
  1089. end;
  1090. OP_DIV,
  1091. OP_IDIV:
  1092. begin
  1093. internalerror(2002081601);
  1094. end;
  1095. OP_MUL,
  1096. OP_IMUL:
  1097. begin
  1098. { NOTE: better have this as fast as possible on every CPU in all cases,
  1099. because the compiler uses OP_IMUL for array indexing... (KB) }
  1100. { ColdFire doesn't support MULS/MULU <imm>,dX }
  1101. if not (CPUM68K_HAS_MULIMM in cpu_capabilities[current_settings.cputype]) then
  1102. begin
  1103. { move const to a register first }
  1104. scratch_reg := getintregister(list,OS_INT);
  1105. a_load_const_reg(list, size, a, scratch_reg);
  1106. { do the multiplication }
  1107. scratch_reg2 := force_to_dataregister(list, size, reg);
  1108. sign_extend(list, size, scratch_reg2);
  1109. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  1110. { move the value back to the original register }
  1111. move_if_needed(list, size, scratch_reg2, reg);
  1112. end
  1113. else
  1114. begin
  1115. if CPUM68K_HAS_32BITMUL in cpu_capabilities[current_settings.cputype] then
  1116. begin
  1117. { do the multiplication }
  1118. scratch_reg := force_to_dataregister(list, size, reg);
  1119. sign_extend(list, size, scratch_reg);
  1120. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  1121. { move the value back to the original register }
  1122. move_if_needed(list, size, scratch_reg, reg);
  1123. end
  1124. else
  1125. { Fallback branch, plain 68000 for now }
  1126. if not optimize_const_mul_to_shift_sub_add(list, 5, a, size, reg) then
  1127. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1128. if op = OP_MUL then
  1129. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1130. else
  1131. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1132. end;
  1133. end;
  1134. OP_ROL,
  1135. OP_ROR,
  1136. OP_SAR,
  1137. OP_SHL,
  1138. OP_SHR :
  1139. begin
  1140. scratch_reg := force_to_dataregister(list, size, reg);
  1141. sign_extend(list, size, scratch_reg);
  1142. { some special cases which can generate smarter code
  1143. using the SWAP instruction }
  1144. if (a = 16) then
  1145. begin
  1146. if (op = OP_SHL) then
  1147. begin
  1148. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1149. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1150. end
  1151. else if (op = OP_SHR) then
  1152. begin
  1153. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1154. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1155. end
  1156. else if (op = OP_SAR) then
  1157. begin
  1158. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1159. list.concat(taicpu.op_reg(A_EXT,S_L,scratch_reg));
  1160. end
  1161. else if (op = OP_ROR) or (op = OP_ROL) then
  1162. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg))
  1163. end
  1164. else if (a >= 1) and (a <= 8) then
  1165. begin
  1166. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1167. end
  1168. else if (a >= 9) and (a < 16) then
  1169. begin
  1170. { Use two ops instead of const -> reg + shift with reg, because
  1171. this way is the same in length and speed but has less register
  1172. pressure }
  1173. list.concat(taicpu.op_const_reg(opcode, S_L, 8, scratch_reg));
  1174. list.concat(taicpu.op_const_reg(opcode, S_L, a-8, scratch_reg));
  1175. end
  1176. else
  1177. begin
  1178. { move const to a register first }
  1179. scratch_reg2 := getintregister(list,OS_INT);
  1180. a_load_const_reg(list, size, a, scratch_reg2);
  1181. { do the operation }
  1182. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1183. end;
  1184. { move the value back to the original register }
  1185. move_if_needed(list, size, scratch_reg, reg);
  1186. end;
  1187. else
  1188. internalerror(2002072901);
  1189. end;
  1190. end;
  1191. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1192. var
  1193. opcode: tasmop;
  1194. opsize: topsize;
  1195. href : treference;
  1196. hreg : tregister;
  1197. begin
  1198. optimize_op_const(size, op, a);
  1199. opcode := topcg2tasmop[op];
  1200. opsize := TCGSize2OpSize[size];
  1201. { on ColdFire all arithmetic operations are only possible on 32bit }
  1202. if needs_unaligned(ref.alignment,size) or
  1203. (not (CPUM68K_HAS_BYTEWORDMATH in cpu_capabilities[current_settings.cputype]) and (opsize <> S_L)
  1204. and not (op in [OP_NONE,OP_MOVE])) then
  1205. begin
  1206. inherited;
  1207. exit;
  1208. end;
  1209. case op of
  1210. OP_NONE :
  1211. begin
  1212. { opcode was optimized away }
  1213. end;
  1214. OP_MOVE :
  1215. begin
  1216. { Optimized, replaced with a simple load }
  1217. a_load_const_ref(list,size,a,ref);
  1218. end;
  1219. OP_AND,
  1220. OP_OR,
  1221. OP_XOR :
  1222. begin
  1223. //list.concat(tai_comment.create(strpnew('a_op_const_ref: bitwise')));
  1224. hreg:=getintregister(list,size);
  1225. a_load_const_reg(list,size,a,hreg);
  1226. href:=ref;
  1227. fixref(list,href,false);
  1228. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1229. end;
  1230. OP_ADD,
  1231. OP_SUB :
  1232. begin
  1233. href:=ref;
  1234. { add/sub works the same way, so have it unified here }
  1235. if (a >= 1) and (a <= 8) then
  1236. begin
  1237. fixref(list,href,false);
  1238. if (op = OP_ADD) then
  1239. opcode:=A_ADDQ
  1240. else
  1241. opcode:=A_SUBQ;
  1242. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1243. end
  1244. else
  1245. if not(current_settings.cputype in cpu_coldfire) then
  1246. begin
  1247. fixref(list,href,false);
  1248. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1249. end
  1250. else
  1251. { on ColdFire, ADDI/SUBI cannot act on memory
  1252. so we can only go through a register }
  1253. inherited;
  1254. end;
  1255. else begin
  1256. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1257. inherited;
  1258. end;
  1259. end;
  1260. end;
  1261. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1262. var
  1263. hreg1, hreg2: tregister;
  1264. opcode : tasmop;
  1265. opsize : topsize;
  1266. begin
  1267. opcode := topcg2tasmop[op];
  1268. if not (CPUM68K_HAS_BYTEWORDMATH in cpu_capabilities[current_settings.cputype]) then
  1269. opsize := S_L
  1270. else
  1271. opsize := TCGSize2OpSize[size];
  1272. case op of
  1273. OP_ADD,
  1274. OP_SUB:
  1275. begin
  1276. if current_settings.cputype in cpu_coldfire then
  1277. begin
  1278. { operation only allowed only a longword }
  1279. sign_extend(list, size, src);
  1280. sign_extend(list, size, dst);
  1281. end;
  1282. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1283. end;
  1284. OP_SAR,
  1285. OP_SHL,
  1286. OP_SHR,
  1287. OP_ROR,
  1288. OP_ROL:
  1289. begin
  1290. { the compiler has code on the higher levels to try to prevent generating
  1291. ROR/ROL instructions on m68k CPUs that don't support it (ColdFire family) }
  1292. if (op in [OP_ROR,OP_ROL]) and
  1293. not (CPUM68K_HAS_ROLROR in cpu_capabilities[current_settings.cputype]) then
  1294. internalerror(2025091101);
  1295. { load to data registers }
  1296. hreg1 := force_to_dataregister(list, size, src);
  1297. hreg2 := force_to_dataregister(list, size, dst);
  1298. if not (CPUM68K_HAS_BYTEWORDMATH in cpu_capabilities[current_settings.cputype]) then
  1299. { source for these ops are always modulo 64 on m68k,
  1300. so we don't need to extend the src register }
  1301. sign_extend(list, size, hreg2);
  1302. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1303. { move back result into destination register }
  1304. move_if_needed(list, size, hreg2, dst);
  1305. end;
  1306. OP_AND,
  1307. OP_OR,
  1308. OP_XOR:
  1309. begin
  1310. { load to data registers }
  1311. hreg1 := force_to_dataregister(list, size, src);
  1312. hreg2 := force_to_dataregister(list, size, dst);
  1313. if current_settings.cputype in cpu_coldfire then
  1314. begin
  1315. sign_extend(list, size, hreg1);
  1316. sign_extend(list, size, hreg2);
  1317. end;
  1318. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1319. { move back result into destination register }
  1320. move_if_needed(list, size, hreg2, dst);
  1321. end;
  1322. OP_DIV,
  1323. OP_IDIV :
  1324. begin
  1325. internalerror(2002081602);
  1326. end;
  1327. OP_MUL,
  1328. OP_IMUL:
  1329. begin
  1330. if not (CPUM68K_HAS_32BITMUL in cpu_capabilities[current_settings.cputype]) then
  1331. if op = OP_MUL then
  1332. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1333. else
  1334. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1335. else
  1336. begin
  1337. { 68020+ and ColdFire codepath, probably could be improved }
  1338. hreg1 := force_to_dataregister(list, size, src);
  1339. hreg2 := force_to_dataregister(list, size, dst);
  1340. sign_extend(list, size, hreg1);
  1341. sign_extend(list, size, hreg2);
  1342. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1343. { move back result into destination register }
  1344. move_if_needed(list, size, hreg2, dst);
  1345. end;
  1346. end;
  1347. OP_NEG,
  1348. OP_NOT :
  1349. begin
  1350. { if there are two operands, move the register,
  1351. since the operation will only be done on the result
  1352. register. }
  1353. if (src<>dst) then
  1354. a_load_reg_reg(list,size,size,src,dst);
  1355. hreg2 := force_to_dataregister(list, size, dst);
  1356. { coldfire only supports long version }
  1357. if current_settings.cputype in cpu_ColdFire then
  1358. sign_extend(list, size, hreg2);
  1359. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1360. { move back the result to the result register if needed }
  1361. move_if_needed(list, size, hreg2, dst);
  1362. end;
  1363. else
  1364. internalerror(2002072902);
  1365. end;
  1366. end;
  1367. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1368. var
  1369. opcode : tasmop;
  1370. opsize : topsize;
  1371. href : treference;
  1372. hreg : tregister;
  1373. begin
  1374. opcode := topcg2tasmop[op];
  1375. opsize := TCGSize2OpSize[size];
  1376. { on ColdFire all arithmetic operations are only possible on 32bit
  1377. and addressing modes are limited }
  1378. if needs_unaligned(ref.alignment,size) or
  1379. (not (CPUM68K_HAS_BYTEWORDMATH in cpu_capabilities[current_settings.cputype]) and (opsize <> S_L)) then
  1380. begin
  1381. //list.concat(tai_comment.create(strpnew('a_op_reg_ref: inherited #1')));
  1382. inherited;
  1383. exit;
  1384. end;
  1385. case op of
  1386. OP_ADD,
  1387. OP_SUB,
  1388. OP_OR,
  1389. OP_XOR,
  1390. OP_AND:
  1391. begin
  1392. //list.concat(tai_comment.create(strpnew('a_op_reg_ref: normal op')));
  1393. href:=ref;
  1394. fixref(list,href,false);
  1395. { areg -> ref arithmetic operations are impossible on 68k }
  1396. hreg:=force_to_dataregister(list,size,reg);
  1397. { add/sub works the same way, so have it unified here }
  1398. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1399. end;
  1400. else begin
  1401. //list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited #2')));
  1402. inherited;
  1403. end;
  1404. end;
  1405. end;
  1406. procedure tcg68k.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1407. var
  1408. opcode : tasmop;
  1409. opsize : topsize;
  1410. href : treference;
  1411. hreg : tregister;
  1412. begin
  1413. opcode := topcg2tasmop[op];
  1414. opsize := TCGSize2OpSize[size];
  1415. { on ColdFire all arithmetic operations are only possible on 32bit
  1416. and addressing modes are limited }
  1417. if needs_unaligned(ref.alignment,size) or
  1418. (not (CPUM68K_HAS_BYTEWORDMATH in cpu_capabilities[current_settings.cputype]) and (opsize <> S_L)) then
  1419. begin
  1420. //list.concat(tai_comment.create(strpnew('a_op_ref_reg: inherited #1')));
  1421. inherited;
  1422. exit;
  1423. end;
  1424. case op of
  1425. OP_ADD,
  1426. OP_SUB,
  1427. OP_OR,
  1428. OP_AND,
  1429. OP_MUL,
  1430. OP_IMUL:
  1431. begin
  1432. //list.concat(tai_comment.create(strpnew('a_op_ref_reg: normal op')));
  1433. href:=ref;
  1434. { Coldfire doesn't support d(Ax,Dx) for long MULx... }
  1435. fixref(list,href,(op in [OP_MUL,OP_IMUL]) and
  1436. (current_settings.cputype in cpu_coldfire));
  1437. list.concat(taicpu.op_ref_reg(opcode, opsize, href, reg));
  1438. end;
  1439. else begin
  1440. //list.concat(tai_comment.create(strpnew('a_op_ref_reg inherited #2')));
  1441. inherited;
  1442. end;
  1443. end;
  1444. end;
  1445. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1446. l : tasmlabel);
  1447. var
  1448. hregister : tregister;
  1449. instr : taicpu;
  1450. need_temp_reg : boolean;
  1451. temp_size: topsize;
  1452. begin
  1453. need_temp_reg := false;
  1454. { plain 68000 doesn't support address registers for TST }
  1455. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1456. (a = 0) and isaddressregister(reg);
  1457. { ColdFire doesn't support address registers for CMPI }
  1458. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1459. and (a <> 0) and isaddressregister(reg));
  1460. if need_temp_reg then
  1461. begin
  1462. hregister := getintregister(list,OS_INT);
  1463. temp_size := TCGSize2OpSize[size];
  1464. if temp_size < S_W then
  1465. temp_size := S_W;
  1466. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1467. add_move_instruction(instr);
  1468. list.concat(instr);
  1469. reg := hregister;
  1470. { do sign extension if size had to be modified }
  1471. if temp_size <> TCGSize2OpSize[size] then
  1472. begin
  1473. sign_extend(list, size, reg);
  1474. size:=OS_INT;
  1475. end;
  1476. end;
  1477. if a = 0 then
  1478. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1479. else
  1480. begin
  1481. { ColdFire ISA A also needs S_L for CMPI }
  1482. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1483. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1484. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1485. default. (KB) }
  1486. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c,cpu_cfv4e]} then
  1487. begin
  1488. sign_extend(list, size, reg);
  1489. size:=OS_INT;
  1490. end;
  1491. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1492. end;
  1493. { emit the actual jump to the label }
  1494. a_jmp_cond(list,cmp_op,l);
  1495. end;
  1496. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1497. var
  1498. tmpref: treference;
  1499. begin
  1500. { optimize for usage of TST here, so ref compares against zero, which is the
  1501. most common case by far in the RTL code at least (KB) }
  1502. if not needs_unaligned(ref.alignment,size) and (a = 0) then
  1503. begin
  1504. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1505. tmpref:=ref;
  1506. fixref(list,tmpref,false);
  1507. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1508. a_jmp_cond(list,cmp_op,l);
  1509. end
  1510. else
  1511. begin
  1512. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1513. inherited;
  1514. end;
  1515. end;
  1516. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1517. begin
  1518. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c,cpu_cfv4e]) then
  1519. begin
  1520. sign_extend(list,size,reg1);
  1521. sign_extend(list,size,reg2);
  1522. size:=OS_INT;
  1523. end;
  1524. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1525. { emit the actual jump to the label }
  1526. a_jmp_cond(list,cmp_op,l);
  1527. end;
  1528. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1529. var
  1530. ai: taicpu;
  1531. begin
  1532. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1533. ai.is_jmp := true;
  1534. list.concat(ai);
  1535. end;
  1536. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1537. var
  1538. ai: taicpu;
  1539. begin
  1540. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1541. ai.is_jmp := true;
  1542. list.concat(ai);
  1543. end;
  1544. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1545. var
  1546. ai : taicpu;
  1547. begin
  1548. if not (f in FloatResFlags) then
  1549. ai := Taicpu.op_sym(A_BXX,S_NO,l)
  1550. else
  1551. ai := Taicpu.op_sym(A_FBXX,S_NO,l);
  1552. ai.SetCondition(flags_to_cond(f));
  1553. ai.is_jmp := true;
  1554. list.concat(ai);
  1555. end;
  1556. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1557. var
  1558. ai : taicpu;
  1559. htrue: tasmlabel;
  1560. begin
  1561. if isaddressregister(reg) then
  1562. internalerror(2017051701);
  1563. if (f in FloatResFlags) then
  1564. begin
  1565. //list.concat(tai_comment.create(strpnew('flags2reg: float resflags')));
  1566. current_asmdata.getjumplabel(htrue);
  1567. a_load_const_reg(current_asmdata.CurrAsmList,OS_32,1,reg);
  1568. a_jmp_flags(list, f, htrue);
  1569. a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,reg);
  1570. a_label(current_asmdata.CurrAsmList,htrue);
  1571. exit;
  1572. end;
  1573. ai:=Taicpu.Op_reg(A_Sxx,S_B,reg);
  1574. ai.SetCondition(flags_to_cond(f));
  1575. list.concat(ai);
  1576. { Scc stores a complete byte of 1s, but the compiler expects only one
  1577. bit set, so ensure this is the case }
  1578. if not (current_settings.cputype in cpu_coldfire) then
  1579. begin
  1580. if size in [OS_S8,OS_8] then
  1581. list.concat(taicpu.op_reg(A_NEG,S_B,reg))
  1582. else
  1583. list.concat(taicpu.op_const_reg(A_AND,TCgSize2OpSize[size],1,reg));
  1584. end
  1585. else
  1586. list.concat(taicpu.op_const_reg(A_AND,S_L,1,reg));
  1587. end;
  1588. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1589. const
  1590. lentocgsize: array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  1591. var
  1592. helpsize : longint;
  1593. i : byte;
  1594. hregister : tregister;
  1595. iregister : tregister;
  1596. jregister : tregister;
  1597. hl : tasmlabel;
  1598. srcrefp,dstrefp : treference;
  1599. srcref,dstref : treference;
  1600. begin
  1601. if (len < 1) then
  1602. exit;
  1603. if (len = 1) or
  1604. ((len in [2,4]) and
  1605. not needs_unaligned(source.alignment,lentocgsize[len]) and
  1606. not needs_unaligned(dest.alignment,lentocgsize[len])) then
  1607. begin
  1608. //list.concat(tai_comment.create(strpnew('g_concatcopy: small')));
  1609. a_load_ref_ref(list,lentocgsize[len],lentocgsize[len],source,dest);
  1610. exit;
  1611. end;
  1612. //list.concat(tai_comment.create(strpnew('g_concatcopy')));
  1613. hregister := getintregister(list,OS_INT);
  1614. iregister:=getaddressregister(list);
  1615. reference_reset_base(srcref,iregister,0,source.temppos,source.alignment,source.volatility);
  1616. srcrefp:=srcref;
  1617. srcrefp.direction := dir_inc;
  1618. jregister:=getaddressregister(list);
  1619. reference_reset_base(dstref,jregister,0,dest.temppos,dest.alignment,dest.volatility);
  1620. dstrefp:=dstref;
  1621. dstrefp.direction := dir_inc;
  1622. { iregister = source }
  1623. { jregister = destination }
  1624. a_loadaddr_ref_reg(list,source,iregister);
  1625. a_loadaddr_ref_reg(list,dest,jregister);
  1626. if not (needs_unaligned(source.alignment,OS_INT) or needs_unaligned(dest.alignment,OS_INT)) then
  1627. begin
  1628. if not ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=16))) then
  1629. begin
  1630. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1631. helpsize := len - len mod 4;
  1632. len := len mod 4;
  1633. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1634. current_asmdata.getjumplabel(hl);
  1635. a_label(list,hl);
  1636. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcrefp,dstrefp));
  1637. if (current_settings.cputype in cpu_coldfire) or ((helpsize div 4)-1 > high(smallint)) then
  1638. begin
  1639. { Coldfire does not support DBRA, also it is word only }
  1640. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1641. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1642. end
  1643. else
  1644. list.concat(taicpu.op_reg_sym(A_DBRA,S_NO,hregister,hl));
  1645. end;
  1646. helpsize:=len div 4;
  1647. { move a dword x times }
  1648. for i:=1 to helpsize do
  1649. begin
  1650. dec(len,4);
  1651. if (len > 0) then
  1652. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcrefp,dstrefp))
  1653. else
  1654. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcref,dstref));
  1655. end;
  1656. { move a word }
  1657. if len>1 then
  1658. begin
  1659. dec(len,2);
  1660. if (len > 0) then
  1661. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,srcrefp,dstrefp))
  1662. else
  1663. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,srcref,dstref));
  1664. end;
  1665. { move a single byte }
  1666. if len>0 then
  1667. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,srcref,dstref));
  1668. end
  1669. else
  1670. begin
  1671. { Fast 68010 loop mode with no possible alignment problems }
  1672. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1673. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1674. current_asmdata.getjumplabel(hl);
  1675. a_label(list,hl);
  1676. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,srcrefp,dstrefp));
  1677. if (len - 1) > high(smallint) then
  1678. begin
  1679. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1680. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1681. end
  1682. else
  1683. list.concat(taicpu.op_reg_sym(A_DBRA,S_NO,hregister,hl));
  1684. end;
  1685. end;
  1686. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1687. var
  1688. hl : tasmlabel;
  1689. ai : taicpu;
  1690. cond : TAsmCond;
  1691. begin
  1692. if not(cs_check_overflow in current_settings.localswitches) then
  1693. exit;
  1694. current_asmdata.getjumplabel(hl);
  1695. if not ((def.typ=pointerdef) or
  1696. ((def.typ=orddef) and
  1697. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1698. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1699. cond:=C_VC
  1700. else
  1701. begin
  1702. { MUL/DIV always sets the overflow flag, and never the carry flag }
  1703. { Note/Fixme: This still doesn't cover the ColdFire, where none of these opcodes
  1704. set either the overflow or the carry flag. So CF must be handled in other ways. }
  1705. case taicpu(list.last).opcode of
  1706. A_MULU,A_MULS,A_DIVS,A_DIVU,A_DIVUL,A_DIVSL:
  1707. cond:=C_VC;
  1708. else
  1709. cond:=C_CC;
  1710. end;
  1711. end;
  1712. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1713. ai.SetCondition(cond);
  1714. ai.is_jmp:=true;
  1715. list.concat(ai);
  1716. a_call_name(list,'FPC_OVERFLOW',false);
  1717. a_label(list,hl);
  1718. end;
  1719. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1720. begin
  1721. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1722. However, a LINK seems faster than two moves on everything from 68000
  1723. to '060, so the two move branch here was dropped. (KB) }
  1724. if not nostackframe then
  1725. begin
  1726. localsize:=align(localsize,4);
  1727. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1728. begin
  1729. if (localsize > high(smallint)) then
  1730. begin
  1731. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1732. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1733. end
  1734. else
  1735. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1736. end
  1737. else
  1738. begin
  1739. if localsize<>0 then
  1740. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1741. current_procinfo.final_localsize:=localsize;
  1742. end;
  1743. end;
  1744. end;
  1745. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1746. var
  1747. r,hregister : TRegister;
  1748. ref : TReference;
  1749. ref2: TReference;
  1750. begin
  1751. { if a subroutine is marked as non-returning, we do
  1752. not generate any exit code, so we really trust the noreturn directive
  1753. }
  1754. if po_noreturn in current_procinfo.procdef.procoptions then
  1755. exit;
  1756. if not nostackframe then
  1757. begin
  1758. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1759. begin
  1760. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1761. { if parasize is less than zero here, we probably have a cdecl function.
  1762. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1763. 68k GCC uses two different methods to free the stack, depending if the target
  1764. architecture supports RTD or not, and one does callee side, the other does
  1765. caller side free, which looks like a PITA to support. We have to figure this
  1766. out later. More info welcomed. (KB) }
  1767. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1768. begin
  1769. if CPUM68K_HAS_RTD in cpu_capabilities[current_settings.cputype] then
  1770. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1771. else
  1772. begin
  1773. { We must pull the PC Counter from the stack, before }
  1774. { restoring the stack pointer, otherwise the PC would }
  1775. { point to nowhere! }
  1776. { Instead of doing a slow copy of the return address while trying }
  1777. { to feed it to the RTS instruction, load the PC to A1 (scratch reg) }
  1778. { then free up the stack allocated for paras, then use a JMP (A1) to }
  1779. { return to the caller with the paras freed. (KB) }
  1780. hregister:=NR_A1;
  1781. cg.a_reg_alloc(list,hregister);
  1782. reference_reset_base(ref,NR_STACK_POINTER_REG,0,ctempposinvalid,4,[]);
  1783. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1784. { instead of using a postincrement above (which also writes the }
  1785. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1786. { below then take that size into account as well, so SP reg is only }
  1787. { written once (KB) }
  1788. parasize:=parasize+4;
  1789. r:=NR_SP;
  1790. { can we do a quick addition ... }
  1791. if (parasize < 9) then
  1792. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1793. else { nope ... }
  1794. begin
  1795. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,ctempposinvalid,4,[]);
  1796. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1797. end;
  1798. reference_reset_base(ref,hregister,0,ctempposinvalid,4,[]);
  1799. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1800. end;
  1801. end
  1802. else
  1803. list.concat(taicpu.op_none(A_RTS,S_NO));
  1804. end
  1805. else
  1806. begin
  1807. if parasize<>0 then
  1808. Internalerror(2020112901);
  1809. if current_procinfo.final_localsize<>0 then
  1810. list.concat(taicpu.op_const_reg(A_ADDA,S_L,current_procinfo.final_localsize,NR_STACK_POINTER_REG));
  1811. list.concat(taicpu.op_none(A_RTS,S_NO));
  1812. end;
  1813. end
  1814. else
  1815. begin
  1816. list.concat(taicpu.op_none(A_RTS,S_NO));
  1817. end;
  1818. { Routines with the poclearstack flag set use only a ret.
  1819. also routines with parasize=0 }
  1820. { TODO: figure out if these are still relevant to us (KB) }
  1821. (*
  1822. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1823. begin
  1824. { complex return values are removed from stack in C code PM }
  1825. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1826. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1827. else
  1828. list.concat(taicpu.op_none(A_RTS,S_NO));
  1829. end
  1830. else if (parasize=0) then
  1831. begin
  1832. list.concat(taicpu.op_none(A_RTS,S_NO));
  1833. end
  1834. else
  1835. *)
  1836. end;
  1837. procedure tcg68k.g_save_registers(list:TAsmList);
  1838. var
  1839. dataregs: tcpuregisterset;
  1840. addrregs: tcpuregisterset;
  1841. fpuregs: tcpuregisterset;
  1842. href : treference;
  1843. hreg : tregister;
  1844. hfreg : tregister;
  1845. size : longint;
  1846. fsize : longint;
  1847. r : integer;
  1848. regs_to_save_int,
  1849. regs_to_save_address,
  1850. regs_to_save_fpu: tcpuregisterarray;
  1851. begin
  1852. { The code generated by the section below, particularly the movem.l
  1853. instruction is known to cause an issue when compiled by some GNU
  1854. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1855. when you run into this problem, just call inherited here instead
  1856. to skip the movem.l generation. But better just use working GNU
  1857. AS version instead. (KB) }
  1858. dataregs:=[];
  1859. addrregs:=[];
  1860. fpuregs:=[];
  1861. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  1862. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  1863. regs_to_save_fpu:=paramanager.get_saved_registers_fpu(current_procinfo.procdef.proccalloption);
  1864. { calculate temp. size }
  1865. size:=0;
  1866. fsize:=0;
  1867. hreg:=NR_NO;
  1868. hfreg:=NR_NO;
  1869. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  1870. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  1871. begin
  1872. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  1873. inc(size,sizeof(aint));
  1874. dataregs:=dataregs + [regs_to_save_int[r]];
  1875. end;
  1876. if uses_registers(R_ADDRESSREGISTER) then
  1877. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  1878. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1879. begin
  1880. hreg:=newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE);
  1881. inc(size,sizeof(aint));
  1882. addrregs:=addrregs + [regs_to_save_address[r]];
  1883. end;
  1884. if uses_registers(R_FPUREGISTER) then
  1885. for r:=low(regs_to_save_fpu) to high(regs_to_save_fpu) do
  1886. if regs_to_save_fpu[r] in rg[R_FPUREGISTER].used_in_proc then
  1887. begin
  1888. hfreg:=newreg(R_FPUREGISTER,regs_to_save_fpu[r],R_SUBNONE);
  1889. inc(fsize,fpuregsize);
  1890. fpuregs:=fpuregs + [regs_to_save_fpu[r]];
  1891. end;
  1892. { 68k has no MM registers }
  1893. if uses_registers(R_MMREGISTER) then
  1894. internalerror(2014030201);
  1895. if (size+fsize) > 0 then
  1896. begin
  1897. tg.GetTemp(list,size+fsize,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1898. include(current_procinfo.flags,pi_has_saved_regs);
  1899. { Copy registers to temp }
  1900. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1901. href:=current_procinfo.save_regs_ref;
  1902. if (abs(current_procinfo.save_regs_ref.offset)>abs(low(smallint))) and
  1903. not (CPUM68K_HAS_BASEDISP in cpu_capabilities[current_settings.cputype]) then
  1904. begin
  1905. href.offset:=0;
  1906. //list.concat(tai_comment.create(strpnew('g_save_registers: large offset')));
  1907. list.concat(taicpu.op_const_reg(A_SUBA,S_L,-current_procinfo.save_regs_ref.offset,href.base));
  1908. end;
  1909. if size > 0 then
  1910. if size = sizeof(aint) then
  1911. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1912. else
  1913. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,[],href));
  1914. if fsize > 0 then
  1915. begin
  1916. { size is always longword aligned, while fsize is not }
  1917. inc(href.offset,size);
  1918. if fsize = fpuregsize then
  1919. list.concat(taicpu.op_reg_ref(A_FMOVE,fpuregopsize,hfreg,href))
  1920. else
  1921. list.concat(taicpu.op_regset_ref(A_FMOVEM,fpuregopsize,[],[],fpuregs,href));
  1922. end;
  1923. if (abs(current_procinfo.save_regs_ref.offset)>abs(low(smallint))) and
  1924. not (CPUM68K_HAS_BASEDISP in cpu_capabilities[current_settings.cputype]) then
  1925. begin
  1926. list.concat(taicpu.op_const_reg(A_ADDA,S_L,-current_procinfo.save_regs_ref.offset,href.base));
  1927. end;
  1928. end;
  1929. end;
  1930. procedure tcg68k.g_restore_registers(list:TAsmList);
  1931. var
  1932. dataregs: tcpuregisterset;
  1933. addrregs: tcpuregisterset;
  1934. fpuregs : tcpuregisterset;
  1935. href : treference;
  1936. r : integer;
  1937. hreg : tregister;
  1938. hfreg : tregister;
  1939. size : longint;
  1940. fsize : longint;
  1941. regs_to_save_int,
  1942. regs_to_save_address,
  1943. regs_to_save_fpu: tcpuregisterarray;
  1944. begin
  1945. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1946. dataregs:=[];
  1947. addrregs:=[];
  1948. fpuregs:=[];
  1949. if not(pi_has_saved_regs in current_procinfo.flags) then
  1950. exit;
  1951. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  1952. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  1953. regs_to_save_fpu:=paramanager.get_saved_registers_fpu(current_procinfo.procdef.proccalloption);
  1954. { Copy registers from temp }
  1955. size:=0;
  1956. fsize:=0;
  1957. hreg:=NR_NO;
  1958. hfreg:=NR_NO;
  1959. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  1960. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  1961. begin
  1962. inc(size,sizeof(aint));
  1963. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  1964. { Allocate register so the optimizer does not remove the load }
  1965. a_reg_alloc(list,hreg);
  1966. dataregs:=dataregs + [regs_to_save_int[r]];
  1967. end;
  1968. if uses_registers(R_ADDRESSREGISTER) then
  1969. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  1970. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1971. begin
  1972. inc(size,sizeof(aint));
  1973. hreg:=newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE);
  1974. { Allocate register so the optimizer does not remove the load }
  1975. a_reg_alloc(list,hreg);
  1976. addrregs:=addrregs + [regs_to_save_address[r]];
  1977. end;
  1978. if uses_registers(R_FPUREGISTER) then
  1979. for r:=low(regs_to_save_fpu) to high(regs_to_save_fpu) do
  1980. if regs_to_save_fpu[r] in rg[R_FPUREGISTER].used_in_proc then
  1981. begin
  1982. inc(fsize,fpuregsize);
  1983. hfreg:=newreg(R_FPUREGISTER,regs_to_save_fpu[r],R_SUBNONE);
  1984. { Allocate register so the optimizer does not remove the load }
  1985. a_reg_alloc(list,hfreg);
  1986. fpuregs:=fpuregs + [regs_to_save_fpu[r]];
  1987. end;
  1988. { 68k has no MM registers }
  1989. if uses_registers(R_MMREGISTER) then
  1990. internalerror(2014030202);
  1991. { Restore registers from temp }
  1992. href:=current_procinfo.save_regs_ref;
  1993. if (abs(current_procinfo.save_regs_ref.offset)>abs(low(smallint))) and
  1994. not (CPUM68K_HAS_BASEDISP in cpu_capabilities[current_settings.cputype]) then
  1995. begin
  1996. href.offset:=0;
  1997. //list.concat(tai_comment.create(strpnew('g_restore_registers: large offset')));
  1998. list.concat(taicpu.op_const_reg(A_SUBA,S_L,-current_procinfo.save_regs_ref.offset,href.base));
  1999. end;
  2000. if size > 0 then
  2001. if size = sizeof(aint) then
  2002. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  2003. else
  2004. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs,[]));
  2005. if fsize > 0 then
  2006. begin
  2007. { size is always longword aligned, while fsize is not }
  2008. inc(href.offset,size);
  2009. if fsize = fpuregsize then
  2010. list.concat(taicpu.op_ref_reg(A_FMOVE,fpuregopsize,href,hfreg))
  2011. else
  2012. list.concat(taicpu.op_ref_regset(A_FMOVEM,fpuregopsize,href,[],[],fpuregs));
  2013. end;
  2014. if (abs(current_procinfo.save_regs_ref.offset)>abs(low(smallint))) and
  2015. not (CPUM68K_HAS_BASEDISP in cpu_capabilities[current_settings.cputype]) then
  2016. begin
  2017. list.concat(taicpu.op_const_reg(A_ADDA,S_L,-current_procinfo.save_regs_ref.offset,href.base));
  2018. end;
  2019. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  2020. end;
  2021. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  2022. begin
  2023. case _newsize of
  2024. OS_S16, OS_16:
  2025. case _oldsize of
  2026. OS_S8:
  2027. begin { 8 -> 16 bit sign extend }
  2028. if (isaddressregister(reg)) then
  2029. internalerror(2014031201);
  2030. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  2031. end;
  2032. OS_8: { 8 -> 16 bit zero extend }
  2033. begin
  2034. if not (CPUM68K_HAS_BYTEWORDMATH in cpu_capabilities[current_settings.cputype]) then
  2035. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  2036. else
  2037. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  2038. end;
  2039. else
  2040. ;
  2041. end;
  2042. OS_S32, OS_32:
  2043. case _oldsize of
  2044. OS_S8:
  2045. begin { 8 -> 32 bit sign extend }
  2046. if (isaddressregister(reg)) then
  2047. internalerror(2014031202);
  2048. if (current_settings.cputype = cpu_MC68000) then
  2049. begin
  2050. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  2051. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  2052. end
  2053. else
  2054. begin
  2055. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  2056. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  2057. end;
  2058. end;
  2059. OS_8: { 8 -> 32 bit zero extend }
  2060. begin
  2061. if (isaddressregister(reg)) then
  2062. internalerror(2015031501);
  2063. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  2064. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  2065. end;
  2066. OS_S16: { 16 -> 32 bit sign extend }
  2067. begin
  2068. { address registers are sign-extended from 16->32 bit anyway
  2069. automagically on every W operation by the CPU, so this is a NOP }
  2070. if not isaddressregister(reg) then
  2071. begin
  2072. //list.concat(tai_comment.create(strpnew('sign extend word')));
  2073. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  2074. end;
  2075. end;
  2076. OS_16:
  2077. begin
  2078. if (isaddressregister(reg)) then
  2079. internalerror(2015031503);
  2080. //list.concat(tai_comment.create(strpnew('zero extend word')));
  2081. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  2082. end;
  2083. else
  2084. ;
  2085. end;
  2086. else
  2087. ;
  2088. end; { otherwise the size is already correct }
  2089. end;
  2090. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  2091. begin
  2092. sign_extend(list, _oldsize, OS_INT, reg);
  2093. end;
  2094. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2095. var
  2096. ai : taicpu;
  2097. begin
  2098. if cond=OC_None then
  2099. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  2100. else
  2101. begin
  2102. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  2103. ai.SetCondition(TOpCmp2AsmCond[cond]);
  2104. end;
  2105. ai.is_jmp:=true;
  2106. list.concat(ai);
  2107. end;
  2108. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  2109. operations on an address register. if the register is a dataregister anyway, it
  2110. just returns it untouched.}
  2111. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  2112. var
  2113. scratch_reg: TRegister;
  2114. begin
  2115. if isaddressregister(reg) then
  2116. begin
  2117. scratch_reg:=getintregister(list,OS_INT);
  2118. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg));
  2119. result:=scratch_reg;
  2120. end
  2121. else
  2122. result:=reg;
  2123. end;
  2124. { moves source register to destination register, if the two are not the same. can be used in pair
  2125. with force_to_dataregister() }
  2126. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  2127. var
  2128. instr: Taicpu;
  2129. begin
  2130. if (src <> dest) then
  2131. begin
  2132. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  2133. add_move_instruction(instr);
  2134. list.concat(instr);
  2135. end;
  2136. end;
  2137. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2138. var
  2139. hsym : tsym;
  2140. href : treference;
  2141. paraloc : Pcgparalocation;
  2142. begin
  2143. { calculate the parameter info for the procdef }
  2144. procdef.init_paraloc_info(callerside);
  2145. hsym:=tsym(procdef.parast.Find('self'));
  2146. if not(assigned(hsym) and
  2147. (hsym.typ=paravarsym)) then
  2148. internalerror(2013100702);
  2149. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2150. while paraloc<>nil do
  2151. with paraloc^ do
  2152. begin
  2153. case loc of
  2154. LOC_REGISTER:
  2155. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2156. LOC_REFERENCE:
  2157. begin
  2158. { offset in the wrapper needs to be adjusted for the stored
  2159. return address }
  2160. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  2161. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  2162. and it's probably smaller code for the majority of cases (if ioffset small, the
  2163. load will use MOVEQ) (KB) }
  2164. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  2165. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  2166. end
  2167. else
  2168. internalerror(2013100703);
  2169. end;
  2170. paraloc:=next;
  2171. end;
  2172. end;
  2173. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2174. begin
  2175. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  2176. end;
  2177. procedure tcg68k.check_register_size(size:tcgsize;reg:tregister);
  2178. begin
  2179. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  2180. internalerror(201512131);
  2181. end;
  2182. function tcg68k.optimize_const_mul_to_shift_sub_add(list: TAsmList; maxops: longint; a: tcgint; size: tcgsize; reg: TRegister): boolean;
  2183. var
  2184. i: longint;
  2185. nextpower: tcgint;
  2186. powerbit: longint;
  2187. submask: tcgint;
  2188. lastshift: longint;
  2189. hreg: tregister;
  2190. firstmov: boolean;
  2191. begin
  2192. nextpower:=nextpowerof2(a,powerbit);
  2193. submask:=nextpower-a;
  2194. result:=not ((popcnt(qword(a)) > maxops) and ((popcnt(qword(submask))+1) > maxops));
  2195. if not result then
  2196. exit;
  2197. list.concat(tai_comment.create(strpnew('optimize_const_mul_to_shift_sub_add, multiplier: '+tostr(a))));
  2198. lastshift:=0;
  2199. hreg:=getintregister(list,OS_INT);
  2200. if (popcnt(qword(a)) < (popcnt(qword(submask))+1)) then
  2201. begin
  2202. { doing additions }
  2203. firstmov:=(a and 1) = 0;
  2204. if not firstmov then
  2205. a_load_reg_reg(list,size,OS_INT,reg,hreg);
  2206. for i:=1 to bsrqword(a) do
  2207. if ((a shr i) and 1) = 1 then
  2208. begin
  2209. if firstmov then
  2210. begin
  2211. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,reg);
  2212. a_load_reg_reg(list,OS_INT,OS_INT,reg,hreg);
  2213. firstmov:=false;
  2214. end
  2215. else
  2216. begin
  2217. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,hreg);
  2218. a_op_reg_reg(list,OP_ADD,OS_INT,hreg,reg);
  2219. end;
  2220. lastshift:=i;
  2221. end;
  2222. end
  2223. else
  2224. begin
  2225. { doing subtractions }
  2226. a_load_const_reg(list,OS_INT,0,hreg);
  2227. for i:=0 to bsrqword(submask) do
  2228. if ((submask shr i) and 1) = 1 then
  2229. begin
  2230. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,reg);
  2231. a_op_reg_reg(list,OP_SUB,OS_INT,reg,hreg);
  2232. lastshift:=i;
  2233. end;
  2234. a_op_const_reg(list,OP_SHL,OS_INT,powerbit-lastshift,reg);
  2235. a_op_reg_reg(list,OP_ADD,OS_INT,hreg,reg);
  2236. end;
  2237. result:=true;
  2238. end;
  2239. {****************************************************************************}
  2240. { TCG64F68K }
  2241. {****************************************************************************}
  2242. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  2243. var
  2244. opcode : tasmop;
  2245. xopcode : tasmop;
  2246. instr : taicpu;
  2247. begin
  2248. opcode := topcg2tasmop[op];
  2249. xopcode := topcg2tasmopx[op];
  2250. case op of
  2251. OP_ADD,OP_SUB:
  2252. begin
  2253. { if one of these three registers is an address
  2254. register, we'll really get into problems! }
  2255. if isaddressregister(regdst.reglo) or
  2256. isaddressregister(regdst.reghi) or
  2257. isaddressregister(regsrc.reghi) then
  2258. internalerror(2014030101);
  2259. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  2260. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  2261. end;
  2262. OP_AND,OP_OR:
  2263. begin
  2264. { at least one of the registers must be a data register }
  2265. if (isaddressregister(regdst.reglo) and
  2266. isaddressregister(regsrc.reglo)) or
  2267. (isaddressregister(regsrc.reghi) and
  2268. isaddressregister(regdst.reghi)) then
  2269. internalerror(2014030102);
  2270. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2271. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2272. end;
  2273. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  2274. OP_IDIV,OP_DIV,
  2275. OP_IMUL,OP_MUL:
  2276. internalerror(2002081701);
  2277. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  2278. OP_SAR,OP_SHL,OP_SHR:
  2279. internalerror(2002081702);
  2280. OP_XOR:
  2281. begin
  2282. if isaddressregister(regdst.reglo) or
  2283. isaddressregister(regsrc.reglo) or
  2284. isaddressregister(regsrc.reghi) or
  2285. isaddressregister(regdst.reghi) then
  2286. internalerror(2014030103);
  2287. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2288. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2289. end;
  2290. OP_NEG,OP_NOT:
  2291. begin
  2292. if isaddressregister(regdst.reglo) or
  2293. isaddressregister(regdst.reghi) then
  2294. internalerror(2014030104);
  2295. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  2296. cg.add_move_instruction(instr);
  2297. list.concat(instr);
  2298. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  2299. cg.add_move_instruction(instr);
  2300. list.concat(instr);
  2301. if (op = OP_NOT) then
  2302. xopcode:=opcode;
  2303. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  2304. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  2305. end;
  2306. else
  2307. ;
  2308. end; { end case }
  2309. end;
  2310. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  2311. var
  2312. href : treference;
  2313. hreg: tregister;
  2314. begin
  2315. case op of
  2316. OP_NEG,OP_NOT:
  2317. begin
  2318. a_load64_ref_reg(list,ref,reg);
  2319. a_op64_reg_reg(list,op,size,reg,reg);
  2320. end;
  2321. OP_AND,OP_OR:
  2322. begin
  2323. href:=ref;
  2324. tcg68k(cg).fixref(list,href,false);
  2325. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reghi));
  2326. inc(href.offset,4);
  2327. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reglo));
  2328. end;
  2329. OP_ADD,OP_SUB:
  2330. begin
  2331. href:=ref;
  2332. tcg68k(cg).fixref(list,href,false);
  2333. hreg:=cg.getintregister(list,OS_32);
  2334. cg.a_load_ref_reg(list,OS_32,OS_32,href,hreg);
  2335. inc(href.offset,4);
  2336. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reglo));
  2337. list.concat(taicpu.op_reg_reg(topcg2tasmopx[op],S_L,hreg,reg.reghi));
  2338. end;
  2339. else
  2340. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  2341. high dword, although low dword can still be handled directly. }
  2342. inherited a_op64_ref_reg(list,op,size,ref,reg);
  2343. end;
  2344. end;
  2345. procedure tcg64f68k.a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const ref : treference);
  2346. var
  2347. href: treference;
  2348. hreg: tregister;
  2349. begin
  2350. reg.reghi:=tcg68k(cg).force_to_dataregister(list,OS_32,reg.reghi);
  2351. reg.reglo:=tcg68k(cg).force_to_dataregister(list,OS_32,reg.reglo);
  2352. case op of
  2353. OP_AND,OP_OR,OP_XOR:
  2354. begin
  2355. href:=ref;
  2356. tcg68k(cg).fixref(list,href,false);
  2357. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reghi,href));
  2358. inc(href.offset,4);
  2359. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reglo,href));
  2360. end;
  2361. OP_ADD,OP_SUB:
  2362. begin
  2363. href:=ref;
  2364. tcg68k(cg).fixref(list,href,false);
  2365. hreg:=cg.getintregister(list,OS_32);
  2366. cg.a_load_ref_reg(list,OS_32,OS_32,href,hreg);
  2367. inc(href.offset,4);
  2368. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reglo,href));
  2369. list.concat(taicpu.op_reg_reg(topcg2tasmopx[op],S_L,reg.reghi,hreg));
  2370. dec(href.offset,4);
  2371. cg.a_load_reg_ref(list,OS_32,OS_32,hreg,href);
  2372. end;
  2373. else
  2374. inherited a_op64_reg_ref(list,op,size,reg,ref);
  2375. end;
  2376. end;
  2377. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  2378. var
  2379. lowvalue : cardinal;
  2380. highvalue : cardinal;
  2381. opcode : tasmop;
  2382. xopcode : tasmop;
  2383. hreg : tregister;
  2384. begin
  2385. { is it optimized out ? }
  2386. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  2387. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  2388. exit; }
  2389. lowvalue := cardinal(value);
  2390. highvalue := value shr 32;
  2391. opcode := topcg2tasmop[op];
  2392. xopcode := topcg2tasmopx[op];
  2393. { the destination registers must be data registers }
  2394. if isaddressregister(regdst.reglo) or
  2395. isaddressregister(regdst.reghi) then
  2396. internalerror(2014030105);
  2397. case op of
  2398. OP_ADD,OP_SUB:
  2399. begin
  2400. hreg:=cg.getintregister(list,OS_INT);
  2401. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2402. cg.a_load_const_reg(list,OS_S32,tcgint(highvalue),hreg);
  2403. { don't use cg.a_op_const_reg() here, because a possible optimized
  2404. ADDQ/SUBQ wouldn't set the eXtend bit }
  2405. list.concat(taicpu.op_const_reg(opcode,S_L,tcgint(lowvalue),regdst.reglo));
  2406. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2407. end;
  2408. OP_AND,OP_OR,OP_XOR:
  2409. begin
  2410. cg.a_op_const_reg(list,op,OS_S32,tcgint(lowvalue),regdst.reglo);
  2411. cg.a_op_const_reg(list,op,OS_S32,tcgint(highvalue),regdst.reghi);
  2412. end;
  2413. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2414. OP_IDIV,OP_DIV,
  2415. OP_IMUL,OP_MUL:
  2416. internalerror(2002081703);
  2417. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2418. OP_SAR,OP_SHL,OP_SHR:
  2419. internalerror(2002081704);
  2420. { these should have been handled already by earlier passes }
  2421. OP_NOT,OP_NEG:
  2422. internalerror(2012110403);
  2423. else
  2424. ;
  2425. end; { end case }
  2426. end;
  2427. procedure tcg64f68k.a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);
  2428. var
  2429. tmpref: treference;
  2430. begin
  2431. tmpref:=ref;
  2432. tcg68k(cg).fixref(list,tmpref,false);
  2433. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reghi,tmpref);
  2434. inc(tmpref.offset,4);
  2435. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reglo,tmpref);
  2436. end;
  2437. procedure tcg64f68k.a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);
  2438. var
  2439. tmpref: treference;
  2440. begin
  2441. { do not allow 64bit values to be loaded to address registers }
  2442. if isaddressregister(reg.reglo) or
  2443. isaddressregister(reg.reghi) then
  2444. internalerror(2016050501);
  2445. tmpref:=ref;
  2446. tcg68k(cg).fixref(list,tmpref,false);
  2447. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reghi);
  2448. inc(tmpref.offset,4);
  2449. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reglo);
  2450. end;
  2451. procedure create_codegen;
  2452. begin
  2453. cg := tcg68k.create;
  2454. cg64 :=tcg64f68k.create;
  2455. end;
  2456. end.