cgcpu.pas 92 KB

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