Page 2 of 5

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 3:42 pm
by Ryu
They are mainly to support the calling conventions. For an example "cdecl" or the C calling convention will create a stack frame in its prologue, and does not clean the stack in its epilogue which is why you have to restore the stack pointer after the call. While the "stdcall" or mainly found in C++ calling conventions will clean the stack in its epilogue before returning. However there are some other code that can be placed in the epilogue and prologue which varies on the compiler. Such as MSVC will add some stack checking calls in debug mode. I'm basically three thumbs up for costum prologue and epilogues which should be defined by the calling convention somehow which I see many many uses and portability it gives to a programming language.

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 5:34 pm
by earlz
I might somehow implement that as custom externs
or something like that or even just type modifiers for functions but anyway

Re:a programming language "kindof" designed for OS

Posted: Sun Aug 06, 2006 2:53 am
by Lord_Coder
Jordan3 wrote: Hi I'm currently designing a programming language
it was encouraged by how many people have to do work arounds in C to get their kernel to do what they want

what I thought about is basically complete linker and compiler control from the source file(conflicts when compiling multiple objs and linking them together but...)

with my current design(its all in my mind so will change as I code)
the features will be:
1. EXTREMELY C based, basically like an enhanced/non-standard C
2. will support a thing I like to call "variable to function aliasing" which is where you can make a special variable like do somehting like this:

Code: Select all

ALIAS unsigned int blah=0:&Func1,&Func2; //Func1 is for when someone reads it, Func2 is when someone sets it

unsigned int Func1(){
    return 1;
}

void Func2(unsigned int blah){
    //you can set something here
}

void _main(void){
    unsigned char tmp;
    blah=23 //this will call Func2(23)
    tmp=blah; //this will call Func1 and will set tmp to 1
}
(very useful for things like registers and ports)

3.it will support linked lists like this:

Code: Select all

typedef struct{
   unsigned int blah;
   NEXT_PTR: void *next; //signifies that this is to be used in linked listing for the "next" pointer
}linky;

void _main(){
   LINKED_LIST linky test[5];
   //that will create 5 of those structs and set next to the next one of course
  test[4].next=0x40
  test[5].blah=20 //writes blah to the address of 0x40
}
of course I will need major optimization or this will be very very slow(and proabbly still will be but is still is easy to do)

4. also will have complete control over optimizations, and if you enable none then it doesn't do those annoying things like store variables in registers
and can do this

Code: Select all

void _main(){
   ?Optimization: variable_registers.on //the ? is for compiler options and the ending .on is to turn it on or off of course
   blah=25;
   //the variable is stored in a temporary register!
   ?Optimization: variable_registers.off; //turn it off
   blah=54; //this is written to the actual memory address of the vairable now!
}
which is very nifty except for I don't know what I will do since the variable is stored in a register, exactly how to write it to the variable when I turn off the optimization but anyway

5.complete control over how the compiler behaves at compile time like this

Code: Select all

?Temp_Register1: EAX;
blah=2+2+blah2; //this will use eax for a temporary vairable for the maths
?Temp_Register1: EBX;
blah=2+2+blah2; //and now it uses EBX
?Temp_Register1: STACK;
blah=2+2+blah2; //and now it uses the stack, and if it uses other vairables then it pusha's them
read on in next post
Your programming language is very cool and can give us a better control of the generated asm code !

Re:a programming language "kindof" designed for OS

Posted: Sun Aug 06, 2006 4:43 am
by Cheery
Why C dialect? Why not simpler language like basic or python dialect?

Many times I've found that C abstracts away too much to be low level language, but it doesn't abstract away enough to be high level language. How could you implement more stuff to it without tieing out yourself?

And how do you implement alternate languages over your language's ABI? (including interpreted languages)

Re:a programming language "kindof" designed for OS

Posted: Sun Aug 06, 2006 8:06 am
by earlz
whats ABI?

also I'm making it C because it is a very common language already for making OS's and almost every tutorial is written in rather C or ASM so it just seems right

also I don't really see a problem with C, it just is a barebone language starting out providing you just what you need, asm and memory ptr access


and besides in basic I'd be expected to make a complete library of functions such as print and input and this way I don't have to make it for you to get started! ;D
And how do you implement alternate languages over your language's ABI? (including interpreted languages)
ok I googled it but I still don't really know what you mean
I really just don't know what you mean so please post and explain that

Re:a programming language "kindof" designed for OS

Posted: Mon Aug 07, 2006 1:51 am
by Cheery
Application Binary Interface.

The one which defines all kinds of function call conventions, how structures are interpreted, is code modifyable runtime, etc.

C is not a barebone either. It already contains the syntax, every example is in C because most people knows C and unix was written with it.

I'd call something like LOGO, Lisp or Forth barebones.

