cgcpu.pas 88 KB

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