C-like compiler with devkit?

Discussion of hardware and software development for Super NES and Super Famicom. See the SNESdev wiki for more information.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
creaothceann
Posts: 611
Joined: Mon Jan 23, 2006 7:47 am
Location: Germany
Contact:

Re: C-like compiler with devkit?

Post by creaothceann »

calima wrote: Sun Apr 30, 2023 9:44 am
psycopathicteen wrote: Sun Apr 30, 2023 8:49 am If somebody is making a C-like compiler, I would like to see something like this:

a = b + c - d | e & f

get assembled as:

lda b
clc
adc c
sec
sbc d
ora e
and f
sta a

Just going left to right and treating the accumulator as whatever the answer to the last arithmetic/logical operator.
That's unlikely to happen since math order and range are usually taken into account.
Maybe this could be temporarily turned on/off?

In Free Pascal a feature like that would look similar to this:

Code: Select all

{$push}  {$OperatorPrecedence off}
a := b + c - d OR e AND f;
{$pop}
My current setup:
Super Famicom ("2/1/3" SNS-CPU-GPM-02) → SCART → OSSC → StarTech USB3HDCAP → AmaRecTV 3.10
kennyroger
Posts: 5
Joined: Fri Mar 31, 2023 9:33 am

Re: C-like compiler with devkit?

Post by kennyroger »

take a look at it
I don't know if this is what you want to do..
but if not..
create and post a video of the project to learn and join together with you

https://github.com/alekmaul/pvsneslib
Myself086
Posts: 158
Joined: Sat Nov 10, 2018 2:49 pm

Re: C-like compiler with devkit?

Post by Myself086 »

kennyroger wrote: Sun Apr 30, 2023 8:00 pm create and post a video of the project to learn and join together with you
Are you asking me to make videos of the making of this project as it develops?
psycopathicteen
Posts: 3140
Joined: Wed May 19, 2010 6:12 pm

Re: C-like compiler with devkit?

Post by psycopathicteen »

calima wrote: Sun Apr 30, 2023 9:44 am That's unlikely to happen since math order and range are usually taken into account.
I thought C already did things from left to right. Which order does C use?
creaothceann
Posts: 611
Joined: Mon Jan 23, 2006 7:47 am
Location: Germany
Contact:

Re: C-like compiler with devkit?

Post by creaothceann »

psycopathicteen wrote: Mon May 01, 2023 7:15 am I thought C already did things from left to right. Which order does C use?
https://en.cppreference.com/w/c/languag ... precedence
My current setup:
Super Famicom ("2/1/3" SNS-CPU-GPM-02) → SCART → OSSC → StarTech USB3HDCAP → AmaRecTV 3.10
psycopathicteen
Posts: 3140
Joined: Wed May 19, 2010 6:12 pm

Re: C-like compiler with devkit?

Post by psycopathicteen »

I wonder why the order of operation works like that. I know in mathematics, people do multiplication and division first so that makes sense, but I don't know why logic is done in that order.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: C-like compiler with devkit?

Post by Pokun »

It follows math rules for things that are used in math, but there are many operators not used in regular math so there is a need for a consensus in which order a C-compliant compiler to work them in so that the same code always compiles the same way.
For example, the unary plus and minus doesn't really exist in math (as -x could always really just be seen as shorthand for 0-x in order to negate x), and there are things like the bitwise and logical operators which comes from Boolean algebra.

So the rules are probably just arbitrarily chosen, just like the rules in regular math were arbitrarily chosen (though most of it a thousands of years ago).
kennyroger
Posts: 5
Joined: Fri Mar 31, 2023 9:33 am

Re: C-like compiler with devkit?

Post by kennyroger »

Myself086 wrote: Sun Apr 30, 2023 10:14 pm
kennyroger wrote: Sun Apr 30, 2023 8:00 pm create and post a video of the project to learn and join together with you
Are you asking me to make videos of the making of this project as it develops?
Vídeos da lib já pronta .. tipo exemplo estilo esse link que te mostrei .. eles tem tutorial mas não está completo.. seria legar ver vídeo sobre entendeu
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: C-like compiler with devkit?

Post by Oziphantom »

& combines while | devides.

so A & B | C & D
(A & B) | (C & D)
A & ( B | C) & D

so you are combining the results of A masked by B and C masked by D. mask A by the result of B or C and then masking by D doesn't really make logical sense.
Myself086
Posts: 158
Joined: Sat Nov 10, 2018 2:49 pm

Re: C-like compiler with devkit?

Post by Myself086 »

Does anyone know a good way of parsing C code?
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: C-like compiler with devkit?

Post by Oziphantom »

most compiler books cover parsing C. There are probably a pile of build ones you can just use, but something like yacc/bison. But C is generally simple enough that strtok will get you quite a long way with it.
Myself086
Posts: 158
Joined: Sat Nov 10, 2018 2:49 pm

Re: C-like compiler with devkit?

Post by Myself086 »

How do we want to deal with arrays containing elements larger than 1 byte?
If we need to pass an index for this array around, we'd lose some performance by having to multiply it each time.
creaothceann
Posts: 611
Joined: Mon Jan 23, 2006 7:47 am
Location: Germany
Contact:

Re: C-like compiler with devkit?

Post by creaothceann »

I found something interesting: a C compiler for 8-bit machines
My current setup:
Super Famicom ("2/1/3" SNS-CPU-GPM-02) → SCART → OSSC → StarTech USB3HDCAP → AmaRecTV 3.10
User avatar
NovaSquirrel
Posts: 483
Joined: Fri Feb 27, 2009 2:35 pm
Location: Fort Wayne, Indiana
Contact:

Re: C-like compiler with devkit?

Post by NovaSquirrel »

Myself086 wrote: Wed May 10, 2023 12:35 am How do we want to deal with arrays containing elements larger than 1 byte?
If we need to pass an index for this array around, we'd lose some performance by having to multiply it each time.
I feel like the ideal outcome here is to have it where the index variable is pre-multiplied, and always contains a multiple of whatever size each array element is. That makes me think of how pointers work in C, where incrementing a short pointer actually adds 2, and incrementing a long pointer actually adds 4, and so on.

So maybe there should be some sort of syntax for "index to [type]" like how C has syntax for "pointer to [type]"? And that would create a data type with that behavior. There could be some behavior where copying an index variable to/from a regular integer variable could convert the value, in order to avoid surprises.
Myself086
Posts: 158
Joined: Sat Nov 10, 2018 2:49 pm

Re: C-like compiler with devkit?

Post by Myself086 »

Posting about progress. I got this code snippet to compile correctly.

Code: Select all

u16 AddTest(regx u16 left, rega u16 right)
{
	return left + right;
}
Generated byte code. It's based around having no register except for "Movr".

Code: Select all

AddTest:
    Param   <u16> Local_0
    Param   <u16> Local_1
    Movr    <u16> Local_0, r1
    Movr    <u16> Local_1, r0
    Add     <u16> Temp_0, Local_0, Local_1
    Return  <u16> Temp_0
    Delete  Temps
Generated 65816 assembly code. It's meant to generate assembly source for my assembler where RETURN can either produce RTS or RTL.

Code: Select all

AddTest:
	STX	Local_0
	STA	Local_1
	CLC
	ADC	Local_0
	STA	Temp_0
	RETURN
"Local" and "Temp" are always located in the direct page. Swapping "rega" and "regx" doesn't add a TXA.

There are a few steps missing to generate binaries but I'm not worried about it at the moment.
Post Reply