cgcpu.pas 81 KB

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