cgcpu.pas 79 KB

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