exp_data.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* exp_data.c: Routines to export a data file
  2. Copyright (C) 2002-2003 Sebastian Reichelt
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "exp_data.h"
  15. #ifdef DATA_VAR_SUPPORT
  16. // Get the file size needed to export the data file.
  17. // Returns 0 on failure.
  18. // Call this function once to receive necessary error messages.
  19. SIZE GetDataFileSize (const PROGRAM *Program)
  20. {
  21. // Get a pointer to the main section.
  22. SECTION *DataSection = Program->DataSection;
  23. if (!DataSection)
  24. {
  25. Error (NULL, "No data section.");
  26. return 0;
  27. }
  28. return DataSection->Size;
  29. }
  30. // Export the internal data structures into a TIOS file.
  31. BOOLEAN ExportDataFile (const PROGRAM *Program, EXP_FILE *File, SIZE FileSize ATTRIBUTE_UNUSED, ProgramCalcs DestCalc ATTRIBUTE_UNUSED)
  32. {
  33. // A simple macro to make the code more readable.
  34. #define FailWithError(Err...) ({ Error (Err); return FALSE; })
  35. const char *SectionFileName = NULL;
  36. OFFSET DataStart = 0;
  37. // Get a pointer to the data section.
  38. SECTION *DataSection = Program->DataSection;
  39. if (!DataSection)
  40. FailWithError (NULL, "No data section.");
  41. SectionFileName = DataSection->FileName;
  42. if (!(DataSection->Data))
  43. FailWithError (SectionFileName, "No section contents.");
  44. // Write out the section contents.
  45. DataStart = ExportTell (File);
  46. ExportWrite (File, DataSection->Data, DataSection->Size, 1);
  47. if (!(IsEmpty (DataSection->Relocs)))
  48. {
  49. RELOC *Reloc;
  50. for_each (Reloc, DataSection->Relocs)
  51. {
  52. // If this can be resolved to a calculator-dependent value, write the
  53. // value into the section data.
  54. if (!(EmitCalcBuiltinValue (Reloc, DestCalc, File, FileSize, DataStart)))
  55. FailWithError (SectionFileName, "Relocs inside data variable not supported.");
  56. }
  57. }
  58. if (!(IsEmpty (DataSection->ROMCalls)))
  59. FailWithError (SectionFileName, "ROM calls inside data variable not supported.");
  60. if (!(IsEmpty (DataSection->RAMCalls)))
  61. FailWithError (SectionFileName, "RAM calls inside data variable not supported.");
  62. if (!(IsEmpty (DataSection->LibCalls)))
  63. FailWithError (SectionFileName, "Library calls inside data variable not supported.");
  64. return TRUE;
  65. #undef FailWithError
  66. }
  67. #endif /* DATA_VAR_SUPPORT */