Runtime during OC running

Source: Internet
Author: User

OC is a fully dynamic language, and everything in OC is implemented based on runtime!

Understand runtime from the following three aspects!

1. Traditional process-oriented language development, such as C language. Implementing the C language compiler is simple, as long as you implement an lalr according to the syntax rulesSyntax analyzerAs you can, compiler optimization is a very difficult topic, which is not discussed here and ignored. Here we have implemented one of the most basic and original goals of the compiler.The function name in the Code is converted into a relative memory address, and the statements that call this function are converted into a JMP jump command.When the program starts running, the call statement can jump to the corresponding function address correctly. This is very good and straightforward,... It's too rigid.Everything is per-determined

2. We want to be flexible, so we need to develop object-oriented languages, such as C ++. C ++ adds the class part based on C. But what exactly does this mean? How should we consider the compiler that is writing it? In fact, it is to let the compiler turn around more,Add a class processing mechanism to the strict C compiler to limit a function to the class environment in which it is located. Each time you request a function call, first find its object and its type, return values, parameters, and so on. After confirming these, the JMP will jump to the required function.In this way, many programs increase flexibility. Similarly, a function call will return completely different results based on the Request Parameters and the class environment. After adding the class mechanism, it simulates the abstract pattern of the real world. Different objects have different attributes and methods. In the same way, different classes have different behaviors! Here we can see what further thinking has been made as a compiler developer. But... We still call C ++ static language.

3. Hope to be more flexible! So we completely abstract the implementation part of the above class into a complete runtime detection environment.This time, write the compiler and even keep the sytax name, name error detection, runtime environment registration of all global classes, functions, variables, and so on, we can add the necessary functions for this layer infinitely. When calling a function, the system first detects the possible parameters in the runtime environment and then performs the JMP jump. This is the runtime.The compiler is more curved than above. However, this layer greatly increases program flexibility. For example, when calling a function in the first two languages, it is very likely that a JMP has an Invalid Address that causes the program crash. However, in this layer, runtime filters out these possibilities. That's why dynamic langauge is stronger. Because the compiler and runtime environment developers have already helped you solve these problems.

Now let's look at objective-C. Can you understand this statement?
Id OBJ = self;
If ([OBJ respondstoselector: @ selector (function1 :)){
}
If ([OBJ iskindofclass: [nsarray class]) {
}
If ([OBJ conformstoprotocol: @ protocol (myprotocol)]) {
}
If ([[OBJ class] issubclassofclass: [nsarray class]) {
}
[OBJ somenonexistfunction];

A seemingly simple statement, but in order to enable the language to implement this capability, the language developer has to make a lot of efforts to implement the runtime environment. The runtime environment is processed here.Weak type,Function Existence Check. Runtime checks whether the corresponding functions and types exist in the registration list, determines the correct function address, and then saves the register status, presses the stack, and CALLS functions.

Id knife = [knife grateknife];
Nsarray * monsterlist = [nsarray array];
[Monsterlist makeobjectsperformselector: @ selector (killmonster :) withobject: knife];

In the C and C ++ years, it was very troublesome to complete this function, but the dynamic language was very simple.

About execution efficiency. "Static language execution efficiency is higher than Dynamic Language execution efficiency. Some CPU computing losses are in the runtime process. The machine commands generated by static languages are more concise. Because of this, developers have made a lot of effort to keep the runtime small. SoObjecitve-C is a super set of C + a small runtime environment.However, in other words, from an algorithm perspective, this complexity is not different, and the big O notation results will not be different. (It's not log (n) vs n ^ 2)

Easy to understand: "RunTime is everything between your each function call ."
Runtime is like the soul of objective-C. Many things appear on this basis. So it refers to what you need to understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.