{"id":55,"date":"2025-01-01T12:34:44","date_gmt":"2025-01-01T12:34:44","guid":{"rendered":"https:\/\/www.soapyfrog.com\/?p=55"},"modified":"2025-01-01T16:51:21","modified_gmt":"2025-01-01T16:51:21","slug":"on-the-dangers-of-an-unfocused-mind","status":"publish","type":"post","link":"https:\/\/www.soapyfrog.com\/index.php\/2025\/01\/01\/on-the-dangers-of-an-unfocused-mind\/","title":{"rendered":"On the dangers of an unfocused mind"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the last post I noted that I have an <em>interpreter<\/em> for SoapyBASIC, but wanted to rework it as a compiler.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The work to-date has been very much side-hustle, personal interest, hasn&#8217;t had a lot of serious time devoted to it, and in doing so, spans about FOUR years!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In that time I&#8217;ve explored different ideas of what I wanted, what it should look and feel like, what the milestones might be, while all the time riding the wave of Swift evolving into a credible language.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As with all <em>old codebases<\/em> there are going to be things you want to do differently, and this is definitely the case here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In particular, the original goal was to <em>interpret<\/em> SoapyBASIC and include an interactive environment, like 1980s microcomputers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This has left me with a single, ever-mutating AST (abstract syntax tree) that evolves through parsing, multiple phases of semantic analysis and finally interpretation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift doesn&#8217;t really like mutable structures and since Swift 6 language model with strict data race safety, it&#8217;s become a bit of a witch-hunt.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Almost there, so let&#8217;s move goalposts!<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So I chose to implement the pipeline using immutable ASTs for each stage.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Lexer to produce a token stream (always was immutable)<\/li>\n\n\n\n<li>Parser to produce a parse only AST<\/li>\n\n\n\n<li>Semantic Analyser to produce a semantic AST<\/li>\n\n\n\n<li>IR generator to generate some intermediate representation, importantly being a flat structure, no longer a tree.<\/li>\n\n\n\n<li>An optimiser, probably<\/li>\n\n\n\n<li>Code generator to produce&#8230; what? Oh right\u2013<\/li>\n\n\n\n<li>A virtual machine with a specific ISA (instruction set architecture) to run the final code.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This was starting to look a lot like a full blown compiler pipeline!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">But there are still clear goals, right?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I have written a compiler before, but that was 30 odd years ago, for something C-like and didn&#8217;t have IR or any kind of optimiser. LLVM didn&#8217;t exist.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span style=\"font-family: var(--wp--preset--font-family--body); font-size: var(--wp--preset--font-size--medium);\">So I got distracted by looking at what my VM would look like<\/span>. I don&#8217;t like where x86_64 went. ARM64 is looking very complicated, RISC-V is promising, and of course I looked at simple stack-based VMs like Java.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I looked at how LLVM\/clang works today.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I pondered how I might have a VM where I had <em>syscalls<\/em> that would implement code loading, even compilation or an IDE\/REPL of some kind? <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I looked at how a compiler might hamper the ability to have a full REPL, although other languages manage this, it&#8217;s not a primary interface for them (Swift, Java, Python, etc).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All this meant three or four weeks of doing very little except making my goals look less well defined and further away.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I did however keep to the <em>immutable plan<\/em>, so to date, I have achieved:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 Lexer to produce a token stream (always was immutable)  <\/li>\n\n\n\n<li>\u2705 Parser to produce a parse only AST<\/li>\n\n\n\n<li>\u2705Semantic Analyser to produce a semantic AST<\/li>\n\n\n\n<li>\u2705 Lots of bug fixes in the original; more testing.<\/li>\n\n\n\n<li>\u2705 Migrated to <a href=\"https:\/\/developer.apple.com\/xcode\/swift-testing\/\">Swift Testing<\/a><\/li>\n\n\n\n<li>\u2705 <a href=\"https:\/\/www.swift.org\/migration\/documentation\/swift-6-concurrency-migration-guide\/swift6mode\/\">Swift 6 language<\/a> model clean<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Current goals, then.<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep on the compiler path<\/li>\n\n\n\n<li>Define an IR (intermediate representation) language to translate the semantic AST into.<\/li>\n\n\n\n<li>Define a target ISA (instruction set architecture) for my VM\u2013 <em>knowing that it will always be a VM and so there&#8217;s really no point in having a nod toward parallelism\/pipelining\/caching etc.<\/em> Maybe it&#8217;ll be stack based like Java?<\/li>\n\n\n\n<li>Embrace <a href=\"https:\/\/en.wikipedia.org\/wiki\/Static_single-assignment_form\">SSA<\/a> (static single assignment) in the IR. \n<ul class=\"wp-block-list\">\n<li>Figure out \u0278 (phi) functions.<\/li>\n\n\n\n<li>Figure out variable lifetimes and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Register_allocation\">register allocation<\/a>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Build an independent assembler for the target ISA.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;d like to see the above done by end 25Q1 (it&#8217;s January 1st 2025 now).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final thoughts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I like being influenced and informed by the likes of LLVM, the retro virtual machine scene, cutting edge RISC ISA designs, and whatever fancy thing catches my eye, but I also want 2025 to be a year of measurable achievements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not just in this, but all areas of retired life.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last post I noted that I have an interpreter for SoapyBASIC, but wanted to rework it as a compiler. The work to-date has been very much side-hustle, personal interest, hasn&#8217;t had a lot of serious time devoted to it, and in doing so, spans about FOUR years! In that time I&#8217;ve explored different [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,13,16,15],"tags":[17,19,12,20],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-lang-design","category-soapybasic","category-swift-lang","category-virtual-machines","tag-basic","tag-language-design","tag-soapybasic","tag-swift-lang"],"_links":{"self":[{"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":3,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":59,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/posts\/55\/revisions\/59"}],"wp:attachment":[{"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}