cpubase.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_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_BST,A_BLD,A_Sxx,A_CLI,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_SBIC,A_SBIS,A_JMP,A_RCALL,A_ICALL,A_EIJMP,
  52. A_RJMP,A_CALL,A_RET,A_RETI,A_CPSE,A_IJMP];
  53. {*****************************************************************************
  54. Registers
  55. *****************************************************************************}
  56. type
  57. { Number of registers used for indexing in tables }
  58. tregisterindex=0..{$i ravrnor.inc}-1;
  59. const
  60. { Available Superregisters }
  61. {$i ravrsup.inc}
  62. { No Subregisters }
  63. R_SUBWHOLE = R_SUBNONE;
  64. { Available Registers }
  65. {$i ravrcon.inc}
  66. NR_XLO = NR_R26;
  67. NR_XHI = NR_R27;
  68. NR_YLO = NR_R28;
  69. NR_YHI = NR_R29;
  70. NR_ZLO = NR_R30;
  71. NR_ZHI = NR_R31;
  72. NIO_SREG = $3f;
  73. NIO_SP_LO = $3d;
  74. NIO_SP_HI = $3e;
  75. { Integer Super registers first and last }
  76. first_int_supreg = RS_R0;
  77. first_int_imreg = $10;
  78. { Float Super register first and last }
  79. first_fpu_supreg = RS_INVALID;
  80. first_fpu_imreg = RS_INVALID;
  81. { MM Super register first and last }
  82. first_mm_supreg = RS_INVALID;
  83. first_mm_imreg = RS_INVALID;
  84. { TODO: Calculate bsstart}
  85. regnumber_count_bsstart = 64;
  86. regnumber_table : array[tregisterindex] of tregister = (
  87. {$i ravrnum.inc}
  88. );
  89. regstabs_table : array[tregisterindex] of shortint = (
  90. {$i ravrsta.inc}
  91. );
  92. regdwarf_table : array[tregisterindex] of shortint = (
  93. {$i ravrdwa.inc}
  94. );
  95. { registers which may be destroyed by calls }
  96. VOLATILE_INTREGISTERS = [RS_R18..RS_R27,RS_R30..RS_R31];
  97. VOLATILE_FPUREGISTERS = [];
  98. type
  99. totherregisterset = set of tregisterindex;
  100. {*****************************************************************************
  101. Conditions
  102. *****************************************************************************}
  103. type
  104. TAsmCond=(C_None,
  105. C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  106. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  107. );
  108. const
  109. cond2str : 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. uppercond2str : array[TAsmCond] of string[2]=('',
  114. 'EQ','NE','CS','CC','MI','PL','VS','VC','HI','LS',
  115. 'GE','LT','GT','LE','AL','NV'
  116. );
  117. {*****************************************************************************
  118. Flags
  119. *****************************************************************************}
  120. type
  121. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  122. F_GE,F_LT,F_GT,F_LE);
  123. {*****************************************************************************
  124. Operands
  125. *****************************************************************************}
  126. taddressmode = (AM_UNCHANGED,AM_POSTINCREMENT,AM_PREDRECEMENT);
  127. {*****************************************************************************
  128. Constants
  129. *****************************************************************************}
  130. const
  131. max_operands = 4;
  132. {# Constant defining possibly all registers which might require saving }
  133. ALL_OTHERREGISTERS = [];
  134. general_superregisters = [RS_R0..RS_R31];
  135. {# Table of registers which can be allocated by the code generator
  136. internally, when generating the code.
  137. }
  138. { legend: }
  139. { xxxregs = set of all possibly used registers of that type in the code }
  140. { generator }
  141. { usableregsxxx = set of all 32bit components of registers that can be }
  142. { possible allocated to a regvar or using getregisterxxx (this }
  143. { excludes registers which can be only used for parameter }
  144. { passing on ABI's that define this) }
  145. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  146. maxintregs = 15;
  147. { to determine how many registers to use for regvars }
  148. maxintscratchregs = 3;
  149. usableregsint = [RS_R4..RS_R10];
  150. c_countusableregsint = 7;
  151. maxfpuregs = 0;
  152. fpuregs = [];
  153. usableregsfpu = [];
  154. c_countusableregsfpu = 0;
  155. mmregs = [];
  156. usableregsmm = [];
  157. c_countusableregsmm = 0;
  158. maxaddrregs = 0;
  159. addrregs = [];
  160. usableregsaddr = [];
  161. c_countusableregsaddr = 0;
  162. {*****************************************************************************
  163. Operand Sizes
  164. *****************************************************************************}
  165. type
  166. topsize = (S_NO,
  167. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  168. S_IS,S_IL,S_IQ,
  169. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  170. );
  171. {*****************************************************************************
  172. Constants
  173. *****************************************************************************}
  174. const
  175. firstsaveintreg = RS_R4;
  176. lastsaveintreg = RS_R10;
  177. firstsavefpureg = RS_INVALID;
  178. lastsavefpureg = RS_INVALID;
  179. firstsavemmreg = RS_INVALID;
  180. lastsavemmreg = RS_INVALID;
  181. maxvarregs = 7;
  182. varregs : Array [1..maxvarregs] of tsuperregister =
  183. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  184. maxfpuvarregs = 1;
  185. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  186. (RS_INVALID);
  187. {*****************************************************************************
  188. Default generic sizes
  189. *****************************************************************************}
  190. { Defines the default address size for a processor, }
  191. OS_ADDR = OS_16;
  192. { the natural int size for a processor, }
  193. OS_INT = OS_16;
  194. OS_SINT = OS_S16;
  195. { the maximum float size for a processor, }
  196. OS_FLOAT = OS_F64;
  197. { the size of a vector register for a processor }
  198. OS_VECTOR = OS_M32;
  199. {*****************************************************************************
  200. Generic Register names
  201. *****************************************************************************}
  202. { Stack pointer register }
  203. NR_STACK_POINTER_REG = NR_R13;
  204. RS_STACK_POINTER_REG = RS_R13;
  205. { Frame pointer register }
  206. RS_FRAME_POINTER_REG = RS_R11;
  207. NR_FRAME_POINTER_REG = NR_R11;
  208. { Register for addressing absolute data in a position independant way,
  209. such as in PIC code. The exact meaning is ABI specific. For
  210. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  211. }
  212. NR_PIC_OFFSET_REG = NR_R9;
  213. { Results are returned in this register (32-bit values) }
  214. NR_FUNCTION_RETURN_REG = NR_R0;
  215. RS_FUNCTION_RETURN_REG = RS_R0;
  216. { Low part of 64bit return value }
  217. NR_FUNCTION_RETURN64_LOW_REG = NR_R0;
  218. RS_FUNCTION_RETURN64_LOW_REG = RS_R0;
  219. { High part of 64bit return value }
  220. NR_FUNCTION_RETURN64_HIGH_REG = NR_R1;
  221. RS_FUNCTION_RETURN64_HIGH_REG = RS_R1;
  222. { The value returned from a function is available in this register }
  223. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  224. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  225. { The lowh part of 64bit value returned from a function }
  226. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  227. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  228. { The high part of 64bit value returned from a function }
  229. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  230. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  231. NR_FPU_RESULT_REG = NR_NO;
  232. NR_MM_RESULT_REG = NR_NO;
  233. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  234. { Offset where the parent framepointer is pushed }
  235. PARENT_FRAMEPOINTER_OFFSET = 0;
  236. {*****************************************************************************
  237. GCC /ABI linking information
  238. *****************************************************************************}
  239. const
  240. { Registers which must be saved when calling a routine declared as
  241. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  242. saved should be the ones as defined in the target ABI and / or GCC.
  243. This value can be deduced from the CALLED_USED_REGISTERS array in the
  244. GCC source.
  245. }
  246. saved_standard_registers : array[0..6] of tsuperregister =
  247. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  248. { Required parameter alignment when calling a routine declared as
  249. stdcall and cdecl. The alignment value should be the one defined
  250. by GCC or the target ABI.
  251. The value of this constant is equal to the constant
  252. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  253. }
  254. std_param_align = 4;
  255. saved_mm_registers : array[0..0] of tsuperregister = (RS_INVALID);
  256. {*****************************************************************************
  257. Helpers
  258. *****************************************************************************}
  259. { Returns the tcgsize corresponding with the size of reg.}
  260. function reg_cgsize(const reg: tregister) : tcgsize;
  261. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  262. procedure inverse_flags(var f: TResFlags);
  263. function flags_to_cond(const f: TResFlags) : TAsmCond;
  264. function findreg_by_number(r:Tregister):tregisterindex;
  265. function std_regnum_search(const s:string):Tregister;
  266. function std_regname(r:Tregister):string;
  267. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  268. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  269. function dwarf_reg(r:tregister):byte;
  270. function GetHigh(const r : TRegister) : TRegister;
  271. { returns the next virtual register }
  272. function GetNextReg(const r : TRegister) : TRegister;
  273. implementation
  274. uses
  275. rgBase,verbose;
  276. const
  277. std_regname_table : array[tregisterindex] of string[7] = (
  278. {$i ravrstd.inc}
  279. );
  280. regnumber_index : array[tregisterindex] of tregisterindex = (
  281. {$i ravrrni.inc}
  282. );
  283. std_regname_index : array[tregisterindex] of tregisterindex = (
  284. {$i ravrsri.inc}
  285. );
  286. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  287. begin
  288. cgsize2subreg:=R_SUBWHOLE;
  289. end;
  290. function reg_cgsize(const reg: tregister): tcgsize;
  291. begin
  292. case getregtype(reg) of
  293. R_INTREGISTER :
  294. reg_cgsize:=OS_8;
  295. else
  296. internalerror(2011021903);
  297. end;
  298. end;
  299. procedure inverse_flags(var f: TResFlags);
  300. const
  301. inv_flags: array[TResFlags] of TResFlags =
  302. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  303. F_LT,F_GE,F_LE,F_GT);
  304. begin
  305. f:=inv_flags[f];
  306. end;
  307. function flags_to_cond(const f: TResFlags) : TAsmCond;
  308. const
  309. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  310. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  311. C_GE,C_LT,C_GT,C_LE);
  312. begin
  313. if f>high(flag_2_cond) then
  314. internalerror(200112301);
  315. result:=flag_2_cond[f];
  316. end;
  317. function findreg_by_number(r:Tregister):tregisterindex;
  318. begin
  319. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  320. end;
  321. function std_regnum_search(const s:string):Tregister;
  322. begin
  323. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  324. end;
  325. function std_regname(r:Tregister):string;
  326. var
  327. p : tregisterindex;
  328. begin
  329. p:=findreg_by_number_table(r,regnumber_index);
  330. if p<>0 then
  331. result:=std_regname_table[p]
  332. else
  333. result:=generic_regname(r);
  334. end;
  335. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  336. const
  337. inverse: array[TAsmCond] of TAsmCond=(C_None,
  338. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  339. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  340. );
  341. begin
  342. result := inverse[c];
  343. end;
  344. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  345. begin
  346. result := c1 = c2;
  347. end;
  348. function rotl(d : dword;b : byte) : dword;
  349. begin
  350. result:=(d shr (32-b)) or (d shl b);
  351. end;
  352. function dwarf_reg(r:tregister):byte;
  353. begin
  354. result:=regdwarf_table[findreg_by_number(r)];
  355. if result=-1 then
  356. internalerror(200603251);
  357. end;
  358. function GetHigh(const r : TRegister) : TRegister;
  359. begin
  360. result:=TRegister(longint(r)+1)
  361. end;
  362. function GetNextReg(const r: TRegister): TRegister;
  363. begin
  364. result:=TRegister(longint(r)+1);
  365. end;
  366. end.