cgcpu.pas 87 KB

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