How? You’d need to compile it down to machine code somehow, for the processor to have any clue how to run it. And you’d need some custom library with custom compile instructions, to be able to control memory allocations, memory addresses etc…
I did a quick search and found two operating systems written in JS, both of which cop out when it comes to the kernel. Did you maybe mix it up with those?
I mean, I’m a bit out of my water there, both in terms of the featureset of PHP and what’s actually needed for a kernel, but I’m still gonna go with no.
For one, PHP uses reference counting + garbage collection for memory management. That’s normally done by the language runtime, which you won’t have when running baremetal.
Maybe you could implement a kernel, which does as few allocations as possible (generally a good idea for a kernel, but no idea, if it’s possible with PHP), and then basically just let it memory leak until everything crashes.
Then again, the kernel is responsible for making processes crash when they have a memory leak. Presumably, our PHP kernel would just start overwriting data from running processes and eventually overwrite itself in memory(?). Either way, it would be horrendous.
Maybe you could also try to implement some basic reference counting into your own PHP code, so that your own code keeps track of how often you’ve used an object in your own code. Certainly doesn’t sound like fun, though.
Well, and secondly, I imagine, you’d also still need an extension of the language, to be able to address actual memory locations and do various operations with them.
I know from Rust, that they’ve got specific functions in the stdlib for that, see for example: https://doc.rust-lang.org/stable/std/ptr/index.html#functions
Presumably, PHP does not have such functions, because its users aren’t normally concerned with that.
Right – I’m not saying you could build a compiler then just go to town. You would still have to build all the tools, using PHP, to interact with hardware, the way other languages do. A horrible idea, lol, but interesting, sort of. Since at its core as long as you can execute logic and read/write to memory, you could do it, I think
But that is what I mean with it needing an extension of the language.
So, I’m not saying you could just build a library that calls existing PHP functions to make it all work. Rather I’m saying there’s certain machine code instructions, which just cannot be expressed in PHP. And we need those machine code instructions for actually managing memory. So, I am talking about reading/writing to memory not being possible, unless we resort to horrible hacks.
Since we are building our own compiler anyways, we could add our own function-stubs and tell our compiler to translate them to those missing machine code instructions. But then that is a superset of PHP. It wouldn’t be possible in PHP itself.
Again, I’m not entirely sure about the above, but my web search skills couldn’t uncover any way to actually just read from a memory address in PHP.
Good thing that this isn’t actually possible…
Don’t tempt fate. There are already two kernels written in javascript
Why would God allow this to happen?
To punish us for the sin that is inventing JavaScript.
How? You’d need to compile it down to machine code somehow, for the processor to have any clue how to run it. And you’d need some custom library with custom compile instructions, to be able to control memory allocations, memory addresses etc…
I did a quick search and found two operating systems written in JS, both of which cop out when it comes to the kernel. Did you maybe mix it up with those?
There’s almost always at least a little ASM sprinkled into any kernel, so that’s not a big deal.
OTOH, there is the factor of “you know how Chrome takes up 2GB per tab? What if that was a whole OS?”
Maybe I did, I haven’t looked any of them carefully
Theoretically if you found a way to compile PHP, you probably could though, right?
I mean, I’m a bit out of my water there, both in terms of the featureset of PHP and what’s actually needed for a kernel, but I’m still gonna go with no.
For one, PHP uses reference counting + garbage collection for memory management. That’s normally done by the language runtime, which you won’t have when running baremetal.
Maybe you could implement a kernel, which does as few allocations as possible (generally a good idea for a kernel, but no idea, if it’s possible with PHP), and then basically just let it memory leak until everything crashes.
Then again, the kernel is responsible for making processes crash when they have a memory leak. Presumably, our PHP kernel would just start overwriting data from running processes and eventually overwrite itself in memory(?). Either way, it would be horrendous.
Maybe you could also try to implement some basic reference counting into your own PHP code, so that your own code keeps track of how often you’ve used an object in your own code. Certainly doesn’t sound like fun, though.
Well, and secondly, I imagine, you’d also still need an extension of the language, to be able to address actual memory locations and do various operations with them.
I know from Rust, that they’ve got specific functions in the stdlib for that, see for example: https://doc.rust-lang.org/stable/std/ptr/index.html#functions
Presumably, PHP does not have such functions, because its users aren’t normally concerned with that.
Right – I’m not saying you could build a compiler then just go to town. You would still have to build all the tools, using PHP, to interact with hardware, the way other languages do. A horrible idea, lol, but interesting, sort of. Since at its core as long as you can execute logic and read/write to memory, you could do it, I think
But that is what I mean with it needing an extension of the language.
So, I’m not saying you could just build a library that calls existing PHP functions to make it all work. Rather I’m saying there’s certain machine code instructions, which just cannot be expressed in PHP. And we need those machine code instructions for actually managing memory. So, I am talking about reading/writing to memory not being possible, unless we resort to horrible hacks.
Since we are building our own compiler anyways, we could add our own function-stubs and tell our compiler to translate them to those missing machine code instructions. But then that is a superset of PHP. It wouldn’t be possible in PHP itself.
Again, I’m not entirely sure about the above, but my web search skills couldn’t uncover any way to actually just read from a memory address in PHP.
I mean, I think we’re saying the same thing, you just have better vocabulary than I :)
I did use a crypto mining OS that was Linux but with lots of scripts that were written in… PHP! I never thought I’d see it.