Re:a programming language "kindof" designed for OS

Posted: Mon Aug 07, 2006 5:52 am
by Solar
Jordan3 wrote: whats ABI?
Something you should know the meaning of before designing your own language. ;D

*ducks and runs*
Cheery wrote: C is not a barebone either. It already contains the syntax, every example is in C because most people knows C and unix was written with it.
Hmmmm... most examples are in C because it requires only the most basic of runtime environments, is well-suited for hardware-banging programming, and interfaces very well with assembler. It is also the language virtually all other language support is written in (e.g. Perl interpreters, C++ compilers, ...), and as a teaching language, gives you a very good tradeoff between teaching simplicity and real-life applicability.

Re:a programming language "kindof" designed for OS

Posted: Mon Aug 07, 2006 11:01 am
by Crazed123
Solar wrote:
Jordan3 wrote: whats ABI?
Something you should know the meaning of before designing your own language. ;D

*ducks and runs*
He's dead right, you know. To write a language you should really know exactly what the running code will look like in memory, ie: the language ABI.
Cheery wrote: C is not a barebone either. It already contains the syntax, every example is in C because most people knows C and unix was written with it.
Hmmmm... most examples are in C because it requires only the most basic of runtime environments, is well-suited for hardware-banging programming, and interfaces very well with assembler. It is also the language virtually all other language support is written in (e.g. Perl interpreters, C++ compilers, ...), and as a teaching language, gives you a very good tradeoff between teaching simplicity and real-life applicability.
A tiny correction: C doesn't require any runtime environment. If you write a kernel in nothing but C and assembler (assuming you don't call any functions you didn't write yourself), the compilation won't require any runtime code at all.

Furthermore, C was actually designed for systems programming. The upside of this is that it makes C a great base to start a systems language from. The downside is that it makes C a rather dirty language, with few of the more useful features other languages have, such as classes, first-class functions/closures, garbage collection (which would be desirable in some places), or built-in data structures (ie: consing, hash tables, etc).

That doesn't mean a good systems programming language lacks those things, however. It just means you have to make the runtime library small and free-standing (not relying on an underlying OS), neither of which is actually so difficult.

Re:a programming language "kindof" designed for OS

Posted: Mon Aug 07, 2006 11:10 pm
by Brendan
Hi,

Let's start from the very beginnng - the syntax and grammar of the language. What is needed, and what is desirable?

Most languages have a few basic things - comments, variables, mathmatical operators, do/until and while loops, if/elseif/else and functions/procedures. On top of this it's convenient to add some extra things like arrays and type-checking. C has all of this (although the type-checking is debatable).

The syntax and grammar should be readable, so that people who have never even seen the language before can at least read the source code and have a fair chance of understanding it. BASIC and Pascal fit in this category, but C fails miserably.

