Page 1 of 5

a programming language "kindof" designed for OS De

Posted: Fri Aug 04, 2006 10:37 pm
by earlz
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

Re:a programming language "kindof" designed for OS

Posted: Fri Aug 04, 2006 10:38 pm
by earlz
6. control over how it compiles for different bits(16,unreal,32)
like this:

Code: Select all

?DEFAULT_PTR: 32BIT; //this is to set the default ptr type if none is specified
16BIT unsigned char* tmp1; //for near ptrs in realmode
20BIT unsigned char* tmp2; //for far ptrs in realmode, at runtime the top 4bits are taken off and switched segments
32BIT unsigned char* tmp3; //for 32bit and also for unreal mode, if in unreal mode then it uses a temp register(32bit) to do it in 32bit
48BIT unsigned char* tmp4; //for 32bit but not realmode, at runtime the top 16bits are taken off and used for a segment switch

?USE: 16BIT;
tmp1=0x1000;
*tmp1=235; // writes it to 0x1000, using the current data segment
tmp2=0x41000;
*tmp2=231; //writes to linear address 0x41000, or segemented: ds:0x400, mem:0x1000 (I think so... haven't done realmode in a while)
//insert code here to switch to protected mode 
?USE: 32BIT;
tmp3=0x100000;
*tmp3=22; //writes 32 to blah
//insert code to switch to unrealmode here
?USE: UNREAL;
//and do it above
well I'm getting tired so....

7. and it ofcourse supports things like far functions in both real, unreal and for segmented protected mode(no code design right now)

8. and possibly section support like this:

Code: Select all

?[HEADER]
//stuff to be put in the top of the binary file here,
JUMP _main(); //this will jump to it instead of calling it
?[_CODE_] //added the _ because of the forum
//_main and such can go here along with data but if you prefer...
?[DATA]
unsigned char blah[43];
//data can go here
?[ENDING]
//stuff to be attached to the end can go here
unsigned short BootSig=0x55AA;
?[BSS: SPECIAL_UNINIT] //The special uninit thing means to not add anyting to it(ignores set values), though I will have an optimization for if the vairable is not equal to anything then use htis section
9. also a special nifty thing for asm stuff(supporting intel syntax!)

Code: Select all

_main(){
unsigned int blah;
asm{ //this applies vairable replacement
mov [_blah],24
}
asm RAW{ //this does not apply variable replacement
mov [_blah],24 ;this will cause an error unless blah is defined in asm somewhere
}
}
now in most C stuff it will not work because stack variables are not in the symbol table but I do variable replacement which will replace variable names with the actual memory or stack address


10. where ever you do a prototype for an external function is where it is put in the binary file,(just hte function, not the whole library/object)

11. supports stronger macro support not sure on how to code this yet so cant really show what I mean(I mean by like for loops which is useful for ISR's)

and well blah hopefull I haven't exceeded my posting limit

and well having to double post it so obviously not


edit:
of course, I probably won't be able to get very far on it but hey thats my goals and just wanted share it with everyone to see if anyone sees any design flaws or comments
I haven't even got to source code formatting/preprocessing! so who knows
btw
I want to be able to support coff, .a(static lib) and probably some sort of custom object format

edit2:
also I will try to keep it from using advanced functions, so all I will need is atoll()(convert string to number), strcmp(), strlen(), memcpy(), fopen,fclose,feof,fgetc and thats it, so it should be able to run on new OS's with the exclusion of nasm, but I will use machine code templates so nasm will not be required unless you use asm{}

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 9:02 am
by proxy
points 1, 2, 3, 11 all fall under the catagory of "c++ gives us this in some way".

also, the linked list stuff, you can sort of do that in c99 with anonymous arrays :). with a little macro trickery, you can make it fairly readable too! try this, it creates a linked list using stack variables (but could be in the data section too).

Code: Select all

#include <stdio.h>
#define cons(x,y) (struct llist[]){{x,y}}

struct llist {
   int value;
   struct llist *next;
};

int main() {
   struct llist *lst = cons(1, cons(2, cons(3, cons(4, 0))));
   struct llist *p = lst;
   
   while(p != 0) {
      printf("%d\n", p->value);
      p = p->next;
   }
   
   return 0;
}

4 i fail to see the point since you can speficy clobber lists in gcc inline asm, or at the very least, if you need register level control, just write that small portion in asm :-P

5. possibly a useful idea :) but if you use inline asm that clobbers registers, most any compiler will be smart enough to change the temp registers it uses.

