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

Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57

1 Answers1

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.

DFriend
  • 8,869
  • 1
  • 13
  • 26
  • 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. – Olle Härstedt Sep 07 '21 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 Sep 07 '21 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 Sep 07 '21 at 22:00
  • OK, but can you actually add keywords and new types of opcodes via extensions...? – Olle Härstedt Sep 08 '21 at 08: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 Sep 08 '21 at 14:56
  • So, yes you are effectively creating new opcodes by writing new extensions. – DFriend Sep 08 '21 at 14:56
  • Comment from Joe Watkins, core dev: "new opcodes [using a php extension] are possible but will stop jit from working, new keywords aren't possible in a practical way either - you would have to implement a preprocessing kind of parser that invokes zend parser ..." – Olle Härstedt Sep 08 '21 at 18:10