Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #103832 > unrolled thread
| Started by | fir <profesor.fir@gmail.com> |
|---|---|
| First post | 2017-02-13 11:43 -0800 |
| Last post | 2017-02-21 11:15 -0800 |
| Articles | 20 on this page of 22 — 5 participants |
Back to article view | Back to comp.lang.c
compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 11:43 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 11:44 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" BartC <bc@freeuk.com> - 2017-02-13 20:10 +0000
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 12:36 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 13:31 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" BartC <bc@freeuk.com> - 2017-02-13 23:21 +0000
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-14 00:27 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" supercat@casperkitty.com - 2017-02-13 13:29 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 13:44 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" supercat@casperkitty.com - 2017-02-13 14:01 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 14:16 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 14:17 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" luser droog <luser.droog@gmail.com> - 2017-02-13 19:36 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-14 00:32 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" John Bode <jfbode1029@gmail.com> - 2017-02-15 13:27 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-15 13:47 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-15 14:13 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-15 14:16 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-15 14:40 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-16 14:57 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-16 15:02 -0800
Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-21 11:15 -0800
Page 1 of 2 [1] 2 Next page →
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-13 11:43 -0800 |
| Subject | compile "a = 2*f+(d-g/3)*(9-i)" |
| Message-ID | <ce233d83-2f17-4dad-8f38-9d0aa0fde624@googlegroups.com> |
if i like to write compiler i should probably start with compiling such as above (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that like mov tmp1 2 mov tmp2 f mul tmp1 tmp2 mov tmp3 d mov tmp4 g mov tmp5 3 div tmp4 tmp3 sub tmp3 tmp4 etc.. how to do that EASILY (easy and short) ?
[toc] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-13 11:44 -0800 |
| Message-ID | <720c394b-bdb5-4b2d-8d51-38185ddd9d8f@googlegroups.com> |
| In reply to | #103832 |
W dniu poniedziałek, 13 lutego 2017 20:43:15 UTC+1 użytkownik fir napisał: > if i like to write compiler i should probably start with compiling such as above > (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that > > like > > mov tmp1 2 > mov tmp2 f > mul tmp1 tmp2 > > mov tmp3 d > mov tmp4 g > mov tmp5 3 > div tmp4 tmp3 > sub tmp3 tmp4 > > etc.. how to do that EASILY (easy and short) ? ps some pseudocode would be much welcome
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2017-02-13 20:10 +0000 |
| Message-ID | <o7t3ne$6e2$1@dont-email.me> |
| In reply to | #103833 |
On 13/02/2017 19:44, fir wrote: > W dniu poniedziałek, 13 lutego 2017 20:43:15 UTC+1 użytkownik fir napisał: >> if i like to write compiler i should probably start with compiling such as above >> (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that >> >> like >> >> mov tmp1 2 >> mov tmp2 f >> mul tmp1 tmp2 >> >> mov tmp3 d >> mov tmp4 g >> mov tmp5 3 >> div tmp4 tmp3 >> sub tmp3 tmp4 >> >> etc.. how to do that EASILY (easy and short) ? > > ps some pseudocode would be much welcome > https://rosettacode.org/wiki/Compiler Look at the links to the tasks (the different parts of a compiler). There is sample code in various languages. Including C. -- bartc
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-13 12:36 -0800 |
| Message-ID | <0d305c57-cf15-42ed-b440-55cb709fbe38@googlegroups.com> |
| In reply to | #103834 |
W dniu poniedziałek, 13 lutego 2017 21:10:21 UTC+1 użytkownik Bart napisał: > On 13/02/2017 19:44, fir wrote: > > W dniu poniedziałek, 13 lutego 2017 20:43:15 UTC+1 użytkownik fir napisał: > >> if i like to write compiler i should probably start with compiling such as above > >> (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that > >> > >> like > >> > >> mov tmp1 2 > >> mov tmp2 f > >> mul tmp1 tmp2 > >> > >> mov tmp3 d > >> mov tmp4 g > >> mov tmp5 3 > >> div tmp4 tmp3 > >> sub tmp3 tmp4 > >> > >> etc.. how to do that EASILY (easy and short) ? > > > > ps some pseudocode would be much welcome > > > > https://rosettacode.org/wiki/Compiler > > Look at the links to the tasks (the different parts of a compiler). > There is sample code in various languages. Including C. > wonder if there some simple rules i could use and what they are a = 2*f+(d-g/3)*(9-i) -> t1=2*f t2=g/3 t3=9-1 a = t1 + (d-t2) * t3 -> t4 = d-t2 a = t1 + t4 * t3 -> t5 = t4* t3 a = t1 + t5 so t1=2*f t2=g/3 t3=9-1 t4=d-t2 t5=t4*t3 a= t1+t5 ? m not sure hoe to do that? define input array in form of 'sylables' (register names + operators) and go in passes and reduce it and create output array as array of only (r = a operator b) ? this could be some idea maybe, could read this general writings but it consumes to much time sometimes to find most important points some more hints?
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-13 13:31 -0800 |
| Message-ID | <a4325d71-9b00-4c85-ba95-913a4fb2fcd1@googlegroups.com> |
| In reply to | #103836 |
W dniu poniedziałek, 13 lutego 2017 21:37:04 UTC+1 użytkownik fir napisał: > W dniu poniedziałek, 13 lutego 2017 21:10:21 UTC+1 użytkownik Bart napisał: > > On 13/02/2017 19:44, fir wrote: > > > W dniu poniedziałek, 13 lutego 2017 20:43:15 UTC+1 użytkownik fir napisał: > > >> if i like to write compiler i should probably start with compiling such as above > > >> (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that > > >> > > >> like > > >> > > >> mov tmp1 2 > > >> mov tmp2 f > > >> mul tmp1 tmp2 > > >> > > >> mov tmp3 d > > >> mov tmp4 g > > >> mov tmp5 3 > > >> div tmp4 tmp3 > > >> sub tmp3 tmp4 > > >> > > >> etc.. how to do that EASILY (easy and short) ? > > > > > > ps some pseudocode would be much welcome > > > > > > > https://rosettacode.org/wiki/Compiler > > > > Look at the links to the tasks (the different parts of a compiler). > > There is sample code in various languages. Including C. > > > wonder if there some simple rules i could use and what they are > > a = 2*f+(d-g/3)*(9-i) -> > > t1=2*f > t2=g/3 > t3=9-1 > > a = t1 + (d-t2) * t3 -> > > t4 = d-t2 > > a = t1 + t4 * t3 -> > > t5 = t4* t3 > > a = t1 + t5 > > so > > t1=2*f > t2=g/3 > t3=9-1 > t4=d-t2 > t5=t4*t3 > a= t1+t5 > > ? > > m not sure hoe to do that? define input array > in form of 'sylables' (register names + operators) and go in passes and reduce it and > create output array as array of only (r = a operator b) ? this could be some idea maybe, > > could read this general writings but it consumes to much time sometimes to find > most important points > > some more hints? this seem even probably good noncomplex idea, i implemented basics of it, rest i do maybe toomorrow and will see. what else then i need basic construct like loop if and function, i will see
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2017-02-13 23:21 +0000 |
| Message-ID | <o7teuv$jt1$1@dont-email.me> |
| In reply to | #103836 |
On 13/02/2017 20:36, fir wrote:
> W dniu poniedziałek, 13 lutego 2017 21:10:21 UTC+1 użytkownik Bart napisał:
>> https://rosettacode.org/wiki/Compiler
>>
>> Look at the links to the tasks (the different parts of a compiler).
>> There is sample code in various languages. Including C.
>>
> wonder if there some simple rules i could use and what they are
> a = 2*f+(d-g/3)*(9-i) ->
>
> t1=2*f
> t2=g/3
> t3=9-1
> m not sure hoe to do that? define input array
> in form of 'sylables' (register names + operators) and go in passes and reduce it and
> create output array as array of only (r = a operator b) ? this could be some idea maybe,
>
> could read this general writings but it consumes to much time sometimes to find
> most important points
>
> some more hints?
What are you trying to do?
* Evaluate an expression?
* Convert an expression into code for VM?
* Into code for a real processor?
* Into three-address code (this is what your t1/t2/t3 looks like)?
What format is the expression in:
* In text as it is normally written
* In text but in reverse Polish (that might be easier for you...)
* Represented as a data structure (because tokenising is very fiddly)?
If this is aiming towards writing a compiler or something, then there's
quite a lot involved, but also masses of information around.
I would also recommend not using C for this, not unless speed is a
consideration. Probably you will get fed up with it, that will be sooner
if you have to mess about with C taking care of millions of details.
But here is one demo in C, that takes an expression 2+2 represented as
reverse polish in a data structure, and evaluates it. I've no idea if
this is of any use (I just fancied writing some C):
#include <stdio.h>
#include <stdlib.h>
typedef long long int Int;
enum {add,sub,mul,divide,load,end};
typedef struct {int op; Int value;} Term;
Term expr[]={ // 2+2
{load, 2},
{load, 2},
{add, 0},
{end, 0}};
Int stack[1000];
int stackindex=-1;
Int pop(void){return stack[stackindex--];}
void push(Int x){stack[++stackindex]=x;}
Int evaluate(Term* expr){
Int b,result, stop=0;
while (1) {
switch (expr->op) {
case add:
push(pop()+pop());
break;
case sub:
b=pop();
push(pop()-b);
break;
case mul:
push(pop()*pop());
break;
case divide:
b=pop();
if (b==0) {puts("div by zero"); exit(0);}
push(pop()/b);
break;
case load:
push(expr->value);
break;
case end:
return pop();
}
++expr;
}
return 0;
}
int main(void){
Int x;
x=evaluate(expr);
printf("Result = %lld\n",x);
}
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-14 00:27 -0800 |
| Message-ID | <f5c16da6-bfba-4450-b658-8de7aafc9a4b@googlegroups.com> |
| In reply to | #103850 |
W dniu wtorek, 14 lutego 2017 00:21:55 UTC+1 użytkownik Bart napisał:
> On 13/02/2017 20:36, fir wrote:
> > W dniu poniedziałek, 13 lutego 2017 21:10:21 UTC+1 użytkownik Bart napisał:
>
> >> https://rosettacode.org/wiki/Compiler
> >>
> >> Look at the links to the tasks (the different parts of a compiler).
> >> There is sample code in various languages. Including C.
> >>
> > wonder if there some simple rules i could use and what they are
>
> > a = 2*f+(d-g/3)*(9-i) ->
> >
> > t1=2*f
> > t2=g/3
> > t3=9-1
>
> > m not sure hoe to do that? define input array
> > in form of 'sylables' (register names + operators) and go in passes and reduce it and
> > create output array as array of only (r = a operator b) ? this could be some idea maybe,
> >
> > could read this general writings but it consumes to much time sometimes to find
> > most important points
> >
> > some more hints?
>
> What are you trying to do?
>
> * Evaluate an expression?
> * Convert an expression into code for VM?
> * Into code for a real processor?
> * Into three-address code (this is what your t1/t2/t3 looks like)?
>
it was written in first post and later:
convert expression in virtual assembly text
thise thre adress code (i cal it 'fours' as it is 3 registers and one operator) dont know better name) characterizes the quality if
i break input into list of it i can generate
asm code from this strightforward way
> What format is the expression in:
>
> * In text as it is normally written
> * In text but in reverse Polish (that might be easier for you...)
> * Represented as a data structure (because tokenising is very fiddly)?
>
tokenising input into inear array i know how to do (i will not reverse anything just store
tokens with their types in such exact order
as in inout text expression)
> If this is aiming towards writing a compiler or something, then there's
> quite a lot involved, but also masses of information around.
>
> I would also recommend not using C for this, not unless speed is a
> consideration. Probably you will get fed up with it, that will be sooner
> if you have to mess about with C taking care of millions of details.
>
not really i vrite in c much easier fast and nice than say in javascript
> But here is one demo in C, that takes an expression 2+2 represented as
> reverse polish in a data structure, and evaluates it. I've no idea if
> this is of any use (I just fancied writing some C):
>
> #include <stdio.h>
> #include <stdlib.h>
>
> typedef long long int Int;
>
> enum {add,sub,mul,divide,load,end};
>
> typedef struct {int op; Int value;} Term;
>
> Term expr[]={ // 2+2
> {load, 2},
> {load, 2},
> {add, 0},
> {end, 0}};
>
> Int stack[1000];
> int stackindex=-1;
>
> Int pop(void){return stack[stackindex--];}
> void push(Int x){stack[++stackindex]=x;}
>
> Int evaluate(Term* expr){
> Int b,result, stop=0;
>
> while (1) {
> switch (expr->op) {
> case add:
> push(pop()+pop());
> break;
> case sub:
> b=pop();
> push(pop()-b);
> break;
> case mul:
> push(pop()*pop());
> break;
> case divide:
> b=pop();
> if (b==0) {puts("div by zero"); exit(0);}
> push(pop()/b);
> break;
> case load:
> push(expr->value);
> break;
> case end:
> return pop();
> }
> ++expr;
> }
> return 0;
> }
>
> int main(void){
> Int x;
>
> x=evaluate(expr);
>
> printf("Result = %lld\n",x);
>
> }
>
>
i dont quite get this push/pops do this is for guqrantee thet you can generate asm code in one pass? i dont see the code generation
of asm commands, do they need to be inserted
into it?
as i said i will try some 'multipass' (lilu dallas multipass) method that come to my mind
- in each pas i will convert some "a op b" 'triads' from input into list of "t1 = a op b" 'fours' in another array until will empty, should work but will try it later in evening
- seems better as no push pop needed
[toc] | [prev] | [next] | [standalone]
| From | supercat@casperkitty.com |
|---|---|
| Date | 2017-02-13 13:29 -0800 |
| Message-ID | <01e9ff0f-a5f5-474e-ac40-d1f366ce6221@googlegroups.com> |
| In reply to | #103832 |
On Monday, February 13, 2017 at 1:43:15 PM UTC-6, fir wrote:
> if i like to write compiler i should probably start with compiling such as above
> (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that
If you build an expression tree, it's possible to construct a simple compiler
by defining a little piece of code for each node type which will typically
evaluate each child node and then do something afterward. A post-order
traversal of the tree for "a = 2*f+(d-g/3)*(9-i)" would yield:
; a
push &a
; 2
push 2
; f
push &f
; [value conversion]
pop r0
load r0,@r0
push r0
; *
pop r0
pop r1
mul r0,r1
push r0
; d
mov r0,&d
; [value conversion]
pop r0
load r0,@r0
push r0
; g
mov r0,&g
; [value conversion]
pop r0
mov r0,@r0
push r0
; 3
push 3
; /
pop r0
pop r1
div r0,r1
push r0
; -
pop r1
pop r0
sub r0,r1
push r0
; 9
push 9
; i
mov r0,&i
; [value conversion]
pop r0
load r0,@r0
push r0
; *
pop r0
pop r1
mul r0,r1
push r0
; +
pop r0
pop r1
add r0,r1
push r0
; =
pop r1
pop r0
store r0,@r1
Note that each kind of token always yields the exact same code, except that
the code variable names and numeric constants includes operand values. The
generated code would be horribly efficient, but something called a
"peephole optimizer" can improve things a lot by looking for code sequences
like "push r0 / pop r0". It may also be possible for the code that handles
some tokens like "=" to look for common usage cases (like a simple variable
on the left-hand side) and use more efficient code to handle them directly
(thus avoiding the need to push the address of "a" and pop it later).
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-13 13:44 -0800 |
| Message-ID | <5ee7c15c-bebc-4dab-8f4b-4c59dd753791@googlegroups.com> |
| In reply to | #103839 |
W dniu poniedziałek, 13 lutego 2017 22:29:47 UTC+1 użytkownik supe...@casperkitty.com napisał: > On Monday, February 13, 2017 at 1:43:15 PM UTC-6, fir wrote: > > if i like to write compiler i should probably start with compiling such as above > > (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that > > If you build an expression tree, it's possible to construct a simple compiler > by defining a little piece of code for each node type which will typically > evaluate each child node and then do something afterward. A post-order > traversal of the tree for "a = 2*f+(d-g/3)*(9-i)" would yield: > > ; a > push &a > ; 2 > push 2 > ; f > push &f > ; [value conversion] > pop r0 > load r0,@r0 > push r0 > ; * > pop r0 > pop r1 > mul r0,r1 > push r0 > ; d > mov r0,&d > ; [value conversion] > pop r0 > load r0,@r0 > push r0 > ; g > mov r0,&g > ; [value conversion] > pop r0 > mov r0,@r0 > push r0 > ; 3 > push 3 > ; / > pop r0 > pop r1 > div r0,r1 > push r0 > ; - > pop r1 > pop r0 > sub r0,r1 > push r0 > ; 9 > push 9 > ; i > mov r0,&i > ; [value conversion] > pop r0 > load r0,@r0 > push r0 > ; * > pop r0 > pop r1 > mul r0,r1 > push r0 > ; + > pop r0 > pop r1 > add r0,r1 > push r0 > ; = > pop r1 > pop r0 > store r0,@r1 > > Note that each kind of token always yields the exact same code, except that > the code variable names and numeric constants includes operand values. The > generated code would be horribly efficient, but something called a > "peephole optimizer" can improve things a lot by looking for code sequences > like "push r0 / pop r0". It may also be possible for the code that handles > some tokens like "=" to look for common usage cases (like a simple variable > on the left-hand side) and use more efficient code to handle them directly > (thus avoiding the need to push the address of "a" and pop it later). interesting.. you mean using this push pop i may generate it easier? (not read in in it to much yet) - but assembly full of push pop will be slover... the metod i given above (be generating linear array of tokens (which may be register operator and immediate value) and then converting it in a passes to list of 'fours' in such kind as above which then can be flushed to asm easily seem to be easy too.. will se it later but it fortunatelly seems easy - like compiler possibly made in one day of coding (but i prefer taking breaks so will see it toomorrow)
[toc] | [prev] | [next] | [standalone]
| From | supercat@casperkitty.com |
|---|---|
| Date | 2017-02-13 14:01 -0800 |
| Message-ID | <10ee0181-e86e-426a-a2d7-3e8a3274905a@googlegroups.com> |
| In reply to | #103841 |
On Monday, February 13, 2017 at 3:44:20 PM UTC-6, fir wrote: > interesting.. you mean using this push pop i may generate it easier? (not read in in it to much yet) - but assembly full of push pop will be slover... the metod i given above (be generating linear array of tokens (which may be register operator and immediate value) > and then converting it in a passes to list of > 'fours' in such kind as above which then can be flushed to asm easily > > seem to be easy too.. will se it later > but it fortunatelly seems easy - like > compiler possibly made in one day of coding > (but i prefer taking breaks so will see it toomorrow) If one were to actually generate all those pushes and pops the resulting code would be rather slow, but in practice many of them can be eliminated. Starting with pushes and pops and then having the peephole optimizer find ways to eliminate them when possible will ensure that if an expression gets so complicated that the optimizer can't eliminate all the pushes and pops the compiler will still be able to generate working code. An alternative approach would be to start by allocating registers and then "spill them" to the stack when needed, but I think starting with a stack- based approach will make it easier to get something working.
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-13 14:16 -0800 |
| Message-ID | <2e8c1437-17c4-4d6a-b26e-055e5f941bc0@googlegroups.com> |
| In reply to | #103843 |
W dniu poniedziałek, 13 lutego 2017 23:01:46 UTC+1 użytkownik supe...@casperkitty.com napisał: > On Monday, February 13, 2017 at 3:44:20 PM UTC-6, fir wrote: > > interesting.. you mean using this push pop i may generate it easier? (not read in in it to much yet) - but assembly full of push pop will be slover... the metod i given above (be generating linear array of tokens (which may be register operator and immediate value) > > and then converting it in a passes to list of > > 'fours' in such kind as above which then can be flushed to asm easily > > > > seem to be easy too.. will se it later > > but it fortunatelly seems easy - like > > compiler possibly made in one day of coding > > (but i prefer taking breaks so will see it toomorrow) > > If one were to actually generate all those pushes and pops the resulting > code would be rather slow, but in practice many of them can be eliminated. > Starting with pushes and pops and then having the peephole optimizer > find ways to eliminate them when possible will ensure that if an expression > gets so complicated that the optimizer can't eliminate all the pushes and > pops the compiler will still be able to generate working code. An > alternative approach would be to start by allocating registers and then > "spill them" to the stack when needed, but I think starting with a stack- > based approach will make it easier to get something working. as i define my own assembler i may add even a 200 temporary registers just to not care of the free pool.. need simplicity as i said at start i will be usling some language simpler than b (a ? ą ?) i will only use 26 regiosters a b c .. z in expresions mixed with immediates and alos will define static arrays adresable by this registers will se how far it can go .. using full name stack locals is more handy though (and not much harder, as register is some place in memory same as locals on stack so b is really nicer to write codes in - such a (i will cal it a temporarily) its only simpler as i got predefined local names and dont need to parse and stack a list it is anyway important to think what a should look like (it should be really minimal subset of b i think - leading towards easier compiler design of it)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-13 14:17 -0800 |
| Message-ID | <bb7d8f7c-1dce-4da4-9177-c3acbd952aa1@googlegroups.com> |
| In reply to | #103845 |
W dniu poniedziałek, 13 lutego 2017 23:16:16 UTC+1 użytkownik fir napisał: > W dniu poniedziałek, 13 lutego 2017 23:01:46 UTC+1 użytkownik supe...@casperkitty.com napisał: > > On Monday, February 13, 2017 at 3:44:20 PM UTC-6, fir wrote: > > > interesting.. you mean using this push pop i may generate it easier? (not read in in it to much yet) - but assembly full of push pop will be slover... the metod i given above (be generating linear array of tokens (which may be register operator and immediate value) > > > and then converting it in a passes to list of > > > 'fours' in such kind as above which then can be flushed to asm easily > > > > > > seem to be easy too.. will se it later > > > but it fortunatelly seems easy - like > > > compiler possibly made in one day of coding > > > (but i prefer taking breaks so will see it toomorrow) > > > > If one were to actually generate all those pushes and pops the resulting > > code would be rather slow, but in practice many of them can be eliminated. > > Starting with pushes and pops and then having the peephole optimizer > > find ways to eliminate them when possible will ensure that if an expression > > gets so complicated that the optimizer can't eliminate all the pushes and > > pops the compiler will still be able to generate working code. An > > alternative approach would be to start by allocating registers and then > > "spill them" to the stack when needed, but I think starting with a stack- > > based approach will make it easier to get something working. > > as i define my own assembler i may add even a 200 temporary registers just to not care of > the free pool.. need simplicity > > as i said at start i will be usling some language simpler than b (a ? ą ?) i will only use 26 regiosters a b c .. z in expresions > mixed with immediates and alos will define > static arrays adresable by this registers > > will se how far it can go .. using full name stack locals is more handy though (and not > much harder, as register is some place in memory same as locals on stack so b is really > nicer to write codes in - such a (i will cal it a temporarily) its only simpler as i got predefined local names and dont need to parse > and stack a list > > it is anyway important to think what a should look like (it should be really minimal subset of b i think - leading towards easier compiler design of it) ps if i would like to puff myself (which i can do fur fun, i could say ritchie invented c over thompson invented b over fir invented a ;-) but this is kinf of joke ;-C
[toc] | [prev] | [next] | [standalone]
| From | luser droog <luser.droog@gmail.com> |
|---|---|
| Date | 2017-02-13 19:36 -0800 |
| Message-ID | <45ce8cc9-4335-4c12-835d-8d0d063ca2d8@googlegroups.com> |
| In reply to | #103832 |
On Monday, February 13, 2017 at 1:43:15 PM UTC-6, fir wrote: > if i like to write compiler i should probably start with compiling such as above > (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that > > like > > mov tmp1 2 > mov tmp2 f > mul tmp1 tmp2 > > mov tmp3 d > mov tmp4 g > mov tmp5 3 > div tmp4 tmp3 > sub tmp3 tmp4 > > etc.. how to do that EASILY (easy and short) ? Just recently, I think I found the answer to this: https://en.wikipedia.org/wiki/META_II http://www.ibm-1401.info/Meta-II-schorre.pdf http://www.bayfronttechnologies.com/mc_tutorial.html Still finding pieces of my mind all over the place over here.
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-14 00:32 -0800 |
| Message-ID | <dd3911ea-cc8e-42fb-ba8d-8e6cf5792c9b@googlegroups.com> |
| In reply to | #103856 |
W dniu wtorek, 14 lutego 2017 04:37:04 UTC+1 użytkownik luser droog napisał: > On Monday, February 13, 2017 at 1:43:15 PM UTC-6, fir wrote: > > if i like to write compiler i should probably start with compiling such as above > > (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that > > > > like > > > > mov tmp1 2 > > mov tmp2 f > > mul tmp1 tmp2 > > > > mov tmp3 d > > mov tmp4 g > > mov tmp5 3 > > div tmp4 tmp3 > > sub tmp3 tmp4 > > > > etc.. how to do that EASILY (easy and short) ? > > Just recently, I think I found the answer to this: > > https://en.wikipedia.org/wiki/META_II > http://www.ibm-1401.info/Meta-II-schorre.pdf > http://www.bayfronttechnologies.com/mc_tutorial.html > > Still finding pieces of my mind all over the place over here. reading brings a problem that many things are convoluted, quicker 'hacky' method is based on direct hints based on the things that are known etc (though reading is ok too probably, but not necessary if someone want to do something 'right now' - i would prefer reading bigger text but maybe later, after doin ;-c )
[toc] | [prev] | [next] | [standalone]
| From | John Bode <jfbode1029@gmail.com> |
|---|---|
| Date | 2017-02-15 13:27 -0800 |
| Message-ID | <a14eb456-ae68-409d-aca7-4a9b51d033f1@googlegroups.com> |
| In reply to | #103832 |
On Monday, February 13, 2017 at 1:43:15 PM UTC-6, fir wrote: > if i like to write compiler i should probably start with compiling such as above > (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that > > like > > mov tmp1 2 > mov tmp2 f > mul tmp1 tmp2 > > mov tmp3 d > mov tmp4 g > mov tmp5 3 > div tmp4 tmp3 > sub tmp3 tmp4 > > etc.. how to do that EASILY (easy and short) ? The *easy* thing to do is use a toolset like flex and bison to generate your parser and add your custom processing logic to it. There's an example of a simple calculator at http://www-bcf.usc.edu/~breichar/teaching/2011cs360/calculator.pdf that should get you started. Less easy would be writing your own lexer and parser. For arithmetic expressions like your example, that shouldn't take more than a day or so.
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-15 13:47 -0800 |
| Message-ID | <313a93a1-ef98-4d5b-8f51-25ccd686acda@googlegroups.com> |
| In reply to | #103971 |
W dniu środa, 15 lutego 2017 22:27:59 UTC+1 użytkownik John Bode napisał: > On Monday, February 13, 2017 at 1:43:15 PM UTC-6, fir wrote: > > if i like to write compiler i should probably start with compiling such as above > > (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that > > > > like > > > > mov tmp1 2 > > mov tmp2 f > > mul tmp1 tmp2 > > > > mov tmp3 d > > mov tmp4 g > > mov tmp5 3 > > div tmp4 tmp3 > > sub tmp3 tmp4 > > > > etc.. how to do that EASILY (easy and short) ? > > The *easy* thing to do is use a toolset like flex and bison to generate your parser and add your custom processing logic to it. > > There's an example of a simple calculator at > http://www-bcf.usc.edu/~breichar/teaching/2011cs360/calculator.pdf that > should get you started. > > Less easy would be writing your own lexer and parser. For arithmetic > expressions like your example, that shouldn't take more than a day or so. i wrote the tokenizer as i call it it is 100 linec of c, i also wrote the reducer as i call it (takes tokenized expression and makes list of 'atom expressions' (a = b op c) as i call it (it is 150 lines of c but it is yet not finished (i mostly written reduction of * / + - and not yet parenthesis) it is not hard, though i stopped it to make some contemplation (i often need some contemplation to refuel some energy, so now i got a break) (i hope i will refuel soon and get back yet this week as it would be nice to get it done)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-15 14:13 -0800 |
| Message-ID | <b84d2d03-e068-4160-b96e-15f7ac973868@googlegroups.com> |
| In reply to | #103972 |
W dniu środa, 15 lutego 2017 22:47:31 UTC+1 użytkownik fir napisał:
> W dniu środa, 15 lutego 2017 22:27:59 UTC+1 użytkownik John Bode napisał:
> > On Monday, February 13, 2017 at 1:43:15 PM UTC-6, fir wrote:
> > > if i like to write compiler i should probably start with compiling such as above
> > > (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that
> > >
> > > like
> > >
> > > mov tmp1 2
> > > mov tmp2 f
> > > mul tmp1 tmp2
> > >
> > > mov tmp3 d
> > > mov tmp4 g
> > > mov tmp5 3
> > > div tmp4 tmp3
> > > sub tmp3 tmp4
> > >
> > > etc.. how to do that EASILY (easy and short) ?
> >
> > The *easy* thing to do is use a toolset like flex and bison to generate your parser and add your custom processing logic to it.
> >
> > There's an example of a simple calculator at
> > http://www-bcf.usc.edu/~breichar/teaching/2011cs360/calculator.pdf that
> > should get you started.
> >
> > Less easy would be writing your own lexer and parser. For arithmetic
> > expressions like your example, that shouldn't take more than a day or so.
>
> i wrote the tokenizer as i call it it is 100 linec of c, i also wrote the reducer as i call it (takes tokenized expression and makes
> list of 'atom expressions' (a = b op c)
> as i call it (it is 150 lines of c but it is yet not finished (i mostly written reduction of * / + - and not yet parenthesis)
>
> it is not hard, though i stopped it to make
> some contemplation (i often need some contemplation to refuel some energy, so now
> i got a break) (i hope i will refuel soon and get back yet this week as it would be nice to
> get it done)
i may say though how i do it and how
slight problems i encountered ,
maybe some will give some good hint (though im sleepy and my tooth hurt destroyed by 3 pseudodentists who destroyed my quite healthy teeth before they ruined it - i swear i will never forget that )
say i got c
i tokenized it so i got table where i got it
the same but tokenized so i got types (if register, immediate, operator or whitespace) and values in easy accesible way
i just reduce muls, i mean i found muls and
replace 2*f here avove as t1 = 2*f
t1 = 2*f
a = t1+(d-g/3)*(9-i)
(the second mul i cant reduce as it mouls
peranthesis block not registers or immediates)
i reduce divs (i mean i do the same with divs)
t1 = 2*f
t2 = g/3
a = t1+(d-t2)*(9-i)
now i can reduce subs
t1 = 2*f
t2 = g/3
t3 = d - t2
t4 = 9-i
a = t1+t3*t4
and again i could reduse muls
t1 = 2*f
t2 = g/3
t3 = d-t2
t4 = 9-i
t5 = t3*t4
a = t1 + t5
and now yet i can reduce adds
t1 = 2*f
t2 = g/3
t3 = d-t2
t4 = 9-i
t5 = t3*t4
t6 = t1 + t5
a = t6
this is algorithm i use
the 'reduction' functions i got written
but only for "* / + - ", they are called
readuce_tokenized_list('*');
readuce_tokenized_list('/');
readuce_tokenized_list('-');
readuce_tokenized_list('+');
already written it and it work, the
problem is in what order to call it if
i got paranthesis in expressions, if i get
expression without parenthesis i just can
call
void reduce_all()
{
readuce_tokenized_list('*');
readuce_tokenized_list('/');
readuce_tokenized_list('-');
readuce_tokenized_list('+');
}
and it is done, but if i got parenthesis i must yet invent exact order how to call that
(and yet write additional function for parenthesis reduction) i not written it yet
and im sleepy - how to exactly do that to make
that simple and logical (and with easy code)?
(i will think on it toomorrow or later)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-15 14:16 -0800 |
| Message-ID | <bd196f44-7b63-4793-a769-60fdcf826e0c@googlegroups.com> |
| In reply to | #103974 |
W dniu środa, 15 lutego 2017 23:13:13 UTC+1 użytkownik fir napisał:
> W dniu środa, 15 lutego 2017 22:47:31 UTC+1 użytkownik fir napisał:
> > W dniu środa, 15 lutego 2017 22:27:59 UTC+1 użytkownik John Bode napisał:
> > > On Monday, February 13, 2017 at 1:43:15 PM UTC-6, fir wrote:
> > > > if i like to write compiler i should probably start with compiling such as above
> > > > (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that
> > > >
> > > > like
> > > >
> > > > mov tmp1 2
> > > > mov tmp2 f
> > > > mul tmp1 tmp2
> > > >
> > > > mov tmp3 d
> > > > mov tmp4 g
> > > > mov tmp5 3
> > > > div tmp4 tmp3
> > > > sub tmp3 tmp4
> > > >
> > > > etc.. how to do that EASILY (easy and short) ?
> > >
> > > The *easy* thing to do is use a toolset like flex and bison to generate your parser and add your custom processing logic to it.
> > >
> > > There's an example of a simple calculator at
> > > http://www-bcf.usc.edu/~breichar/teaching/2011cs360/calculator.pdf that
> > > should get you started.
> > >
> > > Less easy would be writing your own lexer and parser. For arithmetic
> > > expressions like your example, that shouldn't take more than a day or so.
> >
> > i wrote the tokenizer as i call it it is 100 linec of c, i also wrote the reducer as i call it (takes tokenized expression and makes
> > list of 'atom expressions' (a = b op c)
> > as i call it (it is 150 lines of c but it is yet not finished (i mostly written reduction of * / + - and not yet parenthesis)
> >
> > it is not hard, though i stopped it to make
> > some contemplation (i often need some contemplation to refuel some energy, so now
> > i got a break) (i hope i will refuel soon and get back yet this week as it would be nice to
> > get it done)
>
> i may say though how i do it and how
> slight problems i encountered ,
> maybe some will give some good hint (though im sleepy and my tooth hurt destroyed by 3 pseudodentists who destroyed my quite healthy teeth before they ruined it - i swear i will never forget that )
>
> say i got c
say i got a = 2*f+(d-g/3)*(9-i)
> i tokenized it so i got table where i got it
> the same but tokenized so i got types (if register, immediate, operator or whitespace) and values in easy accesible way
>
> i just reduce muls, i mean i found muls and
> replace 2*f here avove as t1 = 2*f
>
> t1 = 2*f
> a = t1+(d-g/3)*(9-i)
>
> (the second mul i cant reduce as it mouls
> peranthesis block not registers or immediates)
>
> i reduce divs (i mean i do the same with divs)
>
> t1 = 2*f
> t2 = g/3
>
> a = t1+(d-t2)*(9-i)
>
> now i can reduce subs
>
> t1 = 2*f
> t2 = g/3
> t3 = d - t2
> t4 = 9-i
>
> a = t1+t3*t4
>
> and again i could reduse muls
>
> t1 = 2*f
> t2 = g/3
> t3 = d-t2
> t4 = 9-i
> t5 = t3*t4
> a = t1 + t5
>
> and now yet i can reduce adds
> t1 = 2*f
> t2 = g/3
> t3 = d-t2
> t4 = 9-i
> t5 = t3*t4
> t6 = t1 + t5
> a = t6
>
> this is algorithm i use
>
>
> the 'reduction' functions i got written
> but only for "* / + - ", they are called
>
> readuce_tokenized_list('*');
> readuce_tokenized_list('/');
> readuce_tokenized_list('-');
> readuce_tokenized_list('+');
>
> already written it and it work, the
> problem is in what order to call it if
> i got paranthesis in expressions, if i get
> expression without parenthesis i just can
> call
>
> void reduce_all()
> {
> readuce_tokenized_list('*');
> readuce_tokenized_list('/');
> readuce_tokenized_list('-');
> readuce_tokenized_list('+');
> }
> and it is done, but if i got parenthesis i must yet invent exact order how to call that
> (and yet write additional function for parenthesis reduction) i not written it yet
> and im sleepy - how to exactly do that to make
> that simple and logical (and with easy code)?
>
> (i will think on it toomorrow or later)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-15 14:40 -0800 |
| Message-ID | <be83d4fa-9c09-4e51-891b-273e0ac69920@googlegroups.com> |
| In reply to | #103975 |
W dniu środa, 15 lutego 2017 23:16:46 UTC+1 użytkownik fir napisał:
> W dniu środa, 15 lutego 2017 23:13:13 UTC+1 użytkownik fir napisał:
> > W dniu środa, 15 lutego 2017 22:47:31 UTC+1 użytkownik fir napisał:
> > > W dniu środa, 15 lutego 2017 22:27:59 UTC+1 użytkownik John Bode napisał:
> > > > On Monday, February 13, 2017 at 1:43:15 PM UTC-6, fir wrote:
> > > > > if i like to write compiler i should probably start with compiling such as above
> > > > > (assume a f d g i are 32bit int (or float dont matter) registers and i need to produce classical simple wirtual assembly for that
> > > > >
> > > > > like
> > > > >
> > > > > mov tmp1 2
> > > > > mov tmp2 f
> > > > > mul tmp1 tmp2
> > > > >
> > > > > mov tmp3 d
> > > > > mov tmp4 g
> > > > > mov tmp5 3
> > > > > div tmp4 tmp3
> > > > > sub tmp3 tmp4
> > > > >
> > > > > etc.. how to do that EASILY (easy and short) ?
> > > >
> > > > The *easy* thing to do is use a toolset like flex and bison to generate your parser and add your custom processing logic to it.
> > > >
> > > > There's an example of a simple calculator at
> > > > http://www-bcf.usc.edu/~breichar/teaching/2011cs360/calculator.pdf that
> > > > should get you started.
> > > >
> > > > Less easy would be writing your own lexer and parser. For arithmetic
> > > > expressions like your example, that shouldn't take more than a day or so.
> > >
> > > i wrote the tokenizer as i call it it is 100 linec of c, i also wrote the reducer as i call it (takes tokenized expression and makes
> > > list of 'atom expressions' (a = b op c)
> > > as i call it (it is 150 lines of c but it is yet not finished (i mostly written reduction of * / + - and not yet parenthesis)
> > >
> > > it is not hard, though i stopped it to make
> > > some contemplation (i often need some contemplation to refuel some energy, so now
> > > i got a break) (i hope i will refuel soon and get back yet this week as it would be nice to
> > > get it done)
> >
> > i may say though how i do it and how
> > slight problems i encountered ,
> > maybe some will give some good hint (though im sleepy and my tooth hurt destroyed by 3 pseudodentists who destroyed my quite healthy teeth before they ruined it - i swear i will never forget that )
> >
> > say i got c
>
> say i got a = 2*f+(d-g/3)*(9-i)
>
> > i tokenized it so i got table where i got it
> > the same but tokenized so i got types (if register, immediate, operator or whitespace) and values in easy accesible way
> >
> > i just reduce muls, i mean i found muls and
> > replace 2*f here avove as t1 = 2*f
> >
> > t1 = 2*f
> > a = t1+(d-g/3)*(9-i)
> >
> > (the second mul i cant reduce as it mouls
> > peranthesis block not registers or immediates)
> >
> > i reduce divs (i mean i do the same with divs)
> >
> > t1 = 2*f
> > t2 = g/3
> >
> > a = t1+(d-t2)*(9-i)
> >
> > now i can reduce subs
> >
> > t1 = 2*f
> > t2 = g/3
> > t3 = d - t2
> > t4 = 9-i
> >
> > a = t1+t3*t4
> >
> > and again i could reduse muls
> >
> > t1 = 2*f
> > t2 = g/3
> > t3 = d-t2
> > t4 = 9-i
> > t5 = t3*t4
> > a = t1 + t5
> >
> > and now yet i can reduce adds
> > t1 = 2*f
> > t2 = g/3
> > t3 = d-t2
> > t4 = 9-i
> > t5 = t3*t4
> > t6 = t1 + t5
> > a = t6
> >
> > this is algorithm i use
> >
> >
> > the 'reduction' functions i got written
> > but only for "* / + - ", they are called
> >
> > readuce_tokenized_list('*');
> > readuce_tokenized_list('/');
> > readuce_tokenized_list('-');
> > readuce_tokenized_list('+');
> >
> > already written it and it work, the
> > problem is in what order to call it if
> > i got paranthesis in expressions, if i get
> > expression without parenthesis i just can
> > call
> >
> > void reduce_all()
> > {
> > readuce_tokenized_list('*');
> > readuce_tokenized_list('/');
> > readuce_tokenized_list('-');
> > readuce_tokenized_list('+');
> > }
> > and it is done, but if i got parenthesis i must yet invent exact order how to call that
> > (and yet write additional function for parenthesis reduction) i not written it yet
> > and im sleepy - how to exactly do that to make
> > that simple and logical (and with easy code)?
> >
> > (i will think on it toomorrow or later)
i mean such algorith will probably work
repeat until done
{
find the top level parenthesis
{
reduce '*'
reduce '/'
reduce '+'
reduce '-'
}
}
(by top level i mean parenthesis block where no other parentesis block is nested)
it is maybe even not so complex but i thought
on some algorithm that would spare me to write this parenthesis function (or only some very
simple as i got those */+- reducing functions
already written and wanted done it quick - but
maybe sadly i will need to write this " find the top level parenthesis" code and nested reductions in it ;c;c im sleepy, damn at least weather is warmer (i was tired with a month or more with average -7 or about)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-16 14:57 -0800 |
| Message-ID | <09101204-d07d-4308-9060-62d6939d7029@googlegroups.com> |
| In reply to | #103972 |
i wrote the reducer.c, almost 500 lines but wery weirdly written - and withot edge cases etc, I was tired and sleepy and if im tired i just go to make something work in many shortcuts and to clarify it later - realy weidly written i also wrote genaretor.c which makes assembly form this 'atoms' list (50 linec of c) (so i got tokenizer.c 150 lines, reducer.c 500 lines, and generator.c 50 lines) for input "a = 2*f+(d-g/3)*(9-i)" it gives such output div t200 3 mov t201 d sub t201 t200 mov t202 9 sub t202 i mov t203 2 mul t203 f mov t204 t201 mul t204 t202 mov t205 t203 add t205 t204 mov a t205 it is probably ok.. this could be optimised as some movs here seem not needed but im to sleepy anyway its ok it works, after all it is already some working nanocompiler ;c the rest is only to build it up ;c
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web