Programming language for self-modifying code?

19,460

Solution 1

Malbolge would be a good place to start. Every instruction is self-modifying, and it's a lot of fun(*) to play with.

(*) Disclaimer: May not actually be fun.

Solution 2

I highly recommend Lisp. Lisp data can be read and exec'd as code. Lisp code can be written out as data.

It is considered one of the canonical self-modifiable languages.

Example list(data):

'(+ 1 2 3) 

or, calling the data as code

(eval '(+ 1 2 3)) 

runs the + function.

You can also go in and edit the members of the lists on the fly.

edit:

I wrote a program to dynamically generate a program and evaluate it on the fly, then report to me how it did compared to a baseline(div by 0 was the usual report, ha).

Solution 3

Every answer so far is about reflection/runtime compilation, but in the comments you mentioned you're interested in actual self-modifying code - code that modifies itself in-memory.

There is no way to do this in C#, Java, or even (portably) in C - that is, you cannot modify the loaded in-memory binary using these languages.

In general, the only way to do this is with assembly, and it's highly processor-dependent. In fact, it's highly operating-system dependent as well: to protect against polymorphic viruses, most modern operating systems (including Windows XP+, Linux, and BSD) enforce W^X, meaning you have to go through some trouble to write polymorphic executables in those operating systems, for the ones that allow it at all.

It may be possible in some interpreted languages to have the program modify its own source-code while it's running. Perl, Python (see here), and every implementation of Javascript I know of do not allow this, though.

Solution 4

May I suggest Python, a nice very high-level dynamic language which has rich introspection included (and by e.g. usage of compile, eval or exec permits a form of self-modifying code). A very simple example based upon your question:

def label1(a,b,c):
    c=a+b
    return c

a,b,c=10,20,0    
print label1(a,b,c) # prints 30

newdef= \
"""
def label1(a,b,c):
    c=a*b
    return c
"""
exec(newdef,globals(),globals())

print label1(a,b,c) # prints 200

Note that in the code sample above c is only altered in the function scope.

Solution 5

Personally, I find it quite strange that you find assembly easier to handle than C#. I find it even stranger that you think that assembly isn't as powerful: you can't get any more powerful than raw machine language. Anyway, to each his/her own.

C# has great reflection services, but if you have an aversion to that.. If you're really comfortable with C or C++, you could always write a program that writes C/C++ and issues it to a compiler. This would only be viable if your solution doesn't require a quick self-rewriting turn-around time (on the order of tens of seconds or more).

Javascript and Python both support reflection as well. If you're thinking of learning a new, fun programming language that's powerful but not massively technically demanding, I'd suggest Python.

Share:
19,460
Betamoo
Author by

Betamoo

I love Algorithms, Programming, AI and machine learning In my spare time, I like playing real time strategy games, chess, watching movies.

Updated on June 18, 2022

Comments

  • Betamoo
    Betamoo about 2 years
    • I am recently thinking about writing self-modifying programs, I think it may be powerful and fun. So I am currently looking for a language that allows modifying a program's own code easily.
    • I read about C# (as a way around) and the ability to compile and execute code in runtime, but that is too painful.
    • I am also thinking about assembly. It is easier there to change running code but it is not very powerful (very raw).

    Can you suggest a powerful language or feature that supports modifying code in runtime?

    Example

    This is what I mean by modifying code in runtime:

      Start:
      a=10,b=20,c=0;
      label1: c=a+b;
      ....
      label1= c=a*b;
      goto label1;
    

    and may be building a list of instructions:

      code1.add(c=a+b);
      code1.add(c=c*(c-1));
      code1. execute();
    
  • Ken
    Ken about 14 years
    I see nothing strange here. Assembly is about as simple as you can get, while C# is huge and complex -- and I say this as someone who's been writing C# for a couple years! As for power, I think in programming it usually means something like "expressive ability" or "abstraction-building ability" (obviously the literal meaning of "power" is useless here!), and assembly language is exceptionally poor at that.
  • Warty
    Warty about 14 years
    The programming language where it took a few years for a person to write hello world... Oh wait, the hello world program was generated by a computer. Have fun lulz.
  • Betamoo
    Betamoo about 14 years
    @Reinderien: I mean in terms of changing existing code, assembly is easier than C#... Also in terms of readability and writability, assembly is less powerful...
  • Paul Nathan
    Paul Nathan about 14 years
    I think asm is much easier than C# or Java. It is very straightforward and uncomplicated.
  • Reinderien
    Reinderien about 14 years
    I agree that C# is huge and complex. That being said, compare the number of lines of code needed to open a file in C# versus assembly. For modern programming tasks, assembly is simply not an option. For sub-sub-subtasks that require heavy optimization, perhaps. As for power, I essentially meant the number of things that you're allowed to directly manipulate. This isn't always helpful, however; it's like being given a pile of carbon and being told to build a dog. Assembly can get very tricky to modify on-the-fly - you essentially have to act as a compiler. You need to do all the addressing.
  • L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳
    L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ about 14 years
    @Ken @Reinderien @Paul: Assembly is harder than C#. Period. As hard as effective C#? Maybe not. Effective assembly is certainly more hard than effective C#, though. You have to master your architecture, take into account increased cyclomatic complexity, caching, pipelining, interrupts, and possibly completely redesign your entire algorithm when the requirements change in order to make it as efficient as possible (to save registers, align some data, fix some ILP, etc), and If at hardware level: control the CPU (MSRs, TLB, Page Tables, I/O, control registers, etc).
  • Paul Nathan
    Paul Nathan about 14 years
    @Longpoke: How is that different from mastering the C# semantics and the gigantic library packages and their different versions?
  • Reinderien
    Reinderien about 14 years
    @Paul: Show me an assembly IDE with code completion and embedded documentation. The difference is that to make an effective C# application, you need to learn a very small subset of very well-documented libraries with oodles of examples. To make an effective assembly application, you need to drink from the firehose and understand the architecture from the bottom up, arcane abbreviations, electrical engineering issues and all.
  • Reinderien
    Reinderien about 14 years
    You have a good point. Depending on how you look at it, C# / other .NET flavours / Java use bytecode instead of proper assembly, but the issue stands.
  • Reinderien
    Reinderien about 14 years
    I should add that, if you were to do it the slightly roundabout and hacky way of iteratively issuing source code to a compiler, running the binary, and then having that binary in turn modify the source code to re-issue it to the compiler... That would (effectively) have read-write access to the program's code, even though the read-write turnaround time is bad.
  • ChristopheD
    ChristopheD about 14 years
    Although I agree with the gist of the sentiment, I would suggest the possibility (in Python for example: geofft.mit.edu/blog/sipb/73) of directly alterering generated/running bytecode (which conceptually then doesn't differ much from doing the same with assembly).
  • Paul Nathan
    Paul Nathan about 14 years
    @Reinderien: Honestly, I've used C# and assembly and I don't see a significant difference. I can get the job done in either.
  • Ken
    Ken about 14 years
    Longpoke: For a given task, an assembly program to do the same thing will be more complex than a C# program to do that task. But learning to write assembly is far simpler than learning to write C#. (Saying "Period" in bold text doesn't make your argument any stronger.) Most of the things you mention (caching, pipelining, etc.) can be ignored in assembly, or taken into account in C#, too.
  • L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳
    L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ about 14 years
    @Ken: Why do you need to use assembly if you aren't using the full bleeding edge of it (or writing a compiler)? If you aren't pushing it to the limits, the compiler is going to murder you.