bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How does runtime stack work?


From: Wolfgang Laun
Subject: Re: How does runtime stack work?
Date: Sun, 23 May 2021 09:18:15 +0200

If you have a portable (written in C, C++ or Java) translator T(L1,B) for a
language L1 into some bytecode B and a portable interpreter I(B,*) for B on
a machine M1, you can compile and execute L1 on M1.

To be able to do the same on machines M2, M3,..., you just compile T(L1,B)
and I(B,*) for M2, M3,...

If the bytecode B is well-designed, you can write another translator
T(L2,B), which (given all the bytecode interpreters) will enable you to
execute L2 on M1, M2, M3,... And so on for languages L3, L4,...

There is a similar approach for compilers: if you divide a compiler in a
frontend (parser) and a backend (code generator) designed to create and
process respectively a syntax tree, you can write frontends for the
languages and backends for the machines and combine them as required.

For high-level languages, the execution time gap between interpretation and
execution (compiled code) tends to be smaller than for a low-level
language. Consider that a pattern matching operation will be *compiled *into
a library routine call anyway, you just have a minimal overhead for a
indirect (=interpreted) call.

Much more computing time is wasted due to bad implementation decisions or
poor algorithms, not due to the difference between interpretation and
compilation.

Wolfgang



On Fri, 21 May 2021 at 15:09, Peng Yu <pengyu.ut@gmail.com> wrote:

> OK. Thanks. I would like to understand how gawk is implemented internally.
>
> So byte code implementation is slower than directly generating machine
> code but is easier to implement?
>
> On 5/21/21, arnold@skeeve.com <arnold@skeeve.com> wrote:
> > Peng Yu <pengyu.ut@gmail.com> wrote:
> >
> >> Hi,
> >>
> >> I see a stack is used in gawk. I don't understand what this is used
> >> for. Could anybody help explain? Thanks.
> >
> > Out of curiosity, why do you ask?
> >
> > To briefly answer the question, gawk reads the program and converts
> > it into an internal form, often called "byte code". The internal form
> > represents instructions for a "virtual machine" that is stack based.
> > Operands are pushed onto the stack. Operators pop the operands, do the
> > operation, and push the result back onto the stack. For example,
> >
> >       x = 3 + 5
> >
> > would turn into something like:
> >
> >       PUSH    3
> >       PUSH    5
> >       ADD             # 3 and 5 are popped off the stack, 8 is pushed on
> it
> >       STORE   x       # 8 popped off the stack and stored into 'x'
> >
> > This is one of several standard mechanisms for implementing programming
> > language interpreters.
> >
> > There have been hardware computers that are stack based as well,
> > although I am not sure which modern architectures, if any, are
> > stack based.
> >
> > HTH,
> >
> > Arnold
> >
>
>
> --
> Regards,
> Peng
>
>

-- 
Wolfgang Laun


reply via email to

[Prev in Thread] Current Thread [Next in Thread]