navrutil.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. {
  2. Copyright (c) 2015 by Jeppe Johansen
  3. AVR version of some node tree helper routines
  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. unit navrutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. fmodule,
  23. node,nbas,
  24. ngenutil,
  25. symtype,symconst,symsym,symdef;
  26. type
  27. { tavrnodeutils }
  28. tavrnodeutils = class(tnodeutils)
  29. protected
  30. class procedure insert_init_final_table(main: tmodule; entries:tfplist); override;
  31. public
  32. class procedure InsertMemorySizes; override;
  33. end;
  34. implementation
  35. uses
  36. verbose,cutils,globtype,globals,constexp,
  37. aasmdata,aasmtai,aasmcpu,aasmcnst,aasmbase,
  38. cpubase,
  39. symbase,symcpu,symtable,defutil,
  40. ncnv,ncon,ninl,ncal,nld,nmem,
  41. systems,
  42. CPUInfo,
  43. ppu,
  44. pass_1;
  45. class procedure tavrnodeutils.insert_init_final_table(main: tmodule; entries:tfplist);
  46. var
  47. op : TAsmOp;
  48. initList, finalList, header: TAsmList;
  49. entry : pinitfinalentry;
  50. i : longint;
  51. begin
  52. initList:=TAsmList.create;
  53. finalList:=TAsmList.create;
  54. if CPUAVR_HAS_JMP_CALL in cpu_capabilities[current_settings.cputype] then
  55. op:=A_CALL
  56. else
  57. op:=A_RCALL;
  58. for i:=0 to entries.count-1 do
  59. begin
  60. entry:=pinitfinalentry(entries[i]);
  61. if entry^.finifunc<>'' then
  62. finalList.Concat(taicpu.op_sym(op,current_asmdata.RefAsmSymbol(entry^.finifunc,AT_FUNCTION)));
  63. if entry^.initfunc<>'' then
  64. initList.Concat(taicpu.op_sym(op,current_asmdata.RefAsmSymbol(entry^.initfunc,AT_FUNCTION)));
  65. end;
  66. initList.Concat(taicpu.op_none(A_RET));
  67. finalList.Concat(taicpu.op_none(A_RET));
  68. begin
  69. header:=TAsmList.create;
  70. new_section(header, sec_code, 'FPC_INIT_FUNC_TABLE', 1);
  71. header.concat(tai_symbol.Createname_global('FPC_INIT_FUNC_TABLE',AT_FUNCTION,0,voidcodepointertype));
  72. initList.insertList(header);
  73. header.free;
  74. current_asmdata.AsmLists[al_procedures].concatList(initList);
  75. end;
  76. begin
  77. header:=TAsmList.create;
  78. new_section(header, sec_code, 'FPC_FINALIZE_FUNC_TABLE', 1);
  79. header.concat(tai_symbol.Createname_global('FPC_FINALIZE_FUNC_TABLE',AT_FUNCTION,0,voidcodepointertype));
  80. finalList.insertList(header);
  81. header.free;
  82. current_asmdata.AsmLists[al_procedures].concatList(finalList);
  83. end;
  84. initList.Free;
  85. finalList.Free;
  86. inherited insert_init_final_table(main,entries);
  87. end;
  88. class procedure tavrnodeutils.InsertMemorySizes;
  89. var
  90. tcb: ttai_typedconstbuilder;
  91. notename, strtable: shortstring;
  92. sym: tasmsymbol;
  93. defstr, defu32: tdef;
  94. begin
  95. { Store device information in the .note.gnu.avr.deviceinfo section.
  96. Layout of.note.gnu.avr.deviceinfo:
  97. note_name_len: dword
  98. note_desc_len: dword // Size of the Tavr_desc data record
  99. note_type: dword = 1
  100. Tavr_desc = record
  101. note_name: char[note_name_len] = 'AVR'#0;
  102. flash_start,
  103. flash_size,
  104. sram_start,
  105. sram_size,
  106. eeprom_start,
  107. eeprom_size: dword;
  108. offset_table_len: dword;
  109. offset_table: char[] = #0+mcuname#0+#0;
  110. end }
  111. notename:='AVR'#0;
  112. strtable:=#0+lower(embedded_controllers[current_settings.controllertype].controllertypestr)+#0#0;
  113. tcb:=ctai_typedconstbuilder.create([tcalo_no_dead_strip]);
  114. defu32:=corddef.create(u32bit,0,$FFFFFFFF,false);
  115. tcb.maybe_begin_aggregate(defu32);
  116. tcb.emit_tai(tai_const.Create_32bit_unaligned(length(notename)),defu32);
  117. tcb.emit_tai(tai_const.Create_32bit_unaligned(length(strtable)+32),defu32);
  118. tcb.emit_tai(tai_const.Create_32bit_unaligned(1),defu32);
  119. defstr:=carraydef.getreusable(cansichartype,length(notename));
  120. tcb.maybe_begin_aggregate(defstr);
  121. tcb.emit_tai(Tai_string.Create(notename),defstr);
  122. tcb.maybe_begin_aggregate(defu32);
  123. tcb.emit_tai(tai_const.Create_32bit_unaligned(embedded_controllers[current_settings.controllertype].flashbase),defu32);
  124. tcb.emit_tai(tai_const.Create_32bit_unaligned(embedded_controllers[current_settings.controllertype].flashsize),defu32);
  125. tcb.emit_tai(tai_const.Create_32bit_unaligned(embedded_controllers[current_settings.controllertype].srambase),defu32);
  126. tcb.emit_tai(tai_const.Create_32bit_unaligned(embedded_controllers[current_settings.controllertype].sramsize),defu32);
  127. tcb.emit_tai(tai_const.Create_32bit_unaligned(embedded_controllers[current_settings.controllertype].eeprombase),defu32);
  128. tcb.emit_tai(tai_const.Create_32bit_unaligned(embedded_controllers[current_settings.controllertype].eepromsize),defu32);
  129. tcb.emit_tai(tai_const.Create_32bit_unaligned(8),defu32); // Size of string offset table
  130. tcb.emit_tai(tai_const.Create_32bit_unaligned(1),defu32); // Offset of string in table
  131. tcb.maybe_begin_aggregate(defstr);
  132. tcb.emit_tai(Tai_string.Create(strtable),defstr);
  133. tcb.maybe_end_aggregate(defstr);
  134. tcb.maybe_end_aggregate(defu32);
  135. tcb.maybe_end_aggregate(defstr);
  136. tcb.maybe_end_aggregate(defu32);
  137. sym:=current_asmdata.DefineAsmSymbol('__AVR_deviceinfo',AB_LOCAL,AT_DATA,defstr);
  138. current_asmdata.asmlists[al_globals].concatlist(
  139. tcb.get_final_asmlist(sym,defstr,sec_note,'.gnu.avr.deviceinfo',const_align(32))
  140. );
  141. defu32.free;
  142. tcb.free;
  143. inherited InsertMemorySizes;
  144. end;
  145. begin
  146. cnodeutils:=tavrnodeutils;
  147. end.