2
0

cgcpu.pas 79 KB

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