a programming language "kindof" designed for OS De
Re:a programming language "kindof" designed for OS
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
I might somehow implement that as custom externs
or something like that or even just type modifiers for functions but anyway
or something like that or even just type modifiers for functions but anyway
Re:a programming language "kindof" designed for OS
Your programming language is very cool and can give us a better control of the generated asm code !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:(very useful for things like registers and ports)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 }
3.it will support linked lists like this: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)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 }
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 thiswhich 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 anywayCode: 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! }
5.complete control over how the compiler behaves at compile time like thisread on in next postCode: 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
Code: Select all
Re:a programming language "kindof" designed for OS
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)
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
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
I really just don't know what you mean so please post and explain that
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
ok I googled it but I still don't really know what you meanAnd how do you implement alternate languages over your language's ABI? (including interpreted languages)
I really just don't know what you mean so please post and explain that
Re:a programming language "kindof" designed for OS
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.
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
Something you should know the meaning of before designing your own language. ;DJordan3 wrote: whats ABI?
*ducks and runs*
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.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.
Every good solution is obvious once you've found it.
Re:a programming language "kindof" designed for OS
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.Solar wrote:Something you should know the meaning of before designing your own language. ;DJordan3 wrote: whats ABI?
*ducks and runs*
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.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.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.
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
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:
First this would be expanded:
Then optimized:
Then converted back to smaller functions:
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:
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:
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]
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);
}
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;
}
Code: Select all
int bar(char c, char d) {
return 9 * c - 2 * d + 6;
}
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;
}
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;
}
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);
}
[continued]
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Re:a programming language "kindof" designed for OS
[continued]
The next step is to handle multiple return parameters:
If you combine all of this you might end up with something like this:
Which is expanded into:
Then optimized into:
Then converted back into:
Then optimized to:
To finally produce something like:
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
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);
}
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;
}
Code: Select all
u32 makeDarker(u32 colour) {
return (colour & 0xFF000000) | ( ( (colour >> 17) & 0x7F) << 16) | ( ( (colour >> 9) & 0x7F) << 8) | ( (colour & 0xFF) >> 1);
}
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);
}
Code: Select all
enum tempArrayTypes {VAL16, VAL8};
u32 makeDarker(u32 colour) {
return (colour & 0xFF000000) | tempArray(VAL16, colour) | tempArray(VAL8, colour) | ( (colour & 0xFF) >> 1);
}
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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Re:a programming language "kindof" designed for OS
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);
}
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
Hi,
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
That's the point...Candy wrote:Given a powerful enough language and a brain that understands it in full, you can create it yourself.
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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Re:a programming language "kindof" designed for OS
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.Crazed123 wrote:
A tiny correction: C doesn't require any runtime environment.
Every good solution is obvious once you've found it.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:a programming language "kindof" designed for OS
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.
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
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 ?
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);
Code: Select all
X(&r, &g, &b) = split_color(darkblue);
Re:a programming language "kindof" designed for OS
php does something like this...if my memory is good it does something like this
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.
Code: Select all
$blah = array( "red" => "200", "blue" => "100", "green" => "142");
list($red, $blue, $green) = extract($blah);
But still like Pipe is saying I'm sure we can implement that through some sort of operator overloading.