6 and 7, useful to be honest, modern OSes spend pretty much there entire life in protected mode. So unless you are writing the bootloader too, it simply wouldn't get used.

8. any of linker script/pragmas/gcc attributes will do this.

9. useful, looks JUST like visual c++ except for the raw thing, and yes VC++ supports stack variables too. It's not that hard, you dont need to worry about sybols at all cause you know how to address the variable at compile time when generating the function. (think about it, you know how to access all the variable in the c code...)

10. interesting, but what if the prototype is declared in several files (like in a header file which is included in many source file). Perhaps you meant definition not prototype? because prototype is really a declaration, not a definition which would be the full function with its body.

not horrible ideas, i just am not seeing the big payoff.

proxy

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 9:44 am
by earlz
meh, probably not, and after trying to get tabbed, randomly spaced code to interpretatble it really doesn't seem wrth it but still

also
how would you do #2 in C++? and another thing is I wont have that random extra space so you could possibly use it for bootloader making, of course would most likely not be fast/effiecent enough though but it is possible

also(again) for #8 it could make it very useful for custom executable formats, so users for a small OS won't have to use a special port of gcc or whatever

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 9:53 am
by GLneo
i think #2 is very cool, that would be useful reading and writing to drivers too!

p.s. your language should be called c=1; ;D

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 10:02 am
by earlz
lol
I though about just C+ or -C+ because it does have its bad tradeoffs...


bad thing about this design is its not OO but OO I do not even fully understand(still reading through my C++ book) so its just too complex though I want to support what I believe is called Object Based, to where you can do things like make functions in structs/classes but without having to worry about polymorphism and virtual stuff

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 10:12 am
by bluecode
point 2 would be done via overloading the = operator of a C++ class.

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 10:15 am
by proxy
#2 is pretty easy in c++ to be honest, basic operator overloading...


struct T {
   T() {
      // do what you want on creation, or nothing at all
   }

   T &operator=(unsigned int value) {
      // do what you want when an object
      // of type T is assigned
      return *this;
   }

   operator unsigned int() const {
      // reading it results in value 1 being returned..
      return 1;
   }

private:
   // likely want to overload copy/assignment
   // operators for type T to prevent misusage
   // but not neccessary
   T &operator=(T &);
   T(const T&);

private:
   unsigned int data;
};

proxy

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 10:20 am
by earlz
ahhhh I get it.... wait no I don't
nah I'm sure its possible just I have much more C experience than C++

also has anyone ever noticed you can kindof put an OO face onto C using function ptrs?
like so

Code: Select all

typedef void (*Func)(void);
typedef struct{
  Func Function=&MyFunc; //not sure tghis is legal but...
}Blah;
void MyFunc(){
//blah!
}
Blah My;
void _main(){
   My.Function();
}
though it really loses the ease of OO but still looks neat

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 10:25 am
by proxy
jordan, what you described is litterally what a virtual function table in c++ looks like under the hood.

in fact you can take it a little further an make it really c++-ish :) Some macro trickery and you can do a lot of c++ style programming in C.

struct T;
int F1(struct T*this, int param);

struct T {
int (*func)(struct T*, int);
};

struct T object;
object.func = F1;

/* later in code */
object.func(&object, 5);


/* we now have simulated c++ calling convention by providing a "this" pointer (which is implicit in c++) */

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 10:28 am
by proxy
BTW, for function pointers, you dont need to use the address of operator "&" to get the address of a function (though it is equally legal), the name itself is a pointer.

typedef void (*f_ptr)(void);

void test(void);

/* these are the same */
f_ptr p = &test;
f_ptr p = test;

proxy

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 10:29 am
by earlz
hmmm, nifty......


edit:
hmmm intresting... the C preproccessor leaves taking off random spaces/tabs to the compiler! if you just preproccess it only does # stuff and removes comments!

haven't really thought about relying on the interpretor to parse spaces and such

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 10:36 am
by GLneo
imho kernel dev is easier in c, but driver writing is simplifed by making an object for each device, oo c could be very helpful!

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 11:52 am
by AlmostHome
Can I make a request? An OSdeving language should allow the use of customized calling conventions (if possible), along with custom function prologue/epilogue procedures. If that was allowed (or every language-compiler combination supported a "naked" pragma) I wouldn't have to write an assembler file full of ISR entries and exits.

Re:a programming language "kindof" designed for OS

Posted: Sat Aug 05, 2006 12:03 pm
by earlz
along with custom function prologue/epilogue procedures
what do you mean?