In general, replace CREATE3/CREATE4 with CREATEF, where the same executing contract is used for initcode, but a different code section other than 0 is used.
Using the factory pattern will require two levels of nesting if done in a EOF container. First there is the executing container, then there is the container that holds the exec code, then there will be the deployed contract itself.
+-----------------------------+
| Factory Contract |
| +-------------------------+ |
| | Child contract initcode | |
| | +----------------+ | |
| | | Child Contract | | |
| | +----------------+ | |
| +-------------------------+ |
+-----------------------------+
There will be three separate EOF headers to parse. Let's not forget, this also has to be deployed, so the master deployer will have at least 3 layers of nested contracts.
+---------------------------------+
| Factory Contract Deployer |
| +-----------------------------+ |
| | Factory Contract | |
| | +-------------------------+ | |
| | | Child contract initcode | | |
| | | +----------------+ | | |
| | | | Child Contract | | | |
| | | +----------------+ | | |
| | +-------------------------+ | |
| +-----------------------------+ |
+---------------------------------+
Instead of going to a separate EOF container that contains initcode, the current container will contain the initcode at a different code setcion than the main entry point. The CREATEF call, instead of pointing to a subcointainer, will point to a code section to perform the pre-deployment initialization.
+--------------------------------------+
| Factory Contract |
| sections 0-n Normal factory code |
| section m Child contract initcode |
| +----------------+ |
| | Child Contract | |
| +----------------+ |
+--------------------------------------+
This also allows for multiple contract to re-use subroutines from other initcode and the main factory code. A single CREATEF operation replaces CREATE3/CREATE4 and two return operations replace RETURNCONTRAcT, one referencing a EOF subcontainer index and one referening a hash of a container in the transaction body of a new transaction type.
Instead of two create variants there are two contract return variants
RETURN_CONTRACT_INDEX instruction
RETURN_CONTRACT_HASH instruction
The two return operations could be combined if we added a magic rule that any return value with 30 or more leading zeros refers to a container index. Or if we also refered to the subcontainers by hash.