?login_element?

Subversion Repositories NedoOS

Rev

Rev 625 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  
  3.   SjASMPlus Z80 Cross Compiler
  4.  
  5.   This is modified sources of SjASM by Aprisobal - aprisobal@tut.by
  6.  
  7.   Copyright (c) 2006 Sjoerd Mastijn
  8.  
  9.   This software is provided 'as-is', without any express or implied warranty.
  10.   In no event will the authors be held liable for any damages arising from the
  11.   use of this software.
  12.  
  13.   Permission is granted to anyone to use this software for any purpose,
  14.   including commercial applications, and to alter it and redistribute it freely,
  15.   subject to the following restrictions:
  16.  
  17.   1. The origin of this software must not be misrepresented; you must not claim
  18.          that you wrote the original software. If you use this software in a product,
  19.          an acknowledgment in the product documentation would be appreciated but is
  20.          not required.
  21.  
  22.   2. Altered source versions must be plainly marked as such, and must not be
  23.          misrepresented as being the original software.
  24.  
  25.   3. This notice may not be removed or altered from any source distribution.
  26.  
  27. */
  28.  
  29. // sjasm.cpp
  30.  
  31. #include "sjdefs.h"
  32. #include <cstdlib>
  33. #include <chrono>
  34. #include <ctime>
  35.  
  36. static void PrintHelpMain() {
  37.         // Please keep help lines at most 79 characters long (cursor at column 88 after last char)
  38.         //     |<-- ...8901234567890123456789012345678901234567890123456789012... 80 chars -->|
  39.         _COUT "Based on code of SjASM by Sjoerd Mastijn (http://www.xl2s.tk)" _ENDL;
  40.         _COUT "Copyright 2004-2022 by Aprisobal and all other participants" _ENDL;
  41.         //_COUT "Patches by Antipod / boo_boo / PulkoMandy and others" _ENDL;
  42.         //_COUT "Tidy up by Tygrys / UB880D / Cizo / mborik / z00m" _ENDL;
  43.         _COUT "\nUsage:\nsjasmplus [options] sourcefile(s)" _ENDL;
  44.         _COUT "\nOption flags as follows:" _ENDL;
  45.         _COUT "  -h or --help[=warnings]  Help information (you see it)" _ENDL;
  46.         _COUT "  --zxnext[=cspect]        Enable ZX Spectrum Next Z80 extensions (Z80N)" _ENDL;
  47.         _COUT "  --i8080                  Limit valid instructions to i8080 only (+ no fakes)" _ENDL;
  48.         _COUT "  --lr35902                Sharp LR35902 CPU instructions mode (+ no fakes)" _ENDL;
  49.         _COUT "  --outprefix=<path>       Prefix for save/output/.. filenames in directives" _ENDL;
  50.         _COUT "  -i<path> or -I<path> or --inc=<path> ( --inc without \"=\" to empty the list)" _ENDL;
  51.         _COUT "                           Include path (later defined have higher priority)" _ENDL;
  52.         _COUT "  --lst[=<filename>]       Save listing to <filename> (<source>.lst is default)" _ENDL;
  53.         _COUT "  --lstlab[=sort]          Append [sorted] symbol table to listing" _ENDL;
  54.         _COUT "  --sym=<filename>         Save symbol table to <filename>" _ENDL;
  55.         _COUT "  --exp=<filename>         Save exports to <filename> (see EXPORT pseudo-op)" _ENDL;
  56.         //_COUT "  --autoreloc              Switch to autorelocation mode. See more in docs." _ENDL;
  57.         _COUT "  --raw=<filename>         Machine code saved also to <filename> (- is STDOUT)" _ENDL;
  58.         _COUT "  --sld[=<filename>]       Save Source Level Debugging data to <filename>" _ENDL;
  59.         _COUT " Note: use OUTPUT, LUA/ENDLUA and other pseudo-ops to control output" _ENDL;
  60.         _COUT " Logging:" _ENDL;
  61.         _COUT "  --nologo                 Do not show startup message" _ENDL;
  62.         _COUT "  --msg=[all|war|err|none|lst|lstlab]" _ENDL;
  63.         _COUT "                           Stderr messages verbosity (\"all\" is default)" _ENDL;
  64.         _COUT "  --fullpath               Show full path to file in errors" _ENDL;
  65.         _COUT "  --color=[on|off|auto]    Enable or disable ANSI coloring of warnings/errors" _ENDL;
  66.         _COUT " Other:" _ENDL;
  67.         _COUT "  -D<NAME>[=<value>]       Define <NAME> as <value>" _ENDL;
  68.         _COUT "  -                        Reads STDIN as source (even in between regular files)" _ENDL;
  69.         _COUT "  --longptr                No device: program counter $ can go beyond 0x10000" _ENDL;
  70.         _COUT "  --reversepop             Enable reverse POP order (as in base SjASM version)" _ENDL;
  71.         _COUT "  --dirbol                 Enable directives from the beginning of line" _ENDL;
  72.         _COUT "  --dos866                 Encode from Windows codepage to DOS 866 (Cyrillic)" _ENDL;
  73.         _COUT "  --syntax=<...>           Adjust parsing syntax, check docs for details." _ENDL;
  74. }
  75.  
  76. namespace Options {
  77.         const STerminalColorSequences tcols_ansi = {
  78.                 /*end*/         "\033[m",
  79.                 /*display*/     "\033[36m",             // Cyan
  80.                 /*warning*/     "\033[33m",             // Yellow
  81.                 /*error*/       "\033[31m",             // Red
  82.                 /*bold*/        "\033[1m"               // bold
  83.         };
  84.  
  85.         const STerminalColorSequences tcols_none = {
  86.                 /*end*/         "",
  87.                 /*display*/     "",
  88.                 /*warning*/     "",
  89.                 /*error*/       "",
  90.                 /*bold*/        ""
  91.         };
  92.  
  93.         const STerminalColorSequences* tcols = &tcols_none;
  94.  
  95.         void SetTerminalColors(bool enabled) {
  96.                 tcols = enabled ? &tcols_ansi : &tcols_none;
  97.         }
  98.  
  99.         char OutPrefix[LINEMAX] = {0};
  100.         char SymbolListFName[LINEMAX] = {0};
  101.         char ListingFName[LINEMAX] = {0};
  102.         char ExportFName[LINEMAX] = {0};
  103.         char DestinationFName[LINEMAX] = {0};
  104.         char RAWFName[LINEMAX] = {0};
  105.         char UnrealLabelListFName[LINEMAX] = {0};
  106.         char CSpectMapFName[LINEMAX] = {0};
  107.         int CSpectMapPageSize = 0x4000;
  108.         char SourceLevelDebugFName[LINEMAX] = {0};
  109.         bool IsDefaultSldName = false;
  110.  
  111.         char ZX_SnapshotFName[LINEMAX] = {0};
  112.         char ZX_TapeFName[LINEMAX] = {0};
  113.  
  114.         EOutputVerbosity OutputVerbosity = OV_ALL;
  115.         bool IsLabelTableInListing = 0;
  116.         bool IsDefaultListingName = false;
  117.         bool IsShowFullPath = 0;
  118.         bool AddLabelListing = false;
  119.         bool HideLogo = 0;
  120.         bool ShowHelp = false;
  121.         bool ShowHelpWarnings = false;
  122.         bool ShowVersion = false;
  123.         bool NoDestinationFile = true;          // no *.out file by default
  124.         SSyntax syx, systemSyntax;
  125.         bool IsI8080 = false;
  126.         bool IsLR35902 = false;
  127.         bool IsLongPtr = false;
  128.         bool SortSymbols = false;
  129.         bool IsBigEndian = false;
  130.         bool EmitVirtualLabels = false;
  131.  
  132.         // Include directories list is initialized with "." directory
  133.         CStringsList* IncludeDirsList = new CStringsList(".");
  134.  
  135.         CDefineTable CmdDefineTable;            // is initialized by constructor
  136.  
  137.         static const char* fakes_disabled_txt_error = "Fake instructions are not enabled";
  138.         static const char* fakes_in_i8080_txt_error = "Fake instructions are not implemented in i8080 mode";
  139.         static const char* fakes_in_lr35902_txt_error = "Fake instructions are not implemented in Sharp LR35902 mode";
  140.  
  141.         // returns true if fakes are completely disabled, false when they are enabled
  142.         // showMessage=true: will also display error/warning (use when fake ins. is emitted)
  143.         // showMessage=false: can be used to silently check if fake instructions are even possible
  144.         bool noFakes(bool showMessage) {
  145.                 bool fakesDisabled = Options::IsI8080 || Options::IsLR35902 || (!syx.FakeEnabled);
  146.                 if (!showMessage) return fakesDisabled;
  147.                 if (fakesDisabled) {
  148.                         const char* errorTxt = fakes_disabled_txt_error;
  149.                         if (Options::IsI8080) errorTxt = fakes_in_i8080_txt_error;
  150.                         if (Options::IsLR35902) errorTxt = fakes_in_lr35902_txt_error;
  151.                         Error(errorTxt, bp, SUPPRESS);
  152.                         return true;
  153.                 }
  154.                 if (syx.FakeWarning) {
  155.                         WarningById(W_FAKE, bp);
  156.                 }
  157.                 return false;
  158.         }
  159.  
  160.         std::stack<SSyntax> SSyntax::syxStack;
  161.  
  162.         void SSyntax::resetCurrentSyntax() {
  163.                 new (&syx) SSyntax();   // restore defaults in current syntax
  164.         }
  165.  
  166.         void SSyntax::pushCurrentSyntax() {
  167.                 syxStack.push(syx);             // store current syntax options into stack
  168.         }
  169.  
  170.         bool SSyntax::popSyntax() {
  171.                 if (syxStack.empty()) return false;     // no syntax stored in stack
  172.                 syx = syxStack.top();   // copy the syntax values from stack
  173.                 syxStack.pop();
  174.                 return true;
  175.         }
  176.  
  177.         void SSyntax::restoreSystemSyntax() {
  178.                 while (!syxStack.empty()) syxStack.pop();       // empty the syntax stack first
  179.                 syx = systemSyntax;             // reset to original system syntax
  180.         }
  181.  
  182. } // eof namespace Options
  183.  
  184. static void PrintHelp(bool forceMainHelp) {
  185.         if (forceMainHelp || Options::ShowHelp) PrintHelpMain();
  186.         if (Options::ShowHelpWarnings) PrintHelpWarnings();
  187. }
  188.  
  189. CDevice *Devices = nullptr;
  190. CDevice *Device = nullptr;
  191. CDevicePage *Page = nullptr;
  192. char* DeviceID = nullptr;
  193. TextFilePos globalDeviceSourcePos;
  194. aint deviceDirectivesCount = 0;
  195. static char* globalDeviceID = nullptr;
  196. static aint globalDeviceZxRamTop = 0;
  197.  
  198. // extend
  199. const char* fileNameFull = nullptr, * fileName = nullptr;       //fileName is either full or basename (--fullpath)
  200. char* lp, line[LINEMAX], temp[LINEMAX], * bp;
  201. char sline[LINEMAX2], sline2[LINEMAX2], * substitutedLine, * eolComment, ModuleName[LINEMAX];
  202.  
  203. SSource::SSource(SSource && src) {      // move constructor, "pick" the stdin pointer
  204.         memcpy(fname, src.fname, MAX_PATH);
  205.         stdin_log = src.stdin_log;
  206.         src.fname[0] = 0;
  207.         src.stdin_log = nullptr;
  208. }
  209.  
  210. SSource::SSource(const char* newfname) : stdin_log(nullptr) {
  211.         STRNCPY(fname, MAX_PATH, newfname, MAX_PATH-1);
  212.         fname[MAX_PATH-1] = 0;
  213. }
  214.  
  215. SSource::SSource(int) {
  216.         fname[0] = 0;
  217.         stdin_log = new stdin_log_t();
  218.         stdin_log->reserve(50*1024);
  219. }
  220.  
  221. SSource::~SSource() {
  222.         if (stdin_log) delete stdin_log;
  223. }
  224.  
  225. std::vector<SSource> sourceFiles;
  226.  
  227. int ConvertEncoding = ENCWIN;
  228.  
  229. EDispMode PseudoORG = DISP_NONE;
  230. bool IsLabelNotFound = false, IsSubstituting = false;
  231. int pass = 0, ErrorCount = 0, WarningCount = 0, IncludeLevel = -1;
  232. int IsRunning = 0, donotlist = 0, listmacro = 0;
  233. int adrdisp = 0, dispPageNum = LABEL_PAGE_UNDEFINED, StartAddress = -1;
  234. byte* MemoryPointer=NULL;
  235. int macronummer = 0, lijst = 0, reglenwidth = 0;
  236. source_positions_t sourcePosStack;
  237. source_positions_t smartSmcLines;
  238. source_positions_t::size_type smartSmcIndex;
  239. uint32_t maxlin = 0;
  240. aint CurAddress = 0, CompiledCurrentLine = 0, LastParsedLabelLine = 0, PredefinedCounter = 0;
  241. aint destlen = 0, size = -1L, comlin = 0;
  242. const char* CurrentDirectory=NULL;
  243.  
  244. char* vorlabp=NULL, * macrolabp=NULL, * LastParsedLabel=NULL;
  245. std::stack<SRepeatStack> RepeatStack;
  246. CStringsList* lijstp = NULL;
  247. CLabelTable LabelTable;
  248. CTemporaryLabelTable TemporaryLabelTable;
  249. CDefineTable DefineTable;
  250. CMacroDefineTable MacroDefineTable;
  251. CMacroTable MacroTable;
  252. CStructureTable StructureTable;
  253.  
  254. // reserve keywords in labels table, to detect when user is defining label colliding with keyword
  255. static void ReserveLabelKeywords() {
  256.         for (const char* keyword : {
  257.                 "abs", "and", "exist", "high", "low", "mod", "norel", "not", "or", "shl", "shr", "xor"
  258.         }) {
  259.                 LabelTable.Insert(keyword, -65536, LABEL_IS_UNDEFINED|LABEL_IS_KEYWORD);
  260.         }
  261. }
  262.  
  263. void InitPass() {
  264.         assert(sourcePosStack.empty());                         // there's no source position [left] in the stack
  265.         Relocation::InitPass();
  266.         Options::SSyntax::restoreSystemSyntax();        // release all stored syntax variants and reset to initial
  267.         uint32_t maxpow10 = 1;
  268.         reglenwidth = 0;
  269.         do {
  270.                 ++reglenwidth;
  271.                 maxpow10 *= 10;
  272.                 if (maxpow10 < 10) ExitASM(1);  // 32b overflow
  273.         } while (maxpow10 <= maxlin);
  274.         *ModuleName = 0;
  275.         SetLastParsedLabel(nullptr);
  276.         if (vorlabp) free(vorlabp);
  277.         vorlabp = STRDUP("_");
  278.         macrolabp = NULL;
  279.         listmacro = 0;
  280.         CurAddress = 0;
  281.         CompiledCurrentLine = 0;
  282.         smartSmcIndex = 0;
  283.         PseudoORG = DISP_NONE; adrdisp = 0; dispPageNum = LABEL_PAGE_UNDEFINED;
  284.         ListAddress = 0; macronummer = 0; lijst = 0; comlin = 0;
  285.         lijstp = NULL;
  286.         DidEmitByte();                          // reset the emitted flag
  287.         StructureTable.ReInit();
  288.         MacroTable.ReInit();
  289.         MacroDefineTable.ReInit();
  290.         DefineTable = Options::CmdDefineTable;
  291.         TemporaryLabelTable.InitPass();
  292.  
  293.         // reset "device" stuff + detect "global device" directive
  294.         if (globalDeviceID) {           // globalDeviceID detector has to trigger before every pass
  295.                 free(globalDeviceID);
  296.                 globalDeviceID = nullptr;
  297.         }
  298.         if (1 < pass && 1 == deviceDirectivesCount && Devices) {        // only single DEVICE used
  299.                 globalDeviceID = STRDUP(Devices->ID);           // make it global for next pass
  300.                 globalDeviceZxRamTop = Devices->ZxRamTop;
  301.         }
  302.         if (Devices) delete Devices;
  303.         Devices = Device = nullptr;
  304.         DeviceID = nullptr;
  305.         Page = nullptr;
  306.         deviceDirectivesCount = 0;
  307.         // resurrect "global" device here
  308.         if (globalDeviceID) {
  309.                 sourcePosStack.push_back(globalDeviceSourcePos);
  310.                 if (!SetDevice(globalDeviceID, globalDeviceZxRamTop)) {         // manually tested (remove "!")
  311.                         Error("Failed to re-initialize global device", globalDeviceID, FATAL);
  312.                 }
  313.                 sourcePosStack.pop_back();
  314.         }
  315.  
  316.         // predefined defines - (deprecated) classic sjasmplus v1.x (till v1.15.1)
  317.         DefineTable.Replace("_SJASMPLUS", "1");
  318.         DefineTable.Replace("_RELEASE", "0");
  319.         DefineTable.Replace("_VERSION", "__VERSION__");
  320.         DefineTable.Replace("_ERRORS", "__ERRORS__");
  321.         DefineTable.Replace("_WARNINGS", "__WARNINGS__");
  322.         // predefined defines - sjasmplus v2.x-like (since v1.16.0)
  323.         // __DATE__ and __TIME__ are defined just once in main(...) (stored in Options::CmdDefineTable)
  324.         DefineTable.Replace("__SJASMPLUS__", VERSION_NUM);              // modified from _SJASMPLUS
  325.         DefineTable.Replace("__VERSION__", "\"" VERSION "\"");  // migrated from _VERSION
  326.         DefineTable.Replace("__ERRORS__", ErrorCount);                  // migrated from _ERRORS (can be already > 0 from earlier pass)
  327.         DefineTable.Replace("__WARNINGS__", WarningCount);              // migrated from _WARNINGS (can be already > 0 from earlier pass)
  328.         DefineTable.Replace("__PASS__", pass);                                  // current pass of assembler
  329.         DefineTable.Replace("__INCLUDE_LEVEL__", "-1");                 // include nesting
  330.         DefineTable.Replace("__BASE_FILE__", "<none>");                 // the include-level 0 file
  331.         DefineTable.Replace("__FILE__", "<none>");                              // current file
  332.         DefineTable.Replace("__LINE__", "<dynamic value>");             // current line in current file
  333.         DefineTable.Replace("__COUNTER__", "<dynamic value>");  // gcc-like, incremented upon every use
  334.         PredefinedCounter = 0;
  335. }
  336.  
  337. void FreeRAM() {
  338.         if (Devices) {
  339.                 delete Devices;         Devices = nullptr;
  340.         }
  341.         if (globalDeviceID) {
  342.                 free(globalDeviceID);   globalDeviceID = nullptr;
  343.         }
  344.         for (CDeviceDef* deviceDef : DefDevices) delete deviceDef;
  345.         DefDevices.clear();
  346.         lijstp = NULL;          // do not delete this, should be released by owners of DUP/regular macros
  347.         free(vorlabp);          vorlabp = NULL;
  348.         LabelTable.RemoveAll();
  349.         DefineTable.RemoveAll();
  350.         SetLastParsedLabel(nullptr);
  351.         if (PreviousIsLabel) {
  352.                 free(PreviousIsLabel);
  353.                 PreviousIsLabel = nullptr;
  354.         }
  355.         if (Options::IncludeDirsList) delete Options::IncludeDirsList;
  356.         ReleaseArchivedFilenames();
  357. }
  358.  
  359.  
  360. void ExitASM(int p) {
  361.         FreeRAM();
  362.         if (pass == LASTPASS) {
  363.                 Close();
  364.         }
  365.         exit(p);
  366. }
  367.  
  368. namespace Options {
  369.  
  370.         class COptionsParser {
  371.         private:
  372.                 const char* arg;
  373.                 char opt[LINEMAX];
  374.                 char val[LINEMAX];
  375.  
  376.                 // returns 1 when argument was processed (keyword detected, value copied into buffer)
  377.                 // If buffer == NULL, only detection of keyword + check for non-zero "value" is done (no copy)
  378.                 int CheckAssignmentOption(const char* keyword, char* buffer, const size_t bufferSize) {
  379.                         if (strcmp(keyword, opt)) return 0;             // detect "keyword" (return 0 if not)
  380.                         if (*val) {
  381.                                 if (NULL != buffer) STRCPY(buffer, bufferSize, val);
  382.                         } else {
  383.                                 Error("no parameters found in", arg, ALL);
  384.                         }
  385.                         return 1;       // keyword detected, option was processed
  386.                 }
  387.  
  388.                 static void splitByChar(const char* s, const int splitter,
  389.                                                            char* v1, const size_t v1Size,
  390.                                                            char* v2, const size_t v2Size) {
  391.                         // only non-zero splitter character is supported
  392.                         const char* spos = splitter ? STRCHR(s, splitter) : NULL;
  393.                         if (NULL == spos) {
  394.                                 // splitter character not found, copy whole input string into v1, v2 = empty string
  395.                                 STRCPY(v1, v1Size, s);
  396.                                 v2[0] = 0;
  397.                         } else {
  398.                                 // splitter found, copy string ahead splitter to v1, after it to v2
  399.                                 STRNCPY(v1, v1Size, s, spos - s);
  400.                                 v1[spos - s] = 0;
  401.                                 STRCPY(v2, v2Size, spos + 1);
  402.                         }
  403.                 }
  404.  
  405.                 void parseSyntaxValue() {
  406.                         // Options::syx is expected to be already in default state before entering this
  407.                         for (const auto & syntaxOption : val) {
  408.                                 switch (syntaxOption) {
  409.                                 case 0:   return;
  410.                                 // f F - instructions: fake warning, no fakes (default = fake enabled)
  411.                                 case 'f': syx.FakeEnabled = syx.FakeWarning = true; break;
  412.                                 case 'F': syx.FakeEnabled = false; break;
  413.                                 // a - multi-argument delimiter ",," (default is ",")
  414.                                 case 'a': syx.MultiArg = &doubleComma; break;
  415.                                 // b - single parentheses enforce mem access (default = relaxed syntax)
  416.                                 case 'b': syx.MemoryBrackets = 1; break;
  417.                                 // B - memory access brackets [] required (default = relaxed syntax)
  418.                                 case 'B': syx.MemoryBrackets = 2; break;
  419.                                 // l L - warn/error about labels using keywords (default = no message)
  420.                                 case 'l':
  421.                                 case 'L':
  422.                                 {
  423.                                         const char this_option_is[2] = { syntaxOption, 0 };
  424.                                         Error("Syntax option not implemented yet", this_option_is, PASS03);
  425.                                         break;
  426.                                 }
  427.                                 // i - case insensitive instructions/directives (default = same case required)
  428.                                 case 'i': syx.CaseInsensitiveInstructions = true; break;
  429.                                 // w - warnings option: report warnings as errors
  430.                                 case 'w': syx.WarningsAsErrors = true; break;
  431.                                 // M - alias "m" and "M" for "(hl)" to cover 8080-like syntax: ADD A,M
  432.                                 case 'M': syx.Is_M_Memory = true; break;
  433.                                 // s - switch off sub-word substitution in DEFINEs (s like "Simple defines" or "Sub word")
  434.                                 case 's': syx.IsSubwordSubstitution = false; break;
  435.                                 // unrecognized option
  436.                                 default:
  437.                                         const char this_option_is[2] = { syntaxOption, 0 };
  438.                                         Error("Unrecognized syntax option", this_option_is, PASS03);
  439.                                         break;
  440.                                 }
  441.                         }
  442.                 }
  443.  
  444.         public:
  445.                 void GetOptions(const char* const * const argv, int& i, bool onlySyntaxOptions = false) {
  446.                         while ((arg=argv[i]) && ('-' == arg[0])) {
  447.                                 bool doubleDash = false;
  448.                                 // copy "option" (up to '=' char) into `opt`, copy "value" (after '=') into `val`
  449.                                 if ('-' == arg[1]) {    // double-dash detected, value is expected after "="
  450.                                         doubleDash = true;
  451.                                         splitByChar(arg + 2, '=', opt, LINEMAX, val, LINEMAX);
  452.                                 } else {                                // single dash, parse value from second character onward
  453.                                         opt[0] = arg[1];        // copy only single letter into `opt`
  454.                                         opt[1] = 0;
  455.                                         if (opt[0]) {           // if it was not empty, try to copy also `val`
  456.                                                 STRCPY(val, LINEMAX, arg + 2);
  457.                                         }
  458.                                 }
  459.  
  460.                                 // check for particular options and setup option value by it
  461.                                 // first check all syntax-only options which may be modified by OPT directive
  462.                                 if (!strcmp(opt, "zxnext")) {
  463.                                         if (IsI8080) Error("Can't enable Next extensions while in i8080 mode", nullptr, FATAL);
  464.                                         if (IsLR35902) Error("Can't enable Next extensions while in Sharp LR35902 mode", nullptr, FATAL);
  465.                                         syx.IsNextEnabled = 1;
  466.                                         if (!strcmp(val, "cspect")) syx.IsNextEnabled = 2;      // CSpect emulator extensions
  467.                                 } else if (!strcmp(opt, "reversepop")) {
  468.                                         syx.IsReversePOP = true;
  469.                                 } else if (!strcmp(opt, "dirbol")) {
  470.                                         syx.IsPseudoOpBOF = true;
  471.                                 } else if (!strcmp(opt, "nofakes")) {
  472.                                         syx.FakeEnabled = false;
  473.                                         // was deprecated, as it is provided by `--syntax=F` too, but now I decided to keep also this older option
  474.                                         // (it's less cryptic than the --syntax letter soup, one duplicity of functionality will not end the world, right?)
  475.                                 } else if (!strcmp(opt, "syntax")) {
  476.                                         parseSyntaxValue();
  477.                                 } else if (!doubleDash && 'W' == opt[0]) {
  478.                                         CliWoption(val);
  479.                                 } else if (onlySyntaxOptions) {
  480.                                         // rest of the options is available only when launching the sjasmplus
  481.                                         return;
  482.                                 } else if (!strcmp(opt, "lr35902")) {
  483.                                         IsLR35902 = true;
  484.                                         // force (silently) other CPU modes OFF
  485.                                         IsI8080 = false;
  486.                                         syx.IsNextEnabled = 0;
  487.                                 } else if (!strcmp(opt, "i8080")) {
  488.                                         IsI8080 = true;
  489.                                         // force (silently) other CPU modes OFF
  490.                                         IsLR35902 = false;
  491.                                         syx.IsNextEnabled = 0;
  492.                                 } else if ((!doubleDash && 'h' == opt[0] && !val[0]) || (doubleDash && !strcmp(opt, "help"))) {
  493.                                         ShowHelp |= strcmp("warnings", val);
  494.                                         ShowHelpWarnings |= !strcmp("warnings", val);
  495.                                 } else if (doubleDash && !strcmp(opt, "version")) {
  496.                                         ShowVersion = true;
  497.                                 } else if (!strcmp(opt, "lstlab")) {
  498.                                         AddLabelListing = true;
  499.                                         if (val[0]) SortSymbols = !strcmp("sort", val);
  500.                                 } else if (!strcmp(opt, "longptr")) {
  501.                                         IsLongPtr = true;
  502.                                 } else if (CheckAssignmentOption("msg", NULL, 0)) {
  503.                                         if (!*val) {
  504.                                                 // nothing to do, CheckAssignmentOption already displayed error
  505.                                         } else if (!strcmp("none", val)) {
  506.                                                 OutputVerbosity = OV_NONE;
  507.                                                 HideLogo = true;
  508.                                         } else if (!strcmp("err", val)) {
  509.                                                 OutputVerbosity = OV_ERROR;
  510.                                         } else if (!strcmp("war", val)) {
  511.                                                 OutputVerbosity = OV_WARNING;
  512.                                         } else if (!strcmp("all", val)) {
  513.                                                 OutputVerbosity = OV_ALL;
  514.                                         } else if (!strcmp("lst", val)) {
  515.                                                 OutputVerbosity = OV_LST;
  516.                                                 AddLabelListing = false;
  517.                                                 HideLogo = true;
  518.                                         } else if (!strcmp("lstlab", val)) {
  519.                                                 OutputVerbosity = OV_LST;
  520.                                                 AddLabelListing = true;
  521.                                                 SortSymbols = true;
  522.                                                 HideLogo = true;
  523.                                         } else {
  524.                                                 Error("unexpected parameter in", arg, ALL);
  525.                                         }
  526.                                 } else if (!strcmp(opt, "lst") && !val[0]) {
  527.                                         IsDefaultListingName = true;
  528.                                 } else if (!strcmp(opt, "sld") && !val[0]) {
  529.                                         IsDefaultSldName = true;
  530.                                 } else if (
  531.                                         CheckAssignmentOption("outprefix", OutPrefix, LINEMAX) ||
  532.                                         CheckAssignmentOption("sym", SymbolListFName, LINEMAX) ||
  533.                                         CheckAssignmentOption("lst", ListingFName, LINEMAX) ||
  534.                                         CheckAssignmentOption("exp", ExportFName, LINEMAX) ||
  535.                                         CheckAssignmentOption("sld", SourceLevelDebugFName, LINEMAX) ||
  536.                                         CheckAssignmentOption("raw", RAWFName, LINEMAX) ) {
  537.                                         // was proccessed inside CheckAssignmentOption function
  538.                                 } else if (!strcmp(opt, "fullpath")) {
  539.                                         IsShowFullPath = 1;
  540.                                 } else if (!strcmp(opt, "color")) {
  541.                                         if (!strcmp("on", val)) {
  542.                                                 SetTerminalColors(true);
  543.                                         } else if (!strcmp("off", val)) {
  544.                                                 SetTerminalColors(false);
  545.                                         } else if (!strcmp("auto", val)) {
  546.                                                 // already heuristically detected, nothing to do
  547.                                         } else {
  548.                                                 Error("invalid --color setting (use: on|off|auto)", val, ALL);
  549.                                         }
  550.                                 } else if (!strcmp(opt, "nologo")) {
  551.                                         HideLogo = 1;
  552.                                 } else if (!strcmp(opt, "dos866")) {
  553.                                         ConvertEncoding = ENCDOS;
  554.                                 } else if ((doubleDash && !strcmp(opt, "inc")) ||
  555.                                                         (!doubleDash && 'i' == opt[0]) ||
  556.                                                         (!doubleDash && 'I' == opt[0])) {
  557.                                         if (*val) {
  558.                                                 IncludeDirsList = new CStringsList(val, IncludeDirsList);
  559.                                         } else {
  560.                                                 if (!doubleDash || '=' == arg[5]) {
  561.                                                         Error("no include path found in", arg, ALL);
  562.                                                 } else {        // individual `--inc` without "=path" will RESET include dirs
  563.                                                         if (IncludeDirsList) delete IncludeDirsList;
  564.                                                         IncludeDirsList = nullptr;
  565.                                                 }
  566.                                         }
  567.                                 } else if (!doubleDash && 'D' == opt[0]) {
  568.                                         char defN[LINEMAX], defV[LINEMAX];
  569.                                         if (*val) {             // for -Dname=value the `val` contains "name=value" string
  570.                                                 splitByChar(val, '=', defN, LINEMAX, defV, LINEMAX);
  571.                                                 CmdDefineTable.Add(defN, defV, NULL);
  572.                                         } else {
  573.                                                 Error("no parameters found in", arg, ALL);
  574.                                         }
  575.                                 } else if (!doubleDash && 0 == opt[0]) {
  576.                                         // only single "-" was on command line = source STDIN
  577.                                         sourceFiles.push_back(SSource(1));              // special constructor for stdin input
  578.                                 } else {
  579.                                         Error("unrecognized option", arg, ALL);
  580.                                 }
  581.  
  582.                                 ++i;                                    // next CLI argument
  583.                         } // end of while ((arg=argv[i]) && ('-' == arg[0]))
  584.                 }
  585.         };
  586.  
  587.         int parseSyntaxOptions(int n, char** options) {
  588.                 if (n <= 0) return 0;
  589.                 int i = 0;
  590.                 Options::COptionsParser optParser;
  591.                 optParser.GetOptions(options, i, true);
  592.                 return i;
  593.         }
  594. }
  595.  
  596. // ==============================================================================================
  597. // == UnitTest++ part, checking if unit tests are requested and does launch test-runner then   ==
  598. // ==============================================================================================
  599. #ifdef ADD_UNIT_TESTS
  600.  
  601. # include "UnitTest++/UnitTest++.h"
  602.  
  603. # define STOP_MAKE_BY_NON_ZERO_EXIT_CODE 0
  604.  
  605. //detect "--unittest" switch, prepare UnitTest++, run the test runner, collect results, exit
  606. # define CHECK_UNIT_TESTS \
  607.         { \
  608.                 if (2 == argc && !strcmp("--unittest", argv[1])) { \
  609.                         _COUT "SjASMPlus \033[96mv" VERSION "\033[0m | \033[95mrunning unit tests:\033[0m" _ENDL _END \
  610.                         int exitCode = STOP_MAKE_BY_NON_ZERO_EXIT_CODE + UnitTest::RunAllTests(); \
  611.                         if (exitCode) _COUT "\033[91mNon-zero result from test runner!\033[0m" _ENDL _END \
  612.                         else _COUT "\033[92mOK: 0 UnitTest++ tests failed.\033[0m" _ENDL _END \
  613.                         exit(exitCode); \
  614.                 } \
  615.         }
  616. #else
  617.  
  618. # define CHECK_UNIT_TESTS { /* no unit tests in this build */ }
  619.  
  620. #endif
  621.  
  622. // == end of UnitTest++ part ====================================================================
  623.  
  624. static char launch_directory[MAX_PATH];
  625.  
  626. #ifdef WIN32
  627. int main(int argc, char* argv[]) {
  628. #else
  629. int main(int argc, char **argv) {
  630. #endif
  631.         // existence of NO_COLOR env.var. disables auto-colors: http://no-color.org/
  632.         const char* envNoColor = std::getenv("NO_COLOR");
  633.         // try to auto-detect ANSI-colour support (true if env.var. TERM exist and contains "color" substring)
  634.         const char* envTerm = std::getenv("TERM");
  635.         Options::SetTerminalColors(!envNoColor && envTerm && strstr(envTerm, "color"));
  636.  
  637.         const char* logo = "SjASMPlus Z80 Cross-Assembler v" VERSION " (https://github.com/z00m128/sjasmplus)";
  638.  
  639.         sourcePosStack.reserve(32);
  640.         smartSmcLines.reserve(64);
  641.         sourceFiles.reserve(32);
  642.         archivedFileNames.reserve(64);
  643.  
  644.         CHECK_UNIT_TESTS                // UnitTest++ extra handling in specially built executable
  645.  
  646.         const word little_endian_test[] = { 0x1234 };
  647.         const byte le_test_byte = *reinterpret_cast<const byte*>(little_endian_test);
  648.         Options::IsBigEndian = (0x12 == le_test_byte);
  649.  
  650.         // start counter
  651.         long dwStart = GetTickCount();
  652.  
  653.         // get current directory
  654.         SJ_GetCurrentDirectory(MAX_PATH, launch_directory);
  655.         CurrentDirectory = launch_directory;
  656.  
  657.         Options::COptionsParser optParser;
  658.         char* envFlags = std::getenv("SJASMPLUSOPTS");
  659.         if (nullptr != envFlags) {
  660.                 // split environment arguments into "argc, argv" like variables (by white-space)
  661.                 char* parsedOptsArray[33] {};   // there must be one more nullptr in the array (32+1)
  662.                 int optI = 0, charI = 0;
  663.                 while (optI < 32 && !SkipBlanks(envFlags)) {
  664.                         parsedOptsArray[optI++] = temp + charI;
  665.                         while (*envFlags && !White(*envFlags) && charI < LINEMAX-1) temp[charI++] = *envFlags++;
  666.                         temp[charI++] = 0;
  667.                 }
  668.                 if (!SkipBlanks(envFlags)) {
  669.                         Error("SJASMPLUSOPTS environment variable contains too many options (max is 32)", nullptr, ALL);
  670.                 }
  671.                 // process environment variable ahead of command line options (in the same way)
  672.                 int i = 0;
  673.                 while (parsedOptsArray[i]) {
  674.                         optParser.GetOptions(parsedOptsArray, i);
  675.                         if (!parsedOptsArray[i]) break;
  676.                         sourceFiles.push_back(SSource(parsedOptsArray[i++]));
  677.                 }
  678.         }
  679.  
  680.         // setup __DATE__ and __TIME__ macros (setup them just once, not every pass!)
  681.         auto now = std::chrono::system_clock::now();
  682.         std::time_t now_c = std::chrono::system_clock::to_time_t(now);
  683.         std::tm now_tm = *std::localtime(&now_c);       // lgtm [cpp/potentially-dangerous-function]
  684.         char dateBuffer[32] = {}, timeBuffer[32] = {};
  685.         SPRINTF3(dateBuffer, 30, "\"%04d-%02d-%02d\"", now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday);
  686.         SPRINTF3(timeBuffer, 30, "\"%02d:%02d:%02d\"", now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec);
  687.         Options::CmdDefineTable.Add("__DATE__", dateBuffer, nullptr);
  688.         Options::CmdDefineTable.Add("__TIME__", timeBuffer, nullptr);
  689.  
  690.         int i = 1;
  691.         if (argc > 1) {
  692.                 while (argv[i]) {
  693.                         optParser.GetOptions(argv, i);
  694.                         if (!argv[i]) break;
  695.                         sourceFiles.push_back(SSource(argv[i++]));
  696.                 }
  697.         }
  698.         // warn about BE-host only when there's any CLI argument && after CLI options were parsed
  699.         if (2 <= argc && Options::IsBigEndian) WarningById(W_BE_HOST, nullptr, W_EARLY);
  700.         if (Options::IsDefaultListingName && Options::ListingFName[0]) {
  701.                 Error("Using both  --lst  and  --lst=<filename>  is not possible.", NULL, FATAL);
  702.         }
  703.         if (OV_LST == Options::OutputVerbosity && (Options::IsDefaultListingName || Options::ListingFName[0])) {
  704.                 Error("Using  --msg=lst[lab]  and other list options is not possible.", NULL, FATAL);
  705.         }
  706.         if (Options::IsDefaultSldName && Options::SourceLevelDebugFName[0]) {
  707.                 Error("Using both  --sld  and  --sld=<filename>  is not possible.", NULL, FATAL);
  708.         }
  709.         Options::systemSyntax = Options::syx;           // create copy of initial system settings of syntax
  710.  
  711.         if (argc == 1 || Options::ShowHelp || Options::ShowHelpWarnings) {
  712.                 _COUT logo _ENDL;
  713.                 PrintHelp(argc == 1);
  714.                 exit(argc == 1);
  715.         }
  716.  
  717.         if (!Options::HideLogo) {
  718.                 _CERR logo _ENDL;
  719.         }
  720.  
  721.         if (!Options::IsShowFullPath && (Options::IsDefaultSldName || Options::SourceLevelDebugFName[0])) {
  722.                 Warning("missing  --fullpath  with  --sld  may produce incomplete file paths.", NULL, W_EARLY);
  723.         }
  724.  
  725.         if (Options::ShowVersion) {
  726.                 if (Options::HideLogo) {        // if "sjasmplus --version --nologo", emit only the raw VERSION
  727.                         _CERR VERSION _ENDL;
  728.                 }
  729.                 // otherwise the full logo was already printed
  730.                 // now check if there were some sources to assemble, if NOT, exit with "OK"!
  731.                 if (0 == sourceFiles.size()) exit(0);
  732.         }
  733.  
  734.         // exit with error if no input file were specified
  735.         if (0 == sourceFiles.size()) {
  736.                 Error("no inputfile(s)", nullptr, ALL);
  737.                 exit(1);
  738.         }
  739.  
  740.         // create default output name, if not specified
  741.         ConstructDefaultFilename(Options::DestinationFName, LINEMAX, ".out");
  742.         int base_encoding = ConvertEncoding;
  743.  
  744.         // init some vars
  745.         InitCPU();
  746.  
  747.         // open lists (if not set to "default" file name, then the OpenFile will handle it)
  748.         OpenList();
  749.  
  750.         ReserveLabelKeywords();
  751.  
  752.         do {
  753.                 ++pass;
  754.                 if (pass == LASTPASS) OpenSld();        //open source level debugging file (BEFORE InitPass)
  755.                 InitPass();
  756.                 if (pass == LASTPASS) OpenDest();
  757.  
  758.                 for (SSource & src : sourceFiles) {
  759.                         IsRunning = 1;
  760.                         ConvertEncoding = base_encoding;
  761.                         OpenFile(src.fname, false, src.stdin_log);
  762.                 }
  763.  
  764.                 while (!RepeatStack.empty()) {
  765.                         sourcePosStack.push_back(RepeatStack.top().sourcePos);  // mark DUP line with error
  766.                         Error("[DUP/REPT] missing EDUP/ENDR to end repeat-block");
  767.                         sourcePosStack.pop_back();
  768.                         RepeatStack.pop();
  769.                 }
  770.  
  771.                 if (DISP_NONE != PseudoORG) {
  772.                         CurAddress = adrdisp;
  773.                         PseudoORG = DISP_NONE;
  774.                 }
  775.  
  776.                 if (Options::OutputVerbosity <= OV_ALL) {
  777.                         if (pass != LASTPASS) {
  778.                                 _CERR "Pass " _CMDL pass _CMDL " complete (" _CMDL ErrorCount _CMDL " errors)" _ENDL;
  779.                         } else {
  780.                                 _CERR "Pass 3 complete" _ENDL;
  781.                         }
  782.                 }
  783.         } while (pass < LASTPASS);
  784.  
  785.         pass = 9999; /* added for detect end of compiling */
  786.  
  787.         // dump label table into listing file, the explicit one (Options::IsDefaultListingName == false)
  788.         if (Options::AddLabelListing) LabelTable.Dump();
  789.  
  790.         Close();
  791.  
  792.         if (Options::UnrealLabelListFName[0]) {
  793.                 LabelTable.DumpForUnreal();
  794.         }
  795.  
  796.         if (Options::CSpectMapFName[0]) {
  797.                 LabelTable.DumpForCSpect();
  798.         }
  799.  
  800.         if (Options::SymbolListFName[0]) {
  801.                 LabelTable.DumpSymbols();
  802.         }
  803.  
  804.         if (Options::OutputVerbosity <= OV_ALL) {
  805.                 _CERR "Errors: " _CMDL ErrorCount _CMDL ", warnings: " _CMDL WarningCount _CMDL ", compiled: " _CMDL CompiledCurrentLine _CMDL " lines" _END;
  806.  
  807.                 double dwCount;
  808.                 dwCount = GetTickCount() - dwStart;
  809.                 if (dwCount < 0) dwCount = 0;
  810.                 char workTimeTxt[200] = "";
  811.                 SPRINTF1(workTimeTxt, 200, ", work time: %.3f seconds", dwCount / 1000);
  812.  
  813.                 _CERR workTimeTxt _ENDL;
  814.         }
  815.  
  816.         cout << flush;
  817.  
  818.         // free RAM
  819.         FreeRAM();
  820.  
  821.         lua_impl_close();
  822.  
  823.         return (ErrorCount != 0);
  824. }
  825. //eof sjasm.cpp
  826.