cgcpu.pas 92 KB

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