cpubase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_SBRC,A_SBRS,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,
  38. A_MUL,A_MULS,A_MULSU,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_SBIC,A_SBIS,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_SEC,A_SEH,A_SEI,A_SEN,A_SER,A_SES,A_SET,A_SEV,A_SEZ,
  44. A_CLC,A_CLH,A_CLI,A_CLN,A_CLR,A_CLS,A_CLT,A_CLV,A_CLZ,
  45. A_BST,A_BLD,A_BREAK,A_NOP,A_SLEEP,A_WDR,A_XCH);
  46. { This should define the array of instructions as string }
  47. op2strtable=array[tasmop] of string[11];
  48. const
  49. { First value of opcode enumeration }
  50. firstop = low(tasmop);
  51. { Last value of opcode enumeration }
  52. lastop = high(tasmop);
  53. { call/reg instructions (A_RCALL,A_ICALL,A_CALL,A_RET,A_RETI) are not considered as jmp instructions for the usage cases of
  54. this set }
  55. jmp_instructions = [A_BRxx,A_SBIC,A_SBIS,A_JMP,A_EIJMP,A_RJMP,A_CPSE,A_IJMP];
  56. call_jmp_instructions = [A_ICALL,A_RCALL,A_CALL,A_RET,A_RETI]+jmp_instructions;
  57. {*****************************************************************************
  58. Registers
  59. *****************************************************************************}
  60. type
  61. { Number of registers used for indexing in tables }
  62. tregisterindex=0..{$i ravrnor.inc}-1;
  63. const
  64. { Available Superregisters }
  65. {$i ravrsup.inc}
  66. { No Subregisters }
  67. R_SUBWHOLE = R_SUBNONE;
  68. { Available Registers }
  69. {$i ravrcon.inc}
  70. NR_XLO = NR_R26;
  71. NR_XHI = NR_R27;
  72. NR_YLO = NR_R28;
  73. NR_YHI = NR_R29;
  74. NR_ZLO = NR_R30;
  75. NR_ZHI = NR_R31;
  76. NIO_SREG = $3f;
  77. NIO_SP_LO = $3d;
  78. NIO_SP_HI = $3e;
  79. { Integer Super registers first and last }
  80. first_int_supreg = RS_R0;
  81. first_int_imreg = $20;
  82. { Float Super register first and last }
  83. first_fpu_supreg = RS_INVALID;
  84. first_fpu_imreg = 0;
  85. { MM Super register first and last }
  86. first_mm_supreg = RS_INVALID;
  87. first_mm_imreg = 0;
  88. regnumber_count_bsstart = 32;
  89. regnumber_table : array[tregisterindex] of tregister = (
  90. {$i ravrnum.inc}
  91. );
  92. regstabs_table : array[tregisterindex] of shortint = (
  93. {$i ravrsta.inc}
  94. );
  95. regdwarf_table : array[tregisterindex] of shortint = (
  96. {$i ravrdwa.inc}
  97. );
  98. { registers which may be destroyed by calls }
  99. VOLATILE_INTREGISTERS = [RS_R0,RS_R1,RS_R18..RS_R27,RS_R30,RS_R31];
  100. VOLATILE_FPUREGISTERS = [];
  101. type
  102. totherregisterset = set of tregisterindex;
  103. {*****************************************************************************
  104. Conditions
  105. *****************************************************************************}
  106. type
  107. TAsmCond=(C_None,
  108. C_CC,C_CS,C_EQ,C_GE,C_HC,C_HS,C_ID,C_IE,C_LO,C_LT,
  109. C_MI,C_NE,C_PL,C_SH,C_TC,C_TS,C_VC,C_VS
  110. );
  111. const
  112. cond2str : array[TAsmCond] of string[2]=('',
  113. 'cc','cs','eq','ge','hc','hs','id','ie','lo','lt',
  114. 'mi','ne','pl','sh','tc','ts','vc','vs'
  115. );
  116. uppercond2str : array[TAsmCond] of string[2]=('',
  117. 'CC','CS','EQ','GE','HC','HS','ID','IE','LO','LT',
  118. 'MI','NE','PL','SH','TC','TS','VC','VS'
  119. );
  120. {*****************************************************************************
  121. Flags
  122. *****************************************************************************}
  123. type
  124. TResFlags = (F_NotPossible,F_CC,F_CS,F_EQ,F_GE,F_LO,F_LT,
  125. F_NE,F_SH,F_VC,F_VS,F_PL,F_MI);
  126. {*****************************************************************************
  127. Operands
  128. *****************************************************************************}
  129. taddressmode = (AM_UNCHANGED,AM_POSTINCREMENT,AM_PREDRECEMENT);
  130. {*****************************************************************************
  131. Constants
  132. *****************************************************************************}
  133. const
  134. max_operands = 2;
  135. maxintregs = 15;
  136. maxfpuregs = 0;
  137. maxaddrregs = 0;
  138. {*****************************************************************************
  139. Operand Sizes
  140. *****************************************************************************}
  141. type
  142. topsize = (S_NO,
  143. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  144. S_IS,S_IL,S_IQ,
  145. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  146. );
  147. {*****************************************************************************
  148. Constants
  149. *****************************************************************************}
  150. const
  151. firstsaveintreg = RS_R4;
  152. lastsaveintreg = RS_R10;
  153. firstsavefpureg = RS_INVALID;
  154. lastsavefpureg = RS_INVALID;
  155. firstsavemmreg = RS_INVALID;
  156. lastsavemmreg = RS_INVALID;
  157. maxvarregs = 7;
  158. varregs : Array [1..maxvarregs] of tsuperregister =
  159. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  160. maxfpuvarregs = 1;
  161. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  162. (RS_INVALID);
  163. {*****************************************************************************
  164. Default generic sizes
  165. *****************************************************************************}
  166. { Defines the default address size for a processor, }
  167. OS_ADDR = OS_16;
  168. { the natural int size for a processor,
  169. has to match osuinttype/ossinttype as initialized in psystem,
  170. initially, this was OS_16/OS_S16 on avr, but experience has
  171. proven that it is better to make it 8 Bit thus having the same
  172. size as a register.
  173. }
  174. OS_INT = OS_8;
  175. OS_SINT = OS_S8;
  176. { the maximum float size for a processor, }
  177. OS_FLOAT = OS_F64;
  178. { the size of a vector register for a processor }
  179. OS_VECTOR = OS_M32;
  180. {*****************************************************************************
  181. Generic Register names
  182. *****************************************************************************}
  183. { Stack pointer register }
  184. NR_STACK_POINTER_REG = NR_R13;
  185. RS_STACK_POINTER_REG = RS_R13;
  186. { Frame pointer register }
  187. RS_FRAME_POINTER_REG = RS_R28;
  188. NR_FRAME_POINTER_REG = NR_R28;
  189. { Register for addressing absolute data in a position independant way,
  190. such as in PIC code. The exact meaning is ABI specific. For
  191. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  192. }
  193. NR_PIC_OFFSET_REG = NR_R9;
  194. { Results are returned in this register (32-bit values) }
  195. NR_FUNCTION_RETURN_REG = NR_R24;
  196. RS_FUNCTION_RETURN_REG = RS_R24;
  197. { Low part of 64bit return value }
  198. NR_FUNCTION_RETURN64_LOW_REG = NR_R22;
  199. RS_FUNCTION_RETURN64_LOW_REG = RS_R22;
  200. { High part of 64bit return value }
  201. NR_FUNCTION_RETURN64_HIGH_REG = NR_R1;
  202. RS_FUNCTION_RETURN64_HIGH_REG = RS_R1;
  203. { The value returned from a function is available in this register }
  204. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  205. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  206. { The lowh part of 64bit value returned from a function }
  207. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  208. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  209. { The high part of 64bit value returned from a function }
  210. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  211. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  212. NR_FPU_RESULT_REG = NR_NO;
  213. NR_MM_RESULT_REG = NR_NO;
  214. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  215. { Offset where the parent framepointer is pushed }
  216. PARENT_FRAMEPOINTER_OFFSET = 0;
  217. NR_DEFAULTFLAGS = NR_SREG;
  218. RS_DEFAULTFLAGS = RS_SREG;
  219. {*****************************************************************************
  220. GCC /ABI linking information
  221. *****************************************************************************}
  222. const
  223. { Required parameter alignment when calling a routine declared as
  224. stdcall and cdecl. The alignment value should be the one defined
  225. by GCC or the target ABI.
  226. The value of this constant is equal to the constant
  227. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  228. }
  229. std_param_align = 4;
  230. {*****************************************************************************
  231. Helpers
  232. *****************************************************************************}
  233. { Returns the tcgsize corresponding with the size of reg.}
  234. function reg_cgsize(const reg: tregister) : tcgsize;
  235. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  236. procedure inverse_flags(var f: TResFlags);
  237. function flags_to_cond(const f: TResFlags) : TAsmCond;
  238. function findreg_by_number(r:Tregister):tregisterindex;
  239. function std_regnum_search(const s:string):Tregister;
  240. function std_regname(r:Tregister):string;
  241. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  242. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  243. function dwarf_reg(r:tregister):byte;
  244. function dwarf_reg_no_error(r:tregister):shortint;
  245. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  246. implementation
  247. uses
  248. rgBase,verbose;
  249. const
  250. std_regname_table : TRegNameTable = (
  251. {$i ravrstd.inc}
  252. );
  253. regnumber_index : array[tregisterindex] of tregisterindex = (
  254. {$i ravrrni.inc}
  255. );
  256. std_regname_index : array[tregisterindex] of tregisterindex = (
  257. {$i ravrsri.inc}
  258. );
  259. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  260. begin
  261. cgsize2subreg:=R_SUBWHOLE;
  262. end;
  263. function reg_cgsize(const reg: tregister): tcgsize;
  264. begin
  265. case getregtype(reg) of
  266. R_INTREGISTER :
  267. reg_cgsize:=OS_8;
  268. R_ADDRESSREGISTER :
  269. reg_cgsize:=OS_16;
  270. else
  271. internalerror(2011021905);
  272. end;
  273. end;
  274. procedure inverse_flags(var f: TResFlags);
  275. const
  276. inv_flags: array[TResFlags] of TResFlags =
  277. (F_NotPossible,F_CS,F_CC,F_NE,F_LT,F_SH,F_GE,
  278. F_NE,F_LO,F_VS,F_VC,F_MI,F_PL);
  279. begin
  280. f:=inv_flags[f];
  281. end;
  282. function flags_to_cond(const f: TResFlags) : TAsmCond;
  283. const
  284. flag_2_cond: array[F_CC..F_MI] of TAsmCond =
  285. (C_CC,C_CS,C_EQ,C_GE,C_LO,C_LT,
  286. C_NE,C_SH,C_VC,C_VS,C_PL,C_MI);
  287. begin
  288. if f=F_NotPossible then
  289. internalerror(2011022101);
  290. if f>high(flag_2_cond) then
  291. internalerror(200112301);
  292. result:=flag_2_cond[f];
  293. end;
  294. function findreg_by_number(r:Tregister):tregisterindex;
  295. begin
  296. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  297. end;
  298. function std_regnum_search(const s:string):Tregister;
  299. begin
  300. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  301. end;
  302. function std_regname(r:Tregister):string;
  303. var
  304. p : tregisterindex;
  305. begin
  306. p:=findreg_by_number_table(r,regnumber_index);
  307. if p<>0 then
  308. result:=std_regname_table[p]
  309. else
  310. result:=generic_regname(r);
  311. end;
  312. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  313. const
  314. inverse: array[TAsmCond] of TAsmCond=(C_None,
  315. C_CS,C_CC,C_NE,C_LT,C_HS,C_HC,C_IE,C_ID,C_SH,C_GE,
  316. C_PL,C_EQ,C_MI,C_LO,C_TS,C_TC,C_VS,C_VC);
  317. begin
  318. result := inverse[c];
  319. end;
  320. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  321. begin
  322. result := c1 = c2;
  323. end;
  324. function rotl(d : dword;b : byte) : dword;
  325. begin
  326. result:=(d shr (32-b)) or (d shl b);
  327. end;
  328. function dwarf_reg(r:tregister):byte;
  329. var
  330. reg : shortint;
  331. begin
  332. reg:=regdwarf_table[findreg_by_number(r)];
  333. if reg=-1 then
  334. internalerror(200603251);
  335. result:=reg;
  336. end;
  337. function dwarf_reg_no_error(r:tregister):shortint;
  338. begin
  339. result:=regdwarf_table[findreg_by_number(r)];
  340. end;
  341. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  342. begin
  343. is_calljmp:= o in call_jmp_instructions;
  344. end;
  345. end.