cgcpu.pas 86 KB

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