flat assembler
Message board for the users of flat assembler.

 Index > Projects and Ideas > idea for code generator for math functions (optimizing compi
Author
tthsqe

Joined: 20 May 2009
Posts: 739
tthsqe 30 May 2012, 06:05
I had an idea to implement a quality code generator for math functions.
The user would type in a function in a HLL as
Code:
`f(x,y)=x+sin(y)    `

and the program would try to generate good code for computing this function.
This might help when you just need something that works and you don't want to mess around with the tricky fpu stack...

Edit:
To clarify what I mean, I will give an example from my prototype code generator. I think it does produce quality output, as you can see.
suppose the HLL code is something like: the following. ({a,b}={c,d} is a parallel copy)
Code:
```f(x,y):
while x^2 + y^2 < R,
{x, y} = {x^2 - y^2 + X, 2 x y + Y}
end while
return x + y
```

then the program produces the code
Code:
```f:      fld  tword[2]
fld  tword[Y]
fld  tword[X]
fld  tword[R]
fld  tword[esp+4+16*1]  ; 2nd argument
fld  tword[esp+4+16*0]  ; 1st argument
fxch  st4
fstp  tword[esp-16*1]    ; spill location
.1:     fld  st3
fmul  st0,st0
fld  st1
fmul  st0,st0
fld  st0
fcomip  st0,st4
jnb  .2
fsubp  st1,st0
fxch  st1
fmul  st0,st5
fmulp  st4,st0
fld  tword[esp-16*1]
fstp  st5
fxch  st2
fxch  st3
jmp  .1
.2:    fstp  st0
fstp  st0
fstp  st1
fstp  st1
fstp  st2
ret                     ; return value on fpu stack    ```

the fpu stack is a real pain to optimize mechanically!

Last edited by tthsqe on 03 Jun 2012, 16:02; edited 4 times in total
30 May 2012, 06:05
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 19076
revolution 30 May 2012, 06:08
I think that is a task for an HLL compiler. Write it in C (or whatever language one prefers), compile, and extract the resulting assembly code.

However don't expect it to be good code, it will be adequate.
30 May 2012, 06:08
tthsqe

Joined: 20 May 2009
Posts: 739
tthsqe 30 May 2012, 06:54
hmmm..
That doesn't always work:
What if I don't have a HLL compiler? (Or I can't get it to work)
What if the compiler places transcendental functions in some other place?
What if the program could use some simplifying assumptions about the function to produce better code than the compiler.

I was thinking it to be more of a fun exercise in basic HLL -> quality asm transformation. I admit it is probably not that useful.
30 May 2012, 06:54
shutdownall

Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 30 May 2012, 09:12
Well you could create a mathlib (inc) which could be included in projects when needed and publish it here.
30 May 2012, 09:12
typedef

Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 30 May 2012, 09:41
tthsqe wrote:
hmmm..
That doesn't always work:
What if I don't have a HLL compiler? (Or I can't get it to work)
What if the compiler places transcendental functions in some other place?
What if the program could use some simplifying assumptions about the function to produce better code than the compiler.

I was thinking it to be more of a fun exercise in basic HLL -> quality asm transformation. I admit it is probably not that useful.

Use naked functions. You must however make the prologue and epilogue in ASM (inline) like so.

Code:
```__declspec(naked) type convention func_name(parameters...)
{
__asm push ebp
__asm mov ebp, esp

__asm mov esp, ebp
__asm pop ebp
ret                               // return that complies with appropriate convention used.
}
```

Your function will then start with MOV EDI,EDI. You can then re-assemble the code in FASM with probably some optimization and tweaks to suit. Good luck.
30 May 2012, 09:41
JohnFound

Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 30 May 2012, 09:54
Learn, to think assembly language. Then you will be able to freely write any function.
30 May 2012, 09:54
tthsqe

Joined: 20 May 2009
Posts: 739
tthsqe 03 Jun 2012, 23:12
I find thinking in assembly very error-prone when dealing with the x87 fpu - what I'm working on is a homemade optimizing compiler for HLL -> fpu to explore how compilers like gcc or icc work.
03 Jun 2012, 23:12

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
I think the optimizer (or the brain) should turn to FPU only for FSINCOS and such functions, because stuff like MULSD, ADDSD can be accomplished out-of-order in the SSE.
26 Jun 2012, 22:35
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum