struct.inc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ; vim:ft=fasm:
  2. ; taken from: https://github.com/tgrysztar/fasmg
  3. ; Basic implementation of STRUCT and UNION.
  4. ; A structure should be declared like:
  5. ; struct POINT
  6. ; x dd ?
  7. ; y dd ?
  8. ; ends
  9. ; This defines field offsets like POINT.x and POINT.y and allows to instantiate structure
  10. ; either in default form:
  11. ; first POINT
  12. ; or with some (or all) of the fields initialized with specified values:
  13. ; second POINT x:10
  14. ; A union can be used as a part of structure or standalone:
  15. ; union
  16. ; name dq ?
  17. ; initial db ?
  18. ; endu
  19. macro struct? name
  20. macro ends?!
  21. end namespace
  22. iterate definition, args@struct
  23. match name:value, definition
  24. store value at .name
  25. else match name==value, definition
  26. store value at .name
  27. else match value, definition
  28. err 'unnamed values not supported'
  29. end match
  30. end iterate
  31. esc end struc
  32. virtual at 0
  33. name name
  34. sizeof.name = $
  35. end virtual
  36. purge ends?
  37. end macro
  38. esc struc name args@struct&
  39. label . : sizeof.name
  40. namespace .
  41. end macro
  42. macro union?
  43. local union
  44. union = $
  45. macro ? line&
  46. match =endu?, line
  47. purge ?
  48. else if $-union = 0
  49. line
  50. else
  51. virtual at union
  52. line
  53. local size
  54. size = $-union
  55. end virtual
  56. if size > $-union
  57. rb size-($-union)
  58. end if
  59. end if
  60. end macro
  61. end macro