cgcpu.pas 94 KB

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