{-
  Steps required:
    1. Uniqify all load and store addresses for non-external globals by incrementing everything
      1. build for each file the increment for variable locations for that file

    2. For each external variable in each file
      1. Find the file which declares it (or error)
      2. Get the (now unique) address of that global
      3. Replace all loads and stores to the external variable name in the current file with that address

    3. Uniquify all loop labels by incrementing everything

    functions required:

    numloops(file)
    numGlobalsExported(file)
    replaceLoadsStores(file, ext_name/address, newaddress)
-}

main = do
  args <- getArgs
  if length args > 1 then do
    tables <- mapM buildTableForFile args
    files <- mapM buildProgramForFile args
    let filesAndTables = zip files tables
    let globalIncrements = buildGlobalIncrements tables
    let uniqified = uniqifyLoadsAndStores filesAndTables globalIncrements
    --putStrLn $ concatMap show tables
    --putStrLn $ concatMap show $ map numGlobalsExported tables
    --mapM_ (putStrLn . show) files
    --mapM_ (putStrLn . show) globalIncrements
    --mapM_ (putStrLn . show) uniqified
    let outputCode = [(head files)] ++ (map fst uniqified)
    putStrLn $ B.unpack $ dumpAssembler outputCode
  else do
    error $ "Usage: tld <1.asm> <2.asm> ... <n.asm> <output.asm>"
main = do
  args <- getArgs
  if length args > 1 then do
    tables <- mapM buildTableForFile args
    files <- mapM buildProgramForFile args
    let filesAndTables = zip files tables
    let globalIncrements = buildGlobalIncrements tables
    let uniqified = uniqifyLoadsAndStores filesAndTables globalIncrements
    --putStrLn $ concatMap show tables
    --putStrLn $ concatMap show $ map numGlobalsExported tables
    --mapM_ (putStrLn . show) files
    --mapM_ (putStrLn . show) globalIncrements
    --mapM_ (putStrLn . show) uniqified
    let outputCode = [(head files)] ++ (map fst uniqified)
    putStrLn $ B.unpack $ dumpAssembler outputCode
  else do
    error $ "Usage: tld <1.asm> <2.asm> ... <n.asm> <output.asm>"
main = do
  args <- getArgs
  if length args > 1 then do
    tables <- mapM buildTableForFile args
    files <- mapM buildProgramForFile args
    let filesAndTables = zip files tables
    let globalIncrements = buildGlobalIncrements tables
    let uniqified = uniqifyLoadsAndStores filesAndTables globalIncrements
    --putStrLn $ concatMap show tables
    --putStrLn $ concatMap show $ map numGlobalsExported tables
    --mapM_ (putStrLn . show) files
    --mapM_ (putStrLn . show) globalIncrements
    --mapM_ (putStrLn . show) uniqified
    let outputCode = [(head files)] ++ (map fst uniqified)
    putStrLn $ B.unpack $ dumpAssembler outputCode
  else do
    error $ "Usage: tld <1.asm> <2.asm> ... <n.asm> <output.asm>"