cgcpu.pas 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412
  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. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  69. { # Sign or zero extend the register to a full 32-bit value.
  70. The new value is left in the same register.
  71. }
  72. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  73. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  74. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  75. function fixref(list: TAsmList; var ref: treference): boolean;
  76. function force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  77. procedure move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  78. protected
  79. procedure call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  80. procedure call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  81. private
  82. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  83. 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. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[pushsize],a,ref));
  269. end
  270. else
  271. inherited a_load_const_cgpara(list,size,a,cgpara);
  272. end;
  273. procedure tcg68k.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  274. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  275. var
  276. pushsize : tcgsize;
  277. tmpreg : tregister;
  278. href : treference;
  279. ref : treference;
  280. begin
  281. if not assigned(paraloc) then
  282. exit;
  283. { TODO: FIX ME!!! this also triggers location bug }
  284. {if (paraloc^.loc<>LOC_REFERENCE) or
  285. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  286. (tcgsize2size[paraloc^.size]>sizeof(tcgint)) then
  287. internalerror(200501162);}
  288. { Pushes are needed in reverse order, add the size of the
  289. current location to the offset where to load from. This
  290. prevents wrong calculations for the last location when
  291. the size is not a power of 2 }
  292. if assigned(paraloc^.next) then
  293. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  294. { Push the data starting at ofs }
  295. href:=r;
  296. inc(href.offset,ofs);
  297. fixref(list,href);
  298. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  299. pushsize:=paraloc^.size
  300. else
  301. pushsize:=int_cgsize(cgpara.alignment);
  302. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, tcgsize2size[pushsize]);
  303. ref.direction := dir_dec;
  304. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  305. begin
  306. tmpreg:=getintregister(list,pushsize);
  307. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  308. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],tmpreg,ref));
  309. end
  310. else
  311. list.concat(taicpu.op_ref_ref(A_MOVE,tcgsize2opsize[pushsize],href,ref));
  312. end;
  313. var
  314. len : tcgint;
  315. href : treference;
  316. begin
  317. { cgpara.size=OS_NO requires a copy on the stack }
  318. if use_push(cgpara) then
  319. begin
  320. { Record copy? }
  321. if (cgpara.size in [OS_NO,OS_F64]) or (size 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(pd,1,paraloc1);
  573. paramanager.getintparaloc(pd,2,paraloc2);
  574. paramanager.getintparaloc(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(pd,1,paraloc1);
  606. paramanager.getintparaloc(pd,2,paraloc2);
  607. paramanager.getintparaloc(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. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[tosize],longint(a),href));
  754. end;
  755. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  756. var
  757. href : treference;
  758. begin
  759. href := ref;
  760. fixref(list,href);
  761. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  762. a_load_reg_reg(list,fromsize,tosize,register,register);
  763. { move to destination reference }
  764. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],register,href));
  765. end;
  766. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  767. var
  768. aref: treference;
  769. bref: treference;
  770. tmpref : treference;
  771. dofix : boolean;
  772. hreg: TRegister;
  773. begin
  774. aref := sref;
  775. bref := dref;
  776. fixref(list,aref);
  777. fixref(list,bref);
  778. if TCGSize2OpSize[fromsize]<>TCGSize2OpSize[tosize] then
  779. begin
  780. { if we need to change the size then always use a temporary
  781. register }
  782. hreg:=getintregister(list,fromsize);
  783. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],aref,hreg));
  784. sign_extend(list,fromsize,tosize,hreg);
  785. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,bref));
  786. exit;
  787. end;
  788. { Coldfire dislikes certain move combinations }
  789. if current_settings.cputype in cpu_coldfire then
  790. begin
  791. { TODO : move.b/w only allowed in newer coldfires... (ISA_B+) }
  792. dofix:=false;
  793. if { (d16,Ax) and (d8,Ax,Xi) }
  794. (
  795. (aref.base<>NR_NO) and
  796. (
  797. (aref.index<>NR_NO) or
  798. (aref.offset<>0)
  799. )
  800. ) or
  801. { (xxx) }
  802. assigned(aref.symbol) then
  803. begin
  804. if aref.index<>NR_NO then
  805. begin
  806. dofix:={ (d16,Ax) and (d8,Ax,Xi) }
  807. (
  808. (bref.base<>NR_NO) and
  809. (
  810. (bref.index<>NR_NO) or
  811. (bref.offset<>0)
  812. )
  813. ) or
  814. { (xxx) }
  815. assigned(bref.symbol);
  816. end
  817. else
  818. { offset <> 0, but no index }
  819. begin
  820. dofix:={ (d8,Ax,Xi) }
  821. (
  822. (bref.base<>NR_NO) and
  823. (bref.index<>NR_NO)
  824. ) or
  825. { (xxx) }
  826. assigned(bref.symbol);
  827. end;
  828. end;
  829. if dofix then
  830. begin
  831. hreg:=getaddressregister(list);
  832. reference_reset_base(tmpref,hreg,0,0);
  833. list.concat(taicpu.op_ref_reg(A_LEA,S_L,aref,hreg));
  834. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],tmpref,bref));
  835. exit;
  836. end;
  837. end;
  838. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  839. end;
  840. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  841. var
  842. instr : taicpu;
  843. hreg : tregister;
  844. opsize : topsize;
  845. begin
  846. { move to destination register }
  847. opsize:=TCGSize2OpSize[fromsize];
  848. if isaddressregister(reg2) and not (opsize in [S_L]) then
  849. begin
  850. hreg:=cg.getintregister(list,OS_ADDR);
  851. instr:=taicpu.op_reg_reg(A_MOVE,TCGSize2OpSize[fromsize],reg1,hreg);
  852. add_move_instruction(instr);
  853. list.concat(instr);
  854. sign_extend(list,fromsize,hreg);
  855. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg2));
  856. end
  857. else
  858. begin
  859. if (reg1<>reg2) then
  860. begin
  861. instr:=taicpu.op_reg_reg(A_MOVE,opsize,reg1,reg2);
  862. add_move_instruction(instr);
  863. list.concat(instr);
  864. end;
  865. sign_extend(list,fromsize,reg2);
  866. end;
  867. end;
  868. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  869. var
  870. href : treference;
  871. hreg : tregister;
  872. size : tcgsize;
  873. opsize: topsize;
  874. begin
  875. href:=ref;
  876. fixref(list,href);
  877. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  878. size:=fromsize
  879. else
  880. size:=tosize;
  881. opsize:=TCGSize2OpSize[size];
  882. if isaddressregister(register) and not (opsize in [S_L]) then
  883. begin
  884. hreg:=getintregister(list,OS_ADDR);
  885. list.concat(taicpu.op_ref_reg(A_MOVE,opsize,href,hreg));
  886. sign_extend(list,size,hreg);
  887. a_load_reg_reg(list,OS_ADDR,OS_ADDR,hreg,register);
  888. end
  889. else
  890. begin
  891. list.concat(taicpu.op_ref_reg(A_MOVE,opsize,href,register));
  892. { extend the value in the register }
  893. sign_extend(list, size, register);
  894. end;
  895. end;
  896. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  897. var
  898. href : treference;
  899. hreg : tregister;
  900. begin
  901. href:=ref;
  902. fixref(list, href);
  903. if not isaddressregister(r) then
  904. begin
  905. hreg:=getaddressregister(list);
  906. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  907. a_load_reg_reg(list, OS_ADDR, OS_ADDR, hreg, r);
  908. end
  909. else
  910. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  911. end;
  912. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  913. var
  914. instr : taicpu;
  915. begin
  916. instr:=taicpu.op_reg_reg(A_FMOVE,S_FX,reg1,reg2);
  917. add_move_instruction(instr);
  918. list.concat(instr);
  919. end;
  920. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  921. var
  922. opsize : topsize;
  923. href : treference;
  924. begin
  925. opsize := tcgsize2opsize[fromsize];
  926. { extended is not supported, since it is not available on Coldfire }
  927. if opsize = S_FX then
  928. internalerror(20020729);
  929. href := ref;
  930. fixref(list,href);
  931. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  932. end;
  933. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  934. var
  935. opsize : topsize;
  936. href : treference;
  937. begin
  938. opsize := tcgsize2opsize[tosize];
  939. { extended is not supported, since it is not available on Coldfire }
  940. if opsize = S_FX then
  941. internalerror(20020729);
  942. href := ref;
  943. fixref(list,href);
  944. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg,href));
  945. end;
  946. procedure tcg68k.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const reg : tregister;const cgpara : tcgpara);
  947. var
  948. ref : treference;
  949. begin
  950. if use_push(cgpara) and (current_settings.fputype in [fpu_68881]) then
  951. begin
  952. cgpara.check_simple_location;
  953. { FIXME: 68k cg really needs to support 2 byte stack alignment, otherwise the "Extended"
  954. floating point type cannot work (KB) }
  955. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  956. ref.direction := dir_dec;
  957. list.concat(taicpu.op_reg_ref(A_FMOVE,tcgsize2opsize[cgpara.location^.size],reg,ref));
  958. end
  959. else
  960. inherited a_loadfpu_reg_cgpara(list,size,reg,cgpara);
  961. end;
  962. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  963. var
  964. href : treference;
  965. fref : treference;
  966. freg : tregister;
  967. begin
  968. if current_settings.fputype = fpu_soft then
  969. case cgpara.location^.loc of
  970. LOC_REFERENCE,LOC_CREFERENCE:
  971. begin
  972. case size of
  973. OS_F64:
  974. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  975. OS_F32:
  976. a_load_ref_cgpara(list,size,ref,cgpara);
  977. else
  978. internalerror(2013021201);
  979. end;
  980. end;
  981. else
  982. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  983. end
  984. else
  985. if use_push(cgpara) and (current_settings.fputype in [fpu_68881]) then
  986. begin
  987. fref:=ref;
  988. fixref(list,fref);
  989. { fmove can't do <ea> -> <ea>, so move it to an fpreg first }
  990. freg:=getfpuregister(list,size);
  991. a_loadfpu_ref_reg(list,size,size,fref,freg);
  992. reference_reset_base(href, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  993. href.direction := dir_dec;
  994. list.concat(taicpu.op_reg_ref(A_FMOVE,tcgsize2opsize[cgpara.location^.size],freg,href));
  995. end
  996. else
  997. begin
  998. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara inherited')));
  999. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1000. end;
  1001. end;
  1002. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  1003. var
  1004. scratch_reg : tregister;
  1005. scratch_reg2: tregister;
  1006. opcode : tasmop;
  1007. begin
  1008. optimize_op_const(size, op, a);
  1009. opcode := topcg2tasmop[op];
  1010. case op of
  1011. OP_NONE :
  1012. begin
  1013. { Opcode is optimized away }
  1014. end;
  1015. OP_MOVE :
  1016. begin
  1017. { Optimized, replaced with a simple load }
  1018. a_load_const_reg(list,size,a,reg);
  1019. end;
  1020. OP_ADD,
  1021. OP_SUB:
  1022. begin
  1023. { add/sub works the same way, so have it unified here }
  1024. if (a >= 1) and (a <= 8) then
  1025. if (op = OP_ADD) then
  1026. opcode:=A_ADDQ
  1027. else
  1028. opcode:=A_SUBQ;
  1029. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  1030. end;
  1031. OP_AND,
  1032. OP_OR,
  1033. OP_XOR:
  1034. begin
  1035. scratch_reg := force_to_dataregister(list, size, reg);
  1036. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1037. move_if_needed(list, size, scratch_reg, reg);
  1038. end;
  1039. OP_DIV,
  1040. OP_IDIV:
  1041. begin
  1042. internalerror(20020816);
  1043. end;
  1044. OP_MUL,
  1045. OP_IMUL:
  1046. begin
  1047. { NOTE: better have this as fast as possible on every CPU in all cases,
  1048. because the compiler uses OP_IMUL for array indexing... (KB) }
  1049. { ColdFire doesn't support MULS/MULU <imm>,dX }
  1050. if current_settings.cputype in cpu_coldfire then
  1051. begin
  1052. { move const to a register first }
  1053. scratch_reg := getintregister(list,OS_INT);
  1054. a_load_const_reg(list, size, a, scratch_reg);
  1055. { do the multiplication }
  1056. scratch_reg2 := force_to_dataregister(list, size, reg);
  1057. sign_extend(list, size, scratch_reg2);
  1058. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  1059. { move the value back to the original register }
  1060. move_if_needed(list, size, scratch_reg2, reg);
  1061. end
  1062. else
  1063. begin
  1064. if current_settings.cputype = cpu_mc68020 then
  1065. begin
  1066. { do the multiplication }
  1067. scratch_reg := force_to_dataregister(list, size, reg);
  1068. sign_extend(list, size, scratch_reg);
  1069. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  1070. { move the value back to the original register }
  1071. move_if_needed(list, size, scratch_reg, reg);
  1072. end
  1073. else
  1074. { Fallback branch, plain 68000 for now }
  1075. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1076. if op = OP_MUL then
  1077. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1078. else
  1079. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1080. end;
  1081. end;
  1082. OP_ROL,
  1083. OP_ROR,
  1084. OP_SAR,
  1085. OP_SHL,
  1086. OP_SHR :
  1087. begin
  1088. scratch_reg := force_to_dataregister(list, size, reg);
  1089. sign_extend(list, size, scratch_reg);
  1090. { some special cases which can generate smarter code
  1091. using the SWAP instruction }
  1092. if (a = 16) then
  1093. begin
  1094. if (op = OP_SHL) then
  1095. begin
  1096. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1097. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1098. end
  1099. else if (op = OP_SHR) then
  1100. begin
  1101. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1102. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1103. end
  1104. else if (op = OP_SAR) then
  1105. begin
  1106. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1107. list.concat(taicpu.op_reg(A_EXT,S_L,scratch_reg));
  1108. end
  1109. else if (op = OP_ROR) or (op = OP_ROL) then
  1110. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg))
  1111. end
  1112. else if (a >= 1) and (a <= 8) then
  1113. begin
  1114. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1115. end
  1116. else if (a >= 9) and (a < 16) then
  1117. begin
  1118. { Use two ops instead of const -> reg + shift with reg, because
  1119. this way is the same in length and speed but has less register
  1120. pressure }
  1121. list.concat(taicpu.op_const_reg(opcode, S_L, 8, scratch_reg));
  1122. list.concat(taicpu.op_const_reg(opcode, S_L, a-8, scratch_reg));
  1123. end
  1124. else
  1125. begin
  1126. { move const to a register first }
  1127. scratch_reg2 := getintregister(list,OS_INT);
  1128. a_load_const_reg(list, size, a, scratch_reg2);
  1129. { do the operation }
  1130. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1131. end;
  1132. { move the value back to the original register }
  1133. move_if_needed(list, size, scratch_reg, reg);
  1134. end;
  1135. else
  1136. internalerror(20020729);
  1137. end;
  1138. end;
  1139. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1140. var
  1141. opcode: tasmop;
  1142. opsize: topsize;
  1143. href : treference;
  1144. begin
  1145. optimize_op_const(size, op, a);
  1146. opcode := topcg2tasmop[op];
  1147. opsize := TCGSize2OpSize[size];
  1148. { on ColdFire all arithmetic operations are only possible on 32bit }
  1149. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1150. and not (op in [OP_NONE,OP_MOVE])) then
  1151. begin
  1152. inherited;
  1153. exit;
  1154. end;
  1155. case op of
  1156. OP_NONE :
  1157. begin
  1158. { opcode was optimized away }
  1159. end;
  1160. OP_MOVE :
  1161. begin
  1162. { Optimized, replaced with a simple load }
  1163. a_load_const_ref(list,size,a,ref);
  1164. end;
  1165. OP_ADD,
  1166. OP_SUB :
  1167. begin
  1168. href:=ref;
  1169. fixref(list,href);
  1170. { add/sub works the same way, so have it unified here }
  1171. if (a >= 1) and (a <= 8) then
  1172. begin
  1173. if (op = OP_ADD) then
  1174. opcode:=A_ADDQ
  1175. else
  1176. opcode:=A_SUBQ;
  1177. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1178. end
  1179. else
  1180. if not(current_settings.cputype in cpu_coldfire) then
  1181. list.concat(taicpu.op_const_ref(opcode, opsize, a, href))
  1182. else
  1183. { on ColdFire, ADDI/SUBI cannot act on memory
  1184. so we can only go through a register }
  1185. inherited;
  1186. end;
  1187. else begin
  1188. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1189. inherited;
  1190. end;
  1191. end;
  1192. end;
  1193. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1194. var
  1195. hreg1, hreg2: tregister;
  1196. opcode : tasmop;
  1197. opsize : topsize;
  1198. begin
  1199. opcode := topcg2tasmop[op];
  1200. if current_settings.cputype in cpu_coldfire then
  1201. opsize := S_L
  1202. else
  1203. opsize := TCGSize2OpSize[size];
  1204. case op of
  1205. OP_ADD,
  1206. OP_SUB:
  1207. begin
  1208. if current_settings.cputype in cpu_coldfire then
  1209. begin
  1210. { operation only allowed only a longword }
  1211. sign_extend(list, size, src);
  1212. sign_extend(list, size, dst);
  1213. end;
  1214. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1215. end;
  1216. OP_AND,OP_OR,
  1217. OP_SAR,OP_SHL,
  1218. OP_SHR,OP_XOR:
  1219. begin
  1220. { load to data registers }
  1221. hreg1 := force_to_dataregister(list, size, src);
  1222. hreg2 := force_to_dataregister(list, size, dst);
  1223. if current_settings.cputype in cpu_coldfire then
  1224. begin
  1225. { operation only allowed only a longword }
  1226. {!***************************************
  1227. in the case of shifts, the value to
  1228. shift by, should already be valid, so
  1229. no need to sign extend the value
  1230. !
  1231. }
  1232. if op in [OP_AND,OP_OR,OP_XOR] then
  1233. sign_extend(list, size, hreg1);
  1234. sign_extend(list, size, hreg2);
  1235. end;
  1236. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1237. { move back result into destination register }
  1238. move_if_needed(list, size, hreg2, dst);
  1239. end;
  1240. OP_DIV,
  1241. OP_IDIV :
  1242. begin
  1243. internalerror(20020816);
  1244. end;
  1245. OP_MUL,
  1246. OP_IMUL:
  1247. begin
  1248. if (current_settings.cputype <> cpu_mc68020) and
  1249. (not (current_settings.cputype in cpu_coldfire)) then
  1250. if op = OP_MUL then
  1251. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1252. else
  1253. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1254. else
  1255. begin
  1256. { 68020+ and ColdFire codepath, probably could be improved }
  1257. hreg1 := force_to_dataregister(list, size, src);
  1258. hreg2 := force_to_dataregister(list, size, dst);
  1259. sign_extend(list, size, hreg1);
  1260. sign_extend(list, size, hreg2);
  1261. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1262. { move back result into destination register }
  1263. move_if_needed(list, size, hreg2, dst);
  1264. end;
  1265. end;
  1266. OP_NEG,
  1267. OP_NOT :
  1268. begin
  1269. { if there are two operands, move the register,
  1270. since the operation will only be done on the result
  1271. register. }
  1272. if (src<>dst) then
  1273. a_load_reg_reg(list,size,size,src,dst);
  1274. hreg2 := force_to_dataregister(list, size, dst);
  1275. { coldfire only supports long version }
  1276. if current_settings.cputype in cpu_ColdFire then
  1277. sign_extend(list, size, hreg2);
  1278. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1279. { move back the result to the result register if needed }
  1280. move_if_needed(list, size, hreg2, dst);
  1281. end;
  1282. else
  1283. internalerror(20020729);
  1284. end;
  1285. end;
  1286. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1287. var
  1288. opcode : tasmop;
  1289. opsize : topsize;
  1290. href : treference;
  1291. hreg : tregister;
  1292. begin
  1293. opcode := topcg2tasmop[op];
  1294. opsize := TCGSize2OpSize[size];
  1295. { on ColdFire all arithmetic operations are only possible on 32bit
  1296. and addressing modes are limited }
  1297. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1298. begin
  1299. inherited;
  1300. exit;
  1301. end;
  1302. case op of
  1303. OP_ADD,
  1304. OP_SUB :
  1305. begin
  1306. href:=ref;
  1307. fixref(list,href);
  1308. { areg -> ref arithmetic operations are impossible on 68k }
  1309. hreg:=force_to_dataregister(list,size,reg);
  1310. { add/sub works the same way, so have it unified here }
  1311. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1312. end;
  1313. else begin
  1314. // list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited')));
  1315. inherited;
  1316. end;
  1317. end;
  1318. end;
  1319. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1320. l : tasmlabel);
  1321. var
  1322. hregister : tregister;
  1323. instr : taicpu;
  1324. need_temp_reg : boolean;
  1325. temp_size: topsize;
  1326. begin
  1327. need_temp_reg := false;
  1328. { plain 68000 doesn't support address registers for TST }
  1329. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1330. (a = 0) and isaddressregister(reg);
  1331. { ColdFire doesn't support address registers for CMPI }
  1332. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1333. and (a <> 0) and isaddressregister(reg));
  1334. if need_temp_reg then
  1335. begin
  1336. hregister := getintregister(list,OS_INT);
  1337. temp_size := TCGSize2OpSize[size];
  1338. if temp_size < S_W then
  1339. temp_size := S_W;
  1340. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1341. add_move_instruction(instr);
  1342. list.concat(instr);
  1343. reg := hregister;
  1344. { do sign extension if size had to be modified }
  1345. if temp_size <> TCGSize2OpSize[size] then
  1346. begin
  1347. sign_extend(list, size, reg);
  1348. size:=OS_INT;
  1349. end;
  1350. end;
  1351. if a = 0 then
  1352. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1353. else
  1354. begin
  1355. { ColdFire ISA A also needs S_L for CMPI }
  1356. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1357. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1358. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1359. default. (KB) }
  1360. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c]} then
  1361. begin
  1362. sign_extend(list, size, reg);
  1363. size:=OS_INT;
  1364. end;
  1365. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1366. end;
  1367. { emit the actual jump to the label }
  1368. a_jmp_cond(list,cmp_op,l);
  1369. end;
  1370. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1371. var
  1372. tmpref: treference;
  1373. begin
  1374. { optimize for usage of TST here, so ref compares against zero, which is the
  1375. most common case by far in the RTL code at least (KB) }
  1376. if (a = 0) then
  1377. begin
  1378. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1379. tmpref:=ref;
  1380. fixref(list,tmpref);
  1381. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1382. a_jmp_cond(list,cmp_op,l);
  1383. end
  1384. else
  1385. begin
  1386. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1387. inherited;
  1388. end;
  1389. end;
  1390. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1391. begin
  1392. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c]) then
  1393. begin
  1394. sign_extend(list,size,reg1);
  1395. sign_extend(list,size,reg2);
  1396. size:=OS_INT;
  1397. end;
  1398. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1399. { emit the actual jump to the label }
  1400. a_jmp_cond(list,cmp_op,l);
  1401. end;
  1402. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1403. var
  1404. ai: taicpu;
  1405. begin
  1406. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s));
  1407. ai.is_jmp := true;
  1408. list.concat(ai);
  1409. end;
  1410. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1411. var
  1412. ai: taicpu;
  1413. begin
  1414. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1415. ai.is_jmp := true;
  1416. list.concat(ai);
  1417. end;
  1418. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1419. var
  1420. ai : taicpu;
  1421. begin
  1422. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  1423. ai.SetCondition(flags_to_cond(f));
  1424. ai.is_jmp := true;
  1425. list.concat(ai);
  1426. end;
  1427. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1428. var
  1429. ai : taicpu;
  1430. hreg : tregister;
  1431. instr : taicpu;
  1432. begin
  1433. { move to a Dx register? }
  1434. if (isaddressregister(reg)) then
  1435. hreg:=getintregister(list,OS_INT)
  1436. else
  1437. hreg:=reg;
  1438. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  1439. ai.SetCondition(flags_to_cond(f));
  1440. list.concat(ai);
  1441. { Scc stores a complete byte of 1s, but the compiler expects only one
  1442. bit set, so ensure this is the case }
  1443. list.concat(taicpu.op_const_reg(A_AND,S_L,1,hreg));
  1444. if hreg<>reg then
  1445. begin
  1446. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg);
  1447. add_move_instruction(instr);
  1448. list.concat(instr);
  1449. end;
  1450. end;
  1451. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1452. var
  1453. helpsize : longint;
  1454. i : byte;
  1455. hregister : tregister;
  1456. iregister : tregister;
  1457. jregister : tregister;
  1458. hp1 : treference;
  1459. hp2 : treference;
  1460. hl : tasmlabel;
  1461. srcref,dstref : treference;
  1462. begin
  1463. hregister := getintregister(list,OS_INT);
  1464. { from 12 bytes movs is being used }
  1465. if ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=12))) then
  1466. begin
  1467. srcref := source;
  1468. dstref := dest;
  1469. helpsize:=len div 4;
  1470. { move a dword x times }
  1471. for i:=1 to helpsize do
  1472. begin
  1473. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  1474. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  1475. inc(srcref.offset,4);
  1476. inc(dstref.offset,4);
  1477. dec(len,4);
  1478. end;
  1479. { move a word }
  1480. if len>1 then
  1481. begin
  1482. a_load_ref_reg(list,OS_16,OS_16,srcref,hregister);
  1483. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  1484. inc(srcref.offset,2);
  1485. inc(dstref.offset,2);
  1486. dec(len,2);
  1487. end;
  1488. { move a single byte }
  1489. if len>0 then
  1490. begin
  1491. a_load_ref_reg(list,OS_8,OS_8,srcref,hregister);
  1492. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  1493. end
  1494. end
  1495. else
  1496. begin
  1497. iregister:=getaddressregister(list);
  1498. jregister:=getaddressregister(list);
  1499. { reference for move (An)+,(An)+ }
  1500. reference_reset(hp1,source.alignment);
  1501. hp1.base := iregister; { source register }
  1502. hp1.direction := dir_inc;
  1503. reference_reset(hp2,dest.alignment);
  1504. hp2.base := jregister;
  1505. hp2.direction := dir_inc;
  1506. { iregister = source }
  1507. { jregister = destination }
  1508. a_loadaddr_ref_reg(list,source,iregister);
  1509. a_loadaddr_ref_reg(list,dest,jregister);
  1510. { double word move only on 68020+ machines }
  1511. { because of possible alignment problems }
  1512. { use fast loop mode }
  1513. if (current_settings.cputype=cpu_MC68020) then
  1514. begin
  1515. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1516. helpsize := len - len mod 4;
  1517. len := len mod 4;
  1518. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1519. current_asmdata.getjumplabel(hl);
  1520. a_label(list,hl);
  1521. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  1522. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1523. if len > 1 then
  1524. begin
  1525. dec(len,2);
  1526. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  1527. end;
  1528. if len = 1 then
  1529. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1530. end
  1531. else
  1532. begin
  1533. { Fast 68010 loop mode with no possible alignment problems }
  1534. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1535. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1536. current_asmdata.getjumplabel(hl);
  1537. a_label(list,hl);
  1538. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1539. if current_settings.cputype in cpu_coldfire then
  1540. begin
  1541. { Coldfire does not support DBRA }
  1542. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1543. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1544. end
  1545. else
  1546. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1547. end;
  1548. end;
  1549. end;
  1550. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1551. var
  1552. hl : tasmlabel;
  1553. ai : taicpu;
  1554. cond : TAsmCond;
  1555. begin
  1556. if not(cs_check_overflow in current_settings.localswitches) then
  1557. exit;
  1558. current_asmdata.getjumplabel(hl);
  1559. if not ((def.typ=pointerdef) or
  1560. ((def.typ=orddef) and
  1561. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1562. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1563. cond:=C_VC
  1564. else
  1565. cond:=C_CC;
  1566. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1567. ai.SetCondition(cond);
  1568. ai.is_jmp:=true;
  1569. list.concat(ai);
  1570. a_call_name(list,'FPC_OVERFLOW',false);
  1571. a_label(list,hl);
  1572. end;
  1573. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1574. begin
  1575. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1576. However, a LINK seems faster than two moves on everything from 68000
  1577. to '060, so the two move branch here was dropped. (KB) }
  1578. if not nostackframe then
  1579. begin
  1580. { size can't be negative }
  1581. if (localsize < 0) then
  1582. internalerror(2006122601);
  1583. if (localsize > high(smallint)) then
  1584. begin
  1585. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1586. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1587. end
  1588. else
  1589. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1590. end;
  1591. end;
  1592. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1593. var
  1594. r,hregister : TRegister;
  1595. ref : TReference;
  1596. ref2: TReference;
  1597. begin
  1598. if not nostackframe then
  1599. begin
  1600. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1601. { if parasize is less than zero here, we probably have a cdecl function.
  1602. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1603. 68k GCC uses two different methods to free the stack, depending if the target
  1604. architecture supports RTD or not, and one does callee side, the other does
  1605. caller side free, which looks like a PITA to support. We have to figure this
  1606. out later. More info welcomed. (KB) }
  1607. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1608. begin
  1609. if current_settings.cputype=cpu_mc68020 then
  1610. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1611. else
  1612. begin
  1613. { We must pull the PC Counter from the stack, before }
  1614. { restoring the stack pointer, otherwise the PC would }
  1615. { point to nowhere! }
  1616. { Instead of doing a slow copy of the return address while trying }
  1617. { to feed it to the RTS instruction, load the PC to A0 (scratch reg) }
  1618. { then free up the stack allocated for paras, then use a JMP (A0) to }
  1619. { return to the caller with the paras freed. (KB) }
  1620. hregister:=NR_A0;
  1621. cg.a_reg_alloc(list,hregister);
  1622. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1623. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1624. { instead of using a postincrement above (which also writes the }
  1625. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1626. { below then take that size into account as well, so SP reg is only }
  1627. { written once (KB) }
  1628. parasize:=parasize+4;
  1629. r:=NR_SP;
  1630. { can we do a quick addition ... }
  1631. if (parasize < 9) then
  1632. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1633. else { nope ... }
  1634. begin
  1635. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,4);
  1636. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1637. end;
  1638. reference_reset_base(ref,hregister,0,4);
  1639. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1640. end;
  1641. end
  1642. else
  1643. list.concat(taicpu.op_none(A_RTS,S_NO));
  1644. end
  1645. else
  1646. begin
  1647. list.concat(taicpu.op_none(A_RTS,S_NO));
  1648. end;
  1649. { Routines with the poclearstack flag set use only a ret.
  1650. also routines with parasize=0 }
  1651. { TODO: figure out if these are still relevant to us (KB) }
  1652. (*
  1653. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1654. begin
  1655. { complex return values are removed from stack in C code PM }
  1656. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1657. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1658. else
  1659. list.concat(taicpu.op_none(A_RTS,S_NO));
  1660. end
  1661. else if (parasize=0) then
  1662. begin
  1663. list.concat(taicpu.op_none(A_RTS,S_NO));
  1664. end
  1665. else
  1666. *)
  1667. end;
  1668. procedure tcg68k.g_save_registers(list:TAsmList);
  1669. var
  1670. dataregs: tcpuregisterset;
  1671. addrregs: tcpuregisterset;
  1672. fpuregs: tcpuregisterset;
  1673. href : treference;
  1674. hreg : tregister;
  1675. hfreg : tregister;
  1676. size : longint;
  1677. fsize : longint;
  1678. r : integer;
  1679. begin
  1680. { The code generated by the section below, particularly the movem.l
  1681. instruction is known to cause an issue when compiled by some GNU
  1682. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1683. when you run into this problem, just call inherited here instead
  1684. to skip the movem.l generation. But better just use working GNU
  1685. AS version instead. (KB) }
  1686. dataregs:=[];
  1687. addrregs:=[];
  1688. fpuregs:=[];
  1689. { calculate temp. size }
  1690. size:=0;
  1691. fsize:=0;
  1692. hreg:=NR_NO;
  1693. hfreg:=NR_NO;
  1694. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1695. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1696. begin
  1697. hreg:=newreg(R_INTREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1698. inc(size,sizeof(aint));
  1699. dataregs:=dataregs + [saved_standard_registers[r]];
  1700. end;
  1701. if uses_registers(R_ADDRESSREGISTER) then
  1702. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1703. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1704. begin
  1705. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1706. inc(size,sizeof(aint));
  1707. addrregs:=addrregs + [saved_address_registers[r]];
  1708. end;
  1709. if uses_registers(R_FPUREGISTER) then
  1710. for r:=low(saved_fpu_registers) to high(saved_fpu_registers) do
  1711. if saved_fpu_registers[r] in rg[R_FPUREGISTER].used_in_proc then
  1712. begin
  1713. hfreg:=newreg(R_FPUREGISTER,saved_fpu_registers[r],R_SUBWHOLE);
  1714. inc(fsize,12{sizeof(extended)});
  1715. fpuregs:=fpuregs + [saved_fpu_registers[r]];
  1716. end;
  1717. { 68k has no MM registers }
  1718. if uses_registers(R_MMREGISTER) then
  1719. internalerror(2014030201);
  1720. if (size+fsize) > 0 then
  1721. begin
  1722. tg.GetTemp(list,size+fsize,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1723. include(current_procinfo.flags,pi_has_saved_regs);
  1724. { Copy registers to temp }
  1725. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1726. href:=current_procinfo.save_regs_ref;
  1727. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1728. begin
  1729. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1730. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1731. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1732. end;
  1733. if size > 0 then
  1734. if size = sizeof(aint) then
  1735. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1736. else
  1737. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,[],href));
  1738. if fsize > 0 then
  1739. begin
  1740. { size is always longword aligned, while fsize is not }
  1741. inc(href.offset,size);
  1742. if fsize = 12{sizeof(extended)} then
  1743. list.concat(taicpu.op_reg_ref(A_FMOVE,S_FX,hfreg,href))
  1744. else
  1745. list.concat(taicpu.op_regset_ref(A_FMOVEM,S_FX,[],[],fpuregs,href));
  1746. end;
  1747. end;
  1748. end;
  1749. procedure tcg68k.g_restore_registers(list:TAsmList);
  1750. var
  1751. dataregs: tcpuregisterset;
  1752. addrregs: tcpuregisterset;
  1753. fpuregs : tcpuregisterset;
  1754. href : treference;
  1755. r : integer;
  1756. hreg : tregister;
  1757. hfreg : tregister;
  1758. size : longint;
  1759. fsize : longint;
  1760. begin
  1761. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1762. dataregs:=[];
  1763. addrregs:=[];
  1764. fpuregs:=[];
  1765. if not(pi_has_saved_regs in current_procinfo.flags) then
  1766. exit;
  1767. { Copy registers from temp }
  1768. size:=0;
  1769. fsize:=0;
  1770. hreg:=NR_NO;
  1771. hfreg:=NR_NO;
  1772. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1773. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1774. begin
  1775. inc(size,sizeof(aint));
  1776. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1777. { Allocate register so the optimizer does not remove the load }
  1778. a_reg_alloc(list,hreg);
  1779. dataregs:=dataregs + [saved_standard_registers[r]];
  1780. end;
  1781. if uses_registers(R_ADDRESSREGISTER) then
  1782. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1783. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1784. begin
  1785. inc(size,sizeof(aint));
  1786. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1787. { Allocate register so the optimizer does not remove the load }
  1788. a_reg_alloc(list,hreg);
  1789. addrregs:=addrregs + [saved_address_registers[r]];
  1790. end;
  1791. if uses_registers(R_FPUREGISTER) then
  1792. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1793. if saved_fpu_registers[r] in rg[R_FPUREGISTER].used_in_proc then
  1794. begin
  1795. inc(fsize,12{sizeof(extended)});
  1796. hfreg:=newreg(R_FPUREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1797. { Allocate register so the optimizer does not remove the load }
  1798. a_reg_alloc(list,hfreg);
  1799. fpuregs:=fpuregs + [saved_fpu_registers[r]];
  1800. end;
  1801. { 68k has no MM registers }
  1802. if uses_registers(R_MMREGISTER) then
  1803. internalerror(2014030202);
  1804. { Restore registers from temp }
  1805. href:=current_procinfo.save_regs_ref;
  1806. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1807. begin
  1808. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1809. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1810. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1811. end;
  1812. if size > 0 then
  1813. if size = sizeof(aint) then
  1814. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  1815. else
  1816. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs,[]));
  1817. if fsize > 0 then
  1818. begin
  1819. { size is always longword aligned, while fsize is not }
  1820. inc(href.offset,size);
  1821. if fsize = 12{sizeof(extended)} then
  1822. list.concat(taicpu.op_ref_reg(A_FMOVE,S_FX,href,hfreg))
  1823. else
  1824. list.concat(taicpu.op_ref_regset(A_FMOVEM,S_FX,href,[],[],fpuregs));
  1825. end;
  1826. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1827. end;
  1828. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1829. begin
  1830. case _newsize of
  1831. OS_S16, OS_16:
  1832. case _oldsize of
  1833. OS_S8:
  1834. begin { 8 -> 16 bit sign extend }
  1835. if (isaddressregister(reg)) then
  1836. internalerror(2014031201);
  1837. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1838. end;
  1839. OS_8: { 8 -> 16 bit zero extend }
  1840. begin
  1841. if (current_settings.cputype in cpu_coldfire) then
  1842. { ColdFire has no ANDI.W }
  1843. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  1844. else
  1845. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  1846. end;
  1847. end;
  1848. OS_S32, OS_32:
  1849. case _oldsize of
  1850. OS_S8:
  1851. begin { 8 -> 32 bit sign extend }
  1852. if (isaddressregister(reg)) then
  1853. internalerror(2014031202);
  1854. if (current_settings.cputype = cpu_MC68000) then
  1855. begin
  1856. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1857. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1858. end
  1859. else
  1860. begin
  1861. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  1862. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1863. end;
  1864. end;
  1865. OS_8: { 8 -> 32 bit zero extend }
  1866. begin
  1867. if (isaddressregister(reg)) then
  1868. internalerror(2015031501);
  1869. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1870. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1871. end;
  1872. OS_S16: { 16 -> 32 bit sign extend }
  1873. begin
  1874. { address registers are sign-extended from 16->32 bit anyway
  1875. automagically on every W operation by the CPU, so this is a NOP }
  1876. if not isaddressregister(reg) then
  1877. begin
  1878. //list.concat(tai_comment.create(strpnew('sign extend word')));
  1879. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1880. end;
  1881. end;
  1882. OS_16:
  1883. begin
  1884. if (isaddressregister(reg)) then
  1885. internalerror(2015031502);
  1886. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1887. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1888. end;
  1889. end;
  1890. end; { otherwise the size is already correct }
  1891. end;
  1892. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1893. begin
  1894. sign_extend(list, _oldsize, OS_INT, reg);
  1895. end;
  1896. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1897. var
  1898. ai : taicpu;
  1899. begin
  1900. if cond=OC_None then
  1901. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1902. else
  1903. begin
  1904. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1905. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1906. end;
  1907. ai.is_jmp:=true;
  1908. list.concat(ai);
  1909. end;
  1910. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  1911. operations on an address register. if the register is a dataregister anyway, it
  1912. just returns it untouched.}
  1913. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  1914. var
  1915. scratch_reg: TRegister;
  1916. instr: Taicpu;
  1917. begin
  1918. if isaddressregister(reg) then
  1919. begin
  1920. scratch_reg:=getintregister(list,OS_INT);
  1921. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  1922. add_move_instruction(instr);
  1923. list.concat(instr);
  1924. result:=scratch_reg;
  1925. end
  1926. else
  1927. result:=reg;
  1928. end;
  1929. { moves source register to destination register, if the two are not the same. can be used in pair
  1930. with force_to_dataregister() }
  1931. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  1932. var
  1933. instr: Taicpu;
  1934. begin
  1935. if (src <> dest) then
  1936. begin
  1937. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  1938. add_move_instruction(instr);
  1939. list.concat(instr);
  1940. end;
  1941. end;
  1942. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1943. var
  1944. hsym : tsym;
  1945. href : treference;
  1946. paraloc : Pcgparalocation;
  1947. begin
  1948. { calculate the parameter info for the procdef }
  1949. procdef.init_paraloc_info(callerside);
  1950. hsym:=tsym(procdef.parast.Find('self'));
  1951. if not(assigned(hsym) and
  1952. (hsym.typ=paravarsym)) then
  1953. internalerror(2013100702);
  1954. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1955. while paraloc<>nil do
  1956. with paraloc^ do
  1957. begin
  1958. case loc of
  1959. LOC_REGISTER:
  1960. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  1961. LOC_REFERENCE:
  1962. begin
  1963. { offset in the wrapper needs to be adjusted for the stored
  1964. return address }
  1965. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  1966. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  1967. and it's probably smaller code for the majority of cases (if ioffset small, the
  1968. load will use MOVEQ) (KB) }
  1969. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  1970. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  1971. end
  1972. else
  1973. internalerror(2013100703);
  1974. end;
  1975. paraloc:=next;
  1976. end;
  1977. end;
  1978. procedure tcg68k.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1979. procedure getselftoa0(offs:longint);
  1980. var
  1981. href : treference;
  1982. selfoffsetfromsp : longint;
  1983. begin
  1984. { move.l offset(%sp),%a0 }
  1985. { framepointer is pushed for nested procs }
  1986. if procdef.parast.symtablelevel>normal_function_level then
  1987. selfoffsetfromsp:=sizeof(aint)
  1988. else
  1989. selfoffsetfromsp:=0;
  1990. reference_reset_base(href,NR_SP,selfoffsetfromsp+offs,4);
  1991. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  1992. end;
  1993. procedure loadvmttoa0;
  1994. var
  1995. href : treference;
  1996. begin
  1997. { move.l (%a0),%a0 ; load vmt}
  1998. reference_reset_base(href,NR_A0,0,4);
  1999. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  2000. end;
  2001. procedure op_ona0methodaddr;
  2002. var
  2003. href : treference;
  2004. begin
  2005. if (procdef.extnumber=$ffff) then
  2006. Internalerror(2013100701);
  2007. reference_reset_base(href,NR_A0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),4);
  2008. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,NR_A0));
  2009. reference_reset_base(href,NR_A0,0,4);
  2010. list.concat(taicpu.op_ref(A_JMP,S_NO,href));
  2011. end;
  2012. var
  2013. make_global : boolean;
  2014. begin
  2015. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  2016. Internalerror(200006137);
  2017. if not assigned(procdef.struct) or
  2018. (procdef.procoptions*[po_classmethod, po_staticmethod,
  2019. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  2020. Internalerror(200006138);
  2021. if procdef.owner.symtabletype<>ObjectSymtable then
  2022. Internalerror(200109191);
  2023. make_global:=false;
  2024. if (not current_module.is_unit) or
  2025. create_smartlink or
  2026. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  2027. make_global:=true;
  2028. if make_global then
  2029. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  2030. else
  2031. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  2032. { set param1 interface to self }
  2033. g_adjust_self_value(list,procdef,ioffset);
  2034. { case 4 }
  2035. if (po_virtualmethod in procdef.procoptions) and
  2036. not is_objectpascal_helper(procdef.struct) then
  2037. begin
  2038. getselftoa0(4);
  2039. loadvmttoa0;
  2040. op_ona0methodaddr;
  2041. end
  2042. { case 0 }
  2043. else
  2044. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  2045. List.concat(Tai_symbol_end.Createname(labelname));
  2046. end;
  2047. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2048. begin
  2049. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  2050. end;
  2051. {****************************************************************************}
  2052. { TCG64F68K }
  2053. {****************************************************************************}
  2054. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  2055. var
  2056. opcode : tasmop;
  2057. xopcode : tasmop;
  2058. instr : taicpu;
  2059. begin
  2060. opcode := topcg2tasmop[op];
  2061. xopcode := topcg2tasmopx[op];
  2062. case op of
  2063. OP_ADD,OP_SUB:
  2064. begin
  2065. { if one of these three registers is an address
  2066. register, we'll really get into problems! }
  2067. if isaddressregister(regdst.reglo) or
  2068. isaddressregister(regdst.reghi) or
  2069. isaddressregister(regsrc.reghi) then
  2070. internalerror(2014030101);
  2071. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  2072. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  2073. end;
  2074. OP_AND,OP_OR:
  2075. begin
  2076. { at least one of the registers must be a data register }
  2077. if (isaddressregister(regdst.reglo) and
  2078. isaddressregister(regsrc.reglo)) or
  2079. (isaddressregister(regsrc.reghi) and
  2080. isaddressregister(regdst.reghi)) then
  2081. internalerror(2014030102);
  2082. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2083. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2084. end;
  2085. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  2086. OP_IDIV,OP_DIV,
  2087. OP_IMUL,OP_MUL:
  2088. internalerror(2002081701);
  2089. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  2090. OP_SAR,OP_SHL,OP_SHR:
  2091. internalerror(2002081702);
  2092. OP_XOR:
  2093. begin
  2094. if isaddressregister(regdst.reglo) or
  2095. isaddressregister(regsrc.reglo) or
  2096. isaddressregister(regsrc.reghi) or
  2097. isaddressregister(regdst.reghi) then
  2098. internalerror(2014030103);
  2099. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2100. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2101. end;
  2102. OP_NEG,OP_NOT:
  2103. begin
  2104. if isaddressregister(regdst.reglo) or
  2105. isaddressregister(regdst.reghi) then
  2106. internalerror(2014030104);
  2107. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  2108. cg.add_move_instruction(instr);
  2109. list.concat(instr);
  2110. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  2111. cg.add_move_instruction(instr);
  2112. list.concat(instr);
  2113. if (op = OP_NOT) then
  2114. xopcode:=opcode;
  2115. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  2116. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  2117. end;
  2118. end; { end case }
  2119. end;
  2120. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  2121. var
  2122. tempref : treference;
  2123. begin
  2124. case op of
  2125. OP_NEG,OP_NOT:
  2126. begin
  2127. a_load64_ref_reg(list,ref,reg);
  2128. a_op64_reg_reg(list,op,size,reg,reg);
  2129. end;
  2130. OP_AND,OP_OR:
  2131. begin
  2132. tempref:=ref;
  2133. tcg68k(cg).fixref(list,tempref);
  2134. inc(tempref.offset,4);
  2135. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reglo));
  2136. dec(tempref.offset,4);
  2137. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reghi));
  2138. end;
  2139. else
  2140. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  2141. high dword, although low dword can still be handled directly. }
  2142. inherited a_op64_ref_reg(list,op,size,ref,reg);
  2143. end;
  2144. end;
  2145. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  2146. var
  2147. lowvalue : cardinal;
  2148. highvalue : cardinal;
  2149. opcode : tasmop;
  2150. xopcode : tasmop;
  2151. hreg : tregister;
  2152. begin
  2153. { is it optimized out ? }
  2154. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  2155. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  2156. exit; }
  2157. lowvalue := cardinal(value);
  2158. highvalue := value shr 32;
  2159. opcode := topcg2tasmop[op];
  2160. xopcode := topcg2tasmopx[op];
  2161. { the destination registers must be data registers }
  2162. if isaddressregister(regdst.reglo) or
  2163. isaddressregister(regdst.reghi) then
  2164. internalerror(2014030105);
  2165. case op of
  2166. OP_ADD,OP_SUB:
  2167. begin
  2168. hreg:=cg.getintregister(list,OS_INT);
  2169. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2170. cg.a_load_const_reg(list,OS_S32,longint(highvalue),hreg);
  2171. { don't use cg.a_op_const_reg() here, because a possible optimized
  2172. ADDQ/SUBQ wouldn't set the eXtend bit }
  2173. list.concat(taicpu.op_const_reg(opcode,S_L,lowvalue,regdst.reglo));
  2174. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2175. end;
  2176. OP_AND,OP_OR,OP_XOR:
  2177. begin
  2178. cg.a_op_const_reg(list,op,OS_S32,longint(lowvalue),regdst.reglo);
  2179. cg.a_op_const_reg(list,op,OS_S32,longint(highvalue),regdst.reghi);
  2180. end;
  2181. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2182. OP_IDIV,OP_DIV,
  2183. OP_IMUL,OP_MUL:
  2184. internalerror(2002081701);
  2185. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2186. OP_SAR,OP_SHL,OP_SHR:
  2187. internalerror(2002081702);
  2188. { these should have been handled already by earlier passes }
  2189. OP_NOT,OP_NEG:
  2190. internalerror(2012110403);
  2191. end; { end case }
  2192. end;
  2193. procedure create_codegen;
  2194. begin
  2195. cg := tcg68k.create;
  2196. cg64 :=tcg64f68k.create;
  2197. end;
  2198. end.