cpubase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. {
  2. Copyright (c) 2006 by Florian Klaempfl
  3. Contains the base types for the AVR
  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. {# Base unit for processor information. This unit contains
  18. enumerations of registers, opcodes, sizes, and other
  19. such things which are processor specific.
  20. }
  21. unit cpubase;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cutils,cclasses,
  26. globtype,globals,
  27. cpuinfo,
  28. aasmbase,
  29. cgbase
  30. ;
  31. {*****************************************************************************
  32. Assembler Opcodes
  33. *****************************************************************************}
  34. type
  35. TAsmOp=(A_None,
  36. A_ADD,A_ADC,A_ADIW,A_SUB,A_SUBI,A_SBC,A_SBCI,A_SBIW,A_AND,A_ANDI,
  37. A_OR,A_ORI,A_EOR,A_COM,A_NEG,A_SBR,A_CBR,A_INC,A_DEC,A_TST,A_CLR,
  38. A_SER,A_MUL,A_MULS,A_FMUL,A_FMULS,A_FMULSU,A_RJMP,A_IJMP,
  39. A_EIJMP,A_JMP,A_RCALL,A_ICALL,R_EICALL,A_CALL,A_RET,A_RETI,A_CPSE,
  40. A_CP,A_CPC,A_CPI,A_SBxx,A_BRxx,A_MOV,A_MOVW,A_LDI,A_LDS,A_LD,A_LDD,
  41. A_STS,A_ST,A_STD,A_LPM,A_ELPM,A_SPM,A_IN,A_OUT,A_PUSH,A_POP,
  42. A_LSL,A_LSR,A_ROL,A_ROR,A_ASR,A_SWAP,A_BSET,A_BCLR,A_SBI,A_CBI,
  43. A_BST,A_BLD,A_Sxx,A_Cxx,A_BRAK,A_NOP,A_SLEEP,A_WDR);
  44. { This should define the array of instructions as string }
  45. op2strtable=array[tasmop] of string[11];
  46. const
  47. { First value of opcode enumeration }
  48. firstop = low(tasmop);
  49. { Last value of opcode enumeration }
  50. lastop = high(tasmop);
  51. jmp_instructions = [A_BRxx,A_SBxx,A_JMP,A_RCALL,A_ICALL,A_EIJMP,A_RJMP,A_CALL,A_RET,A_RETI,A_CPSE,A_IJMP];
  52. {*****************************************************************************
  53. Registers
  54. *****************************************************************************}
  55. type
  56. { Number of registers used for indexing in tables }
  57. tregisterindex=0..{$i ravrnor.inc}-1;
  58. const
  59. { Available Superregisters }
  60. {$i ravrsup.inc}
  61. { No Subregisters }
  62. R_SUBWHOLE = R_SUBNONE;
  63. { Available Registers }
  64. {$i ravrcon.inc}
  65. NR_XLO = NR_R26;
  66. NR_XHI = NR_R27;
  67. NR_YLO = NR_R28;
  68. NR_YHI = NR_R29;
  69. NR_ZLO = NR_R30;
  70. NR_ZHI = NR_R31;
  71. { Integer Super registers first and last }
  72. first_int_supreg = RS_R0;
  73. first_int_imreg = $10;
  74. { Float Super register first and last }
  75. first_fpu_supreg = RS_INVALID;
  76. first_fpu_imreg = RS_INVALID;
  77. { MM Super register first and last }
  78. first_mm_supreg = RS_INVALID;
  79. first_mm_imreg = RS_INVALID;
  80. { TODO: Calculate bsstart}
  81. regnumber_count_bsstart = 64;
  82. regnumber_table : array[tregisterindex] of tregister = (
  83. {$i ravrnum.inc}
  84. );
  85. regstabs_table : array[tregisterindex] of shortint = (
  86. {$i ravrsta.inc}
  87. );
  88. regdwarf_table : array[tregisterindex] of shortint = (
  89. {$i ravrdwa.inc}
  90. );
  91. { registers which may be destroyed by calls }
  92. VOLATILE_INTREGISTERS = [RS_R18..RS_R27,RS_R30..RS_R31];
  93. VOLATILE_FPUREGISTERS = [];
  94. type
  95. totherregisterset = set of tregisterindex;
  96. {*****************************************************************************
  97. Conditions
  98. *****************************************************************************}
  99. type
  100. TAsmCond=(C_None,
  101. C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  102. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  103. );
  104. const
  105. cond2str : array[TAsmCond] of string[2]=('',
  106. 'eq','ne','cs','cc','mi','pl','vs','vc','hi','ls',
  107. 'ge','lt','gt','le','al','nv'
  108. );
  109. uppercond2str : array[TAsmCond] of string[2]=('',
  110. 'EQ','NE','CS','CC','MI','PL','VS','VC','HI','LS',
  111. 'GE','LT','GT','LE','AL','NV'
  112. );
  113. {*****************************************************************************
  114. Flags
  115. *****************************************************************************}
  116. type
  117. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  118. F_GE,F_LT,F_GT,F_LE);
  119. {*****************************************************************************
  120. Operands
  121. *****************************************************************************}
  122. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  123. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  124. tupdatereg = (UR_None,UR_Update);
  125. pshifterop = ^tshifterop;
  126. tshifterop = record
  127. shiftmode : tshiftmode;
  128. rs : tregister;
  129. shiftimm : byte;
  130. end;
  131. {*****************************************************************************
  132. Constants
  133. *****************************************************************************}
  134. const
  135. max_operands = 4;
  136. {# Constant defining possibly all registers which might require saving }
  137. ALL_OTHERREGISTERS = [];
  138. general_superregisters = [RS_R0..RS_R31];
  139. {# Table of registers which can be allocated by the code generator
  140. internally, when generating the code.
  141. }
  142. { legend: }
  143. { xxxregs = set of all possibly used registers of that type in the code }
  144. { generator }
  145. { usableregsxxx = set of all 32bit components of registers that can be }
  146. { possible allocated to a regvar or using getregisterxxx (this }
  147. { excludes registers which can be only used for parameter }
  148. { passing on ABI's that define this) }
  149. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  150. maxintregs = 15;
  151. { to determine how many registers to use for regvars }
  152. maxintscratchregs = 3;
  153. usableregsint = [RS_R4..RS_R10];
  154. c_countusableregsint = 7;
  155. maxfpuregs = 0;
  156. fpuregs = [];
  157. usableregsfpu = [];
  158. c_countusableregsfpu = 0;
  159. mmregs = [];
  160. usableregsmm = [];
  161. c_countusableregsmm = 0;
  162. maxaddrregs = 0;
  163. addrregs = [];
  164. usableregsaddr = [];
  165. c_countusableregsaddr = 0;
  166. {*****************************************************************************
  167. Operand Sizes
  168. *****************************************************************************}
  169. type
  170. topsize = (S_NO,
  171. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  172. S_IS,S_IL,S_IQ,
  173. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  174. );
  175. {*****************************************************************************
  176. Constants
  177. *****************************************************************************}
  178. const
  179. firstsaveintreg = RS_R4;
  180. lastsaveintreg = RS_R10;
  181. firstsavefpureg = RS_INVALID;
  182. lastsavefpureg = RS_INVALID;
  183. firstsavemmreg = RS_INVALID;
  184. lastsavemmreg = RS_INVALID;
  185. maxvarregs = 7;
  186. varregs : Array [1..maxvarregs] of tsuperregister =
  187. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  188. maxfpuvarregs = 1;
  189. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  190. (RS_INVALID);
  191. {*****************************************************************************
  192. Default generic sizes
  193. *****************************************************************************}
  194. { Defines the default address size for a processor, }
  195. OS_ADDR = OS_16;
  196. { the natural int size for a processor, }
  197. OS_INT = OS_16;
  198. OS_SINT = OS_S16;
  199. { the maximum float size for a processor, }
  200. OS_FLOAT = OS_F64;
  201. { the size of a vector register for a processor }
  202. OS_VECTOR = OS_M32;
  203. {*****************************************************************************
  204. Generic Register names
  205. *****************************************************************************}
  206. { Stack pointer register }
  207. NR_STACK_POINTER_REG = NR_R13;
  208. RS_STACK_POINTER_REG = RS_R13;
  209. { Frame pointer register }
  210. RS_FRAME_POINTER_REG = RS_R11;
  211. NR_FRAME_POINTER_REG = NR_R11;
  212. { Register for addressing absolute data in a position independant way,
  213. such as in PIC code. The exact meaning is ABI specific. For
  214. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  215. }
  216. NR_PIC_OFFSET_REG = NR_R9;
  217. { Results are returned in this register (32-bit values) }
  218. NR_FUNCTION_RETURN_REG = NR_R0;
  219. RS_FUNCTION_RETURN_REG = RS_R0;
  220. { Low part of 64bit return value }
  221. NR_FUNCTION_RETURN64_LOW_REG = NR_R0;
  222. RS_FUNCTION_RETURN64_LOW_REG = RS_R0;
  223. { High part of 64bit return value }
  224. NR_FUNCTION_RETURN64_HIGH_REG = NR_R1;
  225. RS_FUNCTION_RETURN64_HIGH_REG = RS_R1;
  226. { The value returned from a function is available in this register }
  227. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  228. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  229. { The lowh part of 64bit value returned from a function }
  230. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  231. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  232. { The high part of 64bit value returned from a function }
  233. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  234. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  235. NR_FPU_RESULT_REG = NR_NO;
  236. NR_MM_RESULT_REG = NR_NO;
  237. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  238. { Offset where the parent framepointer is pushed }
  239. PARENT_FRAMEPOINTER_OFFSET = 0;
  240. {*****************************************************************************
  241. GCC /ABI linking information
  242. *****************************************************************************}
  243. const
  244. { Registers which must be saved when calling a routine declared as
  245. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  246. saved should be the ones as defined in the target ABI and / or GCC.
  247. This value can be deduced from the CALLED_USED_REGISTERS array in the
  248. GCC source.
  249. }
  250. saved_standard_registers : array[0..6] of tsuperregister =
  251. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  252. { Required parameter alignment when calling a routine declared as
  253. stdcall and cdecl. The alignment value should be the one defined
  254. by GCC or the target ABI.
  255. The value of this constant is equal to the constant
  256. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  257. }
  258. std_param_align = 4;
  259. saved_mm_registers : array[0..0] of tsuperregister = (RS_INVALID);
  260. {*****************************************************************************
  261. Helpers
  262. *****************************************************************************}
  263. { Returns the tcgsize corresponding with the size of reg.}
  264. function reg_cgsize(const reg: tregister) : tcgsize;
  265. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  266. procedure inverse_flags(var f: TResFlags);
  267. function flags_to_cond(const f: TResFlags) : TAsmCond;
  268. function findreg_by_number(r:Tregister):tregisterindex;
  269. function std_regnum_search(const s:string):Tregister;
  270. function std_regname(r:Tregister):string;
  271. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  272. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  273. function is_pc(const r : tregister) : boolean;
  274. function dwarf_reg(r:tregister):byte;
  275. function GetHigh(const r : TRegister) : TRegister;
  276. implementation
  277. uses
  278. rgBase,verbose;
  279. const
  280. std_regname_table : array[tregisterindex] of string[7] = (
  281. {$i ravrstd.inc}
  282. );
  283. regnumber_index : array[tregisterindex] of tregisterindex = (
  284. {$i ravrrni.inc}
  285. );
  286. std_regname_index : array[tregisterindex] of tregisterindex = (
  287. {$i ravrsri.inc}
  288. );
  289. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  290. begin
  291. cgsize2subreg:=R_SUBWHOLE;
  292. end;
  293. function reg_cgsize(const reg: tregister): tcgsize;
  294. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  295. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO,OS_NO,OS_NO,OS_NO);
  296. begin
  297. case getregtype(reg) of
  298. R_INTREGISTER :
  299. reg_cgsize:=OS_32;
  300. R_FPUREGISTER :
  301. reg_cgsize:=OS_F80;
  302. else
  303. internalerror(200303181);
  304. end;
  305. end;
  306. procedure inverse_flags(var f: TResFlags);
  307. const
  308. inv_flags: array[TResFlags] of TResFlags =
  309. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  310. F_LT,F_GE,F_LE,F_GT);
  311. begin
  312. f:=inv_flags[f];
  313. end;
  314. function flags_to_cond(const f: TResFlags) : TAsmCond;
  315. const
  316. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  317. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  318. C_GE,C_LT,C_GT,C_LE);
  319. begin
  320. if f>high(flag_2_cond) then
  321. internalerror(200112301);
  322. result:=flag_2_cond[f];
  323. end;
  324. function findreg_by_number(r:Tregister):tregisterindex;
  325. begin
  326. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  327. end;
  328. function std_regnum_search(const s:string):Tregister;
  329. begin
  330. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  331. end;
  332. function std_regname(r:Tregister):string;
  333. var
  334. p : tregisterindex;
  335. begin
  336. p:=findreg_by_number_table(r,regnumber_index);
  337. if p<>0 then
  338. result:=std_regname_table[p]
  339. else
  340. result:=generic_regname(r);
  341. end;
  342. procedure shifterop_reset(var so : tshifterop);
  343. begin
  344. FillChar(so,sizeof(so),0);
  345. end;
  346. function is_pc(const r : tregister) : boolean;
  347. begin
  348. is_pc:=(r=NR_R15);
  349. end;
  350. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  351. const
  352. inverse: array[TAsmCond] of TAsmCond=(C_None,
  353. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  354. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  355. );
  356. begin
  357. result := inverse[c];
  358. end;
  359. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  360. begin
  361. result := c1 = c2;
  362. end;
  363. function rotl(d : dword;b : byte) : dword;
  364. begin
  365. result:=(d shr (32-b)) or (d shl b);
  366. end;
  367. function dwarf_reg(r:tregister):byte;
  368. begin
  369. result:=regdwarf_table[findreg_by_number(r)];
  370. if result=-1 then
  371. internalerror(200603251);
  372. end;
  373. function GetHigh(const r : TRegister) : TRegister;
  374. begin
  375. result:=TRegister(longint(r)+1)
  376. end;
  377. end.