Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

How are operators organized/saved in the memory in context of a programming language. Are they procedures/functions saved somewhere and compilers just manipulate things to call these procs whenever the operators are used in the program?

share|improve this question
    
Which language/compiler are you asking about? And which operator? –  Caleb Sep 11 '12 at 16:10
    
my question was in a generic/more prevalent way. but if you know a particular compiler/language that does the operator in a subroutine kind of way, I will get my answer there –  kishu27 Sep 11 '12 at 16:51
    
operators are functions like any other. The language designers just allow then to be written differently to help make the programmer's life easier. –  James Sep 11 '12 at 21:34
    
Actually most operators are not stored in memory but transformed directly in CPU instructions. –  m3th0dman Sep 12 '12 at 8:06
add comment

5 Answers

Most operators are just "syntactic sugar" for functions or procedures, so you can look at them mostly the same way:

  • They may be ordinary lists of machine instructions stored at a particular address to be called like an ordinary function. Sometimes these functions are actually callable as normal functions if you know the correct syntax, but in other situations/languages it may be that the compiler handles it completely internally without granting developers access to the functionality through anything but the compiler's own lexer and parser.
  • Sometimes the compiler can replace the operator at the place it is called, in the source, to avoid the overhead of the machine instructions needed for a function call, directly placing the machine code there (i.e. like inlining, in some cases EXACTLY like inlining).
  • Sometimes you might be working in an interpreter / runtime, where the functions are stored in an intermediate language which is "interpreted" by the runtime for the correct machine code, which could conceivably also be inlined but would still need to be interpreted.
  • In some cases, where the operands of the operator are types that are very close or identical to the machine code instruction set the operators may get translated directly into only very few statements. How many statements would mostly depend on whether the operands involved are currently stored in registers or memory (which is a whole compiler topic on its own).
  • In nearly all cases, what actually happens inside the compiler, interpreter or runtime will depend on the types of the operands. An operator between two operands of the same type will end up resolving to the operator for that type (which may or may not be entirely different from the same operator for different types). An operator between operands of different types may be implemented directly (which is rare) or additionally call conversion operators on one or more of the operands to turn them into types that can be operated on, before actually performing the operator itself.
share|improve this answer
    
You might want to add a point about operator overloading and how an int + will be one point in your list while a string + will be a different point in your list. –  Guy Coder Sep 11 '12 at 16:49
add comment

The parser converts it first into an abstract syntax tree. The kind of operator ends up as a node, and the operands end up as its child nodes. The compiler then basically walks the abstract syntax tree and does different things depending on the types of the operands.

Most of the time, the operator code is going to be simple enough to inline. However, there are also situations where it's complex enough to create a function call. For example, in C++ you can override operators on classes by creating your own functions to handle them. Usually in simple, interpreted domain specific languages it also ends up as a function call because that's the easiest way to do it when performance isn't paramount.

share|improve this answer
add comment

The whole file and code that it contains is loaded to the memory. The reserved expressions like operators and commands are treated in different ways by each different compiler.

The compiler plays most important role in this case. Compilers may act differently depending on which programming language and version are you using.

For more detailed information here is a nice article to look - Operator (programming) - Wiki

share|improve this answer
    
yea I had a look at that article before posting. it hints towards what I am asking but doesn't answer it if a particular compiler/platform implements this in the way I am thinking. just one line there A compiler can implement operators and functions with subroutine calls or with inline code. Although, thanks for your answer +1 –  kishu27 Sep 11 '12 at 15:43
    
yes, your question is a compiler specific question. it would really depend how compiler treats them. –  Yusubov Sep 11 '12 at 17:22
add comment

In compiled languages, entire expressions, including the operators that they contain, get transformed to binary code corresponding to the calculation represented by the expression. An operator gets translated into a sequence of one or more binary instructions to the CPU, directing it to perform the required operation. For example

a = b + 2;

may get converted into something like this:

LOAD  R1, @b
LOAD  R0, #2
ADD   R0, R1
STORE R0, @a

@a and @b represent addresses of variables a and b; #2 represents the value of an integer constant 2.

If an operator does not correspond to a single assembly instruction, a compiler implementation may choose an inlined or a non-inlined function to implement it. For example, in 8-bit CPUs that lacked multiplication instructions (and were short on memory, restricting the opportunities for inlining, e.g. 6502), multiplication and division have been commonly implemented as subroutines.

In interpreted languages operators are stored as part of the code, in the form of a data structure if preprocessing is used, or in their textual form in the rare case when preprocessing is not used.

share|improve this answer
    
yes. let's take a more complex example, like of a ternary operator. is that too converted to a assembly code snippet and the codes replaced, or they call a subroutine to do that? if they do then I get my answer :) thanks –  kishu27 Sep 11 '12 at 15:46
1  
@kishu27 Conditional operators would almost certainly be inlined. To inline or to not inline is dependent a lot on the memory constraints, and on the length of the code to inline. With ternary operator, the amount of operator-specific code is very small; for 16-bit division on 6502, the amount of code is relatively large. –  dasblinkenlight Sep 11 '12 at 15:50
    
got it.. thanks –  kishu27 Sep 11 '12 at 16:06
add comment

In a language like C or C++, simple native types like int, char, float, double in full features architectures with floating point hardware may typically become sequential machine instructions like the other statements in the code block.

In an architecture that does not have native floating point hardware (or even 32 bit ints), simple expressions may generate subroutine calls to run time libraries that supplement the device's basic capabilities. I did an experiment with some eight bit micros vs. an X86 compiler a few years back and found big differences in the size of code generated. x86 was 1/3 as big, and unlike the 8 bit architectures that had many run time calls for simple things like adding 32-bit and 64-bit integers, it was all generated by instructions sequences.

Often constants are inserted with the instructions as immediate operands. In some architectures, the immediate data will be pushed on the stack or written relative to a stack pointer or a base pointer. In an executable, the inserted instructions and the immediate operands will typically be in the code segment. For complex data like arrays, strings, and user defined variables, if they are constant, they too may be emitted to a code segment, but what is more common is that during run time they are copied to a data segment that is initialized by the loader or the run time system.

Compilers have many methods of optimizing expressions, so if you single step and see what seems like very few instructions relative to an expression, it may mean that the constants in the expression were simplified. There is also a technique in which common subexpressions are evaluated once before the first usage, then when needed again, the partial result is just used from a temporary variable or a register.

This group, or Stack Exchange Code Review would perhaps be a great venue to explain what is happening if you have additional questions about a block of source code and the machine instructions that it generates, particularly as relates to compiler optimizations.

When operators or expressions are used with complex data types like arrays, strings, structs, or classes, there may be some cases where things are performed sequentially, but generally, more complex types required functions calls. A worthwhile exception to this is when a compiler provides OpenMP extensions that perform fine grain parallelism on small blocks of code that is dispatched across multiple cores. But as interesting as it might be, the details are probably out of scope for this question.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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