To improve productivity you want some form of error detection, so that if the source code is wrong it can tell the author exactly where the problem is (e.g. so that they don't need to spend hours searching for a missing bracket or curly brace). Unfortunately C fails miserably again.

It's also a very good idea to remove all machine-dependant behaviour from the language, so that people can write portable code. C tries, but if you need a 32 bit integer do you use a short, an int or a long? This includes the "standard libraries", which should be standard and should do enough to write simple applications. For C, the standard libraries are standard, but don't do enough. This means non-standard libraries are often required, which makes things difficult because they are non-standard. To get around the problems caused people use other tools (like autoconfigure).

Once you've decided on the syntax & grammar, the next step is the pre-processor. Quite frankly I'm used to assembly where the pre-processor is normally reasonably powerful. The pre-processor for C is a joke. To get around this people use scripting languages to "pre-pre-process" (e.g. Perl).

Next comes the parser, but for C the syntax & grammar makes it one of the hardest languages to parse.

Finally there's compiling, optimizing, assembling and linking. These steps seem fairly similar regardless of the syntax and grammer. However there's certain optimizations that can be done relatively easily if the syntax & grammer is suitable.

The first optimization is "function inlining". My idea here is to inline everything (so that there's one huge function), then optimize it, then do pattern matching to find commonly repeated sequences to turn back into functions, then optimize again. For a simple example, consider the following:

Code: Select all

int foo(char a, char b) {
   b = b * a;
   return b + a + 3;
}

int bar(char c, char d) {
    return foo(3, c) + foo(2, d) - foo(c, d) + foo(c , 5);
}
First this would be expanded:

Code: Select all

int bar(char b, char c) {
    temp1 = 3 * c;
    temp1 = temp1 + 3 + 3;

    temp2 = 2 * d;
    temp2 = temp2 + 2 + 3;

    temp3 = c * 5;
    temp3 = temp3 + c + 5;

    return temp1 - temp2 + temp3;
}
Then optimized:

Code: Select all

int bar(char c, char d) {
    return 9 * c - 2 * d + 6;
}
Then converted back to smaller functions:

Code: Select all

int tempFunction(char a, char b) {
    return a * b;
}

int bar(char c, char d) {
    return tempFunction(9, c) - tempFunction(2, d) + 6;
}
Of course the example is too small, but you get the idea. This also allows the compiler to optimize for size or speed relatively easily, just by being more intelligent when deciding which matched patterns are converted back into functions.

Another idea is the ability to replace an entire function with a table lookup. For example, consider this:

Code: Select all

Uint16 someFunction(Uint8 a, Uint16 b) {
    Uint16 result;

    ..heaps of complex stuff with no side-effects..

    return result;
}
It's reasonably obvious from the function declaration that the complex stuff can be replaced by a table of 256 * 65536 16 bit integers. During compilation, the table can be auto-generated by interpretting the function with each possible combination of input parameters.

With strong type-checking this becomes more viable - what if the variable "a" above always contained either 0, 1 or 2, and the variable "b" was always between 50 and 1050? In this case you could automatically generate code to check that the input paramaters are within range and then replace the function with a table of 3 * 1000 16 bit integers.

For consistancy, you could use the same syntax to access the elements of an array as you would to call a function. For example:

Code: Select all

char foo[5] = {1, 3, 5, 7, 11};

char bar(char a) {
   return a * a;
}

char example(void) {
   char x, y;

   return foo(x) + bar(y);
}
This allows the array "foo" to be changed into a function, or the function "bar" to be changed into an array at any time, without any of the other code being changed.

[continued]

Re:a programming language "kindof" designed for OS

Posted: Mon Aug 07, 2006 11:11 pm
by Brendan
[continued]

The next step is to handle multiple return parameters:

Code: Select all

char foo[5][2] = { {1, 2},
                         {3, 4},
                         {5, 6},
                         {7, 8},
                         {11, 12} };

(char, char) bar(char a, char b) {
   return a * a, b * a;
}

(char, char) example(void) {
   char x, y;

   (x, y) = foo(x);
   (x, y) = bar(x, y);

   return x, y;
}

If you combine all of this you might end up with something like this:

Code: Select all

(u8, u8, u8, u8) splitColours(u32 colour) {
    u8 alpha, red, green, blue;

    alpha = (colour >> 24) & 0xFF;
    red = (colour >> 16) & 0xFF;
    green = (colour >> 8) & 0xFF;
    blue = colour & 0xFF;

    return alpha, red, green, blue;
}


u32 combineColours(u8 alpha, u8 red, u8 green, u8 blue) {
    u32 colour;

    colour = alpha << 24 | red << 16 | green << 8 | blue;
    return colour;
}


u32 makeDarker(u32 colour) {
    u8 alpha, red, green, blue;

    (alpha, red, green, blue) = splitColours(colour);
    red = red >> 1;
    green = green >> 1;
    blue = blue >> 1;
    return combineColours(alpha, red, green, blue);
}
Which is expanded into:

Code: Select all

u32 makeDarker(u32 colour) {
    u8 alpha, red, green, blue;

    temp1 = (colour >> 24) & 0xFF;
    temp2 = (colour >> 16) & 0xFF;
    temp3 = (colour >> 8) & 0xFF;
    temp4 = colour & 0xFF;
    alpha = temp1;
    red = temp1;
    green = temp1;
    blue = temp1;
    red = red >> 1;
    green = green >> 1;
    blue = blue >> 1;
    temp5 = alpha << 24 | red << 16 | green << 8 | blue;
    return temp5;
}
Then optimized into:

Code: Select all

u32 makeDarker(u32 colour) {
    return (colour & 0xFF000000) | ( ( (colour >> 17) & 0x7F) << 16) | ( ( (colour >> 9) & 0x7F) << 8) | ( (colour & 0xFF) >> 1);
}
Then converted back into:

Code: Select all

u32 tempFunction(u8 a, u8 b) {
    return ( ( (a >> (b + 1)) & 0x7F) << b);
}

u32 makeDarker(u32 colour) {
    return (colour & 0xFF000000) | tempFunction(colour, 16) | tempFunction(colour, 8) | ( (colour & 0xFF) >> 1);
}
Then optimized to:

Code: Select all

enum tempArrayTypes {VAL16, VAL8};

u32 makeDarker(u32 colour) {
    return (colour & 0xFF000000) | tempArray(VAL16, colour) | tempArray(VAL8, colour) | ( (colour & 0xFF) >> 1);
}
To finally produce something like:

Code: Select all

makeDarker:
     mov esi, eax
     and eax, 0xFF000000
     or eax, [esi * 4]
     or eax, [esi * 4 + 256 * 4]
     and esi,0xFF
     shr esi,1
     or eax,esi
     ret

As a final note, please don't take any of this too seriously - my reason for posting all this is to get you thinking about different possibilities (none of it is really intended to be taken literally). :)


Cheers,

Brendan

Re:a programming language "kindof" designed for OS

Posted: Tue Aug 08, 2006 10:31 am
by Candy

Code: Select all

class color {
 public:
  char r, g, b, a;
  color(char r, char b, char g, char a) : r(r), g(g), b(b), a(a) {}
  color operator>>(int amount) {
   color retVal(*this);
   retVal.r = r >> amount;
   retVal.g =  >> amount;
   retVal.b = r >> amount;
   retVal.a = r >> amount;
   return retVal;
  }
  color mix(const color &other, double amount) {
   if (amount <= 0.0f) return color(*this);
   if (amount >= 1.0f) return color(other);
   color retVal;
   retVal.r = (char)((other.r * amount) + (r * (1.0f - amount)));
   retVal.g = (char)((other.g * amount) + (g * (1.0f - amount)));
   retVal.b = (char)((other.b * amount) + (b * (1.0f - amount)));
   retVal.a = (char)((other.a * amount) + (a * (1.0f - amount)));
   return retVal;
  }
  operator unsigned int () {
   unsigned int retVal = 0;
   retVal += a << 24;
   retVal += b << 16;
   retVal += g << 8;
   retVal += r << 0;
   return retVal;
  }
}

int main() {
  color a(0x123456), b(255, 255, 255, 0);
  color c(a.mix(b, 0.43f));
  printf("mix of these two is %06x or (%2d, %2d, %2d)\n", (unsigned int)c, c.r, c.g, c.b);
}
Yes, I had to ruin it with that god awful printf syntax.

Given a powerful enough language and a brain that understands it in full, you can create it yourself.

Notes for people interested:
- You need to actually create a proper copy constructor. This one will probably work but will stop working as soon as you add some interesting stuff.
- You could easily make it a template class. Add "template <class C>" above the "class" line and substitute every char with C. The encoding and decoding would need to be adjusted for non-8-bit-integer values, though.
- You'd need to hack it a bit more for the class to be real fun, such as adding a bunch of other operators and so on.
* for a bonus: figure out how to properly make trinary operators.

Re:a programming language "kindof" designed for OS

Posted: Wed Aug 09, 2006 12:25 am
by Brendan
Hi,
Candy wrote:Given a powerful enough language and a brain that understands it in full, you can create it yourself.
That's the point...

Can a person who is not familiar with the language understand it? I'm reasonable familiar with C and I have a hard time understanding it, so no.

Is it portable? Depending on the platform/compiler, a char is either signed or unsigned and when signed integers are shifted to the right they may be zero extended or sign extended. Therefore, this code is not portable.

Can a function return multiple values? This one is debatable - a function can return a single structure (although if you compile it and look at the output don't be surprised if it's actually using a pointer to a structure as a hidden input parameter). In any case, the C calling convention won't allow it.

Of course I'm not saying C/C++ is bad, only that there's ways to do things differently, or better (or worse)...


Cheers,

Brendan

Re:a programming language "kindof" designed for OS

Posted: Wed Aug 09, 2006 2:25 am
by Solar
Crazed123 wrote:
A tiny correction: C doesn't require any runtime environment.
When you compile to a freestanding application (i.e., a kernel), no. When you compile to userspace, yes. But as I said, it is minimal.

Re:a programming language "kindof" designed for OS

Posted: Wed Aug 09, 2006 3:08 am
by Pype.Clicker
The notion of "returning multiple values" is not much differrent from "returning a single list of values". what is indeed interesting is the possibility to catch that list and assign its elements to a collection of variables.

In other words, if you could have an object X which you can use to build a list of l-values, e.g.

Code: Select all

   uint r; uint g; uint b;
   X(&r, &g, &b);
And then you could overload the operator that copy the list of values returned by "split_color" into X so that variables r, g and b are indeed modified, e.g. something like

Code: Select all

   X(&r, &g, &b) = split_color(darkblue);
that'd do the trick, no ? you could just use r, g and b ... I'm not a C++ guru, but iirc, there should be no major technical difficulty to achieve this, right ?

Re:a programming language "kindof" designed for OS

Posted: Wed Aug 09, 2006 5:02 am
by DruG5t0r3
php does something like this...if my memory is good it does something like this

Code: Select all

$blah = array( "red" => "200", "blue" => "100", "green" => "142");
list($red, $blue, $green) = extract($blah);
OK...I know PHP is way out of it when talking about OSdev...

But still like Pipe is saying I'm sure we can implement that through some sort of operator overloading.