Systems - NedoLang: The path to self-compilation (part 2).

Info Guide #12
Path to self-compilation
Alone Coder

   Once I started implementing types,
The compiler began to swell rapidly. Soh─
The first version in C++ was damaged, convert─
based on Delphi. It was only 3
November (the project started on September 14), and then
12 modules with a thickness of 109 kilos were already sticking out
bytes. Why C++? Because the system must
to work under Linux. Why Delphi?
Because it’s easier to start there. More readable.
   My tongue was also so full of itself
readable - with a bunch of keywords for everything
cases of life. Due to this, in the command field
I was waiting for a command, not something
other. It was enough to check 1-2 characters
commands, which is what I did when I got rid of
AnsiString.

   Some probing of the masses showed
that people don’t want to write codogen again─
speaker from our corporate language is the highest─
level 1 (its name means nothing to you─
zhet) into the intermediate language that is there
now Si. At the same moment it dawned on me
revelation that without self-compilation I am this
I can’t debug a masterpiece at all.
   Usually promoting new languages for self─
compilations are done by writing the first
stripped-down assembler compiler. U
I already had a stripped-down C++ compiler.
Why write a new one? HereI didn't bother writing
new. I converted it to C. And I could even
compile with IAR and SDCC. But that's it
dreams of constantly hanging in48K memory
the system crashed against reality - size co─
yes.
   I've learned so much about assembly language in─
dachas (up to temporary storage of the tag
call while the parameters are being calculated), which
a very serious assembler was required. Mo─
you could have guessed what it would weigh
no less than the compiler itself. And in48K they
they won't fit together. It’s not even known whether it will fit
is the assembler with all the compiler marks,
when will this compiler be assembled─
vat. (I didn't plan a linker, instead
pieces of code should have scattered the load─
chick in the OS - and only if there is 48K memory
there are really a lot of programs.)
   In general, an assembler can be with a procedure─
frames, optimization, macros, locales,
automatic tags, etc., or without anything at all,
maybe even from tokenized tex─
the one with chewed up expressions and marks
fixed length. Was it possible at all?
make a compiler without assembler.

   So as not to think for a long time, I took it out of the pile
extracts of the assembler project translating
from a half-digested performance.  This
was once planned to replace ALASM.
It had to work terribly fast, obs─
puddling incoming tokens by moving through that─blitz True, it should have worked quickly─
bot if it was written in assembler
Z80. I wrote this in C. And tokenizer- 
detokenizer included.
   Tokens were selected taking into account the following
restrictions:
  - the tokenizer does not know the command codes;
  - the tokenizer sees only the left side─
text, therefore does not distinguish between homonymous 
commands (for example, there are a bunch of different commands 
LD );
 - detokenizer (aka exporter) is simple
substitutes token texts or direct 
text after the "straight text" token. This 
so that the future assembly editor shell─ 
which easily displayed tokenized 
performance. 
   To realize this, it was necessary
invisible command format tokens that
are written after the command body, and also formats
expressions that are written after the second
operand for addition, etc. Delivery formats─
There were so many elk that 256 codes were barely enough.
But in general, by the end of the year everything worked.
   Only in the assembly code wasswitch by
tokens.
   This was a slightly different level
"simplicity" of the language.switch can be replaced with
statically initialized array uka─
developers on functions, but this also needs to be done─
lyse, and it will work slower - you─
call instead of transition (following the sign
in CNo). But you don’t have to replace it, then you have to
already at the assembler level to support calculations─
labels to be placed. After all, the assembler would receive
input only the name of the currentswitch and name─
bath constant for the branchcase, which
needs to be calculated and attached to the name. write
in case straight numbers would be unsupported─
mo. And so I already had a file with all that─
kenami from the tokenizer.

   So I didn't just have to write
a new language, and a language compatible with C is almost
comparable capabilities. It was then - and
not before - I go to self-compilation.

 10.24.2016:
 - the restriction on word alternation has been removed
and characters in the lexer. Now the syntax is:
var int a
var byte bb
var long LONA
var int array[15]

