1

Searching for it I found this blog post: https://www.npopov.com/2017/04/14/PHP-7-Virtual-machine.html

Does it cover "everything" needed to add a new opcode, or all the places I'd need to touch in the engine? Or is it better to just start grepping in the code-base? Maybe there's a commit that can be used as a prototype or example?

Edit, last opcode added was this: https://github.com/php/php-src/pull/7019/files#diff-773bdb31563a0f907c75068675f6056b25f003e61f46928a31d9837ae107460d

1 Answer 1

1

PHP source code is written in the c programming language. That is what you are looking at in your link to the "last opcode added". That's not opcode it is source code. The output of the PHP interpreter (Zend Virtual Machine) reads the contents of php files and turns it into opcode. Opcode is not written directly by programmers.

If you want to write a custom extension for PHP you will need to write it in c and structure it so it can be loaded as an external mod. To say, "It's not trivial" would be a massive understatement.

There is a hybrid system that allows you to write PHP-like code that can be compiled into a standalone PHP extension. Check out the Zephir language. Stack overflow has lots of questions tagged with 'zephir'. I have read about it but not used it.

7
  • I was thinking it would be easier to fork php-src than to write a new extension. It's just for learning purpose. Or like a first step in learning to contribute. Commented Sep 7, 2021 at 14:16
  • If you want to contribute you will be writing c. The blog page you linked is probably less valueable than his two previous posts about Internal Value Representation in PHP 7.
    – DFriend
    Commented Sep 7, 2021 at 21:55
  • Writing an extension is actually less daunting than you think. Check out the Phalcon PHP framework. It is based on the idea that custom extensions can be fast and fun. As you dig around you will note they are behind the Aephir language I mentioned in the answer.
    – DFriend
    Commented Sep 7, 2021 at 22:00
  • OK, but can you actually add keywords and new types of opcodes via extensions...? Commented Sep 8, 2021 at 8:16
  • The php interpreter reads text files containing php code, analyzes it and transforms into opcodes. The opcode is used by the VM to map to the machine code which was produced by compiling the c source files. During the c source compile process the VM figures out what the opcode is going to be that associates php code with the c version of that function.
    – DFriend
    Commented Sep 8, 2021 at 14:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.