; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Executable file formating macro library.
;
;  Target OS: KolibriOS
;
;  Dependencies:
;
;  Notes:
;_________________________________________________________________________________________

TargetOS equ KolibriOS

STACK_SIZE = 1024 * 1024 ; 1MB

hdrVersionConsole = 2

macro @BinaryType type, model {
  if type eq GUI | type eq console
        format binary as 'kex'
        use32
        org     $0

        db      'MENUET01'
        dd      hdrVersionConsole
        dd      start
        dd      __end_code
        dd      __end_data + STACK_SIZE
        dd      __end_data + STACK_SIZE
        dd      __bufferArguments
        dd      __bufferPath

  STDIO  dd     0
  STDDBG dd     0
  end if

  addrHeapStart = __end_data + STACK_SIZE

; ???????????????????????
  if type eq DLL
    format PE GUI 4.0 DLL
    entry EntryPoint
    section '.code' code readable writeable executable
  end if

  postpone \{
    ?FAKE_COMPILE = 0
    match =TRUE, ___FAKE_COMPILE \\{
      ?FAKE_COMPILE = 1
    \\}

    if ?FAKE_COMPILE = 0

      __AllDataSection
      __AllImportSection

    end if
  \}


}


macro __AllDataSection {
  local begin

  align 4
  begin = $
  disp 3, 'Initialized data address: $', <begin, 16>, 13
  IncludeIGlobals
  DispSize 'Initialized data', ($ - begin)

  align 4
  begin = $
  disp 3, 'Strings address: $', <$, 16>, 13
  _IncludeAllStrings
  DispSize 'String data', ($ - begin)

  align 4
  begin = $
  disp 3, 'I18n strings data:', <begin, 16>, 13
  __IncludeI18nStrings
  DispSize 'I18n strings data', ($ - begin)

  __end_code:

  if used bufferArguments
    bufferArguments rb 1024
    __bufferArguments = bufferArguments
  else
    __bufferArguments = 0
  end if

  if used bufferPath
    bufferPath       rb 1024
    __bufferPath = bufferPath
  else
    __bufferPath = 0
  end if

  align 4
  begin = $
  disp 3, 'Uninitialized data address: $', <begin, 16>, 13
  IncludeUGlobals
  DispSize 'Uninitialized data', ($ - begin)

  align 4
  begin = $
  disp 3, 'I18n strings placeholders: $', <begin, 16>, 13
  IncludeI18nLabels
  DispSize 'I18n strings placeholders', ($ - begin)

  align 4096
  __end_data:
}


macro __create_dll_list {
  DynamicLibList equ
  rept DynamicLibList@count i:1 \{
    match any, DynamicLibList \\{
      match name:filename, DynamicLibList\\#i \\\{
        DynamicLibList equ DynamicLibList, name, \\\`name\\\#'.dll'
      \\\}
    \\}

    match , DynamicLibList \\{
      match name:filename, DynamicLibList\\#i \\\{
        DynamicLibList equ name, \\\`name\\\#'.dll'
      \\\}
    \\}
  \}
}

macro __create_all_imports {
   __create_dll_list

   match liblist, DynamicLibList \{
     library liblist
   \}

  rept DynamicLibList@count i:1 \{
    match name:filename, DynamicLibList\#i \\{
      \\include filename
    \\}
  \}
}


macro __AllImportSection {
;  section '.idata' import readable
;    __create_all_imports
}