module vasya(

proc xx local()
(
)

func byte byter local()
(
 var byte byterbb
 let byterbb=byterbb
 let LONA$=LONA$
{let bb$=$3a^(bb$^
        ?byter({stacked}{byterbb=%1111})
             )}
 let bb$=%111^bb$^byterbb
)%101

func long longer
local(long L1 long L2){recursive local}
(
let a$=(+1);
let LONA$=?longer()+$01020304;
let bb$=(%1);
)LONA$

func int dup local(int r){recursive local}
(
var int p1
var uint p2
if(  let p2=p1 &+$ffff;
)~()
*(@array$+[7*WORDSIZE])(555){write array}
let p2=*(+@array$+p1*+[WORDSIZE]);
                        {read from array}
)(p1+p1)

  25.10.2016:
  - слово local больше не нужно для нере─
курсивных процедур
  - команда записи в память теперь начина─
ется со словаpoke (как в Бейсике):
   poke (@array$+[7*WORDSIZE]),555
  - выделен   модуль typecodes  с  кодами
типов,  он   используется   в compiler  и
emitcommands

  26.10.2016:
  - равенство и неравенство пишутсяlike==
and!= (as in C)
 - full-fledged work with arrays via
a[N] (as in C)
 - the long-added typeBOOL has been implemented.
The idea was to store it directly in the flag
register (easiestCY ), then if/while
directly along the flag:
 AND:
 jr FALSE,$+2+2
 pop af
 push af
 pop bc
 OR:
 jr TRUE,$+2+2
 pop af
 push af
 pop bc
 XOR (only for flag in CY):
 jr FALSE,$+2+3
 pop af
 ccf   push af
   pop af
 pushvar (только для флага в CY):
   push af;зависит от типа выше
   ld a,(var)
   rlca
 popvar (только для флага в CY):
   rrca
   ld (var),a
   pop af;зависит от типа выше
 LESS (старое меньше нового):
    ;если leftsaved=FALSE
    ;ex de,hl
    ;pop hl
   or a
   sbc hl,de
 MORE (новое меньше старого):
    ;если leftsaved=FALSE
    ;ex de,hl
    ;pop hl
   ex de,hl
   or a
   sbc hl,de
 MOREEQ(старое >= нового):
    ;если leftsaved=FALSE
    ;ex de,hl
    ;pop hl
   or a
   sbc hl,de
   ccf
 EQ:
    ;если leftsaved=FALSE
    ;ex de,hl
    ;pop hl
   or a
   sbc hl,de
   jr z,$+2+1;CY=0
   scf
   ccf
 NOTEQ:
    ;если leftsaved=FALSE
    ;ex de,hl
    ;pop hl
   or a
   sbc hl,de
   jr z,$+2+1;CY=0
   scf
Но я решил, что проще  в аккумуляторе (как
байтовый тип)

  27.10.2016:
  ->= и <= (как в Си)
  -repeat .. until (как в Паскале)
 - the word loop has been added to the while command
(where in Pascal it isdo, but do interferes
C compatibility)
  - the word then was added to the commandif (as
in Pascal)
  - when calling recursive functions after
The function name is written with the wordrecursive, and
stacked is not written
 - attempt to remove all duplicate calls
to the lines so that you can stream them from
input file (gluing areas vi─
responsibility was planned to be assigned to the assembly─
ler)

 10/28/2016:
 - command blocks are not highlighted round,
and with curly braces (as in C). Branded
nested comment with curly braces
is now formatted as{ ... }
 -poke now looks like this (compatible with
C):
 poke *(a$)=( " as" " ) {write memory}
 - definition of the transition label is now
looks not like :mylabel , but like LABEL
mylabel: (compatible with C)
 - global variables start with_
(at first I did it from a point, but changed my mind
due to incompatibility with C)
  - functions in expressions are called via
prefix@ (was ? , it is reserved for Boolean
constants/*?*/_TRUE and /*?*/_FALSE), and about─
routines are called viaCALL (compatible with
C)
  - in the definition of a function instead of a block
local is now a regular declaration of local
variables in the body throughRECURSIVEVAR and fi─
curly bracket after the declaration (limit─
is the place where local variables are saved─
are stored on the stack and then restored)
  - Coma is supported (and recommended)
nds in capital letters (as in Oberon, plus
Usually in C the definitions are in capital letters─
mi)
  - all squiggles that are incompatible with C can be
escape with a comment/*...*/ , which
for NedoLang - not a comment. Real
comment -/**...*/
 To understand the idea of compatibility of your
language from C, cf. example from the original
Bourne Shell: 

#define BEGIN{
#define END    }
#define SWITCH switch(
#define IN     ){
#define ENDSW  }
#define FOR    for(
#define WHILE  while(
#define DO     ){
#define OD     ;}
#define REP    do{
#define PER    }while(
#define DONE   );
#define LOOP   for(;;){
#define POOL   }

  29.10.2016:
  - командаreturn перенесена в тело функ─
ции (как в Си)
  - числовые константы совместимы с Си

  31.10.2016:
  - убрана   зависимость   от   библиотеки
SysUtils (функцияIntToStr ) - in recursive calculation procedures
expressions, local string rewriting has been removed─
variable

 11/01/2016:
 - all local string conversions have been removed─
variable, for this purpose inif, etc. are generated
special marks for assembler:
@TEMPELSE, @TEMPENDIF...

 11/02/2016:
 - when calling a function, the type of the function is written
and parameters
  - string constants work: compile─
are fighting with the automatic tag

 11/03/2016 - the compiler has been moved from
Delphi in C++ Builder 

11/05/2016 - stringsAnsiString replaced
to MYSTRING (defined aschar*, but with
length in the first byte), first compilation
on IAR (its own module was written for this
syscalls with string and file processes─
fools, the latter in the form of stubs)

 11/22/2016 - created a sandbox project on
C++, where I experimented, what speeds
In general, the compiler can eat it. Thishe
eats:
 i=+(uint)+(uint)0;
 j = + +i;
 k = + -i;
 And here are the curly braces in an arbitrary
place - no.  And I really wanted to screen them─
write code that shouldn't be seen
NedoLang. 
   But compilers are generally different.

 11/23/2016
 - pointers appeared (typespint, etc.)
 - variables are unloaded into a separate
assembler file (before this they were unloading─
disappeared from memory at the end of compilation, but I
there were still hopes of getting rid of the table
labels in the compiler thanks to manual pro─
writing types in calls)

 11/24/2016 - Pascal string format
replaced with blue

 11/25/2016:
 - added commandbreak to exit
cycles because I couldn't get rid of
break is everywhere in the compiler. The best that I am
invented instead of break , - complex format
loop with precondition:
//string comparison 
//a) the characters do not match (notгодно) 
//б) символы совпали (продолжаем,если 
//ненулевые, иначе годно) 
WHILE((
 PRECOND(//precond1
   LET c1=s1[i+FIRST],
   LET c2=s2[i]
 ),COND(c1==c2) LUCK(//cond1==TRUE
                    //(одинаковые символы)
   COND(c1!='') LUCK(//cond2==TRUE
                      //(not end)
     LET i=i+1
   )FAIL(LET result=TRUE),NODO
                            //cond2==FALSE
                           //(end)
 )FAIL(LET result=FALSE),NODO
                            //cond1==FALSE
                  //(неодинаковые символы)
))LOOP{
}
   Всё понятно? Нет? Ладно,дам подсказку:)
#define PRECOND /**/
#define COND /**/
#define LUCK ?
#defineFAIL :(
#define NODO FALSE)
 Unfortunately, curly braces cannot be used─
use in the ternary operator.
  -if now ends with endif and else
optional

 11/30/2016 - tokenizer and de─ written
tokenizer (not separately, but as part of that
same programs)

 12/01/2016 - assembler written (also in
composition)

 12/02/2016 - manual written

 12/05/2016:
 - removed@TEMPELSE,@TEMPENDIF ...,instead
these are numbered labels
  - added comment// to the end of the line─
ki
  - added commandasm

 12.12.2016 - bugfixes in the tokenizer

 12/13/2016 - wrote in the report approximately
the following:
 "Writing a full C compiler
or C++ is a huge task that requires pro─
professional programmers. Besides the pain─
The problem is still in the scope of the language standard
that the language contains many subtleties
in text parsing and working with types, dovo
very difficult to implement. But since we
we use only a small subset
C language, we can implement a compiler
only for this subset, and parsing
Simplify the text using arrangement in
compiler hint text.
 At the moment we have a prototype
compiler from a language similar to C to
assembly text.
 ...
 There are also the beginnings of a self-written essay─bler, so far only for the command system
8-bit Z80 processor. Support for others
architectures will only be affected specifically by you─
divided part of the compiler and assembler
(less than 40% of the volume of the source text, and
rather monotonous part) and maybe
added relatively easily.
 The problem will be in the full implementation
command systems (we do not know in advance what
may be required, but we can approximately
evaluate this using self-compilation, then
is when we get the compiler com─
piled my own source text) and
full debugging with documentation, which
paradise may take timesignificant pain─
more than the development itself."

   What you need to do for self-compilation:

  # constant arrays for type sizes
and register names (can be replaced with 
functions or filling arrays) 
  #forwardfunction definitions or declarations─
The function is also changed when using 
  - if you write a challenge
/*@*/(type).name(/*par1=*/n1,/*par2=*/n2)
 then C will see(type).name(n1,n2)
 - if you write a call
/*@type.*/name(
 /*type par1=*/n1,/*type par2=*/n2)
then C will seename(n1,n2)
 - instead of/*@*/you can writeCALL, but not
in the expression (since there may be an identifier 
CALL )
 - when called from an expression, an ide is expected─
ntifier or define, as in the case 
true/false
 #include
#define(prefix and register codes)
  # label table must be defined
on the array! or remove it altogether (hard 
write types everywhere and alternate csegand 
dseg )

Share your thoughts about the article